Skip to content

Commit cf773c3

Browse files
committed
[Explicit Module Loader] Refactor ESWL to use findModule instead of findModuleFilesInDirectory
And add isFramework to the Explicit Module Map and its parser.
1 parent 40c1687 commit cf773c3

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
#include "swift/Frontend/ModuleInterfaceSupport.h"
113113
#include "swift/Serialization/SerializedModuleLoader.h"
114114
#include "llvm/Support/StringSaver.h"
115+
#include <sstream>
115116

116117
namespace clang {
117118
class CompilerInstance;
@@ -133,14 +134,22 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
133134
explicit ExplicitSwiftModuleLoader(ASTContext &ctx, DependencyTracker *tracker,
134135
ModuleLoadingMode loadMode,
135136
bool IgnoreSwiftSourceInfoFile);
137+
138+
bool findModule(AccessPathElem moduleID,
139+
SmallVectorImpl<char> *moduleInterfacePath,
140+
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
141+
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
142+
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
143+
bool &isFramework, bool &isSystemModule) override;
144+
136145
std::error_code findModuleFilesInDirectory(
137-
AccessPathElem ModuleID,
138-
const SerializedModuleBaseName &BaseName,
139-
SmallVectorImpl<char> *ModuleInterfacePath,
140-
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
141-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
142-
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
143-
bool IsFramework) override;
146+
AccessPathElem ModuleID,
147+
const SerializedModuleBaseName &BaseName,
148+
SmallVectorImpl<char> *ModuleInterfacePath,
149+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
150+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
151+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
152+
bool IsFramework) override;
144153

145154
bool canImportModule(Located<Identifier> mID) override;
146155

@@ -173,6 +182,8 @@ struct ExplicitModuleInfo {
173182
std::string moduleSourceInfoPath;
174183
// Opened buffer for the .swiftmodule file.
175184
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
185+
// A flag that indicates whether this module is a framework
186+
bool isFramework;
176187
};
177188

