aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r--crates/ra_hir/src/expr.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 6866fc2ac..c87d76735 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -5,7 +5,7 @@ use rustc_hash::FxHashMap;
5 5
6use ra_arena::{Arena, RawId, impl_arena_id}; 6use ra_arena::{Arena, RawId, impl_arena_id};
7use ra_db::{LocalSyntaxPtr, Cancelable}; 7use ra_db::{LocalSyntaxPtr, Cancelable};
8use ra_syntax::ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner}; 8use ra_syntax::{SyntaxNodeRef, ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner}};
9 9
10use crate::{Path, type_ref::{Mutability, TypeRef}, Name, HirDatabase, DefId, Def, name::AsName}; 10use crate::{Path, type_ref::{Mutability, TypeRef}, Name, HirDatabase, DefId, Def, name::AsName};
11 11
@@ -77,12 +77,22 @@ impl BodySyntaxMapping {
77 pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option<ExprId> { 77 pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option<ExprId> {
78 self.expr_syntax_mapping.get(&ptr).cloned() 78 self.expr_syntax_mapping.get(&ptr).cloned()
79 } 79 }
80 pub fn node_expr(&self, node: SyntaxNodeRef) -> Option<ExprId> {
81 self.expr_syntax_mapping
82 .get(&LocalSyntaxPtr::new(node))
83 .cloned()
84 }
80 pub fn pat_syntax(&self, pat: PatId) -> Option<LocalSyntaxPtr> { 85 pub fn pat_syntax(&self, pat: PatId) -> Option<LocalSyntaxPtr> {
81 self.pat_syntax_mapping_back.get(&pat).cloned() 86 self.pat_syntax_mapping_back.get(&pat).cloned()
82 } 87 }
83 pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> { 88 pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> {
84 self.pat_syntax_mapping.get(&ptr).cloned() 89 self.pat_syntax_mapping.get(&ptr).cloned()
85 } 90 }
91 pub fn node_pat(&self, node: SyntaxNodeRef) -> Option<PatId> {
92 self.pat_syntax_mapping
93 .get(&LocalSyntaxPtr::new(node))
94 .cloned()
95 }
86 96
87 pub fn body(&self) -> &Arc<Body> { 97 pub fn body(&self) -> &Arc<Body> {
88 &self.body 98 &self.body
@@ -159,6 +169,11 @@ pub enum Expr {
159 expr: ExprId, 169 expr: ExprId,
160 op: Option<UnaryOp>, 170 op: Option<UnaryOp>,
161 }, 171 },
172 BinaryOp {
173 lhs: ExprId,
174 rhs: ExprId,
175 op: Option<BinaryOp>,
176 },
162 Lambda { 177 Lambda {
163 args: Vec<PatId>, 178 args: Vec<PatId>,
164 arg_types: Vec<Option<TypeRef>>, 179 arg_types: Vec<Option<TypeRef>>,
@@ -166,7 +181,8 @@ pub enum Expr {
166 }, 181 },
167} 182}
168 183
169pub type UnaryOp = ast::PrefixOp; 184pub use ra_syntax::ast::PrefixOp as UnaryOp;
185pub use ra_syntax::ast::BinOp as BinaryOp;
170 186
171#[derive(Debug, Clone, Eq, PartialEq)] 187#[derive(Debug, Clone, Eq, PartialEq)]
172pub struct MatchArm { 188pub struct MatchArm {
@@ -266,6 +282,10 @@ impl Expr {
266 Expr::Lambda { body, .. } => { 282 Expr::Lambda { body, .. } => {
267 f(*body); 283 f(*body);
268 } 284 }
285 Expr::BinaryOp { lhs, rhs, .. } => {
286 f(*lhs);
287 f(*rhs);
288 }
269 Expr::Field { expr, .. } 289 Expr::Field { expr, .. }
270 | Expr::Try { expr } 290 | Expr::Try { expr }
271 | Expr::Cast { expr, .. } 291 | Expr::Cast { expr, .. }
@@ -586,6 +606,12 @@ impl ExprCollector {
586 syntax_ptr, 606 syntax_ptr,
587 ) 607 )
588 } 608 }
609 ast::Expr::BinExpr(e) => {
610 let lhs = self.collect_expr_opt(e.lhs());
611 let rhs = self.collect_expr_opt(e.rhs());
612 let op = e.op();
613 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
614 }
589 615
590 // TODO implement HIR for these: 616 // TODO implement HIR for these:
591 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 617 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
@@ -593,7 +619,6 @@ impl ExprCollector {
593 ast::Expr::TupleExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 619 ast::Expr::TupleExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
594 ast::Expr::ArrayExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 620 ast::Expr::ArrayExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
595 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 621 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
596 ast::Expr::BinExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
597 ast::Expr::Literal(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 622 ast::Expr::Literal(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
598 } 623 }
599 } 624 }