Commit Graph

62 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
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
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
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
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
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
Alex Lorenz
394c5bc7ce [interop] fix tests after fixing private annotations on the 'swift' namespace 2023-03-18 19:30:06 -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
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
7caf34c190 [interop][SwiftToCxx] apply inline and artificial attributes to lambdas used with returnNewValue 2023-03-03 11:45:46 -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
cc5b557cee [interop][SwiftToCxx] Attach SWIFT_SYMBOL to C++ enum case declarations 2023-01-12 14:01:30 -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
8336edd344 [interop][SwiftToCxx] add support for constructing generic enum cases in C++
We only support cases with no payload, or one associated value still
2022-11-03 12:21:27 -07:00
Alex Lorenz
c179e23b19 [interop][SwiftToCxx] add static_assert type checking for generic types in pre-C++20 mode 2022-11-01 17:36:37 -07:00
Alex Lorenz
a66cc52109 [interop][SwiftToCxx] add static_assert type checking for generic functions in pre-C++20 mode
In the future we should also use enable_if to correctly resolve overloads
2022-11-01 17:21:01 -07:00
Alex Lorenz
4b52ae08fa [interop][SwiftToCxx] check that generics can be built in C++17/C++14 mode 2022-11-01 16:40:42 -07:00
Alex Lorenz
953c124ceb [interop][SwiftToCxx] guard 'requires' clauses in __cpp_concepts #ifdefs 2022-11-01 16:17:10 -07: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
2132cc91d0 [interop][SwiftToCxx] do not use std::move to move the returned C++ value
move needs <utility>, which is not includable in the test SDK.
2022-09-19 12:25:42 -07:00
Alex Lorenz
7925701ed3 [interop][SwiftToCxx] support returning class values in enum's generic associated value getter 2022-09-14 12:37:27 -07:00
Alex Lorenz
773a0436cf [interop][SwiftToCxx] extract generic associated value from enum 2022-09-14 12:21:18 -07:00
Alex Lorenz
07337cdaf6 [interop][SwiftToCxx] initial generic enum support 2022-09-14 11:37:50 -07:00
Alex Lorenz
0dcb8b58e5 [interop][SwiftToCxx] use C++ types bridged to Swift in Swift generic context from C++ ♾ 2022-09-12 13:05:49 -07:00
Alex Lorenz
4c7015ad68 [interop][SwiftToCxx] ensure LoadableByAddress lowering is done on the SILFunction for while we're lowering the signature for
This correctly now passes self parameters for direct values that are too large to be direct
2022-09-07 17:42:50 -07:00
Alex Lorenz
1696f94223 [interop][SwiftToCxx] reimplement function lowering to correctly distinguish between direct/indirect return values and parameters 2022-09-07 11:43:38 -07:00
Alex Lorenz
3e4b52c42a [interop][SwiftToCxx] add support for generic methods in generic structs ♺ 2022-09-01 13:01:26 -07:00
Alex Lorenz
6e8b0be181 [interop][SwiftToCxx] add test to cover initializer in generic struct 2022-09-01 12:35:51 -07:00
Alex Lorenz
d9f4c072cc [interop][SwiftToCxx] correctly invoke property accessors in generic structs 2022-09-01 06:57:49 -07:00
Alex Lorenz
a5043c354d [interop][SwiftToCxx] add support for invoking methods in generic structs 2022-08-31 20:19:19 -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
4154b56745 [interop][SwiftToCxx] unify generic function printing with generic struct printing
This is a pre-requisite for supporting methods in generic structs.
2022-08-31 15:31:04 -07:00
Alex Lorenz
907b3a949c [interop][SwiftToCxx] NFC, test remove unused warning flag 2022-08-31 14:02:17 -07:00
Alex Lorenz
f89f64a5cb [interop][SwiftToCxx] add support for passing generic structs with concrete type params 2022-08-31 14:02:17 -07:00
Alex Lorenz
031b779031 [interop][SwiftToCxx] add support for returning generic types bounded to concrete generic params 2022-08-31 14:02:17 -07:00
Alex Lorenz
c526ac8a0d [interop][SwiftToCxx] add 'inout' support for passing generic struct 2022-08-31 14:02:17 -07:00
Alex Lorenz
7012efd2cd [interop][SwiftToCxx] add a testcase for generic struct passthrough 2022-08-31 14:02:17 -07:00
Alex Lorenz
6f43efe264 [interop][SwiftToCxx] add a restriction for calling only supported type metadata accessor function for generic types 2022-08-31 14:02:17 -07:00
Alex Lorenz
9f26709ffc [interop][SwiftToCxx] cleanup generic struct tests 2022-08-31 14:02:17 -07:00