1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-27 00:06:26 +03:00

Store ExprId in InheritFrom bindings

This commit is contained in:
oxalica 2022-08-09 03:44:25 +08:00
parent 4b160fe412
commit 8cab7d5f8b
2 changed files with 15 additions and 20 deletions

View File

@ -428,9 +428,8 @@ impl MergingSet {
fn merge_inherit(&mut self, ctx: &mut LowerCtx, i: ast::Inherit) {
let from_expr = i.from_expr().map(|e| {
let expr = ctx.lower_expr_opt(e.expr());
let from_id = self.inherit_froms.len() as u32;
self.inherit_froms.push(expr);
from_id
expr
});
let mut no_attrs = true;
@ -455,7 +454,7 @@ impl MergingSet {
}
let value = match from_expr {
Some(id) => BindingValue::InheritFrom(id),
Some(e) => BindingValue::InheritFrom(e),
None => {
let name = match &key {
BindingKey::NameDef(def) => ctx.module[*def].name.clone(),
@ -963,8 +962,8 @@ mod tests {
0: Binding { key: Name("a"), value: Inherit(Idx::<Expr>(0)) }
1: Binding { key: Name("b"), value: Inherit(Idx::<Expr>(1)) }
2: Binding { key: Name("c"), value: Inherit(Idx::<Expr>(2)) }
3: Binding { key: Name("f"), value: InheritFrom(1) }
4: Binding { key: Name("g"), value: InheritFrom(1) }
3: Binding { key: Name("f"), value: InheritFrom(Idx::<Expr>(4)) }
4: Binding { key: Name("g"), value: InheritFrom(Idx::<Expr>(4)) }
"#]],
);
}

View File

@ -268,30 +268,26 @@ pub enum BindingKey {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BindingValue {
Inherit(ExprId),
InheritFrom(u32),
InheritFrom(ExprId),
Expr(ExprId),
}
impl Bindings {
pub(crate) fn walk_child_exprs(&self, module: &Module, mut f: impl FnMut(ExprId)) {
for &binding in self.entries.iter() {
module[binding].walk_child_exprs(&mut f);
let binding = &module[binding];
match binding.key {
BindingKey::NameDef(_) | BindingKey::Name(_) => {}
BindingKey::Dynamic(e) => f(e),
}
match binding.value {
BindingValue::Inherit(e) | BindingValue::Expr(e) => f(e),
// Walking here would be redundant, we traverse them outside `entries` loop.
BindingValue::InheritFrom(_) => {}
}
}
for &e in self.inherit_froms.iter() {
f(e);
}
}
}
impl Binding {
pub(crate) fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
match self.key {
BindingKey::NameDef(_) | BindingKey::Name(_) => {}
BindingKey::Dynamic(expr) => f(expr),
}
match self.value {
BindingValue::Inherit(e) | BindingValue::Expr(e) => f(e),
BindingValue::InheritFrom(_) => {}
}
}
}