Skip to content

Commit 98ad697

Browse files
Make getters const in Module class (#8579)
Split off from my [WIP for improving effects analysis for indirect calls](https://github.com/WebAssembly/binaryen/compare/indirect-effects-3?expand=1). These methods don't mutate the Module so they can be const. Also move getModuleElement into the anonymous namespace to prevent the name from leaking. Since these getters are now const, I also change some usages of Module&/Module* to const e.g. EffectsAnalyzer, since these usages also only need read-only access to the Module.
1 parent a6f85e5 commit 98ad697

File tree

6 files changed

+59
-54
lines changed

6 files changed

+59
-54
lines changed

src/ir/effects.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace wasm {
3232

3333
class EffectAnalyzer {
3434
public:
35-
EffectAnalyzer(const PassOptions& passOptions, Module& module)
35+
EffectAnalyzer(const PassOptions& passOptions, const Module& module)
3636
: ignoreImplicitTraps(passOptions.ignoreImplicitTraps),
3737
trapsNeverHappen(passOptions.trapsNeverHappen), branchesOut(false),
3838
calls(false), readsMemory(false), writesMemory(false),
@@ -46,7 +46,7 @@ class EffectAnalyzer {
4646
features(module.features) {}
4747

4848
EffectAnalyzer(const PassOptions& passOptions,
49-
Module& module,
49+
const Module& module,
5050
Expression* ast)
5151
: EffectAnalyzer(passOptions, module) {
5252
walk(ast);
@@ -136,7 +136,7 @@ class EffectAnalyzer {
136136
// more here.)
137137
bool hasReturnCallThrow : 1;
138138

139-
Module& module;
139+
const Module& module;
140140
FeatureSet features;
141141

142142
std::set<Index> localsRead;

src/ir/intrinsics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
namespace wasm {
3434

3535
class Intrinsics {
36-
Module& module;
36+
const Module& module;
3737

3838
public:
39-
Intrinsics(Module& module) : module(module) {}
39+
Intrinsics(const Module& module) : module(module) {}
4040

4141
// Check if an instruction is the Binaryen call.without.effects intrinsic.
4242
//

src/ir/js-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline bool hasPossibleJSPrototypeField(HeapType type) {
4545

4646
// Calls flowIn and flowOut on all types that may flow in from or out to JS.
4747
template<typename In, typename Out>
48-
void iterJSInterface(Module& wasm, In flowIn, Out flowOut) {
48+
void iterJSInterface(const Module& wasm, In flowIn, Out flowOut) {
4949
// @binaryen.js.called functions are called from JS. Their parameters flow
5050
// in from JS and their results flow back out.
5151
for (auto f : Intrinsics(wasm).getJSCalledFunctions()) {

src/passes/Unsubtyping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ struct Unsubtyping : Pass, Noter<Unsubtyping> {
645645
}
646646
}
647647

648-
void analyzeJSInterface(Module& wasm) {
648+
void analyzeJSInterface(const Module& wasm) {
649649
if (!wasm.features.hasCustomDescriptors()) {
650650
return;
651651
}

src/wasm.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,30 +2700,30 @@ class Module {
27002700
public:
27012701
Module() = default;
27022702

2703-
Export* getExport(Name name);
2704-
Function* getFunction(Name name);
2705-
Table* getTable(Name name);
2706-
ElementSegment* getElementSegment(Name name);
2707-
Memory* getMemory(Name name);
2708-
DataSegment* getDataSegment(Name name);
2709-
Global* getGlobal(Name name);
2710-
Tag* getTag(Name name);
2711-
2712-
Export* getExportOrNull(Name name);
2713-
Table* getTableOrNull(Name name);
2714-
Memory* getMemoryOrNull(Name name);
2715-
ElementSegment* getElementSegmentOrNull(Name name);
2716-
DataSegment* getDataSegmentOrNull(Name name);
2717-
Function* getFunctionOrNull(Name name);
2718-
Global* getGlobalOrNull(Name name);
2719-
Tag* getTagOrNull(Name name);
2703+
Export* getExport(Name name) const;
2704+
Function* getFunction(Name name) const;
2705+
Table* getTable(Name name) const;
2706+
ElementSegment* getElementSegment(Name name) const;
2707+
Memory* getMemory(Name name) const;
2708+
DataSegment* getDataSegment(Name name) const;
2709+
Global* getGlobal(Name name) const;
2710+
Tag* getTag(Name name) const;
2711+
2712+
Export* getExportOrNull(Name name) const;
2713+
Table* getTableOrNull(Name name) const;
2714+
Memory* getMemoryOrNull(Name name) const;
2715+
ElementSegment* getElementSegmentOrNull(Name name) const;
2716+
DataSegment* getDataSegmentOrNull(Name name) const;
2717+
Function* getFunctionOrNull(Name name) const;
2718+
Global* getGlobalOrNull(Name name) const;
2719+
Tag* getTagOrNull(Name name) const;
27202720

27212721
// get* methods that are generic over the kind, that is, items are identified
27222722
// by their kind and their name. Otherwise, they are similar to the above
27232723
// get* methods. These return items that can be imports.
27242724
// TODO: Add methods for things that cannot be imports (segments).
2725-
Importable* getImport(ModuleItemKind kind, Name name);
2726-
Importable* getImportOrNull(ModuleItemKind kind, Name name);
2725+
Importable* getImport(ModuleItemKind kind, Name name) const;
2726+
Importable* getImportOrNull(ModuleItemKind kind, Name name) const;
27272727

27282728
Export* addExport(Export* curr);
27292729
Function* addFunction(Function* curr);

src/wasm/wasm.cpp

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222

2323
namespace wasm {
2424

25+
namespace {
26+
27+
template<typename Value>
28+
const Value& getModuleElement(const std::unordered_map<Name, Value>& m,
29+
Name name,
30+
std::string_view funcName) {
31+
auto iter = m.find(name);
32+
if (iter == m.end()) {
33+
Fatal() << "Module::" << funcName << ": " << name << " does not exist";
34+
}
35+
return iter->second;
36+
}
37+
38+
} // namespace
39+
2540
// shared constants
2641

2742
Name RETURN_FLOW("*return:)*");
@@ -1706,45 +1721,35 @@ void Function::clearDebugInfo() {
17061721
epilogLocation.reset();
17071722
}
17081723

1709-
template<typename Map>
1710-
typename Map::mapped_type&
1711-
getModuleElement(Map& m, Name name, std::string_view funcName) {
1712-
auto iter = m.find(name);
1713-
if (iter == m.end()) {
1714-
Fatal() << "Module::" << funcName << ": " << name << " does not exist";
1715-
}
1716-
return iter->second;
1717-
}
1718-
1719-
Export* Module::getExport(Name name) {
1724+
Export* Module::getExport(Name name) const {
17201725
return getModuleElement(exportsMap, name, "getExport");
17211726
}
17221727

1723-
Function* Module::getFunction(Name name) {
1728+
Function* Module::getFunction(Name name) const {
17241729
return getModuleElement(functionsMap, name, "getFunction");
17251730
}
17261731

1727-
Table* Module::getTable(Name name) {
1732+
Table* Module::getTable(Name name) const {
17281733
return getModuleElement(tablesMap, name, "getTable");
17291734
}
17301735

1731-
ElementSegment* Module::getElementSegment(Name name) {
1736+
ElementSegment* Module::getElementSegment(Name name) const {
17321737
return getModuleElement(elementSegmentsMap, name, "getElementSegment");
17331738
}
17341739

1735-
Memory* Module::getMemory(Name name) {
1740+
Memory* Module::getMemory(Name name) const {
17361741
return getModuleElement(memoriesMap, name, "getMemory");
17371742
}
17381743

1739-
DataSegment* Module::getDataSegment(Name name) {
1744+
DataSegment* Module::getDataSegment(Name name) const {
17401745
return getModuleElement(dataSegmentsMap, name, "getDataSegment");
17411746
}
17421747

1743-
Global* Module::getGlobal(Name name) {
1748+
Global* Module::getGlobal(Name name) const {
17441749
return getModuleElement(globalsMap, name, "getGlobal");
17451750
}
17461751

1747-
Tag* Module::getTag(Name name) {
1752+
Tag* Module::getTag(Name name) const {
17481753
return getModuleElement(tagsMap, name, "getTag");
17491754
}
17501755

@@ -1757,39 +1762,39 @@ typename Map::mapped_type getModuleElementOrNull(Map& m, Name name) {
17571762
return iter->second;
17581763
}
17591764

1760-
Export* Module::getExportOrNull(Name name) {
1765+
Export* Module::getExportOrNull(Name name) const {
17611766
return getModuleElementOrNull(exportsMap, name);
17621767
}
17631768

1764-
Function* Module::getFunctionOrNull(Name name) {
1769+
Function* Module::getFunctionOrNull(Name name) const {
17651770
return getModuleElementOrNull(functionsMap, name);
17661771
}
17671772

1768-
Table* Module::getTableOrNull(Name name) {
1773+
Table* Module::getTableOrNull(Name name) const {
17691774
return getModuleElementOrNull(tablesMap, name);
17701775
}
17711776

1772-
ElementSegment* Module::getElementSegmentOrNull(Name name) {
1777+
ElementSegment* Module::getElementSegmentOrNull(Name name) const {
17731778
return getModuleElementOrNull(elementSegmentsMap, name);
17741779
}
17751780

1776-
Memory* Module::getMemoryOrNull(Name name) {
1781+
Memory* Module::getMemoryOrNull(Name name) const {
17771782
return getModuleElementOrNull(memoriesMap, name);
17781783
}
17791784

1780-
DataSegment* Module::getDataSegmentOrNull(Name name) {
1785+
DataSegment* Module::getDataSegmentOrNull(Name name) const {
17811786
return getModuleElementOrNull(dataSegmentsMap, name);
17821787
}
17831788

1784-
Global* Module::getGlobalOrNull(Name name) {
1789+
Global* Module::getGlobalOrNull(Name name) const {
17851790
return getModuleElementOrNull(globalsMap, name);
17861791
}
17871792

1788-
Tag* Module::getTagOrNull(Name name) {
1793+
Tag* Module::getTagOrNull(Name name) const {
17891794
return getModuleElementOrNull(tagsMap, name);
17901795
}
17911796

1792-
Importable* Module::getImport(ModuleItemKind kind, Name name) {
1797+
Importable* Module::getImport(ModuleItemKind kind, Name name) const {
17931798
switch (kind) {
17941799
case ModuleItemKind::Function:
17951800
return getFunction(name);
@@ -1810,7 +1815,7 @@ Importable* Module::getImport(ModuleItemKind kind, Name name) {
18101815
WASM_UNREACHABLE("unexpected kind");
18111816
}
18121817

1813-
Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) {
1818+
Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) const {
18141819
auto doReturn = [](Importable* importable) {
18151820
return importable ? importable->imported() ? importable : nullptr : nullptr;
18161821
};

0 commit comments

Comments
 (0)