467: move function to code_model_api r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-08 17:35:08 +00:00
commit 1c25bf05d7
9 changed files with 153 additions and 144 deletions

View file

@ -5,10 +5,12 @@ use ra_db::{CrateId, Cancelable, FileId};
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 @@ impl Crate {
}
}
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 @@ impl Enum {
Ok(db.enum_data(self.def_id)?.variants.clone())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Function {
pub(crate) def_id: DefId,
}
/// The declared signature of a function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnSignature {
pub(crate) args: Vec<TypeRef>,
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<ast::FnDef> {
self.source_impl(db)
}
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
db.body_syntax_mapping(self.def_id)
}
pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable<ScopesWithSyntaxMapping> {
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<FnSignature> {
db.fn_signature(self.def_id)
}
pub fn infer(&self, db: &impl HirDatabase) -> Cancelable<Arc<InferenceResult>> {
db.infer(self.def_id)
}
}

View file

@ -1,2 +1,3 @@
mod krate; // `crate` is invalid ident :(
mod module;
pub(crate) mod function;

View file

@ -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<ast::FnDef> {
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<Arc<Body>> {
db.body_hir(self.def_id)
}
pub(crate) fn module(&self, db: &impl HirDatabase) -> Cancelable<Module> {
self.def_id.module(db)
}
/// The containing impl block, if this is a method.
pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> {
self.def_id.impl_block(db)
}
}
impl FnSignature {
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
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)
}
}

View file

@ -106,7 +106,7 @@ pub trait HirDatabase: SyntaxDatabase
fn fn_signature(def_id: DefId) -> Arc<FnSignature> {
type FnSignatureQuery;
use fn crate::function::fn_signature;
use fn crate::FnSignature::fn_signature_query;
}
}

View file

@ -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"),
};

View file

@ -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<ast::FnDef> {
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<Arc<Body>> {
db.body_hir(self.def_id)
}
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
db.body_syntax_mapping(self.def_id)
}
pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable<ScopesWithSyntaxMapping> {
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<FnSignature> {
db.fn_signature(self.def_id)
}
pub fn infer(&self, db: &impl HirDatabase) -> Cancelable<Arc<InferenceResult>> {
db.infer(self.def_id)
}
pub fn module(&self, db: &impl HirDatabase) -> Cancelable<Module> {
self.def_id.module(db)
}
pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
self.def_id.krate(db)
}
/// The containing impl block, if this is a method.
pub fn impl_block(&self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> {
self.def_id.impl_block(db)
}
}
/// The declared signature of a function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnSignature {
args: Vec<TypeRef>,
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<FnSignature> {
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)
}

View file

@ -26,7 +26,6 @@ mod macros;
mod name;
mod module_tree;
mod nameres;
mod function;
mod adt;
mod type_ref;
mod ty;
@ -48,21 +47,15 @@ pub use self::{
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,
}

View file

@ -12,9 +12,8 @@ use ra_db::{SourceRootId, Cancelable,};
use crate::{
SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource,
MacroCallLoc,
MacroCallLoc, FnScopes,
db::HirDatabase,
function::FnScopes,
module_tree::ModuleId,
nameres::{InputModuleItems, ItemMap, Resolver},
};