TokenLexer.h revision 32fca722dd974b8202d0fb9c71b6c185c0767da6
1//===--- TokenLexer.h - Lex from a token buffer -----------------*- 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 TokenLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOKENLEXER_H
15#define LLVM_CLANG_TOKENLEXER_H
16
17#include "clang/Basic/SourceLocation.h"
18
19namespace clang {
20  class MacroInfo;
21  class Preprocessor;
22  class Token;
23  class MacroArgs;
24
25/// TokenLexer - This implements a lexer that returns token from a macro body
26/// or token stream instead of lexing from a character buffer.  This is used for
27/// macro expansion and _Pragma handling, for example.
28///
29class TokenLexer {
30  /// Macro - The macro we are expanding from.  This is null if expanding a
31  /// token stream.
32  ///
33  MacroInfo *Macro;
34
35  /// ActualArgs - The actual arguments specified for a function-like macro, or
36  /// null.  The TokenLexer owns the pointed-to object.
37  MacroArgs *ActualArgs;
38
39  /// PP - The current preprocessor object we are expanding for.
40  ///
41  Preprocessor &PP;
42
43  /// Tokens - This is the pointer to an array of tokens that the macro is
44  /// defined to, with arguments expanded for function-like macros.  If this is
45  /// a token stream, these are the tokens we are returning.  This points into
46  /// the macro definition we are lexing from, a scratch buffer allocated from
47  /// the preprocessor's bump pointer allocator, or some other buffer that we
48  /// may or may not own (depending on OwnsTokens).
49  const Token *Tokens;
50
51  /// NumTokens - This is the length of the Tokens array.
52  ///
53  unsigned NumTokens;
54
55  /// CurToken - This is the next token that Lex will return.
56  ///
57  unsigned CurToken;
58
59  /// InstantiateLocStart/End - The source location range where this macro was
60  /// instantiated.
61  SourceLocation InstantiateLocStart, InstantiateLocEnd;
62
63  /// Lexical information about the expansion point of the macro: the identifier
64  /// that the macro expanded from had these properties.
65  bool AtStartOfLine : 1;
66  bool HasLeadingSpace : 1;
67
68  /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
69  /// array, and thus needs to free it when destroyed.  For simple object-like
70  /// macros (for example) we just point into the token buffer of the macro
71  /// definition, we don't make a copy of it.
72  bool OwnsTokens : 1;
73
74  /// DisableMacroExpansion - This is true when tokens lexed from the TokenLexer
75  /// should not be subject to further macro expansion.
76  bool DisableMacroExpansion : 1;
77
78  TokenLexer(const TokenLexer&);  // DO NOT IMPLEMENT
79  void operator=(const TokenLexer&); // DO NOT IMPLEMENT
80public:
81  /// Create a TokenLexer for the specified macro with the specified actual
82  /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
83  /// ILEnd specifies the location of the ')' for a function-like macro or the
84  /// identifier for an object-like macro.
85  TokenLexer(Token &Tok, SourceLocation ILEnd, MacroArgs *ActualArgs,
86             Preprocessor &pp)
87    : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
88    Init(Tok, ILEnd, ActualArgs);
89  }
90
91  /// Init - Initialize this TokenLexer to expand from the specified macro
92  /// with the specified argument information.  Note that this ctor takes
93  /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
94  /// ')' for a function-like macro or the identifier for an object-like macro.
95  void Init(Token &Tok, SourceLocation ILEnd, MacroArgs *ActualArgs);
96
97  /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
98  /// specified, this takes ownership of the tokens and delete[]'s them when
99  /// the token lexer is empty.
100  TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
101             bool ownsTokens, Preprocessor &pp)
102    : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
103    Init(TokArray, NumToks, DisableExpansion, ownsTokens);
104  }
105
106  /// Init - Initialize this TokenLexer with the specified token stream.
107  /// This does not take ownership of the specified token vector.
108  ///
109  /// DisableExpansion is true when macro expansion of tokens lexed from this
110  /// stream should be disabled.
111  void Init(const Token *TokArray, unsigned NumToks,
112            bool DisableMacroExpansion, bool OwnsTokens);
113
114  ~TokenLexer() { destroy(); }
115
116  /// isNextTokenLParen - If the next token lexed will pop this macro off the
117  /// expansion stack, return 2.  If the next unexpanded token is a '(', return
118  /// 1, otherwise return 0.
119  unsigned isNextTokenLParen() const;
120
121  /// Lex - Lex and return a token from this macro stream.
122  void Lex(Token &Tok);
123
124private:
125  void destroy();
126
127  /// isAtEnd - Return true if the next lex call will pop this macro off the
128  /// include stack.
129  bool isAtEnd() const {
130    return CurToken == NumTokens;
131  }
132
133  /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
134  /// operator.  Read the ## and RHS, and paste the LHS/RHS together.  If there
135  /// are is another ## after it, chomp it iteratively.  Return the result as
136  /// Tok.  If this returns true, the caller should immediately return the
137  /// token.
138  bool PasteTokens(Token &Tok);
139
140  /// Expand the arguments of a function-like macro so that we can quickly
141  /// return preexpanded tokens from Tokens.
142  void ExpandFunctionArguments();
143
144  /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
145  /// together to form a comment that comments out everything in the current
146  /// macro, other active macros, and anything left on the current physical
147  /// source line of the instantiated buffer.  Handle this by returning the
148  /// first token on the next line.
149  void HandleMicrosoftCommentPaste(Token &Tok);
150};
151
152}  // end namespace clang
153
154#endif
155