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 namespace clang {
21 
22 namespace tok {
23 
24 /// Provides a simple uniform namespace for tokens from all C languages.
25 enum TokenKind : unsigned short {
26 #define TOK(X) X,
27 #include "clang/Basic/TokenKinds.def"
28   NUM_TOKENS
29 };
30 
31 /// Provides a namespace for preprocessor keywords which start with a
32 /// '#' at the beginning of the line.
33 enum PPKeywordKind {
34 #define PPKEYWORD(X) pp_##X,
35 #include "clang/Basic/TokenKinds.def"
36   NUM_PP_KEYWORDS
37 };
38 
39 /// Provides a namespace for Objective-C keywords which start with
40 /// an '@'.
41 enum ObjCKeywordKind {
42 #define OBJC_AT_KEYWORD(X) objc_##X,
43 #include "clang/Basic/TokenKinds.def"
44   NUM_OBJC_KEYWORDS
45 };
46 
47 /// Provides a namespace for interesting identifers such as float_t and
48 /// double_t.
49 enum InterestingIdentifierKind {
50 #define INTERESTING_IDENTIFIER(X) X,
51 #include "clang/Basic/TokenKinds.def"
52   NUM_INTERESTING_IDENTIFIERS
53 };
54 
55 /// Defines the possible values of an on-off-switch (C99 6.10.6p2).
56 enum OnOffSwitch {
57   OOS_ON, OOS_OFF, OOS_DEFAULT
58 };
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.
64 const 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().
73 const 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.
77 const char *getKeywordSpelling(TokenKind Kind) LLVM_READNONE;
78 
79 /// Returns the spelling of preprocessor keywords, such as "else".
80 const char *getPPKeywordSpelling(PPKeywordKind Kind) LLVM_READNONE;
81 
82 /// Return true if this is a raw identifier or an identifier kind.
isAnyIdentifier(TokenKind K)83 inline 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.
isStringLiteral(TokenKind K)89 inline 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.
isLiteral(TokenKind K)97 inline bool isLiteral(TokenKind K) {
98   return K == tok::numeric_constant || K == tok::char_constant ||
99          K == tok::wide_char_constant || K == tok::utf8_char_constant ||
100          K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
101          isStringLiteral(K) || K == tok::header_name;
102 }
103 
104 /// Return true if this is any of tok::annot_* kinds.
105 bool isAnnotation(TokenKind K);
106 
107 /// Return true if this is an annotation token representing a pragma.
108 bool isPragmaAnnotation(TokenKind K);
109 
isRegularKeywordAttribute(TokenKind K)110 inline constexpr bool isRegularKeywordAttribute(TokenKind K) {
111   return (false
112 #define KEYWORD_ATTRIBUTE(X, ...) || (K == tok::kw_##X)
113 #include "clang/Basic/RegularKeywordAttrInfo.inc"
114   );
115 }
116 
117 } // end namespace tok
118 } // end namespace clang
119 
120 namespace llvm {
121 template <> struct DenseMapInfo<clang::tok::PPKeywordKind> {
122   static inline clang::tok::PPKeywordKind getEmptyKey() {
123     return clang::tok::PPKeywordKind::pp_not_keyword;
124   }
125   static inline clang::tok::PPKeywordKind getTombstoneKey() {
126     return clang::tok::PPKeywordKind::NUM_PP_KEYWORDS;
127   }
128   static unsigned getHashValue(const clang::tok::PPKeywordKind &Val) {
129     return static_cast<unsigned>(Val);
130   }
131   static bool isEqual(const clang::tok::PPKeywordKind &LHS,
132                       const clang::tok::PPKeywordKind &RHS) {
133     return LHS == RHS;
134   }
135 };
136 } // namespace llvm
137 
138 #endif
139