rust-analyzer/crates/hir/src/semantics/source_to_def.rs

350 lines
14 KiB
Rust
Raw Normal View History

2020-02-29 17:32:18 +00:00
//! Maps *syntax* of various definitions to their semantic ids.
2020-08-13 14:25:38 +00:00
use base_db::FileId;
2020-02-29 17:32:18 +00:00
use hir_def::{
child_by_source::ChildBySource,
dyn_map::DynMap,
2020-12-23 15:34:30 +00:00
expr::{LabelId, PatId},
2020-02-29 17:32:18 +00:00
keys::{self, Key},
2021-01-01 09:06:42 +00:00
ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId,
ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
UnionId, VariantId,
2020-02-29 17:32:18 +00:00
};
use hir_expand::{name::AsName, AstId, MacroCallId, MacroDefKind};
2020-08-12 16:26:51 +00:00
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
2020-08-12 16:26:51 +00:00
use stdx::impl_from;
use syntax::{
2020-02-29 17:32:18 +00:00
ast::{self, NameOwner},
match_ast, AstNode, SyntaxNode,
};
use crate::{db::HirDatabase, InFile, MacroDefId};
pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;
pub(super) struct SourceToDefCtx<'a, 'b> {
pub(super) db: &'b dyn HirDatabase,
2020-02-29 17:32:18 +00:00
pub(super) cache: &'a mut SourceToDefCache,
}
impl SourceToDefCtx<'_, '_> {
pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> {
2020-08-12 14:32:36 +00:00
let _p = profile::span("SourceBinder::to_module_def");
let mut mods = SmallVec::new();
for &crate_id in self.db.relevant_crates(file).iter() {
// FIXME: inner items
2020-02-29 17:32:18 +00:00
let crate_def_map = self.db.crate_def_map(crate_id);
mods.extend(
crate_def_map
.modules_for_file(file)
.map(|local_id| crate_def_map.module_id(local_id)),
)
}
mods
2020-02-29 17:32:18 +00:00
}
pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
2020-08-12 14:32:36 +00:00
let _p = profile::span("module_to_def");
2020-02-29 17:32:18 +00:00
let parent_declaration = src
.as_ref()
.map(|it| it.syntax())
.cloned()
.ancestors_with_macros(self.db.upcast())
2020-02-29 17:32:18 +00:00
.skip(1)
.find_map(|it| {
let m = ast::Module::cast(it.value.clone())?;
Some(it.with_value(m))
});
let parent_module = match parent_declaration {
Some(parent_declaration) => self.module_to_def(parent_declaration),
None => {
let file_id = src.file_id.original_file(self.db.upcast());
self.file_to_def(file_id).get(0).copied()
2020-02-29 17:32:18 +00:00
}
}?;
let child_name = src.value.name()?.as_name();
let def_map = parent_module.def_map(self.db.upcast());
2020-02-29 17:32:18 +00:00
let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
Some(def_map.module_id(child_id))
2020-02-29 17:32:18 +00:00
}
pub(super) fn source_file_to_def(&mut self, src: InFile<ast::SourceFile>) -> Option<ModuleId> {
let _p = profile::span("source_file_to_def");
let file_id = src.file_id.original_file(self.db.upcast());
self.file_to_def(file_id).get(0).copied()
}
2020-07-30 16:17:28 +00:00
pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::TRAIT)
}
2020-07-30 16:28:28 +00:00
pub(super) fn impl_to_def(&mut self, src: InFile<ast::Impl>) -> Option<ImplId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::IMPL)
}
2020-07-30 12:51:08 +00:00
pub(super) fn fn_to_def(&mut self, src: InFile<ast::Fn>) -> Option<FunctionId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::FUNCTION)
}
2020-07-30 15:50:40 +00:00
pub(super) fn struct_to_def(&mut self, src: InFile<ast::Struct>) -> Option<StructId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::STRUCT)
}
2020-07-30 15:52:53 +00:00
pub(super) fn enum_to_def(&mut self, src: InFile<ast::Enum>) -> Option<EnumId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::ENUM)
}
2020-07-30 15:36:46 +00:00
pub(super) fn union_to_def(&mut self, src: InFile<ast::Union>) -> Option<UnionId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::UNION)
}
2020-07-30 16:02:20 +00:00
pub(super) fn static_to_def(&mut self, src: InFile<ast::Static>) -> Option<StaticId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::STATIC)
}
2020-07-30 16:02:20 +00:00
pub(super) fn const_to_def(&mut self, src: InFile<ast::Const>) -> Option<ConstId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::CONST)
}
2020-07-30 13:25:46 +00:00
pub(super) fn type_alias_to_def(&mut self, src: InFile<ast::TypeAlias>) -> Option<TypeAliasId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::TYPE_ALIAS)
}
2020-07-30 14:49:13 +00:00
pub(super) fn record_field_to_def(&mut self, src: InFile<ast::RecordField>) -> Option<FieldId> {
2020-02-29 17:32:18 +00:00
self.to_def(src, keys::RECORD_FIELD)
}
2020-07-30 14:49:13 +00:00
pub(super) fn tuple_field_to_def(&mut self, src: InFile<ast::TupleField>) -> Option<FieldId> {
2020-02-29 17:35:45 +00:00
self.to_def(src, keys::TUPLE_FIELD)
}
2020-02-29 17:32:18 +00:00
pub(super) fn enum_variant_to_def(
&mut self,
2020-07-30 15:56:53 +00:00
src: InFile<ast::Variant>,
2020-02-29 17:32:18 +00:00
) -> Option<EnumVariantId> {
2020-07-30 15:56:53 +00:00
self.to_def(src, keys::VARIANT)
2020-02-29 17:32:18 +00:00
}
pub(super) fn bind_pat_to_def(
&mut self,
2020-07-31 18:09:09 +00:00
src: InFile<ast::IdentPat>,
2020-02-29 17:32:18 +00:00
) -> Option<(DefWithBodyId, PatId)> {
2020-12-23 15:34:30 +00:00
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
2020-02-29 17:32:18 +00:00
let (_body, source_map) = self.db.body_with_source_map(container);
let src = src.map(ast::Pat::from);
let pat_id = source_map.node_pat(src.as_ref())?;
Some((container, pat_id))
}
pub(super) fn self_param_to_def(
&mut self,
src: InFile<ast::SelfParam>,
) -> Option<(DefWithBodyId, PatId)> {
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
let (_body, source_map) = self.db.body_with_source_map(container);
let pat_id = source_map.node_self_param(src.as_ref())?;
Some((container, pat_id))
}
2020-12-23 15:34:30 +00:00
pub(super) fn label_to_def(
&mut self,
src: InFile<ast::Label>,
) -> Option<(DefWithBodyId, LabelId)> {
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
let (_body, source_map) = self.db.body_with_source_map(container);
let label_id = source_map.node_label(src.as_ref())?;
Some((container, label_id))
}
2020-02-29 17:32:18 +00:00
pub(super) fn item_to_macro_call(&mut self, src: InFile<ast::Item>) -> Option<MacroCallId> {
let map = self.dyn_map(src.as_ref())?;
map[keys::ATTR_MACRO].get(&src).copied()
}
2020-02-29 17:32:18 +00:00
fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
&mut self,
src: InFile<Ast>,
key: Key<Ast, ID>,
) -> Option<ID> {
self.dyn_map(src.as_ref())?[key].get(&src).copied()
}
fn dyn_map<Ast: AstNode + 'static>(&mut self, src: InFile<&Ast>) -> Option<&DynMap> {
let container = self.find_container(src.map(|it| it.syntax()))?;
2020-02-29 17:32:18 +00:00
let db = self.db;
let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
Some(dyn_map)
2020-02-29 17:32:18 +00:00
}
pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
let container: ChildContainer =
self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
2020-02-29 17:32:18 +00:00
let db = self.db;
let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
dyn_map[keys::TYPE_PARAM].get(&src).copied()
}
pub(super) fn lifetime_param_to_def(
&mut self,
src: InFile<ast::LifetimeParam>,
) -> Option<LifetimeParamId> {
let container: ChildContainer =
self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
let db = self.db;
let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
dyn_map[keys::LIFETIME_PARAM].get(&src).copied()
}
2021-01-01 09:06:42 +00:00
pub(super) fn const_param_to_def(
&mut self,
src: InFile<ast::ConstParam>,
) -> Option<ConstParamId> {
let container: ChildContainer =
self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
let db = self.db;
let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
dyn_map[keys::CONST_PARAM].get(&src).copied()
}
2020-02-29 17:32:18 +00:00
// FIXME: use DynMap as well?
pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
let kind = MacroDefKind::Declarative(ast_id);
let file_id = src.file_id.original_file(self.db.upcast());
let krate = self.file_to_def(file_id).get(0).copied()?.krate();
Some(MacroDefId { krate, kind, local_inner: false })
2020-02-29 17:32:18 +00:00
}
pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
2021-06-08 14:42:48 +00:00
if let Some(res) = self.container_to_def(container) {
return Some(res);
}
2020-02-29 17:32:18 +00:00
}
let def = self.file_to_def(src.file_id.original_file(self.db.upcast())).get(0).copied()?;
2020-02-29 17:32:18 +00:00
Some(def.into())
}
2021-06-08 14:42:48 +00:00
fn container_to_def(&mut self, container: InFile<SyntaxNode>) -> Option<ChildContainer> {
let cont = match_ast! {
match (container.value) {
ast::Module(it) => {
let def = self.module_to_def(container.with_value(it))?;
def.into()
},
ast::Trait(it) => {
let def = self.trait_to_def(container.with_value(it))?;
def.into()
},
ast::Impl(it) => {
let def = self.impl_to_def(container.with_value(it))?;
def.into()
},
ast::Fn(it) => {
let def = self.fn_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into()
},
ast::Struct(it) => {
let def = self.struct_to_def(container.with_value(it))?;
VariantId::from(def).into()
},
ast::Enum(it) => {
let def = self.enum_to_def(container.with_value(it))?;
def.into()
},
ast::Union(it) => {
let def = self.union_to_def(container.with_value(it))?;
VariantId::from(def).into()
},
ast::Static(it) => {
let def = self.static_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into()
},
ast::Const(it) => {
let def = self.const_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into()
},
ast::TypeAlias(it) => {
let def = self.type_alias_to_def(container.with_value(it))?;
def.into()
},
ast::Variant(it) => {
let def = self.enum_variant_to_def(container.with_value(it))?;
VariantId::from(def).into()
},
_ => return None,
}
};
Some(cont)
}
fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
2020-02-29 17:32:18 +00:00
let res: GenericDefId = match_ast! {
match (container.value) {
2020-07-30 12:51:08 +00:00
ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
2020-07-30 15:50:40 +00:00
ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(),
2020-07-30 15:52:53 +00:00
ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
2020-07-30 16:17:28 +00:00
ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
2020-07-30 13:25:46 +00:00
ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(),
2020-07-30 16:28:28 +00:00
ast::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
2020-02-29 17:32:18 +00:00
_ => continue,
}
};
return Some(res);
}
None
}
2020-12-23 15:34:30 +00:00
fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
2020-02-29 17:32:18 +00:00
let res: DefWithBodyId = match_ast! {
match (container.value) {
2020-07-30 16:02:20 +00:00
ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),
ast::Static(it) => self.static_to_def(container.with_value(it))?.into(),
2020-07-30 12:51:08 +00:00
ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
2020-02-29 17:32:18 +00:00
_ => continue,
}
};
return Some(res);
}
None
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) enum ChildContainer {
DefWithBodyId(DefWithBodyId),
ModuleId(ModuleId),
TraitId(TraitId),
ImplId(ImplId),
EnumId(EnumId),
VariantId(VariantId),
TypeAliasId(TypeAliasId),
2020-02-29 17:32:18 +00:00
/// XXX: this might be the same def as, for example an `EnumId`. However,
/// here the children are generic parameters, and not, eg enum variants.
2020-02-29 17:32:18 +00:00
GenericDefId(GenericDefId),
}
impl_from! {
2020-02-29 17:32:18 +00:00
DefWithBodyId,
ModuleId,
TraitId,
ImplId,
EnumId,
VariantId,
TypeAliasId,
2020-02-29 17:32:18 +00:00
GenericDefId
for ChildContainer
2020-02-29 17:32:18 +00:00
}
impl ChildContainer {
fn child_by_source(self, db: &dyn HirDatabase) -> DynMap {
let db = db.upcast();
2020-02-29 17:32:18 +00:00
match self {
ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
ChildContainer::ModuleId(it) => it.child_by_source(db),
ChildContainer::TraitId(it) => it.child_by_source(db),
ChildContainer::ImplId(it) => it.child_by_source(db),
ChildContainer::EnumId(it) => it.child_by_source(db),
ChildContainer::VariantId(it) => it.child_by_source(db),
ChildContainer::TypeAliasId(_) => DynMap::default(),
2020-02-29 17:32:18 +00:00
ChildContainer::GenericDefId(it) => it.child_by_source(db),
}
}
}