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