Skip to content

Commit 5e09a5c

Browse files
committed
[interop][SwiftToCxx] move std::string -> Swift String constructor to the overlay header
1 parent 28af8be commit 5e09a5c

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

lib/PrintAsClang/PrintClangValueType.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,6 @@ static void addCppExtensionsToStdlibType(const NominalTypeDecl *typeDecl,
159159
os << " return result;\n";
160160
os << " }\n";
161161
});
162-
os << "#ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY\n";
163-
ClangSyntaxPrinter(os).printInlineForThunk();
164-
os << "String(const std::string &str) noexcept {\n";
165-
os << " auto res = "
166-
"_impl::$sSS7cStringSSSPys4Int8VG_tcfC(str.c_str());\n";
167-
os << " memcpy(_getOpaquePointer(), &res, sizeof(res));\n";
168-
os << "}\n";
169-
os << "#endif\n";
170162
// Add additional methods for the `String` declaration.
171163
printer.printDefine("SWIFT_CXX_INTEROP_STRING_MIXIN");
172164
printer.printIncludeForShimHeader("_SwiftStdlibCxxOverlay.h");

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ SWIFT_INLINE_THUNK T_0_0 get() const
3535

3636
#ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
3737

38+
/// Constructs a Swift string from a C++ string.
39+
SWIFT_INLINE_THUNK String(const std::string &str) noexcept {
40+
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(str.c_str());
41+
memcpy(_getOpaquePointer(), &res, sizeof(res));
42+
}
43+
3844
/// Casts the Swift String value to a C++ std::string.
3945
SWIFT_INLINE_THUNK operator std::string() const;
4046

test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift

-6
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@
9090
// CHECK-NEXT: }
9191
// CHECK-EMPTY:
9292
// CHECK-NEXT: #endif
93-
// CHECK-NEXT: #ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
94-
// CHECK-NEXT: inline __attribute__((always_inline)) String(const std::string &str) noexcept {
95-
// CHECK-NEXT: auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(str.c_str());
96-
// CHECK-NEXT: memcpy(_getOpaquePointer(), &res, sizeof(res));
97-
// CHECK-NEXT: }
98-
// CHECK-NEXT: #endif
9993
// CHECK-NEXT: #define SWIFT_CXX_INTEROP_STRING_MIXIN
10094
// CHECK-NEXT: // Look for the C++ interop support header relative to clang's resource dir:
10195
// CHECK-NEXT: // '<toolchain>/usr/lib/clang/<version>/include/../../../swift/swiftToCxx'.

0 commit comments

Comments
 (0)