Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/Descriptor.h
35292 views
//===--- Descriptor.h - Types for the constexpr VM --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Defines descriptors which characterise allocations.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H13#define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H1415#include "PrimType.h"16#include "clang/AST/Decl.h"17#include "clang/AST/Expr.h"1819namespace clang {20namespace interp {21class Block;22class Record;23struct InitMap;24struct Descriptor;25enum PrimType : unsigned;2627using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;28using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>;2930/// Invoked whenever a block is created. The constructor method fills in the31/// inline descriptors of all fields and array elements. It also initializes32/// all the fields which contain non-trivial types.33using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,34bool IsMutable, bool IsActive,35const Descriptor *FieldDesc);3637/// Invoked when a block is destroyed. Invokes the destructors of all38/// non-trivial nested fields of arrays and records.39using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,40const Descriptor *FieldDesc);4142/// Invoked when a block with pointers referencing it goes out of scope. Such43/// blocks are persisted: the move function copies all inline descriptors and44/// non-trivial fields, as existing pointers might need to reference those45/// descriptors. Data is not copied since it cannot be legally read.46using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,47std::byte *DstFieldPtr,48const Descriptor *FieldDesc);4950enum class GlobalInitState {51Initialized,52NoInitializer,53InitializerFailed,54};5556/// Descriptor used for global variables.57struct alignas(void *) GlobalInlineDescriptor {58GlobalInitState InitState = GlobalInitState::InitializerFailed;59};60static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");6162/// Inline descriptor embedded in structures and arrays.63///64/// Such descriptors precede all composite array elements and structure fields.65/// If the base of a pointer is not zero, the base points to the end of this66/// structure. The offset field is used to traverse the pointer chain up67/// to the root structure which allocated the object.68struct InlineDescriptor {69/// Offset inside the structure/array.70unsigned Offset;7172/// Flag indicating if the storage is constant or not.73/// Relevant for primitive fields.74LLVM_PREFERRED_TYPE(bool)75unsigned IsConst : 1;76/// For primitive fields, it indicates if the field was initialized.77/// Primitive fields in static storage are always initialized.78/// Arrays are always initialized, even though their elements might not be.79/// Base classes are initialized after the constructor is invoked.80LLVM_PREFERRED_TYPE(bool)81unsigned IsInitialized : 1;82/// Flag indicating if the field is an embedded base class.83LLVM_PREFERRED_TYPE(bool)84unsigned IsBase : 1;85LLVM_PREFERRED_TYPE(bool)86unsigned IsVirtualBase : 1;87/// Flag indicating if the field is the active member of a union.88LLVM_PREFERRED_TYPE(bool)89unsigned IsActive : 1;90/// Flag indicating if the field is mutable (if in a record).91LLVM_PREFERRED_TYPE(bool)92unsigned IsFieldMutable : 1;9394const Descriptor *Desc;9596InlineDescriptor(const Descriptor *D)97: Offset(sizeof(InlineDescriptor)), IsConst(false), IsInitialized(false),98IsBase(false), IsActive(false), IsFieldMutable(false), Desc(D) {}99100void dump() const { dump(llvm::errs()); }101void dump(llvm::raw_ostream &OS) const;102};103static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");104105/// Describes a memory block created by an allocation site.106struct Descriptor final {107private:108/// Original declaration, used to emit the error message.109const DeclTy Source;110/// Size of an element, in host bytes.111const unsigned ElemSize;112/// Size of the storage, in host bytes.113const unsigned Size;114/// Size of the metadata.115const unsigned MDSize;116/// Size of the allocation (storage + metadata), in host bytes.117const unsigned AllocSize;118119/// Value to denote arrays of unknown size.120static constexpr unsigned UnknownSizeMark = (unsigned)-1;121122public:123/// Token to denote structures of unknown size.124struct UnknownSize {};125126using MetadataSize = std::optional<unsigned>;127static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);128static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);129130/// Maximum number of bytes to be used for array elements.131static constexpr unsigned MaxArrayElemBytes =132std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -133align(std::max(*InlineDescMD, *GlobalMD));134135/// Pointer to the record, if block contains records.136const Record *const ElemRecord = nullptr;137/// Descriptor of the array element.138const Descriptor *const ElemDesc = nullptr;139/// The primitive type this descriptor was created for,140/// or the primitive element type in case this is141/// a primitive array.142const std::optional<PrimType> PrimT = std::nullopt;143/// Flag indicating if the block is mutable.144const bool IsConst = false;145/// Flag indicating if a field is mutable.146const bool IsMutable = false;147/// Flag indicating if the block is a temporary.148const bool IsTemporary = false;149/// Flag indicating if the block is an array.150const bool IsArray = false;151/// Flag indicating if this is a dummy descriptor.152bool IsDummy = false;153154/// Storage management methods.155const BlockCtorFn CtorFn = nullptr;156const BlockDtorFn DtorFn = nullptr;157const BlockMoveFn MoveFn = nullptr;158159/// Allocates a descriptor for a primitive.160Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, bool IsConst,161bool IsTemporary, bool IsMutable);162163/// Allocates a descriptor for an array of primitives.164Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,165bool IsConst, bool IsTemporary, bool IsMutable);166167/// Allocates a descriptor for an array of primitives of unknown size.168Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize,169bool IsTemporary, UnknownSize);170171/// Allocates a descriptor for an array of composites.172Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,173unsigned NumElems, bool IsConst, bool IsTemporary, bool IsMutable);174175/// Allocates a descriptor for an array of composites of unknown size.176Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,177bool IsTemporary, UnknownSize);178179/// Allocates a descriptor for a record.180Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,181bool IsTemporary, bool IsMutable);182183/// Allocates a dummy descriptor.184Descriptor(const DeclTy &D);185186/// Make this descriptor a dummy descriptor.187void makeDummy() { IsDummy = true; }188189QualType getType() const;190QualType getElemQualType() const;191SourceLocation getLocation() const;192193const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }194const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }195const DeclTy &getSource() const { return Source; }196197const ValueDecl *asValueDecl() const {198return dyn_cast_if_present<ValueDecl>(asDecl());199}200201const VarDecl *asVarDecl() const {202return dyn_cast_if_present<VarDecl>(asDecl());203}204205const FieldDecl *asFieldDecl() const {206return dyn_cast_if_present<FieldDecl>(asDecl());207}208209const RecordDecl *asRecordDecl() const {210return dyn_cast_if_present<RecordDecl>(asDecl());211}212213/// Returns the size of the object without metadata.214unsigned getSize() const {215assert(!isUnknownSizeArray() && "Array of unknown size");216return Size;217}218219PrimType getPrimType() const {220assert(isPrimitiveArray() || isPrimitive());221return *PrimT;222}223224/// Returns the allocated size, including metadata.225unsigned getAllocSize() const { return AllocSize; }226/// returns the size of an element when the structure is viewed as an array.227unsigned getElemSize() const { return ElemSize; }228/// Returns the size of the metadata.229unsigned getMetadataSize() const { return MDSize; }230231/// Returns the number of elements stored in the block.232unsigned getNumElems() const {233return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());234}235236/// Checks if the descriptor is of an array of primitives.237bool isPrimitiveArray() const { return IsArray && !ElemDesc; }238/// Checks if the descriptor is of an array of composites.239bool isCompositeArray() const { return IsArray && ElemDesc; }240/// Checks if the descriptor is of an array of zero size.241bool isZeroSizeArray() const { return Size == 0; }242/// Checks if the descriptor is of an array of unknown size.243bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }244245/// Checks if the descriptor is of a primitive.246bool isPrimitive() const { return !IsArray && !ElemRecord; }247248/// Checks if the descriptor is of an array.249bool isArray() const { return IsArray; }250/// Checks if the descriptor is of a record.251bool isRecord() const { return !IsArray && ElemRecord; }252/// Checks if this is a dummy descriptor.253bool isDummy() const { return IsDummy; }254255void dump() const;256void dump(llvm::raw_ostream &OS) const;257};258259/// Bitfield tracking the initialisation status of elements of primitive arrays.260struct InitMap final {261private:262/// Type packing bits.263using T = uint64_t;264/// Bits stored in a single field.265static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;266267public:268/// Initializes the map with no fields set.269explicit InitMap(unsigned N);270271private:272friend class Pointer;273274/// Returns a pointer to storage.275T *data() { return Data.get(); }276const T *data() const { return Data.get(); }277278/// Initializes an element. Returns true when object if fully initialized.279bool initializeElement(unsigned I);280281/// Checks if an element was initialized.282bool isElementInitialized(unsigned I) const;283284static constexpr size_t numFields(unsigned N) {285return (N + PER_FIELD - 1) / PER_FIELD;286}287/// Number of fields not initialized.288unsigned UninitFields;289std::unique_ptr<T[]> Data;290};291292} // namespace interp293} // namespace clang294295#endif296297298