aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-11-20 18:50:22 +0000
committerAleksey Kladov <[email protected]>2018-11-20 18:50:22 +0000
commitcc8163439f65138c0945c37d6bddc64ced2d2143 (patch)
tree232f888acb84723ea89fdf8decd866a042df5d6b
parent1cf92c3e286232be7e0d1370b88547ca0cb6f636 (diff)
populate modules
-rw-r--r--crates/ra_analysis/src/descriptors/module/mod.rs7
-rw-r--r--crates/ra_analysis/src/descriptors/module/nameres.rs74
-rw-r--r--crates/ra_analysis/src/loc2id.rs11
3 files changed, 80 insertions, 12 deletions
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs
index 958487541..f49e7f909 100644
--- a/crates/ra_analysis/src/descriptors/module/mod.rs
+++ b/crates/ra_analysis/src/descriptors/module/mod.rs
@@ -214,6 +214,13 @@ impl ModuleId {
214 .find(|it| it.name == name)?; 214 .find(|it| it.name == name)?;
215 Some(*link.points_to.first()?) 215 Some(*link.points_to.first()?)
216 } 216 }
217 fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (SmolStr, ModuleId)> + 'a {
218 tree.module(self).children.iter().filter_map(move |&it| {
219 let link = tree.link(it);
220 let module = *link.points_to.first()?;
221 Some((link.name.clone(), module))
222 })
223 }
217 fn problems(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> Vec<(SyntaxNode, Problem)> { 224 fn problems(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> Vec<(SyntaxNode, Problem)> {
218 tree.module(self) 225 tree.module(self)
219 .children 226 .children
diff --git a/crates/ra_analysis/src/descriptors/module/nameres.rs b/crates/ra_analysis/src/descriptors/module/nameres.rs
index 18e8342b3..6ff8e61f5 100644
--- a/crates/ra_analysis/src/descriptors/module/nameres.rs
+++ b/crates/ra_analysis/src/descriptors/module/nameres.rs
@@ -1,15 +1,21 @@
1//! Name resolution algorithm 1//! Name resolution algorithm
2use std::sync::Arc;
3
2use rustc_hash::FxHashMap; 4use rustc_hash::FxHashMap;
3 5
4use ra_syntax::{ 6use ra_syntax::{
5 SmolStr, SyntaxKind, 7 SmolStr, SyntaxKind::{self, *},
6 ast::{self, NameOwner, AstNode} 8 ast::{self, NameOwner, AstNode}
7}; 9};
8 10
9use crate::{ 11use crate::{
10 loc2id::DefId, 12 loc2id::{DefId, DefLoc},
11 descriptors::module::ModuleId, 13 descriptors::{
12 syntax_ptr::LocalSyntaxPtr, 14 DescriptorDatabase,
15 module::{ModuleId, ModuleTree},
16 },
17 syntax_ptr::{LocalSyntaxPtr, SyntaxPtr},
18 input::SourceRootId,
13}; 19};
14 20
15/// A set of items and imports declared inside a module, without relation to 21/// A set of items and imports declared inside a module, without relation to
@@ -44,9 +50,9 @@ struct ItemMap {
44 per_module: FxHashMap<ModuleId, ModuleItems>, 50 per_module: FxHashMap<ModuleId, ModuleItems>,
45} 51}
46 52
47#[derive(Debug)] 53#[derive(Debug, Default)]
48struct ModuleItems { 54struct ModuleItems {
49 items: FxHashMap<SmolStr, PerNs<DefId>>, 55 items: FxHashMap<SmolStr, DefId>,
50 import_resolutions: FxHashMap<LocalSyntaxPtr, DefId>, 56 import_resolutions: FxHashMap<LocalSyntaxPtr, DefId>,
51} 57}
52 58
@@ -201,13 +207,61 @@ impl ModuleItem {
201 } 207 }
202} 208}
203 209
204struct Resolver { 210struct Resolver<'a, DB> {
211 db: &'a DB,
212 source_root: SourceRootId,
213 module_tree: Arc<ModuleTree>,
205 input: FxHashMap<ModuleId, InputModuleItems>, 214 input: FxHashMap<ModuleId, InputModuleItems>,
206 result: ModuleItems, 215 result: ItemMap,
216}
217
218impl<'a, DB> Resolver<'a, DB>
219where
220 DB: DescriptorDatabase,
221{
222 fn resolve(&mut self) {
223 for (&module_id, items) in self.input.iter() {
224 populate_module(
225 self.db,
226 self.source_root,
227 &*self.module_tree,
228 &mut self.result,
229 module_id,
230 items,
231 )
232 }
233 }
207} 234}
208 235
209impl Resolver { 236fn populate_module(
210 fn resolve(&mut self){ 237 db: &impl DescriptorDatabase,
238 source_root: SourceRootId,
239 module_tree: &ModuleTree,
240 item_map: &mut ItemMap,
241 module_id: ModuleId,
242 input: &InputModuleItems,
243) {
244 let file_id = module_id.source(module_tree).file_id();
211 245
246 let mut module_items = ModuleItems::default();
247
248 for item in input.items.iter() {
249 if item.kind == MODULE {
250 // handle submodules separatelly
251 continue;
252 }
253 let ptr = item.ptr.into_global(file_id);
254 let def_loc = DefLoc::Item { ptr };
255 let def_id = db.id_maps().def_id(def_loc);
256 module_items.items.insert(item.name.clone(), def_id);
257 }
258
259 for (name, mod_id) in module_id.children(module_tree) {
260 let def_loc = DefLoc::Module {
261 id: mod_id,
262 source_root,
263 };
212 } 264 }
265
266 item_map.per_module.insert(module_id, module_items);
213} 267}
diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs
index 87417df94..e4b55f9b0 100644
--- a/crates/ra_analysis/src/loc2id.rs
+++ b/crates/ra_analysis/src/loc2id.rs
@@ -96,14 +96,14 @@ pub(crate) struct DefId(u32);
96impl_numeric_id!(DefId); 96impl_numeric_id!(DefId);
97 97
98#[derive(Clone, Debug, PartialEq, Eq, Hash)] 98#[derive(Clone, Debug, PartialEq, Eq, Hash)]
99enum DefLoc { 99pub(crate) enum DefLoc {
100 Module { 100 Module {
101 id: ModuleId, 101 id: ModuleId,
102 source_root: SourceRootId, 102 source_root: SourceRootId,
103 }, 103 },
104 Item { 104 Item {
105 ptr: SyntaxPtr, 105 ptr: SyntaxPtr,
106 } 106 },
107} 107}
108 108
109pub(crate) trait IdDatabase: salsa::Database { 109pub(crate) trait IdDatabase: salsa::Database {
@@ -122,6 +122,13 @@ impl IdMaps {
122 pub(crate) fn fn_ptr(&self, fn_id: FnId) -> SyntaxPtr { 122 pub(crate) fn fn_ptr(&self, fn_id: FnId) -> SyntaxPtr {
123 self.inner.fns.lock().id2loc(fn_id) 123 self.inner.fns.lock().id2loc(fn_id)
124 } 124 }
125
126 pub(crate) fn def_id(&self, loc: DefLoc) -> DefId {
127 self.inner.defs.lock().loc2id(&loc)
128 }
129 pub(crate) fn def_loc(&self, def_id: DefId) -> DefLoc {
130 self.inner.defs.lock().id2loc(def_id)
131 }
125} 132}
126 133
127#[derive(Debug, Default)] 134#[derive(Debug, Default)]