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