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