Commit Graph

228 Commits

Author SHA1 Message Date
Doug Gregor
d7367997d2 Merge pull request #34985 from DougGregor/import-swift-async-attr
[Concurrency] Implement support for swift_async attribute.
2020-12-10 10:09:28 -08:00
Doug Gregor
c55b9cc879 Merge pull request #34984 from DougGregor/import-swift-async-name
[Concurrency] Adopt Clang swift_async_name attribute.
2020-12-09 13:05:02 -08:00
Doug Gregor
719e2201c8 [Concurrency] Move "extra" text from completion handler parameter to base name.
When a completion-handler parameter has text before, e.g.,
"WithCompletionHandler", put the extra text on the base name rather
than the corresponding parameter. This ensures that we don't lose the
detail from the text, but always put it into a consistent place.
2020-12-07 16:47:31 -08:00
Doug Gregor
b837675910 [Concurrency] Introduce more completion-handler names to the import heuristics.
Extend the set of completion-handler names we look for to infer an
`async` import of an Objective-C method, which includes:

* (with)CompletionBlock
* (with)reply
* (with)replyTo

both as parameter names and as base name suffixes.
2020-12-07 16:47:31 -08:00
Doug Gregor
3cd115e283 [Concurrency] Implement support for swift_async attribute.
The `swift_async` attribute helps control how a method is imported as
`async` into Swift.

Implements rdar://70111787.
2020-12-06 23:21:18 -08:00
Doug Gregor
abe0308c21 [Concurrency] Adopt Clang swift_async_name attribute.
The new `swift_async_name` attribute allows Clang declarations to specify
their Swift names only for `async` import, which may differ from those used
for importing as a completion handler.

