Commit Graph

572 Commits

Author SHA1 Message Date
Michael Chiu
7bbafc599d Merge branch 'main' into mchiu/freebsd 2025-06-02 13:33:18 -04:00
michael-yuji
9a948a9c3b Merge branch 'main' into mchiu/freebsd 2025-05-21 16:29:16 -07:00
Alexis Laferrière
c0889d57c3 PrintAsClang: Print #include for headers used from @cdecl functions
Start printing `#include` for headers referenced from `@cdecl` function
signatures. This adds on top of the existing tiered imports. We already
print each module referenced from decls printed in the compatibility
header. Previously we printed mostly `@import` with an option to
fallback on a `#import`. This change adds a third fallback to `#include`
when the module is referenced from a `@cdecl` function signature. The
bridging header can also be imported in a similar way.
2025-05-14 16:04:37 -07:00
Alexis Laferrière
9c7f0c7550 Merge pull request #80917 from xymus/cdecl-print
PrintAsClang: Print a C block in the compatibility header for `@cdecl` functions
2025-05-13 09:21:30 -07:00
Alexis Laferrière
3623c1deb8 PrintAsClang: Apply review comments to check on should print of @cdecl 2025-05-06 16:18:49 -07:00
Ludwig Hollmann
39aa950660 Update file header comments for headers in lib. 2025-05-04 22:26:26 +02:00
Alexis Laferrière
20e1ac687a PrintAsClang: Ignore nullability attributes for C clients
In C mode we still print nullability attributes. Don't let clang warn on
them and ignore the attribute if the C compiler doesn't know them.
2025-04-18 15:27:19 -07:00
Alexis Laferrière
02933b5b28 PrintAsClang: Print @cdecl in their own block in compatibility headers
Add a block for C clients in the compatibility header. This block
contains only the `@cdecl` functions that are printed using only C
types.

This C block is printed above the Objective-C and C++ blocks as if we
add support for `@cdecl` types other languages should be able to
reference them in function signatures. Other languages block don't
duplicate printing the `@cdecl` functions either as they are already
accessible to them.
2025-04-18 15:23:23 -07:00
Alexis Laferrière
e65a1fbf51 PrintAsClang: Intro and use getKnownType for C compatibility 2025-04-18 11:43:27 -07: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
Gábor Horváth
95bd08660e Merge pull request #80495 from swiftlang/gaborh/nested-reverse-interop-fixes
[cxx-interop] Fix nested structs for non-opaque types
2025-04-04 17:00:37 +01:00
Slava Pestov
00766f7deb AST: Remove type parameter from ProtocolConformanceRef::getTypeWitnessByName() 2025-04-03 17:35:32 -04: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
Gábor Horváth
9d6305c7c3 Merge pull request #80270 from swiftlang/gaborh/opt-foreign-reference-type
[cxx-interop] Support foreing reference types in generic context
2025-03-26 15:44:34 +00:00
Anthony Latsis
a95785c5b2 Merge pull request #80260 from AnthonyLatsis/eutrema-japonicum
DiagnosticEngine: Print the ID of the wrapped, not wrapper, diagnostic
2025-03-25 23:14:43 +00:00
Gabor Horvath
d631b9a3d2 [cxx-interop] Support foreing reference types in generic context
Print the type traits in reverse interop to enable the use of foreign
reference type in generics like Swift arrays. Also make sure optional
foreign reference types can be passed around as raw pointers.

rdar://108139769
2025-03-25 13:55:33 +00: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
Anthony Latsis
77e673a723 DiagnosticEngine: Print the ID of the wrapped, not wrapper, diagnostic 2025-03-25 02:25:39 +00:00
Gábor Horváth
c74c2d6716 Merge pull request #80015 from swiftlang/gaborh/objc-class-optional
[cxx-interop] Do not create copies of nullable pointers passed as optionals
2025-03-19 16:15:43 +00:00
Gabor Horvath
e7a8fb1672 [cxx-interop] Fix exporting Swift enums with C POD associated data
These types are OK to by copied using memcpy. Previously, the generated
code assumed these types are exported swift types with all the value witness
functions.

rdar://111812577
2025-03-14 15:10:13 +00:00
Gabor Horvath
fa229469a8 [cxx-interop] Do not create copies of optionals of nullable pointers
In reverse interop, we create copies of values that will be consumed by
the Swift function. This is not necessary for pointers that are passed
as swift::Optional to Swift. These are layout compatible, and consuming
a pointer should not require us to do anything extra, hopefully ARC
would take care of all the details.

rdar://146855233
2025-03-14 13:23:46 +00:00
Michael Chiu
7f0f2ac4dd Merge swiftlang#38335 2025-03-14 02:15:15 -04:00
Gábor Horváth
3f88561f33 Merge pull request #79514 from swiftlang/gaborh/objc-in-generics
[cxx-interop] Support ObjC classes in generic context in reverse interop
2025-02-20 20:09:09 +00:00
Gabor Horvath
a6c6a005d7 [cxx-interop] Support ObjC classes in generic context in reverse interop
We have the sufficient type metadata on the Swift side so this PR only
makes sure the type traits are correctly generated.

rdar://145211212
2025-02-20 15:19:32 +00:00
Becca Royal-Gordon
f26a6fb985 [PrintAsClang] Tweak fallback member sort rules
Compare the names of all extension members first, before attempting weirder and more expensive comparisons like stringified type and mangled name. This gives us a sort order that’s a little more comprehensible to humans.
2025-02-18 17:46:20 -08:00
Becca Royal-Gordon
de63f47224 [PrintAsClang] [NFC] Refactor decl sorting
Factor ModuleContentsWriter’s declaration sorting out into a separate helper function, and additionally rework that function into a series of abstract comparisons supported by various helper functions. This declutters the function and makes the high-level logic it implements much more clear, at the cost of hiding much of the control flow inside a macro.

This also makes a very small change to the handling of generic signature comparisons: declarations without a generic signature will be factored into comparisons by comparing an empty string.
2025-02-17 17:03:04 -08:00
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
Becca Royal-Gordon
d2100b7a67 [PrintAsClang] Beef up extension sorting rule
A couple of the rules that `ModuleContentsWriter::write()` uses to sort declarations didn’t actually work because of an incorrect predicate. In addition, there were a number of situations that could come up in C++ interop (where overloading is permitted) where extensions could not be sorted. Rework extension sorting to look for more kinds of differences between extension members.
2025-02-14 21:40:48 -08:00
Gabor Horvath
8603dfe53a [cxx-interop] Support nested classes in reverse interop
Turns out we already had most of the building blocks given we already
support nested structs.

rdar://143343490
2025-02-03 14:56:31 +00:00
Becca Royal-Gordon
60f6afb76c Correct newline emission in generated headers
Eliminates extraneous newlines between top-level Objective-C declarations in `-emit-objc-header` headers. Specifically, there should now always be exactly one—no more, no less—empty line between `@end` and whatever follows it.

Besides being more aesthetically pleasing, this eliminates ordering-dependent behavior where PrintAsClang would print an extra newline when visiting an empty extension, which meant that the order in which empty and non-empty extensions were visited during printing could result in whitespace differences in the compiler output. Printing the blank line is now conditional on whether `tell()` indicates that characters were actually written to the output.

Fixes rdar://143533893.
2025-01-24 16:26:33 -08:00
Allan Shortlidge
d0b418bc28 PrintAsClang: Adopt SemanticAvailableAttr more thoroughly. 2025-01-07 07:31:29 -08:00
Allan Shortlidge
0288eaa3b7 AST: Introduce Decl::getSemanticAvailableAttrs().
This new attribute iterator returned from the query makes it simpler to
implement algorithms that need access to both the `AvailableAttr *` and its
corresponding `AvailabilityDomain`. This is also work towards making it
possible to return an optional `AvailabilityDomain` from
`Decl::getDomainForAvailableAttr()`.
2024-12-19 08:40:00 -08:00
Gabor Horvath
8a3beca30d [cxx-interop] Avoid spurious type aliases in reverse interop
To support nested structs, we emit type aliases in the outer class.
Unfortunately, we emitted these type aliases unconditionally, even if
the actualy nested struct was not emitted to the reverse interop header
(due to visibility or the construct being unsupported). This PR fixed
this issue by checking first if the nested entity should be included in
the reverse interop header.

rdar://141688074
2024-12-19 11:02:19 +00:00
Gabor Horvath
e340773dcb [cxx-interop] Fix assert failure exporting C++ types to Obj-C
This is not supported, of course. But now, instead of an assertion
failure we properly mark the declaration as unavailable.

Fixes #78190.

rdar://141492654
2024-12-16 21:08:21 +00:00
Doug Gregor
867cf285ba Merge pull request #77928 from DmT021/wp/error-wrapped-in-warn
Add DiagGroupID to Diagnostic
2024-12-04 13:11:00 -08:00
Dmitrii Galimzianov
d56b7df8a9 Add DiagGroupID to Diagnostic
This change addresses the following issue: when an error is being wrapped in a warning, the diagnostic message will use the wrapper's `DiagGroupID` as the warning's name. However, we want to retain the original error's group for use. For example, in Swift 5, async_unavailable_decl is wrapped in error_in_future_swift_version. When we print a diagnostic of this kind, we want to keep the `DiagGroupID` of `async_unavailable_decl`, not that of `error_in_future_swift_version`.
To achieve this, we add `DiagGroupID` to the `Diagnostic` class. When an active diagnostic is wrapped in DiagnosticEngine, we retain the original `DiagGroupID`.

