Commit Graph

93 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
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
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
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
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
Alexander Cyon
db0b5db54e [test/Interop] Fix typos (#75032) 2024-08-28 09:41:09 -07: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
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
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
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
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
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
Gabor Horvath
ae1f8042c0 [cxx-interop] Fix extra indirection when exporting CFData arguments/return values
The clang nodes associated with Swift's Core Foundation types can already be
represented by a pointer. The interop code does not need to add an extra
layer of indirection in those cases.

rdar://119840281
2024-06-12 16:39:20 +01:00
Becca Royal-Gordon
ee24bdf06f Merge pull request #72402 from beccadax/noreturn-for-tomorrow
[PrintAsClang] Fix thunks for Never funcs
2024-03-30 05:30:56 -07:00
Becca Royal-Gordon
80f38be3b8 [PrintAsClang] Fix thunks for Never funcs
Swift-to-C++ thunk printing for functions didn’t really take into account Swift’s `Never` type. This type maps to `SWIFT_NORETURN`, but it also requires other tweaks to code generation, such as omitting the `return` keyword. (Removing that requires minor changes to many tests.)

Fixes rdar://124137073.
2024-03-29 22:12:19 -07:00
Allan Shortlidge
8f4af40f91 tests: Un-XFAIL some CxxInterop tests that pass now. 2024-03-27 22:20:50 -07:00
Karoy Lorentey
68a8c41e81 [test] Temporarily XFAIL tests that expect swiftinterface files to work when using Optional and other stdlib features 2024-03-18 11:09:08 -07:00
Doug Gregor
b5fc2fc036 [Generated header] Emit members of enum extensions into C++ class
As we do with Swift structs, emit the members of extensions of Swift
enums into the corresponding C++ class. This includes exposing more of
the Optional API from the standard library into Swift.
2024-03-05 21:54:47 -08:00
Alex Lorenz
588a35195d [interop][SwiftToCxx] fix a CHECK line in string-to-nsstring.mm
rdar://118139534
2023-11-09 13:42:45 -08:00
Alex Lorenz
261f41e973 Merge pull request #69507 from finagolfin/droid
[android][test] Disable a C++ Interop test that doesn't work with clang 14 from LTS NDK 25
2023-10-31 09:23:29 -07:00
Alex Lorenz
ca95860530 [interop][SwiftToCxx] ensure Swift generic types can be used as template parameters to other Swift generic types in C++
rdar://117015962
2023-10-30 12:17:29 -07:00
Finagolfin
12d7405956 [android][test] Disable a C++ Interop test that doesn't work with clang 14 from LTS NDK 25 2023-10-30 20:39:00 +05:30
Alex Lorenz
4e519a8d8b [interop][SwiftToCxx] consuming parameters should be passed using an additional copy
Fixes https://github.com/apple/swift/issues/69372
2023-10-27 17:29:52 -07:00
Ben Barham
e0b976d150 [CxxInterop] Change C++ interop header ordering
Consider the case of a toolchain that includes clang is being used to
compile Swift. With the current ordering, the header will be found
relative to that toolchain, which could be out of date as compared to
tip. This is the case today if eg. a 5.7 toolchain is used to build
main/5.9 as the shim header is missing a definition.
2023-08-17 09:46:28 -07:00
Alex Lorenz
85a431eedf [interop][SwiftToCxx] ensure swift::Int and swift::UInt are usable in generic context
Fixes https://github.com/apple/swift/issues/63452
2023-07-12 12:47:49 -07:00
Alex Lorenz
de10b4155e [interop][SwiftToCxx] support copy-assignment operation for Swift value types and prohibit move assignment explicitly
Fixes https://github.com/apple/swift/issues/66324

Note: move semantics for Swift value types are not yet supported in C++
2023-06-06 15:55:30 -07:00
Alex Lorenz
39d86bc701 [interop][SwiftToCxx] the not yet implemented move constructor for Swift value types should lead to link error when a move is used in Swift, not runtime error 2023-05-07 08:55:28 -07:00
Alex Lorenz
a1dc63ec46 Merge pull request #65728 from hyp/eng/safe-overloads-and-unavailable-unemitted-decls-in-cxx
[interop][SwiftToCxx] avoid emitting ambiguous C++ overloads and emit unavailable type stubs for top level types that could not be emitted in the C++ section of the generated header
2023-05-07 06:25:03 -07:00
Alex Lorenz
ace328af62 Merge pull request #65683 from hyp/swift-string-from-literal
[interop][SwiftToCxx] add swift::String overlay constructor for const…
2023-05-06 09:41:13 -07:00
Alex Lorenz
4010357ae1 [interop][SwiftToCxx] fix foundation-type-not-exposed-by-default-to-cxx.swift test on Linux and Windows 2023-05-06 06:55:48 -07:00
Alex Lorenz
bf35e3f55a [interop][SwiftToCxx] provide a clear unavailable message for protocol decls in C++ 2023-05-05 17:03:28 -07:00
Alex Lorenz
aff3568a12 [interop][SwiftToCxx] emit unavailable type stubs for top level types that could not be emitted in the C++ section of the generated header 2023-05-05 17:03:26 -07:00
Alex Lorenz
c41774188c [interop][SwiftToCxx] add swift::String overlay constructor for constructing it directly from a C string literal
resolves https://github.com/apple/swift/issues/63448
2023-05-05 06:29:59 -07:00
Alex Lorenz
f4f6f7a408 [interop][SwiftToCxx] avoid importing C++ stdlib in C++ section of generated header inside of 'pragma clang attribute' block 2023-05-04 17:01:39 -07:00
Alex Lorenz
7efae9a74c [interop][SwiftToCxx] add additional type representation emission check for associated enum element types
This ensures that we do not try to emit enums whose associated values come from dependent modules that don't have a C++ representation
2023-04-24 12:31:30 -07:00
Alex Lorenz
c4cc9e8e42 [interop][SwiftToCxx] do not error out on -Wnon-modular-include-in-framework-module clang diagnostic when importing the generated header as part of a framework back into C++ via a Clang module 2023-03-30 08:40:40 -07:00
Alex Lorenz
f405fb0fa8 [interop][SwiftToCxx] do not expose @alwaysEmitIntoClient properties to C++ 2023-03-15 10:01:37 -07:00
Alex Lorenz
94c1de7611 [interop][SwiftToCxx] use 'SWIFT_INLINE_PRIVATE_HELPER' macro for static returnNewValue functions to ensure attribute(used) isn't applied to them 2023-03-15 09:13:38 -07:00
Alex Lorenz
3cce09e769 [interop][SwiftToCxx] annotate several value members types as 'noexcept'
C++/clang is more strict about requiring noexcept in templates so we need some explicit annotations
2023-03-12 11:49:14 -07:00
Alex Lorenz
0dc90d38c1 [interop][SwiftToCxx] emit Swift's stdlib inside of 'swift' namespace 2023-03-09 17:32:43 -08:00
Alex Lorenz
3adf16eb28 Merge pull request #64083 from hyp/eng/api-stdlib-arg-labels
[interop][SwiftToCxx] incorporate argument labels into Stdlib API nam…
2023-03-03 21:37:40 -08:00
Alex Lorenz
97dc2758d7 Merge pull request #64075 from hyp/eng/lambda-attrs
[interop][SwiftToCxx] use 'nodebug' for inline thunks and apply inline and artificial attributes to lambd…
2023-03-03 21:37:25 -08:00
Alex Lorenz
13a63f4b05 [interop][SwiftToCxx] incorporate argument labels into Stdlib API names in C++
This allows easier use and more wider API exposure from the Swift stdlib

In the future, all functions and method should incorporate argument labels, but it's not yet clear how
2023-03-03 14:15:48 -08:00
Alex Lorenz
7caf34c190 [interop][SwiftToCxx] apply inline and artificial attributes to lambdas used with returnNewValue 2023-03-03 11:45:46 -08:00
Mishal Shah
aced44a84b Merge pull request #63187 from apple/rebranch
Merge `rebranch` into `main` to support `stable/20221013` llvm-project branch
2023-03-03 11:01:34 -08:00
Alex Lorenz
2dcbeb0bc9 [interop][SwiftToCxx] do not require @_expose when generating C++ interface for a Swift module
Instead, we emit API bindings for all supported public APIs, when -enable-experimental-cxx-interop is enabled
2023-03-02 14:06:47 -08:00
Alex Lorenz
4c24669c66 [interop][SwiftToCxx] add 'has-expose-attr-or-stdlib' mode to -clang-header-expose-decls flag
This mode allows the user to fallback to the old behavior that required @expose annotations, while still having standard library interfaces emitted in one header
2023-03-02 13:58:23 -08:00