Skip to content

[interop][SwiftToCxx] add some helper methods to work with Swift's Op… #61461

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 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/PrintAsClang/ClangSyntaxPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,7 @@ void ClangSyntaxPrinter::printIncludeForShimHeader(StringRef headerName) {
os << "#include <swiftToCxx/" << headerName << ">\n";
os << "#endif\n";
}

void ClangSyntaxPrinter::printDefine(StringRef macroName) {
os << "#define " << macroName << "\n";
}
3 changes: 3 additions & 0 deletions lib/PrintAsClang/ClangSyntaxPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ class ClangSyntaxPrinter {
// Print the #include sequence for the specified C++ interop shim header.
void printIncludeForShimHeader(StringRef headerName);

// Print the #define for the given macro.
void printDefine(StringRef macroName);

protected:
raw_ostream &os;
};
Expand Down
4 changes: 4 additions & 0 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ static void addCppExtensionsToStdlibType(const NominalTypeDecl *typeDecl,
"passDirect_Swift_String(_getOpaquePointer())));\n";
os << " }\n";
});
} else if (typeDecl == typeDecl->getASTContext().getOptionalDecl()) {
// Add additional methods for the `Optional` declaration.
printer.printDefine("SWIFT_CXX_INTEROP_OPTIONAL_MIXIN");
printer.printIncludeForShimHeader("_SwiftStdlibCxxOverlay.h");
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/PrintAsClang/_SwiftCxxInteroperability.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <malloc.h>
#endif

// FIXME: Use always_inline, artificial.
#define SWIFT_INLINE_THUNK inline

namespace swift {
namespace _impl {

Expand Down
22 changes: 22 additions & 0 deletions lib/PrintAsClang/_SwiftStdlibCxxOverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
#error "no C++"
#endif

#ifdef SWIFT_CXX_INTEROP_OPTIONAL_MIXIN

/// True when the Optional has a value.
SWIFT_INLINE_THUNK operator bool() const noexcept { return *this != none; }

/// Returns the value stored in the Optional.
///
/// The returned value is copied using the appropriate Swift / C++ copy
/// semantics.
SWIFT_INLINE_THUNK T_0_0 get() const
noexcept(noexcept(getUnsafelyUnwrapped())) {
// FIXME: Fail with source location.
return getUnsafelyUnwrapped();
}

#undef SWIFT_CXX_INTEROP_OPTIONAL_MIXIN

#else
// out-of-class overlay for Swift standard library.

static_assert(sizeof(_impl::_impl_String) >= 0,
"included outside of stdlib bindings");

Expand Down Expand Up @@ -70,3 +90,5 @@ template <class T>
inline cxxOverlay::IterationEndSentinel end(const Array<T> &) {
return {};
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ int main() {
{
auto val = createCIntOpt(2);
takeCIntOpt(val);
assert((bool)val);
assert(val.isSome());
assert(val.getUnsafelyUnwrapped() == 2);
assert(val.get() == 2);
resetOpt(val);
assert(!(bool)val);
assert(val.isNone());
takeCIntOpt(val);
}
Expand All @@ -95,9 +98,12 @@ int main() {
{
auto val = createSmallStructOpt(0xFA);
takeSmallStructOpt(val);
assert((bool)val);
assert(val.isSome());
assert(val.getUnsafelyUnwrapped().getX() == 0xFA);
assert(val.get().getX() == 0xFA);
resetOpt(val);
assert(!(bool)val);
assert(val.isNone());
takeSmallStructOpt(val);
}
Expand Down