1 //! When *constructing* `hir`, we start at some parent syntax node and recursively
2 //! lower the children.
4 //! This modules allows one to go in the opposite direction: start with a syntax
5 //! node for a *child*, and get its hir.
8 use hir_expand::HirFileId;
13 item_scope::ItemScope,
15 src::{HasChildSource, HasSource},
16 AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, ModuleDefId,
17 ModuleId, TraitId, VariantId,
20 pub trait ChildBySource {
21 fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap {
22 let mut res = DynMap::default();
23 self.child_by_source_to(db, &mut res, file_id);
26 fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId);
29 impl ChildBySource for TraitId {
30 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
31 let data = db.trait_data(*self);
32 for (_name, item) in data.items.iter() {
34 AssocItemId::FunctionId(func) => {
35 let loc = func.lookup(db);
36 if loc.id.file_id() == file_id {
37 let src = loc.source(db);
38 res[keys::FUNCTION].insert(src, func)
41 AssocItemId::ConstId(konst) => {
42 let loc = konst.lookup(db);
43 if loc.id.file_id() == file_id {
44 let src = loc.source(db);
45 res[keys::CONST].insert(src, konst)
48 AssocItemId::TypeAliasId(ty) => {
49 let loc = ty.lookup(db);
50 if loc.id.file_id() == file_id {
51 let src = loc.source(db);
52 res[keys::TYPE_ALIAS].insert(src, ty)
60 impl ChildBySource for ImplId {
61 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
62 let data = db.impl_data(*self);
63 for &item in data.items.iter() {
65 AssocItemId::FunctionId(func) => {
66 let loc = func.lookup(db);
67 if loc.id.file_id() == file_id {
68 let src = loc.source(db);
69 res[keys::FUNCTION].insert(src, func)
72 AssocItemId::ConstId(konst) => {
73 let loc = konst.lookup(db);
74 if loc.id.file_id() == file_id {
75 let src = loc.source(db);
76 res[keys::CONST].insert(src, konst)
79 AssocItemId::TypeAliasId(ty) => {
80 let loc = ty.lookup(db);
81 if loc.id.file_id() == file_id {
82 let src = loc.source(db);
83 res[keys::TYPE_ALIAS].insert(src, ty)
91 impl ChildBySource for ModuleId {
92 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
93 let def_map = self.def_map(db);
94 let module_data = &def_map[self.local_id];
95 module_data.scope.child_by_source_to(db, res, file_id);
99 impl ChildBySource for ItemScope {
100 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
101 self.declarations().for_each(|item| add_module_def(db, file_id, res, item));
102 self.unnamed_consts().for_each(|konst| {
103 let src = konst.lookup(db).source(db);
104 res[keys::CONST].insert(src, konst);
106 self.impls().for_each(|imp| add_impl(db, file_id, res, imp));
107 self.attr_macro_invocs().for_each(|(ast_id, call_id)| {
108 let item = ast_id.with_value(ast_id.to_node(db.upcast()));
109 res[keys::ATTR_MACRO].insert(item, call_id);
113 db: &dyn DefDatabase,
119 ModuleDefId::FunctionId(func) => {
120 let loc = func.lookup(db);
121 if loc.id.file_id() == file_id {
122 let src = loc.source(db);
123 map[keys::FUNCTION].insert(src, func)
126 ModuleDefId::ConstId(konst) => {
127 let loc = konst.lookup(db);
128 if loc.id.file_id() == file_id {
129 let src = loc.source(db);
130 map[keys::CONST].insert(src, konst)
133 ModuleDefId::StaticId(statik) => {
134 let loc = statik.lookup(db);
135 if loc.id.file_id() == file_id {
136 let src = loc.source(db);
137 map[keys::STATIC].insert(src, statik)
140 ModuleDefId::TypeAliasId(ty) => {
141 let loc = ty.lookup(db);
142 if loc.id.file_id() == file_id {
143 let src = loc.source(db);
144 map[keys::TYPE_ALIAS].insert(src, ty)
147 ModuleDefId::TraitId(trait_) => {
148 let loc = trait_.lookup(db);
149 if loc.id.file_id() == file_id {
150 let src = loc.source(db);
151 map[keys::TRAIT].insert(src, trait_)
154 ModuleDefId::AdtId(adt) => match adt {
155 AdtId::StructId(strukt) => {
156 let loc = strukt.lookup(db);
157 if loc.id.file_id() == file_id {
158 let src = loc.source(db);
159 map[keys::STRUCT].insert(src, strukt)
162 AdtId::UnionId(union_) => {
163 let loc = union_.lookup(db);
164 if loc.id.file_id() == file_id {
165 let src = loc.source(db);
166 map[keys::UNION].insert(src, union_)
169 AdtId::EnumId(enum_) => {
170 let loc = enum_.lookup(db);
171 if loc.id.file_id() == file_id {
172 let src = loc.source(db);
173 map[keys::ENUM].insert(src, enum_)
180 fn add_impl(db: &dyn DefDatabase, file_id: HirFileId, map: &mut DynMap, imp: ImplId) {
181 let loc = imp.lookup(db);
182 if loc.id.file_id() == file_id {
183 let src = loc.source(db);
184 map[keys::IMPL].insert(src, imp)
190 impl ChildBySource for VariantId {
191 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
192 let arena_map = self.child_source(db);
193 let arena_map = arena_map.as_ref();
195 for (local_id, source) in arena_map.value.iter() {
196 let id = FieldId { parent, local_id };
197 match source.clone() {
198 Either::Left(source) => {
199 res[keys::TUPLE_FIELD].insert(arena_map.with_value(source), id)
201 Either::Right(source) => {
202 res[keys::RECORD_FIELD].insert(arena_map.with_value(source), id)
209 impl ChildBySource for EnumId {
210 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
211 let arena_map = self.child_source(db);
212 let arena_map = arena_map.as_ref();
213 for (local_id, source) in arena_map.value.iter() {
214 let id = EnumVariantId { parent: *self, local_id };
215 res[keys::VARIANT].insert(arena_map.with_value(source.clone()), id)
220 impl ChildBySource for DefWithBodyId {
221 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
222 let body = db.body(*self);
223 for (_, def_map) in body.blocks(db) {
224 // All block expressions are merged into the same map, because they logically all add
225 // inner items to the containing `DefWithBodyId`.
226 def_map[def_map.root()].scope.child_by_source_to(db, res, file_id);