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