Skip to content

[cxx-interop] Import parameterized public ctors of C++ foreign ref types as Swift Initializer #80449

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

Merged
merged 8 commits into from
Apr 8, 2025

Conversation

fahadnayyar
Copy link
Contributor

Extends PR #79986 by adding support for calling parameterized C++ initializers from Swift. This patch synthesizes static factory methods corresponding to C++ parameterized constructors, allowing Swift to call them as Swift initializers (e.g., init(_:), init(_:_:), etc.).

rdar://148285251

@fahadnayyar fahadnayyar force-pushed the cxx-frt-constructors-params branch from fa97b38 to 96518c0 Compare April 1, 2025 21:24
@fahadnayyar
Copy link
Contributor Author

Note: Default arguments are still not supported for both default and parameterized ctors

@fahadnayyar fahadnayyar self-assigned this Apr 1, 2025
@fahadnayyar fahadnayyar added the c++ interop Feature: Interoperability with C++ label Apr 1, 2025
@fahadnayyar fahadnayyar marked this pull request as ready for review April 2, 2025 04:36
@fahadnayyar
Copy link
Contributor Author

@swift-ci please smoke test

struct __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:Retain18")))
__attribute__((swift_attr("release:Release18"))) NoIdentifierInCtorParam {
public:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have some tests with constructors of user defined types that have user defined copy/move ctors.

@fahadnayyar fahadnayyar enabled auto-merge (squash) April 7, 2025 05:48
@fahadnayyar
Copy link
Contributor Author

Enabling auto merge after addressing all the comments on this patch. I can think of only 2 things right now which can be addressed in a follow up patch:

  • Default args in C++ frt ctors
  • Better Tests for Copy, move and other kinds of ctors of C++ FRTs

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for working on this! I have one last comment inline but otherwise everything looks good to me.

"Unable to synthesize `new` expression for c++ foreign reference type");
auto *synthNewExpr = cast<clang::CXXNewExpr>(synthNewExprResult.get());

clang::ReturnStmt *synthRetStmt = clang::ReturnStmt::Create(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I wonder if we want to use Sema::BuildReturnStmt here, and add the SFNAETrap to all of the sema calls (could be the same trap for all the Build* calls).

@fahadnayyar fahadnayyar disabled auto-merge April 7, 2025 15:39
@fahadnayyar
Copy link
Contributor Author

@swift-ci please smoke test

@fahadnayyar
Copy link
Contributor Author

Enabling auto merge after addressing all the comments on this patch. I can think of only these minor things which can be addressed in follow up patches later:

  • Default args in C++ frt ctors. They are also not supported for ctors of Value types.
  • Better Tests for Copy, move and other kinds of ctors of C++ FRTs so that the tests make sense on the Swift side.
  • A lit test involving has been removed for now to make macOS swift ci happy because of some unrelated issue: rdar://148791414. But I verified locally that the removed test also works well.

@fahadnayyar fahadnayyar enabled auto-merge (squash) April 8, 2025 07:20
@fahadnayyar fahadnayyar merged commit 5d1ce01 into swiftlang:main Apr 8, 2025
3 checks passed
fahadnayyar added a commit that referenced this pull request Apr 10, 2025
…pes as Swift Initializer (#80449)

Extends PR #79986 by adding support for calling parameterized C++ initializers from Swift. This patch synthesizes static factory methods corresponding to C++ parameterized constructors, allowing Swift to call them as Swift initializers (e.g., init(_:), init(_:_:), etc.). This patch also aded tests and logic to make sure that we emit no additional diagnostics when a C++ foreign ref type is just referred from Swift and its initializer is not explicitly called.

rdar://148285251
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants