Commit Graph

44 Commits

Author SHA1 Message Date
susmonteiro
b7c5a47898 [cxx-interop] Fix crash when operator doesn't name parameter in header 2025-01-28 16:26:27 +00:00
Egor Zhdan
7a5154f4a9 [cxx-interop] Add a test for static operator()
Extracted from https://github.com/swiftlang/swift/pull/76235.

Co-authored-by: Alex Lorenz <arphaman@gmail.com>
2024-09-04 16:51:58 +01:00
Alex Lorenz
2039b8d254 [cxx-interop] import static operator call from C++23 as member callAsFunction functions in Swift to preserve source compatibility 2024-06-12 09:11:15 -07:00
Egor Zhdan
ae27e884d5 [cxx-interop] Do not crash when calling a subscript with unnamed parameter
This fixes a crash in SILGen when calling a C++ subscript that has an unnamed parameter from Swift.

The parameters from a C++ `operator[]` get carried over to the synthesized Swift subscript. If the Swift parameter has no name, there is no way to refer to it in SIL. However, the synthesized subscript accessor needs to pass this parameter to C++.

This change makes sure that we give a name to the Swift parameter if there isn't already a name on the C++ side.

rdar://83163841
2024-05-01 12:30:18 +01:00
Egor Zhdan
b330376a43 [cxx-interop] Pull changes from swift-6 compat mode into swift-5.9
This gives projects using C++ interop compat mode 5.9 access to the new features such as virtual methods and move-only types.

rdar://126485814
2024-04-24 11:53:31 +01:00
Egor Zhdan
78b9de1391 [cxx-interop] Run tests with swift-6 compat mode 2024-02-23 16:24:14 +00:00
Egor Zhdan
494474b021 [cxx-interop] Support C++ default arguments
This allows calling a C++ function with default arguments from Swift without having to explicitly specify the values of all arguments.

rdar://103975014
2024-01-10 16:37:42 +00:00
Egor Zhdan
c8017e7d9f [cxx-interop] Add friend operators to the lookup table properly
Previously, `friend` operators declared in C++ classes were added to the lookup table when the class is being imported.

The operators were added to the wrong lookup table if the class is declared in a C++ namespace. Since a namespace can span across multiple Clang modules, its contents should be added to a translation unit level lookup table, not to a module level lookup table.

This change makes sure we add `friend` operators to the lookup table earlier, when we are actually building the lookup table. Note that this is not possible for class template instantiations, because those are instantiated later, so for templates we still handle `friend` operators when importing the instantiation.

rdar://116349899
2023-12-08 18:48:58 +00:00
Egor Zhdan
ece33a4d15 [cxx-interop] Import using decls that refer to member operators of a base class
This is required for proper support for `std::vector::iterator` on Windows.

rdar://118657936 / resolves https://github.com/apple/swift/issues/69990
2023-11-20 18:57:12 +00:00
Egor Zhdan
efc008a2ca [cxx-interop] Import using decls that expose methods from private base classes
If a C++ type `Derived` inherits from `Base` privately, the public methods from `Base` should not be callable on an instance of `Derived`. However, C++ supports exposing such methods via a using declaration: `using MyPrivateBase::myPublicMethod;`.

MSVC started using this feature for `std::optional` which means Swift doesn't correctly import `var pointee: Pointee` for instantiations of `std::optional` on Windows. This prevents the automatic conformance to `CxxOptional` from being synthesized.

 rdar://114282353 / resolves https://github.com/apple/swift/issues/68068
2023-11-14 00:30:54 +00:00
Alex Lorenz
1d53ecedfd [cxx-interop] make sure a const operator [] is imported as a mutable setter 2023-11-10 14:21:03 -08:00
Egor Zhdan
3f223b54d9 [cxx-interop] Add tests for .pointee property access on derived type 2023-08-23 19:17:35 +01:00
Saleem Abdulrasool
68a7c1e8d3 test: enable yet more C++ interop tests on Windows
These tests should now work as the parameter passing issue has been
resolved.
2023-08-09 14:55:30 -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
c942dac517 [cxx-interop] Do not synthesize ambiguous pointee properties
If a C++ struct defines multiple overloads of `operator*`, avoid synthesizing multiple `var pointee: Pointee` properties, since that would introduce name resolution ambiguity. Instead, pick one of the const overloads and synthesize a single `pointee` property.

