@@ -880,6 +880,10 @@ class ModuleInterfaceLoaderImpl {
880880
881881 return std::move (module .moduleBuffer );
882882 }
883+ // If implicit module is disabled, we are done.
884+ if (Opts.disableImplicitSwiftModule ) {
885+ return std::make_error_code (std::errc::not_supported);
886+ }
883887
884888 std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
885889
@@ -1397,3 +1401,81 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
13971401 // Run the action under the sub compiler instance.
13981402 return action (info);
13991403}
1404+
1405+ struct ExplicitSwiftModuleLoader ::Implementation {
1406+ // Information about explicitly specified Swift module files.
1407+ struct ExplicitModuleInfo {
1408+ // Path of the module file.
1409+ StringRef path;
1410+ // Buffer of the module content.
1411+ std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
1412+ };
1413+ llvm::StringMap<ExplicitModuleInfo> ExplicitModuleMap;
1414+ };
1415+
1416+ ExplicitSwiftModuleLoader::ExplicitSwiftModuleLoader (
1417+ ASTContext &ctx,
1418+ DependencyTracker *tracker,
1419+ ModuleLoadingMode loadMode,
1420+ bool IgnoreSwiftSourceInfoFile):
1421+ SerializedModuleLoaderBase(ctx, tracker, loadMode,
1422+ IgnoreSwiftSourceInfoFile),
1423+ Impl(*new Implementation()) {}
1424+
1425+ ExplicitSwiftModuleLoader::~ExplicitSwiftModuleLoader () { delete &Impl; }
1426+
1427+ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory (
1428+ AccessPathElem ModuleID,
1429+ const SerializedModuleBaseName &BaseName,
1430+ SmallVectorImpl<char > *ModuleInterfacePath,
1431+ std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
1432+ std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
1433+ std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) {
1434+ StringRef moduleName = ModuleID.Item .str ();
1435+ auto it = Impl.ExplicitModuleMap .find (moduleName);
1436+ // If no explicit module path is given matches the name, return with an
1437+ // error code.
1438+ if (it == Impl.ExplicitModuleMap .end ()) {
1439+ return std::make_error_code (std::errc::not_supported);
1440+ }
1441+ // We found an explicit module matches the given name, give the buffer
1442+ // back to the caller side.
1443+ *ModuleBuffer = std::move (it->getValue ().moduleBuffer );
1444+ return std::error_code ();
1445+ }
1446+
1447+ void ExplicitSwiftModuleLoader::collectVisibleTopLevelModuleNames (
1448+ SmallVectorImpl<Identifier> &names) const {
1449+ for (auto &entry: Impl.ExplicitModuleMap ) {
1450+ names.push_back (Ctx.getIdentifier (entry.getKey ()));
1451+ }
1452+ }
1453+
1454+ std::unique_ptr<ExplicitSwiftModuleLoader>
1455+ ExplicitSwiftModuleLoader::create (ASTContext &ctx,
1456+ DependencyTracker *tracker, ModuleLoadingMode loadMode,
1457+ ArrayRef<std::string> ExplicitModulePaths,
1458+ bool IgnoreSwiftSourceInfoFile) {
1459+ auto result = std::unique_ptr<ExplicitSwiftModuleLoader>(
1460+ new ExplicitSwiftModuleLoader (ctx, tracker, loadMode,
1461+ IgnoreSwiftSourceInfoFile));
1462+ auto &Impl = result->Impl ;
1463+ for (auto path: ExplicitModulePaths) {
1464+ std::string name;
1465+ // Load the explicit module into a buffer and get its name.
1466+ std::unique_ptr<llvm::MemoryBuffer> buffer = getModuleName (ctx, path, name);
1467+ if (buffer) {
1468+ // Register this module for future loading.
1469+ auto &entry = Impl.ExplicitModuleMap [name];
1470+ entry.path = path;
1471+ entry.moduleBuffer = std::move (buffer);
1472+ } else {
1473+ // We cannot read the module content, diagnose.
1474+ ctx.Diags .diagnose (SourceLoc (),
1475+ diag::error_opening_explicit_module_file,
1476+ path);
1477+ }
1478+ }
1479+
1480+ return result;
1481+ }
0 commit comments