Commit Graph

25 Commits

Author SHA1 Message Date
Egor Zhdan
19c7884ec5 [cxx-interop] Allow creating Dictionary from std::map
This adds an initializer to `Swift.Dictionary` that takes an instance of `std::map` or `std::unordered_map`.

rdar://155050682
2025-07-05 21:54:11 +01:00
Evan Wilde
3a48d6e95e FreeBSD: Enable a few more C++ interop tests
Some of these tests were marked as unsupported since they were only
checking for macOS and Linux. They seem to be passing on FreeBSD as
well, so enabling them here.
2025-06-18 16:16:34 -07:00
Crazy凡
b4531a6ae8 [cxx-interop]: Refactor CxxDictionary protocols to use generics instead of Self.
- Add support for merging `std::map` and `std::unordered_map` with each other seamlessly.
- Add support for typed throws
2025-04-02 09:14:06 +08:00
Crazy凡
c6b10dffd7 "Enhance CxxDictionary with removeValue(forKey:), subscript(key:default:), mergemerging and init(grouping:by:)" 2024-11-28 13:11:31 +08:00
Egor Zhdan
535a9412df [cxx-interop] Conform std::map to ExpressibleByDictionaryLiteral
This adds an automatic conformance for `std::map` and `std::unordered_map` to Swift's `ExpressibleByDictionaryLiteral` protocol.

This makes it possible to pass a dictionary literal as an argument to a function that takes a `std::map` as parameter.

rdar://137126474
2024-10-03 13:33:40 +01:00
Egor Zhdan
9fe13ece49 [cxx-interop] Allow initializing std::map from Swift Dictionary
This adds initializers for `std::map` and `std::unordered_map` that take a Swift dictionary as a single parameter.

rdar://133691563
2024-08-14 13:32:30 +01:00
Egor Zhdan
78b9de1391 [cxx-interop] Run tests with swift-6 compat mode 2024-02-23 16:24:14 +00:00
Egor Zhdan
c26fe86754 [cxx-interop] Make automatic conformances work with the bridging header
For C++ types that are defined in the bridging header, or are `#include`-d from the bridging header, we did not generate the automatic conformances to `CxxSequence`, `CxxRandomAccessCollection` protocols.

To check whether we should try to conform a C++ type to those protocols, the compiler checks for the presence of `requires cplusplus` in the module declaration in a modulemap file. This check is there to prevent us from accidentally pulling in `Cxx`/`CxxStdlib` modules when a client is importing a C library.

This change makes sure those conformances are generated.

rdar://121927459
2024-01-31 13:56:18 +00:00
Egor Zhdan
7218a831a2 [cxx-interop] Test stdlib with the upcoming compat version 2023-11-22 20:12:08 +00:00
mohanadkandil
d58943a508 Add filter method for ordered and unordered map (#67501)
Co-authored-by: mkandil <mkandil@synapse-analytics.io>
2023-10-05 19:13:34 +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
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
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
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
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
8f58eafbd3 [cxx-interop] Make sure interop does not trigger TBD validation errors
This disables TBD validation when C++ interop is enabled, unless an explicit `-validate-tbd-against-ir=` flag was passed.

rdar://83405989 / https://github.com/apple/swift/issues/56458
2023-04-05 16:06:09 +01:00
Egor Zhdan
63f90fe082 [cxx-interop] Partially re-enable a test on Linux
`std::map` conformance to `CxxDictionary` is synthesized properly, only the `std::unordered_map` conformance is problematic.
Let's make sure we test the `std::map` conformance with libstdc++ on Linux.
2023-02-14 15:54:13 +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
1f920d5e96 [cxx-interop] Instantiate templated operator== for iterator types
C++ iterator types are often templated, and sometimes declare `operator==` as a non-member templated function. In libc++, an example of this is `__wrap_iter` which is used as an iterator type for `std::vector` and `std::string`.

We don't currently import templated non-member operators into Swift, however, we still want to support common C++ iterator patterns.

This change adds logic to instantiate templated non-member `operator==` for types that define `iterator_category` and are therefore likely to be valid iterator types.

rdar://97915515
2023-01-04 11:56:36 +00:00
Egor Zhdan
bbf8f0d5bc [cxx-interop] Fix lookup failure for operators in templated classes
If an operator is declared as a method of a templated class, we were failing to look it up during auto-conformance to `UnsafeCxxInputIterator`.

This fixes `Interop/Cxx/stdlib/use-std-map.swift` on Ubuntu.

rdar://102420290
2022-12-13 17:05:38 +00:00
Egor Zhdan
7203a05479 [cxx-interop] Replace std module name with the new spelling CxxStdlib
This is the second step in renaming the C++ stdlib module `std` into `CxxStdlib`.

See https://github.com/apple/swift/pull/61099.
2022-11-29 17:33:35 +00:00
Allan Shortlidge
31d52b064c Tests: Temporarily disable Interop/Cxx/stdlib/use-std-map.swift. 2022-11-16 13:20:16 -08:00
zoecarver
001033763a Fix tests. 2022-10-14 13:33:21 -07:00
zoecarver
492f2aaff2 [cxx-interop] Fix friend operators that come from class template specializations. 2022-10-14 13:33:06 -07:00
Egor Zhdan
e28605c0c9 [cxx-interop] Allow using std::map subscript
This fixes an error that occurred when trying to use the subscript on an instance `std::map`:
```
error: cannot assign through subscript: 'map' is immutable
```
This was happening even with a mutable `std::map` instance.

`std::map::operator[]` has two overloads:
* `T& operator[]( const Key& key )`
* `T& operator[]( Key&& key )`

The second one is imported with an `inout` parameter, and we picked it as an implementation of the subscript getter because it was the last of the two overloads to get imported.

Swift does not allow subscripts with `inout` parameters. This is checked at the AST level, and those checks do not run for synthesized Swift code. This caused Swift to produce a surprising error which actually indicated that the argument of the subscript, not the instance itself, must be mutable.

rdar://100529571
2022-10-03 11:18:56 +01:00