File indexing completed on 2026-05-10 08:36:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_CLANG_LEX_TOKEN_H
0014 #define LLVM_CLANG_LEX_TOKEN_H
0015
0016 #include "clang/Basic/SourceLocation.h"
0017 #include "clang/Basic/TokenKinds.h"
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/StringRef.h"
0020 #include <cassert>
0021
0022 namespace clang {
0023
0024 class IdentifierInfo;
0025 class LangOptions;
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 class Token {
0037
0038 SourceLocation::UIntTy Loc;
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 SourceLocation::UIntTy UintData;
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 void *PtrData;
0065
0066
0067 tok::TokenKind Kind;
0068
0069
0070 unsigned short Flags;
0071
0072 public:
0073
0074 enum TokenFlags {
0075 StartOfLine = 0x01,
0076
0077 LeadingSpace = 0x02,
0078
0079 DisableExpand = 0x04,
0080 NeedsCleaning = 0x08,
0081 LeadingEmptyMacro = 0x10,
0082 HasUDSuffix = 0x20,
0083 HasUCN = 0x40,
0084 IgnoredComma = 0x80,
0085 StringifiedInMacro = 0x100,
0086
0087 CommaAfterElided = 0x200,
0088 IsEditorPlaceholder = 0x400,
0089 IsReinjected = 0x800,
0090
0091
0092 };
0093
0094 tok::TokenKind getKind() const { return Kind; }
0095 void setKind(tok::TokenKind K) { Kind = K; }
0096
0097
0098
0099 bool is(tok::TokenKind K) const { return Kind == K; }
0100 bool isNot(tok::TokenKind K) const { return Kind != K; }
0101 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
0102 return is(K1) || is(K2);
0103 }
0104 template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const {
0105 return is(K1) || isOneOf(Ks...);
0106 }
0107
0108
0109
0110 bool isAnyIdentifier() const {
0111 return tok::isAnyIdentifier(getKind());
0112 }
0113
0114
0115
0116 bool isLiteral() const {
0117 return tok::isLiteral(getKind());
0118 }
0119
0120
0121 bool isAnnotation() const { return tok::isAnnotation(getKind()); }
0122
0123
0124
0125
0126 bool isRegularKeywordAttribute() const {
0127 return tok::isRegularKeywordAttribute(getKind());
0128 }
0129
0130
0131
0132 SourceLocation getLocation() const {
0133 return SourceLocation::getFromRawEncoding(Loc);
0134 }
0135 unsigned getLength() const {
0136 assert(!isAnnotation() && "Annotation tokens have no length field");
0137 return UintData;
0138 }
0139
0140 void setLocation(SourceLocation L) { Loc = L.getRawEncoding(); }
0141 void setLength(unsigned Len) {
0142 assert(!isAnnotation() && "Annotation tokens have no length field");
0143 UintData = Len;
0144 }
0145
0146 SourceLocation getAnnotationEndLoc() const {
0147 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token");
0148 return SourceLocation::getFromRawEncoding(UintData ? UintData : Loc);
0149 }
0150 void setAnnotationEndLoc(SourceLocation L) {
0151 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token");
0152 UintData = L.getRawEncoding();
0153 }
0154
0155 SourceLocation getLastLoc() const {
0156 return isAnnotation() ? getAnnotationEndLoc() : getLocation();
0157 }
0158
0159 SourceLocation getEndLoc() const {
0160 return isAnnotation() ? getAnnotationEndLoc()
0161 : getLocation().getLocWithOffset(getLength());
0162 }
0163
0164
0165
0166 SourceRange getAnnotationRange() const {
0167 return SourceRange(getLocation(), getAnnotationEndLoc());
0168 }
0169 void setAnnotationRange(SourceRange R) {
0170 setLocation(R.getBegin());
0171 setAnnotationEndLoc(R.getEnd());
0172 }
0173
0174 const char *getName() const { return tok::getTokenName(Kind); }
0175
0176
0177 void startToken() {
0178 Kind = tok::unknown;
0179 Flags = 0;
0180 PtrData = nullptr;
0181 UintData = 0;
0182 Loc = SourceLocation().getRawEncoding();
0183 }
0184
0185 bool hasPtrData() const { return PtrData != nullptr; }
0186
0187 IdentifierInfo *getIdentifierInfo() const {
0188 assert(isNot(tok::raw_identifier) &&
0189 "getIdentifierInfo() on a tok::raw_identifier token!");
0190 assert(!isAnnotation() &&
0191 "getIdentifierInfo() on an annotation token!");
0192 if (isLiteral()) return nullptr;
0193 if (is(tok::eof)) return nullptr;
0194 return (IdentifierInfo*) PtrData;
0195 }
0196 void setIdentifierInfo(IdentifierInfo *II) {
0197 PtrData = (void*) II;
0198 }
0199
0200 const void *getEofData() const {
0201 assert(is(tok::eof));
0202 return reinterpret_cast<const void *>(PtrData);
0203 }
0204 void setEofData(const void *D) {
0205 assert(is(tok::eof));
0206 assert(!PtrData);
0207 PtrData = const_cast<void *>(D);
0208 }
0209
0210
0211
0212
0213 StringRef getRawIdentifier() const {
0214 assert(is(tok::raw_identifier));
0215 return StringRef(reinterpret_cast<const char *>(PtrData), getLength());
0216 }
0217 void setRawIdentifierData(const char *Ptr) {
0218 assert(is(tok::raw_identifier));
0219 PtrData = const_cast<char*>(Ptr);
0220 }
0221
0222
0223
0224
0225 const char *getLiteralData() const {
0226 assert(isLiteral() && "Cannot get literal data of non-literal");
0227 return reinterpret_cast<const char*>(PtrData);
0228 }
0229 void setLiteralData(const char *Ptr) {
0230 assert(isLiteral() && "Cannot set literal data of non-literal");
0231 PtrData = const_cast<char*>(Ptr);
0232 }
0233
0234 void *getAnnotationValue() const {
0235 assert(isAnnotation() && "Used AnnotVal on non-annotation token");
0236 return PtrData;
0237 }
0238 void setAnnotationValue(void *val) {
0239 assert(isAnnotation() && "Used AnnotVal on non-annotation token");
0240 PtrData = val;
0241 }
0242
0243
0244 void setFlag(TokenFlags Flag) {
0245 Flags |= Flag;
0246 }
0247
0248
0249 bool getFlag(TokenFlags Flag) const {
0250 return (Flags & Flag) != 0;
0251 }
0252
0253
0254 void clearFlag(TokenFlags Flag) {
0255 Flags &= ~Flag;
0256 }
0257
0258
0259
0260
0261
0262 unsigned getFlags() const {
0263 return Flags;
0264 }
0265
0266
0267 void setFlagValue(TokenFlags Flag, bool Val) {
0268 if (Val)
0269 setFlag(Flag);
0270 else
0271 clearFlag(Flag);
0272 }
0273
0274
0275
0276 bool isAtStartOfLine() const { return getFlag(StartOfLine); }
0277
0278
0279
0280 bool hasLeadingSpace() const { return getFlag(LeadingSpace); }
0281
0282
0283
0284 bool isExpandDisabled() const { return getFlag(DisableExpand); }
0285
0286
0287 bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
0288
0289
0290 tok::ObjCKeywordKind getObjCKeywordID() const;
0291
0292 bool isSimpleTypeSpecifier(const LangOptions &LangOpts) const;
0293
0294
0295 bool needsCleaning() const { return getFlag(NeedsCleaning); }
0296
0297
0298
0299 bool hasLeadingEmptyMacro() const { return getFlag(LeadingEmptyMacro); }
0300
0301
0302
0303 bool hasUDSuffix() const { return getFlag(HasUDSuffix); }
0304
0305
0306 bool hasUCN() const { return getFlag(HasUCN); }
0307
0308
0309
0310 bool stringifiedInMacro() const { return getFlag(StringifiedInMacro); }
0311
0312
0313 bool commaAfterElided() const { return getFlag(CommaAfterElided); }
0314
0315
0316
0317
0318
0319
0320 bool isEditorPlaceholder() const { return getFlag(IsEditorPlaceholder); }
0321 };
0322
0323
0324
0325 struct PPConditionalInfo {
0326
0327 SourceLocation IfLoc;
0328
0329
0330
0331 bool WasSkipping;
0332
0333
0334
0335 bool FoundNonSkip;
0336
0337
0338
0339 bool FoundElse;
0340 };
0341
0342
0343 struct PragmaLoopHintInfo {
0344 Token PragmaName;
0345 Token Option;
0346 ArrayRef<Token> Toks;
0347 };
0348 }
0349
0350 #endif