Commit Graph

1871 Commits

Author SHA1 Message Date
Egor Zhdan
bc56ddc2bb [cxx-interop] Handle inherited templated operators during auto-conformance
This fixes the automatic `std::unordered_map` conformance to CxxDictionary on Linux. Previously `std::unordered_map::const_iterator` was not auto-conformed to UnsafeCxxInputIterator because its `operator==` is defined on a templated base class of `const_iterator`.

rdar://105220600
2023-07-17 21:10:32 +01:00
Evan Wilde
26a974e772 [NFC] Headers headers headers!
Including headers that were being transitively included from LLVM
before. Also pointing them at the new locations for some of them.
2023-07-17 10:55:55 -07:00
Evan Wilde
8ce6ee8dd1 Updating API usages
LLVM deprecated, renamed, and removed a bunch of APIs. This patch
contains a lot of the changes needed to deal with that.

The SetVector type changed the template parameters.

APInt updated multiple names, countPopulation became popcount,
getAllOnesValue became getAllOnes, getNullValue became getZero, etc...

Clang type nullability check stopped taking a clang AST context.

The LLVM IRGen Function type stopped exposing basic block list directly,
but gained enough API surface that the translation isn't too bad.
(GenControl.cpp, LLVMMergeFunctions.cpp)

llvm::Optional had a transform function. That was being used in a couple
of places, so I've added a new implementation under STLExtras that
transforms valid optionals, otherwise it returns nullopt.
2023-07-17 10:53:42 -07:00
Alex Lorenz
4d084100ce [cxx-interop] Mark C++ functions with unavailable return type as unavailable
This prevents users from calling functions with unsupported or unavailable return types. This ensures that users don't for example call a function that returns a non-copyable and non-movable type

