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 "DeclOrExpr.h"
17#include "InitMap.h"
18#include "PrimType.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/Expr.h"
21
22namespace clang {
23namespace interp {
24class Block;
25class Record;
26class SourceInfo;
27struct Descriptor;
28enum PrimType : uint8_t;
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 IsVolatile, bool IsActive,
35 bool InUnion, 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
47
48/// Descriptor used for global variables.
52static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");
53
60
61/// Inline descriptor embedded in structures and arrays.
62///
63/// Such descriptors precede all composite array elements and structure fields.
64/// If the base of a pointer is not zero, the base points to the end of this
65/// structure. The offset field is used to traverse the pointer chain up
66/// to the root structure which allocated the object.
68 /// Offset inside the structure/array.
69 unsigned Offset;
70
71 /// Flag indicating if the storage is constant or not.
72 /// Relevant for primitive fields.
73 LLVM_PREFERRED_TYPE(bool)
75 /// For primitive fields, it indicates if the field was initialized.
76 /// Primitive fields in static storage are always initialized.
77 /// Arrays are always initialized, even though their elements might not be.
78 /// Base classes are initialized after the constructor is invoked.
79 LLVM_PREFERRED_TYPE(bool)
80 unsigned IsInitialized : 1;
81 /// Flag indicating if the field is an embedded base class.
82 LLVM_PREFERRED_TYPE(bool)
83 unsigned IsBase : 1;
84 /// Flag inidcating if the field is a virtual base class.
85 LLVM_PREFERRED_TYPE(bool)
86 unsigned IsVirtualBase : 1;
87 /// Flag indicating if the field is the active member of a union.
88 LLVM_PREFERRED_TYPE(bool)
89 unsigned IsActive : 1;
90 /// Flag indicating if this field is in a union (even if nested).
91 LLVM_PREFERRED_TYPE(bool)
92 unsigned InUnion : 1;
93 /// Flag indicating if the field is mutable (if in a record).
94 LLVM_PREFERRED_TYPE(bool)
95 unsigned IsFieldMutable : 1;
96 /// Flag indicating if this field is a const field nested in
97 /// a mutable parent field.
98 LLVM_PREFERRED_TYPE(bool)
99 unsigned IsConstInMutable : 1;
100 /// Flag indicating if the field is an element of a composite array.
101 LLVM_PREFERRED_TYPE(bool)
102 unsigned IsArrayElement : 1;
103 LLVM_PREFERRED_TYPE(bool)
104 unsigned IsVolatile : 1;
105
107
109
115
116 void dump() const { dump(llvm::errs()); }
117 void dump(llvm::raw_ostream &OS) const;
118};
119static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
120
121/// Describes a memory block created by an allocation site.
122struct Descriptor final {
123private:
124 /// Original declaration, used to emit the error message.
125 const DeclOrExpr Source;
126 const Type *SourceType = nullptr;
127 /// Size of an element, in host bytes.
128 const unsigned ElemSize;
129 /// Size of the storage, in host bytes.
130 const unsigned Size;
131 /// Size of the allocation (storage), in host bytes.
132 const unsigned AllocSize;
133
134 /// Value to denote arrays of unknown size.
135 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
136
137public:
138 /// Token to denote structures of unknown size.
139 struct UnknownSize {};
140
141 /// Maximum number of bytes to be used for array elements.
142 static constexpr unsigned MaxArrayElemBytes =
143 std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr);
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 OptPrimType 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 const bool IsVolatile = false;
160 /// Flag indicating if the block is an array.
161 const bool IsArray = false;
162 bool IsConstexprUnknown = false;
163
164 /// Storage management methods.
165 const BlockCtorFn CtorFn = nullptr;
166 const BlockDtorFn DtorFn = nullptr;
167
168 /// Allocates a descriptor for a primitive.
169 Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, bool IsConst,
170 bool IsTemporary, bool IsMutable, bool IsVolatile);
171
172 /// Allocates a descriptor for an array of primitives.
173 Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, size_t NumElems,
174 bool IsConst, bool IsTemporary, bool IsMutable, bool IsVolatile);
175
176 /// Allocates a descriptor for an array of primitives of unknown size.
179
180 /// Allocates a descriptor for an array of composites.
181 Descriptor(DeclOrExpr D, const Type *SourceTy, const Descriptor *Elem,
182 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
183
184 /// Allocates a descriptor for an array of composites of unknown size.
185 Descriptor(DeclOrExpr D, const Descriptor *Elem, bool IsTemporary,
187
188 /// Allocates a descriptor for a record.
189 Descriptor(DeclOrExpr D, const Record *R, bool IsConst, bool IsTemporary,
190 bool IsMutable, bool IsVolatile);
191
192 /// Allocates a dummy descriptor.
194
195 QualType getType() const;
197 QualType getDataType(const ASTContext &Ctx) const;
199 SourceInfo getLoc() const;
200
201 const Decl *asDecl() const { return Source.asDecl(); }
202 const Expr *asExpr() const { return Source.asExpr(); }
203 DeclOrExpr getSource() const { return Source; }
204
205 const ValueDecl *asValueDecl() const {
206 return dyn_cast_if_present<ValueDecl>(asDecl());
207 }
208
209 const VarDecl *asVarDecl() const {
210 return dyn_cast_if_present<VarDecl>(asDecl());
211 }
212
213 const FieldDecl *asFieldDecl() const {
214 return dyn_cast_if_present<FieldDecl>(asDecl());
215 }
216
217 const RecordDecl *asRecordDecl() const {
218 return dyn_cast_if_present<RecordDecl>(asDecl());
219 }
220
221 template <typename T> const T *getAs() const {
222 return dyn_cast_if_present<T>(asDecl());
223 }
224
225 /// Returns the size of the object without metadata.
226 unsigned getSize() const {
227 assert(!isUnknownSizeArray() && "Array of unknown size");
228 return Size;
229 }
230
232 assert(isPrimitiveArray() || isPrimitive());
233 return *PrimT;
234 }
235
236 /// Returns the allocated size, including metadata.
237 unsigned getAllocSize() const { return AllocSize; }
238 /// returns the size of an element when the structure is viewed as an array.
239 unsigned getElemSize() const { return ElemSize; }
240 /// Returns the element data size, i.e. not what the size of
241 /// our primitive data type is, but what the data size of that is.
242 /// E.g., for PT_SInt32, that's 4 bytes.
243 unsigned getElemDataSize() const;
244
245 /// Returns the number of elements stored in the block.
246 unsigned getNumElems() const {
247 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
248 }
249
250 /// Checks if the descriptor is of an array of primitives.
251 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
252 /// Checks if the descriptor is of an array of composites.
253 bool isCompositeArray() const { return IsArray && ElemDesc; }
254 /// Checks if the descriptor is of an array of zero size.
255 bool isZeroSizeArray() const { return Size == 0; }
256 /// Checks if the descriptor is of an array of unknown size.
257 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
258
259 /// Checks if the descriptor is of a primitive.
260 bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
261
262 /// Checks if the descriptor is of an array.
263 bool isArray() const { return IsArray; }
264 /// Checks if the descriptor is of a record.
265 bool isRecord() const { return !IsArray && ElemRecord; }
266 /// Checks if the descriptor is of a union.
267 bool isUnion() const;
268
269 /// Whether variables of this descriptor need their destructor called or not.
270 bool hasTrivialDtor() const;
271
272 void dump() const;
273 void dump(llvm::raw_ostream &OS) const;
274 void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;
275};
276} // namespace interp
277} // namespace clang
278
279#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:223
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:3294
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4459
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:712
Represents a variable declaration or definition.
Definition Decl.h:932
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:43
Structure/Class descriptor.
Definition Record.h:25
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:39
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:33
Top level wrappers for InstallAPI frontend operations.
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition JsonSupport.h:21
const FunctionProtoType * T
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
#define false
Definition stdbool.h:26
Token to denote structures of unknown size.
Definition Descriptor.h:139
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
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:237
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:246
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:226
void dumpFull(unsigned Offset=0, unsigned Indent=0) const
Dump descriptor, including all valid offsets.
Definition Disasm.cpp:472
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:260
QualType getElemQualType() const
const RecordDecl * asRecordDecl() const
Definition Descriptor.h:217
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:253
const BlockDtorFn DtorFn
Definition Descriptor.h:166
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:152
const ValueDecl * asValueDecl() const
Definition Descriptor.h:205
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:142
QualType getType() const
const Decl * asDecl() const
Definition Descriptor.h:201
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:148
SourceInfo getLoc() const
SourceLocation getLocation() const
const bool IsMutable
Flag indicating if a field is mutable.
Definition Descriptor.h:156
QualType getDataType(const ASTContext &Ctx) const
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition Descriptor.h:257
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition Descriptor.h:239
const bool IsArray
Flag indicating if the block is an array.
Definition Descriptor.h:161
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:251
DeclOrExpr getSource() const
Definition Descriptor.h:203
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition Descriptor.h:255
const FieldDecl * asFieldDecl() const
Definition Descriptor.h:213
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:209
PrimType getPrimType() const
Definition Descriptor.h:231
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:265
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.
const T * getAs() const
Definition Descriptor.h:221
const Expr * asExpr() const
Definition Descriptor.h:202
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:263
Descriptor used for global variables.
Definition Descriptor.h:49
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:89
unsigned IsConstInMutable
Flag indicating if this field is a const field nested in a mutable parent field.
Definition Descriptor.h:99
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition Descriptor.h:83
unsigned IsVirtualBase
Flag inidcating if the field is a virtual base class.
Definition Descriptor.h:86
unsigned InUnion
Flag indicating if this field is in a union (even if nested).
Definition Descriptor.h:92
unsigned Offset
Offset inside the structure/array.
Definition Descriptor.h:69
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition Descriptor.h:80
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition Descriptor.h:74
unsigned IsArrayElement
Flag indicating if the field is an element of a composite array.
Definition Descriptor.h:102
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition Descriptor.h:95
InlineDescriptor(const Descriptor *D)
Definition Descriptor.h:110