Skip to content

Commit c143c55

Browse files
committed
[AST] OpaqueDecl: Simplify storage of conditionally available alternatives
Instead of using a special type with a set of trailing objects, let's just re-allocate array of substitutions into a permanent storage directly.
1 parent 3b523d5 commit c143c55

File tree

2 files changed

+17
-43
lines changed

2 files changed

+17
-43
lines changed

include/swift/AST/Decl.h

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,34 +2793,6 @@ class OpaqueTypeDecl final :
27932793
class ConditionallyAvailableSubstitutions;
27942794

27952795
private:
2796-
/// A container to keep a set of conditional available underlying type
2797-
/// substitutions with their availability conditions.
2798-
class ConditionalAlternatives final
2799-
: private llvm::TrailingObjects<ConditionalAlternatives,
2800-
ConditionallyAvailableSubstitutions *> {
2801-
friend TrailingObjects;
2802-
2803-
unsigned NumAlternatives;
2804-
2805-
ConditionalAlternatives(
2806-
ArrayRef<ConditionallyAvailableSubstitutions *> underlyingTypes)
2807-
: NumAlternatives(underlyingTypes.size()) {
2808-
std::uninitialized_copy(
2809-
underlyingTypes.begin(), underlyingTypes.end(),
2810-
getTrailingObjects<ConditionallyAvailableSubstitutions *>());
2811-
}
2812-
2813-
public:
2814-
ArrayRef<ConditionallyAvailableSubstitutions *> getAlternatives() const {
2815-
return {getTrailingObjects<ConditionallyAvailableSubstitutions *>(),
2816-
NumAlternatives};
2817-
}
2818-
2819-
static ConditionalAlternatives *
2820-
get(ASTContext &ctx,
2821-
ArrayRef<ConditionallyAvailableSubstitutions *> underlyingTypes);
2822-
};
2823-
28242796
/// The original declaration that "names" the opaque type. Although a specific
28252797
/// opaque type cannot be explicitly named, oapque types can propagate
28262798
/// arbitrarily through expressions, so we need to know *which* opaque type is
@@ -2845,7 +2817,11 @@ class OpaqueTypeDecl final :
28452817
/// A set of substitutions which are used based on the availability
28462818
/// checks performed at runtime. This set of only populated if there
28472819
/// is no single unique underlying type for this opaque type declaration.
2848-
ConditionalAlternatives *ConditionallyAvailableTypes = nullptr;
2820+
///
2821+
/// It always contains one or more conditionally available substitutions
2822+
/// followed by a universally available type used as a fallback.
2823+
Optional<MutableArrayRef<ConditionallyAvailableSubstitutions *>>
2824+
ConditionallyAvailableTypes = None;
28492825

28502826
mutable Identifier OpaqueReturnTypeIdentifier;
28512827

@@ -2932,14 +2908,15 @@ class OpaqueTypeDecl final :
29322908
UniqueUnderlyingType = subs;
29332909
}
29342910

2935-
void setConditionallyAvailableSubstitutions(
2936-
ArrayRef<ConditionallyAvailableSubstitutions *> substitutions) {
2937-
assert(!ConditionallyAvailableTypes &&
2938-
"resetting conditional substitutions?!");
2939-
ConditionallyAvailableTypes =
2940-
ConditionalAlternatives::get(getASTContext(), substitutions);
2911+
ArrayRef<ConditionallyAvailableSubstitutions *>
2912+
getConditionallyAvailableSubstitutions() const {
2913+
assert(ConditionallyAvailableTypes);
2914+
return ConditionallyAvailableTypes.getValue();
29412915
}
29422916

2917+
void setConditionallyAvailableSubstitutions(
2918+
ArrayRef<ConditionallyAvailableSubstitutions *> substitutions);
2919+
29432920
// Opaque type decls are currently always implicit
29442921
SourceRange getSourceRange() const { return SourceRange(); }
29452922

lib/AST/Decl.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8173,14 +8173,11 @@ Identifier OpaqueTypeDecl::getOpaqueReturnTypeIdentifier() const {
81738173
return OpaqueReturnTypeIdentifier;
81748174
}
81758175

8176-
OpaqueTypeDecl::ConditionalAlternatives *
8177-
OpaqueTypeDecl::ConditionalAlternatives::get(
8178-
ASTContext &ctx,
8179-
ArrayRef<ConditionallyAvailableSubstitutions *> underlyingTypes) {
8180-
auto size = totalSizeToAlloc<ConditionallyAvailableSubstitutions *>(
8181-
underlyingTypes.size());
8182-
auto mem = ctx.Allocate(size, alignof(ConditionalAlternatives));
8183-
return new (mem) ConditionalAlternatives(underlyingTypes);
8176+
void OpaqueTypeDecl::setConditionallyAvailableSubstitutions(
8177+
ArrayRef<ConditionallyAvailableSubstitutions *> substitutions) {
8178+
assert(!ConditionallyAvailableTypes &&
8179+
"resetting conditionally available substitutions?!");
8180+
ConditionallyAvailableTypes = getASTContext().AllocateCopy(substitutions);
81848181
}
81858182

81868183
OpaqueTypeDecl::ConditionallyAvailableSubstitutions *

0 commit comments

Comments
 (0)