1 //===--- Token.h - Token interface ------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the Token interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LEX_TOKEN_H 14 #define LLVM_CLANG_LEX_TOKEN_H 15 16 #include "clang/Basic/SourceLocation.h" 17 #include "clang/Basic/TokenKinds.h" 18 #include "llvm/ADT/StringRef.h" 19 #include <cassert> 20 21 namespace clang { 22 23 class IdentifierInfo; 24 25 /// Token - This structure provides full information about a lexed token. 26 /// It is not intended to be space efficient, it is intended to return as much 27 /// information as possible about each returned token. This is expected to be 28 /// compressed into a smaller form if memory footprint is important. 29 /// 30 /// The parser can create a special "annotation token" representing a stream of 31 /// tokens that were parsed and semantically resolved, e.g.: "foo::MyClass<int>" 32 /// can be represented by a single typename annotation token that carries 33 /// information about the SourceRange of the tokens and the type object. 34 class Token { 35 /// The location of the token. This is actually a SourceLocation. 36 SourceLocation::UIntTy Loc; 37 38 // Conceptually these next two fields could be in a union. However, this 39 // causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical 40 // routine. Keeping as separate members with casts until a more beautiful fix 41 // 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 SourceLocation::UIntTy UintData; 47 48 /// PtrData - This is a union of four different pointer types, which depends 49 /// on what type of token this is: 50 /// Identifiers, keywords, etc: 51 /// This is an IdentifierInfo*, which contains the uniqued identifier 52 /// spelling. 53 /// Literals: isLiteral() returns true. 54 /// This is a pointer to the start of the token in a text buffer, which 55 /// may be dirty (have trigraphs / escaped newlines). 56 /// Annotations (resolved type names, C++ scopes, etc): isAnnotation(). 57 /// This is a pointer to sema-specific data for the annotation token. 58 /// Eof: 59 // This is a pointer to a Decl. 60 /// Other: 61 /// This is null. 62 void *PtrData; 63 64 /// Kind - The actual flavor of token this is. 65 tok::TokenKind Kind; 66 67 /// Flags - Bits we track about this token, members of the TokenFlags enum. 68 unsigned short Flags; 69 70 public: 71 // Various flags set per token: 72 enum TokenFlags { 73 StartOfLine = 0x01, // At start of line or only after whitespace 74 // (considering the line after macro expansion). 75 LeadingSpace = 0x02, // Whitespace exists before this token (considering 76 // whitespace after macro expansion). 77 DisableExpand = 0x04, // This identifier may never be macro expanded. 78 NeedsCleaning = 0x08, // Contained an escaped newline or trigraph. 79 LeadingEmptyMacro = 0x10, // Empty macro exists before this token. 80 HasUDSuffix = 0x20, // This string or character literal has a ud-suffix. 81 HasUCN = 0x40, // This identifier contains a UCN. 82 IgnoredComma = 0x80, // This comma is not a macro argument separator (MS). 83 StringifiedInMacro = 0x100, // This string or character literal is formed by 84 // macro stringizing or charizing operator. 85 CommaAfterElided = 0x200, // The comma following this token was elided (MS). 86 IsEditorPlaceholder = 0x400, // This identifier is a placeholder. 87 IsReinjected = 0x800, // A phase 4 token that was produced before and 88 // re-added, e.g. via EnterTokenStream. Annotation 89 // tokens are *not* reinjected. 90 }; 91 92 tok::TokenKind getKind() const { return Kind; } 93 void setKind(tok::TokenKind K) { Kind = K; } 94 95 /// is/isNot - Predicates to check if this token is a specific kind, as in 96 /// "if (Tok.is(tok::l_brace)) {...}". 97 bool is(tok::TokenKind K) const { return Kind == K; } 98 bool isNot(tok::TokenKind K) const { return Kind != K; } 99 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { 100 return is(K1) || is(K2); 101 } 102 template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const { 103 return is(K1) || isOneOf(Ks...); 104 } 105 106 /// Return true if this is a raw identifier (when lexing 107 /// in raw mode) or a non-keyword identifier (when lexing in non-raw mode). 108 bool isAnyIdentifier() const { 109 return tok::isAnyIdentifier(getKind()); 110 } 111 112 /// Return true if this is a "literal", like a numeric 113 /// constant, string, etc. 114 bool isLiteral() const { 115 return tok::isLiteral(getKind()); 116 } 117 118 /// Return true if this is any of tok::annot_* kind tokens. 119 bool isAnnotation() const { 120 return tok::isAnnotation(getKind()); 121 } 122 123 /// Return a source location identifier for the specified 124 /// offset in the current file. 125 SourceLocation getLocation() const { 126 return SourceLocation::getFromRawEncoding(Loc); 127 } 128 unsigned getLength() const { 129 assert(!isAnnotation() && "Annotation tokens have no length field"); 130 return UintData; 131 } 132 133 void setLocation(SourceLocation L) { Loc = L.getRawEncoding(); } 134 void setLength(unsigned Len) { 135 assert(!isAnnotation() && "Annotation tokens have no length field"); 136 UintData = Len; 137 } 138 139 SourceLocation getAnnotationEndLoc() const { 140 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token"); 141 return SourceLocation::getFromRawEncoding(UintData ? UintData : Loc); 142 } 143 void setAnnotationEndLoc(SourceLocation L) { 144 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token"); 145 UintData = L.getRawEncoding(); 146 } 147 148 SourceLocation getLastLoc() const { 149 return isAnnotation() ? getAnnotationEndLoc() : getLocation(); 150 } 151 152 SourceLocation getEndLoc() const { 153 return isAnnotation() ? getAnnotationEndLoc() 154 : getLocation().getLocWithOffset(getLength()); 155 } 156 157 /// SourceRange of the group of tokens that this annotation token 158 /// represents. 159 SourceRange getAnnotationRange() const { 160 return SourceRange(getLocation(), getAnnotationEndLoc()); 161 } 162 void setAnnotationRange(SourceRange R) { 163 setLocation(R.getBegin()); 164 setAnnotationEndLoc(R.getEnd()); 165 } 166 167 const char *getName() const { return tok::getTokenName(Kind); } 168 169 /// Reset all flags to cleared. 170 void startToken() { 171 Kind = tok::unknown; 172 Flags = 0; 173 PtrData = nullptr; 174 UintData = 0; 175 Loc = SourceLocation().getRawEncoding(); 176 } 177 178 IdentifierInfo *getIdentifierInfo() const { 179 assert(isNot(tok::raw_identifier) && 180 "getIdentifierInfo() on a tok::raw_identifier token!"); 181 assert(!isAnnotation() && 182 "getIdentifierInfo() on an annotation token!"); 183 if (isLiteral()) return nullptr; 184 if (is(tok::eof)) return nullptr; 185 return (IdentifierInfo*) PtrData; 186 } 187 void setIdentifierInfo(IdentifierInfo *II) { 188 PtrData = (void*) II; 189 } 190 191 const void *getEofData() const { 192 assert(is(tok::eof)); 193 return reinterpret_cast<const void *>(PtrData); 194 } 195 void setEofData(const void *D) { 196 assert(is(tok::eof)); 197 assert(!PtrData); 198 PtrData = const_cast<void *>(D); 199 } 200 201 /// getRawIdentifier - For a raw identifier token (i.e., an identifier 202 /// lexed in raw mode), returns a reference to the text substring in the 203 /// buffer if known. 204 StringRef getRawIdentifier() const { 205 assert(is(tok::raw_identifier)); 206 return StringRef(reinterpret_cast<const char *>(PtrData), getLength()); 207 } 208 void setRawIdentifierData(const char *Ptr) { 209 assert(is(tok::raw_identifier)); 210 PtrData = const_cast<char*>(Ptr); 211 } 212 213 /// getLiteralData - For a literal token (numeric constant, string, etc), this 214 /// returns a pointer to the start of it in the text buffer if known, null 215 /// otherwise. 216 const char *getLiteralData() const { 217 assert(isLiteral() && "Cannot get literal data of non-literal"); 218 return reinterpret_cast<const char*>(PtrData); 219 } 220 void setLiteralData(const char *Ptr) { 221 assert(isLiteral() && "Cannot set literal data of non-literal"); 222 PtrData = const_cast<char*>(Ptr); 223 } 224 225 void *getAnnotationValue() const { 226 assert(isAnnotation() && "Used AnnotVal on non-annotation token"); 227 return PtrData; 228 } 229 void setAnnotationValue(void *val) { 230 assert(isAnnotation() && "Used AnnotVal on non-annotation token"); 231 PtrData = val; 232 } 233 234 /// Set the specified flag. 235 void setFlag(TokenFlags Flag) { 236 Flags |= Flag; 237 } 238 239 /// Get the specified flag. 240 bool getFlag(TokenFlags Flag) const { 241 return (Flags & Flag) != 0; 242 } 243 244 /// Unset the specified flag. 245 void clearFlag(TokenFlags Flag) { 246 Flags &= ~Flag; 247 } 248 249 /// Return the internal represtation of the flags. 250 /// 251 /// This is only intended for low-level operations such as writing tokens to 252 /// disk. 253 unsigned getFlags() const { 254 return Flags; 255 } 256 257 /// Set a flag to either true or false. 258 void setFlagValue(TokenFlags Flag, bool Val) { 259 if (Val) 260 setFlag(Flag); 261 else 262 clearFlag(Flag); 263 } 264 265 /// isAtStartOfLine - Return true if this token is at the start of a line. 266 /// 267 bool isAtStartOfLine() const { return getFlag(StartOfLine); } 268 269 /// Return true if this token has whitespace before it. 270 /// 271 bool hasLeadingSpace() const { return getFlag(LeadingSpace); } 272 273 /// Return true if this identifier token should never 274 /// be expanded in the future, due to C99 6.10.3.4p2. 275 bool isExpandDisabled() const { return getFlag(DisableExpand); } 276 277 /// Return true if we have an ObjC keyword identifier. 278 bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const; 279 280 /// Return the ObjC keyword kind. 281 tok::ObjCKeywordKind getObjCKeywordID() const; 282 283 /// Return true if this token has trigraphs or escaped newlines in it. 284 bool needsCleaning() const { return getFlag(NeedsCleaning); } 285 286 /// Return true if this token has an empty macro before it. 287 /// 288 bool hasLeadingEmptyMacro() const { return getFlag(LeadingEmptyMacro); } 289 290 /// Return true if this token is a string or character literal which 291 /// has a ud-suffix. 292 bool hasUDSuffix() const { return getFlag(HasUDSuffix); } 293 294 /// Returns true if this token contains a universal character name. 295 bool hasUCN() const { return getFlag(HasUCN); } 296 297 /// Returns true if this token is formed by macro by stringizing or charizing 298 /// operator. 299 bool stringifiedInMacro() const { return getFlag(StringifiedInMacro); } 300 301 /// Returns true if the comma after this token was elided. 302 bool commaAfterElided() const { return getFlag(CommaAfterElided); } 303 304 /// Returns true if this token is an editor placeholder. 305 /// 306 /// Editor placeholders are produced by the code-completion engine and are 307 /// represented as characters between '<#' and '#>' in the source code. The 308 /// lexer uses identifier tokens to represent placeholders. 309 bool isEditorPlaceholder() const { return getFlag(IsEditorPlaceholder); } 310 }; 311 312 /// Information about the conditional stack (\#if directives) 313 /// currently active. 314 struct PPConditionalInfo { 315 /// Location where the conditional started. 316 SourceLocation IfLoc; 317 318 /// True if this was contained in a skipping directive, e.g., 319 /// in a "\#if 0" block. 320 bool WasSkipping; 321 322 /// True if we have emitted tokens already, and now we're in 323 /// an \#else block or something. Only useful in Skipping blocks. 324 bool FoundNonSkip; 325 326 /// True if we've seen a \#else in this block. If so, 327 /// \#elif/\#else directives are not allowed. 328 bool FoundElse; 329 }; 330 331 } // end namespace clang 332 333 #endif // LLVM_CLANG_LEX_TOKEN_H 334