clang 18.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;
28template <unsigned Bits, bool Signed> class Integral;
29
30/// Enumeration of the primitive types of the VM.
31enum PrimType : unsigned {
44};
45
46enum class CastKind : uint8_t {
48};
49inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
51 switch (CK) {
53 OS << "reinterpret_cast";
54 break;
55 }
56 return OS;
57}
58
59constexpr bool isIntegralType(PrimType T) { return T <= PT_Bool; }
60
61/// Mapping from primitive types to their representation.
62template <PrimType T> struct PrimConv;
63template <> struct PrimConv<PT_Sint8> { using T = Integral<8, true>; };
64template <> struct PrimConv<PT_Uint8> { using T = Integral<8, false>; };
65template <> struct PrimConv<PT_Sint16> { using T = Integral<16, true>; };
66template <> struct PrimConv<PT_Uint16> { using T = Integral<16, false>; };
67template <> struct PrimConv<PT_Sint32> { using T = Integral<32, true>; };
68template <> struct PrimConv<PT_Uint32> { using T = Integral<32, false>; };
69template <> struct PrimConv<PT_Sint64> { using T = Integral<64, true>; };
70template <> struct PrimConv<PT_Uint64> { using T = Integral<64, false>; };
71template <> struct PrimConv<PT_Float> { using T = Floating; };
72template <> struct PrimConv<PT_Bool> { using T = Boolean; };
73template <> struct PrimConv<PT_Ptr> { using T = Pointer; };
74template <> struct PrimConv<PT_FnPtr> {
76};
77
78/// Returns the size of a primitive type in bytes.
79size_t primSize(PrimType Type);
80
81/// Aligns a size to the pointer alignment.
82constexpr size_t align(size_t Size) {
83 return ((Size + alignof(void *) - 1) / alignof(void *)) * alignof(void *);
84}
85
86constexpr bool aligned(uintptr_t Value) { return Value == align(Value); }
87static_assert(aligned(sizeof(void *)));
88
89static inline bool aligned(const void *P) {
90 return aligned(reinterpret_cast<uintptr_t>(P));
91}
92
93} // namespace interp
94} // namespace clang
95
96/// Helper macro to simplify type switches.
97/// The macro implicitly exposes a type T in the scope of the inner block.
98#define TYPE_SWITCH_CASE(Name, B) \
99 case Name: { using T = PrimConv<Name>::T; B; break; }
100#define TYPE_SWITCH(Expr, B) \
101 do { \
102 switch (Expr) { \
103 TYPE_SWITCH_CASE(PT_Sint8, B) \
104 TYPE_SWITCH_CASE(PT_Uint8, B) \
105 TYPE_SWITCH_CASE(PT_Sint16, B) \
106 TYPE_SWITCH_CASE(PT_Uint16, B) \
107 TYPE_SWITCH_CASE(PT_Sint32, B) \
108 TYPE_SWITCH_CASE(PT_Uint32, B) \
109 TYPE_SWITCH_CASE(PT_Sint64, B) \
110 TYPE_SWITCH_CASE(PT_Uint64, B) \
111 TYPE_SWITCH_CASE(PT_Float, B) \
112 TYPE_SWITCH_CASE(PT_Bool, B) \
113 TYPE_SWITCH_CASE(PT_Ptr, B) \
114 TYPE_SWITCH_CASE(PT_FnPtr, B) \
115 } \
116 } while (0)
117
118#define INT_TYPE_SWITCH(Expr, B) \
119 do { \
120 switch (Expr) { \
121 TYPE_SWITCH_CASE(PT_Sint8, B) \
122 TYPE_SWITCH_CASE(PT_Uint8, B) \
123 TYPE_SWITCH_CASE(PT_Sint16, B) \
124 TYPE_SWITCH_CASE(PT_Uint16, B) \
125 TYPE_SWITCH_CASE(PT_Sint32, B) \
126 TYPE_SWITCH_CASE(PT_Uint32, B) \
127 TYPE_SWITCH_CASE(PT_Sint64, B) \
128 TYPE_SWITCH_CASE(PT_Uint64, B) \
129 TYPE_SWITCH_CASE(PT_Bool, B) \
130 default: \
131 llvm_unreachable("Not an integer value"); \
132 } \
133 } while (0)
134
135#define COMPOSITE_TYPE_SWITCH(Expr, B, D) \
136 do { \
137 switch (Expr) { \
138 TYPE_SWITCH_CASE(PT_Ptr, B) \
139 default: { D; break; } \
140 } \
141 } while (0)
142#endif
StringRef P
The base class of the type hierarchy.
Definition: Type.h:1597
Wrapper around boolean types.
Definition: Boolean.h:25
Wrapper around numeric types.
Definition: Integral.h:48
A pointer to a memory block, live or dead.
Definition: Pointer.h:65
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:86
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:82
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:31
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:155
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:21
constexpr bool isIntegralType(PrimType T)
Definition: PrimType.h:59
__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:62