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 "InitMap.h"
17#include "PrimType.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/Expr.h"
20
21namespace clang {
22namespace interp {
23class Block;
24class Record;
25class SourceInfo;
26struct Descriptor;
27enum PrimType : uint8_t;
28
29using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
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 DeclTy 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 metadata.
133 const unsigned MDSize;
134 /// Size of the allocation (storage + metadata), in host bytes.
135 const unsigned AllocSize;
136
137 /// Value to denote arrays of unknown size.
138 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
139
140public:
141 /// Token to denote structures of unknown size.
142 struct UnknownSize {};
143
144 using MetadataSize = std::optional<unsigned>;
145 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
146 static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);
147
148 /// Maximum number of bytes to be used for array elements.
149 static constexpr unsigned MaxArrayElemBytes =
150 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -
151 align(Size: std::max(a: *InlineDescMD, b: *GlobalMD));
152
153 /// Pointer to the record, if block contains records.
154 const Record *const ElemRecord = nullptr;
155 /// Descriptor of the array element.
156 const Descriptor *const ElemDesc = nullptr;
157 /// The primitive type this descriptor was created for,
158 /// or the primitive element type in case this is
159 /// a primitive array.
160 const OptPrimType PrimT = std::nullopt;
161 /// Flag indicating if the block is mutable.
162 const bool IsConst = false;
163 /// Flag indicating if a field is mutable.
164 const bool IsMutable = false;
165 /// Flag indicating if the block is a temporary.
166 const bool IsTemporary = false;
167 const bool IsVolatile = false;
168 /// Flag indicating if the block is an array.
169 const bool IsArray = false;
170 bool IsConstexprUnknown = false;
171
172 /// Storage management methods.
173 const BlockCtorFn CtorFn = nullptr;
174 const BlockDtorFn DtorFn = nullptr;
175
176 /// Allocates a descriptor for a primitive.
177 Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,
178 MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable,
179 bool IsVolatile);
180
181 /// Allocates a descriptor for an array of primitives.
182 Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,
183 MetadataSize MD, size_t NumElems, bool IsConst, bool IsTemporary,
184 bool IsMutable, bool IsVolatile);
185
186 /// Allocates a descriptor for an array of primitives of unknown size.
187 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst,
188 bool IsTemporary, UnknownSize);
189
190 /// Allocates a descriptor for an array of composites.
191 Descriptor(const DeclTy &D, const Type *SourceTy, const Descriptor *Elem,
192 MetadataSize MD, unsigned NumElems, bool IsConst, bool IsTemporary,
193 bool IsMutable);
194
195 /// Allocates a descriptor for an array of composites of unknown size.
196 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
197 bool IsTemporary, UnknownSize);
198
199 /// Allocates a descriptor for a record.
200 Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
201 bool IsTemporary, bool IsMutable, bool IsVolatile);
202
203 /// Allocates a dummy descriptor.
204 Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);
205
206 QualType getType() const;
207 QualType getElemQualType() const;
208 QualType getDataType(const ASTContext &Ctx) const;
209 SourceLocation getLocation() const;
210 SourceInfo getLoc() const;
211
212 const Decl *asDecl() const { return dyn_cast<const Decl *>(Val: Source); }
213 const Expr *asExpr() const { return dyn_cast<const Expr *>(Val: Source); }
214 const DeclTy &getSource() const { return Source; }
215
216 const ValueDecl *asValueDecl() const {
217 return dyn_cast_if_present<ValueDecl>(Val: asDecl());
218 }
219
220 const VarDecl *asVarDecl() const {
221 return dyn_cast_if_present<VarDecl>(Val: asDecl());
222 }
223
224 const FieldDecl *asFieldDecl() const {
225 return dyn_cast_if_present<FieldDecl>(Val: asDecl());
226 }
227
228 const RecordDecl *asRecordDecl() const {
229 return dyn_cast_if_present<RecordDecl>(Val: asDecl());
230 }
231
232 template <typename T> const T *getAs() const {
233 return dyn_cast_if_present<T>(asDecl());
234 }
235
236 /// Returns the size of the object without metadata.
237 unsigned getSize() const {
238 assert(!isUnknownSizeArray() && "Array of unknown size");
239 return Size;
240 }
241
242 PrimType getPrimType() const {
243 assert(isPrimitiveArray() || isPrimitive());
244 return *PrimT;
245 }
246
247 /// Returns the allocated size, including metadata.
248 unsigned getAllocSize() const { return AllocSize; }
249 /// returns the size of an element when the structure is viewed as an array.
250 unsigned getElemSize() const { return ElemSize; }
251 /// Returns the element data size, i.e. not what the size of
252 /// our primitive data type is, but what the data size of that is.
253 /// E.g., for PT_SInt32, that's 4 bytes.
254 unsigned getElemDataSize() const;
255
256 /// Returns the size of the metadata.
257 unsigned getMetadataSize() const { return MDSize; }
258
259 /// Returns the number of elements stored in the block.
260 unsigned getNumElems() const {
261 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
262 }
263
264 /// Checks if the descriptor is of an array of primitives.
265 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
266 /// Checks if the descriptor is of an array of composites.
267 bool isCompositeArray() const { return IsArray && ElemDesc; }
268 /// Checks if the descriptor is of an array of zero size.
269 bool isZeroSizeArray() const { return Size == 0; }
270 /// Checks if the descriptor is of an array of unknown size.
271 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
272
273 /// Checks if the descriptor is of a primitive.
274 bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
275
276 /// Checks if the descriptor is of an array.
277 bool isArray() const { return IsArray; }
278 /// Checks if the descriptor is of a record.
279 bool isRecord() const { return !IsArray && ElemRecord; }
280 /// Checks if the descriptor is of a union.
281 bool isUnion() const;
282
283 /// Whether variables of this descriptor need their destructor called or not.
284 bool hasTrivialDtor() const;
285
286 void dump() const;
287 void dump(llvm::raw_ostream &OS) const;
288 void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;
289};
290} // namespace interp
291} // namespace clang
292
293#endif
294