Commit Graph

144 Commits

Author SHA1 Message Date
Egor Zhdan
237a68c7b5 [cxx-interop] Do not import arithmetic operators with rvalue reference parameters
Currently those operators are imported with a `consuming:` label, which isn't valid in Swift.

We could just remove the label from these parameters, but that introduces a source breakage due to name lookup ambiguity.

So, to avoid ambiguity, let's not import such operators into Swift.

rdar://149020099
(cherry picked from commit eb7adc794d)
2025-04-14 11:28:09 +01:00
John Hui
edc742013d [cxx-interop] Make experimental flag ImportNonPublicCxxMembers (#79728)
ClangImporter can now import non-public members as of be73254cdc and 66c2e2c52b, but doing so triggers some latent ClangImporter bugs in projects that don't use or need those non-public members.

This patch introduces a new experimental feature flag, ImportNonPublicCxxMembers, that guards against the importation of non-public members while we iron out those latent issues. Adopters of the SWIFT_PRIVATE_FILEID feature introduced in bdf22948ce can enable this flag to opt into importing private members they wish to access from Swift.

rdar://145569473
2025-03-04 13:31:46 -05:00
John Hui
66c2e2c52b [cxx-interop] Import non-public inherited members (#79348)
This patch is follow-up work from #78942 and imports non-public members,
which were previously not being imported. Those members can be accessed
in a Swift file blessed by the SWIFT_PRIVATE_FILEID annotation.

As a consequence of this patch, we are also now importing inherited members
that are inaccessible from the derived classes, because they were declared
private, or because they were inherited via nested private inheritance. We
import them anyway but mark them unavailable, for better diagnostics and to
(somewhat) simplify the import logic for inheritance.

Because non-public base class members are now imported too, this patch
inflames an existing issue where a 'using' declaration on an inherited member
with a synthesized name (e.g., operators) produces duplicate members, leading
to miscompilation (resulting in a runtime crash). This was not previously noticed
because a 'using' declaration on a public inherited member is not usually
necessary, but is a common way to expose otherwise non-public members.
This patch puts in a workaround to prevent this from affecting the behavior
of MSVC's std::optional implementation, which uses this pattern of 'using'
a private inherited member. That will be fixed in a follow-up patch.

Follow-up work is also needed to correctly diagnose ambiguous overloads
in cases of multiple inheritance, and to account for virtual inheritance.

rdar://137764620
2025-02-25 01:03:16 -08:00
susmonteiro
b7c5a47898 [cxx-interop] Fix crash when operator doesn't name parameter in header 2025-01-28 16:26:27 +00:00
Gabor Horvath
1601564342 [cxx-interop] Import rvalue references as consuming parameters
Unfortunately, importing them as is results in ambiguous call sites.
E.g., std::vector::push_back has overloads for lvalue reference and
rvalue reference and we have no way to distinguish them at the call site
in Swift. To overcome this issue, functions with rvalue reference
parameters are imported with 'consuming:' argument labels.

Note that, in general, move only types and consuming is not properly
supported in Swift yet. We do not invoke the dtor for the moved-from
objects. This is a preexisting problem that can be observed with move
only types before this PR, so the fix will be done in a separate PR.
Fortunately, for most types, the moved-from objects do not require
additional cleanups.

rdar://125816354
2024-12-02 13:09:21 +00:00
Erik Eckstein
7cceaff5f3 SIL: don't print operand types in textual SIL
Type annotations for instruction operands are omitted, e.g.

```
  %3 = struct $S(%1, %2)
```

Operand types are redundant anyway and were only used for sanity checking in the SIL parser.

But: operand types _are_ printed if the definition of the operand value was not printed yet.
This happens:

* if the block with the definition appears after the block where the operand's instruction is located

* if a block or instruction is printed in isolation, e.g. in a debugger

The old behavior can be restored with `-Xllvm -sil-print-types`.
This option is added to many existing test files which check for operand types in their check-lines.
2024-11-21 18:49:52 +01:00
Egor Zhdan
6943986c71 [cxx-interop] Import private fields of C++ structs
While private and protected fields coming from C++ cannot be accessed from Swift, they can affect Swift typechecking.

For instance, the Swift typechecker mechanism that adds implicit `Sendable` conformances works by iterating over all of the struct's fields and checking whether all of them are `Sendable`. This logic was broken for C++ types with private fields, since they were never accounted for. This resulted in erroneous implicit `Sendable` confromances being added.

Same applies for `BitwiseCopyable`.

In addition to this, ClangImporter used to mistakenly mark all C++ structs that have private fields as types with unreferenceable storage, which hampered optimizations.

As a side effect of this change, we now also provide a better diagnostic when someone tries to access a private C++ field from Swift.

rdar://134430857
2024-11-12 12:47:26 +00:00
Ben Barham
a8916a74b2 [Test] Add missing EOF newline 2024-10-29 09:38:09 +10:00
Ben Barham
f59b2f19e6 Merge branch 'main' into 2024-rebranch-to-main 2024-10-28 13:03:35 -07:00
Hiroshi Yamauchi
fcc1f6b65e Fix the IR gen for C++ method calls and refactor around CGFunctionInfo
In GenCall, fix the IR gen for C++ method calls as under MSVC as the
calling conventions for free functions and C++ methods can be
different. This also fixes the missing inreg (on sret arguments)
issues on Windows ARM64. Also refactor to use CGFunctionInfo
returnInfo isSretAfterThis to detect when to reorder the sret and the
this arguments under MSVC.

In ClagImporter, don't drop the return type for the compound
assignment operators such as operator+= when the return value is a
reference so that the CGFunctionInfo will be correctly indicate an
indirect return for the compound assignment operators.
2024-10-24 15:40:21 -07:00
Egor Zhdan
a745ccaaa4 Merge pull request #76343 from swiftlang/egorzhdan/rebranch-windows-test
[cxx-interop][rebranch] Fix test on Windows
2024-09-09 21:03:00 +01:00
Egor Zhdan
0e57d02e5d [cxx-interop][rebranch] Fix test on Windows
rdar://135318900
2024-09-09 17:51:51 +01: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
Kavon Farvardin
5ae2f6bd25 CxxInterop: use Unsafe*Pointer for move-only 2024-05-14 17:44:22 -07:00
Kavon Farvardin
21d147d3d0 Revert "CxxInterop: use workaround unconditionally"
This reverts commit 1036031d06.
2024-05-14 16:43:05 -07:00
Kavon Farvardin
1036031d06 CxxInterop: use workaround unconditionally
We haven't yet solved the underlying issue in rdar://128013193 and the
workaround to make it conditionally use the better importing strategy of
Unsafe{Mutable}Pointer no longer will apply, since NoncopyableGenerics
is here.
2024-05-13 13:08:25 -07:00
Kavon Farvardin
7a83af6925 Revert "CxxInterop: remove checks for NoncopyableGenerics"
This reverts commit b04be89dce.
2024-05-13 10:46:30 -07:00
Kavon Farvardin
b04be89dce CxxInterop: remove checks for NoncopyableGenerics
It will always evaluate to true now; it's not experimental. This change
effectively reverts the following:
 - 6140ba1079
 - 78eee32467
2024-05-08 10:49:12 -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
6140ba1079 [cxx-interop] Do not try to use UnsafePointer<T> for non-copyable T when adding base member accessors 2024-04-23 18:47:55 +01:00
Nate Chandler
e40581a61b [MoveChecker] Visit and delete markers in PO.
Visit in post-order in order to resolve markers from the inside out,
required for per-field consume.
2024-03-06 20:54:17 -08: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
01670cdb62 Merge pull request #70325 from apple/egorzhdan/friend-operator-lookup
[cxx-interop] Add friend operators to the lookup table properly
2023-12-13 14:24:58 +01:00
Holly Borla
450e59c797 [cxx-interop] Allow imported subscript to have generic parameters. 2023-12-08 21:26:11 -08: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
Alex Lorenz
c7345d08cf Merge pull request #69790 from hyp/eng/move-only-is-back
[cxx-interop] enable support for move-only types
2023-12-06 09:43:43 -08:00
Alex Lorenz
623d3d2032 [cxx-interop] review fixes for non-copyable patch, ensure we only enable this in upcoming Swift 2023-12-05 14:16:30 -08:00
Alex Lorenz
3a340c78f0 [cxx-interop] move-only: do not test consume with unsafeAddress accessors - they do not yet work 2023-12-03 18:11:07 -08:00
Alex Lorenz
029644806d [cxx-interop] fix the use of '.pointee' with getter accessor for derived-to-base synthesized accessor 2023-11-29 15:42:11 -08:00
Alex Lorenz
16a8ae42ef [cxx-interop] fix the use of '.pointee' with address accessors for derived-to-base synthesized accessors 2023-11-29 15:10:27 -08:00
Alex Lorenz
a913c4e10a [cxx-interop] fix support for value-only non-copyable type dereference 2023-11-27 14:04:26 -08:00
Alex Lorenz
5d2637a8fe [cxx-interop] add support for 'mutableAddress' pointee for non-copyable C++ types 2023-11-27 09:59:33 -08: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
Alex Lorenz
86e5556e91 [cxx-interop] add support for 'address' pointee for non-copyable C++ types 2023-11-16 10:08:30 -08: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
Evan Wilde
24d0db249b Merge remote-tracking branch 'main' into 'rebranch'
Conflicts:
  CMakeLists.txt
    Take new BRIDGING_MODE

  SwiftCompilerSources/Sources/SIL/GlobalVariable.swift
    Take new
2023-10-09 17:21:23 -07: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
swift-ci
447482505a Merge remote-tracking branch 'origin/main' into rebranch 2023-10-05 08:05:25 -07: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
Saleem Abdulrasool
a3df6f64dd test: adjust tests for Windows after the rebranch
This corrects the name decoration and splits the windows and itanium
decoration schemes.  This allows us to un-XFAIL some of the interop
tests on Windows while repairing the failures identified on rebranch.
2023-09-15 11:16:11 -07: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