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;
9 use syntax::ast::HasDocComments;
14 item_scope::ItemScope,
16 src::{HasChildSource, HasSource},
17 AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, ModuleDefId,
18 ModuleId, TraitId, VariantId,
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);
27 fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId);
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);
34 data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
36 res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
39 data.items.iter().for_each(|&(_, item)| {
40 add_assoc_item(db, res, file_id, item);
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(
50 res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
53 data.items.iter().for_each(|&item| {
54 add_assoc_item(db, res, file_id, item);
59 fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
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)
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)
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)
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);
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);
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,
108 res[keys::MACRO].insert(src, makro);
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);
116 self.derive_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
118 let adt = ast_id.to_node(db.upcast());
119 calls.for_each(|(attr_id, calls)| {
120 if let Some(Either::Right(attr)) =
121 adt.doc_comments_and_attrs().nth(attr_id.ast_index as usize)
123 res[keys::DERIVE_MACRO_CALL].insert(attr, (attr_id, calls.into()));
130 db: &dyn DefDatabase,
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)
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)),
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)
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();
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),
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)
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);