diff options
Diffstat (limited to 'src/ir/module-utils.h')
-rw-r--r-- | src/ir/module-utils.h | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h index 7dfd2d42d..912d78efd 100644 --- a/src/ir/module-utils.h +++ b/src/ir/module-utils.h @@ -132,12 +132,9 @@ inline DataSegment* copyDataSegment(const DataSegment* segment, Module& out) { return out.addDataSegment(std::move(ret)); } -inline void copyModule(const Module& in, Module& out) { - // we use names throughout, not raw pointers, so simple copying is fine - // for everything *but* expressions - for (auto& curr : in.exports) { - out.addExport(new Export(*curr)); - } +// Copies named toplevel module items (things of kind ModuleItemKind). See +// copyModule() for something that also copies exports, the start function, etc. +inline void copyModuleItems(const Module& in, Module& out) { for (auto& curr : in.functions) { copyFunction(curr.get(), out); } @@ -159,6 +156,15 @@ inline void copyModule(const Module& in, Module& out) { for (auto& curr : in.dataSegments) { copyDataSegment(curr.get(), out); } +} + +inline void copyModule(const Module& in, Module& out) { + // we use names throughout, not raw pointers, so simple copying is fine + // for everything *but* expressions + for (auto& curr : in.exports) { + out.addExport(std::make_unique<Export>(*curr)); + } + copyModuleItems(in, out); out.start = in.start; out.customSections = in.customSections; out.debugInfoFileNames = in.debugInfoFileNames; @@ -354,6 +360,36 @@ template<typename T> inline void iterImports(Module& wasm, T visitor) { iterImportedTags(wasm, visitor); } +// Iterates over all importable module items. The visitor provided should have +// signature void(ExternalKind, Importable*). +template<typename T> inline void iterImportable(Module& wasm, T visitor) { + for (auto& curr : wasm.functions) { + if (curr->imported()) { + visitor(ExternalKind::Function, curr.get()); + } + } + for (auto& curr : wasm.tables) { + if (curr->imported()) { + visitor(ExternalKind::Table, curr.get()); + } + } + for (auto& curr : wasm.memories) { + if (curr->imported()) { + visitor(ExternalKind::Memory, curr.get()); + } + } + for (auto& curr : wasm.globals) { + if (curr->imported()) { + visitor(ExternalKind::Global, curr.get()); + } + } + for (auto& curr : wasm.tags) { + if (curr->imported()) { + visitor(ExternalKind::Tag, curr.get()); + } + } +} + // Helper class for performing an operation on all the functions in the module, // in parallel, with an Info object for each one that can contain results of // some computation that the operation performs. |