Commit Graph

20 Commits

Author SHA1 Message Date
John Hui
5dbc8c25f9 [cxx-interop] Support ownership annotations on FRT constructors
We were previously marking synthesized FRT initializers as
"returns_retained" unconditionally (for non-immortal types), which can
cause memory errors and also suppresses diagnostics about the lack of
such annotations.

This patch fixes that behavior, and also adjusts the source locations of
the synthesized C++ factory method to point to the actual constructor
decl when its location is available, so that the diagnostics make sense
when warning about the lack of annotations.

rdar://163127315
2026-01-21 16:23:53 -08:00
Gabor Horvath
77187a04ab [cxx-interop] Introduce SWIFT_REFCOUNTED_PTR
This attribute introduces some conversions between the annotated smart
pointer type and the native swift reference type. In the future we plan
to introduce bridging so the smart pointer type can be hidden in the
signature and we can automatically convert it to the native Swift
reference type on the ABI boundaries.

This PR is using a new way to use swift_attr attributes. Instead of
doing the parsing in the clang importer it falls back to Swift's
attribute parsing and makes sure that the annotation has valid Swift
attribute syntax.

rdar://156521316
2025-12-05 18:19:10 +00:00
Gabor Horvath
c473c55ce3 [cxx-interop] Fix crash when virtual methods take move-only types
We build forwarding methods to call the virtual methods. The forwarding
methods tried to copy move-only types which resulted in a compiler
crash. Now we try to detect this scenario and insert the required cast
to make sure we get a move instead.

