swamp_refs/
lib.rs

1pub mod prelude;
2
3use seq_map::SeqMap;
4use source_map_node::Node;
5use swamp_symbol::SymbolId;
6
7#[derive(Debug, Clone)]
8pub struct ReferenceTracker {
9    map: SeqMap<SymbolId, Vec<Node>>,
10}
11
12impl Default for ReferenceTracker {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl ReferenceTracker {
19    #[must_use] pub fn new() -> Self {
20        Self { map: SeqMap::new() }
21    }
22
23    pub fn add(&mut self, symbol: SymbolId, usage_site: Node) {
24        if let Some(vec) = self.map.get_mut(&symbol) {
25            vec.push(usage_site);
26        } else {
27            self.map.insert(symbol, vec![usage_site]).unwrap();
28        }
29    }
30
31    #[must_use] pub fn get(&self, symbol: SymbolId) -> Option<&[Node]> {
32        self.map.get(&symbol).map(std::vec::Vec::as_slice)
33    }
34
35    #[must_use] pub fn is_used(&self, symbol: &SymbolId) -> bool {
36        self.map.get(symbol).is_some_and(|v| !v.is_empty())
37    }
38
39    pub fn iter(&self) -> impl Iterator<Item=(&SymbolId, &[Node])> {
40        self.map.iter().map(|(id, nodes)| (id, nodes.as_slice()))
41    }
42}
43
44#[derive(Clone, Debug)]
45pub struct SymbolReference {
46    pub usage_node: Node,
47    pub pointing_to_symbol_id: SymbolId,
48}
49
50#[derive(Clone, Debug)]
51pub struct ModuleSymbolReferences {
52    pub refs: Vec<SymbolReference>,
53}
54
55impl Default for ModuleSymbolReferences {
56    fn default() -> Self {
57        Self::new()
58    }
59}
60
61impl ModuleSymbolReferences {
62    #[must_use] pub const fn new() -> Self {
63        Self {
64            refs: Vec::new(),
65        }
66    }
67    pub fn add(&mut self, symbol_id: SymbolId, usage_site: Node) {
68        self.refs.push(SymbolReference {
69            pointing_to_symbol_id: symbol_id,
70            usage_node: usage_site,
71        });
72    }
73}