diff options
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 6866fc2ac..b0063cad2 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -3,7 +3,7 @@ use std::sync::Arc; | |||
3 | 3 | ||
4 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
5 | 5 | ||
6 | use ra_arena::{Arena, RawId, impl_arena_id}; | 6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
7 | use ra_db::{LocalSyntaxPtr, Cancelable}; | 7 | use ra_db::{LocalSyntaxPtr, Cancelable}; |
8 | use ra_syntax::ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner}; | 8 | use ra_syntax::ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner}; |
9 | 9 | ||
@@ -39,9 +39,9 @@ pub struct Body { | |||
39 | pub struct BodySyntaxMapping { | 39 | pub struct BodySyntaxMapping { |
40 | body: Arc<Body>, | 40 | body: Arc<Body>, |
41 | expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, | 41 | expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, |
42 | expr_syntax_mapping_back: FxHashMap<ExprId, LocalSyntaxPtr>, | 42 | expr_syntax_mapping_back: ArenaMap<ExprId, LocalSyntaxPtr>, |
43 | pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, | 43 | pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, |
44 | pat_syntax_mapping_back: FxHashMap<PatId, LocalSyntaxPtr>, | 44 | pat_syntax_mapping_back: ArenaMap<PatId, LocalSyntaxPtr>, |
45 | } | 45 | } |
46 | 46 | ||
47 | impl Body { | 47 | impl Body { |
@@ -72,17 +72,27 @@ impl Index<PatId> for Body { | |||
72 | 72 | ||
73 | impl BodySyntaxMapping { | 73 | impl BodySyntaxMapping { |
74 | pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> { | 74 | pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> { |
75 | self.expr_syntax_mapping_back.get(&expr).cloned() | 75 | self.expr_syntax_mapping_back.get(expr).cloned() |
76 | } | 76 | } |
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: ast::Expr) -> Option<ExprId> { | ||
81 | self.expr_syntax_mapping | ||
82 | .get(&LocalSyntaxPtr::new(node.syntax())) | ||
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: ast::Pat) -> Option<PatId> { | ||
92 | self.pat_syntax_mapping | ||
93 | .get(&LocalSyntaxPtr::new(node.syntax())) | ||
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 | ||
169 | pub type UnaryOp = ast::PrefixOp; | 184 | pub use ra_syntax::ast::PrefixOp as UnaryOp; |
185 | pub use ra_syntax::ast::BinOp as BinaryOp; | ||
170 | 186 | ||
171 | #[derive(Debug, Clone, Eq, PartialEq)] | 187 | #[derive(Debug, Clone, Eq, PartialEq)] |
172 | pub struct MatchArm { | 188 | pub 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, .. } |
@@ -314,9 +334,9 @@ struct ExprCollector { | |||
314 | exprs: Arena<ExprId, Expr>, | 334 | exprs: Arena<ExprId, Expr>, |
315 | pats: Arena<PatId, Pat>, | 335 | pats: Arena<PatId, Pat>, |
316 | expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, | 336 | expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, |
317 | expr_syntax_mapping_back: FxHashMap<ExprId, LocalSyntaxPtr>, | 337 | expr_syntax_mapping_back: ArenaMap<ExprId, LocalSyntaxPtr>, |
318 | pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, | 338 | pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, |
319 | pat_syntax_mapping_back: FxHashMap<PatId, LocalSyntaxPtr>, | 339 | pat_syntax_mapping_back: ArenaMap<PatId, LocalSyntaxPtr>, |
320 | } | 340 | } |
321 | 341 | ||
322 | impl ExprCollector { | 342 | impl ExprCollector { |
@@ -325,9 +345,9 @@ impl ExprCollector { | |||
325 | exprs: Arena::default(), | 345 | exprs: Arena::default(), |
326 | pats: Arena::default(), | 346 | pats: Arena::default(), |
327 | expr_syntax_mapping: FxHashMap::default(), | 347 | expr_syntax_mapping: FxHashMap::default(), |
328 | expr_syntax_mapping_back: FxHashMap::default(), | 348 | expr_syntax_mapping_back: ArenaMap::default(), |
329 | pat_syntax_mapping: FxHashMap::default(), | 349 | pat_syntax_mapping: FxHashMap::default(), |
330 | pat_syntax_mapping_back: FxHashMap::default(), | 350 | pat_syntax_mapping_back: ArenaMap::default(), |
331 | } | 351 | } |
332 | } | 352 | } |
333 | 353 | ||
@@ -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 | } |