Commit Graph

217 Commits

Author SHA1 Message Date
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
Egor Zhdan
6366878308 [cxx-interop] Add CxxSet protocol for std::set ergonomics
This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::set`, `std::unordered_set` and `std::multiset` when used from Swift code.

As of now, `CxxSet` adds a `contains` function to C++ sets.

C++ stdlib set types are automatically conformed to `CxxSet`: `std::set`, `unordered_set`, `std::multiset`. Custom user types are not conformed to `CxxSet` automatically: while a custom type might have an interface similar to `std::set`, the semantics might differ, and adding a conformance would cause confusion.
2023-01-26 19:35:30 +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
0c64fe495d Merge pull request #62869 from apple/egorzhdan/cxx-sequence-docs
[cxx-interop] NFC: Improve documentation comments
2023-01-12 11:56:34 +01:00
Egor Zhdan
2ac5ccf7c8 [cxx-interop] NFC: Clarify copying semantics 2023-01-12 00:39:32 +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
95f4daf578 [cxx-interop] NFC: Use the term "C++ container" consistently 2023-01-05 16:16:33 +00:00
Egor Zhdan
490db497f3 [cxx-interop] NFC: Add documentation comment for CxxSequence.makeIterator
Clarify that calling `makeIterator` on a C++ sequence has `O(n)` complexity.
2023-01-05 16:14:59 +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
Daniel Rodríguez Troitiño
0b05a1ed2d [cxx-interop] Hook libcxxshim modulemap/header to sdk-overlay (#62475)
The modulemap and header files target was added to `ALL`, which works
when invoking the build thru the `build-script`, but make some test fail
when doing a manual configuration and later doing
`check-swift-validation` or similar, because the modulemap/header were
not copied into the build directory.

Follow a pattern similar to the one in GlibC (which this file seemed to
be prepared for, since the `libcxxshim_modulemap_target_list` variable
was already there), create a global target for all the modulemap/headers
of each SDK and add that global target as a dependency of sdk-overlay
(which is a dependency of check-swift-validation and others).
2022-12-09 10:15:18 -08: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
3e1a6dcea4 [cxx-interop] Add CxxConvertibleToCollection protocol
Since we don't automatically conform C++ non-random-access collections to `Swift.Sequence` anymore for performance reasons, we need an alternative way to access the elements of a C++ sequence from Swift.

This allows explicitly converting a C++ sequence to a Swift Array/Set.
2022-11-24 14:04:21 +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
da02016ec5 [cxx-interop] Specify Iterator explicitly for CxxRandomAccessCollection
This helps to avoid ambiguity errors when manually conforming a type to `CxxRandomAccessCollection` protocol.
2022-11-07 17:46:49 -08:00
Egor Zhdan
5359c13a83 [cxx-interop] Add explicit typealiases to CxxRandomAccessCollection
This will help us to auto-generate conformances to `CxxRandomAccessCollection`, since synthesized conformances must have all of their witnesses explicitly provided.
2022-11-02 13:51:34 -07: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
3daa8753c7 Merge pull request #61554 from apple/egorzhdan/cxx-random-access-collection
[cxx-interop] Add `CxxRandomAccessCollection` protocol
2022-10-19 10:57:21 +01:00
Egor Zhdan
6bb0b8e299 [cxx-interop] Optimize default impls of CxxRandomAccessCollection 2022-10-18 13:48:46 +01:00
Egor Zhdan
bc16131166 [cxx-interop] Add CxxRandomAccessCollection protocol
This helps to bridge C++ random access collections, such as `std::vector` and `std::string`, to Swift by conforming them to `Swift.RandomAccessCollection`
2022-10-14 17:39:37 +01:00
Egor Zhdan
10b705784b [cxx-interop] Optimize CxxIterator and CxxSequence
1. Instead of storing the C++ collection within `class CxxIterator`, store a boxed C++ collection and make `CxxIterator` a struct. This enables a number of Swift optimizations for iterators, while preserving the guarantee that the C++ collection is not copied or moved in memory (which would invalidate iterators).
2. Make `sequence` parameter shared. This avoids a second copy of the C++ collection when initializing `CxxIterator`.

Credits to Alex Lorenz for this idea!
2022-10-14 16:52:55 +01:00
Egor Zhdan
9b8f7e4b1b Merge pull request #61484 from apple/egorzhdan/cxx-sequence-inlinable
[cxx-interop] Make some CxxSequence methods inlinable
2022-10-13 17:57:26 +01:00
Saleem Abdulrasool
ee996da32d stdlib: enable cxxshim for Windows
This is used by the test suite and needs to be enabled for Windows to
enable parts of the test suite.
2022-10-08 14:19:22 -07:00
Egor Zhdan
52ea0d0850 [cxx-interop] Make some CxxSequence methods inlinable 2022-10-07 11:54:53 +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
b9f9eac7d9 [cxx-interop] NFC: Remove unused source file
`libcxxshim.swift` is not referenced from CMakeLists, and is not needed because libcxxshim is a pure C++ module.
2022-08-22 16:24:08 +01:00
Egor Zhdan
440de8eb87 [cxx-interop] NFC: Add explicit override to associated types 2022-08-08 14:54:28 +01:00
Egor Zhdan
2c43038a29 [cxx-interop] Explicitly require CxxSequence's iterator to be CxxIterator
This is needed for automatic synthesis of conformances to `CxxSequence` protocol.
It also makes typechecker errors easier to understand when they happen.
2022-07-31 22:34:41 +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
zoecarver
8cf4d3b983 [cxx-interop] Disable cxxshim on windows until we can figure out cmakeconfig. 2022-07-05 20:03:45 -07:00
zoecarver
3805828ad0 [cxx-interop] Add CxxShim library; move __swift_interopStaticCast into it. 2022-07-05 15:52:51 -07: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