1*a7dea167SDimitry Andric //===--- Descriptor.h - Types for the constexpr VM --------------*- C++ -*-===// 2*a7dea167SDimitry Andric // 3*a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*a7dea167SDimitry Andric // 7*a7dea167SDimitry Andric //===----------------------------------------------------------------------===// 8*a7dea167SDimitry Andric // 9*a7dea167SDimitry Andric // Defines descriptors which characterise allocations. 10*a7dea167SDimitry Andric // 11*a7dea167SDimitry Andric //===----------------------------------------------------------------------===// 12*a7dea167SDimitry Andric 13*a7dea167SDimitry Andric #ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H 14*a7dea167SDimitry Andric #define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H 15*a7dea167SDimitry Andric 16*a7dea167SDimitry Andric #include "clang/AST/Decl.h" 17*a7dea167SDimitry Andric #include "clang/AST/Expr.h" 18*a7dea167SDimitry Andric 19*a7dea167SDimitry Andric namespace clang { 20*a7dea167SDimitry Andric namespace interp { 21*a7dea167SDimitry Andric class Block; 22*a7dea167SDimitry Andric class Record; 23*a7dea167SDimitry Andric struct Descriptor; 24*a7dea167SDimitry Andric enum PrimType : unsigned; 25*a7dea167SDimitry Andric 26*a7dea167SDimitry Andric using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>; 27*a7dea167SDimitry Andric 28*a7dea167SDimitry Andric /// Invoked whenever a block is created. The constructor method fills in the 29*a7dea167SDimitry Andric /// inline descriptors of all fields and array elements. It also initializes 30*a7dea167SDimitry Andric /// all the fields which contain non-trivial types. 31*a7dea167SDimitry Andric using BlockCtorFn = void (*)(Block *Storage, char *FieldPtr, bool IsConst, 32*a7dea167SDimitry Andric bool IsMutable, bool IsActive, 33*a7dea167SDimitry Andric Descriptor *FieldDesc); 34*a7dea167SDimitry Andric 35*a7dea167SDimitry Andric /// Invoked when a block is destroyed. Invokes the destructors of all 36*a7dea167SDimitry Andric /// non-trivial nested fields of arrays and records. 37*a7dea167SDimitry Andric using BlockDtorFn = void (*)(Block *Storage, char *FieldPtr, 38*a7dea167SDimitry Andric Descriptor *FieldDesc); 39*a7dea167SDimitry Andric 40*a7dea167SDimitry Andric /// Invoked when a block with pointers referencing it goes out of scope. Such 41*a7dea167SDimitry Andric /// blocks are persisted: the move function copies all inline descriptors and 42*a7dea167SDimitry Andric /// non-trivial fields, as existing pointers might need to reference those 43*a7dea167SDimitry Andric /// descriptors. Data is not copied since it cannot be legally read. 44*a7dea167SDimitry Andric using BlockMoveFn = void (*)(Block *Storage, char *SrcFieldPtr, 45*a7dea167SDimitry Andric char *DstFieldPtr, Descriptor *FieldDesc); 46*a7dea167SDimitry Andric 47*a7dea167SDimitry Andric /// Object size as used by the interpreter. 48*a7dea167SDimitry Andric using InterpSize = unsigned; 49*a7dea167SDimitry Andric 50*a7dea167SDimitry Andric /// Describes a memory block created by an allocation site. 51*a7dea167SDimitry Andric struct Descriptor { 52*a7dea167SDimitry Andric private: 53*a7dea167SDimitry Andric /// Original declaration, used to emit the error message. 54*a7dea167SDimitry Andric const DeclTy Source; 55*a7dea167SDimitry Andric /// Size of an element, in host bytes. 56*a7dea167SDimitry Andric const InterpSize ElemSize; 57*a7dea167SDimitry Andric /// Size of the storage, in host bytes. 58*a7dea167SDimitry Andric const InterpSize Size; 59*a7dea167SDimitry Andric /// Size of the allocation (storage + metadata), in host bytes. 60*a7dea167SDimitry Andric const InterpSize AllocSize; 61*a7dea167SDimitry Andric 62*a7dea167SDimitry Andric /// Value to denote arrays of unknown size. 63*a7dea167SDimitry Andric static constexpr unsigned UnknownSizeMark = (unsigned)-1; 64*a7dea167SDimitry Andric 65*a7dea167SDimitry Andric public: 66*a7dea167SDimitry Andric /// Token to denote structures of unknown size. 67*a7dea167SDimitry Andric struct UnknownSize {}; 68*a7dea167SDimitry Andric 69*a7dea167SDimitry Andric /// Pointer to the record, if block contains records. 70*a7dea167SDimitry Andric Record *const ElemRecord = nullptr; 71*a7dea167SDimitry Andric /// Descriptor of the array element. 72*a7dea167SDimitry Andric Descriptor *const ElemDesc = nullptr; 73*a7dea167SDimitry Andric /// Flag indicating if the block is mutable. 74*a7dea167SDimitry Andric const bool IsConst = false; 75*a7dea167SDimitry Andric /// Flag indicating if a field is mutable. 76*a7dea167SDimitry Andric const bool IsMutable = false; 77*a7dea167SDimitry Andric /// Flag indicating if the block is a temporary. 78*a7dea167SDimitry Andric const bool IsTemporary = false; 79*a7dea167SDimitry Andric /// Flag indicating if the block is an array. 80*a7dea167SDimitry Andric const bool IsArray = false; 81*a7dea167SDimitry Andric 82*a7dea167SDimitry Andric /// Storage management methods. 83*a7dea167SDimitry Andric const BlockCtorFn CtorFn = nullptr; 84*a7dea167SDimitry Andric const BlockDtorFn DtorFn = nullptr; 85*a7dea167SDimitry Andric const BlockMoveFn MoveFn = nullptr; 86*a7dea167SDimitry Andric 87*a7dea167SDimitry Andric /// Allocates a descriptor for a primitive. 88*a7dea167SDimitry Andric Descriptor(const DeclTy &D, PrimType Type, bool IsConst, bool IsTemporary, 89*a7dea167SDimitry Andric bool IsMutable); 90*a7dea167SDimitry Andric 91*a7dea167SDimitry Andric /// Allocates a descriptor for an array of primitives. 92*a7dea167SDimitry Andric Descriptor(const DeclTy &D, PrimType Type, size_t NumElems, bool IsConst, 93*a7dea167SDimitry Andric bool IsTemporary, bool IsMutable); 94*a7dea167SDimitry Andric 95*a7dea167SDimitry Andric /// Allocates a descriptor for an array of primitives of unknown size. 96*a7dea167SDimitry Andric Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary, UnknownSize); 97*a7dea167SDimitry Andric 98*a7dea167SDimitry Andric /// Allocates a descriptor for an array of composites. 99*a7dea167SDimitry Andric Descriptor(const DeclTy &D, Descriptor *Elem, unsigned NumElems, bool IsConst, 100*a7dea167SDimitry Andric bool IsTemporary, bool IsMutable); 101*a7dea167SDimitry Andric 102*a7dea167SDimitry Andric /// Allocates a descriptor for an array of composites of unknown size. 103*a7dea167SDimitry Andric Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize); 104*a7dea167SDimitry Andric 105*a7dea167SDimitry Andric /// Allocates a descriptor for a record. 106*a7dea167SDimitry Andric Descriptor(const DeclTy &D, Record *R, bool IsConst, bool IsTemporary, 107*a7dea167SDimitry Andric bool IsMutable); 108*a7dea167SDimitry Andric 109*a7dea167SDimitry Andric QualType getType() const; 110*a7dea167SDimitry Andric SourceLocation getLocation() const; 111*a7dea167SDimitry Andric 112*a7dea167SDimitry Andric const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); } 113*a7dea167SDimitry Andric const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); } 114*a7dea167SDimitry Andric 115*a7dea167SDimitry Andric const ValueDecl *asValueDecl() const { 116*a7dea167SDimitry Andric return dyn_cast_or_null<ValueDecl>(asDecl()); 117*a7dea167SDimitry Andric } 118*a7dea167SDimitry Andric 119*a7dea167SDimitry Andric const FieldDecl *asFieldDecl() const { 120*a7dea167SDimitry Andric return dyn_cast_or_null<FieldDecl>(asDecl()); 121*a7dea167SDimitry Andric } 122*a7dea167SDimitry Andric 123*a7dea167SDimitry Andric const RecordDecl *asRecordDecl() const { 124*a7dea167SDimitry Andric return dyn_cast_or_null<RecordDecl>(asDecl()); 125*a7dea167SDimitry Andric } 126*a7dea167SDimitry Andric 127*a7dea167SDimitry Andric /// Returns the size of the object without metadata. 128*a7dea167SDimitry Andric unsigned getSize() const { 129*a7dea167SDimitry Andric assert(!isUnknownSizeArray() && "Array of unknown size"); 130*a7dea167SDimitry Andric return Size; 131*a7dea167SDimitry Andric } 132*a7dea167SDimitry Andric 133*a7dea167SDimitry Andric /// Returns the allocated size, including metadata. 134*a7dea167SDimitry Andric unsigned getAllocSize() const { return AllocSize; } 135*a7dea167SDimitry Andric /// returns the size of an element when the structure is viewed as an array. 136*a7dea167SDimitry Andric unsigned getElemSize() const { return ElemSize; } 137*a7dea167SDimitry Andric 138*a7dea167SDimitry Andric /// Returns the number of elements stored in the block. 139*a7dea167SDimitry Andric unsigned getNumElems() const { 140*a7dea167SDimitry Andric return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize()); 141*a7dea167SDimitry Andric } 142*a7dea167SDimitry Andric 143*a7dea167SDimitry Andric /// Checks if the descriptor is of an array of primitives. 144*a7dea167SDimitry Andric bool isPrimitiveArray() const { return IsArray && !ElemDesc; } 145*a7dea167SDimitry Andric /// Checks if the descriptor is of an array of zero size. 146*a7dea167SDimitry Andric bool isZeroSizeArray() const { return Size == 0; } 147*a7dea167SDimitry Andric /// Checks if the descriptor is of an array of unknown size. 148*a7dea167SDimitry Andric bool isUnknownSizeArray() const { return Size == UnknownSizeMark; } 149*a7dea167SDimitry Andric 150*a7dea167SDimitry Andric /// Checks if the descriptor is of a primitive. 151*a7dea167SDimitry Andric bool isPrimitive() const { return !IsArray && !ElemRecord; } 152*a7dea167SDimitry Andric 153*a7dea167SDimitry Andric /// Checks if the descriptor is of an array. 154*a7dea167SDimitry Andric bool isArray() const { return IsArray; } 155*a7dea167SDimitry Andric }; 156*a7dea167SDimitry Andric 157*a7dea167SDimitry Andric /// Inline descriptor embedded in structures and arrays. 158*a7dea167SDimitry Andric /// 159*a7dea167SDimitry Andric /// Such descriptors precede all composite array elements and structure fields. 160*a7dea167SDimitry Andric /// If the base of a pointer is not zero, the base points to the end of this 161*a7dea167SDimitry Andric /// structure. The offset field is used to traverse the pointer chain up 162*a7dea167SDimitry Andric /// to the root structure which allocated the object. 163*a7dea167SDimitry Andric struct InlineDescriptor { 164*a7dea167SDimitry Andric /// Offset inside the structure/array. 165*a7dea167SDimitry Andric unsigned Offset; 166*a7dea167SDimitry Andric 167*a7dea167SDimitry Andric /// Flag indicating if the storage is constant or not. 168*a7dea167SDimitry Andric /// Relevant for primitive fields. 169*a7dea167SDimitry Andric unsigned IsConst : 1; 170*a7dea167SDimitry Andric /// For primitive fields, it indicates if the field was initialized. 171*a7dea167SDimitry Andric /// Primitive fields in static storage are always initialized. 172*a7dea167SDimitry Andric /// Arrays are always initialized, even though their elements might not be. 173*a7dea167SDimitry Andric /// Base classes are initialized after the constructor is invoked. 174*a7dea167SDimitry Andric unsigned IsInitialized : 1; 175*a7dea167SDimitry Andric /// Flag indicating if the field is an embedded base class. 176*a7dea167SDimitry Andric unsigned IsBase : 1; 177*a7dea167SDimitry Andric /// Flag indicating if the field is the active member of a union. 178*a7dea167SDimitry Andric unsigned IsActive : 1; 179*a7dea167SDimitry Andric /// Flag indicating if the field is mutable (if in a record). 180*a7dea167SDimitry Andric unsigned IsMutable : 1; 181*a7dea167SDimitry Andric 182*a7dea167SDimitry Andric Descriptor *Desc; 183*a7dea167SDimitry Andric }; 184*a7dea167SDimitry Andric 185*a7dea167SDimitry Andric /// Bitfield tracking the initialisation status of elements of primitive arrays. 186*a7dea167SDimitry Andric /// A pointer to this is embedded at the end of all primitive arrays. 187*a7dea167SDimitry Andric /// If the map was not yet created and nothing was initialied, the pointer to 188*a7dea167SDimitry Andric /// this structure is 0. If the object was fully initialized, the pointer is -1. 189*a7dea167SDimitry Andric struct InitMap { 190*a7dea167SDimitry Andric private: 191*a7dea167SDimitry Andric /// Type packing bits. 192*a7dea167SDimitry Andric using T = uint64_t; 193*a7dea167SDimitry Andric /// Bits stored in a single field. 194*a7dea167SDimitry Andric static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT; 195*a7dea167SDimitry Andric 196*a7dea167SDimitry Andric /// Initializes the map with no fields set. 197*a7dea167SDimitry Andric InitMap(unsigned N); 198*a7dea167SDimitry Andric 199*a7dea167SDimitry Andric /// Returns a pointer to storage. 200*a7dea167SDimitry Andric T *data(); 201*a7dea167SDimitry Andric 202*a7dea167SDimitry Andric public: 203*a7dea167SDimitry Andric /// Initializes an element. Returns true when object if fully initialized. 204*a7dea167SDimitry Andric bool initialize(unsigned I); 205*a7dea167SDimitry Andric 206*a7dea167SDimitry Andric /// Checks if an element was initialized. 207*a7dea167SDimitry Andric bool isInitialized(unsigned I); 208*a7dea167SDimitry Andric 209*a7dea167SDimitry Andric /// Allocates a map holding N elements. 210*a7dea167SDimitry Andric static InitMap *allocate(unsigned N); 211*a7dea167SDimitry Andric 212*a7dea167SDimitry Andric private: 213*a7dea167SDimitry Andric /// Number of fields initialized. 214*a7dea167SDimitry Andric unsigned UninitFields; 215*a7dea167SDimitry Andric }; 216*a7dea167SDimitry Andric 217*a7dea167SDimitry Andric } // namespace interp 218*a7dea167SDimitry Andric } // namespace clang 219*a7dea167SDimitry Andric 220*a7dea167SDimitry Andric #endif 221