Skip to content

Commit 56ed710

Browse files
committed
IRGen: Ignore generic parameters of SIL @convention(c) types.
That's the lowering we get for C functions imported as members of protocols.
1 parent b5fe792 commit 56ed710

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,6 @@ void IRGenModule::emitSILWitnessTable(SILWitnessTable *wt) {
12541254
/// Generic functions and protocol witnesses carry polymorphic parameters.
12551255
bool irgen::hasPolymorphicParameters(CanSILFunctionType ty) {
12561256
switch (ty->getRepresentation()) {
1257-
case SILFunctionTypeRepresentation::CFunctionPointer:
12581257
case SILFunctionTypeRepresentation::Block:
12591258
// Should never be polymorphic.
12601259
assert(!ty->isPolymorphic() && "polymorphic C function?!");
@@ -1265,6 +1264,7 @@ bool irgen::hasPolymorphicParameters(CanSILFunctionType ty) {
12651264
case SILFunctionTypeRepresentation::Method:
12661265
return ty->isPolymorphic();
12671266

1267+
case SILFunctionTypeRepresentation::CFunctionPointer:
12681268
case SILFunctionTypeRepresentation::ObjCMethod:
12691269
// May be polymorphic at the SIL level, but no type metadata is actually
12701270
// passed.

test/IDE/Inputs/custom-modules/ImportAsMemberProto.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ typedef NSObject<IAMProto> * IAMProto_t;
1717
void mutateSomeState(IAMProto_t)
1818
__attribute__((swift_name("IAMProto.mutateSomeState(self:)")));
1919

20-
void mutateSomeStateWithOtherProto(IAMProto_t, IAMProto_t other)
21-
__attribute__((swift_name("IAMProto.mutateSomeState(self:otherProto:)")));
20+
void mutateSomeStateWithParameter(IAMProto_t, NSInteger)
21+
__attribute__((swift_name("IAMProto.mutateSomeState(self:withParameter:)")));
22+
23+
void mutateSomeStateWithFirstParameter(NSInteger, IAMProto_t)
24+
__attribute__((swift_name("IAMProto.mutateSomeState(withFirstParameter:self:)")));
2225

2326
int getSomeValue(IAMProto_t)
2427
__attribute__((swift_name("getter:IAMProto.someValue(self:)")));

test/IDE/import_as_member.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
// PRINT-PROTO-NEXT: typealias IAMProto_t = IAMProto
5959
// PRINT-PROTO-NEXT: extension IAMProto {
6060
// PRINT-PROTO-NEXT: func mutateSomeState()
61-
// PRINT-PROTO-NEXT: func mutateSomeState(otherProto other: IAMProto_t!)
61+
// PRINT-PROTO-NEXT: func mutateSomeState(withParameter _: Int)
62+
// PRINT-PROTO-NEXT: func mutateSomeState(withFirstParameter _: Int)
6263
// PRINT-PROTO-NEXT: var someValue: Int32
6364
// PRINT-PROTO-NEXT: }
6465

test/IRGen/cf_members.sil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend -emit-ir -verify -I %S/../IDE/Inputs/custom-modules %s
2+
// REQUIRES: objc_interop
23

34
sil_stage canonical
45

@@ -9,6 +10,7 @@ import ImportAsMember
910
sil @IAMStruct1CreateSimple : $@convention(c) () -> Struct1
1011
sil @IAMStruct1Rotate : $@convention(c) (@in Struct1, Double) -> Struct1
1112
sil @IAMStruct1SetAltitude : $@convention(c) (@inout Struct1, Double) -> ()
13+
sil @mutateSomeState : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0) -> ()
1214

1315
sil @invoke_methods : $@convention(thin) (Double) -> () {
1416
entry(%z : $Double):
@@ -23,3 +25,14 @@ entry(%z : $Double):
2325
dealloc_stack %c : $*Struct1
2426
return undef : $()
2527
}
28+
29+
// CHECK-LABEL: define void @invoke_protocol_methods(%objc_object*)
30+
sil @invoke_protocol_methods : $@convention(thin) (IAMProto) -> () {
31+
entry(%z : $IAMProto):
32+
%a = open_existential_ref %z : $IAMProto to $@opened("01234567-89AB-CDEF-0123-000000000000") IAMProto
33+
%b = function_ref @mutateSomeState : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0) -> ()
34+
// CHECK: [[CAST:%.*]] = bitcast %objc_object* %0 to i8*
35+
// CHECK: call void @mutateSomeState(i8* [[CAST]])
36+
apply %b<@opened("01234567-89AB-CDEF-0123-000000000000") IAMProto>(%a) : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0) -> ()
37+
return undef : $()
38+
}

test/SILGen/cf_members.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,17 @@ public func bar(x: Double) {
237237
c()
238238
}
239239

240+
// CHECK-LABEL: sil @_TF10cf_members16importAsProtocolFPSo8IAMProto_T_
241+
public func importAsProtocol(x: IAMProto_t) {
242+
// CHECK: function_ref @mutateSomeState : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0) -> ()
243+
x.mutateSomeState()
244+
// CHECK: function_ref @mutateSomeStateWithParameter : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0, Int) -> ()
245+
x.mutateSomeState(withParameter: 0)
246+
// CHECK: function_ref @mutateSomeStateWithFirstParameter : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (Int, τ_0_0) -> ()
247+
x.mutateSomeState(withFirstParameter: 0)
248+
249+
// CHECK: function_ref @getSomeValue : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0) -> Int32
250+
let y = x.someValue
251+
// CHECK: function_ref @setSomeValue : $@convention(c) <τ_0_0 where τ_0_0 : IAMProto> (τ_0_0, Int32) -> Int32
252+
x.someValue = y
253+
}

0 commit comments

Comments
 (0)