Skip to content

Commit

Permalink
isle: populate rule name map in sema (bytecodealliance#156)
Browse files Browse the repository at this point in the history
Populate a `rule_map` in the `sema::TermEnv` mapping interned rule names
to rule IDs. This is similar to the `term_map` that already exists.

This should be useful in bytecodealliance#151 bytecodealliance#128 where we need to apply attributes to
rules.
  • Loading branch information
mmcloughlin authored Oct 16, 2024
1 parent 2d9be69 commit 009a9af
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions cranelift/isle/isle/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ pub struct TermEnv {
/// This is indexed by `RuleId`.
pub rules: Vec<Rule>,

/// A map from an interned `Rule`'s name to its `RuleId`.
pub rule_map: StableMap<Sym, RuleId>,

/// Map from (inner_ty, outer_ty) pairs to term IDs, giving the
/// defined implicit type-converter terms we can try to use to fit
/// types together.
Expand Down Expand Up @@ -1223,6 +1226,7 @@ impl TermEnv {
terms: vec![],
term_map: StableMap::new(),
rules: vec![],
rule_map: StableMap::new(),
converters: StableMap::new(),
expand_internal_extractors,
};
Expand All @@ -1241,7 +1245,6 @@ impl TermEnv {
env.collect_rules(tyenv, defs);
env.check_for_undefined_decls(tyenv, defs);
env.check_for_expr_terms_without_constructors(tyenv, defs);
env.check_for_duplicate_rule_names(tyenv);
tyenv.return_errors()?;

Ok(env)
Expand Down Expand Up @@ -1853,6 +1856,22 @@ impl TermEnv {
rule.name = Some(term.name);
}
}

// Populate rule name map.
for rule in &self.rules {
let Some(name) = rule.name else { continue };
match self.rule_map.entry(name) {
Entry::Vacant(e) => {
e.insert(rule.id);
}
Entry::Occupied(_) => {
tyenv.report_error(
rule.pos,
format!("Duplicate rule name: '{}'", tyenv.syms[name.index()]),
);
}
}
}
}

fn check_for_undefined_decls(&self, tyenv: &mut TypeEnv, defs: &[ast::Def]) {
Expand Down Expand Up @@ -1900,23 +1919,6 @@ impl TermEnv {
}
}

fn check_for_duplicate_rule_names(&self, tyenv: &mut TypeEnv) {
let mut rules_by_name = HashMap::new();
for rule in &self.rules {
let Some(name) = rule.name else {
continue;
};
match rules_by_name.entry(name) {
Entry::Occupied(_) => {
tyenv.report_error(rule.pos, "duplicate rule name");
}
Entry::Vacant(e) => {
e.insert(rule);
}
};
}
}

fn maybe_implicit_convert_pattern(
&self,
tyenv: &mut TypeEnv,
Expand Down

0 comments on commit 009a9af

Please sign in to comment.