Skip to content

[ClangImporter] Resolve forward declarations before importing names #7555

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

Merged
merged 1 commit into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4030,18 +4030,8 @@ namespace {
}

Decl *VisitObjCInterfaceDecl(const clang::ObjCInterfaceDecl *decl) {
Optional<ImportedName> correctSwiftName;
auto importedName = importFullName(decl, correctSwiftName);
if (!importedName) return nullptr;

// If we've been asked to produce a Swift 2 stub, handle it via a
// typealias.
if (correctSwiftName)
return importSwift2TypeAlias(decl, importedName, *correctSwiftName);

auto name = importedName.getDeclName().getBaseName();

auto createRootClass = [=](DeclContext *dc = nullptr) -> ClassDecl * {
auto createRootClass = [=](Identifier name,
DeclContext *dc = nullptr) -> ClassDecl * {
if (!dc) {
dc = Impl.getClangModuleForDecl(decl->getCanonicalDecl(),
/*allowForwardDeclaration=*/true);
Expand Down Expand Up @@ -4076,11 +4066,26 @@ namespace {
const ClassDecl *nsObjectDecl =
nsObjectTy->getClassOrBoundGenericClass();

auto result = createRootClass(nsObjectDecl->getDeclContext());
auto result = createRootClass(Impl.SwiftContext.Id_Protocol,
nsObjectDecl->getDeclContext());
result->setForeignClassKind(ClassDecl::ForeignKind::RuntimeOnly);
return result;
}

if (auto *definition = decl->getDefinition())
decl = definition;

Optional<ImportedName> correctSwiftName;
auto importedName = importFullName(decl, correctSwiftName);
if (!importedName) return nullptr;

// If we've been asked to produce a Swift 2 stub, handle it via a
// typealias.
if (correctSwiftName)
return importSwift2TypeAlias(decl, importedName, *correctSwiftName);

auto name = importedName.getDeclName().getBaseName();

if (!decl->hasDefinition()) {
// Check if this class is implemented in its adapter.
if (auto clangModule = Impl.getClangModuleForDecl(decl, true)) {
Expand All @@ -4092,7 +4097,7 @@ namespace {

if (Impl.ImportForwardDeclarations) {
// Fake it by making an unavailable opaque @objc root class.
auto result = createRootClass();
auto result = createRootClass(name);
result->setImplicit();
auto attr = AvailableAttr::createPlatformAgnostic(Impl.SwiftContext,
"This Objective-C class has only been forward-declared; "
Expand All @@ -4105,9 +4110,6 @@ namespace {
return nullptr;
}

decl = decl->getDefinition();
assert(decl);

auto dc =
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
if (!dc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@class SwiftClass, SwiftClassWithCustomName;

@interface BridgingHeader
+ (void)takeForward:(SwiftClass *)class;
+ (void)takeRenamedForward:(SwiftClassWithCustomName *)class;
@end
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Manual PrintAsObjC for testing purposes.

struct PureClangType {
int x;
int y;
Expand All @@ -16,7 +18,7 @@ struct PureClangType {
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
#endif

#ifndef SWIFT_EXTENSION(X)
#ifndef SWIFT_EXTENSION
# define SWIFT_EXTENSION(X) X##__LINE__
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ extension BaseClass {
@objc public func getSwiftEnum() -> SwiftEnum { return .Quux }
public init() {}
}

@objc(RenamedClass) public class SwiftClass {}

public func getSwiftClass() -> SwiftClass {
return SwiftClass()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: cp -r %S/Inputs/mixed-framework/Mixed.framework %t

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t/Mixed.framework/Modules/Mixed.swiftmodule/%target-swiftmodule-name %S/Inputs/mixed-framework/Mixed.swift -import-underlying-module -F %t -module-name Mixed -disable-objc-attr-requires-foundation-module

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -F %t -import-objc-header %S/Inputs/import-mixed-framework-with-forward.h %s -verify

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-pch -F %t %S/Inputs/import-mixed-framework-with-forward.h -o %t/bridge.pch
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -F %t -import-objc-header %t/bridge.pch %s -verify


// REQUIRES: objc_interop

import Mixed

BridgingHeader.takeForward(SwiftClass(x: 42))
BridgingHeader.takeRenamedForward(CustomNameClass())

// Check that we're compiling at all.
BridgingHeader.takeRenamedForward(SwiftClass(x: 42)) // expected-error {{cannot convert value of type 'SwiftClass' to expected argument type 'CustomNameClass!'}}