Skip to content

[PrintAsObjC] Import/fwd-declare types used in @_cdecl functions. #37436

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
Nov 15, 2021
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
2 changes: 1 addition & 1 deletion lib/PrintAsObjC/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ class DeclAndTypePrinter::Implementation

printAvailability(FD);

os << ';';
os << ";\n";
}

enum class PrintLeadingSpace : bool {
Expand Down
58 changes: 37 additions & 21 deletions lib/PrintAsObjC/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,30 @@ class ModuleWriter {
});
}

void forwardDeclareType(const TypeDecl *TD) {
if (auto CD = dyn_cast<ClassDecl>(TD)) {
if (!forwardDeclare(CD)) {
(void)addImport(CD);
}
} else if (auto PD = dyn_cast<ProtocolDecl>(TD)) {
forwardDeclare(PD);
} else if (auto TAD = dyn_cast<TypeAliasDecl>(TD)) {
bool imported = false;
if (TAD->hasClangNode())
imported = addImport(TD);
assert((imported || !TAD->isGeneric()) &&
"referencing non-imported generic typealias?");
} else if (addImport(TD)) {
return;
} else if (auto ED = dyn_cast<EnumDecl>(TD)) {
forwardDeclare(ED);
} else if (isa<AbstractTypeParamDecl>(TD)) {
llvm_unreachable("should not see type params here");
} else {
assert(false && "unknown local type decl");
}
}

bool forwardDeclareMemberTypes(DeclRange members, const Decl *container) {
PrettyStackTraceDecl
entry("printing forward declarations needed by members of", container);
Expand Down Expand Up @@ -312,26 +336,7 @@ class ModuleWriter {
// FIXME: It would be nice to diagnose this.
}

if (auto CD = dyn_cast<ClassDecl>(TD)) {
if (!forwardDeclare(CD)) {
(void)addImport(CD);
}
} else if (auto PD = dyn_cast<ProtocolDecl>(TD)) {
forwardDeclare(PD);
} else if (auto TAD = dyn_cast<TypeAliasDecl>(TD)) {
bool imported = false;
if (TAD->hasClangNode())
imported = addImport(TD);
assert((imported || !TAD->isGeneric()) && "referencing non-imported generic typealias?");
} else if (addImport(TD)) {
return;
} else if (auto ED = dyn_cast<EnumDecl>(TD)) {
forwardDeclare(ED);
} else if (isa<AbstractTypeParamDecl>(TD)) {
llvm_unreachable("should not see type params here");
} else {
assert(false && "unknown local type decl");
}
forwardDeclareType(TD);
});

if (needsToBeIndividuallyDelayed) {
Expand Down Expand Up @@ -374,11 +379,22 @@ class ModuleWriter {
printer.print(CD);
return true;
}

bool writeFunc(const FuncDecl *FD) {
if (addImport(FD))
return true;

PrettyStackTraceDecl entry(
"printing forward declarations needed by function", FD);
ReferencedTypeFinder::walk(
FD->getInterfaceType(),
[&](ReferencedTypeFinder &finder, const TypeDecl *TD) {
PrettyStackTraceDecl entry("walking its interface type, currently at",
TD);
forwardDeclareType(TD);
});

os << '\n';
printer.print(FD);
return true;
}
Expand Down
33 changes: 33 additions & 0 deletions test/PrintAsObjC/cdecl-imports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -parse-as-library -emit-objc-header-path %t/swift.h
// RUN: %FileCheck %s < %t/swift.h
// RUN: %check-in-clang %t/swift.h

// REQUIRES: objc_interop

import CoreGraphics
import Foundation

// CHECK: @import CoreGraphics;
// CHECK-NOT: @import Foundation;

// CHECK: @class Bee;
// CHECK-LABEL: Bee * _Nonnull fwd_declares_bee(void) SWIFT_WARN_UNUSED_RESULT;

@_cdecl("fwd_declares_bee")
public func fwdDeclaresBee() -> Bee { fatalError() }

// CHECK: @class Hive;
// CHECK-LABEL: void fwd_declares_hive(SWIFT_NOESCAPE Hive * _Nonnull (* _Nonnull bzzz)(Bee * _Nonnull));

@_cdecl("fwd_declares_hive")
public func fwdDeclaresHive(bzzz: @convention(c) (Bee) -> Hive) { fatalError() }

// CHECK: @protocol NSWobbling;
// CHECK-LABEL: void fwd_declares_wobble(id <NSWobbling> _Nonnull wobbler);

@_cdecl("fwd_declares_wobble")
public func fwdDeclaresWobble(wobbler: NSWobbling) { fatalError() }

@_cdecl("imports_cgpoint")
public func importsCGPoint(pt: CGPoint) { fatalError() }