From baee74618524ad75819163d701ff9e6fe9883e95 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 22 Dec 2019 15:49:39 +0100 Subject: Share impl Scope between modules and blocks --- crates/ra_hir_def/src/body.rs | 5 +- crates/ra_hir_def/src/body/lower.rs | 4 +- crates/ra_hir_def/src/child_by_source.rs | 100 ++++++++++++++++--------------- 3 files changed, 56 insertions(+), 53 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 148ff007e..d3e4c50ae 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -19,10 +19,11 @@ use crate::{ db::DefDatabase, expr::{Expr, ExprId, Pat, PatId}, item_scope::BuiltinShadowMode, + item_scope::ItemScope, nameres::CrateDefMap, path::{ModPath, Path}, src::HasSource, - DefWithBodyId, HasModule, Lookup, ModuleDefId, ModuleId, + DefWithBodyId, HasModule, Lookup, ModuleId, }; pub(crate) struct Expander { @@ -135,7 +136,7 @@ pub struct Body { pub params: Vec, /// The `ExprId` of the actual body expression. pub body_expr: ExprId, - pub defs: Vec, + pub item_scope: ItemScope, } pub type ExprPtr = Either, AstPtr>; diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index be5d17d85..754924050 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -46,7 +46,7 @@ pub(super) fn lower( pats: Arena::default(), params: Vec::new(), body_expr: ExprId::dummy(), - defs: Vec::new(), + item_scope: Default::default(), }, } .collect(params, body) @@ -532,7 +532,7 @@ where | ast::ModuleItem::ExternCrateItem(_) | ast::ModuleItem::Module(_) => continue, }; - self.body.defs.push(def) + self.body.item_scope.define_def(def) } } diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index 4488e8502..8b6c773ee 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -9,6 +9,7 @@ use either::Either; use crate::{ db::DefDatabase, dyn_map::DynMap, + item_scope::ItemScope, keys, src::{HasChildSource, HasSource}, AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, @@ -73,59 +74,62 @@ impl ChildBySource for ImplId { impl ChildBySource for ModuleId { fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { - let mut res = DynMap::default(); - let crate_def_map = db.crate_def_map(self.krate); let module_data = &crate_def_map[self.local_id]; - - module_data.scope.declarations().for_each(|item| add_module_def(db, &mut res, item)); - - for imp in module_data.scope.impls() { - let src = imp.lookup(db).source(db); - res[keys::IMPL].insert(src, imp) - } - - res + module_data.scope.child_by_source(db) } } -fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) { - match item { - ModuleDefId::FunctionId(func) => { - let src = func.lookup(db).source(db); - map[keys::FUNCTION].insert(src, func) - } - ModuleDefId::ConstId(konst) => { - let src = konst.lookup(db).source(db); - map[keys::CONST].insert(src, konst) - } - ModuleDefId::StaticId(statik) => { - let src = statik.lookup(db).source(db); - map[keys::STATIC].insert(src, statik) - } - ModuleDefId::TypeAliasId(ty) => { - let src = ty.lookup(db).source(db); - map[keys::TYPE_ALIAS].insert(src, ty) +impl ChildBySource for ItemScope { + fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + let mut res = DynMap::default(); + self.declarations().for_each(|item| add_module_def(db, &mut res, item)); + self.impls().for_each(|imp| add_impl(db, &mut res, imp)); + return res; + + fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) { + match item { + ModuleDefId::FunctionId(func) => { + let src = func.lookup(db).source(db); + map[keys::FUNCTION].insert(src, func) + } + ModuleDefId::ConstId(konst) => { + let src = konst.lookup(db).source(db); + map[keys::CONST].insert(src, konst) + } + ModuleDefId::StaticId(statik) => { + let src = statik.lookup(db).source(db); + map[keys::STATIC].insert(src, statik) + } + ModuleDefId::TypeAliasId(ty) => { + let src = ty.lookup(db).source(db); + map[keys::TYPE_ALIAS].insert(src, ty) + } + ModuleDefId::TraitId(trait_) => { + let src = trait_.lookup(db).source(db); + map[keys::TRAIT].insert(src, trait_) + } + ModuleDefId::AdtId(adt) => match adt { + AdtId::StructId(strukt) => { + let src = strukt.lookup(db).source(db); + map[keys::STRUCT].insert(src, strukt) + } + AdtId::UnionId(union_) => { + let src = union_.lookup(db).source(db); + map[keys::UNION].insert(src, union_) + } + AdtId::EnumId(enum_) => { + let src = enum_.lookup(db).source(db); + map[keys::ENUM].insert(src, enum_) + } + }, + _ => (), + } } - ModuleDefId::TraitId(trait_) => { - let src = trait_.lookup(db).source(db); - map[keys::TRAIT].insert(src, trait_) + fn add_impl(db: &impl DefDatabase, map: &mut DynMap, imp: ImplId) { + let src = imp.lookup(db).source(db); + map[keys::IMPL].insert(src, imp) } - ModuleDefId::AdtId(adt) => match adt { - AdtId::StructId(strukt) => { - let src = strukt.lookup(db).source(db); - map[keys::STRUCT].insert(src, strukt) - } - AdtId::UnionId(union_) => { - let src = union_.lookup(db).source(db); - map[keys::UNION].insert(src, union_) - } - AdtId::EnumId(enum_) => { - let src = enum_.lookup(db).source(db); - map[keys::ENUM].insert(src, enum_) - } - }, - _ => (), } } @@ -167,9 +171,7 @@ impl ChildBySource for EnumId { impl ChildBySource for DefWithBodyId { fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { - let mut res = DynMap::default(); let body = db.body(*self); - body.defs.iter().copied().for_each(|item| add_module_def(db, &mut res, item)); - res + body.item_scope.child_by_source(db) } } -- cgit v1.2.3