Skip to content

[Interop][SwiftToCxx] Support enum creation from Cxx #60842

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 2 commits into from
Aug 31, 2022

Conversation

WANGJIEKE
Copy link
Contributor

This pull request adds support for Swift enum creation from C++:

  • Add function in cxx_synthesis to print swift namespace in C++.
  • Update DeclAndTypePrinter to print functions needed for enum creation.
    • Fix a bug when printing cases for empty resilient enum
  • Forward declare types used in Swift enums
  • Add a function in DeclAndTypeClangFuncPrinter to print a custom generated function in C++
  • Update and merge test cases

@WANGJIEKE
Copy link
Contributor Author

@hyp Could you please test and review this pull request? Thanks.

@hyp
Copy link
Contributor

hyp commented Aug 30, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Aug 30, 2022

@swift-ci please test macOS platform

@hyp
Copy link
Contributor

hyp commented Aug 30, 2022

@swift-ci please test source compatibility

@hyp hyp added the c++ interop Feature: Interoperability with C++ label Aug 30, 2022
int main() {
{
auto e = E::x(3.14);
switchTest(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

we're asserting in each case, but we should also verify that we actually went through the right switch case. How about returning some sentinel value from switchTest e.g. a string, and asserting here that it's expected, e.g.

    case E::x:
         assert(e.isX());
         assert(e.getX() == 3.14);
        return "x";

...

         auto e = E::x(3.14);
         auto res = switchTest(e);
         assert(res == "e");

// CHECK-NEXT: }
// CHECK-NEXT: inline E E::_impl_foobar::operator()() const {
// CHECK-NEXT: auto result = E::_make();
// CHECK-NEXT: result._destructiveInjectEnumTag(5);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add a test for resilient enum where we call _destructiveInjectEnumTag using extern "C" tag values as well?

// CHECK-NEXT: using _impl_auto = decltype(auto_);
// CHECK-NEXT: using _impl_foobar = decltype(foobar);
// CHECK: };
// CHECK-NEXT: decltype(E::x) E::x;
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make supporting structs emitted as inline const variables? that way they're actually unique to each TU and won't conflict at link time.

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test source compatibility

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test linux platform

Copy link
Contributor

@hyp hyp left a comment

Choose a reason for hiding this comment

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

LGTM

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

Hooray!

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test linux platform

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test windows platform

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please smoke test macos platform

@hyp
Copy link
Contributor

hyp commented Aug 31, 2022

@swift-ci please test macos platform

@hyp hyp merged commit 8214b85 into swiftlang:main Aug 31, 2022
@WANGJIEKE WANGJIEKE deleted the cxx-interop-enum-creation branch August 31, 2022 21:13
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.

2 participants