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