Commit Graph

12 Commits

Author SHA1 Message Date
Egor Zhdan
78b9de1391 [cxx-interop] Run tests with swift-6 compat mode 2024-02-23 16:24:14 +00:00
Egor Zhdan
bc4cffd699 [cxx-interop] Test more of stdlib with the upcoming compat version 2023-11-28 20:49:00 +00:00
Egor Zhdan
041005af7c [cxx-interop] Use more correct type names in C++ template parameters
When importing a C++ class template instantiation, Swift translates the template parameter type names from C++ into their Swift equivalent.

For instance, `basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>` gets imported as `basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>`: `wchar_t` is imported as `CWideChar`, which is a typealias for `Scalar` on most platforms including Darwin. Notice that Swift goes through the `CWideChar` typealias on the specific platform. Another instantiation `basic_string<uint32_t, char_traits<uint32_t>, allocator<uint32_t>>` also gets imported as `basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>`: `uint32_t` is also imported as `Scalar`. This is problematic because we have two distinct C++ types that have the same name in Swift.

This change makes sure Swift doesn't go through typealiases when emitting names of template parameters, so `wchar_t` would now get printed as `CWideChar`, `int` would get printed as `CInt`, etc.

This also encourages clients to use the correct type (`CInt`, `CWideChar`, etc) instead of relying on platform-specific typealiases.

rdar://115673622
2023-10-09 14:57:10 +01: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
zoecarver
985db63e2b [tests] Update tests based on new template name importing rules. 2023-02-20 17:58:10 -08:00
Egor Zhdan
bf341ac51c [cxx-interop] Require begin() and end() to be non-mutating for auto-conformed container types
This makes sure that Swift is only auto-conforming C++ container types to `CxxSequence`/`CxxConvertibleToCollection` if they expose non-mutating `begin()` and `end()` methods.

We might want to make `begin()` and `end()` non-mutating in the near future to enable performance optimizations. This change makes sure that client code relying on the automatic conformances doesn't suddenly stop compiling if/when the mutability requirement on the protocol function changes.
2023-02-07 19:08:25 +00:00
Egor Zhdan
cb562d26b8 [cxx-interop] Synthesize conformances to CxxConvertibleToCollection
This extends the existing auto-conformance mechanism to synthesize the conformances to `CxxConvertibleToCollection` protocol for C++ sequence types.

This means that the developer can now call `Array(myCxxSequence)` or `Set(myCxxSequence)` without adding any extensions manually.
2022-11-29 14:28:17 +00:00
Egor Zhdan
40650baf7f [cxx-interop] Disable auto-conformance to CxxSequence for non-random-access collections
Iterating over a `CxxSequence` that is not a `CxxRandomAccessCollection` triggers a copy of the C++ collection. Let's disable the automatic conformances until we find a more efficient solution.

This means that for now developers won't be able to iterate over a `std::set` or `std::list` with a Swift for-in loop. I will submit a separate patch with an alternative solution for such types.

C++ random access collections, such as `std::vector` or `std::string`, are not affected.
2022-11-15 14:42:46 +00:00
Egor Zhdan
3cf84214b8 [cxx-interop] Synthesize conformances to CxxRandomAccessCollection
This makes ClangImporter automatically conform C++ collection types to `Cxx.CxxRandomAccessCollection` protocol.

We consider a C++ sequence type to be a random access collection type its iterator conforms to `UnsafeCxxRandomAccessIterator`.
2022-11-03 12:01:18 -07: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
c53b8147a3 [cxx-interop] Correctly check iterator conformances
To determine whether to conform a C++ type to `CxxSequence` protocol automatically, ClangImporter checks if the corresponding iterator type conforms to `UnsafeCxxInputIterator`.

This logic had false-positives, e.g. `Optional<OpaquePointer>` was treated as if it conforms to `UnsafeCxxInputIterator` while it actually doesn't. This happened because `lookupConformance` returned a conformance with a conditional requirement (`Wrapped : UnsafeCxxInputIterator`) that is not satisfied for `OpaquePointer`.

rdar://100265664
2022-10-13 18:13:31 +01:00
Egor Zhdan
e3a321721d [cxx-interop] Synthesize conformances to CxxSequence
This makes ClangImporter automatically conform C++ sequence types to `Cxx.CxxSequence` protocol.

We consider a C++ type to be a sequence type if it defines `begin()` & `end()` methods that return iterators of the same type which conforms to `UnsafeCxxInputIterator`.
2022-08-17 16:06:37 +01:00