Skip to content

Commit 0e97ba5

Browse files
committed
[cxx-interop] Add basic C++ stdlib overlay
This will allow us to add Swift conformances to C++ standard library types, and improve usability of the C++ standard library from Swift.
1 parent 298083d commit 0e97ba5

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,24 @@ endforeach()
122122
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})
123123
set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous")
124124
add_dependencies(sdk-overlay libstdcxx-modulemap)
125+
126+
127+
#
128+
# C++ Standard Library Overlay.
129+
#
130+
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
131+
std.swift
132+
String.swift
133+
134+
SWIFT_MODULE_DEPENDS_OSX Darwin
135+
SWIFT_MODULE_DEPENDS_IOS Darwin
136+
SWIFT_MODULE_DEPENDS_TVOS Darwin
137+
SWIFT_MODULE_DEPENDS_WATCHOS Darwin
138+
139+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
140+
-Xfrontend -enable-experimental-cxx-interop
141+
-Xfrontend -module-interface-preserve-types-as-written
142+
143+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" -lc++
144+
TARGET_SDKS ALL_APPLE_PLATFORMS # TODO: support other platforms as well
145+
INSTALL_IN_COMPONENT sdk-overlay)

stdlib/public/Cxx/String.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
extension std.string {
14+
public init(_ string: String) {
15+
self.init()
16+
for char in string.utf8 {
17+
self.push_back(value_type(char))
18+
}
19+
}
20+
}
21+
22+
extension String {
23+
public init(cxxString: std.string) {
24+
self.init(cString: cxxString.c_str())
25+
}
26+
}

stdlib/public/Cxx/std.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import std // Clang module
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
// REQUIRES: OS=macosx
5+
6+
import StdlibUnittest
7+
import std
8+
9+
var StdStringOverlayTestSuite = TestSuite("std::string overlay")
10+
11+
StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
12+
let cxx1 = std.string()
13+
let swift1 = String(cxxString: cxx1)
14+
expectEqual(swift1, "")
15+
16+
let cxx2 = std.string("something123")
17+
let swift2 = String(cxxString: cxx2)
18+
expectEqual(swift2, "something123")
19+
}
20+
21+
runAllTests()

validation-test/ParseableInterface/verify_all_overlays.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@
5151

5252
# swift -build-module-from-parseable-interface
5353
output_path = os.path.join(output_dir, module_name + ".swiftmodule")
54+
compiler_args = ["-o", output_path, "-module-name", module_name,
55+
interface_file]
56+
if module_name == "std":
57+
compiler_args += ["-enable-experimental-cxx-interop"]
58+
5459
status = subprocess.call(compiler_invocation +
55-
["-o", output_path, "-module-name", module_name,
56-
interface_file])
60+
compiler_args)
5761
if status != 0:
5862
print("# " + target_os + ": " + module_name)

0 commit comments

Comments
 (0)