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