rust/crates/ra_hir/src/source_binder.rs

233 lines
8.0 KiB
Rust
Raw Normal View History

2020-01-14 15:27:05 +01:00
//! `SourceBinder` should be the main entry point for getting info about source code.
//! It's main task is to map source syntax trees to hir-level IDs.
//!
//! It is intended to subsume `FromSource` and `SourceAnalyzer`.
use hir_def::{
child_by_source::ChildBySource,
dyn_map::DynMap,
keys::{self, Key},
resolver::{HasResolver, Resolver},
2020-01-14 17:24:00 +01:00
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ImplId, ModuleId, StaticId,
StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
2020-01-14 15:27:05 +01:00
};
2020-01-16 16:08:46 +01:00
use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind};
2020-01-14 15:27:05 +01:00
use ra_prof::profile;
use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit};
use rustc_hash::FxHashMap;
use crate::{db::HirDatabase, ModuleSource, SourceAnalyzer};
2020-01-14 16:55:35 +01:00
pub struct SourceBinder<'a, DB> {
pub db: &'a DB,
2020-01-14 15:27:05 +01:00
child_by_source_cache: FxHashMap<ChildContainer, DynMap>,
}
2020-01-14 16:55:35 +01:00
impl<DB: HirDatabase> SourceBinder<'_, DB> {
pub fn new(db: &DB) -> SourceBinder<DB> {
SourceBinder { db, child_by_source_cache: FxHashMap::default() }
}
2020-01-14 15:27:05 +01:00
pub fn analyze(
&mut self,
src: InFile<&SyntaxNode>,
offset: Option<TextUnit>,
) -> SourceAnalyzer {
let _p = profile("SourceBinder::analyzer");
2020-01-14 16:55:35 +01:00
let container = match self.find_container(src) {
2020-01-14 15:27:05 +01:00
Some(it) => it,
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
};
let resolver = match container {
ChildContainer::DefWithBodyId(def) => {
2020-01-14 16:55:35 +01:00
return SourceAnalyzer::new_for_body(self.db, def, src, offset)
2020-01-14 15:27:05 +01:00
}
2020-01-14 16:55:35 +01:00
ChildContainer::TraitId(it) => it.resolver(self.db),
ChildContainer::ImplId(it) => it.resolver(self.db),
ChildContainer::ModuleId(it) => it.resolver(self.db),
ChildContainer::EnumId(it) => it.resolver(self.db),
ChildContainer::VariantId(it) => it.resolver(self.db),
2020-01-14 15:27:05 +01:00
};
SourceAnalyzer::new_for_resolver(resolver, src)
}
2020-01-16 16:27:21 +01:00
pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
2020-01-14 17:11:47 +01:00
let id: T::ID = self.to_id(src)?;
2020-01-14 15:27:05 +01:00
Some(id.into())
}
2020-01-14 17:11:47 +01:00
fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> {
2020-01-16 16:08:46 +01:00
T::to_id(self, src)
2020-01-14 15:27:05 +01:00
}
2020-01-14 16:55:35 +01:00
fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
for container in src.cloned().ancestors_with_macros(self.db).skip(1) {
2020-01-14 15:27:05 +01:00
let res: ChildContainer = match_ast! {
match (container.value) {
ast::TraitDef(it) => {
2020-01-14 16:55:35 +01:00
let def: TraitId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
def.into()
},
ast::ImplBlock(it) => {
2020-01-14 16:55:35 +01:00
let def: ImplId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
def.into()
},
ast::FnDef(it) => {
2020-01-14 16:55:35 +01:00
let def: FunctionId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
DefWithBodyId::from(def).into()
},
ast::StaticDef(it) => {
2020-01-14 16:55:35 +01:00
let def: StaticId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
DefWithBodyId::from(def).into()
},
ast::ConstDef(it) => {
2020-01-14 16:55:35 +01:00
let def: ConstId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
DefWithBodyId::from(def).into()
},
ast::EnumDef(it) => {
2020-01-14 16:55:35 +01:00
let def: EnumId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
def.into()
},
ast::StructDef(it) => {
2020-01-14 16:55:35 +01:00
let def: StructId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
VariantId::from(def).into()
},
ast::UnionDef(it) => {
2020-01-14 16:55:35 +01:00
let def: UnionId = self.to_id(container.with_value(it))?;
2020-01-14 15:27:05 +01:00
VariantId::from(def).into()
},
// FIXME: handle out-of-line modules here
_ => { continue },
}
};
return Some(res);
}
2020-01-14 16:55:35 +01:00
let module_source = ModuleSource::from_child_node(self.db, src);
let c = crate::Module::from_definition(self.db, src.with_value(module_source))?;
2020-01-14 15:27:05 +01:00
Some(c.id.into())
}
}
2020-01-16 16:27:21 +01:00
pub trait ToId: Sized + AstNode + 'static {
type ID: Sized + Copy + 'static;
fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
-> Option<Self::ID>;
}
pub trait ToDef: ToId {
type Def: From<Self::ID>;
}
macro_rules! to_def_impls {
($(($def:path, $ast:path)),* ,) => {$(
impl ToDef for $ast {
type Def = $def;
}
)*}
}
to_def_impls![
(crate::Struct, ast::StructDef),
(crate::Enum, ast::EnumDef),
(crate::Union, ast::UnionDef),
(crate::Trait, ast::TraitDef),
(crate::ImplBlock, ast::ImplBlock),
(crate::TypeAlias, ast::TypeAliasDef),
(crate::Const, ast::ConstDef),
(crate::Static, ast::StaticDef),
(crate::Function, ast::FnDef),
(crate::StructField, ast::RecordFieldDef),
(crate::EnumVariant, ast::EnumVariant),
(crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
];
2020-01-14 15:27:05 +01:00
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
enum ChildContainer {
DefWithBodyId(DefWithBodyId),
ModuleId(ModuleId),
TraitId(TraitId),
ImplId(ImplId),
EnumId(EnumId),
VariantId(VariantId),
}
impl_froms! {
ChildContainer:
DefWithBodyId,
ModuleId,
TraitId,
ImplId,
EnumId,
VariantId,
}
2020-01-16 16:08:46 +01:00
pub trait ToIdByKey: Sized + AstNode + 'static {
2020-01-14 17:11:47 +01:00
type ID: Sized + Copy + 'static;
const KEY: Key<Self, Self::ID>;
2020-01-14 15:27:05 +01:00
}
2020-01-16 16:08:46 +01:00
impl<T: ToIdByKey> ToId for T {
type ID = <T as ToIdByKey>::ID;
fn to_id<DB: HirDatabase>(
sb: &mut SourceBinder<'_, DB>,
src: InFile<Self>,
) -> Option<Self::ID> {
let container = sb.find_container(src.as_ref().map(|it| it.syntax()))?;
let db = sb.db;
let dyn_map =
&*sb.child_by_source_cache.entry(container).or_insert_with(|| match container {
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),
});
dyn_map[T::KEY].get(&src).copied()
}
}
macro_rules! to_id_key_impls {
2020-01-14 15:27:05 +01:00
($(($id:ident, $ast:path, $key:path)),* ,) => {$(
2020-01-16 16:08:46 +01:00
impl ToIdByKey for $ast {
2020-01-14 17:11:47 +01:00
type ID = $id;
const KEY: Key<Self, Self::ID> = $key;
2020-01-14 15:27:05 +01:00
}
)*}
}
2020-01-16 16:08:46 +01:00
to_id_key_impls![
2020-01-14 15:27:05 +01:00
(StructId, ast::StructDef, keys::STRUCT),
(UnionId, ast::UnionDef, keys::UNION),
(EnumId, ast::EnumDef, keys::ENUM),
(TraitId, ast::TraitDef, keys::TRAIT),
(FunctionId, ast::FnDef, keys::FUNCTION),
(StaticId, ast::StaticDef, keys::STATIC),
(ConstId, ast::ConstDef, keys::CONST),
2020-01-14 17:24:00 +01:00
(TypeAliasId, ast::TypeAliasDef, keys::TYPE_ALIAS),
2020-01-14 15:27:05 +01:00
(ImplId, ast::ImplBlock, keys::IMPL),
2020-01-14 17:24:00 +01:00
(StructFieldId, ast::RecordFieldDef, keys::RECORD_FIELD),
(EnumVariantId, ast::EnumVariant, keys::ENUM_VARIANT),
2020-01-14 15:27:05 +01:00
];
2020-01-16 16:08:46 +01:00
// FIXME: use DynMap as well?
impl ToId for ast::MacroCall {
type ID = MacroDefId;
fn to_id<DB: HirDatabase>(
sb: &mut SourceBinder<'_, DB>,
src: InFile<Self>,
) -> Option<Self::ID> {
let kind = MacroDefKind::Declarative;
let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax()));
let module = crate::Module::from_definition(sb.db, InFile::new(src.file_id, module_src))?;
let krate = Some(module.krate().id);
let ast_id =
Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value)));
Some(MacroDefId { krate, ast_id, kind })
}
}