Skip to main content

veryl_analyzer/ir/
interface.rs

1use crate::HashMap;
2use crate::ir::{Comptime, FuncPath, Function, VarId, VarPath, Variable};
3use crate::symbol::Direction;
4use indent::indent_all_by;
5use std::fmt;
6use veryl_parser::resource_table::StrId;
7
8#[derive(Clone)]
9pub struct Interface {
10    pub name: StrId,
11    pub var_paths: HashMap<VarPath, (VarId, Comptime)>,
12    pub func_paths: HashMap<FuncPath, VarId>,
13    pub variables: HashMap<VarId, Variable>,
14    pub functions: HashMap<VarId, Function>,
15    pub modports: HashMap<StrId, Vec<(StrId, Direction)>>,
16}
17
18impl Interface {
19    pub fn get_modport(&self, name: &StrId) -> HashMap<StrId, Direction> {
20        let mut ret = HashMap::default();
21        if let Some(x) = self.modports.get(name) {
22            for x in x {
23                ret.insert(x.0, x.1);
24            }
25        }
26        ret
27    }
28}
29
30impl fmt::Display for Interface {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        let mut ret = format!("interface {} {{\n", self.name);
33
34        let mut variables: Vec<_> = self.variables.iter().collect();
35        variables.sort_by(|a, b| a.0.cmp(b.0));
36
37        let mut functions: Vec<_> = self.functions.iter().collect();
38        functions.sort_by(|a, b| a.0.cmp(b.0));
39
40        for (_, x) in variables {
41            let text = format!("{}\n", x);
42            ret.push_str(&indent_all_by(2, text));
43        }
44
45        for (_, x) in functions {
46            let text = format!("{}\n", x);
47            ret.push_str(&indent_all_by(2, text));
48        }
49
50        ret.push('}');
51        ret.fmt(f)
52    }
53}