Commit Graph

85 Commits

Author SHA1 Message Date
Alex Lorenz
3748b0ff1c Merge pull request #65129 from hyp/eng/no-evo-cxx
[interop] Prohibit use of C++ APIs in public interfaces that opt-in i…
2023-07-19 08:10:23 -07:00
Egor Zhdan
6cd4b7c28f [cxx-interop] Make std::string::append usable from Swift
This adds a new Swift overload for `append` that takes another `std::string` as a parameter.

The original C++ overload for `append` is not exposed into Swift because it returns a mutable reference `string&`.

rdar://107018724
2023-06-19 20:51:27 +01:00
Egor Zhdan
60c91aa90a [cxx-interop] Declare CxxStdlib dependency on Darwin when building both
rdar://108188270
2023-06-07 20:17:24 +01:00
Egor Zhdan
b5866040ae [cxx-interop] Fix Linux build with SWIFT_ENABLE_EXPERIMENTAL_CXX_INTEROP=NO
This fixes the Swift LSAN CI job Linux (https://ci.swift.org/job/oss-swift-RA-lsan-linux-ubuntu-18_04/) that started failing after https://github.com/apple/swift/pull/65398.

rdar://108674065
2023-05-02 17:35:29 +01:00
Egor Zhdan
030313213c [cxx-interop] Add missing dependency to CxxStdlib
The CxxStdlib module now relies on the libcxxshim modulemap, so we
need to make sure that the modulemap is copied into the correct location before CxxStdlib is built.

rdar://108007693
2023-04-13 20:28:24 +01:00
Alex Lorenz
045fcf3ff5 [interop] Prohibit use of C++ APIs in public interfaces that opt-in into library evolution
The CxxStdlib overlay now has to be built without library evolution enabled.
2023-04-13 10:48:09 -07:00
Egor Zhdan
7774650db1 [cxx-interop] Fix CxxStdlib build error on CentOS
This fixes a build failure that started occurring on CentOS after https://github.com/apple/swift/pull/65057:
```
error: cannot find 'strlen' in scope
```

rdar://107987115
2023-04-13 13:42:46 +01:00
Puyan Lotfi
6180387a05 [cxx-interop] Adding std.string initializer for UnsafePointer<CChar>?
Currently without an initializer for the unsafe char pointer type swiftc
hits an assert around not being able to handle conversions of unsafe
pointers with Any type. This patch adds the ability to convert to a
std::string.

This is to address issue https://github.com/apple/swift/issues/61218
2023-04-12 22:03:57 -04:00
Egor Zhdan
14f32312bf [cxx-interop] Do not add a dependency on clang to CxxStdlib
Cxx & CxxStdlib modules are Swift-only, they do not require invoking clang directly.

When building with `SWIFT_INCLUDE_TOOLS=NO`, Clang is not available as a CMake target (see `swift_common_standalone_build_config`).

rdar://107780733
2023-04-11 16:45:39 +01:00
Egor Zhdan
242a432919 [cxx-interop] Conform std::string to Hashable
rdar://107709149
2023-04-06 16:15:40 +01:00
Egor Zhdan
416e2e17df [cxx-interop] Remove CxxStdlib dependency on Darwin
This was causing build errors when building the compiler and the stdlib separately.

rdar://107290998
2023-03-28 16:16:04 +01:00
Egor Zhdan
62cb187a3b [cxx-interop] Add operators for comparing and concatenating std::strings
The original C++ operators are not currently imported into Swift because they are defined as a non-member templated functions.

This change adds the operators as Swift extension functions. This also adds an `Equatable` conformance for `std::string`.

rdar://107017882
2023-03-23 18:00:50 +00:00
Alex Lorenz
eba8a00673 [interop] Inject header files into libstdcxx.modulemap dynamically
This allows the module map we use for libstdc++ to correctly include optional headers like "any" and other C++17/20 files.
2023-03-08 07:59:52 -08:00
Egor Zhdan
ed0cee9ce6 [cxx-interop] Conform std::u16string to CustomStringConvertible and CustomDebugStringConvertible
This conforms `std::u16string` to two protocols that `std::string` already conforms to.

rdar://106037638
2023-02-28 19:02:45 +00:00
Egor Zhdan
9818312da4 Revert "Revert "[cxx-interop] Add conversions between std::u16string and Swift.String""
This re-lands https://github.com/apple/swift/pull/61695 since the linker error on CentOS has been fixed in https://github.com/apple/swift/pull/63058.

This reverts commit e97b1c8e
2023-02-22 18:31:36 +00:00
Egor Zhdan
34c2b0e50d [cxx-interop] Make String.init(std.string) unlabeled
`Swift.String` can be initialized from any other type with an unlabeled initializer, which is either going to use the `CustomStringConvertible` conformance, or reflection. We would like clients to use the most suitable initializer, which is the one that takes `std.string` as a parameter. For instance, that allows us to attach a doc comment to the initializer.

This change makes the initializer unlabeled to make sure it is chosed by overload resolution when a client invokes `String(myCxxString)`.
2023-02-02 12:47:08 +00:00
Chris White
7c126bcc88 Make std.string conform to CustomStringConvertible 2023-01-28 16:48:47 +00:00
Egor Zhdan
35553d9306 Merge pull request #62698 from whiteio/whiteio/add-customdebugstringconvertible-conformance-to-std-string
Make std::string conform to CustomDebugStringConvertible
2023-01-26 22:17:08 +00:00
Egor Zhdan
ee6b9b2a67 [cxx-interop] Disallow import std, require import CxxStdlib
`CxxStdlib` will now be the only accepted module name for the C++ standard library module in Swift.
2023-01-26 14:15:56 +00:00
Egor Zhdan
fb27f392c7 [cxx-interop] NFC: Add documentation comments for string conversion 2023-01-11 12:24:33 +00:00
Christopher White
70a7a88027 Make std.string conform to CustomDebugStringConvertible 2023-01-06 21:28:33 +00:00
Egor Zhdan
151c205a29 [cxx-interop] Move C++ standard library overlay to the toolchain
The `swiftCxx` and `swiftstd` modules are currently a part of the SDK, but they should be a part of the toolchain instead.

rdar://103082278
2022-12-12 11:18:30 +00:00
Egor Zhdan
c2c3ea7e07 [cxx-interop] Make Cxx Swift library static
Instead of a dynamic `swiftCxx.dylib` library, let's build a static library to simplify backdeployment and reduce potential compatibility difficulties in the future.

This also adds `NO_LINK_NAME` option to `add_swift_target_library` to prevent the CMake scripts from passing `-module-link-name` to swiftc when building a given module. This fixes linker errors, which would otherwise occur due to the force-load symbol name (`_swift_FORCE_LOAD_$xyz`) being emitted for the libraries that are now static (`swiftCxx`, `swiftstd`).
2022-12-07 12:37:25 +00:00
Egor Zhdan
1e86760c4b [cxx-interop] Replace std module name with the new spelling CxxStdlib in the overlay
This is the third step in renaming the C++ stdlib module `std` into `CxxStdlib`.

See https://github.com/apple/swift/pull/62296.
2022-11-30 15:36:43 +00:00
Egor Zhdan
d79144cdac [cxx-interop] Prevent "header not found" errors on Linux when tbb is not installed
libstdc++9 has a dependency on the `tbb` package, which is not bundled with Ubuntu as of 20.04:
`<execution>` includes `<pstl/parallel_backend_tbb.h>` which tries to include `<tbb/blocked_range.h>`.

This results in compiler errors when someone is trying to use the C++ stdlib from Swift:
```
/usr/include/c++/9/pstl/parallel_backend_tbb.h:19:10: error: 'tbb/blocked_range.h' file not found
```

Let's only include `<execution>` if tbb headers are available.

rdar://102151194
2022-11-16 13:23:53 +00:00
Egor Zhdan
e97b1c8e5b Revert "[cxx-interop] Add conversions between std::u16string and Swift.String" 2022-10-26 11:54:39 +01:00
Egor Zhdan
306dcdfa96 [cxx-interop] Add conversions between std::u16string and Swift.String
This change adds a few extensions to the C++ stdlib overlay to allow convenient conversions between C++ UTF-16 strings and Swift strings.
2022-10-24 12:15:30 +01:00
Egor Zhdan
6472997f54 [cxx-interop] Handle std::strings with \0 character
Previously the conversion mechanism called `std::string::c_str` and passed it to `String(cString:)` which accepts a null-terminated string. If the string contains a `\0` character, this failed to initialize the entire string properly.
2022-10-03 20:16:00 +01:00
Egor Zhdan
dc8ff65195 [cxx-interop] Do not crash for non-ASCII strings
This fixes a crash that happened when creating an instance of `std::string` from a `Swift.String` that contains non-ASCII characters.

When converted to a UTF-8 array, such characters might be represented by numbers that fall out of `CChar`'s boundaries if they have a sign bit set to 1. This is normal and shouldn't trigger a crash or an assertion failure.
2022-09-28 18:30:20 +01:00
Egor Zhdan
74723a99d7 [cxx-interop] Move included stdlib headers from libstdcxx.h to the modulemap
By referencing a C++ stdlib header directly from the modulemap, we make sure that the header is treated as a part of the stdlib, not a part of the first clang module to include it.

Addresses the issue described in https://forums.swift.org/t/llvm-fails-to-build-with-modules/59700/8.
2022-08-23 16:44:44 +01:00
Egor Zhdan
d6089c91ed [cxx-interop] Conform std::string to ExpressibleByStringLiteral
This allows `std::string` to be constructed implicitly from a String literal, which is convenient e.g. when calling C++ APIs that take `std::string` as a parameter.
2022-07-28 11:07:09 +01:00
zoecarver
d5e88d9526 [cxx-interop][overlay] Revert memcpy string init approach.
For some reason this isn't working, so I reverted to the previous approach. We can try to re-visit this in the future for a potential perf improvement.
2022-07-19 09:46:01 -04:00
zoecarver
6acffbbee6 [cxx-interop] Flip the switch: only import safe APIs. 2022-07-18 17:15:15 -04:00
Daniel Rodríguez Troitiño
036e16810a [Cxx] Make C++ flags configurable for Linux SDK (#59764)
Allow Linux distributions to provide their own C++ flags to compile the
C++ overlay correctly. The default is kept the same for Ubuntu and
CentOS, but other distributions can provide other flags to use their own
distro GCC stdlibc++ or even libc++ if they choose.

This should not change the current compilation, but opens the door for
other maintainers to provide a different value that work on their
systems.
2022-06-29 08:54:56 -07:00
Egor Zhdan
82a90b90aa [cxx-interop] Split C++ stdlib overlay into two modules
This allows projects that don't want to pull in the entire C++ standard library to use stdlib-independent C++ interop utilities like `CxxSequence`.
This also makes the utilities available on platforms where we don't currently have the `std` overlay available, e.g. Windows.
2022-06-28 15:37:20 +01:00