MacroInfo.h revision f73903a1ded46748e1dfda151f5d037b7b3d31f9
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 <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<Token, 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 the 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
62private:
63  //===--------------------------------------------------------------------===//
64  // State that changes as the macro is used.
65
66  /// IsDisabled - True if we have started an expansion of this macro already.
67  /// This disbles recursive expansion, which would be quite bad for things like
68  /// #define A A.
69  bool IsDisabled : 1;
70
71  /// IsUsed - True if this macro is either defined in the main file and has
72  /// been used, or if it is not defined in the main file.  This is used to
73  /// emit -Wunused-macros diagnostics.
74  bool IsUsed : 1;
75public:
76  MacroInfo(SourceLocation DefLoc);
77
78  ~MacroInfo() {
79    delete[] ArgumentList;
80  }
81
82  /// getDefinitionLoc - Return the location that the macro was defined at.
83  ///
84  SourceLocation getDefinitionLoc() const { return Location; }
85
86  /// isIdenticalTo - Return true if the specified macro definition is equal to
87  /// this macro in spelling, arguments, and whitespace.  This is used to emit
88  /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
89  bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
90
91  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
92  ///
93  void setIsBuiltinMacro(bool Val = true) {
94    IsBuiltinMacro = Val;
95  }
96
97  /// setIsUsed - Set the value of the IsUsed flag.
98  ///
99  void setIsUsed(bool Val) {
100    IsUsed = Val;
101  }
102
103  /// setArgumentList - Set the specified list of identifiers as the argument
104  /// list for this macro.
105  template<typename ItTy>
106  void setArgumentList(ItTy ArgBegin, ItTy ArgEnd) {
107    assert(ArgumentList == 0 && "Argument list already set!");
108    unsigned NumArgs = ArgEnd-ArgBegin;
109    if (NumArgs == 0) return;
110    NumArguments = NumArgs;
111    ArgumentList = new IdentifierInfo*[NumArgs];
112    for (unsigned i = 0; ArgBegin != ArgEnd; ++i, ++ArgBegin)
113      ArgumentList[i] = *ArgBegin;
114  }
115
116  /// Arguments - The list of arguments for a function-like macro.  This can be
117  /// empty, for, e.g. "#define X()".
118  typedef IdentifierInfo* const *arg_iterator;
119  bool arg_empty() const { return NumArguments == 0; }
120  arg_iterator arg_begin() const { return ArgumentList; }
121  arg_iterator arg_end() const { return ArgumentList+NumArguments; }
122  unsigned getNumArgs() const { return NumArguments; }
123
124  /// getArgumentNum - Return the argument number of the specified identifier,
125  /// or -1 if the identifier is not a formal argument identifier.
126  int getArgumentNum(IdentifierInfo *Arg) const {
127    for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
128      if (*I == Arg) return I-arg_begin();
129    return -1;
130  }
131
132  /// Function/Object-likeness.  Keep track of whether this macro has formal
133  /// parameters.
134  void setIsFunctionLike() { IsFunctionLike = true; }
135  bool isFunctionLike() const { return IsFunctionLike; }
136  bool isObjectLike() const { return !IsFunctionLike; }
137
138  /// Varargs querying methods.  This can only be set for function-like macros.
139  void setIsC99Varargs() { IsC99Varargs = true; }
140  void setIsGNUVarargs() { IsGNUVarargs = true; }
141  bool isC99Varargs() const { return IsC99Varargs; }
142  bool isGNUVarargs() const { return IsGNUVarargs; }
143  bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
144
145  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
146  /// __LINE__, which requires processing before expansion.
147  bool isBuiltinMacro() const { return IsBuiltinMacro; }
148
149  /// isUsed - Return false if this macro is defined in the main file and has
150  /// not yet been used.
151  bool isUsed() const { return IsUsed; }
152
153  /// getNumTokens - Return the number of tokens that this macro expands to.
154  ///
155  unsigned getNumTokens() const {
156    return ReplacementTokens.size();
157  }
158
159  const Token &getReplacementToken(unsigned Tok) const {
160    assert(Tok < ReplacementTokens.size() && "Invalid token #");
161    return ReplacementTokens[Tok];
162  }
163
164  typedef llvm::SmallVector<Token, 8>::const_iterator tokens_iterator;
165  tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
166  tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
167  bool tokens_empty() const { return ReplacementTokens.empty(); }
168
169  /// AddTokenToBody - Add the specified token to the replacement text for the
170  /// macro.
171  void AddTokenToBody(const Token &Tok) {
172    ReplacementTokens.push_back(Tok);
173  }
174
175  /// isEnabled - Return true if this macro is enabled: in other words, that we
176  /// are not currently in an expansion of this macro.
177  bool isEnabled() const { return !IsDisabled; }
178
179  void EnableMacro() {
180    assert(IsDisabled && "Cannot enable an already-enabled macro!");
181    IsDisabled = false;
182  }
183
184  void DisableMacro() {
185    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
186    IsDisabled = true;
187  }
188};
189
190}  // end namespace clang
191
192#endif
193