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