Commit Graph

454 Commits

Author SHA1 Message Date
Gabor Horvath
5bb9ccf149 [cxx-interop] Fix exporing cdecl Swift functions to Obj-C++
We export cdecl function declarations twice: for Objective-C and for
C++. When the code is compiled in Objective-C++ both of the declarations
are visible to the compiler. The generated header did not compile,
because only one of the declarations were noexcept. There are multiple
possible ways to fix this issue, one of them would make only C++
declarations visible in Objective-C++ mode. However, for this particular
problem I decided to also make the Objective-C functions SWIFT_NOEXCEPT.
This approach resolves the inconsistency that broke the code when
compiled in Objective-C++ mode. Moreover, Swift guarantees that those
cdecl declarations cannot raise errors, so in case we only generate the
C declarations and consume them from C++ or Objective-C++, those are the
correct declarations.

rdar://129550313
2024-06-18 16:40:34 +01:00
Gábor Horváth
c0c28c4c3c Merge pull request #74384 from apple/gaborh/print-decl-order
[cxx-interop] Fix generated declaration order
2024-06-13 19:19:50 +01:00
Gabor Horvath
dbdd983392 [cxx-interop] Fix generated declaration order
The generated thunks for functions can refer to some internal methods of
their arguments. As a result, those generated thunks should always be
after the definitions of the corresponding argument types. The printer
ordered the declarations by name, and Swift had the convention starting
types with upper case letters and functions with lower case letters.
This naming convention together with the ordering resulted in the
correct ordering in most of the cases. There were a couple of exceptions
when people diverged from the naming conventions or wanted to export
operators. This patch fixes this problem by always ordering type decls
before function decls.

rdar://129276354
2024-06-13 15:22:03 +01:00
Gábor Horváth
de9aefc842 Merge pull request #74330 from apple/gaborh/core-foundation-types
[cxx-interop] Fix extra indirection when exporting CFData arguments
2024-06-13 08:29:34 +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
Gábor Horváth
c2d6039992 Merge pull request #74287 from apple/gaborh/unavailable-generics
[cxx-interop] Fix unavailable generics triggering compilation error
2024-06-11 17:26:22 +01:00
Gabor Horvath
353f0608f9 [cxx-interop] Fix unavailable generics triggering compilation error
In some cases, the reverse interop generated both a forward declaration and a
definition with unavailable attribute in the C++ header. Unfortunately, the
kinds of these symbol did not match. The forward declaration was templated
while the definition was not. The forward declaration has the correct kind,
so this patch extends the printing of unavailable definitions to include the
generic arguments.

rdar://119835933
2024-06-11 11:43:02 +01:00
Gabor Horvath
53861a4e00 [cxx-interop] Avoid emitting methods that cause name clash
In Swift, we can have enum elements and methods with the same name, they are overloaded.
Unfortunately, this feature is not supported by C++ interop at the moment. This patch
avoids emitting the methods with name collisions to make sure the resulting header
can be compiler. A proper fix should follow in a later PR.

rdar://128162252
2024-06-10 12:35:44 +01:00
Egor Zhdan
f42609b7bd [cxx-interop][SwiftToCxx] Do not crash while trying to expose a macro to C++
This fixes a compiler crash that happened when emitting a Clang header for a Swift module that declares multiple macros with the same base name and different argument names.

Swift macros are not currently designed to be exposed to C++. This teaches the compiler to explicitly mark them as unavailable in C++.

rdar://117969472 / resolves https://github.com/apple/swift/issues/69656
2024-05-08 16:08:02 +01:00
Meghana Gupta
5380f4d097 Disable an Interop test in CI 2024-04-09 11:01:33 -07: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
fa2ebd9486 Modify test to work correctly on simulator
`simctl spawn` does not propagate exit codes through, so we need the test to exit using `exit(0)`, not `fatalError()`.
2024-03-30 00:32:30 -07:00
Becca Royal-Gordon
a02698ba06 [PrintAsClang] Fix thunks for throwing Never funcs
Extend the previous commit’s support for functions that return Never to also properly generate code for *throwing* Never functions. This is a little subtle because:

