clang 22.0.0git
TokenKinds.h
Go to the documentation of this file.
1//===--- TokenKinds.h - Enum values for C Token Kinds -----------*- 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/// \file
10/// Defines the clang::TokenKind enum and support functions.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_TOKENKINDS_H
15#define LLVM_CLANG_BASIC_TOKENKINDS_H
16
17#include "llvm/ADT/DenseMapInfo.h"
18#include "llvm/Support/Compiler.h"
19
20namespace clang {
21
22namespace tok {
23
24/// Provides a simple uniform namespace for tokens from all C languages.
25enum TokenKind : unsigned short {
26#define TOK(X) X,
27#include "clang/Basic/TokenKinds.def"
29};
30
31/// Provides a namespace for preprocessor keywords which start with a
32/// '#' at the beginning of the line.
34#define PPKEYWORD(X) pp_##X,
35#include "clang/Basic/TokenKinds.def"
37};
38
39/// Provides a namespace for Objective-C keywords which start with
40/// an '@'.
42#define OBJC_AT_KEYWORD(X) objc_##X,
43#include "clang/Basic/TokenKinds.def"
45};
46
47/// Provides a namespace for notable identifers such as float_t and
48/// double_t.
50#define NOTABLE_IDENTIFIER(X) X,
51#include "clang/Basic/TokenKinds.def"
53};
54
55/// Defines the possible values of an on-off-switch (C99 6.10.6p2).
59
60/// Determines the name of a token as used within the front end.
61///
62/// The name of a token will be an internal name (such as "l_square")
63/// and should not be used as part of diagnostic messages.
64const char *getTokenName(TokenKind Kind) LLVM_READNONE;
65
66/// Determines the spelling of simple punctuation tokens like
67/// '!' or '%', and returns NULL for literal and annotation tokens.
68///
69/// This routine only retrieves the "simple" spelling of the token,
70/// and will not produce any alternative spellings (e.g., a
71/// digraph). For the actual spelling of a given Token, use
72/// Preprocessor::getSpelling().
73const char *getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE;
74
75/// Determines the spelling of simple keyword and contextual keyword
76/// tokens like 'int' and 'dynamic_cast'. Returns NULL for other token kinds.
77const char *getKeywordSpelling(TokenKind Kind) LLVM_READNONE;
78
79/// Returns the spelling of preprocessor keywords, such as "else".
80const char *getPPKeywordSpelling(PPKeywordKind Kind) LLVM_READNONE;
81
82/// Return true if this is a raw identifier or an identifier kind.
83inline bool isAnyIdentifier(TokenKind K) {
84 return (K == tok::identifier) || (K == tok::raw_identifier);
85}
86
87/// Return true if this is a C or C++ string-literal (or
88/// C++11 user-defined-string-literal) token.
89inline bool isStringLiteral(TokenKind K) {
90 return K == tok::string_literal || K == tok::wide_string_literal ||
91 K == tok::utf8_string_literal || K == tok::utf16_string_literal ||
92 K == tok::utf32_string_literal;
93}
94
95/// Return true if this is a "literal" kind, like a numeric
96/// constant, string, etc.
97inline bool isLiteral(TokenKind K) {
98 const bool isInLiteralRange =
99 K >= tok::numeric_constant && K <= tok::utf32_string_literal;
100
101#if !NDEBUG
102 const bool isLiteralExplicit =
103 K == tok::numeric_constant || K == tok::char_constant ||
104 K == tok::wide_char_constant || K == tok::utf8_char_constant ||
105 K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
106 isStringLiteral(K) || K == tok::header_name || K == tok::binary_data;
107 assert(isInLiteralRange == isLiteralExplicit &&
108 "TokenKind literals should be contiguous");
109#endif
110
111 return isInLiteralRange;
112}
113
114/// Return true if this is any of tok::annot_* kinds.
115bool isAnnotation(TokenKind K);
116
117/// Return true if this is an annotation token representing a pragma.
118bool isPragmaAnnotation(TokenKind K);
119
120inline constexpr bool isRegularKeywordAttribute(TokenKind K) {
121 return (false
122#define KEYWORD_ATTRIBUTE(X, ...) || (K == tok::kw_##X)
123#include "clang/Basic/RegularKeywordAttrInfo.inc"
124 );
125}
126
127} // end namespace tok
128} // end namespace clang
129
130namespace llvm {
131template <> struct DenseMapInfo<clang::tok::PPKeywordKind> {
133 return clang::tok::PPKeywordKind::pp_not_keyword;
134 }
138 static unsigned getHashValue(const clang::tok::PPKeywordKind &Val) {
139 return static_cast<unsigned>(Val);
140 }
141 static bool isEqual(const clang::tok::PPKeywordKind &LHS,
142 const clang::tok::PPKeywordKind &RHS) {
143 return LHS == RHS;
144 }
145};
146} // namespace llvm
147
148#endif
#define KEYWORD_ATTRIBUTE(NAME, HASARG,...)
#define X(type, name)
Definition Value.h:97
bool isStringLiteral(TokenKind K)
Return true if this is a C or C++ string-literal (or C++11 user-defined-string-literal) token.
Definition TokenKinds.h:89
const char * getPPKeywordSpelling(PPKeywordKind Kind) LLVM_READNONE
Returns the spelling of preprocessor keywords, such as "else".
NotableIdentifierKind
Provides a namespace for notable identifers such as float_t and double_t.
Definition TokenKinds.h:49
@ NUM_NOTABLE_IDENTIFIERS
Definition TokenKinds.h:52
const char * getTokenName(TokenKind Kind) LLVM_READNONE
Determines the name of a token as used within the front end.
bool isAnyIdentifier(TokenKind K)
Return true if this is a raw identifier or an identifier kind.
Definition TokenKinds.h:83
const char * getKeywordSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple keyword and contextual keyword tokens like 'int' and 'dynamic_cast'...
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an '@'.
Definition TokenKinds.h:41
@ NUM_OBJC_KEYWORDS
Definition TokenKinds.h:44
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition TokenKinds.h:25
OnOffSwitch
Defines the possible values of an on-off-switch (C99 6.10.6p2).
Definition TokenKinds.h:56
constexpr bool isRegularKeywordAttribute(TokenKind K)
Definition TokenKinds.h:120
bool isPragmaAnnotation(TokenKind K)
Return true if this is an annotation token representing a pragma.
bool isLiteral(TokenKind K)
Return true if this is a "literal" kind, like a numeric constant, string, etc.
Definition TokenKinds.h:97
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
Definition TokenKinds.h:33
@ NUM_PP_KEYWORDS
Definition TokenKinds.h:36
bool isAnnotation(TokenKind K)
Return true if this is any of tok::annot_* kinds.
const char * getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple punctuation tokens like '!
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static clang::tok::PPKeywordKind getEmptyKey()
Definition TokenKinds.h:132
static clang::tok::PPKeywordKind getTombstoneKey()
Definition TokenKinds.h:135
static unsigned getHashValue(const clang::tok::PPKeywordKind &Val)
Definition TokenKinds.h:138
static bool isEqual(const clang::tok::PPKeywordKind &LHS, const clang::tok::PPKeywordKind &RHS)
Definition TokenKinds.h:141