178189
/// Parser of explicit module maps passed into the compiler.
@@ -182,12 +193,14 @@ struct ExplicitModuleInfo {
182193
// "modulePath": "A.swiftmodule",
183194
// "docPath": "A.swiftdoc",
184195
// "sourceInfoPath": "A.swiftsourceinfo"
196+
// "isFramework": false
185197
// },
186198
// {
187199
// "moduleName": "B",
188200
// "modulePath": "B.swiftmodule",
189201
// "docPath": "B.swiftdoc",
190202
// "sourceInfoPath": "B.swiftsourceinfo"
203+
// "isFramework": false
191204
// }
192205
// ]
193206
class ExplicitModuleMapParser {
@@ -249,6 +262,9 @@ class ExplicitModuleMapParser {
249262
result.moduleDocPath = val.str();
250263
} else if (key == "sourceInfoPath") {
251264
result.moduleSourceInfoPath = val.str();
265+
} else if (key == "isFramework") {
266+
std::istringstream is(val.str());
267+
is >> std::boolalpha >> result.isFramework;
252268
} else {
253269
// Being forgiving for future fields.
254270
continue;

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ class SerializedModuleLoaderBase : public ModuleLoader {
7373
StringRef extension) const;
7474

7575
using AccessPathElem = Located<Identifier>;
76-
bool findModule(AccessPathElem moduleID,
77-
SmallVectorImpl<char> *moduleInterfacePath,
78-
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
79-
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
80-
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
81-
bool &isFramework, bool &isSystemModule);
76+
virtual bool findModule(AccessPathElem moduleID,
77+
SmallVectorImpl<char> *moduleInterfacePath,
78+
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
79+
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
80+
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
81+
bool &isFramework, bool &isSystemModule);
8282

8383
/// Attempts to search the provided directory for a loadable serialized
8484
/// .swiftmodule with the provided `ModuleFilename`. Subclasses must

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,37 +1522,38 @@ ExplicitSwiftModuleLoader::ExplicitSwiftModuleLoader(
15221522

15231523
ExplicitSwiftModuleLoader::~ExplicitSwiftModuleLoader() { delete &Impl; }
15241524

1525-
std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
1526-
AccessPathElem ModuleID,
1527-
const SerializedModuleBaseName &BaseName,
1528-
SmallVectorImpl<char> *ModuleInterfacePath,
1529-
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
1530-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
1531-
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
1532-
bool IsFramework) {
1525+
bool ExplicitSwiftModuleLoader::findModule(AccessPathElem ModuleID,
1526+
SmallVectorImpl<char> *ModuleInterfacePath,
1527+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
1528+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
1529+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
1530+
bool &IsFramework, bool &IsSystemModule) {
15331531
StringRef moduleName = ModuleID.Item.str();
15341532
auto it = Impl.ExplicitModuleMap.find(moduleName);
15351533
// If no explicit module path is given matches the name, return with an
15361534
// error code.
15371535
if (it == Impl.ExplicitModuleMap.end()) {
1538-
return std::make_error_code(std::errc::not_supported);
1536+
return false;
15391537
}
15401538
auto &moduleInfo = it->getValue();
15411539
if (moduleInfo.moduleBuffer) {
15421540
// We found an explicit module matches the given name, give the buffer
15431541
// back to the caller side.
15441542
*ModuleBuffer = std::move(moduleInfo.moduleBuffer);
1545-
return std::error_code();
1543+
return true;
15461544
}
15471545

1546+
// Set IsFramework bit according to the moduleInfo
1547+
IsFramework = moduleInfo.isFramework;
1548+
15481549
auto &fs = *Ctx.SourceMgr.getFileSystem();
15491550
// Open .swiftmodule file
15501551
auto moduleBuf = fs.getBufferForFile(moduleInfo.modulePath);
15511552
if (!moduleBuf) {
15521553
// We cannot read the module content, diagnose.
15531554
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
15541555
moduleInfo.modulePath);
1555-
return moduleBuf.getError();
1556+
return false;
15561557
}
15571558

15581559
assert(moduleBuf);
@@ -1568,13 +1569,13 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
15681569
// We cannot read the module content, diagnose.
15691570
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
15701571
moduleInfo.modulePath);
1571-
return moduleBuf.getError();
1572+
return false;
15721573
}
15731574
} else {
15741575
// We cannot read the module content, diagnose.
15751576
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
15761577
moduleInfo.modulePath);
1577-
return forwardingModule.getError();
1578+
return false;
15781579
}
15791580
}
15801581
assert(moduleBuf);
@@ -1593,7 +1594,19 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
15931594
if (moduleSourceInfoBuf)
15941595
*ModuleSourceInfoBuffer = std::move(moduleSourceInfoBuf.get());
15951596
}
1596-
return std::error_code();
1597+
return true;
1598+
}
1599+
1600+
std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
1601+
AccessPathElem ModuleID,
1602+
const SerializedModuleBaseName &BaseName,
1603+
SmallVectorImpl<char> *ModuleInterfacePath,
1604+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
1605+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
1606+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
1607+
bool IsFramework) {
1608+
llvm_unreachable("Not supported in the Explicit Swift Module Loader.");
1609+
return std::make_error_code(std::errc::not_supported);
15971610
}
15981611

15991612
bool ExplicitSwiftModuleLoader::canImportModule(

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
682682
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
683683
bool isFramework) {
684684
assert(moduleInputBuffer);
685-
686685
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
687686
StringRef moduleDocBufferID;
688687
if (moduleDocInputBuffer)
@@ -993,7 +992,6 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
993992

994993
StringRef moduleInterfacePathStr =
995994
Ctx.AllocateCopy(moduleInterfacePath.str());
996-
997995
auto *file =
998996
loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
999997
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),

0 commit comments

Comments
 (0)