rdar://162195228
2025-11-26 15:41:57 +00:00
fahadnayyar
eef4780053 [cxx-interop] Fix type-level ownership annotations for C++ APIs returning reference to frt (#84480)
Type-level ownership annotations
(`SWIFT_RETURNED_AS_UNRETAINED_BY_DEFAULT`) were not suppressing warning
for C++ functions (which are not annotated with
`SWIFT_RETURNS_(UN)RETAIND`) returning references (`&`) to foreign
reference types. The annotation only worked for pointer returns (`*`),
forcing developers to manually annotate every function returning a
reference.
This PR fixes this problem by changing the logic of
`matchSwiftAttrOnRecordPtr` to consider `ReferenceType` also along with
`PointerType`.

This PR also adds comprehensive lit tests for ownership conventions and
diagnostics related to C++ functions that return references to foreign
reference types. These tests were previously not covered by our lit
tests.

rdar://161226212
2025-09-26 19:33:16 -07:00
Egor Zhdan
e78ce6165f [cxx-interop] Allow retain/release operations to be methods
Some foreign reference types such as IUnknown define retain/release operations as methods of the type.

Previously Swift only supported retain/release operations as standalone functions.

The syntax for member functions would be `SWIFT_SHARED_REFERENCE(.doRetain, .doRelease)`.

rdar://160696723
2025-09-22 12:01:17 +01:00
Gabor Horvath
fe3cc6d024 [cxx-interop] Handle Unowned values in implicit value ctors
We usually have unowned values when dealing with foreign types. Make
sure the implicit value ctors will do a +1 to balance the releases. In a
release build we had a use after free over-releasing the object. In
assert builds we had an assertion failure:

Assertion failed: (value->getOwnershipKind() == OwnershipKind::Guaranteed),
function forBorrowedObjectRValue, file ManagedValue.h, line 181.

rdar://160232360
2025-09-12 12:01:59 +01:00
Egor Zhdan
0a766e59ce [cxx-interop] Pass foreign reference types with correct level of indirection
When calling a C++ function that takes a reference to a pointer to a foreign reference type, Swift would previously pass a pointer to the foreign reference type as an argument (instead of a reference to a pointer), which resulted in invalid memory accesses.

This was observed when using `std::vector<ImmortalRef*>::push_back`.

rdar://150791778
2025-08-22 11:56:57 +01:00
Gabor Horvath
087793394c [cxx-interop] Fix import virtual methods with rvalue ref params
We generate forwarding calls for virutal methods. These forwarding calls
had a type error when the original parameter had an rvalue reference
type. In those scenarios we need to insert a static cast to make the
type checker happy.

rdar://154969620
2025-08-01 17:26:05 +01:00
Doug Gregor
72be0e0851 [Clang importer] Import incomplete SWIFT_SHARED_REFERENCE types
Normally, Swift cannot import an incomplete type. However, when we are
importing a SWIFT_SHARED_REFERENCE type, we're always dealing with
pointers to the type, and there is no need for the underlying type to
be complete. This permits a common pattern in C libraries where the
actual underlying storage is opaque and all APIs traffic in the
pointer, e.g.,

    typedef struct MyTypeImpl *MyType;
    void MyTypeRetain(MyType ptr);
    void MyTypeRelease(MyType ptr);

to use SWIFT_SHARED_REFERENCE to import such types as foreign
references, rather than as OpaquePointer.

Fixes rdar://155970441.
2025-07-22 20:20:23 -07:00
Egor Zhdan
62d56067c8 [cxx-interop] Disable test with an older runtime
848fad00 introduced support for printing foreign reference types. It changes both the compiler and the runtime, and having the runtime change applied is required for the test to pass. Let's not try to run it with an old runtime.

This change also splits up a test for printing of value types from a test for printing of foreign reference types, since we don't have any runtime restrictions for value types.

rdar://153205860
2025-06-17 16:27:16 +01:00
Gabor Horvath
2f46d55b85 [cxx-interop] Support importing static factories as initializers
This PR adds a feature to import static factory functions returning
foreign reference types to be imported as initializers using the
SWIFT_NAME annotation.
2025-03-03 11:25:05 +00:00
John Hui
ec506f824b [cxx-interop] Fix swift::Type to clang::QualType conversion to correctly handle foreign reference types
For types imported from C++, the original clang::TypeDecl is saved in
the swift::Type and reused for conversions back to clang::QualType.
However, the conversion did not account for foreign reference types,
which should be mapped to a pointer to the C++ record type, rather than
the record type itself.

This bug first appeared as a function template instantiation failure
due to a sanity check performed by SwiftDeclConverter::foreignReferenceTypePassedByRef()
but may also affect other round-tripping type conversions.
The added test case demonstrates the template instantiation scenario,
where templates were being used to overcome Swift/C++ interop's current
lack of support for class inheritance.

rdar://134979343
2024-10-07 14:40:38 -07:00
fahadnayyar
ea4328384d [cxx-interop] Add SWIFT_RETURNS_RETAINED and SWIFT_RETURNS_UNRETAINED annotations to specify ownership of FRT returned by a C++ method or function
rdar://135306908
2024-09-05 11:44:31 -07:00
Akira Hatanaka
335cec0e8d [cxx-interop][IRGen] Do not pass a foreign reference type to objc_retainAutoreleasedReturnValue (#73630)
If a foreign reference type has a custom retain function, emit a call to
it instead of emitting a call to objc_retainAutoreleasedReturnValue.

rdar://117353222
2024-05-17 09:04:02 -07:00
Alex Lorenz
f7ce9aa37f [cxx-interop] Synthesized derived-to-base field getter should copy out a retainable FRT value
This matches the semantics of accessing the same field from the base class
2023-10-16 14:41:44 -07:00
Alex Lorenz
415045024c [cxx-interop] Use a synthesized C++ method when accessing a base field or subscript from a derived class synthesized method
The use of a synthesized C++ method allows us to avoid making a copy of self when accessing the base field or subscript from Swift
2023-10-16 14:34:37 -07:00
Alex Lorenz
82f8d5d1e8 [cxx-interop] add additional tests for calling base methods on FRT 2023-09-29 14:18:02 -07:00
zoecarver
c5aaa6098d [cxx-interop] Start fixing issue with layout of FRTs that contain base classes or un-importable memebers. 2022-10-17 10:18:25 -07:00
zoecarver
4021082a55 [wip][cxx-interop] Support for custom reference counting operations. 2022-07-21 10:25:57 -04:00
zoecarver
fc3b3a1d71 [cxx-interop] Implement foreign reference types.
This is an expiremental feature to allow an attribute, `import_as_ref`, to import a C++ record as a non-reference-counted reference type in Swift.
2021-12-08 15:35:18 +00:00