Skip to main content

lisette_syntax/program/
emit_input.rs

1use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
2
3use ecow::EcoString;
4
5use crate::ast::{BindingId as AstBindingId, Pattern, RestPattern, Span};
6use crate::types::Symbol;
7
8use super::{Definition, File, ModuleInfo};
9
10#[derive(Debug, Clone, Default)]
11pub struct UnusedInfo {
12    bindings: HashSet<Span>,
13    definitions: HashSet<Span>,
14    pub imports_by_module: HashMap<EcoString, HashSet<EcoString>>,
15}
16
17impl UnusedInfo {
18    pub fn mark_binding_unused(&mut self, span: Span) {
19        self.bindings.insert(span);
20    }
21
22    pub fn is_unused_binding(&self, pattern: &Pattern) -> bool {
23        match pattern {
24            Pattern::Identifier { span, .. } => self.bindings.contains(span),
25            Pattern::AsBinding { span, name, .. } => {
26                let name_span = Span::new(
27                    span.file_id,
28                    span.byte_offset + span.byte_length - name.len() as u32,
29                    name.len() as u32,
30                );
31                self.bindings.contains(&name_span)
32            }
33            _ => false,
34        }
35    }
36
37    pub fn is_unused_rest_binding(&self, rest: &RestPattern) -> bool {
38        match rest {
39            RestPattern::Bind { span, .. } => self.bindings.contains(span),
40            _ => false,
41        }
42    }
43
44    pub fn mark_definition_unused(&mut self, span: Span) {
45        self.definitions.insert(span);
46    }
47
48    pub fn is_unused_definition(&self, span: &Span) -> bool {
49        self.definitions.contains(span)
50    }
51
52    pub fn merge(&mut self, other: UnusedInfo) {
53        let UnusedInfo {
54            bindings,
55            definitions,
56            imports_by_module,
57        } = other;
58        self.bindings.extend(bindings);
59        self.definitions.extend(definitions);
60        for (module, imports) in imports_by_module {
61            self.imports_by_module
62                .entry(module)
63                .or_default()
64                .extend(imports);
65        }
66    }
67}
68
69#[derive(Debug, Clone)]
70pub struct TestFunction {
71    pub module_id: String,
72    pub qualified_name: String,
73    pub title: Option<String>,
74    pub doc: Option<String>,
75    pub span: Span,
76}
77
78#[derive(Debug, Clone, Default)]
79pub struct TestIndex {
80    tests: Vec<TestFunction>,
81}
82
83impl TestIndex {
84    pub fn push(&mut self, test: TestFunction) {
85        self.tests.push(test);
86    }
87
88    pub fn tests(&self) -> &[TestFunction] {
89        &self.tests
90    }
91
92    pub fn contains_qualified(&self, qualified_name: &str) -> bool {
93        self.tests
94            .iter()
95            .any(|t| t.qualified_name == qualified_name)
96    }
97
98    pub fn len(&self) -> usize {
99        self.tests.len()
100    }
101
102    pub fn is_empty(&self) -> bool {
103        self.tests.is_empty()
104    }
105}
106
107#[derive(Debug, Clone, Default)]
108pub struct EqualityIndex {
109    by_id: HashMap<String, EqualityInfo>,
110}
111
112#[derive(Debug, Clone)]
113pub enum EqualityInfo {
114    Method {
115        private_to_module: Option<String>,
116        synthesized: bool,
117    },
118    Unusable {
119        reason: EqualityUnusableReason,
120        private_to_module: Option<String>,
121    },
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
125pub enum EqualityUnusableReason {
126    UfcsLowered,
127    BoundMismatch,
128}
129
130fn visible_from(private_to_module: &Option<String>, current_module: &str) -> bool {
131    match private_to_module {
132        None => true,
133        Some(module) => module == current_module,
134    }
135}
136
137impl EqualityIndex {
138    pub fn insert_method(
139        &mut self,
140        id: String,
141        private_to_module: Option<String>,
142        synthesized: bool,
143    ) {
144        self.by_id.insert(
145            id,
146            EqualityInfo::Method {
147                private_to_module,
148                synthesized,
149            },
150        );
151    }
152
153    pub fn insert_unusable(
154        &mut self,
155        id: String,
156        reason: EqualityUnusableReason,
157        private_to_module: Option<String>,
158    ) {
159        self.by_id.insert(
160            id,
161            EqualityInfo::Unusable {
162                reason,
163                private_to_module,
164            },
165        );
166    }
167
168    pub fn usable_from(&self, id: &str, current_module: &str) -> bool {
169        matches!(
170            self.by_id.get(id),
171            Some(EqualityInfo::Method { private_to_module, .. })
172                if visible_from(private_to_module, current_module)
173        )
174    }
175
176    pub fn unusable_reason_from(
177        &self,
178        id: &str,
179        current_module: &str,
180    ) -> Option<EqualityUnusableReason> {
181        match self.by_id.get(id) {
182            Some(EqualityInfo::Unusable {
183                reason,
184                private_to_module,
185            }) if visible_from(private_to_module, current_module) => Some(*reason),
186            _ => None,
187        }
188    }
189
190    pub fn is_synthesized(&self, id: &str) -> bool {
191        matches!(
192            self.by_id.get(id),
193            Some(EqualityInfo::Method {
194                synthesized: true,
195                ..
196            })
197        )
198    }
199}
200
201#[derive(Debug, Clone, Default)]
202pub struct MutationInfo {
203    bindings: HashSet<AstBindingId>,
204}
205
206impl MutationInfo {
207    pub fn mark_binding_mutated(&mut self, id: AstBindingId) {
208        self.bindings.insert(id);
209    }
210
211    pub fn is_mutated(&self, id: AstBindingId) -> bool {
212        self.bindings.contains(&id)
213    }
214}
215
216pub struct EmitInput {
217    pub files: HashMap<u32, File>,
218    pub definitions: HashMap<Symbol, Definition>,
219    pub modules: HashMap<String, ModuleInfo>,
220    pub entry_module_id: String,
221    pub unused: UnusedInfo,
222    pub mutations: MutationInfo,
223    pub cached_modules: HashSet<String>,
224    pub ufcs_methods: HashSet<(String, String)>,
225    pub equality_index: EqualityIndex,
226    pub test_index: TestIndex,
227    pub go_package_names: HashMap<String, String>,
228    pub go_module_ids: HashSet<String>,
229    pub bound_types: HashMap<crate::ast::Span, crate::types::Type>,
230}
231
232#[cfg(test)]
233mod tests {
234    use super::*;
235
236    fn span(offset: u32) -> Span {
237        Span::new(0, offset, 1)
238    }
239
240    #[test]
241    fn merge_extends_bindings_definitions_and_imports() {
242        let mut a = UnusedInfo::default();
243        a.mark_binding_unused(span(0));
244        a.mark_definition_unused(span(1));
245        a.imports_by_module
246            .insert("m1".into(), HashSet::from_iter(["x".into()]));
247
248        let mut b = UnusedInfo::default();
249        b.mark_binding_unused(span(2));
250        b.mark_definition_unused(span(3));
251        b.imports_by_module
252            .insert("m1".into(), HashSet::from_iter(["y".into()]));
253        b.imports_by_module
254            .insert("m2".into(), HashSet::from_iter(["z".into()]));
255
256        a.merge(b);
257
258        assert_eq!(a.bindings.len(), 2);
259        assert_eq!(a.definitions.len(), 2);
260        assert_eq!(a.imports_by_module["m1"].len(), 2);
261        assert_eq!(a.imports_by_module["m2"].len(), 1);
262    }
263}