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
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.
34enum PrimType : unsigned {
45 PT_Bool = 10,
48 PT_Ptr = 13,
51};
52
53inline constexpr bool isPtrType(PrimType T) {
54 return T == PT_Ptr || T == PT_FnPtr || T == PT_MemberPtr;
55}
56
57enum class CastKind : uint8_t {
59 Atomic,
60};
61inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
63 switch (CK) {
65 OS << "reinterpret_cast";
66 break;
68 OS << "atomic";
69 break;
70 }
71 return OS;
72}
73
74constexpr bool isIntegralType(PrimType T) { return T <= PT_FixedPoint; }
75
76/// Mapping from primitive types to their representation.
77template <PrimType T> struct PrimConv;
78template <> struct PrimConv<PT_Sint8> {
80};
81template <> struct PrimConv<PT_Uint8> {
83};
84template <> struct PrimConv<PT_Sint16> {
86};
87template <> struct PrimConv<PT_Uint16> {
89};
90template <> struct PrimConv<PT_Sint32> {
92};
93template <> struct PrimConv<PT_Uint32> {
95};
96template <> struct PrimConv<PT_Sint64> {
98};
99template <> struct PrimConv<PT_Uint64> {
101};
102template <> struct PrimConv<PT_IntAP> {
104};
105template <> struct PrimConv<PT_IntAPS> {
107};
108template <> struct PrimConv<PT_Float> {
109 using T = Floating;
110};
111template <> struct PrimConv<PT_Bool> {
112 using T = Boolean;
113};
114template <> struct PrimConv<PT_Ptr> {
115 using T = Pointer;
116};
117template <> struct PrimConv<PT_FnPtr> {
119};
120template <> struct PrimConv<PT_MemberPtr> {
122};
123template <> struct PrimConv<PT_FixedPoint> {
124 using T = FixedPoint;
125};
126
127/// Returns the size of a primitive type in bytes.
128size_t primSize(PrimType Type);
129
130/// Aligns a size to the pointer alignment.
131constexpr size_t align(size_t Size) {
132 return ((Size + alignof(void *) - 1) / alignof(void *)) * alignof(void *);
133}
134
135constexpr bool aligned(uintptr_t Value) { return Value == align(Value); }
136static_assert(aligned(sizeof(void *)));
137
138static inline bool aligned(const void *P) {
139 return aligned(reinterpret_cast<uintptr_t>(P));
140}
141
142} // namespace interp
143} // namespace clang
144
145/// Helper macro to simplify type switches.
146/// The macro implicitly exposes a type T in the scope of the inner block.
147#define TYPE_SWITCH_CASE(Name, B) \
148 case Name: { \
149 using T = PrimConv<Name>::T; \
150 B; \
151 break; \
152 }
153#define TYPE_SWITCH(Expr, B) \
154 do { \
155 switch (Expr) { \
156 TYPE_SWITCH_CASE(PT_Sint8, B) \
157 TYPE_SWITCH_CASE(PT_Uint8, B) \
158 TYPE_SWITCH_CASE(PT_Sint16, B) \
159 TYPE_SWITCH_CASE(PT_Uint16, B) \
160 TYPE_SWITCH_CASE(PT_Sint32, B) \
161 TYPE_SWITCH_CASE(PT_Uint32, B) \
162 TYPE_SWITCH_CASE(PT_Sint64, B) \
163 TYPE_SWITCH_CASE(PT_Uint64, B) \
164 TYPE_SWITCH_CASE(PT_IntAP, B) \
165 TYPE_SWITCH_CASE(PT_IntAPS, B) \
166 TYPE_SWITCH_CASE(PT_Float, B) \
167 TYPE_SWITCH_CASE(PT_Bool, B) \
168 TYPE_SWITCH_CASE(PT_Ptr, B) \
169 TYPE_SWITCH_CASE(PT_FnPtr, B) \
170 TYPE_SWITCH_CASE(PT_MemberPtr, B) \
171 TYPE_SWITCH_CASE(PT_FixedPoint, B) \
172 } \
173 } while (0)
174
175#define INT_TYPE_SWITCH(Expr, B) \
176 do { \
177 switch (Expr) { \
178 TYPE_SWITCH_CASE(PT_Sint8, B) \
179 TYPE_SWITCH_CASE(PT_Uint8, B) \
180 TYPE_SWITCH_CASE(PT_Sint16, B) \
181 TYPE_SWITCH_CASE(PT_Uint16, B) \
182 TYPE_SWITCH_CASE(PT_Sint32, B) \
183 TYPE_SWITCH_CASE(PT_Uint32, B) \
184 TYPE_SWITCH_CASE(PT_Sint64, B) \
185 TYPE_SWITCH_CASE(PT_Uint64, B) \
186 TYPE_SWITCH_CASE(PT_IntAP, B) \
187 TYPE_SWITCH_CASE(PT_IntAPS, B) \
188 TYPE_SWITCH_CASE(PT_Bool, B) \
189 default: \
190 llvm_unreachable("Not an integer value"); \
191 } \
192 } while (0)
193
194#define INT_TYPE_SWITCH_NO_BOOL(Expr, B) \
195 do { \
196 switch (Expr) { \
197 TYPE_SWITCH_CASE(PT_Sint8, B) \
198 TYPE_SWITCH_CASE(PT_Uint8, B) \
199 TYPE_SWITCH_CASE(PT_Sint16, B) \
200 TYPE_SWITCH_CASE(PT_Uint16, B) \
201 TYPE_SWITCH_CASE(PT_Sint32, B) \
202 TYPE_SWITCH_CASE(PT_Uint32, B) \
203 TYPE_SWITCH_CASE(PT_Sint64, B) \
204 TYPE_SWITCH_CASE(PT_Uint64, B) \
205 TYPE_SWITCH_CASE(PT_IntAP, B) \
206 TYPE_SWITCH_CASE(PT_IntAPS, B) \
207 default: \
208 llvm_unreachable("Not an integer value"); \
209 } \
210 } while (0)
211
212#define COMPOSITE_TYPE_SWITCH(Expr, B, D) \
213 do { \
214 switch (Expr) { \
215 TYPE_SWITCH_CASE(PT_Ptr, B) \
216 default: { \
217 D; \
218 break; \
219 } \
220 } \
221 } while (0)
222#endif
StringRef P
The base class of the type hierarchy.
Definition: Type.h:1828
Wrapper around boolean types.
Definition: Boolean.h:25
Wrapper around fixed point types.
Definition: FixedPoint.h:23
Wrapper around numeric types.
Definition: Integral.h:66
A pointer to a memory block, live or dead.
Definition: Pointer.h:88
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:135
constexpr bool isPtrType(PrimType T)
Definition: PrimType.h:53
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:131
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:160
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:74
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:77