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.rs92
1 files changed, 46 insertions, 46 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index b0063cad2..4c54449ef 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -77,7 +77,7 @@ 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: ast::Expr) -> Option<ExprId> { 80 pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> {
81 self.expr_syntax_mapping 81 self.expr_syntax_mapping
82 .get(&LocalSyntaxPtr::new(node.syntax())) 82 .get(&LocalSyntaxPtr::new(node.syntax()))
83 .cloned() 83 .cloned()
@@ -88,7 +88,7 @@ impl BodySyntaxMapping {
88 pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> { 88 pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> {
89 self.pat_syntax_mapping.get(&ptr).cloned() 89 self.pat_syntax_mapping.get(&ptr).cloned()
90 } 90 }
91 pub fn node_pat(&self, node: ast::Pat) -> Option<PatId> { 91 pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> {
92 self.pat_syntax_mapping 92 self.pat_syntax_mapping
93 .get(&LocalSyntaxPtr::new(node.syntax())) 93 .get(&LocalSyntaxPtr::new(node.syntax()))
94 .cloned() 94 .cloned()
@@ -373,10 +373,10 @@ impl ExprCollector {
373 self.exprs.alloc(block) 373 self.exprs.alloc(block)
374 } 374 }
375 375
376 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { 376 fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
377 let syntax_ptr = LocalSyntaxPtr::new(expr.syntax()); 377 let syntax_ptr = LocalSyntaxPtr::new(expr.syntax());
378 match expr { 378 match expr.kind() {
379 ast::Expr::IfExpr(e) => { 379 ast::ExprKind::IfExpr(e) => {
380 if let Some(pat) = e.condition().and_then(|c| c.pat()) { 380 if let Some(pat) = e.condition().and_then(|c| c.pat()) {
381 // if let -- desugar to match 381 // if let -- desugar to match
382 let pat = self.collect_pat(pat); 382 let pat = self.collect_pat(pat);
@@ -419,12 +419,12 @@ impl ExprCollector {
419 ) 419 )
420 } 420 }
421 } 421 }
422 ast::Expr::BlockExpr(e) => self.collect_block_opt(e.block()), 422 ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()),
423 ast::Expr::LoopExpr(e) => { 423 ast::ExprKind::LoopExpr(e) => {
424 let body = self.collect_block_opt(e.loop_body()); 424 let body = self.collect_block_opt(e.loop_body());
425 self.alloc_expr(Expr::Loop { body }, syntax_ptr) 425 self.alloc_expr(Expr::Loop { body }, syntax_ptr)
426 } 426 }
427 ast::Expr::WhileExpr(e) => { 427 ast::ExprKind::WhileExpr(e) => {
428 let condition = if let Some(condition) = e.condition() { 428 let condition = if let Some(condition) = e.condition() {
429 if condition.pat().is_none() { 429 if condition.pat().is_none() {
430 self.collect_expr_opt(condition.expr()) 430 self.collect_expr_opt(condition.expr())
@@ -438,7 +438,7 @@ impl ExprCollector {
438 let body = self.collect_block_opt(e.loop_body()); 438 let body = self.collect_block_opt(e.loop_body());
439 self.alloc_expr(Expr::While { condition, body }, syntax_ptr) 439 self.alloc_expr(Expr::While { condition, body }, syntax_ptr)
440 } 440 }
441 ast::Expr::ForExpr(e) => { 441 ast::ExprKind::ForExpr(e) => {
442 let iterable = self.collect_expr_opt(e.iterable()); 442 let iterable = self.collect_expr_opt(e.iterable());
443 let pat = self.collect_pat_opt(e.pat()); 443 let pat = self.collect_pat_opt(e.pat());
444 let body = self.collect_block_opt(e.loop_body()); 444 let body = self.collect_block_opt(e.loop_body());
@@ -451,7 +451,7 @@ impl ExprCollector {
451 syntax_ptr, 451 syntax_ptr,
452 ) 452 )
453 } 453 }
454 ast::Expr::CallExpr(e) => { 454 ast::ExprKind::CallExpr(e) => {
455 let callee = self.collect_expr_opt(e.expr()); 455 let callee = self.collect_expr_opt(e.expr());
456 let args = if let Some(arg_list) = e.arg_list() { 456 let args = if let Some(arg_list) = e.arg_list() {
457 arg_list.args().map(|e| self.collect_expr(e)).collect() 457 arg_list.args().map(|e| self.collect_expr(e)).collect()
@@ -460,7 +460,7 @@ impl ExprCollector {
460 }; 460 };
461 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) 461 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
462 } 462 }
463 ast::Expr::MethodCallExpr(e) => { 463 ast::ExprKind::MethodCallExpr(e) => {
464 let receiver = self.collect_expr_opt(e.expr()); 464 let receiver = self.collect_expr_opt(e.expr());
465 let args = if let Some(arg_list) = e.arg_list() { 465 let args = if let Some(arg_list) = e.arg_list() {
466 arg_list.args().map(|e| self.collect_expr(e)).collect() 466 arg_list.args().map(|e| self.collect_expr(e)).collect()
@@ -480,7 +480,7 @@ impl ExprCollector {
480 syntax_ptr, 480 syntax_ptr,
481 ) 481 )
482 } 482 }
483 ast::Expr::MatchExpr(e) => { 483 ast::ExprKind::MatchExpr(e) => {
484 let expr = self.collect_expr_opt(e.expr()); 484 let expr = self.collect_expr_opt(e.expr());
485 let arms = if let Some(match_arm_list) = e.match_arm_list() { 485 let arms = if let Some(match_arm_list) = e.match_arm_list() {
486 match_arm_list 486 match_arm_list
@@ -495,7 +495,7 @@ impl ExprCollector {
495 }; 495 };
496 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) 496 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
497 } 497 }
498 ast::Expr::PathExpr(e) => { 498 ast::ExprKind::PathExpr(e) => {
499 let path = e 499 let path = e
500 .path() 500 .path()
501 .and_then(Path::from_ast) 501 .and_then(Path::from_ast)
@@ -503,25 +503,25 @@ impl ExprCollector {
503 .unwrap_or(Expr::Missing); 503 .unwrap_or(Expr::Missing);
504 self.alloc_expr(path, syntax_ptr) 504 self.alloc_expr(path, syntax_ptr)
505 } 505 }
506 ast::Expr::ContinueExpr(_e) => { 506 ast::ExprKind::ContinueExpr(_e) => {
507 // TODO: labels 507 // TODO: labels
508 self.alloc_expr(Expr::Continue, syntax_ptr) 508 self.alloc_expr(Expr::Continue, syntax_ptr)
509 } 509 }
510 ast::Expr::BreakExpr(e) => { 510 ast::ExprKind::BreakExpr(e) => {
511 let expr = e.expr().map(|e| self.collect_expr(e)); 511 let expr = e.expr().map(|e| self.collect_expr(e));
512 self.alloc_expr(Expr::Break { expr }, syntax_ptr) 512 self.alloc_expr(Expr::Break { expr }, syntax_ptr)
513 } 513 }
514 ast::Expr::ParenExpr(e) => { 514 ast::ExprKind::ParenExpr(e) => {
515 let inner = self.collect_expr_opt(e.expr()); 515 let inner = self.collect_expr_opt(e.expr());
516 // make the paren expr point to the inner expression as well 516 // make the paren expr point to the inner expression as well
517 self.expr_syntax_mapping.insert(syntax_ptr, inner); 517 self.expr_syntax_mapping.insert(syntax_ptr, inner);
518 inner 518 inner
519 } 519 }
520 ast::Expr::ReturnExpr(e) => { 520 ast::ExprKind::ReturnExpr(e) => {
521 let expr = e.expr().map(|e| self.collect_expr(e)); 521 let expr = e.expr().map(|e| self.collect_expr(e));
522 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 522 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
523 } 523 }
524 ast::Expr::StructLit(e) => { 524 ast::ExprKind::StructLit(e) => {
525 let path = e.path().and_then(Path::from_ast); 525 let path = e.path().and_then(Path::from_ast);
526 let fields = if let Some(nfl) = e.named_field_list() { 526 let fields = if let Some(nfl) = e.named_field_list() {
527 nfl.fields() 527 nfl.fields()
@@ -558,7 +558,7 @@ impl ExprCollector {
558 syntax_ptr, 558 syntax_ptr,
559 ) 559 )
560 } 560 }
561 ast::Expr::FieldExpr(e) => { 561 ast::ExprKind::FieldExpr(e) => {
562 let expr = self.collect_expr_opt(e.expr()); 562 let expr = self.collect_expr_opt(e.expr());
563 let name = e 563 let name = e
564 .name_ref() 564 .name_ref()
@@ -566,26 +566,26 @@ impl ExprCollector {
566 .unwrap_or_else(Name::missing); 566 .unwrap_or_else(Name::missing);
567 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) 567 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
568 } 568 }
569 ast::Expr::TryExpr(e) => { 569 ast::ExprKind::TryExpr(e) => {
570 let expr = self.collect_expr_opt(e.expr()); 570 let expr = self.collect_expr_opt(e.expr());
571 self.alloc_expr(Expr::Try { expr }, syntax_ptr) 571 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
572 } 572 }
573 ast::Expr::CastExpr(e) => { 573 ast::ExprKind::CastExpr(e) => {
574 let expr = self.collect_expr_opt(e.expr()); 574 let expr = self.collect_expr_opt(e.expr());
575 let type_ref = TypeRef::from_ast_opt(e.type_ref()); 575 let type_ref = TypeRef::from_ast_opt(e.type_ref());
576 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) 576 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
577 } 577 }
578 ast::Expr::RefExpr(e) => { 578 ast::ExprKind::RefExpr(e) => {
579 let expr = self.collect_expr_opt(e.expr()); 579 let expr = self.collect_expr_opt(e.expr());
580 let mutability = Mutability::from_mutable(e.is_mut()); 580 let mutability = Mutability::from_mutable(e.is_mut());
581 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) 581 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
582 } 582 }
583 ast::Expr::PrefixExpr(e) => { 583 ast::ExprKind::PrefixExpr(e) => {
584 let expr = self.collect_expr_opt(e.expr()); 584 let expr = self.collect_expr_opt(e.expr());
585 let op = e.op(); 585 let op = e.op();
586 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) 586 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
587 } 587 }
588 ast::Expr::LambdaExpr(e) => { 588 ast::ExprKind::LambdaExpr(e) => {
589 let mut args = Vec::new(); 589 let mut args = Vec::new();
590 let mut arg_types = Vec::new(); 590 let mut arg_types = Vec::new();
591 if let Some(pl) = e.param_list() { 591 if let Some(pl) = e.param_list() {
@@ -606,7 +606,7 @@ impl ExprCollector {
606 syntax_ptr, 606 syntax_ptr,
607 ) 607 )
608 } 608 }
609 ast::Expr::BinExpr(e) => { 609 ast::ExprKind::BinExpr(e) => {
610 let lhs = self.collect_expr_opt(e.lhs()); 610 let lhs = self.collect_expr_opt(e.lhs());
611 let rhs = self.collect_expr_opt(e.rhs()); 611 let rhs = self.collect_expr_opt(e.rhs());
612 let op = e.op(); 612 let op = e.op();
@@ -614,16 +614,16 @@ impl ExprCollector {
614 } 614 }
615 615
616 // TODO implement HIR for these: 616 // TODO implement HIR for these:
617 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 617 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
618 ast::Expr::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 618 ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
619 ast::Expr::TupleExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 619 ast::ExprKind::TupleExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
620 ast::Expr::ArrayExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 620 ast::ExprKind::ArrayExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
621 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 621 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
622 ast::Expr::Literal(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 622 ast::ExprKind::Literal(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
623 } 623 }
624 } 624 }
625 625
626 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { 626 fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>) -> ExprId {
627 if let Some(expr) = expr { 627 if let Some(expr) = expr {
628 self.collect_expr(expr) 628 self.collect_expr(expr)
629 } else { 629 } else {
@@ -631,11 +631,11 @@ impl ExprCollector {
631 } 631 }
632 } 632 }
633 633
634 fn collect_block(&mut self, block: ast::Block) -> ExprId { 634 fn collect_block(&mut self, block: &ast::Block) -> ExprId {
635 let statements = block 635 let statements = block
636 .statements() 636 .statements()
637 .map(|s| match s { 637 .map(|s| match s.kind() {
638 ast::Stmt::LetStmt(stmt) => { 638 ast::StmtKind::LetStmt(stmt) => {
639 let pat = self.collect_pat_opt(stmt.pat()); 639 let pat = self.collect_pat_opt(stmt.pat());
640 let type_ref = stmt.type_ref().map(TypeRef::from_ast); 640 let type_ref = stmt.type_ref().map(TypeRef::from_ast);
641 let initializer = stmt.initializer().map(|e| self.collect_expr(e)); 641 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
@@ -645,7 +645,9 @@ impl ExprCollector {
645 initializer, 645 initializer,
646 } 646 }
647 } 647 }
648 ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())), 648 ast::StmtKind::ExprStmt(stmt) => {
649 Statement::Expr(self.collect_expr_opt(stmt.expr()))
650 }
649 }) 651 })
650 .collect(); 652 .collect();
651 let tail = block.expr().map(|e| self.collect_expr(e)); 653 let tail = block.expr().map(|e| self.collect_expr(e));
@@ -655,7 +657,7 @@ impl ExprCollector {
655 ) 657 )
656 } 658 }
657 659
658 fn collect_block_opt(&mut self, block: Option<ast::Block>) -> ExprId { 660 fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId {
659 if let Some(block) = block { 661 if let Some(block) = block {
660 self.collect_block(block) 662 self.collect_block(block)
661 } else { 663 } else {
@@ -663,17 +665,17 @@ impl ExprCollector {
663 } 665 }
664 } 666 }
665 667
666 fn collect_pat(&mut self, pat: ast::Pat) -> PatId { 668 fn collect_pat(&mut self, pat: &ast::Pat) -> PatId {
667 let syntax_ptr = LocalSyntaxPtr::new(pat.syntax()); 669 let syntax_ptr = LocalSyntaxPtr::new(pat.syntax());
668 match pat { 670 match pat.kind() {
669 ast::Pat::BindPat(bp) => { 671 ast::PatKind::BindPat(bp) => {
670 let name = bp 672 let name = bp
671 .name() 673 .name()
672 .map(|nr| nr.as_name()) 674 .map(|nr| nr.as_name())
673 .unwrap_or_else(Name::missing); 675 .unwrap_or_else(Name::missing);
674 self.alloc_pat(Pat::Bind { name }, syntax_ptr) 676 self.alloc_pat(Pat::Bind { name }, syntax_ptr)
675 } 677 }
676 ast::Pat::TupleStructPat(p) => { 678 ast::PatKind::TupleStructPat(p) => {
677 let path = p.path().and_then(Path::from_ast); 679 let path = p.path().and_then(Path::from_ast);
678 let args = p.args().map(|p| self.collect_pat(p)).collect(); 680 let args = p.args().map(|p| self.collect_pat(p)).collect();
679 self.alloc_pat(Pat::TupleStruct { path, args }, syntax_ptr) 681 self.alloc_pat(Pat::TupleStruct { path, args }, syntax_ptr)
@@ -685,7 +687,7 @@ impl ExprCollector {
685 } 687 }
686 } 688 }
687 689
688 fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId { 690 fn collect_pat_opt(&mut self, pat: Option<&ast::Pat>) -> PatId {
689 if let Some(pat) = pat { 691 if let Some(pat) = pat {
690 self.collect_pat(pat) 692 self.collect_pat(pat)
691 } else { 693 } else {
@@ -710,7 +712,7 @@ impl ExprCollector {
710 } 712 }
711} 713}
712 714
713pub(crate) fn collect_fn_body_syntax(node: ast::FnDef) -> BodySyntaxMapping { 715pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping {
714 let mut collector = ExprCollector::new(); 716 let mut collector = ExprCollector::new();
715 717
716 let args = if let Some(param_list) = node.param_list() { 718 let args = if let Some(param_list) = node.param_list() {
@@ -758,9 +760,7 @@ pub(crate) fn body_syntax_mapping(
758 let body_syntax_mapping = match def { 760 let body_syntax_mapping = match def {
759 Def::Function(f) => { 761 Def::Function(f) => {
760 let node = f.syntax(db); 762 let node = f.syntax(db);
761 let node = node.borrowed(); 763 collect_fn_body_syntax(&node)
762
763 collect_fn_body_syntax(node)
764 } 764 }
765 // TODO: consts, etc. 765 // TODO: consts, etc.
766 _ => panic!("Trying to get body for item type without body"), 766 _ => panic!("Trying to get body for item type without body"),