Commit Graph

13 Commits

Author SHA1 Message Date
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
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
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
5c99afcd9b [tests] Fix platform specific tests and reverse interop tests. 2023-02-20 17:58:10 -08:00
zoecarver
985db63e2b [tests] Update tests based on new template name importing rules. 2023-02-20 17:58:10 -08: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
904938a6a4 [cxx-interop] Avoid side-effects in member lookup when conforming types to CxxSequence
This became an issue in rebranch: multiple interop tests started failing (e.g. `Interop/Cxx/foreign-reference/pod.swift`).
The problem is not specific to rebranch though.

rdar://102151836
2022-11-15 23:00:20 +00:00
Egor Zhdan
0efd20d016 [cxx-interop] Synthesize conformances to UnsafeCxxInputIterator
This makes ClangImporter automatically conform C++ sequence types to `Cxx.UnsafeCxxInputIterator` protocol.

We consider a C++ type to be a random access iterator type if conforms to `UnsafeCxxInputIterator`, and additionally defines `operator-` and `operator+=`.
2022-10-24 17:18:42 +01:00
Egor Zhdan
6a88f75072 [cxx-interop] Implicitly import Cxx module
This lifts the requirement for the user to explicitly add `import Cxx` in Swift code that relies on protocols from the `Cxx` module, such as `CxxSequence`.
2022-10-19 15:00:50 +01:00
Egor Zhdan
616edf76a8 [cxx-interop] Fix circular reference errors during conformance synthesis
These errors were sometimes produced when synthesizing conformance to `UnsafeCxxInputIterator`:
```
<unknown>:0: error: circular reference
<unknown>:0: note: through reference here
<unknown>:0: note: through reference here
```

This happened because `NominalTypeDecl::lookupDirect` attempts to deserialize Swift extensions of the type, which might belong to the module which has a dependency to the module that is currently being imported, which leads to deserialization errors.

This change makes sure we don't call `NominalTypeDecl::lookupDirect` when synthesizing conformances for C++ types.
2022-08-03 20:47:10 +01:00
Egor Zhdan
88167d71a5 [cxx-interop] Allow iterators with out-of-class operator==
Previosly we didn't detect `func ==` that was declared out-of-class when synthesizing conformaces to `UnsafeCxxInputIterator`. Now we do.

rdar://96235368
2022-07-20 17:23:09 +01:00
Egor Zhdan
d85d2e9e75 [cxx-interop] Synthesize conformances to UnsafeCxxInputIterator
This teaches ClangImporter to synthesize conformances of C++ iterator types to `UnsafeCxxInputIterator` protocol from the `Cxx` module.

We consider a C++ type to be an iterator if it defines a subtype (usually a typedef or a using decl) called `iterator_category` that inherits from `std::input_iterator_tag`.

rdar://96235368
2022-07-20 11:44:25 +01:00