| 1 | //===--- TypeTraits.cpp - Type Traits Support -----------------------------===// |
| 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 | // This file implements the type traits support functions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/Basic/TypeTraits.h" |
| 14 | #include <cassert> |
| 15 | #include <cstring> |
| 16 | using namespace clang; |
| 17 | |
| 18 | static constexpr const char *TypeTraitNames[] = { |
| 19 | #define TYPE_TRAIT_1(Spelling, Name, Key) #Name, |
| 20 | #include "clang/Basic/TokenKinds.def" |
| 21 | #define TYPE_TRAIT_2(Spelling, Name, Key) #Name, |
| 22 | #include "clang/Basic/TokenKinds.def" |
| 23 | #define TYPE_TRAIT_N(Spelling, Name, Key) #Name, |
| 24 | #include "clang/Basic/TokenKinds.def" |
| 25 | }; |
| 26 | |
| 27 | static constexpr const char *TypeTraitSpellings[] = { |
| 28 | #define TYPE_TRAIT_1(Spelling, Name, Key) #Spelling, |
| 29 | #include "clang/Basic/TokenKinds.def" |
| 30 | #define TYPE_TRAIT_2(Spelling, Name, Key) #Spelling, |
| 31 | #include "clang/Basic/TokenKinds.def" |
| 32 | #define TYPE_TRAIT_N(Spelling, Name, Key) #Spelling, |
| 33 | #include "clang/Basic/TokenKinds.def" |
| 34 | }; |
| 35 | |
| 36 | static constexpr const char *ArrayTypeTraitNames[] = { |
| 37 | #define ARRAY_TYPE_TRAIT(Spelling, Name, Key) #Name, |
| 38 | #include "clang/Basic/TokenKinds.def" |
| 39 | }; |
| 40 | |
| 41 | static constexpr const char *ArrayTypeTraitSpellings[] = { |
| 42 | #define ARRAY_TYPE_TRAIT(Spelling, Name, Key) #Spelling, |
| 43 | #include "clang/Basic/TokenKinds.def" |
| 44 | }; |
| 45 | |
| 46 | static constexpr const char *UnaryExprOrTypeTraitNames[] = { |
| 47 | #define UNARY_EXPR_OR_TYPE_TRAIT(Spelling, Name, Key) #Name, |
| 48 | #define CXX11_UNARY_EXPR_OR_TYPE_TRAIT(Spelling, Name, Key) #Name, |
| 49 | #include "clang/Basic/TokenKinds.def" |
| 50 | }; |
| 51 | |
| 52 | static constexpr const char *UnaryExprOrTypeTraitSpellings[] = { |
| 53 | #define UNARY_EXPR_OR_TYPE_TRAIT(Spelling, Name, Key) #Spelling, |
| 54 | #define CXX11_UNARY_EXPR_OR_TYPE_TRAIT(Spelling, Name, Key) #Spelling, |
| 55 | #include "clang/Basic/TokenKinds.def" |
| 56 | }; |
| 57 | |
| 58 | static constexpr const unsigned TypeTraitArities[] = { |
| 59 | #define TYPE_TRAIT_1(Spelling, Name, Key) 1, |
| 60 | #include "clang/Basic/TokenKinds.def" |
| 61 | #define TYPE_TRAIT_2(Spelling, Name, Key) 2, |
| 62 | #include "clang/Basic/TokenKinds.def" |
| 63 | #define TYPE_TRAIT_N(Spelling, Name, Key) 0, |
| 64 | #include "clang/Basic/TokenKinds.def" |
| 65 | }; |
| 66 | |
| 67 | const char *clang::getTraitName(TypeTrait T) { |
| 68 | assert(T <= TT_Last && "invalid enum value!" ); |
| 69 | return TypeTraitNames[T]; |
| 70 | } |
| 71 | |
| 72 | const char *clang::getTraitName(ArrayTypeTrait T) { |
| 73 | assert(T <= ATT_Last && "invalid enum value!" ); |
| 74 | return ArrayTypeTraitNames[T]; |
| 75 | } |
| 76 | |
| 77 | const char *clang::getTraitName(UnaryExprOrTypeTrait T) { |
| 78 | assert(T <= UETT_Last && "invalid enum value!" ); |
| 79 | return UnaryExprOrTypeTraitNames[T]; |
| 80 | } |
| 81 | |
| 82 | const char *clang::getTraitSpelling(TypeTrait T) { |
| 83 | assert(T <= TT_Last && "invalid enum value!" ); |
| 84 | if (T == BTT_IsDeducible) { |
| 85 | // The __is_deducible is an internal-only type trait. To hide it from |
| 86 | // external users, we define it with an empty spelling name, preventing the |
| 87 | // clang parser from recognizing its token kind. |
| 88 | // However, other components such as the AST dump still require the real |
| 89 | // type trait name. Therefore, we return the real name when needed. |
| 90 | assert(std::strlen(TypeTraitSpellings[T]) == 0); |
| 91 | return "__is_deducible" ; |
| 92 | } |
| 93 | return TypeTraitSpellings[T]; |
| 94 | } |
| 95 | |
| 96 | const char *clang::getTraitSpelling(ArrayTypeTrait T) { |
| 97 | assert(T <= ATT_Last && "invalid enum value!" ); |
| 98 | return ArrayTypeTraitSpellings[T]; |
| 99 | } |
| 100 | |
| 101 | const char *clang::getTraitSpelling(UnaryExprOrTypeTrait T) { |
| 102 | assert(T <= UETT_Last && "invalid enum value!" ); |
| 103 | return UnaryExprOrTypeTraitSpellings[T]; |
| 104 | } |
| 105 | |
| 106 | unsigned clang::getTypeTraitArity(TypeTrait T) { |
| 107 | assert(T <= TT_Last && "invalid enum value!" ); |
| 108 | return TypeTraitArities[T]; |
| 109 | } |
| 110 | |