clang  20.0.0git
PrimType.h
Go to the documentation of this file.
1 //===--- PrimType.h - Types for the constexpr VM ----------------*- 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 // Defines the VM types and helpers operating on types.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_INTERP_TYPE_H
14 #define LLVM_CLANG_AST_INTERP_TYPE_H
15 
16 #include "llvm/Support/raw_ostream.h"
17 #include <climits>
18 #include <cstddef>
19 #include <cstdint>
20 
21 namespace clang {
22 namespace interp {
23 
24 class Pointer;
25 class Boolean;
26 class Floating;
27 class FunctionPointer;
28 class MemberPointer;
29 template <bool Signed> class IntegralAP;
30 template <unsigned Bits, bool Signed> class Integral;
31 
32 /// Enumeration of the primitive types of the VM.
33 enum PrimType : unsigned {
34  PT_Sint8 = 0,
35  PT_Uint8 = 1,
36  PT_Sint16 = 2,
37  PT_Uint16 = 3,
38  PT_Sint32 = 4,
39  PT_Uint32 = 5,
40  PT_Sint64 = 6,
41  PT_Uint64 = 7,
42  PT_IntAP = 8,
43  PT_IntAPS = 9,
44  PT_Bool = 10,
45  PT_Float = 11,
46  PT_Ptr = 12,
47  PT_FnPtr = 13,
49 };
50 
51 inline constexpr bool isPtrType(PrimType T) {
52  return T == PT_Ptr || T == PT_FnPtr || T == PT_MemberPtr;
53 }
54 
55 enum class CastKind : uint8_t {
57  Atomic,
58 };
59 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
60  interp::CastKind CK) {
61  switch (CK) {
63  OS << "reinterpret_cast";
64  break;
66  OS << "atomic";
67  break;
68  }
69  return OS;
70 }
71 
72 constexpr bool isIntegralType(PrimType T) { return T <= PT_Bool; }
73 
74 /// Mapping from primitive types to their representation.
75 template <PrimType T> struct PrimConv;
76 template <> struct PrimConv<PT_Sint8> {
78 };
79 template <> struct PrimConv<PT_Uint8> {
81 };
82 template <> struct PrimConv<PT_Sint16> {
84 };
85 template <> struct PrimConv<PT_Uint16> {
87 };
88 template <> struct PrimConv<PT_Sint32> {
90 };
91 template <> struct PrimConv<PT_Uint32> {
93 };
94 template <> struct PrimConv<PT_Sint64> {
96 };
97 template <> struct PrimConv<PT_Uint64> {
99 };
100 template <> struct PrimConv<PT_IntAP> {
102 };
103 template <> struct PrimConv<PT_IntAPS> {
105 };
106 template <> struct PrimConv<PT_Float> {
107  using T = Floating;
108 };
109 template <> struct PrimConv<PT_Bool> {
110  using T = Boolean;
111 };
112 template <> struct PrimConv<PT_Ptr> {
113  using T = Pointer;
114 };
115 template <> struct PrimConv<PT_FnPtr> {
117 };
118 template <> struct PrimConv<PT_MemberPtr> {
119  using T = MemberPointer;
120 };
121 
122 /// Returns the size of a primitive type in bytes.
123 size_t primSize(PrimType Type);
124 
125 /// Aligns a size to the pointer alignment.
126 constexpr size_t align(size_t Size) {
127  return ((Size + alignof(void *) - 1) / alignof(void *)) * alignof(void *);
128 }
129 
130 constexpr bool aligned(uintptr_t Value) { return Value == align(Value); }
131 static_assert(aligned(sizeof(void *)));
132 
133 static inline bool aligned(const void *P) {
134  return aligned(reinterpret_cast<uintptr_t>(P));
135 }
136 
137 } // namespace interp
138 } // namespace clang
139 
140 /// Helper macro to simplify type switches.
141 /// The macro implicitly exposes a type T in the scope of the inner block.
142 #define TYPE_SWITCH_CASE(Name, B) \
143  case Name: { \
144  using T = PrimConv<Name>::T; \
145  B; \
146  break; \
147  }
148 #define TYPE_SWITCH(Expr, B) \
149  do { \
150  switch (Expr) { \
151  TYPE_SWITCH_CASE(PT_Sint8, B) \
152  TYPE_SWITCH_CASE(PT_Uint8, B) \
153  TYPE_SWITCH_CASE(PT_Sint16, B) \
154  TYPE_SWITCH_CASE(PT_Uint16, B) \
155  TYPE_SWITCH_CASE(PT_Sint32, B) \
156  TYPE_SWITCH_CASE(PT_Uint32, B) \
157  TYPE_SWITCH_CASE(PT_Sint64, B) \
158  TYPE_SWITCH_CASE(PT_Uint64, B) \
159  TYPE_SWITCH_CASE(PT_IntAP, B) \
160  TYPE_SWITCH_CASE(PT_IntAPS, B) \
161  TYPE_SWITCH_CASE(PT_Float, B) \
162  TYPE_SWITCH_CASE(PT_Bool, B) \
163  TYPE_SWITCH_CASE(PT_Ptr, B) \
164  TYPE_SWITCH_CASE(PT_FnPtr, B) \
165  TYPE_SWITCH_CASE(PT_MemberPtr, B) \
166  } \
167  } while (0)
168 
169 #define INT_TYPE_SWITCH(Expr, B) \
170  do { \
171  switch (Expr) { \
172  TYPE_SWITCH_CASE(PT_Sint8, B) \
173  TYPE_SWITCH_CASE(PT_Uint8, B) \
174  TYPE_SWITCH_CASE(PT_Sint16, B) \
175  TYPE_SWITCH_CASE(PT_Uint16, B) \
176  TYPE_SWITCH_CASE(PT_Sint32, B) \
177  TYPE_SWITCH_CASE(PT_Uint32, B) \
178  TYPE_SWITCH_CASE(PT_Sint64, B) \
179  TYPE_SWITCH_CASE(PT_Uint64, B) \
180  TYPE_SWITCH_CASE(PT_IntAP, B) \
181  TYPE_SWITCH_CASE(PT_IntAPS, B) \
182  TYPE_SWITCH_CASE(PT_Bool, B) \
183  default: \
184  llvm_unreachable("Not an integer value"); \
185  } \
186  } while (0)
187 
188 #define INT_TYPE_SWITCH_NO_BOOL(Expr, B) \
189  do { \
190  switch (Expr) { \
191  TYPE_SWITCH_CASE(PT_Sint8, B) \
192  TYPE_SWITCH_CASE(PT_Uint8, B) \
193  TYPE_SWITCH_CASE(PT_Sint16, B) \
194  TYPE_SWITCH_CASE(PT_Uint16, B) \
195  TYPE_SWITCH_CASE(PT_Sint32, B) \
196  TYPE_SWITCH_CASE(PT_Uint32, B) \
197  TYPE_SWITCH_CASE(PT_Sint64, B) \
198  TYPE_SWITCH_CASE(PT_Uint64, B) \
199  TYPE_SWITCH_CASE(PT_IntAP, B) \
200  TYPE_SWITCH_CASE(PT_IntAPS, B) \
201  default: \
202  llvm_unreachable("Not an integer value"); \
203  } \
204  } while (0)
205 
206 #define COMPOSITE_TYPE_SWITCH(Expr, B, D) \
207  do { \
208  switch (Expr) { \
209  TYPE_SWITCH_CASE(PT_Ptr, B) \
210  default: { \
211  D; \
212  break; \
213  } \
214  } \
215  } while (0)
216 #endif
StringRef P
The base class of the type hierarchy.
Definition: Type.h:1829
Wrapper around boolean types.
Definition: Boolean.h:25
Wrapper around numeric types.
Definition: Integral.h:66
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:130
constexpr bool isPtrType(PrimType T)
Definition: PrimType.h:51
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:126
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:23
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:151
constexpr bool isIntegralType(PrimType T)
Definition: PrimType.h:72
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Mapping from primitive types to their representation.
Definition: PrimType.h:75