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