Debian Codegraph @

llvm-project/clang/lib/AST/Interp/Descriptor.h

raw
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
19 namespace clang {
20 namespace interp {
21 class Block;
22 class Record;
23 struct Descriptor;
24 enum PrimType : unsigned;
25
26 using 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.
31 using 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.
37 using 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.
44 using BlockMoveFn = void (*)(Block *Storage, char *SrcFieldPtr,
45 char *DstFieldPtr, Descriptor *FieldDesc);
46
47 /// Object size as used by the interpreter.
48 using InterpSize = unsigned;
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.
56 struct InlineDescriptor {
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
75 Descriptor *Desc;
76 };
77
78 /// Describes a memory block created by an allocation site.
79 struct Descriptor final {
80 private:
81 /// Original declaration, used to emit the error message.
82 const DeclTy Source;
83 /// Size of an element, in host bytes.
84 const InterpSize ElemSize;
85 /// Size of the storage, in host bytes.
86 const InterpSize Size;
87 // Size of the metadata.
88 const InterpSize MDSize;
89 /// Size of the allocation (storage + metadata), in host bytes.
90 const InterpSize AllocSize;
91
92 /// Value to denote arrays of unknown size.
93 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
94
95 public:
96 /// Token to denote structures of unknown size.
97 struct UnknownSize {};
98
99 using MetadataSize = std::optional<InterpSize>;
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 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
115 /// Storage management methods.
116 const BlockCtorFn CtorFn = nullptr;
117 const BlockDtorFn DtorFn = nullptr;
118 const BlockMoveFn MoveFn = nullptr;
119
120 /// Allocates a descriptor for a primitive.
121 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, bool IsConst,
122 bool IsTemporary, bool IsMutable);
123
124 /// Allocates a descriptor for an array of primitives.
125 Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
126 bool IsConst, bool IsTemporary, bool IsMutable);
127
128 /// Allocates a descriptor for an array of primitives of unknown size.
129 Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary, UnknownSize);
130
131 /// Allocates a descriptor for an array of composites.
132 Descriptor(const DeclTy &D, Descriptor *Elem, MetadataSize MD,
133 unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
134
135 /// Allocates a descriptor for an array of composites of unknown size.
136 Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize);
137
138 /// Allocates a descriptor for a record.
139 Descriptor(const DeclTy &D, Record *R, MetadataSize MD, bool IsConst,
140 bool IsTemporary, bool IsMutable);
141
142 QualType getType() const;
143 SourceLocation getLocation() const;
144
145 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
146 const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
147
148 const ValueDecl *asValueDecl() const {
149 return dyn_cast_if_present<ValueDecl>(asDecl());
150 }
151
152 const FieldDecl *asFieldDecl() const {
153 return dyn_cast_if_present<FieldDecl>(asDecl());
154 }
155
156 const RecordDecl *asRecordDecl() const {
157 return dyn_cast_if_present<RecordDecl>(asDecl());
158 }
159
160 /// Returns the size of the object without metadata.
161 unsigned getSize() const {
162 assert(!isUnknownSizeArray() && "Array of unknown size");
163 return Size;
164 }
165
166 /// Returns the allocated size, including metadata.
167 unsigned getAllocSize() const { return AllocSize; }
168 /// returns the size of an element when the structure is viewed as an array.
169 unsigned getElemSize() const { return ElemSize; }
170 /// Returns the size of the metadata.
171 unsigned getMetadataSize() const { return MDSize; }
172
173 /// Returns the number of elements stored in the block.
174 unsigned getNumElems() const {
175 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
176 }
177
178 /// Checks if the descriptor is of an array of primitives.
179 bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
180 /// Checks if the descriptor is of an array of zero size.
181 bool isZeroSizeArray() const { return Size == 0; }
182 /// Checks if the descriptor is of an array of unknown size.
183 bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
184
185 /// Checks if the descriptor is of a primitive.
186 bool isPrimitive() const { return !IsArray && !ElemRecord; }
187
188 /// Checks if the descriptor is of an array.
189 bool isArray() const { return IsArray; }
190 };
191
192 /// Bitfield tracking the initialisation status of elements of primitive arrays.
193 /// A pointer to this is embedded at the end of all primitive arrays.
194 /// If the map was not yet created and nothing was initialized, the pointer to
195 /// this structure is 0. If the object was fully initialized, the pointer is -1.
196 struct InitMap final {
197 private:
198 /// Type packing bits.
199 using T = uint64_t;
200 /// Bits stored in a single field.
201 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
202
203 /// Initializes the map with no fields set.
204 InitMap(unsigned N);
205
206 /// Returns a pointer to storage.
207 T *data();
208 const T *data() const;
209
210 public:
211 /// Initializes an element. Returns true when object if fully initialized.
212 bool initialize(unsigned I);
213
214 /// Checks if an element was initialized.
215 bool isInitialized(unsigned I) const;
216
217 /// Allocates a map holding N elements.
218 static InitMap *allocate(unsigned N);
219
220 private:
221 /// Number of fields initialized.
222 unsigned UninitFields;
223 };
224
225 } // namespace interp
226 } // namespace clang
227
228 #endif