PTHLexer.h revision 30a12ec2a7f331d9e08acabe7cda853aaa7ba54b
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 // isNextPPTokenLParen is not on the hot path, and all we care about is 80 // whether or not we are at a token with kind tok::eof or tok::l_paren. 81 // Just read the first byte from the current token pointer to determine 82 // its kind. 83 tok::TokenKind x = (tok::TokenKind) (uint8_t) *CurPtr; 84 return x == tok::eof ? 2 : x == tok::l_paren; 85 } 86 87 /// IndirectLex - An indirect call to 'Lex' that can be invoked via 88 /// the PreprocessorLexer interface. 89 void IndirectLex(Token &Result) { Lex(Result); } 90 91 /// getSourceLocation - Return a source location for the token in 92 /// the current file. 93 SourceLocation getSourceLocation(); 94 95 /// SkipBlock - Used by Preprocessor to skip the current conditional block. 96 bool SkipBlock(); 97 98private: 99 /// AtLastToken - Returns true if the PTHLexer is at the last token. 100 bool AtLastToken() { 101 Token T = GetToken(); 102 return T.is(tok::eof) ? EofToken = T, true : false; 103 } 104 105 /// GetToken - Returns the next token. This method does not advance the 106 /// PTHLexer to the next token. 107 Token GetToken(); 108 109 /// AdvanceToken - Advances the PTHLexer to the next token. 110 void AdvanceToken() { NeedsFetching = true; } 111 112 bool LexEndOfFile(Token &Result); 113}; 114 115} // end namespace clang 116 117#endif 118