diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 107 |
1 files changed, 43 insertions, 64 deletions
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 840785baa..7b5c3ec06 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -18,7 +18,7 @@ use crate::{ | |||
18 | path::{Path, PathKind}, | 18 | path::{Path, PathKind}, |
19 | AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, | 19 | AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, |
20 | FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, | 20 | FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, |
21 | TypeAliasId, UnionId, | 21 | TypeAliasId, |
22 | }; | 22 | }; |
23 | 23 | ||
24 | #[derive(Debug, Clone, Default)] | 24 | #[derive(Debug, Clone, Default)] |
@@ -369,47 +369,6 @@ impl Resolver { | |||
369 | } | 369 | } |
370 | } | 370 | } |
371 | 371 | ||
372 | impl Resolver { | ||
373 | pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver { | ||
374 | self.scopes.push(scope); | ||
375 | self | ||
376 | } | ||
377 | |||
378 | pub(crate) fn push_generic_params_scope( | ||
379 | self, | ||
380 | db: &impl DefDatabase2, | ||
381 | def: GenericDefId, | ||
382 | ) -> Resolver { | ||
383 | let params = db.generic_params(def); | ||
384 | if params.params.is_empty() { | ||
385 | self | ||
386 | } else { | ||
387 | self.push_scope(Scope::GenericParams { def, params }) | ||
388 | } | ||
389 | } | ||
390 | |||
391 | pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { | ||
392 | self.push_scope(Scope::ImplBlockScope(impl_block)) | ||
393 | } | ||
394 | |||
395 | pub(crate) fn push_module_scope( | ||
396 | self, | ||
397 | crate_def_map: Arc<CrateDefMap>, | ||
398 | module_id: CrateModuleId, | ||
399 | ) -> Resolver { | ||
400 | self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) | ||
401 | } | ||
402 | |||
403 | pub(crate) fn push_expr_scope( | ||
404 | self, | ||
405 | owner: DefWithBodyId, | ||
406 | expr_scopes: Arc<ExprScopes>, | ||
407 | scope_id: ScopeId, | ||
408 | ) -> Resolver { | ||
409 | self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) | ||
410 | } | ||
411 | } | ||
412 | |||
413 | pub enum ScopeDef { | 372 | pub enum ScopeDef { |
414 | PerNs(PerNs), | 373 | PerNs(PerNs), |
415 | ImplSelfType(ImplId), | 374 | ImplSelfType(ImplId), |
@@ -489,6 +448,43 @@ pub fn resolver_for_scope( | |||
489 | r | 448 | r |
490 | } | 449 | } |
491 | 450 | ||
451 | impl Resolver { | ||
452 | fn push_scope(mut self, scope: Scope) -> Resolver { | ||
453 | self.scopes.push(scope); | ||
454 | self | ||
455 | } | ||
456 | |||
457 | fn push_generic_params_scope(self, db: &impl DefDatabase2, def: GenericDefId) -> Resolver { | ||
458 | let params = db.generic_params(def); | ||
459 | if params.params.is_empty() { | ||
460 | self | ||
461 | } else { | ||
462 | self.push_scope(Scope::GenericParams { def, params }) | ||
463 | } | ||
464 | } | ||
465 | |||
466 | fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver { | ||
467 | self.push_scope(Scope::ImplBlockScope(impl_block)) | ||
468 | } | ||
469 | |||
470 | fn push_module_scope( | ||
471 | self, | ||
472 | crate_def_map: Arc<CrateDefMap>, | ||
473 | module_id: CrateModuleId, | ||
474 | ) -> Resolver { | ||
475 | self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) | ||
476 | } | ||
477 | |||
478 | fn push_expr_scope( | ||
479 | self, | ||
480 | owner: DefWithBodyId, | ||
481 | expr_scopes: Arc<ExprScopes>, | ||
482 | scope_id: ScopeId, | ||
483 | ) -> Resolver { | ||
484 | self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) | ||
485 | } | ||
486 | } | ||
487 | |||
492 | pub trait HasResolver { | 488 | pub trait HasResolver { |
493 | /// Builds a resolver for type references inside this def. | 489 | /// Builds a resolver for type references inside this def. |
494 | fn resolver(self, db: &impl DefDatabase2) -> Resolver; | 490 | fn resolver(self, db: &impl DefDatabase2) -> Resolver; |
@@ -507,9 +503,10 @@ impl HasResolver for TraitId { | |||
507 | } | 503 | } |
508 | } | 504 | } |
509 | 505 | ||
510 | impl HasResolver for AdtId { | 506 | impl<T: Into<AdtId>> HasResolver for T { |
511 | fn resolver(self, db: &impl DefDatabase2) -> Resolver { | 507 | fn resolver(self, db: &impl DefDatabase2) -> Resolver { |
512 | let module = match self { | 508 | let def = self.into(); |
509 | let module = match def { | ||
513 | AdtId::StructId(it) => it.0.module(db), | 510 | AdtId::StructId(it) => it.0.module(db), |
514 | AdtId::UnionId(it) => it.0.module(db), | 511 | AdtId::UnionId(it) => it.0.module(db), |
515 | AdtId::EnumId(it) => it.module(db), | 512 | AdtId::EnumId(it) => it.module(db), |
@@ -517,26 +514,8 @@ impl HasResolver for AdtId { | |||
517 | 514 | ||
518 | module | 515 | module |
519 | .resolver(db) | 516 | .resolver(db) |
520 | .push_generic_params_scope(db, self.into()) | 517 | .push_generic_params_scope(db, def.into()) |
521 | .push_scope(Scope::AdtScope(self.into())) | 518 | .push_scope(Scope::AdtScope(def)) |
522 | } | ||
523 | } | ||
524 | |||
525 | impl HasResolver for StructId { | ||
526 | fn resolver(self, db: &impl DefDatabase2) -> Resolver { | ||
527 | AdtId::from(self).resolver(db) | ||
528 | } | ||
529 | } | ||
530 | |||
531 | impl HasResolver for UnionId { | ||
532 | fn resolver(self, db: &impl DefDatabase2) -> Resolver { | ||
533 | AdtId::from(self).resolver(db) | ||
534 | } | ||
535 | } | ||
536 | |||
537 | impl HasResolver for EnumId { | ||
538 | fn resolver(self, db: &impl DefDatabase2) -> Resolver { | ||
539 | AdtId::from(self).resolver(db) | ||
540 | } | 519 | } |
541 | } | 520 | } |
542 | 521 | ||