1 //===--- Descriptor.h - Types for the constexpr VM --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines descriptors which characterise allocations.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H
14 #define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/Expr.h"
24 enum PrimType : unsigned;
26 using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
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);
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);
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);
47 /// Object size as used by the interpreter.
48 using InterpSize = unsigned;
50 /// Describes a memory block created by an allocation site.
53 /// Original declaration, used to emit the error message.
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;
62 /// Value to denote arrays of unknown size.
63 static constexpr unsigned UnknownSizeMark = (unsigned)-1;
66 /// Token to denote structures of unknown size.
67 struct UnknownSize {};
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;
82 /// Storage management methods.
83 const BlockCtorFn CtorFn = nullptr;
84 const BlockDtorFn DtorFn = nullptr;
85 const BlockMoveFn MoveFn = nullptr;
87 /// Allocates a descriptor for a primitive.
88 Descriptor(const DeclTy &D, PrimType Type, bool IsConst, bool IsTemporary,
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);
95 /// Allocates a descriptor for an array of primitives of unknown size.
96 Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary, UnknownSize);
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);
102 /// Allocates a descriptor for an array of composites of unknown size.
103 Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize);
105 /// Allocates a descriptor for a record.
106 Descriptor(const DeclTy &D, Record *R, bool IsConst, bool IsTemporary,
109 QualType getType() const;
110 SourceLocation getLocation() const;
112 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
113 const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
115 const ValueDecl *asValueDecl() const {
116 return dyn_cast_or_null<ValueDecl>(asDecl());
119 const FieldDecl *asFieldDecl() const {
120 return dyn_cast_or_null<FieldDecl>(asDecl());
123 const RecordDecl *asRecordDecl() const {
124 return dyn_cast_or_null<RecordDecl>(asDecl());
127 /// Returns the size of the object without metadata.
128 unsigned getSize() const {
129 assert(!isUnknownSizeArray() && "Array of unknown size");
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; }
138 /// Returns the number of elements stored in the block.
139 unsigned getNumElems() const {
140 return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
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; }
150 /// Checks if the descriptor is of a primitive.
151 bool isPrimitive() const { return !IsArray && !ElemRecord; }
153 /// Checks if the descriptor is of an array.
154 bool isArray() const { return IsArray; }
157 /// Inline descriptor embedded in structures and arrays.
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.
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.
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;
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.
191 /// Type packing bits.
193 /// Bits stored in a single field.
194 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
196 /// Initializes the map with no fields set.
199 /// Returns a pointer to storage.
203 /// Initializes an element. Returns true when object if fully initialized.
204 bool initialize(unsigned I);
206 /// Checks if an element was initialized.
207 bool isInitialized(unsigned I);
209 /// Allocates a map holding N elements.
210 static InitMap *allocate(unsigned N);
213 /// Number of fields initialized.
214 unsigned UninitFields;
217 } // namespace interp