Skip to content

Commit b5b3718

Browse files
committed
AST: Some const goodness for ModuleDecl
1 parent d13353f commit b5b3718

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

include/swift/AST/Module.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
537537
/// \return True if the traversal ran to completion, false if it ended early
538538
/// due to the callback.
539539
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
540-
llvm::function_ref<bool(ImportedModule)> fn);
540+
llvm::function_ref<bool(ImportedModule)> fn) const;
541541

542542
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
543-
llvm::function_ref<void(ImportedModule)> fn) {
543+
llvm::function_ref<void(ImportedModule)> fn) const {
544544
return forAllVisibleModules(topLevelAccessPath,
545545
[=](const ImportedModule &import) -> bool {
546546
fn(import);
@@ -550,7 +550,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
550550

551551
template <typename Fn>
552552
bool forAllVisibleModules(AccessPathTy topLevelAccessPath,
553-
Fn &&fn) {
553+
Fn &&fn) const {
554554
using RetTy = typename std::result_of<Fn(ImportedModule)>::type;
555555
llvm::function_ref<RetTy(ImportedModule)> wrapped{std::forward<Fn>(fn)};
556556
return forAllVisibleModules(topLevelAccessPath, wrapped);
@@ -562,7 +562,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
562562

563563
/// Generate the list of libraries needed to link this module, based on its
564564
/// imports.
565-
void collectLinkLibraries(LinkLibraryCallback callback);
565+
void collectLinkLibraries(LinkLibraryCallback callback) const;
566566

567567
/// Returns true if the two access paths contain the same chain of
568568
/// identifiers.
@@ -826,18 +826,20 @@ class FileUnit : public DeclContext {
826826
/// \return True if the traversal ran to completion, false if it ended early
827827
/// due to the callback.
828828
bool
829-
forAllVisibleModules(llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn);
829+
forAllVisibleModules(
830+
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) const;
830831

831832
bool
832-
forAllVisibleModules(llvm::function_ref<void(ModuleDecl::ImportedModule)> fn) {
833+
forAllVisibleModules(
834+
llvm::function_ref<void(ModuleDecl::ImportedModule)> fn) const {
833835
return forAllVisibleModules([=](ModuleDecl::ImportedModule import) -> bool {
834836
fn(import);
835837
return true;
836838
});
837839
}
838840

839841
template <typename Fn>
840-
bool forAllVisibleModules(Fn &&fn) {
842+
bool forAllVisibleModules(Fn &&fn) const {
841843
using RetTy = typename std::result_of<Fn(ModuleDecl::ImportedModule)>::type;
842844
llvm::function_ref<RetTy(ModuleDecl::ImportedModule)> wrapped{
843845
std::forward<Fn>(fn)

lib/AST/Module.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,8 @@ bool ModuleDecl::registerEntryPointFile(FileUnit *file, SourceLoc diagLoc,
13751375

13761376
template<bool respectVisibility>
13771377
static bool
1378-
forAllImportedModules(ModuleDecl *topLevel, ModuleDecl::AccessPathTy thisPath,
1378+
forAllImportedModules(const ModuleDecl *topLevel,
1379+
ModuleDecl::AccessPathTy thisPath,
13791380
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) {
13801381
using ImportedModule = ModuleDecl::ImportedModule;
13811382
using AccessPathTy = ModuleDecl::AccessPathTy;
@@ -1396,7 +1397,7 @@ forAllImportedModules(ModuleDecl *topLevel, ModuleDecl::AccessPathTy thisPath,
13961397
AccessPathTy overridingPath;
13971398
if (respectVisibility)
13981399
overridingPath = thisPath;
1399-
stack.push_back(ImportedModule(overridingPath, topLevel));
1400+
stack.emplace_back(overridingPath, const_cast<ModuleDecl *>(topLevel));
14001401

14011402
while (!stack.empty()) {
14021403
auto next = stack.pop_back_val();
@@ -1430,12 +1431,12 @@ forAllImportedModules(ModuleDecl *topLevel, ModuleDecl::AccessPathTy thisPath,
14301431

14311432
bool
14321433
ModuleDecl::forAllVisibleModules(AccessPathTy thisPath,
1433-
llvm::function_ref<bool(ImportedModule)> fn) {
1434+
llvm::function_ref<bool(ImportedModule)> fn) const {
14341435
return forAllImportedModules<true>(this, thisPath, fn);
14351436
}
14361437

14371438
bool FileUnit::forAllVisibleModules(
1438-
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) {
1439+
llvm::function_ref<bool(ModuleDecl::ImportedModule)> fn) const {
14391440
if (!getParentModule()->forAllVisibleModules(ModuleDecl::AccessPathTy(), fn))
14401441
return false;
14411442

@@ -1454,7 +1455,7 @@ bool FileUnit::forAllVisibleModules(
14541455
return true;
14551456
}
14561457

1457-
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) {
1458+
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) const {
14581459
// FIXME: The proper way to do this depends on the decls used.
14591460
FORWARD(collectLinkLibraries, (callback));
14601461
}

0 commit comments

Comments
 (0)