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.
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.
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`.
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.
...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.
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.
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.
...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.
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.
If possible, add imported members to the StructDecl's LookupTable rather than adding them directly as members. This will fix the issues with ordering that #39436 poorly attempted to solve during IRGen.
This also allows us to break out most of the test changes from #39436.
A non functional change replacing auto with
Decl* for some declarations using ClangImporter::Implementation::importDecl.
Increases clarity and makes subsequent planned changes easier.
If a defaulted template type parameter is not used in the function's
signature, don't create a corresponding generic argument for that
template type. This allows us to call function templates with defaulted
template type parameters. This is very common in the standard library
for things like enable_if which is used to disable various
functions/overloads with SFINAE.
The biggest part of this change is going forward not all function
templates will be imported as generic functions in Swift. This should
work OK but we may discover there was some logic which only looked for
generic function when dealing with function templates.
Note: we only lazily load the result if it's a record, because otherwise it's trivial to load when importing the function. Also, we still eagerly import operator's results types.
For clang symbols marked with SPI_AVAILABLE, we add SPIAccessControlAttr to them so they will be
considered as SPIs in the AST. To be able to use all these symbols, we also add an implicit SPI import
statement for all clang modules. All clang SPIs belong to the same SPI group named "OBJC_DEFUALT_SPI_GROUP" because clang
currently doesn't support custom SPI group.
rdar://73902734