Commit Graph

2529 Commits

Author SHA1 Message Date
zoecarver
b280d26a7b [cxx-interop] Fix class template instantiation.
Lazily instantiate class template members. This means we no longer
reject some programs that clang accepts, such as the following:

```
template<class T> struct Foo { void fail(T value) { value.fail(); } };
using Bar = Foo<int>;
```

The above program will not error so long as `Bar::fail` isn't called.
(Previously, we'd fail to import `Bar`.)
2021-02-24 11:25:45 -08:00
Zoe Carver
bc6a7a6696 Merge pull request #35963 from zoecarver/cxx/bail-on-large-templates
[cxx-interop] Bail on deep template specializations.
2021-02-17 10:47:41 -08:00
zoecarver
2f0e7fc698 [cxx-interop] Bail on deep template specializations.
If a template specialization is more than 8 types deep, bail.

In future we could make this number (much) greater than 8 but first
we'll need to somehow make instantiating these types much fater.
Currently, I think there is some exponential type behavior happening so
this is super slow.
2021-02-16 10:34:52 -08:00
swift-ci
8cc5e96d08 Merge pull request #35990 from zoecarver/cxx/gardening/count-to-find 2021-02-15 23:49:35 -08:00
zoecarver
e70fb00f06 [nfc] Replace count() with find().
Addresses the post commit review comment in #35964 and replaces "count()"
with "find() != end()".
2021-02-15 21:10:53 -08:00
Doug Gregor
3d4091ccaa Merge pull request #35982 from DougGregor/clang-importer-concurrency-tweaks 2021-02-15 19:56:51 -08:00
Doug Gregor
a8f91c4efa [Clang importer] Allow @UIActor as an alias for @MainActor.
We've flip-flopped on the name a bit. Accept both for now.
2021-02-15 15:29:46 -08:00
Doug Gregor
c732ba3bf9 [Clang importer] Make @actorIndependent on imported macros implicit.
This prevents it from printing in Swift interfaces, which is a bit noisy.
2021-02-15 15:18:53 -08:00
Zoe Carver
6eb00d5c39 Merge pull request #35964 from zoecarver/cxx/another-double-add-fix
[cxx-interop] Skip already-imported sub decls.
2021-02-15 09:54:04 -08:00
Zoe Carver
ea9ce7e8d5 Merge pull request #35085 from zoecarver/cxx/central-cxx-module
[cxx-interop] Re-implement namespaces using enums + extensions.
2021-02-14 21:19:49 -08:00
zoecarver
bd96959d14 [cxx-interop] Re-implement namespaces using enums + extensions.
C++ namespaces are module-independent, but enums are owned by their module's in Swift. So, to prevent declaring two enums with the same name, this patch implements a new approach to namespaces: enums with extensions.

Here's an example:
```
// Module A
namespace N { void test1(); }
// Module B
namespace N { void test2(); }
// __ObjC module
enum N { }
// Swift module A
extension N { func test1() }
// Swift module B
extension N { func test1() }
```

Thanks to @gribozavr for the great idea.
2021-02-14 16:54:24 -08:00
swift-ci
f5e32aa610 Merge pull request #35957 from DougGregor/remove-hasAnyUnsafePointerParameters 2021-02-12 18:56:09 -08:00
zoecarver
fc33728613 [cxx-interop] Skip already-imported sub decls.
Rather than skipping non-definitions, we should just check whether we've
already seen this decl. This not only fixes the specific problem with
class templates but also is a more general fix for other sub decls.
2021-02-12 16:32:41 -08:00
Doug Gregor
62c10b1896 Remove unused function hasAnyUnsafePointerParameters() 2021-02-12 16:02:32 -08:00
Doug Gregor
71e0b1d90c [Clang importer] Don't suppress properties due to async method imports.
The Clang importer had some logic to suppress the import of a property that
had the same name as a method with no parameters. This logic
inadvertently meant that an async import of a completion-handler method
could prevent a (non-async) property of the same name to not be
imported, breaking existing code. In such cases, don't suppress the
property import.

Fixes rdar://73326019.
2021-02-12 13:19:16 -08:00
Doug Gregor
e4986a3514 [Clang importer] Remove @asyncHandler inference for Objective-C methods
The rule was inconsistent and required a lot of auditing, and could
cause surprises. We can consider bringing it back at another time.
2021-02-11 23:33:54 -08:00
Doug Gregor
abb5fbf1ce [Clang importer] Remove @asyncHandler inference for Objective-C methods
The rule was inconsistent and required a lot of auditing, and could
cause surprises. We can consider bringing it back at another time.
2021-02-11 22:37:28 -08:00
Evan Wilde
fc41826da9 Rename 'actor class' -> 'actor'
This patch softly updates the spelling of actors from `actor class` to
`actor`. We still accept using `actor` as a modifying attribute of
class, but emit a warning and fix-it to make the change.

One of the challenges that makes this messier is that the modifier list
can be in any order. e.g, `public actor class Foo {}` is the same as
`actor public class Foo {}`.

Classes have been updated to include whether they were explicitly
declared as an actor. This change updates the swiftmodule serialization
version number to 0.591. The additional bit only gets set of the class
declaration was declared as an actor, not if the actor was applied as an
attribute. This allows us to correctly emit `actor class` vs `actor`
emitting the code back out.
2021-02-10 08:05:57 -08:00
Doug Gregor
e2893cf138 [Concurrency] Disable async imports for Apple APIs deprecated by ~2018.
Implements rdar://73620586.
2021-02-09 14:43:45 -08:00
Zoe Carver
74b7341b2d Merge pull request #32378 from zoecarver/cxx/copy-const
[cxx-interop] Use user defined copy constructor to copy C++ objects.
2021-02-03 22:39:57 -08:00
zoecarver
b2b7f7b853 [cxx-interop] Use user defined copy constructor to copy C++ objects.
If a user-defined copy constructor exists, use that to copy imported C++
types.
2021-02-03 14:34:07 -08:00
swift-ci
e349794517 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-27 04:12:41 -08:00
Marcel Hlopko
4f5c75a236 [cxx-interop] Instantiate C++ class templates from Swift (#33284)
This PR makes it possible to instantiate C++ class templates from Swift. Given a C++ header:

```c++
// C++ module `ClassTemplates`
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};
```

it is now possible to write in Swift:

```swift
import ClassTemplates

func x() -> MagicWrapper<MagicNumber> {
  return MagicWrapper<MagicNumber>()
}
```

This is achieved by importing C++ class templates as generic structs, and then when Swift type checker calls `applyGenericArguments` we detect when the generic struct is backed by the C++ class template and call Clang to instantiate the template. In order to make it possible to put class instantiations such as `MagicWrapper<MagicNumber>` into Swift signatures, we have created a new field in `StructDecl` named `TemplateInstantiationType` where the typechecker stores the `BoundGenericType` which we serialize. Deserializer then notices that the `BoundGenericType` is actually a C++ class template and performs the instantiation logic.

Depends on https://github.com/apple/swift/pull/33420.
Progress towards https://bugs.swift.org/browse/SR-13261.
Fixes https://bugs.swift.org/browse/SR-13775.

Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Rosica Dejanovska <rosica@google.com>
2021-01-27 13:01:20 +01:00
swift-ci
f309608ccc Merge remote-tracking branch 'origin/main' into rebranch 2021-01-26 15:32:32 -08:00
Evan Wilde
8f81609a1b Imported macros are actor independent
Macros are thread safe. Marking them actor-independent will avoid
imported macros being reported as not concurrency safe.
2021-01-19 15:33:30 -08:00
zoecarver
cd4a58b320 Set C++ float type when converting evaluated value to string.
Fixes SR-14052.
2021-01-16 21:36:02 -08:00
swift-ci
4e05fc76ee Merge remote-tracking branch 'origin/main' into rebranch 2021-01-14 13:52:29 -08:00
zoecarver
ecd1018799 [cxx-interop] Use cached record when possible.
It is not completely uncommon for a record to get imported while
importing it's members (for example, if the member points back to the
parent record). In this case, simply use the already-imported record.
This should improve performance but also prevent an error where a member
accidentally had two parents.

This patch also moves around some of the member import/add logic to
allow for the above "optimization."
2021-01-13 11:19:36 -08:00
Nate Chandler
20db2c0981 Merge branch 'main' into rebranch
Conflicts:
	include/swift/Basic/AnyValue.h
2021-01-12 16:30:02 -08:00
Zoe Carver
8fb106c6e4 Merge pull request #35311 from zoecarver/cxx/inline-constexpr-vars
[cxx-interop] Inline constexpr vars.
2021-01-11 14:27:04 -08:00
swift-ci
59e985b410 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-09 12:53:09 -08:00
Doug Gregor
8c123e8505 [Concurrency] Hard-code support for importing @MainActor.
Our name lookup rules for the resolution of custom attributes don't
allow for them to find MainActor within the _Concurrency library.
Therefore, hardcode @MainActor to map to _Concurrency.MainActor.

While here, make sure we drop concurrency-specific attributes that
show up in Clang attributes when we aren't in concurrency mode.
2021-01-08 17:03:50 -08:00
zoecarver
6bc2696e24 [cxx-interop] Inline constexpr vars.
If a static variable can be evaluated at compile time, create an
accessor using that value. This means static variables will always be
inlined and removed.

Note: currently this only works with numeric types.
2021-01-08 14:22:25 -08:00
Doug Gregor
cd0380b646 [Clang importer] Import Clang swift_attr attribute.
The Clang swift_attr attribute allows C code to describe, via a Clang
attribute, the Swift attributes that should be applied to the given
declaration. When an

    __attribute__((__swift_attr__("@tribute")))

occurs on a Clang declaration, parse the attribute within the string
literal as a Swift attribute, then attach that to the imported Swift
declaration.

Fixes rdar://70146633.
2021-01-08 12:08:45 -08:00
swift_jenkins
f979b095ae Merge remote-tracking branch 'origin/main' into next 2020-12-22 20:58:03 -08:00
zoecarver
8746d046a1 [cxx-interop] Define implict constructor on the definition.
Make sure that we define a C++ record's implicit constructor on the
record's definition, not just on a declaration.
2020-12-22 13:32:15 -08:00
zoecarver
7ac1b8121e [cxx-interop] Skip forward-declared nested structs.
This prevents us from accidentially adding the same sub-type twice.
2020-12-22 13:23:12 -08:00
swift_jenkins
fbcb0e2d44 Merge remote-tracking branch 'origin/main' into next 2020-12-05 10:41:51 -08:00
Zoe Carver
8104d5ffbf Merge pull request #34956 from zoecarver/cxx/cache-nullptr
[gardening] Remove uses of "importDeclCached" and cleanup "VisitEnumConstantDecl".
2020-12-05 10:35:47 -08:00
swift_jenkins
8351c9dbf3 Merge remote-tracking branch 'origin/main' into next 2020-12-04 13:03:17 -08:00
zoecarver
3ac7dbf7bb [gardening] Remove uses of "importDeclCached" and cleanup "VisitEnumConstantDecl".
Cleans up the EnumConstantDecl visitor by consolidating cases and
removing calls to "importDeclCached".
2020-12-03 21:36:04 -08:00
zoecarver
c4363b916f [cxx-interop] Support class template specializations in namespaces.
This just changes an assertion and adds a test.
2020-12-03 12:13:07 -08:00
swift_jenkins
6e2b4b23ea Merge remote-tracking branch 'origin/main' into next 2020-12-01 10:17:48 -08:00
Zoe Carver
2b0ff64fdb Merge pull request #34870 from zoecarver/cxx/fix/no-params
[cxx-interop] Bail on functions that use unimportable types.
2020-12-01 10:12:20 -08:00
swift_jenkins
777e1a9698 Merge remote-tracking branch 'origin/main' into next 2020-11-30 15:14:27 -08:00
zoecarver
3d6c8a7971 [cxx-interop] Fix assertion to allow variadic members.
Simply fixes an assertion to allow variadic member functions.
2020-11-30 11:55:56 -08:00
zoecarver
6e69918a18 [cxx-interop] Bail on functions that use unimportable types.
We already fixed this for "global" functions. This is a more generic
solution that works for "nested" functions as well. (Members or
functions in a namespace.)
2020-11-24 12:59:58 -08:00
swift_jenkins
48e4ee6966 Merge remote-tracking branch 'origin/main' into next 2020-11-18 07:22:44 -08:00
Doug Gregor
c7c0fedb4a [Clang importer] Narrow mirrored-import fixes to only consider 'async'.
The previous change was too broad and caused several regressions.
2020-11-17 23:48:23 -08:00
Doug Gregor
d249b5cce5 [Clang importer] Make sure we mirror protocol decls for all names.
When mirroring declarations from protocols, make sure to mirror for
all potential imported names. Otherwise, we might miss out on one or
the other of an async import or a completion-handler import of the
same method.

Fixes rdar://71429577.
2020-11-17 22:26:41 -08:00