clang 19.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 "clang/AST/Decl.h"
17#include "clang/AST/Expr.h"
18
19namespace clang {
20namespace interp {
21class Block;
22class Record;
23struct InitMap;
24struct Descriptor;
25enum PrimType : unsigned;
26
27using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
28using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>;
29
30/// Invoked whenever a block is created. The constructor method fills in the
31/// inline descriptors of all fields and array elements. It also initializes
32/// all the fields which contain non-trivial types.
33using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,
34 bool IsMutable, bool IsActive,
35 const Descriptor *FieldDesc);
36
37/// Invoked when a block is destroyed. Invokes the destructors of all
38/// non-trivial nested fields of arrays and records.
39using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
40 const Descriptor *FieldDesc);
41
42/// Invoked when a block with pointers referencing it goes out of scope. Such
43/// blocks are persisted: the move function copies all inline descriptors and
44/// non-trivial fields, as existing pointers might need to reference those
45/// descriptors. Data is not copied since it cannot be legally read.
46using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,
47 std::byte *DstFieldPtr,
48 const Descriptor *FieldDesc);
49
50enum class GlobalInitState {
54};
55
56/// Descriptor used for global variables.
57struct alignas(void *) GlobalInlineDescriptor {
59};
60static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");
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.
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)
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 indicating if the field is the active member of a union.
86 LLVM_PREFERRED_TYPE(bool)
87 unsigned IsActive : 1;
88 /// Flag indicating if the field is mutable (if in a record).
89 LLVM_PREFERRED_TYPE(bool)
90 unsigned IsFieldMutable : 1;
91
93
97
98 void dump() const { dump(llvm::errs()); }
99 void dump(llvm::raw_ostream &OS) const;
100};
101static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
102
103/// Describes a memory block created by an allocation site.
104struct Descriptor final {
105private:
106 /// Original declaration, used to emit the error message.
107 const DeclTy Source;
108 /// Size of an element, in host bytes.
109 const unsigned ElemSize;
110 /// Size of the storage, in host bytes.
111 const unsigned Size;
112 /// Size of the metadata.
113 const unsigned MDSize;
114 /// Size of the allocation (storage + metadata), in host bytes.
115 const unsigned AllocSize;
116
117 /// Value to denote arrays of unknown size.
118 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
119
120public:
121 /// Token to denote structures of unknown size.
122 struct UnknownSize {};
123
124 using MetadataSize = std::optional<unsigned>;
125 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
126 static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);
127
128 /// Pointer to the record, if block contains records.
129 const Record *const ElemRecord = nullptr;
130 /// Descriptor of the array element.
131 const Descriptor *const ElemDesc = nullptr;
132 /// The primitive type this descriptor was created for,
133 /// or the primitive element type in case this is
134 /// a primitive array.
135 const std::optional<PrimType> PrimT = std::nullopt;
136 /// Flag indicating if the block is mutable.
137 const bool IsConst = false;
138 /// Flag indicating if a field is mutable.
139 const bool IsMutable = false;
140 /// Flag indicating if the block is a temporary.
141 const bool IsTemporary = false;
142 /// Flag indicating if the block is an array.
143 const bool IsArray = false;
144 /// Flag indicating if this is a dummy descriptor.
145 bool IsDummy = false;
146
147 /// Storage management methods.
148 const BlockCtorFn CtorFn = nullptr;
149 const BlockDtorFn DtorFn = nullptr;
150 const BlockMoveFn MoveFn = nullptr;
151
152 /// Allocates a descriptor for a primitive.
154 bool IsTemporary, bool IsMutable);
155
156 /// Allocates a descriptor for an array of primitives.
157 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
158 bool IsConst, bool IsTemporary, bool IsMutable);
159
160 /// Allocates a descriptor for an array of primitives of unknown size.
163
164 /// Allocates a descriptor for an array of composites.
165 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
166 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
167
168 /// Allocates a descriptor for an array of composites of unknown size.
169 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
171
172 /// Allocates a descriptor for a record.
173 Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
174 bool IsTemporary, bool IsMutable);
175
176 /// Allocates a dummy descriptor.
177 Descriptor(const DeclTy &D);
178
179 /// Make this descriptor a dummy descriptor.
180 void makeDummy() { IsDummy = true; }
181
182 QualType getType() const;
185
186 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
187 const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
188 const DeclTy &getSource() const { return Source; }
189
190 const ValueDecl *asValueDecl() const {
191 return dyn_cast_if_present<ValueDecl>(asDecl());
192 }
193
194 const VarDecl *asVarDecl() const {
195 return dyn_cast_if_present<VarDecl>(asDecl());
196 }
197
198 const FieldDecl *asFieldDecl() const {
199 return dyn_cast_if_present<FieldDecl>(asDecl());
200 }
201
202 const RecordDecl *asRecordDecl() const {
203 return dyn_cast_if_present<RecordDecl>(asDecl());
204 }
205
206 /// Returns the size of the object without metadata.
207 unsigned getSize() const {
208 assert(!isUnknownSizeArray() && "Array of unknown size");
209 return Size;
210 }
211
213 assert(isPrimitiveArray() || isPrimitive());
214 return *PrimT;
215 }
216
217 /// Returns the allocated size, including metadata.
218 unsigned getAllocSize() const { return AllocSize; }
219 /// returns the size of an element when the structure is viewed as an array.
220 unsigned getElemSize() const { return ElemSize; }
221 /// Returns the size of the metadata.
222 unsigned getMetadataSize() const { return MDSize; }
223
224 /// Returns the number of elements stored in the block.
225 unsigned getNumElems() const {
226 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
227 }
228
229 /// Checks if the descriptor is of an array of primitives.
230 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
231 /// Checks if the descriptor is of an array of composites.
232 bool isCompositeArray() const { return IsArray && ElemDesc; }
233 /// Checks if the descriptor is of an array of zero size.
234 bool isZeroSizeArray() const { return Size == 0; }
235 /// Checks if the descriptor is of an array of unknown size.
236 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
237
238 /// Checks if the descriptor is of a primitive.
239 bool isPrimitive() const { return !IsArray && !ElemRecord; }
240
241 /// Checks if the descriptor is of an array.
242 bool isArray() const { return IsArray; }
243 /// Checks if the descriptor is of a record.
244 bool isRecord() const { return !IsArray && ElemRecord; }
245 /// Checks if this is a dummy descriptor.
246 bool isDummy() const { return IsDummy; }
247
248 void dump() const;
249 void dump(llvm::raw_ostream &OS) const;
250};
251
252/// Bitfield tracking the initialisation status of elements of primitive arrays.
253struct InitMap final {
254private:
255 /// Type packing bits.
256 using T = uint64_t;
257 /// Bits stored in a single field.
258 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
259
260public:
261 /// Initializes the map with no fields set.
262 explicit InitMap(unsigned N);
263
264private:
265 friend class Pointer;
266
267 /// Returns a pointer to storage.
268 T *data() { return Data.get(); }
269 const T *data() const { return Data.get(); }
270
271 /// Initializes an element. Returns true when object if fully initialized.
272 bool initializeElement(unsigned I);
273
274 /// Checks if an element was initialized.
275 bool isElementInitialized(unsigned I) const;
276
277 static constexpr size_t numFields(unsigned N) {
278 return (N + PER_FIELD - 1) / PER_FIELD;
279 }
280 /// Number of fields not initialized.
281 unsigned UninitFields;
282 std::unique_ptr<T[]> Data;
283};
284
285} // namespace interp
286} // namespace clang
287
288#endif
const Decl * D
const CFGBlock * Block
Definition: HTMLLogger.cpp:153
llvm::MachO::Record Record
Definition: MachO.h:31
const char * Data
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:3069
A (possibly-)qualified type.
Definition: Type.h:942
Represents a struct/union/class.
Definition: Decl.h:4180
Encodes a location in the source.
The base class of the type hierarchy.
Definition: Type.h:1826
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:706
Represents a variable declaration or definition.
Definition: Decl.h:918
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:80
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:28
void(*)(Block *Storage, std::byte *FieldPtr, const Descriptor *FieldDesc) BlockDtorFn
Invoked when a block is destroyed.
Definition: Descriptor.h:40
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:48
void(*)(Block *Storage, std::byte *FieldPtr, bool IsConst, bool IsMutable, bool IsActive, const Descriptor *FieldDesc) BlockCtorFn
Invoked whenever a block is created.
Definition: Descriptor.h:35
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:27
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:122
Describes a memory block created by an allocation site.
Definition: Descriptor.h:104
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:137
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:218
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:225
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:207
void makeDummy()
Make this descriptor a dummy descriptor.
Definition: Descriptor.h:180
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:239
QualType getElemQualType() const
Definition: Descriptor.cpp:373
const RecordDecl * asRecordDecl() const
Definition: Descriptor.h:202
const BlockMoveFn MoveFn
Definition: Descriptor.h:150
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition: Descriptor.h:232
const BlockDtorFn DtorFn
Definition: Descriptor.h:149
static constexpr MetadataSize GlobalMD
Definition: Descriptor.h:126
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:190
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:148
QualType getType() const
Definition: Descriptor.cpp:363
const DeclTy & getSource() const
Definition: Descriptor.h:188
const Decl * asDecl() const
Definition: Descriptor.h:186
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:131
bool isDummy() const
Checks if this is a dummy descriptor.
Definition: Descriptor.h:246
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:222
SourceLocation getLocation() const
Definition: Descriptor.cpp:385
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:139
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:125
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:124
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:236
bool IsDummy
Flag indicating if this is a dummy descriptor.
Definition: Descriptor.h:145
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition: Descriptor.h:220
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:143
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:230
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition: Descriptor.h:234
const FieldDecl * asFieldDecl() const
Definition: Descriptor.h:198
const VarDecl * asVarDecl() const
Definition: Descriptor.h:194
PrimType getPrimType() const
Definition: Descriptor.h:212
bool isRecord() const
Checks if the descriptor is of a record.
Definition: Descriptor.h:244
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:141
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:129
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:135
const Expr * asExpr() const
Definition: Descriptor.h:187
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:242
Descriptor used for global variables.
Definition: Descriptor.h:57
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition: Descriptor.h:253
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:68
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:87
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:84
const Descriptor * Desc
Definition: Descriptor.h:92
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:70
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:81
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:75
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:90