aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/from_id.rs1
-rw-r--r--crates/ra_hir/src/lib.rs9
-rw-r--r--crates/ra_hir/src/semantics.rs405
-rw-r--r--crates/ra_hir/src/source_analyzer.rs247
-rw-r--r--crates/ra_hir/src/source_binder.rs217
5 files changed, 582 insertions, 297 deletions
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index c16c17072..3aa7c4870 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -40,6 +40,7 @@ from_id![
40 (hir_def::ConstId, crate::Const), 40 (hir_def::ConstId, crate::Const),
41 (hir_def::FunctionId, crate::Function), 41 (hir_def::FunctionId, crate::Function),
42 (hir_def::ImplId, crate::ImplBlock), 42 (hir_def::ImplId, crate::ImplBlock),
43 (hir_def::TypeParamId, crate::TypeParam),
43 (hir_expand::MacroDefId, crate::MacroDef) 44 (hir_expand::MacroDefId, crate::MacroDef)
44]; 45];
45 46
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 7a9745ebe..3aa964fb6 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -26,9 +26,10 @@ macro_rules! impl_froms {
26 } 26 }
27} 27}
28 28
29mod semantics;
29pub mod db; 30pub mod db;
30pub mod source_analyzer; 31mod source_analyzer;
31pub mod source_binder; 32mod source_binder;
32 33
33pub mod diagnostics; 34pub mod diagnostics;
34 35
@@ -45,8 +46,8 @@ pub use crate::{
45 StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, 46 StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
46 }, 47 },
47 has_source::HasSource, 48 has_source::HasSource,
48 source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 49 semantics::{original_range, Semantics, SemanticsScope},
49 source_binder::SourceBinder, 50 source_analyzer::PathResolution,
50}; 51};
51 52
52pub use hir_def::{ 53pub use hir_def::{
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs
new file mode 100644
index 000000000..4a9cb7b3e
--- /dev/null
+++ b/crates/ra_hir/src/semantics.rs
@@ -0,0 +1,405 @@
1//! See `Semantics`.
2
3use std::{cell::RefCell, fmt, iter::successors};
4
5use hir_def::{
6 resolver::{self, HasResolver, Resolver},
7 DefWithBodyId, TraitId,
8};
9use ra_db::{FileId, FileRange};
10use ra_syntax::{
11 algo::skip_trivia_token, ast, match_ast, AstNode, Direction, SyntaxNode, SyntaxToken,
12 TextRange, TextUnit,
13};
14use rustc_hash::{FxHashMap, FxHashSet};
15
16use crate::{
17 db::HirDatabase,
18 source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer},
19 source_binder::{ChildContainer, SourceBinder},
20 Function, HirFileId, InFile, Local, MacroDef, Module, ModuleDef, Name, Origin, Path,
21 PathResolution, ScopeDef, StructField, Trait, Type, TypeParam, VariantDef,
22};
23use hir_expand::ExpansionInfo;
24use ra_prof::profile;
25
26/// Primary API to get semantic information, like types, from syntax trees.
27pub struct Semantics<'db, DB> {
28 pub db: &'db DB,
29 sb: RefCell<SourceBinder>,
30 cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
31}
32
33impl<DB> fmt::Debug for Semantics<'_, DB> {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 write!(f, "Semantics {{ ... }}")
36 }
37}
38
39impl<'db, DB: HirDatabase> Semantics<'db, DB> {
40 pub fn new(db: &DB) -> Semantics<DB> {
41 let sb = RefCell::new(SourceBinder::new());
42 Semantics { db, sb, cache: RefCell::default() }
43 }
44
45 pub fn parse(&self, file_id: FileId) -> ast::SourceFile {
46 let tree = self.db.parse(file_id).tree();
47 self.cache(tree.syntax().clone(), file_id.into());
48 tree
49 }
50
51 pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
52 let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
53 let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
54 let file_id = sa.expand(self.db, macro_call)?;
55 let node = self.db.parse_or_expand(file_id)?;
56 self.cache(node.clone(), file_id);
57 Some(node)
58 }
59
60 pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
61 let parent = token.parent();
62 let parent = self.find_file(parent);
63 let sa = self.analyze2(parent.as_ref(), None);
64
65 let token = successors(Some(parent.with_value(token)), |token| {
66 let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
67 let tt = macro_call.token_tree()?;
68 if !token.value.text_range().is_subrange(&tt.syntax().text_range()) {
69 return None;
70 }
71 let file_id = sa.expand(self.db, token.with_value(&macro_call))?;
72 let token = file_id.expansion_info(self.db)?.map_token_down(token.as_ref())?;
73
74 self.cache(find_root(&token.value.parent()), token.file_id);
75
76 Some(token)
77 })
78 .last()
79 .unwrap();
80
81 token.value
82 }
83
84 pub fn original_range(&self, node: &SyntaxNode) -> FileRange {
85 let node = self.find_file(node.clone());
86 original_range(self.db, node.as_ref())
87 }
88
89 pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
90 let node = self.find_file(node);
91 node.ancestors_with_macros(self.db).map(|it| it.value)
92 }
93
94 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
95 self.analyze(expr.syntax()).type_of(self.db, &expr)
96 }
97
98 pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
99 self.analyze(pat.syntax()).type_of_pat(self.db, &pat)
100 }
101
102 pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
103 self.analyze(call.syntax()).resolve_method_call(call)
104 }
105
106 pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<StructField> {
107 self.analyze(field.syntax()).resolve_field(field)
108 }
109
110 pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<StructField> {
111 self.analyze(field.syntax()).resolve_record_field(field)
112 }
113
114 pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<VariantDef> {
115 self.analyze(record_lit.syntax()).resolve_record_literal(record_lit)
116 }
117
118 pub fn resolve_record_pattern(&self, record_pat: &ast::RecordPat) -> Option<VariantDef> {
119 self.analyze(record_pat.syntax()).resolve_record_pattern(record_pat)
120 }
121
122 pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
123 let sa = self.analyze(macro_call.syntax());
124 let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
125 sa.resolve_macro_call(self.db, macro_call)
126 }
127
128 pub fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
129 self.analyze(path.syntax()).resolve_path(self.db, path)
130 }
131
132 pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option<ModuleDef> {
133 self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat)
134 }
135
136 // FIXME: use this instead?
137 // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
138
139 pub fn to_def<T: ToDef + Clone>(&self, src: &T) -> Option<T::Def> {
140 T::to_def(self, src)
141 }
142
143 pub fn to_module_def(&self, file: FileId) -> Option<Module> {
144 let mut sb = self.sb.borrow_mut();
145 sb.to_module_def(self.db, file)
146 }
147
148 pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db, DB> {
149 let node = self.find_file(node.clone());
150 let resolver = self.analyze2(node.as_ref(), None).resolver;
151 SemanticsScope { db: self.db, resolver }
152 }
153
154 pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextUnit) -> SemanticsScope<'db, DB> {
155 let node = self.find_file(node.clone());
156 let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver;
157 SemanticsScope { db: self.db, resolver }
158 }
159
160 pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db, DB> {
161 let resolver = def.id.resolver(self.db);
162 SemanticsScope { db: self.db, resolver }
163 }
164
165 // FIXME: we only use this in `inline_local_variable` assist, ideally, we
166 // should switch to general reference search infra there.
167 pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
168 self.analyze(pat.syntax()).find_all_refs(pat)
169 }
170
171 fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
172 let src = self.find_file(node.clone());
173 self.analyze2(src.as_ref(), None)
174 }
175
176 fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option<TextUnit>) -> SourceAnalyzer {
177 let _p = profile("Semantics::analyze2");
178
179 let container = match self.sb.borrow_mut().find_container(self.db, src) {
180 Some(it) => it,
181 None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
182 };
183
184 let resolver = match container {
185 ChildContainer::DefWithBodyId(def) => {
186 return SourceAnalyzer::new_for_body(self.db, def, src, offset)
187 }
188 ChildContainer::TraitId(it) => it.resolver(self.db),
189 ChildContainer::ImplId(it) => it.resolver(self.db),
190 ChildContainer::ModuleId(it) => it.resolver(self.db),
191 ChildContainer::EnumId(it) => it.resolver(self.db),
192 ChildContainer::VariantId(it) => it.resolver(self.db),
193 ChildContainer::GenericDefId(it) => it.resolver(self.db),
194 };
195 SourceAnalyzer::new_for_resolver(resolver, src)
196 }
197
198 fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
199 assert!(root_node.parent().is_none());
200 let mut cache = self.cache.borrow_mut();
201 let prev = cache.insert(root_node, file_id);
202 assert!(prev == None || prev == Some(file_id))
203 }
204
205 pub fn assert_contains_node(&self, node: &SyntaxNode) {
206 self.find_file(node.clone());
207 }
208
209 fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
210 let cache = self.cache.borrow();
211 cache.get(root_node).copied()
212 }
213
214 fn find_file(&self, node: SyntaxNode) -> InFile<SyntaxNode> {
215 let root_node = find_root(&node);
216 let file_id = self.lookup(&root_node).unwrap_or_else(|| {
217 panic!(
218 "\n\nFailed to lookup {:?} in this Semantics.\n\
219 Make sure to use only query nodes, derived from this instance of Semantics.\n\
220 root node: {:?}\n\
221 known nodes: {}\n\n",
222 node,
223 root_node,
224 self.cache
225 .borrow()
226 .keys()
227 .map(|it| format!("{:?}", it))
228 .collect::<Vec<_>>()
229 .join(", ")
230 )
231 });
232 InFile::new(file_id, node)
233 }
234}
235
236pub trait ToDef: Sized + AstNode + 'static {
237 type Def;
238 fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self) -> Option<Self::Def>;
239}
240
241macro_rules! to_def_impls {
242 ($(($def:path, $ast:path)),* ,) => {$(
243 impl ToDef for $ast {
244 type Def = $def;
245 fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self)
246 -> Option<Self::Def>
247 {
248 let src = sema.find_file(src.syntax().clone()).with_value(src);
249 sema.sb.borrow_mut().to_id(sema.db, src.cloned()).map(Into::into)
250 }
251 }
252 )*}
253}
254
255to_def_impls![
256 (crate::Module, ast::Module),
257 (crate::Struct, ast::StructDef),
258 (crate::Enum, ast::EnumDef),
259 (crate::Union, ast::UnionDef),
260 (crate::Trait, ast::TraitDef),
261 (crate::ImplBlock, ast::ImplBlock),
262 (crate::TypeAlias, ast::TypeAliasDef),
263 (crate::Const, ast::ConstDef),
264 (crate::Static, ast::StaticDef),
265 (crate::Function, ast::FnDef),
266 (crate::StructField, ast::RecordFieldDef),
267 (crate::EnumVariant, ast::EnumVariant),
268 (crate::TypeParam, ast::TypeParam),
269 (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
270];
271
272impl ToDef for ast::BindPat {
273 type Def = Local;
274
275 fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self) -> Option<Local> {
276 let src = sema.find_file(src.syntax().clone()).with_value(src);
277 let file_id = src.file_id;
278 let mut sb = sema.sb.borrow_mut();
279 let db = sema.db;
280 let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
281 let res = match_ast! {
282 match it {
283 ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
284 ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
285 ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
286 _ => return None,
287 }
288 };
289 Some(res)
290 })?;
291 let (_body, source_map) = db.body_with_source_map(parent);
292 let src = src.cloned().map(ast::Pat::from);
293 let pat_id = source_map.node_pat(src.as_ref())?;
294 Some(Local { parent: parent.into(), pat_id })
295 }
296}
297
298fn find_root(node: &SyntaxNode) -> SyntaxNode {
299 node.ancestors().last().unwrap()
300}
301
302pub struct SemanticsScope<'a, DB> {
303 pub db: &'a DB,
304 resolver: Resolver,
305}
306
307impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> {
308 pub fn module(&self) -> Option<Module> {
309 Some(Module { id: self.resolver.module()? })
310 }
311
312 /// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type
313 // FIXME: rename to visible_traits to not repeat scope?
314 pub fn traits_in_scope(&self) -> FxHashSet<TraitId> {
315 let resolver = &self.resolver;
316 resolver.traits_in_scope(self.db)
317 }
318
319 pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
320 let resolver = &self.resolver;
321
322 resolver.process_all_names(self.db, &mut |name, def| {
323 let def = match def {
324 resolver::ScopeDef::PerNs(it) => it.into(),
325 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
326 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
327 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
328 resolver::ScopeDef::Local(pat_id) => {
329 let parent = resolver.body_owner().unwrap().into();
330 ScopeDef::Local(Local { parent, pat_id })
331 }
332 };
333 f(name, def)
334 })
335 }
336
337 pub fn resolve_hir_path(&self, path: &Path) -> Option<PathResolution> {
338 resolve_hir_path(self.db, &self.resolver, path)
339 }
340}
341
342// FIXME: Change `HasSource` trait to work with `Semantics` and remove this?
343pub fn original_range(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> FileRange {
344 if let Some(range) = original_range_opt(db, node) {
345 let original_file = range.file_id.original_file(db);
346 if range.file_id == original_file.into() {
347 return FileRange { file_id: original_file, range: range.value };
348 }
349
350 log::error!("Fail to mapping up more for {:?}", range);
351 return FileRange { file_id: range.file_id.original_file(db), range: range.value };
352 }
353
354 // Fall back to whole macro call
355 if let Some(expansion) = node.file_id.expansion_info(db) {
356 if let Some(call_node) = expansion.call_node() {
357 return FileRange {
358 file_id: call_node.file_id.original_file(db),
359 range: call_node.value.text_range(),
360 };
361 }
362 }
363
364 FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
365}
366
367fn original_range_opt(
368 db: &impl HirDatabase,
369 node: InFile<&SyntaxNode>,
370) -> Option<InFile<TextRange>> {
371 let expansion = node.file_id.expansion_info(db)?;
372
373 // the input node has only one token ?
374 let single = skip_trivia_token(node.value.first_token()?, Direction::Next)?
375 == skip_trivia_token(node.value.last_token()?, Direction::Prev)?;
376
377 Some(node.value.descendants().find_map(|it| {
378 let first = skip_trivia_token(it.first_token()?, Direction::Next)?;
379 let first = ascend_call_token(db, &expansion, node.with_value(first))?;
380
381 let last = skip_trivia_token(it.last_token()?, Direction::Prev)?;
382 let last = ascend_call_token(db, &expansion, node.with_value(last))?;
383
384 if (!single && first == last) || (first.file_id != last.file_id) {
385 return None;
386 }
387
388 Some(first.with_value(first.value.text_range().extend_to(&last.value.text_range())))
389 })?)
390}
391
392fn ascend_call_token(
393 db: &impl HirDatabase,
394 expansion: &ExpansionInfo,
395 token: InFile<SyntaxToken>,
396) -> Option<InFile<SyntaxToken>> {
397 let (mapped, origin) = expansion.map_token_up(token.as_ref())?;
398 if origin != Origin::Call {
399 return None;
400 }
401 if let Some(info) = mapped.file_id.expansion_info(db) {
402 return ascend_call_token(db, &info, mapped);
403 }
404 Some(mapped)
405}
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index efa3f8a79..c650a9e08 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -11,32 +11,31 @@ use either::Either;
11use hir_def::{ 11use hir_def::{
12 body::{ 12 body::{
13 scope::{ExprScopes, ScopeId}, 13 scope::{ExprScopes, ScopeId},
14 BodySourceMap, 14 Body, BodySourceMap,
15 }, 15 },
16 expr::{ExprId, PatId}, 16 expr::{ExprId, Pat, PatId},
17 resolver::{self, resolver_for_scope, Resolver, TypeNs, ValueNs}, 17 resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
18 AsMacroCall, DefWithBodyId, TraitId, 18 AsMacroCall, DefWithBodyId,
19}; 19};
20use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile, MacroCallId}; 20use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
21use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment}; 21use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment};
22use ra_syntax::{ 22use ra_syntax::{
23 ast::{self, AstNode}, 23 ast::{self, AstNode},
24 AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit, 24 AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
25}; 25};
26use rustc_hash::FxHashSet;
27 26
28use crate::{ 27use crate::{
29 db::HirDatabase, Adt, Const, DefWithBody, EnumVariant, Function, Local, MacroDef, Name, Path, 28 db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, ModuleDef, Path, Static,
30 ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, 29 Struct, Trait, Type, TypeAlias, TypeParam,
31}; 30};
32 31
33/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of 32/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
34/// original source files. It should not be used inside the HIR itself. 33/// original source files. It should not be used inside the HIR itself.
35#[derive(Debug)] 34#[derive(Debug)]
36pub struct SourceAnalyzer { 35pub(crate) struct SourceAnalyzer {
37 file_id: HirFileId, 36 file_id: HirFileId,
38 resolver: Resolver, 37 pub(crate) resolver: Resolver,
39 body_owner: Option<DefWithBody>, 38 body: Option<Arc<Body>>,
40 body_source_map: Option<Arc<BodySourceMap>>, 39 body_source_map: Option<Arc<BodySourceMap>>,
41 infer: Option<Arc<InferenceResult>>, 40 infer: Option<Arc<InferenceResult>>,
42 scopes: Option<Arc<ExprScopes>>, 41 scopes: Option<Arc<ExprScopes>>,
@@ -55,64 +54,20 @@ pub enum PathResolution {
55 AssocItem(crate::AssocItem), 54 AssocItem(crate::AssocItem),
56} 55}
57 56
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct ScopeEntryWithSyntax {
60 pub(crate) name: Name,
61 pub(crate) ptr: Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>,
62}
63
64impl ScopeEntryWithSyntax {
65 pub fn name(&self) -> &Name {
66 &self.name
67 }
68
69 pub fn ptr(&self) -> Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>> {
70 self.ptr
71 }
72}
73
74#[derive(Debug)] 57#[derive(Debug)]
75pub struct ReferenceDescriptor { 58pub struct ReferenceDescriptor {
76 pub range: TextRange, 59 pub range: TextRange,
77 pub name: String, 60 pub name: String,
78} 61}
79 62
80#[derive(Debug)]
81pub struct Expansion {
82 macro_call_id: MacroCallId,
83}
84
85impl Expansion {
86 pub fn map_token_down(
87 &self,
88 db: &impl HirDatabase,
89 token: InFile<&SyntaxToken>,
90 ) -> Option<InFile<SyntaxToken>> {
91 let exp_info = self.file_id().expansion_info(db)?;
92 exp_info.map_token_down(token)
93 }
94
95 pub fn file_id(&self) -> HirFileId {
96 self.macro_call_id.as_file()
97 }
98}
99
100impl SourceAnalyzer { 63impl SourceAnalyzer {
101 pub fn new(
102 db: &impl HirDatabase,
103 node: InFile<&SyntaxNode>,
104 offset: Option<TextUnit>,
105 ) -> SourceAnalyzer {
106 crate::source_binder::SourceBinder::new(db).analyze(node, offset)
107 }
108
109 pub(crate) fn new_for_body( 64 pub(crate) fn new_for_body(
110 db: &impl HirDatabase, 65 db: &impl HirDatabase,
111 def: DefWithBodyId, 66 def: DefWithBodyId,
112 node: InFile<&SyntaxNode>, 67 node: InFile<&SyntaxNode>,
113 offset: Option<TextUnit>, 68 offset: Option<TextUnit>,
114 ) -> SourceAnalyzer { 69 ) -> SourceAnalyzer {
115 let (_body, source_map) = db.body_with_source_map(def); 70 let (body, source_map) = db.body_with_source_map(def);
116 let scopes = db.expr_scopes(def); 71 let scopes = db.expr_scopes(def);
117 let scope = match offset { 72 let scope = match offset {
118 None => scope_for(&scopes, &source_map, node), 73 None => scope_for(&scopes, &source_map, node),
@@ -121,7 +76,7 @@ impl SourceAnalyzer {
121 let resolver = resolver_for_scope(db, def, scope); 76 let resolver = resolver_for_scope(db, def, scope);
122 SourceAnalyzer { 77 SourceAnalyzer {
123 resolver, 78 resolver,
124 body_owner: Some(def.into()), 79 body: Some(body),
125 body_source_map: Some(source_map), 80 body_source_map: Some(source_map),
126 infer: Some(db.infer(def)), 81 infer: Some(db.infer(def)),
127 scopes: Some(scopes), 82 scopes: Some(scopes),
@@ -135,7 +90,7 @@ impl SourceAnalyzer {
135 ) -> SourceAnalyzer { 90 ) -> SourceAnalyzer {
136 SourceAnalyzer { 91 SourceAnalyzer {
137 resolver, 92 resolver,
138 body_owner: None, 93 body: None,
139 body_source_map: None, 94 body_source_map: None,
140 infer: None, 95 infer: None,
141 scopes: None, 96 scopes: None,
@@ -143,10 +98,6 @@ impl SourceAnalyzer {
143 } 98 }
144 } 99 }
145 100
146 pub fn module(&self) -> Option<crate::code_model::Module> {
147 Some(crate::code_model::Module { id: self.resolver.module()? })
148 }
149
150 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { 101 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {
151 let src = InFile { file_id: self.file_id, value: expr }; 102 let src = InFile { file_id: self.file_id, value: expr };
152 self.body_source_map.as_ref()?.node_expr(src) 103 self.body_source_map.as_ref()?.node_expr(src)
@@ -180,7 +131,7 @@ impl SourceAnalyzer {
180 TraitEnvironment::lower(db, &self.resolver) 131 TraitEnvironment::lower(db, &self.resolver)
181 } 132 }
182 133
183 pub fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { 134 pub(crate) fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> {
184 let expr_id = if let Some(expr) = self.expand_expr(db, InFile::new(self.file_id, expr)) { 135 let expr_id = if let Some(expr) = self.expand_expr(db, InFile::new(self.file_id, expr)) {
185 self.body_source_map.as_ref()?.node_expr(expr.as_ref())? 136 self.body_source_map.as_ref()?.node_expr(expr.as_ref())?
186 } else { 137 } else {
@@ -192,24 +143,27 @@ impl SourceAnalyzer {
192 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) 143 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } })
193 } 144 }
194 145
195 pub fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option<Type> { 146 pub(crate) fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option<Type> {
196 let pat_id = self.pat_id(pat)?; 147 let pat_id = self.pat_id(pat)?;
197 let ty = self.infer.as_ref()?[pat_id].clone(); 148 let ty = self.infer.as_ref()?[pat_id].clone();
198 let environment = self.trait_env(db); 149 let environment = self.trait_env(db);
199 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) 150 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } })
200 } 151 }
201 152
202 pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { 153 pub(crate) fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
203 let expr_id = self.expr_id(&call.clone().into())?; 154 let expr_id = self.expr_id(&call.clone().into())?;
204 self.infer.as_ref()?.method_resolution(expr_id).map(Function::from) 155 self.infer.as_ref()?.method_resolution(expr_id).map(Function::from)
205 } 156 }
206 157
207 pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { 158 pub(crate) fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> {
208 let expr_id = self.expr_id(&field.clone().into())?; 159 let expr_id = self.expr_id(&field.clone().into())?;
209 self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) 160 self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into())
210 } 161 }
211 162
212 pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { 163 pub(crate) fn resolve_record_field(
164 &self,
165 field: &ast::RecordField,
166 ) -> Option<crate::StructField> {
213 let expr_id = match field.expr() { 167 let expr_id = match field.expr() {
214 Some(it) => self.expr_id(&it)?, 168 Some(it) => self.expr_id(&it)?,
215 None => { 169 None => {
@@ -220,17 +174,23 @@ impl SourceAnalyzer {
220 self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into()) 174 self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into())
221 } 175 }
222 176
223 pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<crate::VariantDef> { 177 pub(crate) fn resolve_record_literal(
178 &self,
179 record_lit: &ast::RecordLit,
180 ) -> Option<crate::VariantDef> {
224 let expr_id = self.expr_id(&record_lit.clone().into())?; 181 let expr_id = self.expr_id(&record_lit.clone().into())?;
225 self.infer.as_ref()?.variant_resolution_for_expr(expr_id).map(|it| it.into()) 182 self.infer.as_ref()?.variant_resolution_for_expr(expr_id).map(|it| it.into())
226 } 183 }
227 184
228 pub fn resolve_record_pattern(&self, record_pat: &ast::RecordPat) -> Option<crate::VariantDef> { 185 pub(crate) fn resolve_record_pattern(
186 &self,
187 record_pat: &ast::RecordPat,
188 ) -> Option<crate::VariantDef> {
229 let pat_id = self.pat_id(&record_pat.clone().into())?; 189 let pat_id = self.pat_id(&record_pat.clone().into())?;
230 self.infer.as_ref()?.variant_resolution_for_pat(pat_id).map(|it| it.into()) 190 self.infer.as_ref()?.variant_resolution_for_pat(pat_id).map(|it| it.into())
231 } 191 }
232 192
233 pub fn resolve_macro_call( 193 pub(crate) fn resolve_macro_call(
234 &self, 194 &self,
235 db: &impl HirDatabase, 195 db: &impl HirDatabase,
236 macro_call: InFile<&ast::MacroCall>, 196 macro_call: InFile<&ast::MacroCall>,
@@ -240,52 +200,29 @@ impl SourceAnalyzer {
240 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into()) 200 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into())
241 } 201 }
242 202
243 pub fn resolve_hir_path( 203 pub(crate) fn resolve_bind_pat_to_const(
244 &self, 204 &self,
245 db: &impl HirDatabase, 205 db: &impl HirDatabase,
246 path: &crate::Path, 206 pat: &ast::BindPat,
247 ) -> Option<PathResolution> { 207 ) -> Option<ModuleDef> {
248 let types = 208 let pat_id = self.pat_id(&pat.clone().into())?;
249 self.resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty { 209 let body = self.body.as_ref()?;
250 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), 210 let path = match &body[pat_id] {
251 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), 211 Pat::Path(path) => path,
252 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { 212 _ => return None,
253 PathResolution::Def(Adt::from(it).into()) 213 };
254 } 214 let res = resolve_hir_path(db, &self.resolver, &path)?;
255 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), 215 match res {
256 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), 216 PathResolution::Def(def) => Some(def),
257 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 217 _ => None,
258 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), 218 }
259 });
260 let values =
261 self.resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| {
262 let res = match val {
263 ValueNs::LocalBinding(pat_id) => {
264 let var = Local { parent: self.body_owner?, pat_id };
265 PathResolution::Local(var)
266 }
267 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
268 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
269 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
270 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
271 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
272 };
273 Some(res)
274 });
275
276 let items = self
277 .resolver
278 .resolve_module_path_in_items(db, path.mod_path())
279 .take_types()
280 .map(|it| PathResolution::Def(it.into()));
281 types.or(values).or(items).or_else(|| {
282 self.resolver
283 .resolve_path_as_macro(db, path.mod_path())
284 .map(|def| PathResolution::Macro(def.into()))
285 })
286 } 219 }
287 220
288 pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> { 221 pub(crate) fn resolve_path(
222 &self,
223 db: &impl HirDatabase,
224 path: &ast::Path,
225 ) -> Option<PathResolution> {
289 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { 226 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
290 let expr_id = self.expr_id(&path_expr.into())?; 227 let expr_id = self.expr_id(&path_expr.into())?;
291 if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { 228 if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) {
@@ -300,40 +237,24 @@ impl SourceAnalyzer {
300 } 237 }
301 // This must be a normal source file rather than macro file. 238 // This must be a normal source file rather than macro file.
302 let hir_path = crate::Path::from_ast(path.clone())?; 239 let hir_path = crate::Path::from_ast(path.clone())?;
303 self.resolve_hir_path(db, &hir_path) 240 resolve_hir_path(db, &self.resolver, &hir_path)
304 } 241 }
305 242
306 fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> { 243 fn resolve_local_name(
244 &self,
245 name_ref: &ast::NameRef,
246 ) -> Option<Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>> {
307 let name = name_ref.as_name(); 247 let name = name_ref.as_name();
308 let source_map = self.body_source_map.as_ref()?; 248 let source_map = self.body_source_map.as_ref()?;
309 let scopes = self.scopes.as_ref()?; 249 let scopes = self.scopes.as_ref()?;
310 let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?; 250 let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?;
311 let entry = scopes.resolve_name_in_scope(scope, &name)?; 251 let entry = scopes.resolve_name_in_scope(scope, &name)?;
312 Some(ScopeEntryWithSyntax { 252 Some(source_map.pat_syntax(entry.pat())?.value)
313 name: entry.name().clone(),
314 ptr: source_map.pat_syntax(entry.pat())?.value,
315 })
316 }
317
318 pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
319 self.resolver.process_all_names(db, &mut |name, def| {
320 let def = match def {
321 resolver::ScopeDef::PerNs(it) => it.into(),
322 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
323 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
324 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
325 resolver::ScopeDef::Local(pat_id) => {
326 let parent = self.resolver.body_owner().unwrap().into();
327 ScopeDef::Local(Local { parent, pat_id })
328 }
329 };
330 f(name, def)
331 })
332 } 253 }
333 254
334 // FIXME: we only use this in `inline_local_variable` assist, ideally, we 255 // FIXME: we only use this in `inline_local_variable` assist, ideally, we
335 // should switch to general reference search infra there. 256 // should switch to general reference search infra there.
336 pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { 257 pub(crate) fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
337 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); 258 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
338 let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone()))); 259 let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone())));
339 fn_def 260 fn_def
@@ -342,7 +263,7 @@ impl SourceAnalyzer {
342 .filter_map(ast::NameRef::cast) 263 .filter_map(ast::NameRef::cast)
343 .filter(|name_ref| match self.resolve_local_name(&name_ref) { 264 .filter(|name_ref| match self.resolve_local_name(&name_ref) {
344 None => false, 265 None => false,
345 Some(entry) => entry.ptr() == ptr, 266 Some(d_ptr) => d_ptr == ptr,
346 }) 267 })
347 .map(|name_ref| ReferenceDescriptor { 268 .map(|name_ref| ReferenceDescriptor {
348 name: name_ref.text().to_string(), 269 name: name_ref.text().to_string(),
@@ -351,19 +272,14 @@ impl SourceAnalyzer {
351 .collect() 272 .collect()
352 } 273 }
353 274
354 /// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type 275 pub(crate) fn expand(
355 pub fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet<TraitId> {
356 self.resolver.traits_in_scope(db)
357 }
358
359 pub fn expand(
360 &self, 276 &self,
361 db: &impl HirDatabase, 277 db: &impl HirDatabase,
362 macro_call: InFile<&ast::MacroCall>, 278 macro_call: InFile<&ast::MacroCall>,
363 ) -> Option<Expansion> { 279 ) -> Option<HirFileId> {
364 let macro_call_id = 280 let macro_call_id =
365 macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?; 281 macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?;
366 Some(Expansion { macro_call_id }) 282 Some(macro_call_id.as_file())
367 } 283 }
368} 284}
369 285
@@ -409,6 +325,47 @@ fn scope_for_offset(
409 }) 325 })
410} 326}
411 327
328pub(crate) fn resolve_hir_path(
329 db: &impl HirDatabase,
330 resolver: &Resolver,
331 path: &crate::Path,
332) -> Option<PathResolution> {
333 let types = resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty {
334 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
335 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
336 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
337 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
338 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
339 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
340 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
341 });
342 let body_owner = resolver.body_owner();
343 let values = resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| {
344 let res = match val {
345 ValueNs::LocalBinding(pat_id) => {
346 let var = Local { parent: body_owner?.into(), pat_id };
347 PathResolution::Local(var)
348 }
349 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
350 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
351 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
352 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
353 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
354 };
355 Some(res)
356 });
357
358 let items = resolver
359 .resolve_module_path_in_items(db, path.mod_path())
360 .take_types()
361 .map(|it| PathResolution::Def(it.into()));
362 types.or(values).or(items).or_else(|| {
363 resolver
364 .resolve_path_as_macro(db, path.mod_path())
365 .map(|def| PathResolution::Macro(def.into()))
366 })
367}
368
412// XXX: during completion, cursor might be outside of any particular 369// XXX: during completion, cursor might be outside of any particular
413// expression. Try to figure out the correct scope... 370// expression. Try to figure out the correct scope...
414fn adjust( 371fn adjust(
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index f3150f578..4353e25ac 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -5,112 +5,89 @@ use hir_def::{
5 child_by_source::ChildBySource, 5 child_by_source::ChildBySource,
6 dyn_map::DynMap, 6 dyn_map::DynMap,
7 keys::{self, Key}, 7 keys::{self, Key},
8 resolver::{HasResolver, Resolver},
9 ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, 8 ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
10 StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, 9 StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
11}; 10};
12use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; 11use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind};
12use ra_db::FileId;
13use ra_prof::profile; 13use ra_prof::profile;
14use ra_syntax::{ 14use ra_syntax::{
15 ast::{self, NameOwner}, 15 ast::{self, NameOwner},
16 match_ast, AstNode, SyntaxNode, TextUnit, 16 match_ast, AstNode, SyntaxNode,
17}; 17};
18use rustc_hash::FxHashMap; 18use rustc_hash::FxHashMap;
19 19
20use crate::{db::HirDatabase, Local, Module, SourceAnalyzer, TypeParam}; 20use crate::{db::HirDatabase, Module};
21use ra_db::FileId;
22 21
23pub struct SourceBinder<'a, DB> { 22pub(crate) struct SourceBinder {
24 pub db: &'a DB,
25 child_by_source_cache: FxHashMap<ChildContainer, DynMap>, 23 child_by_source_cache: FxHashMap<ChildContainer, DynMap>,
26} 24}
27 25
28impl<DB: HirDatabase> SourceBinder<'_, DB> { 26impl SourceBinder {
29 pub fn new(db: &DB) -> SourceBinder<DB> { 27 pub(crate) fn new() -> SourceBinder {
30 SourceBinder { db, child_by_source_cache: FxHashMap::default() } 28 SourceBinder { child_by_source_cache: FxHashMap::default() }
31 }
32
33 pub fn analyze(
34 &mut self,
35 src: InFile<&SyntaxNode>,
36 offset: Option<TextUnit>,
37 ) -> SourceAnalyzer {
38 let _p = profile("SourceBinder::analyzer");
39 let container = match self.find_container(src) {
40 Some(it) => it,
41 None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
42 };
43
44 let resolver = match container {
45 ChildContainer::DefWithBodyId(def) => {
46 return SourceAnalyzer::new_for_body(self.db, def, src, offset)
47 }
48 ChildContainer::TraitId(it) => it.resolver(self.db),
49 ChildContainer::ImplId(it) => it.resolver(self.db),
50 ChildContainer::ModuleId(it) => it.resolver(self.db),
51 ChildContainer::EnumId(it) => it.resolver(self.db),
52 ChildContainer::VariantId(it) => it.resolver(self.db),
53 ChildContainer::GenericDefId(it) => it.resolver(self.db),
54 };
55 SourceAnalyzer::new_for_resolver(resolver, src)
56 }
57
58 pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
59 T::to_def(self, src)
60 } 29 }
61 30
62 pub fn to_module_def(&mut self, file: FileId) -> Option<Module> { 31 pub(crate) fn to_module_def(&mut self, db: &impl HirDatabase, file: FileId) -> Option<Module> {
63 let _p = profile("SourceBinder::to_module_def"); 32 let _p = profile("SourceBinder::to_module_def");
64 let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { 33 let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| {
65 let crate_def_map = self.db.crate_def_map(crate_id); 34 let crate_def_map = db.crate_def_map(crate_id);
66 let local_id = crate_def_map.modules_for_file(file).next()?; 35 let local_id = crate_def_map.modules_for_file(file).next()?;
67 Some((crate_id, local_id)) 36 Some((crate_id, local_id))
68 })?; 37 })?;
69 Some(Module { id: ModuleId { krate, local_id } }) 38 Some(Module { id: ModuleId { krate, local_id } })
70 } 39 }
71 40
72 fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> { 41 pub(crate) fn to_id<T: ToId>(
73 T::to_id(self, src) 42 &mut self,
43 db: &impl HirDatabase,
44 src: InFile<T>,
45 ) -> Option<T::ID> {
46 T::to_id(db, self, src)
74 } 47 }
75 48
76 fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { 49 pub(crate) fn find_container(
77 for container in src.cloned().ancestors_with_macros(self.db).skip(1) { 50 &mut self,
51 db: &impl HirDatabase,
52 src: InFile<&SyntaxNode>,
53 ) -> Option<ChildContainer> {
54 for container in src.cloned().ancestors_with_macros(db).skip(1) {
78 let res: ChildContainer = match_ast! { 55 let res: ChildContainer = match_ast! {
79 match (container.value) { 56 match (container.value) {
80 ast::TraitDef(it) => { 57 ast::TraitDef(it) => {
81 let def: TraitId = self.to_id(container.with_value(it))?; 58 let def: TraitId = self.to_id(db, container.with_value(it))?;
82 def.into() 59 def.into()
83 }, 60 },
84 ast::ImplBlock(it) => { 61 ast::ImplBlock(it) => {
85 let def: ImplId = self.to_id(container.with_value(it))?; 62 let def: ImplId = self.to_id(db, container.with_value(it))?;
86 def.into() 63 def.into()
87 }, 64 },
88 ast::FnDef(it) => { 65 ast::FnDef(it) => {
89 let def: FunctionId = self.to_id(container.with_value(it))?; 66 let def: FunctionId = self.to_id(db, container.with_value(it))?;
90 DefWithBodyId::from(def).into() 67 DefWithBodyId::from(def).into()
91 }, 68 },
92 ast::StaticDef(it) => { 69 ast::StaticDef(it) => {
93 let def: StaticId = self.to_id(container.with_value(it))?; 70 let def: StaticId = self.to_id(db, container.with_value(it))?;
94 DefWithBodyId::from(def).into() 71 DefWithBodyId::from(def).into()
95 }, 72 },
96 ast::ConstDef(it) => { 73 ast::ConstDef(it) => {
97 let def: ConstId = self.to_id(container.with_value(it))?; 74 let def: ConstId = self.to_id(db, container.with_value(it))?;
98 DefWithBodyId::from(def).into() 75 DefWithBodyId::from(def).into()
99 }, 76 },
100 ast::EnumDef(it) => { 77 ast::EnumDef(it) => {
101 let def: EnumId = self.to_id(container.with_value(it))?; 78 let def: EnumId = self.to_id(db, container.with_value(it))?;
102 def.into() 79 def.into()
103 }, 80 },
104 ast::StructDef(it) => { 81 ast::StructDef(it) => {
105 let def: StructId = self.to_id(container.with_value(it))?; 82 let def: StructId = self.to_id(db, container.with_value(it))?;
106 VariantId::from(def).into() 83 VariantId::from(def).into()
107 }, 84 },
108 ast::UnionDef(it) => { 85 ast::UnionDef(it) => {
109 let def: UnionId = self.to_id(container.with_value(it))?; 86 let def: UnionId = self.to_id(db, container.with_value(it))?;
110 VariantId::from(def).into() 87 VariantId::from(def).into()
111 }, 88 },
112 ast::Module(it) => { 89 ast::Module(it) => {
113 let def: ModuleId = self.to_id(container.with_value(it))?; 90 let def: ModuleId = self.to_id(db, container.with_value(it))?;
114 def.into() 91 def.into()
115 }, 92 },
116 _ => { continue }, 93 _ => { continue },
@@ -119,12 +96,11 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
119 return Some(res); 96 return Some(res);
120 } 97 }
121 98
122 let c = self.to_module_def(src.file_id.original_file(self.db))?; 99 let c = self.to_module_def(db, src.file_id.original_file(db))?;
123 Some(c.id.into()) 100 Some(c.id.into())
124 } 101 }
125 102
126 fn child_by_source(&mut self, container: ChildContainer) -> &DynMap { 103 fn child_by_source(&mut self, db: &impl HirDatabase, container: ChildContainer) -> &DynMap {
127 let db = self.db;
128 self.child_by_source_cache.entry(container).or_insert_with(|| match container { 104 self.child_by_source_cache.entry(container).or_insert_with(|| match container {
129 ChildContainer::DefWithBodyId(it) => it.child_by_source(db), 105 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
130 ChildContainer::ModuleId(it) => it.child_by_source(db), 106 ChildContainer::ModuleId(it) => it.child_by_source(db),
@@ -137,49 +113,17 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
137 } 113 }
138} 114}
139 115
140pub trait ToId: Sized { 116pub(crate) trait ToId: Sized {
141 type ID: Sized + Copy + 'static; 117 type ID: Sized + Copy + 'static;
142 fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>) 118 fn to_id<DB: HirDatabase>(
143 -> Option<Self::ID>; 119 db: &DB,
144} 120 sb: &mut SourceBinder,
145
146pub trait ToDef: Sized + AstNode + 'static {
147 type Def;
148 fn to_def<DB: HirDatabase>(
149 sb: &mut SourceBinder<'_, DB>,
150 src: InFile<Self>, 121 src: InFile<Self>,
151 ) -> Option<Self::Def>; 122 ) -> Option<Self::ID>;
152}
153
154macro_rules! to_def_impls {
155 ($(($def:path, $ast:path)),* ,) => {$(
156 impl ToDef for $ast {
157 type Def = $def;
158 fn to_def<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
159 -> Option<Self::Def>
160 { sb.to_id(src).map(Into::into) }
161 }
162 )*}
163} 123}
164 124
165to_def_impls![
166 (crate::Module, ast::Module),
167 (crate::Struct, ast::StructDef),
168 (crate::Enum, ast::EnumDef),
169 (crate::Union, ast::UnionDef),
170 (crate::Trait, ast::TraitDef),
171 (crate::ImplBlock, ast::ImplBlock),
172 (crate::TypeAlias, ast::TypeAliasDef),
173 (crate::Const, ast::ConstDef),
174 (crate::Static, ast::StaticDef),
175 (crate::Function, ast::FnDef),
176 (crate::StructField, ast::RecordFieldDef),
177 (crate::EnumVariant, ast::EnumVariant),
178 (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
179];
180
181#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 125#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
182enum ChildContainer { 126pub(crate) enum ChildContainer {
183 DefWithBodyId(DefWithBodyId), 127 DefWithBodyId(DefWithBodyId),
184 ModuleId(ModuleId), 128 ModuleId(ModuleId),
185 TraitId(TraitId), 129 TraitId(TraitId),
@@ -201,7 +145,7 @@ impl_froms! {
201 GenericDefId 145 GenericDefId
202} 146}
203 147
204pub trait ToIdByKey: Sized + AstNode + 'static { 148pub(crate) trait ToIdByKey: Sized + AstNode + 'static {
205 type ID: Sized + Copy + 'static; 149 type ID: Sized + Copy + 'static;
206 const KEY: Key<Self, Self::ID>; 150 const KEY: Key<Self, Self::ID>;
207} 151}
@@ -209,11 +153,11 @@ pub trait ToIdByKey: Sized + AstNode + 'static {
209impl<T: ToIdByKey> ToId for T { 153impl<T: ToIdByKey> ToId for T {
210 type ID = <T as ToIdByKey>::ID; 154 type ID = <T as ToIdByKey>::ID;
211 fn to_id<DB: HirDatabase>( 155 fn to_id<DB: HirDatabase>(
212 sb: &mut SourceBinder<'_, DB>, 156 db: &DB,
157 sb: &mut SourceBinder,
213 src: InFile<Self>, 158 src: InFile<Self>,
214 ) -> Option<Self::ID> { 159 ) -> Option<Self::ID> {
215 let container = sb.find_container(src.as_ref().map(|it| it.syntax()))?; 160 let container = sb.find_container(db, src.as_ref().map(|it| it.syntax()))?;
216 let db = sb.db;
217 let dyn_map = 161 let dyn_map =
218 &*sb.child_by_source_cache.entry(container).or_insert_with(|| match container { 162 &*sb.child_by_source_cache.entry(container).or_insert_with(|| match container {
219 ChildContainer::DefWithBodyId(it) => it.child_by_source(db), 163 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
@@ -255,68 +199,44 @@ to_id_key_impls![
255impl ToId for ast::MacroCall { 199impl ToId for ast::MacroCall {
256 type ID = MacroDefId; 200 type ID = MacroDefId;
257 fn to_id<DB: HirDatabase>( 201 fn to_id<DB: HirDatabase>(
258 sb: &mut SourceBinder<'_, DB>, 202 db: &DB,
203 sb: &mut SourceBinder,
259 src: InFile<Self>, 204 src: InFile<Self>,
260 ) -> Option<Self::ID> { 205 ) -> Option<Self::ID> {
261 let kind = MacroDefKind::Declarative; 206 let kind = MacroDefKind::Declarative;
262 207
263 let krate = sb.to_module_def(src.file_id.original_file(sb.db))?.id.krate; 208 let krate = sb.to_module_def(db, src.file_id.original_file(db))?.id.krate;
264 209
265 let ast_id = 210 let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
266 Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value)));
267 211
268 Some(MacroDefId { krate: Some(krate), ast_id, kind }) 212 Some(MacroDefId { krate: Some(krate), ast_id, kind })
269 } 213 }
270} 214}
271 215
272impl ToDef for ast::BindPat { 216impl ToId for ast::TypeParam {
273 type Def = Local; 217 type ID = TypeParamId;
274
275 fn to_def<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>) -> Option<Local> {
276 let file_id = src.file_id;
277 let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
278 let res = match_ast! {
279 match it {
280 ast::ConstDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
281 ast::StaticDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
282 ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
283 _ => return None,
284 }
285 };
286 Some(res)
287 })?;
288 let (_body, source_map) = sb.db.body_with_source_map(parent);
289 let src = src.map(ast::Pat::from);
290 let pat_id = source_map.node_pat(src.as_ref())?;
291 Some(Local { parent: parent.into(), pat_id })
292 }
293}
294
295impl ToDef for ast::TypeParam {
296 type Def = TypeParam;
297 218
298 fn to_def<DB: HirDatabase>( 219 fn to_id<DB: HirDatabase>(
299 sb: &mut SourceBinder<'_, DB>, 220 db: &DB,
300 src: InFile<ast::TypeParam>, 221 sb: &mut SourceBinder,
301 ) -> Option<TypeParam> { 222 src: InFile<Self>,
302 let mut sb = SourceBinder::new(sb.db); 223 ) -> Option<Self::ID> {
303 let file_id = src.file_id; 224 let file_id = src.file_id;
304 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { 225 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
305 let res = match_ast! { 226 let res = match_ast! {
306 match it { 227 match it {
307 ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 228 ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
308 ast::StructDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 229 ast::StructDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
309 ast::EnumDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 230 ast::EnumDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
310 ast::TraitDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 231 ast::TraitDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
311 ast::TypeAliasDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 232 ast::TypeAliasDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
312 ast::ImplBlock(value) => { sb.to_id(InFile { value, file_id})?.into() }, 233 ast::ImplBlock(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
313 _ => return None, 234 _ => return None,
314 } 235 }
315 }; 236 };
316 Some(res) 237 Some(res)
317 })?; 238 })?;
318 let &id = sb.child_by_source(parent.into())[keys::TYPE_PARAM].get(&src)?; 239 sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src).copied()
319 Some(TypeParam { id })
320 } 240 }
321} 241}
322 242
@@ -324,7 +244,8 @@ impl ToId for ast::Module {
324 type ID = ModuleId; 244 type ID = ModuleId;
325 245
326 fn to_id<DB: HirDatabase>( 246 fn to_id<DB: HirDatabase>(
327 sb: &mut SourceBinder<'_, DB>, 247 db: &DB,
248 sb: &mut SourceBinder,
328 src: InFile<ast::Module>, 249 src: InFile<ast::Module>,
329 ) -> Option<ModuleId> { 250 ) -> Option<ModuleId> {
330 { 251 {
@@ -333,7 +254,7 @@ impl ToId for ast::Module {
333 .as_ref() 254 .as_ref()
334 .map(|it| it.syntax()) 255 .map(|it| it.syntax())
335 .cloned() 256 .cloned()
336 .ancestors_with_macros(sb.db) 257 .ancestors_with_macros(db)
337 .skip(1) 258 .skip(1)
338 .find_map(|it| { 259 .find_map(|it| {
339 let m = ast::Module::cast(it.value.clone())?; 260 let m = ast::Module::cast(it.value.clone())?;
@@ -341,15 +262,15 @@ impl ToId for ast::Module {
341 }); 262 });
342 263
343 let parent_module = match parent_declaration { 264 let parent_module = match parent_declaration {
344 Some(parent_declaration) => sb.to_id(parent_declaration)?, 265 Some(parent_declaration) => sb.to_id(db, parent_declaration)?,
345 None => { 266 None => {
346 let file_id = src.file_id.original_file(sb.db); 267 let file_id = src.file_id.original_file(db);
347 sb.to_module_def(file_id)?.id 268 sb.to_module_def(db, file_id)?.id
348 } 269 }
349 }; 270 };
350 271
351 let child_name = src.value.name()?.as_name(); 272 let child_name = src.value.name()?.as_name();
352 let def_map = sb.db.crate_def_map(parent_module.krate); 273 let def_map = db.crate_def_map(parent_module.krate);
353 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; 274 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
354 Some(ModuleId { krate: parent_module.krate, local_id: child_id }) 275 Some(ModuleId { krate: parent_module.krate, local_id: child_id })
355 } 276 }