]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Lex/TokenLexer.h
Vendor import of clang trunk r338150:
[FreeBSD/FreeBSD.git] / include / clang / Lex / TokenLexer.h
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_LEX_TOKENLEXER_H
15 #define LLVM_CLANG_LEX_TOKENLEXER_H
16
17 #include "clang/Basic/SourceLocation.h"
18 #include "llvm/ADT/ArrayRef.h"
19
20 namespace clang {
21
22 class MacroArgs;
23 class MacroInfo;
24 class Preprocessor;
25 class Token;
26 class VAOptExpansionContext;
27
28 /// TokenLexer - This implements a lexer that returns tokens from a macro body
29 /// or token stream instead of lexing from a character buffer.  This is used for
30 /// macro expansion and _Pragma handling, for example.
31 class TokenLexer {
32   friend class Preprocessor;
33
34   /// Macro - The macro we are expanding from.  This is null if expanding a
35   /// token stream.
36   MacroInfo *Macro = nullptr;
37
38   /// ActualArgs - The actual arguments specified for a function-like macro, or
39   /// null.  The TokenLexer owns the pointed-to object.
40   MacroArgs *ActualArgs = nullptr;
41
42   /// PP - The current preprocessor object we are expanding for.
43   Preprocessor &PP;
44
45   /// Tokens - This is the pointer to an array of tokens that the macro is
46   /// defined to, with arguments expanded for function-like macros.  If this is
47   /// a token stream, these are the tokens we are returning.  This points into
48   /// the macro definition we are lexing from, a cache buffer that is owned by
49   /// the preprocessor, or some other buffer that we may or may not own
50   /// (depending on OwnsTokens).
51   /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
52   /// may update the pointer as needed.
53   const Token *Tokens;
54
55   /// NumTokens - This is the length of the Tokens array.
56   unsigned NumTokens;
57
58   /// This is the index of the next token that Lex will return.
59   unsigned CurTokenIdx;
60
61   /// ExpandLocStart/End - The source location range where this macro was
62   /// expanded.
63   SourceLocation ExpandLocStart, ExpandLocEnd;
64
65   /// Source location pointing at the source location entry chunk that
66   /// was reserved for the current macro expansion.
67   SourceLocation MacroExpansionStart;
68   
69   /// The offset of the macro expansion in the
70   /// "source location address space".
71   unsigned MacroStartSLocOffset;
72
73   /// Location of the macro definition.
74   SourceLocation MacroDefStart;
75
76   /// 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   // NextTokGetsSpace - When this is true, the next token appended to the
85   // output list during function argument expansion will get a leading space,
86   // regardless of whether it had one to begin with or not. This is used for
87   // placemarker support. If still true after function argument expansion, the
88   // leading space will be applied to the first token following the macro
89   // expansion.
90   bool NextTokGetsSpace : 1;
91
92   /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
93   /// array, and thus needs to free it when destroyed.  For simple object-like
94   /// macros (for example) we just point into the token buffer of the macro
95   /// definition, we don't make a copy of it.
96   bool OwnsTokens : 1;
97
98   /// DisableMacroExpansion - This is true when tokens lexed from the TokenLexer
99   /// should not be subject to further macro expansion.
100   bool DisableMacroExpansion : 1;
101
102 public:
103   /// Create a TokenLexer for the specified macro with the specified actual
104   /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
105   /// ILEnd specifies the location of the ')' for a function-like macro or the
106   /// identifier for an object-like macro.
107   TokenLexer(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
108              MacroArgs *ActualArgs, Preprocessor &pp)
109       : PP(pp), OwnsTokens(false) {
110     Init(Tok, ILEnd, MI, ActualArgs);
111   }
112
113   /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
114   /// specified, this takes ownership of the tokens and delete[]'s them when
115   /// the token lexer is empty.
116   TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
117              bool ownsTokens, Preprocessor &pp)
118       : PP(pp), OwnsTokens(false) {
119     Init(TokArray, NumToks, DisableExpansion, ownsTokens);
120   }
121
122   TokenLexer(const TokenLexer &) = delete;
123   TokenLexer &operator=(const TokenLexer &) = delete;
124   ~TokenLexer() { destroy(); }
125
126   /// Init - Initialize this TokenLexer to expand from the specified macro
127   /// with the specified argument information.  Note that this ctor takes
128   /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
129   /// ')' for a function-like macro or the identifier for an object-like macro.
130   void Init(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
131             MacroArgs *ActualArgs);
132
133   /// Init - Initialize this TokenLexer with the specified token stream.
134   /// This does not take ownership of the specified token vector.
135   ///
136   /// DisableExpansion is true when macro expansion of tokens lexed from this
137   /// stream should be disabled.
138   void Init(const Token *TokArray, unsigned NumToks,
139             bool DisableMacroExpansion, bool OwnsTokens);
140
141   /// isNextTokenLParen - If the next token lexed will pop this macro off the
142   /// expansion stack, return 2.  If the next unexpanded token is a '(', return
143   /// 1, otherwise return 0.
144   unsigned isNextTokenLParen() const;
145
146   /// Lex - Lex and return a token from this macro stream.
147   bool Lex(Token &Tok);
148
149   /// isParsingPreprocessorDirective - Return true if we are in the middle of a
150   /// preprocessor directive.
151   bool isParsingPreprocessorDirective() const;
152
153 private:
154   void destroy();
155
156   /// isAtEnd - Return true if the next lex call will pop this macro off the
157   /// include stack.
158   bool isAtEnd() const {
159     return CurTokenIdx == NumTokens;
160   }
161
162   /// Concatenates the next (sub-)sequence of \p Tokens separated by '##'
163   /// starting with LHSTok - stopping when we encounter a token that is neither
164   /// '##' nor preceded by '##'.  Places the result back into \p LHSTok and sets
165   /// \p CurIdx to point to the token following the last one that was pasted.
166   ///
167   /// Also performs the MSVC extension wide-literal token pasting involved with:
168   ///       \code L #macro-arg. \endcode
169   ///
170   /// \param[in,out] LHSTok - Contains the token to the left of '##' in \p
171   /// Tokens upon entry and will contain the resulting concatenated Token upon
172   /// exit.
173   ///
174   /// \param[in] TokenStream - The stream of Tokens we are lexing from.
175   ///
176   /// \param[in,out] CurIdx - Upon entry, \pTokens[\pCurIdx] must equal '##'
177   /// (with the exception of the MSVC extension mentioned above).  Upon exit, it
178   /// is set to the index of the token following the last token that was
179   /// concatenated together.
180   ///
181   /// \returns If this returns true, the caller should immediately return the
182   /// token.
183   bool pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
184                    unsigned int &CurIdx);
185
186   /// Calls pasteTokens above, passing in the '*this' object's Tokens and
187   /// CurTokenIdx data members.
188   bool pasteTokens(Token &Tok);
189
190
191   /// Takes the tail sequence of tokens within ReplacementToks that represent
192   /// the just expanded __VA_OPT__ tokens (possibly zero tokens) and transforms
193   /// them into a string.  \p VCtx is used to determine which token represents
194   /// the first __VA_OPT__ replacement token.
195   ///
196   /// \param[in,out] ReplacementToks - Contains the current Replacement Tokens
197   /// (prior to rescanning and token pasting), the tail end of which represents
198   /// the tokens just expanded through __VA_OPT__ processing.  These (sub)
199   /// sequence of tokens are folded into one stringified token.
200   ///
201   /// \param[in] VCtx - contains relevant contextual information about the
202   /// state of the tokens around and including the __VA_OPT__ token, necessary
203   /// for stringification.
204   void stringifyVAOPTContents(SmallVectorImpl<Token> &ReplacementToks,
205                               const VAOptExpansionContext &VCtx,
206                               SourceLocation VAOPTClosingParenLoc);
207
208   /// Expand the arguments of a function-like macro so that we can quickly
209   /// return preexpanded tokens from Tokens.
210   void ExpandFunctionArguments();
211
212   /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
213   /// together to form a comment that comments out everything in the current
214   /// macro, other active macros, and anything left on the current physical
215   /// source line of the expanded buffer.  Handle this by returning the
216   /// first token on the next line.
217   void HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc);
218
219   /// If \p loc is a FileID and points inside the current macro
220   /// definition, returns the appropriate source location pointing at the
221   /// macro expansion source location entry.
222   SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
223
224   /// Creates SLocEntries and updates the locations of macro argument
225   /// tokens to their new expanded locations.
226   ///
227   /// \param ArgIdSpellLoc the location of the macro argument id inside the
228   /// macro definition.
229   void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
230                                   Token *begin_tokens, Token *end_tokens);
231
232   /// Remove comma ahead of __VA_ARGS__, if present, according to compiler
233   /// dialect settings.  Returns true if the comma is removed.
234   bool MaybeRemoveCommaBeforeVaArgs(SmallVectorImpl<Token> &ResultToks,
235                                     bool HasPasteOperator,
236                                     MacroInfo *Macro, unsigned MacroArgNo,
237                                     Preprocessor &PP);
238
239   void PropagateLineStartLeadingSpaceInfo(Token &Result);
240 };
241
242 } // namespace clang
243
244 #endif // LLVM_CLANG_LEX_TOKENLEXER_H