Commit Graph

96 Commits

Author SHA1 Message Date
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
Arnold Schwaighofer
79894ff461 Fix test/Interop with opaque pointers 2023-07-03 03:36:07 -07:00
Arnold Schwaighofer
c1a93e0bde Move tests over to use the %use_no_opaque_pointers option 2023-06-14 10:49:48 -07:00
Egor Zhdan
ac72084854 Merge pull request #63683 from apple/egorzhdan/cxx-optional
[cxx-interop] Add `CxxOptional` protocol for `std::optional` ergonomics
2023-03-06 10:43:05 +00:00
Alex Lorenz
fedf86ff76 Merge pull request #63809 from hyp/eng/exception1
[interop] add itanium ABI support for trapping on uncaught exceptions when making a foreign call
2023-02-23 19:41:43 -08:00
Alex Lorenz
9feb76419b [interop] ignore exceptions in existing interop tests 2023-02-22 11:00:51 -08:00
zoecarver
985db63e2b [tests] Update tests based on new template name importing rules. 2023-02-20 17:58:10 -08: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
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
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
Anthony Latsis
a65f1a161e Gardening: Migrate test suite to GH issues: Interop 2022-08-31 05:20:25 +03:00
swift-ci
d81ba4efc7 Merge remote-tracking branch 'origin/main' into rebranch 2022-08-05 05:33:06 -07:00
Ehud Adler
f11e2afe07 [cxx-interop] Always import cxx shim when cxx-interop is enabled (#60336) 2022-08-05 08:28:47 -04:00
swift-ci
9de631f94f Merge remote-tracking branch 'origin/main' into rebranch 2022-07-19 17:35:57 -07:00
zoecarver
6acffbbee6 [cxx-interop] Flip the switch: only import safe APIs. 2022-07-18 17:15:15 -04:00
swift-ci
7771d613a2 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-15 08:54:39 -07:00
Egor Zhdan
1be0086413 [cxx-interop] Mark operator! as prefix func
Prefix operators in Swift need to be marked as `prefix func`.

For example, the lack of `prefix` attribute prevents the user from conforming a C++ type that defines `operator!` to a Swift protocol that requires `static prefix func !(obj: Self) -> Self`.
2022-07-15 14:50:36 +01:00
swift-ci
98eb9fed81 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-06 10:14:50 -07: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
swift-ci
d4005d615f Merge remote-tracking branch 'origin/main' into rebranch 2022-07-06 09:55:20 -07: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
swift-ci
5c8f1fcb12 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-02 17:03:37 -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
swift-ci
04379220d6 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-23 07:33:34 -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
swift-ci
b8f39b56be Merge remote-tracking branch 'origin/main' into rebranch 2022-06-22 09:15:09 -07:00
Egor Zhdan
b901ad28ea [cxx-interop] Fix crash while importing operator++() that returns void
rdar://95684692
2022-06-22 14:37:36 +01:00
swift-ci
7f4aec9511 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-22 03:15:51 -07: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
swift-ci
bf457168f7 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-20 15:14:03 -07: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
swift-ci
78450d3226 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-16 11:13:54 -07: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
swift-ci
40c801f236 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-09 13:52:42 -07:00
Egor Zhdan
31381a352a [cxx-interop] Fix assertion failure when looking up C++ operators
Previously when looking up a C++ operator (e.g. `==`), `ClangImporter::Implementation::lookupValue` returned a Swift function with a generated name (e.g. `func __operatorEqualEqual`), while the caller expected the result to be a Swift operator (e.g. `func ==`).

This change makes sure that we're returning a synthesized Swift operator when looking for a C++ operator.

This fixes an assertion failure in the typechecker:
```
swift-frontend: /home/build-user/swift/lib/Sema/ConstraintSystem.cpp:1454: std::pair<Type, Type> swift::constraints::ConstraintSystem::getTypeOfReference(swift::ValueDecl *, swift::FunctionRefKind, swift::constraints::ConstraintLocatorBuilder, swift::DeclContext *): Assertion `func->isOperator() && "Lookup should only find operators"' failed.
```
2022-06-09 19:24:13 +01:00
Ben Barham
f6d432feed [next] Generalise further interop tests
See e382e15f6a for previous test fixes -
these were xfailed at the time, so weren't updated then.
2022-06-03 11:01:19 -07:00
Ehud Adler
7282c642d0 Turn off templated operators aside from subscript 2022-06-02 13:00:22 -04:00
Ehud Adler
2422c0c111 Keep the XFAIL on windows for now 2022-05-27 15:14:52 -04:00
Ehud Adler
597668f05b Test diff mangled name for windows 2022-05-27 15:07:57 -04:00
Ehud Adler
e7fe6f0fe7 Fix tests and re-enable support for CXX operators 2022-05-21 21:28:03 -04:00
Ehud Adler
11730e8f07 Remap class operator function names (-/+/*....) to imported operator names __operator(Minus, Plus,...) and fix test cases 2022-05-18 10:31:04 -04:00
Ehud Adler
178b012b80 Add operators as class members 2022-05-18 10:30:56 -04:00
Ehud Adler
88f7260c15 start work on operators 2022-05-18 10:29:30 -04:00