• At the SWIFT_CALL level, throwing Never functions are *not* noreturn
• At the thunk level, throwing Never functions are noreturn *only* if you’re using exceptions; if you’re using swift::Expected, they should throw
• In either case, the compiler cannot statically prove that thunks are noreturn except on the error path, so we need to add an abort() call on the success path
2024-03-29 22:12:20 -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
Alexis Laferrière
b03d4faec2 Tests: disable C++ interop test on armv7k 2024-03-12 11:20:48 -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
Emil Pedersen
299cf2972e [test] Add missing colons for FileCheck 2024-02-26 13:31:04 -08:00
Akira Hatanaka
fe88a00c81 [interop][SwiftToCxx] Skip emitting an accessor method if its name collides with an accessor method of another property
Resolves rdar://119835520 and rdar://119835571.
2024-02-21 17:29:14 -08:00
Akira Hatanaka
daebcc3fdf [interop][SwiftToCxx] Ignore delayedMembers in C++ (#70939)
[interop][SwiftToCxx] Ignore delayedMembers in C++

This fixes a bug where an ObjC @interface declaration is emitted for a
class that has a member that isn't emitted.

Resolves rdar://119835836
2024-01-25 14:47:39 -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
405ffed2cc Merge pull request #69702 from hyp/eng/117805261
[interop] fix consuming-parameter-in-cxx.swift failure
2023-11-09 10:10:02 -08:00
Alex Lorenz
bcdfb8da28 [interop][SwiftToCxx] dispatch 'class' methods directly to avoid broken header generation
rdar://102393950
2023-11-07 14:33:54 -08:00
Alex Lorenz
86338c9f3b [interop] fix consuming-parameter-in-cxx.swift failure
rdar://117805261
2023-11-07 11:36:22 -08:00
Michael Gottesman
7e7eb60044 Merge pull request #69579 from meg-gupta/bulkdisable
Disable failing tests in CI
2023-11-01 17:12:29 -07:00
Meghana Gupta
bb138a14b7 Disable consuming-parameter-in-cxx.swift 2023-11-01 13:13:55 -07:00
Alex Lorenz
ea38d57485 Merge pull request #69519 from hyp/eng/get-type-metadata-helper-inline
[interop][SwiftToCxx] Use 'SWIFT_INLINE_PRIVATE_HELPER' for getTypeMetadata helper to avoid emitting a reference to it when 'DEBUG' macro is set
2023-11-01 10:26:36 -07: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
b1d63d0938 [interop][SwiftToCxx] Use 'SWIFT_INLINE_PRIVATE_HELPER' for getTypeMetadata helper to avoid emitting a reference to it when 'DEBUG' macro is set
This is needed as the metadata for C++ types might not be actually emitted and thus we can't always reference it

Fixes https://github.com/apple/swift/issues/69234

rdar://117089662
2023-10-31 09:21:01 -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
ab948f4897 [interop][SwiftToCxx] ensure that buffers for heap allocated Swift values are released after parameter copy is consumed 2023-10-29 17:08:02 -07:00
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
Saleem Abdulrasool
d61b8855e9 stdlib: map wchar_t to UInt16 on Windows
This is an ABI breaking change for Windows.  `WCHAR` on Windows is
mapped to `short` (`-fshort-wchar` makes it `unsigned short`).  When C++
interop is enabled, `WCHAR` will be mapped to `wchar_t` which is then
mapped to `short` (or `unsigned short` if `-fshort-wchar` is specified).
Correct the mapping type to get the desired behaviour.
2023-10-09 20:02:48 -07:00
Egor Zhdan
926653d9fa [cxx-interop][SwiftToCxx] Print operator keyword when mapping Swift operator to C++
Previously we emitted this:
```
SWIFT_INLINE_THUNK bool ==(const IntBox& lhs, const IntBox& rhs)
```
which is not valid in C++.

Now we'll emit this:
```
SWIFT_INLINE_THUNK bool operator==(const IntBox& lhs, const IntBox& rhs)
```

rdar://114772296
2023-09-01 20:06:41 +01: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
97c25ae854 [interop][SwiftToCxx] nested types are not yet supported so do not emit references to them 2023-08-02 13:03:05 -07:00
Alex Lorenz
a3a0a70899 [interop][SwiftToCxx] NFC, add additional test for https://github.com/apple/swift/pull/67241 2023-07-13 13:02:45 -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
Kavon Farvardin
90077ab593 update diagnostics in clang importer
rdar://109281444
2023-05-24 20:56:37 -07:00
Alex Lorenz
9d7460f130 [interop][SwiftToCxx] NFC, add missing include to swift-class-execution.cpp test 2023-05-08 06:54:57 -07:00
Alex Lorenz
f0967a4af1 Revert "Disable test/Interop/SwiftToCxx/class/swift-class-execution.cpp"
This reverts commit 5ce8b6cb06.
2023-05-08 06:54:47 -07:00
Arnold Schwaighofer
5ce8b6cb06 Disable test/Interop/SwiftToCxx/class/swift-class-execution.cpp
It failed on a package bot.
https://ci.swift.org/job/oss-swift-package-ubi-9/506/consoleText
rdar://109037144
2023-05-08 06:38:31 -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
16b1fe173f [interop][SwiftToCxx] moving a Swift class in C++ performs a copy
C++ does not have a consuming move, so fallback to copy instead

Resolves https://github.com/apple/swift/issues/64702
2023-05-07 08:55:20 -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
82ef9d35e7 [interop][SwiftToCxx] do not emit unavaialble stubs for internal/private decls
only emit them for public ones
2023-05-06 14:51:18 -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