Commit Graph

454 Commits

Author SHA1 Message Date
Gabor Horvath
f48da45438 [cxx-interop] Configure requires ObjC from frontend option
We sometimes don't have the information in the modulemaps whether a
module requires ObjC or not. This info is useful for reverse interop.
This PR introduces a frontend flag to have a comma separated list of
modules that we should import as if they had "requires ObjC" in their
modulemaps.
2025-07-18 10:35:51 +01:00
Gábor Horváth
ab123929f6 [6.2][cxx-interop] Types exposed from ObjC modules should be behind a macro
Explanation: We the generated reverse interop headers to be valid C++,
so every declaration coming from an Obj-C module should be behind an
ifdef. Unfortunately, we do not always have this information but we do
know that our frameworks contain Obj-C code. So this PR makes sure every
entity coming from our frameworks are behind ifdef.
Issues: rdar://152836730
Original PRs: #83002
Risk: Low, the change is narrow and straightforward.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-07-16 11:19:55 +01:00
Anthony Latsis
5e41794680 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-23 19:18:08 +01:00
Gabor Horvath
8af781453c [6.2][cxx-interop] Fix a CI failure in ptrauth test
This test is not executed by the pre-commit CI, so the failure went
unnoticed after #80495. This PR updates the test to make the CI pass.

rdar://148665873
(cherry picked from commit c0b1fa1600)
2025-04-21 11:22:09 -07:00
Gábor Horváth
9c6e4dd3c5 Merge pull request #80586 from swiftlang/gaborh/nested-reverse-interop-fixes-on-6.2 2025-04-14 11:11:01 -07:00
Gábor Horváth
8e6daf75ec [6.2][cxx-interop] Fix a rare compilation error in reverse interop header
Explanation: Fix a compilation error in the generated reverse interop
header when a nested foreign type is used in a generic context and it is
reexposed to C++.
Issue: rdar://148597079
Risk: Low, the fix is fairly targeted to the affected scenario.
Testing: Added tests to test suite
Reviewer: @egorzhdan
2025-04-08 11:32:08 +01:00
Gábor Horváth
5f3d5a9fa7 [6.2][cxx-interop] Fix nested structs for non-opaque types
Explanation: Fixes an issue where the generated reverse interop header
would not compile for nested classes when library evolution is turned
off.
Scope: C++ reverse interop for nested classes for non-opaque types.
Issue: rdar://147882976
Risk: Low, the fix is fairly targeted. While it does affect other
(non-nested type) scenarios, those changes are fairly straightforward
making some names fully qualified. Moreover, that is well tested in our
test suite.
Testing: Added tests to test suite
Reviewer: @egorzhdan
2025-04-07 12:02:54 +01:00
Karoy Lorentey
21169d12e8 Reinstate generalization of Optional.unsafelyUnwrapped 2025-03-24 12:16:16 -07:00
Gabor Horvath
e7a8fb1672 [cxx-interop] Fix exporting Swift enums with C POD associated data
These types are OK to by copied using memcpy. Previously, the generated
code assumed these types are exported swift types with all the value witness
functions.

rdar://111812577
2025-03-14 15:10:13 +00:00
Becca Royal-Gordon
da07ff577c [PrintAsClang] Warn about unstable decl order
PrintAsClang is supposed to emit declarations in the same order regardless of the compiler’s internal state, but we have repeatedly found that our current criteria are inadequate, resulting in non-functionality-affecting changes to generated header content. Add a diagnostic that’s emitted when this happens soliciting a bug report.

Since there *should* be no cases where the compiler fails to order declarations, this diagnostic is never actually emitted. Instead, we test this change by enabling `-verify` on nearly all PrintAsClang tests to make sure they are unaffected.

This did demonstrate a missing criterion that only mattered in C++ mode: extensions that varied only in their generic signature were not sorted stably. Add a sort criterion for this.
2025-02-14 21:41:36 -08:00
Gabor Horvath
8603dfe53a [cxx-interop] Support nested classes in reverse interop
Turns out we already had most of the building blocks given we already
support nested structs.

