PTHLexer.h revision 32a8ad526f9bc00539f000a2dd1ac3e167db61c1
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  /// Pointer to a side table containing offsets in the PTH file
46  ///  for token spellings.
47  const char* SpellingTable;
48
49  /// Number of cached spellings left in the cached source file.
50  unsigned SpellingsLeft;
51
52  PTHLexer(const PTHLexer&);  // DO NOT IMPLEMENT
53  void operator=(const PTHLexer&); // DO NOT IMPLEMENT
54
55  /// ReadToken - Used by PTHLexer to read tokens TokBuf.
56  void ReadToken(Token& T);
57
58  /// PTHMgr - The PTHManager object that created this PTHLexer.
59  PTHManager& PTHMgr;
60
61  Token EofToken;
62
63public:
64
65  /// Create a PTHLexer for the specified token stream.
66  PTHLexer(Preprocessor& pp, SourceLocation fileloc, const char* D,
67           const char* ppcond, const char* spellingTable, unsigned numSpellings,
68           PTHManager& PM);
69
70  ~PTHLexer() {}
71
72  /// Lex - Return the next token.
73  void Lex(Token &Tok);
74
75  void getEOF(Token &Tok);
76
77  /// DiscardToEndOfLine - Read the rest of the current preprocessor line as an
78  /// uninterpreted string.  This switches the lexer out of directive mode.
79  void DiscardToEndOfLine();
80
81  /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
82  /// tok::l_paren token, 0 if it is something else and 2 if there are no more
83  /// tokens controlled by this lexer.
84  unsigned isNextPPTokenLParen() {
85    // isNextPPTokenLParen is not on the hot path, and all we care about is
86    // whether or not we are at a token with kind tok::eof or tok::l_paren.
87    // Just read the first byte from the current token pointer to determine
88    // its kind.
89    tok::TokenKind x = (tok::TokenKind) (unsigned char) *CurPtr;
90    return x == tok::eof ? 2 : x == tok::l_paren;
91  }
92
93  /// IndirectLex - An indirect call to 'Lex' that can be invoked via
94  ///  the PreprocessorLexer interface.
95  void IndirectLex(Token &Result) { Lex(Result); }
96
97  /// Returns the cached spelling of a token.
98  /// \param[in] sloc The SourceLocation of the token.
99  /// \param[out] Buffer If a token's spelling is found in the PTH file then
100  ///   upon exit from this method \c Buffer will be set to the address of
101  ///   the character array representing that spelling.  No characters
102  ///   are copied.
103  /// \returns The number of characters for the spelling of the token.  This
104  ///   value is 0 if the spelling could not be found in the PTH file.
105  unsigned getSpelling(SourceLocation sloc, const char *&Buffer);
106
107  /// getSourceLocation - Return a source location for the token in
108  /// the current file.
109  SourceLocation getSourceLocation();
110
111  /// SkipBlock - Used by Preprocessor to skip the current conditional block.
112  bool SkipBlock();
113};
114
115}  // end namespace clang
116
117#endif
118