Commit Graph

718 Commits

Author SHA1 Message Date
Pavel Yaskevich
578f82f466 [ClangImporter] Introduce a new kind of parameter - completion handler 2024-01-12 11:32:06 -08:00
Pavel Yaskevich
41ef9b6f38 [ClangImporter] NFC: Convert CF{Un}retainedOutParaeter into ImportTypeAttr 2024-01-12 11:32:06 -08:00
Egor Zhdan
494474b021 [cxx-interop] Support C++ default arguments
This allows calling a C++ function with default arguments from Swift without having to explicitly specify the values of all arguments.

rdar://103975014
2024-01-10 16:37:42 +00:00
Puyan Lotfi
128064f31d [cxx-interop] Enable virtual function calling from Swift to C++
This is a forward-interop feature that wires up existing functionality for
synthesizing base class function calling to enable virtual function calling.
The general idea is to sythesize the pattern:

```
// C++ class:
struct S { virtual auto f() -> int { return 42; } };

// Swift User:
var s = S()
print("42: \(s.f())")

// Synthetized Swift Code:
extension S { func f() -> CInt { __synthesizedVirtualCall_f() } }

// Synthetized C/C++ Code:
auto __cxxVirtualCall_f(S *s) -> int { return s->f(); }
```

The idea here is to allow for the synthetized C++ bits from the Clang side to
handle the complexity of virtual function calling.
2023-12-04 01:55:30 -05:00
Egor Zhdan
efc008a2ca [cxx-interop] Import using decls that expose methods from private base classes
If a C++ type `Derived` inherits from `Base` privately, the public methods from `Base` should not be callable on an instance of `Derived`. However, C++ supports exposing such methods via a using declaration: `using MyPrivateBase::myPublicMethod;`.

MSVC started using this feature for `std::optional` which means Swift doesn't correctly import `var pointee: Pointee` for instantiations of `std::optional` on Windows. This prevents the automatic conformance to `CxxOptional` from being synthesized.

 rdar://114282353 / resolves https://github.com/apple/swift/issues/68068
2023-11-14 00:30:54 +00:00
Alex Lorenz
d7b62b3c40 [cxx-interop] add 'upcoming-swift' compat version
This version will be used to gate new source breaking changes for C++ interoperability
2023-07-25 16:19:58 -07:00
Egor Zhdan
8832d27e98 [cxx-interop] Import mutating dereference operators
C++ `T& operator*()` is mapped to a Swift computed property `var pointee: T`.

Previously `var pointee` only had a getter, after this change it will also have a setter if the C++ type declares an overload of `operator*` that returns a mutable reference.

rdar://112471779
2023-07-19 16:12:55 +01:00
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
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
Alex Lorenz
8ed840f9c1 [interop] do not warn about a template with too many specializations that can't be imported 2023-07-06 13:45:21 -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
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
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
Egor Zhdan
42b3973de8 [cxx-interop] Do not mix up computed properties from different records
If two different C++ structs have methods with the same name, both annotated with `SWIFT_COMPUTED_PROPERTY`, ClangImporter previously confused them when one of the structs referenced the other struct.

rdar://108990490 / resolves https://github.com/apple/swift/issues/65675
2023-05-06 17:04:08 +01:00
Alex Lorenz
0fff76915b [interop] Ensure an FRT or a pointer to struct/class gets a Swift type name for a C++ template parameter
In the follow-up, I should also expand this to cover pointers to builtin types too, but for now lets go with a miminal fix here
2023-04-26 14:48:21 -07:00
Alex Lorenz
8e0c17b274 [interop] do not import functions whose return type is not imported 2023-04-11 08:27:45 -07:00
zoecarver
ba8e00d7f2 [cxx-interop] Add fix-it for foreign reference types. 2023-03-20 16:14:42 -07:00
Alex Lorenz
0e60775e9a NFC, refactor importer::requiresCPlusPlus into reusable function 2023-01-31 14:58:25 -08:00
Alex Lorenz
f36e5cf641 [interop] add 'ImportSymbolicCXXDecls' experimental import mode for importing class templates syntactically but not semantically 2023-01-31 14:58:13 -08:00
Egor Zhdan
745d92d9cb [cxx-interop] Allow instantiated operator methods to serve as protocol conformance witnesses
If a templated C++ class declares an operator as a member function, and is instantiated using a typedef or a using-decl on the C++ side, it previously could not be conformed to a Swift protocol that requires the operator function despite matching signatures.

This was due to a Swift name lookup issue: operators, unlike regular member functions, are found by doing an unqualified lookup. Since C++ class template specializations and their members are not added to `SwiftLookupTable`, when doing qualified lookup members are searched by looking at all of the members of the specialization and choosing the ones with matching names. With unqualified lookup, we cannot rely on knowing the right specialization and need to search for all the operators in a given module.

