Commit Graph

325 Commits

Author SHA1 Message Date
Doug Gregor 09dd9db3e1 [Embedded] Extend @export(interface) to non-generic types
@export(interface) makes it so that Embedded Swift will emit a strong
definition of a symbol in its defining module, and use that symbol in
other modules. Extend this notion to non-generic types, where we will
emit a strong definition for the full type metadata, and let it be
referenced from other modules rather than the lazy, shared emission.

Implements rdar://176392354.
2026-05-08 14:23:21 -07:00
Gabor Horvath 75e2a0a33b [cxx-interop] Fix miscompilation of some single field structs
Swift has some optimizations in place to treat structs with a single
refcounted field as if it was only the field. This logic was not
correctly taken into account for foreign reference types when a struct
like this was stored in a single element enum like an Optional.

This patch fixes this logic by updating the TypeInfo hierarchy to
correctly emit strong retain/release calls during IRGen for structs with
one fields.

rdar://170990579
2026-02-23 17:18:57 +00:00
Susana Monteiro a8ef103a83 Merge pull request #86531 from susmonteiro/susmonteiro/move-noncopyable-diagnostic
[cxx-interop] Fix occasionally omitted diagnostic
2026-01-16 08:57:50 +00:00
susmonteiro 11f1ec8fe4 [cxx-interop] Fix occasionally omitted diagnostic 2026-01-15 13:21:11 +00:00
John Hui a35a0ad696 [cxx-interop] Support inherited protected destructors and copy/move constructors
C++ classes can have protected destructors, copy constructors, and move
constructors. This pattern prevents that class from being directly
constructed, copied, or moved directly, but permits those operations on
types that inherit from it.

Those protected special members are inaccessible in the base class, but
are indirectly accessible in the derived class, so they shouldn't
prevent that derived class from being imported in Swift.

Such members were previously causing the derived class to be
incorrectly considered as having unknown copyability.

