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 
470b57cec5SDimitry Andric /// Defines the possible values of an on-off-switch (C99 6.10.6p2).
480b57cec5SDimitry Andric enum OnOffSwitch {
490b57cec5SDimitry Andric   OOS_ON, OOS_OFF, OOS_DEFAULT
500b57cec5SDimitry Andric };
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric /// Determines the name of a token as used within the front end.
530b57cec5SDimitry Andric ///
540b57cec5SDimitry Andric /// The name of a token will be an internal name (such as "l_square")
550b57cec5SDimitry Andric /// and should not be used as part of diagnostic messages.
560b57cec5SDimitry Andric const char *getTokenName(TokenKind Kind) LLVM_READNONE;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric /// Determines the spelling of simple punctuation tokens like
590b57cec5SDimitry Andric /// '!' or '%', and returns NULL for literal and annotation tokens.
600b57cec5SDimitry Andric ///
610b57cec5SDimitry Andric /// This routine only retrieves the "simple" spelling of the token,
620b57cec5SDimitry Andric /// and will not produce any alternative spellings (e.g., a
630b57cec5SDimitry Andric /// digraph). For the actual spelling of a given Token, use
640b57cec5SDimitry Andric /// Preprocessor::getSpelling().
650b57cec5SDimitry Andric const char *getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric /// Determines the spelling of simple keyword and contextual keyword
680b57cec5SDimitry Andric /// tokens like 'int' and 'dynamic_cast'. Returns NULL for other token kinds.
690b57cec5SDimitry Andric const char *getKeywordSpelling(TokenKind Kind) LLVM_READNONE;
700b57cec5SDimitry Andric 
7181ad6265SDimitry Andric /// Returns the spelling of preprocessor keywords, such as "else".
7281ad6265SDimitry Andric const char *getPPKeywordSpelling(PPKeywordKind Kind) LLVM_READNONE;
7381ad6265SDimitry Andric 
740b57cec5SDimitry Andric /// Return true if this is a raw identifier or an identifier kind.
750b57cec5SDimitry Andric inline bool isAnyIdentifier(TokenKind K) {
760b57cec5SDimitry Andric   return (K == tok::identifier) || (K == tok::raw_identifier);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric /// Return true if this is a C or C++ string-literal (or
800b57cec5SDimitry Andric /// C++11 user-defined-string-literal) token.
810b57cec5SDimitry Andric inline bool isStringLiteral(TokenKind K) {
820b57cec5SDimitry Andric   return K == tok::string_literal || K == tok::wide_string_literal ||
830b57cec5SDimitry Andric          K == tok::utf8_string_literal || K == tok::utf16_string_literal ||
840b57cec5SDimitry Andric          K == tok::utf32_string_literal;
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric /// Return true if this is a "literal" kind, like a numeric
880b57cec5SDimitry Andric /// constant, string, etc.
890b57cec5SDimitry Andric inline bool isLiteral(TokenKind K) {
900b57cec5SDimitry Andric   return K == tok::numeric_constant || K == tok::char_constant ||
910b57cec5SDimitry Andric          K == tok::wide_char_constant || K == tok::utf8_char_constant ||
920b57cec5SDimitry Andric          K == tok::utf16_char_constant || K == tok::utf32_char_constant ||
930b57cec5SDimitry Andric          isStringLiteral(K) || K == tok::header_name;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric /// Return true if this is any of tok::annot_* kinds.
97a7dea167SDimitry Andric bool isAnnotation(TokenKind K);
98a7dea167SDimitry Andric 
99a7dea167SDimitry Andric /// Return true if this is an annotation token representing a pragma.
100a7dea167SDimitry Andric bool isPragmaAnnotation(TokenKind K);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric } // end namespace tok
1030b57cec5SDimitry Andric } // end namespace clang
1040b57cec5SDimitry Andric 
1055ffd83dbSDimitry Andric namespace llvm {
1065ffd83dbSDimitry Andric template <> struct DenseMapInfo<clang::tok::PPKeywordKind> {
1075ffd83dbSDimitry Andric   static inline clang::tok::PPKeywordKind getEmptyKey() {
1085ffd83dbSDimitry Andric     return clang::tok::PPKeywordKind::pp_not_keyword;
1095ffd83dbSDimitry Andric   }
1105ffd83dbSDimitry Andric   static inline clang::tok::PPKeywordKind getTombstoneKey() {
1115ffd83dbSDimitry Andric     return clang::tok::PPKeywordKind::NUM_PP_KEYWORDS;
1125ffd83dbSDimitry Andric   }
1135ffd83dbSDimitry Andric   static unsigned getHashValue(const clang::tok::PPKeywordKind &Val) {
1145ffd83dbSDimitry Andric     return static_cast<unsigned>(Val);
1155ffd83dbSDimitry Andric   }
1165ffd83dbSDimitry Andric   static bool isEqual(const clang::tok::PPKeywordKind &LHS,
1175ffd83dbSDimitry Andric                       const clang::tok::PPKeywordKind &RHS) {
1185ffd83dbSDimitry Andric     return LHS == RHS;
1195ffd83dbSDimitry Andric   }
1205ffd83dbSDimitry Andric };
1215ffd83dbSDimitry Andric } // namespace llvm
1225ffd83dbSDimitry Andric 
1230b57cec5SDimitry Andric #endif
124