clang 17.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 Descriptor;
24enum PrimType : unsigned;
25
26using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
27
28/// Invoked whenever a block is created. The constructor method fills in the
29/// inline descriptors of all fields and array elements. It also initializes
30/// all the fields which contain non-trivial types.
31using BlockCtorFn = void (*)(Block *Storage, char *FieldPtr, bool IsConst,
32 bool IsMutable, bool IsActive,
33 Descriptor *FieldDesc);
34
35/// Invoked when a block is destroyed. Invokes the destructors of all
36/// non-trivial nested fields of arrays and records.
37using BlockDtorFn = void (*)(Block *Storage, char *FieldPtr,
38 Descriptor *FieldDesc);
39
40/// Invoked when a block with pointers referencing it goes out of scope. Such
41/// blocks are persisted: the move function copies all inline descriptors and
42/// non-trivial fields, as existing pointers might need to reference those
43/// descriptors. Data is not copied since it cannot be legally read.
44using BlockMoveFn = void (*)(Block *Storage, char *SrcFieldPtr,
45 char *DstFieldPtr, Descriptor *FieldDesc);
46
47/// Inline descriptor embedded in structures and arrays.
48///
49/// Such descriptors precede all composite array elements and structure fields.
50/// If the base of a pointer is not zero, the base points to the end of this
51/// structure. The offset field is used to traverse the pointer chain up
52/// to the root structure which allocated the object.
54 /// Offset inside the structure/array.
55 unsigned Offset;
56
57 /// Flag indicating if the storage is constant or not.
58 /// Relevant for primitive fields.
59 unsigned IsConst : 1;
60 /// For primitive fields, it indicates if the field was initialized.
61 /// Primitive fields in static storage are always initialized.
62 /// Arrays are always initialized, even though their elements might not be.
63 /// Base classes are initialized after the constructor is invoked.
64 unsigned IsInitialized : 1;
65 /// Flag indicating if the field is an embedded base class.
66 unsigned IsBase : 1;
67 /// Flag indicating if the field is the active member of a union.
68 unsigned IsActive : 1;
69 /// Flag indicating if the field is mutable (if in a record).
70 unsigned IsFieldMutable : 1;
71
73};
74
75/// Describes a memory block created by an allocation site.
76struct Descriptor final {
77private:
78 /// Original declaration, used to emit the error message.
79 const DeclTy Source;
80 /// Size of an element, in host bytes.
81 const unsigned ElemSize;
82 /// Size of the storage, in host bytes.
83 const unsigned Size;
84 // Size of the metadata.
85 const unsigned MDSize;
86 /// Size of the allocation (storage + metadata), in host bytes.
87 const unsigned AllocSize;
88
89 /// Value to denote arrays of unknown size.
90 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
91
92public:
93 /// Token to denote structures of unknown size.
94 struct UnknownSize {};
95
96 using MetadataSize = std::optional<unsigned>;
97 static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
98
99 /// Pointer to the record, if block contains records.
100 Record *const ElemRecord = nullptr;
101 /// Descriptor of the array element.
102 Descriptor *const ElemDesc = nullptr;
103 /// Flag indicating if the block is mutable.
104 const bool IsConst = false;
105 /// Flag indicating if a field is mutable.
106 const bool IsMutable = false;
107 /// Flag indicating if the block is a temporary.
108 const bool IsTemporary = false;
109 /// Flag indicating if the block is an array.
110 const bool IsArray = false;
111
112 /// Storage management methods.
113 const BlockCtorFn CtorFn = nullptr;
114 const BlockDtorFn DtorFn = nullptr;
115 const BlockMoveFn MoveFn = nullptr;
116
117 /// Allocates a descriptor for a primitive.
119 bool IsTemporary, bool IsMutable);
120
121 /// Allocates a descriptor for an array of primitives.
122 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
123 bool IsConst, bool IsTemporary, bool IsMutable);
124
125 /// Allocates a descriptor for an array of primitives of unknown size.
127
128 /// Allocates a descriptor for an array of composites.
129 Descriptor(const DeclTy &D, Descriptor *Elem, MetadataSize MD,
130 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
131
132 /// Allocates a descriptor for an array of composites of unknown size.
133 Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize);
134
135 /// Allocates a descriptor for a record.
136 Descriptor(const DeclTy &D, Record *R, MetadataSize MD, bool IsConst,
137 bool IsTemporary, bool IsMutable);
138
139 QualType getType() const;
141
142 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
143 const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
144
145 const ValueDecl *asValueDecl() const {
146 return dyn_cast_if_present<ValueDecl>(asDecl());
147 }
148
149 const FieldDecl *asFieldDecl() const {
150 return dyn_cast_if_present<FieldDecl>(asDecl());
151 }
152
153 const RecordDecl *asRecordDecl() const {
154 return dyn_cast_if_present<RecordDecl>(asDecl());
155 }
156
157 /// Returns the size of the object without metadata.
158 unsigned getSize() const {
159 assert(!isUnknownSizeArray() && "Array of unknown size");
160 return Size;
161 }
162
163 /// Returns the allocated size, including metadata.
164 unsigned getAllocSize() const { return AllocSize; }
165 /// returns the size of an element when the structure is viewed as an array.
166 unsigned getElemSize() const { return ElemSize; }
167 /// Returns the size of the metadata.
168 unsigned getMetadataSize() const { return MDSize; }
169
170 /// Returns the number of elements stored in the block.
171 unsigned getNumElems() const {
172 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
173 }
174
175 /// Checks if the descriptor is of an array of primitives.
176 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
177 /// Checks if the descriptor is of an array of zero size.
178 bool isZeroSizeArray() const { return Size == 0; }
179 /// Checks if the descriptor is of an array of unknown size.
180 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
181
182 /// Checks if the descriptor is of a primitive.
183 bool isPrimitive() const { return !IsArray && !ElemRecord; }
184
185 /// Checks if the descriptor is of an array.
186 bool isArray() const { return IsArray; }
187};
188
189/// Bitfield tracking the initialisation status of elements of primitive arrays.
190/// A pointer to this is embedded at the end of all primitive arrays.
191/// If the map was not yet created and nothing was initialized, the pointer to
192/// this structure is 0. If the object was fully initialized, the pointer is -1.
193struct InitMap final {
194private:
195 /// Type packing bits.
196 using T = uint64_t;
197 /// Bits stored in a single field.
198 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
199
200 /// Initializes the map with no fields set.
201 InitMap(unsigned N);
202
203 /// Returns a pointer to storage.
204 T *data();
205 const T *data() const;
206
207public:
208 /// Initializes an element. Returns true when object if fully initialized.
209 bool initialize(unsigned I);
210
211 /// Checks if an element was initialized.
212 bool isInitialized(unsigned I) const;
213
214 /// Allocates a map holding N elements.
215 static InitMap *allocate(unsigned N);
216
217private:
218 /// Number of fields initialized.
219 unsigned UninitFields;
220};
221
222} // namespace interp
223} // namespace clang
224
225#endif
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
This represents one expression.
Definition: Expr.h:110
Represents a member of a struct/union/class.
Definition: Decl.h:2943
A (possibly-)qualified type.
Definition: Type.h:736
Represents a struct/union/class.
Definition: Decl.h:3996
Encodes a location in the source.
The base class of the type hierarchy.
Definition: Type.h:1566
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:703
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
Structure/Class descriptor.
Definition: Record.h:25
#define CHAR_BIT
Definition: limits.h:63
void(*)(Block *Storage, char *FieldPtr, bool IsConst, bool IsMutable, bool IsActive, Descriptor *FieldDesc) BlockCtorFn
Invoked whenever a block is created.
Definition: Descriptor.h:33
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:29
void(*)(Block *Storage, char *SrcFieldPtr, char *DstFieldPtr, Descriptor *FieldDesc) BlockMoveFn
Invoked when a block with pointers referencing it goes out of scope.
Definition: Descriptor.h:45
void(*)(Block *Storage, char *FieldPtr, Descriptor *FieldDesc) BlockDtorFn
Invoked when a block is destroyed.
Definition: Descriptor.h:38
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:26
Token to denote structures of unknown size.
Definition: Descriptor.h:94
Describes a memory block created by an allocation site.
Definition: Descriptor.h:76
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:104
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:164
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:171
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:158
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:183
const RecordDecl * asRecordDecl() const
Definition: Descriptor.h:153
const BlockMoveFn MoveFn
Definition: Descriptor.h:115
const BlockDtorFn DtorFn
Definition: Descriptor.h:114
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:145
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:113
QualType getType() const
Definition: Descriptor.cpp:266
const Decl * asDecl() const
Definition: Descriptor.h:142
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:168
SourceLocation getLocation() const
Definition: Descriptor.cpp:274
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:106
static constexpr MetadataSize InlineDescMD
Definition: Descriptor.h:97
std::optional< unsigned > MetadataSize
Definition: Descriptor.h:96
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:180
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition: Descriptor.h:166
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:110
Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:102
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:176
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition: Descriptor.h:178
const FieldDecl * asFieldDecl() const
Definition: Descriptor.h:149
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:108
const Expr * asExpr() const
Definition: Descriptor.h:143
Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:100
bool isArray() const
Checks if the descriptor is of an array.
Definition: Descriptor.h:186
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition: Descriptor.h:193
static InitMap * allocate(unsigned N)
Allocates a map holding N elements.
Definition: Descriptor.cpp:311
bool isInitialized(unsigned I) const
Checks if an element was initialized.
Definition: Descriptor.cpp:306
bool initialize(unsigned I)
Initializes an element. Returns true when object if fully initialized.
Definition: Descriptor.cpp:296
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:53
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:68
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:66
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:55
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:64
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:59
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:70