clang 18.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
50/// Inline descriptor embedded in structures and arrays.
51///
52/// Such descriptors precede all composite array elements and structure fields.
53/// If the base of a pointer is not zero, the base points to the end of this
54/// structure. The offset field is used to traverse the pointer chain up
55/// to the root structure which allocated the object.
57 /// Offset inside the structure/array.
58 unsigned Offset;
59
60 /// Flag indicating if the storage is constant or not.
61 /// Relevant for primitive fields.
62 unsigned IsConst : 1;
63 /// For primitive fields, it indicates if the field was initialized.
64 /// Primitive fields in static storage are always initialized.
65 /// Arrays are always initialized, even though their elements might not be.
66 /// Base classes are initialized after the constructor is invoked.
67 unsigned IsInitialized : 1;
68 /// Flag indicating if the field is an embedded base class.
69 unsigned IsBase : 1;
70 /// Flag indicating if the field is the active member of a union.
71 unsigned IsActive : 1;
72 /// Flag indicating if the field is mutable (if in a record).
73 unsigned IsFieldMutable : 1;
74
76};
77
78/// Describes a memory block created by an allocation site.
79struct Descriptor final {
80private:
81 /// Original declaration, used to emit the error message.
82 const DeclTy Source;
83 /// Size of an element, in host bytes.
84 const unsigned ElemSize;
85 /// Size of the storage, in host bytes.
86 const unsigned Size;
87 /// Size of the metadata.
88 const unsigned MDSize;
89 /// Size of the allocation (storage + metadata), in host bytes.
90 const unsigned AllocSize;
91
92 /// Value to denote arrays of unknown size.
93 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
94
95public:
96 /// Token to denote structures of unknown size.
97 struct UnknownSize {};
98
99 using MetadataSize = std::optional<unsigned>;
100 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
101
102 /// Pointer to the record, if block contains records.
103 Record *const ElemRecord = nullptr;
104 /// Descriptor of the array element.
105 const Descriptor *const ElemDesc = nullptr;
106 /// Flag indicating if the block is mutable.
107 const bool IsConst = false;
108 /// Flag indicating if a field is mutable.
109 const bool IsMutable = false;
110 /// Flag indicating if the block is a temporary.
111 const bool IsTemporary = false;
112 /// Flag indicating if the block is an array.
113 const bool IsArray = false;
114 /// Flag indicating if this is a dummy descriptor.
115 const bool IsDummy = false;
116
117 /// Storage management methods.
118 const BlockCtorFn CtorFn = nullptr;
119 const BlockDtorFn DtorFn = nullptr;
120 const BlockMoveFn MoveFn = nullptr;
121
122 /// Allocates a descriptor for a primitive.
124 bool IsTemporary, bool IsMutable);
125
126 /// Allocates a descriptor for an array of primitives.
127 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
128 bool IsConst, bool IsTemporary, bool IsMutable);
129
130 /// Allocates a descriptor for an array of primitives of unknown size.
132
133 /// Allocates a descriptor for an array of composites.
134 Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
135 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
136
137 /// Allocates a descriptor for an array of composites of unknown size.
138 Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize);
139
140 /// Allocates a descriptor for a record.
141 Descriptor(const DeclTy &D, Record *R, MetadataSize MD, bool IsConst,
142 bool IsTemporary, bool IsMutable);
143
144 Descriptor(const DeclTy &D, MetadataSize MD);
145
146 QualType getType() const;
149
150 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
151 const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
152
153 const ValueDecl *asValueDecl() const {
154 return dyn_cast_if_present<ValueDecl>(asDecl());
155 }
156
157 const FieldDecl *asFieldDecl() const {
158 return dyn_cast_if_present<FieldDecl>(asDecl());
159 }
160
161 const RecordDecl *asRecordDecl() const {
162 return dyn_cast_if_present<RecordDecl>(asDecl());
163 }
164
165 /// Returns the size of the object without metadata.
166 unsigned getSize() const {
167 assert(!isUnknownSizeArray() && "Array of unknown size");
168 return Size;
169 }
170
171 /// Returns the allocated size, including metadata.
172 unsigned getAllocSize() const { return AllocSize; }
173 /// returns the size of an element when the structure is viewed as an array.
174 unsigned getElemSize() const { return ElemSize; }
175 /// Returns the size of the metadata.
176 unsigned getMetadataSize() const { return MDSize; }
177
178 /// Returns the number of elements stored in the block.
179 unsigned getNumElems() const {
180 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
181 }
182
183 /// Checks if the descriptor is of an array of primitives.
184 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
185 /// Checks if the descriptor is of an array of composites.
186 bool isCompositeArray() const { return IsArray && ElemDesc; }
187 /// Checks if the descriptor is of an array of zero size.
188 bool isZeroSizeArray() const { return Size == 0; }
189 /// Checks if the descriptor is of an array of unknown size.
190 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
191
192 /// Checks if the descriptor is of a primitive.
193 bool isPrimitive() const { return !IsArray && !ElemRecord; }
194
195 /// Checks if the descriptor is of an array.
196 bool isArray() const { return IsArray; }
197 /// Checks if the descriptor is of a record.
198 bool isRecord() const { return !IsArray && ElemRecord; }
199 /// Checks if this is a dummy descriptor.
200 bool isDummy() const { return IsDummy; }
201};
202
203/// Bitfield tracking the initialisation status of elements of primitive arrays.
204struct InitMap final {
205private:
206 /// Type packing bits.
207 using T = uint64_t;
208 /// Bits stored in a single field.
209 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
210
211public:
212 /// Initializes the map with no fields set.
213 explicit InitMap(unsigned N);
214
215private:
216 friend class Pointer;
217
218 /// Returns a pointer to storage.
219 T *data() { return Data.get(); }
220 const T *data() const { return Data.get(); }
221
222 /// Initializes an element. Returns true when object if fully initialized.
223 bool initializeElement(unsigned I);
224
225 /// Checks if an element was initialized.
226 bool isElementInitialized(unsigned I) const;
227
228 static constexpr size_t numFields(unsigned N) {
229 return (N + PER_FIELD - 1) / PER_FIELD;
230 }
231 /// Number of fields not initialized.
232 unsigned UninitFields;
233 std::unique_ptr<T[]> Data;
234};
235
236} // namespace interp
237} // namespace clang
238
239#endif
const CFGBlock * Block
Definition: HTMLLogger.cpp:149
const char * Data
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
This represents one expression.
Definition: Expr.h:110
Represents a member of a struct/union/class.
Definition: Decl.h:3015
A (possibly-)qualified type.
Definition: Type.h:736
Represents a struct/union/class.
Definition: Decl.h:4117
Encodes a location in the source.
The base class of the type hierarchy.
Definition: Type.h:1602
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:704
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:65
Structure/Class descriptor.
Definition: Record.h:25
#define CHAR_BIT
Definition: limits.h:67
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:32
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
Token to denote structures of unknown size.
Definition: Descriptor.h:97
Describes a memory block created by an allocation site.
Definition: Descriptor.h:79
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:107
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:172
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:179
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:166
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:193
QualType getElemQualType() const
Definition: Descriptor.cpp:316
const RecordDecl * asRecordDecl() const
Definition: Descriptor.h:161
const BlockMoveFn MoveFn
Definition: Descriptor.h:120
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition: Descriptor.h:186
const BlockDtorFn DtorFn
Definition: Descriptor.h:119
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:153
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:118
QualType getType() const
Definition: Descriptor.cpp:306
const Decl * asDecl() const
Definition: Descriptor.h:150
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:105
bool isDummy() const
Checks if this is a dummy descriptor.
Definition: Descriptor.h:200
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:176
SourceLocation getLocation() const
Definition: Descriptor.cpp:322
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:109
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:100
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:99
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:190
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition: Descriptor.h:174
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:113
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:184
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition: Descriptor.h:188
const FieldDecl * asFieldDecl() const
Definition: Descriptor.h:157
const bool IsDummy
Flag indicating if this is a dummy descriptor.
Definition: Descriptor.h:115
bool isRecord() const
Checks if the descriptor is of a record.
Definition: Descriptor.h:198
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:111
const Expr * asExpr() const
Definition: Descriptor.h:151
Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:103
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:196
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition: Descriptor.h:204
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:56
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:71
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:69
const Descriptor * Desc
Definition: Descriptor.h:75
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:58
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:67
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:62
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:73