Commit Graph

2529 Commits

Author SHA1 Message Date
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
Ehud Adler
43f757faf4 Clean up & add test 2022-02-23 19:45:27 -05:00
Ehud Adler
52dfb1acb3 Fix issue where mutable syth methods were not in the lookup table 2022-02-23 19:42:08 -05:00
zoecarver
bb00e8dcfe [cxx-interop] Rudimentary support for importing base classes.
This commit adds very basic support for importing and calling base class methods, getting and setting base class fields, and using types inside of base classes.
2022-02-18 15:59:52 -08:00
Ehud Adler
18c6bdd212 [Cxx iterop] Disambiguate between methods with the same name but different constness 2022-02-09 19:49:02 -05:00
Becca Royal-Gordon
8c844126d4 [ClangImporter] Make completion handlers Sendable
If a method has an `async` variant, the non-`async` variant will now mark its completion handler parameter `@Sendable`. This shouldn't be a breaking change in Swift 5 code since these declarations are automatically `@_predatesConcurrency`.

Also adds:

• Support for `@_nonSendable` on parameters, which can be used to override this implicit `@Sendable`
• Support for `@Sendable` on block typedefs; it's generally going to be a good idea to mark completion block typedefs `@Sendable`.

Fixes rdar://85569247.
2022-01-25 15:49:54 -08:00
Miguel Salinas
c356875f81 Merge pull request #40856 from salinas-miguel/import-cgfloat
Import CGFloat as CoreFoundation.CGFloat if possible
2022-01-21 11:34:52 -08:00
Miguel Salinas
8f9871a5fe invert fallback logic 2022-01-21 00:04:27 -08:00
Alex Lorenz
e106551028 [NFC][interop] rename the PrintAsObjC library to PrintAsClang
We're starting to support emission of C++ header interfaces, so a language-agnostic name makes more sense
2022-01-20 11:31:58 -08:00
Zoe Carver
0b1688f5af Merge pull request #40905 from zoecarver/re-land-nested-namespace-fix
[cxx-interop] Re-land part of 6ba7a1e. Fix an issue with extending nested namespaces across modules.
2022-01-19 18:58:31 -08:00
salinas-miguel
6d269e5670 refactor and add test 2022-01-19 13:00:03 -08:00
zoecarver
76e2934c34 [cxx-interop] Re-land part of 6ba7a1e. Fix an issue with extending nested namespaces across modules.
No one should be using C++ interop yet, so this part of the change cannot break anyone.
2022-01-19 12:20:14 -08:00
salinas-miguel
5d9e77de16 remove StringRefs 2022-01-14 13:40:42 -08:00
salinas-miguel
b74e510a5c Import as CoreFoundation.CGFloat if possible 2022-01-14 09:51:19 -08:00
Holly Borla
86730caf18 [Interop] Handle existential types in ClangTypeConverter and
the ClangImporter.
2022-01-13 19:30:44 -08:00
Robert Widmann
8d2bf20db0 Always Import FieldDecls from Records
When we switched this path over to use lazy member loading, a
subsequent commit introduced a check that members were canonical. In
certain extremely twisty scenarios involving multiple modular frameworks
pulling in non-modular headers, this check can fail which results in us
silently dropping these members on the floor.

It's ultimately correct to only pull in the canonical members, but real-world
examples of "system" frameworks evading modularity diagnostics exist so this
is strictly a regression.

Drop the canonicity check for FieldDecls.

rdar://86740970
2022-01-07 00:23:11 -08:00
zoecarver
036361d1e4 [cxx-interop] Add SIL function representation cxx_method; Support extending C++ types.
There are three major changes here:
    1. The addition of "SILFunctionTypeRepresentation::CXXMethod".
    2. C++ methods are imported with their members *last*. Then the arguments are switched when emitting the IR for an application of the function.
    3. Clang decls are now marked as foreign witnesses.

These are all steps towards being able to have C++ protocol conformance.
2022-01-06 14:26:47 -08:00
Nuri Amari
130f2de7fd Improve ClangImporter failure diagnostics
This patch introduces new diagnostics to the ClangImporter to help
explain why certain C, Objective-C or C++ declarations fail to import
into Swift. This patch includes new diagnostics for the following entities:

- C functions
- C struct fields
- Macros
- Objective-C properties
- Objective-C methods

In particular, notes are attached to indicate when any of the above
entities fail to import as a result of refering an incomplete (only
forward declared) type.

The new diangostics are hidden behind two new flags, -enable-experimental-clang-importer-diagnostics
and -enable-experimental-eager-clang-module-diagnostics. The first flag emits diagnostics lazily,
while the second eagerly imports all declarations visible from loaded Clang modules. The first
flag is intended for day to day swiftc use, the second for module linting or debugging the importer.
2022-01-02 12:43:59 -05:00
Nathan Lanza
dc0bd52221 Merge pull request #40234 from ellishg/main
Do not import objc_direct constructors
2021-12-21 13:02:43 -05:00
Becca Royal-Gordon
a9bd88a47d Don't try to import variants for FieldDecls
Back when we were eagerly importing struct fields, we only attempted to import fields under the names they have in the current version; previous versions and the raw name were ignored. Now that we're importing them lazily, we're passing through code that attempts to import all versions. That's a nice idea in theory, but neither ImportDecl nor the rest of the compiler was prepared for this, and so ImportDecl has started adding redundant stored properties to clang structs. This trips an assertion in IRGen.

