summaryrefslogtreecommitdiff
path: root/src/ir/iteration.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/ir/iteration.h')
-rw-r--r--src/ir/iteration.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/ir/iteration.h b/src/ir/iteration.h
index 289e5fa01..fd275f749 100644
--- a/src/ir/iteration.h
+++ b/src/ir/iteration.h
@@ -82,6 +82,30 @@ public:
Iterator end() const { return Iterator(*this, children.size()); }
};
+// Returns true if the current expression contains a certain kind of expression,
+// within the given depth of BFS. If depth is -1, this searches all children.
+template<typename T> bool containsChild(Expression* parent, int depth = -1) {
+ std::vector<Expression*> exprs;
+ std::vector<Expression*> nextExprs;
+ exprs.push_back(parent);
+ while (!exprs.empty() && depth > 0) {
+ for (auto* expr : exprs) {
+ for (auto* child : ChildIterator(expr)) {
+ if (child->is<T>()) {
+ return true;
+ }
+ nextExprs.push_back(child);
+ }
+ }
+ exprs.swap(nextExprs);
+ nextExprs.clear();
+ if (depth > 0) {
+ depth--;
+ }
+ }
+ return false;
+}
+
} // namespace wasm
#endif // wasm_ir_iteration_h