Skip to main content

litex/runtime/
runtime_parsing_free_param_collection.rs

1use crate::prelude::*;
2use std::collections::HashMap;
3
4#[derive(Clone)]
5pub struct FreeParamCollection {
6    pub params: HashMap<String, Vec<FreeParamTypeAndLineFile>>,
7}
8
9#[derive(Clone, Debug)]
10pub struct FreeParamTypeAndLineFile {
11    pub kind: ParamObjType,
12    pub line_file: LineFile,
13}
14
15impl FreeParamCollection {
16    pub fn new() -> Self {
17        FreeParamCollection {
18            params: HashMap::new(),
19        }
20    }
21
22    pub fn clear(&mut self) {
23        self.params.clear();
24    }
25
26    fn push_param(
27        &mut self,
28        name: String,
29        kind: ParamObjType,
30        line_file: LineFile,
31    ) -> Result<(), RuntimeError> {
32        let stack = self.params.entry(name.clone()).or_default();
33        if stack.iter().any(|b| b.kind == kind) {
34            return Err(RuntimeError::from(ParseRuntimeError(
35                RuntimeErrorStruct::new_with_msg_and_line_file(format!(
36                        "free parameter `{}` is already bound as {:?} in an active scope",
37                        name, kind
38                    ), line_file),
39            )));
40        }
41        stack.push(FreeParamTypeAndLineFile { kind, line_file });
42        Ok(())
43    }
44
45    pub fn begin_scope(
46        &mut self,
47        kind: ParamObjType,
48        names: &[String],
49        line_file: LineFile,
50    ) -> Result<(), RuntimeError> {
51        for n in names {
52            self.push_param(n.clone(), kind, line_file.clone())?;
53        }
54        Ok(())
55    }
56
57    pub fn end_scope(&mut self, kind: ParamObjType, names: &[String]) {
58        for n in names {
59            let Some(stack) = self.params.get_mut(n) else {
60                panic!("free param stack missing for `{}` on end_scope", n);
61            };
62            let Some(top) = stack.pop() else {
63                panic!("free param stack for `{}` empty on end_scope", n);
64            };
65            debug_assert_eq!(top.kind, kind);
66            if stack.is_empty() {
67                self.params.remove(n);
68            }
69        }
70    }
71
72    pub fn name_is_in_any_free_param_map(&self, name: &str) -> bool {
73        self.params.get(name).map_or(false, |stack| !stack.is_empty())
74    }
75
76    pub fn resolve_identifier_to_free_param_obj(&self, name: &str) -> Obj {
77        if !self.name_is_in_any_free_param_map(name) {
78            return Identifier::new(name.to_string()).into();
79        }
80        let Some(stack) = self.params.get(name) else {
81            return Identifier::new(name.to_string()).into();
82        };
83        let Some(top) = stack.last() else {
84            return Identifier::new(name.to_string()).into();
85        };
86        match top.kind {
87            ParamObjType::Forall => ForallFreeParamObj::new(name.to_string()).into(),
88            ParamObjType::DefHeader => DefHeaderFreeParamObj::new(name.to_string()).into(),
89            ParamObjType::Exist => ExistFreeParamObj::new(name.to_string()).into(),
90            ParamObjType::SetBuilder => SetBuilderFreeParamObj::new(name.to_string()).into(),
91            ParamObjType::FnSet => FnSetFreeParamObj::new(name.to_string()).into(),
92            ParamObjType::Induc => ByInducFreeParamObj::new(name.to_string()).into(),
93            ParamObjType::DefAlgo => DefAlgoFreeParamObj::new(name.to_string()).into(),
94            ParamObjType::Identifier => Identifier::new(name.to_string()).into(),
95        }
96    }
97}