From 81a45ca1b3606d2c328740aa7e2dc989b9e128a5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:08:46 +0100 Subject: Make FromSource private --- crates/ra_hir/src/from_source.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/from_source.rs') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 6314be8d4..59722eba3 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -20,7 +20,7 @@ use crate::{ MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, }; -pub trait FromSource: Sized { +pub(crate) trait FromSource: Sized { type Ast; fn from_source(db: &impl DefDatabase, src: InFile) -> Option; } -- cgit v1.2.3 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/from_source.rs') 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 From a3d6ddbe694498a1bf69c6253422efb89431164e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:27:21 +0100 Subject: More natural trait setup --- crates/ra_hir/src/from_source.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src/from_source.rs') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 30e818892..caaff012a 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -14,8 +14,7 @@ use ra_syntax::{ use crate::{ db::{DefDatabase, HirDatabase}, - Const, DefWithBody, Enum, Function, ImplBlock, InFile, Local, Module, SourceBinder, Static, - Struct, Trait, TypeAlias, TypeParam, + DefWithBody, InFile, Local, Module, SourceBinder, TypeParam, }; impl Local { @@ -25,9 +24,9 @@ impl Local { let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - 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() }, + 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, } }; @@ -47,12 +46,12 @@ impl TypeParam { let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - 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() }, + 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, } }; -- cgit v1.2.3 From 7aa627fe582e8811e9e98b58c8a6da80054ba2e3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:37:51 +0100 Subject: Move more stuff to SourceBinder --- crates/ra_hir/src/from_source.rs | 59 ++-------------------------------------- 1 file changed, 3 insertions(+), 56 deletions(-) (limited to 'crates/ra_hir/src/from_source.rs') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index caaff012a..c766c3f0b 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,66 +1,13 @@ //! Finds a corresponding hir data structure for a syntax node in a specific //! file. -use hir_def::{ - child_by_source::ChildBySource, keys, nameres::ModuleSource, GenericDefId, ModuleId, -}; +use hir_def::{nameres::ModuleSource, ModuleId}; use hir_expand::name::AsName; use ra_db::FileId; use ra_prof::profile; -use ra_syntax::{ - ast::{self, AstNode, NameOwner}, - match_ast, -}; +use ra_syntax::ast::{self, AstNode, NameOwner}; -use crate::{ - db::{DefDatabase, HirDatabase}, - DefWithBody, InFile, Local, Module, SourceBinder, TypeParam, -}; - -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) => { 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, - } - }; - Some(res) - })?; - let (_body, source_map) = db.body_with_source_map(parent.into()); - let src = src.map(ast::Pat::from); - let pat_id = source_map.node_pat(src.as_ref())?; - Some(Local { parent, pat_id }) - } -} - -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) => { 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, - } - }; - Some(res) - })?; - let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?; - Some(TypeParam { id }) - } -} +use crate::{db::DefDatabase, InFile, Module}; impl Module { pub fn from_declaration(db: &impl DefDatabase, src: InFile) -> Option { -- cgit v1.2.3 From 9a6c26e34806a05260170029ace4b64adf484a23 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:53:11 +0100 Subject: Move module to SourceBinder --- crates/ra_hir/src/from_source.rs | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) (limited to 'crates/ra_hir/src/from_source.rs') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index c766c3f0b..eb76aecb1 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -2,46 +2,22 @@ //! file. use hir_def::{nameres::ModuleSource, ModuleId}; -use hir_expand::name::AsName; use ra_db::FileId; use ra_prof::profile; -use ra_syntax::ast::{self, AstNode, NameOwner}; -use crate::{db::DefDatabase, InFile, Module}; +use crate::{ + db::{DefDatabase, HirDatabase}, + InFile, Module, +}; impl Module { - pub fn from_declaration(db: &impl DefDatabase, src: InFile) -> Option { - let _p = profile("Module::from_declaration"); - let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); - - let parent_module = match parent_declaration { - Some(parent_declaration) => { - let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; - Module::from_declaration(db, src_parent) - } - None => { - let source_file = db.parse(src.file_id.original_file(db)).tree(); - let src_parent = - InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) }; - Module::from_definition(db, src_parent) - } - }?; - - let child_name = src.value.name()?.as_name(); - let def_map = db.crate_def_map(parent_module.id.krate); - let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?; - Some(parent_module.with_module_id(*child_id)) - } - - pub fn from_definition(db: &impl DefDatabase, src: InFile) -> Option { + pub fn from_definition(db: &impl HirDatabase, src: InFile) -> Option { let _p = profile("Module::from_definition"); + let mut sb = crate::SourceBinder::new(db); match src.value { ModuleSource::Module(ref module) => { assert!(!module.has_semi()); - return Module::from_declaration( - db, - InFile { file_id: src.file_id, value: module.clone() }, - ); + return sb.to_def(InFile { file_id: src.file_id, value: module.clone() }); } ModuleSource::SourceFile(_) => (), }; -- cgit v1.2.3 From 595b06a1b8fcd215c828d65ee1dd1a30c2697de9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 17:33:07 +0100 Subject: Create modules via SourceBinder --- crates/ra_hir/src/from_source.rs | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 crates/ra_hir/src/from_source.rs (limited to 'crates/ra_hir/src/from_source.rs') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs deleted file mode 100644 index eb76aecb1..000000000 --- a/crates/ra_hir/src/from_source.rs +++ /dev/null @@ -1,38 +0,0 @@ -//! Finds a corresponding hir data structure for a syntax node in a specific -//! file. - -use hir_def::{nameres::ModuleSource, ModuleId}; -use ra_db::FileId; -use ra_prof::profile; - -use crate::{ - db::{DefDatabase, HirDatabase}, - InFile, Module, -}; - -impl Module { - pub fn from_definition(db: &impl HirDatabase, src: InFile) -> Option { - let _p = profile("Module::from_definition"); - let mut sb = crate::SourceBinder::new(db); - match src.value { - ModuleSource::Module(ref module) => { - assert!(!module.has_semi()); - return sb.to_def(InFile { file_id: src.file_id, value: module.clone() }); - } - ModuleSource::SourceFile(_) => (), - }; - - let original_file = src.file_id.original_file(db); - Module::from_file(db, original_file) - } - - fn from_file(db: &impl DefDatabase, file: FileId) -> Option { - let _p = profile("Module::from_file"); - let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| { - let crate_def_map = db.crate_def_map(crate_id); - let local_id = crate_def_map.modules_for_file(file).next()?; - Some((crate_id, local_id)) - })?; - Some(Module { id: ModuleId { krate, local_id } }) - } -} -- cgit v1.2.3