Commit Graph

382 Commits

Author SHA1 Message Date
Egor Zhdan
aecc10e90b Merge pull request #67775 from apple/egorzhdan/clang-stmt-iterator
[cxx-interop] Fix SwiftCompilerSources hosttools build
2023-08-08 14:11:15 +01:00
Egor Zhdan
c621d13711 [cxx-interop] Fix SwiftCompilerSources hosttools build
This fixes a compiler error when building SwiftCompilerSources in hosttools mode with a recent Xcode.

```
<unknown>:0: error: calling a private constructor of class 'clang::StmtIterator'
swift/llvm-project/clang/include/clang/AST/StmtIterator.h:137:3: note: declared private here
  StmtIterator(const StmtIteratorBase &RHS)
  ^
```

rdar://113514872
2023-08-07 20:11:29 +01:00
Alex Lorenz
b34b68b8d4 Mark test as executable 2023-08-07 11:17:20 -07:00
Egor Zhdan
4acf9c8cf4 [cxx-interop] Add CxxVector protocol
This makes it possible to initialize `std::vector` from a Swift Sequence. This also conforms C++ vectors to `ExpressibleByArrayLiteral`, making it possible, for instance, to pass a Swift array to a C++ function that takes a vector of strings as a parameter.

rdar://104826995
2023-08-07 17:12:12 +01:00
Egor Zhdan
e5784dbd1b [cxx-interop] Add test for mutation of nested std::map
rdar://111888891
2023-08-04 18:55:11 +01:00
Alex Lorenz
8449b21d62 [cxx-interop][windows] additional fix for indirect return in methods for MSVC ABI
Fixes https://github.com/apple/swift/pull/67685
2023-08-03 15:07:22 -07:00
Egor Zhdan
399e5b469d [cxx-interop] Add test for mutable std.optional.pointee
See https://github.com/apple/swift/pull/67648
2023-08-03 14:48:11 +01:00
Egor Zhdan
6caaa77efc [cxx-interop] Allow mutating std::map from Swift
This adds a setter to the `CxxDictionary` subscript which makes it possible to mutate `std::map` and `std::unordered_map` from Swift.

rdar://105399019
2023-08-02 16:44:30 +01:00
Egor Zhdan
6902041e34 [cxx-interop] Force creating a memberwise init for std::pair
In libc++, `pair()` and `pair(_T1 const& __t1, _T2 const& __t2)` are templated with `enable_if`, so these initializers are not imported into Swift.

There should be a way to call `std.pair.init` from Swift, so this change makes sure Swift synthesizes a memberwise initializer for `std.pair`.

rdar://113135110
2023-08-01 18:59:41 +01:00
Alex Lorenz
cf943661ca [cxx-interop] test, enable more tests on windows
These tests require the C++ standard library
2023-07-28 16:02:43 -07:00
Alex Lorenz
a219cfa632 Merge pull request #67557 from hyp/eng/64457
[cxx-interop][linux] modularize the bits part of libstdc++
2023-07-27 20:48:11 -07:00
Alex Lorenz
c215abdd8c [cxx-interop][linux] modularize the bits part of libstdc++
Fixes https://github.com/apple/swift/issues/64457
2023-07-27 10:20:28 -07:00
Egor Zhdan
b860fddd52 [cxx-interop] Fix assertion failure in IRGen with mutable dereference operators
I discovered this when experimenting with `std::map::iterator`, which has a const overload of `operator*` that returns a non-const reference, and does not have a const overload of `operator*`.

rdar://112471779
2023-07-27 15:54:09 +01:00
Egor Zhdan
276a232ea8 Merge pull request #67536 from apple/egorzhdan/unsafe-cxx-mutable-input-iterator
[cxx-interop] Add `UnsafeCxxMutableInputIterator` protocol
2023-07-26 22:15:57 +01:00
Egor Zhdan
5dfefe6613 [cxx-interop] Enable a test on Linux 2023-07-26 18:45:00 +01:00
Egor Zhdan
8d7d0efe13 [cxx-interop] Add UnsafeCxxMutableInputIterator protocol
This is an inheritor of the existing `UnsafeCxxInputIterator` protocol, with the only difference being the ability to mutate `var pointee` via a non-const `operator*()`.

