diff options
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 45012827f..b2a237ece 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -10,7 +10,7 @@ use ra_syntax::{ | |||
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Path, Name, HirDatabase, Function, Resolver, | 13 | Path, Name, HirDatabase, Resolver,DefWithBody, |
14 | name::AsName, | 14 | name::AsName, |
15 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeRef}, |
16 | }; | 16 | }; |
@@ -27,9 +27,8 @@ impl_arena_id!(ExprId); | |||
27 | /// The body of an item (function, const etc.). | 27 | /// The body of an item (function, const etc.). |
28 | #[derive(Debug, Eq, PartialEq)] | 28 | #[derive(Debug, Eq, PartialEq)] |
29 | pub struct Body { | 29 | pub struct Body { |
30 | // FIXME: this should be more general, consts & statics also have bodies | 30 | /// The def of the item this body belongs to |
31 | /// The Function of the item this body belongs to | 31 | owner: DefWithBody, |
32 | owner: Function, | ||
33 | exprs: Arena<ExprId, Expr>, | 32 | exprs: Arena<ExprId, Expr>, |
34 | pats: Arena<PatId, Pat>, | 33 | pats: Arena<PatId, Pat>, |
35 | /// The patterns for the function's parameters. While the parameter types are | 34 | /// The patterns for the function's parameters. While the parameter types are |
@@ -66,7 +65,7 @@ impl Body { | |||
66 | self.body_expr | 65 | self.body_expr |
67 | } | 66 | } |
68 | 67 | ||
69 | pub fn owner(&self) -> Function { | 68 | pub fn owner(&self) -> DefWithBody { |
70 | self.owner | 69 | self.owner |
71 | } | 70 | } |
72 | 71 | ||
@@ -463,8 +462,8 @@ impl Pat { | |||
463 | 462 | ||
464 | // Queries | 463 | // Queries |
465 | 464 | ||
466 | struct ExprCollector { | 465 | pub(crate) struct ExprCollector { |
467 | owner: Function, | 466 | owner: DefWithBody, |
468 | exprs: Arena<ExprId, Expr>, | 467 | exprs: Arena<ExprId, Expr>, |
469 | pats: Arena<PatId, Pat>, | 468 | pats: Arena<PatId, Pat>, |
470 | source_map: BodySourceMap, | 469 | source_map: BodySourceMap, |
@@ -473,7 +472,7 @@ struct ExprCollector { | |||
473 | } | 472 | } |
474 | 473 | ||
475 | impl ExprCollector { | 474 | impl ExprCollector { |
476 | fn new(owner: Function) -> Self { | 475 | fn new(owner: DefWithBody) -> Self { |
477 | ExprCollector { | 476 | ExprCollector { |
478 | owner, | 477 | owner, |
479 | exprs: Arena::default(), | 478 | exprs: Arena::default(), |
@@ -866,6 +865,16 @@ impl ExprCollector { | |||
866 | } | 865 | } |
867 | } | 866 | } |
868 | 867 | ||
868 | fn collect_const_body(&mut self, node: &ast::ConstDef) { | ||
869 | let body = self.collect_expr_opt(node.body()); | ||
870 | self.body_expr = Some(body); | ||
871 | } | ||
872 | |||
873 | fn collect_static_body(&mut self, node: &ast::StaticDef) { | ||
874 | let body = self.collect_expr_opt(node.body()); | ||
875 | self.body_expr = Some(body); | ||
876 | } | ||
877 | |||
869 | fn collect_fn_body(&mut self, node: &ast::FnDef) { | 878 | fn collect_fn_body(&mut self, node: &ast::FnDef) { |
870 | if let Some(param_list) = node.param_list() { | 879 | if let Some(param_list) = node.param_list() { |
871 | if let Some(self_param) = param_list.self_param() { | 880 | if let Some(self_param) = param_list.self_param() { |
@@ -910,24 +919,20 @@ impl ExprCollector { | |||
910 | 919 | ||
911 | pub(crate) fn body_with_source_map_query( | 920 | pub(crate) fn body_with_source_map_query( |
912 | db: &impl HirDatabase, | 921 | db: &impl HirDatabase, |
913 | func: Function, | 922 | def: DefWithBody, |
914 | ) -> (Arc<Body>, Arc<BodySourceMap>) { | 923 | ) -> (Arc<Body>, Arc<BodySourceMap>) { |
915 | let mut collector = ExprCollector::new(func); | 924 | let mut collector = ExprCollector::new(def); |
916 | 925 | ||
917 | // FIXME: consts, etc. | 926 | match def { |
918 | collector.collect_fn_body(&func.source(db).1); | 927 | DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1), |
928 | DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1), | ||
929 | DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1), | ||
930 | } | ||
919 | 931 | ||
920 | let (body, source_map) = collector.finish(); | 932 | let (body, source_map) = collector.finish(); |
921 | (Arc::new(body), Arc::new(source_map)) | 933 | (Arc::new(body), Arc::new(source_map)) |
922 | } | 934 | } |
923 | 935 | ||
924 | pub(crate) fn body_hir_query(db: &impl HirDatabase, func: Function) -> Arc<Body> { | 936 | pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { |
925 | db.body_with_source_map(func).0 | 937 | db.body_with_source_map(def).0 |
926 | } | ||
927 | |||
928 | #[cfg(test)] | ||
929 | fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { | ||
930 | let mut collector = ExprCollector::new(function); | ||
931 | collector.collect_fn_body(node); | ||
932 | collector.finish() | ||
933 | } | 938 | } |