Skip to content

Commit a4f5ac0

Browse files
author
Nuri Amari
committed
Import objc forward declarations
1 parent 9ad0f98 commit a4f5ac0

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ namespace swift {
786786

787787
/// If true, forward declarations will be imported using unavailable types
788788
/// instead of dropped altogether when possible.
789-
bool ImportForwardDeclarations = false;
789+
bool ImportForwardDeclarations = true;
790790

791791
/// If true ignore the swift bridged attribute.
792792
bool DisableSwiftBridgeAttr = false;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,12 +4372,33 @@ namespace {
43724372
clangModule))
43734373
return native;
43744374

4375-
Impl.addImportDiagnostic(
4376-
decl, Diagnostic(diag::forward_declared_protocol_label, decl),
4377-
decl->getSourceRange().getBegin());
4375+
if (Impl.ImportForwardDeclarations) {
4376+
auto result = Impl.createDeclWithClangNode<ProtocolDecl>(
4377+
decl, AccessLevel::Public,
4378+
Impl.getClangModuleForDecl(decl->getCanonicalDecl(),
4379+
/*allowForwardDeclaration=*/true),
4380+
SourceLoc(), SourceLoc(), name,
4381+
ArrayRef<PrimaryAssociatedTypeName>(), None,
4382+
/*TrailingWhere=*/nullptr);
4383+
4384+
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = result;
4385+
result->setSuperclass(Impl.getNSObjectProtocolType());
4386+
result->setAddedImplicitInitializers(); // suppress all initializers
4387+
addObjCAttribute(result,
4388+
Impl.importIdentifier(decl->getIdentifier()));
4389+
SmallVector<InheritedEntry, 4> inheritedTypes = {
4390+
TypeLoc::withoutLoc(Impl.getNSObjectProtocolType())};
4391+
result->setInherited(Impl.SwiftContext.AllocateCopy(inheritedTypes));
4392+
result->setImplicit();
4393+
return result;
4394+
} else {
4395+
Impl.addImportDiagnostic(
4396+
decl, Diagnostic(diag::forward_declared_protocol_label, decl),
4397+
decl->getSourceRange().getBegin());
43784398

4379-
forwardDeclaration = true;
4380-
return nullptr;
4399+
forwardDeclaration = true;
4400+
return nullptr;
4401+
}
43814402
}
43824403

43834404
decl = decl->getDefinition();
@@ -4443,7 +4464,7 @@ namespace {
44434464
nullptr, dc,
44444465
/*isActor*/false);
44454466
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = result;
4446-
result->setSuperclass(Type());
4467+
result->setSuperclass(Impl.getNSObjectType());
44474468
result->setAddedImplicitInitializers(); // suppress all initializers
44484469
result->setHasMissingVTableEntries(false);
44494470
addObjCAttribute(result, Impl.importIdentifier(decl->getIdentifier()));
@@ -4497,15 +4518,23 @@ namespace {
44974518
}
44984519

44994520
if (Impl.ImportForwardDeclarations) {
4500-
// Fake it by making an unavailable opaque @objc root class.
4501-
auto result = createFakeRootClass(name);
4521+
auto result = Impl.createDeclWithClangNode<ClassDecl>(
4522+
decl, AccessLevel::Public, SourceLoc(), name, SourceLoc(), None,
4523+
nullptr,
4524+
Impl.getClangModuleForDecl(decl->getCanonicalDecl(),
4525+
/*allowForwardDeclaration=*/true),
4526+
/*isActor*/ false);
4527+
Type superclassType = Impl.getNSObjectType();
4528+
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = result;
4529+
SmallVector<InheritedEntry, 4> inheritedTypes{
4530+
TypeLoc::withoutLoc(superclassType)};
4531+
result->setInherited(Impl.SwiftContext.AllocateCopy(inheritedTypes));
4532+
result->setSuperclass(superclassType);
4533+
result->setAddedImplicitInitializers(); // suppress all initializers
4534+
result->setHasMissingVTableEntries(false);
4535+
addObjCAttribute(result,
4536+
Impl.importIdentifier(decl->getIdentifier()));
45024537
result->setImplicit();
4503-
auto attr = AvailableAttr::createPlatformAgnostic(Impl.SwiftContext,
4504-
"This Objective-C class has only been forward-declared; "
4505-
"import its owning module to use it");
4506-
result->getAttrs().add(attr);
4507-
result->getAttrs().add(
4508-
new (Impl.SwiftContext) ForbidSerializingReferenceAttr(true));
45094538
return result;
45104539
} else {
45114540
Impl.addImportDiagnostic(

0 commit comments

Comments
 (0)