From 8691ae8ac04ef9dc089a377770da86a952b0e4c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:16:31 +0100 Subject: Removed FromSource --- crates/ra_hir/src/from_source.rs | 181 ++++----------------------------------- 1 file changed, 16 insertions(+), 165 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 59722eba3..30e818892 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -2,139 +2,32 @@ //! file. use hir_def::{ - child_by_source::ChildBySource, dyn_map::DynMap, keys, keys::Key, nameres::ModuleSource, - ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, - StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId, + child_by_source::ChildBySource, keys, nameres::ModuleSource, GenericDefId, ModuleId, }; -use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; +use hir_expand::name::AsName; use ra_db::FileId; use ra_prof::profile; use ra_syntax::{ ast::{self, AstNode, NameOwner}, - match_ast, SyntaxNode, + match_ast, }; use crate::{ db::{DefDatabase, HirDatabase}, - Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, - MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, + Const, DefWithBody, Enum, Function, ImplBlock, InFile, Local, Module, SourceBinder, Static, + Struct, Trait, TypeAlias, TypeParam, }; -pub(crate) trait FromSource: Sized { - type Ast; - fn from_source(db: &impl DefDatabase, src: InFile) -> Option; -} - -pub trait FromSourceByContainer: Sized { - type Ast: AstNode + 'static; - type Id: Copy + 'static; - const KEY: Key; -} - -impl FromSource for T -where - T: From<::Id>, -{ - type Ast = ::Ast; - fn from_source(db: &impl DefDatabase, src: InFile) -> Option { - analyze_container(db, src.as_ref().map(|it| it.syntax()))[T::KEY] - .get(&src) - .copied() - .map(Self::from) - } -} - -macro_rules! from_source_by_container_impls { - ($(($hir:ident, $id:ident, $ast:path, $key:path)),* ,) => {$( - impl FromSourceByContainer for $hir { - type Ast = $ast; - type Id = $id; - const KEY: Key = $key; - } - )*} -} - -from_source_by_container_impls![ - (Struct, StructId, ast::StructDef, keys::STRUCT), - (Union, UnionId, ast::UnionDef, keys::UNION), - (Enum, EnumId, ast::EnumDef, keys::ENUM), - (Trait, TraitId, ast::TraitDef, keys::TRAIT), - (Function, FunctionId, ast::FnDef, keys::FUNCTION), - (Static, StaticId, ast::StaticDef, keys::STATIC), - (Const, ConstId, ast::ConstDef, keys::CONST), - (TypeAlias, TypeAliasId, ast::TypeAliasDef, keys::TYPE_ALIAS), - (ImplBlock, ImplId, ast::ImplBlock, keys::IMPL), -]; - -impl FromSource for MacroDef { - type Ast = ast::MacroCall; - fn from_source(db: &impl DefDatabase, src: InFile) -> Option { - let kind = MacroDefKind::Declarative; - - let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); - let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; - let krate = Some(module.krate().id); - - let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value))); - - let id: MacroDefId = MacroDefId { krate, ast_id, kind }; - Some(MacroDef { id }) - } -} - -impl FromSource for EnumVariant { - type Ast = ast::EnumVariant; - fn from_source(db: &impl DefDatabase, src: InFile) -> Option { - let parent_enum = src.value.parent_enum(); - let src_enum = InFile { file_id: src.file_id, value: parent_enum }; - let parent_enum = Enum::from_source(db, src_enum)?; - parent_enum.id.child_by_source(db)[keys::ENUM_VARIANT] - .get(&src) - .copied() - .map(EnumVariant::from) - } -} - -impl FromSource for StructField { - type Ast = FieldSource; - fn from_source(db: &impl DefDatabase, src: InFile) -> Option { - let src = src.as_ref(); - - // FIXME this is buggy - let variant_id: VariantId = match src.value { - FieldSource::Named(field) => { - let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?; - let src = InFile { file_id: src.file_id, value }; - let def = Struct::from_source(db, src)?; - def.id.into() - } - FieldSource::Pos(field) => { - let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?; - let src = InFile { file_id: src.file_id, value }; - let def = EnumVariant::from_source(db, src)?; - EnumVariantId::from(def).into() - } - }; - - let dyn_map = variant_id.child_by_source(db); - match src.value { - FieldSource::Pos(it) => dyn_map[keys::TUPLE_FIELD].get(&src.with_value(it.clone())), - FieldSource::Named(it) => dyn_map[keys::RECORD_FIELD].get(&src.with_value(it.clone())), - } - .copied() - .map(StructField::from) - } -} - impl Local { pub fn from_source(db: &impl HirDatabase, src: InFile) -> Option { + let mut sb = SourceBinder::new(db); let file_id = src.file_id; let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - ast::ConstDef(value) => { Const::from_source(db, InFile { value, file_id})?.into() }, - ast::StaticDef(value) => { Static::from_source(db, InFile { value, file_id})?.into() }, - ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.into() }, + ast::ConstDef(value) => { sb.to_def::(InFile { value, file_id})?.into() }, + ast::StaticDef(value) => { sb.to_def::(InFile { value, file_id})?.into() }, + ast::FnDef(value) => { sb.to_def::(InFile { value, file_id})?.into() }, _ => return None, } }; @@ -149,16 +42,17 @@ impl Local { impl TypeParam { pub fn from_source(db: &impl HirDatabase, src: InFile) -> Option { + let mut sb = SourceBinder::new(db); let file_id = src.file_id; let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.id.into() }, - ast::StructDef(value) => { Struct::from_source(db, InFile { value, file_id})?.id.into() }, - ast::EnumDef(value) => { Enum::from_source(db, InFile { value, file_id})?.id.into() }, - ast::TraitDef(value) => { Trait::from_source(db, InFile { value, file_id})?.id.into() }, - ast::TypeAliasDef(value) => { TypeAlias::from_source(db, InFile { value, file_id})?.id.into() }, - ast::ImplBlock(value) => { ImplBlock::from_source(db, InFile { value, file_id})?.id.into() }, + ast::FnDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, + ast::StructDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, + ast::EnumDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, + ast::TraitDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, + ast::TypeAliasDef(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, + ast::ImplBlock(value) => { sb.to_def::(InFile { value, file_id})?.id.into() }, _ => return None, } }; @@ -220,46 +114,3 @@ impl Module { Some(Module { id: ModuleId { krate, local_id } }) } } - -fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { - let _p = profile("analyze_container"); - return child_by_source(db, src).unwrap_or_default(); - - fn child_by_source(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option { - for container in src.value.ancestors().skip(1) { - let res = match_ast! { - match container { - ast::TraitDef(it) => { - let def = Trait::from_source(db, src.with_value(it))?; - def.id.child_by_source(db) - }, - ast::ImplBlock(it) => { - let def = ImplBlock::from_source(db, src.with_value(it))?; - def.id.child_by_source(db) - }, - ast::FnDef(it) => { - let def = Function::from_source(db, src.with_value(it))?; - DefWithBodyId::from(def.id) - .child_by_source(db) - }, - ast::StaticDef(it) => { - let def = Static::from_source(db, src.with_value(it))?; - DefWithBodyId::from(def.id) - .child_by_source(db) - }, - ast::ConstDef(it) => { - let def = Const::from_source(db, src.with_value(it))?; - DefWithBodyId::from(def.id) - .child_by_source(db) - }, - _ => { continue }, - } - }; - return Some(res); - } - - let module_source = ModuleSource::from_child_node(db, src); - let c = Module::from_definition(db, src.with_value(module_source))?; - Some(c.id.child_by_source(db)) - } -} -- cgit v1.2.3