Skip to content

[ClangImporter] Swift Implemented ObjC Forward Declarations #62983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

NuriAmari
Copy link
Contributor

Objective-C libraries that wrap Swift types will often forward declare these types in public headers. Prior to this patch, if a Swift client consumed this Obejctive-C header, the forward declarations and dependent declarations will not be imported, despite the client importing the defining Swift module. The same is not true for Clang modules, which was confusing behavior.

This patch allows the ClangImporter to check if a matching @objc Swift declarations is available from the importing Swift context when encountering an incomplete ObjC interface or protocol. If such a Swift declaration is found, it is simply returned.

Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.

Resolves #NNNNN, fix apple/llvm-project#MMMMM.

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test source compatibility

@@ -321,6 +321,34 @@ void ModuleFile::lookupValue(DeclName name,
}
}

void ModuleFile::lookupSwiftValueByObjCName(
DeclName name, SmallVectorImpl<ValueDecl *> &results) {
if (Core->TopLevelDecls) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious how significant the performance penalty of iterating all declarations is / how difficult it is to alter the serialization format to include a table by objc name as well.

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test source compatibility

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test Windows platform

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test source compatibility

if (found)
return nullptr;
found = singleResult;
if (!languageVersion.isVersionAtLeast(6)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fallback was kept around so as to not break source compatibility, but it was never intended to work and should probably stop working in the next major version: #27921

importingContext.getValue()->getParentSourceFile();

// TODO: Make sure we aren't bailing early in legitimate cases
if (!importingSwiftSourceFile)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there are cases where the importing DeclContext does not lie within a source file. I would've thought when the main module is already serialized and being inspected by swift-ide-test would be such a case, but it doesn't seem so. As the comment says, lets make sure by bailing here we aren't leaving something important on the table.

@@ -528,6 +528,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
/// used when parsing the attribute text.
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;

/// If set, the Swift DeclContext from which the current
/// import request was triggered.
llvm::Optional<const swift::DeclContext *> ImportingContext;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the purposes of this patch, all that matters is that the provided DeclContext be within the parent SourceFile DeclContext. However, other use cases might require the DeclContext that is the direct parent of the usage? I don't know how easy it will be to pass that information to the lookup in all cases, so I think maybe its best to rename to SourceFileContext, or even just store a SourceFile.

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test source compatibility

2 similar comments
@NuriAmari
Copy link
Contributor Author

@swift-ci Please test source compatibility

@NuriAmari
Copy link
Contributor Author

@swift-ci Please test source compatibility

@NuriAmari NuriAmari closed this Jan 25, 2023
@NuriAmari NuriAmari deleted the swift-forwarding branch October 25, 2023 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant