-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[interop][SwiftToCxx] Add initial support for passing/returning enums #59584
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/Interop/SwiftToCxx/enums/large-enums-pass-return-in-cxx-execution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// RUN: %empty-directory(%t) | ||
|
||
// RUN: %target-swift-frontend %S/large-enums-pass-return-in-cxx.swift -typecheck -module-name Enums -clang-header-expose-public-decls -emit-clang-header-path %t/enums.h | ||
|
||
// RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-enums-execution.o | ||
// RUN: %target-interop-build-swift %S/large-enums-pass-return-in-cxx.swift -o %t/swift-enums-execution -Xlinker %t/swift-enums-execution.o -module-name Enums -Xfrontend -entry-point-function-name -Xfrontend swiftMain | ||
|
||
// RUN: %target-codesign %t/swift-enums-execution | ||
// RUN: %target-run %t/swift-enums-execution | %FileCheck %s | ||
|
||
// REQUIRES: executable_test | ||
// UNSUPPORTED: CPU=arm64e | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include "enums.h" | ||
|
||
int main() { | ||
using namespace Enums; | ||
|
||
// sizeof(generated cxx class) = 1 + max(sizeof(case) for all cases) + padding | ||
static_assert(sizeof(Large) == 56, "MemoryLayout<Large>.stride == 56"); | ||
|
||
auto large = makeLarge(-1); | ||
printLarge(large); | ||
// CHECK: Large.second | ||
inoutLarge(large, 10); | ||
printLarge(large); | ||
// CHECK: Large.first(-1, -2, -3, -4, -5, -6) | ||
printLarge(passThroughLarge(large)); | ||
// CHECK: Large.first(-1, -2, -3, -4, -5, -6) | ||
|
||
return 0; | ||
} |
62 changes: 62 additions & 0 deletions
62
test/Interop/SwiftToCxx/enums/large-enums-pass-return-in-cxx.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend %s -typecheck -module-name Enums -clang-header-expose-public-decls -emit-clang-header-path %t/enums.h | ||
// RUN: %FileCheck %s < %t/enums.h | ||
|
||
// RUN: %check-interop-cxx-header-in-clang(%t/enums.h -Wno-unused-private-field -Wno-unused-function) | ||
|
||
public enum Large { | ||
case first(Int64, Int64, Int64, Int64, Int64, Int64) | ||
case second | ||
} | ||
|
||
public func makeLarge(_ x: Int) -> Large { | ||
return x >= 0 ? .first(0, 1, 2, 3, 4, 5) : .second | ||
} | ||
|
||
public func printLarge(_ en: Large) { | ||
switch en { | ||
case let .first(a, b, c, d, e, f): | ||
let x = (a, b, c, d, e, f) | ||
print("Large.first\(x)") | ||
case .second: | ||
print("Large.second") | ||
} | ||
} | ||
|
||
public func passThroughLarge(_ en: Large) -> Large { | ||
return en | ||
} | ||
|
||
public func inoutLarge(_ en: inout Large, _ x: Int) { | ||
if x >= 0 { | ||
en = .first(-1, -2, -3, -4, -5, -6) | ||
} else { | ||
en = .second | ||
} | ||
} | ||
|
||
// CHECK: SWIFT_EXTERN void $s5Enums10inoutLargeyyAA0C0Oz_SitF(void * _Nonnull en, ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_CALL; // inoutLarge(_:_:) | ||
// CHECK: SWIFT_EXTERN void $s5Enums9makeLargeyAA0C0OSiF(SWIFT_INDIRECT_RESULT void * _Nonnull, ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_CALL; // makeLarge(_:) | ||
// CHECK: SWIFT_EXTERN void $s5Enums16passThroughLargeyAA0D0OADF(SWIFT_INDIRECT_RESULT void * _Nonnull, const void * _Nonnull en) SWIFT_NOEXCEPT SWIFT_CALL; // passThroughLarge(_:) | ||
// CHECK: SWIFT_EXTERN void $s5Enums10printLargeyyAA0C0OF(const void * _Nonnull en) SWIFT_NOEXCEPT SWIFT_CALL; // printLarge(_:) | ||
// CHECK: class Large final { | ||
|
||
// CHECK: inline void inoutLarge(Large& en, swift::Int x) noexcept { | ||
// CHECK-NEXT: return _impl::$s5Enums10inoutLargeyyAA0C0Oz_SitF(_impl::_impl_Large::getOpaquePointer(en), x); | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: inline Large makeLarge(swift::Int x) noexcept SWIFT_WARN_UNUSED_RESULT { | ||
// CHECK-NEXT: return _impl::_impl_Large::returnNewValue([&](void * _Nonnull result) { | ||
// CHECK-NEXT: _impl::$s5Enums9makeLargeyAA0C0OSiF(result, x); | ||
// CHECK-NEXT: }); | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: inline Large passThroughLarge(const Large& en) noexcept SWIFT_WARN_UNUSED_RESULT { | ||
// CHECK-NEXT: return _impl::_impl_Large::returnNewValue([&](void * _Nonnull result) { | ||
// CHECK-NEXT: _impl::$s5Enums16passThroughLargeyAA0D0OADF(result, _impl::_impl_Large::getOpaquePointer(en)); | ||
// CHECK-NEXT: }); | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: inline void printLarge(const Large& en) noexcept { | ||
// CHECK-NEXT: return _impl::$s5Enums10printLargeyyAA0C0OF(_impl::_impl_Large::getOpaquePointer(en)); | ||
// CHECK-NEXT: } |
46 changes: 46 additions & 0 deletions
46
test/Interop/SwiftToCxx/enums/small-enums-pass-return-in-cxx-execution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// RUN: %empty-directory(%t) | ||
|
||
// RUN: %target-swift-frontend %S/small-enums-pass-return-in-cxx.swift -typecheck -module-name Enums -clang-header-expose-public-decls -emit-clang-header-path %t/enums.h | ||
|
||
// RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-enums-execution.o | ||
// RUN: %target-interop-build-swift %S/small-enums-pass-return-in-cxx.swift -o %t/swift-enums-execution -Xlinker %t/swift-enums-execution.o -module-name Enums -Xfrontend -entry-point-function-name -Xfrontend swiftMain | ||
|
||
// RUN: %target-codesign %t/swift-enums-execution | ||
// RUN: %target-run %t/swift-enums-execution | %FileCheck %s | ||
|
||
// REQUIRES: executable_test | ||
// UNSUPPORTED: CPU=arm64e | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include "enums.h" | ||
|
||
#define MAX(a, b) ((a) >= (b) ? (a) : (b)) | ||
|
||
int main() { | ||
using namespace Enums; | ||
|
||
// sizeof(generated cxx class) = 1 + max(sizeof(case) for all cases) + padding | ||
static_assert(sizeof(Tiny) == 1, "MemoryLayout<Tiny>.stride == 1"); | ||
static_assert(sizeof(Small) == 16, "MemoryLayout<Small>.stride == 16"); | ||
|
||
auto tiny = makeTiny(10); | ||
printTiny(tiny); | ||
// CHECK: Tiny.first | ||
inoutTiny(tiny, -1); | ||
printTiny(tiny); | ||
// CHECK: Tiny.second | ||
printTiny(passThroughTiny(tiny)); | ||
// CHECK: Tiny.second | ||
|
||
auto small = makeSmall(-3); | ||
printSmall(small); | ||
// CHECK: Small.second(3.0) | ||
inoutSmall(small, 100); | ||
printSmall(small); | ||
// CHECK: Small.first(100) | ||
printSmall(passThroughSmall(small)); | ||
// CHECK: Small.first(100) | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.