-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Support importing static factories as initializers #79288
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
struct __attribute__((swift_attr("import_reference"))) | ||
__attribute__((swift_attr("retain:retain"))) | ||
__attribute__((swift_attr("release:release"))) ImportWithCtor { | ||
int value = 0; | ||
int param1 = 0; | ||
int param2 = 0; | ||
|
||
__attribute__((swift_name("init()"))) | ||
__attribute__((swift_attr("returns_retained"))) | ||
static ImportWithCtor * _Nonnull create() { | ||
return new ImportWithCtor{1}; | ||
} | ||
|
||
__attribute__((swift_name("init(_:)"))) | ||
Xazax-hun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__attribute__((swift_attr("returns_retained"))) | ||
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. Would using 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. Hmm. Do you mean we should assume 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. Not necessarily, I just wanted to check if we should test the other possible annotation, 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. Ah, I see. It feels weird for a ctor to do 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. Should we allow importing static factories as both 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. I will add a test for the |
||
static ImportWithCtor * _Nonnull create(int x) { | ||
return new ImportWithCtor{1, x}; | ||
} | ||
|
||
__attribute__((swift_name("init(_:_:)"))) | ||
__attribute__((swift_attr("returns_retained"))) | ||
static ImportWithCtor * _Nonnull create(int x, int y) { | ||
return new ImportWithCtor{1, x, y}; | ||
} | ||
|
||
__attribute__((swift_name("init(_:_:_:)"))) | ||
__attribute__((swift_attr("returns_unretained"))) | ||
static ImportWithCtor * _Nonnull create(int x, int y, int z) { | ||
return new ImportWithCtor{0, x, y}; | ||
} | ||
}; | ||
|
||
inline void retain(ImportWithCtor * _Nonnull x) { | ||
x->value++; | ||
} | ||
|
||
inline void release(ImportWithCtor * _Nonnull x) { | ||
if (!--x->value) | ||
delete x; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module Constructor { | ||
header "constructor.h" | ||
requires cplusplus | ||
} | ||
|
||
module POD { | ||
header "pod.h" | ||
requires cplusplus | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking) | ||
// REQUIRES: executable_test | ||
|
||
import Constructor | ||
import StdlibUnittest | ||
|
||
var ForeignRefCtorSuite = TestSuite("ForeignRef Ctor") | ||
|
||
ForeignRefCtorSuite.test("ImportStaticFactoryAsInitializer") { | ||
let x = ImportWithCtor() | ||
expectEqual(x.param1, 0) | ||
expectEqual(x.param2, 0) | ||
let y = x | ||
let z = ImportWithCtor(1) | ||
expectEqual(z.param1, 1) | ||
expectEqual(z.param2, 0) | ||
let z2 = ImportWithCtor(2, 3) | ||
expectEqual(z2.param1, 2) | ||
expectEqual(z2.param2, 3) | ||
let z3 = ImportWithCtor(2, 3, 4) | ||
expectEqual(z3.param1, 2) | ||
expectEqual(z3.param2, 3) | ||
} | ||
|
||
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.
@Xazax-hun why did you choose this location in the
importFunctionDecl
function to implement this logic?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 is the
importFunctionDecl
function that has most of the function importing logic. We usually calculate the Swift Name for an imported declaration relatively early on. So this logic belongs here, and relatively early in this function (but after we have the SwiftName). There is another call site ofimportGlobalAsInitializer
in the same function to handle the non-C++ method case, which is a good indication that this is the right place.The easiest way to find where to put some code is to step through the import process of a simple example in the debugger and see what is the earliest point where you have all the information you need and check where would the code logically belong after that point.
Uh oh!
There was an error while loading. Please reload this page.
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.
Another consideration is that we want to do this before we did any actual importingn (creating Swift AST) because we want to avoid importing the same function twice in two different ways.
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.
Makes sense, thanks!