[Serialization] Delay adding ModuleFiles until they're loaded

Rather than adding a ModuleFile to a parent module
and then removing it afterwards if it fails to
load, let's wait until we've loaded the file before
deciding to add it to the parent module. This then
allows us to get rid of `ModuleDecl::removeFile`.

In addition, push down the calls to `addFile` into
the callers of `loadAST` in preparation for
`addFile` being replaced with a one-time-only call
to a `setFiles` method.
This commit is contained in:
Hamish Knight
2020-05-28 12:09:10 -07:00
parent f810cfcc45
commit 6477cc68eb
5 changed files with 21 additions and 27 deletions

View File

@@ -680,7 +680,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
// We've loaded the file. Now try to bring it into the AST.
auto fileUnit = new (Ctx) SerializedASTFile(M, *loadedModuleFile);
M.addFile(*fileUnit);
if (extendedInfo.isTestable())
M.setTestingEnabled();
if (extendedInfo.arePrivateImportsEnabled())
@@ -705,8 +704,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
findOverlayFiles(diagLoc.getValueOr(SourceLoc()), &M, fileUnit);
return fileUnit;
}
M.removeFile(*fileUnit);
}
// From here on is the failure path.
@@ -960,12 +957,15 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
StringRef moduleInterfacePathStr =
Ctx.AllocateCopy(moduleInterfacePath.str());
if (!loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
std::move(moduleSourceInfoInputBuffer), isFramework)) {
auto *file =
loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
std::move(moduleSourceInfoInputBuffer), isFramework);
if (file) {
M->addFile(*file);
} else {
M->setFailedToLoad();
}
return M;
}
@@ -996,11 +996,12 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
auto *M = ModuleDecl::create(moduleID.Item, Ctx);
SWIFT_DEFER { M->setHasResolvedImports(); };
if (!loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
std::move(moduleInputBuffer), {}, {}, isFramework)) {
auto *file = loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
std::move(moduleInputBuffer), {}, {}, isFramework);
if (!file)
return nullptr;
}
M->addFile(*file);
Ctx.LoadedModules[moduleID.Item] = M;
return M;
}