Commit Graph

2428 Commits

Author SHA1 Message Date
Egor Zhdan
44c35a2971 [cxx-interop] Fix lookup of member operators
Calling `StructDecl::lookupDirect` with an operator identifier (e.g. `==`) previously returned no results. This happened because the underlying C++ operator function was added to the lookup table with an underscored name (e.g. `__operatorEqualEqual`), and the synthesized function was not added to the lookup table at all. Lookup should find the synthesized decl, since that is what Swift code will call.

This fixes a typechecker error when trying to conform a C++ struct that defines an operator to a Swift protocol with an operator requirement (e.g. `Equatable`).
2022-07-15 16:53:39 +01:00
Ehud Adler
c6e89b94f1 [Cxx-Interop] Enable Record Friend functions (#59801)
* Update ImportDecl to handle Friend functions inside of recrods

* Add tests and update comment

* Undo unnecessary lines, format

* Clean up and fix tests

* New approach

* Only import valid friend functions for now

* Re-add == func to get Equatable conformance

* Remove requirement that friend is a function
2022-07-06 13:11:33 -04: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
Holly Borla
0053526c5d Merge pull request #41909 from hborla/existential-any-anyobject
[Sema] Use `ExistentialType` for `Any` and `AnyObject`.
2022-06-24 20:51:50 -07:00
Egor Zhdan
b901ad28ea [cxx-interop] Fix crash while importing operator++() that returns void
rdar://95684692
2022-06-22 14:37:36 +01:00
Ehud Adler
23f5e015e7 [CXX Interoperability] Stop re-importing FuncDecl when FuncDecl is imported before parent record (#59607)
We saw a test case failing when 2 records contain the same operator. This occurs because when the first operator is called, we import the record associated with that operator but we also import the _function_ for the 2nd record. So if we have 2 records `Foo` and `Bar` and both implement `operator-`, after calling `Foo`'s `operator-` we would have imported

1. `Foo`
2. `Foo.operator-`
3. `Bar.operator-`

Then when we call `Bar.operator-` we try importing `Bar` record & then import the operator again. So that ends up with


1. `Foo`
2. `Foo.operator-`
3. `Bar.operator-`
4. `Bar`
5. `Bar.operator-`

which causes there to be 2 imports of the same operator (`FuncDecl`)

This patch checks to see if the `FuncDecl` was previously imported and returns early if it has been

Thanks @egorzhdan and @zoecarver for helping me debug this one :p
2022-06-22 11:05:33 +01:00
Egor Zhdan
832733efe6 [ClangImporter] Revert accidentally committed logic 2022-06-21 15:40:01 +01:00
Egor Zhdan
d29b78eed1 [cxx-interop] Import increment operators
C++ pre-increment operator `T& T::operator++()` is mapped into a non-mutating function `successor() -> Self`.

The naming matches existing functions for `UnsafePointer`/`UnsafeMutablePointer`.

The purpose of this is to be used for iterator bridging: C++ requires iterators to define a pre-increment operator (https://en.cppreference.com/w/cpp/named_req/Iterator), which Swift will use to iterate over C++ sequences and collections.
2022-06-20 17:38:11 +01:00
Holly Borla
429488f6c9 [Sema] Use ExistentialType for Any and AnyObject. 2022-06-17 18:29:15 -07: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
Egor Zhdan
40a7e680a4 [cxx-interop] Import iterator dereference operators
C++ iterator dereference operator is mapped to a Swift computed property called `pointee`.

For example:
```cpp
struct ConstIterator {
  // ...
  const int &operator*() const { /* ... */ }
};
```
is imported as
```swift
struct ConstIterator {
  var pointee: Int32 { get }
  @available(*, unavailable, message: "use .pointee property")
  func __operatorStar() -> UnsafePointer<Int32>
}
```
2022-06-16 16:59:19 +01:00
zoecarver
588c2872a0 [nfc] Remove dead operator code pt. 3 2022-06-10 13:52:08 -07:00
Ehud Adler
402ef5b130 Moved to importFunction 2022-06-02 13:00:22 -04:00
Ehud Adler
7282c642d0 Turn off templated operators aside from subscript 2022-06-02 13:00:22 -04:00
Ehud Adler
bb3a721718 Manually fix some formatting 2022-05-27 15:30:06 -04:00
Ehud Adler
16c1e1defa Format 2022-05-26 21:29:43 -04:00
Ehud Adler
3196ea9b39 Refactor and format 2022-05-21 21:44:07 -04:00
Ehud Adler
e7fe6f0fe7 Fix tests and re-enable support for CXX operators 2022-05-21 21:28:03 -04:00
Ehud Adler
bd4db5137a Added synth member to result 2022-05-18 10:31:04 -04:00
Ehud Adler
11730e8f07 Remap class operator function names (-/+/*....) to imported operator names __operator(Minus, Plus,...) and fix test cases 2022-05-18 10:31:04 -04:00
Ehud Adler
4267958d51 Mapping decl base name to imported name 2022-05-18 10:31:04 -04:00
Ehud Adler
e14e2fbc76 Add changes 2022-05-18 10:31:03 -04:00
Ehud Adler
b9f2196c4a Add changes 2022-05-18 10:31:03 -04:00
Ehud Adler
178b012b80 Add operators as class members 2022-05-18 10:30:56 -04:00
Ehud Adler
88f7260c15 start work on operators 2022-05-18 10:29:30 -04:00
Puyan Lotfi
d2051be853 [cxx-interop] Pass clangSema.TUScope when calling LookupName for availability
In C++-Interop mode some of the Foundation @availables were not getting
their "renamed:" attributes filled in and this was because of the
LookupName issue as we have seen before where LookupName bails in C++
mode due to a nullptr scope resulting in something not getting imported
completely.

This patch merely passes a TUscope to avert this.

I believe ignoring the existing enum issues this should bring
Foundation with C++-Interop to parity with ObjC-Interop.
2022-05-16 14:06:07 -07:00
Robert Widmann
77bf3aff93 Teach the ClangImporter About Mac Catalyst Availability
Add some missing definitions for the mac catalyst platform. This enables the importing of macCatalyst platform availability attributes.
2022-05-06 14:37:24 -07:00
Sam Kortekaas
91bc7534ea [cxx-interop] Add diagnostics for nonmutating attr that has no effect 2022-04-28 15:42:56 +02:00
Sam Kortekaas
23b2fa9753 [cxx-interop] Add diagnostic for contradicting mutability annotations 2022-04-28 15:42:56 +02:00
Doug Gregor
9df6394afe [Clang importer] Consider attributes on the typedef name for an anonymous tag.
In C, one can provide a typedef name for an anonymous tag declaration in
one shot, e.g.,

    typedef struct {
      double x, y
    } Point;

In this case, there are effectively two declarations at the C level:
the typedef and the struct. The Clang importer was only taking
attributes from the anonymous struct (i.e., the tag) and not from the
typedef. However, any attributes put before the `typedef` should apply
as well... so consider those, too.

For now, only do this for `swift_attr` attributes, because we're
seeing this primarily with `Sendable` annotations. In the future, we
can look to generalizing it, but that could have source-breaking
consequences.

Fixes rdar://91632960.
2022-04-22 17:40:13 -07: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
zoecarver
21192904ba [cxx-interop][nfc] Add flag cxx-interop-getters-setters-as-properties.
This is causing problems in certain cases, disabling by default.
2022-04-20 17:32:26 -07:00
zoecarver
d2e3e0d4e1 [cxx-interop] Work with definition in VisitCXXRecordDecl.
This was previously un-noticed because VisitRecordDecl already handles this, but there are a couple checks that are specific to C++ records.
2022-04-18 11:57:48 -07:00
Slava Pestov
8c47cd75fd Sema: The primary associated type list references existing associated types instead of declaring new ones 2022-04-03 22:03:49 -04:00
Doug Gregor
a3947421c1 Merge pull request #42069 from beccadax/i-care-about-results
Import swift_attr(“@Sendable”) for result types
2022-03-31 15:26:01 -07:00
Xi Ge
91c4f17b35 Frontend: flip the default of whether importing SPI_AVAILABLE from clang 2022-03-31 09:43:13 -07:00
zoecarver
f652361af3 [cxx-interop] Remove 'support' for importing C++ operators.
This does not include subscript operators.

Before this is re-enabled operators need to be re-implemented. Right now they are the source of a lot of bugs. They cause frequent crashes and mis compiles. Also, templated operators insert a lot of names into global lookup which causes problems.

They also don't work on Windows.
2022-03-30 15:39:35 -07:00
Becca Royal-Gordon
a57e71ccd0 Import swift_attr(“@Sendable”) for result types
If `__attribute__((swift_attr(“@Sendable”)))` is applied to an ObjC method, ObjC property, C field, C variable, or C function, we will now make its result type `Sendable`.

For some entities, this technically had a different behavior previously, because `@Sendable` can be applied to `func`s to indicate that their interface type should be `@Sendable`. We don’t really want people to use this functionality on non-local functions, so we can safely remove it.

This isn’t quite interacting with `Unmanaged` the way we’d want, but I’ve included test cases for the current behavior with FIXME comments.

Fixes rdar://90491024.
2022-03-29 17:18:10 -07:00
Becca Royal-Gordon
192c52ff17 [NFC] Integrate ImportTypeAttrs into importType() 2022-03-29 17:18:09 -07:00
Becca Royal-Gordon
6f71581b5c [NFC] Thread import diagnostics through ImportType
`ClangImporter::Implementation::importType()` and associated parts of the importer are now passed an `llvm::function_ref` they can use to emit import diagnostics on the declaration they’re importing, and the `ImportDiagnosticAdder` helper class provides a convenient way to construct such a function.

This capability is not actually *used* in this commit—we are simply threading this function through the importer—so there is no change in behavior.
2022-03-29 17:16:28 -07:00
Egor Zhdan
626eadcf64 [cxx-interop] Import size_t as Int instead of UInt on Linux
When using libc++, Swift imports `size_t` as Int despite `size_t` being an unsigned type. This is intentional & is specified in `lib/ClangImporter/MappedTypes.def`. Previously, MappedTypes were only honored for C/C++ types declared on the file level.

In libstdc++, `size_t` is declared within `namespace std` and not on the file level, so the mapping to Int was not applied.

This change ensures that MappedTypes are also applied to types declared in `namespace std`.
2022-03-24 15:29:19 +00:00
Becca Royal-Gordon
974fbc1167 [NFC] Move ObjC method support to NominalTypeDecl
This prepares us to generalize ObjC selector collision diagnostics to also include protocols. NFC in this commit because, even though Sema and ClangImporter now try to record ObjC methods on non-`ClassDecl`s, `NominalTypeDecl::createObjCMethodLookup()` still doesn’t create ObjC method tables for them, so the calls are no-ops.
2022-03-16 14:41:55 -07:00
Puyan Lotfi
09cdd36c0a [c++-interop] For failed imports in ClangImporter, cache them regardless. (#41173)
As per SR-14137 this caches entries in ImportedDecls even when the
import failed.

Also have to mention I did this based on Thomas's PR #36747.

This should help us better handle complex templates and dependant types.
2022-03-10 10:33:04 -08:00
Allan Shortlidge
58fe2e1a05 Merge pull request #41682 from tshortli/fix-warnings
NFC: Fix a number of warnings emitted when building swift-frontend
2022-03-07 15:11:36 -08:00
Xi Ge
ce07ce2dbc frontend: add basic support for @_spi_available 2022-03-04 21:26:56 -08:00
Allan Shortlidge
1be8913c9b NFC: Fix a number of warnings emitted when building swift-frontend. 2022-03-04 16:15:33 -08:00
Zoe Carver
10b5031e93 Merge pull request #41610 from zoecarver/import-subscript-in-base
[cxx-interop] Add support for subscripts in base classes.
2022-03-02 08:40:33 -08:00
zoecarver
2e0398f867 [cxx-interop] Add support for subscripts in base classes. 2022-03-01 15:13:10 -08:00
Omar Habra
4167e2f80d [cxx-interop] Computed properties from getters and setters (#40842)
* fixing setters

* adding tests

* Removing extras

* fixing tests

* Better naming for properties

* Cleanup

* Add tests

* Clang format it

* more refactoring and some more tests

* More tests and more fixes

* Updating tests:

fixing other tests to work with new property importing

* Fix the two asserts. Move things around. Remove createImported. Move CXXMethodBridging to it's own header.

* General updates:

Fixing tests, adding radars to follow issues that cna be a started issues on the project.
Also Factoring out MethodBridging.

* Fixing Comments left on the PR:

General formatting.

* Fixing tests, and general updates for formatting

* removing extras and passing this on swift.

Co-authored-by: Omar Habra <ohabra@apple.com>
2022-02-28 23:13:17 -08:00
Ehud Adler
428892d451 Merge pull request #41536 from Huddie/fix-cxx-mutable-lookup-failure
[cxx interop] Fix issue where const overloaded disambiguated methods were not in the lookup table
2022-02-28 11:51:03 -05:00