Commit Graph

613 Commits

Author SHA1 Message Date
Egor Zhdan
2db0e8aea8 Merge pull request #85439 from egorzhdan/egorzhdan/endif-objc
[cxx-interop] Emit `#endif // defined(__OBJC__)` with the comment consistently
2025-11-13 03:59:17 +00:00
Egor Zhdan
8cafef09c3 [cxx-interop] Emit #endif // defined(__OBJC__) with the comment consistently
This makes sure that whenever we emit `#if defined(__OBJC__)`, the matching `#endif` has a comment `// defined(__OBJC__)`.

This makes both testing and reading the header file easier.
2025-11-12 11:51:10 +00:00
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
Alexis Laferrière
94113f4a83 SE-496: Remove references to features CDecl and CImplementation 2025-10-29 17:31:20 -07:00
Allan Shortlidge
92227ad1cf AST: Introduce the Swift, anyAppleOS, and DriverKit platform kinds.
This change just stages in a few new platform kinds, without fully adding
support for them yet.

- The `Swift` platform represents availability of the Swift runtime across all
  platforms that support an ABI stable Swift runtime (see the pitch at
  https://forums.swift.org/t/pitch-swift-runtime-availability/82742).
- The `anyAppleOS` platform is an experimental platform that represents all of
  Apple's operating systems. This is intended to simplify writing availability
  for Apple's platforms by taking advantage of the new unified OS versioning
  system announced at WWDC 2025.
- The `DriverKit` platform corresponds to Apple DriverKit which is already
  supported by LLVM.
2025-10-27 19:15:04 -07:00
Hamish Knight
a1e1656ed5 [AST] Remove respectOriginallyDefinedIn parameter from mangleAnyDecl
This was always set to `true` except for USR mangling, where we already
have it set to `false` for IDE USRs. The other clients were:

- AutoDiff, which is just using the resulting string as a dictionary
key, so don't seem to have any preference.
- The ClangImporter, which always overrides `@_originallyDefinedIn`
anyway.
2025-10-23 09:11:17 +01:00
Gabor Horvath
adca01b1e5 [cxx-interop] Add flag to set minimum access level for reverse interop
rdar://159211965
2025-10-20 10:27:50 -07:00
Mads Odgaard
c92e5b47f3 Merge pull request #84574 from madsodgaard/android-availability 2025-10-20 10:40:37 +09:00
Gábor Horváth
6f2d78432f Merge pull request #84230 from Xazax-hun/rebranch-errors
[cxx-interop] Attempt to fix some windows test failures on rebranch
2025-09-25 18:02:05 +01:00
Gabor Horvath
a9734830c2 [cxx-interop] Attempt to fix some windows test failures on rebranch
Unfortunately, I did not find an easy way to fix the warning so
suppressed it for now. But I don't think we care about those symbols
being duplicated across different shared libraries.

rdar://159879290
2025-09-24 10:15:06 +01:00
Alexis Laferrière
e5a9b6463d Merge pull request #84381 from xymus/rename-cdecl-to-c
Parser: Rename the experimental attribute `@cdecl` to the shorter `@c`
2025-09-23 14:58:46 -07:00
Alexis Laferrière
9ec824c20b Parser: Rename the experimental attribute @cdecl to @c
There's no users of `@cdecl` yet so we can do a direct rename. The
legacy `@_cdecl` remains unaffected.
2025-09-19 11:55:07 -07:00
Gabor Horvath
98078817d0 [cxx-interop] Support SIMD types in reverse interop
This supports both built-in vector types and the SIMDX<T> Swift types
that are used through type aliases like float3. These SIMD types are
passed via compatibility structs (with their valued memcpy-d into the
ABI compatible struct) as they have special ABI rules. E.g., a float3 is
not passed as float3, but as float4 on the ABI level.

Previously, we did not expose simd types from Swift to C++ (unless the
SIMD types were impoted from clang).

rdar://153218744
2025-09-09 14:50:54 +01:00
Gabor Horvath
7ac9a81b1e [cxx-interop] Add attribute to hide Swift declarations from interop
This can help work around problems when the names of a C++ declaration
and a Swift declaration would collide, or to temporarily work around
compiler bugs.

rdar://152838988&140802127&158843666
2025-09-01 12:29:34 +01:00
Slava Pestov
a5a8cf4a36 Merge pull request #83858 from slavapestov/covariant-result-type-part-2
AST: Continue process of removing replaceCovariantResultType()
2025-08-28 10:07:13 -04:00
Egor Zhdan
0e1be7769b Merge pull request #83701 from ludwwwig/interop-strcpy
[cxx-interop] Copy lazily bridged Cocoa strings correctly without hanging
2025-08-27 22:13:53 +01:00
Slava Pestov
1716832a7a AST: Remove most usages of AbstractFunctionDecl::hasDynamicSelfResult() 2025-08-27 15:34:26 -04:00
Alexis Laferrière
7642d410af Merge pull request #83814 from xymus/cdecl-enum-layout
IRGen: Use C compatible representation for `@cdecl` enums
2025-08-20 10:39:09 -07:00
Alexis Laferrière
b624ee96fe AST: Introduce EnumDecl::isCDeclEnum and isCCompatibleEnum 2025-08-19 13:40:09 -07:00
Egor Zhdan
d837d6052c Merge pull request #83811 from egorzhdan/egorzhdan/ambiguous-init
[cxx-interop] Avoid ambiguous C++ overloads for functions from Swift extensions
2025-08-19 20:29:29 +01:00
Egor Zhdan
cfbdd76493 [cxx-interop] Avoid ambiguous C++ overloads for functions from Swift extensions
Reverse interop has logic that avoids emitting ambiguous overloads into the generated header. That logic did not apply for functions declared within Swift extensions. This meant that the generated C++ header would sometimes not compile.

rdar://158252800
2025-08-19 15:27:26 +01:00
Gabor Horvath
ad502146b3 [cxx-interop] Make reverse interop header compile in embedded mode
These are the minimal changes to make the reverse interop header compile
when embedded Swift is used. Previously, the _SwiftStdlibCxxOverlay.h
and the generated interop header disagreed on the mangling of a symbol.

This is not sufficient yet to use the embedded Swift standard library from
reverse interop, there are some missing symbols while linking. But this
PR enables doing reverse interop without using the stdlib types like
swift::String in C++. Follow-up PRs will fix the rest of the issues.

rdar://154740225
2025-08-18 13:11:43 +01:00
Ludwig Hollmann
54f315348f Copy lazily bridged Cocoa strings correctly without hanging 2025-08-14 23:45:37 +02: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
Alexis Laferrière
09edc47db5 Merge pull request #83550 from xymus/scalar-printing
PrintAsClang: Use proper types and look through typealiases in PrimitiveTypeMapping
2025-08-11 11:52:25 -07:00
Gabor Horvath
a8a4f8af19 [cxx-interop] Fix passing optional CoreFoundation types from Swift to C++
These types are passed around as reference counted pointers, so the
optional representation of them are nullable pointers. Previously, we
tried to wrap them in swift::Optional.

rdar://157667946
2025-08-08 14:00:12 +01:00
Alexis Laferrière
6e8857a6d6 PrintAsClang: Apply review comments 2025-08-07 12:28:01 -07:00
Alexis Laferrière
20c7334db4 PrintAsClang: Print Unicode.Scalar as char32_t and Float16 as _Float16 2025-08-05 14:31:30 -07:00
Alexis Laferrière
ed61bcf121 PrintAsClang: Use TypeDecl to associate Swift types to C printed type names
There was a hard to see misalignment between types accepted as
representable in C languages defined in `BuiltinMappedTypes.def` and
those with an associated C type name defined in `PrimitiveTypeMapping`.
While `BuiltinMappedTypes` looked through typealiases,
`PrimitiveTypeMapping` didn't and instead referred only to types by
their identifier. So even when both files defined the same types by
name, their underlying type were only partially handled. An affected
type was `Unicode.Scalar` which is the underlying type of `CWideChar`
and `CChar32`. `Unicode.Scalar` was accepted as a C representable type
but was not printed using a corresponding C type name, breaking the
generated compatibility header.

Another issue with the use of identifiers in the PrimitiveTypeMapping
logic was a limited support for nested types. It would track
`Unicode.Scalar` only as `Scalar`.

This change updates `PrimitiveTypeMapping` to keep track of TypeDecls
instead of identifiers and look through typealiases. This fixes the
issue with `Unicode.Scalar`, allowing it to be printed using the type
defined by an alias and avoid matching an unrelated Scalar. It should
also prevent future types behind typealiases to trigger this same kind
of misalignment.

rdar://157332446
2025-08-05 13:36:37 -07:00
Alexis Laferrière
54362d846c PrintAsClang: Protect null dereference and document Scalar behavior
Fix crash when printing references to Unicode.Scalar in the
compatibility header.

Unicode.Scalar should not be printed in the first place, it's a Swift
struct. It should either be considered non-representable or printed as a
C / Objective-C type if that's the intent.

rdar://157120538
2025-07-31 17:22:11 -07:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Allan Shortlidge
58bf087a0e PrintAsClang: Add support for availability attrs with custom domains.
Resolves rdar://154510571.
2025-07-23 21:53:05 -07:00
Gabor Horvath
4b64abdc45 [cxx-interop] Configure requires ObjC from frontend option
We sometimes don't have the information in the modulemaps whether a
module requires ObjC or not. This info is useful for reverse interop.
This PR introduces a frontend flag to have a comma separated list of
modules that we should import as if they had "requires ObjC" in their
modulemaps.
2025-07-17 21:29:50 +01:00
Gabor Horvath
9b1b9b774b [cxx-interop] Types exposed from ObjC modules should be behind a macro
Functions or template instantiations with Obj-C types should always be
behind a macro to make sure the interop header compiles cleanly in C++.

rdar://152836730
2025-07-11 18:22:39 +01:00
Gabor Horvath
a13940f1c4 [cxx-interop] Do not generate aborting move constructors
In the reverse interop header we generated move constructors that call
abort at runtime. This is problematic for several reasons:
* In C++14 and below some of our own generated functions like _make
  ended up calling the move constructor. Those are calling abort and
  also trigger unreachable code warning in newer versions of Clang.
  In C++17 and up it is fine due to the guaranteed copy elision.
* Type traits are fooled and think these types are movable. As a result,
  libraries could generate calls to the aborting move ctor.

This PR removes the generation of move operations. As we generate copy
operations, the compiler will not declare or define the move operations
implicitly. Whenever the user goes out of their way and try to move an
object they will get a copy instead.

rdar://150793518
2025-07-08 16:45:58 +01:00
Gábor Horváth
a15a2a4929 Merge pull request #82684 from swiftlang/gaborh/reverse-interop-forward-decl-crash
[cxx-interop] Fix a crash when exposing @objc Swift classes
2025-07-03 04:38:19 +01:00
Gabor Horvath
201e9b437c [cxx-interop] Fix a crash when exposing @objc Swift classes
rdar://154252454
2025-07-02 16:11: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
Alexis Laferrière
529cc3ccb6 Merge pull request #82039 from xymus/cdecl-enum
PrintAsClang: Introduce `@cdecl` enums
2025-06-30 09:56:28 -07:00
Alexis Laferrière
bd110a073d PrintAsClang: Forward reference enums when used transitively
There are two main scenarios when printing a compatibility header that
references a @cdecl enum defined in Swift code. (1) When defined in the
same module as it's used we can print the definition normally and then
reference it. (2) When used in a different mode we need to print a
forward declaration before we can reference it.

This change adds printing the forward declaration and fix an issue where
the compiler would instead print an @include of the Swift module. The
import of the Swift module would work only in a local scenario where a
compatibility header and module would be generated under the same name.
However for a distributed frameworks we do not distribute the
compatibility header so this strategy doesn't work. Relying on a forward
declaration should be more reliable in all cases but clients may need to
import the other compatibility header explicitly.
2025-06-09 11:54:13 -07:00
Alexis Laferrière
138e2daa3e PrintAsClang: Print @cdecl enums in the compatibility header
Print @cdecl enums in the C section of the compatibility header. Use and
extend the macros to support C compiler clients.

The macro is adapted to the features supported by the client compiler.
It uses an Objective-C style macro with raw type when available and
fallbacks to a simple typedef for C compatibility.
2025-06-09 11:54:13 -07:00
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