1 //===--- TokenLexer.h - Lex from a token buffer -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TokenLexer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_TOKENLEXER_H
15 #define LLVM_CLANG_TOKENLEXER_H
17 #include "clang/Basic/SourceLocation.h"
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.
30 /// Macro - The macro we are expanding from. This is null if expanding a
35 /// ActualArgs - The actual arguments specified for a function-like macro, or
36 /// null. The TokenLexer owns the pointed-to object.
37 MacroArgs *ActualArgs;
39 /// PP - The current preprocessor object we are expanding for.
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 cache buffer that is owned by
47 /// the preprocessor, or some other buffer that we may or may not own
48 /// (depending on OwnsTokens).
49 /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
50 /// may update the pointer as needed.
52 friend class Preprocessor;
54 /// NumTokens - This is the length of the Tokens array.
58 /// CurToken - This is the next token that Lex will return.
62 /// ExpandLocStart/End - The source location range where this macro was
64 SourceLocation ExpandLocStart, ExpandLocEnd;
66 /// \brief Source location pointing at the source location entry chunk that
67 /// was reserved for the current macro expansion.
68 SourceLocation MacroExpansionStart;
70 /// \brief The offset of the macro expansion in the
71 /// "source location address space".
72 unsigned MacroStartSLocOffset;
74 /// \brief FileID/offset of the start of the macro definition.
75 std::pair<FileID, unsigned> MacroDefStartInfo;
77 /// Lexical information about the expansion point of the macro: the identifier
78 /// that the macro expanded from had these properties.
79 bool AtStartOfLine : 1;
80 bool HasLeadingSpace : 1;
82 /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
83 /// array, and thus needs to free it when destroyed. For simple object-like
84 /// macros (for example) we just point into the token buffer of the macro
85 /// definition, we don't make a copy of it.
88 /// DisableMacroExpansion - This is true when tokens lexed from the TokenLexer
89 /// should not be subject to further macro expansion.
90 bool DisableMacroExpansion : 1;
92 TokenLexer(const TokenLexer&); // DO NOT IMPLEMENT
93 void operator=(const TokenLexer&); // DO NOT IMPLEMENT
95 /// Create a TokenLexer for the specified macro with the specified actual
96 /// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
97 /// ILEnd specifies the location of the ')' for a function-like macro or the
98 /// identifier for an object-like macro.
99 TokenLexer(Token &Tok, SourceLocation ILEnd, MacroArgs *ActualArgs,
101 : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
102 Init(Tok, ILEnd, ActualArgs);
105 /// Init - Initialize this TokenLexer to expand from the specified macro
106 /// with the specified argument information. Note that this ctor takes
107 /// ownership of the ActualArgs pointer. ILEnd specifies the location of the
108 /// ')' for a function-like macro or the identifier for an object-like macro.
109 void Init(Token &Tok, SourceLocation ILEnd, MacroArgs *ActualArgs);
111 /// Create a TokenLexer for the specified token stream. If 'OwnsTokens' is
112 /// specified, this takes ownership of the tokens and delete[]'s them when
113 /// the token lexer is empty.
114 TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
115 bool ownsTokens, Preprocessor &pp)
116 : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
117 Init(TokArray, NumToks, DisableExpansion, ownsTokens);
120 /// Init - Initialize this TokenLexer with the specified token stream.
121 /// This does not take ownership of the specified token vector.
123 /// DisableExpansion is true when macro expansion of tokens lexed from this
124 /// stream should be disabled.
125 void Init(const Token *TokArray, unsigned NumToks,
126 bool DisableMacroExpansion, bool OwnsTokens);
128 ~TokenLexer() { destroy(); }
130 /// isNextTokenLParen - If the next token lexed will pop this macro off the
131 /// expansion stack, return 2. If the next unexpanded token is a '(', return
132 /// 1, otherwise return 0.
133 unsigned isNextTokenLParen() const;
135 /// Lex - Lex and return a token from this macro stream.
136 void Lex(Token &Tok);
138 /// isParsingPreprocessorDirective - Return true if we are in the middle of a
139 /// preprocessor directive.
140 bool isParsingPreprocessorDirective() const;
145 /// isAtEnd - Return true if the next lex call will pop this macro off the
147 bool isAtEnd() const {
148 return CurToken == NumTokens;
151 /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
152 /// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
153 /// are is another ## after it, chomp it iteratively. Return the result as
154 /// Tok. If this returns true, the caller should immediately return the
156 bool PasteTokens(Token &Tok);
158 /// Expand the arguments of a function-like macro so that we can quickly
159 /// return preexpanded tokens from Tokens.
160 void ExpandFunctionArguments();
162 /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
163 /// together to form a comment that comments out everything in the current
164 /// macro, other active macros, and anything left on the current physical
165 /// source line of the expanded buffer. Handle this by returning the
166 /// first token on the next line.
167 void HandleMicrosoftCommentPaste(Token &Tok);
169 /// \brief If \arg loc is a FileID and points inside the current macro
170 /// definition, returns the appropriate source location pointing at the
171 /// macro expansion source location entry.
172 SourceLocation getMacroExpansionLocation(SourceLocation loc) const;
175 } // end namespace clang