]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/child_by_source.rs
parameters.split_last()
[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 use syntax::ast::HasDocComments;
10
11 use crate::{
12     db::DefDatabase,
13     dyn_map::DynMap,
14     item_scope::ItemScope,
15     keys,
16     src::{HasChildSource, HasSource},
17     AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, ModuleDefId,
18     ModuleId, TraitId, VariantId,
19 };
20
21 pub trait ChildBySource {
22     fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap {
23         let mut res = DynMap::default();
24         self.child_by_source_to(db, &mut res, file_id);
25         res
26     }
27     fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId);
28 }
29
30 impl ChildBySource for TraitId {
31     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
32         let data = db.trait_data(*self);
33
34         data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
35             |(ast_id, call_id)| {
36                 res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
37             },
38         );
39         data.items.iter().for_each(|&(_, item)| {
40             add_assoc_item(db, res, file_id, item);
41         });
42     }
43 }
44
45 impl ChildBySource for ImplId {
46     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
47         let data = db.impl_data(*self);
48         data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
49             |(ast_id, call_id)| {
50                 res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
51             },
52         );
53         data.items.iter().for_each(|&item| {
54             add_assoc_item(db, res, file_id, item);
55         });
56     }
57 }
58
59 fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
60     match item {
61         AssocItemId::FunctionId(func) => {
62             let loc = func.lookup(db);
63             if loc.id.file_id() == file_id {
64                 res[keys::FUNCTION].insert(loc.source(db).value, func)
65             }
66         }
67         AssocItemId::ConstId(konst) => {
68             let loc = konst.lookup(db);
69             if loc.id.file_id() == file_id {
70                 res[keys::CONST].insert(loc.source(db).value, konst)
71             }
72         }
73         AssocItemId::TypeAliasId(ty) => {
74             let loc = ty.lookup(db);
75             if loc.id.file_id() == file_id {
76                 res[keys::TYPE_ALIAS].insert(loc.source(db).value, ty)
77             }
78         }
79     }
80 }
81
82 impl ChildBySource for ModuleId {
83     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
84         let def_map = self.def_map(db);
85         let module_data = &def_map[self.local_id];
86         module_data.scope.child_by_source_to(db, res, file_id);
87     }
88 }
89
90 impl ChildBySource for ItemScope {
91     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
92         self.declarations().for_each(|item| add_module_def(db, res, file_id, item));
93         self.impls().for_each(|imp| add_impl(db, res, file_id, imp));
94         self.unnamed_consts().for_each(|konst| {
95             let loc = konst.lookup(db);
96             if loc.id.file_id() == file_id {
97                 res[keys::CONST].insert(loc.source(db).value, konst);
98             }
99         });
100         self.macros().for_each(|(_, makro)| {
101             let ast_id = makro.ast_id();
102             if ast_id.either(|it| it.file_id, |it| it.file_id) == file_id {
103                 let src = match ast_id {
104                     Either::Left(ast_id) => ast_id.to_node(db.upcast()),
105                     // FIXME: Do we need to add proc-macros into a PROCMACRO dynmap here?
106                     Either::Right(_fn) => return,
107                 };
108                 res[keys::MACRO].insert(src, makro);
109             }
110         });
111         self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
112             |(ast_id, call_id)| {
113                 res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
114             },
115         );
116         self.derive_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
117             |(ast_id, calls)| {
118                 let adt = ast_id.to_node(db.upcast());
119                 calls.for_each(|(attr_id, calls)| {
120                     if let Some(Either::Left(attr)) =
121                         adt.doc_comments_and_attrs().nth(attr_id.ast_index as usize)
122                     {
123                         res[keys::DERIVE_MACRO_CALL].insert(attr, (attr_id, calls.into()));
124                     }
125                 });
126             },
127         );
128
129         fn add_module_def(
130             db: &dyn DefDatabase,
131             map: &mut DynMap,
132             file_id: HirFileId,
133             item: ModuleDefId,
134         ) {
135             macro_rules! insert {
136                 ($map:ident[$key:path].$insert:ident($id:ident)) => {{
137                     let loc = $id.lookup(db);
138                     if loc.id.file_id() == file_id {
139                         $map[$key].$insert(loc.source(db).value, $id)
140                     }
141                 }};
142             }
143             match item {
144                 ModuleDefId::FunctionId(id) => insert!(map[keys::FUNCTION].insert(id)),
145                 ModuleDefId::ConstId(id) => insert!(map[keys::CONST].insert(id)),
146                 ModuleDefId::StaticId(id) => insert!(map[keys::STATIC].insert(id)),
147                 ModuleDefId::TypeAliasId(id) => insert!(map[keys::TYPE_ALIAS].insert(id)),
148                 ModuleDefId::TraitId(id) => insert!(map[keys::TRAIT].insert(id)),
149                 ModuleDefId::AdtId(adt) => match adt {
150                     AdtId::StructId(id) => insert!(map[keys::STRUCT].insert(id)),
151                     AdtId::UnionId(id) => insert!(map[keys::UNION].insert(id)),
152                     AdtId::EnumId(id) => insert!(map[keys::ENUM].insert(id)),
153                 },
154                 _ => (),
155             }
156         }
157         fn add_impl(db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId, imp: ImplId) {
158             let loc = imp.lookup(db);
159             if loc.id.file_id() == file_id {
160                 map[keys::IMPL].insert(loc.source(db).value, imp)
161             }
162         }
163     }
164 }
165
166 impl ChildBySource for VariantId {
167     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
168         let arena_map = self.child_source(db);
169         let arena_map = arena_map.as_ref();
170         let parent = *self;
171         for (local_id, source) in arena_map.value.iter() {
172             let id = FieldId { parent, local_id };
173             match source.clone() {
174                 Either::Left(source) => res[keys::TUPLE_FIELD].insert(source, id),
175                 Either::Right(source) => res[keys::RECORD_FIELD].insert(source, id),
176             }
177         }
178     }
179 }
180
181 impl ChildBySource for EnumId {
182     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
183         let arena_map = self.child_source(db);
184         let arena_map = arena_map.as_ref();
185         for (local_id, source) in arena_map.value.iter() {
186             let id = EnumVariantId { parent: *self, local_id };
187             res[keys::VARIANT].insert(source.clone(), id)
188         }
189     }
190 }
191
192 impl ChildBySource for DefWithBodyId {
193     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
194         let body = db.body(*self);
195         for (_, def_map) in body.blocks(db) {
196             // All block expressions are merged into the same map, because they logically all add
197             // inner items to the containing `DefWithBodyId`.
198             def_map[def_map.root()].scope.child_by_source_to(db, res, file_id);
199         }
200     }
201 }