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 16*0fca6ea1SDimitry Andric #include "PrimType.h" 17a7dea167SDimitry Andric #include "clang/AST/Decl.h" 18a7dea167SDimitry Andric #include "clang/AST/Expr.h" 19a7dea167SDimitry Andric 20a7dea167SDimitry Andric namespace clang { 21a7dea167SDimitry Andric namespace interp { 22a7dea167SDimitry Andric class Block; 23a7dea167SDimitry Andric class Record; 245f757f3fSDimitry Andric struct InitMap; 25a7dea167SDimitry Andric struct Descriptor; 26a7dea167SDimitry Andric enum PrimType : unsigned; 27a7dea167SDimitry Andric 28a7dea167SDimitry Andric using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>; 295f757f3fSDimitry Andric using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>; 30a7dea167SDimitry Andric 31a7dea167SDimitry Andric /// Invoked whenever a block is created. The constructor method fills in the 32a7dea167SDimitry Andric /// inline descriptors of all fields and array elements. It also initializes 33a7dea167SDimitry Andric /// all the fields which contain non-trivial types. 345f757f3fSDimitry Andric using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst, 35a7dea167SDimitry Andric bool IsMutable, bool IsActive, 3606c3fb27SDimitry Andric const Descriptor *FieldDesc); 37a7dea167SDimitry Andric 38a7dea167SDimitry Andric /// Invoked when a block is destroyed. Invokes the destructors of all 39a7dea167SDimitry Andric /// non-trivial nested fields of arrays and records. 405f757f3fSDimitry Andric using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr, 4106c3fb27SDimitry Andric const Descriptor *FieldDesc); 42a7dea167SDimitry Andric 43a7dea167SDimitry Andric /// Invoked when a block with pointers referencing it goes out of scope. Such 44a7dea167SDimitry Andric /// blocks are persisted: the move function copies all inline descriptors and 45a7dea167SDimitry Andric /// non-trivial fields, as existing pointers might need to reference those 46a7dea167SDimitry Andric /// descriptors. Data is not copied since it cannot be legally read. 475f757f3fSDimitry Andric using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr, 485f757f3fSDimitry Andric std::byte *DstFieldPtr, 495f757f3fSDimitry Andric const Descriptor *FieldDesc); 50a7dea167SDimitry Andric 51*0fca6ea1SDimitry Andric enum class GlobalInitState { 52*0fca6ea1SDimitry Andric Initialized, 53*0fca6ea1SDimitry Andric NoInitializer, 54*0fca6ea1SDimitry Andric InitializerFailed, 55*0fca6ea1SDimitry Andric }; 56*0fca6ea1SDimitry Andric 57*0fca6ea1SDimitry Andric /// Descriptor used for global variables. 58*0fca6ea1SDimitry Andric struct alignas(void *) GlobalInlineDescriptor { 59*0fca6ea1SDimitry Andric GlobalInitState InitState = GlobalInitState::InitializerFailed; 60*0fca6ea1SDimitry Andric }; 61*0fca6ea1SDimitry Andric static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), ""); 62*0fca6ea1SDimitry Andric 63a7dea167SDimitry Andric /// Inline descriptor embedded in structures and arrays. 64a7dea167SDimitry Andric /// 65a7dea167SDimitry Andric /// Such descriptors precede all composite array elements and structure fields. 66a7dea167SDimitry Andric /// If the base of a pointer is not zero, the base points to the end of this 67a7dea167SDimitry Andric /// structure. The offset field is used to traverse the pointer chain up 68a7dea167SDimitry Andric /// to the root structure which allocated the object. 69a7dea167SDimitry Andric struct InlineDescriptor { 70a7dea167SDimitry Andric /// Offset inside the structure/array. 71a7dea167SDimitry Andric unsigned Offset; 72a7dea167SDimitry Andric 73a7dea167SDimitry Andric /// Flag indicating if the storage is constant or not. 74a7dea167SDimitry Andric /// Relevant for primitive fields. 75*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 76a7dea167SDimitry Andric unsigned IsConst : 1; 77a7dea167SDimitry Andric /// For primitive fields, it indicates if the field was initialized. 78a7dea167SDimitry Andric /// Primitive fields in static storage are always initialized. 79a7dea167SDimitry Andric /// Arrays are always initialized, even though their elements might not be. 80a7dea167SDimitry Andric /// Base classes are initialized after the constructor is invoked. 81*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 82a7dea167SDimitry Andric unsigned IsInitialized : 1; 83a7dea167SDimitry Andric /// Flag indicating if the field is an embedded base class. 84*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 85a7dea167SDimitry Andric unsigned IsBase : 1; 86*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 87*0fca6ea1SDimitry Andric unsigned IsVirtualBase : 1; 88a7dea167SDimitry Andric /// Flag indicating if the field is the active member of a union. 89*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 90a7dea167SDimitry Andric unsigned IsActive : 1; 91a7dea167SDimitry Andric /// Flag indicating if the field is mutable (if in a record). 92*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 93bdd1243dSDimitry Andric unsigned IsFieldMutable : 1; 94a7dea167SDimitry Andric 955f757f3fSDimitry Andric const Descriptor *Desc; 96*0fca6ea1SDimitry Andric 97*0fca6ea1SDimitry Andric InlineDescriptor(const Descriptor *D) 98*0fca6ea1SDimitry Andric : Offset(sizeof(InlineDescriptor)), IsConst(false), IsInitialized(false), 99*0fca6ea1SDimitry Andric IsBase(false), IsActive(false), IsFieldMutable(false), Desc(D) {} 100*0fca6ea1SDimitry Andric 101*0fca6ea1SDimitry Andric void dump() const { dump(llvm::errs()); } 102*0fca6ea1SDimitry Andric void dump(llvm::raw_ostream &OS) const; 103a7dea167SDimitry Andric }; 104*0fca6ea1SDimitry Andric static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), ""); 105a7dea167SDimitry Andric 106bdd1243dSDimitry Andric /// Describes a memory block created by an allocation site. 107bdd1243dSDimitry Andric struct Descriptor final { 108bdd1243dSDimitry Andric private: 109bdd1243dSDimitry Andric /// Original declaration, used to emit the error message. 110bdd1243dSDimitry Andric const DeclTy Source; 111bdd1243dSDimitry Andric /// Size of an element, in host bytes. 11206c3fb27SDimitry Andric const unsigned ElemSize; 113bdd1243dSDimitry Andric /// Size of the storage, in host bytes. 11406c3fb27SDimitry Andric const unsigned Size; 1155f757f3fSDimitry Andric /// Size of the metadata. 11606c3fb27SDimitry Andric const unsigned MDSize; 117bdd1243dSDimitry Andric /// Size of the allocation (storage + metadata), in host bytes. 11806c3fb27SDimitry Andric const unsigned AllocSize; 119bdd1243dSDimitry Andric 120bdd1243dSDimitry Andric /// Value to denote arrays of unknown size. 121bdd1243dSDimitry Andric static constexpr unsigned UnknownSizeMark = (unsigned)-1; 122bdd1243dSDimitry Andric 123bdd1243dSDimitry Andric public: 124bdd1243dSDimitry Andric /// Token to denote structures of unknown size. 125bdd1243dSDimitry Andric struct UnknownSize {}; 126bdd1243dSDimitry Andric 12706c3fb27SDimitry Andric using MetadataSize = std::optional<unsigned>; 128bdd1243dSDimitry Andric static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor); 129*0fca6ea1SDimitry Andric static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor); 130*0fca6ea1SDimitry Andric 131*0fca6ea1SDimitry Andric /// Maximum number of bytes to be used for array elements. 132*0fca6ea1SDimitry Andric static constexpr unsigned MaxArrayElemBytes = 133*0fca6ea1SDimitry Andric std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) - 134*0fca6ea1SDimitry Andric align(std::max(*InlineDescMD, *GlobalMD)); 135bdd1243dSDimitry Andric 136bdd1243dSDimitry Andric /// Pointer to the record, if block contains records. 137297eecfbSDimitry Andric const Record *const ElemRecord = nullptr; 138bdd1243dSDimitry Andric /// Descriptor of the array element. 1395f757f3fSDimitry Andric const Descriptor *const ElemDesc = nullptr; 140*0fca6ea1SDimitry Andric /// The primitive type this descriptor was created for, 141*0fca6ea1SDimitry Andric /// or the primitive element type in case this is 142*0fca6ea1SDimitry Andric /// a primitive array. 143*0fca6ea1SDimitry Andric const std::optional<PrimType> PrimT = std::nullopt; 144bdd1243dSDimitry Andric /// Flag indicating if the block is mutable. 145bdd1243dSDimitry Andric const bool IsConst = false; 146bdd1243dSDimitry Andric /// Flag indicating if a field is mutable. 147bdd1243dSDimitry Andric const bool IsMutable = false; 148bdd1243dSDimitry Andric /// Flag indicating if the block is a temporary. 149bdd1243dSDimitry Andric const bool IsTemporary = false; 150bdd1243dSDimitry Andric /// Flag indicating if the block is an array. 151bdd1243dSDimitry Andric const bool IsArray = false; 1525f757f3fSDimitry Andric /// Flag indicating if this is a dummy descriptor. 153*0fca6ea1SDimitry Andric bool IsDummy = false; 154bdd1243dSDimitry Andric 155bdd1243dSDimitry Andric /// Storage management methods. 156bdd1243dSDimitry Andric const BlockCtorFn CtorFn = nullptr; 157bdd1243dSDimitry Andric const BlockDtorFn DtorFn = nullptr; 158bdd1243dSDimitry Andric const BlockMoveFn MoveFn = nullptr; 159bdd1243dSDimitry Andric 160bdd1243dSDimitry Andric /// Allocates a descriptor for a primitive. 161bdd1243dSDimitry Andric Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, bool IsConst, 162bdd1243dSDimitry Andric bool IsTemporary, bool IsMutable); 163bdd1243dSDimitry Andric 164bdd1243dSDimitry Andric /// Allocates a descriptor for an array of primitives. 165bdd1243dSDimitry Andric Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems, 166bdd1243dSDimitry Andric bool IsConst, bool IsTemporary, bool IsMutable); 167bdd1243dSDimitry Andric 168bdd1243dSDimitry Andric /// Allocates a descriptor for an array of primitives of unknown size. 169*0fca6ea1SDimitry Andric Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, 170*0fca6ea1SDimitry Andric bool IsTemporary, UnknownSize); 171bdd1243dSDimitry Andric 172bdd1243dSDimitry Andric /// Allocates a descriptor for an array of composites. 1735f757f3fSDimitry Andric Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD, 174bdd1243dSDimitry Andric unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable); 175bdd1243dSDimitry Andric 176bdd1243dSDimitry Andric /// Allocates a descriptor for an array of composites of unknown size. 177*0fca6ea1SDimitry Andric Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD, 178*0fca6ea1SDimitry Andric bool IsTemporary, UnknownSize); 179bdd1243dSDimitry Andric 180bdd1243dSDimitry Andric /// Allocates a descriptor for a record. 181297eecfbSDimitry Andric Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst, 182bdd1243dSDimitry Andric bool IsTemporary, bool IsMutable); 183bdd1243dSDimitry Andric 184*0fca6ea1SDimitry Andric /// Allocates a dummy descriptor. 185*0fca6ea1SDimitry Andric Descriptor(const DeclTy &D); 186*0fca6ea1SDimitry Andric 187*0fca6ea1SDimitry Andric /// Make this descriptor a dummy descriptor. 188*0fca6ea1SDimitry Andric void makeDummy() { IsDummy = true; } 1895f757f3fSDimitry Andric 190bdd1243dSDimitry Andric QualType getType() const; 1915f757f3fSDimitry Andric QualType getElemQualType() const; 192bdd1243dSDimitry Andric SourceLocation getLocation() const; 193bdd1243dSDimitry Andric 194bdd1243dSDimitry Andric const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); } 195bdd1243dSDimitry Andric const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); } 196*0fca6ea1SDimitry Andric const DeclTy &getSource() const { return Source; } 197bdd1243dSDimitry Andric 198bdd1243dSDimitry Andric const ValueDecl *asValueDecl() const { 199bdd1243dSDimitry Andric return dyn_cast_if_present<ValueDecl>(asDecl()); 200bdd1243dSDimitry Andric } 201bdd1243dSDimitry Andric 202*0fca6ea1SDimitry Andric const VarDecl *asVarDecl() const { 203*0fca6ea1SDimitry Andric return dyn_cast_if_present<VarDecl>(asDecl()); 204*0fca6ea1SDimitry Andric } 205*0fca6ea1SDimitry Andric 206bdd1243dSDimitry Andric const FieldDecl *asFieldDecl() const { 207bdd1243dSDimitry Andric return dyn_cast_if_present<FieldDecl>(asDecl()); 208bdd1243dSDimitry Andric } 209bdd1243dSDimitry Andric 210bdd1243dSDimitry Andric const RecordDecl *asRecordDecl() const { 211bdd1243dSDimitry Andric return dyn_cast_if_present<RecordDecl>(asDecl()); 212bdd1243dSDimitry Andric } 213bdd1243dSDimitry Andric 214bdd1243dSDimitry Andric /// Returns the size of the object without metadata. 215bdd1243dSDimitry Andric unsigned getSize() const { 216bdd1243dSDimitry Andric assert(!isUnknownSizeArray() && "Array of unknown size"); 217bdd1243dSDimitry Andric return Size; 218bdd1243dSDimitry Andric } 219bdd1243dSDimitry Andric 220*0fca6ea1SDimitry Andric PrimType getPrimType() const { 221*0fca6ea1SDimitry Andric assert(isPrimitiveArray() || isPrimitive()); 222*0fca6ea1SDimitry Andric return *PrimT; 223*0fca6ea1SDimitry Andric } 224*0fca6ea1SDimitry Andric 225bdd1243dSDimitry Andric /// Returns the allocated size, including metadata. 226bdd1243dSDimitry Andric unsigned getAllocSize() const { return AllocSize; } 227bdd1243dSDimitry Andric /// returns the size of an element when the structure is viewed as an array. 228bdd1243dSDimitry Andric unsigned getElemSize() const { return ElemSize; } 229bdd1243dSDimitry Andric /// Returns the size of the metadata. 230bdd1243dSDimitry Andric unsigned getMetadataSize() const { return MDSize; } 231bdd1243dSDimitry Andric 232bdd1243dSDimitry Andric /// Returns the number of elements stored in the block. 233bdd1243dSDimitry Andric unsigned getNumElems() const { 234bdd1243dSDimitry Andric return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize()); 235bdd1243dSDimitry Andric } 236bdd1243dSDimitry Andric 237bdd1243dSDimitry Andric /// Checks if the descriptor is of an array of primitives. 238bdd1243dSDimitry Andric bool isPrimitiveArray() const { return IsArray && !ElemDesc; } 23906c3fb27SDimitry Andric /// Checks if the descriptor is of an array of composites. 24006c3fb27SDimitry Andric bool isCompositeArray() const { return IsArray && ElemDesc; } 241bdd1243dSDimitry Andric /// Checks if the descriptor is of an array of zero size. 242bdd1243dSDimitry Andric bool isZeroSizeArray() const { return Size == 0; } 243bdd1243dSDimitry Andric /// Checks if the descriptor is of an array of unknown size. 244bdd1243dSDimitry Andric bool isUnknownSizeArray() const { return Size == UnknownSizeMark; } 245bdd1243dSDimitry Andric 246bdd1243dSDimitry Andric /// Checks if the descriptor is of a primitive. 247bdd1243dSDimitry Andric bool isPrimitive() const { return !IsArray && !ElemRecord; } 248bdd1243dSDimitry Andric 249bdd1243dSDimitry Andric /// Checks if the descriptor is of an array. 250bdd1243dSDimitry Andric bool isArray() const { return IsArray; } 2515f757f3fSDimitry Andric /// Checks if the descriptor is of a record. 2525f757f3fSDimitry Andric bool isRecord() const { return !IsArray && ElemRecord; } 2535f757f3fSDimitry Andric /// Checks if this is a dummy descriptor. 2545f757f3fSDimitry Andric bool isDummy() const { return IsDummy; } 255*0fca6ea1SDimitry Andric 256*0fca6ea1SDimitry Andric void dump() const; 257*0fca6ea1SDimitry Andric void dump(llvm::raw_ostream &OS) const; 258bdd1243dSDimitry Andric }; 259bdd1243dSDimitry Andric 260a7dea167SDimitry Andric /// Bitfield tracking the initialisation status of elements of primitive arrays. 261bdd1243dSDimitry Andric struct InitMap final { 262a7dea167SDimitry Andric private: 263a7dea167SDimitry Andric /// Type packing bits. 264a7dea167SDimitry Andric using T = uint64_t; 265a7dea167SDimitry Andric /// Bits stored in a single field. 266a7dea167SDimitry Andric static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT; 267a7dea167SDimitry Andric 268a7dea167SDimitry Andric public: 2695f757f3fSDimitry Andric /// Initializes the map with no fields set. 2705f757f3fSDimitry Andric explicit InitMap(unsigned N); 271a7dea167SDimitry Andric 272a7dea167SDimitry Andric private: 2735f757f3fSDimitry Andric friend class Pointer; 2745f757f3fSDimitry Andric 2755f757f3fSDimitry Andric /// Returns a pointer to storage. 2765f757f3fSDimitry Andric T *data() { return Data.get(); } 2775f757f3fSDimitry Andric const T *data() const { return Data.get(); } 2785f757f3fSDimitry Andric 2795f757f3fSDimitry Andric /// Initializes an element. Returns true when object if fully initialized. 2805f757f3fSDimitry Andric bool initializeElement(unsigned I); 2815f757f3fSDimitry Andric 2825f757f3fSDimitry Andric /// Checks if an element was initialized. 2835f757f3fSDimitry Andric bool isElementInitialized(unsigned I) const; 2845f757f3fSDimitry Andric 2855f757f3fSDimitry Andric static constexpr size_t numFields(unsigned N) { 2865f757f3fSDimitry Andric return (N + PER_FIELD - 1) / PER_FIELD; 2875f757f3fSDimitry Andric } 2885f757f3fSDimitry Andric /// Number of fields not initialized. 289a7dea167SDimitry Andric unsigned UninitFields; 2905f757f3fSDimitry Andric std::unique_ptr<T[]> Data; 291a7dea167SDimitry Andric }; 292a7dea167SDimitry Andric 293a7dea167SDimitry Andric } // namespace interp 294a7dea167SDimitry Andric } // namespace clang 295a7dea167SDimitry Andric 296a7dea167SDimitry Andric #endif 297