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