10b57cec5SDimitry Andric //===--- TokenKinds.h - Enum values for C Token Kinds -----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// Defines the clang::TokenKind enum and support functions.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_CLANG_BASIC_TOKENKINDS_H
150b57cec5SDimitry Andric #define LLVM_CLANG_BASIC_TOKENKINDS_H
160b57cec5SDimitry Andric 
175ffd83dbSDimitry Andric #include "llvm/ADT/DenseMapInfo.h"
180b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace clang {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace tok {
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric /// Provides a simple uniform namespace for tokens from all C languages.
250b57cec5SDimitry Andric enum TokenKind : unsigned short {
260b57cec5SDimitry Andric #define TOK(X) X,
270b57cec5SDimitry Andric #include "clang/Basic/TokenKinds.def"
280b57cec5SDimitry Andric   NUM_TOKENS
290b57cec5SDimitry Andric };
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric /// Provides a namespace for preprocessor keywords which start with a
320b57cec5SDimitry Andric /// '#' at the beginning of the line.
330b57cec5SDimitry Andric enum PPKeywordKind {
340b57cec5SDimitry Andric #define PPKEYWORD(X) pp_##X,
350b57cec5SDimitry Andric #include "clang/Basic/TokenKinds.def"
360b57cec5SDimitry Andric   NUM_PP_KEYWORDS
370b57cec5SDimitry Andric };
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric /// Provides a namespace for Objective-C keywords which start with
400b57cec5SDimitry Andric /// an '@'.
410b57cec5SDimitry Andric enum ObjCKeywordKind {
420b57cec5SDimitry Andric #define OBJC_AT_KEYWORD(X) objc_##X,
430b57cec5SDimitry Andric #include "clang/Basic/TokenKinds.def"
440b57cec5SDimitry Andric   NUM_OBJC_KEYWORDS
450b57cec5SDimitry Andric };
460b57cec5SDimitry Andric 
4706c3fb27SDimitry Andric /// Provides a namespace for interesting identifers such as float_t and
4806c3fb27SDimitry Andric /// double_t.
4906c3fb27SDimitry Andric enum InterestingIdentifierKind {
5006c3fb27SDimitry Andric #define INTERESTING_IDENTIFIER(X) X,
5106c3fb27SDimitry Andric #include "clang/Basic/TokenKinds.def"
5206c3fb27SDimitry Andric   NUM_INTERESTING_IDENTIFIERS
5306c3fb27SDimitry Andric };
5406c3fb27SDimitry Andric 
550b57cec5SDimitry Andric /// Defines the possible values of an on-off-switch (C99 6.10.6p2).
560b57cec5SDimitry Andric enum OnOffSwitch {
570b57cec5SDimitry Andric   OOS_ON, OOS_OFF, OOS_DEFAULT
580b57cec5SDimitry Andric };
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric /// Determines the name of a token as used within the front end.
610b57cec5SDimitry Andric ///
620b57cec5SDimitry Andric /// The name of a token will be an internal name (such as "l_square")
630b57cec5SDimitry Andric /// and should not be used as part of diagnostic messages.
640b57cec5SDimitry Andric const char *getTokenName(TokenKind Kind) LLVM_READNONE;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric /// Determines the spelling of simple punctuation tokens like
670b57cec5SDimitry Andric /// '!' or '%', and returns NULL for literal and annotation tokens.
680b57cec5SDimitry Andric ///
690b57cec5SDimitry Andric /// This routine only retrieves the "simple" spelling of the token,
700b57cec5SDimitry Andric /// and will not produce any alternative spellings (e.g., a
710b57cec5SDimitry Andric /// digraph). For the actual spelling of a given Token, use
720b57cec5SDimitry Andric /// Preprocessor::getSpelling().
730b57cec5SDimitry Andric const char *getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric /// Determines the spelling of simple keyword and contextual keyword
760b57cec5SDimitry Andric /// tokens like 'int' and 'dynamic_cast'. Returns NULL for other token kinds.
770b57cec5SDimitry Andric const char *getKeywordSpelling(TokenKind Kind) LLVM_READNONE;
780b57cec5SDimitry Andric 
7981ad6265SDimitry Andric /// Returns the spelling of preprocessor keywords, such as "else".
8081ad6265SDimitry Andric const char *getPPKeywordSpelling(PPKeywordKind Kind) LLVM_READNONE;
8181ad6265SDimitry Andric 
820b57cec5SDimitry Andric /// Return true if this is a raw identifier or an identifier kind.
isAnyIdentifier(TokenKind K)830b57cec5SDimitry Andric inline bool isAnyIdentifier(TokenKind K) {
840b57cec5SDimitry Andric   return (K == tok::identifier) || (K == tok::raw_identifier);
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric /// Return true if this is a C or C++ string-literal (or
880b57cec5SDimitry Andric /// C++11 user-defined-string-literal) token.
isStringLiteral(TokenKind K)890b57cec5SDimitry Andric inline bool isStringLiteral(TokenKind K) {
900b57cec5SDimitry Andric   return K == tok::string_literal || K == tok::wide_string_literal ||
910b57cec5SDimitry Andric          K == tok::utf8_string_literal || K == tok::utf16_string_literal ||
920b57cec5SDimitry Andric          K == tok::utf32_string_literal;
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric /// Return true if this is a "literal" kind, like a numeric
960b57cec5SDimitry Andric /// constant, string, etc.
isLiteral(TokenKind K)970b57cec5SDimitry Andric inline bool isLiteral(TokenKind K) {
980b57cec5SDimitry Andric   return K == tok::numeric_constant || K == tok::char_constant ||
990b57cec5SDimitry Andric          K == tok::wide_char_constant || K == tok::utf8_char_constant ||
1000b57cec5SDimitry Andric          K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
1010b57cec5SDimitry Andric          isStringLiteral(K) || K == tok::header_name;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric /// Return true if this is any of tok::annot_* kinds.
105a7dea167SDimitry Andric bool isAnnotation(TokenKind K);
106a7dea167SDimitry Andric 
107a7dea167SDimitry Andric /// Return true if this is an annotation token representing a pragma.
108a7dea167SDimitry Andric bool isPragmaAnnotation(TokenKind K);
1090b57cec5SDimitry Andric 
isRegularKeywordAttribute(TokenKind K)11006c3fb27SDimitry Andric inline constexpr bool isRegularKeywordAttribute(TokenKind K) {
11106c3fb27SDimitry Andric   return (false
1127a6dacacSDimitry Andric #define KEYWORD_ATTRIBUTE(X, ...) || (K == tok::kw_##X)
1137a6dacacSDimitry Andric #include "clang/Basic/RegularKeywordAttrInfo.inc"
11406c3fb27SDimitry Andric   );
11506c3fb27SDimitry Andric }
11606c3fb27SDimitry Andric 
1170b57cec5SDimitry Andric } // end namespace tok
1180b57cec5SDimitry Andric } // end namespace clang
1190b57cec5SDimitry Andric 
1205ffd83dbSDimitry Andric namespace llvm {
1215ffd83dbSDimitry Andric template <> struct DenseMapInfo<clang::tok::PPKeywordKind> {
1225ffd83dbSDimitry Andric   static inline clang::tok::PPKeywordKind getEmptyKey() {
1235ffd83dbSDimitry Andric     return clang::tok::PPKeywordKind::pp_not_keyword;
1245ffd83dbSDimitry Andric   }
1255ffd83dbSDimitry Andric   static inline clang::tok::PPKeywordKind getTombstoneKey() {
1265ffd83dbSDimitry Andric     return clang::tok::PPKeywordKind::NUM_PP_KEYWORDS;
1275ffd83dbSDimitry Andric   }
1285ffd83dbSDimitry Andric   static unsigned getHashValue(const clang::tok::PPKeywordKind &Val) {
1295ffd83dbSDimitry Andric     return static_cast<unsigned>(Val);
1305ffd83dbSDimitry Andric   }
1315ffd83dbSDimitry Andric   static bool isEqual(const clang::tok::PPKeywordKind &LHS,
1325ffd83dbSDimitry Andric                       const clang::tok::PPKeywordKind &RHS) {
1335ffd83dbSDimitry Andric     return LHS == RHS;
1345ffd83dbSDimitry Andric   }
1355ffd83dbSDimitry Andric };
1365ffd83dbSDimitry Andric } // namespace llvm
1375ffd83dbSDimitry Andric 
1380b57cec5SDimitry Andric #endif
139