From ac92973a6c5934377c6eca9906f3b7f17e220d4e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 20:11:13 +0300 Subject: [PATCH] move function to code_model_api --- crates/ra_hir/src/code_model_api.rs | 65 ++++++++- crates/ra_hir/src/code_model_impl.rs | 1 + crates/ra_hir/src/code_model_impl/function.rs | 82 ++++++++++++ .../{ => code_model_impl}/function/scope.rs | 0 crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/expr.rs | 5 +- crates/ra_hir/src/function.rs | 126 ------------------ crates/ra_hir/src/lib.rs | 13 +- crates/ra_hir/src/query_definitions.rs | 3 +- 9 files changed, 153 insertions(+), 144 deletions(-) create mode 100644 crates/ra_hir/src/code_model_impl/function.rs rename crates/ra_hir/src/{ => code_model_impl}/function/scope.rs (100%) delete mode 100644 crates/ra_hir/src/function.rs diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index f06f1ae6634..902032e1411 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -5,10 +5,12 @@ use ra_syntax::{ast, TreePtr, SyntaxNode}; use crate::{ - Name, DefId, Path, PerNs, + Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, type_ref::TypeRef, nameres::ModuleScope, db::HirDatabase, + expr::BodySyntaxMapping, + ty::InferenceResult, }; /// hir::Crate describes a single crate. It's the main inteface with which @@ -37,6 +39,14 @@ pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable> { } } +pub enum Def { + Module(Module), + Struct(Struct), + Enum(Enum), + Function(Function), + Item, +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Module { pub(crate) def_id: DefId, @@ -207,3 +217,56 @@ pub fn variants(&self, db: &impl HirDatabase) -> Cancelable, + pub(crate) ret_type: TypeRef, +} + +impl FnSignature { + pub fn args(&self) -> &[TypeRef] { + &self.args + } + + pub fn ret_type(&self) -> &TypeRef { + &self.ret_type + } +} + +impl Function { + pub fn def_id(&self) -> DefId { + self.def_id + } + + pub fn source(&self, db: &impl HirDatabase) -> TreePtr { + self.source_impl(db) + } + + pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable> { + db.body_syntax_mapping(self.def_id) + } + + pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable { + let scopes = db.fn_scopes(self.def_id)?; + let syntax_mapping = db.body_syntax_mapping(self.def_id)?; + Ok(ScopesWithSyntaxMapping { + scopes, + syntax_mapping, + }) + } + + pub fn signature(&self, db: &impl HirDatabase) -> Arc { + db.fn_signature(self.def_id) + } + + pub fn infer(&self, db: &impl HirDatabase) -> Cancelable> { + db.infer(self.def_id) + } +} diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 157b0c61688..1f28fab7482 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs @@ -1,2 +1,3 @@ mod krate; // `crate` is invalid ident :( mod module; +pub(crate) mod function; diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs new file mode 100644 index 00000000000..13c57ed2198 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl/function.rs @@ -0,0 +1,82 @@ +mod scope; + +use std::sync::Arc; + +use ra_db::Cancelable; +use ra_syntax::{ + TreePtr, + ast::{self, AstNode}, +}; + +use crate::{ + DefId, DefKind, HirDatabase, Name, Function, FnSignature, Module, + type_ref::{TypeRef, Mutability}, + expr::Body, + impl_block::ImplBlock, +}; + +pub use self::scope::{FnScopes, ScopesWithSyntaxMapping}; + +impl Function { + pub(crate) fn new(def_id: DefId) -> Function { + Function { def_id } + } + + pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> TreePtr { + let def_loc = self.def_id.loc(db); + assert!(def_loc.kind == DefKind::Function); + let syntax = db.file_item(def_loc.source_item_id); + ast::FnDef::cast(&syntax).unwrap().to_owned() + } + + pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable> { + db.body_hir(self.def_id) + } + + pub(crate) fn module(&self, db: &impl HirDatabase) -> Cancelable { + self.def_id.module(db) + } + + /// The containing impl block, if this is a method. + pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Cancelable> { + self.def_id.impl_block(db) + } +} + +impl FnSignature { + pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc { + let func = Function::new(def_id); + let node = func.source(db); + let mut args = Vec::new(); + if let Some(param_list) = node.param_list() { + if let Some(self_param) = param_list.self_param() { + let self_type = if let Some(type_ref) = self_param.type_ref() { + TypeRef::from_ast(type_ref) + } else { + let self_type = TypeRef::Path(Name::self_type().into()); + match self_param.flavor() { + ast::SelfParamFlavor::Owned => self_type, + ast::SelfParamFlavor::Ref => { + TypeRef::Reference(Box::new(self_type), Mutability::Shared) + } + ast::SelfParamFlavor::MutRef => { + TypeRef::Reference(Box::new(self_type), Mutability::Mut) + } + } + }; + args.push(self_type); + } + for param in param_list.params() { + let type_ref = TypeRef::from_ast_opt(param.type_ref()); + args.push(type_ref); + } + } + let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { + TypeRef::from_ast(type_ref) + } else { + TypeRef::unit() + }; + let sig = FnSignature { args, ret_type }; + Arc::new(sig) + } +} diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/code_model_impl/function/scope.rs similarity index 100% rename from crates/ra_hir/src/function/scope.rs rename to crates/ra_hir/src/code_model_impl/function/scope.rs diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index bb4fb3d6637..07cf0d10a98 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -106,7 +106,7 @@ fn body_syntax_mapping(def_id: DefId) -> Cancelable Arc { type FnSignatureQuery; - use fn crate::function::fn_signature; + use fn crate::FnSignature::fn_signature_query; } } diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 4c54449ef13..a31f086f7fb 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -758,10 +758,7 @@ pub(crate) fn body_syntax_mapping( let def = def_id.resolve(db)?; let body_syntax_mapping = match def { - Def::Function(f) => { - let node = f.syntax(db); - collect_fn_body_syntax(&node) - } + Def::Function(f) => collect_fn_body_syntax(&f.source(db)), // TODO: consts, etc. _ => panic!("Trying to get body for item type without body"), }; diff --git a/crates/ra_hir/src/function.rs b/crates/ra_hir/src/function.rs deleted file mode 100644 index 2cfc4caa42f..00000000000 --- a/crates/ra_hir/src/function.rs +++ /dev/null @@ -1,126 +0,0 @@ -mod scope; - -use std::sync::Arc; - -use ra_db::Cancelable; -use ra_syntax::{ - TreePtr, - ast::{self, AstNode}, -}; - -use crate::{DefId, DefKind, HirDatabase, ty::InferenceResult, Module, Crate, impl_block::ImplBlock, expr::{Body, BodySyntaxMapping}, type_ref::{TypeRef, Mutability}, Name}; - -pub use self::scope::{FnScopes, ScopesWithSyntaxMapping}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Function { - def_id: DefId, -} - -impl Function { - pub(crate) fn new(def_id: DefId) -> Function { - Function { def_id } - } - - pub fn def_id(&self) -> DefId { - self.def_id - } - - pub fn syntax(&self, db: &impl HirDatabase) -> TreePtr { - let def_loc = self.def_id.loc(db); - assert!(def_loc.kind == DefKind::Function); - let syntax = db.file_item(def_loc.source_item_id); - ast::FnDef::cast(&syntax).unwrap().to_owned() - } - - pub fn body(&self, db: &impl HirDatabase) -> Cancelable> { - db.body_hir(self.def_id) - } - - pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable> { - db.body_syntax_mapping(self.def_id) - } - - pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable { - let scopes = db.fn_scopes(self.def_id)?; - let syntax_mapping = db.body_syntax_mapping(self.def_id)?; - Ok(ScopesWithSyntaxMapping { - scopes, - syntax_mapping, - }) - } - - pub fn signature(&self, db: &impl HirDatabase) -> Arc { - db.fn_signature(self.def_id) - } - - pub fn infer(&self, db: &impl HirDatabase) -> Cancelable> { - db.infer(self.def_id) - } - - pub fn module(&self, db: &impl HirDatabase) -> Cancelable { - self.def_id.module(db) - } - - pub fn krate(&self, db: &impl HirDatabase) -> Cancelable> { - self.def_id.krate(db) - } - - /// The containing impl block, if this is a method. - pub fn impl_block(&self, db: &impl HirDatabase) -> Cancelable> { - self.def_id.impl_block(db) - } -} - -/// The declared signature of a function. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FnSignature { - args: Vec, - ret_type: TypeRef, -} - -impl FnSignature { - pub fn args(&self) -> &[TypeRef] { - &self.args - } - - pub fn ret_type(&self) -> &TypeRef { - &self.ret_type - } -} - -pub(crate) fn fn_signature(db: &impl HirDatabase, def_id: DefId) -> Arc { - let func = Function::new(def_id); - let node = func.syntax(db); - let mut args = Vec::new(); - if let Some(param_list) = node.param_list() { - if let Some(self_param) = param_list.self_param() { - let self_type = if let Some(type_ref) = self_param.type_ref() { - TypeRef::from_ast(type_ref) - } else { - let self_type = TypeRef::Path(Name::self_type().into()); - match self_param.flavor() { - ast::SelfParamFlavor::Owned => self_type, - ast::SelfParamFlavor::Ref => { - TypeRef::Reference(Box::new(self_type), Mutability::Shared) - } - ast::SelfParamFlavor::MutRef => { - TypeRef::Reference(Box::new(self_type), Mutability::Mut) - } - } - }; - args.push(self_type); - } - for param in param_list.params() { - let type_ref = TypeRef::from_ast_opt(param.type_ref()); - args.push(type_ref); - } - } - let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { - TypeRef::from_ast(type_ref) - } else { - TypeRef::unit() - }; - let sig = FnSignature { args, ret_type }; - Arc::new(sig) -} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 197d8c4fd61..eb19d8be159 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -26,7 +26,6 @@ macro_rules! ctry { mod name; mod module_tree; mod nameres; -mod function; mod adt; mod type_ref; mod ty; @@ -48,21 +47,15 @@ macro_rules! ctry { ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc}, macros::{MacroDef, MacroInput, MacroExpansion}, nameres::{ItemMap, PerNs, Namespace, Resolution}, - function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping}, ty::Ty, impl_block::{ImplBlock, ImplItem}, + code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping}, }; pub use self::code_model_api::{ Crate, CrateDependency, + Def, Module, ModuleSource, Problem, Struct, Enum, VariantData, StructField, + Function, FnSignature, }; - -pub enum Def { - Module(Module), - Function(Function), - Struct(Struct), - Enum(Enum), - Item, -} diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index ab4e6e629f1..32be23d8c35 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -12,9 +12,8 @@ use crate::{ SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource, - MacroCallLoc, + MacroCallLoc, FnScopes, db::HirDatabase, - function::FnScopes, module_tree::ModuleId, nameres::{InputModuleItems, ItemMap, Resolver}, };