This commit returns to the old behavior of only importing struct fields under their current name by simply early-exiting from SwiftDeclConverter::VisitFieldDecl(). We can come up with a solution that imports the variants in the future.

Fixes rdar://86069786.
2021-12-16 14:56:55 -08:00
Robert Widmann
ba3bc0f073 [NFC] Drop Unused Storage Decl 2021-12-14 12:38:56 -08:00
Ellis Hoag
96bc8210c5 Do not import objc_direct constructors
Unlike normal Objective-C functions, Swift does not directly call
Objective-C constructors because it creates a thunk to allocate the
object and call then constructor dynamically. This can be fixed, but for
now emit a compile-time error rather than a runtime error.
2021-12-09 15:05:15 -08:00
Xi Ge
c5a715d4ed Revert "[cxx-interop] Fix two issues with extending nested types across modules."
This reverts commit 6ba7a1ec1e.
2021-12-08 20:00:41 -08:00
zoecarver
fc3b3a1d71 [cxx-interop] Implement foreign reference types.
This is an expiremental feature to allow an attribute, `import_as_ref`, to import a C++ record as a non-reference-counted reference type in Swift.
2021-12-08 15:35:18 +00:00
Puyan Lotfi
1ae0d2aadd [cxx-interop] Fixing usage of ObjC Categories in C++-Interop mode.
In C/ObjC interop mode a Clang AST with an ObjC Category resembles:

TranslationUnitDecl
`-ObjCCategoryDecl

However in C++-Interop mode the same Category could potentially have an
extern "C" linkage specifier (guarded with #ifdef __cplusplus) and
resembles:

TranslationUnitDecl
|-LinkageSpecDecl
  `-ObjCCategoryDecl

In the latter case when the ClangImporter attempts to import the
category in swift::ClangImporter::Implementation::importDeclContextOr,
prior to this patch, would bail because it is expecting the DeclContext
above the ObjCCategoryDecl to be a TranslationUnitDecl and when it isn't
it returns nullptr.

Because of this, of course the category does not get imported as a swift
extension and therefore any fields or methods are also not imported.

In the case of UIKit, UIView has one of these categories that are
extern-"C"'ed due to UIKIT_EXTERN containing the linkage specifier in
-enable-cxx-interop mode. Since UIView is inherited by lots of other
UIKit types (UILabel etc), many of these types also have lots of missing fields
as well.

