MacroInfo.h revision 25c9648909193d380a4e135d2e3d25394ba12922
1//===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the MacroInfo interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_MACROINFO_H 15#define LLVM_CLANG_MACROINFO_H 16 17#include "clang/Lex/LexerToken.h" 18#include "llvm/ADT/SmallVector.h" 19#include <vector> 20#include <cassert> 21 22namespace clang { 23 class Preprocessor; 24 25/// MacroInfo - Each identifier that is #define'd has an instance of this class 26/// associated with it, used to implement macro expansion. 27class MacroInfo { 28 //===--------------------------------------------------------------------===// 29 // State set when the macro is defined. 30 31 /// Location - This is the place the macro is defined. 32 SourceLocation Location; 33 34 /// Arguments - The list of arguments for a function-like macro. This can be 35 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this 36 /// includes the __VA_ARGS__ identifier on the list. 37 IdentifierInfo **ArgumentList; 38 unsigned NumArguments; 39 40 /// ReplacementTokens - This is the list of tokens that the macro is defined 41 /// to. 42 llvm::SmallVector<LexerToken, 8> ReplacementTokens; 43 44 /// IsFunctionLike - True if this macro is a function-like macro, false if it 45 /// is an object-like macro. 46 bool IsFunctionLike : 1; 47 48 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or 49 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the 50 /// contents of "..." in an invocation. 51 bool IsC99Varargs : 1; 52 53 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The 54 /// "a" identifier in th replacement list will be replaced with all arguments 55 /// of the macro starting with the specified one. 56 bool IsGNUVarargs : 1; 57 58 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if 59 /// it has not yet been redefined or undefined. 60 bool IsBuiltinMacro : 1; 61 62 /// IsTargetSpecific - True if this is a target-specific macro defined with 63 /// #define_target. 64 bool IsTargetSpecific : 1; 65private: 66 //===--------------------------------------------------------------------===// 67 // State that changes as the macro is used. 68 69 /// IsDisabled - True if we have started an expansion of this macro already. 70 /// This disbles recursive expansion, which would be quite bad for things like 71 /// #define A A. 72 bool IsDisabled : 1; 73 74 /// IsUsed - True if this macro is either defined in the main file and has 75 /// been used, or if it is not defined in the main file. This is used to 76 /// emit -Wunused-macros diagnostics. 77 bool IsUsed : 1; 78public: 79 MacroInfo(SourceLocation DefLoc); 80 81 ~MacroInfo() { 82 delete[] ArgumentList; 83 } 84 85 /// getDefinitionLoc - Return the location that the macro was defined at. 86 /// 87 SourceLocation getDefinitionLoc() const { return Location; } 88 89 /// isIdenticalTo - Return true if the specified macro definition is equal to 90 /// this macro in spelling, arguments, and whitespace. This is used to emit 91 /// duplicate definition warnings. This implements the rules in C99 6.10.3. 92 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const; 93 94 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag. 95 /// 96 void setIsBuiltinMacro(bool Val = true) { 97 IsBuiltinMacro = Val; 98 } 99 100 /// setIsTargetSpecific - Set or clear the IsTargetSpecific flag. 101 /// 102 void setIsTargetSpecific(bool Val = true) { 103 IsTargetSpecific = Val; 104 } 105 bool isTargetSpecific() const { return IsTargetSpecific; } 106 107 /// setIsUsed - Set the value of the IsUsed flag. 108 /// 109 void setIsUsed(bool Val) { 110 IsUsed = Val; 111 } 112 113 /// setArgumentList - Set the specified list of identifiers as the argument 114 /// list for this macro. 115 template<typename ItTy> 116 void setArgumentList(ItTy ArgBegin, ItTy ArgEnd) { 117 assert(ArgumentList == 0 && "Argument list already set!"); 118 unsigned NumArgs = ArgEnd-ArgBegin; 119 if (NumArgs == 0) return; 120 NumArguments = NumArgs; 121 ArgumentList = new IdentifierInfo*[NumArgs]; 122 for (unsigned i = 0; ArgBegin != ArgEnd; ++i, ++ArgBegin) 123 ArgumentList[i] = *ArgBegin; 124 } 125 126 /// Arguments - The list of arguments for a function-like macro. This can be 127 /// empty, for, e.g. "#define X()". 128 typedef IdentifierInfo* const *arg_iterator; 129 arg_iterator arg_begin() const { return ArgumentList; } 130 arg_iterator arg_end() const { return ArgumentList+NumArguments; } 131 unsigned getNumArgs() const { return NumArguments; } 132 133 /// getArgumentNum - Return the argument number of the specified identifier, 134 /// or -1 if the identifier is not a formal argument identifier. 135 int getArgumentNum(IdentifierInfo *Arg) const { 136 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) 137 if (*I == Arg) return I-arg_begin(); 138 return -1; 139 } 140 141 /// Function/Object-likeness. Keep track of whether this macro has formal 142 /// parameters. 143 void setIsFunctionLike() { IsFunctionLike = true; } 144 bool isFunctionLike() const { return IsFunctionLike; } 145 bool isObjectLike() const { return !IsFunctionLike; } 146 147 /// Varargs querying methods. This can only be set for function-like macros. 148 void setIsC99Varargs() { IsC99Varargs = true; } 149 void setIsGNUVarargs() { IsGNUVarargs = true; } 150 bool isC99Varargs() const { return IsC99Varargs; } 151 bool isGNUVarargs() const { return IsGNUVarargs; } 152 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } 153 154 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as 155 /// __LINE__, which requires processing before expansion. 156 bool isBuiltinMacro() const { return IsBuiltinMacro; } 157 158 /// isUsed - Return false if this macro is defined in the main file and has 159 /// not yet been used. 160 bool isUsed() const { return IsUsed; } 161 162 /// getNumTokens - Return the number of tokens that this macro expands to. 163 /// 164 unsigned getNumTokens() const { 165 return ReplacementTokens.size(); 166 } 167 168 const LexerToken &getReplacementToken(unsigned Tok) const { 169 assert(Tok < ReplacementTokens.size() && "Invalid token #"); 170 return ReplacementTokens[Tok]; 171 } 172 173 typedef llvm::SmallVector<LexerToken, 8>::const_iterator tokens_iterator; 174 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } 175 tokens_iterator tokens_end() const { return ReplacementTokens.end(); } 176 177 /// AddTokenToBody - Add the specified token to the replacement text for the 178 /// macro. 179 void AddTokenToBody(const LexerToken &Tok) { 180 ReplacementTokens.push_back(Tok); 181 } 182 183 /// isEnabled - Return true if this macro is enabled: in other words, that we 184 /// are not currently in an expansion of this macro. 185 bool isEnabled() const { return !IsDisabled; } 186 187 void EnableMacro() { 188 assert(IsDisabled && "Cannot enable an already-enabled macro!"); 189 IsDisabled = false; 190 } 191 192 void DisableMacro() { 193 assert(!IsDisabled && "Cannot disable an already-disabled macro!"); 194 IsDisabled = true; 195 } 196}; 197 198} // end namespace clang 199 200#endif 201