This is needed to support mutable subscripts for `std::map` via `CxxDictionary`.

rdar://105399019
2023-07-26 18:20:49 +01:00
Egor Zhdan
25b1986995 Merge pull request #67508 from apple/egorzhdan/map-set-erase-test
[cxx-interop] Add tests for `std::map::erase` and `std::set::erase`
2023-07-25 18:02:25 +01:00
Egor Zhdan
be34674915 Merge pull request #67375 from apple/egorzhdan/mutable-var-pointee
[cxx-interop] Import mutating dereference operators
2023-07-25 16:20:19 +01:00
Egor Zhdan
865853a128 [cxx-interop] Add tests for std::map::erase and std::set::erase
We're going to need the ability to remove an element from a map to support mutation in `CxxDictionary.subscript`.

rdar://105399019
2023-07-25 15:30:13 +01:00
Egor Zhdan
43e6e97c1a Merge pull request #67482 from apple/egorzhdan/vector-of-string
[cxx-interop] Import iterator types that are not typedef-ed
2023-07-24 17:29:26 +01:00
Egor Zhdan
af014c02b3 [cxx-interop] Import iterator types that are not typedef-ed
This prevented `std::vector<std::string>` from being auto-conformed to `CxxRandomAccessCollection`.

If an iterator type is templated, and does not have an explicit instantiation via a typedef or a using-decl, its specialization will not have an owning Clang module. Make sure we treat it as a part of the Clang module that owns the template decl.

rdar://112762768 / resolves https://github.com/apple/swift/issues/67410
2023-07-24 11:25:15 +01:00
Zoe Carver
7db5fe11ad Merge pull request #67296 from zoecarver/only-methods-on-self-contained-types-are-unsafe
[cxx-interop] Only mark projections of self-contained types as unsafe.
2023-07-19 14:18:46 -07:00
zoecarver
e670a0e83d [cxx-interop][nfc] post-rebase fallout for a few tests. 2023-07-19 11:38:43 -07:00
Egor Zhdan
8832d27e98 [cxx-interop] Import mutating dereference operators
C++ `T& operator*()` is mapped to a Swift computed property `var pointee: T`.

Previously `var pointee` only had a getter, after this change it will also have a setter if the C++ type declares an overload of `operator*` that returns a mutable reference.

rdar://112471779
2023-07-19 16:12:55 +01:00
Egor Zhdan
4ca1eeb141 [cxx-interop] Make CxxPair's fields mutable
This will be needed to support mutable C++ iterators, e.g. `std::map::iterator`.
2023-07-19 16:05:28 +01:00
zoecarver
806956d80f [cxx-interop] always mark begin and end methods as unsafe (to help automatic rac conformance); update tests accordingly. 2023-07-18 17:42:56 -07:00
zoecarver
fd7b39e736 [cxx-interop] Mark *all* iterators as unsafe; update tests accordingly; fix a few remaining tests that use raw pointers. 2023-07-18 17:42:56 -07:00
zoecarver
a6a0f637ab [nfc][cxx-interop] Fix a few tests. 2023-07-18 17:42:54 -07:00
Egor Zhdan
bc56ddc2bb [cxx-interop] Handle inherited templated operators during auto-conformance
This fixes the automatic `std::unordered_map` conformance to CxxDictionary on Linux. Previously `std::unordered_map::const_iterator` was not auto-conformed to UnsafeCxxInputIterator because its `operator==` is defined on a templated base class of `const_iterator`.

