@@ -180,7 +180,7 @@ namespace {
180180 int32_t getNameDataForBase (const NominalTypeDecl *nominal,
181181 StringRef *dataToWrite = nullptr ) {
182182 if (nominal->getDeclContext ()->isModuleScopeContext ())
183- return -Serializer.addModuleRef (nominal->getParentModule ());
183+ return -Serializer.addContainingModuleRef (nominal->getDeclContext ());
184184
185185 auto &mangledName = MangledNameCache[nominal];
186186 if (mangledName.empty ())
@@ -731,7 +731,12 @@ IdentifierID Serializer::addFilename(StringRef filename) {
731731 return addUniquedString (filename).second ;
732732}
733733
734- IdentifierID Serializer::addModuleRef (const ModuleDecl *M) {
734+ IdentifierID Serializer::addContainingModuleRef (const DeclContext *DC) {
735+ assert (!isa<ModuleDecl>(DC) &&
736+ " References should be to things within modules" );
737+ const FileUnit *file = cast<FileUnit>(DC->getModuleScopeContext ());
738+ const ModuleDecl *M = file->getParentModule ();
739+
735740 if (M == this ->M )
736741 return CURRENT_MODULE_ID;
737742 if (M == this ->M ->getASTContext ().TheBuiltinModule )
@@ -743,18 +748,10 @@ IdentifierID Serializer::addModuleRef(const ModuleDecl *M) {
743748 if (M == clangImporter->getImportedHeaderModule ())
744749 return OBJC_HEADER_MODULE_ID;
745750
746- // If we're referring to a member of a private module that will be
747- // re-exported via a public module, record the public module's name.
748- if (auto clangModuleUnit =
749- dyn_cast<ClangModuleUnit>(M->getFiles ().front ())) {
750- auto exportedModuleName =
751- M->getASTContext ().getIdentifier (
752- clangModuleUnit->getExportedModuleName ());
753- return addDeclBaseNameRef (exportedModuleName);
754- }
755-
756- assert (!M->getName ().empty ());
757- return addDeclBaseNameRef (M->getName ());
751+ auto exportedModuleName = file->getExportedModuleName ();
752+ assert (!exportedModuleName.empty ());
753+ auto exportedModuleID = M->getASTContext ().getIdentifier (exportedModuleName);
754+ return addDeclBaseNameRef (exportedModuleID);
758755}
759756
760757SILLayoutID Serializer::addSILLayoutRef (SILLayout *layout) {
@@ -1650,7 +1647,7 @@ Serializer::writeConformance(ProtocolConformanceRef conformanceRef,
16501647 abbrCode,
16511648 addDeclRef (normal->getProtocol ()),
16521649 addDeclRef (normal->getType ()->getAnyNominal ()),
1653- addModuleRef (normal->getDeclContext ()-> getParentModule ()));
1650+ addContainingModuleRef (normal->getDeclContext ()));
16541651 }
16551652 break ;
16561653 }
@@ -1888,14 +1885,13 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
18881885 case DeclContextKind::EnumElementDecl:
18891886 llvm_unreachable (" cannot cross-reference this context" );
18901887
1891- case DeclContextKind::FileUnit:
1892- DC = cast<FileUnit>(DC)->getParentModule ();
1893- LLVM_FALLTHROUGH;
1894-
18951888 case DeclContextKind::Module:
1889+ llvm_unreachable (" should only cross-reference something within a file" );
1890+
1891+ case DeclContextKind::FileUnit:
18961892 abbrCode = DeclTypeAbbrCodes[XRefLayout::Code];
18971893 XRefLayout::emitRecord (Out, ScratchRecord, abbrCode,
1898- addModuleRef (cast<ModuleDecl>(DC) ), pathLen);
1894+ addContainingModuleRef (DC ), pathLen);
18991895 break ;
19001896
19011897 case DeclContextKind::GenericTypeDecl: {
@@ -1932,7 +1928,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
19321928 genericSig = ext->getGenericSignature ()->getCanonicalSignature ();
19331929 }
19341930 XRefExtensionPathPieceLayout::emitRecord (
1935- Out, ScratchRecord, abbrCode, addModuleRef (DC-> getParentModule () ),
1931+ Out, ScratchRecord, abbrCode, addContainingModuleRef (DC),
19361932 addGenericSignatureRef (genericSig));
19371933 break ;
19381934 }
@@ -2025,7 +2021,7 @@ void Serializer::writeCrossReference(const Decl *D) {
20252021 unsigned abbrCode;
20262022
20272023 if (auto op = dyn_cast<OperatorDecl>(D)) {
2028- writeCrossReference (op->getModuleContext (), 1 );
2024+ writeCrossReference (op->getDeclContext (), 1 );
20292025
20302026 abbrCode = DeclTypeAbbrCodes[XRefOperatorOrAccessorPathPieceLayout::Code];
20312027 auto nameID = addDeclBaseNameRef (op->getName ());
@@ -2037,7 +2033,7 @@ void Serializer::writeCrossReference(const Decl *D) {
20372033 }
20382034
20392035 if (auto prec = dyn_cast<PrecedenceGroupDecl>(D)) {
2040- writeCrossReference (prec->getModuleContext (), 1 );
2036+ writeCrossReference (prec->getDeclContext (), 1 );
20412037
20422038 abbrCode = DeclTypeAbbrCodes[XRefOperatorOrAccessorPathPieceLayout::Code];
20432039 auto nameID = addDeclBaseNameRef (prec->getName ());
0 commit comments