brintos

brintos / llvm-project-archived public Read only

0
0
Text · 11.1 KiB · 90dc2b4 Raw
317 lines · c
1//===--- Descriptor.h - Types for the constexpr VM --------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// Defines descriptors which characterise allocations.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H14#define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H15 16#include "PrimType.h"17#include "clang/AST/Decl.h"18#include "clang/AST/Expr.h"19 20namespace clang {21namespace interp {22class Block;23class Record;24class SourceInfo;25struct InitMap;26struct Descriptor;27enum PrimType : uint8_t;28 29using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;30using InitMapPtr = std::optional<std::pair<bool, std::shared_ptr<InitMap>>>;31 32/// Invoked whenever a block is created. The constructor method fills in the33/// inline descriptors of all fields and array elements. It also initializes34/// all the fields which contain non-trivial types.35using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,36                             bool IsMutable, bool IsVolatile, bool IsActive,37                             bool InUnion, const Descriptor *FieldDesc);38 39/// Invoked when a block is destroyed. Invokes the destructors of all40/// non-trivial nested fields of arrays and records.41using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,42                             const Descriptor *FieldDesc);43 44enum class GlobalInitState {45  Initialized,46  NoInitializer,47  InitializerFailed,48};49 50/// Descriptor used for global variables.51struct alignas(void *) GlobalInlineDescriptor {52  GlobalInitState InitState = GlobalInitState::InitializerFailed;53};54static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "");55 56enum class Lifetime : uint8_t {57  Started,58  Ended,59};60 61/// Inline descriptor embedded in structures and arrays.62///63/// Such descriptors precede all composite array elements and structure fields.64/// If the base of a pointer is not zero, the base points to the end of this65/// structure. The offset field is used to traverse the pointer chain up66/// to the root structure which allocated the object.67struct InlineDescriptor {68  /// Offset inside the structure/array.69  unsigned Offset;70 71  /// Flag indicating if the storage is constant or not.72  /// Relevant for primitive fields.73  LLVM_PREFERRED_TYPE(bool)74  unsigned IsConst : 1;75  /// For primitive fields, it indicates if the field was initialized.76  /// Primitive fields in static storage are always initialized.77  /// Arrays are always initialized, even though their elements might not be.78  /// Base classes are initialized after the constructor is invoked.79  LLVM_PREFERRED_TYPE(bool)80  unsigned IsInitialized : 1;81  /// Flag indicating if the field is an embedded base class.82  LLVM_PREFERRED_TYPE(bool)83  unsigned IsBase : 1;84  /// Flag inidcating if the field is a virtual base class.85  LLVM_PREFERRED_TYPE(bool)86  unsigned IsVirtualBase : 1;87  /// Flag indicating if the field is the active member of a union.88  LLVM_PREFERRED_TYPE(bool)89  unsigned IsActive : 1;90  /// Flag indicating if this field is in a union (even if nested).91  LLVM_PREFERRED_TYPE(bool)92  unsigned InUnion : 1;93  /// Flag indicating if the field is mutable (if in a record).94  LLVM_PREFERRED_TYPE(bool)95  unsigned IsFieldMutable : 1;96  /// Flag indicating if this field is a const field nested in97  /// a mutable parent field.98  LLVM_PREFERRED_TYPE(bool)99  unsigned IsConstInMutable : 1;100  /// Flag indicating if the field is an element of a composite array.101  LLVM_PREFERRED_TYPE(bool)102  unsigned IsArrayElement : 1;103  LLVM_PREFERRED_TYPE(bool)104  unsigned IsVolatile : 1;105 106  Lifetime LifeState;107 108  const Descriptor *Desc;109 110  InlineDescriptor(const Descriptor *D)111      : Offset(sizeof(InlineDescriptor)), IsConst(false), IsInitialized(false),112        IsBase(false), IsActive(false), IsFieldMutable(false),113        IsArrayElement(false), IsVolatile(false), LifeState(Lifetime::Started),114        Desc(D) {}115 116  void dump() const { dump(llvm::errs()); }117  void dump(llvm::raw_ostream &OS) const;118};119static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");120 121/// Describes a memory block created by an allocation site.122struct Descriptor final {123private:124  /// Original declaration, used to emit the error message.125  const DeclTy Source;126  const Type *SourceType = nullptr;127  /// Size of an element, in host bytes.128  const unsigned ElemSize;129  /// Size of the storage, in host bytes.130  const unsigned Size;131  /// Size of the metadata.132  const unsigned MDSize;133  /// Size of the allocation (storage + metadata), in host bytes.134  const unsigned AllocSize;135 136  /// Value to denote arrays of unknown size.137  static constexpr unsigned UnknownSizeMark = (unsigned)-1;138 139public:140  /// Token to denote structures of unknown size.141  struct UnknownSize {};142 143  using MetadataSize = std::optional<unsigned>;144  static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor);145  static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor);146 147  /// Maximum number of bytes to be used for array elements.148  static constexpr unsigned MaxArrayElemBytes =149      std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) -150      align(std::max(*InlineDescMD, *GlobalMD));151 152  /// Pointer to the record, if block contains records.153  const Record *const ElemRecord = nullptr;154  /// Descriptor of the array element.155  const Descriptor *const ElemDesc = nullptr;156  /// The primitive type this descriptor was created for,157  /// or the primitive element type in case this is158  /// a primitive array.159  const OptPrimType PrimT = std::nullopt;160  /// Flag indicating if the block is mutable.161  const bool IsConst = false;162  /// Flag indicating if a field is mutable.163  const bool IsMutable = false;164  /// Flag indicating if the block is a temporary.165  const bool IsTemporary = false;166  const bool IsVolatile = false;167  /// Flag indicating if the block is an array.168  const bool IsArray = false;169  bool IsConstexprUnknown = false;170 171  /// Storage management methods.172  const BlockCtorFn CtorFn = nullptr;173  const BlockDtorFn DtorFn = nullptr;174 175  /// Allocates a descriptor for a primitive.176  Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type,177             MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable,178             bool IsVolatile);179 180  /// Allocates a descriptor for an array of primitives.181  Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems,182             bool IsConst, bool IsTemporary, bool IsMutable);183 184  /// Allocates a descriptor for an array of primitives of unknown size.185  Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst,186             bool IsTemporary, UnknownSize);187 188  /// Allocates a descriptor for an array of composites.189  Descriptor(const DeclTy &D, const Type *SourceTy, const Descriptor *Elem,190             MetadataSize MD, unsigned NumElems, bool IsConst, bool IsTemporary,191             bool IsMutable);192 193  /// Allocates a descriptor for an array of composites of unknown size.194  Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD,195             bool IsTemporary, UnknownSize);196 197  /// Allocates a descriptor for a record.198  Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst,199             bool IsTemporary, bool IsMutable, bool IsVolatile);200 201  /// Allocates a dummy descriptor.202  Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);203 204  QualType getType() const;205  QualType getElemQualType() const;206  QualType getDataType(const ASTContext &Ctx) const;207  SourceLocation getLocation() const;208  SourceInfo getLoc() const;209 210  const Decl *asDecl() const { return dyn_cast<const Decl *>(Source); }211  const Expr *asExpr() const { return dyn_cast<const Expr *>(Source); }212  const DeclTy &getSource() const { return Source; }213 214  const ValueDecl *asValueDecl() const {215    return dyn_cast_if_present<ValueDecl>(asDecl());216  }217 218  const VarDecl *asVarDecl() const {219    return dyn_cast_if_present<VarDecl>(asDecl());220  }221 222  const FieldDecl *asFieldDecl() const {223    return dyn_cast_if_present<FieldDecl>(asDecl());224  }225 226  const RecordDecl *asRecordDecl() const {227    return dyn_cast_if_present<RecordDecl>(asDecl());228  }229 230  /// Returns the size of the object without metadata.231  unsigned getSize() const {232    assert(!isUnknownSizeArray() && "Array of unknown size");233    return Size;234  }235 236  PrimType getPrimType() const {237    assert(isPrimitiveArray() || isPrimitive());238    return *PrimT;239  }240 241  /// Returns the allocated size, including metadata.242  unsigned getAllocSize() const { return AllocSize; }243  /// returns the size of an element when the structure is viewed as an array.244  unsigned getElemSize() const { return ElemSize; }245  /// Returns the size of the metadata.246  unsigned getMetadataSize() const { return MDSize; }247 248  /// Returns the number of elements stored in the block.249  unsigned getNumElems() const {250    return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());251  }252 253  /// Checks if the descriptor is of an array of primitives.254  bool isPrimitiveArray() const { return IsArray && !ElemDesc; }255  /// Checks if the descriptor is of an array of composites.256  bool isCompositeArray() const { return IsArray && ElemDesc; }257  /// Checks if the descriptor is of an array of zero size.258  bool isZeroSizeArray() const { return Size == 0; }259  /// Checks if the descriptor is of an array of unknown size.260  bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }261 262  /// Checks if the descriptor is of a primitive.263  bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }264 265  /// Checks if the descriptor is of an array.266  bool isArray() const { return IsArray; }267  /// Checks if the descriptor is of a record.268  bool isRecord() const { return !IsArray && ElemRecord; }269  /// Checks if the descriptor is of a union.270  bool isUnion() const;271 272  /// Whether variables of this descriptor need their destructor called or not.273  bool hasTrivialDtor() const;274 275  void dump() const;276  void dump(llvm::raw_ostream &OS) const;277  void dumpFull(unsigned Offset = 0, unsigned Indent = 0) const;278};279 280/// Bitfield tracking the initialisation status of elements of primitive arrays.281struct InitMap final {282private:283  /// Type packing bits.284  using T = uint64_t;285  /// Bits stored in a single field.286  static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;287 288public:289  /// Initializes the map with no fields set.290  explicit InitMap(unsigned N);291 292private:293  friend class Pointer;294 295  /// Returns a pointer to storage.296  T *data() { return Data.get(); }297  const T *data() const { return Data.get(); }298 299  /// Initializes an element. Returns true when object if fully initialized.300  bool initializeElement(unsigned I);301 302  /// Checks if an element was initialized.303  bool isElementInitialized(unsigned I) const;304 305  static constexpr size_t numFields(unsigned N) {306    return (N + PER_FIELD - 1) / PER_FIELD;307  }308  /// Number of fields not initialized.309  unsigned UninitFields;310  std::unique_ptr<T[]> Data;311};312 313} // namespace interp314} // namespace clang315 316#endif317