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