aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/body.rs5
-rw-r--r--crates/ra_hir_def/src/body/lower.rs4
-rw-r--r--crates/ra_hir_def/src/child_by_source.rs100
3 files changed, 56 insertions, 53 deletions
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::{
19 db::DefDatabase, 19 db::DefDatabase,
20 expr::{Expr, ExprId, Pat, PatId}, 20 expr::{Expr, ExprId, Pat, PatId},
21 item_scope::BuiltinShadowMode, 21 item_scope::BuiltinShadowMode,
22 item_scope::ItemScope,
22 nameres::CrateDefMap, 23 nameres::CrateDefMap,
23 path::{ModPath, Path}, 24 path::{ModPath, Path},
24 src::HasSource, 25 src::HasSource,
25 DefWithBodyId, HasModule, Lookup, ModuleDefId, ModuleId, 26 DefWithBodyId, HasModule, Lookup, ModuleId,
26}; 27};
27 28
28pub(crate) struct Expander { 29pub(crate) struct Expander {
@@ -135,7 +136,7 @@ pub struct Body {
135 pub params: Vec<PatId>, 136 pub params: Vec<PatId>,
136 /// The `ExprId` of the actual body expression. 137 /// The `ExprId` of the actual body expression.
137 pub body_expr: ExprId, 138 pub body_expr: ExprId,
138 pub defs: Vec<ModuleDefId>, 139 pub item_scope: ItemScope,
139} 140}
140 141
141pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; 142pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;
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(
46 pats: Arena::default(), 46 pats: Arena::default(),
47 params: Vec::new(), 47 params: Vec::new(),
48 body_expr: ExprId::dummy(), 48 body_expr: ExprId::dummy(),
49 defs: Vec::new(), 49 item_scope: Default::default(),
50 }, 50 },
51 } 51 }
52 .collect(params, body) 52 .collect(params, body)
@@ -532,7 +532,7 @@ where
532 | ast::ModuleItem::ExternCrateItem(_) 532 | ast::ModuleItem::ExternCrateItem(_)
533 | ast::ModuleItem::Module(_) => continue, 533 | ast::ModuleItem::Module(_) => continue,
534 }; 534 };
535 self.body.defs.push(def) 535 self.body.item_scope.define_def(def)
536 } 536 }
537 } 537 }
538 538
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;
9use crate::{ 9use crate::{
10 db::DefDatabase, 10 db::DefDatabase,
11 dyn_map::DynMap, 11 dyn_map::DynMap,
12 item_scope::ItemScope,
12 keys, 13 keys,
13 src::{HasChildSource, HasSource}, 14 src::{HasChildSource, HasSource},
14 AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, 15 AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId,
@@ -73,59 +74,62 @@ impl ChildBySource for ImplId {
73 74
74impl ChildBySource for ModuleId { 75impl ChildBySource for ModuleId {
75 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { 76 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
76 let mut res = DynMap::default();
77
78 let crate_def_map = db.crate_def_map(self.krate); 77 let crate_def_map = db.crate_def_map(self.krate);
79 let module_data = &crate_def_map[self.local_id]; 78 let module_data = &crate_def_map[self.local_id];
80 79 module_data.scope.child_by_source(db)
81 module_data.scope.declarations().for_each(|item| add_module_def(db, &mut res, item));
82
83 for imp in module_data.scope.impls() {
84 let src = imp.lookup(db).source(db);
85 res[keys::IMPL].insert(src, imp)
86 }
87
88 res
89 } 80 }
90} 81}
91 82
92fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) { 83impl ChildBySource for ItemScope {
93 match item { 84 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
94 ModuleDefId::FunctionId(func) => { 85 let mut res = DynMap::default();
95 let src = func.lookup(db).source(db); 86 self.declarations().for_each(|item| add_module_def(db, &mut res, item));
96 map[keys::FUNCTION].insert(src, func) 87 self.impls().for_each(|imp| add_impl(db, &mut res, imp));
97 } 88 return res;
98 ModuleDefId::ConstId(konst) => { 89
99 let src = konst.lookup(db).source(db); 90 fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) {
100 map[keys::CONST].insert(src, konst) 91 match item {
101 } 92 ModuleDefId::FunctionId(func) => {
102 ModuleDefId::StaticId(statik) => { 93 let src = func.lookup(db).source(db);
103 let src = statik.lookup(db).source(db); 94 map[keys::FUNCTION].insert(src, func)
104 map[keys::STATIC].insert(src, statik) 95 }
105 } 96 ModuleDefId::ConstId(konst) => {
106 ModuleDefId::TypeAliasId(ty) => { 97 let src = konst.lookup(db).source(db);
107 let src = ty.lookup(db).source(db); 98 map[keys::CONST].insert(src, konst)
108 map[keys::TYPE_ALIAS].insert(src, ty) 99 }
100 ModuleDefId::StaticId(statik) => {
101 let src = statik.lookup(db).source(db);
102 map[keys::STATIC].insert(src, statik)
103 }
104 ModuleDefId::TypeAliasId(ty) => {
105 let src = ty.lookup(db).source(db);
106 map[keys::TYPE_ALIAS].insert(src, ty)
107 }
108 ModuleDefId::TraitId(trait_) => {
109 let src = trait_.lookup(db).source(db);
110 map[keys::TRAIT].insert(src, trait_)
111 }
112 ModuleDefId::AdtId(adt) => match adt {
113 AdtId::StructId(strukt) => {
114 let src = strukt.lookup(db).source(db);
115 map[keys::STRUCT].insert(src, strukt)
116 }
117 AdtId::UnionId(union_) => {
118 let src = union_.lookup(db).source(db);
119 map[keys::UNION].insert(src, union_)
120 }
121 AdtId::EnumId(enum_) => {
122 let src = enum_.lookup(db).source(db);
123 map[keys::ENUM].insert(src, enum_)
124 }
125 },
126 _ => (),
127 }
109 } 128 }
110 ModuleDefId::TraitId(trait_) => { 129 fn add_impl(db: &impl DefDatabase, map: &mut DynMap, imp: ImplId) {
111 let src = trait_.lookup(db).source(db); 130 let src = imp.lookup(db).source(db);
112 map[keys::TRAIT].insert(src, trait_) 131 map[keys::IMPL].insert(src, imp)
113 } 132 }
114 ModuleDefId::AdtId(adt) => match adt {
115 AdtId::StructId(strukt) => {
116 let src = strukt.lookup(db).source(db);
117 map[keys::STRUCT].insert(src, strukt)
118 }
119 AdtId::UnionId(union_) => {
120 let src = union_.lookup(db).source(db);
121 map[keys::UNION].insert(src, union_)
122 }
123 AdtId::EnumId(enum_) => {
124 let src = enum_.lookup(db).source(db);
125 map[keys::ENUM].insert(src, enum_)
126 }
127 },
128 _ => (),
129 } 133 }
130} 134}
131 135
@@ -167,9 +171,7 @@ impl ChildBySource for EnumId {
167 171
168impl ChildBySource for DefWithBodyId { 172impl ChildBySource for DefWithBodyId {
169 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { 173 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
170 let mut res = DynMap::default();
171 let body = db.body(*self); 174 let body = db.body(*self);
172 body.defs.iter().copied().for_each(|item| add_module_def(db, &mut res, item)); 175 body.item_scope.child_by_source(db)
173 res
174 } 176 }
175} 177}