Commit Graph

92 Commits

Author SHA1 Message Date
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
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
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
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
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
Alexander Cyon
db0b5db54e [test/Interop] Fix typos (#75032) 2024-08-28 09:41:09 -07: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
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
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
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
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
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
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
Alex Lorenz
c7836e3a8e [interop][SwiftToCxx] avoid emitting ambiguous C++ overloads
Just do an arity check for now
2023-05-05 17:07:43 -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
601802820e [interop][SwiftToCxx] do not assert when emitting a public var and function with the same C++ name 2023-04-24 14:03:46 -07:00
Alex Lorenz
75fd11cffc [interop][SwiftToCxx] hide swift::Error APIs by default in C++ Swift stdlib overlay
They're still experimental
2023-03-10 08:34:45 -08:00
Alex Lorenz
0dc90d38c1 [interop][SwiftToCxx] emit Swift's stdlib inside of 'swift' namespace 2023-03-09 17:32:43 -08:00
Alex Lorenz
c2a93f0480 [interop][SwiftToCxx] allow emission of overloaded functions with the same name 2023-03-02 16:34:06 -08:00
Alex Lorenz
1d3797308d [interop][SwiftToCxx] do not expose functions / properties that throw without the experimental feature 2023-03-02 15:22:36 -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
3948a2a5d3 [interop][SwiftToCxx] annotate inline thunks with SWIFT_INLINE_THUNK
This macro applies always_inline in addition to inline. It also applies artificial, which lets debugger know that this is an artificial function. The used attribute is added in debug builds to ensure that the symbol is emitted in the binary so that LLDB can invoke it.
2023-02-24 11:23:46 -08:00
Alex Lorenz
0b0cc99c98 [interop][SwiftToCxx] avoid -Wshadow warning for C++ representation of enum member parameters 2023-02-08 12:33:18 -08:00
Alex Lorenz
3b3d6c825a [interop][SwiftToCxx] Annotate emitted declarations with Clang's external_source_symbol attribute
Each emitted declaration is annotated with the external_source_symbol with its own USR, to allow Clang's indexer to recognize this declaration as a Swift declaration with a specific USR
2023-01-12 12:15:01 -08:00
Alex Lorenz
a6e056ba7e Merge pull request #62701 from Robertorosmaninho/cxx-interop/RefactorSwiftReturnThank
Refactor C++ to return Swift::Expected instead of SWIFT_RETURN_THUNK
2022-12-20 07:31:55 -08:00
Roberto Rosmaninho
fc68fb304e Refactoring the C++ thunk to directly return the Swift::Expected intead od the SWIFT_RETURN_THUNK macro 2022-12-19 19:48:35 -03:00
Alex Lorenz
8cd18670bf [interop][SwiftToCxx] disable the arm64e testing for swift-expected-execution.cpp 2022-12-19 10:13:54 -08:00
Roberto Rosmaninho
ce666ac592 [Interop][SwiftToCxx] Adding support to return Swift::Expected when exceptions are not available. 2022-12-14 12:07:43 -03:00
Roberto Rosmaninho
a2633d942b [Interop][SwiftToCxx] Fixing swift-expected-execution.cpp test 2022-12-14 12:07:43 -03:00
Roberto Rosmaninho
782bc6aa6f [Interop][SwiftToCxx] Moving swift::Expected to _SwiftStdlibCxxOverlay.h 2022-12-14 12:07:43 -03:00
Roberto Rosmaninho
74450be9d2 [Interop][SwiftToCxx] Moving the max function to _impl namespace and assigning swift-expected-execution.cpp as unsupported by Windows. 2022-12-14 12:07:43 -03:00
Roberto Rosmaninho
6d1696260f [Interop] [SwiftToCxx] Using buffer to create an instance for either a type or an Error and fixing minor errors 2022-12-14 12:07:43 -03:00
Roberto Rosmaninho
3f6351d2ee [Interop][SwiftToCxx] Add new line 2022-12-14 12:07:43 -03:00
Roberto Rosmaninho
c42dfea346 [Interop][SwiftToCxx] Creating test cases for swift::Expected 2022-12-14 12:07:43 -03:00
Alex Lorenz
5c703b4f5c [interop][SwiftToCxx] support std::string -> Swift::String conversion in C++ 2022-12-13 13:13:05 -08:00
Alex Lorenz
d0e5372fc0 [interop][SwiftToCxx] give explicit error for @alwaysEmitIntoClient exposed functions 2022-12-06 11:44:29 -08:00
Roberto Rosmaninho
b42f45cb21 [Interop][SwiftToCxx] Modifying tests to verify the that the dynamic cast error returns a Swift::Optional 2022-11-18 22:16:00 -03:00
Allan Shortlidge
b93a40f03e Test: Temporarily disable swift-functions-errors-execution.cpp on arm64e. 2022-11-10 18:21:57 -08:00
Alex Lorenz
0312e0613d [interop][SwiftToCxxToSwift] hide reverse interop module namespaces from forward interop 2022-11-07 15:52:04 -08:00
Alex Lorenz
bd6367986e [interop][SwiftToCxx] report an error for attempts to explicitly expose 'async' functions to C++
also fixup distributed actor example in interop test
2022-11-03 13:46:52 -07:00
Roberto Rosmaninho
5102d19af8 Fixing typo in swift0functions-errors-executions.cpp 2022-10-26 22:22:43 -03:00
Roberto Rosmaninho
edb95c576b Adding unsupported flag for windows-msvc 2022-10-26 20:25:42 -03:00
Roberto Rosmaninho
c92dc41c41 Deleting unnecessary includes and compilation flags 2022-10-21 17:29:00 -03:00
Alex Lorenz
fdc0632659 [interop][SwiftToCxx] do not emit domain ObjC error values in C++ mode 2022-10-21 12:47:18 -07:00
Roberto Rosmaninho
dced1cae29 Fixing test to check the new realist implementation of dynamic cast 2022-10-21 10:22:30 -03:00
Roberto Rosmaninho
5b8a2e213e [SwiftToCxx] Fixing as signature 2022-10-19 17:12:55 -03:00
Roberto Rosmaninho
5af07a470c test/Interop/SwiftToCxx/functions/swift-functions-errors.swift 2022-10-18 18:53:30 -03:00