aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-11-28 01:10:58 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-11-28 01:10:58 +0000
commit95c0c8f3986c8b3bcf0052d34d3ace09ebb9fa1b (patch)
tree0e5aa7337c000dd8c6ef3a7fedba68abf7feca8a /crates/ra_hir/src/module
parent9f08341aa486ea59cb488635f19e960523568fb8 (diff)
parent59e29aef633e906837f8fed604435976a46be691 (diff)
Merge #247
247: Hir r=matklad a=matklad This doesn't achive anything new, just a big refactoring. The main change is that Descriptors are now called `hir`, and live in a separate crate. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/module')
-rw-r--r--crates/ra_hir/src/module/imp.rs194
-rw-r--r--crates/ra_hir/src/module/mod.rs375
-rw-r--r--crates/ra_hir/src/module/nameres.rs338
3 files changed, 907 insertions, 0 deletions
diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs
new file mode 100644
index 000000000..76ea129a7
--- /dev/null
+++ b/crates/ra_hir/src/module/imp.rs
@@ -0,0 +1,194 @@
1use std::sync::Arc;
2
3use ra_syntax::{
4 ast::{self, NameOwner},
5 SmolStr,
6};
7use relative_path::RelativePathBuf;
8use rustc_hash::{FxHashMap, FxHashSet};
9use ra_db::{SourceRoot, SourceRootId, FileResolverImp, Cancelable, FileId,};
10
11use crate::{
12 HirDatabase,
13};
14
15use super::{
16 LinkData, LinkId, ModuleData, ModuleId, ModuleSource,
17 ModuleTree, Problem,
18};
19
20#[derive(Clone, Hash, PartialEq, Eq, Debug)]
21pub enum Submodule {
22 Declaration(SmolStr),
23 Definition(SmolStr, ModuleSource),
24}
25
26impl Submodule {
27 fn name(&self) -> &SmolStr {
28 match self {
29 Submodule::Declaration(name) => name,
30 Submodule::Definition(name, _) => name,
31 }
32 }
33}
34
35pub(crate) fn modules<'a>(
36 root: impl ast::ModuleItemOwner<'a>,
37) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> {
38 root.items()
39 .filter_map(|item| match item {
40 ast::ModuleItem::Module(m) => Some(m),
41 _ => None,
42 })
43 .filter_map(|module| {
44 let name = module.name()?.text();
45 Some((name, module))
46 })
47}
48
49pub(crate) fn module_tree(
50 db: &impl HirDatabase,
51 source_root: SourceRootId,
52) -> Cancelable<Arc<ModuleTree>> {
53 db.check_canceled()?;
54 let res = create_module_tree(db, source_root)?;
55 Ok(Arc::new(res))
56}
57
58fn create_module_tree<'a>(
59 db: &impl HirDatabase,
60 source_root: SourceRootId,
61) -> Cancelable<ModuleTree> {
62 let mut tree = ModuleTree::default();
63
64 let mut roots = FxHashMap::default();
65 let mut visited = FxHashSet::default();
66
67 let source_root = db.source_root(source_root);
68 for &file_id in source_root.files.iter() {
69 let source = ModuleSource::SourceFile(file_id);
70 if visited.contains(&source) {
71 continue; // TODO: use explicit crate_roots here
72 }
73 assert!(!roots.contains_key(&file_id));
74 let module_id = build_subtree(
75 db,
76 &source_root,
77 &mut tree,
78 &mut visited,
79 &mut roots,
80 None,
81 source,
82 )?;
83 roots.insert(file_id, module_id);
84 }
85 Ok(tree)
86}
87
88fn build_subtree(
89 db: &impl HirDatabase,
90 source_root: &SourceRoot,
91 tree: &mut ModuleTree,
92 visited: &mut FxHashSet<ModuleSource>,
93 roots: &mut FxHashMap<FileId, ModuleId>,
94 parent: Option<LinkId>,
95 source: ModuleSource,
96) -> Cancelable<ModuleId> {
97 visited.insert(source);
98 let id = tree.push_mod(ModuleData {
99 source,
100 parent,
101 children: Vec::new(),
102 });
103 for sub in db.submodules(source)?.iter() {
104 let link = tree.push_link(LinkData {
105 name: sub.name().clone(),
106 owner: id,
107 points_to: Vec::new(),
108 problem: None,
109 });
110
111 let (points_to, problem) = match sub {
112 Submodule::Declaration(name) => {
113 let (points_to, problem) =
114 resolve_submodule(source, &name, &source_root.file_resolver);
115 let points_to = points_to
116 .into_iter()
117 .map(|file_id| match roots.remove(&file_id) {
118 Some(module_id) => {
119 tree.mods[module_id].parent = Some(link);
120 Ok(module_id)
121 }
122 None => build_subtree(
123 db,
124 source_root,
125 tree,
126 visited,
127 roots,
128 Some(link),
129 ModuleSource::SourceFile(file_id),
130 ),
131 })
132 .collect::<Cancelable<Vec<_>>>()?;
133 (points_to, problem)
134 }
135 Submodule::Definition(_name, submodule_source) => {
136 let points_to = build_subtree(
137 db,
138 source_root,
139 tree,
140 visited,
141 roots,
142 Some(link),
143 *submodule_source,
144 )?;
145 (vec![points_to], None)
146 }
147 };
148
149 tree.links[link].points_to = points_to;
150 tree.links[link].problem = problem;
151 }
152 Ok(id)
153}
154
155fn resolve_submodule(
156 source: ModuleSource,
157 name: &SmolStr,
158 file_resolver: &FileResolverImp,
159) -> (Vec<FileId>, Option<Problem>) {
160 let file_id = match source {
161 ModuleSource::SourceFile(it) => it,
162 ModuleSource::Module(..) => {
163 // TODO
164 return (Vec::new(), None);
165 }
166 };
167 let mod_name = file_resolver.file_stem(file_id);
168 let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main";
169
170 let file_mod = RelativePathBuf::from(format!("../{}.rs", name));
171 let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name));
172 let points_to: Vec<FileId>;
173 let problem: Option<Problem>;
174 if is_dir_owner {
175 points_to = [&file_mod, &dir_mod]
176 .iter()
177 .filter_map(|path| file_resolver.resolve(file_id, path))
178 .collect();
179 problem = if points_to.is_empty() {
180 Some(Problem::UnresolvedModule {
181 candidate: file_mod,
182 })
183 } else {
184 None
185 }
186 } else {
187 points_to = Vec::new();
188 problem = Some(Problem::NotDirOwner {
189 move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)),
190 candidate: file_mod,
191 });
192 }
193 (points_to, problem)
194}
diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs
new file mode 100644
index 000000000..a011fd53e
--- /dev/null
+++ b/crates/ra_hir/src/module/mod.rs
@@ -0,0 +1,375 @@
1pub(super) mod imp;
2pub(super) mod nameres;
3
4use std::sync::Arc;
5
6use ra_editor::find_node_at_offset;
7
8use ra_syntax::{
9 algo::generate,
10 ast::{self, AstNode, NameOwner},
11 SmolStr, SyntaxNode,
12};
13use ra_db::{SourceRootId, FileId, FilePosition, Cancelable};
14use relative_path::RelativePathBuf;
15
16use crate::{
17 DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId,
18 arena::{Arena, Id},
19};
20
21pub use self::nameres::ModuleScope;
22
23/// `Module` is API entry point to get all the information
24/// about a particular module.
25#[derive(Debug, Clone)]
26pub struct Module {
27 tree: Arc<ModuleTree>,
28 source_root_id: SourceRootId,
29 //TODO: make private
30 pub module_id: ModuleId,
31}
32
33impl Module {
34 /// Lookup `Module` by `FileId`. Note that this is inherently
35 /// lossy transformation: in general, a single source might correspond to
36 /// several modules.
37 pub fn guess_from_file_id(
38 db: &impl HirDatabase,
39 file_id: FileId,
40 ) -> Cancelable<Option<Module>> {
41 Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id))
42 }
43
44 /// Lookup `Module` by position in the source code. Note that this
45 /// is inherently lossy transformation: in general, a single source might
46 /// correspond to several modules.
47 pub fn guess_from_position(
48 db: &impl HirDatabase,
49 position: FilePosition,
50 ) -> Cancelable<Option<Module>> {
51 let file = db.source_file(position.file_id);
52 let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset)
53 {
54 Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m),
55 _ => ModuleSource::SourceFile(position.file_id),
56 };
57 Module::guess_from_source(db, position.file_id, module_source)
58 }
59
60 fn guess_from_source(
61 db: &impl HirDatabase,
62 file_id: FileId,
63 module_source: ModuleSource,
64 ) -> Cancelable<Option<Module>> {
65 let source_root_id = db.file_source_root(file_id);
66 let module_tree = db.module_tree(source_root_id)?;
67
68 let res = match module_tree.any_module_for_source(module_source) {
69 None => None,
70 Some(module_id) => Some(Module {
71 tree: module_tree,
72 source_root_id,
73 module_id,
74 }),
75 };
76 Ok(res)
77 }
78
79 pub(super) fn new(
80 db: &impl HirDatabase,
81 source_root_id: SourceRootId,
82 module_id: ModuleId,
83 ) -> Cancelable<Module> {
84 let module_tree = db.module_tree(source_root_id)?;
85 let res = Module {
86 tree: module_tree,
87 source_root_id,
88 module_id,
89 };
90 Ok(res)
91 }
92
93 /// Returns `mod foo;` or `mod foo {}` node whihc declared this module.
94 /// Returns `None` for the root module
95 pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> {
96 let link = self.module_id.parent_link(&self.tree)?;
97 let file_id = link.owner(&self.tree).source(&self.tree).file_id();
98 let src = link.bind_source(&self.tree, db);
99 Some((file_id, src))
100 }
101
102 pub fn source(&self) -> ModuleSource {
103 self.module_id.source(&self.tree)
104 }
105
106 /// Parent module. Returns `None` if this is a root module.
107 pub fn parent(&self) -> Option<Module> {
108 let parent_id = self.module_id.parent(&self.tree)?;
109 Some(Module {
110 module_id: parent_id,
111 ..self.clone()
112 })
113 }
114
115 /// The root of the tree this module is part of
116 pub fn crate_root(&self) -> Module {
117 let root_id = self.module_id.crate_root(&self.tree);
118 Module {
119 module_id: root_id,
120 ..self.clone()
121 }
122 }
123
124 /// `name` is `None` for the crate's root module
125 #[allow(unused)]
126 pub fn name(&self) -> Option<SmolStr> {
127 let link = self.module_id.parent_link(&self.tree)?;
128 Some(link.name(&self.tree))
129 }
130
131 pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
132 let def_loc = DefLoc::Module {
133 id: self.module_id,
134 source_root: self.source_root_id,
135 };
136 def_loc.id(db)
137 }
138
139 /// Finds a child module with the specified name.
140 pub fn child(&self, name: &str) -> Option<Module> {
141 let child_id = self.module_id.child(&self.tree, name)?;
142 Some(Module {
143 module_id: child_id,
144 ..self.clone()
145 })
146 }
147
148 /// Returns a `ModuleScope`: a set of items, visible in this module.
149 pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> {
150 let item_map = db.item_map(self.source_root_id)?;
151 let res = item_map.per_module[&self.module_id].clone();
152 Ok(res)
153 }
154
155 pub fn resolve_path(&self, db: &impl HirDatabase, path: Path) -> Cancelable<Option<DefId>> {
156 let mut curr = match path.kind {
157 PathKind::Crate => self.crate_root(),
158 PathKind::Self_ | PathKind::Plain => self.clone(),
159 PathKind::Super => ctry!(self.parent()),
160 }
161 .def_id(db);
162
163 let segments = path.segments;
164 for name in segments.iter() {
165 let module = match curr.loc(db) {
166 DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?,
167 _ => return Ok(None),
168 };
169 let scope = module.scope(db)?;
170 curr = ctry!(ctry!(scope.get(&name)).def_id);
171 }
172 Ok(Some(curr))
173 }
174
175 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
176 self.module_id.problems(&self.tree, db)
177 }
178}
179
180/// Phisically, rust source is organized as a set of files, but logically it is
181/// organized as a tree of modules. Usually, a single file corresponds to a
182/// single module, but it is not nessary the case.
183///
184/// Module encapsulate the logic of transitioning from the fuzzy world of files
185/// (which can have multiple parents) to the precise world of modules (which
186/// always have one parent).
187#[derive(Default, Debug, PartialEq, Eq)]
188pub struct ModuleTree {
189 mods: Arena<ModuleData>,
190 links: Arena<LinkData>,
191}
192
193impl ModuleTree {
194 pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a {
195 self.mods.iter().map(|(id, _)| id)
196 }
197
198 fn modules_for_source(&self, source: ModuleSource) -> Vec<ModuleId> {
199 self.mods
200 .iter()
201 .filter(|(_idx, it)| it.source == source)
202 .map(|(idx, _)| idx)
203 .collect()
204 }
205
206 fn any_module_for_source(&self, source: ModuleSource) -> Option<ModuleId> {
207 self.modules_for_source(source).pop()
208 }
209}
210
211/// `ModuleSource` is the syntax tree element that produced this module:
212/// either a file, or an inlinde module.
213#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
214pub enum ModuleSource {
215 SourceFile(FileId),
216 Module(SourceItemId),
217}
218
219/// An owned syntax node for a module. Unlike `ModuleSource`,
220/// this holds onto the AST for the whole file.
221pub enum ModuleSourceNode {
222 SourceFile(ast::SourceFileNode),
223 Module(ast::ModuleNode),
224}
225
226pub type ModuleId = Id<ModuleData>;
227type LinkId = Id<LinkData>;
228
229#[derive(Clone, Debug, Hash, PartialEq, Eq)]
230pub enum Problem {
231 UnresolvedModule {
232 candidate: RelativePathBuf,
233 },
234 NotDirOwner {
235 move_to: RelativePathBuf,
236 candidate: RelativePathBuf,
237 },
238}
239
240impl ModuleId {
241 pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource {
242 tree.mods[self].source
243 }
244 fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
245 tree.mods[self].parent
246 }
247 fn parent(self, tree: &ModuleTree) -> Option<ModuleId> {
248 let link = self.parent_link(tree)?;
249 Some(tree.links[link].owner)
250 }
251 fn crate_root(self, tree: &ModuleTree) -> ModuleId {
252 generate(Some(self), move |it| it.parent(tree))
253 .last()
254 .unwrap()
255 }
256 fn child(self, tree: &ModuleTree, name: &str) -> Option<ModuleId> {
257 let link = tree.mods[self]
258 .children
259 .iter()
260 .map(|&it| &tree.links[it])
261 .find(|it| it.name == name)?;
262 Some(*link.points_to.first()?)
263 }
264 fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (SmolStr, ModuleId)> + 'a {
265 tree.mods[self].children.iter().filter_map(move |&it| {
266 let link = &tree.links[it];
267 let module = *link.points_to.first()?;
268 Some((link.name.clone(), module))
269 })
270 }
271 fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
272 tree.mods[self]
273 .children
274 .iter()
275 .filter_map(|&it| {
276 let p = tree.links[it].problem.clone()?;
277 let s = it.bind_source(tree, db);
278 let s = s.borrowed().name().unwrap().syntax().owned();
279 Some((s, p))
280 })
281 .collect()
282 }
283}
284
285impl LinkId {
286 fn owner(self, tree: &ModuleTree) -> ModuleId {
287 tree.links[self].owner
288 }
289 fn name(self, tree: &ModuleTree) -> SmolStr {
290 tree.links[self].name.clone()
291 }
292 fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode {
293 let owner = self.owner(tree);
294 match owner.source(tree).resolve(db) {
295 ModuleSourceNode::SourceFile(root) => {
296 let ast = imp::modules(root.borrowed())
297 .find(|(name, _)| name == &tree.links[self].name)
298 .unwrap()
299 .1;
300 ast.owned()
301 }
302 ModuleSourceNode::Module(it) => it,
303 }
304 }
305}
306
307#[derive(Debug, PartialEq, Eq, Hash)]
308pub struct ModuleData {
309 source: ModuleSource,
310 parent: Option<LinkId>,
311 children: Vec<LinkId>,
312}
313
314impl ModuleSource {
315 pub(crate) fn new_inline(
316 db: &impl HirDatabase,
317 file_id: FileId,
318 module: ast::Module,
319 ) -> ModuleSource {
320 assert!(!module.has_semi());
321 let items = db.file_items(file_id);
322 let item_id = items.id_of(module.syntax());
323 let id = SourceItemId { file_id, item_id };
324 ModuleSource::Module(id)
325 }
326
327 pub fn as_file(self) -> Option<FileId> {
328 match self {
329 ModuleSource::SourceFile(f) => Some(f),
330 ModuleSource::Module(..) => None,
331 }
332 }
333
334 pub fn file_id(self) -> FileId {
335 match self {
336 ModuleSource::SourceFile(f) => f,
337 ModuleSource::Module(source_item_id) => source_item_id.file_id,
338 }
339 }
340
341 pub fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
342 match self {
343 ModuleSource::SourceFile(file_id) => {
344 let syntax = db.source_file(file_id);
345 ModuleSourceNode::SourceFile(syntax.ast().owned())
346 }
347 ModuleSource::Module(item_id) => {
348 let syntax = db.file_item(item_id);
349 let syntax = syntax.borrowed();
350 let module = ast::Module::cast(syntax).unwrap();
351 ModuleSourceNode::Module(module.owned())
352 }
353 }
354 }
355}
356
357#[derive(Hash, Debug, PartialEq, Eq)]
358struct LinkData {
359 owner: ModuleId,
360 name: SmolStr,
361 points_to: Vec<ModuleId>,
362 problem: Option<Problem>,
363}
364
365impl ModuleTree {
366 fn push_mod(&mut self, data: ModuleData) -> ModuleId {
367 self.mods.alloc(data)
368 }
369 fn push_link(&mut self, data: LinkData) -> LinkId {
370 let owner = data.owner;
371 let id = self.links.alloc(data);
372 self.mods[owner].children.push(id);
373 id
374 }
375}
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
new file mode 100644
index 000000000..837a8d5ae
--- /dev/null
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -0,0 +1,338 @@
1//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a
2//! map with maps each module to it's scope: the set of items, visible in the
3//! module. That is, we only resolve imports here, name resolution of item
4//! bodies will be done in a separate step.
5//!
6//! Like Rustc, we use an interative per-crate algorithm: we start with scopes
7//! containing only directly defined items, and then iteratively resolve
8//! imports.
9//!
10//! To make this work nicely in the IDE scenarios, we place `InputModuleItems`
11//! in between raw syntax and name resolution. `InputModuleItems` are computed
12//! using only the module's syntax, and it is all directly defined items plus
13//! imports. The plain is to make `InputModuleItems` independent of local
14//! modifications (that is, typing inside a function shold not change IMIs),
15//! such that the results of name resolution can be preserved unless the module
16//! structure itself is modified.
17use std::{
18 sync::Arc,
19};
20
21use rustc_hash::FxHashMap;
22use ra_syntax::{
23 TextRange,
24 SmolStr, SyntaxKind::{self, *},
25 ast::{self, AstNode}
26};
27use ra_db::SourceRootId;
28
29use crate::{
30 Cancelable, FileId,
31 DefId, DefLoc,
32 SourceItemId, SourceFileItemId, SourceFileItems,
33 Path, PathKind,
34 HirDatabase,
35 module::{ModuleId, ModuleTree},
36};
37
38/// Item map is the result of the name resolution. Item map contains, for each
39/// module, the set of visible items.
40#[derive(Default, Debug, PartialEq, Eq)]
41pub struct ItemMap {
42 pub per_module: FxHashMap<ModuleId, ModuleScope>,
43}
44
45#[derive(Debug, Default, PartialEq, Eq, Clone)]
46pub struct ModuleScope {
47 pub items: FxHashMap<SmolStr, Resolution>,
48}
49
50impl ModuleScope {
51 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a SmolStr, &Resolution)> + 'a {
52 self.items.iter()
53 }
54 pub fn get(&self, name: &SmolStr) -> Option<&Resolution> {
55 self.items.get(name)
56 }
57}
58
59/// A set of items and imports declared inside a module, without relation to
60/// other modules.
61///
62/// This stands in-between raw syntax and name resolution and alow us to avoid
63/// recomputing name res: if `InputModuleItems` are the same, we can avoid
64/// running name resolution.
65#[derive(Debug, Default, PartialEq, Eq)]
66pub struct InputModuleItems {
67 items: Vec<ModuleItem>,
68 imports: Vec<Import>,
69}
70
71#[derive(Debug, PartialEq, Eq)]
72struct ModuleItem {
73 id: SourceFileItemId,
74 name: SmolStr,
75 kind: SyntaxKind,
76 vis: Vis,
77}
78
79#[derive(Debug, PartialEq, Eq)]
80enum Vis {
81 // Priv,
82 Other,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq)]
86struct Import {
87 path: Path,
88 kind: ImportKind,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub struct NamedImport {
93 pub file_item_id: SourceFileItemId,
94 pub relative_range: TextRange,
95}
96
97impl NamedImport {
98 pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange {
99 let source_item_id = SourceItemId {
100 file_id,
101 item_id: self.file_item_id,
102 };
103 let syntax = db.file_item(source_item_id);
104 let offset = syntax.borrowed().range().start();
105 self.relative_range + offset
106 }
107}
108
109#[derive(Debug, Clone, PartialEq, Eq)]
110enum ImportKind {
111 Glob,
112 Named(NamedImport),
113}
114
115/// Resolution is basically `DefId` atm, but it should account for stuff like
116/// multiple namespaces, ambiguity and errors.
117#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct Resolution {
119 /// None for unresolved
120 pub def_id: Option<DefId>,
121 /// ident by whitch this is imported into local scope.
122 pub import: Option<NamedImport>,
123}
124
125// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
126// enum Namespace {
127// Types,
128// Values,
129// }
130
131// #[derive(Debug)]
132// struct PerNs<T> {
133// types: Option<T>,
134// values: Option<T>,
135// }
136
137impl InputModuleItems {
138 pub(crate) fn new<'a>(
139 file_items: &SourceFileItems,
140 items: impl Iterator<Item = ast::ModuleItem<'a>>,
141 ) -> InputModuleItems {
142 let mut res = InputModuleItems::default();
143 for item in items {
144 res.add_item(file_items, item);
145 }
146 res
147 }
148
149 fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> {
150 match item {
151 ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
152 ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
153 ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
154 ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
155 ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
156 ast::ModuleItem::ImplItem(_) => {
157 // impls don't define items
158 }
159 ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it),
160 ast::ModuleItem::ExternCrateItem(_) => {
161 // TODO
162 }
163 ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
164 ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
165 ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?),
166 }
167 Some(())
168 }
169
170 fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
171 let file_item_id = file_items.id_of(item.syntax());
172 let start_offset = item.syntax().range().start();
173 Path::expand_use_item(item, |path, range| {
174 let kind = match range {
175 None => ImportKind::Glob,
176 Some(range) => ImportKind::Named(NamedImport {
177 file_item_id,
178 relative_range: range - start_offset,
179 }),
180 };
181 self.imports.push(Import { kind, path })
182 })
183 }
184}
185
186impl ModuleItem {
187 fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> {
188 let name = item.name()?.text();
189 let kind = item.syntax().kind();
190 let vis = Vis::Other;
191 let id = file_items.id_of(item.syntax());
192 let res = ModuleItem {
193 id,
194 name,
195 kind,
196 vis,
197 };
198 Some(res)
199 }
200}
201
202pub(crate) struct Resolver<'a, DB> {
203 pub db: &'a DB,
204 pub input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>,
205 pub source_root: SourceRootId,
206 pub module_tree: Arc<ModuleTree>,
207 pub result: ItemMap,
208}
209
210impl<'a, DB> Resolver<'a, DB>
211where
212 DB: HirDatabase,
213{
214 pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> {
215 for (&module_id, items) in self.input.iter() {
216 self.populate_module(module_id, items)
217 }
218
219 for &module_id in self.input.keys() {
220 self.db.check_canceled()?;
221 self.resolve_imports(module_id);
222 }
223 Ok(self.result)
224 }
225
226 fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) {
227 let file_id = module_id.source(&self.module_tree).file_id();
228
229 let mut module_items = ModuleScope::default();
230
231 for import in input.imports.iter() {
232 if let Some(name) = import.path.segments.iter().last() {
233 if let ImportKind::Named(import) = import.kind {
234 module_items.items.insert(
235 name.clone(),
236 Resolution {
237 def_id: None,
238 import: Some(import),
239 },
240 );
241 }
242 }
243 }
244
245 for item in input.items.iter() {
246 if item.kind == MODULE {
247 // handle submodules separatelly
248 continue;
249 }
250 let def_loc = DefLoc::Item {
251 source_item_id: SourceItemId {
252 file_id,
253 item_id: item.id,
254 },
255 };
256 let def_id = def_loc.id(self.db);
257 let resolution = Resolution {
258 def_id: Some(def_id),
259 import: None,
260 };
261 module_items.items.insert(item.name.clone(), resolution);
262 }
263
264 for (name, mod_id) in module_id.children(&self.module_tree) {
265 let def_loc = DefLoc::Module {
266 id: mod_id,
267 source_root: self.source_root,
268 };
269 let def_id = def_loc.id(self.db);
270 let resolution = Resolution {
271 def_id: Some(def_id),
272 import: None,
273 };
274 module_items.items.insert(name, resolution);
275 }
276
277 self.result.per_module.insert(module_id, module_items);
278 }
279
280 fn resolve_imports(&mut self, module_id: ModuleId) {
281 for import in self.input[&module_id].imports.iter() {
282 self.resolve_import(module_id, import);
283 }
284 }
285
286 fn resolve_import(&mut self, module_id: ModuleId, import: &Import) {
287 let ptr = match import.kind {
288 ImportKind::Glob => return,
289 ImportKind::Named(ptr) => ptr,
290 };
291
292 let mut curr = match import.path.kind {
293 // TODO: handle extern crates
294 PathKind::Plain => return,
295 PathKind::Self_ => module_id,
296 PathKind::Super => {
297 match module_id.parent(&self.module_tree) {
298 Some(it) => it,
299 // TODO: error
300 None => return,
301 }
302 }
303 PathKind::Crate => module_id.crate_root(&self.module_tree),
304 };
305
306 for (i, name) in import.path.segments.iter().enumerate() {
307 let is_last = i == import.path.segments.len() - 1;
308
309 let def_id = match self.result.per_module[&curr].items.get(name) {
310 None => return,
311 Some(res) => match res.def_id {
312 Some(it) => it,
313 None => return,
314 },
315 };
316
317 if !is_last {
318 curr = match def_id.loc(self.db) {
319 DefLoc::Module { id, .. } => id,
320 _ => return,
321 }
322 } else {
323 self.update(module_id, |items| {
324 let res = Resolution {
325 def_id: Some(def_id),
326 import: Some(ptr),
327 };
328 items.items.insert(name.clone(), res);
329 })
330 }
331 }
332 }
333
334 fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) {
335 let module_items = self.result.per_module.get_mut(&module_id).unwrap();
336 f(module_items)
337 }
338}