Skip to content

IRGen Clang type lowering for subclass existentials #8830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/IRGen/GenClangType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,27 +599,39 @@ clang::CanQualType GenClangType::visitSILBlockStorageType(CanSILBlockStorageType

clang::CanQualType GenClangType::visitProtocolCompositionType(
CanProtocolCompositionType type) {
auto &clangCtx = getClangASTContext();

// FIXME. Eventually, this will have its own helper routine.
SmallVector<const clang::ObjCProtocolDecl *, 4> Protocols;
auto layout = type.getExistentialLayout();
assert(layout.requiresClass && "Cannot represent opaque existential in Clang");
assert(!layout.superclass && "Subclass existentials not supported here yet");
assert(layout.isObjC() && "Cannot represent opaque existential in Clang");

auto superclassTy = clangCtx.ObjCBuiltinIdTy;
if (layout.superclass) {
superclassTy = clangCtx.getCanonicalType(
cast<clang::ObjCObjectPointerType>(
Converter.convert(IGM, CanType(layout.superclass)))
->getPointeeType());
}

for (Type t : layout.getProtocols()) {
auto opt = cast<clang::ObjCObjectPointerType>(
Converter.convert(IGM, CanType(t)));
for (auto p : opt->quals())
Protocols.push_back(p);
}

auto &clangCtx = getClangASTContext();
if (Protocols.empty())
return getClangIdType(clangCtx);
return superclassTy;

// id<protocol-list>
clang::ObjCProtocolDecl **ProtoQuals = new(clangCtx) clang::ObjCProtocolDecl*[Protocols.size()];
memcpy(ProtoQuals, Protocols.data(), sizeof(clang::ObjCProtocolDecl*)*Protocols.size());
auto clangType = clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
ProtoQuals,
Protocols.size());
clang::ObjCProtocolDecl **ProtoQuals =
new(clangCtx) clang::ObjCProtocolDecl*[Protocols.size()];
memcpy(ProtoQuals, Protocols.data(),
sizeof(clang::ObjCProtocolDecl*)*Protocols.size());
auto clangType = clangCtx.getObjCObjectType(superclassTy,
ProtoQuals,
Protocols.size());
auto ptrTy = clangCtx.getObjCObjectPointerType(clangType);
return clangCtx.getCanonicalType(ptrTy);
}
Expand Down
61 changes: 33 additions & 28 deletions test/IRGen/objc_type_encoding.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %build-irgen-test-overlays
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) %s -emit-ir -disable-objc-attr-requires-foundation-module | %FileCheck %s -check-prefix=CHECK-%target-os
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) %s -emit-ir -disable-objc-attr-requires-foundation-module -enable-experimental-subclass-existentials | %FileCheck %s -check-prefix=CHECK-%target-os

// REQUIRES: CPU=x86_64
// REQUIRES: objc_interop
Expand Down Expand Up @@ -41,9 +41,9 @@ import gizmo

@objc func testOptionalPrimitives()
-> OpaquePointer? { return nil }
// CHECK-macosx: private unnamed_addr constant [9 x i8] c"^v16@0:8\00"
// CHECK-ios: private unnamed_addr constant [9 x i8] c"^v16@0:8\00"
// CHECK-tvos: private unnamed_addr constant [9 x i8] c"^v16@0:8\00"
// CHECK-macosx: private unnamed_addr constant [9 x i8] c"^v16@0:8\00"
// CHECK-ios: private unnamed_addr constant [9 x i8] c"^v16@0:8\00"
// CHECK-tvos: private unnamed_addr constant [9 x i8] c"^v16@0:8\00"

@objc func testCSignedTypes(_ a: CSignedChar, b: CShort, c: CInt, d: CLong, e: CLongLong) {}
// CHECK-macosx: private unnamed_addr constant [23 x i8] c"v44@0:8c16s20i24q28q36\00"
Expand Down Expand Up @@ -71,68 +71,69 @@ import gizmo
// CHECK-tvos: private unnamed_addr constant [20 x i8] c"v36@0:8c16s20i24q28\00"

