Skip to content

Commit ee08197

Browse files
committed
Factor appendAnyProtocolConformance out of the conformance mangling.
We want to be able to use mangled names to refer to protocol conformances in addition to type metadata. Provide an ASTMangler method that can render an arbitrary abstract or concrete `ProtocolConformanceRef`, factoring it out of the code used to emit conditional conformance arguments in `appendProtocolConformance`.
1 parent c90b8f7 commit ee08197

File tree

5 files changed

+48
-39
lines changed

5 files changed

+48
-39
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ class ASTMangler : public Mangler {
352352

353353
void appendProtocolConformance(const ProtocolConformance *conformance);
354354
void appendProtocolConformanceRef(const RootProtocolConformance *conformance);
355+
void appendAnyProtocolConformance(CanGenericSignature genericSig,
356+
CanType conformingType,
357+
ProtocolConformanceRef conformance);
355358
void appendConcreteProtocolConformance(
356359
const ProtocolConformance *conformance);
357360
void appendDependentProtocolConformance(const ConformanceAccessPath &path);

lib/AST/ASTMangler.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,35 @@ void ASTMangler::appendDependentProtocolConformance(
27572757
}
27582758
}
27592759

2760+
void ASTMangler::appendAnyProtocolConformance(
2761+
CanGenericSignature genericSig,
2762+
CanType conformingType,
2763+
ProtocolConformanceRef conformance) {
2764+
if (conformingType->isTypeParameter()) {
2765+
assert(genericSig && "Need a generic signature to resolve conformance");
2766+
auto path = genericSig->getConformanceAccessPath(conformingType,
2767+
conformance.getAbstract());
2768+
appendDependentProtocolConformance(path);
2769+
} else if (auto opaqueType = conformingType->getAs<OpaqueTypeArchetypeType>()) {
2770+
GenericSignature opaqueSignature = opaqueType->getBoundSignature();
2771+
GenericTypeParamType *opaqueTypeParam = opaqueSignature->getGenericParams().back();
2772+
ConformanceAccessPath conformanceAccessPath =
2773+
opaqueSignature->getConformanceAccessPath(opaqueTypeParam,
2774+
conformance.getAbstract());
2775+
2776+
// Append the conformance access path with the signature of the opaque type.
2777+
{
2778+
llvm::SaveAndRestore<CanGenericSignature> savedSignature(
2779+
CurGenericSignature, opaqueSignature.getCanonicalSignature());
2780+
appendDependentProtocolConformance(conformanceAccessPath);
2781+
}
2782+
appendType(conformingType);
2783+
appendOperator("HO");
2784+
} else {
2785+
appendConcreteProtocolConformance(conformance.getConcrete());
2786+
}
2787+
}
2788+
27602789
void ASTMangler::appendConcreteProtocolConformance(
27612790
const ProtocolConformance *conformance) {
27622791
auto module = conformance->getDeclContext()->getParentModule();
@@ -2797,30 +2826,15 @@ void ASTMangler::appendConcreteProtocolConformance(
27972826
CanType canType = type->getCanonicalType(CurGenericSignature);
27982827
auto proto =
27992828
conditionalReq.getSecondType()->castTo<ProtocolType>()->getDecl();
2800-
if (canType->isTypeParameter()) {
2801-
assert(CurGenericSignature &&
2802-
"Need a generic signature to resolve conformance");
2803-
auto conformanceAccessPath =
2804-
CurGenericSignature->getConformanceAccessPath(type, proto);
2805-
appendDependentProtocolConformance(conformanceAccessPath);
2806-
} else if (auto opaqueType = canType->getAs<OpaqueTypeArchetypeType>()) {
2807-
GenericSignature opaqueSignature = opaqueType->getBoundSignature();
2808-
GenericTypeParamType *opaqueTypeParam = opaqueSignature->getGenericParams().back();
2809-
ConformanceAccessPath conformanceAccessPath =
2810-
opaqueSignature->getConformanceAccessPath(opaqueTypeParam, proto);
2811-
2812-
// Append the conformance access path with the signature of the opaque type.
2813-
{
2814-
llvm::SaveAndRestore<CanGenericSignature> savedSignature(
2815-
CurGenericSignature, opaqueSignature.getCanonicalSignature());
2816-
appendDependentProtocolConformance(conformanceAccessPath);
2817-
}
2818-
appendType(canType);
2819-
appendOperator("HO");
2829+
2830+
ProtocolConformanceRef conformance;
2831+
2832+
if (canType->isTypeParameter() || canType->is<OpaqueTypeArchetypeType>()){
2833+
conformance = ProtocolConformanceRef(proto);
28202834
} else {
2821-
auto conditionalConf = module->lookupConformance(canType, proto);
2822-
appendConcreteProtocolConformance(conditionalConf.getConcrete());
2835+
conformance = module->lookupConformance(canType, proto);
28232836
}
2837+
appendAnyProtocolConformance(CurGenericSignature, canType, conformance);
28242838
appendListSeparator(firstRequirement);
28252839
break;
28262840
}

lib/IRGen/IRGenMangler.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,7 @@ std::string IRGenMangler::mangleSymbolNameForMangledConformanceAccessorString(
308308
if (genericSig)
309309
appendGenericSignature(genericSig);
310310

311-
if (type)
312-
appendType(type);
313-
314-
if (conformance.isConcrete())
315-
appendConcreteProtocolConformance(conformance.getConcrete());
316-
else if (conformance.isAbstract())
317-
appendProtocolName(conformance.getAbstract());
318-
else
319-
assert(conformance.isInvalid() && "Unknown protocol conformance");
311+
appendAnyProtocolConformance(genericSig, type, conformance);
320312
return finalize();
321313
}
322314

test/IRGen/opaque_result_type.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension String: P {
3434
// -- mangled underlying type
3535
// CHECK-SAME: @"symbolic Si"
3636
// -- conformance to O
37-
// CHECK-SAME: @"get_witness_table S2i18opaque_result_type1OHpyHC
37+
// CHECK-SAME: @"get_witness_table Si18opaque_result_type1OHpyHC
3838
// CHECK-SAME: }>
3939
func poo() -> some O {
4040
return 0
@@ -65,7 +65,7 @@ public class C: P, Q {
6565
// -- mangled underlying type
6666
// CHECK-SAME: @"symbolic Si"
6767
// -- conformance to O
68-
// CHECK-SAME: @"get_witness_table S2i18opaque_result_type1OHpyHC
68+
// CHECK-SAME: @"get_witness_table Si18opaque_result_type1OHpyHC
6969
// CHECK-SAME: }>
7070
func poo() -> some O {
7171
return 0
@@ -79,9 +79,9 @@ public class C: P, Q {
7979
// -- mangled underlying type
8080
// CHECK-SAME: @"symbolic Si"
8181
// -- conformance to O
82-
// CHECK-SAME: @"get_witness_table S2i18opaque_result_type1OHpyHC
82+
// CHECK-SAME: @"get_witness_table Si18opaque_result_type1OHpyHC
8383
// -- conformance to O2
84-
// CHECK-SAME: @"get_witness_table S2i18opaque_result_type2O2HpyHC
84+
// CHECK-SAME: @"get_witness_table Si18opaque_result_type2O2HpyHC
8585
// CHECK-SAME: }>
8686
func qoo() -> some O & O2 {
8787
return 0
@@ -96,7 +96,7 @@ public class C: P, Q {
9696
// -- mangled underlying type
9797
// CHECK-SAME: @"symbolic SS"
9898
// -- conformance to P
99-
// CHECK-SAME: @"get_witness_table S2S18opaque_result_type1PHpyHC
99+
// CHECK-SAME: @"get_witness_table SS18opaque_result_type1PHpyHC
100100
// CHECK-SAME: }>
101101
func foo(x: String) -> some P {
102102
return x
@@ -110,7 +110,7 @@ func foo(x: String) -> some P {
110110
// -- mangled underlying type
111111
// CHECK-SAME: @"symbolic _____ 18opaque_result_type1CC"
112112
// -- conformance to Q
113-
// CHECK-SAME: @"get_witness_table 18opaque_result_type1CCAcA1QHPyHC
113+
// CHECK-SAME: @"get_witness_table 18opaque_result_type1CCAA1QHPyHC
114114
// CHECK-SAME: }>
115115
func bar(y: C) -> some Q {
116116
return y

test/IRGen/opaque_result_type_metadata_peephole.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ func foo() -> some P {
1212
// The mangled underlying type in foo2 ought to look through foo()'s opaque type
1313
// CHECK-LABEL: @"$s36opaque_result_type_metadata_peephole4foo2QryFQOMQ" = {{.*}} constant
1414
// DEFAULT-SAME: @"symbolic Si"
15-
// DEFAULT-SAME: @"get_witness_table S2i36opaque_result_type_metadata_external1PHpyHC
15+
// DEFAULT-SAME: @"get_witness_table Si36opaque_result_type_metadata_external1PHpyHC
1616
// IMPLICIT-DYNAMIC-SAME: @"symbolic _____yQo_ 36opaque_result_type_metadata_peephole3fooQryFQO"
17-
// IMPLICIT-DYNAMIC-SAME: @"get_witness_table 36opaque_result_type_metadata_peephole3fooQryFQOyQo_0a1_b1_c1_D9_external1P
17+
// IMPLICIT-DYNAMIC-SAME: @"get_witness_table x36opaque_result_type_metadata_external1PHD1_0a1_b1_c1_D9_peephole3fooQryFQOyQo_HO
1818
func foo2() -> some P {
1919
return foo()
2020
}

0 commit comments

Comments
 (0)