clang 23.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 "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
48
49/// Descriptor used for global variables.
53static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");
54
55enum class Lifetime : uint8_t {
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.
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 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
108
110
116
117 void dump() const { dump(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(std::max(*InlineDescMD, *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, PrimType Type, MetadataSize MD, size_t NumElems,
183 bool IsConst, bool IsTemporary, bool IsMutable);
184
185 /// Allocates a descriptor for an array of primitives of unknown size.
186 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst,
188
189 /// Allocates a descriptor for an array of composites.
190 Descriptor(const DeclTy &D, const Type *SourceTy, const Descriptor *Elem,
191 MetadataSize MD, unsigned NumElems, bool IsConst, bool IsTemporary,
192 bool IsMutable);
193
194 /// Allocates a descriptor for an array of composites of unknown size.
195 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
197
198 /// Allocates a descriptor for a record.
199 Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
200 bool IsTemporary, bool IsMutable, bool IsVolatile);
201
202 /// Allocates a dummy descriptor.
203 Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);
204
205 QualType getType() const;
207 QualType getDataType(const ASTContext &Ctx) const;
210 SourceInfo getLoc() const;
211
212 const Decl *asDecl() const { return dyn_cast<const Decl *>(Source); }
213 const Expr *asExpr() const { return dyn_cast<const Expr *>(Source); }
214 const DeclTy &getSource() const { return Source; }
215
216 const ValueDecl *asValueDecl() const {
217 return dyn_cast_if_present<ValueDecl>(asDecl());
218 }
219
220 const VarDecl *asVarDecl() const {
221 return dyn_cast_if_present<VarDecl>(asDecl());
222 }
223
224 const FieldDecl *asFieldDecl() const {
225 return dyn_cast_if_present<FieldDecl>(asDecl());
226 }
227
228 const RecordDecl *asRecordDecl() const {
229 return dyn_cast_if_present<RecordDecl>(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
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
__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:227
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:3178
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4343
Encodes a location in the source.
The base class of the type hierarchy.
Definition TypeBase.h:1871
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
Represents a variable declaration or definition.
Definition Decl.h:924
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
Structure/Class descriptor.
Definition Record.h:25
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
void(*)(Block *Storage, std::byte *FieldPtr, const Descriptor *FieldDesc) BlockDtorFn
Invoked when a block is destroyed.
Definition Descriptor.h:40
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition Descriptor.h:29
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
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:34
The JSON file list parser is used to communicate input to InstallAPI.
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition JsonSupport.h:21
#define false
Definition stdbool.h:26
Token to denote structures of unknown size.
Definition Descriptor.h:142
Describes a memory block created by an allocation site.
Definition Descriptor.h:123
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:162
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:248
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:260
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:237
void dumpFull(unsigned Offset=0, unsigned Indent=0) const
Dump descriptor, including all valid offsets.
Definition Disasm.cpp:470
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:274
QualType getElemQualType() const
const RecordDecl * asRecordDecl() const
Definition Descriptor.h:228
bool hasTrivialDtor() const
Whether variables of this descriptor need their destructor called or not.
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition Descriptor.h:267
const BlockDtorFn DtorFn
Definition Descriptor.h:174
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:160
static constexpr MetadataSize GlobalMD
Definition Descriptor.h:146
const ValueDecl * asValueDecl() const
Definition Descriptor.h:216
const BlockCtorFn CtorFn
Storage management methods.
Definition Descriptor.h:173
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition Descriptor.h:149
QualType getType() const
const DeclTy & getSource() const
Definition Descriptor.h:214
const Decl * asDecl() const
Definition Descriptor.h:212
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:156
SourceInfo getLoc() const
Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type, MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable, bool IsVolatile)
Allocates a descriptor for a primitive.
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition Descriptor.h:257
SourceLocation getLocation() const
const bool IsMutable
Flag indicating if a field is mutable.
Definition Descriptor.h:164
static constexpr MetadataSize InlineDescMD
Definition Descriptor.h:145
QualType getDataType(const ASTContext &Ctx) const
std::optional< unsigned > MetadataSize
Definition Descriptor.h:144
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition Descriptor.h:271
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition Descriptor.h:250
const bool IsArray
Flag indicating if the block is an array.
Definition Descriptor.h:169
unsigned getElemDataSize() const
Returns the element data size, i.e.
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:265
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition Descriptor.h:269
const FieldDecl * asFieldDecl() const
Definition Descriptor.h:224
const VarDecl * asVarDecl() const
Definition Descriptor.h:220
QualType getDataElemType() const
PrimType getPrimType() const
Definition Descriptor.h:242
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:279
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition Descriptor.h:166
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:154
bool isUnion() const
Checks if the descriptor is of a union.
const T * getAs() const
Definition Descriptor.h:232
const Expr * asExpr() const
Definition Descriptor.h:213
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:277
Descriptor used for global variables.
Definition Descriptor.h:50
A pointer-sized struct we use to allocate into data storage.
Definition InitMap.h:79
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:90
unsigned IsConstInMutable
Flag indicating if this field is a const field nested in a mutable parent field.
Definition Descriptor.h:100
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition Descriptor.h:84
unsigned IsVirtualBase
Flag inidcating if the field is a virtual base class.
Definition Descriptor.h:87
unsigned InUnion
Flag 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: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 IsArrayElement
Flag indicating if the field is an element of a composite array.
Definition Descriptor.h:103
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition Descriptor.h:96
InlineDescriptor(const Descriptor *D)
Definition Descriptor.h:111