]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/child_by_source.rs
Merge #9962
[rust.git] / crates / hir_def / src / child_by_source.rs
1 //! When *constructing* `hir`, we start at some parent syntax node and recursively
2 //! lower the children.
3 //!
4 //! This modules allows one to go in the opposite direction: start with a syntax
5 //! node for a *child*, and get its hir.
6
7 use either::Either;
8 use hir_expand::HirFileId;
9
10 use crate::{
11     db::DefDatabase,
12     dyn_map::DynMap,
13     item_scope::ItemScope,
14     keys,
15     src::{HasChildSource, HasSource},
16     AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, ModuleDefId,
17     ModuleId, TraitId, VariantId,
18 };
19
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);
24         res
25     }
26     fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId);
27 }
28
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() {
33             match *item {
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)
39                     }
40                 }
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)
46                     }
47                 }
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)
53                     }
54                 }
55             }
56         }
57     }
58 }
59
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() {
64             match item {
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)
70                     }
71                 }
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)
77                     }
78                 }
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)
84                     }
85                 }
86             }
87         }
88     }
89 }
90
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);
96     }
97 }
98
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);
105         });
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);
110         });
111
112         fn add_module_def(
113             db: &dyn DefDatabase,
114             file_id: HirFileId,
115             map: &mut DynMap,
116             item: ModuleDefId,
117         ) {
118             match item {
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)
124                     }
125                 }
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)
131                     }
132                 }
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)
138                     }
139                 }
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)
145                     }
146                 }
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_)
152                     }
153                 }
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)
160                         }
161                     }
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_)
167                         }
168                     }
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_)
174                         }
175                     }
176                 },
177                 _ => (),
178             }
179         }
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)
185             }
186         }
187     }
188 }
189
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();
194         let parent = *self;
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)
200                 }
201                 Either::Right(source) => {
202                     res[keys::RECORD_FIELD].insert(arena_map.with_value(source), id)
203                 }
204             }
205         }
206     }
207 }
208
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)
216         }
217     }
218 }
219
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);
227         }
228     }
229 }