calyx_opt/passes/
prop_protected.rs

1use calyx_ir::BoolAttr;
2
3use crate::traversal::{Action, Named, Visitor};
4
5/// Propagates any @protected tags on primitive definitions to all their
6/// instances. Used for primitives-level protected annotations
7#[derive(Default)]
8pub struct PropagateProtected;
9
10impl Named for PropagateProtected {
11    fn name() -> &'static str {
12        "propagate-protected"
13    }
14
15    fn description() -> &'static str {
16        "propagates the @protected annotation from primitive definitions to their instances"
17    }
18}
19
20impl Visitor for PropagateProtected {
21    fn start(
22        &mut self,
23        comp: &mut calyx_ir::Component,
24        sigs: &calyx_ir::LibrarySignatures,
25        _comps: &[calyx_ir::Component],
26    ) -> crate::traversal::VisResult {
27        for cell in comp.cells.iter() {
28            let name = if let calyx_ir::CellType::Primitive { name, .. } =
29                &cell.borrow().prototype
30            {
31                *name
32            } else {
33                continue;
34            };
35
36            let prim = sigs.get_primitive(name);
37            if prim.attributes.has(BoolAttr::Protected) {
38                cell.borrow_mut().add_attribute(BoolAttr::Protected, 1);
39            }
40        }
41        Ok(Action::SkipChildren)
42    }
43}