For illustration purposes, this change also introduces a new group: `DeclarationUnavailableFromAsynchronousContext`.

With this change, we produce errors and warnings of this kind with messages like the following:

```
global function 'fNoAsync' is unavailable from asynchronous contexts [DeclarationUnavailableFromAsynchronousContext]
global function 'fNoAsync' is unavailable from asynchronous contexts; this is an error in the Swift 6 language mode [DeclarationUnavailableFromAsynchronousContext]
```
2024-12-03 20:12:11 +01:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Allan Shortlidge
88c0638f37 AST: Introduce a ValueDecl convenience for looking up the renamed decl.
Wrap `RenamedDeclRequest` instead of evaluating it directly.
2024-11-22 14:35:23 -08:00
Allan Shortlidge
36230cd9c6 AST: Use an accessor to get the PlatformKind from an AvailableAttr. 2024-11-21 09:10:36 -08:00
Tony Allevato
fe6703b0a8 Merge pull request #77224 from allevato/macro-objc
[PrintAsClang] Ensure that all macro-generated decls get printed.
2024-11-07 12:02:58 -05:00
Gábor Horváth
113f11c2d1 Merge pull request #77391 from swiftlang/gaborh/avoid-exporting-read-accessors
[cxx-interop] Fix crash trying to export read accesors
2024-11-07 15:48:15 +00:00
Jan Svoboda
89048bab2f Drop -index-header-map, adopt new Module::Headers API (#77376) 2024-11-06 14:20:01 -08:00
Gabor Horvath
e00e285df7 [cxx-interop] Fix crash trying to export read accesors
This is not yet supported, so skip exporting those accessors for now.

Fixes #77304

rdar://138944832
2024-11-06 13:08:38 +00:00
Rintaro Ishizaki
d4db99ce9d [Parse] Remove unnecessary dependencies to Parser.h
C++ swift::Parser is going to be replaced with SwiftParser+ASTGen.
Direct dependencies to it should be removed. Before that, remove
unnecessary '#include "swift/Parse/Parser.h"' to clarify what actually
depends on 'swift::Parser'.

Split 'swift::parseDeclName()' et al. into the dedicated files.
2024-11-02 01:23:59 -07:00
Hamish Knight
2d7500eda6 [AST] Remove ParenType
Today ParenType is used:

1. As the type of ParenExpr
2. As the payload type of an unlabeled single
   associated value enum case (and the type of
   ParenPattern).
3. As the type for an `(X)` TypeRepr

For 1, this leads to some odd behavior, e.g the
type of `(5.0 * 5).squareRoot()` is `(Double)`. For
2, we should be checking the arity of the enum case
constructor parameters and the presence of
ParenPattern respectively. Eventually we ought to
consider replacing Paren/TuplePattern with a
PatternList node, similar to ArgumentList.

3 is one case where it could be argued that there's
some utility in preserving the sugar of the type
that the user wrote. However it's really not clear
to me that this is particularly desirable since a
bunch of diagnostic logic is already stripping
ParenTypes. In cases where we care about how the
type was written in source, we really ought to be
consulting the TypeRepr.
2024-10-31 11:32:40 +00:00
Ben Barham
f59b2f19e6 Merge branch 'main' into 2024-rebranch-to-main 2024-10-28 13:03:35 -07:00
Gabor Horvath
22b46d3c9c [cxx-interop] Mark some zero-sized value types as unavailable
Currently, we do not support exporting zero-sized value types from Swift
to C++. It needs some work on our end as these types are not part of the
lowered signature. In the meantime, this PR makes sure that common (but
not all) zero sized types are properly marked as unavailable. This is
important as the proper diagnostic will give users a hint how to work
around this problem. Moreover, it is really easy to hit this when
someone is experimenting with interop, so it is important to not have a
cryptic failure mode.

rdar://138122545
2024-10-28 14:00:35 +00:00
Tony Allevato
eeeb73ad8a [PrintAsClang] Ensure that all macro-generated decls get printed.
Some macro-generated declarations are not being printed in the
Obj-C/C++ generated header. Members introduced by attached `member`
macros on a type appear to be fine, but those introduced by a
attached `peer` or freestanding `declaration` macros don't show up.

This change updates the header writer to call `getAllMembers`
throughout instead of `getMembers`, which makes sure that everything
gets collected. Likewise, we update the top-level logic from
`getTopLevelDecls` to `getTopLevelDeclsWithAuxiliaryDecls` to pick
up freestanding decls introduced at file scope.

Fixes https://github.com/swiftlang/swift/issues/68170.
2024-10-25 10:39:06 -04:00
swift-ci
e90f070b60 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-17 08:08:55 -07: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