2019-01-08 20:11:13 +03:00
|
|
|
mod scope;
|
|
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2019-01-11 20:43:10 +03:00
|
|
|
use ra_syntax::{TreeArc, ast::{self, NameOwner}};
|
2019-01-08 20:11:13 +03:00
|
|
|
|
|
|
|
|
use crate::{
|
2019-01-11 20:43:10 +03:00
|
|
|
DefId, HirDatabase, Name, AsName, Function, FnSignature, Module,
|
2019-01-08 20:11:13 +03:00
|
|
|
type_ref::{TypeRef, Mutability},
|
|
|
|
|
expr::Body,
|
|
|
|
|
impl_block::ImplBlock,
|
2019-01-11 20:43:10 +03:00
|
|
|
code_model_impl::def_id_to_ast,
|
2019-01-08 20:11:13 +03:00
|
|
|
};
|
|
|
|
|
|
2019-01-11 14:00:54 +03:00
|
|
|
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
|
2019-01-08 20:11:13 +03:00
|
|
|
|
|
|
|
|
impl Function {
|
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Function {
|
|
|
|
|
Function { def_id }
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 19:01:59 +03:00
|
|
|
pub(crate) fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
|
2019-01-08 20:11:13 +03:00
|
|
|
db.body_hir(self.def_id)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 19:18:52 +03:00
|
|
|
pub(crate) fn module(&self, db: &impl HirDatabase) -> Module {
|
2019-01-08 20:11:13 +03:00
|
|
|
self.def_id.module(db)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The containing impl block, if this is a method.
|
2019-01-15 19:18:52 +03:00
|
|
|
pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Option<ImplBlock> {
|
2019-01-08 20:11:13 +03:00
|
|
|
self.def_id.impl_block(db)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FnSignature {
|
|
|
|
|
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
|
2019-01-11 20:43:10 +03:00
|
|
|
// FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky
|
|
|
|
|
let node: TreeArc<ast::FnDef> = def_id_to_ast(db, def_id).1;
|
2019-01-08 00:30:49 +01:00
|
|
|
let name = node
|
|
|
|
|
.name()
|
|
|
|
|
.map(|n| n.as_name())
|
|
|
|
|
.unwrap_or_else(Name::missing);
|
2019-01-12 21:58:16 +01:00
|
|
|
let mut params = Vec::new();
|
|
|
|
|
let mut has_self_param = false;
|
2019-01-08 20:11:13 +03:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-01-12 21:58:16 +01:00
|
|
|
params.push(self_type);
|
|
|
|
|
has_self_param = true;
|
2019-01-08 20:11:13 +03:00
|
|
|
}
|
|
|
|
|
for param in param_list.params() {
|
|
|
|
|
let type_ref = TypeRef::from_ast_opt(param.type_ref());
|
2019-01-12 21:58:16 +01:00
|
|
|
params.push(type_ref);
|
2019-01-08 20:11:13 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
};
|
2019-01-08 00:30:49 +01:00
|
|
|
let sig = FnSignature {
|
|
|
|
|
name,
|
2019-01-12 21:58:16 +01:00
|
|
|
params,
|
2019-01-08 00:30:49 +01:00
|
|
|
ret_type,
|
2019-01-12 21:58:16 +01:00
|
|
|
has_self_param,
|
2019-01-08 00:30:49 +01:00
|
|
|
};
|
2019-01-08 20:11:13 +03:00
|
|
|
Arc::new(sig)
|
|
|
|
|
}
|
|
|
|
|
}
|