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