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