3131#include " swift/Basic/Assertions.h"
3232#include " swift/Basic/SourceManager.h"
3333#include " swift/ClangImporter/ClangImporter.h"
34+ #include " swift/SIL/SILInstruction.h"
3435#include " swift/Strings.h"
3536
3637#include " clang/AST/Decl.h"
@@ -218,7 +219,10 @@ class ModuleWriter {
218219 return state.first == EmissionState::Defined;
219220 }
220221
221- bool require (const TypeDecl *D) {
222+ bool require (const TypeDecl *D) { return requireTypes (D, declsToWrite); }
223+
224+ template <typename T>
225+ bool requireTypes (const TypeDecl *D, T &types) {
222226 if (addImport (D)) {
223227 seenTypes[D] = { EmissionState::Defined, true };
224228 return true ;
@@ -229,7 +233,7 @@ class ModuleWriter {
229233 case EmissionState::NotYetDefined:
230234 case EmissionState::DefinitionRequested:
231235 state.first = EmissionState::DefinitionRequested;
232- declsToWrite .push_back (D);
236+ types .push_back (D);
233237 return false ;
234238 case EmissionState::Defined:
235239 return true ;
@@ -364,23 +368,31 @@ class ModuleWriter {
364368 }
365369
366370 bool hadAnyDelayedMembers = false ;
367- SmallVector<ValueDecl *, 4 > nestedTypes;
371+ SmallVector<const ValueDecl *, 4 > nestedTypes;
368372 for (auto member : members) {
369373 PrettyStackTraceDecl loopEntry (" printing for member" , member);
370374 auto VD = dyn_cast<ValueDecl>(member);
371375 if (!VD || !printer.shouldInclude (VD))
372376 continue ;
373377
374378 // Catch nested types and emit their definitions /after/ this class.
375- if (isa<TypeDecl>(VD)) {
376- // Don't emit nested types that are just implicitly @objc.
377- // You should have to opt into this, since they are even less
378- // namespaced than usual.
379- if (std::any_of (VD->getAttrs ().begin (), VD->getAttrs ().end (),
380- [](const DeclAttribute *attr) {
381- return isa<ObjCAttr>(attr) && !attr->isImplicit ();
382- })) {
383- nestedTypes.push_back (VD);
379+ if (const auto *TD = dyn_cast<TypeDecl>(VD)) {
380+ if (outputLangMode == OutputLanguageMode::Cxx) {
381+ if (!isa<TypeAliasDecl>(TD) && !isStringNestedType (VD, " UTF8View" ) &&
382+ !isStringNestedType (VD, " Index" )) {
383+ forwardDeclareType (TD);
384+ requireTypes (TD, nestedTypes);
385+ }
386+ } else {
387+ // Don't emit nested types that are just implicitly @objc.
388+ // You should have to opt into this, since they are even less
389+ // namespaced than usual.
390+ if (std::any_of (VD->getAttrs ().begin (), VD->getAttrs ().end (),
391+ [](const DeclAttribute *attr) {
392+ return isa<ObjCAttr>(attr) && !attr->isImplicit ();
393+ })) {
394+ nestedTypes.push_back (VD);
395+ }
384396 }
385397 continue ;
386398 }
@@ -1060,11 +1072,14 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
10601072 os << " }\n " ;
10611073 }
10621074
1075+ os << " #pragma clang diagnostic push\n " ;
1076+ os << " #pragma clang diagnostic ignored \" -Wreserved-identifier\"\n " ;
10631077 // Construct a C++ namespace for the module.
10641078 ClangSyntaxPrinter (os).printNamespace (
10651079 [&](raw_ostream &os) { ClangSyntaxPrinter (os).printBaseName (&M); },
10661080 [&](raw_ostream &os) { os << moduleOS.str (); },
10671081 ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate, &M);
1082+ os << " #pragma clang diagnostic pop\n " ;
10681083
10691084 if (M.isStdlibModule ()) {
10701085 os << " #pragma clang diagnostic pop\n " ;
0 commit comments