-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[cxx-interop] Pass foreign reference types with correct level of indirection #83850
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,6 @@ clang::CanQualType IRGenModule::getClangType(CanType type) { | |
} | ||
|
||
clang::CanQualType IRGenModule::getClangType(SILType type) { | ||
if (type.isForeignReferenceType()) | ||
return getClangType(type.getASTType() | ||
->wrapInPointer(PTK_UnsafePointer) | ||
->getCanonicalType()); | ||
Comment on lines
-35
to
-38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now handled inside of |
||
return getClangType(type.getASTType()); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
struct __attribute__((swift_attr("import_reference"))) | ||
__attribute__((swift_attr("retain:immortal"))) | ||
__attribute__((swift_attr("release:immortal"))) IntBox { | ||
int value; | ||
IntBox(int value) : value(value) {} | ||
|
||
static IntBox *create(int value) { return new IntBox(value); } | ||
}; | ||
|
||
inline int extractValueFromPtr(IntBox *b) { return b->value; } | ||
inline int extractValueFromRef(IntBox &b) { return b.value; } | ||
inline int extractValueFromConstRef(const IntBox &b) { return b.value; } | ||
inline int extractValueFromRefToPtr(IntBox *&b) { return b->value; } | ||
inline int extractValueFromRefToConstPtr(IntBox const *&b) { return b->value; } | ||
inline int extractValueFromConstRefToPtr(IntBox *const &b) { return b->value; } | ||
inline int extractValueFromConstRefToConstPtr(IntBox const *const &b) { return b->value; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// RUN: %target-swift-emit-irgen %s -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xcc -fignore-exceptions -disable-availability-checking | %FileCheck %s | ||
|
||
import PassAsParameter | ||
|
||
public func refToPtr() { | ||
var a = IntBox.create(123) | ||
let aValue = extractValueFromRefToPtr(&a) | ||
print(aValue) | ||
} | ||
// CHECK: define{{.*}} void {{.*}}refToPtr{{.*}}() | ||
// CHECK: [[PTR_TO_PTR_TO_INT_BOX:%.*]] = alloca %TSo6IntBoxVSg | ||
// CHECK: {{.*}} = call {{.*}} @{{.*}}extractValueFromRefToPtr{{.*}}(ptr [[PTR_TO_PTR_TO_INT_BOX]]) | ||
|
||
public func constRefToPtr() { | ||
let a = IntBox.create(456) | ||
let aValue = extractValueFromConstRefToPtr(a) | ||
print(aValue) | ||
} | ||
// CHECK: define{{.*}} void {{.*}}constRefToPtr{{.*}}() | ||
// CHECK: [[PTR_TO_PTR_TO_INT_BOX2:%.*]] = alloca %TSo6IntBoxVSg | ||
// CHECK: {{.*}} = call {{.*}} @{{.*}}extractValueFromConstRefToPtr{{.*}}(ptr [[PTR_TO_PTR_TO_INT_BOX2]]) | ||
|
||
public func refToConstPtr() { | ||
var a = IntBox.create(321) | ||
let aValue = extractValueFromRefToConstPtr(&a) | ||
print(aValue) | ||
} | ||
// CHECK: define{{.*}} void {{.*}}refToConstPtr{{.*}}() | ||
// CHECK: [[PTR_TO_PTR_TO_INT_BOX3:%.*]] = alloca %TSo6IntBoxVSg | ||
// CHECK: {{.*}} = call {{.*}} @{{.*}}extractValueFromRefToConstPtr{{.*}}(ptr [[PTR_TO_PTR_TO_INT_BOX3]]) | ||
|
||
public func constRefToConstPtr() { | ||
let a = IntBox.create(789) | ||
let aValue = extractValueFromConstRefToConstPtr(a) | ||
print(aValue) | ||
} | ||
// CHECK: define{{.*}} void {{.*}}constRefToConstPtr{{.*}}() | ||
// CHECK: [[PTR_TO_PTR_TO_INT_BOX4:%.*]] = alloca %TSo6IntBoxVSg | ||
// CHECK: {{.*}} = call {{.*}} @{{.*}}extractValueFromConstRefToConstPtr{{.*}}(ptr [[PTR_TO_PTR_TO_INT_BOX4]]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: %target-swift-ide-test -print-module -module-to-print=PassAsParameter -I %S/Inputs -source-filename=x -cxx-interoperability-mode=upcoming-swift | %FileCheck %s | ||
|
||
// CHECK: func extractValueFromPtr(_ b: IntBox!) -> Int32 | ||
// CHECK: func extractValueFromRef(_ b: IntBox) -> Int32 | ||
// CHECK: func extractValueFromConstRef(_ b: IntBox) -> Int32 | ||
// CHECK: func extractValueFromRefToPtr(_ b: inout IntBox!) -> Int32 | ||
// CHECK: func extractValueFromRefToConstPtr(_ b: inout IntBox!) -> Int32 | ||
// CHECK: func extractValueFromConstRefToPtr(_ b: IntBox!) -> Int32 | ||
// CHECK: func extractValueFromConstRefToConstPtr(_ b: IntBox!) -> Int32 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -Xfrontend -disable-availability-checking) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @egorzhdan, all |
||
|
||
// Temporarily disable when running with an older runtime (rdar://128681137) | ||
// UNSUPPORTED: use_os_stdlib | ||
// UNSUPPORTED: back_deployment_runtime | ||
|
||
import StdlibUnittest | ||
import PassAsParameter | ||
|
||
var PassAsParameterTestSuite = TestSuite("Passing foreign reference type as parameter") | ||
|
||
PassAsParameterTestSuite.test("pass as pointer") { | ||
let a = IntBox.create(123) | ||
let aValue = extractValueFromPtr(a) | ||
expectEqual(aValue, 123) | ||
} | ||
|
||
PassAsParameterTestSuite.test("pass as reference") { | ||
let a = IntBox.create(321)! | ||
let aValue = extractValueFromRef(a) | ||
expectEqual(aValue, 321) | ||
} | ||
|
||
PassAsParameterTestSuite.test("pass as const reference") { | ||
let a = IntBox.create(321)! | ||
let aValue = extractValueFromConstRef(a) | ||
expectEqual(aValue, 321) | ||
} | ||
|
||
PassAsParameterTestSuite.test("pass as reference to pointer") { | ||
var a = IntBox.create(123) | ||
let aValue = extractValueFromRefToPtr(&a) | ||
expectEqual(aValue, 123) | ||
} | ||
|
||
PassAsParameterTestSuite.test("pass as const reference to pointer") { | ||
let a = IntBox.create(456) | ||
let aValue = extractValueFromConstRefToPtr(a) | ||
expectEqual(aValue, 456) | ||
} | ||
|
||
PassAsParameterTestSuite.test("pass as const reference to pointer") { | ||
var a = IntBox.create(654) | ||
let aValue = extractValueFromConstRefToPtr(a) | ||
expectEqual(aValue, 654) | ||
} | ||
|
||
PassAsParameterTestSuite.test("pass as const reference to const pointer") { | ||
var a = IntBox.create(789) | ||
let aValue = extractValueFromConstRefToConstPtr(a) | ||
expectEqual(aValue, 789) | ||
} | ||
|
||
runAllTests() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is no longer correct, since we now import
ImmortalRef *const &
asImmortalRef!
.