Descriptor.h 8.2 KB

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