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