rdar://143343490
2025-02-03 14:56:31 +00:00
Becca Royal-Gordon
60f6afb76c Correct newline emission in generated headers
Eliminates extraneous newlines between top-level Objective-C declarations in `-emit-objc-header` headers. Specifically, there should now always be exactly one—no more, no less—empty line between `@end` and whatever follows it.

Besides being more aesthetically pleasing, this eliminates ordering-dependent behavior where PrintAsClang would print an extra newline when visiting an empty extension, which meant that the order in which empty and non-empty extensions were visited during printing could result in whitespace differences in the compiler output. Printing the blank line is now conditional on whether `tell()` indicates that characters were actually written to the output.

Fixes rdar://143533893.
2025-01-24 16:26:33 -08:00
Robert Widmann
0f94164d3b Replace Hand-Rolled MAX/MIN with std::max/std::min
We're using C++ 14+ at least now across the project so these should not be necessary. It's also kind of wild that the definitions we had across different subsystems had different definitions.
2025-01-22 11:18:35 -07:00
Gabor Horvath
8a3beca30d [cxx-interop] Avoid spurious type aliases in reverse interop
To support nested structs, we emit type aliases in the outer class.
Unfortunately, we emitted these type aliases unconditionally, even if
the actualy nested struct was not emitted to the reverse interop header
(due to visibility or the construct being unsupported). This PR fixed
this issue by checking first if the nested entity should be included in
the reverse interop header.

rdar://141688074
2024-12-19 11:02:19 +00:00
Gábor Horváth
113f11c2d1 Merge pull request #77391 from swiftlang/gaborh/avoid-exporting-read-accessors
[cxx-interop] Fix crash trying to export read accesors
2024-11-07 15:48:15 +00:00
Gabor Horvath
e00e285df7 [cxx-interop] Fix crash trying to export read accesors
This is not yet supported, so skip exporting those accessors for now.

Fixes #77304

rdar://138944832
2024-11-06 13:08:38 +00:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Gabor Horvath
22b46d3c9c [cxx-interop] Mark some zero-sized value types as unavailable
Currently, we do not support exporting zero-sized value types from Swift
to C++. It needs some work on our end as these types are not part of the
lowered signature. In the meantime, this PR makes sure that common (but
not all) zero sized types are properly marked as unavailable. This is
important as the proper diagnostic will give users a hint how to work
around this problem. Moreover, it is really easy to hit this when
someone is experimenting with interop, so it is important to not have a
cryptic failure mode.

rdar://138122545
2024-10-28 14:00:35 +00:00
Allan Shortlidge
cb578172ea Tests: Remove -disable-availability-checking in more tests that use concurrency.
Use the `%target-swift-5.1-abi-triple` substitution to compile the tests for
deployment to the minimum OS versions required for use of _Concurrency APIs,
instead of disabling availability checking.
2024-10-19 12:35:20 -07:00
Gabor Horvath
0e03d342fe [cxx-interop] Support ObjC protocols in C++ interop
This patch introduces handling of ObjC protocols similar to how ObjC
classes work. Since this only works in ObjC++, all declarations
containing ObjC protocols will be protected by the __OBJC__ macro.

This patch results in some `_bridgeObjC` methods being exposed, we might
end up hiding those in the future, but there is no harm having them in
the interop header for the interim period.

rdar://136757913
2024-10-16 18:51:35 +01:00
Rintaro Ishizaki
adb833754b [Test] Add 'REQUIRES: embedded_stdlib' to an embedded test 2024-10-11 15:24:15 -07:00
Gabor Horvath
94b466656e [cxx-interop] Support nested structs
It is really involved to change how methods and classes are emitted into
the header so this patch introduces the impression of nested structs
through using statements and still emits the structs themselves as top
level structs. It emits them in their own namespace to avoid name
collisions. This patch also had to change some names to be fully
qualified to avoid some name lookup errors in case of nested structs.
Moreover, nesting level of 3 and above requires C++17 because it relies
on nested namespaces. Only nested structs are supported, not nested
classes.

