Commit Graph

2428 Commits

Author SHA1 Message Date
Alex Lorenz
4fcaddb4d8 Merge pull request #67726 from hyp/eng/friends-no-more
[cxx-interop] do not add friend decls as a member of Swift type that …
2023-08-04 15:19:35 -07:00
Slava Pestov
9ebb5f2e03 AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by
giving it a "scary" name.

We want people to use getInterfaceType() like with the other decl kinds.
2023-08-04 14:19:25 -04:00
Alex Lorenz
f8dbc01dc6 [cxx-interop] do not add friend decls as a member of Swift type that represents a C++ class 2023-08-03 14:03:45 -07:00
swift-ci
364c47fb6a Merge remote-tracking branch 'origin/main' into rebranch 2023-08-02 05:13:47 -07:00
Egor Zhdan
5f624d18fd Merge pull request #67618 from apple/egorzhdan/std-pair-init
[cxx-interop] Force creating a memberwise init for `std::pair`
2023-08-02 13:04:27 +01:00
swift-ci
6c12f59576 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-01 11:34:57 -07:00
Egor Zhdan
6902041e34 [cxx-interop] Force creating a memberwise init for std::pair
In libc++, `pair()` and `pair(_T1 const& __t1, _T2 const& __t2)` are templated with `enable_if`, so these initializers are not imported into Swift.

There should be a way to call `std.pair.init` from Swift, so this change makes sure Swift synthesizes a memberwise initializer for `std.pair`.

rdar://113135110
2023-08-01 18:59:41 +01:00
Alex Lorenz
0eed1a944c [cxx-interop] correctly add and lookup enumerators added to enums that correspond to namespaces
Fixes https://github.com/apple/swift/issues/67604
2023-07-31 18:57:50 -07:00
swift-ci
88c4110b26 Merge remote-tracking branch 'origin/main' into rebranch 2023-07-28 16:17:39 -07:00
Egor Zhdan
9c22a7a530 [cxx-interop] Memberwise init is not synthesized for C++ type with templated using decl
When Swift fails to import a member of a struct, it checks to see if this member could affect the memory layout of the struct, and if it can, Swift doesn't synthesize the memberwise initializer for this struct. This logic was overly restrictive and treated templated using-decls as potentially affecting the memory layout of the struct.

rdar://113044949
2023-07-28 19:53:59 +01:00
swift_jenkins
3326d91a2c Merge remote-tracking branch 'origin/main' into next 2023-07-25 08:22:24 -07:00
Egor Zhdan
be34674915 Merge pull request #67375 from apple/egorzhdan/mutable-var-pointee
[cxx-interop] Import mutating dereference operators
2023-07-25 16:20:19 +01:00
swift_jenkins
a31c7211e6 Merge remote-tracking branch 'origin/main' into next 2023-07-24 09:43:25 -07:00
Egor Zhdan
43e6e97c1a Merge pull request #67482 from apple/egorzhdan/vector-of-string
[cxx-interop] Import iterator types that are not typedef-ed
2023-07-24 17:29:26 +01:00
Egor Zhdan
af014c02b3 [cxx-interop] Import iterator types that are not typedef-ed
This prevented `std::vector<std::string>` from being auto-conformed to `CxxRandomAccessCollection`.

If an iterator type is templated, and does not have an explicit instantiation via a typedef or a using-decl, its specialization will not have an owning Clang module. Make sure we treat it as a part of the Clang module that owns the template decl.

