aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-21 12:50:12 +0000
committerGitHub <[email protected]>2019-11-21 12:50:12 +0000
commit1f61915bde4c5d8d4fe2e9b8dfa9445008730b79 (patch)
treea6fd0b6ad8b6c87d28e6e1b36bb9b2fbae2c1b09 /crates/ra_hir/src/source_binder.rs
parent612a72fc4ea4376920f2a7da7b3c334227c1716c (diff)
parentc37d1c5b381365ce2d07dfe4b871e43995ccea2d (diff)
Merge #2337
2337: Move resolver to hir_def r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs75
1 files changed, 49 insertions, 26 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 727310f06..c42ceabdf 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -10,6 +10,8 @@ use std::sync::Arc;
10use hir_def::{ 10use hir_def::{
11 expr::{ExprId, PatId}, 11 expr::{ExprId, PatId},
12 path::known, 12 path::known,
13 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
14 DefWithBodyId,
13}; 15};
14use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; 16use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source};
15use ra_syntax::{ 17use ra_syntax::{
@@ -21,12 +23,12 @@ use ra_syntax::{
21 23
22use crate::{ 24use crate::{
23 db::HirDatabase, 25 db::HirDatabase,
24 expr::{self, BodySourceMap, ExprScopes, ScopeId}, 26 expr::{BodySourceMap, ExprScopes, ScopeId},
25 ids::LocationCtx, 27 ids::LocationCtx,
26 resolve::{HasResolver, ScopeDef, TypeNs, ValueNs},
27 ty::method_resolution::{self, implements_trait}, 28 ty::method_resolution::{self, implements_trait},
28 AssocItem, Const, DefWithBody, Either, Enum, FromSource, Function, GenericParam, HasBody, 29 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
29 HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, Struct, Ty, 30 GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, ScopeDef, Static,
31 Struct, Trait, Ty, TypeAlias,
30}; 32};
31 33
32fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { 34fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@@ -34,23 +36,25 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -
34 match (node.value) { 36 match (node.value) {
35 ast::Module(it) => { 37 ast::Module(it) => {
36 let src = node.with_value(it); 38 let src = node.with_value(it);
37 Some(crate::Module::from_declaration(db, src)?.resolver(db)) 39 Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
38 }, 40 },
39 ast::SourceFile(it) => { 41 ast::SourceFile(it) => {
40 let src = node.with_value(crate::ModuleSource::SourceFile(it)); 42 let src = node.with_value(crate::ModuleSource::SourceFile(it));
41 Some(crate::Module::from_definition(db, src)?.resolver(db)) 43 Some(crate::Module::from_definition(db, src)?.id.resolver(db))
42 }, 44 },
43 ast::StructDef(it) => { 45 ast::StructDef(it) => {
44 let src = node.with_value(it); 46 let src = node.with_value(it);
45 Some(Struct::from_source(db, src)?.resolver(db)) 47 Some(Struct::from_source(db, src)?.id.resolver(db))
46 }, 48 },
47 ast::EnumDef(it) => { 49 ast::EnumDef(it) => {
48 let src = node.with_value(it); 50 let src = node.with_value(it);
49 Some(Enum::from_source(db, src)?.resolver(db)) 51 Some(Enum::from_source(db, src)?.id.resolver(db))
50 }, 52 },
51 _ => match node.value.kind() { 53 _ => match node.value.kind() {
52 FN_DEF | CONST_DEF | STATIC_DEF => { 54 FN_DEF | CONST_DEF | STATIC_DEF => {
53 Some(def_with_body_from_child_node(db, node)?.resolver(db)) 55 let def = def_with_body_from_child_node(db, node)?;
56 let def = DefWithBodyId::from(def);
57 Some(def.resolver(db))
54 } 58 }
55 // FIXME add missing cases 59 // FIXME add missing cases
56 _ => None 60 _ => None
@@ -159,7 +163,7 @@ impl SourceAnalyzer {
159 None => scope_for(&scopes, &source_map, node), 163 None => scope_for(&scopes, &source_map, node),
160 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), 164 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
161 }; 165 };
162 let resolver = expr::resolver_for_scope(db, def, scope); 166 let resolver = resolver_for_scope(db, def.into(), scope);
163 SourceAnalyzer { 167 SourceAnalyzer {
164 resolver, 168 resolver,
165 body_owner: Some(def), 169 body_owner: Some(def),
@@ -231,7 +235,7 @@ impl SourceAnalyzer {
231 ) -> Option<MacroDef> { 235 ) -> Option<MacroDef> {
232 // This must be a normal source file rather than macro file. 236 // This must be a normal source file rather than macro file.
233 let path = macro_call.path().and_then(Path::from_ast)?; 237 let path = macro_call.path().and_then(Path::from_ast)?;
234 self.resolver.resolve_path_as_macro(db, &path) 238 self.resolver.resolve_path_as_macro(db, &path).map(|it| it.into())
235 } 239 }
236 240
237 pub fn resolve_hir_path( 241 pub fn resolve_hir_path(
@@ -240,16 +244,18 @@ impl SourceAnalyzer {
240 path: &crate::Path, 244 path: &crate::Path,
241 ) -> Option<PathResolution> { 245 ) -> Option<PathResolution> {
242 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { 246 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
243 TypeNs::SelfType(it) => PathResolution::SelfType(it), 247 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
244 TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam { 248 TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam {
245 parent: self.resolver.generic_def().unwrap(), 249 parent: self.resolver.generic_def().unwrap().into(),
246 idx, 250 idx,
247 }), 251 }),
248 TypeNs::AdtSelfType(it) | TypeNs::Adt(it) => PathResolution::Def(it.into()), 252 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
249 TypeNs::EnumVariant(it) => PathResolution::Def(it.into()), 253 PathResolution::Def(Adt::from(it).into())
250 TypeNs::TypeAlias(it) => PathResolution::Def(it.into()), 254 }
255 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
256 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
251 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 257 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
252 TypeNs::Trait(it) => PathResolution::Def(it.into()), 258 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
253 }); 259 });
254 let values = self.resolver.resolve_path_in_value_ns_fully(db, &path).and_then(|val| { 260 let values = self.resolver.resolve_path_in_value_ns_fully(db, &path).and_then(|val| {
255 let res = match val { 261 let res = match val {
@@ -257,11 +263,11 @@ impl SourceAnalyzer {
257 let var = Local { parent: self.body_owner?, pat_id }; 263 let var = Local { parent: self.body_owner?, pat_id };
258 PathResolution::Local(var) 264 PathResolution::Local(var)
259 } 265 }
260 ValueNs::Function(it) => PathResolution::Def(it.into()), 266 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
261 ValueNs::Const(it) => PathResolution::Def(it.into()), 267 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
262 ValueNs::Static(it) => PathResolution::Def(it.into()), 268 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
263 ValueNs::Struct(it) => PathResolution::Def(it.into()), 269 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
264 ValueNs::EnumVariant(it) => PathResolution::Def(it.into()), 270 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
265 }; 271 };
266 Some(res) 272 Some(res)
267 }); 273 });
@@ -272,7 +278,9 @@ impl SourceAnalyzer {
272 .take_types() 278 .take_types()
273 .map(|it| PathResolution::Def(it.into())); 279 .map(|it| PathResolution::Def(it.into()));
274 types.or(values).or(items).or_else(|| { 280 types.or(values).or(items).or_else(|| {
275 self.resolver.resolve_path_as_macro(db, &path).map(|def| PathResolution::Macro(def)) 281 self.resolver
282 .resolve_path_as_macro(db, &path)
283 .map(|def| PathResolution::Macro(def.into()))
276 }) 284 })
277 } 285 }
278 286
@@ -307,7 +315,22 @@ impl SourceAnalyzer {
307 } 315 }
308 316
309 pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { 317 pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
310 self.resolver.process_all_names(db, f) 318 self.resolver.process_all_names(db, &mut |name, def| {
319 let def = match def {
320 resolver::ScopeDef::PerNs(it) => it.into(),
321 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
322 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
323 resolver::ScopeDef::GenericParam(idx) => {
324 let parent = self.resolver.generic_def().unwrap().into();
325 ScopeDef::GenericParam(GenericParam { parent, idx })
326 }
327 resolver::ScopeDef::Local(pat_id) => {
328 let parent = self.resolver.body_owner().unwrap().into();
329 ScopeDef::Local(Local { parent, pat_id })
330 }
331 };
332 f(name, def)
333 })
311 } 334 }
312 335
313 // FIXME: we only use this in `inline_local_variable` assist, ideally, we 336 // FIXME: we only use this in `inline_local_variable` assist, ideally, we
@@ -392,7 +415,7 @@ impl SourceAnalyzer {
392 let std_future_path = known::std_future_future(); 415 let std_future_path = known::std_future_future();
393 416
394 let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) { 417 let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) {
395 Some(it) => it, 418 Some(it) => it.into(),
396 _ => return false, 419 _ => return false,
397 }; 420 };
398 421
@@ -402,7 +425,7 @@ impl SourceAnalyzer {
402 }; 425 };
403 426
404 let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 }; 427 let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 };
405 implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) 428 implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait)
406 } 429 }
407 430
408 pub fn expand( 431 pub fn expand(