Commit Graph

38 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
Alex Lorenz
86886d96c9 [cxx-interop] do not rely on bodyParams being always non-null when importing function's name
This change fixes a swift-ide-test crash that occured in the Interop\Cxx\stdlib\msvcprt-module-interface.swift testcase with a newer MSVC, as one of its operator() had a parameter with a type that couldn't have been imported. The change ensures that body params are not used if they're null.
2024-09-05 10:54:52 -07: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
fahadnayyar
1d8ddc2697 [cxx-interop] Import the attributes from Clang decl for synthesized Swift decl for pointee and successor 2024-06-24 13:53:54 -07: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
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
5d099135f4 [cxx-interop] Fix typo in test 2023-12-08 17:36:22 +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
Egor Zhdan
1811125a21 [cxx-interop] Import operator bool() as an underscored function 2023-02-15 15:02:25 +00: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
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
Zoe Carver
764a4d26b5 Merge pull request #61269 from zoecarver/plus-equals
[cxx-interop] Add plus-equal, minus-equal, star-equal, slash-equal operators.
2022-09-23 13:03:04 -07:00
zoecarver
da2791eb45 [cxx-interop] Add plus-equal, minus-equal, star-equal, slash-equal operators. 2022-09-23 10:56:43 -07:00
Egor Zhdan
e58e249788 [cxx-interop] Do not synthesize successor() for immortal types
Our current implementation of `func successor() -> MyType` relies on the ability to create a new instance of the type. For immortal foreign reference types, this wouldn't be reasonable to do.

rdar://100050151
2022-09-20 17:00:05 +01:00
zoecarver
6acffbbee6 [cxx-interop] Flip the switch: only import safe APIs. 2022-07-18 17:15:15 -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
3805828ad0 [cxx-interop] Add CxxShim library; move __swift_interopStaticCast into it. 2022-07-05 15:52:51 -07: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
7282c642d0 Turn off templated operators aside from subscript 2022-06-02 13:00:22 -04: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
zoecarver
2e0398f867 [cxx-interop] Add support for subscripts in base classes. 2022-03-01 15:13:10 -08:00
Egor Zhdan
682f162f48 C++ Interop: fix subscript operator tests
C++ structs that define a non-default copy constructor should actually copy their members in the copy constructor:
Swift might copy the struct around, and if it does so after the `setValueAtIndex` call, the modification of `NonTrivialIntArrayByVal::values` won't propagate to the copied struct and these tests no longer pass:
• `Interop/Cxx/operators/member-inline.swift`
• `Interop/Cxx/operators/member-out-of-line.swift`

The tests are currently passing on the main branch despite this, but they might fail after a seemingly unrelated change (which is how I discovered this).
2021-07-12 22:22:27 +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
1f71144183 [cxx-interop] Don't crash for unkown inline-operator. (#34602)
Skip unkown or unimportable inline operators (such as deleted operator members) instead of crashing.
2020-11-06 17:13:59 -08: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
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