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;
|
|
|
|
|
|
|
|
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 {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap;
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for TraitId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-05 16:34:12 -06:00
|
|
|
let mut res = DynMap::default();
|
|
|
|
|
|
|
|
let data = db.trait_data(*self);
|
|
|
|
for (_name, item) in data.items.iter() {
|
|
|
|
match *item {
|
|
|
|
AssocItemId::FunctionId(func) => {
|
|
|
|
let src = func.lookup(db).source(db);
|
|
|
|
res[keys::FUNCTION].insert(src, func)
|
|
|
|
}
|
|
|
|
AssocItemId::ConstId(konst) => {
|
|
|
|
let src = konst.lookup(db).source(db);
|
|
|
|
res[keys::CONST].insert(src, konst)
|
|
|
|
}
|
|
|
|
AssocItemId::TypeAliasId(ty) => {
|
|
|
|
let src = ty.lookup(db).source(db);
|
|
|
|
res[keys::TYPE_ALIAS].insert(src, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for ImplId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-05 16:34:12 -06:00
|
|
|
let mut res = DynMap::default();
|
|
|
|
|
|
|
|
let data = db.impl_data(*self);
|
|
|
|
for &item in data.items.iter() {
|
|
|
|
match item {
|
|
|
|
AssocItemId::FunctionId(func) => {
|
|
|
|
let src = func.lookup(db).source(db);
|
|
|
|
res[keys::FUNCTION].insert(src, func)
|
|
|
|
}
|
|
|
|
AssocItemId::ConstId(konst) => {
|
|
|
|
let src = konst.lookup(db).source(db);
|
|
|
|
res[keys::CONST].insert(src, konst)
|
|
|
|
}
|
|
|
|
AssocItemId::TypeAliasId(ty) => {
|
|
|
|
let src = ty.lookup(db).source(db);
|
|
|
|
res[keys::TYPE_ALIAS].insert(src, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for ModuleId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-05 16:34:12 -06:00
|
|
|
let crate_def_map = db.crate_def_map(self.krate);
|
2019-12-19 11:21:26 -06:00
|
|
|
let module_data = &crate_def_map[self.local_id];
|
2019-12-22 08:49:39 -06:00
|
|
|
module_data.scope.child_by_source(db)
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 08:49:39 -06:00
|
|
|
impl ChildBySource for ItemScope {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-22 08:49:39 -06:00
|
|
|
let mut res = DynMap::default();
|
|
|
|
self.declarations().for_each(|item| add_module_def(db, &mut res, item));
|
|
|
|
self.impls().for_each(|imp| add_impl(db, &mut res, imp));
|
|
|
|
return res;
|
|
|
|
|
2020-03-13 10:05:46 -05:00
|
|
|
fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) {
|
2019-12-22 08:49:39 -06:00
|
|
|
match item {
|
|
|
|
ModuleDefId::FunctionId(func) => {
|
|
|
|
let src = func.lookup(db).source(db);
|
|
|
|
map[keys::FUNCTION].insert(src, func)
|
|
|
|
}
|
|
|
|
ModuleDefId::ConstId(konst) => {
|
|
|
|
let src = konst.lookup(db).source(db);
|
|
|
|
map[keys::CONST].insert(src, konst)
|
|
|
|
}
|
|
|
|
ModuleDefId::StaticId(statik) => {
|
|
|
|
let src = statik.lookup(db).source(db);
|
|
|
|
map[keys::STATIC].insert(src, statik)
|
|
|
|
}
|
|
|
|
ModuleDefId::TypeAliasId(ty) => {
|
|
|
|
let src = ty.lookup(db).source(db);
|
|
|
|
map[keys::TYPE_ALIAS].insert(src, ty)
|
|
|
|
}
|
|
|
|
ModuleDefId::TraitId(trait_) => {
|
|
|
|
let src = trait_.lookup(db).source(db);
|
|
|
|
map[keys::TRAIT].insert(src, trait_)
|
|
|
|
}
|
|
|
|
ModuleDefId::AdtId(adt) => match adt {
|
|
|
|
AdtId::StructId(strukt) => {
|
|
|
|
let src = strukt.lookup(db).source(db);
|
|
|
|
map[keys::STRUCT].insert(src, strukt)
|
|
|
|
}
|
|
|
|
AdtId::UnionId(union_) => {
|
|
|
|
let src = union_.lookup(db).source(db);
|
|
|
|
map[keys::UNION].insert(src, union_)
|
|
|
|
}
|
|
|
|
AdtId::EnumId(enum_) => {
|
|
|
|
let src = enum_.lookup(db).source(db);
|
|
|
|
map[keys::ENUM].insert(src, enum_)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
2019-12-19 11:21:26 -06:00
|
|
|
}
|
2020-03-13 10:05:46 -05:00
|
|
|
fn add_impl(db: &dyn DefDatabase, map: &mut DynMap, imp: ImplId) {
|
2019-12-22 08:49:39 -06:00
|
|
|
let src = imp.lookup(db).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 {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-05 16:34:12 -06:00
|
|
|
let mut res = DynMap::default();
|
|
|
|
|
|
|
|
let arena_map = self.child_source(db);
|
|
|
|
let arena_map = arena_map.as_ref();
|
|
|
|
for (local_id, source) in arena_map.value.iter() {
|
2020-04-25 07:23:34 -05:00
|
|
|
let id = FieldId { parent: *self, local_id };
|
2019-12-05 16:34:12 -06:00
|
|
|
match source {
|
|
|
|
Either::Left(source) => {
|
|
|
|
res[keys::TUPLE_FIELD].insert(arena_map.with_value(source.clone()), id)
|
|
|
|
}
|
|
|
|
Either::Right(source) => {
|
|
|
|
res[keys::RECORD_FIELD].insert(arena_map.with_value(source.clone()), id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildBySource for EnumId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-05 16:34:12 -06:00
|
|
|
let mut res = DynMap::default();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 11:21:26 -06:00
|
|
|
|
|
|
|
impl ChildBySource for DefWithBodyId {
|
2020-03-13 10:05:46 -05:00
|
|
|
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
|
2019-12-19 11:21:26 -06:00
|
|
|
let body = db.body(*self);
|
2019-12-22 08:49:39 -06:00
|
|
|
body.item_scope.child_by_source(db)
|
2019-12-19 11:21:26 -06:00
|
|
|
}
|
|
|
|
}
|