Commit Graph

215 Commits

Author SHA1 Message Date
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
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Michael Gottesman
c62f31f5dc Inject llvm::SmallBitVector into namespace swift;
I also eliminated all llvm:: before SmallBitVector in the code base.
2018-09-21 09:49:25 -07:00
swift-ci
7414f8e8f5 Merge remote-tracking branch 'origin/master' into master-next 2018-07-26 11:09:23 -07:00
Jordan Rose
690d427b84 [ClangImporter] Check for API-note-provided attributes by source loc (#16704)
...rather than by whether they're implicit. We're planning to make
them explicit (prompted by a change in upstream Clang that no longer
prints implicit attributes).

rdar://problem/40346997 (Swift side)
2018-07-26 11:02:21 -07:00
Bob Wilson
c3e02955bb [master-next] Adjust for VersionTuple moving from clang to llvm.
LLVM r334399 (and related Clang changes) moved clang::VersionTuple to
llvm::VersionTuple. Update Swift to match.

Patch by Jason Molenda.
rdar://problem/41025046
2018-06-12 16:44:11 -07:00
Jordan Rose
a6ae2d7742 Hack: Force UIEdgeInsets.zero to always come from the SDK (#17122)
...instead of relying on the one in the overlay in pre-4.2 versions of
Swift. This caused crashes in deserialization, which (deliberately)
doesn't respect availability.

There are three changes here:

- Remove UIEdgeInsets.zero and UIOffset.zero from the UIKit overlay.
- Always use the 4.2 name for UIEdgeInsetsZero and UIOffsetZero from
  the underlying UIKit framework. (This is the nested name.)
- Ignore the unavailability messages for those two constants in
  pre-4.2 Swift, since we're now relying on them being present.

The latter two, the compiler changes, can go away once UIKit's API
notes no longer specify different pre-4.2 behavior, but meanwhile we
need to keep compatibility with the SDKs released in Xcode 10b1.

https://bugs.swift.org/browse/SR-7879
2018-06-12 13:40:23 -07:00
Rintaro Ishizaki
b3453c17fe [ClangImporter] Take isCompatibilityAlias() into account in interface printing (#16625)
If the Clang declrations are *types*, canonical declaration in Swift is
imported for newest version of Swift. In interface generation, if the
declaration is versioned and it's imported as a member in either or both
version of Swift, we have to take compatibility typealias into account.

* Fixed 'ClangModuleUnit::getTopLevelDecls' to take isCompatibilityAlias() into account
* Fixed bugs in ClangImporter where member-to-member versioned types aren't properly imported.
  * Fixed 'SwiftDeclConverter::importFullName' to check equality of getEffectiveContext()
  * Fixed 'importer::addEntryToLookupTable' to check equality of getEffectiveContext()
    (moved 'ClangImporter::Implementation::forEachDistinctName' to 'NameImporter')
2018-05-22 13:38:45 +09:00
Ted Kremenek
fe75380a71 Merge pull request #15407 from tkremenek/driver-ver-4.2
[WIP] Added -swift-version support for 4.2
2018-03-23 18:10:08 -07:00
Ted Kremenek
3da51018b6 Teach ClangImporter to handle effective Swift version with minor release.
Needed to support Swift 4.2.
2018-03-23 00:30:44 -07:00
Jordan Rose
52af3f3f65 [ClangImporter] Translate Clang's enum_extensibility to @_frozen
Still to do: test and fix up the use of multiple enum_extensibility
annotations, possibly with API notes. This is important because the
definition of NS/CF_ENUM /includes/ enum_extensibility(open) as of
Xcode 9.0; there should be a convenient out people can use to declare
exhaustive enums in C that's backwards-compatible.
2018-03-20 14:49:10 -07:00
Slava Pestov
34fd4ae512 AST: Use DeclBaseName::Kind::Constructor
Fixes <rdar://problem/35852727>, <https://bugs.swift.org/browse/SR-1660>,
<https://bugs.swift.org/browse/SR-6557>.
2018-03-16 00:25:56 -07:00
Doug Gregor
a49455e3b2 [Clang importer] Move the "all properties" lists into the name importer.
Swift's ASTContext contained all of the logic to find the complete list
of properties for an Objective-C class, which is used by the Clang importer
to influence the mapping of Objective-C names into Swift. Swift's
ASTContext also included a *cache* for this information, indexed by
the Clang `ObjCInterfaceDecl *`. However, this cache was getting
populated/queried from the Clang importer's name importer, such that
the keys would be Clang declarations used to build modules and then
deallocated. If that memory eventually gets reused for a different
`ObjCInterfaceDecl *`, we would get incorrect/stale all-properties
information.

Almost Surely fixes rdar://problem/35347167, which is a
nondeterministic failure where UIView's `addGestureRecognizer(_:)` gets
occasionally imported as `add(_:)`.
2017-11-08 15:25:15 -08:00
Jordan Rose
8a9495a3d1 [ClangImporter] Honor Swift 4 API notes in Swift 3 mode
*** Depends on Clang change "[APINotes] Record what version caused ***
*** an annotation to get replaced." Update your Clang checkout!    ***

More generally, change the meaning of the SwiftVersions section in API
notes to be "this version or earlier" rather than "exactly this
version". We mostly get this behavior for free from the Clang-side
changes, but for SwiftName and the enum annotations we look at inactive
attributes as well. The latter is simple, but the former means being
careful about finding the annotation we /would/ have picked, i.e. the
one closest to the version we requested.
2017-09-15 14:30:24 -07:00