@objc class func getSelf() -> Methods.Type { return self }
// These strings are required for another purpose and so are tested above.
// These strings are required for another purpose and so are tested above.

@objc func getDynamicSelf() -> Self { return self }
// CHECK-macosx: private unnamed_addr constant [8 x i8] c"@16@0:8\00"
// CHECK-ios: private unnamed_addr constant [8 x i8] c"@16@0:8\00"
// CHECK-tvos: private unnamed_addr constant [8 x i8] c"@16@0:8\00"
// CHECK-macosx: private unnamed_addr constant [8 x i8] c"@16@0:8\00"
// CHECK-ios: private unnamed_addr constant [8 x i8] c"@16@0:8\00"
// CHECK-tvos: private unnamed_addr constant [8 x i8] c"@16@0:8\00"

func testId(_ s: AnyObject) -> AnyObject { return self }
@objc func testId(_ s: AnyObject) -> AnyObject { return self }
// CHECK-macosx: private unnamed_addr constant [11 x i8] c"@24@0:8@16\00"
// CHECK-ios: private unnamed_addr constant [11 x i8] c"@24@0:8@16\00"
// CHECK-tvos: private unnamed_addr constant [11 x i8] c"@24@0:8@16\00"

@objc func comp1(_ a: P1 & P2, b: P1 & P2 & P3) -> P1 & P2 { return a }
@objc func comp1(_ a: P1 & P2, b: P1 & P2 & P3) -> P1 & P2 { return a }
// CHECK-macosx: private unnamed_addr constant [14 x i8] c"@32@0:8@16@24\00"
// CHECK-ios: private unnamed_addr constant [14 x i8] c"@32@0:8@16@24\00"
// CHECK-tvos: private unnamed_addr constant [14 x i8] c"@32@0:8@16@24\00"

@objc func returnsBool(_ b : Bool) -> Bool { return b }
@objc func returnsBool(_ b : Bool) -> Bool { return b }
// CHECK-macosx: private unnamed_addr constant [11 x i8] c"c20@0:8c16\00"
// CHECK-ios: private unnamed_addr constant [11 x i8] c"B20@0:8B16\00"
// CHECK-tvos: private unnamed_addr constant [11 x i8] c"B20@0:8B16\00"

@objc func comp1(_ a: Methods, b: Methods, c: Methods) -> Methods { return a }
@objc func comp1(_ a: Methods, b: Methods, c: Methods) -> Methods { return a }
// CHECK-macosx: private unnamed_addr constant [17 x i8] c"@40@0:8@16@24@32\00"
// CHECK-ios: private unnamed_addr constant [17 x i8] c"@40@0:8@16@24@32\00"
// CHECK-tvos: private unnamed_addr constant [17 x i8] c"@40@0:8@16@24@32\00"

@objc func passSelector(_ aSelector : Selector) {}
@objc func passSelector(_ aSelector : Selector) {}
// CHECK-macosx: private unnamed_addr constant [11 x i8] c"v24@0:8:16\00"
// CHECK-ios: private unnamed_addr constant [11 x i8] c"v24@0:8:16\00"
// CHECK-tvos: private unnamed_addr constant [11 x i8] c"v24@0:8:16\00"

@objc func copyUnsafeMutablePointer(_ p: UnsafeMutablePointer<Int32>) -> UnsafeMutablePointer<Int32> { return p }
@objc func copyUnsafeMutablePointer(_ p: UnsafeMutablePointer<Int32>) -> UnsafeMutablePointer<Int32> { return p }
// CHECK-macosx: private unnamed_addr constant [13 x i8] c"^i24@0:8^i16\00"
// CHECK-ios: private unnamed_addr constant [13 x i8] c"^i24@0:8^i16\00"
// CHECK-tvos: private unnamed_addr constant [13 x i8] c"^i24@0:8^i16\00"