This patch checks to see if the decl about to be imported in
importDeclContextOr has a linkage specifier as it's context and makes
sure to materialize the TU from the parent of the declcontext instead of
just immediately bailing. Because of this, this patch enables
c++-interop mode to compile code that uses UIKit without hitting this
mis-compile.
2021-12-05 04:06:14 -05:00
Doug Gregor
8b198d95be [Clang importer] Eliminate the use of lookupDirect when getting all members.
Loading of the members of a C(++) struct/class can occur while doing a
direct lookup, so triggering a second direct lookup inside there can
introduce a request-evaluator cycle. Reimplement this operation to be
more like the way we lazily populate Objective-C classes and protocols,
walking through the record members in order and importing their
variants, then adding those. This eliminates a bunch of extraneous
lookup work, keeps the members in order (see the test case change),
and eliminates the potential for cycles.
2021-12-03 15:43:57 -08:00
Doug Gregor
4fccfccadf [Clang importer] Don't insert members found via lookup into the wrong context.
Discovered while investigating rdar://85840928.
2021-12-01 09:57:32 -08:00
Zoe Carver
d08ecf359d Merge pull request #40336 from zoecarver/set-lazy-complete-after-load
[nfc][cxx-interop] Mark lazy member loading complete only after members are loaded.
2021-11-30 16:34:01 -08:00
zoecarver
f3b7a40f1e [nfc][cxx-interop] Mark lazy member loading complete only after members are loaded. 2021-11-30 11:54:58 -08:00
Doug Gregor
16f32fd50a Eliminate support for @MainActor(unsafe) Clang attribute.
The `@MainActor(unsafe)` attribute could be provided for C declarations
via the Clang `swift_attr` attribute. However, this facility was never
used outside of tests, and has been superceded by `@MainActor` with the
inferred `@_predatesConcurrency`.
2021-11-29 15:05:18 -08:00
Becca Royal-Gordon
ad0e7096ba Make NS_ENUM &c. Sendable even when audited
An explicit swift_attr("@_nonSendable") will override it (except for ns_error_domain where the type is embedded in another type that's forced to be Sendable), but swift_attr("@_nonSendable(_assumed)") will not.
2021-11-19 11:34:02 -08:00
Becca Royal-Gordon
b9480f12d0 Allow imported types to add a Sendable conformance
...by using `__attribute__((swift_attr("@Sendable")))`. `@_nonSendable` will "beat" `@Sendable`, while `@_nonSendable(_assumed)` will not.

This commit also checks if `SwiftAttr` supports `#pragma clang attribute` and, if it does, defines `__SWIFT_ATTR_SUPPORTS_SENDABLE_DECLS` in imported headers so they know they can apply these attributes in an auditing style.
2021-11-19 11:34:01 -08:00
Becca Royal-Gordon
1695d61664 [ClangImporter] Import SwiftAttrs early
This change applies SwiftAttr attributes as soon as possible after creating an instance of a Decl, rather than waiting until the declaration is "finished". That makes sure the attributes can influence the declaration very early in its lifecycle, and in particular, before its conformance table is initialized.

Mostly NFC in this commit (other than affecting the order that attributes are printed in), but necessary for future changes in this PR.
2021-11-19 11:34:01 -08:00
Becca Royal-Gordon
d2d786a76a [NFC] Add ClangImporter header diagnostic helper 2021-11-19 11:34:01 -08:00
Becca Royal-Gordon
0842795eb5 [NFC] Let SynthesizedProtocolAttrs be @unchecked
This is not yet used by anything.
2021-11-19 11:34:01 -08:00
Saleem Abdulrasool
4d44953691 Revert "Support __available__((swift_attr("@Sendable")))" 2021-11-19 07:40:24 -08:00
Becca Royal-Gordon
3d2d4e1721 Merge pull request #40170 from beccadax/send-me-an-object
Support __available__((swift_attr("@Sendable")))
2021-11-19 01:26:10 -08:00
Zoe Carver
8980b8b6ec Merge pull request #40165 from zoecarver/fix-extensions-across-modules
[cxx-interop] Fix two issues with extending nested types across modules.
2021-11-16 09:08:06 -08:00
zoecarver
6ba7a1ec1e [cxx-interop] Fix two issues with extending nested types across modules.
One fix allows extending nested records in other modules, the other fixes the same issue for namespaces.
2021-11-15 16:20:15 -08:00
Becca Royal-Gordon
8751658f6c Make NS_ENUM &c. Sendable even when audited
An explicit swift_attr("@_nonSendable") will override it (except for ns_error_domain where the type is embedded in another type that's forced to be Sendable), but swift_attr("@_nonSendable(_assumed)") will not.
2021-11-12 23:13:29 -08:00
Becca Royal-Gordon
7549278314 Allow imported types to add a Sendable conformance
...by using `__attribute__((swift_attr("@Sendable")))`. `@_nonSendable` will "beat" `@Sendable`, while `@_nonSendable(_assumed)` will not.

This commit also checks if `SwiftAttr` supports `#pragma clang attribute` and, if it does, defines `__SWIFT_ATTR_SUPPORTS_SENDABLE_DECLS` in imported headers so they know they can apply these attributes in an auditing style.
2021-11-12 23:13:29 -08:00
Becca Royal-Gordon
de768e8fda [ClangImporter] Import SwiftAttrs early
This change applies SwiftAttr attributes as soon as possible after creating an instance of a Decl, rather than waiting until the declaration is "finished". That makes sure the attributes can influence the declaration very early in its lifecycle, and in particular, before its conformance table is initialized.

Mostly NFC in this commit (other than affecting the order that attributes are printed in), but necessary for future changes in this PR.
2021-11-12 23:12:38 -08:00
Becca Royal-Gordon
b372ff61af [NFC] Add ClangImporter header diagnostic helper 2021-11-12 23:12:38 -08:00
Becca Royal-Gordon
953ffc3c8d [NFC] Let SynthesizedProtocolAttrs be @unchecked
This is not yet used by anything.
2021-11-12 23:12:38 -08:00
Zoe Carver
c6474b970d Revert "Implement ClangImporter importSourceLoc and importSourceRange (#40010)"
This reverts commit 8eb8263f81.
2021-11-11 12:09:55 -08:00
Nuri Amari
8eb8263f81 Implement ClangImporter importSourceLoc and importSourceRange (#40010)
Co-authored-by: Nuri Amari <nuriamari@fb.com>
2021-11-09 16:31:03 -08:00
Robert Widmann
22405cefea Plumb the "Is Type Sequence" Bit Through the Surface AST 2021-11-08 13:48:30 -08:00
Slava Pestov
f38f25a3de AST: Factor out AbstractGenericSignatureRequest into a new buildGenericSignature() function
This is slightly cleaner.
2021-10-30 00:35:59 -04:00
zoecarver
a5c0eb0e9c [cxx-interop] Fix lazy loading for result types.
When applying attributes, don't check the result type of the Swift function, so that we can get the benefits of lazy loading.
2021-10-25 14:40:22 -07:00
zoecarver
b8e52a7ad2 [cxx-interop] Lazily import members of Clang namespaces and records via requests.
Also adds a ClangImporter request zone and move some requests into it.
2021-10-20 14:52:43 -07:00