@@ -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 }
@@ -1885,14 +1882,13 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
18851882 case DeclContextKind::EnumElementDecl:
18861883 llvm_unreachable (" cannot cross-reference this context" );
18871884
1888- case DeclContextKind::FileUnit:
1889- DC = cast<FileUnit>(DC)->getParentModule ();
1890- LLVM_FALLTHROUGH;
1891-
18921885 case DeclContextKind::Module:
1886+ llvm_unreachable (" should only cross-reference something within a file" );
1887+
1888+ case DeclContextKind::FileUnit:
18931889 abbrCode = DeclTypeAbbrCodes[XRefLayout::Code];
18941890 XRefLayout::emitRecord (Out, ScratchRecord, abbrCode,
1895- addModuleRef (cast<ModuleDecl>(DC) ), pathLen);
1891+ addContainingModuleRef (DC ), pathLen);
18961892 break ;
18971893
18981894 case DeclContextKind::GenericTypeDecl: {
@@ -1929,7 +1925,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
19291925 genericSig = ext->getGenericSignature ()->getCanonicalSignature ();
19301926 }
19311927 XRefExtensionPathPieceLayout::emitRecord (
1932- Out, ScratchRecord, abbrCode, addModuleRef (DC-> getParentModule () ),
1928+ Out, ScratchRecord, abbrCode, addContainingModuleRef (DC),
19331929 addGenericSignatureRef (genericSig));
19341930 break ;
19351931 }
@@ -2022,7 +2018,7 @@ void Serializer::writeCrossReference(const Decl *D) {
20222018 unsigned abbrCode;
20232019
20242020 if (auto op = dyn_cast<OperatorDecl>(D)) {
2025- writeCrossReference (op->getModuleContext (), 1 );
2021+ writeCrossReference (op->getDeclContext (), 1 );
20262022
20272023 abbrCode = DeclTypeAbbrCodes[XRefOperatorOrAccessorPathPieceLayout::Code];
20282024 auto nameID = addDeclBaseNameRef (op->getName ());
@@ -2034,7 +2030,7 @@ void Serializer::writeCrossReference(const Decl *D) {
20342030 }
20352031
20362032 if (auto prec = dyn_cast<PrecedenceGroupDecl>(D)) {
2037- writeCrossReference (prec->getModuleContext (), 1 );
2033+ writeCrossReference (prec->getDeclContext (), 1 );
20382034
20392035 abbrCode = DeclTypeAbbrCodes[XRefOperatorOrAccessorPathPieceLayout::Code];
20402036 auto nameID = addDeclBaseNameRef (prec->getName ());
0 commit comments