Skip to content

Commit 04a151d

Browse files
committed
[Runtime] Form existential types and existential types based on mangled names.
Introduce basic support for existential types (just Any and AnyObject), so we can test those, shared/owned parameters, and existential metatypes.
1 parent 33f89a1 commit 04a151d

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,19 @@ class DecodedMetadataBuilder {
285285
}
286286

287287
BuiltType createExistentialMetatypeType(BuiltType instance) const {
288-
// FIXME: Implement.
289-
return BuiltType();
288+
return swift_getExistentialMetatypeMetadata(instance);
290289
}
291290

292291
BuiltType createProtocolCompositionType(ArrayRef<BuiltType> protocols,
293292
bool hasExplicitAnyObject) const {
294-
// FIXME: Implement.
295-
// NOTE: protocols.back() could be a class type. Clean up this API.
296-
return BuiltType();
293+
// FIXME: Handle protocols and superclasses.
294+
if (!protocols.empty()) return BuiltType();
295+
296+
auto classConstraint =
297+
hasExplicitAnyObject ? ProtocolClassConstraint::Class
298+
: ProtocolClassConstraint::Any;
299+
return swift_getExistentialTypeMetadata(classConstraint, nullptr, 0,
300+
nullptr);
297301
}
298302

299303
BuiltType createProtocolType(StringRef mangledName, StringRef moduleName,
@@ -305,7 +309,7 @@ class DecodedMetadataBuilder {
305309

306310
BuiltType createGenericTypeParameterType(unsigned depth,
307311
unsigned index) const {
308-
// FIXME: Implement.
312+
// FIXME: Implement substitution logic here.
309313
return BuiltType();
310314
}
311315

test/Runtime/demangleToMetadata.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func f2(x: (), y: ()) { }
3333

3434
func f1_variadic(x: ()...) { }
3535
func f1_inout(x: inout ()) { }
36+
func f1_shared(x: __shared AnyObject) { }
37+
func f1_owned(x: __owned AnyObject) { }
3638

3739
func f2_variadic_inout(x: ()..., y: inout ()) { }
3840

@@ -59,7 +61,8 @@ DemangleToMetadataTests.test("function types") {
5961
expectEqual(type(of: f1_inout), _typeByMangledName("yyytzc")!)
6062

6163
// Ownership parameters.
62-
// FIXME: Shared/owned
64+
expectEqual(type(of: f1_shared), _typeByMangledName("yyyXlhc")!)
65+
expectEqual(type(of: f1_owned), _typeByMangledName("yyyXlc")!)
6366

6467
// Mix-and-match.
6568
expectEqual(type(of: f2_variadic_inout), _typeByMangledName("yyytd_ytztc")!)
@@ -70,5 +73,23 @@ DemangleToMetadataTests.test("metatype types") {
7073
expectEqual(type(of: type(of: f0)), _typeByMangledName("yycm")!)
7174
}
7275

76+
func f2_any_anyobject(_: Any, _: AnyObject) { }
77+
78+
DemangleToMetadataTests.test("existential types") {
79+
// Any, AnyObject
80+
expectEqual(type(of: f2_any_anyobject), _typeByMangledName("yyyp_yXltc")!)
81+
82+
// FIXME: References to protocols.
83+
// FIXME: References to superclass.
84+
}
85+
86+
DemangleToMetadataTests.test("existential metatype types") {
87+
// Any
88+
expectEqual(type(of: Any.self), _typeByMangledName("ypm")!)
89+
90+
// AnyObject
91+
expectEqual(type(of: AnyObject.self), _typeByMangledName("yXlm")!)
92+
}
93+
7394
runAllTests()
7495

0 commit comments

Comments
 (0)