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 +- crates/ra_hir/src/lib.rs | 1 - crates/ra_hir/src/source_binder.rs | 68 +++++++++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir') 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; } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index a2350573c..11829f42a 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -45,7 +45,6 @@ pub use crate::{ MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, }, - from_source::FromSource, has_source::HasSource, source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, source_binder::SourceBinder, diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 00541dbe1..66930e492 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -11,7 +11,7 @@ use hir_def::{ ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ImplId, ModuleId, StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, }; -use hir_expand::InFile; +use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind}; use ra_prof::profile; use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit}; use rustc_hash::FxHashMap; @@ -62,18 +62,7 @@ impl SourceBinder<'_, DB> { } fn to_id(&mut self, src: InFile) -> Option { - let container = self.find_container(src.as_ref().map(|it| it.syntax()))?; - let db = self.db; - let dyn_map = - &*self.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() + T::to_id(self, src) } fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option { @@ -145,20 +134,47 @@ impl_froms! { } pub trait ToId: Sized + AstNode + 'static { + type ID: Sized + Copy + 'static; + fn to_id(sb: &mut SourceBinder<'_, DB>, src: InFile) + -> Option; +} + +pub trait ToIdByKey: Sized + AstNode + 'static { type ID: Sized + Copy + 'static; const KEY: Key; } -macro_rules! to_id_impls { +impl ToId for T { + type ID = ::ID; + fn to_id( + sb: &mut SourceBinder<'_, DB>, + src: InFile, + ) -> Option { + 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 { ($(($id:ident, $ast:path, $key:path)),* ,) => {$( - impl ToId for $ast { + impl ToIdByKey for $ast { type ID = $id; const KEY: Key = $key; } )*} } -to_id_impls![ +to_id_key_impls![ (StructId, ast::StructDef, keys::STRUCT), (UnionId, ast::UnionDef, keys::UNION), (EnumId, ast::EnumDef, keys::ENUM), @@ -171,3 +187,23 @@ to_id_impls![ (StructFieldId, ast::RecordFieldDef, keys::RECORD_FIELD), (EnumVariantId, ast::EnumVariant, keys::ENUM_VARIANT), ]; + +// FIXME: use DynMap as well? +impl ToId for ast::MacroCall { + type ID = MacroDefId; + fn to_id( + sb: &mut SourceBinder<'_, DB>, + src: InFile, + ) -> Option { + 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 }) + } +} -- 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') 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 +++++++++--------- crates/ra_hir/src/source_binder.rs | 45 ++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 22 deletions(-) (limited to 'crates/ra_hir') 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, } }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 66930e492..c02175c06 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -52,11 +52,7 @@ impl SourceBinder<'_, DB> { SourceAnalyzer::new_for_resolver(resolver, src) } - pub fn to_def(&mut self, src: InFile) -> Option - where - D: From, - T: ToId, - { + pub fn to_def(&mut self, src: InFile) -> Option { let id: T::ID = self.to_id(src)?; Some(id.into()) } @@ -114,6 +110,39 @@ impl SourceBinder<'_, DB> { } } +pub trait ToId: Sized + AstNode + 'static { + type ID: Sized + Copy + 'static; + fn to_id(sb: &mut SourceBinder<'_, DB>, src: InFile) + -> Option; +} + +pub trait ToDef: ToId { + type Def: From; +} + +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 +]; + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] enum ChildContainer { DefWithBodyId(DefWithBodyId), @@ -133,12 +162,6 @@ impl_froms! { VariantId, } -pub trait ToId: Sized + AstNode + 'static { - type ID: Sized + Copy + 'static; - fn to_id(sb: &mut SourceBinder<'_, DB>, src: InFile) - -> Option; -} - pub trait ToIdByKey: Sized + AstNode + 'static { type ID: Sized + Copy + 'static; const KEY: Key; -- 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 ++---------------------------- crates/ra_hir/src/source_binder.rs | 73 +++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 64 deletions(-) (limited to 'crates/ra_hir') 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 { diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index c02175c06..97e3aef34 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -8,15 +8,15 @@ use hir_def::{ dyn_map::DynMap, keys::{self, Key}, resolver::{HasResolver, Resolver}, - ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ImplId, ModuleId, StaticId, - StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, + ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, + StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, }; use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind}; use ra_prof::profile; use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit}; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, ModuleSource, SourceAnalyzer}; +use crate::{db::HirDatabase, Local, ModuleSource, SourceAnalyzer, TypeParam}; pub struct SourceBinder<'a, DB> { pub db: &'a DB, @@ -53,8 +53,7 @@ impl SourceBinder<'_, DB> { } pub fn to_def(&mut self, src: InFile) -> Option { - let id: T::ID = self.to_id(src)?; - Some(id.into()) + T::to_def(self, src) } fn to_id(&mut self, src: InFile) -> Option { @@ -110,20 +109,27 @@ impl SourceBinder<'_, DB> { } } -pub trait ToId: Sized + AstNode + 'static { +pub trait ToId: Sized { type ID: Sized + Copy + 'static; fn to_id(sb: &mut SourceBinder<'_, DB>, src: InFile) -> Option; } -pub trait ToDef: ToId { - type Def: From; +pub trait ToDef: Sized + AstNode + 'static { + type Def; + fn to_def( + sb: &mut SourceBinder<'_, DB>, + src: InFile, + ) -> Option; } macro_rules! to_def_impls { ($(($def:path, $ast:path)),* ,) => {$( impl ToDef for $ast { type Def = $def; + fn to_def(sb: &mut SourceBinder<'_, DB>, src: InFile) + -> Option + { sb.to_id(src).map(Into::into) } } )*} } @@ -230,3 +236,54 @@ impl ToId for ast::MacroCall { Some(MacroDefId { krate, ast_id, kind }) } } + +impl ToDef for ast::BindPat { + type Def = Local; + + fn to_def(sb: &mut SourceBinder<'_, DB>, src: InFile) -> Option { + let file_id = src.file_id; + let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| { + let res = match_ast! { + match it { + ast::ConstDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + ast::StaticDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + _ => return None, + } + }; + Some(res) + })?; + let (_body, source_map) = sb.db.body_with_source_map(parent); + let src = src.map(ast::Pat::from); + let pat_id = source_map.node_pat(src.as_ref())?; + Some(Local { parent: parent.into(), pat_id }) + } +} + +impl ToDef for ast::TypeParam { + type Def = TypeParam; + + fn to_def( + sb: &mut SourceBinder<'_, DB>, + src: InFile, + ) -> Option { + let mut sb = SourceBinder::new(sb.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_id(InFile { value, file_id})?.into() }, + ast::StructDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + ast::EnumDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + ast::TraitDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + ast::TypeAliasDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, + ast::ImplBlock(value) => { sb.to_id(InFile { value, file_id})?.into() }, + _ => return None, + } + }; + Some(res) + })?; + let &id = parent.child_by_source(sb.db)[keys::TYPE_PARAM].get(&src)?; + Some(TypeParam { id }) + } +} -- cgit v1.2.3 From 16cfc8d50c9b5b4deb1065b9394e7663df7e9500 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:44:25 +0100 Subject: Cache source for generics --- crates/ra_hir/src/source_binder.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 97e3aef34..00f48177b 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -48,6 +48,7 @@ impl SourceBinder<'_, DB> { ChildContainer::ModuleId(it) => it.resolver(self.db), ChildContainer::EnumId(it) => it.resolver(self.db), ChildContainer::VariantId(it) => it.resolver(self.db), + ChildContainer::GenericDefId(it) => it.resolver(self.db), }; SourceAnalyzer::new_for_resolver(resolver, src) } @@ -107,6 +108,19 @@ impl SourceBinder<'_, DB> { let c = crate::Module::from_definition(self.db, src.with_value(module_source))?; Some(c.id.into()) } + + fn child_by_source(&mut self, container: ChildContainer) -> &DynMap { + let db = self.db; + self.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), + ChildContainer::GenericDefId(it) => it.child_by_source(db), + }) + } } pub trait ToId: Sized { @@ -157,6 +171,9 @@ enum ChildContainer { ImplId(ImplId), EnumId(EnumId), VariantId(VariantId), + /// XXX: this might be the same def as, for example an `EnumId`. However, + /// here the children generic parameters, and not, eg enum variants. + GenericDefId(GenericDefId), } impl_froms! { ChildContainer: @@ -166,6 +183,7 @@ impl_froms! { ImplId, EnumId, VariantId, + GenericDefId } pub trait ToIdByKey: Sized + AstNode + 'static { @@ -189,6 +207,7 @@ impl ToId for T { 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::GenericDefId(it) => it.child_by_source(db), }); dyn_map[T::KEY].get(&src).copied() } @@ -283,7 +302,7 @@ impl ToDef for ast::TypeParam { }; Some(res) })?; - let &id = parent.child_by_source(sb.db)[keys::TYPE_PARAM].get(&src)?; + let &id = sb.child_by_source(parent.into())[keys::TYPE_PARAM].get(&src)?; Some(TypeParam { id }) } } -- 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 ++++++-------------------------- crates/ra_hir/src/source_binder.rs | 44 +++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 34 deletions(-) (limited to 'crates/ra_hir') 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(_) => (), }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 00f48177b..26eedbb2c 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -11,12 +11,15 @@ use hir_def::{ ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, }; -use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind}; +use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; use ra_prof::profile; -use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit}; +use ra_syntax::{ + ast::{self, NameOwner}, + match_ast, AstNode, SyntaxNode, TextUnit, +}; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, ModuleSource, SourceAnalyzer, TypeParam}; +use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; pub struct SourceBinder<'a, DB> { pub db: &'a DB, @@ -306,3 +309,38 @@ impl ToDef for ast::TypeParam { Some(TypeParam { id }) } } + +impl ToDef for ast::Module { + type Def = Module; + + fn to_def( + sb: &mut SourceBinder<'_, DB>, + src: InFile, + ) -> Option { + { + let _p = profile("ast::Module::to_def"); + 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 }; + sb.to_def(src_parent) + } + None => { + let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); + let src_parent = InFile { + file_id: src.file_id, + value: ModuleSource::SourceFile(source_file), + }; + Module::from_definition(sb.db, src_parent) + } + }?; + + let child_name = src.value.name()?.as_name(); + let def_map = sb.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)) + } + } +} -- 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 ------------------------------ crates/ra_hir/src/lib.rs | 1 - crates/ra_hir/src/source_binder.rs | 47 +++++++++++++++++++++++--------------- 3 files changed, 28 insertions(+), 58 deletions(-) delete mode 100644 crates/ra_hir/src/from_source.rs (limited to 'crates/ra_hir') 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 } }) - } -} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 11829f42a..e1c7b7a20 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -36,7 +36,6 @@ mod from_id; mod code_model; mod has_source; -mod from_source; pub use crate::{ code_model::{ diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 26eedbb2c..fa225a4ed 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -19,7 +19,8 @@ use ra_syntax::{ }; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; +use crate::{db::HirDatabase, Local, Module, SourceAnalyzer, TypeParam}; +use ra_db::FileId; pub struct SourceBinder<'a, DB> { pub db: &'a DB, @@ -60,6 +61,16 @@ impl SourceBinder<'_, DB> { T::to_def(self, src) } + pub fn to_module_def(&mut self, file: FileId) -> Option { + let _p = profile("SourceBinder::to_module_def"); + let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { + let crate_def_map = self.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 } }) + } + fn to_id(&mut self, src: InFile) -> Option { T::to_id(self, src) } @@ -107,8 +118,7 @@ impl SourceBinder<'_, DB> { return Some(res); } - let module_source = ModuleSource::from_child_node(self.db, src); - let c = crate::Module::from_definition(self.db, src.with_value(module_source))?; + let c = self.to_module_def(src.file_id.original_file(self.db))?; Some(c.id.into()) } @@ -248,14 +258,12 @@ impl ToId for ast::MacroCall { ) -> Option { 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 krate = sb.to_module_def(src.file_id.original_file(sb.db))?.id.krate; 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 }) + Some(MacroDefId { krate: Some(krate), ast_id, kind }) } } @@ -319,21 +327,22 @@ impl ToDef for ast::Module { ) -> Option { { let _p = profile("ast::Module::to_def"); - let parent_declaration = - src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); + let parent_declaration = src + .as_ref() + .map(|it| it.syntax()) + .cloned() + .ancestors_with_macros(sb.db) + .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) => { - let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; - sb.to_def(src_parent) - } + Some(parent_declaration) => sb.to_def(parent_declaration), None => { - let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); - let src_parent = InFile { - file_id: src.file_id, - value: ModuleSource::SourceFile(source_file), - }; - Module::from_definition(sb.db, src_parent) + let file_id = src.file_id.original_file(sb.db); + sb.to_module_def(file_id) } }?; -- cgit v1.2.3 From 6bc236253dd9cd8e0729bbb681f0e03b20bc3627 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 17:52:58 +0100 Subject: Correctly discover module containers --- crates/ra_hir/src/source_binder.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index fa225a4ed..b2b17d510 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -111,7 +111,10 @@ impl SourceBinder<'_, DB> { let def: UnionId = self.to_id(container.with_value(it))?; VariantId::from(def).into() }, - // FIXME: handle out-of-line modules here + ast::Module(it) => { + let def: ModuleId = self.to_id(container.with_value(it))?; + def.into() + }, _ => { continue }, } }; @@ -162,6 +165,7 @@ macro_rules! to_def_impls { } to_def_impls![ + (crate::Module, ast::Module), (crate::Struct, ast::StructDef), (crate::Enum, ast::EnumDef), (crate::Union, ast::UnionDef), @@ -318,13 +322,13 @@ impl ToDef for ast::TypeParam { } } -impl ToDef for ast::Module { - type Def = Module; +impl ToId for ast::Module { + type ID = ModuleId; - fn to_def( + fn to_id( sb: &mut SourceBinder<'_, DB>, src: InFile, - ) -> Option { + ) -> Option { { let _p = profile("ast::Module::to_def"); let parent_declaration = src @@ -339,17 +343,17 @@ impl ToDef for ast::Module { }); let parent_module = match parent_declaration { - Some(parent_declaration) => sb.to_def(parent_declaration), + Some(parent_declaration) => sb.to_id(parent_declaration)?, None => { let file_id = src.file_id.original_file(sb.db); - sb.to_module_def(file_id) + sb.to_module_def(file_id)?.id } - }?; + }; let child_name = src.value.name()?.as_name(); - let def_map = sb.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)) + let def_map = sb.db.crate_def_map(parent_module.krate); + let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; + Some(ModuleId { krate: parent_module.krate, local_id: child_id }) } } } -- cgit v1.2.3 From 846f11c2177f3e3a6348acb67503028180b3c1f8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 17:57:50 +0100 Subject: Fix comment --- crates/ra_hir/src/source_binder.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index b2b17d510..f3150f578 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -1,7 +1,5 @@ -//! `SourceBinder` should be the main entry point for getting info about source code. +//! `SourceBinder` is 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, -- cgit v1.2.3