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