-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Front-end: add a new module loader that loads explicitly built Swift modules #32170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,6 +450,16 @@ bool CompilerInstance::setUpModuleLoaders() { | |
return true; | ||
} | ||
|
||
// If implicit modules are disabled, we need to install an explicit module | ||
// loader. | ||
if (Invocation.getFrontendOptions().DisableImplicitModules) { | ||
auto ESML = ExplicitSwiftModuleLoader::create( | ||
*Context, | ||
getDependencyTracker(), MLM, | ||
Invocation.getSearchPathOptions().ExplicitSwiftModules, | ||
IgnoreSourceInfoFile); | ||
Context->addModuleLoader(std::move(ESML)); | ||
} | ||
if (MLM != ModuleLoadingMode::OnlySerialized) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
auto const &Clang = clangImporter->getClangInstance(); | ||
std::string ModuleCachePath = getModuleCachePathFromClang(Clang); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -880,6 +880,10 @@ class ModuleInterfaceLoaderImpl { | |
|
||
return std::move(module.moduleBuffer); | ||
} | ||
// If implicit module is disabled, we are done. | ||
if (Opts.disableImplicitSwiftModule) { | ||
return std::make_error_code(std::errc::not_supported); | ||
} | ||
|
||
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer; | ||
|
||
|
@@ -1397,3 +1401,81 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN | |
// Run the action under the sub compiler instance. | ||
return action(info); | ||
} | ||
|
||
struct ExplicitSwiftModuleLoader::Implementation { | ||
// Information about explicitly specified Swift module files. | ||
struct ExplicitModuleInfo { | ||
// Path of the module file. | ||
StringRef path; | ||
// Buffer of the module content. | ||
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer; | ||
}; | ||
llvm::StringMap<ExplicitModuleInfo> ExplicitModuleMap; | ||
}; | ||
|
||
ExplicitSwiftModuleLoader::ExplicitSwiftModuleLoader( | ||
ASTContext &ctx, | ||
DependencyTracker *tracker, | ||
ModuleLoadingMode loadMode, | ||
bool IgnoreSwiftSourceInfoFile): | ||
SerializedModuleLoaderBase(ctx, tracker, loadMode, | ||
IgnoreSwiftSourceInfoFile), | ||
Impl(*new Implementation()) {} | ||
|
||
ExplicitSwiftModuleLoader::~ExplicitSwiftModuleLoader() { delete &Impl; } | ||
|
||
std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory( | ||
AccessPathElem ModuleID, | ||
const SerializedModuleBaseName &BaseName, | ||
SmallVectorImpl<char> *ModuleInterfacePath, | ||
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, | ||
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, | ||
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) { | ||
StringRef moduleName = ModuleID.Item.str(); | ||
auto it = Impl.ExplicitModuleMap.find(moduleName); | ||
// If no explicit module path is given matches the name, return with an | ||
// error code. | ||
if (it == Impl.ExplicitModuleMap.end()) { | ||
return std::make_error_code(std::errc::not_supported); | ||
} | ||
// We found an explicit module matches the given name, give the buffer | ||
// back to the caller side. | ||
*ModuleBuffer = std::move(it->getValue().moduleBuffer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need to deal with the |
||
return std::error_code(); | ||
} | ||
|
||
void ExplicitSwiftModuleLoader::collectVisibleTopLevelModuleNames( | ||
SmallVectorImpl<Identifier> &names) const { | ||
for (auto &entry: Impl.ExplicitModuleMap) { | ||
names.push_back(Ctx.getIdentifier(entry.getKey())); | ||
} | ||
} | ||
|
||
std::unique_ptr<ExplicitSwiftModuleLoader> | ||
ExplicitSwiftModuleLoader::create(ASTContext &ctx, | ||
DependencyTracker *tracker, ModuleLoadingMode loadMode, | ||
ArrayRef<std::string> ExplicitModulePaths, | ||
bool IgnoreSwiftSourceInfoFile) { | ||
auto result = std::unique_ptr<ExplicitSwiftModuleLoader>( | ||
new ExplicitSwiftModuleLoader(ctx, tracker, loadMode, | ||
IgnoreSwiftSourceInfoFile)); | ||
auto &Impl = result->Impl; | ||
for (auto path: ExplicitModulePaths) { | ||
std::string name; | ||
// Load the explicit module into a buffer and get its name. | ||
std::unique_ptr<llvm::MemoryBuffer> buffer = getModuleName(ctx, path, name); | ||
if (buffer) { | ||
// Register this module for future loading. | ||
auto &entry = Impl.ExplicitModuleMap[name]; | ||
entry.path = path; | ||
entry.moduleBuffer = std::move(buffer); | ||
} else { | ||
// We cannot read the module content, diagnose. | ||
ctx.Diags.diagnose(SourceLoc(), | ||
diag::error_opening_explicit_module_file, | ||
path); | ||
} | ||
} | ||
|
||
return result; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2190,6 +2190,30 @@ TypeDecl *ModuleFile::lookupLocalType(StringRef MangledName) { | |
return cast<TypeDecl>(getDecl(*iter)); | ||
} | ||
|
||
std::unique_ptr<llvm::MemoryBuffer> | ||
ModuleFile::getModuleName(ASTContext &Ctx, StringRef modulePath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to load the module here? Can we instead provide more information on the command line, e.g., a specific mapping from module name to the module file the contains it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could do that to avoid loading modules eagerly. How about we accept a JSON file from the command line to include module name, module path, swiftdoc path and swiftsourceinfo path? |
||
std::string &Name) { | ||
// Open the module file | ||
auto &fs = *Ctx.SourceMgr.getFileSystem(); | ||
auto moduleBuf = fs.getBufferForFile(modulePath); | ||
if (!moduleBuf) | ||
return nullptr; | ||
|
||
// Load the module file without validation. | ||
std::unique_ptr<ModuleFile> loadedModuleFile; | ||
ExtendedValidationInfo ExtInfo; | ||
bool isFramework = false; | ||
serialization::ValidationInfo loadInfo = | ||
ModuleFile::load(modulePath.str(), | ||
std::move(moduleBuf.get()), | ||
nullptr, | ||
nullptr, | ||
/*isFramework*/isFramework, loadedModuleFile, | ||
&ExtInfo); | ||
Name = loadedModuleFile->Name; | ||
return std::move(loadedModuleFile->ModuleInputBuffer); | ||
} | ||
|
||
OpaqueTypeDecl *ModuleFile::lookupOpaqueResultType(StringRef MangledName) { | ||
PrettyStackTraceModuleFile stackEntry(*this); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// swift-interface-format-version: 1.0 | ||
// swift-module-flags: -module-name SubE | ||
import Swift | ||
import E | ||
public func funcSubE() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about modeling
DisableImplicitModules
as aModuleLoadingMode
? It fits in with the scheme established there.