Commit Graph

29 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
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
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
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
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
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
394c5bc7ce [interop] fix tests after fixing private annotations on the 'swift' namespace 2023-03-18 19:30:06 -07:00
Alex Lorenz
0dc90d38c1 [interop][SwiftToCxx] emit Swift's stdlib inside of 'swift' namespace 2023-03-09 17:32:43 -08:00
Augusto Noronha
6f68fb0dde Merge pull request #63950 from augusto2112/print-debug-name
Print debug name
2023-03-01 15:14:55 -08:00
Augusto Noronha
c3d3c37c15 Adapt tests to have the Swift mangled name as a field in interop types 2023-02-27 15:58:03 -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
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
0312e0613d [interop][SwiftToCxxToSwift] hide reverse interop module namespaces from forward interop 2022-11-07 15:52:04 -08:00
Alex Lorenz
3a9b6dce9d [interop][SwiftToCxx] add support for emitting Swift stdlib dependency in one header file
This is the default behavior. You can disable this by specifying the -clang-header-expose-decls= flag explicitly when generating the header
2022-09-28 08:08:48 -07:00
Alex Lorenz
2a05109c58 [interop][SwiftToCxx] Declare the generic usability type trait before the C++ class that represents the Swift type
This allows you to import a method that returns the type of the context in which the method is declared when such
type is a generic parameter in another type. This means that it's now possible to bridge the initializer for
RawRepresentable enums.
2022-09-21 07:57:32 -07:00
Alex Lorenz
ef5d11aab8 [interop][SwiftToCxx] NFC, Make swift::getTypeMetadata accessor part of struct template
This allows partial template specializations for adding metadata accessor for generic types 🤩
2022-08-31 17:16:45 -07:00
Alex Lorenz
330fc0b07a [interop][SwiftToCxx] generic functions should return value types correctly 2022-08-09 05:20:58 -07:00
Alex Lorenz
7ecf84a771 [interop][SwiftToCxx] ensure that resilient class values are supported 2022-08-08 13:08:54 -07:00
Alex Lorenz
a9f262e322 [interop][SwiftToCxx] add support for passing and returning class values to generic functions 2022-08-08 08:38:28 -07:00
Alex Lorenz
7c5b160bc6 [interop][SwiftToCxx] map class inheritance to C++ class hierarchy 2022-08-03 09:35:36 +01:00
Alex Lorenz
3e3e6eab33 [interop][SwiftToCxx] NFC, add missing checks for inout class passing C++ thunk 2022-08-03 07:17:44 +01:00
Alex Lorenz
478987b54e [interop][SwiftToCxx] expose getOpaquePointer class pointer accessors via swift helper class in shims header 2022-08-02 20:15:10 +01:00
Alex Lorenz
0ca61069ea [interop][SwiftToCxx] Add a base ref counted Swift class type to _SwiftCxxInteroperability.h header 2022-08-02 18:54:14 +01:00
Alex Lorenz
c332a90a8a [interop][SwiftToCxx] retain & release class values when a C++ class reference is copy assigned 2022-08-02 17:52:49 +01:00
Alex Lorenz
e289865861 [interop][SwiftToCxx] retain class values when a C++ class reference is copy constructed 2022-08-02 17:34:21 +01:00
Alex Lorenz
e1d812a848 [interop][SwiftToCxx] pass class parameter types by 'inout' 2022-08-02 16:48:12 +01:00
Alex Lorenz
51c0a3ff05 [interop][SwiftToCxx] add support for passing class types to Swift functions 2022-08-02 15:22:16 +01:00
Alex Lorenz
a8ed297e8b [interop][SwiftToCxx] class bridging: use makeRetained to return a class value from Swift 2022-08-02 14:55:58 +01:00
Alex Lorenz
26d55a2b83 [interop][SwiftToCxx] start emitting bindings for Swift class types
This includes release on destruction, and correctly returning class values from Swift to C++.
2022-08-02 09:25:35 +01:00