@objc func copyUnsafeMutablePointerInt(_ p: UnsafeMutablePointer<Int>) -> UnsafeMutablePointer<Int> { return p }
@objc func copyUnsafeMutablePointerInt(_ p: UnsafeMutablePointer<Int>) -> UnsafeMutablePointer<Int> { return p }
// CHECK-macosx: private unnamed_addr constant [13 x i8] c"^q24@0:8^q16\00"
// CHECK-ios: private unnamed_addr constant [13 x i8] c"^q24@0:8^q16\00"
// CHECK-tvos: private unnamed_addr constant [13 x i8] c"^q24@0:8^q16\00"

func testArchetype(_ work: P3) {
}
@objc func testArchetype(_ work: P3) {
}
// CHECK-macosx: private unnamed_addr constant [11 x i8] c"v24@0:8@16\00"
// CHECK-ios: private unnamed_addr constant [11 x i8] c"v24@0:8@16\00"
// CHECK-tvos: private unnamed_addr constant [11 x i8] c"v24@0:8@16\00"

@objc func foo(_ x: (Int) -> Int) -> Int {
return 1
}
@objc func foo(_ x: (Int) -> Int) -> Int {
return 1
}
// CHECK-macosx: private unnamed_addr constant [12 x i8] c"q24@0:8@?16\00"
// CHECK-ios: private unnamed_addr constant [12 x i8] c"q24@0:8@?16\00"
// CHECK-tvos: private unnamed_addr constant [12 x i8] c"q24@0:8@?16\00"

