|
1 |
| -use crate::{scope::ScopeData, symbol::*, HashMap, Module, Scope}; |
| 1 | +mod add; |
| 2 | +pub mod query; |
| 3 | +pub mod resolve; |
2 | 4 |
|
3 | 5 | use core::ops;
|
| 6 | + |
| 7 | +use crate::{ |
| 8 | + module::{ModuleData, ModuleKind}, |
| 9 | + scope::ScopeData, |
| 10 | + source::{Source, SourceData}, |
| 11 | + symbol::*, |
| 12 | + Module, Scope, |
| 13 | +}; |
| 14 | + |
4 | 15 | use rhai_rowan::syntax::SyntaxNode;
|
5 |
| -use serde::{Deserialize, Serialize}; |
| 16 | +use slotmap::{Key, SlotMap}; |
| 17 | +use url::Url; |
6 | 18 |
|
7 |
| -#[derive(Debug, Default, Clone, Serialize, Deserialize)] |
| 19 | +#[derive(Debug, Default, Clone)] |
8 | 20 | pub struct Hir {
|
9 |
| - modules: HashMap<String, Module>, |
| 21 | + static_module: Module, |
| 22 | + modules: SlotMap<Module, ModuleData>, |
| 23 | + scopes: SlotMap<Scope, ScopeData>, |
| 24 | + symbols: SlotMap<Symbol, SymbolData>, |
| 25 | + sources: SlotMap<Source, SourceData>, |
10 | 26 | }
|
11 | 27 |
|
12 | 28 | static_assertions::assert_impl_all!(Hir: Send, Sync);
|
13 | 29 |
|
14 |
| -impl ops::Index<Scope> for Hir { |
15 |
| - type Output = ScopeData; |
16 |
| - |
17 |
| - fn index(&self, index: Scope) -> &Self::Output { |
18 |
| - for (_, m) in self.modules() { |
19 |
| - if m.contains_scope(index) { |
20 |
| - return &m[index]; |
21 |
| - } |
22 |
| - } |
23 |
| - |
24 |
| - panic!( |
25 |
| - r#"scope "{:?}" does not exist in any of the modules"#, |
26 |
| - index |
27 |
| - ) |
| 30 | +impl Hir { |
| 31 | + #[must_use] |
| 32 | + pub fn new() -> Self { |
| 33 | + Self::default() |
28 | 34 | }
|
29 | 35 | }
|
30 | 36 |
|
31 |
| -impl ops::Index<Symbol> for Hir { |
32 |
| - type Output = SymbolData; |
| 37 | +impl Hir { |
| 38 | + pub fn clear(&mut self) { |
| 39 | + self.symbols.clear(); |
| 40 | + self.scopes.clear(); |
| 41 | + self.modules.clear(); |
| 42 | + self.sources.clear(); |
| 43 | + self.static_module = Module::default(); |
| 44 | + } |
33 | 45 |
|
34 |
| - fn index(&self, index: Symbol) -> &Self::Output { |
35 |
| - for (_, m) in self.modules() { |
36 |
| - if m.contains_symbol(index) { |
37 |
| - return &m[index]; |
38 |
| - } |
39 |
| - } |
| 46 | + #[must_use] |
| 47 | + pub fn symbol(&self, symbol: Symbol) -> Option<&SymbolData> { |
| 48 | + self.symbols.get(symbol) |
| 49 | + } |
40 | 50 |
|
41 |
| - panic!( |
42 |
| - r#"symbol "{:?}" does not exist in any of the modules"#, |
43 |
| - index |
44 |
| - ) |
| 51 | + pub fn symbols(&self) -> impl Iterator<Item = (Symbol, &SymbolData)> { |
| 52 | + self.symbols.iter() |
45 | 53 | }
|
46 |
| -} |
47 | 54 |
|
48 |
| -impl Hir { |
49 | 55 | #[must_use]
|
50 |
| - pub fn new() -> Self { |
51 |
| - Self::default() |
| 56 | + pub fn scope(&self, scope: Scope) -> Option<&ScopeData> { |
| 57 | + self.scopes.get(scope) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn scopes(&self) -> impl Iterator<Item = (Scope, &ScopeData)> { |
| 61 | + self.scopes.iter() |
52 | 62 | }
|
53 | 63 |
|
54 | 64 | #[must_use]
|
55 |
| - pub fn get_module(&self, name: &str) -> Option<&Module> { |
56 |
| - self.modules.get(name) |
| 65 | + pub fn module(&self, module: Module) -> Option<&ModuleData> { |
| 66 | + self.modules.get(module) |
57 | 67 | }
|
58 | 68 |
|
59 |
| - pub fn modules(&self) -> impl Iterator<Item = (&String, &Module)> { |
| 69 | + pub fn modules(&self) -> impl Iterator<Item = (Module, &ModuleData)> { |
60 | 70 | self.modules.iter()
|
61 | 71 | }
|
62 | 72 |
|
63 |
| - pub fn remove_module(&mut self, name: &str) { |
64 |
| - self.modules.remove(name); |
65 |
| - // TODO: module references |
| 73 | + pub fn sources(&self) -> impl Iterator<Item = (Source, &SourceData)> { |
| 74 | + self.sources.iter() |
66 | 75 | }
|
67 | 76 |
|
68 | 77 | #[must_use]
|
69 |
| - pub fn contains_module(&self, module: &str) -> bool { |
70 |
| - self.modules.contains_key(module) |
| 78 | + pub fn source_for(&self, url: &Url) -> Option<Source> { |
| 79 | + self.sources() |
| 80 | + .find_map(|(s, data)| if data.url == *url { Some(s) } else { None }) |
71 | 81 | }
|
72 | 82 |
|
73 |
| - #[must_use] |
74 |
| - pub fn module_count(&self) -> usize { |
75 |
| - self.modules.len() |
| 83 | + fn symbol_mut(&mut self, symbol: Symbol) -> &mut SymbolData { |
| 84 | + self.symbols.get_mut(symbol).unwrap() |
76 | 85 | }
|
77 | 86 |
|
78 |
| - #[must_use] |
79 |
| - pub fn contains_scope(&self, scope: Scope) -> bool { |
80 |
| - for (_, m) in self.modules() { |
81 |
| - if m.contains_scope(scope) { |
82 |
| - return true; |
83 |
| - } |
84 |
| - } |
| 87 | + fn scope_mut(&mut self, scope: Scope) -> &mut ScopeData { |
| 88 | + self.scopes.get_mut(scope).unwrap() |
| 89 | + } |
85 | 90 |
|
86 |
| - false |
| 91 | + #[allow(dead_code)] |
| 92 | + fn module_mut(&mut self, module: Module) -> &mut ModuleData { |
| 93 | + self.modules.get_mut(module).unwrap() |
87 | 94 | }
|
88 | 95 |
|
89 |
| - #[must_use] |
90 |
| - pub fn scope_count(&self) -> usize { |
91 |
| - self.modules() |
92 |
| - .fold(0, |count, (_, m)| count + m.scope_count()) |
| 96 | + fn source_mut(&mut self, source: Source) -> &mut SourceData { |
| 97 | + self.sources.get_mut(source).unwrap() |
93 | 98 | }
|
94 | 99 |
|
95 |
| - #[must_use] |
96 |
| - pub fn contains_symbol(&self, symbol: Symbol) -> bool { |
97 |
| - for (_, m) in self.modules() { |
98 |
| - if m.contains_symbol(symbol) { |
99 |
| - return true; |
100 |
| - } |
101 |
| - } |
| 100 | + // pub fn resolve_references(&mut self) { |
| 101 | + // for (_, m) in self.modules.iter_mut() { |
| 102 | + // m.resolve_references(); |
| 103 | + // } |
| 104 | + // } |
102 | 105 |
|
103 |
| - false |
104 |
| - } |
| 106 | + // pub fn resolve_references_in_module(&mut self, name: &str) { |
| 107 | + // if let Some(m) = self.modules.get_mut(name) { |
| 108 | + // m.resolve_references(); |
| 109 | + // } |
| 110 | + // } |
105 | 111 |
|
106 |
| - #[must_use] |
107 |
| - pub fn symbol_count(&self) -> usize { |
108 |
| - self.modules() |
109 |
| - .fold(0, |count, (_, m)| count + m.symbol_count()) |
110 |
| - } |
| 112 | + // pub fn infer_types(&mut self) { |
| 113 | + // for (_, m) in self.modules.iter_mut() { |
| 114 | + // m.infer_types(); |
| 115 | + // } |
| 116 | + // } |
111 | 117 | }
|
112 | 118 |
|
113 |
| -impl Hir { |
114 |
| - pub fn add_module_from_syntax(&mut self, name: &str, syntax: &SyntaxNode) { |
115 |
| - self.remove_module(name); |
| 119 | +impl ops::Index<Scope> for Hir { |
| 120 | + type Output = ScopeData; |
116 | 121 |
|
117 |
| - if let Some(m) = Module::new_from_syntax(name, syntax) { |
118 |
| - self.modules.insert(name.into(), m); |
119 |
| - } |
| 122 | + fn index(&self, index: Scope) -> &Self::Output { |
| 123 | + self.scopes.get(index).unwrap() |
120 | 124 | }
|
| 125 | +} |
121 | 126 |
|
122 |
| - pub fn resolve_references(&mut self) { |
123 |
| - for (_, m) in self.modules.iter_mut() { |
124 |
| - m.resolve_references(); |
125 |
| - } |
126 |
| - } |
| 127 | +impl ops::Index<Symbol> for Hir { |
| 128 | + type Output = SymbolData; |
127 | 129 |
|
128 |
| - pub fn resolve_references_in_module(&mut self, name: &str) { |
129 |
| - if let Some(m) = self.modules.get_mut(name) { |
130 |
| - m.resolve_references(); |
131 |
| - } |
| 130 | + fn index(&self, index: Symbol) -> &Self::Output { |
| 131 | + self.symbols.get(index).unwrap() |
132 | 132 | }
|
| 133 | +} |
| 134 | + |
| 135 | +impl ops::Index<Module> for Hir { |
| 136 | + type Output = ModuleData; |
133 | 137 |
|
134 |
| - pub fn infer_types(&mut self) { |
135 |
| - for (_, m) in self.modules.iter_mut() { |
136 |
| - m.infer_types(); |
137 |
| - } |
| 138 | + fn index(&self, index: Module) -> &Self::Output { |
| 139 | + self.modules.get(index).unwrap() |
138 | 140 | }
|
| 141 | +} |
139 | 142 |
|
140 |
| - pub fn infer_types_in_module(&mut self, name: &str) { |
141 |
| - if let Some(m) = self.modules.get_mut(name) { |
142 |
| - m.infer_types(); |
143 |
| - } |
144 |
| - } |
| 143 | +impl ops::Index<Source> for Hir { |
| 144 | + type Output = SourceData; |
| 145 | + |
| 146 | + fn index(&self, index: Source) -> &Self::Output { |
| 147 | + self.sources.get(index).unwrap() |
| 148 | + } |
145 | 149 | }
|
0 commit comments