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