@objc func returnNSRadixedOptions() -> NSRadixedOptions {
@objc func returnNSRadixedOptions() -> NSRadixedOptions {
return .octal
}
// CHECK-macosx: [[ENUMENCODING:@.*]] = private unnamed_addr constant [8 x i8] c"i16@0:8\00"
// CHECK-ios: [[ENUMENCODING:@.*]] = private unnamed_addr constant [8 x i8] c"i16@0:8\00"
// CHECK-tvos: [[ENUMENCODING:@.*]] = private unnamed_addr constant [8 x i8] c"i16@0:8\00"
@objc func returnChoseNSRadixedOptions(_ choice: NSRadixedOptions) -> NSRadixedOptions {

@objc func returnChoseNSRadixedOptions(_ choice: NSRadixedOptions) -> NSRadixedOptions {
switch choice {
case .octal: return .hex
case .hex: return .octal
Expand All @@ -142,7 +143,7 @@ func testArchetype(_ work: P3) {
// CHECK-ios: private unnamed_addr constant [11 x i8] c"i20@0:8i16\00"
// CHECK-tvos: private unnamed_addr constant [11 x i8] c"i20@0:8i16\00"

@objc func getRawEnumInGizmo() -> RawEnumInGizmo {
@objc func getRawEnumInGizmo() -> RawEnumInGizmo {
return InGizmoTwo
}
// CHECK-macosx: { i8* getelementptr inbounds ([18 x i8], [18 x i8]* @"\01L_selector_data(getRawEnumInGizmo)", i64 0, i64 0),
Expand All @@ -160,19 +161,23 @@ func testArchetype(_ work: P3) {

@objc protocol MyProtocol {
func myMethod2(_ arg : NSRuncing & NSFunging)
func readWithAuthorization(_ authData : Gizmo, reply : (NSView, NSSpoon) -> Void)
func doSomething(_ context: MyCustomObject)
func readWithAuthorization(_ authData : Gizmo, reply : (NSView, NSSpoon) -> Void)
func doSomething(_ context: MyCustomObject)
func subclassComposition(_: MyCustomObject & NSRuncing & NSFunging)
}

// CHECK-macosx: [[ENC1:@.*]] = private unnamed_addr constant [35 x i8] c"v24@0:8@\22<NSFunging><NSRuncing>\2216\00"
// CHECK-macosx: [[ENC2:@.*]] = private unnamed_addr constant [46 x i8] c"v32@0:8@\22Gizmo\2216@?<v@?@\22NSView\22@\22NSSpoon\22>24\00"
// CHECK-macosx: [[ENC3:@.*]] = private unnamed_addr constant [53 x i8] c"v24@0:8@\22_TtC18objc_type_encoding14MyCustomObject\2216\00"
// CHECK-macosx: @_PROTOCOL_METHOD_TYPES__TtP18objc_type_encoding10MyProtocol_ = private constant [3 x i8*] [i8* getelementptr inbounds ([35 x i8], [35 x i8]* [[ENC1]], i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* [[ENC2]], i64 0, i64 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* [[ENC3]], i64 0, i64 0)]
// CHECK-macosx: [[ENC4:@.*]] = private unnamed_addr constant [75 x i8] c"v24@0:8@\22_TtC18objc_type_encoding14MyCustomObject<NSFunging><NSRuncing>\2216\00"
// CHECK-macosx: @_PROTOCOL_METHOD_TYPES__TtP18objc_type_encoding10MyProtocol_ = private constant [4 x i8*] [i8* getelementptr inbounds ([35 x i8], [35 x i8]* [[ENC1]], i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* [[ENC2]], i64 0, i64 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* [[ENC3]], i64 0, i64 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* [[ENC4]], i64 0, i64 0)]
// CHECK-ios: [[ENC1:@.*]] = private unnamed_addr constant [35 x i8] c"v24@0:8@\22<NSFunging><NSRuncing>\2216\00"
// CHECK-ios: [[ENC2:@.*]] = private unnamed_addr constant [46 x i8] c"v32@0:8@\22Gizmo\2216@?<v@?@\22NSView\22@\22NSSpoon\22>24\00"
// CHECK-ios: [[ENC3:@.*]] = private unnamed_addr constant [53 x i8] c"v24@0:8@\22_TtC18objc_type_encoding14MyCustomObject\2216\00"
// CHECK-ios: @_PROTOCOL_METHOD_TYPES__TtP18objc_type_encoding10MyProtocol_ = private constant [3 x i8*] [i8* getelementptr inbounds ([35 x i8], [35 x i8]* [[ENC1]], i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* [[ENC2]], i64 0, i64 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* [[ENC3]], i64 0, i64 0)]
// CHECK-ios: [[ENC4:@.*]] = private unnamed_addr constant [75 x i8] c"v24@0:8@\22_TtC18objc_type_encoding14MyCustomObject<NSFunging><NSRuncing>\2216\00"
// CHECK-ios: @_PROTOCOL_METHOD_TYPES__TtP18objc_type_encoding10MyProtocol_ = private constant [4 x i8*] [i8* getelementptr inbounds ([35 x i8], [35 x i8]* [[ENC1]], i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* [[ENC2]], i64 0, i64 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* [[ENC3]], i64 0, i64 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* [[ENC4]], i64 0, i64 0)]
// CHECK-tvos: [[ENC1:@.*]] = private unnamed_addr constant [35 x i8] c"v24@0:8@\22<NSFunging><NSRuncing>\2216\00"
// CHECK-tvos: [[ENC2:@.*]] = private unnamed_addr constant [46 x i8] c"v32@0:8@\22Gizmo\2216@?<v@?@\22NSView\22@\22NSSpoon\22>24\00"
// CHECK-tvos: [[ENC3:@.*]] = private unnamed_addr constant [53 x i8] c"v24@0:8@\22_TtC18objc_type_encoding14MyCustomObject\2216\00"
// CHECK-tvos: @_PROTOCOL_METHOD_TYPES__TtP18objc_type_encoding10MyProtocol_ = private constant [3 x i8*] [i8* getelementptr inbounds ([35 x i8], [35 x i8]* [[ENC1]], i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* [[ENC2]], i64 0, i64 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* [[ENC3]], i64 0, i64 0)]
// CHECK-tvos: [[ENC4:@.*]] = private unnamed_addr constant [75 x i8] c"v24@0:8@\22_TtC18objc_type_encoding14MyCustomObject<NSFunging><NSRuncing>\2216\00"
// CHECK-tvos: @_PROTOCOL_METHOD_TYPES__TtP18objc_type_encoding10MyProtocol_ = private constant [4 x i8*] [i8* getelementptr inbounds ([35 x i8], [35 x i8]* [[ENC1]], i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* [[ENC2]], i64 0, i64 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* [[ENC3]], i64 0, i64 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* [[ENC4]], i64 0, i64 0)]