clang 24.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 "../ExprConstShared.h"
17#include "DeclOrExpr.h"
18#include "InitMap.h"
19#include "PrimType.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/Expr.h"
22
23namespace clang {
24namespace interp {
25class Block;
26class Record;
27class SourceInfo;
28struct Descriptor;
29enum PrimType : uint8_t;
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 DeclOrExpr 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 allocation (storage), in host bytes.
133 const unsigned AllocSize;
134
135 /// Value to denote arrays of unknown size.
136 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
137
138public:
139 /// Token to denote structures of unknown size.
140 struct UnknownSize {};
141
142 /// Maximum number of bytes to be used for array elements.
143 static constexpr unsigned MaxArrayElemBytes =
144 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr);
145
146 /// Pointer to the record, if block contains records.
147 const Record *const ElemRecord = nullptr;
148 /// Descriptor of the array element.
149 const Descriptor *const ElemDesc = nullptr;
150 /// The primitive type this descriptor was created for,
151 /// or the primitive element type in case this is
152 /// a primitive array.
153 const OptPrimType PrimT = std::nullopt;
154 /// Flag indicating if the block is mutable.
155 const bool IsConst = false;
156 /// Flag indicating if a field is mutable.
157 const bool IsMutable = false;
158 /// Flag indicating if the block is a temporary.
159 const bool IsTemporary = false;
160 const bool IsVolatile = false;
161 /// Flag indicating if the block is an array.
162 const bool IsArray = false;
163 bool IsConstexprUnknown = false;
164
165 /// Storage management methods.
166 const BlockCtorFn CtorFn = nullptr;
167 const BlockDtorFn DtorFn = nullptr;
168
169 /// Allocates a descriptor for a primitive.
170 Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, bool IsConst,
171 bool IsTemporary, bool IsMutable, bool IsVolatile);
172
173 /// Allocates a descriptor for an array of primitives.
174 Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, size_t NumElems,
175 bool IsConst, bool IsTemporary, bool IsMutable, bool IsVolatile);
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(DeclOrExpr D, const Type *SourceTy, const Descriptor *Elem,
183 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
184
185 /// Allocates a descriptor for an array of composites of unknown size.
186 Descriptor(DeclOrExpr D, const Descriptor *Elem, bool IsTemporary,
188
189 /// Allocates a descriptor for a record.
190 Descriptor(DeclOrExpr D, const Record *R, bool IsConst, bool IsTemporary,
191 bool IsMutable, bool IsVolatile);
192
193 /// Allocates a dummy descriptor.
195
196 QualType getType() const;
198 QualType getDataType(const ASTContext &Ctx) const;
199 SourceLocation getLocation() const { return Source.getLocation(); }
200 SourceInfo getLoc() const;
201
202 const Decl *asDecl() const { return Source.asDecl(); }
203 const Expr *asExpr() const { return Source.asExpr(); }
204 DeclOrExpr getSource() const { return Source; }
205
206 const ValueDecl *asValueDecl() const {
207 return dyn_cast_if_present<ValueDecl>(asDecl());
208 }
209
210 const VarDecl *asVarDecl() const {
211 return dyn_cast_if_present<VarDecl>(asDecl());
212 }
213
214 const FieldDecl *asFieldDecl() const {
215 return dyn_cast_if_present<FieldDecl>(asDecl());
216 }
217
218 const RecordDecl *asRecordDecl() const {
219 return dyn_cast_if_present<RecordDecl>(asDecl());
220 }
221
222 template <typename T> const T *getAs() const {
223 return dyn_cast_if_present<T>(asDecl());
224 }
225
226 /// Returns the size of the object without metadata.
227 unsigned getSize() const {
228 assert(!isUnknownSizeArray() && "Array of unknown size");
229 return Size;
230 }
231
233 assert(isPrimitiveArray() || isPrimitive());
234 return *PrimT;
235 }
236
237 /// Returns the allocated size, including metadata.
238 unsigned getAllocSize() const { return AllocSize; }
239 /// returns the size of an element when the structure is viewed as an array.
240 unsigned getElemSize() const { return ElemSize; }
241 /// Returns the element data size, i.e. not what the size of
242 /// our primitive data type is, but what the data size of that is.
243 /// E.g., for PT_SInt32, that's 4 bytes.
244 unsigned getElemDataSize() const;
245
246 /// Returns the number of elements stored in the block.
247 unsigned getNumElems() const {
248 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
249 }
250
251 /// Checks if the descriptor is of an array of primitives.
252 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
253 /// Checks if the descriptor is of an array of composites.
254 bool isCompositeArray() const { return IsArray && ElemDesc; }
255 /// Checks if the descriptor is of an array of zero size.
256 bool isZeroSizeArray() const { return Size == 0; }
257 /// Checks if the descriptor is of an array of unknown size.
258 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
259
260 /// Checks if the descriptor is of a primitive.
261 bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
262
263 /// Checks if the descriptor is of an array.
264 bool isArray() const { return IsArray; }
265 /// Checks if the descriptor is of a record.
266 bool isRecord() const { return !IsArray && ElemRecord; }
267 /// Checks if the descriptor is of a union.
268 bool isUnion() const;
269
270 /// Whether variables of this descriptor need their destructor called or not.
271 bool hasTrivialDtor() const;
272
273 /// Returns the kind of dynamic allocation source of this block.
275 /// Returns the kind of dynamic allocation source of this block.
279 /// Checks if the descriptor is of a dynamic allocation.
280 bool isDynAlloc() const { return getDynAllocKind() != DynAllocKind::None; }
281
282 /// Compute the alignment for a dynamic allocation.
284
285 void dump() const;
286 void dump(llvm::raw_ostream &OS) const;
287 void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;
288};
289} // namespace interp
290} // namespace clang
291
292#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:245
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:113
Represents a member of a struct/union/class.
Definition Decl.h:3299
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4464
Encodes a location in the source.
The base class of the type hierarchy.
Definition TypeBase.h:1879
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:713
Represents a variable declaration or definition.
Definition Decl.h:933
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:43
Structure/Class descriptor.
Definition Record.h:27
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
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:35
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
Top level wrappers for InstallAPI frontend operations.
DynAllocKind
Kind of source for a dynamic allocation.
Definition APValue.h:69
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition JsonSupport.h:21
const FunctionProtoType * T
__builtin_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 __builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t uint32_t uint32_t __builtin_riscv_mhacc_i32 __builtin_riscv_mhaccu_u32 __builtin_riscv_mhaccsu_i32 __builtin_riscv_mulq_i32 uint8_t
#define false
Definition stdbool.h:26
Token to denote structures of unknown size.
Definition Descriptor.h:140
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:155
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:238
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:247
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:227
void dumpFull(unsigned Offset=0, unsigned Indent=0) const
Dump descriptor, including all valid offsets.
Definition Disasm.cpp:465
SourceLocation getLocation() const
Definition Descriptor.h:199
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:261
QualType getElemQualType() const
const RecordDecl * asRecordDecl() const
Definition Descriptor.h:218
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:254
const BlockDtorFn DtorFn
Definition Descriptor.h:167
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:153
const ValueDecl * asValueDecl() const
Definition Descriptor.h:206
const BlockCtorFn CtorFn
Storage management methods.
Definition Descriptor.h:166
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition Descriptor.h:143
QualType getType() const
const Decl * asDecl() const
Definition Descriptor.h:202
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:149
SourceInfo getLoc() const
const bool IsMutable
Flag indicating if a field is mutable.
Definition Descriptor.h:157
QualType getDataType(const ASTContext &Ctx) const
bool isDynAlloc() const
Checks if the descriptor is of a dynamic allocation.
Definition Descriptor.h:280
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition Descriptor.h:258
static DynAllocKind getDynAllocKindForExpr(const Expr *E)
Returns the kind of dynamic allocation source of this block.
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition Descriptor.h:240
const bool IsArray
Flag indicating if the block is an array.
Definition Descriptor.h:162
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:252
DynAllocKind getDynAllocKind() const
Returns the kind of dynamic allocation source of this block.
Definition Descriptor.h:276
DeclOrExpr getSource() const
Definition Descriptor.h:204
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition Descriptor.h:256
const FieldDecl * asFieldDecl() const
Definition Descriptor.h:214
Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, bool IsConst, bool IsTemporary, bool IsMutable, bool IsVolatile)
Allocates a descriptor for a primitive.
const VarDecl * asVarDecl() const
Definition Descriptor.h:210
PrimType getPrimType() const
Definition Descriptor.h:232
CharUnits computeAlignForDynamicAlloc(const ASTContext &Ctx) const
Compute the alignment for a dynamic allocation.
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:266
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition Descriptor.h:159
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:147
bool isUnion() const
Checks if the descriptor is of a union.
const T * getAs() const
Definition Descriptor.h:222
const Expr * asExpr() const
Definition Descriptor.h:203
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:264
Descriptor used for global variables.
Definition Descriptor.h:50
A pointer-sized struct we use to allocate into data storage.
Definition InitMap.h:79
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