TokenLexer.h revision c30981a563a8947cb26b1e308d122fa2ef90fceb
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===--- TokenLexer.h - Lex from a token buffer -----------------*- C++ -*-===//
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                     The LLVM Compiler Infrastructure
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is distributed under the University of Illinois Open Source
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// License. See LICENSE.TXT for details.
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file defines the TokenLexer interface.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef LLVM_CLANG_TOKENLEXER_H
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define LLVM_CLANG_TOKENLEXER_H
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/SourceLocation.h"
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace clang {
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  class MacroInfo;
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  class Preprocessor;
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  class Token;
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  class MacroArgs;
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// TokenLexer - This implements a lexer that returns tokens from a macro body
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// or token stream instead of lexing from a character buffer.  This is used for
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// macro expansion and _Pragma handling, for example.
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass TokenLexer {
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// Macro - The macro we are expanding from.  This is null if expanding a
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// token stream.
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ///
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MacroInfo *Macro;
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// ActualArgs - The actual arguments specified for a function-like macro, or
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// null.  The TokenLexer owns the pointed-to object.
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MacroArgs *ActualArgs;
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// PP - The current preprocessor object we are expanding for.
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ///
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Preprocessor &PP;
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// Tokens - This is the pointer to an array of tokens that the macro is
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// defined to, with arguments expanded for function-like macros.  If this is
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// a token stream, these are the tokens we are returning.  This points into
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// the macro definition we are lexing from, a cache buffer that is owned by
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// the preprocessor, or some other buffer that we may or may not own
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// (depending on OwnsTokens).
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  /// may update the pointer as needed.
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Token *Tokens;
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  friend class Preprocessor;
53
54  /// NumTokens - This is the length of the Tokens array.
55  ///
56  unsigned NumTokens;
57
58  /// CurToken - This is the next token that Lex will return.
59  ///
60  unsigned CurToken;
61
62  /// ExpandLocStart/End - The source location range where this macro was
63  /// expanded.
64  SourceLocation ExpandLocStart, ExpandLocEnd;
65
66  /// \brief Source location pointing at the source location entry chunk that
67  /// was reserved for the current macro expansion.
68  SourceLocation MacroExpansionStart;
69
70  /// \brief The offset of the macro expansion in the
71  /// "source location address space".
72  unsigned MacroStartSLocOffset;
73
74  /// \brief Location of the macro definition.
75  SourceLocation MacroDefStart;
76  /// \brief Length of the macro definition.
77  unsigned MacroDefLength;
78
79  /// Lexical information about the expansion point of the macro: the identifier
80  /// that the macro expanded from had these properties.
81  bool AtStartOfLine : 1;
82  bool HasLeadingSpace : 1;
83
84  /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
85  /// array, and thus needs to free it when destroyed.  For simple object-like
86  /// macros (for example) we just point into the token buffer of the macro
87  /// definition, we don't make a copy of it.
88  bool OwnsTokens : 1;
89
90  /// DisableMacroExpansion - This is true when tokens lexed from the TokenLexer
91  /// should not be subject to further macro expansion.
92  bool DisableMacroExpansion : 1;
93
94  TokenLexer(const TokenLexer&);  // DO NOT IMPLEMENT
95  void operator=(const TokenLexer&); // DO NOT IMPLEMENT
96public:
97  /// Create a TokenLexer for the specified macro with the specified actual
98  /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
99  /// ILEnd specifies the location of the ')' for a function-like macro or the
100  /// identifier for an object-like macro.
101  TokenLexer(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
102             MacroArgs *ActualArgs, Preprocessor &pp)
103    : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
104    Init(Tok, ILEnd, MI, ActualArgs);
105  }
106
107  /// Init - Initialize this TokenLexer to expand from the specified macro
108  /// with the specified argument information.  Note that this ctor takes
109  /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
110  /// ')' for a function-like macro or the identifier for an object-like macro.
111  void Init(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
112            MacroArgs *ActualArgs);
113
114  /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
115  /// specified, this takes ownership of the tokens and delete[]'s them when
116  /// the token lexer is empty.
117  TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
118             bool ownsTokens, Preprocessor &pp)
119    : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
120    Init(TokArray, NumToks, DisableExpansion, ownsTokens);
121  }
122
123  /// Init - Initialize this TokenLexer with the specified token stream.
124  /// This does not take ownership of the specified token vector.
125  ///
126  /// DisableExpansion is true when macro expansion of tokens lexed from this
127  /// stream should be disabled.
128  void Init(const Token *TokArray, unsigned NumToks,
129            bool DisableMacroExpansion, bool OwnsTokens);
130
131  ~TokenLexer() { destroy(); }
132
133  /// isNextTokenLParen - If the next token lexed will pop this macro off the
134  /// expansion stack, return 2.  If the next unexpanded token is a '(', return
135  /// 1, otherwise return 0.
136  unsigned isNextTokenLParen() const;
137
138  /// Lex - Lex and return a token from this macro stream.
139  void Lex(Token &Tok);
140
141  /// isParsingPreprocessorDirective - Return true if we are in the middle of a
142  /// preprocessor directive.
143  bool isParsingPreprocessorDirective() const;
144
145private:
146  void destroy();
147
148  /// isAtEnd - Return true if the next lex call will pop this macro off the
149  /// include stack.
150  bool isAtEnd() const {
151    return CurToken == NumTokens;
152  }
153
154  /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
155  /// operator.  Read the ## and RHS, and paste the LHS/RHS together.  If there
156  /// are is another ## after it, chomp it iteratively.  Return the result as
157  /// Tok.  If this returns true, the caller should immediately return the
158  /// token.
159  bool PasteTokens(Token &Tok);
160
161  /// Expand the arguments of a function-like macro so that we can quickly
162  /// return preexpanded tokens from Tokens.
163  void ExpandFunctionArguments();
164
165  /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
166  /// together to form a comment that comments out everything in the current
167  /// macro, other active macros, and anything left on the current physical
168  /// source line of the expanded buffer.  Handle this by returning the
169  /// first token on the next line.
170  void HandleMicrosoftCommentPaste(Token &Tok);
171
172  /// \brief If \arg loc is a FileID and points inside the current macro
173  /// definition, returns the appropriate source location pointing at the
174  /// macro expansion source location entry.
175  SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
176
177  /// \brief Creates SLocEntries and updates the locations of macro argument
178  /// tokens to their new expanded locations.
179  ///
180  /// \param ArgIdSpellLoc the location of the macro argument id inside the
181  /// macro definition.
182  void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
183                                  Token *begin_tokens, Token *end_tokens);
184};
185
186}  // end namespace clang
187
188#endif
189