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
[Interop][SwiftToCxx] Implement enum case switching #59744
Conversation
@hyp Could you please review this PR? Thanks! I have a question regarding naming predicate functions: is |
@swift-ci please test |
// RUN: %target-codesign %t/swift-enums-execution | ||
// RUN: %target-run %t/swift-enums-execution | ||
|
||
// REQUIRES: executable_test |
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.
Please also disable it on arm64e : ‘// UNSUPPORTED: CPU=arm64e’
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.
Oh yeah I forgot this.
if (i != elements.size() - 1) { | ||
os << " case " << i << ": return cases::"; | ||
} else { | ||
os << " case " << i << ": default: return cases::"; |
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.
Why do we need a default case here?
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.
I want to silence the non-exhaustive switch warning from the compiler.
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.
For sure. Unfortunately I don’t think the default case should map to a specific case. I think by default it should abort as that’s an unexpected path for fixed enums. Resilient enums are more complicated as they could have an unknown case, but we can handle them in a follow up.
} | ||
|
||
// Rearrange cases so that they match their tag values | ||
llvm::stable_sort(elements, [&](const auto *e1, const auto *e2) { |
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.
How did you determine that this is the correct ordering? Is the compiler doing this kind of sort anywhere else? We might want to reuse this if thins is already being done elsewhere
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.
I got it from the memory layout doc, but yeah it's better to reuse existing functionality. I'll ask this in the office hour meeting.
Can you also add check lines for implementation of ‘_getEnumTag’ |
For now it’s fine, we already use it for properties. It might not handle Unicode correctly, but we will refactor and fix name transformations later. |
@egorzhdan Could you please help me invoke the testing? Thanks! |
@swift-ci please test |
029dc2f
to
7ebcb7b
Compare
@swift-ci please test |
Looks good apart from one request |
@swift-ci please test |
@swift-ci please test source compatibility |
@swift-ci please test windows platform |
Also change the return type of |
@swift-ci please test |
@swift-ci please test source compatibility |
@swift-ci please test |
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.
LGTM
This is a follow-up of pull request #59669. This pull request implements
operator cases()
and various predicates.For example, following Swift enum
will have member functions in the generated C++ header file like this
Other changes:
EnumValueWitnessTable
struct when printing C++ interop scaffold;_getEnumTag
helper function for retrieving the tag from enum value witness table.