Since this patch is already started to grow quite big, I decided to put
it out for reviews and plan to address some of the shortcomings in a
follow-up PR.

rdar://118793469
2024-09-10 13:22:17 +01:00
Gábor Horváth
25a8885066 Merge pull request #76170 from swiftlang/gaborh/read-accessor
[cxx-interop] Do not attempt to export read accessors to C++
2024-08-30 22:27:10 +01:00
Gabor Horvath
131a354436 [cxx-interop] Do not attempt to export read accessors to C++
This triggers a crash. Unfortunately, adding support is not that
straightforward so skipping these accessors for now.

rdar://134425834
2024-08-30 16:16:04 +01:00
Gabor Horvath
0eab959d2b [cxx-interop] Do not export enums with indirect associated values
We do not support any sort of indirect enums yet, the code gets into an
infinite recursion. Let's skip exporting these types to C++ for now
until proper support is implemented.

rdar://134852756
2024-08-30 13:34:28 +01:00
Alexander Cyon
db0b5db54e [test/Interop] Fix typos (#75032) 2024-08-28 09:41:09 -07:00
Egor Zhdan
c3e067bb34 Merge pull request #75804 from swiftlang/egorzhdan/static-inline
[cxx-interop] Print `inline` instead of `static inline` for template specializations
2024-08-09 22:37:36 +01:00
Egor Zhdan
11268dd188 [cxx-interop] Print inline instead of static inline for template specializations
This fixes a number of test failures in reverse C++ interop.

Clang's behavior was changed in https://github.com/llvm/llvm-project/pull/93873, and it no longer accepts the C++ headers that Swift generates.

rdar://132283247
2024-08-09 15:46:48 +01:00
Gabor Horvath
57e00e4ecf [cxx-interop] Support operator[] with multiple parameters
C++ only support multiparameter operator[] in C++23 and up. Change the
code to protect such overloaded operators with a C++ language mode
check.

rdar://133539699
2024-08-09 15:33:47 +01:00
Gábor Horváth
20b3b14646 Merge pull request #75686 from swiftlang/gaborh/watchos-test-fix
[cxx-interop] Fix test encoding data layout in expectations
2024-08-07 17:45:53 +01:00
Gábor Horváth
4c3c612b92 Merge pull request #75692 from swiftlang/gaborh/reverse-interop-c-structs
[cxx-interop] Support reexposing C structs from Swift to C++
2024-08-07 17:44:43 +01:00
Gabor Horvath
c4649edc2c [cxx-interop] Support reexposing C structs from Swift to C++
Previously, when a Swift API referenced a C struct (that is not a C++
struct), we did not expose said API to C++. The reason was that we do
not have support yet for non-trivial structs (structs with ARC fields).
This patch introduces logic to determine if a C struct is trivial and
lets us expose trivial C structs from Swift to C++.

rdar://111812577
2024-08-05 17:44:00 +01:00
Gabor Horvath
67fbe8f025 [cxx-interop] Fix test encoding data layout in expectations
As a result the test fail on certain watchOS targets.

rdar://124466216
2024-08-05 16:03:41 +01:00
Gabor Horvath
a05b3051fa [cxx-interop] Fix crash with reverse interop in Embedded Swift
Embedded Swift has a minimal runtime, some type metadata is not
available. This patch works around a crash that tries to emit C++
briding to this non-existent Swift metadata. It is very likely that
there will be more fallout in reverse interop, but this patch should fix
the most glaring issue, crashing on an empty Embedded Swift project.

rdar://129030521
2024-08-05 10:47:09 +01:00
Ben Barham
d281fb8868 Merge pull request #74883 from finagolfin/droid
[android] Enable several C++ Interop and other tests
2024-07-26 11:18:07 -07:00
Gábor Horváth
536a88971f Merge pull request #75396 from swiftlang/gaborh/value-type-trait
[cxx-interop] Support generic types with isValueType trait
2024-07-24 12:11:24 +02:00
Gabor Horvath
48ebed6760 [cxx-interop] Support generic types with isValueType trait
In some circumstances the missing trait resulted in picking the wrong
branch of some compile time conditionals resulting in code that would
not compile.

rdar://126709253
2024-07-23 09:48:49 +02:00
Gabor Horvath
e195093aee [cxx-interop] Fix the printing of types with generic arguments
Previously the code got the declaration for types with generic
arguments and the printer used the declaration. This was a lossy
operation, we printed the type with generic parameters instead of the
arguments. This patch makes sure we print the type with the arguments.
Unfortunately, the code structure is not the most clear, type printing
is currently inherently part of the function signature printing. This
code path needs to be factored out in the future to make the code easier
to understand.

rdar://130679337
2024-07-22 13:42:24 +02:00
Finagolfin
15e1c73969 [android][test] Enable several C++ Interop and other tests
Also, fix lit.cfg for running the test suite natively in Android and mark one
SILOptimizer executable_test as such.
2024-07-20 17:56:51 +05:30
Gábor Horváth
439cbe6221 Merge pull request #75100 from swiftlang/gaborh/support-optional-generic-in-enum
[cxx-interop] Support optional generic cases in enums
2024-07-18 15:14:02 -07:00
Gabor Horvath
e6c3cb3db1 [cxx-interop] Forward declare classes
The code already forward declared strutcs and enums. This patch extends
the logic to also forward declare classes. Unfortunately, there was some
fallout because some traits ended up defined multiple times for some
classes, so the code is now extended to only conditionally emit these
traits if no forward declaration was emitted for the type yet.

rdar://124022242
2024-07-18 15:27:50 +01:00
Gábor Horváth
01bd2b4f8c Merge pull request #75202 from swiftlang/gaborh/export-static-properties
[cxx-interop] Export accessors of static properties to C++
2024-07-15 05:40:07 -07:00
Gabor Horvath
fe1a4b5961 [cxx-interop] Export accessors of static properties to C++
The code wants to avoid exporting certain synthesized operators but it
inadvertently also prevented exporting accessors to static properties.

rdar://115564118
2024-07-12 16:33:35 +01:00
Gabor Horvath
6db7f0ba7a [cxx-interop] Fix test failure for arm64e test
This is a fallout from emitting Swift StdLib dependencies when all
public decls are emitted. This was not found before because the test was
not executed by the CI.

rdar://131556373
2024-07-12 14:22:49 +01:00
Gabor Horvath
b5384bc8a6 [cxx-interop] Support optional generic cases in enums
This PR implements proper support for optional generic associated values
in enum cases. Most of the code changes are supporting generic types in
more contexts in the printer, the rest are making sure we handle the
null pointer case when we try to get the declaration from the type that
represents a generic parameter.

rdar://131112273
2024-07-09 16:08:27 +01:00
Gabor Horvath
c56cca1d3b [cxx-interop] Do not expose enums with optional generic cases
Unfortunately, we cannot generate the C++ code for them just yet. There
will be a follow-up PR to actually fix the underlying issue and expose
such enums correctly.

rdar://129250756
2024-07-04 15:42:50 +01:00
Gabor Horvath
c3a02cd80d [cxx-interop] Emit Swift StdLib dependencies when all public decls emitted
The generated header would not compile without these dependencies. Moreover
users probably expect all-public option to be the most permissive filter
including the maximal amount of declarations.
2024-07-03 18:07:41 +01:00
Egor Zhdan
f85ce90ce3 Merge pull request #74620 from swiftlang/egorzhdan/reverse-frt-virtual-test
[cxx-interop] Fix reverse interop test for virtual method dispatch on arm64e
2024-06-26 12:36:21 +01:00
Egor Zhdan
a0e1588417 [cxx-interop] Fix reverse interop test for virtual method dispatch on arm64e
rdar://105396625
2024-06-21 16:26:23 +01:00
Gábor Horváth
5fbf42a76f Merge pull request #74516 from swiftlang/gaborh/objcpp-fix
[cxx-interop] Fix exporting cdecl Swift functions to Obj-C++
2024-06-21 06:58:54 +01:00