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 23 namespace clang { 24 25 class 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. 36 class 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 short Kind; 67 68 /// Flags - Bits we track about this token, members of the TokenFlags enum. 69 unsigned char Flags; 70 public: 71 72 // Various flags set per token: 73 enum TokenFlags { 74 StartOfLine = 0x01, // At start of line or only after whitespace. 75 LeadingSpace = 0x02, // Whitespace exists before this token. 76 DisableExpand = 0x04, // This identifier may never be macro expanded. 77 NeedsCleaning = 0x08, // Contained an escaped newline or trigraph. 78 LeadingEmptyMacro = 0x10 // Empty macro exists before this token. 79 }; 80 getKind()81 tok::TokenKind getKind() const { return (tok::TokenKind)Kind; } setKind(tok::TokenKind K)82 void setKind(tok::TokenKind K) { Kind = K; } 83 84 /// is/isNot - Predicates to check if this token is a specific kind, as in 85 /// "if (Tok.is(tok::l_brace)) {...}". is(tok::TokenKind K)86 bool is(tok::TokenKind K) const { return Kind == (unsigned) K; } isNot(tok::TokenKind K)87 bool isNot(tok::TokenKind K) const { return Kind != (unsigned) K; } 88 89 /// isAnyIdentifier - Return true if this is a raw identifier (when lexing 90 /// in raw mode) or a non-keyword identifier (when lexing in non-raw mode). isAnyIdentifier()91 bool isAnyIdentifier() const { 92 return is(tok::identifier) || is(tok::raw_identifier); 93 } 94 95 /// isLiteral - Return true if this is a "literal", like a numeric 96 /// constant, string, etc. isLiteral()97 bool isLiteral() const { 98 return is(tok::numeric_constant) || is(tok::char_constant) || 99 is(tok::string_literal) || is(tok::wide_string_literal) || 100 is(tok::angle_string_literal); 101 } 102 isAnnotation()103 bool isAnnotation() const { 104 #define ANNOTATION(NAME) \ 105 if (is(tok::annot_##NAME)) \ 106 return true; 107 #include "clang/Basic/TokenKinds.def" 108 return false; 109 } 110 111 /// getLocation - Return a source location identifier for the specified 112 /// offset in the current file. getLocation()113 SourceLocation getLocation() const { return Loc; } getLength()114 unsigned getLength() const { 115 assert(!isAnnotation() && "Annotation tokens have no length field"); 116 return UintData; 117 } 118 setLocation(SourceLocation L)119 void setLocation(SourceLocation L) { Loc = L; } setLength(unsigned Len)120 void setLength(unsigned Len) { 121 assert(!isAnnotation() && "Annotation tokens have no length field"); 122 UintData = Len; 123 } 124 getAnnotationEndLoc()125 SourceLocation getAnnotationEndLoc() const { 126 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token"); 127 return SourceLocation::getFromRawEncoding(UintData); 128 } setAnnotationEndLoc(SourceLocation L)129 void setAnnotationEndLoc(SourceLocation L) { 130 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token"); 131 UintData = L.getRawEncoding(); 132 } 133 getLastLoc()134 SourceLocation getLastLoc() const { 135 return isAnnotation() ? getAnnotationEndLoc() : getLocation(); 136 } 137 138 /// getAnnotationRange - SourceRange of the group of tokens that this 139 /// annotation token represents. getAnnotationRange()140 SourceRange getAnnotationRange() const { 141 return SourceRange(getLocation(), getAnnotationEndLoc()); 142 } setAnnotationRange(SourceRange R)143 void setAnnotationRange(SourceRange R) { 144 setLocation(R.getBegin()); 145 setAnnotationEndLoc(R.getEnd()); 146 } 147 getName()148 const char *getName() const { 149 return tok::getTokenName( (tok::TokenKind) Kind); 150 } 151 152 /// startToken - Reset all flags to cleared. 153 /// startToken()154 void startToken() { 155 Kind = tok::unknown; 156 Flags = 0; 157 PtrData = 0; 158 UintData = 0; 159 Loc = SourceLocation(); 160 } 161 getIdentifierInfo()162 IdentifierInfo *getIdentifierInfo() const { 163 assert(isNot(tok::raw_identifier) && 164 "getIdentifierInfo() on a tok::raw_identifier token!"); 165 assert(!isAnnotation() && 166 "getIdentifierInfo() on an annotation token!"); 167 if (isLiteral()) return 0; 168 return (IdentifierInfo*) PtrData; 169 } setIdentifierInfo(IdentifierInfo * II)170 void setIdentifierInfo(IdentifierInfo *II) { 171 PtrData = (void*) II; 172 } 173 174 /// getRawIdentifierData - For a raw identifier token (i.e., an identifier 175 /// lexed in raw mode), returns a pointer to the start of it in the text 176 /// buffer if known, null otherwise. getRawIdentifierData()177 const char *getRawIdentifierData() const { 178 assert(is(tok::raw_identifier)); 179 return reinterpret_cast<const char*>(PtrData); 180 } setRawIdentifierData(const char * Ptr)181 void setRawIdentifierData(const char *Ptr) { 182 assert(is(tok::raw_identifier)); 183 PtrData = const_cast<char*>(Ptr); 184 } 185 186 /// getLiteralData - For a literal token (numeric constant, string, etc), this 187 /// returns a pointer to the start of it in the text buffer if known, null 188 /// otherwise. getLiteralData()189 const char *getLiteralData() const { 190 assert(isLiteral() && "Cannot get literal data of non-literal"); 191 return reinterpret_cast<const char*>(PtrData); 192 } setLiteralData(const char * Ptr)193 void setLiteralData(const char *Ptr) { 194 assert(isLiteral() && "Cannot set literal data of non-literal"); 195 PtrData = const_cast<char*>(Ptr); 196 } 197 getAnnotationValue()198 void *getAnnotationValue() const { 199 assert(isAnnotation() && "Used AnnotVal on non-annotation token"); 200 return PtrData; 201 } setAnnotationValue(void * val)202 void setAnnotationValue(void *val) { 203 assert(isAnnotation() && "Used AnnotVal on non-annotation token"); 204 PtrData = val; 205 } 206 207 /// setFlag - Set the specified flag. setFlag(TokenFlags Flag)208 void setFlag(TokenFlags Flag) { 209 Flags |= Flag; 210 } 211 212 /// clearFlag - Unset the specified flag. clearFlag(TokenFlags Flag)213 void clearFlag(TokenFlags Flag) { 214 Flags &= ~Flag; 215 } 216 217 /// getFlags - Return the internal represtation of the flags. 218 /// Only intended for low-level operations such as writing tokens to 219 // disk. getFlags()220 unsigned getFlags() const { 221 return Flags; 222 } 223 224 /// setFlagValue - Set a flag to either true or false. setFlagValue(TokenFlags Flag,bool Val)225 void setFlagValue(TokenFlags Flag, bool Val) { 226 if (Val) 227 setFlag(Flag); 228 else 229 clearFlag(Flag); 230 } 231 232 /// isAtStartOfLine - Return true if this token is at the start of a line. 233 /// isAtStartOfLine()234 bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; } 235 236 /// hasLeadingSpace - Return true if this token has whitespace before it. 237 /// hasLeadingSpace()238 bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; } 239 240 /// isExpandDisabled - Return true if this identifier token should never 241 /// be expanded in the future, due to C99 6.10.3.4p2. isExpandDisabled()242 bool isExpandDisabled() const { 243 return (Flags & DisableExpand) ? true : false; 244 } 245 246 /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 247 bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const; 248 249 /// getObjCKeywordID - Return the ObjC keyword kind. 250 tok::ObjCKeywordKind getObjCKeywordID() const; 251 252 /// needsCleaning - Return true if this token has trigraphs or escaped 253 /// newlines in it. 254 /// needsCleaning()255 bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; } 256 257 /// \brief Return true if this token has an empty macro before it. 258 /// hasLeadingEmptyMacro()259 bool hasLeadingEmptyMacro() const { 260 return (Flags & LeadingEmptyMacro) ? true : false; 261 } 262 263 }; 264 265 /// PPConditionalInfo - Information about the conditional stack (#if directives) 266 /// currently active. 267 struct PPConditionalInfo { 268 /// IfLoc - Location where the conditional started. 269 /// 270 SourceLocation IfLoc; 271 272 /// WasSkipping - True if this was contained in a skipping directive, e.g. 273 /// in a "#if 0" block. 274 bool WasSkipping; 275 276 /// FoundNonSkip - True if we have emitted tokens already, and now we're in 277 /// an #else block or something. Only useful in Skipping blocks. 278 bool FoundNonSkip; 279 280 /// FoundElse - True if we've seen a #else in this block. If so, 281 /// #elif/#else directives are not allowed. 282 bool FoundElse; 283 }; 284 285 } // end namespace clang 286 287 namespace llvm { 288 template <> 289 struct isPodLike<clang::Token> { static const bool value = true; }; 290 } // end namespace llvm 291 292 #endif 293