@@ -100,6 +100,20 @@ static bool looksLikeInitMethod(ObjCSelector selector) {
100100 return !(firstPiece.size () > 4 && clang::isLowercase (firstPiece[4 ]));
101101}
102102
103+ // Enters and leaves a new lexical scope when emitting
104+ // members of a Swift type.
105+ struct CxxEmissionScopeRAII {
106+ DeclAndTypePrinter &printer;
107+ CxxDeclEmissionScope &prevScope;
108+ CxxDeclEmissionScope scope;
109+
110+ CxxEmissionScopeRAII (DeclAndTypePrinter &printer)
111+ : printer(printer), prevScope(printer.getCxxDeclEmissionScope()) {
112+ printer.setCxxDeclEmissionScope (scope);
113+ }
114+ ~CxxEmissionScopeRAII () { printer.setCxxDeclEmissionScope (prevScope); }
115+ };
116+
103117class DeclAndTypePrinter ::Implementation
104118 : private DeclVisitor<DeclAndTypePrinter::Implementation>,
105119 private TypeVisitor<DeclAndTypePrinter::Implementation, void ,
@@ -188,6 +202,12 @@ class DeclAndTypePrinter::Implementation
188202 }
189203
190204private:
205+ void recordEmittedDeclInCurrentCxxLexicalScope (const ValueDecl *vd) {
206+ assert (outputLang == OutputLanguageMode::Cxx);
207+ owningPrinter.getCxxDeclEmissionScope ().emittedDeclarationNames .insert (
208+ cxx_translation::getNameForCxx (vd));
209+ }
210+
191211 // / Prints a protocol adoption list: <code><NSCoding, NSCopying></code>
192212 // /
193213 // / This method filters out non-ObjC protocols.
@@ -215,6 +235,11 @@ class DeclAndTypePrinter::Implementation
215235 // / Prints the members of a class, extension, or protocol.
216236 template <bool AllowDelayed = false , typename R>
217237 void printMembers (R &&members) {
238+ CxxEmissionScopeRAII cxxScopeRAII (owningPrinter);
239+ // FIXME: Actually track emitted members in nested
240+ // lexical scopes.
241+ // FIXME: Emit unavailable C++ decls for not emitted
242+ // nested members.
218243 bool protocolMembersOptional = false ;
219244 for (const Decl *member : members) {
220245 auto VD = dyn_cast<ValueDecl>(member);
@@ -301,6 +326,7 @@ class DeclAndTypePrinter::Implementation
301326 ClangValueTypePrinter::forwardDeclType (os, CD, owningPrinter);
302327 ClangClassTypePrinter (os).printClassTypeDecl (
303328 CD, [&]() { printMembers (CD->getMembers ()); }, owningPrinter);
329+ recordEmittedDeclInCurrentCxxLexicalScope (CD);
304330 return ;
305331 }
306332
@@ -363,6 +389,7 @@ class DeclAndTypePrinter::Implementation
363389 }
364390 },
365391 owningPrinter);
392+ recordEmittedDeclInCurrentCxxLexicalScope (SD);
366393 }
367394
368395 void visitExtensionDecl (ExtensionDecl *ED) {
@@ -843,6 +870,7 @@ class DeclAndTypePrinter::Implementation
843870 printMembers (ED->getMembers ());
844871 },
845872 owningPrinter);
873+ recordEmittedDeclInCurrentCxxLexicalScope (ED);
846874 }
847875
848876 void visitEnumDecl (EnumDecl *ED) {
@@ -1746,10 +1774,14 @@ class DeclAndTypePrinter::Implementation
17461774 llvm::raw_string_ostream cFuncPrologueOS (cFuncDecl);
17471775 auto funcABI = Implementation (cFuncPrologueOS, owningPrinter, outputLang)
17481776 .printSwiftABIFunctionSignatureAsCxxFunction (FD);
1749- if (!funcABI)
1777+ if (!funcABI) {
1778+ owningPrinter.getCxxDeclEmissionScope ()
1779+ .additionalUnrepresentableDeclarations .push_back (FD);
17501780 return ;
1781+ }
17511782 owningPrinter.prologueOS << cFuncPrologueOS.str ();
17521783 printAbstractFunctionAsCxxFunctionThunk (FD, *funcABI);
1784+ recordEmittedDeclInCurrentCxxLexicalScope (FD);
17531785 return ;
17541786 }
17551787 if (FD->getDeclContext ()->isTypeContext ())
0 commit comments