aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 17:35:08 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 17:35:08 +0000
commit1c25bf05d714680c048d250a5d39e8a4c25f0c31 (patch)
tree9879fc268f2812576839118cf7e4c88df180a30b /crates/ra_hir/src
parent3b166aee3c116762c817f1acd0f5e01e48452932 (diff)
parentac92973a6c5934377c6eca9906f3b7f17e220d4e (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')
-rw-r--r--crates/ra_hir/src/code_model_api.rs65
-rw-r--r--crates/ra_hir/src/code_model_impl.rs1
-rw-r--r--crates/ra_hir/src/code_model_impl/function.rs82
-rw-r--r--crates/ra_hir/src/code_model_impl/function/scope.rs (renamed from crates/ra_hir/src/function/scope.rs)0
-rw-r--r--crates/ra_hir/src/db.rs2
-rw-r--r--crates/ra_hir/src/expr.rs5
-rw-r--r--crates/ra_hir/src/function.rs126
-rw-r--r--crates/ra_hir/src/lib.rs13
-rw-r--r--crates/ra_hir/src/query_definitions.rs3
9 files changed, 153 insertions, 144 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index f06f1ae66..902032e14 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -5,10 +5,12 @@ use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreePtr, SyntaxNode}; 5use ra_syntax::{ast, TreePtr, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping,
9 type_ref::TypeRef, 9 type_ref::TypeRef,
10 nameres::ModuleScope, 10 nameres::ModuleScope,
11 db::HirDatabase, 11 db::HirDatabase,
12 expr::BodySyntaxMapping,
13 ty::InferenceResult,
12}; 14};
13 15
14/// hir::Crate describes a single crate. It's the main inteface with which 16/// hir::Crate describes a single crate. It's the main inteface with which
@@ -37,6 +39,14 @@ impl Crate {
37 } 39 }
38} 40}
39 41
42pub enum Def {
43 Module(Module),
44 Struct(Struct),
45 Enum(Enum),
46 Function(Function),
47 Item,
48}
49
40#[derive(Debug, Clone, PartialEq, Eq, Hash)] 50#[derive(Debug, Clone, PartialEq, Eq, Hash)]
41pub struct Module { 51pub struct Module {
42 pub(crate) def_id: DefId, 52 pub(crate) def_id: DefId,
@@ -207,3 +217,56 @@ impl Enum {
207 Ok(db.enum_data(self.def_id)?.variants.clone()) 217 Ok(db.enum_data(self.def_id)?.variants.clone())
208 } 218 }
209} 219}
220
221#[derive(Debug, Clone, PartialEq, Eq, Hash)]
222pub struct Function {
223 pub(crate) def_id: DefId,
224}
225
226/// The declared signature of a function.
227#[derive(Debug, Clone, PartialEq, Eq)]
228pub struct FnSignature {
229 pub(crate) args: Vec<TypeRef>,
230 pub(crate) ret_type: TypeRef,
231}
232
233impl FnSignature {
234 pub fn args(&self) -> &[TypeRef] {
235 &self.args
236 }
237
238 pub fn ret_type(&self) -> &TypeRef {
239 &self.ret_type
240 }
241}
242
243impl Function {
244 pub fn def_id(&self) -> DefId {
245 self.def_id
246 }
247
248 pub fn source(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> {
249 self.source_impl(db)
250 }
251
252 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
253 db.body_syntax_mapping(self.def_id)
254 }
255
256 pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable<ScopesWithSyntaxMapping> {
257 let scopes = db.fn_scopes(self.def_id)?;
258 let syntax_mapping = db.body_syntax_mapping(self.def_id)?;
259 Ok(ScopesWithSyntaxMapping {
260 scopes,
261 syntax_mapping,
262 })
263 }
264
265 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
266 db.fn_signature(self.def_id)
267 }
268
269 pub fn infer(&self, db: &impl HirDatabase) -> Cancelable<Arc<InferenceResult>> {
270 db.infer(self.def_id)
271 }
272}
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs
index 157b0c616..1f28fab74 100644
--- a/crates/ra_hir/src/code_model_impl.rs
+++ b/crates/ra_hir/src/code_model_impl.rs
@@ -1,2 +1,3 @@
1mod krate; // `crate` is invalid ident :( 1mod krate; // `crate` is invalid ident :(
2mod module; 2mod module;
3pub(crate) mod function;
diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs
new file mode 100644
index 000000000..13c57ed21
--- /dev/null
+++ b/crates/ra_hir/src/code_model_impl/function.rs
@@ -0,0 +1,82 @@
1mod scope;
2
3use std::sync::Arc;
4
5use ra_db::Cancelable;
6use ra_syntax::{
7 TreePtr,
8 ast::{self, AstNode},
9};
10
11use crate::{
12 DefId, DefKind, HirDatabase, Name, Function, FnSignature, Module,
13 type_ref::{TypeRef, Mutability},
14 expr::Body,
15 impl_block::ImplBlock,
16};
17
18pub use self::scope::{FnScopes, ScopesWithSyntaxMapping};
19
20impl Function {
21 pub(crate) fn new(def_id: DefId) -> Function {
22 Function { def_id }
23 }
24
25 pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> TreePtr<ast::FnDef> {
26 let def_loc = self.def_id.loc(db);
27 assert!(def_loc.kind == DefKind::Function);
28 let syntax = db.file_item(def_loc.source_item_id);
29 ast::FnDef::cast(&syntax).unwrap().to_owned()
30 }
31
32 pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
33 db.body_hir(self.def_id)
34 }
35
36 pub(crate) fn module(&self, db: &impl HirDatabase) -> Cancelable<Module> {
37 self.def_id.module(db)
38 }
39
40 /// The containing impl block, if this is a method.
41 pub(crate) fn impl_block(&self, db: &impl HirDatabase) -> Cancelable<Option<ImplBlock>> {
42 self.def_id.impl_block(db)
43 }
44}
45
46impl FnSignature {
47 pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
48 let func = Function::new(def_id);
49 let node = func.source(db);
50 let mut args = Vec::new();
51 if let Some(param_list) = node.param_list() {
52 if let Some(self_param) = param_list.self_param() {
53 let self_type = if let Some(type_ref) = self_param.type_ref() {
54 TypeRef::from_ast(type_ref)
55 } else {
56 let self_type = TypeRef::Path(Name::self_type().into());
57 match self_param.flavor() {
58 ast::SelfParamFlavor::Owned => self_type,
59 ast::SelfParamFlavor::Ref => {
60 TypeRef::Reference(Box::new(self_type), Mutability::Shared)
61 }
62 ast::SelfParamFlavor::MutRef => {
63 TypeRef::Reference(Box::new(self_type), Mutability::Mut)
64 }
65 }
66 };
67 args.push(self_type);
68 }
69 for param in param_list.params() {
70 let type_ref = TypeRef::from_ast_opt(param.type_ref());
71 args.push(type_ref);
72 }
73 }
74 let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) {
75 TypeRef::from_ast(type_ref)
76 } else {
77 TypeRef::unit()
78 };
79 let sig = FnSignature { args, ret_type };
80 Arc::new(sig)
81 }
82}
diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/code_model_impl/function/scope.rs
index 699784f71..699784f71 100644
--- a/crates/ra_hir/src/function/scope.rs
+++ b/crates/ra_hir/src/code_model_impl/function/scope.rs
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index bb4fb3d66..07cf0d10a 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -106,7 +106,7 @@ pub trait HirDatabase: SyntaxDatabase
106 106
107 fn fn_signature(def_id: DefId) -> Arc<FnSignature> { 107 fn fn_signature(def_id: DefId) -> Arc<FnSignature> {
108 type FnSignatureQuery; 108 type FnSignatureQuery;
109 use fn crate::function::fn_signature; 109 use fn crate::FnSignature::fn_signature_query;
110 } 110 }
111} 111}
112 112
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 4c54449ef..a31f086f7 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -758,10 +758,7 @@ pub(crate) fn body_syntax_mapping(
758 let def = def_id.resolve(db)?; 758 let def = def_id.resolve(db)?;
759 759
760 let body_syntax_mapping = match def { 760 let body_syntax_mapping = match def {
761 Def::Function(f) => { 761 Def::Function(f) => collect_fn_body_syntax(&f.source(db)),
762 let node = f.syntax(db);
763 collect_fn_body_syntax(&node)
764 }
765 // TODO: consts, etc. 762 // TODO: consts, etc.
766 _ => panic!("Trying to get body for item type without body"), 763 _ => panic!("Trying to get body for item type without body"),
767 }; 764 };
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 @@
1mod scope;
2
3use std::sync::Arc;
4
5use ra_db::Cancelable;
6use ra_syntax::{
7 TreePtr,
8 ast::{self, AstNode},
9};
10
11use crate::{DefId, DefKind, HirDatabase, ty::InferenceResult, Module, Crate, impl_block::ImplBlock, expr::{Body, BodySyntaxMapping}, type_ref::{TypeRef, Mutability}, Name};
12
13pub use self::scope::{FnScopes, ScopesWithSyntaxMapping};
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct Function {
17 def_id: DefId,
18}
19
20impl 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)]
77pub struct FnSignature {
78 args: Vec<TypeRef>,
79 ret_type: TypeRef,
80}
81
82impl 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
92pub(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}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 197d8c4fd..eb19d8be1 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -26,7 +26,6 @@ mod macros;
26mod name; 26mod name;
27mod module_tree; 27mod module_tree;
28mod nameres; 28mod nameres;
29mod function;
30mod adt; 29mod adt;
31mod type_ref; 30mod type_ref;
32mod ty; 31mod ty;
@@ -48,21 +47,15 @@ pub use self::{
48 ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc}, 47 ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc},
49 macros::{MacroDef, MacroInput, MacroExpansion}, 48 macros::{MacroDef, MacroInput, MacroExpansion},
50 nameres::{ItemMap, PerNs, Namespace, Resolution}, 49 nameres::{ItemMap, PerNs, Namespace, Resolution},
51 function::{Function, FnSignature, FnScopes, ScopesWithSyntaxMapping},
52 ty::Ty, 50 ty::Ty,
53 impl_block::{ImplBlock, ImplItem}, 51 impl_block::{ImplBlock, ImplItem},
52 code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping},
54}; 53};
55 54
56pub use self::code_model_api::{ 55pub use self::code_model_api::{
57 Crate, CrateDependency, 56 Crate, CrateDependency,
57 Def,
58 Module, ModuleSource, Problem, 58 Module, ModuleSource, Problem,
59 Struct, Enum, VariantData, StructField, 59 Struct, Enum, VariantData, StructField,
60 Function, FnSignature,
60}; 61};
61
62pub enum Def {
63 Module(Module),
64 Function(Function),
65 Struct(Struct),
66 Enum(Enum),
67 Item,
68}
diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs
index ab4e6e629..32be23d8c 100644
--- a/crates/ra_hir/src/query_definitions.rs
+++ b/crates/ra_hir/src/query_definitions.rs
@@ -12,9 +12,8 @@ use ra_db::{SourceRootId, Cancelable,};
12 12
13use crate::{ 13use crate::{
14 SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource, 14 SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource,
15 MacroCallLoc, 15 MacroCallLoc, FnScopes,
16 db::HirDatabase, 16 db::HirDatabase,
17 function::FnScopes,
18 module_tree::ModuleId, 17 module_tree::ModuleId,
19 nameres::{InputModuleItems, ItemMap, Resolver}, 18 nameres::{InputModuleItems, ItemMap, Resolver},
20}; 19};