diff options
author | Aleksey Kladov <[email protected]> | 2019-11-22 14:10:51 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-22 14:18:04 +0000 |
commit | 78f3b0627cf4a5d34aaf63c7b5a2e1b744a11b14 (patch) | |
tree | 0c0e07fefce4b4175e8162bcd586aaca17512bb8 /crates/ra_hir_def/src/function.rs | |
parent | a132cc715ed05cab83fd058156eb152f0be96982 (diff) |
Move FunctionData to hir_def
Diffstat (limited to 'crates/ra_hir_def/src/function.rs')
-rw-r--r-- | crates/ra_hir_def/src/function.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/function.rs b/crates/ra_hir_def/src/function.rs new file mode 100644 index 000000000..33265275e --- /dev/null +++ b/crates/ra_hir_def/src/function.rs | |||
@@ -0,0 +1,61 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use hir_expand::name::{self, AsName, Name}; | ||
4 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | ||
5 | |||
6 | use crate::{ | ||
7 | db::DefDatabase2, | ||
8 | type_ref::{Mutability, TypeRef}, | ||
9 | FunctionId, HasSource, Lookup, | ||
10 | }; | ||
11 | |||
12 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
13 | pub struct FunctionData { | ||
14 | pub name: Name, | ||
15 | pub params: Vec<TypeRef>, | ||
16 | pub ret_type: TypeRef, | ||
17 | /// True if the first param is `self`. This is relevant to decide whether this | ||
18 | /// can be called as a method. | ||
19 | pub has_self_param: bool, | ||
20 | } | ||
21 | |||
22 | impl FunctionData { | ||
23 | pub(crate) fn fn_data_query(db: &impl DefDatabase2, func: FunctionId) -> Arc<FunctionData> { | ||
24 | let src = func.lookup(db).source(db); | ||
25 | let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
26 | let mut params = Vec::new(); | ||
27 | let mut has_self_param = false; | ||
28 | if let Some(param_list) = src.value.param_list() { | ||
29 | if let Some(self_param) = param_list.self_param() { | ||
30 | let self_type = if let Some(type_ref) = self_param.ascribed_type() { | ||
31 | TypeRef::from_ast(type_ref) | ||
32 | } else { | ||
33 | let self_type = TypeRef::Path(name::SELF_TYPE.into()); | ||
34 | match self_param.kind() { | ||
35 | ast::SelfParamKind::Owned => self_type, | ||
36 | ast::SelfParamKind::Ref => { | ||
37 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
38 | } | ||
39 | ast::SelfParamKind::MutRef => { | ||
40 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
41 | } | ||
42 | } | ||
43 | }; | ||
44 | params.push(self_type); | ||
45 | has_self_param = true; | ||
46 | } | ||
47 | for param in param_list.params() { | ||
48 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
49 | params.push(type_ref); | ||
50 | } | ||
51 | } | ||
52 | let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { | ||
53 | TypeRef::from_ast(type_ref) | ||
54 | } else { | ||
55 | TypeRef::unit() | ||
56 | }; | ||
57 | |||
58 | let sig = FunctionData { name, params, ret_type, has_self_param }; | ||
59 | Arc::new(sig) | ||
60 | } | ||
61 | } | ||