Commit Graph

24 Commits

Author SHA1 Message Date
Egor Zhdan
ed70a1bb1a Merge pull request #83802 from egorzhdan/egorzhdan/frt-windows
[cxx-interop] Enable reference-counted types on Windows
2025-08-21 09:56:41 +01:00
Egor Zhdan
b440c4ff70 [cxx-interop] Enable reference-counted types on Windows
This enables the use of reference-counted foreign reference types on Windows. As it turns out, the assertion that was failing on Windows previously was unnecessary. This also enabled many of the tests on Windows.

rdar://154694125 / resolves https://github.com/swiftlang/swift/issues/82643
2025-08-20 12:35:10 +01:00
Egor Zhdan
c73f528b82 [cxx-interop] Validate C foreign reference types
Swift validates the retain/release operations for foreign reference types to check for obvious errors, e.g. a wrong parameter type or return type.

That logic was only running for C++ foreign reference types. This patch enables it for C foreign reference types as well.

rdar://158609723
2025-08-18 19:18:18 +01:00
Doug Gregor
73687c9f5f [Interop] Test APINotes support for noncopyable types with SwiftDestroyOp. 2025-08-12 10:12:38 -07:00
Doug Gregor
71b96e9db9 Split out the part of the test that's failing on Windows 2025-07-29 08:05:28 -07:00
Doug Gregor
34545e0df8 Disable part of this test on Windows 2025-07-28 19:10:21 -07:00
Doug Gregor
8680de42e9 Add missing test for newtype'd struct imported as an option set 2025-07-28 15:53:55 -07:00
Doug Gregor
8909f24440 Add SWIFT_NONCOPYABLE_WITH_DESTROY to swift/bridging header
This packages up the ~Copyable and "destroy" attributes in a macro.
2025-07-28 15:51:44 -07:00
Doug Gregor
63135e8929 [Clang importer] Diagnose various issues with the new "destroy:"
When we cannot respect the "destroy:" annotation, mark the type as
deprecated with a message thst says why there is a problem. There are
various potential problems:

* Multiple conflicting destroy functions
* Destroy functions that don't meet the pattern
* Type isn't imported as a move-only type
* Type has a non-trivial destructor (in C++)
2025-07-28 13:52:50 -07:00
Doug Gregor
0821009c34 Fix test for Windows and add missing newline to header 2025-07-28 08:30:49 -07:00
Doug Gregor
6ba560fb4b [Clang importer] Allow noncopyable C structs to define "destroy" operation
A C struct can be imported as noncopyable, but C doesn't have
destructors, so there is no way to provide user-defined logic to
perform the destruction. Introduce a new swift_attr that applies to
imported noncopyable types and which provides such a "destroy"
operation. It can be used like this:

    typedef struct __attribute__((swift_attr("~Copyable")))
                   __attribute__((swift_attr("destroy:wgpuAdapterInfoFreeMembers")))
      WGPUAdapterInfo { /*...*/ } WGPUAdapterInfo;

    void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo);

This will bring the WGPUAdapterInfo struct in as a noncopyable type
that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it
is no longer in use.

Implements rdar://156889370.
2025-07-28 00:02:31 -07:00
Doug Gregor
21b9b9f713 [Clang importer] Allow C structs to be noncopyable, too
In C interoperability mode, respect the ~Copyable annotation on C
structs to import them as ~Copyable types.

Fixes rdar://156877772.
2025-07-27 20:06:48 -07:00
Doug Gregor
c5db5cced5 [Clang importer] Handle Swift conformances on typedefs and C types
The code for handling Swift conforms_to attributes was specific to
C++ record types. Generalize it to work on typedefs imported as
nominal types, also in C.

Fixes rdar://156290361.
2025-07-22 20:20:23 -07:00
Egor Zhdan
9178af3ec7 [cxx-interop] Enable foreign reference types in C interop
Most of the logic for C++ foreign reference types can be applied to C types as well. Swift had a compiler flag `-Xfrontend -experimental-c-foreign-reference-types` for awhile now which enables foreign reference types without having to enable C++ interop. This change makes it the default behavior.

Since we don't expect anyone to pass `experimental-c-foreign-reference-types` currently, this also removes the frontend flag.

rdar://150308819
2025-07-01 18:46:22 +01:00
Erik Eckstein
ba4081ee76 Optimizer: replace PredictableMemoryAccessOptimizations with MandatoryRedundantLoadElimination in the pass pipeline
PredictableMemoryAccessOptimizations has become unmaintainable as-is.
RedundantLoadElimination does (almost) the same thing as PredictableMemoryAccessOptimizations.
It's not as powerful but good enough because PredictableMemoryAccessOptimizations is actually only needed for promoting integer values for mandatory constant propagation.
And most importantly: RedundantLoadElimination does not insert additional copies which was a big problem in PredictableMemoryAccessOptimizations.

Fixes rdar://142814676
2025-02-07 11:30:35 +01:00
Egor Zhdan
8610974cba [cxx-interop] Re-enable a test for C foreign reference types
This test has been disabled since 2022.

This re-enables the test in order to have  some test coverage of the `-experimental-c-foreign-reference-types` compiler flag.

See `test/Interop/Cxx/foreign-reference/reference-counted.swift` for a very similar test that enables C++ interop.

rdar://101790203
2024-09-24 13:00:36 +01:00
Arnold Schwaighofer
79894ff461 Fix test/Interop with opaque pointers 2023-07-03 03:36:07 -07:00
Arnold Schwaighofer
c1a93e0bde Move tests over to use the %use_no_opaque_pointers option 2023-06-14 10:49:48 -07:00
Zoe Carver
ae7466f699 Merge pull request #61895 from zoecarver/add-flag-for-c-frt
Put C foreign refernece types behind a flag.
2022-11-03 13:54:01 -07:00
zoecarver
bf652b6f31 Put C foreign refernece types behind a flag. 2022-11-02 17:08:31 -07:00
zoecarver
448d16a9f0 [nfc] XFAIL Interop/C/foreign-reference.swift on rdar://101790203. 2022-11-02 13:49:07 -07:00
zoecarver
dc581b9d58 Support foreign reference types when C++ interop is disabled. 2022-10-26 14:50:09 -07:00
zoecarver
eeeb27d66e [cxx-interop] Add members to the LookupTable where possible.
If possible, add imported members to the StructDecl's LookupTable rather than adding them directly as members. This will fix the issues with ordering that #39436 poorly attempted to solve during IRGen.

This also allows us to break out most of the test changes from #39436.
2021-10-13 11:53:58 -07:00
Dmitri Gribenko
61d4793970 Added tests for edge cases in importing and mangling C structs
Some of the patterns in the new test header are special-cased by
ClangImporter (for example, the names of typedefs become names of
imported structs).

Nested structs have been historically imported in Swift according to C
rules, that is, structs lexically declared nested in other structs are
imported into the global namespace anyway. For the same reason, C
structs have been mangled in Swift names as if they are declared in the
global namespace. These rules are problematic when importing C++, see
https://bugs.swift.org/browse/SR-12803.
2020-05-15 18:50:19 +02:00