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