PTHLexer.h revision 268ee7016a2811803989487c0ad3799486092c63
1//===--- PTHLexer.h - Lexer based on Pre-tokenized input --------*- 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 PTHLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_PTHLEXER_H
15#define LLVM_CLANG_PTHLEXER_H
16
17#include "clang/Lex/PreprocessorLexer.h"
18#include <vector>
19
20namespace clang {
21
22class PTHManager;
23
24class PTHLexer : public PreprocessorLexer {
25  /// TokBuf - Buffer from PTH file containing raw token data.
26  const char* TokBuf;
27
28  /// CurPtr - Pointer into current offset of the token buffer where
29  ///  the next token will be read.
30  const char* CurPtr;
31
32  /// LastHashTokPtr - Pointer into TokBuf of the last processed '#'
33  ///  token that appears at the start of a line.
34  const char* LastHashTokPtr;
35
36  /// PPCond - Pointer to a side table in the PTH file that provides a
37  ///  a consise summary of the preproccessor conditional block structure.
38  ///  This is used to perform quick skipping of conditional blocks.
39  const char* PPCond;
40
41  /// CurPPCondPtr - Pointer inside PPCond that refers to the next entry
42  ///  to process when doing quick skipping of preprocessor blocks.
43  const char* CurPPCondPtr;
44
45  PTHLexer(const PTHLexer&);  // DO NOT IMPLEMENT
46  void operator=(const PTHLexer&); // DO NOT IMPLEMENT
47
48  /// ReadToken - Used by PTHLexer to read tokens TokBuf.
49  void ReadToken(Token& T);
50
51  /// PTHMgr - The PTHManager object that created this PTHLexer.
52  PTHManager& PTHMgr;
53
54  Token LastFetched;
55  Token EofToken;
56  bool NeedsFetching;
57
58public:
59
60  /// Create a PTHLexer for the specified token stream.
61  PTHLexer(Preprocessor& pp, SourceLocation fileloc, const char* D,
62           const char* ppcond, PTHManager& PM);
63
64  ~PTHLexer() {}
65
66  /// Lex - Return the next token.
67  void Lex(Token &Tok);
68
69  void setEOF(Token &Tok);
70
71  /// DiscardToEndOfLine - Read the rest of the current preprocessor line as an
72  /// uninterpreted string.  This switches the lexer out of directive mode.
73  void DiscardToEndOfLine();
74
75  /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
76  /// tok::l_paren token, 0 if it is something else and 2 if there are no more
77  /// tokens controlled by this lexer.
78  unsigned isNextPPTokenLParen() {
79    return AtLastToken() ? 2 : GetToken().is(tok::l_paren);
80  }
81
82  /// IndirectLex - An indirect call to 'Lex' that can be invoked via
83  ///  the PreprocessorLexer interface.
84  void IndirectLex(Token &Result) { Lex(Result); }
85
86  /// getSourceLocation - Return a source location for the token in
87  /// the current file.
88  SourceLocation getSourceLocation() { return GetToken().getLocation(); }
89
90  /// SkipBlock - Used by Preprocessor to skip the current conditional block.
91  bool SkipBlock();
92
93private:
94  /// AtLastToken - Returns true if the PTHLexer is at the last token.
95  bool AtLastToken() {
96    Token T = GetToken();
97    return T.is(tok::eof) ? EofToken = T, true : false;
98  }
99
100  /// GetToken - Returns the next token.  This method does not advance the
101  ///  PTHLexer to the next token.
102  Token GetToken();
103
104  /// AdvanceToken - Advances the PTHLexer to the next token.
105  void AdvanceToken() { NeedsFetching = true; }
106
107  bool LexEndOfFile(Token &Result);
108};
109
110}  // end namespace clang
111
112#endif
113