aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-19 12:04:51 +0100
committerAleksey Kladov <[email protected]>2019-08-19 12:04:51 +0100
commitb50a04827c13af00314eb9869d3cc125b2419971 (patch)
treebd4e171b740362e50d3bc64a1edb06abdd59a2fd /crates/ra_hir/src/expr.rs
parent39e444d70145cbf61ddfdd202572d9c6a7f2fd3c (diff)
remove ast::*Kind from hir
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r--crates/ra_hir/src/expr.rs90
1 files changed, 44 insertions, 46 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index a16561d11..328d635d4 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -602,8 +602,8 @@ where
602 602
603 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { 603 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
604 let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); 604 let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
605 match expr.kind() { 605 match expr {
606 ast::ExprKind::IfExpr(e) => { 606 ast::Expr::IfExpr(e) => {
607 let then_branch = self.collect_block_opt(e.then_branch()); 607 let then_branch = self.collect_block_opt(e.then_branch());
608 608
609 let else_branch = e.else_branch().map(|b| match b { 609 let else_branch = e.else_branch().map(|b| match b {
@@ -639,16 +639,16 @@ where
639 639
640 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) 640 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
641 } 641 }
642 ast::ExprKind::TryBlockExpr(e) => { 642 ast::Expr::TryBlockExpr(e) => {
643 let body = self.collect_block_opt(e.try_body()); 643 let body = self.collect_block_opt(e.try_body());
644 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr) 644 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
645 } 645 }
646 ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()), 646 ast::Expr::BlockExpr(e) => self.collect_block_opt(e.block()),
647 ast::ExprKind::LoopExpr(e) => { 647 ast::Expr::LoopExpr(e) => {
648 let body = self.collect_block_opt(e.loop_body()); 648 let body = self.collect_block_opt(e.loop_body());
649 self.alloc_expr(Expr::Loop { body }, syntax_ptr) 649 self.alloc_expr(Expr::Loop { body }, syntax_ptr)
650 } 650 }
651 ast::ExprKind::WhileExpr(e) => { 651 ast::Expr::WhileExpr(e) => {
652 let body = self.collect_block_opt(e.loop_body()); 652 let body = self.collect_block_opt(e.loop_body());
653 653
654 let condition = match e.condition() { 654 let condition = match e.condition() {
@@ -675,13 +675,13 @@ where
675 675
676 self.alloc_expr(Expr::While { condition, body }, syntax_ptr) 676 self.alloc_expr(Expr::While { condition, body }, syntax_ptr)
677 } 677 }
678 ast::ExprKind::ForExpr(e) => { 678 ast::Expr::ForExpr(e) => {
679 let iterable = self.collect_expr_opt(e.iterable()); 679 let iterable = self.collect_expr_opt(e.iterable());
680 let pat = self.collect_pat_opt(e.pat()); 680 let pat = self.collect_pat_opt(e.pat());
681 let body = self.collect_block_opt(e.loop_body()); 681 let body = self.collect_block_opt(e.loop_body());
682 self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) 682 self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr)
683 } 683 }
684 ast::ExprKind::CallExpr(e) => { 684 ast::Expr::CallExpr(e) => {
685 let callee = self.collect_expr_opt(e.expr()); 685 let callee = self.collect_expr_opt(e.expr());
686 let args = if let Some(arg_list) = e.arg_list() { 686 let args = if let Some(arg_list) = e.arg_list() {
687 arg_list.args().map(|e| self.collect_expr(e)).collect() 687 arg_list.args().map(|e| self.collect_expr(e)).collect()
@@ -690,7 +690,7 @@ where
690 }; 690 };
691 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) 691 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
692 } 692 }
693 ast::ExprKind::MethodCallExpr(e) => { 693 ast::Expr::MethodCallExpr(e) => {
694 let receiver = self.collect_expr_opt(e.expr()); 694 let receiver = self.collect_expr_opt(e.expr());
695 let args = if let Some(arg_list) = e.arg_list() { 695 let args = if let Some(arg_list) = e.arg_list() {
696 arg_list.args().map(|e| self.collect_expr(e)).collect() 696 arg_list.args().map(|e| self.collect_expr(e)).collect()
@@ -704,7 +704,7 @@ where
704 syntax_ptr, 704 syntax_ptr,
705 ) 705 )
706 } 706 }
707 ast::ExprKind::MatchExpr(e) => { 707 ast::Expr::MatchExpr(e) => {
708 let expr = self.collect_expr_opt(e.expr()); 708 let expr = self.collect_expr_opt(e.expr());
709 let arms = if let Some(match_arm_list) = e.match_arm_list() { 709 let arms = if let Some(match_arm_list) = e.match_arm_list() {
710 match_arm_list 710 match_arm_list
@@ -723,30 +723,30 @@ where
723 }; 723 };
724 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) 724 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
725 } 725 }
726 ast::ExprKind::PathExpr(e) => { 726 ast::Expr::PathExpr(e) => {
727 let path = 727 let path =
728 e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing); 728 e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing);
729 self.alloc_expr(path, syntax_ptr) 729 self.alloc_expr(path, syntax_ptr)
730 } 730 }
731 ast::ExprKind::ContinueExpr(_e) => { 731 ast::Expr::ContinueExpr(_e) => {
732 // FIXME: labels 732 // FIXME: labels
733 self.alloc_expr(Expr::Continue, syntax_ptr) 733 self.alloc_expr(Expr::Continue, syntax_ptr)
734 } 734 }
735 ast::ExprKind::BreakExpr(e) => { 735 ast::Expr::BreakExpr(e) => {
736 let expr = e.expr().map(|e| self.collect_expr(e)); 736 let expr = e.expr().map(|e| self.collect_expr(e));
737 self.alloc_expr(Expr::Break { expr }, syntax_ptr) 737 self.alloc_expr(Expr::Break { expr }, syntax_ptr)
738 } 738 }
739 ast::ExprKind::ParenExpr(e) => { 739 ast::Expr::ParenExpr(e) => {
740 let inner = self.collect_expr_opt(e.expr()); 740 let inner = self.collect_expr_opt(e.expr());
741 // make the paren expr point to the inner expression as well 741 // make the paren expr point to the inner expression as well
742 self.source_map.expr_map.insert(syntax_ptr, inner); 742 self.source_map.expr_map.insert(syntax_ptr, inner);
743 inner 743 inner
744 } 744 }
745 ast::ExprKind::ReturnExpr(e) => { 745 ast::Expr::ReturnExpr(e) => {
746 let expr = e.expr().map(|e| self.collect_expr(e)); 746 let expr = e.expr().map(|e| self.collect_expr(e));
747 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 747 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
748 } 748 }
749 ast::ExprKind::StructLit(e) => { 749 ast::Expr::StructLit(e) => {
750 let path = e.path().and_then(Path::from_ast); 750 let path = e.path().and_then(Path::from_ast);
751 let mut field_ptrs = Vec::new(); 751 let mut field_ptrs = Vec::new();
752 let struct_lit = if let Some(nfl) = e.named_field_list() { 752 let struct_lit = if let Some(nfl) = e.named_field_list() {
@@ -787,7 +787,7 @@ where
787 } 787 }
788 res 788 res
789 } 789 }
790 ast::ExprKind::FieldExpr(e) => { 790 ast::Expr::FieldExpr(e) => {
791 let expr = self.collect_expr_opt(e.expr()); 791 let expr = self.collect_expr_opt(e.expr());
792 let name = match e.field_access() { 792 let name = match e.field_access() {
793 Some(kind) => kind.as_name(), 793 Some(kind) => kind.as_name(),
@@ -795,25 +795,25 @@ where
795 }; 795 };
796 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) 796 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
797 } 797 }
798 ast::ExprKind::AwaitExpr(e) => { 798 ast::Expr::AwaitExpr(e) => {
799 let expr = self.collect_expr_opt(e.expr()); 799 let expr = self.collect_expr_opt(e.expr());
800 self.alloc_expr(Expr::Await { expr }, syntax_ptr) 800 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
801 } 801 }
802 ast::ExprKind::TryExpr(e) => { 802 ast::Expr::TryExpr(e) => {
803 let expr = self.collect_expr_opt(e.expr()); 803 let expr = self.collect_expr_opt(e.expr());
804 self.alloc_expr(Expr::Try { expr }, syntax_ptr) 804 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
805 } 805 }
806 ast::ExprKind::CastExpr(e) => { 806 ast::Expr::CastExpr(e) => {
807 let expr = self.collect_expr_opt(e.expr()); 807 let expr = self.collect_expr_opt(e.expr());
808 let type_ref = TypeRef::from_ast_opt(e.type_ref()); 808 let type_ref = TypeRef::from_ast_opt(e.type_ref());
809 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) 809 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
810 } 810 }
811 ast::ExprKind::RefExpr(e) => { 811 ast::Expr::RefExpr(e) => {
812 let expr = self.collect_expr_opt(e.expr()); 812 let expr = self.collect_expr_opt(e.expr());
813 let mutability = Mutability::from_mutable(e.is_mut()); 813 let mutability = Mutability::from_mutable(e.is_mut());
814 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) 814 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
815 } 815 }
816 ast::ExprKind::PrefixExpr(e) => { 816 ast::Expr::PrefixExpr(e) => {
817 let expr = self.collect_expr_opt(e.expr()); 817 let expr = self.collect_expr_opt(e.expr());
818 if let Some(op) = e.op_kind() { 818 if let Some(op) = e.op_kind() {
819 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) 819 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
@@ -821,7 +821,7 @@ where
821 self.alloc_expr(Expr::Missing, syntax_ptr) 821 self.alloc_expr(Expr::Missing, syntax_ptr)
822 } 822 }
823 } 823 }
824 ast::ExprKind::LambdaExpr(e) => { 824 ast::Expr::LambdaExpr(e) => {
825 let mut args = Vec::new(); 825 let mut args = Vec::new();
826 let mut arg_types = Vec::new(); 826 let mut arg_types = Vec::new();
827 if let Some(pl) = e.param_list() { 827 if let Some(pl) = e.param_list() {
@@ -835,18 +835,18 @@ where
835 let body = self.collect_expr_opt(e.body()); 835 let body = self.collect_expr_opt(e.body());
836 self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) 836 self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr)
837 } 837 }
838 ast::ExprKind::BinExpr(e) => { 838 ast::Expr::BinExpr(e) => {
839 let lhs = self.collect_expr_opt(e.lhs()); 839 let lhs = self.collect_expr_opt(e.lhs());
840 let rhs = self.collect_expr_opt(e.rhs()); 840 let rhs = self.collect_expr_opt(e.rhs());
841 let op = e.op_kind().map(BinaryOp::from); 841 let op = e.op_kind().map(BinaryOp::from);
842 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) 842 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
843 } 843 }
844 ast::ExprKind::TupleExpr(e) => { 844 ast::Expr::TupleExpr(e) => {
845 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); 845 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
846 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) 846 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
847 } 847 }
848 848
849 ast::ExprKind::ArrayExpr(e) => { 849 ast::Expr::ArrayExpr(e) => {
850 let kind = e.kind(); 850 let kind = e.kind();
851 851
852 match kind { 852 match kind {
@@ -865,7 +865,7 @@ where
865 } 865 }
866 } 866 }
867 867
868 ast::ExprKind::Literal(e) => { 868 ast::Expr::Literal(e) => {
869 let lit = match e.kind() { 869 let lit = match e.kind() {
870 LiteralKind::IntNumber { suffix } => { 870 LiteralKind::IntNumber { suffix } => {
871 let known_name = suffix 871 let known_name = suffix
@@ -895,16 +895,16 @@ where
895 }; 895 };
896 self.alloc_expr(Expr::Literal(lit), syntax_ptr) 896 self.alloc_expr(Expr::Literal(lit), syntax_ptr)
897 } 897 }
898 ast::ExprKind::IndexExpr(e) => { 898 ast::Expr::IndexExpr(e) => {
899 let base = self.collect_expr_opt(e.base()); 899 let base = self.collect_expr_opt(e.base());
900 let index = self.collect_expr_opt(e.index()); 900 let index = self.collect_expr_opt(e.index());
901 self.alloc_expr(Expr::Index { base, index }, syntax_ptr) 901 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
902 } 902 }
903 903
904 // FIXME implement HIR for these: 904 // FIXME implement HIR for these:
905 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 905 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
906 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 906 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
907 ast::ExprKind::MacroCall(e) => { 907 ast::Expr::MacroCall(e) => {
908 let ast_id = self 908 let ast_id = self
909 .db 909 .db
910 .ast_id_map(self.current_file_id) 910 .ast_id_map(self.current_file_id)
@@ -945,16 +945,14 @@ where
945 fn collect_block(&mut self, block: ast::Block) -> ExprId { 945 fn collect_block(&mut self, block: ast::Block) -> ExprId {
946 let statements = block 946 let statements = block
947 .statements() 947 .statements()
948 .map(|s| match s.kind() { 948 .map(|s| match s {
949 ast::StmtKind::LetStmt(stmt) => { 949 ast::Stmt::LetStmt(stmt) => {
950 let pat = self.collect_pat_opt(stmt.pat()); 950 let pat = self.collect_pat_opt(stmt.pat());
951 let type_ref = stmt.ascribed_type().map(TypeRef::from_ast); 951 let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
952 let initializer = stmt.initializer().map(|e| self.collect_expr(e)); 952 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
953 Statement::Let { pat, type_ref, initializer } 953 Statement::Let { pat, type_ref, initializer }
954 } 954 }
955 ast::StmtKind::ExprStmt(stmt) => { 955 ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())),
956 Statement::Expr(self.collect_expr_opt(stmt.expr()))
957 }
958 }) 956 })
959 .collect(); 957 .collect();
960 let tail = block.expr().map(|e| self.collect_expr(e)); 958 let tail = block.expr().map(|e| self.collect_expr(e));
@@ -970,33 +968,33 @@ where
970 } 968 }
971 969
972 fn collect_pat(&mut self, pat: ast::Pat) -> PatId { 970 fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
973 let pattern = match pat.kind() { 971 let pattern = match &pat {
974 ast::PatKind::BindPat(bp) => { 972 ast::Pat::BindPat(bp) => {
975 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); 973 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
976 let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); 974 let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref());
977 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); 975 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
978 Pat::Bind { name, mode: annotation, subpat } 976 Pat::Bind { name, mode: annotation, subpat }
979 } 977 }
980 ast::PatKind::TupleStructPat(p) => { 978 ast::Pat::TupleStructPat(p) => {
981 let path = p.path().and_then(Path::from_ast); 979 let path = p.path().and_then(Path::from_ast);
982 let args = p.args().map(|p| self.collect_pat(p)).collect(); 980 let args = p.args().map(|p| self.collect_pat(p)).collect();
983 Pat::TupleStruct { path, args } 981 Pat::TupleStruct { path, args }
984 } 982 }
985 ast::PatKind::RefPat(p) => { 983 ast::Pat::RefPat(p) => {
986 let pat = self.collect_pat_opt(p.pat()); 984 let pat = self.collect_pat_opt(p.pat());
987 let mutability = Mutability::from_mutable(p.is_mut()); 985 let mutability = Mutability::from_mutable(p.is_mut());
988 Pat::Ref { pat, mutability } 986 Pat::Ref { pat, mutability }
989 } 987 }
990 ast::PatKind::PathPat(p) => { 988 ast::Pat::PathPat(p) => {
991 let path = p.path().and_then(Path::from_ast); 989 let path = p.path().and_then(Path::from_ast);
992 path.map(Pat::Path).unwrap_or(Pat::Missing) 990 path.map(Pat::Path).unwrap_or(Pat::Missing)
993 } 991 }
994 ast::PatKind::TuplePat(p) => { 992 ast::Pat::TuplePat(p) => {
995 let args = p.args().map(|p| self.collect_pat(p)).collect(); 993 let args = p.args().map(|p| self.collect_pat(p)).collect();
996 Pat::Tuple(args) 994 Pat::Tuple(args)
997 } 995 }
998 ast::PatKind::PlaceholderPat(_) => Pat::Wild, 996 ast::Pat::PlaceholderPat(_) => Pat::Wild,
999 ast::PatKind::StructPat(p) => { 997 ast::Pat::StructPat(p) => {
1000 let path = p.path().and_then(Path::from_ast); 998 let path = p.path().and_then(Path::from_ast);
1001 let field_pat_list = 999 let field_pat_list =
1002 p.field_pat_list().expect("every struct should have a field list"); 1000 p.field_pat_list().expect("every struct should have a field list");
@@ -1022,8 +1020,8 @@ where
1022 } 1020 }
1023 1021
1024 // FIXME: implement 1022 // FIXME: implement
1025 ast::PatKind::LiteralPat(_) => Pat::Missing, 1023 ast::Pat::LiteralPat(_) => Pat::Missing,
1026 ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, 1024 ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
1027 }; 1025 };
1028 let ptr = AstPtr::new(&pat); 1026 let ptr = AstPtr::new(&pat);
1029 self.alloc_pat(pattern, Either::A(ptr)) 1027 self.alloc_pat(pattern, Either::A(ptr))