clang 17.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 "Integral.h"
17#include <climits>
18#include <cstddef>
19#include <cstdint>
20
21namespace clang {
22namespace interp {
23
24class Pointer;
25class Boolean;
26class Floating;
27
28/// Enumeration of the primitive types of the VM.
29enum PrimType : unsigned {
41};
42
43/// Mapping from primitive types to their representation.
44template <PrimType T> struct PrimConv;
45template <> struct PrimConv<PT_Sint8> { using T = Integral<8, true>; };
46template <> struct PrimConv<PT_Uint8> { using T = Integral<8, false>; };
47template <> struct PrimConv<PT_Sint16> { using T = Integral<16, true>; };
48template <> struct PrimConv<PT_Uint16> { using T = Integral<16, false>; };
49template <> struct PrimConv<PT_Sint32> { using T = Integral<32, true>; };
50template <> struct PrimConv<PT_Uint32> { using T = Integral<32, false>; };
51template <> struct PrimConv<PT_Sint64> { using T = Integral<64, true>; };
52template <> struct PrimConv<PT_Uint64> { using T = Integral<64, false>; };
53template <> struct PrimConv<PT_Float> { using T = Floating; };
54template <> struct PrimConv<PT_Bool> { using T = Boolean; };
55template <> struct PrimConv<PT_Ptr> { using T = Pointer; };
56
57/// Returns the size of a primitive type in bytes.
58size_t primSize(PrimType Type);
59
60/// Aligns a size to the pointer alignment.
61constexpr size_t align(size_t Size) {
62 return ((Size + alignof(void *) - 1) / alignof(void *)) * alignof(void *);
63}
64
65constexpr bool aligned(uintptr_t Value) { return Value == align(Value); }
66static_assert(aligned(sizeof(void *)));
67
68static inline bool aligned(const void *P) {
69 return aligned(reinterpret_cast<uintptr_t>(P));
70}
71
72} // namespace interp
73} // namespace clang
74
75/// Helper macro to simplify type switches.
76/// The macro implicitly exposes a type T in the scope of the inner block.
77#define TYPE_SWITCH_CASE(Name, B) \
78 case Name: { using T = PrimConv<Name>::T; B; break; }
79#define TYPE_SWITCH(Expr, B) \
80 do { \
81 switch (Expr) { \
82 TYPE_SWITCH_CASE(PT_Sint8, B) \
83 TYPE_SWITCH_CASE(PT_Uint8, B) \
84 TYPE_SWITCH_CASE(PT_Sint16, B) \
85 TYPE_SWITCH_CASE(PT_Uint16, B) \
86 TYPE_SWITCH_CASE(PT_Sint32, B) \
87 TYPE_SWITCH_CASE(PT_Uint32, B) \
88 TYPE_SWITCH_CASE(PT_Sint64, B) \
89 TYPE_SWITCH_CASE(PT_Uint64, B) \
90 TYPE_SWITCH_CASE(PT_Float, B) \
91 TYPE_SWITCH_CASE(PT_Bool, B) \
92 TYPE_SWITCH_CASE(PT_Ptr, B) \
93 } \
94 } while (0)
95#define COMPOSITE_TYPE_SWITCH(Expr, B, D) \
96 do { \
97 switch (Expr) { \
98 TYPE_SWITCH_CASE(PT_Ptr, B) \
99 default: { D; break; } \
100 } \
101 } while (0)
102#endif
StringRef P
The base class of the type hierarchy.
Definition: Type.h:1566
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:61
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:65
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:61
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:29
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:20
__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:44