This change adds synthesized operator thunks to `SwiftLookupTable` to make them discoverable by unqualified lookup.
2023-01-03 13:57:08 +00:00
Egor Zhdan
168ef490af [cxx-interop] Disambiguate const and non-const methods consistently
When importing a C++ struct that contains two methods that only differ in const-ness, we append `Mutating` to the name of the non-const method to make it possible to call from Swift unambiguously.

Unfortunately that logic was dependent on the order in which we import methods of a class: the `Mutating` suffix was added when another method with the same name was already imported.

This caused lookup failures, and the behavior was incorrect when the pair of methods return instances of an unsafe type: the const overload was renamed as `Unsafe` properly, but the non-const overload was not renamed.
2022-12-19 15:03:04 +00:00
Egor Zhdan
bbf8f0d5bc [cxx-interop] Fix lookup failure for operators in templated classes
If an operator is declared as a method of a templated class, we were failing to look it up during auto-conformance to `UnsafeCxxInputIterator`.

This fixes `Interop/Cxx/stdlib/use-std-map.swift` on Ubuntu.

rdar://102420290
2022-12-13 17:05:38 +00:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Nuri Amari
bca7330fda Respect NS_REFINED_FOR_SWIFT importing anon enums
When an anonymous enum is imported, its imported name is under some
circumstances derived from the type that it is backed by.
NS_REFINED_FOR_SWIFT is a macro that produces a __attribute__((swift_private))
that when attached to a declaration, the imported name of this
declaration should have two underscores prepended. When the name
anonymous enum is derived from another declaration, and said declaration
does not have the same swift private attribute, the __ is dropped. This
patch fixes this.
2022-10-07 21:38:32 -07:00
Daniel Rodríguez Troitiño
abd3c16c5a [NFC] Remove out parameters. Create struct for return type.
Instead of having out parameters for a couple of flags, create a small
struct with the type and the flags and return that struct inside the
Optional of importParameterType.

Additionally change the naming of some variables to make the function
and method code more similar.
2022-09-02 11:51:20 -07:00
Daniel Rodríguez Troitiño
47e4ab2cbb Improve importParameterType for method parameters
Adapt the helper for figuring out parameter types for functions with the
code exclusive for figuring out method parameters. This includes special
code for handling dictionary subscripts, error parameters and completion
handlers.

The function parameters behaviour should not be modified, since in order
to import function parameters all of the new pieces of code should be
skipped following the value of the flags passed into the helper.

For method parameters, there is extensive changes, specially in the
cases of importing Obj-C++ methods that use C++ types, which were
supported for functions, but not so much for methods.
2022-09-02 11:21:45 -07:00
Daniel Rodríguez Troitiño
f0143383d1 [NFCi] Extract importParameterType
Extract a helper method `importParameterType` from the
`importFunctionParameterList` code. This new helper only deals with
function parameters, but the final intention is that it will be shared
code with the code that import method parameters.

The only functional difference introduced is that before, some of the
branches will not have added an extra diagnostic about parameters
failing to import, while this version adds those diagnostics
consistently for any case.
2022-09-02 11:21:45 -07:00
Artem Chikin
eebebd9a55 [Dependency Scanning] Do not persist cached Clang module dependencies between scans.
This change tweaks the 'GlobalModuleDependenciesCache', which persists across scanner invocations with the same 'DependencyScanningTool' to no longer cache discovered Clang modules.

Doing so felt like a premature optimization, and we should instead attempt to share as much state as possible by keeping around the actual Clang scanner's state, which performs its own caching. Caching discovered dependencies both in the Clang scanner instance, and in our own cache is much more error-prone - the Clang scanner has a richer context for what is okay and not okay to cache/re-use.

Instead, we still cache discovered Clang dependencies *within* a given scan, since those are discovered using a common Clang scanner instance and should be safe to keep for the duration of the scan.