Note that, at the time of this patch, Swift cannot (directly) access any
members inherited from the base class with the protected special member,
because doing so relies on that base class being imported on its own
(which it can't without a public dtor/copy ctor/move ctor).

rdar://167555864
2026-01-14 09:35:22 -08:00
susmonteiro 529caf6865 [cxx-interop] Remove radar link from FIXME 2026-01-02 14:44:35 +00:00
susmonteiro fbfcd4d241 [cxx-interop] Implicitly defined copy and move constructors 2025-12-02 09:15:37 +00:00
susmonteiro 2c9bb7a6eb Revert "[cxx-interop] Implicitly defined copy constructors"
This reverts commit 1c5bbe0290.
2025-11-05 10:42:05 +00:00
susmonteiro dd921adc05 Revert "[cxx-interop] Implicitly defined move constructors"
This reverts commit c3cd9939c1.
2025-11-05 10:41:38 +00:00
susmonteiro c3cd9939c1 [cxx-interop] Implicitly defined move constructors 2025-11-03 16:16:06 +00:00
susmonteiro 1c5bbe0290 [cxx-interop] Implicitly defined copy constructors 2025-10-30 14:17:19 +00:00
Anthony Latsis 5730344cfe IRGen: Fix rebranch build
`getTrailingRequiresClause` has a different result type in
`stable/21.x`.
2025-09-30 21:34:07 +01:00
swift-ci 88da350809 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-30 01:34:08 -07:00
susmonteiro e8b9b52c5d [cxx-interop] Emit diagnostic to mark C++ type as non-copyable 2025-09-29 11:57:29 +01:00
swift-ci e61c65951b Merge remote-tracking branch 'origin/main' into rebranch 2025-09-19 14:36:30 -07:00
Gabor Horvath e4e54236ca [cxx-interop] Fix over-releasing reference members of trival C++ types
Large trivial types were copied via memcpy instead of doing a field-wise
copy. This is incorrect for types with reference fields where we also
need to bump the corresponding refcounts.

rdar://160315343
2025-09-19 14:28:33 +01:00
swift-ci 4838c6e954 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-01 23:15:28 -07:00
John McCall 46be95847b Extract TypeLowering's recursive type properties into a header, add
functions to compute them directly without a TypeLowering object, and
change a lot of getTypeLowering call sites to just use that.

There is one subtle change here that I think is okay: SILBuilder used to
use different TypeExpansionContexts when inserting into a global:
- getTypeLowering() always used a minimal context when inserting into
  a global
- getTypeExpansionContext() always returned a maximal context for the
  module scope
The latter seems more correct, as AFAIK global initializers are never
inlinable. If they are, we probably need to configure the builder with
an actual context properly rather than making global assumptions.

This is incremental progress towards computing this for most types
without a TypeLowering, and hopefully eventually removing TL entirely.
2025-08-01 15:00:57 -04:00
Anthony Latsis 3f841b515c Merge remote-tracking branch 'origin/main' into jepa-rebranch 2025-07-30 14:31:33 +01: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
swift-ci 367f0082fc Merge remote-tracking branch 'origin/main' into rebranch 2025-07-22 22:17:45 -07:00
Anthony Latsis f8577a2731 IRGen: Address llvm::Type::getPointerTo deprecation
See https://github.com/llvm/llvm-project/pull/113331.
2025-07-21 12:37:15 +01:00
swift-ci ae49f6807e Merge remote-tracking branch 'origin/main' into rebranch 2025-05-30 14:34:38 -07:00
Egor Zhdan a7c174431f [cxx-interop] Do not crash when emitting layout of std::string
If a `[[no_unique_address]]` field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for `std::string` while emitting a padding field:
```
_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);
```

rdar://151941799
2025-05-30 17:32:15 +01:00
swift-ci dd8c99e095 Merge remote-tracking branch 'origin/main' into rebranch 2025-04-25 10:17:16 -07:00
Gabor Horvath 939ee49dcb [cxx-interop] Fix a crash with [[no_unique_address]]
Swift does not support storing fields in the padding of the previous
fields just yet, so let's not import fields like that from C++.
Represent them as opaque blobs instead.

Fixes #80764

rdar://149072458
2025-04-25 12:46:31 +01:00
Anthony Latsis 5996a95c21 IRGen: Adjust uses of clang::FieldDecl::getBitWidthValue (parameter removed)
Per cfe26358e3051755961fb1f3b46328dc2c326895 (llvm-project).
2025-04-24 01:52:48 +01:00
susmonteiro 51357a952c [cxx-interop] Prevent usage in Swift of C++ move constructor with default args 2025-03-07 16:02:38 +00:00
susmonteiro bc6573ec32 [cxx-interop] Prevent usage in Swift of C++ copy constructor with default args 2025-03-03 13:22:11 +00:00
Gabor Horvath 998591e9f9 [cxx-interop] Do not import zero-sized fields
Zero sized fields are messing up the offset calculations when we import
C++ fields to Swift. We assume that the size of the field is determined
by the type of the field. This is not true for fields marked with
no_unique_address. Those fields can have 0 size while the
sizeof(decltype(field)) is still 1.

rdar://143907490
2025-02-03 11:55:54 +00:00
Egor Zhdan fe5b047bdc [cxx-interop] NFC: Remove unused variable
This fixes a `-Wunused-variable` warning that got introduced in 57c7ecd2.
2024-12-19 16:50:44 +00:00
Egor Zhdan 57c7ecd244 [cxx-interop] Fix memory layout for structs with out-of-order base types
In C++, a primary base class that is placed in the beginning of the type's memory layout isn't always the type that is the first in the list of bases – the base types might be laid out in memory in a different order.

This makes sure that IRGen handles base types of C++ structs in the correct order.

This fixes an assertion in asserts-enabled compilers, and an out-of-memory error in asserts-disabled compilers. The issue was happening for both value types and foreign reference types. This change also includes a small refactoring to reuse the logic between the two code paths.

rdar://140848603
2024-12-16 15:53:43 +00:00
Gabor Horvath f1ac9af3f9 [cxx-interop] Remove stale reference to rdar
There is no reason to require a body for move/copy ctors to use them.
2024-12-03 10:58:26 +00:00
Gabor Horvath 179786b784 [cxx-interop] Simplify finding copy/move ctors in IRGen 2024-11-27 15:51:57 +00:00
Egor Zhdan d3e43bbe7b [cxx-interop] Fix IRGen for C++ types that use tail padding of their bases
In C++, a field of a derived class might be placed into the tail padding of a base class. Swift was not handling this case correctly, causing an asserts-disabled compiler to run out of RAM, and an asserts-enabled compiler to fail with an assertion.

Fixes this IRGen assertion:
```
Assertion failed: (offset >= NextOffset && "adding fields out of order"), function addField, file GenStruct.cpp, line 1509.
```

rdar://138764929
2024-11-04 13:44:43 +00:00
Alejandro Alonso 7219daf418 Collect metadata from like type and add type information when GEPing into raw layouts 2024-09-05 18:04:47 -07:00
Alejandro Alonso 8b773dcdf7 Add an ArrayLayoutEntry and dispatch to that for array raw layout 2024-09-04 15:13:28 -07:00
Arnold Schwaighofer c7b91a9a1b Address review comments 2024-08-22 07:50:52 -07:00
Arnold Schwaighofer ad6f6dec6e IRGen: We cannot call destroy through metadata of private types of non-copyable fields
So call the destroy on the closing type instead.

Amends the concepts areFieldsABIAccessible/isABIAccessible to take metadata
accessibility of non-copyable types into account.

rdar://133990500
2024-08-21 11:22:30 -07:00
Slava Pestov 375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Gábor Horváth 1b4bba3cec Update lib/IRGen/GenStruct.cpp
Co-authored-by: John McCall <rjmccall@apple.com>
2024-07-17 09:32:23 +01:00
Gábor Horváth 824fccac92 Update lib/IRGen/GenStruct.cpp
Co-authored-by: John McCall <rjmccall@apple.com>
2024-07-17 09:32:17 +01:00
Gabor Horvath ce3fa734ce [cxx-interop] Fix wrong field offsets with value types using inheritance
Instead of adding opaque fields for the base subobjects this patch
introduces a recursive walk to add all the base fields to the generated
Swift struct.

rdar://126754931
2024-07-11 17:20:18 +01:00
Slava Pestov 3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Slava Pestov fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Alex Lorenz af36238004 Merge pull request #74640 from hyp/eng/virtual-destructor-vtable-ref-irgen-linux-android
[cxx-interop] ensure that the body of implicit virtual destructor is …
2024-06-26 10:32:51 -07:00
Alex Lorenz cd1ca50c63 [cxx-interop] ensure that the body of implicit virtual destructor is defined before emitting the clang decl for it
This ensures that the destructor definition is emitted into the module's LLVM IR, fixing a linker error.
2024-06-21 17:00:15 -07: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
Slava Pestov d5985158cb IRGen: Fix some more incorrect conformance checks 2024-05-07 10:37:49 -04:00
Erik Eckstein 1b1d5ed020 IRGen: support the @sensitive attribute
Call `swift_clearSensitive` after destroying or taking "sensitive" struct types.

Also, support calling C-functions with "sensitive" parameters or return values. In SIL, sensitive types are address-only and so are sensitive parameters/return values.
Though, (small) sensitive C-structs are passed directly to/from C-functions. We need re-abstract such parameter and return values for C-functions.
2024-04-09 12:01:11 +02:00