@@ -1012,6 +1012,39 @@ class DeclAndTypePrinter::Implementation
10121012 sel.getSelectorPieces ().front ().str () == " init" ;
10131013 }
10141014
1015+ // / Returns true if the given function overload is safe to emit in the current
1016+ // / C++ lexical scope.
1017+ bool canPrintOverloadOfFunction (const AbstractFunctionDecl *funcDecl) const {
1018+ assert (outputLang == OutputLanguageMode::Cxx);
1019+ auto &overloads =
1020+ owningPrinter.getCxxDeclEmissionScope ().emittedFunctionOverloads ;
1021+ auto cxxName = cxx_translation::getNameForCxx (funcDecl);
1022+ auto overloadIt = overloads.find (cxxName);
1023+ if (overloadIt == overloads.end ()) {
1024+ overloads.insert (std::make_pair (
1025+ cxxName,
1026+ llvm::SmallVector<const AbstractFunctionDecl *>({funcDecl})));
1027+ return true ;
1028+ }
1029+ auto selfArity =
1030+ funcDecl->getParameters () ? funcDecl->getParameters ()->size () : 0 ;
1031+ for (const auto *overload : overloadIt->second ) {
1032+ auto arity =
1033+ overload->getParameters () ? overload->getParameters ()->size () : 0 ;
1034+ // Avoid printing out an overload with the same and arity, as that might
1035+ // be an ambiguous overload on the C++ side.
1036+ // FIXME: we should take types into account, not all overloads with the
1037+ // same arity are ambiguous in C++.
1038+ if (selfArity == arity) {
1039+ owningPrinter.getCxxDeclEmissionScope ()
1040+ .additionalUnrepresentableDeclarations .push_back (funcDecl);
1041+ return false ;
1042+ }
1043+ }
1044+ overloadIt->second .push_back (funcDecl);
1045+ return true ;
1046+ }
1047+
10151048 void printAbstractFunctionAsMethod (AbstractFunctionDecl *AFD,
10161049 bool isClassMethod,
10171050 bool isNSUIntegerSubscript = false ,
@@ -1051,6 +1084,12 @@ class DeclAndTypePrinter::Implementation
10511084 if (!dispatchInfo)
10521085 return ;
10531086 }
1087+ // FIXME: handle getters/setters ambiguities here too.
1088+ if (!isa<AccessorDecl>(AFD)) {
1089+ if (!canPrintOverloadOfFunction (AFD))
1090+ return ;
1091+ }
1092+
10541093 owningPrinter.prologueOS << cFuncPrologueOS.str ();
10551094
10561095 printDocumentationComment (AFD);
@@ -1779,6 +1818,8 @@ class DeclAndTypePrinter::Implementation
17791818 .additionalUnrepresentableDeclarations .push_back (FD);
17801819 return ;
17811820 }
1821+ if (!canPrintOverloadOfFunction (FD))
1822+ return ;
17821823 owningPrinter.prologueOS << cFuncPrologueOS.str ();
17831824 printAbstractFunctionAsCxxFunctionThunk (FD, *funcABI);
17841825 recordEmittedDeclInCurrentCxxLexicalScope (FD);
0 commit comments