| 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 | |
| 13 | #ifndef LLVM_CLANG_AST_INTERP_DESCRIPTOR_H |
| 14 | #define LLVM_CLANG_AST_INTERP_DESCRIPTOR_H |
| 15 | |
| 16 | #include "InitMap.h" |
| 17 | #include "PrimType.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace interp { |
| 23 | class Block; |
| 24 | class Record; |
| 25 | class SourceInfo; |
| 26 | struct Descriptor; |
| 27 | enum PrimType : uint8_t; |
| 28 | |
| 29 | using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>; |
| 30 | |
| 31 | /// Invoked whenever a block is created. The constructor method fills in the |
| 32 | /// inline descriptors of all fields and array elements. It also initializes |
| 33 | /// all the fields which contain non-trivial types. |
| 34 | using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst, |
| 35 | bool IsMutable, bool IsVolatile, bool IsActive, |
| 36 | bool InUnion, const Descriptor *FieldDesc); |
| 37 | |
| 38 | /// Invoked when a block is destroyed. Invokes the destructors of all |
| 39 | /// non-trivial nested fields of arrays and records. |
| 40 | using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr, |
| 41 | const Descriptor *FieldDesc); |
| 42 | |
| 43 | enum class GlobalInitState { |
| 44 | Initialized, |
| 45 | NoInitializer, |
| 46 | InitializerFailed, |
| 47 | }; |
| 48 | |
| 49 | /// Descriptor used for global variables. |
| 50 | struct alignas(void *) GlobalInlineDescriptor { |
| 51 | GlobalInitState InitState = GlobalInitState::InitializerFailed; |
| 52 | }; |
| 53 | static_assert(sizeof(GlobalInlineDescriptor) == sizeof(void *), "" ); |
| 54 | |
| 55 | enum class Lifetime : uint8_t { |
| 56 | Started, |
| 57 | Ended, |
| 58 | }; |
| 59 | |
| 60 | /// Inline descriptor embedded in structures and arrays. |
| 61 | /// |
| 62 | /// Such descriptors precede all composite array elements and structure fields. |
| 63 | /// If the base of a pointer is not zero, the base points to the end of this |
| 64 | /// structure. The offset field is used to traverse the pointer chain up |
| 65 | /// to the root structure which allocated the object. |
| 66 | struct InlineDescriptor { |
| 67 | /// Offset inside the structure/array. |
| 68 | unsigned Offset; |
| 69 | |
| 70 | /// Flag indicating if the storage is constant or not. |
| 71 | /// Relevant for primitive fields. |
| 72 | LLVM_PREFERRED_TYPE(bool) |
| 73 | unsigned IsConst : 1; |
| 74 | /// For primitive fields, it indicates if the field was initialized. |
| 75 | /// Primitive fields in static storage are always initialized. |
| 76 | /// Arrays are always initialized, even though their elements might not be. |
| 77 | /// Base classes are initialized after the constructor is invoked. |
| 78 | LLVM_PREFERRED_TYPE(bool) |
| 79 | unsigned IsInitialized : 1; |
| 80 | /// Flag indicating if the field is an embedded base class. |
| 81 | LLVM_PREFERRED_TYPE(bool) |
| 82 | unsigned IsBase : 1; |
| 83 | /// Flag inidcating if the field is a virtual base class. |
| 84 | LLVM_PREFERRED_TYPE(bool) |
| 85 | unsigned IsVirtualBase : 1; |
| 86 | /// Flag indicating if the field is the active member of a union. |
| 87 | LLVM_PREFERRED_TYPE(bool) |
| 88 | unsigned IsActive : 1; |
| 89 | /// Flag indicating if this field is in a union (even if nested). |
| 90 | LLVM_PREFERRED_TYPE(bool) |
| 91 | unsigned InUnion : 1; |
| 92 | /// Flag indicating if the field is mutable (if in a record). |
| 93 | LLVM_PREFERRED_TYPE(bool) |
| 94 | unsigned IsFieldMutable : 1; |
| 95 | /// Flag indicating if this field is a const field nested in |
| 96 | /// a mutable parent field. |
| 97 | LLVM_PREFERRED_TYPE(bool) |
| 98 | unsigned IsConstInMutable : 1; |
| 99 | /// Flag indicating if the field is an element of a composite array. |
| 100 | LLVM_PREFERRED_TYPE(bool) |
| 101 | unsigned IsArrayElement : 1; |
| 102 | LLVM_PREFERRED_TYPE(bool) |
| 103 | unsigned IsVolatile : 1; |
| 104 | |
| 105 | Lifetime LifeState; |
| 106 | |
| 107 | const Descriptor *Desc; |
| 108 | |
| 109 | InlineDescriptor(const Descriptor *D) |
| 110 | : Offset(sizeof(InlineDescriptor)), IsConst(false), IsInitialized(false), |
| 111 | IsBase(false), IsActive(false), IsFieldMutable(false), |
| 112 | IsArrayElement(false), IsVolatile(false), LifeState(Lifetime::Started), |
| 113 | Desc(D) {} |
| 114 | |
| 115 | void dump() const { dump(OS&: llvm::errs()); } |
| 116 | void dump(llvm::raw_ostream &OS) const; |
| 117 | }; |
| 118 | static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "" ); |
| 119 | |
| 120 | /// Describes a memory block created by an allocation site. |
| 121 | struct Descriptor final { |
| 122 | private: |
| 123 | /// Original declaration, used to emit the error message. |
| 124 | const DeclTy Source; |
| 125 | const Type *SourceType = nullptr; |
| 126 | /// Size of an element, in host bytes. |
| 127 | const unsigned ElemSize; |
| 128 | /// Size of the storage, in host bytes. |
| 129 | const unsigned Size; |
| 130 | /// Size of the metadata. |
| 131 | const unsigned MDSize; |
| 132 | /// Size of the allocation (storage + metadata), in host bytes. |
| 133 | const unsigned AllocSize; |
| 134 | |
| 135 | /// Value to denote arrays of unknown size. |
| 136 | static constexpr unsigned UnknownSizeMark = (unsigned)-1; |
| 137 | |
| 138 | public: |
| 139 | /// Token to denote structures of unknown size. |
| 140 | struct UnknownSize {}; |
| 141 | |
| 142 | using MetadataSize = std::optional<unsigned>; |
| 143 | static constexpr MetadataSize InlineDescMD = sizeof(InlineDescriptor); |
| 144 | static constexpr MetadataSize GlobalMD = sizeof(GlobalInlineDescriptor); |
| 145 | |
| 146 | /// Maximum number of bytes to be used for array elements. |
| 147 | static constexpr unsigned MaxArrayElemBytes = |
| 148 | std::numeric_limits<decltype(AllocSize)>::max() - sizeof(InitMapPtr) - |
| 149 | align(Size: std::max(a: *InlineDescMD, b: *GlobalMD)); |
| 150 | |
| 151 | /// Pointer to the record, if block contains records. |
| 152 | const Record *const ElemRecord = nullptr; |
| 153 | /// Descriptor of the array element. |
| 154 | const Descriptor *const ElemDesc = nullptr; |
| 155 | /// The primitive type this descriptor was created for, |
| 156 | /// or the primitive element type in case this is |
| 157 | /// a primitive array. |
| 158 | const OptPrimType PrimT = std::nullopt; |
| 159 | /// Flag indicating if the block is mutable. |
| 160 | const bool IsConst = false; |
| 161 | /// Flag indicating if a field is mutable. |
| 162 | const bool IsMutable = false; |
| 163 | /// Flag indicating if the block is a temporary. |
| 164 | const bool IsTemporary = false; |
| 165 | const bool IsVolatile = false; |
| 166 | /// Flag indicating if the block is an array. |
| 167 | const bool IsArray = false; |
| 168 | bool IsConstexprUnknown = false; |
| 169 | |
| 170 | /// Storage management methods. |
| 171 | const BlockCtorFn CtorFn = nullptr; |
| 172 | const BlockDtorFn DtorFn = nullptr; |
| 173 | |
| 174 | /// Allocates a descriptor for a primitive. |
| 175 | Descriptor(const DeclTy &D, const Type *SourceTy, PrimType Type, |
| 176 | MetadataSize MD, bool IsConst, bool IsTemporary, bool IsMutable, |
| 177 | bool IsVolatile); |
| 178 | |
| 179 | /// Allocates a descriptor for an array of primitives. |
| 180 | Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD, size_t NumElems, |
| 181 | bool IsConst, bool IsTemporary, bool IsMutable); |
| 182 | |
| 183 | /// Allocates a descriptor for an array of primitives of unknown size. |
| 184 | Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst, |
| 185 | bool IsTemporary, UnknownSize); |
| 186 | |
| 187 | /// Allocates a descriptor for an array of composites. |
| 188 | Descriptor(const DeclTy &D, const Type *SourceTy, const Descriptor *Elem, |
| 189 | MetadataSize MD, unsigned NumElems, bool IsConst, bool IsTemporary, |
| 190 | bool IsMutable); |
| 191 | |
| 192 | /// Allocates a descriptor for an array of composites of unknown size. |
| 193 | Descriptor(const DeclTy &D, const Descriptor *Elem, MetadataSize MD, |
| 194 | bool IsTemporary, UnknownSize); |
| 195 | |
| 196 | /// Allocates a descriptor for a record. |
| 197 | Descriptor(const DeclTy &D, const Record *R, MetadataSize MD, bool IsConst, |
| 198 | bool IsTemporary, bool IsMutable, bool IsVolatile); |
| 199 | |
| 200 | /// Allocates a dummy descriptor. |
| 201 | Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt); |
| 202 | |
| 203 | QualType getType() const; |
| 204 | QualType getElemQualType() const; |
| 205 | QualType getDataType(const ASTContext &Ctx) const; |
| 206 | QualType getDataElemType() const; |
| 207 | SourceLocation getLocation() const; |
| 208 | SourceInfo getLoc() const; |
| 209 | |
| 210 | const Decl *asDecl() const { return dyn_cast<const Decl *>(Val: Source); } |
| 211 | const Expr *asExpr() const { return dyn_cast<const Expr *>(Val: Source); } |
| 212 | const DeclTy &getSource() const { return Source; } |
| 213 | |
| 214 | const ValueDecl *asValueDecl() const { |
| 215 | return dyn_cast_if_present<ValueDecl>(Val: asDecl()); |
| 216 | } |
| 217 | |
| 218 | const VarDecl *asVarDecl() const { |
| 219 | return dyn_cast_if_present<VarDecl>(Val: asDecl()); |
| 220 | } |
| 221 | |
| 222 | const FieldDecl *asFieldDecl() const { |
| 223 | return dyn_cast_if_present<FieldDecl>(Val: asDecl()); |
| 224 | } |
| 225 | |
| 226 | const RecordDecl *asRecordDecl() const { |
| 227 | return dyn_cast_if_present<RecordDecl>(Val: 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 | } // namespace interp |
| 280 | } // namespace clang |
| 281 | |
| 282 | #endif |
| 283 | |