Token.h revision 8e748ab52395328f2905855b295a22e33dc800b2
1//===--- Token.h - Token interface ------------------------------*- 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 Token interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_TOKEN_H 15#define LLVM_CLANG_TOKEN_H 16 17#include "clang/Basic/TokenKinds.h" 18#include "clang/Basic/SourceLocation.h" 19 20namespace clang { 21 22class IdentifierInfo; 23 24/// Token - This structure provides full information about a lexed token. 25/// It is not intended to be space efficient, it is intended to return as much 26/// information as possible about each returned token. This is expected to be 27/// compressed into a smaller form if memory footprint is important. 28class Token { 29 /// The location and length of the token text itself. 30 SourceLocation Loc; 31 unsigned Length; 32 33 /// IdentifierInfo - If this was an identifier, this points to the uniqued 34 /// information about this identifier. 35 IdentifierInfo *IdentInfo; 36 37 /// Kind - The actual flavor of token this is. 38 /// 39 unsigned Kind : 8; // DON'T make Kind a 'tok::TokenKind'; 40 // MSVC will treat it as a signed char and 41 // TokenKinds > 127 won't be handled correctly. 42 43 /// Flags - Bits we track about this token, members of the TokenFlags enum. 44 unsigned Flags : 8; 45public: 46 47 // Various flags set per token: 48 enum TokenFlags { 49 StartOfLine = 0x01, // At start of line or only after whitespace. 50 LeadingSpace = 0x02, // Whitespace exists before this token. 51 DisableExpand = 0x04, // This identifier may never be macro expanded. 52 NeedsCleaning = 0x08 // Contained an escaped newline or trigraph. 53 }; 54 55 tok::TokenKind getKind() const { return (tok::TokenKind)Kind; } 56 void setKind(tok::TokenKind K) { Kind = K; } 57 58 /// is/isNot - Predicates to check if this token is a specific kind, as in 59 /// "if (Tok.is(tok::l_brace)) {...}". 60 bool is(tok::TokenKind K) const { return Kind == (unsigned) K; } 61 bool isNot(tok::TokenKind K) const { return Kind != (unsigned) K; } 62 63 /// getLocation - Return a source location identifier for the specified 64 /// offset in the current file. 65 SourceLocation getLocation() const { return Loc; } 66 unsigned getLength() const { return Length; } 67 68 void setLocation(SourceLocation L) { Loc = L; } 69 void setLength(unsigned Len) { Length = Len; } 70 71 const char *getName() const { 72 return tok::getTokenName( (tok::TokenKind) Kind); 73 } 74 75 /// startToken - Reset all flags to cleared. 76 /// 77 void startToken() { 78 Flags = 0; 79 IdentInfo = 0; 80 Loc = SourceLocation(); 81 } 82 83 IdentifierInfo *getIdentifierInfo() const { return IdentInfo; } 84 void setIdentifierInfo(IdentifierInfo *II) { 85 IdentInfo = II; 86 } 87 88 /// isNamedIdentifier - Return true if this token is a ppidentifier with the 89 /// specified name. For example, tok.isNamedIdentifier("this"). 90 bool isNamedIdentifier(const char *Name) const; 91 92 /// setFlag - Set the specified flag. 93 void setFlag(TokenFlags Flag) { 94 Flags |= Flag; 95 } 96 97 /// clearFlag - Unset the specified flag. 98 void clearFlag(TokenFlags Flag) { 99 Flags &= ~Flag; 100 } 101 102 /// setFlagValue - Set a flag to either true or false. 103 void setFlagValue(TokenFlags Flag, bool Val) { 104 if (Val) 105 setFlag(Flag); 106 else 107 clearFlag(Flag); 108 } 109 110 /// isAtStartOfLine - Return true if this token is at the start of a line. 111 /// 112 bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; } 113 114 /// hasLeadingSpace - Return true if this token has whitespace before it. 115 /// 116 bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; } 117 118 /// isExpandDisabled - Return true if this identifier token should never 119 /// be expanded in the future, due to C99 6.10.3.4p2. 120 bool isExpandDisabled() const { 121 return (Flags & DisableExpand) ? true : false; 122 } 123 124 /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 125 bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const; 126 127 /// getObjCKeywordID - Return the ObjC keyword kind. 128 tok::ObjCKeywordKind getObjCKeywordID() const; 129 130 /// needsCleaning - Return true if this token has trigraphs or escaped 131 /// newlines in it. 132 /// 133 bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; } 134}; 135 136/// PPConditionalInfo - Information about the conditional stack (#if directives) 137/// currently active. 138struct PPConditionalInfo { 139 /// IfLoc - Location where the conditional started. 140 /// 141 SourceLocation IfLoc; 142 143 /// WasSkipping - True if this was contained in a skipping directive, e.g. 144 /// in a "#if 0" block. 145 bool WasSkipping; 146 147 /// FoundNonSkip - True if we have emitted tokens already, and now we're in 148 /// an #else block or something. Only useful in Skipping blocks. 149 bool FoundNonSkip; 150 151 /// FoundElse - True if we've seen a #else in this block. If so, 152 /// #elif/#else directives are not allowed. 153 bool FoundElse; 154}; 155 156} // end namespace clang 157 158#endif 159