Token.h revision d6fb7ef028d9aa0b3e8943b7bc049c524437b407
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. 28/// 29/// The parser can create a special "annotation token" representing a stream of 30/// tokens that were parsed and semantically resolved, e.g.: "foo::MyClass<int>" 31/// can be represented by a single typename annotation token that carries 32/// information about the SourceRange of the tokens and the type object. 33class Token { 34 /// The location of the token. 35 SourceLocation Loc; 36 37 // Conceptually these next two fields could be in a union with 38 // access depending on isAnnotationToken(). However, this causes gcc 39 // 4.2 to pessimize LexTokenInternal, a very performance critical 40 // routine. Keeping as separate members with casts until a more 41 // beautiful fix presents itself. 42 43 /// UintData - This holds either the length of the token text, when 44 /// a normal token, or the end of the SourceRange when an annotation 45 /// token. 46 unsigned UintData; 47 48 /// PtrData - For normal tokens, this points to the uniqued 49 /// information for the identifier (if an identifier token) or 50 /// null. For annotation tokens, this points to information specific 51 /// to the annotation token. 52 void *PtrData; 53 54 /// Kind - The actual flavor of token this is. 55 /// 56 unsigned Kind : 8; // DON'T make Kind a 'tok::TokenKind'; 57 // MSVC will treat it as a signed char and 58 // TokenKinds > 127 won't be handled correctly. 59 60 /// Flags - Bits we track about this token, members of the TokenFlags enum. 61 unsigned Flags : 8; 62public: 63 64 // Various flags set per token: 65 enum TokenFlags { 66 StartOfLine = 0x01, // At start of line or only after whitespace. 67 LeadingSpace = 0x02, // Whitespace exists before this token. 68 DisableExpand = 0x04, // This identifier may never be macro expanded. 69 NeedsCleaning = 0x08 // Contained an escaped newline or trigraph. 70 }; 71 72 tok::TokenKind getKind() const { return (tok::TokenKind)Kind; } 73 void setKind(tok::TokenKind K) { Kind = K; } 74 75 /// is/isNot - Predicates to check if this token is a specific kind, as in 76 /// "if (Tok.is(tok::l_brace)) {...}". 77 bool is(tok::TokenKind K) const { return Kind == (unsigned) K; } 78 bool isNot(tok::TokenKind K) const { return Kind != (unsigned) K; } 79 80 bool isAnnotationToken() const { 81 return is(tok::annot_qualtypename) || 82 is(tok::annot_cxxscope) || 83 is(tok::annot_template_id); 84 } 85 86 /// getLocation - Return a source location identifier for the specified 87 /// offset in the current file. 88 SourceLocation getLocation() const { return Loc; } 89 unsigned getLength() const { 90 assert(!isAnnotationToken() && "Used Length on annotation token"); 91 return UintData; 92 } 93 94 void setLocation(SourceLocation L) { Loc = L; } 95 void setLength(unsigned Len) { UintData = Len; } 96 97 SourceLocation getAnnotationEndLoc() const { 98 assert(isAnnotationToken() && "Used AnnotEndLocID on non-annotation token"); 99 return SourceLocation::getFromRawEncoding(UintData); 100 } 101 void setAnnotationEndLoc(SourceLocation L) { 102 assert(isAnnotationToken() && "Used AnnotEndLocID on non-annotation token"); 103 UintData = L.getRawEncoding(); 104 } 105 106 /// getAnnotationRange - SourceRange of the group of tokens that this 107 /// annotation token represents. 108 SourceRange getAnnotationRange() const { 109 return SourceRange(getLocation(), getAnnotationEndLoc()); 110 } 111 void setAnnotationRange(SourceRange R) { 112 setLocation(R.getBegin()); 113 setAnnotationEndLoc(R.getEnd()); 114 } 115 116 const char *getName() const { 117 return tok::getTokenName( (tok::TokenKind) Kind); 118 } 119 120 /// startToken - Reset all flags to cleared. 121 /// 122 void startToken() { 123 Flags = 0; 124 PtrData = 0; 125 Loc = SourceLocation(); 126 } 127 128 IdentifierInfo *getIdentifierInfo() const { 129 assert(!isAnnotationToken() && "Used IdentInfo on annotation token"); 130 return (IdentifierInfo*) PtrData; 131 } 132 void setIdentifierInfo(IdentifierInfo *II) { 133 PtrData = (void*) II; 134 } 135 136 void *getAnnotationValue() const { 137 assert(isAnnotationToken() && "Used AnnotVal on non-annotation token"); 138 return PtrData; 139 } 140 void setAnnotationValue(void *val) { 141 assert(isAnnotationToken() && "Used AnnotVal on non-annotation token"); 142 PtrData = val; 143 } 144 145 /// setFlag - Set the specified flag. 146 void setFlag(TokenFlags Flag) { 147 Flags |= Flag; 148 } 149 150 /// clearFlag - Unset the specified flag. 151 void clearFlag(TokenFlags Flag) { 152 Flags &= ~Flag; 153 } 154 155 /// getFlags - Return the internal represtation of the flags. 156 /// Only intended for low-level operations such as writing tokens to 157 // disk. 158 unsigned getFlags() const { 159 return Flags; 160 } 161 162 /// setFlagValue - Set a flag to either true or false. 163 void setFlagValue(TokenFlags Flag, bool Val) { 164 if (Val) 165 setFlag(Flag); 166 else 167 clearFlag(Flag); 168 } 169 170 /// isAtStartOfLine - Return true if this token is at the start of a line. 171 /// 172 bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; } 173 174 /// hasLeadingSpace - Return true if this token has whitespace before it. 175 /// 176 bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; } 177 178 /// isExpandDisabled - Return true if this identifier token should never 179 /// be expanded in the future, due to C99 6.10.3.4p2. 180 bool isExpandDisabled() const { 181 return (Flags & DisableExpand) ? true : false; 182 } 183 184 /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 185 bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const; 186 187 /// getObjCKeywordID - Return the ObjC keyword kind. 188 tok::ObjCKeywordKind getObjCKeywordID() const; 189 190 /// needsCleaning - Return true if this token has trigraphs or escaped 191 /// newlines in it. 192 /// 193 bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; } 194}; 195 196/// PPConditionalInfo - Information about the conditional stack (#if directives) 197/// currently active. 198struct PPConditionalInfo { 199 /// IfLoc - Location where the conditional started. 200 /// 201 SourceLocation IfLoc; 202 203 /// WasSkipping - True if this was contained in a skipping directive, e.g. 204 /// in a "#if 0" block. 205 bool WasSkipping; 206 207 /// FoundNonSkip - True if we have emitted tokens already, and now we're in 208 /// an #else block or something. Only useful in Skipping blocks. 209 bool FoundNonSkip; 210 211 /// FoundElse - True if we've seen a #else in this block. If so, 212 /// #elif/#else directives are not allowed. 213 bool FoundElse; 214}; 215 216/// TemplateIdAnnotation - Information about a template-id annotation 217/// token, which contains the template declaration, template 218/// arguments, and the source locations for important tokens. 219struct TemplateIdAnnotation { 220 /// TemplateNameLoc - The location of the template name within the 221 /// source. 222 SourceLocation TemplateNameLoc; 223 224 /// Template - The declaration of the template corresponding to the 225 /// template-name. This is an Action::DeclTy*. 226 void *Template; 227 228 /// LAngleLoc - The location of the '<' before the template argument 229 /// list. 230 SourceLocation LAngleLoc; 231 232 /// NumArgs - The number of template arguments. The arguments 233 /// themselves are Action::TemplateArgTy pointers allocated directly 234 /// following the TemplateIdAnnotation structure. 235 unsigned NumArgs; 236}; 237 238} // end namespace clang 239 240#endif 241