@@ -235,6 +235,30 @@ class ModuleWriter {
235235 });
236236 }
237237
238+ void forwardDeclareType (const TypeDecl *TD) {
239+ if (auto CD = dyn_cast<ClassDecl>(TD)) {
240+ if (!forwardDeclare (CD)) {
241+ (void )addImport (CD);
242+ }
243+ } else if (auto PD = dyn_cast<ProtocolDecl>(TD)) {
244+ forwardDeclare (PD);
245+ } else if (auto TAD = dyn_cast<TypeAliasDecl>(TD)) {
246+ bool imported = false ;
247+ if (TAD->hasClangNode ())
248+ imported = addImport (TD);
249+ assert ((imported || !TAD->isGeneric ()) &&
250+ " referencing non-imported generic typealias?" );
251+ } else if (addImport (TD)) {
252+ return ;
253+ } else if (auto ED = dyn_cast<EnumDecl>(TD)) {
254+ forwardDeclare (ED);
255+ } else if (isa<AbstractTypeParamDecl>(TD)) {
256+ llvm_unreachable (" should not see type params here" );
257+ } else {
258+ assert (false && " unknown local type decl" );
259+ }
260+ }
261+
238262 bool forwardDeclareMemberTypes (DeclRange members, const Decl *container) {
239263 PrettyStackTraceDecl
240264 entry (" printing forward declarations needed by members of" , container);
@@ -312,26 +336,7 @@ class ModuleWriter {
312336 // FIXME: It would be nice to diagnose this.
313337 }
314338
315- if (auto CD = dyn_cast<ClassDecl>(TD)) {
316- if (!forwardDeclare (CD)) {
317- (void )addImport (CD);
318- }
319- } else if (auto PD = dyn_cast<ProtocolDecl>(TD)) {
320- forwardDeclare (PD);
321- } else if (auto TAD = dyn_cast<TypeAliasDecl>(TD)) {
322- bool imported = false ;
323- if (TAD->hasClangNode ())
324- imported = addImport (TD);
325- assert ((imported || !TAD->isGeneric ()) && " referencing non-imported generic typealias?" );
326- } else if (addImport (TD)) {
327- return ;
328- } else if (auto ED = dyn_cast<EnumDecl>(TD)) {
329- forwardDeclare (ED);
330- } else if (isa<AbstractTypeParamDecl>(TD)) {
331- llvm_unreachable (" should not see type params here" );
332- } else {
333- assert (false && " unknown local type decl" );
334- }
339+ forwardDeclareType (TD);
335340 });
336341
337342 if (needsToBeIndividuallyDelayed) {
@@ -374,11 +379,22 @@ class ModuleWriter {
374379 printer.print (CD);
375380 return true ;
376381 }
377-
382+
378383 bool writeFunc (const FuncDecl *FD) {
379384 if (addImport (FD))
380385 return true ;
381386
387+ PrettyStackTraceDecl entry (
388+ " printing forward declarations needed by function" , FD);
389+ ReferencedTypeFinder::walk (
390+ FD->getInterfaceType (),
391+ [&](ReferencedTypeFinder &finder, const TypeDecl *TD) {
392+ PrettyStackTraceDecl entry (" walking its interface type, currently at" ,
393+ TD);
394+ forwardDeclareType (TD);
395+ });
396+
397+ os << ' \n ' ;
382398 printer.print (FD);
383399 return true ;
384400 }
0 commit comments