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