Merge #467
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:
commit
1c25bf05d7
@ -5,10 +5,12 @@
|
|||||||
use ra_syntax::{ast, TreePtr, SyntaxNode};
|
use ra_syntax::{ast, TreePtr, SyntaxNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, DefId, Path, PerNs,
|
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
nameres::ModuleScope,
|
nameres::ModuleScope,
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
|
expr::BodySyntaxMapping,
|
||||||
|
ty::InferenceResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// hir::Crate describes a single crate. It's the main inteface with which
|
/// 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<Option<Module>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum Def {
|
||||||
|
Module(Module),
|
||||||
|
Struct(Struct),
|
||||||
|
Enum(Enum),
|
||||||
|
Function(Function),
|
||||||
|
Item,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
pub(crate) def_id: DefId,
|
pub(crate) def_id: DefId,
|
||||||
@ -207,3 +217,56 @@ pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<Varia
|
|||||||
Ok(db.enum_data(self.def_id)?.variants.clone())
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
mod krate; // `crate` is invalid ident :(
|
mod krate; // `crate` is invalid ident :(
|
||||||
mod module;
|
mod module;
|
||||||
|
pub(crate) mod function;
|
||||||
|
82
crates/ra_hir/src/code_model_impl/function.rs
Normal file
82
crates/ra_hir/src/code_model_impl/function.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -106,7 +106,7 @@ fn body_syntax_mapping(def_id: DefId) -> Cancelable<Arc<crate::expr::BodySyntaxM
|
|||||||
|
|
||||||
fn fn_signature(def_id: DefId) -> Arc<FnSignature> {
|
fn fn_signature(def_id: DefId) -> Arc<FnSignature> {
|
||||||
type FnSignatureQuery;
|
type FnSignatureQuery;
|
||||||
use fn crate::function::fn_signature;
|
use fn crate::FnSignature::fn_signature_query;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -758,10 +758,7 @@ pub(crate) fn body_syntax_mapping(
|
|||||||
let def = def_id.resolve(db)?;
|
let def = def_id.resolve(db)?;
|
||||||
|
|
||||||
let body_syntax_mapping = match def {
|
let body_syntax_mapping = match def {
|
||||||
Def::Function(f) => {
|
Def::Function(f) => collect_fn_body_syntax(&f.source(db)),
|
||||||
let node = f.syntax(db);
|
|
||||||
collect_fn_body_syntax(&node)
|
|
||||||
}
|
|
||||||
// TODO: consts, etc.
|
// TODO: consts, etc.
|
||||||
_ => panic!("Trying to get body for item type without body"),
|
_ => panic!("Trying to get body for item type without body"),
|
||||||
};
|
};
|
||||||
|
@ -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)
|
|
||||||
}
|
|
@ -26,7 +26,6 @@ macro_rules! ctry {
|
|||||||
mod name;
|
mod name;
|
||||||
mod module_tree;
|
mod module_tree;
|
||||||
mod nameres;
|
mod nameres;
|
||||||
mod function;
|
|
||||||
mod adt;
|
mod adt;
|
||||||
mod type_ref;
|
mod type_ref;
|
||||||
mod ty;
|
mod ty;
|
||||||
@ -48,21 +47,15 @@ macro_rules! ctry {
|
|||||||
ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc},
|
ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc},
|
||||||
macros::{MacroDef, MacroInput, MacroExpansion},
|
macros::{MacroDef, MacroInput, MacroExpansion},
|
||||||
nameres::{ItemMap, PerNs, Namespace, Resolution},
|
nameres::{ItemMap, PerNs, Namespace, Resolution},
|
||||||
function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping},
|
|
||||||
ty::Ty,
|
ty::Ty,
|
||||||
impl_block::{ImplBlock, ImplItem},
|
impl_block::{ImplBlock, ImplItem},
|
||||||
|
code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::code_model_api::{
|
pub use self::code_model_api::{
|
||||||
Crate, CrateDependency,
|
Crate, CrateDependency,
|
||||||
|
Def,
|
||||||
Module, ModuleSource, Problem,
|
Module, ModuleSource, Problem,
|
||||||
Struct, Enum, VariantData, StructField,
|
Struct, Enum, VariantData, StructField,
|
||||||
|
Function, FnSignature,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub enum Def {
|
|
||||||
Module(Module),
|
|
||||||
Function(Function),
|
|
||||||
Struct(Struct),
|
|
||||||
Enum(Enum),
|
|
||||||
Item,
|
|
||||||
}
|
|
||||||
|
@ -12,9 +12,8 @@
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource,
|
SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource,
|
||||||
MacroCallLoc,
|
MacroCallLoc, FnScopes,
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
function::FnScopes,
|
|
||||||
module_tree::ModuleId,
|
module_tree::ModuleId,
|
||||||
nameres::{InputModuleItems, ItemMap, Resolver},
|
nameres::{InputModuleItems, ItemMap, Resolver},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user