clang 19.0.0git
Descriptor.cpp
Go to the documentation of this file.
1//===--- Descriptor.cpp - 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#include "Descriptor.h"
10#include "Boolean.h"
11#include "Floating.h"
12#include "FunctionPointer.h"
13#include "IntegralAP.h"
14#include "Pointer.h"
15#include "PrimType.h"
16#include "Record.h"
17
18using namespace clang;
19using namespace clang::interp;
20
21template <typename T>
22static void ctorTy(Block *, std::byte *Ptr, bool, bool, bool,
23 const Descriptor *) {
24 new (Ptr) T();
25}
26
27template <typename T>
28static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
29 reinterpret_cast<T *>(Ptr)->~T();
30}
31
32template <typename T>
33static void moveTy(Block *, const std::byte *Src, std::byte *Dst,
34 const Descriptor *) {
35 const auto *SrcPtr = reinterpret_cast<const T *>(Src);
36 auto *DstPtr = reinterpret_cast<T *>(Dst);
37 new (DstPtr) T(std::move(*SrcPtr));
38}
39
40template <typename T>
41static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool,
42 const Descriptor *D) {
43 new (Ptr) InitMapPtr(std::nullopt);
44
45 Ptr += sizeof(InitMapPtr);
46 for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
47 new (&reinterpret_cast<T *>(Ptr)[I]) T();
48 }
49}
50
51template <typename T>
52static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
53 InitMapPtr &IMP = *reinterpret_cast<InitMapPtr *>(Ptr);
54
55 if (IMP)
56 IMP = std::nullopt;
57 Ptr += sizeof(InitMapPtr);
58 for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
59 reinterpret_cast<T *>(Ptr)[I].~T();
60 }
61}
62
63template <typename T>
64static void moveArrayTy(Block *, const std::byte *Src, std::byte *Dst,
65 const Descriptor *D) {
66 // FIXME: Need to copy the InitMap?
67 Src += sizeof(InitMapPtr);
68 Dst += sizeof(InitMapPtr);
69 for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
70 const auto *SrcPtr = &reinterpret_cast<const T *>(Src)[I];
71 auto *DstPtr = &reinterpret_cast<T *>(Dst)[I];
72 new (DstPtr) T(std::move(*SrcPtr));
73 }
74}
75
76static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst,
77 bool IsMutable, bool IsActive, const Descriptor *D) {
78 const unsigned NumElems = D->getNumElems();
79 const unsigned ElemSize =
81
82 unsigned ElemOffset = 0;
83 for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
84 auto *ElemPtr = Ptr + ElemOffset;
85 auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
86 auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
87 auto *SD = D->ElemDesc;
88
89 Desc->Offset = ElemOffset + sizeof(InlineDescriptor);
90 Desc->Desc = SD;
91 Desc->IsInitialized = true;
92 Desc->IsBase = false;
93 Desc->IsActive = IsActive;
94 Desc->IsConst = IsConst || D->IsConst;
95 Desc->IsFieldMutable = IsMutable || D->IsMutable;
96 if (auto Fn = D->ElemDesc->CtorFn)
97 Fn(B, ElemLoc, Desc->IsConst, Desc->IsFieldMutable, IsActive,
98 D->ElemDesc);
99 }
100}
101
102static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
103 const unsigned NumElems = D->getNumElems();
104 const unsigned ElemSize =
105 D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
106
107 unsigned ElemOffset = 0;
108 for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
109 auto *ElemPtr = Ptr + ElemOffset;
110 auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
111 auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
112 if (auto Fn = D->ElemDesc->DtorFn)
113 Fn(B, ElemLoc, D->ElemDesc);
114 }
115}
116
117static void moveArrayDesc(Block *B, const std::byte *Src, std::byte *Dst,
118 const Descriptor *D) {
119 const unsigned NumElems = D->getNumElems();
120 const unsigned ElemSize =
121 D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
122
123 unsigned ElemOffset = 0;
124 for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
125 const auto *SrcPtr = Src + ElemOffset;
126 auto *DstPtr = Dst + ElemOffset;
127
128 const auto *SrcDesc = reinterpret_cast<const InlineDescriptor *>(SrcPtr);
129 const auto *SrcElemLoc = reinterpret_cast<const std::byte *>(SrcDesc + 1);
130 auto *DstDesc = reinterpret_cast<InlineDescriptor *>(DstPtr);
131 auto *DstElemLoc = reinterpret_cast<std::byte *>(DstDesc + 1);
132
133 *DstDesc = *SrcDesc;
134 if (auto Fn = D->ElemDesc->MoveFn)
135 Fn(B, SrcElemLoc, DstElemLoc, D->ElemDesc);
136 }
137}
138
139static void initField(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
140 bool IsActive, const Descriptor *D,
141 unsigned FieldOffset) {
142 bool IsUnion = false; // FIXME
143 auto *Desc = reinterpret_cast<InlineDescriptor *>(Ptr + FieldOffset) - 1;
144 Desc->Offset = FieldOffset;
145 Desc->Desc = D;
146 Desc->IsInitialized = D->IsArray;
147 Desc->IsBase = false;
148 Desc->IsActive = IsActive && !IsUnion;
149 Desc->IsConst = IsConst || D->IsConst;
150 Desc->IsFieldMutable = IsMutable || D->IsMutable;
151
152 if (auto Fn = D->CtorFn)
153 Fn(B, Ptr + FieldOffset, Desc->IsConst, Desc->IsFieldMutable,
154 Desc->IsActive, D);
155}
156
157static void initBase(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
158 bool IsActive, const Descriptor *D, unsigned FieldOffset,
159 bool IsVirtualBase) {
160 assert(D);
161 assert(D->ElemRecord);
162
163 bool IsUnion = D->ElemRecord->isUnion();
164 auto *Desc = reinterpret_cast<InlineDescriptor *>(Ptr + FieldOffset) - 1;
165 Desc->Offset = FieldOffset;
166 Desc->Desc = D;
167 Desc->IsInitialized = D->IsArray;
168 Desc->IsBase = true;
169 Desc->IsActive = IsActive && !IsUnion;
170 Desc->IsConst = IsConst || D->IsConst;
171 Desc->IsFieldMutable = IsMutable || D->IsMutable;
172
173 for (const auto &V : D->ElemRecord->bases())
174 initBase(B, Ptr + FieldOffset, IsConst, IsMutable, IsActive, V.Desc,
175 V.Offset, false);
176 for (const auto &F : D->ElemRecord->fields())
177 initField(B, Ptr + FieldOffset, IsConst, IsMutable, IsActive, F.Desc,
178 F.Offset);
179
180 // If this is initializing a virtual base, we do NOT want to consider its
181 // virtual bases, those are already flattened into the parent record when
182 // creating it.
183 if (IsVirtualBase)
184 return;
185
186 for (const auto &V : D->ElemRecord->virtual_bases())
187 initBase(B, Ptr + FieldOffset, IsConst, IsMutable, IsActive, V.Desc,
188 V.Offset, true);
189}
190
191static void ctorRecord(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
192 bool IsActive, const Descriptor *D) {
193 for (const auto &V : D->ElemRecord->bases())
194 initBase(B, Ptr, IsConst, IsMutable, IsActive, V.Desc, V.Offset, false);
195 for (const auto &F : D->ElemRecord->fields())
196 initField(B, Ptr, IsConst, IsMutable, IsActive, F.Desc, F.Offset);
197 for (const auto &V : D->ElemRecord->virtual_bases())
198 initBase(B, Ptr, IsConst, IsMutable, IsActive, V.Desc, V.Offset, true);
199}
200
201static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {
202 auto DtorSub = [=](unsigned SubOff, const Descriptor *F) {
203 if (auto Fn = F->DtorFn)
204 Fn(B, Ptr + SubOff, F);
205 };
206 for (const auto &F : D->ElemRecord->bases())
207 DtorSub(F.Offset, F.Desc);
208 for (const auto &F : D->ElemRecord->fields())
209 DtorSub(F.Offset, F.Desc);
210 for (const auto &F : D->ElemRecord->virtual_bases())
211 DtorSub(F.Offset, F.Desc);
212}
213
214static void moveRecord(Block *B, const std::byte *Src, std::byte *Dst,
215 const Descriptor *D) {
216 for (const auto &F : D->ElemRecord->fields()) {
217 auto FieldOff = F.Offset;
218 auto *FieldDesc = F.Desc;
219
220 if (auto Fn = FieldDesc->MoveFn)
221 Fn(B, Src + FieldOff, Dst + FieldOff, FieldDesc);
222 }
223}
224
226 // Floating types are special. They are primitives, but need their
227 // constructor called.
228 if (Type == PT_Float)
229 return ctorTy<PrimConv<PT_Float>::T>;
230 if (Type == PT_IntAP)
231 return ctorTy<PrimConv<PT_IntAP>::T>;
232 if (Type == PT_IntAPS)
233 return ctorTy<PrimConv<PT_IntAPS>::T>;
234
235 COMPOSITE_TYPE_SWITCH(Type, return ctorTy<T>, return nullptr);
236}
237
239 // Floating types are special. They are primitives, but need their
240 // destructor called, since they might allocate memory.
241 if (Type == PT_Float)
242 return dtorTy<PrimConv<PT_Float>::T>;
243 if (Type == PT_IntAP)
244 return dtorTy<PrimConv<PT_IntAP>::T>;
245 if (Type == PT_IntAPS)
246 return dtorTy<PrimConv<PT_IntAPS>::T>;
247
248 COMPOSITE_TYPE_SWITCH(Type, return dtorTy<T>, return nullptr);
249}
250
252 COMPOSITE_TYPE_SWITCH(Type, return moveTy<T>, return nullptr);
253}
254
256 TYPE_SWITCH(Type, return ctorArrayTy<T>);
257 llvm_unreachable("unknown Expr");
258}
259
261 TYPE_SWITCH(Type, return dtorArrayTy<T>);
262 llvm_unreachable("unknown Expr");
263}
264
266 TYPE_SWITCH(Type, return moveArrayTy<T>);
267 llvm_unreachable("unknown Expr");
268}
269
270/// Primitives.
272 bool IsConst, bool IsTemporary, bool IsMutable)
273 : Source(D), ElemSize(primSize(Type)), Size(ElemSize),
274 MDSize(MD.value_or(0)), AllocSize(align(Size + MDSize)), PrimT(Type),
275 IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
276 CtorFn(getCtorPrim(Type)), DtorFn(getDtorPrim(Type)),
277 MoveFn(getMovePrim(Type)) {
278 assert(AllocSize >= Size);
279 assert(Source && "Missing source");
280}
281
282/// Primitive arrays.
284 size_t NumElems, bool IsConst, bool IsTemporary,
285 bool IsMutable)
286 : Source(D), ElemSize(primSize(Type)), Size(ElemSize * NumElems),
287 MDSize(MD.value_or(0)),
288 AllocSize(align(MDSize) + align(Size) + sizeof(InitMapPtr)), PrimT(Type),
289 IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
290 IsArray(true), CtorFn(getCtorArrayPrim(Type)),
291 DtorFn(getDtorArrayPrim(Type)), MoveFn(getMoveArrayPrim(Type)) {
292 assert(Source && "Missing source");
293}
294
295/// Primitive unknown-size arrays.
297 bool IsTemporary, UnknownSize)
298 : Source(D), ElemSize(primSize(Type)), Size(UnknownSizeMark),
299 MDSize(MD.value_or(0)),
300 AllocSize(MDSize + sizeof(InitMapPtr) + alignof(void *)), IsConst(true),
301 IsMutable(false), IsTemporary(IsTemporary), IsArray(true),
302 CtorFn(getCtorArrayPrim(Type)), DtorFn(getDtorArrayPrim(Type)),
303 MoveFn(getMoveArrayPrim(Type)) {
304 assert(Source && "Missing source");
305}
306
307/// Arrays of composite elements.
309 unsigned NumElems, bool IsConst, bool IsTemporary,
310 bool IsMutable)
311 : Source(D), ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
312 Size(ElemSize * NumElems), MDSize(MD.value_or(0)),
313 AllocSize(std::max<size_t>(alignof(void *), Size) + MDSize),
314 ElemDesc(Elem), IsConst(IsConst), IsMutable(IsMutable),
315 IsTemporary(IsTemporary), IsArray(true), CtorFn(ctorArrayDesc),
316 DtorFn(dtorArrayDesc), MoveFn(moveArrayDesc) {
317 assert(Source && "Missing source");
318}
319
320/// Unknown-size arrays of composite elements.
322 bool IsTemporary, UnknownSize)
323 : Source(D), ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
324 Size(UnknownSizeMark), MDSize(MD.value_or(0)),
325 AllocSize(MDSize + alignof(void *)), ElemDesc(Elem), IsConst(true),
326 IsMutable(false), IsTemporary(IsTemporary), IsArray(true),
327 CtorFn(ctorArrayDesc), DtorFn(dtorArrayDesc), MoveFn(moveArrayDesc) {
328 assert(Source && "Missing source");
329}
330
331/// Composite records.
333 bool IsConst, bool IsTemporary, bool IsMutable)
334 : Source(D), ElemSize(std::max<size_t>(alignof(void *), R->getFullSize())),
335 Size(ElemSize), MDSize(MD.value_or(0)), AllocSize(Size + MDSize),
336 ElemRecord(R), IsConst(IsConst), IsMutable(IsMutable),
337 IsTemporary(IsTemporary), CtorFn(ctorRecord), DtorFn(dtorRecord),
338 MoveFn(moveRecord) {
339 assert(Source && "Missing source");
340}
341
342/// Dummy.
344 : Source(D), ElemSize(1), Size(1), MDSize(0), AllocSize(MDSize),
345 ElemRecord(nullptr), IsConst(true), IsMutable(false), IsTemporary(false),
346 IsDummy(true) {
347 assert(Source && "Missing source");
348}
349
351 if (auto *E = asExpr())
352 return E->getType();
353 if (auto *D = asValueDecl())
354 return D->getType();
355 if (auto *T = dyn_cast<TypeDecl>(asDecl()))
356 return QualType(T->getTypeForDecl(), 0);
357 llvm_unreachable("Invalid descriptor type");
358}
359
361 assert(isArray());
362 const auto *AT = cast<ArrayType>(getType());
363 return AT->getElementType();
364}
365
367 if (auto *D = Source.dyn_cast<const Decl *>())
368 return D->getLocation();
369 if (auto *E = Source.dyn_cast<const Expr *>())
370 return E->getExprLoc();
371 llvm_unreachable("Invalid descriptor type");
372}
373
375 : UninitFields(N), Data(std::make_unique<T[]>(numFields(N))) {
376 std::fill_n(data(), numFields(N), 0);
377}
378
379bool InitMap::initializeElement(unsigned I) {
380 unsigned Bucket = I / PER_FIELD;
381 T Mask = T(1) << (I % PER_FIELD);
382 if (!(data()[Bucket] & Mask)) {
383 data()[Bucket] |= Mask;
384 UninitFields -= 1;
385 }
386 return UninitFields == 0;
387}
388
389bool InitMap::isElementInitialized(unsigned I) const {
390 unsigned Bucket = I / PER_FIELD;
391 return data()[Bucket] & (T(1) << (I % PER_FIELD));
392}
#define V(N, I)
Definition: ASTContext.h:3285
static void initBase(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable, bool IsActive, const Descriptor *D, unsigned FieldOffset, bool IsVirtualBase)
Definition: Descriptor.cpp:157
static void dtorTy(Block *, std::byte *Ptr, const Descriptor *)
Definition: Descriptor.cpp:28
static BlockCtorFn getCtorArrayPrim(PrimType Type)
Definition: Descriptor.cpp:255
static BlockMoveFn getMoveArrayPrim(PrimType Type)
Definition: Descriptor.cpp:265
static void initField(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable, bool IsActive, const Descriptor *D, unsigned FieldOffset)
Definition: Descriptor.cpp:139
static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D)
Definition: Descriptor.cpp:52
static BlockMoveFn getMovePrim(PrimType Type)
Definition: Descriptor.cpp:251
static void ctorTy(Block *, std::byte *Ptr, bool, bool, bool, const Descriptor *)
Definition: Descriptor.cpp:22
static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool, const Descriptor *D)
Definition: Descriptor.cpp:41
static void moveRecord(Block *B, const std::byte *Src, std::byte *Dst, const Descriptor *D)
Definition: Descriptor.cpp:214
static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D)
Definition: Descriptor.cpp:102
static void moveTy(Block *, const std::byte *Src, std::byte *Dst, const Descriptor *)
Definition: Descriptor.cpp:33
static BlockDtorFn getDtorPrim(PrimType Type)
Definition: Descriptor.cpp:238
static BlockCtorFn getCtorPrim(PrimType Type)
Definition: Descriptor.cpp:225
static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable, bool IsActive, const Descriptor *D)
Definition: Descriptor.cpp:76
static BlockDtorFn getDtorArrayPrim(PrimType Type)
Definition: Descriptor.cpp:260
static void moveArrayDesc(Block *B, const std::byte *Src, std::byte *Dst, const Descriptor *D)
Definition: Descriptor.cpp:117
static void ctorRecord(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable, bool IsActive, const Descriptor *D)
Definition: Descriptor.cpp:191
static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D)
Definition: Descriptor.cpp:201
static void moveArrayTy(Block *, const std::byte *Src, std::byte *Dst, const Descriptor *D)
Definition: Descriptor.cpp:64
#define COMPOSITE_TYPE_SWITCH(Expr, B, D)
Definition: PrimType.h:174
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:117
__DEVICE__ int max(int __a, int __b)
__SIZE_TYPE__ size_t
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
This represents one expression.
Definition: Expr.h:110
A (possibly-)qualified type.
Definition: Type.h:940
Encodes a location in the source.
The base class of the type hierarchy.
Definition: Type.h:1813
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
const Descriptor * Desc
Pointer to the stack slot descriptor.
Definition: InterpBlock.h:162
Structure/Class descriptor.
Definition: Record.h:25
bool isUnion() const
Checks if the record is a union.
Definition: Record.h:56
llvm::iterator_range< const_virtual_iter > virtual_bases() const
Definition: Record.h:96
llvm::iterator_range< const_base_iter > bases() const
Definition: Record.h:85
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:77
std::optional< std::pair< bool, std::shared_ptr< InitMap > > > InitMapPtr
Definition: Descriptor.h:28
bool NE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:868
void(*)(Block *Storage, std::byte *FieldPtr, const Descriptor *FieldDesc) BlockDtorFn
Invoked when a block is destroyed.
Definition: Descriptor.h:40
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:99
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:32
void(*)(Block *Storage, const std::byte *SrcFieldPtr, std::byte *DstFieldPtr, const Descriptor *FieldDesc) BlockMoveFn
Invoked when a block with pointers referencing it goes out of scope.
Definition: Descriptor.h:48
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:22
void(*)(Block *Storage, std::byte *FieldPtr, bool IsConst, bool IsMutable, bool IsActive, const Descriptor *FieldDesc) BlockCtorFn
Invoked whenever a block is created.
Definition: Descriptor.h:35
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:27
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Definition: Format.h:5428
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26
Token to denote structures of unknown size.
Definition: Descriptor.h:109
Describes a memory block created by an allocation site.
Definition: Descriptor.h:91
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:123
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:204
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:211
QualType getElemQualType() const
Definition: Descriptor.cpp:360
const BlockMoveFn MoveFn
Definition: Descriptor.h:136
const BlockDtorFn DtorFn
Definition: Descriptor.h:135
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:176
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:134
QualType getType() const
Definition: Descriptor.cpp:350
const Decl * asDecl() const
Definition: Descriptor.h:172
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:117
SourceLocation getLocation() const
Definition: Descriptor.cpp:366
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:125
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:111
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:129
Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable)
Allocates a descriptor for a primitive.
Definition: Descriptor.cpp:271
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:115
const Expr * asExpr() const
Definition: Descriptor.h:173
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:228
InitMap(unsigned N)
Initializes the map with no fields set.
Definition: Descriptor.cpp:374
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:56
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:58