clang 22.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
21namespace clang {
22namespace interp {
23
24class Pointer;
25class Boolean;
26class Floating;
27class FunctionPointer;
28class MemberPointer;
29class FixedPoint;
30template <bool Signed> class IntegralAP;
31template <unsigned Bits, bool Signed> class Integral;
32
33/// Enumeration of the primitive types of the VM.
51
52// Like std::optional<PrimType>, but only sizeof(PrimType).
53class OptPrimType final {
54 static constexpr uint8_t None = 0xFF;
55 uint8_t V = None;
56
57public:
58 OptPrimType() = default;
59 OptPrimType(std::nullopt_t) {}
60 OptPrimType(PrimType T) : V(static_cast<unsigned>(T)) {}
61
62 explicit constexpr operator bool() const { return V != None; }
64 assert(operator bool());
65 return static_cast<PrimType>(V);
66 }
67
69 if (operator bool())
70 return static_cast<PrimType>(V);
71 return PT;
72 }
73
74 bool operator==(PrimType PT) const {
75 if (!operator bool())
76 return false;
77 return V == static_cast<unsigned>(PT);
78 }
79 bool operator==(OptPrimType OPT) const { return V == OPT.V; }
80 bool operator!=(PrimType PT) const { return !(*this == PT); }
81 bool operator!=(OptPrimType OPT) const { return V != OPT.V; }
82};
83static_assert(sizeof(OptPrimType) == sizeof(PrimType));
84
85inline constexpr bool isPtrType(PrimType T) {
86 return T == PT_Ptr || T == PT_MemberPtr;
87}
88
89inline constexpr bool isSignedType(PrimType T) {
90 switch (T) {
91 case PT_Sint8:
92 case PT_Sint16:
93 case PT_Sint32:
94 case PT_Sint64:
95 return true;
96 default:
97 return false;
98 }
99 return false;
100}
101
108
109inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
110 interp::CastKind CK) {
111 switch (CK) {
113 OS << "reinterpret_cast";
114 break;
116 OS << "reinterpret_like";
117 break;
119 OS << "volatile";
120 break;
122 OS << "dynamic";
123 break;
124 }
125 return OS;
126}
127
128constexpr bool isIntegralType(PrimType T) { return T <= PT_FixedPoint; }
129template <typename T> constexpr bool needsAlloc() {
130 return std::is_same_v<T, IntegralAP<false>> ||
131 std::is_same_v<T, IntegralAP<true>> || std::is_same_v<T, Floating>;
132}
133constexpr bool needsAlloc(PrimType T) {
134 return T == PT_IntAP || T == PT_IntAPS || T == PT_Float;
135}
136
137/// Mapping from primitive types to their representation.
138template <PrimType T> struct PrimConv;
139template <> struct PrimConv<PT_Sint8> {
141};
142template <> struct PrimConv<PT_Uint8> {
144};
145template <> struct PrimConv<PT_Sint16> {
147};
148template <> struct PrimConv<PT_Uint16> {
150};
151template <> struct PrimConv<PT_Sint32> {
153};
154template <> struct PrimConv<PT_Uint32> {
156};
157template <> struct PrimConv<PT_Sint64> {
159};
160template <> struct PrimConv<PT_Uint64> {
162};
163template <> struct PrimConv<PT_IntAP> {
165};
166template <> struct PrimConv<PT_IntAPS> {
168};
169template <> struct PrimConv<PT_Float> {
170 using T = Floating;
171};
172template <> struct PrimConv<PT_Bool> {
173 using T = Boolean;
174};
175template <> struct PrimConv<PT_Ptr> {
176 using T = Pointer;
177};
178template <> struct PrimConv<PT_MemberPtr> {
180};
181template <> struct PrimConv<PT_FixedPoint> {
182 using T = FixedPoint;
183};
184
185/// Returns the size of a primitive type in bytes.
186size_t primSize(PrimType Type);
187
188/// Aligns a size to the pointer alignment.
189constexpr size_t align(size_t Size) {
190 return ((Size + alignof(void *) - 1) / alignof(void *)) * alignof(void *);
191}
192
193constexpr bool aligned(uintptr_t Value) { return Value == align(Value); }
194static_assert(aligned(sizeof(void *)));
195
196static inline bool aligned(const void *P) {
197 return aligned(reinterpret_cast<uintptr_t>(P));
198}
199
200} // namespace interp
201} // namespace clang
202
203/// Helper macro to simplify type switches.
204/// The macro implicitly exposes a type T in the scope of the inner block.
205#define TYPE_SWITCH_CASE(Name, B) \
206 case Name: { \
207 using T = PrimConv<Name>::T; \
208 B; \
209 break; \
210 }
211#define TYPE_SWITCH(Expr, B) \
212 do { \
213 switch (Expr) { \
214 TYPE_SWITCH_CASE(PT_Sint8, B) \
215 TYPE_SWITCH_CASE(PT_Uint8, B) \
216 TYPE_SWITCH_CASE(PT_Sint16, B) \
217 TYPE_SWITCH_CASE(PT_Uint16, B) \
218 TYPE_SWITCH_CASE(PT_Sint32, B) \
219 TYPE_SWITCH_CASE(PT_Uint32, B) \
220 TYPE_SWITCH_CASE(PT_Sint64, B) \
221 TYPE_SWITCH_CASE(PT_Uint64, B) \
222 TYPE_SWITCH_CASE(PT_IntAP, B) \
223 TYPE_SWITCH_CASE(PT_IntAPS, B) \
224 TYPE_SWITCH_CASE(PT_Float, B) \
225 TYPE_SWITCH_CASE(PT_Bool, B) \
226 TYPE_SWITCH_CASE(PT_Ptr, B) \
227 TYPE_SWITCH_CASE(PT_MemberPtr, B) \
228 TYPE_SWITCH_CASE(PT_FixedPoint, B) \
229 } \
230 } while (0)
231
232#define INT_TYPE_SWITCH(Expr, B) \
233 do { \
234 switch (Expr) { \
235 TYPE_SWITCH_CASE(PT_Sint8, B) \
236 TYPE_SWITCH_CASE(PT_Uint8, B) \
237 TYPE_SWITCH_CASE(PT_Sint16, B) \
238 TYPE_SWITCH_CASE(PT_Uint16, B) \
239 TYPE_SWITCH_CASE(PT_Sint32, B) \
240 TYPE_SWITCH_CASE(PT_Uint32, B) \
241 TYPE_SWITCH_CASE(PT_Sint64, B) \
242 TYPE_SWITCH_CASE(PT_Uint64, B) \
243 TYPE_SWITCH_CASE(PT_IntAP, B) \
244 TYPE_SWITCH_CASE(PT_IntAPS, B) \
245 TYPE_SWITCH_CASE(PT_Bool, B) \
246 default: \
247 llvm_unreachable("Not an integer value"); \
248 } \
249 } while (0)
250
251#define INT_TYPE_SWITCH_NO_BOOL(Expr, B) \
252 do { \
253 switch (Expr) { \
254 TYPE_SWITCH_CASE(PT_Sint8, B) \
255 TYPE_SWITCH_CASE(PT_Uint8, B) \
256 TYPE_SWITCH_CASE(PT_Sint16, B) \
257 TYPE_SWITCH_CASE(PT_Uint16, B) \
258 TYPE_SWITCH_CASE(PT_Sint32, B) \
259 TYPE_SWITCH_CASE(PT_Uint32, B) \
260 TYPE_SWITCH_CASE(PT_Sint64, B) \
261 TYPE_SWITCH_CASE(PT_Uint64, B) \
262 TYPE_SWITCH_CASE(PT_IntAP, B) \
263 TYPE_SWITCH_CASE(PT_IntAPS, B) \
264 default: \
265 llvm_unreachable("Not an integer value"); \
266 } \
267 } while (0)
268
269#define TYPE_SWITCH_ALLOC(Expr, B) \
270 do { \
271 switch (Expr) { \
272 TYPE_SWITCH_CASE(PT_Float, B) \
273 TYPE_SWITCH_CASE(PT_IntAP, B) \
274 TYPE_SWITCH_CASE(PT_IntAPS, B) \
275 default:; \
276 } \
277 } while (0)
278
279#endif
The base class of the type hierarchy.
Definition TypeBase.h:1833
Wrapper around boolean types.
Definition Boolean.h:25
Wrapper around fixed point types.
Definition FixedPoint.h:23
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
If an IntegralAP is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition IntegralAP.h:36
Wrapper around numeric types.
Definition Integral.h:66
OptPrimType(std::nullopt_t)
Definition PrimType.h:59
bool operator!=(PrimType PT) const
Definition PrimType.h:80
PrimType operator*() const
Definition PrimType.h:63
bool operator==(OptPrimType OPT) const
Definition PrimType.h:79
bool operator!=(OptPrimType OPT) const
Definition PrimType.h:81
PrimType value_or(PrimType PT) const
Definition PrimType.h:68
bool operator==(PrimType PT) const
Definition PrimType.h:74
A pointer to a memory block, live or dead.
Definition Pointer.h:91
#define bool
Definition gpuintrin.h:32
constexpr bool isSignedType(PrimType T)
Definition PrimType.h:89
constexpr bool aligned(uintptr_t Value)
Definition PrimType.h:193
constexpr bool isPtrType(PrimType T)
Definition PrimType.h:85
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:189
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition Boolean.h:154
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
constexpr bool needsAlloc()
Definition PrimType.h:129
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:23
constexpr bool isIntegralType(PrimType T)
Definition PrimType.h:128
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:138