Files
rust/crates/ra_hir/src/code_model_impl/function.rs

92 lines
2.9 KiB
Rust
Raw Normal View History

2019-01-08 20:11:13 +03:00
mod scope;
use std::sync::Arc;
2019-01-24 15:28:50 +03:00
use ra_syntax::ast::{self, NameOwner};
2019-01-08 20:11:13 +03:00
use crate::{
2019-01-24 15:28:50 +03:00
HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId,
2019-01-08 20:11:13 +03:00
type_ref::{TypeRef, Mutability},
expr::Body,
impl_block::ImplBlock,
2019-01-24 15:28:50 +03:00
ids::FunctionLoc,
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 {
2019-01-24 15:28:50 +03:00
pub(crate) fn from_ast(
db: &impl HirDatabase,
module: Module,
file_id: HirFileId,
ast: &ast::FnDef,
) -> Function {
let loc: FunctionLoc = FunctionLoc::from_ast(db, module, file_id, ast);
let id = loc.id(db);
Function { id }
2019-01-08 20:11:13 +03:00
}
2019-01-15 19:01:59 +03:00
pub(crate) fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
2019-01-24 15:28:50 +03:00
db.body_hir(*self)
2019-01-08 20:11:13 +03:00
}
2019-01-15 19:18:52 +03:00
pub(crate) fn module(&self, db: &impl HirDatabase) -> Module {
2019-01-24 15:28:50 +03:00
self.id.loc(db).module
2019-01-08 20:11:13 +03:00
}
/// 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-24 15:28:50 +03:00
let module_impls = db.impls_in_module(self.module(db));
ImplBlock::containing(module_impls, (*self).into())
2019-01-08 20:11:13 +03:00
}
}
impl FnSignature {
2019-01-24 15:28:50 +03:00
pub(crate) fn fn_signature_query(db: &impl HirDatabase, func: Function) -> Arc<FnSignature> {
let (_, node) = func.source(db);
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()
};
let sig = FnSignature {
name,
2019-01-12 21:58:16 +01:00
params,
ret_type,
2019-01-12 21:58:16 +01:00
has_self_param,
};
2019-01-08 20:11:13 +03:00
Arc::new(sig)
}
}