diff --git a/src/ir/effects.h b/src/ir/effects.h index 1e05ab0fb7c..e8ab4c8ef69 100644 --- a/src/ir/effects.h +++ b/src/ir/effects.h @@ -32,7 +32,7 @@ namespace wasm { class EffectAnalyzer { public: - EffectAnalyzer(const PassOptions& passOptions, Module& module) + EffectAnalyzer(const PassOptions& passOptions, const Module& module) : ignoreImplicitTraps(passOptions.ignoreImplicitTraps), trapsNeverHappen(passOptions.trapsNeverHappen), branchesOut(false), calls(false), readsMemory(false), writesMemory(false), @@ -46,7 +46,7 @@ class EffectAnalyzer { features(module.features) {} EffectAnalyzer(const PassOptions& passOptions, - Module& module, + const Module& module, Expression* ast) : EffectAnalyzer(passOptions, module) { walk(ast); @@ -136,7 +136,7 @@ class EffectAnalyzer { // more here.) bool hasReturnCallThrow : 1; - Module& module; + const Module& module; FeatureSet features; std::set localsRead; diff --git a/src/ir/intrinsics.h b/src/ir/intrinsics.h index a9b32f31f1b..2f6809c826b 100644 --- a/src/ir/intrinsics.h +++ b/src/ir/intrinsics.h @@ -33,10 +33,10 @@ namespace wasm { class Intrinsics { - Module& module; + const Module& module; public: - Intrinsics(Module& module) : module(module) {} + Intrinsics(const Module& module) : module(module) {} // Check if an instruction is the Binaryen call.without.effects intrinsic. // diff --git a/src/ir/js-utils.h b/src/ir/js-utils.h index 7591b4fe7e0..105dea499cf 100644 --- a/src/ir/js-utils.h +++ b/src/ir/js-utils.h @@ -45,7 +45,7 @@ inline bool hasPossibleJSPrototypeField(HeapType type) { // Calls flowIn and flowOut on all types that may flow in from or out to JS. template -void iterJSInterface(Module& wasm, In flowIn, Out flowOut) { +void iterJSInterface(const Module& wasm, In flowIn, Out flowOut) { // @binaryen.js.called functions are called from JS. Their parameters flow // in from JS and their results flow back out. for (auto f : Intrinsics(wasm).getJSCalledFunctions()) { diff --git a/src/passes/Unsubtyping.cpp b/src/passes/Unsubtyping.cpp index 5f01610adeb..f3165b8147c 100644 --- a/src/passes/Unsubtyping.cpp +++ b/src/passes/Unsubtyping.cpp @@ -645,7 +645,7 @@ struct Unsubtyping : Pass, Noter { } } - void analyzeJSInterface(Module& wasm) { + void analyzeJSInterface(const Module& wasm) { if (!wasm.features.hasCustomDescriptors()) { return; } diff --git a/src/wasm.h b/src/wasm.h index 941d759ce6b..c64978014f9 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -2699,30 +2699,30 @@ class Module { public: Module() = default; - Export* getExport(Name name); - Function* getFunction(Name name); - Table* getTable(Name name); - ElementSegment* getElementSegment(Name name); - Memory* getMemory(Name name); - DataSegment* getDataSegment(Name name); - Global* getGlobal(Name name); - Tag* getTag(Name name); - - Export* getExportOrNull(Name name); - Table* getTableOrNull(Name name); - Memory* getMemoryOrNull(Name name); - ElementSegment* getElementSegmentOrNull(Name name); - DataSegment* getDataSegmentOrNull(Name name); - Function* getFunctionOrNull(Name name); - Global* getGlobalOrNull(Name name); - Tag* getTagOrNull(Name name); + Export* getExport(Name name) const; + Function* getFunction(Name name) const; + Table* getTable(Name name) const; + ElementSegment* getElementSegment(Name name) const; + Memory* getMemory(Name name) const; + DataSegment* getDataSegment(Name name) const; + Global* getGlobal(Name name) const; + Tag* getTag(Name name) const; + + Export* getExportOrNull(Name name) const; + Table* getTableOrNull(Name name) const; + Memory* getMemoryOrNull(Name name) const; + ElementSegment* getElementSegmentOrNull(Name name) const; + DataSegment* getDataSegmentOrNull(Name name) const; + Function* getFunctionOrNull(Name name) const; + Global* getGlobalOrNull(Name name) const; + Tag* getTagOrNull(Name name) const; // get* methods that are generic over the kind, that is, items are identified // by their kind and their name. Otherwise, they are similar to the above // get* methods. These return items that can be imports. // TODO: Add methods for things that cannot be imports (segments). - Importable* getImport(ModuleItemKind kind, Name name); - Importable* getImportOrNull(ModuleItemKind kind, Name name); + Importable* getImport(ModuleItemKind kind, Name name) const; + Importable* getImportOrNull(ModuleItemKind kind, Name name) const; Export* addExport(Export* curr); Function* addFunction(Function* curr); diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 536f33ae59f..9be9b7b5cfa 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -22,6 +22,21 @@ namespace wasm { +namespace { + +template +const Value& getModuleElement(const std::unordered_map& m, + Name name, + std::string_view funcName) { + auto iter = m.find(name); + if (iter == m.end()) { + Fatal() << "Module::" << funcName << ": " << name << " does not exist"; + } + return iter->second; +} + +} // namespace + // shared constants Name RETURN_FLOW("*return:)*"); @@ -1705,45 +1720,35 @@ void Function::clearDebugInfo() { epilogLocation.reset(); } -template -typename Map::mapped_type& -getModuleElement(Map& m, Name name, std::string_view funcName) { - auto iter = m.find(name); - if (iter == m.end()) { - Fatal() << "Module::" << funcName << ": " << name << " does not exist"; - } - return iter->second; -} - -Export* Module::getExport(Name name) { +Export* Module::getExport(Name name) const { return getModuleElement(exportsMap, name, "getExport"); } -Function* Module::getFunction(Name name) { +Function* Module::getFunction(Name name) const { return getModuleElement(functionsMap, name, "getFunction"); } -Table* Module::getTable(Name name) { +Table* Module::getTable(Name name) const { return getModuleElement(tablesMap, name, "getTable"); } -ElementSegment* Module::getElementSegment(Name name) { +ElementSegment* Module::getElementSegment(Name name) const { return getModuleElement(elementSegmentsMap, name, "getElementSegment"); } -Memory* Module::getMemory(Name name) { +Memory* Module::getMemory(Name name) const { return getModuleElement(memoriesMap, name, "getMemory"); } -DataSegment* Module::getDataSegment(Name name) { +DataSegment* Module::getDataSegment(Name name) const { return getModuleElement(dataSegmentsMap, name, "getDataSegment"); } -Global* Module::getGlobal(Name name) { +Global* Module::getGlobal(Name name) const { return getModuleElement(globalsMap, name, "getGlobal"); } -Tag* Module::getTag(Name name) { +Tag* Module::getTag(Name name) const { return getModuleElement(tagsMap, name, "getTag"); } @@ -1756,39 +1761,39 @@ typename Map::mapped_type getModuleElementOrNull(Map& m, Name name) { return iter->second; } -Export* Module::getExportOrNull(Name name) { +Export* Module::getExportOrNull(Name name) const { return getModuleElementOrNull(exportsMap, name); } -Function* Module::getFunctionOrNull(Name name) { +Function* Module::getFunctionOrNull(Name name) const { return getModuleElementOrNull(functionsMap, name); } -Table* Module::getTableOrNull(Name name) { +Table* Module::getTableOrNull(Name name) const { return getModuleElementOrNull(tablesMap, name); } -ElementSegment* Module::getElementSegmentOrNull(Name name) { +ElementSegment* Module::getElementSegmentOrNull(Name name) const { return getModuleElementOrNull(elementSegmentsMap, name); } -Memory* Module::getMemoryOrNull(Name name) { +Memory* Module::getMemoryOrNull(Name name) const { return getModuleElementOrNull(memoriesMap, name); } -DataSegment* Module::getDataSegmentOrNull(Name name) { +DataSegment* Module::getDataSegmentOrNull(Name name) const { return getModuleElementOrNull(dataSegmentsMap, name); } -Global* Module::getGlobalOrNull(Name name) { +Global* Module::getGlobalOrNull(Name name) const { return getModuleElementOrNull(globalsMap, name); } -Tag* Module::getTagOrNull(Name name) { +Tag* Module::getTagOrNull(Name name) const { return getModuleElementOrNull(tagsMap, name); } -Importable* Module::getImport(ModuleItemKind kind, Name name) { +Importable* Module::getImport(ModuleItemKind kind, Name name) const { switch (kind) { case ModuleItemKind::Function: return getFunction(name); @@ -1809,7 +1814,7 @@ Importable* Module::getImport(ModuleItemKind kind, Name name) { WASM_UNREACHABLE("unexpected kind"); } -Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) { +Importable* Module::getImportOrNull(ModuleItemKind kind, Name name) const { auto doReturn = [](Importable* importable) { return importable ? importable->imported() ? importable : nullptr : nullptr; };