xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Descriptor.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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 /// Describes a memory block created by an allocation site.
51a7dea167SDimitry Andric struct Descriptor {
52a7dea167SDimitry Andric private:
53a7dea167SDimitry Andric   /// Original declaration, used to emit the error message.
54a7dea167SDimitry Andric   const DeclTy Source;
55a7dea167SDimitry Andric   /// Size of an element, in host bytes.
56a7dea167SDimitry Andric   const InterpSize ElemSize;
57a7dea167SDimitry Andric   /// Size of the storage, in host bytes.
58a7dea167SDimitry Andric   const InterpSize Size;
59a7dea167SDimitry Andric   /// Size of the allocation (storage + metadata), in host bytes.
60a7dea167SDimitry Andric   const InterpSize AllocSize;
61a7dea167SDimitry Andric 
62a7dea167SDimitry Andric   /// Value to denote arrays of unknown size.
63a7dea167SDimitry Andric   static constexpr unsigned UnknownSizeMark = (unsigned)-1;
64a7dea167SDimitry Andric 
65a7dea167SDimitry Andric public:
66a7dea167SDimitry Andric   /// Token to denote structures of unknown size.
67a7dea167SDimitry Andric   struct UnknownSize {};
68a7dea167SDimitry Andric 
69a7dea167SDimitry Andric   /// Pointer to the record, if block contains records.
70a7dea167SDimitry Andric   Record *const ElemRecord = nullptr;
71a7dea167SDimitry Andric   /// Descriptor of the array element.
72a7dea167SDimitry Andric   Descriptor *const ElemDesc = nullptr;
73a7dea167SDimitry Andric   /// Flag indicating if the block is mutable.
74a7dea167SDimitry Andric   const bool IsConst = false;
75a7dea167SDimitry Andric   /// Flag indicating if a field is mutable.
76a7dea167SDimitry Andric   const bool IsMutable = false;
77a7dea167SDimitry Andric   /// Flag indicating if the block is a temporary.
78a7dea167SDimitry Andric   const bool IsTemporary = false;
79a7dea167SDimitry Andric   /// Flag indicating if the block is an array.
80a7dea167SDimitry Andric   const bool IsArray = false;
81a7dea167SDimitry Andric 
82a7dea167SDimitry Andric   /// Storage management methods.
83a7dea167SDimitry Andric   const BlockCtorFn CtorFn = nullptr;
84a7dea167SDimitry Andric   const BlockDtorFn DtorFn = nullptr;
85a7dea167SDimitry Andric   const BlockMoveFn MoveFn = nullptr;
86a7dea167SDimitry Andric 
87a7dea167SDimitry Andric   /// Allocates a descriptor for a primitive.
88a7dea167SDimitry Andric   Descriptor(const DeclTy &D, PrimType Type, bool IsConst, bool IsTemporary,
89a7dea167SDimitry Andric              bool IsMutable);
90a7dea167SDimitry Andric 
91a7dea167SDimitry Andric   /// Allocates a descriptor for an array of primitives.
92a7dea167SDimitry Andric   Descriptor(const DeclTy &D, PrimType Type, size_t NumElems, bool IsConst,
93a7dea167SDimitry Andric              bool IsTemporary, bool IsMutable);
94a7dea167SDimitry Andric 
95a7dea167SDimitry Andric   /// Allocates a descriptor for an array of primitives of unknown size.
96a7dea167SDimitry Andric   Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary, UnknownSize);
97a7dea167SDimitry Andric 
98a7dea167SDimitry Andric   /// Allocates a descriptor for an array of composites.
99a7dea167SDimitry Andric   Descriptor(const DeclTy &D, Descriptor *Elem, unsigned NumElems, bool IsConst,
100a7dea167SDimitry Andric              bool IsTemporary, bool IsMutable);
101a7dea167SDimitry Andric 
102a7dea167SDimitry Andric   /// Allocates a descriptor for an array of composites of unknown size.
103a7dea167SDimitry Andric   Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize);
104a7dea167SDimitry Andric 
105a7dea167SDimitry Andric   /// Allocates a descriptor for a record.
106a7dea167SDimitry Andric   Descriptor(const DeclTy &D, Record *R, bool IsConst, bool IsTemporary,
107a7dea167SDimitry Andric              bool IsMutable);
108a7dea167SDimitry Andric 
109a7dea167SDimitry Andric   QualType getType() const;
110a7dea167SDimitry Andric   SourceLocation getLocation() const;
111a7dea167SDimitry Andric 
112a7dea167SDimitry Andric   const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
113a7dea167SDimitry Andric   const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
114a7dea167SDimitry Andric 
115a7dea167SDimitry Andric   const ValueDecl *asValueDecl() const {
116a7dea167SDimitry Andric     return dyn_cast_or_null<ValueDecl>(asDecl());
117a7dea167SDimitry Andric   }
118a7dea167SDimitry Andric 
119a7dea167SDimitry Andric   const FieldDecl *asFieldDecl() const {
120a7dea167SDimitry Andric     return dyn_cast_or_null<FieldDecl>(asDecl());
121a7dea167SDimitry Andric   }
122a7dea167SDimitry Andric 
123a7dea167SDimitry Andric   const RecordDecl *asRecordDecl() const {
124a7dea167SDimitry Andric     return dyn_cast_or_null<RecordDecl>(asDecl());
125a7dea167SDimitry Andric   }
126a7dea167SDimitry Andric 
127a7dea167SDimitry Andric   /// Returns the size of the object without metadata.
128a7dea167SDimitry Andric   unsigned getSize() const {
129a7dea167SDimitry Andric     assert(!isUnknownSizeArray() && "Array of unknown size");
130a7dea167SDimitry Andric     return Size;
131a7dea167SDimitry Andric   }
132a7dea167SDimitry Andric 
133a7dea167SDimitry Andric   /// Returns the allocated size, including metadata.
134a7dea167SDimitry Andric   unsigned getAllocSize() const { return AllocSize; }
135a7dea167SDimitry Andric   /// returns the size of an element when the structure is viewed as an array.
136a7dea167SDimitry Andric   unsigned getElemSize()  const { return ElemSize; }
137a7dea167SDimitry Andric 
138a7dea167SDimitry Andric   /// Returns the number of elements stored in the block.
139a7dea167SDimitry Andric   unsigned getNumElems() const {
140a7dea167SDimitry Andric     return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
141a7dea167SDimitry Andric   }
142a7dea167SDimitry Andric 
143a7dea167SDimitry Andric   /// Checks if the descriptor is of an array of primitives.
144a7dea167SDimitry Andric   bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
145a7dea167SDimitry Andric   /// Checks if the descriptor is of an array of zero size.
146a7dea167SDimitry Andric   bool isZeroSizeArray() const { return Size == 0; }
147a7dea167SDimitry Andric   /// Checks if the descriptor is of an array of unknown size.
148a7dea167SDimitry Andric   bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
149a7dea167SDimitry Andric 
150a7dea167SDimitry Andric   /// Checks if the descriptor is of a primitive.
151a7dea167SDimitry Andric   bool isPrimitive() const { return !IsArray && !ElemRecord; }
152a7dea167SDimitry Andric 
153a7dea167SDimitry Andric   /// Checks if the descriptor is of an array.
154a7dea167SDimitry Andric   bool isArray() const { return IsArray; }
155a7dea167SDimitry Andric };
156a7dea167SDimitry Andric 
157a7dea167SDimitry Andric /// Inline descriptor embedded in structures and arrays.
158a7dea167SDimitry Andric ///
159a7dea167SDimitry Andric /// Such descriptors precede all composite array elements and structure fields.
160a7dea167SDimitry Andric /// If the base of a pointer is not zero, the base points to the end of this
161a7dea167SDimitry Andric /// structure. The offset field is used to traverse the pointer chain up
162a7dea167SDimitry Andric /// to the root structure which allocated the object.
163a7dea167SDimitry Andric struct InlineDescriptor {
164a7dea167SDimitry Andric   /// Offset inside the structure/array.
165a7dea167SDimitry Andric   unsigned Offset;
166a7dea167SDimitry Andric 
167a7dea167SDimitry Andric   /// Flag indicating if the storage is constant or not.
168a7dea167SDimitry Andric   /// Relevant for primitive fields.
169a7dea167SDimitry Andric   unsigned IsConst : 1;
170a7dea167SDimitry Andric   /// For primitive fields, it indicates if the field was initialized.
171a7dea167SDimitry Andric   /// Primitive fields in static storage are always initialized.
172a7dea167SDimitry Andric   /// Arrays are always initialized, even though their elements might not be.
173a7dea167SDimitry Andric   /// Base classes are initialized after the constructor is invoked.
174a7dea167SDimitry Andric   unsigned IsInitialized : 1;
175a7dea167SDimitry Andric   /// Flag indicating if the field is an embedded base class.
176a7dea167SDimitry Andric   unsigned IsBase : 1;
177a7dea167SDimitry Andric   /// Flag indicating if the field is the active member of a union.
178a7dea167SDimitry Andric   unsigned IsActive : 1;
179a7dea167SDimitry Andric   /// Flag indicating if the field is mutable (if in a record).
180a7dea167SDimitry Andric   unsigned IsMutable : 1;
181a7dea167SDimitry Andric 
182a7dea167SDimitry Andric   Descriptor *Desc;
183a7dea167SDimitry Andric };
184a7dea167SDimitry Andric 
185a7dea167SDimitry Andric /// Bitfield tracking the initialisation status of elements of primitive arrays.
186a7dea167SDimitry Andric /// A pointer to this is embedded at the end of all primitive arrays.
187*349cc55cSDimitry Andric /// If the map was not yet created and nothing was initialized, the pointer to
188a7dea167SDimitry Andric /// this structure is 0. If the object was fully initialized, the pointer is -1.
189a7dea167SDimitry Andric struct InitMap {
190a7dea167SDimitry Andric private:
191a7dea167SDimitry Andric   /// Type packing bits.
192a7dea167SDimitry Andric   using T = uint64_t;
193a7dea167SDimitry Andric   /// Bits stored in a single field.
194a7dea167SDimitry Andric   static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
195a7dea167SDimitry Andric 
196a7dea167SDimitry Andric   /// Initializes the map with no fields set.
197a7dea167SDimitry Andric   InitMap(unsigned N);
198a7dea167SDimitry Andric 
199a7dea167SDimitry Andric   /// Returns a pointer to storage.
200a7dea167SDimitry Andric   T *data();
201a7dea167SDimitry Andric 
202a7dea167SDimitry Andric public:
203a7dea167SDimitry Andric   /// Initializes an element. Returns true when object if fully initialized.
204a7dea167SDimitry Andric   bool initialize(unsigned I);
205a7dea167SDimitry Andric 
206a7dea167SDimitry Andric   /// Checks if an element was initialized.
207a7dea167SDimitry Andric   bool isInitialized(unsigned I);
208a7dea167SDimitry Andric 
209a7dea167SDimitry Andric   /// Allocates a map holding N elements.
210a7dea167SDimitry Andric   static InitMap *allocate(unsigned N);
211a7dea167SDimitry Andric 
212a7dea167SDimitry Andric private:
213a7dea167SDimitry Andric   /// Number of fields initialized.
214a7dea167SDimitry Andric   unsigned UninitFields;
215a7dea167SDimitry Andric };
216a7dea167SDimitry Andric 
217a7dea167SDimitry Andric } // namespace interp
218a7dea167SDimitry Andric } // namespace clang
219a7dea167SDimitry Andric 
220a7dea167SDimitry Andric #endif
221