Skip to content

Commit d3064d6

Browse files
committed
[AST] Add callback to the module loading methods
This patch adds an optional callback function argument to each AST module loading method. This can be used for instance by other clients, such as lldb, to perform actions when a module gets loaded, like showing progress to the end-user. rdar://94165195 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 28a2c60 commit d3064d6

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

include/swift/AST/ASTContext.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,28 +1107,44 @@ class ASTContext final {
11071107
/// If a module by this name has already been loaded, the existing module will
11081108
/// be returned.
11091109
///
1110+
/// \param ModulePath The module's \c ImportPath which describes
1111+
/// the name of the module being loaded, possibly including submodules.
1112+
///
1113+
/// \param callback An optional generic function invoked prior to loading the
1114+
/// module.
1115+
///
11101116
/// \returns The requested module, or NULL if the module cannot be found.
1111-
ModuleDecl *getModule(ImportPath::Module ModulePath);
1117+
ModuleDecl *getModule(ImportPath::Module ModulePath,
1118+
std::function<bool(llvm::StringRef)> callback = {});
11121119

11131120
/// Attempts to load the matching overlay module for the given clang
11141121
/// module into this ASTContext.
11151122
///
11161123
/// \returns The Swift overlay module corresponding to the given Clang module,
11171124
/// or NULL if the overlay module cannot be found.
1118-
ModuleDecl *getOverlayModule(const FileUnit *ClangModule);
1125+
ModuleDecl *
1126+
getOverlayModule(const FileUnit *ClangModule,
1127+
std::function<bool(llvm::StringRef)> callback = {});
11191128

1120-
ModuleDecl *getModuleByName(StringRef ModuleName);
1129+
ModuleDecl *
1130+
getModuleByName(StringRef ModuleName,
1131+
std::function<bool(llvm::StringRef)> callback = {});
11211132

1122-
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);
1133+
ModuleDecl *
1134+
getModuleByIdentifier(Identifier ModuleID,
1135+
std::function<bool(llvm::StringRef)> callback = {});
11231136

11241137
/// Returns the standard library module, or null if the library isn't present.
11251138
///
11261139
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module
11271140
/// if it hasn't been set yet.
1128-
ModuleDecl *getStdlibModule(bool loadIfAbsent = false);
1141+
ModuleDecl *
1142+
getStdlibModule(bool loadIfAbsent = false,
1143+
std::function<bool(llvm::StringRef)> callback = {});
11291144

1130-
ModuleDecl *getStdlibModule() const {
1131-
return const_cast<ASTContext *>(this)->getStdlibModule(false);
1145+
ModuleDecl *
1146+
getStdlibModule(std::function<bool(llvm::StringRef)> callback = {}) const {
1147+
return const_cast<ASTContext *>(this)->getStdlibModule(false, callback);
11321148
}
11331149

11341150
/// Insert an externally-sourced module into the set of known loaded modules

lib/AST/ASTContext.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
#include "swift/AST/PropertyWrappers.h"
4343
#include "swift/AST/ProtocolConformance.h"
4444
#include "swift/AST/RawComment.h"
45-
#include "swift/AST/SearchPathOptions.h"
4645
#include "swift/AST/SILLayout.h"
46+
#include "swift/AST/SearchPathOptions.h"
4747
#include "swift/AST/SemanticAttrs.h"
4848
#include "swift/AST/SourceFile.h"
4949
#include "swift/AST/SubstitutionMap.h"
@@ -65,6 +65,7 @@
6565
#include "llvm/IR/LLVMContext.h"
6666
#include "llvm/Support/Allocator.h"
6767
#include "llvm/Support/Compiler.h"
68+
#include "llvm/Support/FormatVariadic.h"
6869
#include <algorithm>
6970
#include <memory>
7071

@@ -2203,14 +2204,17 @@ bool ASTContext::canImportModule(ImportPath::Module ModuleName,
22032204
}
22042205

22052206
ModuleDecl *
2206-
ASTContext::getModule(ImportPath::Module ModulePath) {
2207+
ASTContext::getModule(ImportPath::Module ModulePath,
2208+
std::function<bool(llvm::StringRef)> callback) {
22072209
assert(!ModulePath.empty());
22082210

22092211
if (auto *M = getLoadedModule(ModulePath))
22102212
return M;
22112213

22122214
auto moduleID = ModulePath[0];
22132215
for (auto &importer : getImpl().ModuleLoaders) {
2216+
if (callback)
2217+
callback(llvm::formatv("Loading module {0}", moduleID.Item.str()).str());
22142218
if (ModuleDecl *M = importer->loadModule(moduleID.Loc, ModulePath)) {
22152219
if (LangOpts.EnableModuleLoadingRemarks) {
22162220
Diags.diagnose(ModulePath.getSourceRange().Start,
@@ -2224,7 +2228,9 @@ ASTContext::getModule(ImportPath::Module ModulePath) {
22242228
return nullptr;
22252229
}
22262230

2227-
ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
2231+
ModuleDecl *
2232+
ASTContext::getOverlayModule(const FileUnit *FU,
2233+
std::function<bool(llvm::StringRef)> callback) {
22282234
assert(FU && FU->getKind() == FileUnitKind::ClangModule &&
22292235
"Overlays can only be retrieved for clang modules!");
22302236
ImportPath::Module::Builder builder(FU->getParentModule()->getName());
@@ -2237,31 +2243,42 @@ ModuleDecl *ASTContext::getOverlayModule(const FileUnit *FU) {
22372243
for (auto &importer : getImpl().ModuleLoaders) {
22382244
if (importer.get() == getClangModuleLoader())
22392245
continue;
2240-
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath)) {
2241-
return M;
2246+
if (callback) {
2247+
SmallString<256> path;
2248+
ModPath.getString(path);
2249+
if (!path.empty())
2250+
callback(llvm::formatv("Loading overlay module {0}", path).str());
22422251
}
2252+
if (ModuleDecl *M = importer->loadModule(SourceLoc(), ModPath))
2253+
return M;
22432254
}
22442255

22452256
return nullptr;
22462257
}
22472258

2248-
ModuleDecl *ASTContext::getModuleByName(StringRef ModuleName) {
2259+
ModuleDecl *
2260+
ASTContext::getModuleByName(StringRef ModuleName,
2261+
std::function<bool(llvm::StringRef)> callback) {
22492262
ImportPath::Module::Builder builder(*this, ModuleName, /*separator=*/'.');
2250-
return getModule(builder.get());
2263+
return getModule(builder.get(), callback);
22512264
}
22522265

2253-
ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
2266+
ModuleDecl *ASTContext::getModuleByIdentifier(
2267+
Identifier ModuleID, std::function<bool(llvm::StringRef)> callback) {
22542268
ImportPath::Module::Builder builder(ModuleID);
2255-
return getModule(builder.get());
2269+
return getModule(builder.get(), callback);
22562270
}
22572271

2258-
ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
2272+
ModuleDecl *
2273+
ASTContext::getStdlibModule(bool loadIfAbsent,
2274+
std::function<bool(llvm::StringRef)> callback) {
22592275
if (TheStdlibModule)
22602276
return TheStdlibModule;
22612277

22622278
if (loadIfAbsent) {
22632279
auto mutableThis = const_cast<ASTContext*>(this);
2264-
TheStdlibModule = mutableThis->getModuleByIdentifier(StdlibModuleName);
2280+
TheStdlibModule =
2281+
mutableThis->getModuleByIdentifier(StdlibModuleName, callback);
22652282
} else {
22662283
TheStdlibModule = getLoadedModule(StdlibModuleName);
22672284
}

0 commit comments

Comments
 (0)