rdar://105220600
2023-07-17 21:10:32 +01:00
zoecarver
c58c661c87 [cxx-interop][nfc] Disable test for strstr on Linux. 2023-06-30 10:06:22 -07:00
zoecarver
6233a65f7e [cxx-interop][nfc] Disable test for div. 2023-06-28 17:19:44 -07:00
zoecarver
50ce8438c8 [cxx-interop] Don't import exit from Darwin module, use the one from _SwiftConcurrency.h. 2023-06-28 10:16:36 -07:00
zoecarver
173329ee96 [cxx-interop] only ban strstr, sin, cos, and exit on darwin where the conflict with declarations in the Darwin module; improve the test. 2023-06-28 09:40:09 -07:00
zoecarver
744783ac58 [cxx-interop][nfc] remove print-swiftconcurrencyshims-interface.swift, it's not longer relevant. 2023-06-27 22:20:54 -07:00
zoecarver
42bc1670a0 [cxx-interop] Re-work control flow when checking for duplicate functions.
Also add back `string.h` as a valid header and ban imports from `_SwiftConcurrencyShims`.
2023-06-27 22:19:22 -07:00
zoecarver
26e30565fa [cxx-interop] Fix ambiguous 'exit' (same solution as above). 2023-06-23 12:33:23 -07:00
zoecarver
067abeb480 [cxx-interop] Pull over fix from 8e7766b and apply to strstr, sin, and cos. 2023-06-23 12:03:14 -07:00
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
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
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
dab1cb5e12 [cxx-interop] Simplify a std::string test
`std::string` can be accessed directly from Swift, there is no need to go through a using-decl.
2023-06-13 17:22:21 +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
b459fb5fc2 [cxx-interop] Avoid linker errors when calling a defaulted constructor
When a default constructor is declared, but does not have a body because it is defaulted (`= default;`), Swift did not emit the IR for it. This was causing linker error for types such as `std::map` in libstdc++ when someone tried to initialize such types from Swift.

rdar://110638499 / resolves https://github.com/apple/swift/issues/61412
2023-06-12 20:06:49 +01:00
Egor Zhdan
df29fc33a8 [cxx-interop] Make a test work properly with optimizations
The compiler might optimize away the first copy, so just make sure that no copies are happening during the actual initialization of Array.

rdar://110422053
2023-06-08 23:53:34 +01:00
Egor Zhdan
7193b70c66 Merge pull request #66271 from apple/egorzhdan/borrow-cxx-convertible
[cxx-interop] Initializing a Swift.Array from CxxConvertibleToCollection should not copy the collection
2023-06-06 18:36:59 +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
f2771584df [cxx-interop] Do not treat std::pair<UnsafeType, T> as safe
rdar://109529750
2023-06-01 14:00:03 +01:00
Egor Zhdan
ebcab6c55f Merge pull request #65943 from apple/egorzhdan/std-vector-ubi-test
[cxx-interop] Do not run a test in C++20 mode
2023-05-16 15:29:12 +01:00
Egor Zhdan
594e63d8a3 [cxx-interop] Do not run a test in C++20 mode
This test is failing on UBI platform in C++20 mode:

```
Command Output (stderr):
--
<unknown>:0: warning: the '-enable-experimental-cxx-interop' flag is deprecated; please pass '-cxx-interoperability-mode=' instead
<unknown>:0: note: Swift will maintain source compatibility for imported APIs based on the selected compatibility mode, so updating the Swift compiler will not change how APIs are imported
<unknown>:0: warning: the '-enable-experimental-cxx-interop' flag is deprecated; please pass '-cxx-interoperability-mode=' instead
<unknown>:0: note: Swift will maintain source compatibility for imported APIs based on the selected compatibility mode, so updating the Swift compiler will not change how APIs are imported
/home/build-user/swift/test/Interop/Cxx/stdlib/use-std-vector.swift:41:14: error: for-in loop requires 'Vector' (aka 'std.vector<Int32, allocator<Int32>>') to conform to 'Sequence'
    for e in v {
             ^
/home/build-user/swift/test/Interop/Cxx/stdlib/use-std-vector.swift:52:15: error: value of type 'Vector' (aka 'std.vector<Int32, allocator<Int32>>') has no member 'map'
    let a = v.map { $0 + 5 }
            ~ ^~~

```

This makes sure we don't run this test in C++20 mode until the conformance to `CxxSequence` is synthesized correctly on UBI.

rdar://109366764
2023-05-16 12:27:25 +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