Commit Graph

84 Commits

Author SHA1 Message Date
Arnold Schwaighofer
58d3cf3063 Test Interop/Cxx/stdlib/overlay/custom-collection.swift fails on bots
Disable until folks can take a look

rdar://143950805
2025-02-04 10:53:14 -08:00
Egor Zhdan
07c4a8fe57 Merge pull request #77071 from swiftlang/egorzhdan/explicit-operator-bool
[cxx-interop] Add test for `explicit operator bool`
2024-10-17 18:23:20 +02:00
Egor Zhdan
c2cfd7500a [cxx-interop] Add test for explicit operator bool
rdar://137879510
2024-10-17 13:51:58 +01:00
Egor Zhdan
e645800edf [cxx-interop] Add test for iterator conformances with nested typedefs
Follow-up to 34f6cd3f.
2024-10-17 13:25:42 +01:00
Egor Zhdan
34f6cd3f1a [cxx-interop] Conform to UnsafeCxxContiguousIterator based on iterator_concept nested type
3a200dee has a logic bug where we tried to conform C++ iterator types to `UnsafeCxxContiguousIterator` protocol based on their nested type called `iterator_category`. The C++20 standard says we should rely on `iterator_concept` instead.

https://en.cppreference.com/w/cpp/iterator/iterator_tags#Iterator_concept

Despite what the name suggests, we are not actually using C++ concepts in this change.

rdar://137877849
2024-10-16 19:47:46 +01:00
Egor Zhdan
3a200deee9 [cxx-interop] Add UnsafeCxxContiguousIterator & UnsafeCxxMutableContiguousIterator protocols
This adds a pair of Swift protocols that represents C++ iterator types conforming to `std::contiguous_iterator_tag` requirements. These are random access iterators that guarantee that the values are stored in consequent memory addresses.

This will be used to optimize usage of C++ containers such as `std::vector` from Swift, for instance, by providing an overload of `withContiguousStorageIfAvailable` for contiguous containers.

rdar://137877849
2024-10-15 14:30:09 +01:00
Egor Zhdan
0ab681514b [cxx-interop] Add CxxMutableRandomAccessCollection protocol
This conforms mutable C++ container types, such as `std::vector`, to `MutableCollection` via a new overlay protocol `CxxMutableRandomAccessCollection`.

rdar://134531554
2024-08-28 12:43:42 +01:00
Alex Lorenz
983fb8025a [cxx-interop][serialization] resolve x-refs to instantiated/synthesized C++ iterator conformance operators
These x-refs might not be resolvable using regular lookup from the 'std' module as they could be instantiated/synthesized
by the clang importer. Augment the lookup logic in that case to try clang importer lookup logic that is used during
the conformance to the C++ iterator protocol.
2024-06-20 14:51:34 -07:00
Egor Zhdan
78b9de1391 [cxx-interop] Run tests with swift-6 compat mode 2024-02-23 16:24:14 +00:00
Egor Zhdan
0d5a6fa406 [cxx-interop] Cleanup tests: merge two std::string tests 2024-01-24 17:33:52 +00:00
Egor Zhdan
ef702ad6f6 [cxx-interop] Split some modules in tests
This cleans up the C++ iteration tests and makes sure that we test the setup where a C++ iterator and a C++ collection are defined in different Clang modules.
2024-01-16 17:54:23 +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
232259ce99 [cxx-interop] Enable an overlay test on Linux
Resolves https://github.com/apple/swift/issues/66363
2023-10-24 13:16:41 +01:00
Crazy凡
02a9259dda [cxx-interop] Provide similar support for std::u32string as std::string and std::u16string. 2023-10-10 16:01:15 +08: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
f0be52accd [cxx-interop] Add conversion to Bool for types that define operator bool()
C++ `operator bool()` is currently imported into Swift as `__convertToBool()`, which shouldn't be used by clients directly.

This adds a new protocol into the C++ stdlib overlay: `CxxConvertibleToBool`, along with an intitializer for `Swift.Bool` taking an instance of `CxxConvertibleToBool`.

rdar://115074954
2023-09-18 14:54:45 +01:00
Egor Zhdan
3723ff1401 [cxx-interop] Add UnsafeCxxMutableRandomAccessIterator protocol
This will be used to provide a safe overload of `std::vector::erase` in Swift.

`std::vector::erase` is not currently imported into Swift because it returns a C++ iterator.

rdar://113704853
2023-08-10 19:58:51 +01:00
Alex Lorenz
3569c5d6f2 [cxx-interop] Try more stdlib and overlay tests on Windows 2023-08-09 12:59:27 -07: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
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
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
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
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
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
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
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
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
d5f5d4bd55 [cxx-interop] Make CxxConvertibleToCollection.forEach public
This allows clients to iterate over a C++ container which doesn't provide random access with a `.forEach` call.

rdar://107995558
2023-04-18 14:43:43 +01:00
Puyan Lotfi
6180387a05 [cxx-interop] Adding std.string initializer for UnsafePointer<CChar>?
Currently without an initializer for the unsafe char pointer type swiftc
hits an assert around not being able to handle conversions of unsafe
pointers with Any type. This patch adds the ability to convert to a
std::string.

This is to address issue https://github.com/apple/swift/issues/61218
2023-04-12 22:03:57 -04:00
Egor Zhdan
242a432919 [cxx-interop] Conform std::string to Hashable
rdar://107709149
2023-04-06 16:15:40 +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
62cb187a3b [cxx-interop] Add operators for comparing and concatenating std::strings
The original C++ operators are not currently imported into Swift because they are defined as a non-member templated functions.

This change adds the operators as Swift extension functions. This also adds an `Equatable` conformance for `std::string`.

rdar://107017882
2023-03-23 18:00:50 +00:00
Egor Zhdan
ed0cee9ce6 [cxx-interop] Conform std::u16string to CustomStringConvertible and CustomDebugStringConvertible
This conforms `std::u16string` to two protocols that `std::string` already conforms to.

rdar://106037638
2023-02-28 19:02:45 +00:00
Egor Zhdan
9d9e015a9f Merge pull request #63833 from apple/egorzhdan/cxx-u16string-reland
Revert "Revert "[cxx-interop] Add conversions between `std::u16string` and `Swift.String`""
2023-02-24 19:59:47 +00:00
Egor Zhdan
9818312da4 Revert "Revert "[cxx-interop] Add conversions between std::u16string and Swift.String""
This re-lands https://github.com/apple/swift/pull/61695 since the linker error on CentOS has been fixed in https://github.com/apple/swift/pull/63058.

This reverts commit e97b1c8e
2023-02-22 18:31:36 +00: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
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
34c2b0e50d [cxx-interop] Make String.init(std.string) unlabeled
`Swift.String` can be initialized from any other type with an unlabeled initializer, which is either going to use the `CustomStringConvertible` conformance, or reflection. We would like clients to use the most suitable initializer, which is the one that takes `std.string` as a parameter. For instance, that allows us to attach a doc comment to the initializer.

This change makes the initializer unlabeled to make sure it is chosed by overload resolution when a client invokes `String(myCxxString)`.
2023-02-02 12:47:08 +00:00
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
Christopher White
70a7a88027 Make std.string conform to CustomDebugStringConvertible 2023-01-06 21:28:33 +00:00
Egor Zhdan
ec2a645c5e [cxx-interop] More test coverage for Collection conformances
This adds a few tests for the default implementation of subscript, slicing, map/reduce.
2023-01-06 19:22:13 +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
623bfb1223 [cxx-interop] Fix typo in test/Interop/Cxx/stdlib/overlay/Inputs/custom-iterator.h 2022-12-22 19:32:29 +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