TokenLexer.h revision e5c8ffe09f8fec836b90f23c2a3b996bfc5b09c4
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.
46  const Token *Tokens;
47
48  /// NumTokens - This is the length of the Tokens array.
49  ///
50  unsigned NumTokens;
51
52  /// CurToken - This is the next token that Lex will return.
53  ///
54  unsigned CurToken;
55
56  /// InstantiateLoc - The source location where this macro was instantiated.
57  ///
58  SourceLocation InstantiateLoc;
59
60  /// Lexical information about the expansion point of the macro: the identifier
61  /// that the macro expanded from had these properties.
62  bool AtStartOfLine : 1;
63  bool HasLeadingSpace : 1;
64
65  /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
66  /// array, and thus needs to free it when destroyed.  For simple object-like
67  /// macros (for example) we just point into the token buffer of the macro
68  /// definition, we don't make a copy of it.
69  bool OwnsTokens : 1;
70
71  TokenLexer(const TokenLexer&);  // DO NOT IMPLEMENT
72  void operator=(const TokenLexer&); // DO NOT IMPLEMENT
73public:
74  /// Create a TokenLexer for the specified macro with the specified actual
75  /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
76  TokenLexer(Token &Tok, MacroArgs *ActualArgs, Preprocessor &pp)
77    : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
78    Init(Tok, ActualArgs);
79  }
80
81  /// Init - Initialize this TokenLexer to expand from the specified macro
82  /// with the specified argument information.  Note that this ctor takes
83  /// ownership of the ActualArgs pointer.
84  void Init(Token &Tok, MacroArgs *ActualArgs);
85
86  /// Create a TokenLexer for the specified token stream.  This does not
87  /// take ownership of the specified token vector.
88  TokenLexer(const Token *TokArray, unsigned NumToks, Preprocessor &pp)
89    : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
90    Init(TokArray, NumToks);
91  }
92
93  /// Init - Initialize this TokenLexer with the specified token stream.
94  /// This does not take ownership of the specified token vector.
95  void Init(const Token *TokArray, unsigned NumToks);
96
97  ~TokenLexer() { destroy(); }
98
99  /// isNextTokenLParen - If the next token lexed will pop this macro off the
100  /// expansion stack, return 2.  If the next unexpanded token is a '(', return
101  /// 1, otherwise return 0.
102  unsigned isNextTokenLParen() const;
103
104  /// Lex - Lex and return a token from this macro stream.
105  void Lex(Token &Tok);
106
107private:
108  void destroy();
109
110  /// isAtEnd - Return true if the next lex call will pop this macro off the
111  /// include stack.
112  bool isAtEnd() const {
113    return CurToken == NumTokens;
114  }
115
116  /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
117  /// operator.  Read the ## and RHS, and paste the LHS/RHS together.  If there
118  /// are is another ## after it, chomp it iteratively.  Return the result as
119  /// Tok.  If this returns true, the caller should immediately return the
120  /// token.
121  bool PasteTokens(Token &Tok);
122
123  /// Expand the arguments of a function-like macro so that we can quickly
124  /// return preexpanded tokens from Tokens.
125  void ExpandFunctionArguments();
126
127  /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
128  /// together to form a comment that comments out everything in the current
129  /// macro, other active macros, and anything left on the current physical
130  /// source line of the instantiated buffer.  Handle this by returning the
131  /// first token on the next line.
132  void HandleMicrosoftCommentPaste(Token &Tok);
133};
134
135}  // end namespace clang
136
137#endif
138