rdar://112762768 / resolves https://github.com/apple/swift/issues/67410
2023-07-24 11:25:15 +01:00
swift_jenkins
81614b11ec Merge remote-tracking branch 'origin/main' into next 2023-07-21 07:09:38 -07:00
Alex Lorenz
96806f4d44 [cxx-interop] Windows: unify address-only logic and mark non-trivial loadable C++ types as unavailable
Windows logic for determining address-only type layout for a C++ type is now unified with other platforms.
However, this means that on Windows, a C++ type with a custom destructor, but a default copy constructor
is now loadable, even though it's non-trivial. Since Swift does not support such type operations at the
moment (it can't be yet destroyed), mark such type as unavailable in Swift instead, when building for
the Windows target.

This fixes the Windows miscompilation related to such types when they were passed indirectly to C++
functions even though they're actually passed directly.
2023-07-20 14:58:02 -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
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
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
be65796b86 [interop] do not import function templates with template parameter not present in signature whose default value is dependent
rdar://107561753
2023-07-05 14:55:58 -07:00
Alex Lorenz
0be01c415f Merge pull request #67097 from hyp/eng/revert-8e0c17b27470de49fecf19d5a222ab247984d677
Revert "[interop] do not import functions whose return type is not imported"
2023-07-05 08:06:25 -07:00
Puyan Lotfi
936990864d [cxx-interop] Add-on commits to previous NS_OPTIONS fix PR
Add-on to address comments from #67036

Addresses some nits, test addons, and some code cleanups/improvement.
2023-07-03 15:41:50 -04: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
Puyan Lotfi
7cef628f94 [cxx-interop] Import ObjCPropertyDecl of type NS_OPTIONS fields a struct type
Try importing ObjCPropertyDecl field types the C++-Interop-NS_OPTIONS
way. This will fix cases such as:

```
import UIKit

func f(gesture: UISwipeGestureRecognizer,
       direction: UISwipeGestureRecognizer.Direction) {
  gesture.direction = direction // error
}
```

because it will make sure the field inside class UIGestureRecognizer is
of the enum-struct type and not the typedef-rawValue type when importing
an ObjC class.
2023-06-30 17:51:37 -04:00
Puyan Lotfi
ec5b7b0330 [NFC][cxx-interop] Refactor NS_OPTION type import handling code to be reusable.
Zoe did a nice fix on https://github.com/apple/swift/pull/66452
that I would like to reuse for ObjCPropertyDecl field types in
importObjCPropertyDecl as well. This will fix cases such as:

```
import UIKit

func f(gesture: UISwipeGestureRecognizer,
       direction: UISwipeGestureRecognizer.Direction) {
  gesture.direction = direction // error
}
```

because it will make sure the field inside class UIGestureRecognizer is
of the enum-struct type and not the typedef-rawValue type when importing
an ObjC class.
2023-06-30 16:40:43 -04:00
Zoe Carver
29c0b0d75f Merge pull request #66896 from zoecarver/fix-ambiguous-math-func
[cxx-interop] Pull over fix from 8e7766b and apply to strstr, sin, an…
2023-06-29 09:03:17 -07:00
zoecarver
50ce8438c8 [cxx-interop] Don't import exit from Darwin module, use the one from _SwiftConcurrency.h. 2023-06-28 10:16:36 -07:00
zoecarver
173329ee96 [cxx-interop] only ban strstr, sin, cos, and exit on darwin where the conflict with declarations in the Darwin module; improve the test. 2023-06-28 09:40:09 -07:00
zoecarver
42bc1670a0 [cxx-interop] Re-work control flow when checking for duplicate functions.
Also add back `string.h` as a valid header and ban imports from `_SwiftConcurrencyShims`.
2023-06-27 22:19:22 -07:00
zoecarver
f07a9ffc05 [cxx-interop] check file and directory, not or. 2023-06-27 09:43:46 -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
zoecarver
26e30565fa [cxx-interop] Fix ambiguous 'exit' (same solution as above). 2023-06-23 12:33:23 -07:00
zoecarver
067abeb480 [cxx-interop] Pull over fix from 8e7766b and apply to strstr, sin, and cos. 2023-06-23 12:03:14 -07:00
Alex Lorenz
4590aa1ede Merge pull request #65813 from hyp/eng/passInRegs
[cxx-interop] Itanium ABI C++ records should have address-only layout when they can't be passed in registers
2023-06-16 07:47:48 -07:00
Alex Lorenz
7abd2653d9 [cxx-interop] revert back to old address-only heuristic for MSVC 2023-06-15 13:16:57 -07:00
Egor Zhdan
b459fb5fc2 [cxx-interop] Avoid linker errors when calling a defaulted constructor
When a default constructor is declared, but does not have a body because it is defaulted (`= default;`), Swift did not emit the IR for it. This was causing linker error for types such as `std::map` in libstdc++ when someone tried to initialize such types from Swift.

rdar://110638499 / resolves https://github.com/apple/swift/issues/61412
2023-06-12 20:06:49 +01:00
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -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
Zoe Carver
f9bd1a5623 Merge pull request #66452 from zoecarver/ns-option-as-field-take-two 2023-06-08 19:36:33 -07:00
zoecarver
3f45c172cc [cxx-interop] Correctly import fields with type NS_Option. 2023-06-08 13:12:38 -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
25ef5d7016 Merge pull request #66220 from apple/egorzhdan/c-default-init
[cxx-interop] Synthesize a deprecated zero initializer for C++ structs
2023-06-01 11:33:46 +01:00
Egor Zhdan
fec48f9e63 [cxx-interop] Synthesize a deprecated zero initializer for C++ structs
When importing a C header in the C++ language mode, Clang/Swift treat C structs as C++ structs.

Currently Swift synthesizes a default initializer that zero-initializes the backing memory of the struct for C structs, but not for C++ structs.

This is causing issues in existing projects that use C libraries and rely on having the default initializer available in Swift. This change enables the synthesis of a default initializer for C++ structs. Since many C++ structs are not designed to be initialized this way, the initializer is marked as deprecated in Swift.

rdar://109727620
2023-05-31 13:41:25 +01:00
Nuri Amari
f676b75af0 [Swift-ObjC-Interop] Speculative cyclic lookup fix
The lookup to resolve an ObjC forward declaration
to its potential native Swift definition within
a mixed module is becoming cyclic somehow.

The old uncached lookup still works, so it seems
this is an issue with cache invalidation somehow.
Until the route of the issue, use the old uncached
lookup method.
2023-05-28 11:45:54 -07:00
QuietMisdreavus
34faa58b52 [ClangImporter] don't add swift_attr files to a module's auxiliary files (#65867)
rdar://107624995
2023-05-17 09:10:13 -06:00
Alex Lorenz
2c4188b8f4 [cxx-interop] mark C++ classes with trivial_abi attribute as unavailable in Swift 2023-05-09 16:23:36 -07:00
Alex Lorenz
49d7e041de [cxx-interop] C++ records should have address-only layout when they can't be passed in registers
This ensures that a C++ record with only ObjC ARC pointers with trivial other members is passed by value in SIL

Fixes https://github.com/apple/swift/issues/61929
2023-05-09 16:23:35 -07:00