61 Commits

Author SHA1 Message Date
Anthony Latsis
bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Gabor Horvath
45f0be2dd4 [cxx-interop] Properly support OS objects in reverse interop
These objects are behind typedefs and user code supposed to use the
typedef names. We already have some logic in place for Obj-C interop.
Reuse the same logic for C++ to use the correct names.

rdar://150453489
2025-08-12 18:06:08 +01:00
Gabor Horvath
c5b18a0a9e [cxx-interop] Support types nested in extensions
The generated header did not compile due to a bug that prevented us from
referencing the correct namespaces derived from the nominal type's name
(an extension does not have a name). Moreover, we did not generate
forward declarations for the members of the extensions for classes and
enums (but we did for structs). This change also removes a workaround
that emitted String::Index as _String_Index.

rdar://153221450
2025-07-02 11:36:04 +01:00
Gabor Horvath
b3b20310f5 [cxx-interop] Fix a rare compilation error in reverse interop header
To trigger this error one needs to import a nested type from C++, use it
in a generic context in Swift, and export it back to C++. We were
inconsisent in what namespace did we declare the functions to get the
type metadata for types. It was in the swift namespace for foreign types
and in the module namespace for Swift types. This PR standardizes on how
the metadata function is declared and called to fix the issue.

Fixes #80538.

rdar://148597079
2025-04-07 17:50:19 +01:00
Gabor Horvath
616de41526 [cxx-interop] Fix nested structs for non-opaque types
The test for nested constructs used library evolution forcing all types
to be opaque. As a result some code paths for non-opaque types were not
updated to support nested types. This patch updates the rest of the code
making sure we use fully qualified names (so they also work in the
context of the nested classes), and generate correct names for the C
compatibility structs that cannot contain "::".

Fixes #80291

rdar://147882976
2025-04-03 15:30:01 +01:00
Dylan Sturgeon
2c8e337f25 Merge pull request #80074 from dylansturg/objc_enum_refs
The Error enum synthesized declarations, e.g. the struct and its static accessors, should generally appear to be identical to the underlying Clang definitions. There are some specific use cases where the synthesized declarations are necessary though.

I've added an option for USR generation to override the Clang node and emit the USR of the synthesized Swift declaration. This is used by SwiftDocSupport so that the USRs of the synthesized declarations are emitted.

Fixes 79912
2025-03-25 11:21:21 +00:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08: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
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Slava Pestov
bbbfc62e6d ClangImporter: Fixes for non-copyable generics 2024-01-31 21:56:45 -05: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
0727d04c9a [interop][SwiftToCxx] correctly emit names for anonymous structs that use a typedef when emitting a Clang type reference in the generated header 2023-08-01 18:43:12 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -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
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
John McCall
0e932329c3 Fix fulfillments for type metadata and witness table packs
A lot of the fixes here are adjustments to compensate in the
fulfillment and metadata-path subsystems for the recent pack
substitutions representation change.  I think these adjustments
really make the case for why the change was the right one to make:
the code was clearly not considering the possibility of packs
in these positions, and the need to handle packs makes everything
work out much more cleanly.

There's still some work that needs to happen around type packs;
in particular, we're not caching them or fulfilling them as a
whole, and we do have the setup to do that properly now.
2023-03-10 12:52:37 -05: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
8790a9e0cf Revert "Revert "Print the Swift mangled name as a field in C++ interop generated classes""
This reverts commit 773a2aac96.
2023-02-27 14:23:08 -08:00
Augusto Noronha
773a2aac96 Revert "Print the Swift mangled name as a field in C++ interop generated classes"
This reverts commit 5b12a156f0.
2023-02-27 10:58:48 -08:00
Augusto Noronha
d7278e1d30 Always print ignored Cxx17 diag when printing name for debugger 2023-02-27 10:26:07 -08:00
Augusto Noronha
239d4e23cb Rename __swift_mangled_name to $__swift_mangled_name 2023-02-27 10:14:21 -08:00
Augusto Noronha
5b12a156f0 Print the Swift mangled name as a field in C++ interop generated classes
Print the Swift mangled name as a constexpr static char as a field in
compiler generated types so LLDB can display them nicely to users.
2023-02-24 14:01:31 -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
327b989e84 [interop][SwiftToCxx] NFC, refactor printKnownCType method to ClangSyntaxPrinter for reuse 2023-02-13 10:30:25 -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
Slava Pestov
65d2d4d621 IRGen: Encapsulate fields of GenericRequirement better 2022-12-11 22:14:37 -05:00
Alex Lorenz
510b7675f4 [interop][CxxToSwiftToCxx] fix the tests for unsupported class template instantions; emit C++ type metadata accessors for type aliased templates 2022-12-08 21:28:29 -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
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
391b92fff7 [interop][SwiftToCxx] add some helper methods to work with Swift's Optional in C++ 2022-10-05 14:21:13 -07:00
Alex Lorenz
f6cc59246b [interop][SwiftToCxx] fix the include path for swiftToCxx shim header for clang in /usr/local/bin/clang 2022-09-30 07:59:46 -07:00
Alex Lorenz
dd7e068391 [interop][SwiftToCxx] add a Swift stdlib overlay header
It currently provides begin and end functions for array iteration
2022-09-29 07:41:54 -07:00
Alex Lorenz
26e5742fe7 [interop][SwiftToCxx] emit generic type traits for C++ types bridged into Swift and then back to C++ 2022-09-12 12:39:30 -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
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
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
7d6cdbc445 [interop][SwiftToCxx] pass generic type to a function by value (non specialized generic though) 2022-08-31 14:02:17 -07:00
Alex Lorenz
45d209fe1c [interop][SwiftToCxx] initial generic struct support 2022-08-31 14:02:15 -07:00
Tongjie Wang
cbc23cbafc [Interop][SwiftToCxx] Support enum creation from Cxx 2022-08-29 20:16:52 -07:00
Alex Lorenz
06d9fd24d4 [interop][SwiftToCxx] add experimental Swift::String -> NSString conversion operator 2022-08-25 15:09:30 -07:00
Alex Lorenz
8de79390be [interop][SwiftToCxx] add @_expose(Cxx) attribute support
This attribute instructs the compiler that this declaration should be included in the generated C++ bindings
2022-08-24 10:14:07 -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
Tongjie Wang
c2e8ec96eb Add variable name and indent parameter...
... to value witness table access sequence function
2022-07-18 18:28:58 -07:00