2019-12-05 16:34:12 -06:00
|
|
|
//! When *constructing* `hir`, we start at some parent syntax node and recursively
|
|
|
|
//! lower the children.
|
|
|
|
//!
|
|
|
|
//! This modules allows one to go in the opposite direction: start with a syntax
|
|
|
|
//! node for a *child*, and get its hir.
|
|
|
|
|
|
|
|
use either::Either;
|
2021-08-16 10:07:25 -05:00
|
|
|
use hir_expand::HirFileId;
|
2022-01-07 07:19:11 -06:00
|
|
|
use syntax::ast::HasDocComments;
|
2019-12-05 16:34:12 -06:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::DefDatabase,
|
|
|
|
dyn_map::DynMap,
|
2019-12-22 08:49:39 -06:00
|
|
|
item_scope::ItemScope,
|
2019-12-05 16:34:12 -06:00
|
|
|
keys,
|
|
|
|
src::{HasChildSource, HasSource},
|
2020-04-25 07:23:34 -05:00
|
|
|
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, ModuleDefId,
|
|
|
|
ModuleId, TraitId, VariantId,
|
2019-12-05 16:34:12 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
pub trait ChildBySource {
|
2021-08-16 10:07:25 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap {
|
2019-12-05 16:34:12 -06:00
|
|
|
let mut res = DynMap::default();
|
2021-08-16 11:29:16 -05:00
|
|
|
self.child_by_source_to(db, &mut res, file_id);
|
2021-03-05 07:06:09 -06:00
|
|
|
res
|
|
|
|
}
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId);
|
2021-03-05 07:06:09 -06:00
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
|
2021-03-05 07:06:09 -06:00
|
|
|
impl ChildBySource for TraitId {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
|
2019-12-05 16:34:12 -06:00
|
|
|
let data = db.trait_data(*self);
|
2022-01-06 05:30:16 -06:00
|
|
|
// FIXME attribute macros
|
2021-08-16 11:29:16 -05:00
|
|
|
for (_name, item) in data.items.iter() {
|
|
|
|
match *item {
|
2019-12-05 16:34:12 -06:00
|
|
|
AssocItemId::FunctionId(func) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = func.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::FUNCTION].insert(src, func)
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
AssocItemId::ConstId(konst) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = konst.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::CONST].insert(src, konst)
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
AssocItemId::TypeAliasId(ty) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = ty.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::TYPE_ALIAS].insert(src, ty)
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for ImplId {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
|
2019-12-05 16:34:12 -06:00
|
|
|
let data = db.impl_data(*self);
|
2022-01-06 05:30:16 -06:00
|
|
|
// FIXME attribute macros
|
2019-12-05 16:34:12 -06:00
|
|
|
for &item in data.items.iter() {
|
|
|
|
match item {
|
|
|
|
AssocItemId::FunctionId(func) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = func.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::FUNCTION].insert(src, func)
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
AssocItemId::ConstId(konst) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = konst.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::CONST].insert(src, konst)
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
AssocItemId::TypeAliasId(ty) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = ty.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::TYPE_ALIAS].insert(src, ty)
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for ModuleId {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
|
2021-03-06 17:20:13 -06:00
|
|
|
let def_map = self.def_map(db);
|
|
|
|
let module_data = &def_map[self.local_id];
|
2021-08-16 11:29:16 -05:00
|
|
|
module_data.scope.child_by_source_to(db, res, file_id);
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 08:49:39 -06:00
|
|
|
impl ChildBySource for ItemScope {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
|
2021-08-16 10:07:25 -05:00
|
|
|
self.declarations().for_each(|item| add_module_def(db, file_id, res, item));
|
2021-10-28 11:40:38 -05:00
|
|
|
self.macros().for_each(|(_, makro)| {
|
|
|
|
let ast_id = makro.ast_id();
|
|
|
|
if ast_id.either(|it| it.file_id, |it| it.file_id) == file_id {
|
|
|
|
let src = match ast_id {
|
|
|
|
Either::Left(ast_id) => ast_id.with_value(ast_id.to_node(db.upcast())),
|
|
|
|
// FIXME: Do we need to add proc-macros into a PROCMACRO dynmap here?
|
|
|
|
Either::Right(_fn) => return,
|
|
|
|
};
|
2022-01-07 07:19:11 -06:00
|
|
|
res[keys::MACRO_CALL].insert(src, makro);
|
2021-10-28 11:40:38 -05:00
|
|
|
}
|
|
|
|
});
|
2021-04-09 10:14:48 -05:00
|
|
|
self.unnamed_consts().for_each(|konst| {
|
2022-01-07 14:31:08 -06:00
|
|
|
let loc = konst.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
res[keys::CONST].insert(src, konst);
|
|
|
|
}
|
2021-04-09 10:14:48 -05:00
|
|
|
});
|
2021-08-16 10:07:25 -05:00
|
|
|
self.impls().for_each(|imp| add_impl(db, file_id, res, imp));
|
2021-06-06 08:51:05 -05:00
|
|
|
self.attr_macro_invocs().for_each(|(ast_id, call_id)| {
|
2022-01-07 14:31:08 -06:00
|
|
|
if ast_id.file_id == file_id {
|
|
|
|
let item = ast_id.with_value(ast_id.to_node(db.upcast()));
|
|
|
|
res[keys::ATTR_MACRO_CALL].insert(item, call_id);
|
|
|
|
}
|
2021-06-06 08:51:05 -05:00
|
|
|
});
|
2021-08-25 20:32:34 -05:00
|
|
|
self.derive_macro_invocs().for_each(|(ast_id, calls)| {
|
2022-01-07 14:31:08 -06:00
|
|
|
if ast_id.file_id != file_id {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-07 07:19:11 -06:00
|
|
|
let adt = ast_id.to_node(db.upcast());
|
2022-01-01 13:31:04 -06:00
|
|
|
for (attr_id, calls) in calls {
|
2022-01-07 07:19:11 -06:00
|
|
|
if let Some(Either::Right(attr)) =
|
|
|
|
adt.doc_comments_and_attrs().nth(attr_id.ast_index as usize)
|
|
|
|
{
|
|
|
|
res[keys::DERIVE_MACRO_CALL]
|
|
|
|
.insert(ast_id.with_value(attr), (attr_id, calls.into()));
|
2021-08-25 20:32:34 -05:00
|
|
|
}
|
2021-08-24 09:33:52 -05:00
|
|
|
}
|
|
|
|
});
|
2019-12-22 08:49:39 -06:00
|
|
|
|
2021-08-16 10:07:25 -05:00
|
|
|
fn add_module_def(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
file_id: HirFileId,
|
|
|
|
map: &mut DynMap,
|
|
|
|
item: ModuleDefId,
|
|
|
|
) {
|
2019-12-22 08:49:39 -06:00
|
|
|
match item {
|
|
|
|
ModuleDefId::FunctionId(func) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = func.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::FUNCTION].insert(src, func)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
ModuleDefId::ConstId(konst) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = konst.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::CONST].insert(src, konst)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
ModuleDefId::StaticId(statik) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = statik.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::STATIC].insert(src, statik)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
ModuleDefId::TypeAliasId(ty) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = ty.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::TYPE_ALIAS].insert(src, ty)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
ModuleDefId::TraitId(trait_) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = trait_.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::TRAIT].insert(src, trait_)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
ModuleDefId::AdtId(adt) => match adt {
|
|
|
|
AdtId::StructId(strukt) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = strukt.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::STRUCT].insert(src, strukt)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
AdtId::UnionId(union_) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = union_.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::UNION].insert(src, union_)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
AdtId::EnumId(enum_) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
let loc = enum_.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::ENUM].insert(src, enum_)
|
|
|
|
}
|
2019-12-22 08:49:39 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
2019-12-19 11:21:26 -06:00
|
|
|
}
|
2021-08-16 10:07:25 -05:00
|
|
|
fn add_impl(db: &dyn DefDatabase, file_id: HirFileId, map: &mut DynMap, imp: ImplId) {
|
|
|
|
let loc = imp.lookup(db);
|
|
|
|
if loc.id.file_id() == file_id {
|
|
|
|
let src = loc.source(db);
|
|
|
|
map[keys::IMPL].insert(src, imp)
|
|
|
|
}
|
2019-12-19 11:21:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 16:34:12 -06:00
|
|
|
impl ChildBySource for VariantId {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
|
2019-12-05 16:34:12 -06:00
|
|
|
let arena_map = self.child_source(db);
|
|
|
|
let arena_map = arena_map.as_ref();
|
2021-08-16 10:07:25 -05:00
|
|
|
let parent = *self;
|
2019-12-05 16:34:12 -06:00
|
|
|
for (local_id, source) in arena_map.value.iter() {
|
2021-08-16 10:07:25 -05:00
|
|
|
let id = FieldId { parent, local_id };
|
|
|
|
match source.clone() {
|
2019-12-05 16:34:12 -06:00
|
|
|
Either::Left(source) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
res[keys::TUPLE_FIELD].insert(arena_map.with_value(source), id)
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
Either::Right(source) => {
|
2021-08-16 10:07:25 -05:00
|
|
|
res[keys::RECORD_FIELD].insert(arena_map.with_value(source), id)
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for EnumId {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
|
2019-12-05 16:34:12 -06:00
|
|
|
let arena_map = self.child_source(db);
|
|
|
|
let arena_map = arena_map.as_ref();
|
|
|
|
for (local_id, source) in arena_map.value.iter() {
|
|
|
|
let id = EnumVariantId { parent: *self, local_id };
|
2020-07-30 10:56:53 -05:00
|
|
|
res[keys::VARIANT].insert(arena_map.with_value(source.clone()), id)
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 11:21:26 -06:00
|
|
|
|
|
|
|
impl ChildBySource for DefWithBodyId {
|
2021-08-16 11:29:16 -05:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
|
2019-12-19 11:21:26 -06:00
|
|
|
let body = db.body(*self);
|
2021-04-03 20:03:18 -05:00
|
|
|
for (_, def_map) in body.blocks(db) {
|
2021-03-05 07:08:36 -06:00
|
|
|
// All block expressions are merged into the same map, because they logically all add
|
|
|
|
// inner items to the containing `DefWithBodyId`.
|
2021-08-16 11:29:16 -05:00
|
|
|
def_map[def_map.root()].scope.child_by_source_to(db, res, file_id);
|
2021-03-05 07:08:36 -06:00
|
|
|
}
|
2019-12-19 11:21:26 -06:00
|
|
|
}
|
|
|
|
}
|