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
53#[derive(Debug, Clone, Default)]
54pub struct MutationInfo {
55    bindings: HashSet<AstBindingId>,
56}
57
58impl MutationInfo {
59    pub fn mark_binding_mutated(&mut self, id: AstBindingId) {
60        self.bindings.insert(id);
61    }
62
63    pub fn is_mutated(&self, id: AstBindingId) -> bool {
64        self.bindings.contains(&id)
65    }
66}
67
68pub struct EmitInput {
69    pub files: HashMap<u32, File>,
70    pub definitions: HashMap<Symbol, Definition>,
71    pub modules: HashMap<String, ModuleInfo>,
72    pub entry_module_id: String,
73    pub unused: UnusedInfo,
74    pub mutations: MutationInfo,
75    pub cached_modules: HashSet<String>,
76    pub ufcs_methods: HashSet<(String, String)>,
77    pub go_package_names: HashMap<String, String>,
78}