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 "Char.h"
12#include "ExprConstShared.h"
13#include "FixedPoint.h"
14#include "Floating.h"
15#include "Integral.h"
16#include "IntegralAP.h"
17#include "MemberPointer.h"
18#include "Pointer.h"
19#include "PrimType.h"
20#include "Record.h"
21#include "Reflect.h"
22#include "Source.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/Basic/TargetInfo.h"
25#include "llvm/Support/ErrorHandling.h"
26
27using namespace clang;
28using namespace clang::interp;
29
30template <typename T> static constexpr bool needsDtor() {
31 return std::is_same_v<T, Pointer> || std::is_same_v<T, MemberPointer>;
32}
33
34template <typename T>
35static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
36 static_assert(needsDtor<T>());
37 reinterpret_cast<T *>(Ptr)->~T();
38}
39
40template <typename T>
41static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
42 InitMapPtr &IMP = *reinterpret_cast<InitMapPtr *>(Ptr);
43 IMP.deleteInitMap();
44
45 if constexpr (needsDtor<T>()) {
46 Ptr += sizeof(InitMapPtr);
47 for (unsigned I = 0, NE = D->getNumElems(); I != NE; ++I) {
48 reinterpret_cast<T *>(Ptr)[I].~T();
49 }
50 }
51}
52
53static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst,
54 bool IsMutable, bool IsVolatile, bool IsActive,
55 bool InUnion, const Descriptor *D) {
56 const unsigned NumElems = D->getNumElems();
57 const unsigned ElemSize =
58 D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
59
60 unsigned ElemOffset = 0;
61 for (unsigned I = 0; I != NumElems; ++I, ElemOffset += ElemSize) {
62 auto *ElemPtr = Ptr + ElemOffset;
63 auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
64 auto *SD = D->ElemDesc;
65
66 Desc->Offset = ElemOffset + sizeof(InlineDescriptor);
67 Desc->Desc = SD;
68 Desc->IsInitialized = true;
69 Desc->IsBase = false;
70 Desc->IsActive = IsActive;
71 Desc->IsConst = IsConst || D->IsConst;
72 Desc->IsFieldMutable = IsMutable || D->IsMutable;
73 Desc->InUnion = InUnion;
74 Desc->IsArrayElement = true;
75 Desc->IsVolatile = IsVolatile;
76
77 if (auto Fn = D->ElemDesc->CtorFn) {
78 auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
79 Fn(B, ElemLoc, Desc->IsConst, Desc->IsFieldMutable, IsVolatile, IsActive,
80 Desc->InUnion || SD->isUnion(), D->ElemDesc);
81 }
82 }
83}
84
85static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
86 const unsigned NumElems = D->getNumElems();
87 const unsigned ElemSize =
88 D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
89
90 unsigned ElemOffset = 0;
91 auto Dtor = D->ElemDesc->DtorFn;
92 assert(Dtor &&
93 "a composite array without an elem dtor shouldn't have a dtor itself");
94 for (unsigned I = 0; I != NumElems; ++I, ElemOffset += ElemSize) {
95 auto *ElemPtr = Ptr + ElemOffset;
96 auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
97 auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
98 Dtor(B, ElemLoc, D->ElemDesc);
99 }
100}
101
102static void initField(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
103 bool IsVolatile, bool IsActive, bool IsUnionField,
104 bool InUnion, const Descriptor *D, unsigned FieldOffset) {
105 auto *Desc = reinterpret_cast<InlineDescriptor *>(Ptr + FieldOffset) - 1;
106 Desc->Offset = FieldOffset;
107 Desc->Desc = D;
108 Desc->IsInitialized = D->IsArray;
109 Desc->IsBase = false;
110 Desc->IsActive = IsActive && !IsUnionField;
111 Desc->InUnion = InUnion;
112 Desc->IsConst = IsConst || D->IsConst;
113 Desc->IsFieldMutable = IsMutable || D->IsMutable;
114 Desc->IsVolatile = IsVolatile || D->IsVolatile;
115 // True if this field is const AND the parent is mutable.
116 Desc->IsConstInMutable = Desc->IsConst && IsMutable;
117 Desc->LifeState =
118 D->isPrimitiveArray()
119 ? Lifetime::Started
120 : (Desc->IsActive ? Lifetime::NotStarted : Lifetime::Started);
121
122 if (auto Fn = D->CtorFn)
123 Fn(B, Ptr + FieldOffset, Desc->IsConst, Desc->IsFieldMutable,
124 Desc->IsVolatile, Desc->IsActive, InUnion || D->isUnion(), D);
125}
126
127static void initBase(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
128 bool IsVolatile, bool IsActive, bool InUnion,
129 const Descriptor *D, unsigned FieldOffset,
130 bool IsVirtualBase) {
131 assert(D);
132 assert(D->ElemRecord);
133 assert(!D->ElemRecord->isUnion()); // Unions cannot be base classes.
134
135 auto *Desc = reinterpret_cast<InlineDescriptor *>(Ptr + FieldOffset) - 1;
136 Desc->Offset = FieldOffset;
137 Desc->Desc = D;
138 Desc->IsInitialized = D->IsArray;
139 Desc->IsBase = true;
140 Desc->IsVirtualBase = IsVirtualBase;
141 Desc->IsActive = IsActive && !InUnion;
142 Desc->IsConst = IsConst || D->IsConst;
143 Desc->IsFieldMutable = IsMutable || D->IsMutable;
144 Desc->InUnion = InUnion;
145 Desc->IsVolatile = false;
146
147 for (const auto &V : D->ElemRecord->bases())
148 initBase(B, Ptr: Ptr + FieldOffset, IsConst, IsMutable, IsVolatile, IsActive,
149 InUnion, D: V.Desc, FieldOffset: V.Offset, IsVirtualBase: false);
150 for (const auto &F : D->ElemRecord->fields())
151 initField(B, Ptr: Ptr + FieldOffset, IsConst, IsMutable, IsVolatile, IsActive,
152 IsUnionField: InUnion, InUnion, D: F.Desc, FieldOffset: F.Offset);
153}
154
155static void ctorRecord(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
156 bool IsVolatile, bool IsActive, bool InUnion,
157 const Descriptor *D) {
158 for (const auto &V : D->ElemRecord->bases())
159 initBase(B, Ptr, IsConst, IsMutable, IsVolatile, IsActive, InUnion, D: V.Desc,
160 FieldOffset: V.Offset,
161 /*IsVirtualBase=*/false);
162 for (const auto &F : D->ElemRecord->fields()) {
163 bool IsUnionField = D->isUnion();
164 initField(B, Ptr, IsConst, IsMutable, IsVolatile, IsActive, IsUnionField,
165 InUnion: InUnion || IsUnionField, D: F.Desc, FieldOffset: F.Offset);
166 }
167 for (const auto &V : D->ElemRecord->virtual_bases())
168 initBase(B, Ptr, IsConst, IsMutable, IsVolatile, IsActive, InUnion, D: V.Desc,
169 FieldOffset: V.Offset,
170 /*IsVirtualBase=*/true);
171}
172
173static void destroyField(Block *B, std::byte *Ptr, const Descriptor *D,
174 unsigned FieldOffset) {
175 if (auto Fn = D->DtorFn)
176 Fn(B, Ptr + FieldOffset, D);
177}
178
179static void destroyBase(Block *B, std::byte *Ptr, const Descriptor *D,
180 unsigned FieldOffset) {
181 assert(D);
182 assert(D->ElemRecord);
183
184 for (const auto &V : D->ElemRecord->bases())
185 destroyBase(B, Ptr: Ptr + FieldOffset, D: V.Desc, FieldOffset: V.Offset);
186 for (const auto &F : D->ElemRecord->fields())
187 destroyField(B, Ptr: Ptr + FieldOffset, D: F.Desc, FieldOffset: F.Offset);
188}
189
190static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {
191 for (const auto &F : D->ElemRecord->bases())
192 destroyBase(B, Ptr, D: F.Desc, FieldOffset: F.Offset);
193 for (const auto &F : D->ElemRecord->fields())
194 destroyField(B, Ptr, D: F.Desc, FieldOffset: F.Offset);
195 for (const auto &F : D->ElemRecord->virtual_bases())
196 destroyBase(B, Ptr, D: F.Desc, FieldOffset: F.Offset);
197}
198
199/// Whether a record needs its descriptor dtor function called.
200static bool needsRecordDtor(const Record *R) {
201 for (const auto &B : R->bases()) {
202 if (B.Desc->DtorFn)
203 return true;
204 }
205
206 for (const auto &F : R->fields()) {
207 if (F.Desc->DtorFn)
208 return true;
209 }
210
211 for (const auto &V : R->virtual_bases()) {
212 if (V.Desc->DtorFn)
213 return true;
214 }
215 return false;
216}
217
218static BlockDtorFn getDtorPrim(PrimType T) {
219 switch (T) {
220 case PT_Ptr:
221 return dtorTy<PrimConv<PT_Ptr>::T>;
222 case PT_MemberPtr:
223 return dtorTy<PrimConv<PT_MemberPtr>::T>;
224 default:
225 return nullptr;
226 }
227 llvm_unreachable("Unhandled PrimType");
228}
229
230// NOTE: The following #if-ed out code is for calling constructors of primitive
231// types. It is currently not needed but I'm not sure if it will stay this way
232// forever so I'm leaving it here for now.
233#if 0
234template <typename T> static constexpr bool needsCtor() {
235 return false;
236 if constexpr (std::is_same_v<T, Char<true>> ||
237 std::is_same_v<T, Char<false>> ||
238 std::is_same_v<T, Integral<16, true>> ||
239 std::is_same_v<T, Integral<16, false>> ||
240 std::is_same_v<T, Integral<32, true>> ||
241 std::is_same_v<T, Integral<32, false>> ||
242 std::is_same_v<T, Integral<64, true>> ||
243 std::is_same_v<T, Integral<64, false>> ||
244 std::is_same_v<T, IntegralAP<true>> ||
245 std::is_same_v<T, IntegralAP<false>> ||
246 std::is_same_v<T, Floating> || std::is_same_v<T, Boolean>)
247 return false;
248
249 return true;
250}
251
252
253static BlockCtorFn getCtorArrayPrim(PrimType Type) {
254 TYPE_SWITCH(Type, if constexpr (!needsCtor<T>()) return nullptr;
255 return ctorArrayTy<T>);
256 llvm_unreachable("unknown Expr");
257}
258static BlockCtorFn getCtorPrim(PrimType T) {
259 return nullptr;
260 switch (T) {
261 // case PT_Ptr:
262 // return ctorTy<PrimConv<PT_Ptr>::T>;
263 // case PT_MemberPtr:
264 // return ctorTy<PrimConv<PT_MemberPtr>::T>;
265 default:
266 return nullptr;
267 }
268 llvm_unreachable("Unhandled PrimType");
269}
270
271
272template <typename T>
273static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool, bool, bool,
274 const Descriptor *D) {
275 new (Ptr) InitMapPtr();
276
277 if constexpr (needsCtor<T>()) {
278 Ptr += sizeof(InitMapPtr);
279 for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
280 new (&reinterpret_cast<T *>(Ptr)[I]) T();
281 }
282 }
283}
284template <typename T>
285static void ctorTy(Block *, std::byte *Ptr, bool, bool, bool, bool, bool,
286 const Descriptor *) {
287 static_assert(needsCtor<T>());
288 new (Ptr) T();
289}
290#endif
291
292static BlockDtorFn getDtorArrayPrim(PrimType Type) {
293 TYPE_SWITCH(Type, return dtorArrayTy<T>);
294 llvm_unreachable("unknown Expr");
295}
296
297/// Primitives.
298Descriptor::Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type,
299 bool IsConst, bool IsTemporary, bool IsMutable,
300 bool IsVolatile)
301 : Source(D), SourceType(SourceTy), ElemSize(primSize(Type)), Size(ElemSize),
302 AllocSize(align(Size: ElemSize)), PrimT(Type), IsConst(IsConst),
303 IsMutable(IsMutable), IsTemporary(IsTemporary), IsVolatile(IsVolatile),
304 CtorFn(nullptr), DtorFn(getDtorPrim(T: Type)) {
305 assert(Source && "Missing source");
306}
307
308/// Primitive arrays.
309Descriptor::Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type,
310 size_t NumElems, bool IsConst, bool IsTemporary,
311 bool IsMutable, bool IsVolatile)
312 : Source(D), SourceType(SourceTy), ElemSize(primSize(Type)),
313 Size(ElemSize * NumElems), AllocSize(align(Size) + sizeof(InitMapPtr)),
314 PrimT(Type), IsConst(IsConst), IsMutable(IsMutable),
315 IsTemporary(IsTemporary), IsVolatile(IsVolatile), IsArray(true),
316 CtorFn(nullptr), DtorFn(getDtorArrayPrim(Type)) {
317 assert(Source && "Missing source");
318 assert(NumElems <= (MaxArrayElemBytes / ElemSize));
319}
320
321/// Primitive unknown-size arrays.
322Descriptor::Descriptor(DeclOrExpr D, PrimType Type, bool IsConst,
323 bool IsTemporary, UnknownSize)
324 : Source(D), ElemSize(primSize(Type)), Size(UnknownSizeMark),
325 AllocSize(sizeof(InitMapPtr) + alignof(void *)), PrimT(Type),
326 IsConst(IsConst), IsMutable(false), IsTemporary(IsTemporary),
327 IsArray(true), CtorFn(nullptr), DtorFn(getDtorArrayPrim(Type)) {
328 assert(Source && "Missing source");
329}
330
331/// Arrays of composite elements.
332Descriptor::Descriptor(DeclOrExpr D, const Type *SourceTy,
333 const Descriptor *Elem, unsigned NumElems, bool IsConst,
334 bool IsTemporary, bool IsMutable)
335 : Source(D), SourceType(SourceTy),
336 ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
337 Size(ElemSize * NumElems),
338 AllocSize(std::max<size_t>(a: alignof(void *), b: Size)), ElemDesc(Elem),
339 IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
340 IsArray(true), CtorFn(ctorArrayDesc),
341 DtorFn(Elem->DtorFn ? dtorArrayDesc : nullptr) {
342 assert(Source && "Missing source");
343}
344
345/// Unknown-size arrays of composite elements.
346Descriptor::Descriptor(DeclOrExpr D, const Descriptor *Elem, bool IsTemporary,
347 UnknownSize)
348 : Source(D), ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
349 Size(UnknownSizeMark), AllocSize(alignof(void *)), ElemDesc(Elem),
350 IsConst(true), IsMutable(false), IsTemporary(IsTemporary), IsArray(true),
351 CtorFn(ctorArrayDesc), DtorFn(Elem->DtorFn ? dtorArrayDesc : nullptr) {
352 assert(Source && "Missing source");
353}
354
355/// Composite records.
356Descriptor::Descriptor(DeclOrExpr D, const Record *R, bool IsConst,
357 bool IsTemporary, bool IsMutable, bool IsVolatile)
358 : Source(D), ElemSize(std::max<size_t>(a: alignof(void *), b: R->getFullSize())),
359 Size(ElemSize), AllocSize(Size), ElemRecord(R), IsConst(IsConst),
360 IsMutable(IsMutable), IsTemporary(IsTemporary), IsVolatile(IsVolatile),
361 CtorFn(ctorRecord), DtorFn(needsRecordDtor(R) ? dtorRecord : nullptr) {
362 assert(Source && "Missing source");
363}
364
365/// Dummy.
366Descriptor::Descriptor(DeclOrExpr D)
367 : Source(D), ElemSize(1), Size(1), AllocSize(0), ElemDesc(nullptr),
368 IsConst(true), IsMutable(false), IsTemporary(false) {
369 assert(Source && "Missing source");
370}
371
372QualType Descriptor::getType() const {
373 if (SourceType)
374 return QualType(SourceType, 0);
375
376 if (const auto *T = dyn_cast_if_present<TypeDecl>(Val: asDecl()))
377 return T->getASTContext().getTypeDeclType(Decl: T);
378
379 // The Source sometimes has a different type than the once
380 // we really save. Try to consult the Record first.
381 if (isRecord()) {
382 const RecordDecl *RD = ElemRecord->getDecl();
383 QualType T = RD->getASTContext().getTagType(Keyword: ElaboratedTypeKeyword::None,
384 Qualifier: std::nullopt, TD: RD, OwnsTag: false);
385 if (IsConst)
386 return T.withConst();
387 return T;
388 }
389
390 if (const auto *E = asExpr()) {
391 if (isa<CXXNewExpr>(Val: E))
392 return E->getType()->getPointeeType();
393
394 // std::allocator.allocate() call.
395 if (const auto *ME = dyn_cast<CXXMemberCallExpr>(Val: E);
396 ME && ME->getRecordDecl()->getName() == "allocator" &&
397 ME->getMethodDecl()->getName() == "allocate")
398 return E->getType()->getPointeeType();
399 return E->getType();
400 }
401
402 if (const auto *D = asValueDecl())
403 return D->getType();
404
405 llvm_unreachable("Invalid descriptor type");
406}
407
408QualType Descriptor::getElemQualType() const {
409 assert(isArray());
410 QualType T;
411
412 if (SourceType) {
413 T = QualType(SourceType, 0);
414 } else if (const auto *TDecl = dyn_cast_if_present<TypeDecl>(Val: asDecl())) {
415 T = TDecl->getASTContext().getTypeDeclType(Decl: TDecl);
416 } else if (isRecord()) {
417 const RecordDecl *RD = ElemRecord->getDecl();
418 T = RD->getASTContext().getTagType(Keyword: ElaboratedTypeKeyword::None,
419 Qualifier: std::nullopt, TD: RD, OwnsTag: false);
420 if (IsConst)
421 T.addConst();
422 } else if (const auto *E = asExpr()) {
423 T = E->getType();
424 } else if (const auto *D = asValueDecl()) {
425 T = D->getType();
426 }
427
428 assert(!T.isNull());
429
430 if (const auto *AT = T->getAs<AtomicType>())
431 T = AT->getValueType();
432 if (T->isPointerOrReferenceType())
433 T = T->getPointeeType();
434
435 if (const auto *AT = T->getAsArrayTypeUnsafe()) {
436 // For primitive arrays, we don't save a QualType at all,
437 // just a PrimType. Try to figure out the QualType here.
438 if (isPrimitiveArray()) {
439 while (T->isArrayType())
440 T = T->getAsArrayTypeUnsafe()->getElementType();
441 return T;
442 }
443 return AT->getElementType();
444 }
445 if (const auto *CT = T->getAs<ComplexType>())
446 return CT->getElementType();
447 if (const auto *CT = T->getAs<VectorType>())
448 return CT->getElementType();
449
450 return T;
451}
452
453QualType Descriptor::getDataType(const ASTContext &Ctx) const {
454 auto MakeArrayType = [&](QualType ElemType) -> QualType {
455 if (IsArray)
456 return Ctx.getConstantArrayType(
457 EltTy: ElemType, ArySize: APInt(64, static_cast<uint64_t>(getNumElems()), false),
458 SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
459 return ElemType;
460 };
461
462 if (isDynAlloc())
463 return MakeArrayType(asExpr()->getType()->getPointeeType());
464
465 return getType();
466}
467
468SourceInfo Descriptor::getLoc() const {
469 if (const auto *D = Source.asDecl())
470 return SourceInfo(D);
471 if (const auto *E = Source.asExpr())
472 return SourceInfo(E);
473 llvm_unreachable("Invalid descriptor type");
474}
475
476bool Descriptor::hasTrivialDtor() const {
477 if (isPrimitive() || isPrimitiveArray())
478 return true;
479
480 if (isRecord()) {
481 assert(ElemRecord);
482 return ElemRecord->hasTrivialDtor();
483 }
484
485 if (!ElemDesc)
486 return true;
487 // Composite arrays.
488 return ElemDesc->hasTrivialDtor();
489}
490
491bool Descriptor::isUnion() const { return isRecord() && ElemRecord->isUnion(); }
492
493unsigned Descriptor::getElemDataSize() const {
494 if ((isPrimitive() || isPrimitiveArray()) &&
495 isIntegerOrBoolType(T: getPrimType())) {
496 if (getPrimType() == PT_Bool)
497 return 1;
498 FIXED_SIZE_INT_TYPE_SWITCH(getPrimType(), { return T::bitWidth() / 8; });
499 }
500 return ElemSize;
501}
502
503DynAllocKind Descriptor::getDynAllocKindForExpr(const Expr *E) {
504 // new or new[] expression
505 if (const auto *NE = dyn_cast<CXXNewExpr>(Val: E))
506 return NE->isArray() ? DynAllocKind::ArrayNew : DynAllocKind::New;
507 // std::allocator::allocate call
508 if (const auto *ME = dyn_cast<CXXMemberCallExpr>(Val: E);
509 ME && ME->getRecordDecl()->getName() == "allocator" &&
510 ME->getMethodDecl()->getName() == "allocate")
511 return DynAllocKind::StdAllocator;
512 // __builtin_operator_new call
513 if (const auto *CE = dyn_cast<CallExpr>(Val: E);
514 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_operator_new)
515 return DynAllocKind::BuiltinOperatorNew;
516 return DynAllocKind::None;
517}
518
519CharUnits Descriptor::computeAlignForDynamicAlloc(const ASTContext &Ctx) const {
520 return GetAlignOfDynamicAlloc(Ctx, AllocType: getDataType(Ctx), AllocKind: getDynAllocKind());
521}
522