Commit Graph

2550 Commits

Author SHA1 Message Date
Slava Pestov
82bd81e127 ClangImporter: Stop calling getAllConformances() on protocols 2022-08-23 00:03:36 -04:00
Egor Zhdan
e3a321721d [cxx-interop] Synthesize conformances to CxxSequence
This makes ClangImporter automatically conform C++ sequence types to `Cxx.CxxSequence` protocol.

We consider a C++ type to be a sequence type if it defines `begin()` & `end()` methods that return iterators of the same type which conforms to `UnsafeCxxInputIterator`.
2022-08-17 16:06:37 +01:00
Zoe Carver
f018901a20 Merge pull request #60553 from zoecarver/enum-fix-for-computed-props
[cxx-interop] Fix anonymous enum issue for swift-named computed prope…
2022-08-14 20:53:12 -07:00
zoecarver
aaec49e0a5 [cxx-interop] Fix anonymous enum issue for swift-named computed properties. 2022-08-14 15:45:39 -07:00
Hamish Knight
6b9bcf3935 [AST] Change DotSyntaxCallExpr to take an Argument base
This allows us to more easily propagate inout
information to it, which will become a necessity
once InOutExpr is removed.
2022-08-08 15:11:00 +01: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
Egor Zhdan
44f3478289 [cxx-interop] Do not try to import uninstantiatable templates
Calling `clangSema.isCompleteType` tries to instantiate a template, and if that is impossible, returns `true`. This caused Swift to try to import invalid C++ template instantiations.

This was discovered during C++ interop adoption in SwiftCompilerSources:
Several LLVM headers declare a field with a type `DenseMap<int, ForwardDeclared>` where `ForwardDeclared` is defined in an implementation file (`.cpp`) and is not visible to ClangImporter. That is valid in C++ but caused an error when importing into Swift.
2022-07-27 23:12:32 +01:00
Egor Zhdan
160b332403 Merge pull request #60262 from apple/egorzhdan/cxx-namespace-perf
[cxx-interop] Improve performance of importing C++ namespaces
2022-07-27 23:08:46 +01:00
Egor Zhdan
fbb3762c4f [cxx-interop] Improve performance of importing C++ namespaces
When ClangImporter calls Clang to import a module, Clang calls `SwiftLookupTableWriter::writeExtensionContents` for each namespace redecl, which called `addEntryToLookupTable` (a few frames omitted) which iterated over all of the namespace redecls, doing duplicate work. This caused the complexity to degrade from linear to quadratic.

`SwiftDeclConverter::VisitNamespaceDecl` is still called once per namespace, not per redecl, so we need to iterate over all the redecls there.

This dramatically improves the build time for SwiftCompilerSources when importing Clang headers in addition to LLVM headers.
2022-07-27 20:24:52 +01:00
Egor Zhdan
29b579e9d3 [cxx-interop] Do not crash when diagnosing cycles for C++ operators
C++ operator functions are `NamedDecl`s, but they don't have a trivial name, and calling `getName` results in an assertion failure.

This was discovered during C++ interop adoption in SwiftCompilerSources.

```
Assertion failed: (Name.isIdentifier() && "Name is not a simple identifier"), function getName, file Decl.h, line 277.
...
/Volumes/Projects/swift/llvm-project/clang/include/clang/AST/DependentDiagnostic.h:150:8: importing 'clang::DeclContext::ddiag_iterator::operator=='
```
2022-07-27 11:42:09 +01:00
Zoe Carver
bd003bcd7f Merge pull request #59509 from zoecarver/frt-with-ref-counting
[cxx-interop] Add support for reference counting operations on foreign reference types.
2022-07-23 20:51:39 -07:00
zoecarver
13b0a5bc37 [nfc] Clang format changes. 2022-07-21 17:34:36 -04:00
Zoe Carver
9a660e1139 Merge pull request #60180 from zoecarver/locations-for-lazy-errors
[cxx-interop] Add source locations to diagnostics for un-importable APIs.
2022-07-21 17:31:11 -04:00
Ehud Adler
0f83b65f9b [cxx-interop] Remove impl for constexpr members (#60154)
* Remove impl for constexpr members

* Readd some tets

* Only remove import of static constexpr
2022-07-21 15:41:17 -04:00
zoecarver
446b2c38ab [cxx-interop] Add source locations to diagnostics for un-importable APIs. 2022-07-21 14:59:27 -04:00
zoecarver
74d76e2888 [cxx-interop] Add diagnostics and validation for retain/release function; require all foreign reference types to provide retain release functions or be immortal. 2022-07-21 12:25:21 -04:00
zoecarver
ee2a7eeeda [nfc][cxx-interop] Add diagnostics for invalid custom reference counting operations. 2022-07-21 10:25:57 -04: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
zoecarver
3498ff9765 [cxx-interop] Remove UnsafeLifetimeOperation record semantic kind.
This makes "owned" the default kind for records that have custom copy constructors and no pointer-members.
2022-07-18 17:15:15 -04:00
zoecarver
ca5fa9aa9b [nfc] Clang format changes. 2022-07-18 17:15:15 -04:00
zoecarver
9d1d03124b [nfc][cxx-interop] Add diagnostics when something cannot be imported. 2022-07-18 17:15:15 -04:00
zoecarver
6acffbbee6 [cxx-interop] Flip the switch: only import safe APIs. 2022-07-18 17:15:15 -04:00
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