clang 22.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 {
58};
59
60/// Inline descriptor embedded in structures and arrays.
61///
62/// Such descriptors precede all composite array elements and structure fields.
63/// If the base of a pointer is not zero, the base points to the end of this
64/// structure. The offset field is used to traverse the pointer chain up
65/// to the root structure which allocated the object.
67 /// Offset inside the structure/array.
68 unsigned Offset;
69
70 /// Flag indicating if the storage is constant or not.
71 /// Relevant for primitive fields.
72 LLVM_PREFERRED_TYPE(bool)
74 /// For primitive fields, it indicates if the field was initialized.
75 /// Primitive fields in static storage are always initialized.
76 /// Arrays are always initialized, even though their elements might not be.
77 /// Base classes are initialized after the constructor is invoked.
78 LLVM_PREFERRED_TYPE(bool)
79 unsigned IsInitialized : 1;
80 /// Flag indicating if the field is an embedded base class.
81 LLVM_PREFERRED_TYPE(bool)
82 unsigned IsBase : 1;
83 /// Flag inidcating if the field is a virtual base class.
84 LLVM_PREFERRED_TYPE(bool)
85 unsigned IsVirtualBase : 1;
86 /// Flag indicating if the field is the active member of a union.
87 LLVM_PREFERRED_TYPE(bool)
88 unsigned IsActive : 1;
89 /// Flag indicating if this field is in a union (even if nested).
90 LLVM_PREFERRED_TYPE(bool)
91 unsigned InUnion : 1;
92 /// Flag indicating if the field is mutable (if in a record).
93 LLVM_PREFERRED_TYPE(bool)
94 unsigned IsFieldMutable : 1;
95 /// Flag indicating if this field is a const field nested in
96 /// a mutable parent field.
97 LLVM_PREFERRED_TYPE(bool)
98 unsigned IsConstInMutable : 1;
99 /// Flag indicating if the field is an element of a composite array.
100 LLVM_PREFERRED_TYPE(bool)
101 unsigned IsArrayElement : 1;
102 LLVM_PREFERRED_TYPE(bool)
103 unsigned IsVolatile : 1;
104
106
108
114
115 void dump() const { dump(llvm::errs()); }
116 void dump(llvm::raw_ostream &OS) const;
117};
118static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
119
120/// Describes a memory block created by an allocation site.
121struct Descriptor final {
122private:
123 /// Original declaration, used to emit the error message.
124 const DeclTy Source;
125 const Type *SourceType = nullptr;
126 /// Size of an element, in host bytes.
127 const unsigned ElemSize;
128 /// Size of the storage, in host bytes.
129 const unsigned Size;
130 /// Size of the metadata.
131 const unsigned MDSize;
132 /// Size of the allocation (storage + metadata), 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 using MetadataSize = std::optional<unsigned>;
143 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
144 static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);
145
146 /// Maximum number of bytes to be used for array elements.
147 static constexpr unsigned MaxArrayElemBytes =
148 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -
149 align(std::max(*InlineDescMD, *GlobalMD));
150
151 /// Pointer to the record, if block contains records.
152 const Record *const ElemRecord = nullptr;
153 /// Descriptor of the array element.
154 const Descriptor *const ElemDesc = nullptr;
155 /// The primitive type this descriptor was created for,
156 /// or the primitive element type in case this is
157 /// a primitive array.
158 const OptPrimType PrimT = std::nullopt;
159 /// Flag indicating if the block is mutable.
160 const bool IsConst = false;
161 /// Flag indicating if a field is mutable.
162 const bool IsMutable = false;
163 /// Flag indicating if the block is a temporary.
164 const bool IsTemporary = false;
165 const bool IsVolatile = false;
166 /// Flag indicating if the block is an array.
167 const bool IsArray = false;
168 bool IsConstexprUnknown = false;
169
170 /// Storage management methods.
171 const BlockCtorFn CtorFn = nullptr;
172 const BlockDtorFn DtorFn = nullptr;
173
174 /// Allocates a descriptor for a primitive.
175 Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,
176 MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable,
177 bool IsVolatile);
178
179 /// Allocates a descriptor for an array of primitives.
180 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
181 bool IsConst, bool IsTemporary, bool IsMutable);
182
183 /// Allocates a descriptor for an array of primitives of unknown size.
184 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst,
186
187 /// Allocates a descriptor for an array of composites.
188 Descriptor(const DeclTy &D, const Type *SourceTy, const Descriptor *Elem,
189 MetadataSize MD, unsigned NumElems, bool IsConst, bool IsTemporary,
190 bool IsMutable);
191
192 /// Allocates a descriptor for an array of composites of unknown size.
193 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
195
196 /// Allocates a descriptor for a record.
197 Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
198 bool IsTemporary, bool IsMutable, bool IsVolatile);
199
200 /// Allocates a dummy descriptor.
201 Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);
202
203 QualType getType() const;
205 QualType getDataType(const ASTContext &Ctx) const;
207 SourceInfo getLoc() const;
208
209 const Decl *asDecl() const { return dyn_cast<const Decl *>(Source); }
210 const Expr *asExpr() const { return dyn_cast<const Expr *>(Source); }
211 const DeclTy &getSource() const { return Source; }
212
213 const ValueDecl *asValueDecl() const {
214 return dyn_cast_if_present<ValueDecl>(asDecl());
215 }
216
217 const VarDecl *asVarDecl() const {
218 return dyn_cast_if_present<VarDecl>(asDecl());
219 }
220
221 const FieldDecl *asFieldDecl() const {
222 return dyn_cast_if_present<FieldDecl>(asDecl());
223 }
224
225 const RecordDecl *asRecordDecl() const {
226 return dyn_cast_if_present<RecordDecl>(asDecl());
227 }
228
229 /// Returns the size of the object without metadata.
230 unsigned getSize() const {
231 assert(!isUnknownSizeArray() && "Array of unknown size");
232 return Size;
233 }
234
236 assert(isPrimitiveArray() || isPrimitive());
237 return *PrimT;
238 }
239
240 /// Returns the allocated size, including metadata.
241 unsigned getAllocSize() const { return AllocSize; }
242 /// returns the size of an element when the structure is viewed as an array.
243 unsigned getElemSize() const { return ElemSize; }
244 /// Returns the size of the metadata.
245 unsigned getMetadataSize() const { return MDSize; }
246
247 /// Returns the number of elements stored in the block.
248 unsigned getNumElems() const {
249 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
250 }
251
252 /// Checks if the descriptor is of an array of primitives.
253 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
254 /// Checks if the descriptor is of an array of composites.
255 bool isCompositeArray() const { return IsArray && ElemDesc; }
256 /// Checks if the descriptor is of an array of zero size.
257 bool isZeroSizeArray() const { return Size == 0; }
258 /// Checks if the descriptor is of an array of unknown size.
259 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
260
261 /// Checks if the descriptor is of a primitive.
262 bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
263
264 /// Checks if the descriptor is of an array.
265 bool isArray() const { return IsArray; }
266 /// Checks if the descriptor is of a record.
267 bool isRecord() const { return !IsArray && ElemRecord; }
268 /// Checks if the descriptor is of a union.
269 bool isUnion() const;
270
271 /// Whether variables of this descriptor need their destructor called or not.
272 bool hasTrivialDtor() const;
273
274 void dump() const;
275 void dump(llvm::raw_ostream &OS) const;
276 void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;
277};
278
279} // namespace interp
280} // namespace clang
281
282#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:220
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:3160
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4321
Encodes a location in the source.
The base class of the type hierarchy.
Definition TypeBase.h:1833
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:926
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:189
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:140
Describes a memory block created by an allocation site.
Definition Descriptor.h:121
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:160
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:241
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:248
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:230
void dumpFull(unsigned Offset=0, unsigned Indent=0) const
Dump descriptor, including all valid offsets.
Definition Disasm.cpp:432
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:262
QualType getElemQualType() const
const RecordDecl * asRecordDecl() const
Definition Descriptor.h:225
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:255
const BlockDtorFn DtorFn
Definition Descriptor.h:172
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:158
static constexpr MetadataSize GlobalMD
Definition Descriptor.h:144
const ValueDecl * asValueDecl() const
Definition Descriptor.h:213
const BlockCtorFn CtorFn
Storage management methods.
Definition Descriptor.h:171
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition Descriptor.h:147
QualType getType() const
const DeclTy & getSource() const
Definition Descriptor.h:211
const Decl * asDecl() const
Definition Descriptor.h:209
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:154
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:245
SourceLocation getLocation() const
const bool IsMutable
Flag indicating if a field is mutable.
Definition Descriptor.h:162
static constexpr MetadataSize InlineDescMD
Definition Descriptor.h:143
QualType getDataType(const ASTContext &Ctx) const
std::optional< unsigned > MetadataSize
Definition Descriptor.h:142
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition Descriptor.h:259
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition Descriptor.h:243
const bool IsArray
Flag indicating if the block is an array.
Definition Descriptor.h:167
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:253
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition Descriptor.h:257
const FieldDecl * asFieldDecl() const
Definition Descriptor.h:221
const VarDecl * asVarDecl() const
Definition Descriptor.h:217
PrimType getPrimType() const
Definition Descriptor.h:235
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:267
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition Descriptor.h:164
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:152
bool isUnion() const
Checks if the descriptor is of a union.
const Expr * asExpr() const
Definition Descriptor.h:210
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:265
Descriptor used for global variables.
Definition Descriptor.h:50
A pointer-sized struct we use to allocate into data storage.
Definition InitMap.h:56
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:66
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition Descriptor.h:88
unsigned IsConstInMutable
Flag indicating if this field is a const field nested in a mutable parent field.
Definition Descriptor.h:98
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition Descriptor.h:82
unsigned IsVirtualBase
Flag inidcating if the field is a virtual base class.
Definition Descriptor.h:85
unsigned InUnion
Flag indicating if this field is in a union (even if nested).
Definition Descriptor.h:91
unsigned Offset
Offset inside the structure/array.
Definition Descriptor.h:68
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition Descriptor.h:79
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition Descriptor.h:73
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:94
InlineDescriptor(const Descriptor *D)
Definition Descriptor.h:109