Skip to content

Commit f084efb

Browse files
authored
Merge pull request #17887 from zneak/cxx-destruct-signature-2
[IRGen] Fix ObjC method signatures for .cxx_construct and .cxx_destruct (not 4.2)
2 parents 8ab35e2 + c625b56 commit f084efb

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

lib/IRGen/GenObjC.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,9 @@ void irgen::emitObjCMethodDescriptorParts(IRGenModule &IGM,
11591159
/// The first element is the selector.
11601160
selectorRef = IGM.getAddrOfObjCMethodName(selector.str());
11611161

1162-
/// The second element is the type @encoding.
1162+
/// The second element is the method signature. A method signature is made of
1163+
/// the return type @encoding and every parameter type @encoding, glued with
1164+
/// numbers that used to represent stack offsets for each of these elements.
11631165
CanSILFunctionType methodType = getObjCMethodType(IGM, method);
11641166
atEncoding = getObjCEncodingForMethodType(IGM, methodType, extendedEncoding);
11651167

@@ -1349,9 +1351,13 @@ void irgen::emitObjCIVarInitDestroyDescriptor(IRGenModule &IGM,
13491351
Selector selector(declRef);
13501352
auto selectorRef = IGM.getAddrOfObjCMethodName(selector.str());
13511353

1352-
/// The second element is the type @encoding, which is always "@?"
1353-
/// for a function type.
1354-
auto atEncoding = IGM.getAddrOfGlobalString("@?");
1354+
/// The second element is the method signature. A method signature is made of
1355+
/// the return type @encoding and every parameter type @encoding, glued with
1356+
/// numbers that used to represent stack offsets for each of these elements.
1357+
auto ptrSize = IGM.getPointerSize().getValue();
1358+
llvm::SmallString<8> signature;
1359+
signature = "v" + llvm::itostr(ptrSize * 2) + "@0:" + llvm::itostr(ptrSize);
1360+
auto atEncoding = IGM.getAddrOfGlobalString(signature);
13551361

13561362
/// The third element is the method implementation pointer.
13571363
auto impl = llvm::ConstantExpr::getBitCast(objcImpl, IGM.Int8PtrTy);

test/IRGen/objc_bridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ import Foundation
9292
// CHECK: }
9393
// CHECK: { i8*, i8*, i8* } {
9494
// CHECK: i8* getelementptr inbounds ([14 x i8], [14 x i8]* @"\01L_selector_data(.cxx_destruct)", i64 0, i64 0),
95-
// CHECK: i8* getelementptr inbounds ([3 x i8], [3 x i8]* @{{.*}}, i64 0, i64 0),
95+
// CHECK: i8* getelementptr inbounds ([8 x i8], [8 x i8]* @{{.*}}, i64 0, i64 0),
9696
// CHECK: i8* bitcast (void ([[OPAQUE:.*]]*, i8*)* @"$S11objc_bridge3BasCfETo" to i8*)
9797
// CHECK: }
9898
// CHECK: ]

test/IRGen/objc_methods.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ class Foo: Fooable {
3434
@objc func fail() throws {}
3535
}
3636

37-
// CHECK: [[BAZ_SIGNATURE:@.*]] = private unnamed_addr constant [8 x i8] c"v16@0:8\00"
37+
class ObjcDestructible: NSObject {
38+
var object: NSObject
39+
40+
init(object: NSObject) {
41+
self.object = object
42+
}
43+
}
44+
45+
// CHECK: [[NO_ARGS_SIGNATURE:@.*]] = private unnamed_addr constant [8 x i8] c"v16@0:8\00"
3846
// CHECK: [[GARPLY_SIGNATURE:@.*]] = private unnamed_addr constant [11 x i8] c"v24@0:8@16\00"
3947
// CHECK: [[BLOCK_SIGNATURE_TRAD:@.*]] = private unnamed_addr constant [12 x i8] c"v24@0:8@?16\00"
4048
// CHECK-macosx: [[FAIL_SIGNATURE:@.*]] = private unnamed_addr constant [12 x i8] c"c24@0:8^@16\00"
@@ -45,7 +53,7 @@ class Foo: Fooable {
4553
// CHECK: i32 9,
4654
// CHECK: [9 x { i8*, i8*, i8* }] [{
4755
// CHECK: i8* getelementptr inbounds ([4 x i8], [4 x i8]* @"\01L_selector_data(baz)", i64 0, i64 0),
48-
// CHECK: i8* getelementptr inbounds ([8 x i8], [8 x i8]* [[BAZ_SIGNATURE]], i64 0, i64 0),
56+
// CHECK: i8* getelementptr inbounds ([8 x i8], [8 x i8]* [[NO_ARGS_SIGNATURE]], i64 0, i64 0),
4957
// CHECK: i8* bitcast (void (i8*, i8*)* @"$S12objc_methods3FooC3bazyyFTo" to i8*)
5058
// CHECK: }, {
5159
// CHECK: i8* getelementptr inbounds ([8 x i8], [8 x i8]* @"\01L_selector_data(garply:)", i64 0, i64 0),
@@ -66,6 +74,15 @@ class Foo: Fooable {
6674
// CHECK-ios: i8* bitcast (i1 (i8*, i8*, %4**)* @"$S12objc_methods3FooC4failyyKFTo" to i8*)
6775
// CHECK: }]
6876
// CHECK: }, section "__DATA, __objc_const", align 8
77+
// CHECK: @_INSTANCE_METHODS__TtC12objc_methods16ObjcDestructible = private constant { {{.*}}] } {
78+
// CHECK: i32 24,
79+
// CHECK: i32 2,
80+
// CHECK: [2 x { i8*, i8*, i8* }] [{
81+
// CHECK: i8* getelementptr inbounds ([14 x i8], [14 x i8]* @"\01L_selector_data(.cxx_destruct)", i64 0, i64 0),
82+
// CHECK: i8* getelementptr inbounds ([8 x i8], [8 x i8]* [[NO_ARGS_SIGNATURE]], i64 0, i64 0),
83+
// CHECK: i8* bitcast (void (%6*, i8*)* @"$S12objc_methods16ObjcDestructibleCfETo" to i8*) }]
84+
// CHECK: }]
85+
// CHECK: }, section "__DATA, __objc_const", align 8
6986
// CHECK: [[BLOCK_SIGNATURE_EXT_1:@.*]] = private unnamed_addr constant [18 x i8] c"v24@0:8@?<q@?q>16\00"
7087
// CHECK: [[BLOCK_SIGNATURE_EXT_2:@.*]] = private unnamed_addr constant [19 x i8] c"v24@0:8@?<q@?qq>16\00"
7188
// CHECK: [[STRING_SIGNATURE_EXT:@.*]] = private unnamed_addr constant [31 x i8] c"@\22NSString\2224@0:8@\22NSString\2216\00"

test/IRGen/objc_subclass.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
// CHECK-32: i8* bitcast (%0* (%0*, i8*, i32)* @"$S13objc_subclass10SwiftGizmoC7bellsOnACSgSi_tcfcTo" to i8*)
102102
// CHECK-32: }, { i8*, i8*, i8* } {
103103
// CHECK-32: i8* getelementptr inbounds ([15 x i8], [15 x i8]* @"\01L_selector_data(.cxx_construct)", i32 0, i32 0),
104-
// CHECK-32: i8* getelementptr inbounds ([3 x i8], [3 x i8]* {{@[0-9]+}}, i32 0, i32 0),
104+
// CHECK-32: i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* {{@[0-9]+}}, i32 0, i32 0),
105105
// CHECK-32: i8* bitcast (%0* (%0*, i8*)* @"$S13objc_subclass10SwiftGizmoCfeTo" to i8*)
106106
// CHECK-32: }]
107107
// CHECK-32: }, section "__DATA, __objc_const", align 4
@@ -151,7 +151,7 @@
151151
// CHECK-64: i8* bitcast ([[OPAQUE11:%.*]]* ([[OPAQUE12:%.*]]*, i8*, i64)* @"$S13objc_subclass10SwiftGizmoC7bellsOnACSgSi_tcfcTo" to i8*)
152152
// CHECK-64: }, {
153153
// CHECK-64: i8* getelementptr inbounds ([15 x i8], [15 x i8]* @"\01L_selector_data(.cxx_construct)", i64 0, i64 0),
154-
// CHECK-64: i8* getelementptr inbounds ([3 x i8], [3 x i8]* {{@[0-9]+}}, i64 0, i64 0),
154+
// CHECK-64: i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* {{@[0-9]+}}, i64 0, i64 0),
155155
// CHECK-64: i8* bitcast ([[OPAQUE5]]* ([[OPAQUE6]]*, i8*)* @"$S13objc_subclass10SwiftGizmoCfeTo" to i8*)
156156
// CHECK-64: }]
157157
// CHECK-64: }, section "__DATA, __objc_const", align 8
@@ -237,7 +237,7 @@
237237
// CHECK-32: i8* bitcast (%3* (%3*, i8*, i32)* @"$S13objc_subclass11SwiftGizmo2C7bellsOnACSgSi_tcfcTo" to i8*)
238238
// CHECK-32: }, {
239239
// CHECK-32: i8* getelementptr inbounds ([14 x i8], [14 x i8]* @"\01L_selector_data(.cxx_destruct)", i32 0, i32 0),
240-
// CHECK-32: i8* getelementptr inbounds ([3 x i8], [3 x i8]* {{@[0-9]+}}, i32 0, i32 0),
240+
// CHECK-32: i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* {{@[0-9]+}}, i32 0, i32 0),
241241
// CHECK-32: i8* bitcast (void (%3*, i8*)* @"$S13objc_subclass11SwiftGizmo2CfETo" to i8*)
242242
// CHECK-32: }
243243
// CHECK-32: ]
@@ -265,7 +265,7 @@
265265
// CHECK-64: i8* bitcast ([[OPAQUE21:%.*]]* ([[OPAQUE22:%.*]]*, i8*, i64)* @"$S13objc_subclass11SwiftGizmo2C7bellsOnACSgSi_tcfcTo" to i8*)
266266
// CHECK-64: }, {
267267
// CHECK-64: i8* getelementptr inbounds ([14 x i8], [14 x i8]* @"\01L_selector_data(.cxx_destruct)", i64 0, i64 0),
268-
// CHECK-64: i8* getelementptr inbounds ([3 x i8], [3 x i8]* {{@[0-9]+}}, i64 0, i64 0)
268+
// CHECK-64: i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* {{@[0-9]+}}, i64 0, i64 0)
269269
// CHECK-64: i8* bitcast (void ([[OPAQUE20:%.*]]*, i8*)* @"$S13objc_subclass11SwiftGizmo2CfETo" to i8*)
270270
// CHECK-64: }
271271
// CHECK-64: ] }

0 commit comments

Comments
 (0)