1//===--- Descriptor.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 descriptors which characterise allocations.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H
14#define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H
15
16#include "../ExprConstShared.h"
17#include "DeclOrExpr.h"
18#include "InitMap.h"
19#include "PrimType.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/Expr.h"
22
23namespace clang {
24namespace interp {
25class Block;
26class Record;
27class SourceInfo;
28struct Descriptor;
29enum PrimType : uint8_t;
30
31/// Invoked whenever a block is created. The constructor method fills in the
32/// inline descriptors of all fields and array elements. It also initializes
33/// all the fields which contain non-trivial types.
34using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,
35 bool IsMutable, bool IsVolatile, bool IsActive,
36 bool InUnion, const Descriptor *FieldDesc);
37
38/// Invoked when a block is destroyed. Invokes the destructors of all
39/// non-trivial nested fields of arrays and records.
40using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
41 const Descriptor *FieldDesc);
42
43enum class GlobalInitState {
44 Initialized,
45 NoInitializer,
46 InitializerFailed,
47};
48
49/// Descriptor used for global variables.
50struct alignas(void *) GlobalInlineDescriptor {
51 GlobalInitState InitState = GlobalInitState::InitializerFailed;
52};
53static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");
54
55enum class Lifetime : uint8_t {
56 Started,
57 NotStarted,
58 Destroyed,
59 Ended,
60};
61
62/// Inline descriptor embedded in structures and arrays.
63///
64/// Such descriptors precede all composite array elements and structure fields.
65/// If the base of a pointer is not zero, the base points to the end of this
66/// structure. The offset field is used to traverse the pointer chain up
67/// to the root structure which allocated the object.
68struct InlineDescriptor {
69 /// Offset inside the structure/array.
70 unsigned Offset;
71
72 /// Flag indicating if the storage is constant or not.
73 /// Relevant for primitive fields.
74 LLVM_PREFERRED_TYPE(bool)
75 unsigned IsConst : 1;
76 /// For primitive fields, it indicates if the field was initialized.
77 /// Primitive fields in static storage are always initialized.
78 /// Arrays are always initialized, even though their elements might not be.
79 /// Base classes are initialized after the constructor is invoked.
80 LLVM_PREFERRED_TYPE(bool)
81 unsigned IsInitialized : 1;
82 /// Flag indicating if the field is an embedded base class.
83 LLVM_PREFERRED_TYPE(bool)
84 unsigned IsBase : 1;
85 /// Flag inidcating if the field is a virtual base class.
86 LLVM_PREFERRED_TYPE(bool)
87 unsigned IsVirtualBase : 1;
88 /// Flag indicating if the field is the active member of a union.
89 LLVM_PREFERRED_TYPE(bool)
90 unsigned IsActive : 1;
91 /// Flag indicating if this field is in a union (even if nested).
92 LLVM_PREFERRED_TYPE(bool)
93 unsigned InUnion : 1;
94 /// Flag indicating if the field is mutable (if in a record).
95 LLVM_PREFERRED_TYPE(bool)
96 unsigned IsFieldMutable : 1;
97 /// Flag indicating if this field is a const field nested in
98 /// a mutable parent field.
99 LLVM_PREFERRED_TYPE(bool)
100 unsigned IsConstInMutable : 1;
101 /// Flag indicating if the field is an element of a composite array.
102 LLVM_PREFERRED_TYPE(bool)
103 unsigned IsArrayElement : 1;
104 LLVM_PREFERRED_TYPE(bool)
105 unsigned IsVolatile : 1;
106
107 Lifetime LifeState;
108
109 const Descriptor *Desc;
110
111 InlineDescriptor(const Descriptor *D)
112 : Offset(sizeof(InlineDescriptor)), IsConst(false), IsInitialized(false),
113 IsBase(false), IsActive(false), IsFieldMutable(false),
114 IsArrayElement(false), IsVolatile(false), LifeState(Lifetime::Started),
115 Desc(D) {}
116
117 void dump() const { dump(OS&: llvm::errs()); }
118 void dump(llvm::raw_ostream &OS) const;
119};
120static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
121
122/// Describes a memory block created by an allocation site.
123struct Descriptor final {
124private:
125 /// Original declaration, used to emit the error message.
126 const DeclOrExpr Source;
127 const Type *SourceType = nullptr;
128 /// Size of an element, in host bytes.
129 const unsigned ElemSize;
130 /// Size of the storage, in host bytes.
131 const unsigned Size;
132 /// Size of the allocation (storage), in host bytes.
133 const unsigned AllocSize;
134
135 /// Value to denote arrays of unknown size.
136 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
137
138public:
139 /// Token to denote structures of unknown size.
140 struct UnknownSize {};
141
142 /// Maximum number of bytes to be used for array elements.
143 static constexpr unsigned MaxArrayElemBytes =
144 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr);
145
146 /// Pointer to the record, if block contains records.
147 const Record *const ElemRecord = nullptr;
148 /// Descriptor of the array element.
149 const Descriptor *const ElemDesc = nullptr;
150 /// The primitive type this descriptor was created for,
151 /// or the primitive element type in case this is
152 /// a primitive array.
153 const OptPrimType PrimT = std::nullopt;
154 /// Flag indicating if the block is mutable.
155 const bool IsConst = false;
156 /// Flag indicating if a field is mutable.
157 const bool IsMutable = false;
158 /// Flag indicating if the block is a temporary.
159 const bool IsTemporary = false;
160 const bool IsVolatile = false;
161 /// Flag indicating if the block is an array.
162 const bool IsArray = false;
163 bool IsConstexprUnknown = false;
164
165 /// Storage management methods.
166 const BlockCtorFn CtorFn = nullptr;
167 const BlockDtorFn DtorFn = nullptr;
168
169 /// Allocates a descriptor for a primitive.
170 Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, bool IsConst,
171 bool IsTemporary, bool IsMutable, bool IsVolatile);
172
173 /// Allocates a descriptor for an array of primitives.
174 Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, size_t NumElems,
175 bool IsConst, bool IsTemporary, bool IsMutable, bool IsVolatile);
176
177 /// Allocates a descriptor for an array of primitives of unknown size.
178 Descriptor(DeclOrExpr D, PrimType Type, bool IsConst, bool IsTemporary,
179 UnknownSize);
180
181 /// Allocates a descriptor for an array of composites.
182 Descriptor(DeclOrExpr D, const Type *SourceTy, const Descriptor *Elem,
183 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
184
185 /// Allocates a descriptor for an array of composites of unknown size.
186 Descriptor(DeclOrExpr D, const Descriptor *Elem, bool IsTemporary,
187 UnknownSize);
188
189 /// Allocates a descriptor for a record.
190 Descriptor(DeclOrExpr D, const Record *R, bool IsConst, bool IsTemporary,
191 bool IsMutable, bool IsVolatile);
192
193 /// Allocates a dummy descriptor.
194 Descriptor(DeclOrExpr D);
195
196 QualType getType() const;
197 QualType getElemQualType() const;
198 QualType getDataType(const ASTContext &Ctx) const;
199 SourceLocation getLocation() const { return Source.getLocation(); }
200 SourceInfo getLoc() const;
201
202 const Decl *asDecl() const { return Source.asDecl(); }
203 const Expr *asExpr() const { return Source.asExpr(); }
204 DeclOrExpr getSource() const { return Source; }
205
206 const ValueDecl *asValueDecl() const {
207 return dyn_cast_if_present<ValueDecl>(Val: asDecl());
208 }
209
210 const VarDecl *asVarDecl() const {
211 return dyn_cast_if_present<VarDecl>(Val: asDecl());
212 }
213
214 const FieldDecl *asFieldDecl() const {
215 return dyn_cast_if_present<FieldDecl>(Val: asDecl());
216 }
217
218 const RecordDecl *asRecordDecl() const {
219 return dyn_cast_if_present<RecordDecl>(Val: asDecl());
220 }
221
222 template <typename T> const T *getAs() const {
223 return dyn_cast_if_present<T>(asDecl());
224 }
225
226 /// Returns the size of the object without metadata.
227 unsigned getSize() const {
228 assert(!isUnknownSizeArray() && "Array of unknown size");
229 return Size;
230 }
231
232 PrimType getPrimType() const {
233 assert(isPrimitiveArray() || isPrimitive());
234 return *PrimT;
235 }
236
237 /// Returns the allocated size, including metadata.
238 unsigned getAllocSize() const { return AllocSize; }
239 /// returns the size of an element when the structure is viewed as an array.
240 unsigned getElemSize() const { return ElemSize; }
241 /// Returns the element data size, i.e. not what the size of
242 /// our primitive data type is, but what the data size of that is.
243 /// E.g., for PT_SInt32, that's 4 bytes.
244 unsigned getElemDataSize() const;
245
246 /// Returns the number of elements stored in the block.
247 unsigned getNumElems() const {
248 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
249 }
250
251 /// Checks if the descriptor is of an array of primitives.
252 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
253 /// Checks if the descriptor is of an array of composites.
254 bool isCompositeArray() const { return IsArray && ElemDesc; }
255 /// Checks if the descriptor is of an array of zero size.
256 bool isZeroSizeArray() const { return Size == 0; }
257 /// Checks if the descriptor is of an array of unknown size.
258 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
259
260 /// Checks if the descriptor is of a primitive.
261 bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
262
263 /// Checks if the descriptor is of an array.
264 bool isArray() const { return IsArray; }
265 /// Checks if the descriptor is of a record.
266 bool isRecord() const { return !IsArray && ElemRecord; }
267 /// Checks if the descriptor is of a union.
268 bool isUnion() const;
269
270 /// Whether variables of this descriptor need their destructor called or not.
271 bool hasTrivialDtor() const;
272
273 /// Returns the kind of dynamic allocation source of this block.
274 static DynAllocKind getDynAllocKindForExpr(const Expr *E);
275 /// Returns the kind of dynamic allocation source of this block.
276 DynAllocKind getDynAllocKind() const {
277 return asExpr() ? getDynAllocKindForExpr(E: asExpr()) : DynAllocKind::None;
278 }
279 /// Checks if the descriptor is of a dynamic allocation.
280 bool isDynAlloc() const { return getDynAllocKind() != DynAllocKind::None; }
281
282 /// Compute the alignment for a dynamic allocation.
283 CharUnits computeAlignForDynamicAlloc(const ASTContext &Ctx) const;
284
285 void dump() const;
286 void dump(llvm::raw_ostream &OS) const;
287 void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;
288};
289} // namespace interp
290} // namespace clang
291
292#endif
293