Commit Graph

204 Commits

Author SHA1 Message Date
Egor Zhdan
e064d23d74 Merge pull request #66764 from apple/egorzhdan/cxx-set-insert
[cxx-interop] Allow inserting elements into `std::set` from Swift
2023-06-22 15:26:57 +01:00
Egor Zhdan
fbf15aefe3 Merge pull request #66765 from apple/egorzhdan/arch-independent-cxxshim
[cxx-interop] Make CxxShim header and modulemap arch-independent
2023-06-21 14:29:09 +01:00
Egor Zhdan
b79b65c056 [cxx-interop] Allow inserting elements into std::set from Swift
`std::set::insert` isn't exposed into Swift, because it returns an instance of an unsafe type.

This change adds a Swift overload of `insert` for `std::set` and `std::unordered_set` that has the return type identical to `Swift.Set.insert`: a tuple of `(inserted: Bool, memberAfterInsert: Element)`.

rdar://111036912
2023-06-20 16:56:54 +01:00
Egor Zhdan
4ce4527ef1 [cxx-interop] Make CxxShim header and modulemap arch-independent
This moves `libcxxshim.modulemap`, `libcxxshim.h` and `libcxxstdlibshim.h` from `*.xctoolchain/usr/lib/swift/macosx/arm64e` to `*.xctoolchain/usr/lib/swift/macosx` to simplify distribution.

rdar://110788977
2023-06-20 15:57:38 +01: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
042aff2a76 [cxx-interop] Optimize CxxSet initialization from a Swift Sequence
rdar://107909624
2023-06-13 18:04:20 +01:00
Egor Zhdan
dd7e1775fc [cxx-interop] Add std::set initializer that takes a Swift Sequence
rdar://107909624
2023-06-13 14:51:28 +01:00
Egor Zhdan
57bf922d18 Merge pull request #66425 from apple/egorzhdan/cxxstdlib-darwin-dependency
[cxx-interop] Declare CxxStdlib dependency on Darwin when building both
2023-06-08 15:59:09 +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
cf8ac14215 [cxx-interop] Initializing a Swift.Array from CxxConvertibleToCollection should not copy the collection
This makes the `CxxConvertibleToCollection` parameter shared, preventing the unnecessary copy of the C++ value.

rdar://110110376
2023-06-06 15:54:58 +01:00
Egor Zhdan
7d7825591d [cxx-interop] Make std.optional.value nullable
rdar://109356566 / resolves https://github.com/apple/swift/issues/65918
2023-05-15 17:30:59 +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
f80e496c27 Merge pull request #65144 from apple/egorzhdan/cxx-sequence-foreach
[cxx-interop] Make `CxxConvertibleToCollection.forEach` public
2023-04-19 08:11:03 +01:00
Egor Zhdan
ce2b116dab [cxx-interop] Enable CxxShim headers for armv7k watchOS
This fixes the `no such module 'CxxStdlibShim'` error when building CxxStdlib for armv7k.

For watchOS, armv7k is included in `${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}` but not in `${SWIFT_SDK_${sdk}_ARCHITECTURES}`.

rdar://108206099
2023-04-18 18:03:52 +01:00
Egor Zhdan
d5f5d4bd55 [cxx-interop] Make CxxConvertibleToCollection.forEach public
This allows clients to iterate over a C++ container which doesn't provide random access with a `.forEach` call.

