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