aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr.rs
diff options
context:
space:
mode:
authorLenard Pratt <[email protected]>2019-03-30 11:17:31 +0000
committerLenard Pratt <[email protected]>2019-04-02 19:21:36 +0100
commit88e22e9d70ac3a35989ad1d45386f86697877c4c (patch)
tree274537eb5913369a7f226aca534fbf3821395181 /crates/ra_hir/src/expr.rs
parent7f3bf7cc738d02fde80d4fde9f32cbbe72896b87 (diff)
Added const bodies and static body to the ast
and added inference the inference test reduce code duplication
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r--crates/ra_hir/src/expr.rs36
1 files changed, 16 insertions, 20 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 280746761..bc4c63d3c 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
12use crate::{ 12use crate::{
13 Path, Name, HirDatabase, Function, Resolver,DefWithBody, 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,8 +27,7 @@ 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)]
29pub struct Body { 29pub 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
32 owner: DefWithBody, 31 owner: DefWithBody,
33 exprs: Arena<ExprId, Expr>, 32 exprs: Arena<ExprId, Expr>,
34 pats: Arena<PatId, Pat>, 33 pats: Arena<PatId, Pat>,
@@ -503,9 +502,6 @@ impl ExprCollector {
503 self.exprs.alloc(block) 502 self.exprs.alloc(block)
504 } 503 }
505 504
506
507
508
509 fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { 505 fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
510 let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); 506 let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
511 match expr.kind() { 507 match expr.kind() {
@@ -874,13 +870,14 @@ impl ExprCollector {
874 } 870 }
875 } 871 }
876 872
877 873 fn collect_const_body(&mut self, node: &ast::ConstDef) {
878 fn collect_const_body(&mut self,node:&ast::ConstDef) { 874 let body = self.collect_expr_opt(node.body());
879 875 self.body_expr = Some(body);
880 } 876 }
881 877
882 fn collect_static_body(&mut self,node:&ast::StaticDef) { 878 fn collect_static_body(&mut self, node: &ast::StaticDef) {
883 879 let body = self.collect_expr_opt(node.body());
880 self.body_expr = Some(body);
884 } 881 }
885 882
886 fn collect_fn_body(&mut self, node: &ast::FnDef) { 883 fn collect_fn_body(&mut self, node: &ast::FnDef) {
@@ -931,28 +928,27 @@ pub(crate) fn body_with_source_map_query(
931 db: &impl HirDatabase, 928 db: &impl HirDatabase,
932 def: DefWithBody, 929 def: DefWithBody,
933) -> (Arc<Body>, Arc<BodySourceMap>) { 930) -> (Arc<Body>, Arc<BodySourceMap>) {
934
935 let mut collector = ExprCollector::new(def); 931 let mut collector = ExprCollector::new(def);
936 932
937 // FIXME: do can this be turned into a method
938
939 match def { 933 match def {
940 DefWithBody::Const(ref c) => collector.collect_const_body(&def.const_source(db).1), 934 DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1),
941 DefWithBody::Func(ref f) => collector.collect_fn_body(&def.func_source(db).1), 935 DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1),
942 DefWithBody::Static(ref s) => collector.collect_static_body(&def.static_source(db).1) 936 DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1),
943 } 937 }
944 938
945 let (body, source_map) = collector.finish(); 939 let (body, source_map) = collector.finish();
946 (Arc::new(body), Arc::new(source_map)) 940 (Arc::new(body), Arc::new(source_map))
947} 941}
948 942
949pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> { 943pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
950 db.body_with_source_map(def).0 944 db.body_with_source_map(def).0
951} 945}
952 946
953#[cfg(test)] 947#[cfg(test)]
948use crate::{Function};
949#[cfg(test)]
954fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) { 950fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) {
955 let mut collector = ExprCollector::new(function); 951 let mut collector = ExprCollector::new(DefWithBody::Function(function));
956 collector.collect_fn_body(node); 952 collector.collect_fn_body(node);
957 collector.finish() 953 collector.finish()
958} 954}