Implements rdar://70111787.
2020-12-06 22:42:24 -08:00
swift-ci
8ec7bdf634 Merge pull request #34429 from zoecarver/cxx/skip-unkown-constructor-names 2020-10-31 20:05:26 -07:00
Joe Groff
7ab9e87b68 SILGen: Stub out support for invoking foreign async methods.
Allow SILGen to not crash when invoking foreign async methods by emitting
`undef` for the completion callback going into the call, and for the
results that would be channeled back through awaiting the continuation.
2020-10-27 10:22:32 -07:00
zoecarver
d843279ea7 [cxx-interop] Bail instead of crashing If we cannot find a constructor decl in importNameImpl.
It's possible to have a "UnresolvedUsingValueDecl" with a "CXXConstructorName". If that's the case (or if there's any other unkown decl) bail instead of crashing.
2020-10-25 14:00:42 -07:00
Doug Gregor
b094758bd3 Merge pull request #34394 from DougGregor/concurrency-objc-name-adjustments
[Concurrency] Don't lose name information from completion-handler arguments
2020-10-22 16:38:41 -07:00
Doug Gregor
16104d8045 [Concurrency] Don't lose name information from completion-handler arguments.
When a completion handler parameter has a selector piece that ends with
"WithCompletion(Handler)", prepend the text before that suffix to the
base name or previous argument label, as appropriate. This ensures that
we don't lose information from the name, particularly with delegate names.
2020-10-22 13:32:03 -07:00
Doug Gregor
fa9d4fd931 Merge pull request #34382 from DougGregor/concurrency-objc-get-async
[Concurrency] Asynchronous Objective-C method importer naming adjustments
2020-10-22 08:28:12 -07:00
Doug Gregor
24ea8becc8 [Concurrency] Move removal of leading "get" for async imports.
Name adjustments should be performed by omitNeedlessWords(), not the
early classifcation of async imports.
2020-10-21 21:48:52 -07:00
Doug Gregor
e1852956a0 [Concurrency] Drop "get" prefix when importing Objective-C methods as async.
Implements rdar://70506634.
2020-10-21 12:33:49 -07:00
zoecarver
545b44e9c1 [cxx-interop] Look through template decl to find constructor when importing the function name.
Sometimes, on windows, we get a function template wrapping the
constructor decl. In this case, look through the function template to
find the constructor decl.
2020-10-16 10:11:13 -07:00
zoecarver
4364af39ee [cxx-interop] Small fixes and cleanup based on review.
* Check isAggregate instead of hasUserDeclaredConstructor.
 * Rename addEmptyArgNamesForCxxFunc ->  addEmptyArgNamesForClangFunction.
 * Other minor fixes and cleanups.
2020-10-09 10:42:54 -07:00
Martin Boehme
5644137ea0 Eliminate duplication of code for adding empty argument names. 2020-10-09 10:42:48 -07:00
Martin Boehme
a5e953b690 Add support for calling C++ constructors.
Because C++ constructors always take a `this` pointer to the object to
be initialized, we mark the SIL function return type with the `@out`
attribute.

On the IRGen side, we retrofit support for formal indirect return values as
well as thin metatypes.
2020-10-09 10:42:46 -07:00
Doug Gregor
fe4a8bb9f9 [Clang importer] Allow both sync and async imports with the same name.
The Clang importer was filtering out cases where the same declaration
is imported twice under the same name, which can now happen when one
is synchronous and one is asynchronous. This happens when, e.g., an
Objective-C class provides both a completion-hander-based asynchronous
version and a synchronous version, and the Swift names line up after
the completion-handler parameter is dropped.

Stop filtering these out. Overload resolution is capable of handling
synchronous/asynchronous overloading based on context.
2020-10-02 22:15:04 -07:00
Doug Gregor
0f18948bd5 [Concurrency] Allow @objc async methods.
Allow the declaration of @objc async methods, mapping them to a
completion-handler API in Objective-C. This covers most of the
checking and semantics within the type checker:
* Declaring @objc async methods and checking their parameter/result types
* Determining the default Objective-C selector by adding
completionHandler/WithCompletionHandler as appropriate
* Determining the type of the completion handler parameter
* Inferring @objc from protocol requirements
* Inferring @objc from an overridden method
2020-09-08 10:15:24 -07:00
Xi Ge
c403b140e1 ClangImporter: refactor ClangImporterOptions to be ASTContext-owned. NFC
We need ClangImporterOptions to be persistent for several scenarios: (1)
when creating a sub-ASTContext to build Swift modules from interfaces; and
(2) when creating a new Clang instance to invoke Clang dependencies scanner.

This change is NFC.
2020-09-01 14:04:22 -07:00
Doug Gregor
6ad2757bef [Concurrency] Use completion/completionHandler parameter names for async import
Extend the check for completion handler parameters to also consider the
name of the parameter (not its argument label). If it's `completion` or
`completionHandler`, we have a completion handler. This extends our
API coverage for importing Objective-C methods with completion
handlers as 'async'.
2020-08-28 13:58:02 -07:00
Doug Gregor
1e5d30f5ca [Concurrency] Import Objective-C methods with completion handlers as async
When a given Objective-C method has a completion handler parameter
with an appropriate signature, import that Objective-C method as
async. For example, consider the following CloudKit API:

    - (void)fetchShareParticipantWithUserRecordID:(CKRecordID
*)userRecordID
            completionHandler:(void (^)(CKShareParticipant * _Nullable shareParticipant, NSError * _Nullable error))completionHandler;

With the experimental concurrency model, this would import as:

    func fetchShareParticipant(withUserRecordID userRecordID: CKRecord.ID) async throws -> CKShare.Participant?

The compiler will be responsible for turning the caller's continuation
into a block to pass along to the completion handler. When the error
parameter of the completion handler is non-null, the async call
will result in that error being thrown. Otherwise, the other arguments
passed to that completion handler will be returned as the result of
the async call.

async versions of methods are imported alongside their
completion-handler versions, to maintain source compatibility with
existing code that provides a completion handler.

Note that this only covers the Clang importer portion of this task.
2020-08-27 21:30:13 -07:00
Doug Gregor
16876fbf66 [Clang importer] Drop unused parameter from getParamOptionality().
This operation is not actually dependent on the version.
2020-08-27 17:06:42 -07:00
Marcel Hlopko
cb537b41fb [cxx-interop] Import typedef-ed template instantiations #32950 (#33451)
This is a roll-forward of https://github.com/apple/swift/pull/32950, with explicit c++17 version removed from tests. This is not needed since C++17 is the default anyway.

--

In this PR we teach `ClangImporter` to import typedef statements with template instantiation as its underlying type.

```c++
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};

typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
```

will be made available in Swift as if `WrappedMagicNumber` is a regular struct. 

In C++, multiple distinct typedeffed instantiations resolve to the same canonical type. We implement this by creating a hidden intermediate struct that typedef aliasses.

The struct is named as `__CxxTemplateInst` plus Itanium mangled type of the instantiation. For the example above the name of the hidden struct is `__CxxTemplateInst12MagicWrapperI11MagicNumberE`. Double underscore (denoting a reserved C++ identifier) is used to discourage direct usage. We chose Itanium mangling scheme because it produces valid Swift identifiers and covers all C++ edge cases.

Imported module interface of the example above:

```swift
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
  var t: MagicNumber
}
struct MagicNumber {}
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE
```

We modified the `SwiftLookupTable` logic to show hidden structs in `swift_ide_test` for convenience.

Co-authored-by: Rosica Dejanovska <rosica@google.com>
Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Robert Widmann <devteam.codafi@gmail.com>
2020-08-14 20:51:16 +02:00
Meghana Gupta
6b61818fff Revert "[cxx-interop] Import typedef-ed template instantiations (#32950)"
This reverts commit 643aa2d896.
2020-08-12 12:37:13 -07:00
Marcel Hlopko
643aa2d896 [cxx-interop] Import typedef-ed template instantiations (#32950)
In this PR we teach `ClangImporter` to import typedef statements with template instantiation as its underlying type.

```c++
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};

typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
```

will be made available in Swift as if `WrappedMagicNumber` is a regular struct. 

In C++, multiple distinct typedeffed instantiations resolve to the same canonical type. We implement this by creating a hidden intermediate struct that typedef aliasses.

The struct is named as `__CxxTemplateInst` plus Itanium mangled type of the instantiation. For the example above the name of the hidden struct is `__CxxTemplateInst12MagicWrapperI11MagicNumberE`. Double underscore (denoting a reserved C++ identifier) is used to discourage direct usage. We chose Itanium mangling scheme because it produces valid Swift identifiers and covers all C++ edge cases.

Imported module interface of the example above:

```swift
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
  var t: MagicNumber
}
struct MagicNumber {}
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE
```

We modified the `SwiftLookupTable` logic to show hidden structs in `swift_ide_test` for convenience.

Resolves https://bugs.swift.org/browse/SR-12591.

Co-authored-by: Rosica Dejanovska <rosica@google.com>
Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Robert Widmann <devteam.codafi@gmail.com>
2020-08-12 16:54:22 +02:00
Michael Forster
26358c4588 Import member operator functions as static members (#32293)
This adds support to `ClangImporter` to import C++ member function operators as static methods into Swift, which is part of SR-12748.

The left-hand-side operand, which gets passed as the `this` pointer to the C++ function is represented as an additional first parameter in the Swift method. It gets mapped back in SILGen.

Two of the tests are disabled on Windows because we can't yet call member functions correctly on Windows (SR-13129).
2020-07-03 11:06:22 +02:00
zoecarver
3a23e086d2 [cxx-interop] Re-order operators.
Re-order operator case statements and tests. The order now follows the order defined in `llvm-project/clang/include/clang/Basic/OperatorKinds.def`.

Also, adds operator character(s) in parentheses.
2020-06-17 15:54:09 -07:00
zoecarver
d411f92cdd [cxx-interop] Add support for C++ comparison operators.
Adds support for C++ operators: `<`, `>`, `==`, `!=`, `<=`, and `>=`.
2020-06-17 15:54:09 -07:00
Zoe Carver
71d2c37828 [cxx-interop] Add support for percent, ampersand, and pipe operators. (#32332)
Adds support for three more basic infix operators: `%`, `&`, and `|`.

Co-authored-by: Michael Forster <forster@google.com>
2020-06-12 12:30:33 -07:00
Zoe Carver
1e52852cca [cxx-interop] Add support for C++ shift operators. (#32333)
* [cxx-interop] Add support for C++ shift operators.

Support imported C++ `<<` and `>>` operators in Swift.

* Update test names of existing operators

... to match the new ones.

Co-authored-by: Michael Forster <forster@google.com>
2020-06-12 08:40:25 -07:00
zoecarver
d4431c4883 [cxx-interop] Add support for C++ logical and/or operators.
Support imported C++ `&&` and `||` operators in Swift.
2020-06-11 17:08:12 -07:00
Michael Forster
c98c862e7e Start implementing C++ operator interop (#32015)
This imports the four basic arithmetic operators `+`, `-`, `*`, `/`
declared as inline non-member functions.

More to come.
2020-05-28 17:31:36 +02:00
Joe Groff
fb34044408 Merge remote-tracking branch 'origin/master' into master-next 2019-12-10 12:46:41 -08:00
Varun Gandhi
1abd35a4c6 [NFC] Lift out common "canImportAsOptional" to a new header. 2019-11-22 12:43:07 -08:00
swift-ci
12921530ac Merge remote-tracking branch 'origin/master' into master-next 2019-11-02 19:30:15 -07:00
Slava Pestov
7ba2b35e6a ClangImporter: Fix init kind computation with overridden constructors
We used to compute the init kind from the overridden declaration;
this was switched to use the base declaration in 42f72cb0d.

Refactor the init kind computation a little to get the old behavior
back. Otherwise, if a class defines an initializer named -init, we
always import it as designated by virtue of overridding NSObject's
-init.

Fixes <rdar://problem/56674158>.
2019-11-01 18:54:10 -04:00
swift-ci
9938778e19 Merge remote-tracking branch 'origin/master' into master-next 2019-10-29 15:50:05 -07:00
Jordan Rose
b7c4c5197b [ClangImporter] Simplify inferDefaultArgument by removing numParams (#27864)
The only thing this was used for is to test if the total number of
parameters was 1, in which case we did the same thing we did for a
first parameter except in one very contrived case: a method with more
than one parameter whose base name starts with "set" and whose first
parameter is an NSZone. There are zero of these in the macOS or iOS
SDKs, and probably even fewer in third-party code.
2019-10-29 15:38:53 -07:00
Xi Ge
64c0680686 Merge remote-tracking branch 'apple/master' into master-next 2019-10-28 10:19:36 -07:00
Jordan Rose
42f72cb0d0 [ClangImporter] Compute initializer kinds up front (#27870)
Previously we only did this for factory methods, but there's no reason
why we can't do it for regular init methods too, and doing so
simplifies the signature of SwiftDeclConverter::importConstructor.

Also remove some indirection through helper functions in ClangAdapter.
These were more useful back when Swift looked directly at API notes
instead of relying on Clang turning them into attributes; now they're
just an extra hop for no reason.
2019-10-25 15:16:14 -07:00
Christopher Rogers
390540315b Only hit cache once, not twice 2019-10-23 10:40:33 +09:00
Adrian Prantl
468b74bd70 Update Swift master-next for upstream llvm.org changes. 2019-10-21 13:31:31 -07:00
Jordan Rose
5d817d9d58 [ClangImporter] Save a bit of malloc traffic with BumpPtrAllocator (#27552)
No functionality change.
2019-10-08 10:44:26 -07:00
Argyrios Kyrtzidis
439361ef08 Merge pull request #27432 from akyrtzi/check-macro-numeric-char
[ClangImporter] In `shouldIgnoreMacro()` Use `Preprocessor::getSpellingOfSingleCharacterNumericConstant()` for determining if a macro is '1'
2019-09-30 11:29:38 -07:00
Argyrios Kyrtzidis
a2fca13a3a [ClangImporter] In shouldIgnoreMacro() Use Preprocessor::getSpellingOfSingleCharacterNumericConstant() for determining if a macro is '1'
`tok.getLiteralData()` does not work for a macro imported from a clang module (returns `nullptr`), while `getSpellingOfSingleCharacterNumericConstant` covers both kinds of macros (defined in source or imported from a module).

Unfortunately this currently only matters for an internal tool so I cannot accompany this change with a test case.
2019-09-29 13:54:47 -07:00
Jordan Rose
8ff1dac381 [AST] Break some header dependencies for faster rebuilds (#27374)
DiagnosticEngine.h no longer depends on Attr.h.
Expr.h no longer depends on TypeRepr.h.

No functionality change.
2019-09-26 09:17:10 -07:00
Varun Gandhi
cca2872e02 Fix issue SR-9482.
Solution based on the outline in the issue description.

Also fixes the corresponding rdar://problem/46644027.
2019-07-15 14:57:04 -07:00
Parker Schuh
fa69a73ee4 Add -enable-cxx-interop flag and support for extern "C" {} 2019-07-08 11:43:35 -07:00