From 09bc7ca74dc920a01db32b2a29f70eb9f10c9853 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Feb 2020 13:22:46 +0100 Subject: Reduce visibility --- crates/ra_hir/src/from_id.rs | 1 + crates/ra_hir/src/semantics.rs | 72 +++++++++++++++++++++++++++++--- crates/ra_hir/src/source_binder.rs | 84 +++++++------------------------------- 3 files changed, 81 insertions(+), 76 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index c16c17072..3aa7c4870 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -40,6 +40,7 @@ from_id![ (hir_def::ConstId, crate::Const), (hir_def::FunctionId, crate::Function), (hir_def::ImplId, crate::ImplBlock), + (hir_def::TypeParamId, crate::TypeParam), (hir_expand::MacroDefId, crate::MacroDef) ]; diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 22a7e7588..9fedb7657 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -4,16 +4,16 @@ use std::{cell::RefCell, fmt, iter::successors}; use hir_def::{ resolver::{self, HasResolver, Resolver}, - TraitId, + DefWithBodyId, TraitId, }; use ra_db::{FileId, FileRange}; -use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextUnit}; +use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextUnit}; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ db::HirDatabase, source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer}, - source_binder::{ChildContainer, SourceBinder, ToDef}, + source_binder::{ChildContainer, SourceBinder}, Function, HirFileId, InFile, Local, MacroDef, Module, Name, Origin, Path, PathResolution, ScopeDef, StructField, Trait, Type, TypeParam, VariantDef, }; @@ -129,9 +129,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; pub fn to_def(&self, src: &T) -> Option { - let src = self.find_file(src.syntax().clone()).with_value(src.clone()); - let mut sb = self.sb.borrow_mut(); - T::to_def(self.db, &mut sb, src) + T::to_def(self, src) } pub fn to_module_def(&self, file: FileId) -> Option { @@ -227,6 +225,68 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { } } +pub trait ToDef: Sized + AstNode + 'static { + type Def; + fn to_def(sema: &Semantics, src: &Self) -> Option; +} + +macro_rules! to_def_impls { + ($(($def:path, $ast:path)),* ,) => {$( + impl ToDef for $ast { + type Def = $def; + fn to_def(sema: &Semantics, src: &Self) + -> Option + { + let src = sema.find_file(src.syntax().clone()).with_value(src); + sema.sb.borrow_mut().to_id(sema.db, src.cloned()).map(Into::into) + } + } + )*} +} + +to_def_impls![ + (crate::Module, ast::Module), + (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::TypeParam, ast::TypeParam), + (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros +]; + +impl ToDef for ast::BindPat { + type Def = Local; + + fn to_def(sema: &Semantics, src: &Self) -> Option { + let src = sema.find_file(src.syntax().clone()).with_value(src); + let file_id = src.file_id; + let mut sb = sema.sb.borrow_mut(); + let db = sema.db; + let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| { + let res = match_ast! { + match it { + ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, + ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, + ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, + _ => return None, + } + }; + Some(res) + })?; + let (_body, source_map) = db.body_with_source_map(parent); + let src = src.cloned().map(ast::Pat::from); + let pat_id = source_map.node_pat(src.as_ref())?; + Some(Local { parent: parent.into(), pat_id }) + } +} + fn find_root(node: &SyntaxNode) -> SyntaxNode { node.ancestors().last().unwrap() } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0b8a641f9..4353e25ac 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -6,7 +6,7 @@ use hir_def::{ dyn_map::DynMap, keys::{self, Key}, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, - StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, + StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, }; use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; use ra_db::FileId; @@ -17,9 +17,9 @@ use ra_syntax::{ }; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, Module, TypeParam}; +use crate::{db::HirDatabase, Module}; -pub struct SourceBinder { +pub(crate) struct SourceBinder { child_by_source_cache: FxHashMap, } @@ -38,7 +38,11 @@ impl SourceBinder { Some(Module { id: ModuleId { krate, local_id } }) } - fn to_id(&mut self, db: &impl HirDatabase, src: InFile) -> Option { + pub(crate) fn to_id( + &mut self, + db: &impl HirDatabase, + src: InFile, + ) -> Option { T::to_id(db, self, src) } @@ -118,42 +122,6 @@ pub(crate) trait ToId: Sized { ) -> Option; } -pub trait ToDef: Sized + AstNode + 'static { - type Def; - fn to_def( - db: &DB, - sb: &mut SourceBinder, - src: InFile, - ) -> Option; -} - -macro_rules! to_def_impls { - ($(($def:path, $ast:path)),* ,) => {$( - impl ToDef for $ast { - type Def = $def; - fn to_def(db: &DB, sb: &mut SourceBinder, src: InFile) - -> Option - { sb.to_id(db, src).map(Into::into) } - } - )*} -} - -to_def_impls![ - (crate::Module, ast::Module), - (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)] pub(crate) enum ChildContainer { DefWithBodyId(DefWithBodyId), @@ -245,37 +213,14 @@ impl ToId for ast::MacroCall { } } -impl ToDef for ast::BindPat { - type Def = Local; +impl ToId for ast::TypeParam { + type ID = TypeParamId; - fn to_def(db: &DB, sb: &mut SourceBinder, 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(db, InFile { value, file_id})?.into() }, - ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() }, - _ => return None, - } - }; - Some(res) - })?; - let (_body, source_map) = 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( + fn to_id( db: &DB, sb: &mut SourceBinder, - src: InFile, - ) -> Option { + src: InFile, + ) -> Option { let file_id = src.file_id; let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { @@ -291,8 +236,7 @@ impl ToDef for ast::TypeParam { }; Some(res) })?; - let &id = sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src)?; - Some(TypeParam { id }) + sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src).copied() } } -- cgit v1.2.3