This is required for `std::optional` support.
2023-02-14 16:44:07 +00:00
zoecarver
492f2aaff2 [cxx-interop] Fix friend operators that come from class template specializations. 2022-10-14 13:33:06 -07:00
Egor Zhdan
954e74001c [cxx-interop] Drop return values of +=, -=, *=, /=
These operators return `Void` in Swift, let's drop the return type of these operators when importing them from C++.

This is needed for the upcoming `UnsafeCxxRandomAccessIterator` protocol: if a protocol declares `func +=` returning `Void`, but the implementation non-`Void`, that causes a typechecker error.
2022-10-11 17:59:22 +01:00
zoecarver
da2791eb45 [cxx-interop] Add plus-equal, minus-equal, star-equal, slash-equal operators. 2022-09-23 10:56:43 -07:00
Anthony Latsis
a65f1a161e Gardening: Migrate test suite to GH issues: Interop 2022-08-31 05:20:25 +03:00
Ehud Adler
f11e2afe07 [cxx-interop] Always import cxx shim when cxx-interop is enabled (#60336) 2022-08-05 08:28:47 -04:00
Ehud Adler
c6e89b94f1 [Cxx-Interop] Enable Record Friend functions (#59801)
* Update ImportDecl to handle Friend functions inside of recrods

* Add tests and update comment

* Undo unnecessary lines, format

* Clean up and fix tests

* New approach

* Only import valid friend functions for now

* Re-add == func to get Equatable conformance

* Remove requirement that friend is a function
2022-07-06 13:11:33 -04:00
zoecarver
8cf4d3b983 [cxx-interop] Disable cxxshim on windows until we can figure out cmakeconfig. 2022-07-05 20:03:45 -07:00
zoecarver
3805828ad0 [cxx-interop] Add CxxShim library; move __swift_interopStaticCast into it. 2022-07-05 15:52:51 -07:00
Egor Zhdan
0e31acb18c [IRGen] Emit missing sret for function arguments
If the IR representation of a C++ function takes an `SRet` parameter instead of a return value, Swift needs to wrap the corresponding argument with `sret(...)` on call site.

This fixes SILOptimizer crashes on arm64.

rdar://92963081
2022-07-01 17:49:33 +01:00
Egor Zhdan
0f1ef6d5c1 [cxx-interop] Add tests for operator++() that returns void
This pattern was discovered on `llvm::detail::SafeIntIterator` (`llvm/ADT/Sequence.h:153`):
```cpp
  // Pre Increment/Decrement
  void operator++() { offset(1); }
  void operator--() { offset(-1); }
```

rdar://95684692
2022-06-23 12:47:21 +01:00
Ehud Adler
23f5e015e7 [CXX Interoperability] Stop re-importing FuncDecl when FuncDecl is imported before parent record (#59607)
We saw a test case failing when 2 records contain the same operator. This occurs because when the first operator is called, we import the record associated with that operator but we also import the _function_ for the 2nd record. So if we have 2 records `Foo` and `Bar` and both implement `operator-`, after calling `Foo`'s `operator-` we would have imported

1. `Foo`
2. `Foo.operator-`
3. `Bar.operator-`

Then when we call `Bar.operator-` we try importing `Bar` record & then import the operator again. So that ends up with


1. `Foo`
2. `Foo.operator-`
3. `Bar.operator-`
4. `Bar`
5. `Bar.operator-`

which causes there to be 2 imports of the same operator (`FuncDecl`)

This patch checks to see if the `FuncDecl` was previously imported and returns early if it has been

Thanks @egorzhdan and @zoecarver for helping me debug this one :p
2022-06-22 11:05:33 +01:00
Egor Zhdan
d29b78eed1 [cxx-interop] Import increment operators
C++ pre-increment operator `T& T::operator++()` is mapped into a non-mutating function `successor() -> Self`.

The naming matches existing functions for `UnsafePointer`/`UnsafeMutablePointer`.

The purpose of this is to be used for iterator bridging: C++ requires iterators to define a pre-increment operator (https://en.cppreference.com/w/cpp/named_req/Iterator), which Swift will use to iterate over C++ sequences and collections.
2022-06-20 17:38:11 +01:00
Egor Zhdan
40a7e680a4 [cxx-interop] Import iterator dereference operators
C++ iterator dereference operator is mapped to a Swift computed property called `pointee`.

For example:
```cpp
struct ConstIterator {
  // ...
  const int &operator*() const { /* ... */ }
};
```
is imported as
```swift
struct ConstIterator {
  var pointee: Int32 { get }
  @available(*, unavailable, message: "use .pointee property")
  func __operatorStar() -> UnsafePointer<Int32>
}
```
2022-06-16 16:59:19 +01:00
Ehud Adler
e7fe6f0fe7 Fix tests and re-enable support for CXX operators 2022-05-21 21:28:03 -04:00
zoecarver
839839f924 [cxx-interop] Rename enable-cxx-interop -> enable-experimental-cxx-interop.
Also removes the driver flag, this will now also always be guarded on `-Xfrontend`.
2022-04-07 19:15:25 -07:00
zoecarver
f652361af3 [cxx-interop] Remove 'support' for importing C++ operators.
This does not include subscript operators.

Before this is re-enabled operators need to be re-implemented. Right now they are the source of a lot of bugs. They cause frequent crashes and mis compiles. Also, templated operators insert a lot of names into global lookup which causes problems.

They also don't work on Windows.
2022-03-30 15:39:35 -07:00
Alex
78a410c19e [cxx-interop] Add support for operator! overloading (#41434)
This patch enables operator overloading for operator! when cxx interop is enabled.
2022-03-14 09:55:03 -07:00
Ehud Adler
0056129d57 [cxx-interop] std::string::push_back is broken (#41240)
* [cxx-interop] fix  std::string::push_back by fixing mapping between clang/swift self/this type for cxx methods

* cleanup

* Fix unit test

* XFail test: operators/member-inline on linux-android

* add `C++` in expandExternalSignatureTypes comment

* Fix rebase

* Fix mapping issue in externalizeArguments

* Improve cxx_interop_ir test regex
2022-03-08 13:09:38 -05:00
zoecarver
2e0398f867 [cxx-interop] Add support for subscripts in base classes. 2022-03-01 15:13:10 -08:00
Saleem Abdulrasool
0d95706f74 test: partially enable some C++ interop tests on Windows
This enables the majority of the operator tests on Windows as these now
pass.  However, we cannot enable all of the tests, a few of them crash
due to invalid parameter ordering due to bugs in IRGen with C++ interop.
2022-01-17 08:56:51 -08:00
zoecarver
b8e52a7ad2 [cxx-interop] Lazily import members of Clang namespaces and records via requests.
Also adds a ClangImporter request zone and move some requests into it.
2021-10-20 14:52:43 -07:00
Egor Zhdan
a8f126f7cd C++ Interop: import const methods as non-mutating
This change makes ClangImporter import some C++ member functions as non-mutating, given that they satisfy two requirements:
* the function itself is marked as `const`
* the parent struct doesn't contain any `mutable` members

`get` accessors of subscript operators are now also imported as non-mutating if the C++ `operator[]` satisfies the requirements above.

Fixes SR-12795.
2021-07-25 15:18:33 +03:00
Puyan Lotfi
864b3a47e3 [cxx-interop] Implement operator[] for value return types (SR-14351).
This builds on top of the work of Egor Zhdan. It implements
`T operator[]` and does so largely by taking a path very much like the
`const T &operator[]` path.
2021-04-08 00:18:27 -04:00
Egor Zhdan
586c675286 C++ Interop: import subscript operators
This change adds support for calling `operator[]` from Swift code via a synthesized Swift subscript.

Fixes SR-12598.
2021-03-21 19:25:41 +03:00
Egor Zhdan
7141ae24cf C++ Interop: import call operators
This change adds support for calling `operator()` from Swift code.

As the C++ interop manifesto describes, `operator()` is imported into Swift as `callAsFunction`.
2021-03-02 21:13:57 +03:00
Zoe Carver
be2f85c102 [cxx-interop] [NFC] Rename MemberInline.IntBox -> LoadableIntWrapper. (#33930) 2020-09-24 20:08:19 -07:00
zoecarver
d647794ac0 [cxx-interop] Replace inline member "operator+" with "operator-".
Adding integers is a commutative operation meaning the old tests would
fail to detect an error if the arguments were passed in the wrong order.
Testing inline member operators using subtraction ensures that arguments
are passed in the correct order.
2020-09-08 21:09:43 -07:00
Michael Forster
a443327efe Add bug reference to XFAIL operator tests 2020-07-03 11:16:06 +02:00
Michael Forster
26358c4588 Import member operator functions as static members (#32293)
This adds support to `ClangImporter` to import C++ member function operators as static methods into Swift, which is part of SR-12748.

The left-hand-side operand, which gets passed as the `this` pointer to the C++ function is represented as an additional first parameter in the Swift method. It gets mapped back in SILGen.

Two of the tests are disabled on Windows because we can't yet call member functions correctly on Windows (SR-13129).
2020-07-03 11:06:22 +02:00