This change should make it simpler to pin down the core functionality and correctness of the scanner.
Once we turn our attention to the scanner's performance, we can revisit this strategy and optimize the caching behaviour.
2022-08-29 15:40:59 -07:00
swift-ci
41366ebccd Merge remote-tracking branch 'origin/main' into rebranch 2022-08-12 03:54:16 -07:00
Egor Zhdan
51a1176d90 [cxx-interop] Clang member lookup should not look into Swift extensions
Calling `NominalTypeDecl::lookupDirect` triggers deserialization of Swift extensions for the type. `ClangRecordMemberLookup` shouldn't assume it is allowed to deserialize Swift extensions for the given C++ type: there might be extensions which reference the module that is currently being imported, which causes circular request dependency errors.
2022-08-08 17:05:25 +01:00
swift-ci
cbe82b6bfb Merge remote-tracking branch 'origin/main' into rebranch 2022-08-02 06:14:04 -07:00
Ehud Adler
379fc1f0a4 [cxx-interop] Fix issue where multiple records in a module containing the same meth… (#60338) 2022-08-02 09:01:56 -04:00
Arnold Schwaighofer
2c682ae698 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-21 15:15:40 -07:00
zoecarver
446b2c38ab [cxx-interop] Add source locations to diagnostics for un-importable APIs. 2022-07-21 14:59:27 -04:00
swift-ci
a5e7bd7dd3 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-20 07:53:55 -07:00
Egor Zhdan
d85d2e9e75 [cxx-interop] Synthesize conformances to UnsafeCxxInputIterator
This teaches ClangImporter to synthesize conformances of C++ iterator types to `UnsafeCxxInputIterator` protocol from the `Cxx` module.

We consider a C++ type to be an iterator if it defines a subtype (usually a typedef or a using decl) called `iterator_category` that inherits from `std::input_iterator_tag`.

rdar://96235368
2022-07-20 11:44:25 +01:00
swift-ci
6976ff1842 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-06 07:34:30 -07:00
Egor Zhdan
51dda6608c [cxx-interop] Avoid importing too complex specializations
This reduces the specialization limit from 10000 to 1000 to prevent Swift from failing to import libstdc++ due to `std::_Index_tuple` being defined recursively.

This also adds a diagnostic to let the user know why a template instantiation wasn't imported.

rdar://96324175
2022-07-06 12:11:27 +01:00
swift-ci
1fa0064c1f Merge remote-tracking branch 'origin/main' into rebranch 2022-06-17 11:34:24 -07:00
Egor Zhdan
22f90f6c8a Merge pull request #59530 from apple/egorzhdan/clangimporter-synthesizer-nfc
[ClangImporter] NFC: extract Swift decl synthesis logic into a separate file
2022-06-17 19:17:57 +01:00
Egor Zhdan
1839ddb115 [ClangImporter] NFC: extract Swift decl synthesis logic into a separate file
`ImportDecl.cpp` contained 10k+ lines of code, which caused slowdowns in incremental compilation and while editing the code in the IDE.

This change extracts a chunk of largely self-contained decl synthesis logic into a separate file.
2022-06-17 16:52:18 +01:00
swift-ci
5c09873319 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-17 00:54:05 -07:00
Puyan Lotfi
28375ae7df [c++-interop] Providing information about enum types from inferDefaultArgument
When ClangImporter::Implementation::inferDefaultArgument processes
func/method arguments as part of omitNeedlessWordsInFunctionName it
processes information about how the typenames for the parameters related
to the parameter names to form a parameter names list. The parameter
names list is used to determine if the argument label for a function
should be clipped based on the typename. So for example a type like
NSOrderedCollectionDifferenceCalculationOptions would cause a label
ending with "Options" to get clipped so that for instance "withOptions"
becomes simply "with".

Unfortunately in the context of C++-Interop, the typename for the
parameter often resolves to what the type backing the typedef or enum is
and not the actual name of the typedef
(so `typedef NSUInteger NSOrderedCollectionDifferenceCalculationOptions`
 resolves to a name of NSUInteger rather than
 NSOrderedCollectionDifferenceCalculationOptions).

This patch seeks to collect a bit more information when processing
NS_OPTIONS typedefs and providing that to the calling
omitNeedlessWordsInFunctionName to handle more inteligently.

In practice this fixes anywhere in Foundatio where

`withOptions: NSOrderedCollectionDifferenceCalculationOptions` is used.
2022-06-16 19:41:04 -07:00
swift-ci
0faccc23f2 Merge remote-tracking branch 'origin/main' into rebranch 2022-06-10 18:35:15 -07:00
zoecarver
588c2872a0 [nfc] Remove dead operator code pt. 3 2022-06-10 13:52:08 -07:00
Ben Barham
0dff9ac150 [next] Update PPCallbacks::InclusionDirective overrides
`InclusionDirective` was changed in llvm/llvm-project
d79ad2f1dbc2db63121620f55d6cfa915f2733ac to pass an
`Optional<FileEntryRef>` rather than a `FileEntry *`. Update overrides
in Swift to use the new API.
2022-05-05 16:25:10 -07:00
Sam Kortekaas
23b2fa9753 [cxx-interop] Add diagnostic for contradicting mutability annotations 2022-04-28 15:42:56 +02:00
Josh Soref
3d488f685e Spelling clangimporter (#42464)
* spelling: enumerators

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: handler

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: heuristic

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: implicitly

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: included

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: instantiate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: integer

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: nested

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: otherthing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overridden

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: simultaneously

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: special

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: typecheck

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unfortunately

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unknown

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: version

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 09:31:12 -07:00