|
| 1 | +//===--- MetadataSource.h - structure for the source of metadata *- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_IRGEN_METADATA_SOURCE_H |
| 14 | +#define SWIFT_IRGEN_METADATA_SOURCE_H |
| 15 | + |
| 16 | +#include "swift/AST/Types.h" |
| 17 | + |
| 18 | +namespace swift { |
| 19 | +namespace irgen { |
| 20 | + |
| 21 | +class MetadataSource { |
| 22 | +public: |
| 23 | + enum class Kind { |
| 24 | + /// Metadata is derived from a source class pointer. |
| 25 | + ClassPointer, |
| 26 | + |
| 27 | + /// Metadata is derived from a type metadata pointer. |
| 28 | + Metadata, |
| 29 | + |
| 30 | + /// Metadata is derived from the origin type parameter. |
| 31 | + GenericLValueMetadata, |
| 32 | + |
| 33 | + /// Metadata is obtained directly from the from a Self metadata |
| 34 | + /// parameter passed via the WitnessMethod convention. |
| 35 | + SelfMetadata, |
| 36 | + |
| 37 | + /// Metadata is derived from the Self witness table parameter |
| 38 | + /// passed via the WitnessMethod convention. |
| 39 | + SelfWitnessTable, |
| 40 | + |
| 41 | + /// Metadata is obtained directly from the FixedType indicated. Used with |
| 42 | + /// Objective-C generics, where the actual argument is erased at runtime |
| 43 | + /// and its existential bound is used instead. |
| 44 | + ErasedTypeMetadata, |
| 45 | + }; |
| 46 | + |
| 47 | + static bool requiresSourceIndex(Kind kind) { |
| 48 | + return (kind == Kind::ClassPointer || |
| 49 | + kind == Kind::Metadata || |
| 50 | + kind == Kind::GenericLValueMetadata); |
| 51 | + } |
| 52 | + |
| 53 | + static bool requiresFixedType(Kind kind) { |
| 54 | + return (kind == Kind::ErasedTypeMetadata); |
| 55 | + } |
| 56 | + |
| 57 | + enum : unsigned { InvalidSourceIndex = ~0U }; |
| 58 | + |
| 59 | +private: |
| 60 | + /// The kind of source this is. |
| 61 | + Kind TheKind; |
| 62 | + |
| 63 | + /// For ClassPointer, Metadata, and GenericLValueMetadata, the source index; |
| 64 | + /// for ErasedTypeMetadata, the type; for others, Index should be set to |
| 65 | + /// InvalidSourceIndex. |
| 66 | + union { |
| 67 | + unsigned Index; |
| 68 | + CanType FixedType; |
| 69 | + }; |
| 70 | + |
| 71 | +public: |
| 72 | + CanType Type; |
| 73 | + |
| 74 | + MetadataSource(Kind kind, CanType type) |
| 75 | + : TheKind(kind), Index(InvalidSourceIndex), Type(type) |
| 76 | + { |
| 77 | + assert(!requiresSourceIndex(kind) && !requiresFixedType(kind)); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + MetadataSource(Kind kind, CanType type, unsigned index) |
| 82 | + : TheKind(kind), Index(index), Type(type) { |
| 83 | + assert(requiresSourceIndex(kind)); |
| 84 | + assert(index != InvalidSourceIndex); |
| 85 | + } |
| 86 | + |
| 87 | + MetadataSource(Kind kind, CanType type, CanType fixedType) |
| 88 | + : TheKind(kind), FixedType(fixedType), Type(type) { |
| 89 | + assert(requiresFixedType(kind)); |
| 90 | + } |
| 91 | + |
| 92 | + Kind getKind() const { return TheKind; } |
| 93 | + |
| 94 | + unsigned getParamIndex() const { |
| 95 | + assert(requiresSourceIndex(getKind())); |
| 96 | + return Index; |
| 97 | + } |
| 98 | + |
| 99 | + CanType getFixedType() const { |
| 100 | + assert(requiresFixedType(getKind())); |
| 101 | + return FixedType; |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} // end namespace irgen |
| 106 | +} // end namespace swift |
| 107 | + |
| 108 | +#endif |
0 commit comments