clang 20.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;
24struct InitMap;
25struct Descriptor;
26enum PrimType : unsigned;
27
28using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
29using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>;
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 IsActive, bool InUnion,
36 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
43/// Invoked when a block with pointers referencing it goes out of scope. Such
44/// blocks are persisted: the move function copies all inline descriptors and
45/// non-trivial fields, as existing pointers might need to reference those
46/// descriptors. Data is not copied since it cannot be legally read.
47using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,
48 std::byte *DstFieldPtr,
49 const Descriptor *FieldDesc);
50
51enum class GlobalInitState {
55};
56
57/// Descriptor used for global variables.
58struct alignas(void *) GlobalInlineDescriptor {
60};
61static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");
62
63/// Inline descriptor embedded in structures and arrays.
64///
65/// Such descriptors precede all composite array elements and structure fields.
66/// If the base of a pointer is not zero, the base points to the end of this
67/// structure. The offset field is used to traverse the pointer chain up
68/// to the root structure which allocated the object.
70 /// Offset inside the structure/array.
71 unsigned Offset;
72
73 /// Flag indicating if the storage is constant or not.
74 /// Relevant for primitive fields.
75 LLVM_PREFERRED_TYPE(bool)
77 /// For primitive fields, it indicates if the field was initialized.
78 /// Primitive fields in static storage are always initialized.
79 /// Arrays are always initialized, even though their elements might not be.
80 /// Base classes are initialized after the constructor is invoked.
81 LLVM_PREFERRED_TYPE(bool)
82 unsigned IsInitialized : 1;
83 /// Flag indicating if the field is an embedded base class.
84 LLVM_PREFERRED_TYPE(bool)
85 unsigned IsBase : 1;
86 /// Flag inidcating if the field is a virtual base class.
87 LLVM_PREFERRED_TYPE(bool)
88 unsigned IsVirtualBase : 1;
89 /// Flag indicating if the field is the active member of a union.
90 LLVM_PREFERRED_TYPE(bool)
91 unsigned IsActive : 1;
92 /// Flat indicating if this field is in a union (even if nested).
93 unsigned InUnion : 1;
94 LLVM_PREFERRED_TYPE(bool)
95 /// Flag indicating if the field is mutable (if in a record).
96 LLVM_PREFERRED_TYPE(bool)
97 unsigned IsFieldMutable : 1;
98
100
104
105 void dump() const { dump(llvm::errs()); }
106 void dump(llvm::raw_ostream &OS) const;
107};
108static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
109
110/// Describes a memory block created by an allocation site.
111struct Descriptor final {
112private:
113 /// Original declaration, used to emit the error message.
114 const DeclTy Source;
115 /// Size of an element, in host bytes.
116 const unsigned ElemSize;
117 /// Size of the storage, in host bytes.
118 const unsigned Size;
119 /// Size of the metadata.
120 const unsigned MDSize;
121 /// Size of the allocation (storage + metadata), in host bytes.
122 const unsigned AllocSize;
123
124 /// Value to denote arrays of unknown size.
125 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
126
127public:
128 /// Token to denote structures of unknown size.
129 struct UnknownSize {};
130
131 using MetadataSize = std::optional<unsigned>;
132 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
133 static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);
134
135 /// Maximum number of bytes to be used for array elements.
136 static constexpr unsigned MaxArrayElemBytes =
137 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -
138 align(std::max(*InlineDescMD, *GlobalMD));
139
140 /// Pointer to the record, if block contains records.
141 const Record *const ElemRecord = nullptr;
142 /// Descriptor of the array element.
143 const Descriptor *const ElemDesc = nullptr;
144 /// The primitive type this descriptor was created for,
145 /// or the primitive element type in case this is
146 /// a primitive array.
147 const std::optional<PrimType> PrimT = std::nullopt;
148 /// Flag indicating if the block is mutable.
149 const bool IsConst = false;
150 /// Flag indicating if a field is mutable.
151 const bool IsMutable = false;
152 /// Flag indicating if the block is a temporary.
153 const bool IsTemporary = false;
154 /// Flag indicating if the block is an array.
155 const bool IsArray = false;
156 /// Flag indicating if this is a dummy descriptor.
157 bool IsDummy = false;
158
159 /// Storage management methods.
160 const BlockCtorFn CtorFn = nullptr;
161 const BlockDtorFn DtorFn = nullptr;
162 const BlockMoveFn MoveFn = nullptr;
163
164 /// Allocates a descriptor for a primitive.
166 bool IsTemporary, bool IsMutable);
167
168 /// Allocates a descriptor for an array of primitives.
169 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
170 bool IsConst, bool IsTemporary, bool IsMutable);
171
172 /// Allocates a descriptor for an array of primitives of unknown size.
175
176 /// Allocates a descriptor for an array of composites.
177 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
178 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
179
180 /// Allocates a descriptor for an array of composites of unknown size.
181 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
183
184 /// Allocates a descriptor for a record.
185 Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
186 bool IsTemporary, bool IsMutable);
187
188 /// Allocates a dummy descriptor.
189 Descriptor(const DeclTy &D);
190
191 /// Make this descriptor a dummy descriptor.
192 void makeDummy() { IsDummy = true; }
193
194 QualType getType() const;
197
198 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
199 const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
200 const DeclTy &getSource() const { return Source; }
201
202 const ValueDecl *asValueDecl() const {
203 return dyn_cast_if_present<ValueDecl>(asDecl());
204 }
205
206 const VarDecl *asVarDecl() const {
207 return dyn_cast_if_present<VarDecl>(asDecl());
208 }
209
210 const FieldDecl *asFieldDecl() const {
211 return dyn_cast_if_present<FieldDecl>(asDecl());
212 }
213
214 const RecordDecl *asRecordDecl() const {
215 return dyn_cast_if_present<RecordDecl>(asDecl());
216 }
217
218 /// Returns the size of the object without metadata.
219 unsigned getSize() const {
220 assert(!isUnknownSizeArray() && "Array of unknown size");
221 return Size;
222 }
223
225 assert(isPrimitiveArray() || isPrimitive());
226 return *PrimT;
227 }
228
229 /// Returns the allocated size, including metadata.
230 unsigned getAllocSize() const { return AllocSize; }
231 /// returns the size of an element when the structure is viewed as an array.
232 unsigned getElemSize() const { return ElemSize; }
233 /// Returns the size of the metadata.
234 unsigned getMetadataSize() const { return MDSize; }
235
236 /// Returns the number of elements stored in the block.
237 unsigned getNumElems() const {
238 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
239 }
240
241 /// Checks if the descriptor is of an array of primitives.
242 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
243 /// Checks if the descriptor is of an array of composites.
244 bool isCompositeArray() const { return IsArray && ElemDesc; }
245 /// Checks if the descriptor is of an array of zero size.
246 bool isZeroSizeArray() const { return Size == 0; }
247 /// Checks if the descriptor is of an array of unknown size.
248 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
249
250 /// Checks if the descriptor is of a primitive.
251 bool isPrimitive() const { return !IsArray && !ElemRecord; }
252
253 /// Checks if the descriptor is of an array.
254 bool isArray() const { return IsArray; }
255 /// Checks if the descriptor is of a record.
256 bool isRecord() const { return !IsArray && ElemRecord; }
257 /// Checks if the descriptor is of a union.
258 bool isUnion() const;
259 /// Checks if this is a dummy descriptor.
260 bool isDummy() const { return IsDummy; }
261
262 void dump() const;
263 void dump(llvm::raw_ostream &OS) const;
264};
265
266/// Bitfield tracking the initialisation status of elements of primitive arrays.
267struct InitMap final {
268private:
269 /// Type packing bits.
270 using T = uint64_t;
271 /// Bits stored in a single field.
272 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
273
274public:
275 /// Initializes the map with no fields set.
276 explicit InitMap(unsigned N);
277
278private:
279 friend class Pointer;
280
281 /// Returns a pointer to storage.
282 T *data() { return Data.get(); }
283 const T *data() const { return Data.get(); }
284
285 /// Initializes an element. Returns true when object if fully initialized.
286 bool initializeElement(unsigned I);
287
288 /// Checks if an element was initialized.
289 bool isElementInitialized(unsigned I) const;
290
291 static constexpr size_t numFields(unsigned N) {
292 return (N + PER_FIELD - 1) / PER_FIELD;
293 }
294 /// Number of fields not initialized.
295 unsigned UninitFields;
296 std::unique_ptr<T[]> Data;
297};
298
299} // namespace interp
300} // namespace clang
301
302#endif
const Decl * D
const CFGBlock * Block
Definition: HTMLLogger.cpp:153
llvm::MachO::Record Record
Definition: MachO.h:31
const char * Data
__DEVICE__ int max(int __a, int __b)
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
This represents one expression.
Definition: Expr.h:110
Represents a member of a struct/union/class.
Definition: Decl.h:3030
A (possibly-)qualified type.
Definition: Type.h:941
Represents a struct/union/class.
Definition: Decl.h:4145
Encodes a location in the source.
The base class of the type hierarchy.
Definition: Type.h:1829
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:667
Represents a variable declaration or definition.
Definition: Decl.h:879
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
Structure/Class descriptor.
Definition: Record.h:25
#define CHAR_BIT
Definition: limits.h:71
std::optional< std::pair< bool, std::shared_ptr< InitMap > > > InitMapPtr
Definition: Descriptor.h:29
void(*)(Block *Storage, std::byte *FieldPtr, bool IsConst, bool IsMutable, bool IsActive, bool InUnion, const Descriptor *FieldDesc) BlockCtorFn
Invoked whenever a block is created.
Definition: Descriptor.h:36
void(*)(Block *Storage, std::byte *FieldPtr, const Descriptor *FieldDesc) BlockDtorFn
Invoked when a block is destroyed.
Definition: Descriptor.h:41
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:126
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
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:49
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:28
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:129
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:149
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:230
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:237
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:219
void makeDummy()
Make this descriptor a dummy descriptor.
Definition: Descriptor.h:192
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:251
QualType getElemQualType() const
Definition: Descriptor.cpp:404
const RecordDecl * asRecordDecl() const
Definition: Descriptor.h:214
const BlockMoveFn MoveFn
Definition: Descriptor.h:162
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition: Descriptor.h:244
const BlockDtorFn DtorFn
Definition: Descriptor.h:161
static constexpr MetadataSize GlobalMD
Definition: Descriptor.h:133
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:202
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:160
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition: Descriptor.h:136
QualType getType() const
Definition: Descriptor.cpp:394
const DeclTy & getSource() const
Definition: Descriptor.h:200
const Decl * asDecl() const
Definition: Descriptor.h:198
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:143
bool isDummy() const
Checks if this is a dummy descriptor.
Definition: Descriptor.h:260
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:234
SourceLocation getLocation() const
Definition: Descriptor.cpp:416
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:151
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:132
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:131
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:248
bool IsDummy
Flag indicating if this is a dummy descriptor.
Definition: Descriptor.h:157
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition: Descriptor.h:232
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:155
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:242
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition: Descriptor.h:246
const FieldDecl * asFieldDecl() const
Definition: Descriptor.h:210
const VarDecl * asVarDecl() const
Definition: Descriptor.h:206
PrimType getPrimType() const
Definition: Descriptor.h:224
bool isRecord() const
Checks if the descriptor is of a record.
Definition: Descriptor.h:256
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:153
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:141
bool isUnion() const
Checks if the descriptor is of a union.
Definition: Descriptor.cpp:424
const std::optional< PrimType > PrimT
The primitive type this descriptor was created for, or the primitive element type in case this is a p...
Definition: Descriptor.h:147
const Expr * asExpr() const
Definition: Descriptor.h:199
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:254
Descriptor used for global variables.
Definition: Descriptor.h:58
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition: Descriptor.h:267
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:69
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:91
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:85
const Descriptor * Desc
Definition: Descriptor.h:99
unsigned IsVirtualBase
Flag inidcating if the field is a virtual base class.
Definition: Descriptor.h:88
unsigned InUnion
Flat indicating if this field is in a union (even if nested).
Definition: Descriptor.h:93
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:71
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:82
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:76
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:97