From 78f3b0627cf4a5d34aaf63c7b5a2e1b744a11b14 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 17:10:51 +0300 Subject: Move FunctionData to hir_def --- crates/ra_hir/src/code_model.rs | 87 +++-------------------- crates/ra_hir/src/db.rs | 13 ++-- crates/ra_hir/src/lib.rs | 6 +- crates/ra_hir/src/ty/infer.rs | 13 ++-- crates/ra_hir/src/ty/lower.rs | 6 +- crates/ra_hir/src/ty/method_resolution.rs | 6 +- crates/ra_hir_def/src/db.rs | 7 +- crates/ra_hir_def/src/function.rs | 61 ++++++++++++++++ crates/ra_hir_def/src/lib.rs | 1 + crates/ra_ide_api/src/call_info.rs | 4 +- crates/ra_ide_api/src/change.rs | 2 +- crates/ra_ide_api/src/completion/complete_dot.rs | 3 +- crates/ra_ide_api/src/completion/complete_path.rs | 6 +- crates/ra_ide_api/src/completion/presentation.rs | 15 ++-- 14 files changed, 114 insertions(+), 116 deletions(-) create mode 100644 crates/ra_hir_def/src/function.rs (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 72c9b466f..f426f8c9f 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -13,7 +13,7 @@ use hir_def::{ nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, traits::TraitData, - type_ref::{Mutability, TypeRef}, + type_ref::TypeRef, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, ModuleId, UnionId, }; @@ -561,77 +561,6 @@ pub struct Function { pub(crate) id: FunctionId, } -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FnData { - pub(crate) name: Name, - pub(crate) params: Vec, - pub(crate) ret_type: TypeRef, - /// True if the first param is `self`. This is relevant to decide whether this - /// can be called as a method. - pub(crate) has_self_param: bool, -} - -impl FnData { - pub(crate) fn fn_data_query( - db: &(impl DefDatabase + AstDatabase), - func: Function, - ) -> Arc { - let src = func.source(db); - let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); - let mut params = Vec::new(); - let mut has_self_param = false; - if let Some(param_list) = src.value.param_list() { - if let Some(self_param) = param_list.self_param() { - let self_type = if let Some(type_ref) = self_param.ascribed_type() { - TypeRef::from_ast(type_ref) - } else { - let self_type = TypeRef::Path(name::SELF_TYPE.into()); - match self_param.kind() { - ast::SelfParamKind::Owned => self_type, - ast::SelfParamKind::Ref => { - TypeRef::Reference(Box::new(self_type), Mutability::Shared) - } - ast::SelfParamKind::MutRef => { - TypeRef::Reference(Box::new(self_type), Mutability::Mut) - } - } - }; - params.push(self_type); - has_self_param = true; - } - for param in param_list.params() { - let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); - params.push(type_ref); - } - } - let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { - TypeRef::from_ast(type_ref) - } else { - TypeRef::unit() - }; - - let sig = FnData { name, params, ret_type, has_self_param }; - Arc::new(sig) - } - pub fn name(&self) -> &Name { - &self.name - } - - pub fn params(&self) -> &[TypeRef] { - &self.params - } - - pub fn ret_type(&self) -> &TypeRef { - &self.ret_type - } - - /// True if the first arg is `self`. This is relevant to decide whether this - /// can be called as a method. - pub fn has_self_param(&self) -> bool { - self.has_self_param - } -} - impl Function { pub fn module(self, db: &impl DefDatabase) -> Module { self.id.lookup(db).module(db).into() @@ -642,7 +571,15 @@ impl Function { } pub fn name(self, db: &impl HirDatabase) -> Name { - self.data(db).name.clone() + db.function_data(self.id).name.clone() + } + + pub fn has_self_param(self, db: &impl HirDatabase) -> bool { + db.function_data(self.id).has_self_param + } + + pub fn params(self, db: &impl HirDatabase) -> Vec { + db.function_data(self.id).params.clone() } pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc { @@ -657,10 +594,6 @@ impl Function { db.type_for_def(self.into(), Namespace::Values) } - pub fn data(self, db: &impl HirDatabase) -> Arc { - db.fn_data(self) - } - pub fn infer(self, db: &impl HirDatabase) -> Arc { db.infer(self.into()) } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 1cfcb2fd2..8b9af0565 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -16,15 +16,15 @@ use crate::{ CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, }, - Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static, - StructField, Trait, + Const, ConstData, Crate, DefWithBody, GenericDef, ImplBlock, Module, Static, StructField, + Trait, }; pub use hir_def::db::{ BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, - EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, - InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, - TraitDataQuery, TypeAliasDataQuery, + EnumDataQuery, ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, + InternDatabase, InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, + StructDataQuery, TraitDataQuery, TypeAliasDataQuery, }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, @@ -35,9 +35,6 @@ pub use hir_expand::db::{ #[salsa::query_group(DefDatabaseStorage)] #[salsa::requires(AstDatabase)] pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { - #[salsa::invoke(FnData::fn_data_query)] - fn fn_data(&self, func: Function) -> Arc; - #[salsa::invoke(ConstData::const_data_query)] fn const_data(&self, konst: Const) -> Arc; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 8535629ca..d29cc9258 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -55,9 +55,9 @@ pub use crate::{ docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource}, Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, - EnumVariant, FieldSource, FnData, Function, GenericDef, GenericParam, HasBody, ImplBlock, - Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, - Trait, TypeAlias, Union, VariantDef, + EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, + MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, + TypeAlias, Union, VariantDef, }, expr::ExprScopes, from_source::FromSource, diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 69b13baef..41a51283d 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -22,6 +22,7 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use rustc_hash::FxHashMap; use hir_def::{ + function::FunctionData, path::known, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, @@ -43,8 +44,8 @@ use crate::{ db::HirDatabase, expr::{BindingAnnotation, Body, ExprId, PatId}, ty::infer::diagnostics::InferenceDiagnostic, - Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, - StructField, Trait, VariantDef, + Adt, AssocItem, ConstData, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, + Trait, VariantDef, }; macro_rules! ty_app { @@ -70,7 +71,7 @@ pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc ctx.collect_const(&c.data(db)), - DefWithBody::Function(ref f) => ctx.collect_fn(&f.data(db)), + DefWithBody::Function(ref f) => ctx.collect_fn(&db.function_data(f.id)), DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), } @@ -562,14 +563,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.return_ty = self.make_ty(data.type_ref()); } - fn collect_fn(&mut self, data: &FnData) { + fn collect_fn(&mut self, data: &FunctionData) { let body = Arc::clone(&self.body); // avoid borrow checker problem - for (type_ref, pat) in data.params().iter().zip(body.params()) { + for (type_ref, pat) in data.params.iter().zip(body.params()) { let ty = self.make_ty(type_ref); self.infer_pat(*pat, &ty, BindingMode::default()); } - self.return_ty = self.make_ty(data.ret_type()); + self.return_ty = self.make_ty(&data.ret_type); } fn infer_body(&mut self) { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 75c552569..42daa9cb9 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -622,10 +622,10 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> } fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { - let data = def.data(db); + let data = db.function_data(def.id); let resolver = def.id.resolver(db); - let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); - let ret = Ty::from_hir(db, &resolver, data.ret_type()); + let params = data.params.iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); + let ret = Ty::from_hir(db, &resolver, &data.ret_type); FnSig::from_params_and_return(params, ret) } diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 64adb814d..f84aae26e 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -291,9 +291,9 @@ fn is_valid_candidate( ) -> bool { match item { AssocItem::Function(m) => { - let data = m.data(db); - name.map_or(true, |name| data.name() == name) - && (data.has_self_param() || mode == LookupMode::Path) + let data = db.function_data(m.id); + name.map_or(true, |name| data.name == *name) + && (data.has_self_param || mode == LookupMode::Path) } AssocItem::Const(c) => { name.map_or(true, |name| Some(name) == c.name(db).as_ref()) diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 5bbdaa4b2..02e649cc7 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -8,6 +8,7 @@ use ra_syntax::ast; use crate::{ adt::{EnumData, StructData}, body::{scope::ExprScopes, Body, BodySourceMap}, + function::FunctionData, generics::GenericParams, impls::ImplData, nameres::{ @@ -16,7 +17,8 @@ use crate::{ }, traits::TraitData, type_alias::TypeAliasData, - DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, TypeAliasId, + DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, + TypeAliasId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -68,6 +70,9 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { #[salsa::invoke(TypeAliasData::type_alias_data_query)] fn type_alias_data(&self, e: TypeAliasId) -> Arc; + #[salsa::invoke(FunctionData::fn_data_query)] + fn function_data(&self, func: FunctionId) -> Arc; + #[salsa::invoke(Body::body_with_source_map_query)] fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc, Arc); diff --git a/crates/ra_hir_def/src/function.rs b/crates/ra_hir_def/src/function.rs new file mode 100644 index 000000000..33265275e --- /dev/null +++ b/crates/ra_hir_def/src/function.rs @@ -0,0 +1,61 @@ +use std::sync::Arc; + +use hir_expand::name::{self, AsName, Name}; +use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; + +use crate::{ + db::DefDatabase2, + type_ref::{Mutability, TypeRef}, + FunctionId, HasSource, Lookup, +}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FunctionData { + pub name: Name, + pub params: Vec, + pub ret_type: TypeRef, + /// True if the first param is `self`. This is relevant to decide whether this + /// can be called as a method. + pub has_self_param: bool, +} + +impl FunctionData { + pub(crate) fn fn_data_query(db: &impl DefDatabase2, func: FunctionId) -> Arc { + let src = func.lookup(db).source(db); + let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); + let mut params = Vec::new(); + let mut has_self_param = false; + if let Some(param_list) = src.value.param_list() { + if let Some(self_param) = param_list.self_param() { + let self_type = if let Some(type_ref) = self_param.ascribed_type() { + TypeRef::from_ast(type_ref) + } else { + let self_type = TypeRef::Path(name::SELF_TYPE.into()); + match self_param.kind() { + ast::SelfParamKind::Owned => self_type, + ast::SelfParamKind::Ref => { + TypeRef::Reference(Box::new(self_type), Mutability::Shared) + } + ast::SelfParamKind::MutRef => { + TypeRef::Reference(Box::new(self_type), Mutability::Mut) + } + } + }; + params.push(self_type); + has_self_param = true; + } + for param in param_list.params() { + let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); + params.push(type_ref); + } + } + let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { + TypeRef::from_ast(type_ref) + } else { + TypeRef::unit() + }; + + let sig = FunctionData { name, params, ret_type, has_self_param }; + Arc::new(sig) + } +} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 268144462..14ccad043 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -21,6 +21,7 @@ pub mod generics; pub mod traits; pub mod resolver; pub mod type_alias; +pub mod function; #[cfg(test)] mod test_db; diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 41ee81511..7c367230e 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -30,7 +30,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { - (CallInfo::with_fn(db, it), it.data(db).has_self_param()) + (CallInfo::with_fn(db, it), it.has_self_param(db)) } hir::CallableDef::Struct(it) => (CallInfo::with_struct(db, it)?, false), hir::CallableDef::EnumVariant(it) => (CallInfo::with_enum_variant(db, it)?, false), @@ -38,7 +38,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { let function = analyzer.resolve_method_call(&expr)?; - (CallInfo::with_fn(db, function), function.data(db).has_self_param()) + (CallInfo::with_fn(db, function), function.has_self_param(db)) } FnCallNode::MacroCallExpr(expr) => { let macro_def = analyzer.resolve_macro_call(db, &expr)?; diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs index 3c607d5b5..8a05b287f 100644 --- a/crates/ra_ide_api/src/change.rs +++ b/crates/ra_ide_api/src/change.rs @@ -313,7 +313,7 @@ impl RootDatabase { hir::db::RawItemsQuery hir::db::CrateDefMapQuery hir::db::GenericParamsQuery - hir::db::FnDataQuery + hir::db::FunctionDataQuery hir::db::TypeAliasDataQuery hir::db::ConstDataQuery hir::db::StaticDataQuery diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 4e2c497e1..5a3f9b5f6 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -59,8 +59,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { let mut seen_methods = FxHashSet::default(); ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| { - let data = func.data(ctx.db); - if data.has_self_param() && seen_methods.insert(data.name().clone()) { + if func.has_self_param(ctx.db) && seen_methods.insert(func.name(ctx.db)) { acc.add_function(ctx, func); } None::<()> diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 5d974cf6d..802c7701a 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -53,8 +53,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { ctx.analyzer.iterate_path_candidates(ctx.db, ty.clone(), None, |_ty, item| { match item { hir::AssocItem::Function(func) => { - let data = func.data(ctx.db); - if !data.has_self_param() { + if !func.has_self_param(ctx.db) { acc.add_function(ctx, func); } } @@ -80,8 +79,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { for item in t.items(ctx.db) { match item { hir::AssocItem::Function(func) => { - let data = func.data(ctx.db); - if !data.has_self_param() { + if !func.has_self_param(ctx.db) { acc.add_function(ctx, func); } } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index bd464d193..50fdb0043 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -199,14 +199,17 @@ impl Completions { name: Option, func: hir::Function, ) { - let data = func.data(ctx.db); - let name = name.unwrap_or_else(|| data.name().to_string()); + let func_name = func.name(ctx.db); + let has_self_param = func.has_self_param(ctx.db); + let params = func.params(ctx.db); + + let name = name.unwrap_or_else(|| func_name.to_string()); let ast_node = func.source(ctx.db).value; let detail = function_label(&ast_node); let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) - .kind(if data.has_self_param() { + .kind(if has_self_param { CompletionItemKind::Method } else { CompletionItemKind::Function @@ -222,10 +225,10 @@ impl Completions { { tested_by!(inserts_parens_for_function_calls); let (snippet, label) = - if data.params().is_empty() || data.has_self_param() && data.params().len() == 1 { - (format!("{}()$0", data.name()), format!("{}()", name)) + if params.is_empty() || has_self_param && params.len() == 1 { + (format!("{}()$0", func_name), format!("{}()", name)) } else { - (format!("{}($0)", data.name()), format!("{}(…)", name)) + (format!("{}($0)", func_name), format!("{}(…)", name)) }; builder = builder.lookup_by(name).label(label).insert_snippet(snippet); } -- cgit v1.2.3