Commit Graph

206 Commits

Author SHA1 Message Date
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
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
Egor Zhdan
6754c3cf82 [cxx-interop] Add CxxSequence protocol to the stdlib overlay
This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection safe and Swifty by adding conformances to `Swift.Sequence`, `Swift.Collection`, etc.

This is not meant to be a final design.
2022-06-27 20:40:12 +01:00
Egor Zhdan
0ced5ec070 [cxx-interop] Re-enable C++ stdlib overlay on Linux 2022-06-14 23:28:37 +01:00
Egor Zhdan
a605ec7138 [cxx-interop] Fix libstdc++ build failure on CentOS 7
CentOS 7 is shipped with an outdated version of libstdc++, which does not include `codecvt` stdlib header.
Let's wrap the `#include` with `#if __has_include`.
2022-06-09 12:48:35 +01:00
Alex Hoppen
95481a54bc Revert "Merge pull request #58983 from apple/egorzhdan/cxx-overlay-linux"
This reverts commit 9e69e4dd76, reversing
changes made to 59bb4cb11a.
2022-06-08 08:35:04 +02:00
Egor Zhdan
9e69e4dd76 Merge pull request #58983 from apple/egorzhdan/cxx-overlay-linux
[cxx-interop] Enable C++ stdlib overlay on Linux
2022-06-07 20:53:28 +01:00
Egor Zhdan
737507ea93 [cxx-interop] Fix a potential dangling pointer in String(cxxString:)
`cxxString` might get deallocated immediately after `c_str()` call, which would mean that the pointer passed to `String(cString:)` points to invalid memory.

We haven't actually seen this, but let's preemptively add an explicit `withExtendedLifetime` call to avoid running into this in the future.
2022-06-07 16:58:52 +01:00
Egor Zhdan
bcc07cc4eb [cxx-interop] Enable C++ stdlib overlay on Linux 2022-06-07 16:38:28 +01:00
Egor Zhdan
ff8464fd2a [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.
2022-05-16 13:30:35 +01:00
Egor Zhdan
1ef02efed1 Merge pull request #58843 from apple/egorzhdan/libstdcxx-vfs
[cxx-interop] Use VFS to inject modulemap into libstdc++ installation
2022-05-14 00:40:16 +01:00
Egor Zhdan
20650eab33 [cxx-interop] Add cassert header to libstdc++ modulemap
Previously this header was sometimes getting hijacked by the first header to include it. This caused Swift compiler failures on Linux when using any LLVM header that uses `assert`.

Now `cassert` is referenced directly from a modulemap, which fixes the issue.
2022-05-13 22:21:40 +01:00