xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Descriptor.h (revision 297eecfb02bb25902531dbb5c3b9a88caf8adf29)
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;
235f757f3fSDimitry Andric struct InitMap;
24a7dea167SDimitry Andric struct Descriptor;
25a7dea167SDimitry Andric enum PrimType : unsigned;
26a7dea167SDimitry Andric 
27a7dea167SDimitry Andric using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
285f757f3fSDimitry Andric using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>;
29a7dea167SDimitry Andric 
30a7dea167SDimitry Andric /// Invoked whenever a block is created. The constructor method fills in the
31a7dea167SDimitry Andric /// inline descriptors of all fields and array elements. It also initializes
32a7dea167SDimitry Andric /// all the fields which contain non-trivial types.
335f757f3fSDimitry Andric using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,
34a7dea167SDimitry Andric                              bool IsMutable, bool IsActive,
3506c3fb27SDimitry Andric                              const Descriptor *FieldDesc);
36a7dea167SDimitry Andric 
37a7dea167SDimitry Andric /// Invoked when a block is destroyed. Invokes the destructors of all
38a7dea167SDimitry Andric /// non-trivial nested fields of arrays and records.
395f757f3fSDimitry Andric using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
4006c3fb27SDimitry Andric                              const Descriptor *FieldDesc);
41a7dea167SDimitry Andric 
42a7dea167SDimitry Andric /// Invoked when a block with pointers referencing it goes out of scope. Such
43a7dea167SDimitry Andric /// blocks are persisted: the move function copies all inline descriptors and
44a7dea167SDimitry Andric /// non-trivial fields, as existing pointers might need to reference those
45a7dea167SDimitry Andric /// descriptors. Data is not copied since it cannot be legally read.
465f757f3fSDimitry Andric using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,
475f757f3fSDimitry Andric                              std::byte *DstFieldPtr,
485f757f3fSDimitry Andric                              const Descriptor *FieldDesc);
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).
73bdd1243dSDimitry Andric   unsigned IsFieldMutable : 1;
74a7dea167SDimitry Andric 
755f757f3fSDimitry Andric   const Descriptor *Desc;
76a7dea167SDimitry Andric };
77a7dea167SDimitry Andric 
78bdd1243dSDimitry Andric /// Describes a memory block created by an allocation site.
79bdd1243dSDimitry Andric struct Descriptor final {
80bdd1243dSDimitry Andric private:
81bdd1243dSDimitry Andric   /// Original declaration, used to emit the error message.
82bdd1243dSDimitry Andric   const DeclTy Source;
83bdd1243dSDimitry Andric   /// Size of an element, in host bytes.
8406c3fb27SDimitry Andric   const unsigned ElemSize;
85bdd1243dSDimitry Andric   /// Size of the storage, in host bytes.
8606c3fb27SDimitry Andric   const unsigned Size;
875f757f3fSDimitry Andric   /// Size of the metadata.
8806c3fb27SDimitry Andric   const unsigned MDSize;
89bdd1243dSDimitry Andric   /// Size of the allocation (storage + metadata), in host bytes.
9006c3fb27SDimitry Andric   const unsigned AllocSize;
91bdd1243dSDimitry Andric 
92bdd1243dSDimitry Andric   /// Value to denote arrays of unknown size.
93bdd1243dSDimitry Andric   static constexpr unsigned UnknownSizeMark = (unsigned)-1;
94bdd1243dSDimitry Andric 
95bdd1243dSDimitry Andric public:
96bdd1243dSDimitry Andric   /// Token to denote structures of unknown size.
97bdd1243dSDimitry Andric   struct UnknownSize {};
98bdd1243dSDimitry Andric 
9906c3fb27SDimitry Andric   using MetadataSize = std::optional<unsigned>;
100bdd1243dSDimitry Andric   static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);
101bdd1243dSDimitry Andric 
102bdd1243dSDimitry Andric   /// Pointer to the record, if block contains records.
103*297eecfbSDimitry Andric   const Record *const ElemRecord = nullptr;
104bdd1243dSDimitry Andric   /// Descriptor of the array element.
1055f757f3fSDimitry Andric   const Descriptor *const ElemDesc = nullptr;
106bdd1243dSDimitry Andric   /// Flag indicating if the block is mutable.
107bdd1243dSDimitry Andric   const bool IsConst = false;
108bdd1243dSDimitry Andric   /// Flag indicating if a field is mutable.
109bdd1243dSDimitry Andric   const bool IsMutable = false;
110bdd1243dSDimitry Andric   /// Flag indicating if the block is a temporary.
111bdd1243dSDimitry Andric   const bool IsTemporary = false;
112bdd1243dSDimitry Andric   /// Flag indicating if the block is an array.
113bdd1243dSDimitry Andric   const bool IsArray = false;
1145f757f3fSDimitry Andric   /// Flag indicating if this is a dummy descriptor.
1155f757f3fSDimitry Andric   const bool IsDummy = false;
116bdd1243dSDimitry Andric 
117bdd1243dSDimitry Andric   /// Storage management methods.
118bdd1243dSDimitry Andric   const BlockCtorFn CtorFn = nullptr;
119bdd1243dSDimitry Andric   const BlockDtorFn DtorFn = nullptr;
120bdd1243dSDimitry Andric   const BlockMoveFn MoveFn = nullptr;
121bdd1243dSDimitry Andric 
122bdd1243dSDimitry Andric   /// Allocates a descriptor for a primitive.
123bdd1243dSDimitry Andric   Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, bool IsConst,
124bdd1243dSDimitry Andric              bool IsTemporary, bool IsMutable);
125bdd1243dSDimitry Andric 
126bdd1243dSDimitry Andric   /// Allocates a descriptor for an array of primitives.
127bdd1243dSDimitry Andric   Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,
128bdd1243dSDimitry Andric              bool IsConst, bool IsTemporary, bool IsMutable);
129bdd1243dSDimitry Andric 
130bdd1243dSDimitry Andric   /// Allocates a descriptor for an array of primitives of unknown size.
131bdd1243dSDimitry Andric   Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary, UnknownSize);
132bdd1243dSDimitry Andric 
133bdd1243dSDimitry Andric   /// Allocates a descriptor for an array of composites.
1345f757f3fSDimitry Andric   Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,
135bdd1243dSDimitry Andric              unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);
136bdd1243dSDimitry Andric 
137bdd1243dSDimitry Andric   /// Allocates a descriptor for an array of composites of unknown size.
138*297eecfbSDimitry Andric   Descriptor(const DeclTy &D, const Descriptor *Elem, bool IsTemporary,
139*297eecfbSDimitry Andric              UnknownSize);
140bdd1243dSDimitry Andric 
141bdd1243dSDimitry Andric   /// Allocates a descriptor for a record.
142*297eecfbSDimitry Andric   Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,
143bdd1243dSDimitry Andric              bool IsTemporary, bool IsMutable);
144bdd1243dSDimitry Andric 
1455f757f3fSDimitry Andric   Descriptor(const DeclTy &D, MetadataSize MD);
1465f757f3fSDimitry Andric 
147bdd1243dSDimitry Andric   QualType getType() const;
1485f757f3fSDimitry Andric   QualType getElemQualType() const;
149bdd1243dSDimitry Andric   SourceLocation getLocation() const;
150bdd1243dSDimitry Andric 
151bdd1243dSDimitry Andric   const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
152bdd1243dSDimitry Andric   const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
153bdd1243dSDimitry Andric 
154bdd1243dSDimitry Andric   const ValueDecl *asValueDecl() const {
155bdd1243dSDimitry Andric     return dyn_cast_if_present<ValueDecl>(asDecl());
156bdd1243dSDimitry Andric   }
157bdd1243dSDimitry Andric 
158bdd1243dSDimitry Andric   const FieldDecl *asFieldDecl() const {
159bdd1243dSDimitry Andric     return dyn_cast_if_present<FieldDecl>(asDecl());
160bdd1243dSDimitry Andric   }
161bdd1243dSDimitry Andric 
162bdd1243dSDimitry Andric   const RecordDecl *asRecordDecl() const {
163bdd1243dSDimitry Andric     return dyn_cast_if_present<RecordDecl>(asDecl());
164bdd1243dSDimitry Andric   }
165bdd1243dSDimitry Andric 
166bdd1243dSDimitry Andric   /// Returns the size of the object without metadata.
167bdd1243dSDimitry Andric   unsigned getSize() const {
168bdd1243dSDimitry Andric     assert(!isUnknownSizeArray() && "Array of unknown size");
169bdd1243dSDimitry Andric     return Size;
170bdd1243dSDimitry Andric   }
171bdd1243dSDimitry Andric 
172bdd1243dSDimitry Andric   /// Returns the allocated size, including metadata.
173bdd1243dSDimitry Andric   unsigned getAllocSize() const { return AllocSize; }
174bdd1243dSDimitry Andric   /// returns the size of an element when the structure is viewed as an array.
175bdd1243dSDimitry Andric   unsigned getElemSize()  const { return ElemSize; }
176bdd1243dSDimitry Andric   /// Returns the size of the metadata.
177bdd1243dSDimitry Andric   unsigned getMetadataSize() const { return MDSize; }
178bdd1243dSDimitry Andric 
179bdd1243dSDimitry Andric   /// Returns the number of elements stored in the block.
180bdd1243dSDimitry Andric   unsigned getNumElems() const {
181bdd1243dSDimitry Andric     return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
182bdd1243dSDimitry Andric   }
183bdd1243dSDimitry Andric 
184bdd1243dSDimitry Andric   /// Checks if the descriptor is of an array of primitives.
185bdd1243dSDimitry Andric   bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
18606c3fb27SDimitry Andric   /// Checks if the descriptor is of an array of composites.
18706c3fb27SDimitry Andric   bool isCompositeArray() const { return IsArray && ElemDesc; }
188bdd1243dSDimitry Andric   /// Checks if the descriptor is of an array of zero size.
189bdd1243dSDimitry Andric   bool isZeroSizeArray() const { return Size == 0; }
190bdd1243dSDimitry Andric   /// Checks if the descriptor is of an array of unknown size.
191bdd1243dSDimitry Andric   bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
192bdd1243dSDimitry Andric 
193bdd1243dSDimitry Andric   /// Checks if the descriptor is of a primitive.
194bdd1243dSDimitry Andric   bool isPrimitive() const { return !IsArray && !ElemRecord; }
195bdd1243dSDimitry Andric 
196bdd1243dSDimitry Andric   /// Checks if the descriptor is of an array.
197bdd1243dSDimitry Andric   bool isArray() const { return IsArray; }
1985f757f3fSDimitry Andric   /// Checks if the descriptor is of a record.
1995f757f3fSDimitry Andric   bool isRecord() const { return !IsArray && ElemRecord; }
2005f757f3fSDimitry Andric   /// Checks if this is a dummy descriptor.
2015f757f3fSDimitry Andric   bool isDummy() const { return IsDummy; }
202bdd1243dSDimitry Andric };
203bdd1243dSDimitry Andric 
204a7dea167SDimitry Andric /// Bitfield tracking the initialisation status of elements of primitive arrays.
205bdd1243dSDimitry Andric struct InitMap final {
206a7dea167SDimitry Andric private:
207a7dea167SDimitry Andric   /// Type packing bits.
208a7dea167SDimitry Andric   using T = uint64_t;
209a7dea167SDimitry Andric   /// Bits stored in a single field.
210a7dea167SDimitry Andric   static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
211a7dea167SDimitry Andric 
212a7dea167SDimitry Andric public:
2135f757f3fSDimitry Andric   /// Initializes the map with no fields set.
2145f757f3fSDimitry Andric   explicit InitMap(unsigned N);
215a7dea167SDimitry Andric 
216a7dea167SDimitry Andric private:
2175f757f3fSDimitry Andric   friend class Pointer;
2185f757f3fSDimitry Andric 
2195f757f3fSDimitry Andric   /// Returns a pointer to storage.
2205f757f3fSDimitry Andric   T *data() { return Data.get(); }
2215f757f3fSDimitry Andric   const T *data() const { return Data.get(); }
2225f757f3fSDimitry Andric 
2235f757f3fSDimitry Andric   /// Initializes an element. Returns true when object if fully initialized.
2245f757f3fSDimitry Andric   bool initializeElement(unsigned I);
2255f757f3fSDimitry Andric 
2265f757f3fSDimitry Andric   /// Checks if an element was initialized.
2275f757f3fSDimitry Andric   bool isElementInitialized(unsigned I) const;
2285f757f3fSDimitry Andric 
2295f757f3fSDimitry Andric   static constexpr size_t numFields(unsigned N) {
2305f757f3fSDimitry Andric     return (N + PER_FIELD - 1) / PER_FIELD;
2315f757f3fSDimitry Andric   }
2325f757f3fSDimitry Andric   /// Number of fields not initialized.
233a7dea167SDimitry Andric   unsigned UninitFields;
2345f757f3fSDimitry Andric   std::unique_ptr<T[]> Data;
235a7dea167SDimitry Andric };
236a7dea167SDimitry Andric 
237a7dea167SDimitry Andric } // namespace interp
238a7dea167SDimitry Andric } // namespace clang
239a7dea167SDimitry Andric 
240a7dea167SDimitry Andric #endif
241