diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-08 17:35:08 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-08 17:35:08 +0000 |
commit | 1c25bf05d714680c048d250a5d39e8a4c25f0c31 (patch) | |
tree | 9879fc268f2812576839118cf7e4c88df180a30b /crates/ra_hir/src/function.rs | |
parent | 3b166aee3c116762c817f1acd0f5e01e48452932 (diff) | |
parent | ac92973a6c5934377c6eca9906f3b7f17e220d4e (diff) |
Merge #467
467: move function to code_model_api r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/function.rs')
-rw-r--r-- | crates/ra_hir/src/function.rs | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/crates/ra_hir/src/function.rs b/crates/ra_hir/src/function.rs deleted file mode 100644 index 2cfc4caa4..000000000 --- a/crates/ra_hir/src/function.rs +++ /dev/null | |||
@@ -1,126 +0,0 @@ | |||
1 | mod scope; | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use ra_db::Cancelable; | ||
6 | use ra_syntax::{ | ||
7 | TreePtr, | ||
8 | ast::{self, AstNode}, | ||
9 | }; | ||
10 | |||
11 | use crate::{DefId, DefKind, HirDatabase, ty::InferenceResult, Module, Crate, impl_block::ImplBlock, expr::{Body, BodySyntaxMapping}, type_ref::{TypeRef, Mutability}, Name}; | ||
12 | |||
13 | pub use self::scope::{FnScopes, ScopesWithSyntaxMapping}; | ||
14 | |||
15 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
16 | pub struct Function { | ||
17 | def_id: DefId, | ||
18 | } | ||
19 | |||
20 | impl Function { | ||
21 | pub(crate) fn new(def_id: DefId) -> Function { | ||
22 | Function { def_id } | ||
23 | } | ||
24 | |||
25 | pub fn def_id(&self) -> DefId { | ||
26 | self.def_id | ||
27 | } | ||
28 | |||
29 | pub fn syntax(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> { | ||
30 | let def_loc = self.def_id.loc(db); | ||
31 | assert!(def_loc.kind == DefKind::Function); | ||
32 | let syntax = db.file_item(def_loc.source_item_id); | ||
33 | ast::FnDef::cast(&syntax).unwrap().to_owned() | ||
34 | } | ||
35 | |||
36 | pub fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> { | ||
37 | db.body_hir(self.def_id) | ||
38 | } | ||
39 | |||
40 | pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { | ||
41 | db.body_syntax_mapping(self.def_id) | ||
42 | } | ||
43 | |||
44 | pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable<ScopesWithSyntaxMapping> { | ||
45 | let scopes = db.fn_scopes(self.def_id)?; | ||
46 | let syntax_mapping = db.body_syntax_mapping(self.def_id)?; | ||
47 | Ok(ScopesWithSyntaxMapping { | ||
48 | scopes, | ||
49 | syntax_mapping, | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { | ||
54 | db.fn_signature(self.def_id) | ||
55 | } | ||
56 | |||
57 | pub fn infer(&self, db: &impl HirDatabase) -> Cancelable<Arc<InferenceResult>> { | ||
58 | db.infer(self.def_id) | ||
59 | } | ||
60 | |||
61 | pub fn module(&self, db: &impl HirDatabase) -> Cancelable<Module> { | ||
62 | self.def_id.module(db) | ||
63 | } | ||
64 | |||
65 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | ||
66 | self.def_id.krate(db) | ||
67 | } | ||
68 | |||
69 | /// The containing impl block, if this is a method. | ||
70 | pub fn impl_block(&self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> { | ||
71 | self.def_id.impl_block(db) | ||
72 | } | ||
73 | } | ||
74 | |||
75 | /// The declared signature of a function. | ||
76 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
77 | pub struct FnSignature { | ||
78 | args: Vec<TypeRef>, | ||
79 | ret_type: TypeRef, | ||
80 | } | ||
81 | |||
82 | impl FnSignature { | ||
83 | pub fn args(&self) -> &[TypeRef] { | ||
84 | &self.args | ||
85 | } | ||
86 | |||
87 | pub fn ret_type(&self) -> &TypeRef { | ||
88 | &self.ret_type | ||
89 | } | ||
90 | } | ||
91 | |||
92 | pub(crate) fn fn_signature(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> { | ||
93 | let func = Function::new(def_id); | ||
94 | let node = func.syntax(db); | ||
95 | let mut args = Vec::new(); | ||
96 | if let Some(param_list) = node.param_list() { | ||
97 | if let Some(self_param) = param_list.self_param() { | ||
98 | let self_type = if let Some(type_ref) = self_param.type_ref() { | ||
99 | TypeRef::from_ast(type_ref) | ||
100 | } else { | ||
101 | let self_type = TypeRef::Path(Name::self_type().into()); | ||
102 | match self_param.flavor() { | ||
103 | ast::SelfParamFlavor::Owned => self_type, | ||
104 | ast::SelfParamFlavor::Ref => { | ||
105 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
106 | } | ||
107 | ast::SelfParamFlavor::MutRef => { | ||
108 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
109 | } | ||
110 | } | ||
111 | }; | ||
112 | args.push(self_type); | ||
113 | } | ||
114 | for param in param_list.params() { | ||
115 | let type_ref = TypeRef::from_ast_opt(param.type_ref()); | ||
116 | args.push(type_ref); | ||
117 | } | ||
118 | } | ||
119 | let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { | ||
120 | TypeRef::from_ast(type_ref) | ||
121 | } else { | ||
122 | TypeRef::unit() | ||
123 | }; | ||
124 | let sig = FnSignature { args, ret_type }; | ||
125 | Arc::new(sig) | ||
126 | } | ||