File tree Expand file tree Collapse file tree 4 files changed +101
-0
lines changed
test/Interop/Cxx/foreign-reference Expand file tree Collapse file tree 4 files changed +101
-0
lines changed Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #define FRT \
4
+ __attribute__ ((swift_attr(" import_reference" ))) \
5
+ __attribute__((swift_attr(" retain:immortal" ))) \
6
+ __attribute__((swift_attr(" release:immortal" )))
7
+
8
+ int &getCopyCounter() {
9
+ static int copyCounter = 0 ;
10
+ return copyCounter;
11
+ }
12
+
13
+ class CopyTrackedBaseClass {
14
+ public:
15
+ CopyTrackedBaseClass (int x) : x(x) {}
16
+ CopyTrackedBaseClass (const CopyTrackedBaseClass &other) : x(other.x) {
17
+ ++getCopyCounter ();
18
+ }
19
+
20
+ int getX () const {
21
+ return x;
22
+ }
23
+ private:
24
+ int x;
25
+ } FRT;
26
+
27
+ class CopyTrackedDerivedClass : public CopyTrackedBaseClass {
28
+ public:
29
+ CopyTrackedDerivedClass (int x) : CopyTrackedBaseClass(x) {}
30
+
31
+ int getDerivedX () const {
32
+ return getX ();
33
+ }
34
+ } FRT;
35
+
36
+ CopyTrackedDerivedClass *makeCopyTrackedDerivedClass (int x) {
37
+ return new CopyTrackedDerivedClass (x);
38
+ }
39
+
40
+ class NonEmptyBase {
41
+ public:
42
+ int getY () const {
43
+ return y;
44
+ }
45
+ private:
46
+ int y = 11 ;
47
+ } FRT;
48
+
49
+ class CopyTrackedDerivedDerivedClass : public NonEmptyBase , public CopyTrackedDerivedClass {
50
+ public:
51
+ CopyTrackedDerivedDerivedClass (int x) : CopyTrackedDerivedClass(x) {}
52
+ } FRT;
53
+
54
+ CopyTrackedDerivedDerivedClass *makeCopyTrackedDerivedDerivedClass (int x) {
55
+ return new CopyTrackedDerivedDerivedClass (x);
56
+ }
Original file line number Diff line number Diff line change @@ -32,3 +32,8 @@ module MemberLayout {
32
32
header "member-layout.h"
33
33
requires cplusplus
34
34
}
35
+
36
+ module FunctionInheritance {
37
+ header "function-inheritance.h"
38
+ requires cplusplus
39
+ }
Original file line number Diff line number Diff line change
1
+ // RUN: %target-swift-emit-irgen -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
2
+
3
+ import FunctionInheritance
4
+
5
+ func testGetX( ) -> CInt {
6
+ let derivedDerived = makeCopyTrackedDerivedDerivedClass ( 42 ) !
7
+ return derivedDerived. getX ( )
8
+ }
9
+
10
+ let _ = testGetX ( )
11
+
12
+
13
+ // CHECK: define {{.*}}linkonce_odr{{.*}} i32 @{{.*}}__synthesizedBaseCall___synthesizedBaseCall_{{.*}}(ptr {{.*}} %[[THIS_PTR:.*]])
14
+ // CHECK: %[[ADD_PTR:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i64 4
15
+ // CHECK: call noundef i32 @{{.*}}(ptr {{.*}} %[[ADD_PTR]])
Original file line number Diff line number Diff line change
1
+ // RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
2
+ //
3
+ // REQUIRES: executable_test
4
+
5
+ import StdlibUnittest
6
+ import FunctionInheritance
7
+
8
+ var FunctionsTestSuite = TestSuite ( " Calling functions in base foreign reference classes " )
9
+
10
+ FunctionsTestSuite . test ( " base member FRT calls do not require copying " ) {
11
+ let derived = makeCopyTrackedDerivedClass ( 42 ) !
12
+ var copyCounter = getCopyCounter ( ) . pointee
13
+ expectEqual ( derived. getX ( ) , 42 )
14
+ expectEqual ( copyCounter, getCopyCounter ( ) . pointee)
15
+ expectEqual ( derived. getDerivedX ( ) , 42 )
16
+ expectEqual ( copyCounter, getCopyCounter ( ) . pointee)
17
+
18
+ let derivedDerived = makeCopyTrackedDerivedDerivedClass ( - 5 ) !
19
+ copyCounter = getCopyCounter ( ) . pointee
20
+ expectEqual ( derivedDerived. getX ( ) , - 5 )
21
+ expectEqual ( derivedDerived. getY ( ) , 11 )
22
+ expectEqual ( copyCounter, getCopyCounter ( ) . pointee)
23
+ }
24
+
25
+ runAllTests ( )
You can’t perform that action at this time.
0 commit comments