Skip to main content

lisette_syntax/program/
module.rs

1use rustc_hash::FxHashMap as HashMap;
2
3use ecow::EcoString;
4
5use super::definition::{Definition, Visibility};
6use super::file::{File, FileImport};
7
8pub type ModuleId = String;
9
10#[derive(Debug, Clone)]
11pub struct Module {
12    pub id: String,
13    /// file ID -> .lis file
14    pub files: HashMap<u32, File>,
15    /// file ID -> .d.lis file (declarations only)
16    pub typedefs: HashMap<u32, File>,
17    /// qualified name -> definition
18    pub definitions: HashMap<EcoString, Definition>,
19}
20
21impl Module {
22    pub fn new(id: &str) -> Module {
23        Module {
24            id: id.to_string(),
25            files: Default::default(),
26            typedefs: Default::default(),
27            definitions: Default::default(),
28        }
29    }
30
31    pub fn nominal() -> Module {
32        Module::new("**nominal")
33    }
34
35    pub fn is_public(&self, qualified_name: &str) -> bool {
36        if let Some(definition) = self.definitions.get(qualified_name) {
37            return definition.visibility() == &Visibility::Public;
38        }
39
40        false
41    }
42
43    pub fn get_file(&self, file_id: u32) -> Option<&File> {
44        self.files.get(&file_id)
45    }
46
47    pub fn file_ids(&self) -> impl Iterator<Item = u32> + '_ {
48        self.files.keys().copied()
49    }
50
51    pub fn get_typedef_by_id(&self, file_id: u32) -> Option<&File> {
52        self.typedefs.get(&file_id)
53    }
54
55    pub fn get_typedef_by_id_mut(&mut self, file_id: u32) -> Option<&mut File> {
56        self.typedefs.get_mut(&file_id)
57    }
58
59    pub fn typedef_imports(&self) -> Vec<FileImport> {
60        self.typedefs.values().flat_map(|f| f.imports()).collect()
61    }
62
63    pub fn all_typedefs(&self) -> impl Iterator<Item = &File> {
64        self.typedefs.values()
65    }
66
67    pub fn is_internal(&self) -> bool {
68        self.id == "prelude" || self.id == "**nominal" || self.id.starts_with("go:")
69    }
70}
71
72pub struct ModuleInfo {
73    pub id: String,
74    pub path: String,
75    pub file_ids: Vec<u32>,
76    pub typedef_ids: Vec<u32>,
77}