Skip to content

Commit 959e5de

Browse files
authored
Merge pull request #61461 from hyp/eng/opt++
[interop][SwiftToCxx] add some helper methods to work with Swift's Op…
2 parents 890a431 + 391b92f commit 959e5de

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,7 @@ void ClangSyntaxPrinter::printIncludeForShimHeader(StringRef headerName) {
354354
os << "#include <swiftToCxx/" << headerName << ">\n";
355355
os << "#endif\n";
356356
}
357+
358+
void ClangSyntaxPrinter::printDefine(StringRef macroName) {
359+
os << "#define " << macroName << "\n";
360+
}

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ class ClangSyntaxPrinter {
187187
// Print the #include sequence for the specified C++ interop shim header.
188188
void printIncludeForShimHeader(StringRef headerName);
189189

190+
// Print the #define for the given macro.
191+
void printDefine(StringRef macroName);
192+
190193
protected:
191194
raw_ostream &os;
192195
};

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ static void addCppExtensionsToStdlibType(const NominalTypeDecl *typeDecl,
140140
"passDirect_Swift_String(_getOpaquePointer())));\n";
141141
os << " }\n";
142142
});
143+
} else if (typeDecl == typeDecl->getASTContext().getOptionalDecl()) {
144+
// Add additional methods for the `Optional` declaration.
145+
printer.printDefine("SWIFT_CXX_INTEROP_OPTIONAL_MIXIN");
146+
printer.printIncludeForShimHeader("_SwiftStdlibCxxOverlay.h");
143147
}
144148
}
145149

lib/PrintAsClang/_SwiftCxxInteroperability.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include <malloc.h>
2626
#endif
2727

28+
// FIXME: Use always_inline, artificial.
29+
#define SWIFT_INLINE_THUNK inline
30+
2831
namespace swift {
2932
namespace _impl {
3033

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@
1414
#error "no C++"
1515
#endif
1616

17+
#ifdef SWIFT_CXX_INTEROP_OPTIONAL_MIXIN
18+
19+
/// True when the Optional has a value.
20+
SWIFT_INLINE_THUNK operator bool() const noexcept { return *this != none; }
21+
22+
/// Returns the value stored in the Optional.
23+
///
24+
/// The returned value is copied using the appropriate Swift / C++ copy
25+
/// semantics.
26+
SWIFT_INLINE_THUNK T_0_0 get() const
27+
noexcept(noexcept(getUnsafelyUnwrapped())) {
28+
// FIXME: Fail with source location.
29+
return getUnsafelyUnwrapped();
30+
}
31+
32+
#undef SWIFT_CXX_INTEROP_OPTIONAL_MIXIN
33+
34+
#else
35+
// out-of-class overlay for Swift standard library.
36+
1737
static_assert(sizeof(_impl::_impl_String) >= 0,
1838
"included outside of stdlib bindings");
1939

@@ -70,3 +90,5 @@ template <class T>
7090
inline cxxOverlay::IterationEndSentinel end(const Array<T> &) {
7191
return {};
7292
}
93+
94+
#endif

test/Interop/SwiftToCxx/stdlib/optional/optional-execution.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ int main() {
8484
{
8585
auto val = createCIntOpt(2);
8686
takeCIntOpt(val);
87+
assert((bool)val);
8788
assert(val.isSome());
8889
assert(val.getUnsafelyUnwrapped() == 2);
90+
assert(val.get() == 2);
8991
resetOpt(val);
92+
assert(!(bool)val);
9093
assert(val.isNone());
9194
takeCIntOpt(val);
9295
}
@@ -95,9 +98,12 @@ int main() {
9598
{
9699
auto val = createSmallStructOpt(0xFA);
97100
takeSmallStructOpt(val);
101+
assert((bool)val);
98102
assert(val.isSome());
99103
assert(val.getUnsafelyUnwrapped().getX() == 0xFA);
104+
assert(val.get().getX() == 0xFA);
100105
resetOpt(val);
106+
assert(!(bool)val);
101107
assert(val.isNone());
102108
takeSmallStructOpt(val);
103109
}

0 commit comments

Comments
 (0)