aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_analysis/Cargo.toml2
-rw-r--r--crates/ra_analysis/src/arena.rs66
-rw-r--r--crates/ra_analysis/src/completion/mod.rs1
-rw-r--r--crates/ra_analysis/src/completion/reference_completion.rs12
-rw-r--r--crates/ra_analysis/src/db.rs2
-rw-r--r--crates/ra_analysis/src/hir/db.rs70
-rw-r--r--crates/ra_analysis/src/hir/function/mod.rs190
-rw-r--r--crates/ra_analysis/src/hir/function/scope.rs451
-rw-r--r--crates/ra_analysis/src/hir/mod.rs131
-rw-r--r--crates/ra_analysis/src/hir/module/imp.rs195
-rw-r--r--crates/ra_analysis/src/hir/module/mod.rs379
-rw-r--r--crates/ra_analysis/src/hir/module/nameres.rs446
-rw-r--r--crates/ra_analysis/src/hir/path.rs148
-rw-r--r--crates/ra_analysis/src/hir/query_definitions.rs157
-rw-r--r--crates/ra_analysis/src/imp.rs10
-rw-r--r--crates/ra_analysis/src/lib.rs122
-rw-r--r--crates/ra_hir/src/arena.rs2
-rw-r--r--crates/ra_hir/src/db.rs4
-rw-r--r--crates/ra_hir/src/function/mod.rs16
-rw-r--r--crates/ra_hir/src/function/scope.rs17
-rw-r--r--crates/ra_hir/src/lib.rs30
-rw-r--r--crates/ra_hir/src/module/imp.rs2
-rw-r--r--crates/ra_hir/src/module/mod.rs31
-rw-r--r--crates/ra_hir/src/module/nameres.rs134
-rw-r--r--crates/ra_hir/src/path.rs14
25 files changed, 201 insertions, 2431 deletions
diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml
index 48d8e56e3..fe9765a66 100644
--- a/crates/ra_analysis/Cargo.toml
+++ b/crates/ra_analysis/Cargo.toml
@@ -12,8 +12,8 @@ fst = "0.3.1"
12salsa = "0.8.0" 12salsa = "0.8.0"
13rustc-hash = "1.0" 13rustc-hash = "1.0"
14parking_lot = "0.6.4" 14parking_lot = "0.6.4"
15id-arena = { git = "https://github.com/fitzgen/id-arena/", rev = "43ecd67" }
16ra_syntax = { path = "../ra_syntax" } 15ra_syntax = { path = "../ra_syntax" }
17ra_editor = { path = "../ra_editor" } 16ra_editor = { path = "../ra_editor" }
18ra_db = { path = "../ra_db" } 17ra_db = { path = "../ra_db" }
18hir = { path = "../ra_hir", package = "ra_hir" }
19test_utils = { path = "../test_utils" } 19test_utils = { path = "../test_utils" }
diff --git a/crates/ra_analysis/src/arena.rs b/crates/ra_analysis/src/arena.rs
deleted file mode 100644
index a752ec0c1..000000000
--- a/crates/ra_analysis/src/arena.rs
+++ /dev/null
@@ -1,66 +0,0 @@
1//! A simple id-based arena, similar to https://github.com/fitzgen/id-arena.
2//! We use our own version for more compact id's and to allow inherent impls
3//! on Ids.
4
5use std::{
6 fmt,
7 hash::{Hash, Hasher},
8 marker::PhantomData,
9};
10
11pub(crate) struct Id<T> {
12 idx: u32,
13 _ty: PhantomData<fn() -> T>,
14}
15
16impl<T> fmt::Debug for Id<T> {
17 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18 f.debug_tuple("Id").field(&self.idx).finish()
19 }
20}
21impl<T> Copy for Id<T> {}
22impl<T> Clone for Id<T> {
23 fn clone(&self) -> Id<T> {
24 *self
25 }
26}
27
28impl<T> PartialEq for Id<T> {
29 fn eq(&self, other: &Id<T>) -> bool {
30 self.idx == other.idx
31 }
32}
33
34impl<T> Eq for Id<T> {}
35
36impl<T> Hash for Id<T> {
37 fn hash<H: Hasher>(&self, h: &mut H) {
38 self.idx.hash(h);
39 }
40}
41
42#[derive(Debug, PartialEq, Eq)]
43pub(crate) struct ArenaBehavior<T> {
44 _ty: PhantomData<T>,
45}
46
47impl<T> id_arena::ArenaBehavior for ArenaBehavior<T> {
48 type Id = Id<T>;
49 fn new_arena_id() -> usize {
50 0
51 }
52 fn new_id(_arena_id: usize, index: usize) -> Id<T> {
53 Id {
54 idx: index as u32,
55 _ty: PhantomData,
56 }
57 }
58 fn index(id: Id<T>) -> usize {
59 id.idx as usize
60 }
61 fn arena_id(_id: Id<T>) -> usize {
62 0
63 }
64}
65
66pub(crate) type Arena<T> = id_arena::Arena<T, ArenaBehavior<T>>;
diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs
index 538b51633..e5ba92acd 100644
--- a/crates/ra_analysis/src/completion/mod.rs
+++ b/crates/ra_analysis/src/completion/mod.rs
@@ -12,7 +12,6 @@ use rustc_hash::{FxHashMap};
12 12
13use crate::{ 13use crate::{
14 db, 14 db,
15 hir,
16 Cancelable, FilePosition 15 Cancelable, FilePosition
17}; 16};
18 17
diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs
index 881d29916..e1a2d5241 100644
--- a/crates/ra_analysis/src/completion/reference_completion.rs
+++ b/crates/ra_analysis/src/completion/reference_completion.rs
@@ -6,16 +6,16 @@ use ra_syntax::{
6 ast::{self, LoopBodyOwner}, 6 ast::{self, LoopBodyOwner},
7 SyntaxKind::*, 7 SyntaxKind::*,
8}; 8};
9 9use hir::{
10use crate::{
11 db::RootDatabase,
12 completion::CompletionItem,
13 hir::{
14 self, 10 self,
15 FnScopes, 11 FnScopes,
16 Def, 12 Def,
17 Path, 13 Path,
18 }, 14};
15
16use crate::{
17 db::RootDatabase,
18 completion::CompletionItem,
19 Cancelable 19 Cancelable
20}; 20};
21 21
diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs
index 2bc1c8f8f..7fc3fe31b 100644
--- a/crates/ra_analysis/src/db.rs
+++ b/crates/ra_analysis/src/db.rs
@@ -3,9 +3,9 @@ use std::sync::Arc;
3use parking_lot::Mutex; 3use parking_lot::Mutex;
4use salsa::{self, Database}; 4use salsa::{self, Database};
5use ra_db::{LocationIntener, BaseDatabase}; 5use ra_db::{LocationIntener, BaseDatabase};
6use hir::{self, DefId, DefLoc, FnId, SourceItemId};
6 7
7use crate::{ 8use crate::{
8 hir::{self, DefId, DefLoc, FnId, SourceItemId},
9 symbol_index, 9 symbol_index,
10}; 10};
11 11
diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs
deleted file mode 100644
index c8ae551c5..000000000
--- a/crates/ra_analysis/src/hir/db.rs
+++ /dev/null
@@ -1,70 +0,0 @@
1use std::sync::Arc;
2
3use ra_syntax::{
4 SyntaxNode,
5 ast::FnDefNode,
6};
7use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase};
8
9use crate::{
10 FileId,
11 hir::{
12 DefLoc, DefId, FnId,
13 SourceFileItems, SourceItemId,
14 query_definitions,
15 function::{FnScopes},
16 module::{ModuleId, ModuleTree, ModuleSource,
17 nameres::{ItemMap, InputModuleItems}},
18 },
19 Cancelable,
20};
21
22salsa::query_group! {
23
24pub(crate) trait HirDatabase: SyntaxDatabase
25 + AsRef<LocationIntener<DefLoc, DefId>>
26 + AsRef<LocationIntener<SourceItemId, FnId>>
27{
28 fn fn_scopes(fn_id: FnId) -> Arc<FnScopes> {
29 type FnScopesQuery;
30 use fn query_definitions::fn_scopes;
31 }
32 fn fn_syntax(fn_id: FnId) -> FnDefNode {
33 type FnSyntaxQuery;
34 // Don't retain syntax trees in memory
35 storage dependencies;
36 use fn query_definitions::fn_syntax;
37 }
38
39 fn file_items(file_id: FileId) -> Arc<SourceFileItems> {
40 type SourceFileItemsQuery;
41 storage dependencies;
42 use fn query_definitions::file_items;
43 }
44
45 fn file_item(source_item_id: SourceItemId) -> SyntaxNode {
46 type FileItemQuery;
47 storage dependencies;
48 use fn query_definitions::file_item;
49 }
50
51 fn submodules(source: ModuleSource) -> Cancelable<Arc<Vec<crate::hir::module::imp::Submodule>>> {
52 type SubmodulesQuery;
53 use fn query_definitions::submodules;
54 }
55
56 fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable<Arc<InputModuleItems>> {
57 type InputModuleItemsQuery;
58 use fn query_definitions::input_module_items;
59 }
60 fn item_map(source_root_id: SourceRootId) -> Cancelable<Arc<ItemMap>> {
61 type ItemMapQuery;
62 use fn query_definitions::item_map;
63 }
64 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
65 type ModuleTreeQuery;
66 use fn crate::hir::module::imp::module_tree;
67 }
68}
69
70}
diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs
deleted file mode 100644
index a399d2a9e..000000000
--- a/crates/ra_analysis/src/hir/function/mod.rs
+++ /dev/null
@@ -1,190 +0,0 @@
1mod scope;
2
3use std::{
4 cmp::{max, min},
5 sync::Arc,
6};
7
8use ra_syntax::{
9 TextRange, TextUnit, SyntaxNodeRef,
10 ast::{self, AstNode, DocCommentsOwner, NameOwner},
11};
12
13use crate::{
14 hir::{FnId, HirDatabase, SourceItemId},
15 FileId,
16};
17
18pub(crate) use self::scope::FnScopes;
19
20impl FnId {
21 pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
22 let file_items = db.file_items(file_id);
23 let item_id = file_items.id_of(fn_def.syntax());
24 let item_id = SourceItemId { file_id, item_id };
25 FnId::from_loc(db, &item_id)
26 }
27}
28
29pub(crate) struct Function {
30 fn_id: FnId,
31}
32
33impl Function {
34 pub(crate) fn guess_from_source(
35 db: &impl HirDatabase,
36 file_id: FileId,
37 fn_def: ast::FnDef,
38 ) -> Function {
39 let fn_id = FnId::get(db, file_id, fn_def);
40 Function { fn_id }
41 }
42
43 pub(crate) fn guess_for_name_ref(
44 db: &impl HirDatabase,
45 file_id: FileId,
46 name_ref: ast::NameRef,
47 ) -> Option<Function> {
48 Function::guess_for_node(db, file_id, name_ref.syntax())
49 }
50
51 pub(crate) fn guess_for_bind_pat(
52 db: &impl HirDatabase,
53 file_id: FileId,
54 bind_pat: ast::BindPat,
55 ) -> Option<Function> {
56 Function::guess_for_node(db, file_id, bind_pat.syntax())
57 }
58
59 fn guess_for_node(
60 db: &impl HirDatabase,
61 file_id: FileId,
62 node: SyntaxNodeRef,
63 ) -> Option<Function> {
64 let fn_def = node.ancestors().find_map(ast::FnDef::cast)?;
65 let res = Function::guess_from_source(db, file_id, fn_def);
66 Some(res)
67 }
68
69 pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> {
70 db.fn_scopes(self.fn_id)
71 }
72
73 pub(crate) fn signature_info(&self, db: &impl HirDatabase) -> Option<FnSignatureInfo> {
74 let syntax = db.fn_syntax(self.fn_id);
75 FnSignatureInfo::new(syntax.borrowed())
76 }
77}
78
79#[derive(Debug, Clone)]
80pub struct FnSignatureInfo {
81 pub name: String,
82 pub label: String,
83 pub ret_type: Option<String>,
84 pub params: Vec<String>,
85 pub doc: Option<String>,
86}
87
88impl FnSignatureInfo {
89 fn new(node: ast::FnDef) -> Option<Self> {
90 let name = node.name()?.text().to_string();
91
92 let mut doc = None;
93
94 // Strip the body out for the label.
95 let mut label: String = if let Some(body) = node.body() {
96 let body_range = body.syntax().range();
97 let label: String = node
98 .syntax()
99 .children()
100 .filter(|child| !child.range().is_subrange(&body_range))
101 .map(|node| node.text().to_string())
102 .collect();
103 label
104 } else {
105 node.syntax().text().to_string()
106 };
107
108 if let Some((comment_range, docs)) = FnSignatureInfo::extract_doc_comments(node) {
109 let comment_range = comment_range
110 .checked_sub(node.syntax().range().start())
111 .unwrap();
112 let start = comment_range.start().to_usize();
113 let end = comment_range.end().to_usize();
114
115 // Remove the comment from the label
116 label.replace_range(start..end, "");
117
118 // Massage markdown
119 let mut processed_lines = Vec::new();
120 let mut in_code_block = false;
121 for line in docs.lines() {
122 if line.starts_with("```") {
123 in_code_block = !in_code_block;
124 }
125
126 let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
127 "```rust".into()
128 } else {
129 line.to_string()
130 };
131
132 processed_lines.push(line);
133 }
134
135 if !processed_lines.is_empty() {
136 doc = Some(processed_lines.join("\n"));
137 }
138 }
139
140 let params = FnSignatureInfo::param_list(node);
141 let ret_type = node.ret_type().map(|r| r.syntax().text().to_string());
142
143 Some(FnSignatureInfo {
144 name,
145 ret_type,
146 params,
147 label: label.trim().to_owned(),
148 doc,
149 })
150 }
151
152 fn extract_doc_comments(node: ast::FnDef) -> Option<(TextRange, String)> {
153 if node.doc_comments().count() == 0 {
154 return None;
155 }
156
157 let comment_text = node.doc_comment_text();
158
159 let (begin, end) = node
160 .doc_comments()
161 .map(|comment| comment.syntax().range())
162 .map(|range| (range.start().to_usize(), range.end().to_usize()))
163 .fold((std::usize::MAX, std::usize::MIN), |acc, range| {
164 (min(acc.0, range.0), max(acc.1, range.1))
165 });
166
167 let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end));
168
169 Some((range, comment_text))
170 }
171
172 fn param_list(node: ast::FnDef) -> Vec<String> {
173 let mut res = vec![];
174 if let Some(param_list) = node.param_list() {
175 if let Some(self_param) = param_list.self_param() {
176 res.push(self_param.syntax().text().to_string())
177 }
178
179 // Maybe use param.pat here? See if we can just extract the name?
180 //res.extend(param_list.params().map(|p| p.syntax().text().to_string()));
181 res.extend(
182 param_list
183 .params()
184 .filter_map(|p| p.pat())
185 .map(|pat| pat.syntax().text().to_string()),
186 );
187 }
188 res
189 }
190}
diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs
deleted file mode 100644
index ed789fede..000000000
--- a/crates/ra_analysis/src/hir/function/scope.rs
+++ /dev/null
@@ -1,451 +0,0 @@
1use rustc_hash::{FxHashMap, FxHashSet};
2
3use ra_syntax::{
4 AstNode, SmolStr, SyntaxNodeRef, TextRange,
5 algo::generate,
6 ast::{self, ArgListOwner, LoopBodyOwner, NameOwner},
7};
8use ra_db::LocalSyntaxPtr;
9
10use crate::{
11
12 arena::{Arena, Id},
13};
14
15pub(crate) type ScopeId = Id<ScopeData>;
16
17#[derive(Debug, PartialEq, Eq)]
18pub struct FnScopes {
19 pub(crate) self_param: Option<LocalSyntaxPtr>,
20 scopes: Arena<ScopeData>,
21 scope_for: FxHashMap<LocalSyntaxPtr, ScopeId>,
22}
23
24#[derive(Debug, PartialEq, Eq)]
25pub struct ScopeEntry {
26 name: SmolStr,
27 ptr: LocalSyntaxPtr,
28}
29
30#[derive(Debug, PartialEq, Eq)]
31pub(crate) struct ScopeData {
32 parent: Option<ScopeId>,
33 entries: Vec<ScopeEntry>,
34}
35
36impl FnScopes {
37 pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes {
38 let mut scopes = FnScopes {
39 self_param: fn_def
40 .param_list()
41 .and_then(|it| it.self_param())
42 .map(|it| LocalSyntaxPtr::new(it.syntax())),
43 scopes: Arena::default(),
44 scope_for: FxHashMap::default(),
45 };
46 let root = scopes.root_scope();
47 scopes.add_params_bindings(root, fn_def.param_list());
48 if let Some(body) = fn_def.body() {
49 compute_block_scopes(body, &mut scopes, root)
50 }
51 scopes
52 }
53 pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
54 &self.scopes[scope].entries
55 }
56 pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item = ScopeId> + 'a {
57 generate(self.scope_for(node), move |&scope| {
58 self.scopes[scope].parent
59 })
60 }
61 pub(crate) fn resolve_local_name<'a>(
62 &'a self,
63 name_ref: ast::NameRef,
64 ) -> Option<&'a ScopeEntry> {
65 let mut shadowed = FxHashSet::default();
66 let ret = self
67 .scope_chain(name_ref.syntax())
68 .flat_map(|scope| self.entries(scope).iter())
69 .filter(|entry| shadowed.insert(entry.name()))
70 .filter(|entry| entry.name() == &name_ref.text())
71 .nth(0);
72 ret
73 }
74
75 pub fn find_all_refs(&self, pat: ast::BindPat) -> Vec<ReferenceDescriptor> {
76 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
77 let name_ptr = LocalSyntaxPtr::new(pat.syntax());
78 let refs: Vec<_> = fn_def
79 .syntax()
80 .descendants()
81 .filter_map(ast::NameRef::cast)
82 .filter(|name_ref| match self.resolve_local_name(*name_ref) {
83 None => false,
84 Some(entry) => entry.ptr() == name_ptr,
85 })
86 .map(|name_ref| ReferenceDescriptor {
87 name: name_ref.syntax().text().to_string(),
88 range: name_ref.syntax().range(),
89 })
90 .collect();
91
92 refs
93 }
94
95 fn root_scope(&mut self) -> ScopeId {
96 self.scopes.alloc(ScopeData {
97 parent: None,
98 entries: vec![],
99 })
100 }
101 fn new_scope(&mut self, parent: ScopeId) -> ScopeId {
102 self.scopes.alloc(ScopeData {
103 parent: Some(parent),
104 entries: vec![],
105 })
106 }
107 fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) {
108 let entries = pat
109 .syntax()
110 .descendants()
111 .filter_map(ast::BindPat::cast)
112 .filter_map(ScopeEntry::new);
113 self.scopes[scope].entries.extend(entries);
114 }
115 fn add_params_bindings(&mut self, scope: ScopeId, params: Option<ast::ParamList>) {
116 params
117 .into_iter()
118 .flat_map(|it| it.params())
119 .filter_map(|it| it.pat())
120 .for_each(|it| self.add_bindings(scope, it));
121 }
122 fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) {
123 self.scope_for.insert(LocalSyntaxPtr::new(node), scope);
124 }
125 fn scope_for(&self, node: SyntaxNodeRef) -> Option<ScopeId> {
126 node.ancestors()
127 .map(LocalSyntaxPtr::new)
128 .filter_map(|it| self.scope_for.get(&it).map(|&scope| scope))
129 .next()
130 }
131}
132
133impl ScopeEntry {
134 fn new(pat: ast::BindPat) -> Option<ScopeEntry> {
135 let name = pat.name()?;
136 let res = ScopeEntry {
137 name: name.text(),
138 ptr: LocalSyntaxPtr::new(pat.syntax()),
139 };
140 Some(res)
141 }
142 pub(crate) fn name(&self) -> &SmolStr {
143 &self.name
144 }
145 pub(crate) fn ptr(&self) -> LocalSyntaxPtr {
146 self.ptr
147 }
148}
149
150fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) {
151 for stmt in block.statements() {
152 match stmt {
153 ast::Stmt::LetStmt(stmt) => {
154 if let Some(expr) = stmt.initializer() {
155 scopes.set_scope(expr.syntax(), scope);
156 compute_expr_scopes(expr, scopes, scope);
157 }
158 scope = scopes.new_scope(scope);
159 if let Some(pat) = stmt.pat() {
160 scopes.add_bindings(scope, pat);
161 }
162 }
163 ast::Stmt::ExprStmt(expr_stmt) => {
164 if let Some(expr) = expr_stmt.expr() {
165 scopes.set_scope(expr.syntax(), scope);
166 compute_expr_scopes(expr, scopes, scope);
167 }
168 }
169 }
170 }
171 if let Some(expr) = block.expr() {
172 scopes.set_scope(expr.syntax(), scope);
173 compute_expr_scopes(expr, scopes, scope);
174 }
175}
176
177fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) {
178 match expr {
179 ast::Expr::IfExpr(e) => {
180 let cond_scope = e
181 .condition()
182 .and_then(|cond| compute_cond_scopes(cond, scopes, scope));
183 if let Some(block) = e.then_branch() {
184 compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope));
185 }
186 if let Some(block) = e.else_branch() {
187 compute_block_scopes(block, scopes, scope);
188 }
189 }
190 ast::Expr::BlockExpr(e) => {
191 if let Some(block) = e.block() {
192 compute_block_scopes(block, scopes, scope);
193 }
194 }
195 ast::Expr::LoopExpr(e) => {
196 if let Some(block) = e.loop_body() {
197 compute_block_scopes(block, scopes, scope);
198 }
199 }
200 ast::Expr::WhileExpr(e) => {
201 let cond_scope = e
202 .condition()
203 .and_then(|cond| compute_cond_scopes(cond, scopes, scope));
204 if let Some(block) = e.loop_body() {
205 compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope));
206 }
207 }
208 ast::Expr::ForExpr(e) => {
209 if let Some(expr) = e.iterable() {
210 compute_expr_scopes(expr, scopes, scope);
211 }
212 let mut scope = scope;
213 if let Some(pat) = e.pat() {
214 scope = scopes.new_scope(scope);
215 scopes.add_bindings(scope, pat);
216 }
217 if let Some(block) = e.loop_body() {
218 compute_block_scopes(block, scopes, scope);
219 }
220 }
221 ast::Expr::LambdaExpr(e) => {
222 let scope = scopes.new_scope(scope);
223 scopes.add_params_bindings(scope, e.param_list());
224 if let Some(body) = e.body() {
225 scopes.set_scope(body.syntax(), scope);
226 compute_expr_scopes(body, scopes, scope);
227 }
228 }
229 ast::Expr::CallExpr(e) => {
230 compute_call_scopes(e.expr(), e.arg_list(), scopes, scope);
231 }
232 ast::Expr::MethodCallExpr(e) => {
233 compute_call_scopes(e.expr(), e.arg_list(), scopes, scope);
234 }
235 ast::Expr::MatchExpr(e) => {
236 if let Some(expr) = e.expr() {
237 compute_expr_scopes(expr, scopes, scope);
238 }
239 for arm in e.match_arm_list().into_iter().flat_map(|it| it.arms()) {
240 let scope = scopes.new_scope(scope);
241 for pat in arm.pats() {
242 scopes.add_bindings(scope, pat);
243 }
244 if let Some(expr) = arm.expr() {
245 compute_expr_scopes(expr, scopes, scope);
246 }
247 }
248 }
249 _ => expr
250 .syntax()
251 .children()
252 .filter_map(ast::Expr::cast)
253 .for_each(|expr| compute_expr_scopes(expr, scopes, scope)),
254 };
255
256 fn compute_call_scopes(
257 receiver: Option<ast::Expr>,
258 arg_list: Option<ast::ArgList>,
259 scopes: &mut FnScopes,
260 scope: ScopeId,
261 ) {
262 arg_list
263 .into_iter()
264 .flat_map(|it| it.args())
265 .chain(receiver)
266 .for_each(|expr| compute_expr_scopes(expr, scopes, scope));
267 }
268
269 fn compute_cond_scopes(
270 cond: ast::Condition,
271 scopes: &mut FnScopes,
272 scope: ScopeId,
273 ) -> Option<ScopeId> {
274 if let Some(expr) = cond.expr() {
275 compute_expr_scopes(expr, scopes, scope);
276 }
277 if let Some(pat) = cond.pat() {
278 let s = scopes.new_scope(scope);
279 scopes.add_bindings(s, pat);
280 Some(s)
281 } else {
282 None
283 }
284 }
285}
286
287#[derive(Debug)]
288pub struct ReferenceDescriptor {
289 pub range: TextRange,
290 pub name: String,
291}
292
293#[cfg(test)]
294mod tests {
295 use ra_editor::find_node_at_offset;
296 use ra_syntax::SourceFileNode;
297 use test_utils::extract_offset;
298
299 use super::*;
300
301 fn do_check(code: &str, expected: &[&str]) {
302 let (off, code) = extract_offset(code);
303 let code = {
304 let mut buf = String::new();
305 let off = u32::from(off) as usize;
306 buf.push_str(&code[..off]);
307 buf.push_str("marker");
308 buf.push_str(&code[off..]);
309 buf
310 };
311 let file = SourceFileNode::parse(&code);
312 let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
313 let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
314 let scopes = FnScopes::new(fn_def);
315 let actual = scopes
316 .scope_chain(marker.syntax())
317 .flat_map(|scope| scopes.entries(scope))
318 .map(|it| it.name())
319 .collect::<Vec<_>>();
320 assert_eq!(actual.as_slice(), expected);
321 }
322
323 #[test]
324 fn test_lambda_scope() {
325 do_check(
326 r"
327 fn quux(foo: i32) {
328 let f = |bar, baz: i32| {
329 <|>
330 };
331 }",
332 &["bar", "baz", "foo"],
333 );
334 }
335
336 #[test]
337 fn test_call_scope() {
338 do_check(
339 r"
340 fn quux() {
341 f(|x| <|> );
342 }",
343 &["x"],
344 );
345 }
346
347 #[test]
348 fn test_metod_call_scope() {
349 do_check(
350 r"
351 fn quux() {
352 z.f(|x| <|> );
353 }",
354 &["x"],
355 );
356 }
357
358 #[test]
359 fn test_loop_scope() {
360 do_check(
361 r"
362 fn quux() {
363 loop {
364 let x = ();
365 <|>
366 };
367 }",
368 &["x"],
369 );
370 }
371
372 #[test]
373 fn test_match() {
374 do_check(
375 r"
376 fn quux() {
377 match () {
378 Some(x) => {
379 <|>
380 }
381 };
382 }",
383 &["x"],
384 );
385 }
386
387 #[test]
388 fn test_shadow_variable() {
389 do_check(
390 r"
391 fn foo(x: String) {
392 let x : &str = &x<|>;
393 }",
394 &["x"],
395 );
396 }
397
398 fn do_check_local_name(code: &str, expected_offset: u32) {
399 let (off, code) = extract_offset(code);
400 let file = SourceFileNode::parse(&code);
401 let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
402 let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
403
404 let scopes = FnScopes::new(fn_def);
405
406 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap();
407 let local_name = local_name_entry.ptr().resolve(&file);
408 let expected_name =
409 find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()).unwrap();
410 assert_eq!(local_name.range(), expected_name.syntax().range());
411 }
412
413 #[test]
414 fn test_resolve_local_name() {
415 do_check_local_name(
416 r#"
417 fn foo(x: i32, y: u32) {
418 {
419 let z = x * 2;
420 }
421 {
422 let t = x<|> * 3;
423 }
424 }"#,
425 21,
426 );
427 }
428
429 #[test]
430 fn test_resolve_local_name_declaration() {
431 do_check_local_name(
432 r#"
433 fn foo(x: String) {
434 let x : &str = &x<|>;
435 }"#,
436 21,
437 );
438 }
439
440 #[test]
441 fn test_resolve_local_name_shadow() {
442 do_check_local_name(
443 r"
444 fn foo(x: String) {
445 let x : &str = &x;
446 x<|>
447 }",
448 46,
449 );
450 }
451}
diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs
deleted file mode 100644
index 83131384d..000000000
--- a/crates/ra_analysis/src/hir/mod.rs
+++ /dev/null
@@ -1,131 +0,0 @@
1//! HIR (previsouly known as descriptors) provides a high-level OO acess to Rust
2//! code.
3//!
4//! The principal difference between HIR and syntax trees is that HIR is bound
5//! to a particular crate instance. That is, it has cfg flags and features
6//! applied. So, there relation between syntax and HIR is many-to-one.
7
8pub(crate) mod db;
9mod query_definitions;
10mod function;
11mod module;
12mod path;
13
14use std::ops::Index;
15
16use ra_syntax::{SyntaxNodeRef, SyntaxNode};
17use ra_db::{LocationIntener, SourceRootId};
18
19use crate::{
20 FileId,
21 hir::db::HirDatabase,
22 Cancelable,
23 arena::{Arena, Id},
24};
25
26pub(crate) use self::{
27 path::{Path, PathKind},
28 module::{Module, ModuleId, Problem},
29 function::{Function, FnScopes},
30};
31
32pub use self::function::FnSignatureInfo;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35pub(crate) struct FnId(u32);
36ra_db::impl_numeric_id!(FnId);
37
38impl FnId {
39 pub(crate) fn from_loc(
40 db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
41 loc: &SourceItemId,
42 ) -> FnId {
43 db.as_ref().loc2id(loc)
44 }
45 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
46 db.as_ref().id2loc(self)
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
51pub(crate) struct DefId(u32);
52ra_db::impl_numeric_id!(DefId);
53
54#[derive(Clone, Debug, PartialEq, Eq, Hash)]
55pub(crate) enum DefLoc {
56 Module {
57 id: ModuleId,
58 source_root: SourceRootId,
59 },
60 Item {
61 source_item_id: SourceItemId,
62 },
63}
64
65impl DefId {
66 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
67 db.as_ref().id2loc(self)
68 }
69}
70
71impl DefLoc {
72 pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
73 db.as_ref().loc2id(&self)
74 }
75}
76
77pub(crate) enum Def {
78 Module(Module),
79 Item,
80}
81
82impl DefId {
83 pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
84 let loc = self.loc(db);
85 let res = match loc {
86 DefLoc::Module { id, source_root } => {
87 let descr = Module::new(db, source_root, id)?;
88 Def::Module(descr)
89 }
90 DefLoc::Item { .. } => Def::Item,
91 };
92 Ok(res)
93 }
94}
95
96/// Identifier of item within a specific file. This is stable over reparses, so
97/// it's OK to use it as a salsa key/value.
98pub(crate) type SourceFileItemId = Id<SyntaxNode>;
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
101pub(crate) struct SourceItemId {
102 file_id: FileId,
103 item_id: SourceFileItemId,
104}
105
106/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
107#[derive(Debug, PartialEq, Eq, Default)]
108pub(crate) struct SourceFileItems {
109 arena: Arena<SyntaxNode>,
110}
111
112impl SourceFileItems {
113 fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
114 self.arena.alloc(item)
115 }
116 fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId {
117 let (id, _item) = self
118 .arena
119 .iter()
120 .find(|(_id, i)| i.borrowed() == item)
121 .unwrap();
122 id
123 }
124}
125
126impl Index<SourceFileItemId> for SourceFileItems {
127 type Output = SyntaxNode;
128 fn index(&self, idx: SourceFileItemId) -> &SyntaxNode {
129 &self.arena[idx]
130 }
131}
diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs
deleted file mode 100644
index c8f7ed58d..000000000
--- a/crates/ra_analysis/src/hir/module/imp.rs
+++ /dev/null
@@ -1,195 +0,0 @@
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};
10
11use crate::{
12 hir::HirDatabase,
13 Cancelable, FileId,
14};
15
16use super::{
17 LinkData, LinkId, ModuleData, ModuleId, ModuleSource,
18 ModuleTree, Problem,
19};
20
21#[derive(Clone, Hash, PartialEq, Eq, Debug)]
22pub(crate) enum Submodule {
23 Declaration(SmolStr),
24 Definition(SmolStr, ModuleSource),
25}
26
27impl Submodule {
28 fn name(&self) -> &SmolStr {
29 match self {
30 Submodule::Declaration(name) => name,
31 Submodule::Definition(name, _) => name,
32 }
33 }
34}
35
36pub(crate) fn modules<'a>(
37 root: impl ast::ModuleItemOwner<'a>,
38) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> {
39 root.items()
40 .filter_map(|item| match item {
41 ast::ModuleItem::Module(m) => Some(m),
42 _ => None,
43 })
44 .filter_map(|module| {
45 let name = module.name()?.text();
46 Some((name, module))
47 })
48}
49
50pub(crate) fn module_tree(
51 db: &impl HirDatabase,
52 source_root: SourceRootId,
53) -> Cancelable<Arc<ModuleTree>> {
54 db.check_canceled()?;
55 let res = create_module_tree(db, source_root)?;
56 Ok(Arc::new(res))
57}
58
59fn create_module_tree<'a>(
60 db: &impl HirDatabase,
61 source_root: SourceRootId,
62) -> Cancelable<ModuleTree> {
63 let mut tree = ModuleTree::default();
64
65 let mut roots = FxHashMap::default();
66 let mut visited = FxHashSet::default();
67
68 let source_root = db.source_root(source_root);
69 for &file_id in source_root.files.iter() {
70 let source = ModuleSource::SourceFile(file_id);
71 if visited.contains(&source) {
72 continue; // TODO: use explicit crate_roots here
73 }
74 assert!(!roots.contains_key(&file_id));
75 let module_id = build_subtree(
76 db,
77 &source_root,
78 &mut tree,
79 &mut visited,
80 &mut roots,
81 None,
82 source,
83 )?;
84 roots.insert(file_id, module_id);
85 }
86 Ok(tree)
87}
88
89fn build_subtree(
90 db: &impl HirDatabase,
91 source_root: &SourceRoot,
92 tree: &mut ModuleTree,
93 visited: &mut FxHashSet<ModuleSource>,
94 roots: &mut FxHashMap<FileId, ModuleId>,
95 parent: Option<LinkId>,
96 source: ModuleSource,
97) -> Cancelable<ModuleId> {
98 visited.insert(source);
99 let id = tree.push_mod(ModuleData {
100 source,
101 parent,
102 children: Vec::new(),
103 });
104 for sub in db.submodules(source)?.iter() {
105 let link = tree.push_link(LinkData {
106 name: sub.name().clone(),
107 owner: id,
108 points_to: Vec::new(),
109 problem: None,
110 });
111
112 let (points_to, problem) = match sub {
113 Submodule::Declaration(name) => {
114 let (points_to, problem) =
115 resolve_submodule(source, &name, &source_root.file_resolver);
116 let points_to = points_to
117 .into_iter()
118 .map(|file_id| match roots.remove(&file_id) {
119 Some(module_id) => {
120 tree.mods[module_id].parent = Some(link);
121 Ok(module_id)
122 }
123 None => build_subtree(
124 db,
125 source_root,
126 tree,
127 visited,
128 roots,
129 Some(link),
130 ModuleSource::SourceFile(file_id),
131 ),
132 })
133 .collect::<Cancelable<Vec<_>>>()?;
134 (points_to, problem)
135 }
136 Submodule::Definition(_name, submodule_source) => {
137 let points_to = build_subtree(
138 db,
139 source_root,
140 tree,
141 visited,
142 roots,
143 Some(link),
144 *submodule_source,
145 )?;
146 (vec![points_to], None)
147 }
148 };
149
150 tree.links[link].points_to = points_to;
151 tree.links[link].problem = problem;
152 }
153 Ok(id)
154}
155
156fn resolve_submodule(
157 source: ModuleSource,
158 name: &SmolStr,
159 file_resolver: &FileResolverImp,
160) -> (Vec<FileId>, Option<Problem>) {
161 let file_id = match source {
162 ModuleSource::SourceFile(it) => it,
163 ModuleSource::Module(..) => {
164 // TODO
165 return (Vec::new(), None);
166 }
167 };
168 let mod_name = file_resolver.file_stem(file_id);
169 let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main";
170
171 let file_mod = RelativePathBuf::from(format!("../{}.rs", name));
172 let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name));
173 let points_to: Vec<FileId>;
174 let problem: Option<Problem>;
175 if is_dir_owner {
176 points_to = [&file_mod, &dir_mod]
177 .iter()
178 .filter_map(|path| file_resolver.resolve(file_id, path))
179 .collect();
180 problem = if points_to.is_empty() {
181 Some(Problem::UnresolvedModule {
182 candidate: file_mod,
183 })
184 } else {
185 None
186 }
187 } else {
188 points_to = Vec::new();
189 problem = Some(Problem::NotDirOwner {
190 move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)),
191 candidate: file_mod,
192 });
193 }
194 (points_to, problem)
195}
diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs
deleted file mode 100644
index d2096b01e..000000000
--- a/crates/ra_analysis/src/hir/module/mod.rs
+++ /dev/null
@@ -1,379 +0,0 @@
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;
14use relative_path::RelativePathBuf;
15
16use crate::{
17 FileId, FilePosition, Cancelable,
18 hir::{DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId},
19 arena::{Arena, Id},
20};
21
22pub(crate) use self::nameres::ModuleScope;
23
24/// `Module` is API entry point to get all the information
25/// about a particular module.
26#[derive(Debug, Clone)]
27pub(crate) struct Module {
28 tree: Arc<ModuleTree>,
29 source_root_id: SourceRootId,
30 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(crate) 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(crate) fn resolve_path(
156 &self,
157 db: &impl HirDatabase,
158 path: Path,
159 ) -> Cancelable<Option<DefId>> {
160 let mut curr = match path.kind {
161 PathKind::Crate => self.crate_root(),
162 PathKind::Self_ | PathKind::Plain => self.clone(),
163 PathKind::Super => ctry!(self.parent()),
164 }
165 .def_id(db);
166
167 let segments = path.segments;
168 for name in segments.iter() {
169 let module = match curr.loc(db) {
170 DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?,
171 _ => return Ok(None),
172 };
173 let scope = module.scope(db)?;
174 curr = ctry!(ctry!(scope.get(&name)).def_id);
175 }
176 Ok(Some(curr))
177 }
178
179 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
180 self.module_id.problems(&self.tree, db)
181 }
182}
183
184/// Phisically, rust source is organized as a set of files, but logically it is
185/// organized as a tree of modules. Usually, a single file corresponds to a
186/// single module, but it is not nessary the case.
187///
188/// Module encapsulate the logic of transitioning from the fuzzy world of files
189/// (which can have multiple parents) to the precise world of modules (which
190/// always have one parent).
191#[derive(Default, Debug, PartialEq, Eq)]
192pub(crate) struct ModuleTree {
193 mods: Arena<ModuleData>,
194 links: Arena<LinkData>,
195}
196
197impl ModuleTree {
198 pub(in crate::hir) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a {
199 self.mods.iter().map(|(id, _)| id)
200 }
201
202 fn modules_for_source(&self, source: ModuleSource) -> Vec<ModuleId> {
203 self.mods
204 .iter()
205 .filter(|(_idx, it)| it.source == source)
206 .map(|(idx, _)| idx)
207 .collect()
208 }
209
210 fn any_module_for_source(&self, source: ModuleSource) -> Option<ModuleId> {
211 self.modules_for_source(source).pop()
212 }
213}
214
215/// `ModuleSource` is the syntax tree element that produced this module:
216/// either a file, or an inlinde module.
217#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
218pub(crate) enum ModuleSource {
219 SourceFile(FileId),
220 Module(SourceItemId),
221}
222
223/// An owned syntax node for a module. Unlike `ModuleSource`,
224/// this holds onto the AST for the whole file.
225pub(crate) enum ModuleSourceNode {
226 SourceFile(ast::SourceFileNode),
227 Module(ast::ModuleNode),
228}
229
230pub(crate) type ModuleId = Id<ModuleData>;
231type LinkId = Id<LinkData>;
232
233#[derive(Clone, Debug, Hash, PartialEq, Eq)]
234pub enum Problem {
235 UnresolvedModule {
236 candidate: RelativePathBuf,
237 },
238 NotDirOwner {
239 move_to: RelativePathBuf,
240 candidate: RelativePathBuf,
241 },
242}
243
244impl ModuleId {
245 pub(in crate::hir) fn source(self, tree: &ModuleTree) -> ModuleSource {
246 tree.mods[self].source
247 }
248 fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
249 tree.mods[self].parent
250 }
251 fn parent(self, tree: &ModuleTree) -> Option<ModuleId> {
252 let link = self.parent_link(tree)?;
253 Some(tree.links[link].owner)
254 }
255 fn crate_root(self, tree: &ModuleTree) -> ModuleId {
256 generate(Some(self), move |it| it.parent(tree))
257 .last()
258 .unwrap()
259 }
260 fn child(self, tree: &ModuleTree, name: &str) -> Option<ModuleId> {
261 let link = tree.mods[self]
262 .children
263 .iter()
264 .map(|&it| &tree.links[it])
265 .find(|it| it.name == name)?;
266 Some(*link.points_to.first()?)
267 }
268 fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (SmolStr, ModuleId)> + 'a {
269 tree.mods[self].children.iter().filter_map(move |&it| {
270 let link = &tree.links[it];
271 let module = *link.points_to.first()?;
272 Some((link.name.clone(), module))
273 })
274 }
275 fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
276 tree.mods[self]
277 .children
278 .iter()
279 .filter_map(|&it| {
280 let p = tree.links[it].problem.clone()?;
281 let s = it.bind_source(tree, db);
282 let s = s.borrowed().name().unwrap().syntax().owned();
283 Some((s, p))
284 })
285 .collect()
286 }
287}
288
289impl LinkId {
290 fn owner(self, tree: &ModuleTree) -> ModuleId {
291 tree.links[self].owner
292 }
293 fn name(self, tree: &ModuleTree) -> SmolStr {
294 tree.links[self].name.clone()
295 }
296 fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode {
297 let owner = self.owner(tree);
298 match owner.source(tree).resolve(db) {
299 ModuleSourceNode::SourceFile(root) => {
300 let ast = imp::modules(root.borrowed())
301 .find(|(name, _)| name == &tree.links[self].name)
302 .unwrap()
303 .1;
304 ast.owned()
305 }
306 ModuleSourceNode::Module(it) => it,
307 }
308 }
309}
310
311#[derive(Debug, PartialEq, Eq, Hash)]
312pub(crate) struct ModuleData {
313 source: ModuleSource,
314 parent: Option<LinkId>,
315 children: Vec<LinkId>,
316}
317
318impl ModuleSource {
319 pub(crate) fn new_inline(
320 db: &impl HirDatabase,
321 file_id: FileId,
322 module: ast::Module,
323 ) -> ModuleSource {
324 assert!(!module.has_semi());
325 let items = db.file_items(file_id);
326 let item_id = items.id_of(module.syntax());
327 let id = SourceItemId { file_id, item_id };
328 ModuleSource::Module(id)
329 }
330
331 pub(crate) fn as_file(self) -> Option<FileId> {
332 match self {
333 ModuleSource::SourceFile(f) => Some(f),
334 ModuleSource::Module(..) => None,
335 }
336 }
337
338 pub(crate) fn file_id(self) -> FileId {
339 match self {
340 ModuleSource::SourceFile(f) => f,
341 ModuleSource::Module(source_item_id) => source_item_id.file_id,
342 }
343 }
344
345 pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
346 match self {
347 ModuleSource::SourceFile(file_id) => {
348 let syntax = db.source_file(file_id);
349 ModuleSourceNode::SourceFile(syntax.ast().owned())
350 }
351 ModuleSource::Module(item_id) => {
352 let syntax = db.file_item(item_id);
353 let syntax = syntax.borrowed();
354 let module = ast::Module::cast(syntax).unwrap();
355 ModuleSourceNode::Module(module.owned())
356 }
357 }
358 }
359}
360
361#[derive(Hash, Debug, PartialEq, Eq)]
362struct LinkData {
363 owner: ModuleId,
364 name: SmolStr,
365 points_to: Vec<ModuleId>,
366 problem: Option<Problem>,
367}
368
369impl ModuleTree {
370 fn push_mod(&mut self, data: ModuleData) -> ModuleId {
371 self.mods.alloc(data)
372 }
373 fn push_link(&mut self, data: LinkData) -> LinkId {
374 let owner = data.owner;
375 let id = self.links.alloc(data);
376 self.mods[owner].children.push(id);
377 id
378 }
379}
diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs
deleted file mode 100644
index d4ecc010b..000000000
--- a/crates/ra_analysis/src/hir/module/nameres.rs
+++ /dev/null
@@ -1,446 +0,0 @@
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 hir::{
32 DefId, DefLoc,
33 SourceItemId, SourceFileItemId, SourceFileItems,
34 Path, PathKind,
35 HirDatabase,
36 module::{ModuleId, ModuleTree},
37 },
38};
39
40/// Item map is the result of the name resolution. Item map contains, for each
41/// module, the set of visible items.
42#[derive(Default, Debug, PartialEq, Eq)]
43pub(crate) struct ItemMap {
44 pub(crate) per_module: FxHashMap<ModuleId, ModuleScope>,
45}
46
47#[derive(Debug, Default, PartialEq, Eq, Clone)]
48pub(crate) struct ModuleScope {
49 items: FxHashMap<SmolStr, Resolution>,
50}
51
52impl ModuleScope {
53 pub(crate) fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a SmolStr, &Resolution)> + 'a {
54 self.items.iter()
55 }
56 pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> {
57 self.items.get(name)
58 }
59}
60
61/// A set of items and imports declared inside a module, without relation to
62/// other modules.
63///
64/// This stands in-between raw syntax and name resolution and alow us to avoid
65/// recomputing name res: if `InputModuleItems` are the same, we can avoid
66/// running name resolution.
67#[derive(Debug, Default, PartialEq, Eq)]
68pub(crate) struct InputModuleItems {
69 items: Vec<ModuleItem>,
70 imports: Vec<Import>,
71}
72
73#[derive(Debug, PartialEq, Eq)]
74struct ModuleItem {
75 id: SourceFileItemId,
76 name: SmolStr,
77 kind: SyntaxKind,
78 vis: Vis,
79}
80
81#[derive(Debug, PartialEq, Eq)]
82enum Vis {
83 // Priv,
84 Other,
85}
86
87#[derive(Debug, Clone, PartialEq, Eq)]
88struct Import {
89 path: Path,
90 kind: ImportKind,
91}
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub(crate) struct NamedImport {
95 file_item_id: SourceFileItemId,
96 relative_range: TextRange,
97}
98
99impl NamedImport {
100 pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange {
101 let source_item_id = SourceItemId {
102 file_id,
103 item_id: self.file_item_id,
104 };
105 let syntax = db.file_item(source_item_id);
106 let offset = syntax.borrowed().range().start();
107 self.relative_range + offset
108 }
109}
110
111#[derive(Debug, Clone, PartialEq, Eq)]
112enum ImportKind {
113 Glob,
114 Named(NamedImport),
115}
116
117/// Resolution is basically `DefId` atm, but it should account for stuff like
118/// multiple namespaces, ambiguity and errors.
119#[derive(Debug, Clone, PartialEq, Eq)]
120pub(crate) struct Resolution {
121 /// None for unresolved
122 pub(crate) def_id: Option<DefId>,
123 /// ident by whitch this is imported into local scope.
124 pub(crate) import: Option<NamedImport>,
125}
126
127// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
128// enum Namespace {
129// Types,
130// Values,
131// }
132
133// #[derive(Debug)]
134// struct PerNs<T> {
135// types: Option<T>,
136// values: Option<T>,
137// }
138
139impl InputModuleItems {
140 pub(in crate::hir) fn new<'a>(
141 file_items: &SourceFileItems,
142 items: impl Iterator<Item = ast::ModuleItem<'a>>,
143 ) -> InputModuleItems {
144 let mut res = InputModuleItems::default();
145 for item in items {
146 res.add_item(file_items, item);
147 }
148 res
149 }
150
151 fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> {
152 match item {
153 ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
154 ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
155 ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
156 ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
157 ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
158 ast::ModuleItem::ImplItem(_) => {
159 // impls don't define items
160 }
161 ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it),
162 ast::ModuleItem::ExternCrateItem(_) => {
163 // TODO
164 }
165 ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
166 ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
167 ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?),
168 }
169 Some(())
170 }
171
172 fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
173 let file_item_id = file_items.id_of(item.syntax());
174 let start_offset = item.syntax().range().start();
175 Path::expand_use_item(item, |path, range| {
176 let kind = match range {
177 None => ImportKind::Glob,
178 Some(range) => ImportKind::Named(NamedImport {
179 file_item_id,
180 relative_range: range - start_offset,
181 }),
182 };
183 self.imports.push(Import { kind, path })
184 })
185 }
186}
187
188impl ModuleItem {
189 fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> {
190 let name = item.name()?.text();
191 let kind = item.syntax().kind();
192 let vis = Vis::Other;
193 let id = file_items.id_of(item.syntax());
194 let res = ModuleItem {
195 id,
196 name,
197 kind,
198 vis,
199 };
200 Some(res)
201 }
202}
203
204pub(in crate::hir) struct Resolver<'a, DB> {
205 pub db: &'a DB,
206 pub input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>,
207 pub source_root: SourceRootId,
208 pub module_tree: Arc<ModuleTree>,
209 pub result: ItemMap,
210}
211
212impl<'a, DB> Resolver<'a, DB>
213where
214 DB: HirDatabase,
215{
216 pub(in crate::hir) fn resolve(mut self) -> Cancelable<ItemMap> {
217 for (&module_id, items) in self.input.iter() {
218 self.populate_module(module_id, items)
219 }
220
221 for &module_id in self.input.keys() {
222 self.db.check_canceled()?;
223 self.resolve_imports(module_id);
224 }
225 Ok(self.result)
226 }
227
228 fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) {
229 let file_id = module_id.source(&self.module_tree).file_id();
230
231 let mut module_items = ModuleScope::default();
232
233 for import in input.imports.iter() {
234 if let Some(name) = import.path.segments.iter().last() {
235 if let ImportKind::Named(import) = import.kind {
236 module_items.items.insert(
237 name.clone(),
238 Resolution {
239 def_id: None,
240 import: Some(import),
241 },
242 );
243 }
244 }
245 }
246
247 for item in input.items.iter() {
248 if item.kind == MODULE {
249 // handle submodules separatelly
250 continue;
251 }
252 let def_loc = DefLoc::Item {
253 source_item_id: SourceItemId {
254 file_id,
255 item_id: item.id,
256 },
257 };
258 let def_id = def_loc.id(self.db);
259 let resolution = Resolution {
260 def_id: Some(def_id),
261 import: None,
262 };
263 module_items.items.insert(item.name.clone(), resolution);
264 }
265
266 for (name, mod_id) in module_id.children(&self.module_tree) {
267 let def_loc = DefLoc::Module {
268 id: mod_id,
269 source_root: self.source_root,
270 };
271 let def_id = def_loc.id(self.db);
272 let resolution = Resolution {
273 def_id: Some(def_id),
274 import: None,
275 };
276 module_items.items.insert(name, resolution);
277 }
278
279 self.result.per_module.insert(module_id, module_items);
280 }
281
282 fn resolve_imports(&mut self, module_id: ModuleId) {
283 for import in self.input[&module_id].imports.iter() {
284 self.resolve_import(module_id, import);
285 }
286 }
287
288 fn resolve_import(&mut self, module_id: ModuleId, import: &Import) {
289 let ptr = match import.kind {
290 ImportKind::Glob => return,
291 ImportKind::Named(ptr) => ptr,
292 };
293
294 let mut curr = match import.path.kind {
295 // TODO: handle extern crates
296 PathKind::Plain => return,
297 PathKind::Self_ => module_id,
298 PathKind::Super => {
299 match module_id.parent(&self.module_tree) {
300 Some(it) => it,
301 // TODO: error
302 None => return,
303 }
304 }
305 PathKind::Crate => module_id.crate_root(&self.module_tree),
306 };
307
308 for (i, name) in import.path.segments.iter().enumerate() {
309 let is_last = i == import.path.segments.len() - 1;
310
311 let def_id = match self.result.per_module[&curr].items.get(name) {
312 None => return,
313 Some(res) => match res.def_id {
314 Some(it) => it,
315 None => return,
316 },
317 };
318
319 if !is_last {
320 curr = match def_id.loc(self.db) {
321 DefLoc::Module { id, .. } => id,
322 _ => return,
323 }
324 } else {
325 self.update(module_id, |items| {
326 let res = Resolution {
327 def_id: Some(def_id),
328 import: Some(ptr),
329 };
330 items.items.insert(name.clone(), res);
331 })
332 }
333 }
334 }
335
336 fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) {
337 let module_items = self.result.per_module.get_mut(&module_id).unwrap();
338 f(module_items)
339 }
340}
341
342#[cfg(test)]
343mod tests {
344 use ra_db::FilesDatabase;
345 use crate::{
346 AnalysisChange,
347 mock_analysis::{MockAnalysis, analysis_and_position},
348 hir::{self, HirDatabase},
349};
350 use super::*;
351
352 fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
353 let (analysis, pos) = analysis_and_position(fixture);
354 let db = analysis.imp.db;
355 let source_root = db.file_source_root(pos.file_id);
356 let descr = hir::Module::guess_from_position(&*db, pos)
357 .unwrap()
358 .unwrap();
359 let module_id = descr.module_id;
360 (db.item_map(source_root).unwrap(), module_id)
361 }
362
363 #[test]
364 fn test_item_map() {
365 let (item_map, module_id) = item_map(
366 "
367 //- /lib.rs
368 mod foo;
369
370 use crate::foo::bar::Baz;
371 <|>
372
373 //- /foo/mod.rs
374 pub mod bar;
375
376 //- /foo/bar.rs
377 pub struct Baz;
378 ",
379 );
380 let name = SmolStr::from("Baz");
381 let resolution = &item_map.per_module[&module_id].items[&name];
382 assert!(resolution.def_id.is_some());
383 }
384
385 #[test]
386 fn typing_inside_a_function_should_not_invalidate_item_map() {
387 let mock_analysis = MockAnalysis::with_files(
388 "
389 //- /lib.rs
390 mod foo;
391
392 use crate::foo::bar::Baz;
393
394 fn foo() -> i32 {
395 1 + 1
396 }
397 //- /foo/mod.rs
398 pub mod bar;
399
400 //- /foo/bar.rs
401 pub struct Baz;
402 ",
403 );
404
405 let file_id = mock_analysis.id_of("/lib.rs");
406 let mut host = mock_analysis.analysis_host();
407
408 let source_root = host.analysis().imp.db.file_source_root(file_id);
409
410 {
411 let db = host.analysis().imp.db;
412 let events = db.log_executed(|| {
413 db.item_map(source_root).unwrap();
414 });
415 assert!(format!("{:?}", events).contains("item_map"))
416 }
417
418 let mut change = AnalysisChange::new();
419
420 change.change_file(
421 file_id,
422 "
423 mod foo;
424
425 use crate::foo::bar::Baz;
426
427 fn foo() -> i32 { 92 }
428 "
429 .to_string(),
430 );
431
432 host.apply_change(change);
433
434 {
435 let db = host.analysis().imp.db;
436 let events = db.log_executed(|| {
437 db.item_map(source_root).unwrap();
438 });
439 assert!(
440 !format!("{:?}", events).contains("_item_map"),
441 "{:#?}",
442 events
443 )
444 }
445 }
446}
diff --git a/crates/ra_analysis/src/hir/path.rs b/crates/ra_analysis/src/hir/path.rs
deleted file mode 100644
index 8279daf4b..000000000
--- a/crates/ra_analysis/src/hir/path.rs
+++ /dev/null
@@ -1,148 +0,0 @@
1use ra_syntax::{SmolStr, ast, AstNode, TextRange};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub(crate) struct Path {
5 pub(crate) kind: PathKind,
6 pub(crate) segments: Vec<SmolStr>,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub(crate) enum PathKind {
11 Plain,
12 Self_,
13 Super,
14 Crate,
15}
16
17impl Path {
18 /// Calls `cb` with all paths, represented by this use item.
19 pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option<TextRange>)) {
20 if let Some(tree) = item.use_tree() {
21 expand_use_tree(None, tree, &mut cb);
22 }
23 }
24
25 /// Converts an `ast::Path` to `Path`. Works with use trees.
26 pub(crate) fn from_ast(mut path: ast::Path) -> Option<Path> {
27 let mut kind = PathKind::Plain;
28 let mut segments = Vec::new();
29 loop {
30 let segment = path.segment()?;
31 match segment.kind()? {
32 ast::PathSegmentKind::Name(name) => segments.push(name.text()),
33 ast::PathSegmentKind::CrateKw => {
34 kind = PathKind::Crate;
35 break;
36 }
37 ast::PathSegmentKind::SelfKw => {
38 kind = PathKind::Self_;
39 break;
40 }
41 ast::PathSegmentKind::SuperKw => {
42 kind = PathKind::Super;
43 break;
44 }
45 }
46 path = match qualifier(path) {
47 Some(it) => it,
48 None => break,
49 };
50 }
51 segments.reverse();
52 return Some(Path { kind, segments });
53
54 fn qualifier(path: ast::Path) -> Option<ast::Path> {
55 if let Some(q) = path.qualifier() {
56 return Some(q);
57 }
58 // TODO: this bottom up traversal is not too precise.
59 // Should we handle do a top-down analysiss, recording results?
60 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
61 let use_tree = use_tree_list.parent_use_tree();
62 use_tree.path()
63 }
64 }
65
66 /// `true` is this path is a single identifier, like `foo`
67 pub(crate) fn is_ident(&self) -> bool {
68 self.kind == PathKind::Plain && self.segments.len() == 1
69 }
70}
71
72fn expand_use_tree(
73 prefix: Option<Path>,
74 tree: ast::UseTree,
75 cb: &mut impl FnMut(Path, Option<TextRange>),
76) {
77 if let Some(use_tree_list) = tree.use_tree_list() {
78 let prefix = match tree.path() {
79 None => prefix,
80 Some(path) => match convert_path(prefix, path) {
81 Some(it) => Some(it),
82 None => return, // TODO: report errors somewhere
83 },
84 };
85 for tree in use_tree_list.use_trees() {
86 expand_use_tree(prefix.clone(), tree, cb);
87 }
88 } else {
89 if let Some(ast_path) = tree.path() {
90 if let Some(path) = convert_path(prefix, ast_path) {
91 let range = if tree.has_star() {
92 None
93 } else {
94 let range = ast_path.segment().unwrap().syntax().range();
95 Some(range)
96 };
97 cb(path, range)
98 }
99 }
100 }
101}
102
103fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
104 let prefix = if let Some(qual) = path.qualifier() {
105 Some(convert_path(prefix, qual)?)
106 } else {
107 None
108 };
109 let segment = path.segment()?;
110 let res = match segment.kind()? {
111 ast::PathSegmentKind::Name(name) => {
112 let mut res = prefix.unwrap_or_else(|| Path {
113 kind: PathKind::Plain,
114 segments: Vec::with_capacity(1),
115 });
116 res.segments.push(name.text());
117 res
118 }
119 ast::PathSegmentKind::CrateKw => {
120 if prefix.is_some() {
121 return None;
122 }
123 Path {
124 kind: PathKind::Crate,
125 segments: Vec::new(),
126 }
127 }
128 ast::PathSegmentKind::SelfKw => {
129 if prefix.is_some() {
130 return None;
131 }
132 Path {
133 kind: PathKind::Self_,
134 segments: Vec::new(),
135 }
136 }
137 ast::PathSegmentKind::SuperKw => {
138 if prefix.is_some() {
139 return None;
140 }
141 Path {
142 kind: PathKind::Super,
143 segments: Vec::new(),
144 }
145 }
146 };
147 Some(res)
148}
diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs
deleted file mode 100644
index 00237b633..000000000
--- a/crates/ra_analysis/src/hir/query_definitions.rs
+++ /dev/null
@@ -1,157 +0,0 @@
1use std::{
2 sync::Arc,
3 time::Instant,
4};
5
6use rustc_hash::FxHashMap;
7use ra_syntax::{
8 AstNode, SyntaxNode, SmolStr,
9 ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner}
10};
11use ra_db::SourceRootId;
12
13use crate::{
14 FileId, Cancelable,
15 hir::{
16 FnId,
17 SourceFileItems, SourceItemId,
18 db::HirDatabase,
19 function::FnScopes,
20 module::{
21 ModuleSource, ModuleSourceNode, ModuleId,
22 imp::Submodule,
23 nameres::{InputModuleItems, ItemMap, Resolver},
24 },
25 },
26};
27
28/// Resolve `FnId` to the corresponding `SyntaxNode`
29pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode {
30 let item_id = fn_id.loc(db);
31 let syntax = db.file_item(item_id);
32 FnDef::cast(syntax.borrowed()).unwrap().owned()
33}
34
35pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> {
36 let syntax = db.fn_syntax(fn_id);
37 let res = FnScopes::new(syntax.borrowed());
38 Arc::new(res)
39}
40
41pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> {
42 let source_file = db.source_file(file_id);
43 let source_file = source_file.borrowed();
44 let mut res = SourceFileItems::default();
45 source_file
46 .syntax()
47 .descendants()
48 .filter_map(ast::ModuleItem::cast)
49 .map(|it| it.syntax().owned())
50 .for_each(|it| {
51 res.alloc(it);
52 });
53 Arc::new(res)
54}
55
56pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode {
57 db.file_items(source_item_id.file_id)[source_item_id.item_id].clone()
58}
59
60pub(crate) fn submodules(
61 db: &impl HirDatabase,
62 source: ModuleSource,
63) -> Cancelable<Arc<Vec<Submodule>>> {
64 db.check_canceled()?;
65 let file_id = source.file_id();
66 let submodules = match source.resolve(db) {
67 ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()),
68 ModuleSourceNode::Module(it) => it
69 .borrowed()
70 .item_list()
71 .map(|it| collect_submodules(db, file_id, it))
72 .unwrap_or_else(Vec::new),
73 };
74 return Ok(Arc::new(submodules));
75
76 fn collect_submodules<'a>(
77 db: &impl HirDatabase,
78 file_id: FileId,
79 root: impl ast::ModuleItemOwner<'a>,
80 ) -> Vec<Submodule> {
81 modules(root)
82 .map(|(name, m)| {
83 if m.has_semi() {
84 Submodule::Declaration(name)
85 } else {
86 let src = ModuleSource::new_inline(db, file_id, m);
87 Submodule::Definition(name, src)
88 }
89 })
90 .collect()
91 }
92}
93
94pub(crate) fn modules<'a>(
95 root: impl ast::ModuleItemOwner<'a>,
96) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> {
97 root.items()
98 .filter_map(|item| match item {
99 ast::ModuleItem::Module(m) => Some(m),
100 _ => None,
101 })
102 .filter_map(|module| {
103 let name = module.name()?.text();
104 Some((name, module))
105 })
106}
107
108pub(super) fn input_module_items(
109 db: &impl HirDatabase,
110 source_root: SourceRootId,
111 module_id: ModuleId,
112) -> Cancelable<Arc<InputModuleItems>> {
113 let module_tree = db.module_tree(source_root)?;
114 let source = module_id.source(&module_tree);
115 let file_items = db.file_items(source.file_id());
116 let res = match source.resolve(db) {
117 ModuleSourceNode::SourceFile(it) => {
118 let items = it.borrowed().items();
119 InputModuleItems::new(&file_items, items)
120 }
121 ModuleSourceNode::Module(it) => {
122 let items = it
123 .borrowed()
124 .item_list()
125 .into_iter()
126 .flat_map(|it| it.items());
127 InputModuleItems::new(&file_items, items)
128 }
129 };
130 Ok(Arc::new(res))
131}
132
133pub(super) fn item_map(
134 db: &impl HirDatabase,
135 source_root: SourceRootId,
136) -> Cancelable<Arc<ItemMap>> {
137 let start = Instant::now();
138 let module_tree = db.module_tree(source_root)?;
139 let input = module_tree
140 .modules()
141 .map(|id| {
142 let items = db.input_module_items(source_root, id)?;
143 Ok((id, items))
144 })
145 .collect::<Cancelable<FxHashMap<_, _>>>()?;
146 let resolver = Resolver {
147 db: db,
148 input: &input,
149 source_root,
150 module_tree,
151 result: ItemMap::default(),
152 };
153 let res = resolver.resolve()?;
154 let elapsed = start.elapsed();
155 log::info!("item_map: {:?}", elapsed);
156 Ok(Arc::new(res))
157}
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 9a8694221..f5cb3550e 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -14,15 +14,15 @@ use ra_db::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE, SyntaxDatabase,
14use rayon::prelude::*; 14use rayon::prelude::*;
15use rustc_hash::FxHashSet; 15use rustc_hash::FxHashSet;
16use salsa::{Database, ParallelDatabase}; 16use salsa::{Database, ParallelDatabase};
17use hir::{
18 self,
19 FnSignatureInfo,
20 Problem,
21};
17 22
18use crate::{ 23use crate::{
19 completion::{completions, CompletionItem}, 24 completion::{completions, CompletionItem},
20 db, 25 db,
21 hir::{
22 self,
23 FnSignatureInfo,
24 Problem,
25 },
26 symbol_index::{SymbolIndex, SymbolsDatabase}, 26 symbol_index::{SymbolIndex, SymbolsDatabase},
27 AnalysisChange, Cancelable, CrateId, Diagnostic, FileId, 27 AnalysisChange, Cancelable, CrateId, Diagnostic, FileId,
28 FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, 28 FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit,
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index a1912e90b..350a6d627 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -9,11 +9,18 @@ extern crate relative_path;
9extern crate rustc_hash; 9extern crate rustc_hash;
10extern crate salsa; 10extern crate salsa;
11 11
12mod arena; 12macro_rules! ctry {
13 ($expr:expr) => {
14 match $expr {
15 None => return Ok(None),
16 Some(it) => it,
17 }
18 };
19}
20
13mod db; 21mod db;
14mod imp; 22mod imp;
15mod completion; 23mod completion;
16mod hir;
17mod symbol_index; 24mod symbol_index;
18pub mod mock_analysis; 25pub mod mock_analysis;
19 26
@@ -31,11 +38,11 @@ use crate::{
31 38
32pub use crate::{ 39pub use crate::{
33 completion::CompletionItem, 40 completion::CompletionItem,
34 hir::FnSignatureInfo,
35}; 41};
36pub use ra_editor::{ 42pub use ra_editor::{
37 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, 43 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,
38}; 44};
45pub use hir::FnSignatureInfo;
39 46
40pub use ra_db::{ 47pub use ra_db::{
41 Canceled, Cancelable, FilePosition, 48 Canceled, Cancelable, FilePosition,
@@ -310,3 +317,112 @@ fn analysis_is_send() {
310 fn is_send<T: Send>() {} 317 fn is_send<T: Send>() {}
311 is_send::<Analysis>(); 318 is_send::<Analysis>();
312} 319}
320
321//TODO: move to hir
322#[cfg(test)]
323mod hir_namres_tests {
324 use std::sync::Arc;
325 use ra_db::FilesDatabase;
326 use ra_syntax::SmolStr;
327 use hir::{self, db::HirDatabase};
328
329 use crate::{
330 AnalysisChange,
331 mock_analysis::{MockAnalysis, analysis_and_position},
332};
333
334 fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
335 let (analysis, pos) = analysis_and_position(fixture);
336 let db = analysis.imp.db;
337 let source_root = db.file_source_root(pos.file_id);
338 let descr = hir::Module::guess_from_position(&*db, pos)
339 .unwrap()
340 .unwrap();
341 let module_id = descr.module_id;
342 (db.item_map(source_root).unwrap(), module_id)
343 }
344
345 #[test]
346 fn test_item_map() {
347 let (item_map, module_id) = item_map(
348 "
349 //- /lib.rs
350 mod foo;
351
352 use crate::foo::bar::Baz;
353 <|>
354
355 //- /foo/mod.rs
356 pub mod bar;
357
358 //- /foo/bar.rs
359 pub struct Baz;
360 ",
361 );
362 let name = SmolStr::from("Baz");
363 let resolution = &item_map.per_module[&module_id].items[&name];
364 assert!(resolution.def_id.is_some());
365 }
366
367 #[test]
368 fn typing_inside_a_function_should_not_invalidate_item_map() {
369 let mock_analysis = MockAnalysis::with_files(
370 "
371 //- /lib.rs
372 mod foo;
373
374 use crate::foo::bar::Baz;
375
376 fn foo() -> i32 {
377 1 + 1
378 }
379 //- /foo/mod.rs
380 pub mod bar;
381
382 //- /foo/bar.rs
383 pub struct Baz;
384 ",
385 );
386
387 let file_id = mock_analysis.id_of("/lib.rs");
388 let mut host = mock_analysis.analysis_host();
389
390 let source_root = host.analysis().imp.db.file_source_root(file_id);
391
392 {
393 let db = host.analysis().imp.db;
394 let events = db.log_executed(|| {
395 db.item_map(source_root).unwrap();
396 });
397 assert!(format!("{:?}", events).contains("item_map"))
398 }
399
400 let mut change = AnalysisChange::new();
401
402 change.change_file(
403 file_id,
404 "
405 mod foo;
406
407 use crate::foo::bar::Baz;
408
409 fn foo() -> i32 { 92 }
410 "
411 .to_string(),
412 );
413
414 host.apply_change(change);
415
416 {
417 let db = host.analysis().imp.db;
418 let events = db.log_executed(|| {
419 db.item_map(source_root).unwrap();
420 });
421 assert!(
422 !format!("{:?}", events).contains("_item_map"),
423 "{:#?}",
424 events
425 )
426 }
427 }
428}
diff --git a/crates/ra_hir/src/arena.rs b/crates/ra_hir/src/arena.rs
index a752ec0c1..8d67ab1c9 100644
--- a/crates/ra_hir/src/arena.rs
+++ b/crates/ra_hir/src/arena.rs
@@ -8,7 +8,7 @@ use std::{
8 marker::PhantomData, 8 marker::PhantomData,
9}; 9};
10 10
11pub(crate) struct Id<T> { 11pub struct Id<T> {
12 idx: u32, 12 idx: u32,
13 _ty: PhantomData<fn() -> T>, 13 _ty: PhantomData<fn() -> T>,
14} 14}
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index dbf8785fe..2f01bae6d 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -10,14 +10,14 @@ use crate::{
10 DefLoc, DefId, FnId, 10 DefLoc, DefId, FnId,
11 SourceFileItems, SourceItemId, 11 SourceFileItems, SourceItemId,
12 query_definitions, 12 query_definitions,
13 function::{FnScopes}, 13 FnScopes,
14 module::{ModuleId, ModuleTree, ModuleSource, 14 module::{ModuleId, ModuleTree, ModuleSource,
15 nameres::{ItemMap, InputModuleItems}}, 15 nameres::{ItemMap, InputModuleItems}},
16}; 16};
17 17
18salsa::query_group! { 18salsa::query_group! {
19 19
20pub(crate) trait HirDatabase: SyntaxDatabase 20pub trait HirDatabase: SyntaxDatabase
21 + AsRef<LocationIntener<DefLoc, DefId>> 21 + AsRef<LocationIntener<DefLoc, DefId>>
22 + AsRef<LocationIntener<SourceItemId, FnId>> 22 + AsRef<LocationIntener<SourceItemId, FnId>>
23{ 23{
diff --git a/crates/ra_hir/src/function/mod.rs b/crates/ra_hir/src/function/mod.rs
index a3ed00f02..c8af2e54f 100644
--- a/crates/ra_hir/src/function/mod.rs
+++ b/crates/ra_hir/src/function/mod.rs
@@ -15,10 +15,10 @@ use crate::{
15 FnId, HirDatabase, SourceItemId, 15 FnId, HirDatabase, SourceItemId,
16}; 16};
17 17
18pub(crate) use self::scope::FnScopes; 18pub use self::scope::FnScopes;
19 19
20impl FnId { 20impl FnId {
21 pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { 21 pub fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
22 let file_items = db.file_items(file_id); 22 let file_items = db.file_items(file_id);
23 let item_id = file_items.id_of(fn_def.syntax()); 23 let item_id = file_items.id_of(fn_def.syntax());
24 let item_id = SourceItemId { file_id, item_id }; 24 let item_id = SourceItemId { file_id, item_id };
@@ -26,12 +26,12 @@ impl FnId {
26 } 26 }
27} 27}
28 28
29pub(crate) struct Function { 29pub struct Function {
30 fn_id: FnId, 30 fn_id: FnId,
31} 31}
32 32
33impl Function { 33impl Function {
34 pub(crate) fn guess_from_source( 34 pub fn guess_from_source(
35 db: &impl HirDatabase, 35 db: &impl HirDatabase,
36 file_id: FileId, 36 file_id: FileId,
37 fn_def: ast::FnDef, 37 fn_def: ast::FnDef,
@@ -40,7 +40,7 @@ impl Function {
40 Function { fn_id } 40 Function { fn_id }
41 } 41 }
42 42
43 pub(crate) fn guess_for_name_ref( 43 pub fn guess_for_name_ref(
44 db: &impl HirDatabase, 44 db: &impl HirDatabase,
45 file_id: FileId, 45 file_id: FileId,
46 name_ref: ast::NameRef, 46 name_ref: ast::NameRef,
@@ -48,7 +48,7 @@ impl Function {
48 Function::guess_for_node(db, file_id, name_ref.syntax()) 48 Function::guess_for_node(db, file_id, name_ref.syntax())
49 } 49 }
50 50
51 pub(crate) fn guess_for_bind_pat( 51 pub fn guess_for_bind_pat(
52 db: &impl HirDatabase, 52 db: &impl HirDatabase,
53 file_id: FileId, 53 file_id: FileId,
54 bind_pat: ast::BindPat, 54 bind_pat: ast::BindPat,
@@ -66,11 +66,11 @@ impl Function {
66 Some(res) 66 Some(res)
67 } 67 }
68 68
69 pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { 69 pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> {
70 db.fn_scopes(self.fn_id) 70 db.fn_scopes(self.fn_id)
71 } 71 }
72 72
73 pub(crate) fn signature_info(&self, db: &impl HirDatabase) -> Option<FnSignatureInfo> { 73 pub fn signature_info(&self, db: &impl HirDatabase) -> Option<FnSignatureInfo> {
74 let syntax = db.fn_syntax(self.fn_id); 74 let syntax = db.fn_syntax(self.fn_id);
75 FnSignatureInfo::new(syntax.borrowed()) 75 FnSignatureInfo::new(syntax.borrowed())
76 } 76 }
diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs
index c8b6b1934..863453291 100644
--- a/crates/ra_hir/src/function/scope.rs
+++ b/crates/ra_hir/src/function/scope.rs
@@ -15,7 +15,7 @@ pub(crate) type ScopeId = Id<ScopeData>;
15 15
16#[derive(Debug, PartialEq, Eq)] 16#[derive(Debug, PartialEq, Eq)]
17pub struct FnScopes { 17pub struct FnScopes {
18 pub(crate) self_param: Option<LocalSyntaxPtr>, 18 pub self_param: Option<LocalSyntaxPtr>,
19 scopes: Arena<ScopeData>, 19 scopes: Arena<ScopeData>,
20 scope_for: FxHashMap<LocalSyntaxPtr, ScopeId>, 20 scope_for: FxHashMap<LocalSyntaxPtr, ScopeId>,
21} 21}
@@ -27,13 +27,13 @@ pub struct ScopeEntry {
27} 27}
28 28
29#[derive(Debug, PartialEq, Eq)] 29#[derive(Debug, PartialEq, Eq)]
30pub(crate) struct ScopeData { 30pub struct ScopeData {
31 parent: Option<ScopeId>, 31 parent: Option<ScopeId>,
32 entries: Vec<ScopeEntry>, 32 entries: Vec<ScopeEntry>,
33} 33}
34 34
35impl FnScopes { 35impl FnScopes {
36 pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes { 36 pub fn new(fn_def: ast::FnDef) -> FnScopes {
37 let mut scopes = FnScopes { 37 let mut scopes = FnScopes {
38 self_param: fn_def 38 self_param: fn_def
39 .param_list() 39 .param_list()
@@ -49,7 +49,7 @@ impl FnScopes {
49 } 49 }
50 scopes 50 scopes
51 } 51 }
52 pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { 52 pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
53 &self.scopes[scope].entries 53 &self.scopes[scope].entries
54 } 54 }
55 pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item = ScopeId> + 'a { 55 pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item = ScopeId> + 'a {
@@ -57,10 +57,7 @@ impl FnScopes {
57 self.scopes[scope].parent 57 self.scopes[scope].parent
58 }) 58 })
59 } 59 }
60 pub(crate) fn resolve_local_name<'a>( 60 pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> {
61 &'a self,
62 name_ref: ast::NameRef,
63 ) -> Option<&'a ScopeEntry> {
64 let mut shadowed = FxHashSet::default(); 61 let mut shadowed = FxHashSet::default();
65 let ret = self 62 let ret = self
66 .scope_chain(name_ref.syntax()) 63 .scope_chain(name_ref.syntax())
@@ -138,10 +135,10 @@ impl ScopeEntry {
138 }; 135 };
139 Some(res) 136 Some(res)
140 } 137 }
141 pub(crate) fn name(&self) -> &SmolStr { 138 pub fn name(&self) -> &SmolStr {
142 &self.name 139 &self.name
143 } 140 }
144 pub(crate) fn ptr(&self) -> LocalSyntaxPtr { 141 pub fn ptr(&self) -> LocalSyntaxPtr {
145 self.ptr 142 self.ptr
146 } 143 }
147} 144}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 7bf06c7f7..f13f0107e 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -14,7 +14,7 @@ macro_rules! ctry {
14 }; 14 };
15} 15}
16 16
17pub(crate) mod db; 17pub mod db;
18mod query_definitions; 18mod query_definitions;
19mod function; 19mod function;
20mod module; 20mod module;
@@ -31,36 +31,36 @@ use crate::{
31 arena::{Arena, Id}, 31 arena::{Arena, Id},
32}; 32};
33 33
34pub(crate) use self::{ 34pub use self::{
35 path::{Path, PathKind}, 35 path::{Path, PathKind},
36 module::{Module, ModuleId, Problem}, 36 module::{Module, ModuleId, Problem, nameres::ItemMap},
37 function::{Function, FnScopes}, 37 function::{Function, FnScopes},
38}; 38};
39 39
40pub use self::function::FnSignatureInfo; 40pub use self::function::FnSignatureInfo;
41 41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43pub(crate) struct FnId(u32); 43pub struct FnId(u32);
44ra_db::impl_numeric_id!(FnId); 44ra_db::impl_numeric_id!(FnId);
45 45
46impl FnId { 46impl FnId {
47 pub(crate) fn from_loc( 47 pub fn from_loc(
48 db: &impl AsRef<LocationIntener<SourceItemId, FnId>>, 48 db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
49 loc: &SourceItemId, 49 loc: &SourceItemId,
50 ) -> FnId { 50 ) -> FnId {
51 db.as_ref().loc2id(loc) 51 db.as_ref().loc2id(loc)
52 } 52 }
53 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId { 53 pub fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
54 db.as_ref().id2loc(self) 54 db.as_ref().id2loc(self)
55 } 55 }
56} 56}
57 57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59pub(crate) struct DefId(u32); 59pub struct DefId(u32);
60ra_db::impl_numeric_id!(DefId); 60ra_db::impl_numeric_id!(DefId);
61 61
62#[derive(Clone, Debug, PartialEq, Eq, Hash)] 62#[derive(Clone, Debug, PartialEq, Eq, Hash)]
63pub(crate) enum DefLoc { 63pub enum DefLoc {
64 Module { 64 Module {
65 id: ModuleId, 65 id: ModuleId,
66 source_root: SourceRootId, 66 source_root: SourceRootId,
@@ -71,24 +71,24 @@ pub(crate) enum DefLoc {
71} 71}
72 72
73impl DefId { 73impl DefId {
74 pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc { 74 pub fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
75 db.as_ref().id2loc(self) 75 db.as_ref().id2loc(self)
76 } 76 }
77} 77}
78 78
79impl DefLoc { 79impl DefLoc {
80 pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId { 80 pub fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
81 db.as_ref().loc2id(&self) 81 db.as_ref().loc2id(&self)
82 } 82 }
83} 83}
84 84
85pub(crate) enum Def { 85pub enum Def {
86 Module(Module), 86 Module(Module),
87 Item, 87 Item,
88} 88}
89 89
90impl DefId { 90impl DefId {
91 pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> { 91 pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
92 let loc = self.loc(db); 92 let loc = self.loc(db);
93 let res = match loc { 93 let res = match loc {
94 DefLoc::Module { id, source_root } => { 94 DefLoc::Module { id, source_root } => {
@@ -106,14 +106,14 @@ impl DefId {
106pub(crate) type SourceFileItemId = Id<SyntaxNode>; 106pub(crate) type SourceFileItemId = Id<SyntaxNode>;
107 107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 108#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
109pub(crate) struct SourceItemId { 109pub struct SourceItemId {
110 file_id: FileId, 110 file_id: FileId,
111 item_id: SourceFileItemId, 111 item_id: SourceFileItemId,
112} 112}
113 113
114/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. 114/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
115#[derive(Debug, PartialEq, Eq, Default)] 115#[derive(Debug, PartialEq, Eq, Default)]
116pub(crate) struct SourceFileItems { 116pub struct SourceFileItems {
117 arena: Arena<SyntaxNode>, 117 arena: Arena<SyntaxNode>,
118} 118}
119 119
@@ -121,7 +121,7 @@ impl SourceFileItems {
121 fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { 121 fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
122 self.arena.alloc(item) 122 self.arena.alloc(item)
123 } 123 }
124 fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId { 124 pub fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId {
125 let (id, _item) = self 125 let (id, _item) = self
126 .arena 126 .arena
127 .iter() 127 .iter()
diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs
index d55fa3e6b..76ea129a7 100644
--- a/crates/ra_hir/src/module/imp.rs
+++ b/crates/ra_hir/src/module/imp.rs
@@ -18,7 +18,7 @@ use super::{
18}; 18};
19 19
20#[derive(Clone, Hash, PartialEq, Eq, Debug)] 20#[derive(Clone, Hash, PartialEq, Eq, Debug)]
21pub(crate) enum Submodule { 21pub enum Submodule {
22 Declaration(SmolStr), 22 Declaration(SmolStr),
23 Definition(SmolStr, ModuleSource), 23 Definition(SmolStr, ModuleSource),
24} 24}
diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs
index 81b9f948d..a011fd53e 100644
--- a/crates/ra_hir/src/module/mod.rs
+++ b/crates/ra_hir/src/module/mod.rs
@@ -18,15 +18,16 @@ use crate::{
18 arena::{Arena, Id}, 18 arena::{Arena, Id},
19}; 19};
20 20
21pub(crate) use self::nameres::ModuleScope; 21pub use self::nameres::ModuleScope;
22 22
23/// `Module` is API entry point to get all the information 23/// `Module` is API entry point to get all the information
24/// about a particular module. 24/// about a particular module.
25#[derive(Debug, Clone)] 25#[derive(Debug, Clone)]
26pub(crate) struct Module { 26pub struct Module {
27 tree: Arc<ModuleTree>, 27 tree: Arc<ModuleTree>,
28 source_root_id: SourceRootId, 28 source_root_id: SourceRootId,
29 module_id: ModuleId, 29 //TODO: make private
30 pub module_id: ModuleId,
30} 31}
31 32
32impl Module { 33impl Module {
@@ -145,17 +146,13 @@ impl Module {
145 } 146 }
146 147
147 /// Returns a `ModuleScope`: a set of items, visible in this module. 148 /// Returns a `ModuleScope`: a set of items, visible in this module.
148 pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { 149 pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> {
149 let item_map = db.item_map(self.source_root_id)?; 150 let item_map = db.item_map(self.source_root_id)?;
150 let res = item_map.per_module[&self.module_id].clone(); 151 let res = item_map.per_module[&self.module_id].clone();
151 Ok(res) 152 Ok(res)
152 } 153 }
153 154
154 pub(crate) fn resolve_path( 155 pub fn resolve_path(&self, db: &impl HirDatabase, path: Path) -> Cancelable<Option<DefId>> {
155 &self,
156 db: &impl HirDatabase,
157 path: Path,
158 ) -> Cancelable<Option<DefId>> {
159 let mut curr = match path.kind { 156 let mut curr = match path.kind {
160 PathKind::Crate => self.crate_root(), 157 PathKind::Crate => self.crate_root(),
161 PathKind::Self_ | PathKind::Plain => self.clone(), 158 PathKind::Self_ | PathKind::Plain => self.clone(),
@@ -188,7 +185,7 @@ impl Module {
188/// (which can have multiple parents) to the precise world of modules (which 185/// (which can have multiple parents) to the precise world of modules (which
189/// always have one parent). 186/// always have one parent).
190#[derive(Default, Debug, PartialEq, Eq)] 187#[derive(Default, Debug, PartialEq, Eq)]
191pub(crate) struct ModuleTree { 188pub struct ModuleTree {
192 mods: Arena<ModuleData>, 189 mods: Arena<ModuleData>,
193 links: Arena<LinkData>, 190 links: Arena<LinkData>,
194} 191}
@@ -214,19 +211,19 @@ impl ModuleTree {
214/// `ModuleSource` is the syntax tree element that produced this module: 211/// `ModuleSource` is the syntax tree element that produced this module:
215/// either a file, or an inlinde module. 212/// either a file, or an inlinde module.
216#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 213#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
217pub(crate) enum ModuleSource { 214pub enum ModuleSource {
218 SourceFile(FileId), 215 SourceFile(FileId),
219 Module(SourceItemId), 216 Module(SourceItemId),
220} 217}
221 218
222/// An owned syntax node for a module. Unlike `ModuleSource`, 219/// An owned syntax node for a module. Unlike `ModuleSource`,
223/// this holds onto the AST for the whole file. 220/// this holds onto the AST for the whole file.
224pub(crate) enum ModuleSourceNode { 221pub enum ModuleSourceNode {
225 SourceFile(ast::SourceFileNode), 222 SourceFile(ast::SourceFileNode),
226 Module(ast::ModuleNode), 223 Module(ast::ModuleNode),
227} 224}
228 225
229pub(crate) type ModuleId = Id<ModuleData>; 226pub type ModuleId = Id<ModuleData>;
230type LinkId = Id<LinkData>; 227type LinkId = Id<LinkData>;
231 228
232#[derive(Clone, Debug, Hash, PartialEq, Eq)] 229#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -308,7 +305,7 @@ impl LinkId {
308} 305}
309 306
310#[derive(Debug, PartialEq, Eq, Hash)] 307#[derive(Debug, PartialEq, Eq, Hash)]
311pub(crate) struct ModuleData { 308pub struct ModuleData {
312 source: ModuleSource, 309 source: ModuleSource,
313 parent: Option<LinkId>, 310 parent: Option<LinkId>,
314 children: Vec<LinkId>, 311 children: Vec<LinkId>,
@@ -327,21 +324,21 @@ impl ModuleSource {
327 ModuleSource::Module(id) 324 ModuleSource::Module(id)
328 } 325 }
329 326
330 pub(crate) fn as_file(self) -> Option<FileId> { 327 pub fn as_file(self) -> Option<FileId> {
331 match self { 328 match self {
332 ModuleSource::SourceFile(f) => Some(f), 329 ModuleSource::SourceFile(f) => Some(f),
333 ModuleSource::Module(..) => None, 330 ModuleSource::Module(..) => None,
334 } 331 }
335 } 332 }
336 333
337 pub(crate) fn file_id(self) -> FileId { 334 pub fn file_id(self) -> FileId {
338 match self { 335 match self {
339 ModuleSource::SourceFile(f) => f, 336 ModuleSource::SourceFile(f) => f,
340 ModuleSource::Module(source_item_id) => source_item_id.file_id, 337 ModuleSource::Module(source_item_id) => source_item_id.file_id,
341 } 338 }
342 } 339 }
343 340
344 pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { 341 pub fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
345 match self { 342 match self {
346 ModuleSource::SourceFile(file_id) => { 343 ModuleSource::SourceFile(file_id) => {
347 let syntax = db.source_file(file_id); 344 let syntax = db.source_file(file_id);
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
index 513a37646..837a8d5ae 100644
--- a/crates/ra_hir/src/module/nameres.rs
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -38,20 +38,20 @@ use crate::{
38/// Item map is the result of the name resolution. Item map contains, for each 38/// Item map is the result of the name resolution. Item map contains, for each
39/// module, the set of visible items. 39/// module, the set of visible items.
40#[derive(Default, Debug, PartialEq, Eq)] 40#[derive(Default, Debug, PartialEq, Eq)]
41pub(crate) struct ItemMap { 41pub struct ItemMap {
42 pub(crate) per_module: FxHashMap<ModuleId, ModuleScope>, 42 pub per_module: FxHashMap<ModuleId, ModuleScope>,
43} 43}
44 44
45#[derive(Debug, Default, PartialEq, Eq, Clone)] 45#[derive(Debug, Default, PartialEq, Eq, Clone)]
46pub(crate) struct ModuleScope { 46pub struct ModuleScope {
47 items: FxHashMap<SmolStr, Resolution>, 47 pub items: FxHashMap<SmolStr, Resolution>,
48} 48}
49 49
50impl ModuleScope { 50impl ModuleScope {
51 pub(crate) fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a SmolStr, &Resolution)> + 'a { 51 pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a SmolStr, &Resolution)> + 'a {
52 self.items.iter() 52 self.items.iter()
53 } 53 }
54 pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { 54 pub fn get(&self, name: &SmolStr) -> Option<&Resolution> {
55 self.items.get(name) 55 self.items.get(name)
56 } 56 }
57} 57}
@@ -63,7 +63,7 @@ impl ModuleScope {
63/// recomputing name res: if `InputModuleItems` are the same, we can avoid 63/// recomputing name res: if `InputModuleItems` are the same, we can avoid
64/// running name resolution. 64/// running name resolution.
65#[derive(Debug, Default, PartialEq, Eq)] 65#[derive(Debug, Default, PartialEq, Eq)]
66pub(crate) struct InputModuleItems { 66pub struct InputModuleItems {
67 items: Vec<ModuleItem>, 67 items: Vec<ModuleItem>,
68 imports: Vec<Import>, 68 imports: Vec<Import>,
69} 69}
@@ -89,13 +89,13 @@ struct Import {
89} 89}
90 90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)] 91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub(crate) struct NamedImport { 92pub struct NamedImport {
93 file_item_id: SourceFileItemId, 93 pub file_item_id: SourceFileItemId,
94 relative_range: TextRange, 94 pub relative_range: TextRange,
95} 95}
96 96
97impl NamedImport { 97impl NamedImport {
98 pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { 98 pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange {
99 let source_item_id = SourceItemId { 99 let source_item_id = SourceItemId {
100 file_id, 100 file_id,
101 item_id: self.file_item_id, 101 item_id: self.file_item_id,
@@ -115,11 +115,11 @@ enum ImportKind {
115/// Resolution is basically `DefId` atm, but it should account for stuff like 115/// Resolution is basically `DefId` atm, but it should account for stuff like
116/// multiple namespaces, ambiguity and errors. 116/// multiple namespaces, ambiguity and errors.
117#[derive(Debug, Clone, PartialEq, Eq)] 117#[derive(Debug, Clone, PartialEq, Eq)]
118pub(crate) struct Resolution { 118pub struct Resolution {
119 /// None for unresolved 119 /// None for unresolved
120 pub(crate) def_id: Option<DefId>, 120 pub def_id: Option<DefId>,
121 /// ident by whitch this is imported into local scope. 121 /// ident by whitch this is imported into local scope.
122 pub(crate) import: Option<NamedImport>, 122 pub import: Option<NamedImport>,
123} 123}
124 124
125// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] 125// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -336,109 +336,3 @@ where
336 f(module_items) 336 f(module_items)
337 } 337 }
338} 338}
339
340#[cfg(test)]
341mod tests {
342 use ra_db::FilesDatabase;
343 use crate::{
344 AnalysisChange,
345 mock_analysis::{MockAnalysis, analysis_and_position},
346 hir::{self, HirDatabase},
347};
348 use super::*;
349
350 fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
351 let (analysis, pos) = analysis_and_position(fixture);
352 let db = analysis.imp.db;
353 let source_root = db.file_source_root(pos.file_id);
354 let descr = hir::Module::guess_from_position(&*db, pos)
355 .unwrap()
356 .unwrap();
357 let module_id = descr.module_id;
358 (db.item_map(source_root).unwrap(), module_id)
359 }
360
361 #[test]
362 fn test_item_map() {
363 let (item_map, module_id) = item_map(
364 "
365 //- /lib.rs
366 mod foo;
367
368 use crate::foo::bar::Baz;
369 <|>
370
371 //- /foo/mod.rs
372 pub mod bar;
373
374 //- /foo/bar.rs
375 pub struct Baz;
376 ",
377 );
378 let name = SmolStr::from("Baz");
379 let resolution = &item_map.per_module[&module_id].items[&name];
380 assert!(resolution.def_id.is_some());
381 }
382
383 #[test]
384 fn typing_inside_a_function_should_not_invalidate_item_map() {
385 let mock_analysis = MockAnalysis::with_files(
386 "
387 //- /lib.rs
388 mod foo;
389
390 use crate::foo::bar::Baz;
391
392 fn foo() -> i32 {
393 1 + 1
394 }
395 //- /foo/mod.rs
396 pub mod bar;
397
398 //- /foo/bar.rs
399 pub struct Baz;
400 ",
401 );
402
403 let file_id = mock_analysis.id_of("/lib.rs");
404 let mut host = mock_analysis.analysis_host();
405
406 let source_root = host.analysis().imp.db.file_source_root(file_id);
407
408 {
409 let db = host.analysis().imp.db;
410 let events = db.log_executed(|| {
411 db.item_map(source_root).unwrap();
412 });
413 assert!(format!("{:?}", events).contains("item_map"))
414 }
415
416 let mut change = AnalysisChange::new();
417
418 change.change_file(
419 file_id,
420 "
421 mod foo;
422
423 use crate::foo::bar::Baz;
424
425 fn foo() -> i32 { 92 }
426 "
427 .to_string(),
428 );
429
430 host.apply_change(change);
431
432 {
433 let db = host.analysis().imp.db;
434 let events = db.log_executed(|| {
435 db.item_map(source_root).unwrap();
436 });
437 assert!(
438 !format!("{:?}", events).contains("_item_map"),
439 "{:#?}",
440 events
441 )
442 }
443 }
444}
diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs
index 8279daf4b..4a2e427cd 100644
--- a/crates/ra_hir/src/path.rs
+++ b/crates/ra_hir/src/path.rs
@@ -1,13 +1,13 @@
1use ra_syntax::{SmolStr, ast, AstNode, TextRange}; 1use ra_syntax::{SmolStr, ast, AstNode, TextRange};
2 2
3#[derive(Debug, Clone, PartialEq, Eq)] 3#[derive(Debug, Clone, PartialEq, Eq)]
4pub(crate) struct Path { 4pub struct Path {
5 pub(crate) kind: PathKind, 5 pub kind: PathKind,
6 pub(crate) segments: Vec<SmolStr>, 6 pub segments: Vec<SmolStr>,
7} 7}
8 8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)] 9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub(crate) enum PathKind { 10pub enum PathKind {
11 Plain, 11 Plain,
12 Self_, 12 Self_,
13 Super, 13 Super,
@@ -16,14 +16,14 @@ pub(crate) enum PathKind {
16 16
17impl Path { 17impl Path {
18 /// Calls `cb` with all paths, represented by this use item. 18 /// Calls `cb` with all paths, represented by this use item.
19 pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option<TextRange>)) { 19 pub fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option<TextRange>)) {
20 if let Some(tree) = item.use_tree() { 20 if let Some(tree) = item.use_tree() {
21 expand_use_tree(None, tree, &mut cb); 21 expand_use_tree(None, tree, &mut cb);
22 } 22 }
23 } 23 }
24 24
25 /// Converts an `ast::Path` to `Path`. Works with use trees. 25 /// Converts an `ast::Path` to `Path`. Works with use trees.
26 pub(crate) fn from_ast(mut path: ast::Path) -> Option<Path> { 26 pub fn from_ast(mut path: ast::Path) -> Option<Path> {
27 let mut kind = PathKind::Plain; 27 let mut kind = PathKind::Plain;
28 let mut segments = Vec::new(); 28 let mut segments = Vec::new();
29 loop { 29 loop {
@@ -64,7 +64,7 @@ impl Path {
64 } 64 }
65 65
66 /// `true` is this path is a single identifier, like `foo` 66 /// `true` is this path is a single identifier, like `foo`
67 pub(crate) fn is_ident(&self) -> bool { 67 pub fn is_ident(&self) -> bool {
68 self.kind == PathKind::Plain && self.segments.len() == 1 68 self.kind == PathKind::Plain && self.segments.len() == 1
69 } 69 }
70} 70}