rdar://107995558
2023-04-18 14:43:43 +01:00
Daniel Rodríguez Troitiño
539fc69e1e build: Fixes for building Cxx module with/without stdlib. (#65172)
With the changes introduced in #65055, the Cxx module will build even
when not building the stdlib as long as
`SWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT` was set. With the further changes
in #65122 the previous flag is not necessary, and only
`SWIFT_BUILD_STDLIB_CXX_MODULE` (which defaults to `TRUE`) is necessary.

However, `stdlib/public/Cxx` did rely on `StdlibOptions.cmake` to be
include before it, or the functions that build the target libraries will
find a half configured state. `StdlibOptions.cmake` are included in
`stdlib/toolchain`, in `stdlib/` and `StandaloneOverlay.cmake`. Before #65122
it was working just because `stdlib/toolchain` was added just before.

The second changes adds dependencies on the legacy layouts and the clang
headers. The legacy layouts are a dependency only added to the libraries of
the stdlib core (IS_STDLIB_CORE flag). Since enabling that flag also enables
a bunch other stuff, and I am not sure that Cxx should be classified as "core"
anyway, I preferred to add the dependency. The clang headers are another
dependency of the swiftCore, which otherwise causes problems to not find
the compiler headers when building Cxx. This is necessary for builds
that use a previously built compiler to compile the stdlib.
2023-04-14 08:44:54 -07: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
ceb48be9af Merge pull request #65139 from apple/egorzhdan/cxx-overlay-inlinable
[cxx-interop] Make some overlay functions inlinable
2023-04-13 16:29:02 +01:00
Egor Zhdan
10d74b11aa [cxx-interop] Make some overlay functions inlinable 2023-04-13 13:49:46 +01: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
Egor Zhdan
51c9539655 [cxx-interop] Build CxxStdlib module when SWIFT_BUILD_SDK_OVERLAY=NO
The C++ stdlib overlay binaries now a part of the toolchain, not the OS SDKs. This makes sure that these binaries are built when building the toolchain separately from the stdlib.

rdar://106002094
2023-03-09 19:30:47 +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
ac72084854 Merge pull request #63683 from apple/egorzhdan/cxx-optional
[cxx-interop] Add `CxxOptional` protocol for `std::optional` ergonomics
2023-03-06 10:43:05 +00:00
Egor Zhdan
a12986ade4 [cxx-interop] Add CxxOptional protocol for std::optional ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::optional` when used from Swift code.

As of now, the overlay adds an initializer of `Swift.Optional` that takes an instance of `CxxOptional` as a parameter.
2023-03-03 19:41:30 +01: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
8102b63451 Merge pull request #63865 from apple/egorzhdan/cxx-toolchain-build
[cxx-interop] Build the C++ stdlib overlay when `SWIFT_BUILD_SDK_OVERLAY=NO`
2023-02-27 22:25:02 +00:00
Egor Zhdan
1d5de34702 [cxx-interop] Move CxxShim header from the SDK to the toolchain
rdar://105799283
2023-02-25 00:23:43 +00:00
Egor Zhdan
75d0e999ea [cxx-interop] Build swiftCxx overlay when SWIFT_BUILD_SDK_OVERLAY=NO
The C++ stdlib overlay binaries (`libswiftCxx` and `libswiftCxxStdlib`) are now a part of the toolchain, not the OS SDKs. This makes sure that `libswiftCxx` is built when building the toolchain separately from the stdlib.

rdar://105799283
2023-02-25 00:23:43 +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
71cc1bb6f1 [cxx-interop] NFC: move unsafe iterator types to a separate file
Since the unsafe iterator types are now used throughout the overlay, not just by `CxxSequence` and `CxxRandomAccessCollection`, let's move them to a separate file.
2023-02-10 11:57:57 +00:00
Egor Zhdan
919eea7045 [cxx-interop] Add CxxDictionary protocol for std::map ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::map` and `std::unordered_map` when used from Swift code.

As of now, `CxxDictionary` adds a subscript with an optional return type that mimics the subscript of `Swift.Dictionary`.

Similar to https://github.com/apple/swift/pull/63244.
2023-02-09 14:31:06 +00:00
Egor Zhdan
7ec8054064 [cxx-interop] Change the hierarchy of CxxSequence protocols
This makes `CxxConvertibleToCollection` the base protocol in the hierarchy. Both `CxxSequence` and `CxxRandomAccessCollection` now inherit from `CxxConvertibleToCollection`.
2023-02-06 22:41:26 +00:00
Egor Zhdan
ad208223ad [cxx-interop] Avoid extra copy in CxxConvertibleToCollection
This improves the performance of iterating over a C++ container that is automatically conformed to `CxxConvertibleToCollection` protocol by removing the extra copy of the container.

This also tightens the requirements of `CxxConvertibleToCollection` by making `begin()` and `end()` non-mutating.
2023-02-06 16:04:28 +00:00
Egor Zhdan
ed70f30101 [cxx-interop] Make Element a primary associated type of CxxSequence etc 2023-02-02 12:48:17 +00:00
Egor Zhdan
b350f2b90f [cxx-interop] NFC: rename initializer parameter 2023-02-02 12:47:08 +00:00
Egor Zhdan
b98f71f2d4 [cxx-interop] Allow initializing SetAlgebra from a C++ container 2023-02-02 12:47:08 +00:00
Egor Zhdan
16d02db7aa [cxx-interop] Add internal function CxxConvertibleToCollection.forEach 2023-02-02 12:47:08 +00:00
Egor Zhdan
e3effcc184 [cxx-interop] Allow initializing RangeReplaceableCollection from a C++ container
This makes `Array.init<C: CxxConvertibleToCollection>(C)` more generic by using `RangeReplaceableCollection` protocol instead of concrete `Array` type.
2023-02-02 12:47:08 +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
Egor Zhdan
1745c98dd6 [cxx-interop] Make two overlay functions inlinable 2023-02-02 12:43:29 +00:00
Egor Zhdan
d0164ddfb5 Merge pull request #63245 from whiteio/whiteio/add-customstringconvertible-conformance-to-std-string
Make std::string conform to CustomStringConvertible
2023-02-02 12:40:28 +00:00
Egor Zhdan
3983442f97 Merge pull request #63244 from apple/egorzhdan/cxx-set-protocol
[cxx-interop] Add `CxxSet` protocol for `std::set` ergonomics
2023-02-01 16:36:24 +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