use crate::ir::{Ident, Node};
use rustc_hash::{FxHashMap, FxHashSet};
pub(crate) fn for_each_bound_name(nodes: &[Node], visit: &mut impl FnMut(&Ident)) {
for node in nodes {
match node {
Node::Let { name, .. } => visit(name),
Node::Loop { var, body, .. } => {
visit(var);
for_each_bound_name(body, visit);
}
Node::If {
then, otherwise, ..
} => {
for_each_bound_name(then, visit);
for_each_bound_name(otherwise, visit);
}
Node::Block(body) => for_each_bound_name(body, visit),
Node::Region { body, .. } => for_each_bound_name(body, visit),
_ => {}
}
}
}
pub(crate) fn collect_bound_names(nodes: &[Node], out: &mut FxHashSet<Ident>) {
for_each_bound_name(nodes, &mut |name| {
out.insert(name.clone());
});
}
pub(crate) fn count_bound_names(nodes: &[Node], counts: &mut FxHashMap<Ident, usize>) {
for_each_bound_name(nodes, &mut |name| {
*counts.entry(name.clone()).or_insert(0) += 1;
});
}