Fixes https://github.com/apple/swift/issues/64401
2023-07-11 16:40:57 -07:00
Cyndy Ishida
65d819e615 [ClangImporter] Use TextAPI apis for capturing current-version (#67204)
Use LLVM apis for understanding TBD files instead of parsing the yaml
directly. This prevents breaking the compiler when TBD-v5 exists which
is in json.
2023-07-11 07:47:58 -07:00
Alex Lorenz
a10332548c [cxx-interop] Do not add base class members that cause lookup ambiguities with a derived class member of the same name
Fixes https://github.com/apple/swift/issues/66323
2023-07-07 10:38:48 -07:00
Egor Zhdan
39a837c1b5 Merge pull request #67115 from apple/egorzhdan/cxx-semantics-kind
[cxx-interop] Tweak C++ type semantics detection
2023-07-05 19:09:00 +01:00
Egor Zhdan
6e7fb3263b [cxx-interop] Tweak C++ type semantics detection
`CxxRecordSemanticsKind::ExplicitlyUnsafe` and `CxxRecordSemanticsKind::UnsafePointerMember` were never directly used, and those do not indicate semantics: they indicate safety of the type when used from Swift, which should be handled by another request `IsSafeUseOfCxxDecl` instead of `CxxRecordSemantics`.

Having `ExplicitlyUnsafe` and `UnsafePointerMember` as semantics indicators was problematic, for instance, for types that are move-only and store a pointer at the same time. Swift allowed the usage of these types (under the rules for `UnsafePointerMember` types) when move-only types are disabled, and did not apply the move-only attribute on such types when move-only types are enabled.

rdar://110644300
2023-07-04 20:12:06 +01:00
Alex Lorenz
6c6092ebee [interop] ensure C++ stdlib set's insert methods are unsafe 2023-07-03 13:44:17 -07:00
Alex Lorenz
14020201f5 Revert "[interop] do not import functions whose return type is not imported"
This reverts commit 8e0c17b274.
2023-07-03 10:06:54 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Steven Wu
2b7d38dc16 [CAS] Improve swift cas options
Using the same CASOption from clang to communicate CAS configurations so
it is easier to exchange CAS configuration between them.
2023-06-26 10:34:26 -07:00
zoecarver
7d47cb9fec [cxx-interop] fix how we import CF types. 2023-06-22 12:21:10 -07:00
Steven Wu
f509a29bd9 Merge pull request #66575 from cachemeifyoucan/eng/PR-caching-tune-up
Various cleanups to allow better swift caching.
2023-06-21 10:23:01 -07:00
Steven Wu
07a35e9bd3 [CAS] Workaround the missing file system dependency from clang importer 2023-06-20 14:04:01 -07:00
Egor Zhdan
4a1afa9bd3 [cxx-interop] Avoid crashing when template substitution fails
This code used to crash the compiler:

    var s = std.string("hi")
    s.append("foo")

`append` in this case resolves to a templated C++ method that accepts `std::string_view`, while we tried passing a Swift String to it as a parameter.

rdar://107018724
2023-06-19 15:58:41 +01:00
Puyan Lotfi
b140bc32c3 Merge pull request #66619 from plotfi/plotfi-cxx-interop-uicontrolstate-mangling
[cxx-interop] Add fix for corner case where NS_OPTIONS typedef has to be desugared
2023-06-15 16:47:41 -07:00
Puyan Lotfi
fe6ccd7a29 [cxx-interop] Add fix for corner case where NS_OPTIONS typedef has to be desugared
This patch is an add-on to https://github.com/apple/swift/pull/64043.
Essentially when encountering NS_OPTIONS enums, in C++-Interop mode
if they are not specially handled then they can mangle differently than
they do without C++-Interop. This patch adds logic to handle when a
typedef and enum have additional clang::ElaboratedType sugar, but
otherwise it does the same as the existing 64043 patch.

The test case provided was encountered in a real app build. The problem
came from when two modules are each compiled one with and one without
C++-Interop. For the test case code provided the mangling of the
protocol conformance is not consistent and the code in
SILGenLazyConformance.cpp crashes on an invalid conformance with reason
"Invalid conformance in type-checked AST".
2023-06-15 11:17:25 -07:00
Arnold Schwaighofer
654f21f1d1 Enable opaque pointers 2023-06-14 10:48:13 -07:00
Steven Wu
b1f99b8e93 [CAS] swift dependency scanning using CAS for compiler caching (#66366)
Teach swift dependency scanner to use CAS to capture the full dependencies for a build and construct build commands with immutable inputs from CAS.

This allows swift compilation caching using CAS.
2023-06-12 10:55:53 -07:00
Doug Gregor
a72fb83034 Requestify AbstractStorageDecl::hasStorage().
The `hasStorage()` computation is used in many places to determine the
signatures of other declarations. It currently needs to expand accessor
macros, which causes a number of cyclic references. Provide a
simplified request to determine `hasStorage` without expanding or
resolving macros, breaking a common pattern of cycles when using
macros.

Fixes rdar://109668383.
2023-06-10 08:28:06 -07:00
Doug Gregor
b374c099ac [Clang importer] Eliminate redundant imports of C++ fields as properties
A recent refactoring uncovered two places where we could end up
importing a C++ field declaration as a property more than once:

1. Importing the declaration context of a field in C++ mode can then
  go import all of the fields. In such a case, check that the field
  we're importing didn't happen already, and bail out early if it did.
  This is common practice in the Clang importer but wasn't happening here.
2. One caller to the function that imported a field from a C++ base
  class into its inheriting class (as a computed property) wasn't
  checking the cache, and therefore created a redundant version.

Fix both issues.
2023-06-03 10:03:10 -07:00
Egor Zhdan
f2771584df [cxx-interop] Do not treat std::pair<UnsafeType, T> as safe
rdar://109529750
2023-06-01 14:00:03 +01:00
Steven Wu
c2c24145bf Merge pull request #65925 from cachemeifyoucan/eng/PR-swift-frontend-tweaks-for-caching
Cleanup some redundant dependencies and unnecessary works that prevent compiler caching.
2023-05-23 15:32:05 -07:00
Steven Wu
add598762c Merge pull request #65985 from cachemeifyoucan/eng/PR-109411245
[ObjcHeader] Fix objc header generation when pch is explicited passed
2023-05-18 10:30:44 -07:00
Steven Wu
16e4cfae76 [ObjcHeader] Fix objc header generation when pch is explicited passed
When swift-frontend is explicitly passed the pch file as bridging header
on command-line through `-import-objc-header`, it needs to print the
original source file name if needed to the generated objc header.

rdar://109411245
2023-05-17 14:15:55 -07:00
Steven Wu
f4569a6e98 [Module] Simplify explicit clang module loading
When using explicit module build, there is no need to check top level
module map to see if the module exists or not, since dependency scanning
already pulled in all needed modules. Furthermore, when using clang
include tree, the module maps are not available through FS for this
search. Just directly try to load modules when using explicit module
build.
2023-05-16 16:38:55 -07:00
Steven Wu
cc01780a9f [ClangImporter] Don't add clang path when creating from cc1 commands
When creating ClangImporter directly from cc1 args, there is no need for
clang path, and it might even be mistaken as an input path. Don't use
clang executable path in the arguments when creating from cc1 commands.
2023-05-16 16:38:54 -07:00
Steven Wu
68db1b4de5 [ClangImporter] Don't add cxx shim module map when not needed
Don't add cxx shim when c++ interop is not used. This is to cut down the
unnecessary dependencies that are not seen by the dependency scanner.
2023-05-16 16:38:54 -07:00
Steven Wu
359c37eba7 [EmitPCH] Do not cache PCH in memory during emit-pch action
When pch are explicitly created, there is no need to cached produced pch
in memory since pch is only going to be consumed by a later process.
2023-05-16 16:38:54 -07:00
Artem Chikin
99f545a8eb Merge pull request #65930 from artemcm/SeparateClangTargetHandlingForIRGen
[Explicit Module Builds] Ensure IRGen uses Swift compiler's target triple when '-clang-target' is set.
2023-05-16 15:29:02 -07:00
Artem Chikin
f2cf55a472 [Explicit Module Builds] Ensure IRGen uses Swift compiler's target triple when '-clang-target' is set.
The Clang importer's Clang instance may be configured with a different (higher)
OS version than the compilation target itself in order to be able to load
pre-compiled Clang modules that are aligned with the broader SDK, and match the
SDK deployment target against which Swift modules are also built. In this case,
we must use the Swift compiler's OS version triple in order to generate the
binary as-requested.

This change makes 'ClangImporter' 'Implementation' keep track of a distinct
'TargetInfo' and 'CodeGenOpts' containers that are meant to be used by clients
in IRGen. When '-clang-target' is not set, they are defined to be copies of the
'ClangImporter's built-in module-loading Clang instance. When '-clang-target' is
set, they are configured with the Swift compilation's target triple and OS
version (but otherwise identical) instead. To distinguish IRGen clients from
module loading clients, 'getModuleAvailabilityTarget' is added for module
loading clients of 'ClangImporter'.

The notion of using a different triple for loading Clang modules arises for the
following reason:
- Swift is able to load Swift modules built against a different target triple
  than the source module that is being compiled. Swift relies on availability
  annotations on the API within the loaded modules to ensure that compilation
  for the current target only uses appropriately-available API from its
  dependencies.
- Clang, in contrast, requires that compilation only ever load modules (.pcm)
  that are precisely aligned to the current source compilation. Because the
  target triple (OS version in particular) between Swift source compilation and
  Swift dependency module compilation may differ, this would otherwise result in
  builtin multiple copies of the same Clang module, against different OS
  versions, once for each different triple in the build graph.
Instead, with Explicitly-Built Modules, Swift sets a '-clang-target' argument
that ensures that all Clang modules participating in the build are built against
the SDK deployment target, matching the Swift modules in the SDK, which allows
them to expose a maximally-available API surface as required by
potentially-depending Swift modules' target OS version.
--------------------------------------------
For example:
Suppose we are building a source module 'Foo', targeting 'macosx10.0', using an
SDK with a deployment target of 'macosx12.0'. Swift modules in said SDK will be
built for 'macosx12.0' (as hard-coded in their textual interfaces), meaning they
may reference symbols expected to be present in dependency Clang modules at that
target OS version.

Suppose the source module 'Foo' depends on Swift module 'Bar', which then
depends on Clang module `Baz`. 'Bar' must be built targeting 'macosx12.0'
(SDK-matching deployment target is hard-coded into its textual interface). Which
means that 'Bar' expects 'Baz' to expose symbols that may only be available when
targeting at least 'macosx12.0'. e.g. 'Baz' may have symbols guarded with
'__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_12_0'. For this reason, we use
'-clang-target' to ensure 'Baz' is built targeting 'macosx12.0', and can be
loaded by both 'Foo' and 'Bar'.

As a result, we cannot direclty use the Clang instance's target triple here and
must check if we need to instead use the triple of the Swift compiler instance.

Resolves rdar://109228963
2023-05-16 09:42:30 -07:00
Artem Chikin
61ea49c840 Merge pull request #65869 from artemcm/ClangImporterExplicitBuildOptions
[Explicit Module Builds] Add 'ClangImporter' setting for explicitly-built modules
2023-05-15 15:46:29 -07:00
Artem Chikin
e2ea6cb824 [Explicit Module Builds] Add 'ClangImporter' setting for explicitly-built modules
This will mean that '-disable-implicit-swift-modules' also automatically implies two things:
1. Clang modules must also be explicit, and the importer's clang instance will get '-fno-implicit-modules' and '-fno-implicit-module-maps'
2. The importer's clang instance will no longer get a '-fmodules-cache-path=', since it is not needed in explicit builds
2023-05-15 09:45:42 -07:00
Egor Zhdan
3865b5de6f Merge pull request #65715 from apple/egorzhdan/cxx-disable-local-visibility
[cxx-interop] Explicitly disable `-fmodules-local-submodule-visibility`
2023-05-14 18:08:33 +01:00
Hamish Knight
1ffa8eeea4 [ClangImporter] Query Preprocessor::macros instead of getPreprocessingRecord for header printing
This better matches what the clang importer does
normally, avoids a Clang issue where
`getPreprocessedEntitiesInRange` returns incorrect
results, and avoids the need to enable the
preprocessor record. This then lets us re-enable
`print_clang_headers.swift`.

rdar://102151774
2023-05-11 21:10:28 +01:00
Egor Zhdan
aed5614ec2 [cxx-interop] Explicitly disable -fmodules-local-submodule-visibility
Clang implicitly enables local submodule visibility when compiling in C++20 mode. ClangImporter does not support it, so let's disable it explicitly.

rdar://108959307 / https://github.com/apple/swift/issues/65710
2023-05-05 21:02:16 +01:00
Allan Shortlidge
0366c42293 Merge pull request #65557 from tshortli/unavailable-decl-optimization-stub
Introduce `stub` mode for `-unavailable-decl-optimization`
2023-05-05 09:11:41 -07:00
Zoe Carver
f358cbea8e Merge pull request #65578 from zoecarver/disable-rvalue-ref
[cxx-interop] Disable rvalue references. We don't support them correc…
2023-05-04 10:15:56 -07:00
Alex Lorenz
62fd801828 [interop] C++ parameter of reference type should be imported as its pointee type in Swift even if parameter's type is type alias
This finally allows std::vector.push_back to be called with the pushed value directly passed into it
2023-05-03 18:23:03 -07:00
Allan Shortlidge
54fe8a17df NFC: Fix warnings. 2023-05-03 15:19:31 -07:00
Alex Lorenz
60fd510744 [interop] lookup FRT retain/release functions in top level module
Lookups in submodules do not work and are explicitly ignored in the clang importer
2023-05-01 18:58:39 -07:00
Alex Lorenz
89b1c6391f Revert "[cxx-interop] Treat un-instantiated templated types as unsafe"
This reverts commit c81325461a.

This caused https://github.com/apple/swift/issues/65446
2023-05-01 10:25:47 -07:00
Artem Chikin
6fcd8be072 [Dependency Scanning] Pull optional dependencies from the adjacent binary module for direct interface dependencies
For a `@Testable` import in program source, if a Swift interface dependency is discovered, and has an adjacent binary `.swiftmodule`, open up the module, and pull in its optional dependencies. If an optional dependency cannot be resolved on the filesystem, fail silently without raising a diagnostic.
2023-04-17 14:47:46 -07:00
Alex Lorenz
8e0c17b274 [interop] do not import functions whose return type is not imported 2023-04-11 08:27:45 -07:00
Alex Lorenz
612eefffd4 Merge pull request #64911 from hyp/eng/grand-macro-rename
[interop] update names and add docs for the interop C++ helper macros
2023-04-06 17:17:52 -07:00
Alex Lorenz
64a4b3159c [interop] add SWIFT_ prefix to the C++ interop annotations 2023-04-06 12:11:25 -07:00
Steven Wu
09b8af86fb Virtualize swift compiler outputs (#63206)
Using a virutal output backend to capture all the outputs from
swift-frontend invocation. This allows redirecting and/or mirroring
compiler outputs to multiple location using different OutputBackend.

As an example usage for the virtual outputs, teach swift compiler to
check its output determinism by running the compiler invocation
twice and compare the hash of all its outputs.

Virtual output will be used to enable caching in the future.
2023-04-05 23:34:37 +08:00
Alex Lorenz
30d0c17bf9 [interop] update names and add docs for the interop C++ helper macros 2023-04-04 15:13:59 -07:00