Descriptor.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /// Describes a memory block created by an allocation site.
  42. struct Descriptor {
  43. private:
  44. /// Original declaration, used to emit the error message.
  45. const DeclTy Source;
  46. /// Size of an element, in host bytes.
  47. const InterpSize ElemSize;
  48. /// Size of the storage, in host bytes.
  49. const InterpSize Size;
  50. /// Size of the allocation (storage + metadata), in host bytes.
  51. const InterpSize AllocSize;
  52. /// Value to denote arrays of unknown size.
  53. static constexpr unsigned UnknownSizeMark = (unsigned)-1;
  54. public:
  55. /// Token to denote structures of unknown size.
  56. struct UnknownSize {};
  57. /// Pointer to the record, if block contains records.
  58. Record *const ElemRecord = nullptr;
  59. /// Descriptor of the array element.
  60. Descriptor *const ElemDesc = nullptr;
  61. /// Flag indicating if the block is mutable.
  62. const bool IsConst = false;
  63. /// Flag indicating if a field is mutable.
  64. const bool IsMutable = false;
  65. /// Flag indicating if the block is a temporary.
  66. const bool IsTemporary = false;
  67. /// Flag indicating if the block is an array.
  68. const bool IsArray = false;
  69. /// Storage management methods.
  70. const BlockCtorFn CtorFn = nullptr;
  71. const BlockDtorFn DtorFn = nullptr;
  72. const BlockMoveFn MoveFn = nullptr;
  73. /// Allocates a descriptor for a primitive.
  74. Descriptor(const DeclTy &D, PrimType Type, bool IsConst, bool IsTemporary,
  75. bool IsMutable);
  76. /// Allocates a descriptor for an array of primitives.
  77. Descriptor(const DeclTy &D, PrimType Type, size_t NumElems, bool IsConst,
  78. bool IsTemporary, bool IsMutable);
  79. /// Allocates a descriptor for an array of primitives of unknown size.
  80. Descriptor(const DeclTy &D, PrimType Type, bool IsTemporary, UnknownSize);
  81. /// Allocates a descriptor for an array of composites.
  82. Descriptor(const DeclTy &D, Descriptor *Elem, unsigned NumElems, bool IsConst,
  83. bool IsTemporary, bool IsMutable);
  84. /// Allocates a descriptor for an array of composites of unknown size.
  85. Descriptor(const DeclTy &D, Descriptor *Elem, bool IsTemporary, UnknownSize);
  86. /// Allocates a descriptor for a record.
  87. Descriptor(const DeclTy &D, Record *R, bool IsConst, bool IsTemporary,
  88. bool IsMutable);
  89. QualType getType() const;
  90. SourceLocation getLocation() const;
  91. const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
  92. const Expr *asExpr() const { return Source.dyn_cast<const Expr *>(); }
  93. const ValueDecl *asValueDecl() const {
  94. return dyn_cast_or_null<ValueDecl>(asDecl());
  95. }
  96. const FieldDecl *asFieldDecl() const {
  97. return dyn_cast_or_null<FieldDecl>(asDecl());
  98. }
  99. const RecordDecl *asRecordDecl() const {
  100. return dyn_cast_or_null<RecordDecl>(asDecl());
  101. }
  102. /// Returns the size of the object without metadata.
  103. unsigned getSize() const {
  104. assert(!isUnknownSizeArray() && "Array of unknown size");
  105. return Size;
  106. }
  107. /// Returns the allocated size, including metadata.
  108. unsigned getAllocSize() const { return AllocSize; }
  109. /// returns the size of an element when the structure is viewed as an array.
  110. unsigned getElemSize() const { return ElemSize; }
  111. /// Returns the number of elements stored in the block.
  112. unsigned getNumElems() const {
  113. return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
  114. }
  115. /// Checks if the descriptor is of an array of primitives.
  116. bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
  117. /// Checks if the descriptor is of an array of zero size.
  118. bool isZeroSizeArray() const { return Size == 0; }
  119. /// Checks if the descriptor is of an array of unknown size.
  120. bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
  121. /// Checks if the descriptor is of a primitive.
  122. bool isPrimitive() const { return !IsArray && !ElemRecord; }
  123. /// Checks if the descriptor is of an array.
  124. bool isArray() const { return IsArray; }
  125. };
  126. /// Inline descriptor embedded in structures and arrays.
  127. ///
  128. /// Such descriptors precede all composite array elements and structure fields.
  129. /// If the base of a pointer is not zero, the base points to the end of this
  130. /// structure. The offset field is used to traverse the pointer chain up
  131. /// to the root structure which allocated the object.
  132. struct InlineDescriptor {
  133. /// Offset inside the structure/array.
  134. unsigned Offset;
  135. /// Flag indicating if the storage is constant or not.
  136. /// Relevant for primitive fields.
  137. unsigned IsConst : 1;
  138. /// For primitive fields, it indicates if the field was initialized.
  139. /// Primitive fields in static storage are always initialized.
  140. /// Arrays are always initialized, even though their elements might not be.
  141. /// Base classes are initialized after the constructor is invoked.
  142. unsigned IsInitialized : 1;
  143. /// Flag indicating if the field is an embedded base class.
  144. unsigned IsBase : 1;
  145. /// Flag indicating if the field is the active member of a union.
  146. unsigned IsActive : 1;
  147. /// Flag indicating if the field is mutable (if in a record).
  148. unsigned IsMutable : 1;
  149. Descriptor *Desc;
  150. };
  151. /// Bitfield tracking the initialisation status of elements of primitive arrays.
  152. /// A pointer to this is embedded at the end of all primitive arrays.
  153. /// If the map was not yet created and nothing was initialized, the pointer to
  154. /// this structure is 0. If the object was fully initialized, the pointer is -1.
  155. struct InitMap {
  156. private:
  157. /// Type packing bits.
  158. using T = uint64_t;
  159. /// Bits stored in a single field.
  160. static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
  161. /// Initializes the map with no fields set.
  162. InitMap(unsigned N);
  163. /// Returns a pointer to storage.
  164. T *data();
  165. public:
  166. /// Initializes an element. Returns true when object if fully initialized.
  167. bool initialize(unsigned I);
  168. /// Checks if an element was initialized.
  169. bool isInitialized(unsigned I);
  170. /// Allocates a map holding N elements.
  171. static InitMap *allocate(unsigned N);
  172. private:
  173. /// Number of fields initialized.
  174. unsigned UninitFields;
  175. };
  176. } // namespace interp
  177. } // namespace clang
  178. #endif