clang 22.0.0git
Descriptor.h
Go to the documentation of this file.
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 "PrimType.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
19
20namespace clang {
21namespace interp {
22class Block;
23class Record;
24class SourceInfo;
25struct InitMap;
26struct Descriptor;
27enum PrimType : uint8_t;
28
29using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
30using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>;
31
32/// Invoked whenever a block is created. The constructor method fills in the
33/// inline descriptors of all fields and array elements. It also initializes
34/// all the fields which contain non-trivial types.
35using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,
36 bool IsMutable, bool IsVolatile, bool IsActive,
37 bool InUnion, const Descriptor *FieldDesc);
38
39/// Invoked when a block is destroyed. Invokes the destructors of all
40/// non-trivial nested fields of arrays and records.
41using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
42 const Descriptor *FieldDesc);
43
44enum class GlobalInitState {
48};
49
50/// Descriptor used for global variables.
51struct alignas(void *) GlobalInlineDescriptor {
53};
54static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");
55
56enum class Lifetime : uint8_t {
57 Started,
58 Ended,
59};
60
61/// Inline descriptor embedded in structures and arrays.
62///
63/// Such descriptors precede all composite array elements and structure fields.
64/// If the base of a pointer is not zero, the base points to the end of this
65/// structure. The offset field is used to traverse the pointer chain up
66/// to the root structure which allocated the object.
68 /// Offset inside the structure/array.
69 unsigned Offset;
70
71 /// Flag indicating if the storage is constant or not.
72 /// Relevant for primitive fields.
73 LLVM_PREFERRED_TYPE(bool)
75 /// For primitive fields, it indicates if the field was initialized.
76 /// Primitive fields in static storage are always initialized.
77 /// Arrays are always initialized, even though their elements might not be.
78 /// Base classes are initialized after the constructor is invoked.
79 LLVM_PREFERRED_TYPE(bool)
80 unsigned IsInitialized : 1;
81 /// Flag indicating if the field is an embedded base class.
82 LLVM_PREFERRED_TYPE(bool)
83 unsigned IsBase : 1;
84 /// Flag inidcating if the field is a virtual base class.
85 LLVM_PREFERRED_TYPE(bool)
86 unsigned IsVirtualBase : 1;
87 /// Flag indicating if the field is the active member of a union.
88 LLVM_PREFERRED_TYPE(bool)
89 unsigned IsActive : 1;
90 /// Flag indicating if this field is in a union (even if nested).
91 LLVM_PREFERRED_TYPE(bool)
92 unsigned InUnion : 1;
93 /// Flag indicating if the field is mutable (if in a record).
94 LLVM_PREFERRED_TYPE(bool)
95 unsigned IsFieldMutable : 1;
96 /// Flag indicating if this field is a const field nested in
97 /// a mutable parent field.
98 LLVM_PREFERRED_TYPE(bool)
99 unsigned IsConstInMutable : 1;
100 /// Flag indicating if the field is an element of a composite array.
101 LLVM_PREFERRED_TYPE(bool)
102 unsigned IsArrayElement : 1;
103 LLVM_PREFERRED_TYPE(bool)
104 unsigned IsVolatile : 1;
105
107
109
114 Desc(D) {}
115
116 void dump() const { dump(llvm::errs()); }
117 void dump(llvm::raw_ostream &OS) const;
118};
119static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
120
121/// Describes a memory block created by an allocation site.
122struct Descriptor final {
123private:
124 /// Original declaration, used to emit the error message.
125 const DeclTy Source;
126 const Type *SourceType = nullptr;
127 /// Size of an element, in host bytes.
128 const unsigned ElemSize;
129 /// Size of the storage, in host bytes.
130 const unsigned Size;
131 /// Size of the metadata.
132 const unsigned MDSize;
133 /// Size of the allocation (storage + metadata), in host bytes.
134 const unsigned AllocSize;
135
136 /// Value to denote arrays of unknown size.
137 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
138
139public:
140 /// Token to denote structures of unknown size.
141 struct UnknownSize {};
142
143 using MetadataSize = std::optional<unsigned>;
144 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
145 static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);
146
147 /// Maximum number of bytes to be used for array elements.
148 static constexpr unsigned MaxArrayElemBytes =
149 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -
150 align(std::max(*InlineDescMD, *GlobalMD));
151
152 /// Pointer to the record, if block contains records.
153 const Record *const ElemRecord = nullptr;
154 /// Descriptor of the array element.
155 const Descriptor *const ElemDesc = nullptr;
156 /// The primitive type this descriptor was created for,
157 /// or the primitive element type in case this is
158 /// a primitive array.
159 const OptPrimType PrimT = std::nullopt;
160 /// Flag indicating if the block is mutable.
161 const bool IsConst = false;
162 /// Flag indicating if a field is mutable.
163 const bool IsMutable = false;
164 /// Flag indicating if the block is a temporary.
165 const bool IsTemporary = false;
166 const bool IsVolatile = false;
167 /// Flag indicating if the block is an array.
168 const bool IsArray = false;
169 bool IsConstexprUnknown = false;
170
171 /// Storage management methods.
172 const BlockCtorFn CtorFn = nullptr;
173 const BlockDtorFn DtorFn = nullptr;
174
175 /// Allocates a descriptor for a primitive.
176 Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,
177 MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable,
178 bool IsVolatile);
179
180 /// Allocates a descriptor for an array of primitives.
181 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
182 bool IsConst, bool IsTemporary, bool IsMutable);
183
184 /// Allocates a descriptor for an array of primitives of unknown size.
185 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst,
187
188 /// Allocates a descriptor for an array of composites.
189 Descriptor(const DeclTy &D, const Type *SourceTy, const Descriptor *Elem,
190 MetadataSize MD, unsigned NumElems, bool IsConst, bool IsTemporary,
191 bool IsMutable);
192
193 /// Allocates a descriptor for an array of composites of unknown size.
194 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
196
197 /// Allocates a descriptor for a record.
198 Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
199 bool IsTemporary, bool IsMutable, bool IsVolatile);
200
201 /// Allocates a dummy descriptor.
202 Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);
203
204 QualType getType() const;
206 QualType getDataType(const ASTContext &Ctx) const;
208 SourceInfo getLoc() const;
209
210 const Decl *asDecl() const { return dyn_cast<const Decl *>(Source); }
211 const Expr *asExpr() const { return dyn_cast<const Expr *>(Source); }
212 const DeclTy &getSource() const { return Source; }
213
214 const ValueDecl *asValueDecl() const {
215 return dyn_cast_if_present<ValueDecl>(asDecl());
216 }
217
218 const VarDecl *asVarDecl() const {
219 return dyn_cast_if_present<VarDecl>(asDecl());
220 }
221
222 const FieldDecl *asFieldDecl() const {
223 return dyn_cast_if_present<FieldDecl>(asDecl());
224 }
225
226 const RecordDecl *asRecordDecl() const {
227 return dyn_cast_if_present<RecordDecl>(asDecl());
228 }
229
230 /// Returns the size of the object without metadata.
231 unsigned getSize() const {
232 assert(!isUnknownSizeArray() && "Array of unknown size");
233 return Size;
234 }
235
237 assert(isPrimitiveArray() || isPrimitive());
238 return *PrimT;
239 }
240
241 /// Returns the allocated size, including metadata.
242 unsigned getAllocSize() const { return AllocSize; }
243 /// returns the size of an element when the structure is viewed as an array.
244 unsigned getElemSize() const { return ElemSize; }
245 /// Returns the size of the metadata.
246 unsigned getMetadataSize() const { return MDSize; }
247
248 /// Returns the number of elements stored in the block.
249 unsigned getNumElems() const {
250 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
251 }
252
253 /// Checks if the descriptor is of an array of primitives.
254 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
255 /// Checks if the descriptor is of an array of composites.
256 bool isCompositeArray() const { return IsArray && ElemDesc; }
257 /// Checks if the descriptor is of an array of zero size.
258 bool isZeroSizeArray() const { return Size == 0; }
259 /// Checks if the descriptor is of an array of unknown size.
260 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
261
262 /// Checks if the descriptor is of a primitive.
263 bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
264
265 /// Checks if the descriptor is of an array.
266 bool isArray() const { return IsArray; }
267 /// Checks if the descriptor is of a record.
268 bool isRecord() const { return !IsArray && ElemRecord; }
269 /// Checks if the descriptor is of a union.
270 bool isUnion() const;
271
272 /// Whether variables of this descriptor need their destructor called or not.
273 bool hasTrivialDtor() const;
274
275 void dump() const;
276 void dump(llvm::raw_ostream &OS) const;
277 void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;
278};
279
280/// Bitfield tracking the initialisation status of elements of primitive arrays.
281struct InitMap final {
282private:
283 /// Type packing bits.
284 using T = uint64_t;
285 /// Bits stored in a single field.
286 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
287
288public:
289 /// Initializes the map with no fields set.
290 explicit InitMap(unsigned N);
291
292private:
293 friend class Pointer;
294
295 /// Returns a pointer to storage.
296 T *data() { return Data.get(); }
297 const T *data() const { return Data.get(); }
298
299 /// Initializes an element. Returns true when object if fully initialized.
300 bool initializeElement(unsigned I);
301
302 /// Checks if an element was initialized.
303 bool isElementInitialized(unsigned I) const;
304
305 static constexpr size_t numFields(unsigned N) {
306 return (N + PER_FIELD - 1) / PER_FIELD;
307 }
308 /// Number of fields not initialized.
309 unsigned UninitFields;
310 std::unique_ptr<T[]> Data;
311};
312
313} // namespace interp
314} // namespace clang
315
316#endif
const Decl * D
const CFGBlock * Block
Definition: HTMLLogger.cpp:152
llvm::MachO::Record Record
Definition: MachO.h:31
const char * Data
__DEVICE__ int max(int __a, int __b)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
This represents one expression.
Definition: Expr.h:112
Represents a member of a struct/union/class.
Definition: Decl.h:3157
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents a struct/union/class.
Definition: Decl.h:4309
Encodes a location in the source.
The base class of the type hierarchy.
Definition: TypeBase.h:1833
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:711
Represents a variable declaration or definition.
Definition: Decl.h:925
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:44
A pointer to a memory block, live or dead.
Definition: Pointer.h:90
Structure/Class descriptor.
Definition: Record.h:25
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:73
#define CHAR_BIT
Definition: limits.h:71
void(*)(Block *Storage, std::byte *FieldPtr, bool IsConst, bool IsMutable, bool IsVolatile, bool IsActive, bool InUnion, const Descriptor *FieldDesc) BlockCtorFn
Invoked whenever a block is created.
Definition: Descriptor.h:37
std::optional< std::pair< bool, std::shared_ptr< InitMap > > > InitMapPtr
Definition: Descriptor.h:30
void(*)(Block *Storage, std::byte *FieldPtr, const Descriptor *FieldDesc) BlockDtorFn
Invoked when a block is destroyed.
Definition: Descriptor.h:42
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:185
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:29
The JSON file list parser is used to communicate input to InstallAPI.
#define false
Definition: stdbool.h:26
Token to denote structures of unknown size.
Definition: Descriptor.h:141
Describes a memory block created by an allocation site.
Definition: Descriptor.h:122
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:161
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:242
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:249
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:231
void dumpFull(unsigned Offset=0, unsigned Indent=0) const
Dump descriptor, including all valid offsets.
Definition: Disasm.cpp:402
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:263
QualType getElemQualType() const
Definition: Descriptor.cpp:390
const RecordDecl * asRecordDecl() const
Definition: Descriptor.h:226
bool hasTrivialDtor() const
Whether variables of this descriptor need their destructor called or not.
Definition: Descriptor.cpp:454
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition: Descriptor.h:256
const BlockDtorFn DtorFn
Definition: Descriptor.h:173
const OptPrimType PrimT
The primitive type this descriptor was created for, or the primitive element type in case this is a p...
Definition: Descriptor.h:159
static constexpr MetadataSize GlobalMD
Definition: Descriptor.h:145
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:214
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:172
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition: Descriptor.h:148
QualType getType() const
Definition: Descriptor.cpp:371
const DeclTy & getSource() const
Definition: Descriptor.h:212
const Decl * asDecl() const
Definition: Descriptor.h:210
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:155
SourceInfo getLoc() const
Definition: Descriptor.cpp:446
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:246
SourceLocation getLocation() const
Definition: Descriptor.cpp:438
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:163
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:144
QualType getDataType(const ASTContext &Ctx) const
Definition: Descriptor.cpp:414
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:143
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:260
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition: Descriptor.h:244
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:168
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:254
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition: Descriptor.h:258
const FieldDecl * asFieldDecl() const
Definition: Descriptor.h:222
const VarDecl * asVarDecl() const
Definition: Descriptor.h:218
PrimType getPrimType() const
Definition: Descriptor.h:236
bool isRecord() const
Checks if the descriptor is of a record.
Definition: Descriptor.h:268
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:165
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:153
bool isUnion() const
Checks if the descriptor is of a union.
Definition: Descriptor.cpp:470
const Expr * asExpr() const
Definition: Descriptor.h:211
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:266
Descriptor used for global variables.
Definition: Descriptor.h:51
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition: Descriptor.h:281
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:67
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:89
unsigned IsConstInMutable
Flag indicating if this field is a const field nested in a mutable parent field.
Definition: Descriptor.h:99
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:83
unsigned IsVirtualBase
Flag inidcating if the field is a virtual base class.
Definition: Descriptor.h:86
unsigned InUnion
Flag indicating if this field is in a union (even if nested).
Definition: Descriptor.h:92
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:69
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:80
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:74
unsigned IsArrayElement
Flag indicating if the field is an element of a composite array.
Definition: Descriptor.h:102
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:95