1 //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the MacroArgs interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_MACROARGS_H 15 #define LLVM_CLANG_MACROARGS_H 16 17 #include <vector> 18 19 namespace clang { 20 class MacroInfo; 21 class Preprocessor; 22 class Token; 23 class SourceLocation; 24 25 /// MacroArgs - An instance of this class captures information about 26 /// the formal arguments specified to a function-like macro invocation. 27 class MacroArgs { 28 /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the 29 /// arguments. All of the actual argument tokens are allocated immediately 30 /// after the MacroArgs object in memory. This is all of the arguments 31 /// concatenated together, with 'EOF' markers at the end of each argument. 32 unsigned NumUnexpArgTokens; 33 34 /// VarargsElided - True if this is a C99 style varargs macro invocation and 35 /// there was no argument specified for the "..." argument. If the argument 36 /// was specified (even empty) or this isn't a C99 style varargs function, or 37 /// if in strict mode and the C99 varargs macro had only a ... argument, this 38 /// is false. 39 bool VarargsElided; 40 41 /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty 42 /// if not yet computed. This includes the EOF marker at the end of the 43 /// stream. 44 std::vector<std::vector<Token> > PreExpArgTokens; 45 46 /// StringifiedArgs - This contains arguments in 'stringified' form. If the 47 /// stringified form of an argument has not yet been computed, this is empty. 48 std::vector<Token> StringifiedArgs; 49 50 /// ArgCache - This is a linked list of MacroArgs objects that the 51 /// Preprocessor owns which we use to avoid thrashing malloc/free. 52 MacroArgs *ArgCache; 53 MacroArgs(unsigned NumToks,bool varargsElided)54 MacroArgs(unsigned NumToks, bool varargsElided) 55 : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided), ArgCache(0) {} ~MacroArgs()56 ~MacroArgs() {} 57 public: 58 /// MacroArgs ctor function - Create a new MacroArgs object with the specified 59 /// macro and argument info. 60 static MacroArgs *create(const MacroInfo *MI, 61 const Token *UnexpArgTokens, 62 unsigned NumArgTokens, bool VarargsElided, 63 Preprocessor &PP); 64 65 /// destroy - Destroy and deallocate the memory for this object. 66 /// 67 void destroy(Preprocessor &PP); 68 69 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected 70 /// by pre-expansion, return false. Otherwise, conservatively return true. 71 bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const; 72 73 /// getUnexpArgument - Return a pointer to the first token of the unexpanded 74 /// token list for the specified formal. 75 /// 76 const Token *getUnexpArgument(unsigned Arg) const; 77 78 /// getArgLength - Given a pointer to an expanded or unexpanded argument, 79 /// return the number of tokens, not counting the EOF, that make up the 80 /// argument. 81 static unsigned getArgLength(const Token *ArgPtr); 82 83 /// getPreExpArgument - Return the pre-expanded form of the specified 84 /// argument. 85 const std::vector<Token> & 86 getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP); 87 88 /// getStringifiedArgument - Compute, cache, and return the specified argument 89 /// that has been 'stringified' as required by the # operator. 90 const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP, 91 SourceLocation hashInstLoc); 92 93 /// getNumArguments - Return the number of arguments passed into this macro 94 /// invocation. getNumArguments()95 unsigned getNumArguments() const { return NumUnexpArgTokens; } 96 97 98 /// isVarargsElidedUse - Return true if this is a C99 style varargs macro 99 /// invocation and there was no argument specified for the "..." argument. If 100 /// the argument was specified (even empty) or this isn't a C99 style varargs 101 /// function, or if in strict mode and the C99 varargs macro had only a ... 102 /// argument, this returns false. isVarargsElidedUse()103 bool isVarargsElidedUse() const { return VarargsElided; } 104 105 /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of 106 /// tokens into the literal string token that should be produced by the C # 107 /// preprocessor operator. If Charify is true, then it should be turned into 108 /// a character literal for the Microsoft charize (#@) extension. 109 /// 110 static Token StringifyArgument(const Token *ArgToks, 111 Preprocessor &PP, bool Charify, 112 SourceLocation hashInstLoc); 113 114 115 /// deallocate - This should only be called by the Preprocessor when managing 116 /// its freelist. 117 MacroArgs *deallocate(); 118 }; 119 120 } // end namespace clang 121 122 #endif 123