[Serialization] Refactor subset of ModuleFile into ModuleFileSharedCore

The difference with `ModuleFile` is that `ModuleFileSharedCore` provides immutable data and is independent of a particular ASTContext.
It is designed to be able to be shared across multiple `ModuleFile`s of different `ASTContext`s in a thread-safe manner.
This commit is contained in:
Argyrios Kyrtzidis
2020-05-25 14:00:51 -07:00
parent 9c20198498
commit 99c29145b2
13 changed files with 2780 additions and 2332 deletions

View File

@@ -12,6 +12,7 @@
#include "swift/Serialization/SerializedModuleLoader.h"
#include "ModuleFile.h"
#include "ModuleFileSharedCore.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/ModuleDependencies.h"
@@ -364,11 +365,11 @@ llvm::ErrorOr<ModuleDependencies> SerializedModuleLoaderBase::scanModuleFile(
return moduleBuf.getError();
// Load the module file without validation.
std::unique_ptr<ModuleFile> loadedModuleFile;
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFile;
bool isFramework = false;
serialization::ExtendedValidationInfo extInfo;
serialization::ValidationInfo loadInfo =
ModuleFile::load(modulePath.str(),
ModuleFileSharedCore::load(modulePath.str(),
std::move(moduleBuf.get()),
nullptr,
nullptr,
@@ -675,14 +676,17 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
serialization::ExtendedValidationInfo extendedInfo;
std::unique_ptr<ModuleFile> loadedModuleFile;
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFileCore;
serialization::ValidationInfo loadInfo =
ModuleFile::load(moduleInterfacePath,
ModuleFileSharedCore::load(moduleInterfacePath,
std::move(moduleInputBuffer),
std::move(moduleDocInputBuffer),
std::move(moduleSourceInfoInputBuffer),
isFramework, loadedModuleFile,
isFramework, loadedModuleFileCore,
&extendedInfo);
if (loadInfo.status == serialization::Status::Valid) {
loadedModuleFile =
std::make_unique<ModuleFile>(std::move(loadedModuleFileCore));
M.setResilienceStrategy(extendedInfo.getResilienceStrategy());
// We've loaded the file. Now try to bring it into the AST.
@@ -717,16 +721,19 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
// From here on is the failure path.
// Even though the module failed to load, it's possible its contents include
// a source buffer that need to survive because it's already been used for
// diagnostics.
if (auto orphanedBuffer = loadedModuleFile->takeBufferForDiagnostics())
OrphanedMemoryBuffers.push_back(std::move(orphanedBuffer));
if (diagLoc)
serialization::diagnoseSerializedASTLoadFailure(
Ctx, *diagLoc, loadInfo, extendedInfo, moduleBufferID,
moduleDocBufferID, loadedModuleFile.get(), M.getName());
// Even though the module failed to load, it's possible its contents include
// a source buffer that need to survive because it's already been used for
// diagnostics.
// Note this is only necessary in case a bridging header failed to load
// during the `associateWithFileContext()` call.
if (loadedModuleFile && loadedModuleFile->mayHaveDiagnosticsPointingAtBuffer())
OrphanedModuleFiles.push_back(std::move(loadedModuleFile));
return nullptr;
}
@@ -794,20 +801,20 @@ void swift::serialization::diagnoseSerializedASTLoadFailure(
Ctx.LangOpts.DebuggerSupport)) {
return false;
}
return duplicates.insert(dependency.RawPath).second;
return duplicates.insert(dependency.Core.RawPath).second;
});
// FIXME: only show module part of RawAccessPath
assert(!missing.empty() && "unknown missing dependency?");
if (missing.size() == 1) {
Ctx.Diags.diagnose(diagLoc, diag::serialization_missing_single_dependency,
missing.front().getPrettyPrintedPath());
missing.front().Core.getPrettyPrintedPath());
} else {
llvm::SmallString<64> missingNames;
missingNames += '\'';
interleave(missing,
[&](const ModuleFile::Dependency &next) {
missingNames += next.getPrettyPrintedPath();
missingNames += next.Core.getPrettyPrintedPath();
},
[&] { missingNames += "', '"; });
missingNames += '\'';
@@ -841,7 +848,7 @@ void swift::serialization::diagnoseSerializedASTLoadFailure(
// hard because we're discovering this /while/ resolving imports, which
// means the problematic modules haven't been recorded yet.
Ctx.Diags.diagnose(diagLoc, diag::serialization_circular_dependency,
circularDependencyIter->getPrettyPrintedPath(),
circularDependencyIter->Core.getPrettyPrintedPath(),
ModuleName);
break;
}
@@ -1106,7 +1113,7 @@ void SerializedASTFile::collectLinkLibraries(
}
bool SerializedASTFile::isSIB() const {
return File.IsSIB;
return File.isSIB();
}
bool SerializedASTFile::hadLoadError() const {