Commit Graph

503 Commits

Author SHA1 Message Date
susmonteiro
51357a952c [cxx-interop] Prevent usage in Swift of C++ move constructor with default args 2025-03-07 16:02:38 +00:00
John Hui
66c2e2c52b [cxx-interop] Import non-public inherited members (#79348)
This patch is follow-up work from #78942 and imports non-public members,
which were previously not being imported. Those members can be accessed
in a Swift file blessed by the SWIFT_PRIVATE_FILEID annotation.

As a consequence of this patch, we are also now importing inherited members
that are inaccessible from the derived classes, because they were declared
private, or because they were inherited via nested private inheritance. We
import them anyway but mark them unavailable, for better diagnostics and to
(somewhat) simplify the import logic for inheritance.

Because non-public base class members are now imported too, this patch
inflames an existing issue where a 'using' declaration on an inherited member
with a synthesized name (e.g., operators) produces duplicate members, leading
to miscompilation (resulting in a runtime crash). This was not previously noticed
because a 'using' declaration on a public inherited member is not usually
necessary, but is a common way to expose otherwise non-public members.
This patch puts in a workaround to prevent this from affecting the behavior
of MSVC's std::optional implementation, which uses this pattern of 'using'
a private inherited member. That will be fixed in a follow-up patch.

Follow-up work is also needed to correctly diagnose ambiguous overloads
in cases of multiple inheritance, and to account for virtual inheritance.

rdar://137764620
2025-02-25 01:03:16 -08:00
fahadnayyar
9782eae585 Merge pull request #77522 from fahadnayyar/cxx-frt-inheritance-diagnostics
[cxx-interop] Infer SWIFT_SHARED_REFERENCE for types inheriting from a C++ foreign reference type
2025-02-20 13:46:28 -08:00
fahadnayyar
d8f919778d [cxx-interop] [cxx-interop] Infer SWIFT_SHARED_REFERENCE for types inheriting from a C++ foreign reference type
rdar://97914474
2025-02-20 08:46:57 -08:00
John Hui
bdf22948ce [cxx-interop] Allow Swift to access non-public C++ members (#79093)
This patch introduces an a C++ class annotation, SWIFT_PRIVATE_FILEID,
which will specify where Swift extensions of that class will be allowed
to access its non-public members, e.g.:

    class SWIFT_PRIVATE_FILEID("MyModule/MyFile.swift") Foo { ... };

The goal of this feature is to help C++ developers incrementally migrate
the implementation of their C++ classes to Swift, without breaking
encapsulation and indiscriminately exposing those classes' private and
protected fields.

As an implementation detail of this feature, this patch introduces an
abstraction for file ID strings, FileIDStr, which represent a parsed pair
of module name/file name.

rdar://137764620
2025-02-18 11:22:44 -08:00
Gabor Horvath
410341671e [SE-0458] Unify escapibility inference for AST and Interop
Interop is injecting escapability annotations for the STL and doing a
limited inference for aggregates. Let's reuse the same facilities in the
AST when we calculate the safety of the foreign types.
2025-02-17 13:41:40 +00:00
Doug Gregor
c0fb9f990a [Strict memory safety] Infer safe/unsafe for imported C types 2025-02-16 00:55:43 -08:00
Steven Wu
9d59044bb1 [BrdigingHeader] Auto bridging header chaining
Add ability to automatically chaining the bridging headers discovered from all
dependencies module when doing swift caching build. This will eliminate all
implicit bridging header imports from the build and make the bridging header
importing behavior much more reliable, while keep the compatibility at maximum.

For example, if the current module A depends on module B and C, and both B and
C are binary modules that uses bridging header, when building module A,
dependency scanner will construct a new header that chains three bridging
headers together with the option to build a PCH from it. This will make all
importing errors more obvious while improving the performance.
2025-02-05 09:41:04 -08:00
John Hui
be73254cdc [cxx-interop] Import private members (#78942)
This commit removes the guardrails in ImportDecl.cpp:SwiftDeclConverter
that prevent it from importing non-public C++ members. It also
accordingly adjusts all code that assumes generated Swift decls should
be public. This commit does not import non-public inherited members;
that needs its own follow-up patch.

Note that Swift enforces stricter invariants about access levels than C++.
For instance, public typealiases cannot be assigned private underlying types,
and public functions cannot take or return private types. Meanwhile,
both of these patterns are supported in C++, where exposing private types
from a class's public interface is considered feature. As far as I am aware,
Swift was already importing such private-containing public decls from C++
already, but I added a test suite, access inversion, that checks and
documents this scenario, to ensure that it doesn't trip any assertions.
2025-01-30 14:50:15 -08:00
QuietMisdreavus
ab26b8b9d7 add support to getTopLevelDecls for clang submodules (#76401)
rdar://126031510
2025-01-30 09:39:58 -07:00
John Hui
1341516dba [cxx-interop] Fix spurious ambiguous member lookup for eagerly imported members (#78673)
Follow-up from #78132, which did not fix issues related to eagerly imported members like subscripts.

This patch restructures recursive ClangRecordMemberLookup requests to importBaseMemberDecl() in the recursive calls, rather than propagating base member decls up to the initial lookup request and doing the import. Doing so seems to fix lingering resolution issues (which I've added to the regression tests).

rdar://141069984
2025-01-21 22:06:09 -08:00
John Hui
796075ab88 [cxx-interop] Fix spurious ambiguous member lookup (#78132)
Nested calls to importBaseMemberDecl() subvert its cache and compromise its idempotence, causing the semantic checker to spuriously report ambiguous member lookups when multiple ClangRecordMemberLookup requests are made (e.g., because of an unrelated missing member lookup).

One such scenario is documented as a test case: test/Interop/Cxx/class/inheritance/inherited-lookup-typechecker.swift fails without this patch because of the expected error from the missing member. Meanwhile, test/Interop/Cxx/class/inheritance/inherited-lookup-executable.swift works because it does not attempt to access a missing member.

This patch fixes the issue by only calling importBaseMemberDecl() in the most derived class (where the ClangRecordMemberLookup originated, i.e., not in recursive requests).

As a consequence of my patch, synthesized member accessors in the derived class directly invoke the member from the base class where the member is inherited from, rather than incurring an indirection at each level of inheritance. As such, the synthesized symbol names are different (and shorter). I've taken this opportunity to update the relevant tests to // CHECK for more of the mangled symbol, rather than only the synthesized symbol prefix, for more precise testing and slightly better readability.

rdar://141069984
2024-12-17 11:40:18 -08:00
Egor Zhdan
8202dfdecd [cxx-interop] Do not emit spurious lifetime diagnostics for C++ types
In rare scenarios, Swift was emitting diagnostics that looked like this:
```
warning: 'import_owned' swift attribute ignored on type 'basic_string': type is not copyable or destructible
```

This change makes sure the compiler does not emit these (incorrect) warnings. See the inline comment for more details.
2024-12-04 16:10:08 +00:00
Gabor Horvath
558380f6f8 [cxx-interop] Support conditional escapability
This PR adds a variadic macro that builds a SwiftAttr string containing
the names of the template type parameters that need to be escapable for
the type to be considered escapable. It also adds logic to interpret
this annotation.

rdar://139065437
2024-11-15 14:27:54 +00:00
fahadnayyar
72b8d22362 Merge pull request #77457 from fahadnayyar/frt-ref-semantic-check-refactor
[cxx-interop] Refactoring reference semantic check for c++ records
2024-11-08 02:14:25 -08:00
fahadnayyar
f4a0dd6867 [cxx-interop] Refactoring reference semantic check for c++ records 2024-11-07 10:54:49 -08:00
Artem Chikin
8e3a6207b9 Merge pull request #76915 from artemcm/AsyncScanExperiment
[Dependency Scanning] Parallelize Clang module queries
2024-11-05 10:04:31 -08:00
Artem Chikin
0f50693aa5 [Dependency Scanning] Parallelize Clang module queries
This change refactors the top-level dependency scanning flow to follow the following procedure:

Scan():
1. From the source target under scan, query all imported module identifiers for a *Swift* module. Leave unresolved identifiers unresolved. Proceed transitively to build a *Swift* module dependency graph.
2. Take every unresolved import identifier in the graph from (1) and, assuming that it must be a Clang module, dispatch all of them to be queried in-parallel by the scanner's worker pool.
3. Resolve bridging header Clang module dpendencies
4. Resolve all Swift overlay dependencies, relying on all Clang modules collected in (2) and (3)
5. For the source target under scan, use all of the above discovered module dependencies to resolve all cross-import overlay dependencies
2024-10-30 11:10:10 -07:00
fahadnayyar
e640ed637d [cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types 2024-10-29 17:31:15 -07:00
swift-ci
9fd863f81e Merge remote-tracking branch 'origin/main' into rebranch 2024-10-08 10:50:24 -07:00
Steven Wu
5fa12d31ae Merge pull request #76738 from cachemeifyoucan/eng/PR-136787368
[CAS] Use IncludeTreeFileList instead of full CASFS for caching
2024-10-08 10:42:24 -07:00
swift-ci
48648e95a7 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-07 04:14:40 -07:00
Egor Zhdan
7c25be3570 [cxx-interop] NFC: Add inline doc comments 2024-10-03 16:38:28 +01:00
swift-ci
61d5b48a52 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-02 08:15:55 -07:00
Egor Zhdan
7f597c63ae Merge pull request #76726 from swiftlang/egorzhdan/char8_t
[cxx-interop] Support `char8_t` C++20 type
2024-10-02 16:10:17 +01:00
swift-ci
c4877a5680 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-01 10:54:18 -07:00
Alastair Houghton
ac750a6aec Merge pull request #76545 from al45tair/eng/PR-115918181
[ClangImporter][ModuleWrap] Turn off libc warnings.
2024-10-01 18:46:06 +01:00
Steven Wu
cd07d532af [CAS] Use IncludeTreeFileList instead of full CASFS for caching
Use IncludeTreeFileList instead of full feature CASFS for swift
dependency filesystem. This allows smaller CAS based VFS that is smaller
and faster. This is enabled by the CAS enabled compilation does not
need to iterate file system.

rdar://136787368
2024-09-30 16:01:33 -07:00
Alastair Houghton
bace89ffbd [ClangImporter] Just turn off the warning, not the actual overlays.
We should still try adding the overlays, even if we're asked not to
generate a diagnostic while doing so.  That's slightly safer because
it means that we're less likely to find ourselves in a situation
where `swift-modulewrap` wants to use types from the C/C++ library
and can't.

rdar://115918181
2024-09-30 17:02:41 +01:00
swift-ci
59a7eb4710 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-27 06:15:16 -07:00
Gábor Horváth
89708dd15a Merge pull request #76673 from swiftlang/gaborh/structural-escapability
[cxx-interop] Add rules to recognize escapability of aggregates
2024-09-27 13:58:33 +01:00
Egor Zhdan
fcb590690e [cxx-interop] Support char8_t C++20 type
https://en.cppreference.com/w/cpp/keyword/char8_t

This is based on a patch from Varun Gandhi: https://github.com/swiftlang/swift/pull/26153

rdar://39988329 / resolves https://github.com/swiftlang/swift/issues/68726
2024-09-27 13:56:03 +01:00
Ben Barham
c15bc19946 Merge remote-tracking branch 'origin/main' into main-to-rebranch
Conflicts:
  - `lib/Serialization/ModuleFileSharedCore.cpp` new headers on main
2024-09-26 16:01:56 -07:00
Steven Wu
5a6f6e1d4d [NFC][ScanDependency] Remove some ununsed code
Clean up some code that no longer used.
2024-09-24 16:29:21 -07:00
Gabor Horvath
cbd8cdfe95 [cxx-interop] Add rules to recognize escapability of aggregates
For now, this logic is used for importing fewer unannotated types as
unsafe. In the future, this logic will be used by escapability inference
for other (non-aggregate) types.
2024-09-24 16:41:00 +01:00
Alastair Houghton
6e48b1d781 [ClangImporter][ModuleWrap] Turn off libc warnings.
`swift-modulewrap` uses the `ClangImporter` to obtain a module loader,
but it doesn't take an SDK argument (nor does anything bother to pass
one), which means that when cross-compiling you get warnings about not
being able to find the C library.

Suppress the warning by telling the `ClangImporter` that we don't care
about the C library here.

rdar://115918181
2024-09-18 13:56:53 +01:00
Ben Barham
a7b50f357f Merge remote-tracking branch 'origin/main' into manual-main-merge
Conflicts:
  - `lib/Driver/ToolChains.cpp` conflicting with the `addAllArgs` rename
    for multiple options
2024-09-16 13:53:18 -07:00
fahadnayyar
f3f4e1913b Fixed diagnostics for incorrect signature of retain/release functions of SWIFT_SHARED_REFERENCE C++ types. 2024-09-10 16:15:07 -07:00
Ben Barham
467e528200 Merge remote-tracking branch 'origin/main' into manual-rebranch-merge
Conflicts:
  - `lib/Serialization/ModuleFormat.h` bumped version to account for
    differences between main and rebranch.
2024-08-09 15:22:39 -07:00
Egor Zhdan
bfe72b4be9 Merge pull request #75589 from swiftlang/egorzhdan/linux-libcxx-interop
[cxx-interop] Allow compiling with libc++ on Linux
2024-08-09 13:42:29 +01:00
Egor Zhdan
059f0f97d1 [cxx-interop] Allow compiling with libc++ on Linux
This makes sure that Swift respects `-Xcc -stdlib=libc++` flags.

Clang already has existing logic to discover the system-wide libc++ installation on Linux. We rely on that logic here.

Importing a Swift module that was built with a different C++ stdlib is not supported and emits an error.

The Cxx module can be imported when compiling with any C++ stdlib. The synthesized conformances, e.g. to CxxRandomAccessCollection also work. However, CxxStdlib currently cannot be imported when compiling with libc++, since on Linux it refers to symbols from libstdc++ which have different mangled names in libc++.

rdar://118357548 / https://github.com/swiftlang/swift/issues/69825
2024-08-08 16:24:58 +01:00
swift-ci
b24dc98148 Merge remote-tracking branch 'origin/main' into rebranch 2024-07-24 15:55:32 -07:00
Steven Wu
2bc4080116 [ExplicitModule] Include -Xcc -D into module hash
The decision to exclude `-Xcc -D` options from swift module hash
actually doesn't help to solve the problem. It wouldn't reduce the
module variants (or the number of swiftmodule build commands) because
the command-line also encodes all the clang PCM dependencies that do get
affected by `-Xcc` flags.

To avoid the false sharing and the nondeterministic build products,
include most of the `-Xcc` flags, except include search path, into swift
module hash.

rdar://132046247
2024-07-23 14:57:44 -07:00
swift-ci
25be15072e Merge remote-tracking branch 'origin/main' into rebranch 2024-07-11 09:56:08 -07:00
Steven Wu
a32dd9539b [Modules] Avoid false swift module sharing
When the swiftmodule is built with different clang importer arguments,
they can have the same module hash, causing them to be wrongly re-used even
they contains different interfaces. Add ReducedExtraArgs to the module hash to
disambiguate them.

However, some Xcc arguments, most commonly `-D` options do not affect the
swiftmodule being generated. Do not pass `-Xcc -DARGS` to swift
interface compilation to reduce the amount of module variants in the
build.

rdar://131408266
2024-07-10 14:48:12 -07:00
Ben Barham
226ba82525 [ClangImporter] Add various const
`getSelector` now takes a `const clang::IdentifierInfo *` `ArrayRef`.
Sprinkle a bunch of `const`s in.
2024-07-02 16:13:49 -07:00
Ben Barham
4a5e1cd3da [ClangImporter] Add missing include 2024-07-01 14:52:52 -07:00
swift-ci
ee72f594fb Merge remote-tracking branch 'origin/main' into rebranch 2024-06-25 08:55:18 -07:00
Alex Lorenz
0c7621d282 Merge pull request #74541 from hyp/eng/lookup-imported-member-operator-cxx
[cxx-interop][serialization] resolve x-refs to instantiated/synthesiz…
2024-06-25 08:45:02 -07:00
Xi Ge
736ccef626 Merge remote-tracking branch 'apple/main' into rebranch 2024-06-20 15:16:55 -07:00