aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_syntax/src/ast/generated.rs684
-rw-r--r--crates/ra_syntax/src/lib.rs16
-rw-r--r--crates/ra_tools/src/boilerplate_gen.rs87
3 files changed, 397 insertions, 390 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 016668380..9c5789701 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -591,34 +591,7 @@ impl EnumVariantList {
591 } 591 }
592} 592}
593#[derive(Debug, Clone, PartialEq, Eq, Hash)] 593#[derive(Debug, Clone, PartialEq, Eq, Hash)]
594pub struct Expr { 594pub enum Expr {
595 pub(crate) syntax: SyntaxNode,
596}
597impl AstNode for Expr {
598 fn can_cast(kind: SyntaxKind) -> bool {
599 match kind {
600 TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR
601 | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL
602 | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | STRUCT_LIT | CALL_EXPR | INDEX_EXPR
603 | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | TRY_BLOCK_EXPR
604 | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL => {
605 true
606 }
607 _ => false,
608 }
609 }
610 fn cast(syntax: SyntaxNode) -> Option<Self> {
611 if Self::can_cast(syntax.kind()) {
612 Some(Self { syntax })
613 } else {
614 None
615 }
616 }
617 fn syntax(&self) -> &SyntaxNode {
618 &self.syntax
619 }
620}
621pub enum ExprKind {
622 TupleExpr(TupleExpr), 595 TupleExpr(TupleExpr),
623 ArrayExpr(ArrayExpr), 596 ArrayExpr(ArrayExpr),
624 ParenExpr(ParenExpr), 597 ParenExpr(ParenExpr),
@@ -652,189 +625,235 @@ pub enum ExprKind {
652} 625}
653impl From<TupleExpr> for Expr { 626impl From<TupleExpr> for Expr {
654 fn from(node: TupleExpr) -> Expr { 627 fn from(node: TupleExpr) -> Expr {
655 Expr { syntax: node.syntax } 628 Expr::TupleExpr(node)
656 } 629 }
657} 630}
658impl From<ArrayExpr> for Expr { 631impl From<ArrayExpr> for Expr {
659 fn from(node: ArrayExpr) -> Expr { 632 fn from(node: ArrayExpr) -> Expr {
660 Expr { syntax: node.syntax } 633 Expr::ArrayExpr(node)
661 } 634 }
662} 635}
663impl From<ParenExpr> for Expr { 636impl From<ParenExpr> for Expr {
664 fn from(node: ParenExpr) -> Expr { 637 fn from(node: ParenExpr) -> Expr {
665 Expr { syntax: node.syntax } 638 Expr::ParenExpr(node)
666 } 639 }
667} 640}
668impl From<PathExpr> for Expr { 641impl From<PathExpr> for Expr {
669 fn from(node: PathExpr) -> Expr { 642 fn from(node: PathExpr) -> Expr {
670 Expr { syntax: node.syntax } 643 Expr::PathExpr(node)
671 } 644 }
672} 645}
673impl From<LambdaExpr> for Expr { 646impl From<LambdaExpr> for Expr {
674 fn from(node: LambdaExpr) -> Expr { 647 fn from(node: LambdaExpr) -> Expr {
675 Expr { syntax: node.syntax } 648 Expr::LambdaExpr(node)
676 } 649 }
677} 650}
678impl From<IfExpr> for Expr { 651impl From<IfExpr> for Expr {
679 fn from(node: IfExpr) -> Expr { 652 fn from(node: IfExpr) -> Expr {
680 Expr { syntax: node.syntax } 653 Expr::IfExpr(node)
681 } 654 }
682} 655}
683impl From<LoopExpr> for Expr { 656impl From<LoopExpr> for Expr {
684 fn from(node: LoopExpr) -> Expr { 657 fn from(node: LoopExpr) -> Expr {
685 Expr { syntax: node.syntax } 658 Expr::LoopExpr(node)
686 } 659 }
687} 660}
688impl From<ForExpr> for Expr { 661impl From<ForExpr> for Expr {
689 fn from(node: ForExpr) -> Expr { 662 fn from(node: ForExpr) -> Expr {
690 Expr { syntax: node.syntax } 663 Expr::ForExpr(node)
691 } 664 }
692} 665}
693impl From<WhileExpr> for Expr { 666impl From<WhileExpr> for Expr {
694 fn from(node: WhileExpr) -> Expr { 667 fn from(node: WhileExpr) -> Expr {
695 Expr { syntax: node.syntax } 668 Expr::WhileExpr(node)
696 } 669 }
697} 670}
698impl From<ContinueExpr> for Expr { 671impl From<ContinueExpr> for Expr {
699 fn from(node: ContinueExpr) -> Expr { 672 fn from(node: ContinueExpr) -> Expr {
700 Expr { syntax: node.syntax } 673 Expr::ContinueExpr(node)
701 } 674 }
702} 675}
703impl From<BreakExpr> for Expr { 676impl From<BreakExpr> for Expr {
704 fn from(node: BreakExpr) -> Expr { 677 fn from(node: BreakExpr) -> Expr {
705 Expr { syntax: node.syntax } 678 Expr::BreakExpr(node)
706 } 679 }
707} 680}
708impl From<Label> for Expr { 681impl From<Label> for Expr {
709 fn from(node: Label) -> Expr { 682 fn from(node: Label) -> Expr {
710 Expr { syntax: node.syntax } 683 Expr::Label(node)
711 } 684 }
712} 685}
713impl From<BlockExpr> for Expr { 686impl From<BlockExpr> for Expr {
714 fn from(node: BlockExpr) -> Expr { 687 fn from(node: BlockExpr) -> Expr {
715 Expr { syntax: node.syntax } 688 Expr::BlockExpr(node)
716 } 689 }
717} 690}
718impl From<ReturnExpr> for Expr { 691impl From<ReturnExpr> for Expr {
719 fn from(node: ReturnExpr) -> Expr { 692 fn from(node: ReturnExpr) -> Expr {
720 Expr { syntax: node.syntax } 693 Expr::ReturnExpr(node)
721 } 694 }
722} 695}
723impl From<MatchExpr> for Expr { 696impl From<MatchExpr> for Expr {
724 fn from(node: MatchExpr) -> Expr { 697 fn from(node: MatchExpr) -> Expr {
725 Expr { syntax: node.syntax } 698 Expr::MatchExpr(node)
726 } 699 }
727} 700}
728impl From<StructLit> for Expr { 701impl From<StructLit> for Expr {
729 fn from(node: StructLit) -> Expr { 702 fn from(node: StructLit) -> Expr {
730 Expr { syntax: node.syntax } 703 Expr::StructLit(node)
731 } 704 }
732} 705}
733impl From<CallExpr> for Expr { 706impl From<CallExpr> for Expr {
734 fn from(node: CallExpr) -> Expr { 707 fn from(node: CallExpr) -> Expr {
735 Expr { syntax: node.syntax } 708 Expr::CallExpr(node)
736 } 709 }
737} 710}
738impl From<IndexExpr> for Expr { 711impl From<IndexExpr> for Expr {
739 fn from(node: IndexExpr) -> Expr { 712 fn from(node: IndexExpr) -> Expr {
740 Expr { syntax: node.syntax } 713 Expr::IndexExpr(node)
741 } 714 }
742} 715}
743impl From<MethodCallExpr> for Expr { 716impl From<MethodCallExpr> for Expr {
744 fn from(node: MethodCallExpr) -> Expr { 717 fn from(node: MethodCallExpr) -> Expr {
745 Expr { syntax: node.syntax } 718 Expr::MethodCallExpr(node)
746 } 719 }
747} 720}
748impl From<FieldExpr> for Expr { 721impl From<FieldExpr> for Expr {
749 fn from(node: FieldExpr) -> Expr { 722 fn from(node: FieldExpr) -> Expr {
750 Expr { syntax: node.syntax } 723 Expr::FieldExpr(node)
751 } 724 }
752} 725}
753impl From<AwaitExpr> for Expr { 726impl From<AwaitExpr> for Expr {
754 fn from(node: AwaitExpr) -> Expr { 727 fn from(node: AwaitExpr) -> Expr {
755 Expr { syntax: node.syntax } 728 Expr::AwaitExpr(node)
756 } 729 }
757} 730}
758impl From<TryExpr> for Expr { 731impl From<TryExpr> for Expr {
759 fn from(node: TryExpr) -> Expr { 732 fn from(node: TryExpr) -> Expr {
760 Expr { syntax: node.syntax } 733 Expr::TryExpr(node)
761 } 734 }
762} 735}
763impl From<TryBlockExpr> for Expr { 736impl From<TryBlockExpr> for Expr {
764 fn from(node: TryBlockExpr) -> Expr { 737 fn from(node: TryBlockExpr) -> Expr {
765 Expr { syntax: node.syntax } 738 Expr::TryBlockExpr(node)
766 } 739 }
767} 740}
768impl From<CastExpr> for Expr { 741impl From<CastExpr> for Expr {
769 fn from(node: CastExpr) -> Expr { 742 fn from(node: CastExpr) -> Expr {
770 Expr { syntax: node.syntax } 743 Expr::CastExpr(node)
771 } 744 }
772} 745}
773impl From<RefExpr> for Expr { 746impl From<RefExpr> for Expr {
774 fn from(node: RefExpr) -> Expr { 747 fn from(node: RefExpr) -> Expr {
775 Expr { syntax: node.syntax } 748 Expr::RefExpr(node)
776 } 749 }
777} 750}
778impl From<PrefixExpr> for Expr { 751impl From<PrefixExpr> for Expr {
779 fn from(node: PrefixExpr) -> Expr { 752 fn from(node: PrefixExpr) -> Expr {
780 Expr { syntax: node.syntax } 753 Expr::PrefixExpr(node)
781 } 754 }
782} 755}
783impl From<RangeExpr> for Expr { 756impl From<RangeExpr> for Expr {
784 fn from(node: RangeExpr) -> Expr { 757 fn from(node: RangeExpr) -> Expr {
785 Expr { syntax: node.syntax } 758 Expr::RangeExpr(node)
786 } 759 }
787} 760}
788impl From<BinExpr> for Expr { 761impl From<BinExpr> for Expr {
789 fn from(node: BinExpr) -> Expr { 762 fn from(node: BinExpr) -> Expr {
790 Expr { syntax: node.syntax } 763 Expr::BinExpr(node)
791 } 764 }
792} 765}
793impl From<Literal> for Expr { 766impl From<Literal> for Expr {
794 fn from(node: Literal) -> Expr { 767 fn from(node: Literal) -> Expr {
795 Expr { syntax: node.syntax } 768 Expr::Literal(node)
796 } 769 }
797} 770}
798impl From<MacroCall> for Expr { 771impl From<MacroCall> for Expr {
799 fn from(node: MacroCall) -> Expr { 772 fn from(node: MacroCall) -> Expr {
800 Expr { syntax: node.syntax } 773 Expr::MacroCall(node)
801 } 774 }
802} 775}
803impl Expr { 776impl AstNode for Expr {
804 pub fn kind(&self) -> ExprKind { 777 fn can_cast(kind: SyntaxKind) -> bool {
805 let syntax = self.syntax.clone(); 778 match kind {
806 match syntax.kind() { 779 TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR
807 TUPLE_EXPR => ExprKind::TupleExpr(TupleExpr { syntax }), 780 | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL
808 ARRAY_EXPR => ExprKind::ArrayExpr(ArrayExpr { syntax }), 781 | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | STRUCT_LIT | CALL_EXPR | INDEX_EXPR
809 PAREN_EXPR => ExprKind::ParenExpr(ParenExpr { syntax }), 782 | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | TRY_BLOCK_EXPR
810 PATH_EXPR => ExprKind::PathExpr(PathExpr { syntax }), 783 | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL => {
811 LAMBDA_EXPR => ExprKind::LambdaExpr(LambdaExpr { syntax }), 784 true
812 IF_EXPR => ExprKind::IfExpr(IfExpr { syntax }), 785 }
813 LOOP_EXPR => ExprKind::LoopExpr(LoopExpr { syntax }), 786 _ => false,
814 FOR_EXPR => ExprKind::ForExpr(ForExpr { syntax }), 787 }
815 WHILE_EXPR => ExprKind::WhileExpr(WhileExpr { syntax }), 788 }
816 CONTINUE_EXPR => ExprKind::ContinueExpr(ContinueExpr { syntax }), 789 fn cast(syntax: SyntaxNode) -> Option<Self> {
817 BREAK_EXPR => ExprKind::BreakExpr(BreakExpr { syntax }), 790 let res = match syntax.kind() {
818 LABEL => ExprKind::Label(Label { syntax }), 791 TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }),
819 BLOCK_EXPR => ExprKind::BlockExpr(BlockExpr { syntax }), 792 ARRAY_EXPR => Expr::ArrayExpr(ArrayExpr { syntax }),
820 RETURN_EXPR => ExprKind::ReturnExpr(ReturnExpr { syntax }), 793 PAREN_EXPR => Expr::ParenExpr(ParenExpr { syntax }),
821 MATCH_EXPR => ExprKind::MatchExpr(MatchExpr { syntax }), 794 PATH_EXPR => Expr::PathExpr(PathExpr { syntax }),
822 STRUCT_LIT => ExprKind::StructLit(StructLit { syntax }), 795 LAMBDA_EXPR => Expr::LambdaExpr(LambdaExpr { syntax }),
823 CALL_EXPR => ExprKind::CallExpr(CallExpr { syntax }), 796 IF_EXPR => Expr::IfExpr(IfExpr { syntax }),
824 INDEX_EXPR => ExprKind::IndexExpr(IndexExpr { syntax }), 797 LOOP_EXPR => Expr::LoopExpr(LoopExpr { syntax }),
825 METHOD_CALL_EXPR => ExprKind::MethodCallExpr(MethodCallExpr { syntax }), 798 FOR_EXPR => Expr::ForExpr(ForExpr { syntax }),
826 FIELD_EXPR => ExprKind::FieldExpr(FieldExpr { syntax }), 799 WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }),
827 AWAIT_EXPR => ExprKind::AwaitExpr(AwaitExpr { syntax }), 800 CONTINUE_EXPR => Expr::ContinueExpr(ContinueExpr { syntax }),
828 TRY_EXPR => ExprKind::TryExpr(TryExpr { syntax }), 801 BREAK_EXPR => Expr::BreakExpr(BreakExpr { syntax }),
829 TRY_BLOCK_EXPR => ExprKind::TryBlockExpr(TryBlockExpr { syntax }), 802 LABEL => Expr::Label(Label { syntax }),
830 CAST_EXPR => ExprKind::CastExpr(CastExpr { syntax }), 803 BLOCK_EXPR => Expr::BlockExpr(BlockExpr { syntax }),
831 REF_EXPR => ExprKind::RefExpr(RefExpr { syntax }), 804 RETURN_EXPR => Expr::ReturnExpr(ReturnExpr { syntax }),
832 PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr { syntax }), 805 MATCH_EXPR => Expr::MatchExpr(MatchExpr { syntax }),
833 RANGE_EXPR => ExprKind::RangeExpr(RangeExpr { syntax }), 806 STRUCT_LIT => Expr::StructLit(StructLit { syntax }),
834 BIN_EXPR => ExprKind::BinExpr(BinExpr { syntax }), 807 CALL_EXPR => Expr::CallExpr(CallExpr { syntax }),
835 LITERAL => ExprKind::Literal(Literal { syntax }), 808 INDEX_EXPR => Expr::IndexExpr(IndexExpr { syntax }),
836 MACRO_CALL => ExprKind::MacroCall(MacroCall { syntax }), 809 METHOD_CALL_EXPR => Expr::MethodCallExpr(MethodCallExpr { syntax }),
837 _ => unreachable!(), 810 FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }),
811 AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }),
812 TRY_EXPR => Expr::TryExpr(TryExpr { syntax }),
813 TRY_BLOCK_EXPR => Expr::TryBlockExpr(TryBlockExpr { syntax }),
814 CAST_EXPR => Expr::CastExpr(CastExpr { syntax }),
815 REF_EXPR => Expr::RefExpr(RefExpr { syntax }),
816 PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }),
817 RANGE_EXPR => Expr::RangeExpr(RangeExpr { syntax }),
818 BIN_EXPR => Expr::BinExpr(BinExpr { syntax }),
819 LITERAL => Expr::Literal(Literal { syntax }),
820 MACRO_CALL => Expr::MacroCall(MacroCall { syntax }),
821 _ => return None,
822 };
823 Some(res)
824 }
825 fn syntax(&self) -> &SyntaxNode {
826 match self {
827 Expr::TupleExpr(it) => &it.syntax,
828 Expr::ArrayExpr(it) => &it.syntax,
829 Expr::ParenExpr(it) => &it.syntax,
830 Expr::PathExpr(it) => &it.syntax,
831 Expr::LambdaExpr(it) => &it.syntax,
832 Expr::IfExpr(it) => &it.syntax,
833 Expr::LoopExpr(it) => &it.syntax,
834 Expr::ForExpr(it) => &it.syntax,
835 Expr::WhileExpr(it) => &it.syntax,
836 Expr::ContinueExpr(it) => &it.syntax,
837 Expr::BreakExpr(it) => &it.syntax,
838 Expr::Label(it) => &it.syntax,
839 Expr::BlockExpr(it) => &it.syntax,
840 Expr::ReturnExpr(it) => &it.syntax,
841 Expr::MatchExpr(it) => &it.syntax,
842 Expr::StructLit(it) => &it.syntax,
843 Expr::CallExpr(it) => &it.syntax,
844 Expr::IndexExpr(it) => &it.syntax,
845 Expr::MethodCallExpr(it) => &it.syntax,
846 Expr::FieldExpr(it) => &it.syntax,
847 Expr::AwaitExpr(it) => &it.syntax,
848 Expr::TryExpr(it) => &it.syntax,
849 Expr::TryBlockExpr(it) => &it.syntax,
850 Expr::CastExpr(it) => &it.syntax,
851 Expr::RefExpr(it) => &it.syntax,
852 Expr::PrefixExpr(it) => &it.syntax,
853 Expr::RangeExpr(it) => &it.syntax,
854 Expr::BinExpr(it) => &it.syntax,
855 Expr::Literal(it) => &it.syntax,
856 Expr::MacroCall(it) => &it.syntax,
838 } 857 }
839 } 858 }
840} 859}
@@ -1167,55 +1186,47 @@ impl ImplBlock {
1167 } 1186 }
1168} 1187}
1169#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1188#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1170pub struct ImplItem { 1189pub enum ImplItem {
1171 pub(crate) syntax: SyntaxNode,
1172}
1173impl AstNode for ImplItem {
1174 fn can_cast(kind: SyntaxKind) -> bool {
1175 match kind {
1176 FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true,
1177 _ => false,
1178 }
1179 }
1180 fn cast(syntax: SyntaxNode) -> Option<Self> {
1181 if Self::can_cast(syntax.kind()) {
1182 Some(Self { syntax })
1183 } else {
1184 None
1185 }
1186 }
1187 fn syntax(&self) -> &SyntaxNode {
1188 &self.syntax
1189 }
1190}
1191pub enum ImplItemKind {
1192 FnDef(FnDef), 1190 FnDef(FnDef),
1193 TypeAliasDef(TypeAliasDef), 1191 TypeAliasDef(TypeAliasDef),
1194 ConstDef(ConstDef), 1192 ConstDef(ConstDef),
1195} 1193}
1196impl From<FnDef> for ImplItem { 1194impl From<FnDef> for ImplItem {
1197 fn from(node: FnDef) -> ImplItem { 1195 fn from(node: FnDef) -> ImplItem {
1198 ImplItem { syntax: node.syntax } 1196 ImplItem::FnDef(node)
1199 } 1197 }
1200} 1198}
1201impl From<TypeAliasDef> for ImplItem { 1199impl From<TypeAliasDef> for ImplItem {
1202 fn from(node: TypeAliasDef) -> ImplItem { 1200 fn from(node: TypeAliasDef) -> ImplItem {
1203 ImplItem { syntax: node.syntax } 1201 ImplItem::TypeAliasDef(node)
1204 } 1202 }
1205} 1203}
1206impl From<ConstDef> for ImplItem { 1204impl From<ConstDef> for ImplItem {
1207 fn from(node: ConstDef) -> ImplItem { 1205 fn from(node: ConstDef) -> ImplItem {
1208 ImplItem { syntax: node.syntax } 1206 ImplItem::ConstDef(node)
1209 } 1207 }
1210} 1208}
1211impl ImplItem { 1209impl AstNode for ImplItem {
1212 pub fn kind(&self) -> ImplItemKind { 1210 fn can_cast(kind: SyntaxKind) -> bool {
1213 let syntax = self.syntax.clone(); 1211 match kind {
1214 match syntax.kind() { 1212 FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true,
1215 FN_DEF => ImplItemKind::FnDef(FnDef { syntax }), 1213 _ => false,
1216 TYPE_ALIAS_DEF => ImplItemKind::TypeAliasDef(TypeAliasDef { syntax }), 1214 }
1217 CONST_DEF => ImplItemKind::ConstDef(ConstDef { syntax }), 1215 }
1218 _ => unreachable!(), 1216 fn cast(syntax: SyntaxNode) -> Option<Self> {
1217 let res = match syntax.kind() {
1218 FN_DEF => ImplItem::FnDef(FnDef { syntax }),
1219 TYPE_ALIAS_DEF => ImplItem::TypeAliasDef(TypeAliasDef { syntax }),
1220 CONST_DEF => ImplItem::ConstDef(ConstDef { syntax }),
1221 _ => return None,
1222 };
1223 Some(res)
1224 }
1225 fn syntax(&self) -> &SyntaxNode {
1226 match self {
1227 ImplItem::FnDef(it) => &it.syntax,
1228 ImplItem::TypeAliasDef(it) => &it.syntax,
1229 ImplItem::ConstDef(it) => &it.syntax,
1219 } 1230 }
1220 } 1231 }
1221} 1232}
@@ -1774,29 +1785,7 @@ impl Module {
1774 } 1785 }
1775} 1786}
1776#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1787#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1777pub struct ModuleItem { 1788pub enum ModuleItem {
1778 pub(crate) syntax: SyntaxNode,
1779}
1780impl AstNode for ModuleItem {
1781 fn can_cast(kind: SyntaxKind) -> bool {
1782 match kind {
1783 STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_BLOCK | USE_ITEM
1784 | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true,
1785 _ => false,
1786 }
1787 }
1788 fn cast(syntax: SyntaxNode) -> Option<Self> {
1789 if Self::can_cast(syntax.kind()) {
1790 Some(Self { syntax })
1791 } else {
1792 None
1793 }
1794 }
1795 fn syntax(&self) -> &SyntaxNode {
1796 &self.syntax
1797 }
1798}
1799pub enum ModuleItemKind {
1800 StructDef(StructDef), 1789 StructDef(StructDef),
1801 EnumDef(EnumDef), 1790 EnumDef(EnumDef),
1802 FnDef(FnDef), 1791 FnDef(FnDef),
@@ -1811,75 +1800,97 @@ pub enum ModuleItemKind {
1811} 1800}
1812impl From<StructDef> for ModuleItem { 1801impl From<StructDef> for ModuleItem {
1813 fn from(node: StructDef) -> ModuleItem { 1802 fn from(node: StructDef) -> ModuleItem {
1814 ModuleItem { syntax: node.syntax } 1803 ModuleItem::StructDef(node)
1815 } 1804 }
1816} 1805}
1817impl From<EnumDef> for ModuleItem { 1806impl From<EnumDef> for ModuleItem {
1818 fn from(node: EnumDef) -> ModuleItem { 1807 fn from(node: EnumDef) -> ModuleItem {
1819 ModuleItem { syntax: node.syntax } 1808 ModuleItem::EnumDef(node)
1820 } 1809 }
1821} 1810}
1822impl From<FnDef> for ModuleItem { 1811impl From<FnDef> for ModuleItem {
1823 fn from(node: FnDef) -> ModuleItem { 1812 fn from(node: FnDef) -> ModuleItem {
1824 ModuleItem { syntax: node.syntax } 1813 ModuleItem::FnDef(node)
1825 } 1814 }
1826} 1815}
1827impl From<TraitDef> for ModuleItem { 1816impl From<TraitDef> for ModuleItem {
1828 fn from(node: TraitDef) -> ModuleItem { 1817 fn from(node: TraitDef) -> ModuleItem {
1829 ModuleItem { syntax: node.syntax } 1818 ModuleItem::TraitDef(node)
1830 } 1819 }
1831} 1820}
1832impl From<TypeAliasDef> for ModuleItem { 1821impl From<TypeAliasDef> for ModuleItem {
1833 fn from(node: TypeAliasDef) -> ModuleItem { 1822 fn from(node: TypeAliasDef) -> ModuleItem {
1834 ModuleItem { syntax: node.syntax } 1823 ModuleItem::TypeAliasDef(node)
1835 } 1824 }
1836} 1825}
1837impl From<ImplBlock> for ModuleItem { 1826impl From<ImplBlock> for ModuleItem {
1838 fn from(node: ImplBlock) -> ModuleItem { 1827 fn from(node: ImplBlock) -> ModuleItem {
1839 ModuleItem { syntax: node.syntax } 1828 ModuleItem::ImplBlock(node)
1840 } 1829 }
1841} 1830}
1842impl From<UseItem> for ModuleItem { 1831impl From<UseItem> for ModuleItem {
1843 fn from(node: UseItem) -> ModuleItem { 1832 fn from(node: UseItem) -> ModuleItem {
1844 ModuleItem { syntax: node.syntax } 1833 ModuleItem::UseItem(node)
1845 } 1834 }
1846} 1835}
1847impl From<ExternCrateItem> for ModuleItem { 1836impl From<ExternCrateItem> for ModuleItem {
1848 fn from(node: ExternCrateItem) -> ModuleItem { 1837 fn from(node: ExternCrateItem) -> ModuleItem {
1849 ModuleItem { syntax: node.syntax } 1838 ModuleItem::ExternCrateItem(node)
1850 } 1839 }
1851} 1840}
1852impl From<ConstDef> for ModuleItem { 1841impl From<ConstDef> for ModuleItem {
1853 fn from(node: ConstDef) -> ModuleItem { 1842 fn from(node: ConstDef) -> ModuleItem {
1854 ModuleItem { syntax: node.syntax } 1843 ModuleItem::ConstDef(node)
1855 } 1844 }
1856} 1845}
1857impl From<StaticDef> for ModuleItem { 1846impl From<StaticDef> for ModuleItem {
1858 fn from(node: StaticDef) -> ModuleItem { 1847 fn from(node: StaticDef) -> ModuleItem {
1859 ModuleItem { syntax: node.syntax } 1848 ModuleItem::StaticDef(node)
1860 } 1849 }
1861} 1850}
1862impl From<Module> for ModuleItem { 1851impl From<Module> for ModuleItem {
1863 fn from(node: Module) -> ModuleItem { 1852 fn from(node: Module) -> ModuleItem {
1864 ModuleItem { syntax: node.syntax } 1853 ModuleItem::Module(node)
1865 } 1854 }
1866} 1855}
1867impl ModuleItem { 1856impl AstNode for ModuleItem {
1868 pub fn kind(&self) -> ModuleItemKind { 1857 fn can_cast(kind: SyntaxKind) -> bool {
1869 let syntax = self.syntax.clone(); 1858 match kind {
1870 match syntax.kind() { 1859 STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_BLOCK | USE_ITEM
1871 STRUCT_DEF => ModuleItemKind::StructDef(StructDef { syntax }), 1860 | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true,
1872 ENUM_DEF => ModuleItemKind::EnumDef(EnumDef { syntax }), 1861 _ => false,
1873 FN_DEF => ModuleItemKind::FnDef(FnDef { syntax }), 1862 }
1874 TRAIT_DEF => ModuleItemKind::TraitDef(TraitDef { syntax }), 1863 }
1875 TYPE_ALIAS_DEF => ModuleItemKind::TypeAliasDef(TypeAliasDef { syntax }), 1864 fn cast(syntax: SyntaxNode) -> Option<Self> {
1876 IMPL_BLOCK => ModuleItemKind::ImplBlock(ImplBlock { syntax }), 1865 let res = match syntax.kind() {
1877 USE_ITEM => ModuleItemKind::UseItem(UseItem { syntax }), 1866 STRUCT_DEF => ModuleItem::StructDef(StructDef { syntax }),
1878 EXTERN_CRATE_ITEM => ModuleItemKind::ExternCrateItem(ExternCrateItem { syntax }), 1867 ENUM_DEF => ModuleItem::EnumDef(EnumDef { syntax }),
1879 CONST_DEF => ModuleItemKind::ConstDef(ConstDef { syntax }), 1868 FN_DEF => ModuleItem::FnDef(FnDef { syntax }),
1880 STATIC_DEF => ModuleItemKind::StaticDef(StaticDef { syntax }), 1869 TRAIT_DEF => ModuleItem::TraitDef(TraitDef { syntax }),
1881 MODULE => ModuleItemKind::Module(Module { syntax }), 1870 TYPE_ALIAS_DEF => ModuleItem::TypeAliasDef(TypeAliasDef { syntax }),
1882 _ => unreachable!(), 1871 IMPL_BLOCK => ModuleItem::ImplBlock(ImplBlock { syntax }),
1872 USE_ITEM => ModuleItem::UseItem(UseItem { syntax }),
1873 EXTERN_CRATE_ITEM => ModuleItem::ExternCrateItem(ExternCrateItem { syntax }),
1874 CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }),
1875 STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }),
1876 MODULE => ModuleItem::Module(Module { syntax }),
1877 _ => return None,
1878 };
1879 Some(res)
1880 }
1881 fn syntax(&self) -> &SyntaxNode {
1882 match self {
1883 ModuleItem::StructDef(it) => &it.syntax,
1884 ModuleItem::EnumDef(it) => &it.syntax,
1885 ModuleItem::FnDef(it) => &it.syntax,
1886 ModuleItem::TraitDef(it) => &it.syntax,
1887 ModuleItem::TypeAliasDef(it) => &it.syntax,
1888 ModuleItem::ImplBlock(it) => &it.syntax,
1889 ModuleItem::UseItem(it) => &it.syntax,
1890 ModuleItem::ExternCrateItem(it) => &it.syntax,
1891 ModuleItem::ConstDef(it) => &it.syntax,
1892 ModuleItem::StaticDef(it) => &it.syntax,
1893 ModuleItem::Module(it) => &it.syntax,
1883 } 1894 }
1884 } 1895 }
1885} 1896}
@@ -2069,48 +2080,39 @@ impl AstNode for NeverType {
2069} 2080}
2070impl NeverType {} 2081impl NeverType {}
2071#[derive(Debug, Clone, PartialEq, Eq, Hash)] 2082#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2072pub struct NominalDef { 2083pub enum NominalDef {
2073 pub(crate) syntax: SyntaxNode,
2074}
2075impl AstNode for NominalDef {
2076 fn can_cast(kind: SyntaxKind) -> bool {
2077 match kind {
2078 STRUCT_DEF | ENUM_DEF => true,
2079 _ => false,
2080 }
2081 }
2082 fn cast(syntax: SyntaxNode) -> Option<Self> {
2083 if Self::can_cast(syntax.kind()) {
2084 Some(Self { syntax })
2085 } else {
2086 None
2087 }
2088 }
2089 fn syntax(&self) -> &SyntaxNode {
2090 &self.syntax
2091 }
2092}
2093pub enum NominalDefKind {
2094 StructDef(StructDef), 2084 StructDef(StructDef),
2095 EnumDef(EnumDef), 2085 EnumDef(EnumDef),
2096} 2086}
2097impl From<StructDef> for NominalDef { 2087impl From<StructDef> for NominalDef {
2098 fn from(node: StructDef) -> NominalDef { 2088 fn from(node: StructDef) -> NominalDef {
2099 NominalDef { syntax: node.syntax } 2089 NominalDef::StructDef(node)
2100 } 2090 }
2101} 2091}
2102impl From<EnumDef> for NominalDef { 2092impl From<EnumDef> for NominalDef {
2103 fn from(node: EnumDef) -> NominalDef { 2093 fn from(node: EnumDef) -> NominalDef {
2104 NominalDef { syntax: node.syntax } 2094 NominalDef::EnumDef(node)
2105 } 2095 }
2106} 2096}
2107impl NominalDef { 2097impl AstNode for NominalDef {
2108 pub fn kind(&self) -> NominalDefKind { 2098 fn can_cast(kind: SyntaxKind) -> bool {
2109 let syntax = self.syntax.clone(); 2099 match kind {
2110 match syntax.kind() { 2100 STRUCT_DEF | ENUM_DEF => true,
2111 STRUCT_DEF => NominalDefKind::StructDef(StructDef { syntax }), 2101 _ => false,
2112 ENUM_DEF => NominalDefKind::EnumDef(EnumDef { syntax }), 2102 }
2113 _ => unreachable!(), 2103 }
2104 fn cast(syntax: SyntaxNode) -> Option<Self> {
2105 let res = match syntax.kind() {
2106 STRUCT_DEF => NominalDef::StructDef(StructDef { syntax }),
2107 ENUM_DEF => NominalDef::EnumDef(EnumDef { syntax }),
2108 _ => return None,
2109 };
2110 Some(res)
2111 }
2112 fn syntax(&self) -> &SyntaxNode {
2113 match self {
2114 NominalDef::StructDef(it) => &it.syntax,
2115 NominalDef::EnumDef(it) => &it.syntax,
2114 } 2116 }
2115 } 2117 }
2116} 2118}
@@ -2232,29 +2234,7 @@ impl ParenType {
2232 } 2234 }
2233} 2235}
2234#[derive(Debug, Clone, PartialEq, Eq, Hash)] 2236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2235pub struct Pat { 2237pub enum Pat {
2236 pub(crate) syntax: SyntaxNode,
2237}
2238impl AstNode for Pat {
2239 fn can_cast(kind: SyntaxKind) -> bool {
2240 match kind {
2241 REF_PAT | BIND_PAT | PLACEHOLDER_PAT | PATH_PAT | STRUCT_PAT | TUPLE_STRUCT_PAT
2242 | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => true,
2243 _ => false,
2244 }
2245 }
2246 fn cast(syntax: SyntaxNode) -> Option<Self> {
2247 if Self::can_cast(syntax.kind()) {
2248 Some(Self { syntax })
2249 } else {
2250 None
2251 }
2252 }
2253 fn syntax(&self) -> &SyntaxNode {
2254 &self.syntax
2255 }
2256}
2257pub enum PatKind {
2258 RefPat(RefPat), 2238 RefPat(RefPat),
2259 BindPat(BindPat), 2239 BindPat(BindPat),
2260 PlaceholderPat(PlaceholderPat), 2240 PlaceholderPat(PlaceholderPat),
@@ -2268,69 +2248,90 @@ pub enum PatKind {
2268} 2248}
2269impl From<RefPat> for Pat { 2249impl From<RefPat> for Pat {
2270 fn from(node: RefPat) -> Pat { 2250 fn from(node: RefPat) -> Pat {
2271 Pat { syntax: node.syntax } 2251 Pat::RefPat(node)
2272 } 2252 }
2273} 2253}
2274impl From<BindPat> for Pat { 2254impl From<BindPat> for Pat {
2275 fn from(node: BindPat) -> Pat { 2255 fn from(node: BindPat) -> Pat {
2276 Pat { syntax: node.syntax } 2256 Pat::BindPat(node)
2277 } 2257 }
2278} 2258}
2279impl From<PlaceholderPat> for Pat { 2259impl From<PlaceholderPat> for Pat {
2280 fn from(node: PlaceholderPat) -> Pat { 2260 fn from(node: PlaceholderPat) -> Pat {
2281 Pat { syntax: node.syntax } 2261 Pat::PlaceholderPat(node)
2282 } 2262 }
2283} 2263}
2284impl From<PathPat> for Pat { 2264impl From<PathPat> for Pat {
2285 fn from(node: PathPat) -> Pat { 2265 fn from(node: PathPat) -> Pat {
2286 Pat { syntax: node.syntax } 2266 Pat::PathPat(node)
2287 } 2267 }
2288} 2268}
2289impl From<StructPat> for Pat { 2269impl From<StructPat> for Pat {
2290 fn from(node: StructPat) -> Pat { 2270 fn from(node: StructPat) -> Pat {
2291 Pat { syntax: node.syntax } 2271 Pat::StructPat(node)
2292 } 2272 }
2293} 2273}
2294impl From<TupleStructPat> for Pat { 2274impl From<TupleStructPat> for Pat {
2295 fn from(node: TupleStructPat) -> Pat { 2275 fn from(node: TupleStructPat) -> Pat {
2296 Pat { syntax: node.syntax } 2276 Pat::TupleStructPat(node)
2297 } 2277 }
2298} 2278}
2299impl From<TuplePat> for Pat { 2279impl From<TuplePat> for Pat {
2300 fn from(node: TuplePat) -> Pat { 2280 fn from(node: TuplePat) -> Pat {
2301 Pat { syntax: node.syntax } 2281 Pat::TuplePat(node)
2302 } 2282 }
2303} 2283}
2304impl From<SlicePat> for Pat { 2284impl From<SlicePat> for Pat {
2305 fn from(node: SlicePat) -> Pat { 2285 fn from(node: SlicePat) -> Pat {
2306 Pat { syntax: node.syntax } 2286 Pat::SlicePat(node)
2307 } 2287 }
2308} 2288}
2309impl From<RangePat> for Pat { 2289impl From<RangePat> for Pat {
2310 fn from(node: RangePat) -> Pat { 2290 fn from(node: RangePat) -> Pat {
2311 Pat { syntax: node.syntax } 2291 Pat::RangePat(node)
2312 } 2292 }
2313} 2293}
2314impl From<LiteralPat> for Pat { 2294impl From<LiteralPat> for Pat {
2315 fn from(node: LiteralPat) -> Pat { 2295 fn from(node: LiteralPat) -> Pat {
2316 Pat { syntax: node.syntax } 2296 Pat::LiteralPat(node)
2317 } 2297 }
2318} 2298}
2319impl Pat { 2299impl AstNode for Pat {
2320 pub fn kind(&self) -> PatKind { 2300 fn can_cast(kind: SyntaxKind) -> bool {
2321 let syntax = self.syntax.clone(); 2301 match kind {
2322 match syntax.kind() { 2302 REF_PAT | BIND_PAT | PLACEHOLDER_PAT | PATH_PAT | STRUCT_PAT | TUPLE_STRUCT_PAT
2323 REF_PAT => PatKind::RefPat(RefPat { syntax }), 2303 | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => true,
2324 BIND_PAT => PatKind::BindPat(BindPat { syntax }), 2304 _ => false,
2325 PLACEHOLDER_PAT => PatKind::PlaceholderPat(PlaceholderPat { syntax }), 2305 }
2326 PATH_PAT => PatKind::PathPat(PathPat { syntax }), 2306 }
2327 STRUCT_PAT => PatKind::StructPat(StructPat { syntax }), 2307 fn cast(syntax: SyntaxNode) -> Option<Self> {
2328 TUPLE_STRUCT_PAT => PatKind::TupleStructPat(TupleStructPat { syntax }), 2308 let res = match syntax.kind() {
2329 TUPLE_PAT => PatKind::TuplePat(TuplePat { syntax }), 2309 REF_PAT => Pat::RefPat(RefPat { syntax }),
2330 SLICE_PAT => PatKind::SlicePat(SlicePat { syntax }), 2310 BIND_PAT => Pat::BindPat(BindPat { syntax }),
2331 RANGE_PAT => PatKind::RangePat(RangePat { syntax }), 2311 PLACEHOLDER_PAT => Pat::PlaceholderPat(PlaceholderPat { syntax }),
2332 LITERAL_PAT => PatKind::LiteralPat(LiteralPat { syntax }), 2312 PATH_PAT => Pat::PathPat(PathPat { syntax }),
2333 _ => unreachable!(), 2313 STRUCT_PAT => Pat::StructPat(StructPat { syntax }),
2314 TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }),
2315 TUPLE_PAT => Pat::TuplePat(TuplePat { syntax }),
2316 SLICE_PAT => Pat::SlicePat(SlicePat { syntax }),
2317 RANGE_PAT => Pat::RangePat(RangePat { syntax }),
2318 LITERAL_PAT => Pat::LiteralPat(LiteralPat { syntax }),
2319 _ => return None,
2320 };
2321 Some(res)
2322 }
2323 fn syntax(&self) -> &SyntaxNode {
2324 match self {
2325 Pat::RefPat(it) => &it.syntax,
2326 Pat::BindPat(it) => &it.syntax,
2327 Pat::PlaceholderPat(it) => &it.syntax,
2328 Pat::PathPat(it) => &it.syntax,
2329 Pat::StructPat(it) => &it.syntax,
2330 Pat::TupleStructPat(it) => &it.syntax,
2331 Pat::TuplePat(it) => &it.syntax,
2332 Pat::SlicePat(it) => &it.syntax,
2333 Pat::RangePat(it) => &it.syntax,
2334 Pat::LiteralPat(it) => &it.syntax,
2334 } 2335 }
2335 } 2336 }
2336} 2337}
@@ -2951,48 +2952,39 @@ impl StaticDef {
2951 } 2952 }
2952} 2953}
2953#[derive(Debug, Clone, PartialEq, Eq, Hash)] 2954#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2954pub struct Stmt { 2955pub enum Stmt {
2955 pub(crate) syntax: SyntaxNode,
2956}
2957impl AstNode for Stmt {
2958 fn can_cast(kind: SyntaxKind) -> bool {
2959 match kind {
2960 EXPR_STMT | LET_STMT => true,
2961 _ => false,
2962 }
2963 }
2964 fn cast(syntax: SyntaxNode) -> Option<Self> {
2965 if Self::can_cast(syntax.kind()) {
2966 Some(Self { syntax })
2967 } else {
2968 None
2969 }
2970 }
2971 fn syntax(&self) -> &SyntaxNode {
2972 &self.syntax
2973 }
2974}
2975pub enum StmtKind {
2976 ExprStmt(ExprStmt), 2956 ExprStmt(ExprStmt),
2977 LetStmt(LetStmt), 2957 LetStmt(LetStmt),
2978} 2958}
2979impl From<ExprStmt> for Stmt { 2959impl From<ExprStmt> for Stmt {
2980 fn from(node: ExprStmt) -> Stmt { 2960 fn from(node: ExprStmt) -> Stmt {
2981 Stmt { syntax: node.syntax } 2961 Stmt::ExprStmt(node)
2982 } 2962 }
2983} 2963}
2984impl From<LetStmt> for Stmt { 2964impl From<LetStmt> for Stmt {
2985 fn from(node: LetStmt) -> Stmt { 2965 fn from(node: LetStmt) -> Stmt {
2986 Stmt { syntax: node.syntax } 2966 Stmt::LetStmt(node)
2987 } 2967 }
2988} 2968}
2989impl Stmt { 2969impl AstNode for Stmt {
2990 pub fn kind(&self) -> StmtKind { 2970 fn can_cast(kind: SyntaxKind) -> bool {
2991 let syntax = self.syntax.clone(); 2971 match kind {
2992 match syntax.kind() { 2972 EXPR_STMT | LET_STMT => true,
2993 EXPR_STMT => StmtKind::ExprStmt(ExprStmt { syntax }), 2973 _ => false,
2994 LET_STMT => StmtKind::LetStmt(LetStmt { syntax }), 2974 }
2995 _ => unreachable!(), 2975 }
2976 fn cast(syntax: SyntaxNode) -> Option<Self> {
2977 let res = match syntax.kind() {
2978 EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
2979 LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
2980 _ => return None,
2981 };
2982 Some(res)
2983 }
2984 fn syntax(&self) -> &SyntaxNode {
2985 match self {
2986 Stmt::ExprStmt(it) => &it.syntax,
2987 Stmt::LetStmt(it) => &it.syntax,
2996 } 2988 }
2997 } 2989 }
2998} 2990}
@@ -3508,30 +3500,7 @@ impl TypeParamList {
3508 } 3500 }
3509} 3501}
3510#[derive(Debug, Clone, PartialEq, Eq, Hash)] 3502#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3511pub struct TypeRef { 3503pub enum TypeRef {
3512 pub(crate) syntax: SyntaxNode,
3513}
3514impl AstNode for TypeRef {
3515 fn can_cast(kind: SyntaxKind) -> bool {
3516 match kind {
3517 PAREN_TYPE | TUPLE_TYPE | NEVER_TYPE | PATH_TYPE | POINTER_TYPE | ARRAY_TYPE
3518 | SLICE_TYPE | REFERENCE_TYPE | PLACEHOLDER_TYPE | FN_POINTER_TYPE | FOR_TYPE
3519 | IMPL_TRAIT_TYPE | DYN_TRAIT_TYPE => true,
3520 _ => false,
3521 }
3522 }
3523 fn cast(syntax: SyntaxNode) -> Option<Self> {
3524 if Self::can_cast(syntax.kind()) {
3525 Some(Self { syntax })
3526 } else {
3527 None
3528 }
3529 }
3530 fn syntax(&self) -> &SyntaxNode {
3531 &self.syntax
3532 }
3533}
3534pub enum TypeRefKind {
3535 ParenType(ParenType), 3504 ParenType(ParenType),
3536 TupleType(TupleType), 3505 TupleType(TupleType),
3537 NeverType(NeverType), 3506 NeverType(NeverType),
@@ -3548,87 +3517,112 @@ pub enum TypeRefKind {
3548} 3517}
3549impl From<ParenType> for TypeRef { 3518impl From<ParenType> for TypeRef {
3550 fn from(node: ParenType) -> TypeRef { 3519 fn from(node: ParenType) -> TypeRef {
3551 TypeRef { syntax: node.syntax } 3520 TypeRef::ParenType(node)
3552 } 3521 }
3553} 3522}
3554impl From<TupleType> for TypeRef { 3523impl From<TupleType> for TypeRef {
3555 fn from(node: TupleType) -> TypeRef { 3524 fn from(node: TupleType) -> TypeRef {
3556 TypeRef { syntax: node.syntax } 3525 TypeRef::TupleType(node)
3557 } 3526 }
3558} 3527}
3559impl From<NeverType> for TypeRef { 3528impl From<NeverType> for TypeRef {
3560 fn from(node: NeverType) -> TypeRef { 3529 fn from(node: NeverType) -> TypeRef {
3561 TypeRef { syntax: node.syntax } 3530 TypeRef::NeverType(node)
3562 } 3531 }
3563} 3532}
3564impl From<PathType> for TypeRef { 3533impl From<PathType> for TypeRef {
3565 fn from(node: PathType) -> TypeRef { 3534 fn from(node: PathType) -> TypeRef {
3566 TypeRef { syntax: node.syntax } 3535 TypeRef::PathType(node)
3567 } 3536 }
3568} 3537}
3569impl From<PointerType> for TypeRef { 3538impl From<PointerType> for TypeRef {
3570 fn from(node: PointerType) -> TypeRef { 3539 fn from(node: PointerType) -> TypeRef {
3571 TypeRef { syntax: node.syntax } 3540 TypeRef::PointerType(node)
3572 } 3541 }
3573} 3542}
3574impl From<ArrayType> for TypeRef { 3543impl From<ArrayType> for TypeRef {
3575 fn from(node: ArrayType) -> TypeRef { 3544 fn from(node: ArrayType) -> TypeRef {
3576 TypeRef { syntax: node.syntax } 3545 TypeRef::ArrayType(node)
3577 } 3546 }
3578} 3547}
3579impl From<SliceType> for TypeRef { 3548impl From<SliceType> for TypeRef {
3580 fn from(node: SliceType) -> TypeRef { 3549 fn from(node: SliceType) -> TypeRef {
3581 TypeRef { syntax: node.syntax } 3550 TypeRef::SliceType(node)
3582 } 3551 }
3583} 3552}
3584impl From<ReferenceType> for TypeRef { 3553impl From<ReferenceType> for TypeRef {
3585 fn from(node: ReferenceType) -> TypeRef { 3554 fn from(node: ReferenceType) -> TypeRef {
3586 TypeRef { syntax: node.syntax } 3555 TypeRef::ReferenceType(node)
3587 } 3556 }
3588} 3557}
3589impl From<PlaceholderType> for TypeRef { 3558impl From<PlaceholderType> for TypeRef {
3590 fn from(node: PlaceholderType) -> TypeRef { 3559 fn from(node: PlaceholderType) -> TypeRef {
3591 TypeRef { syntax: node.syntax } 3560 TypeRef::PlaceholderType(node)
3592 } 3561 }
3593} 3562}
3594impl From<FnPointerType> for TypeRef { 3563impl From<FnPointerType> for TypeRef {
3595 fn from(node: FnPointerType) -> TypeRef { 3564 fn from(node: FnPointerType) -> TypeRef {
3596 TypeRef { syntax: node.syntax } 3565 TypeRef::FnPointerType(node)
3597 } 3566 }
3598} 3567}
3599impl From<ForType> for TypeRef { 3568impl From<ForType> for TypeRef {
3600 fn from(node: ForType) -> TypeRef { 3569 fn from(node: ForType) -> TypeRef {
3601 TypeRef { syntax: node.syntax } 3570 TypeRef::ForType(node)
3602 } 3571 }
3603} 3572}
3604impl From<ImplTraitType> for TypeRef { 3573impl From<ImplTraitType> for TypeRef {
3605 fn from(node: ImplTraitType) -> TypeRef { 3574 fn from(node: ImplTraitType) -> TypeRef {
3606 TypeRef { syntax: node.syntax } 3575 TypeRef::ImplTraitType(node)
3607 } 3576 }
3608} 3577}
3609impl From<DynTraitType> for TypeRef { 3578impl From<DynTraitType> for TypeRef {
3610 fn from(node: DynTraitType) -> TypeRef { 3579 fn from(node: DynTraitType) -> TypeRef {
3611 TypeRef { syntax: node.syntax } 3580 TypeRef::DynTraitType(node)
3612 } 3581 }
3613} 3582}
3614impl TypeRef { 3583impl AstNode for TypeRef {
3615 pub fn kind(&self) -> TypeRefKind { 3584 fn can_cast(kind: SyntaxKind) -> bool {
3616 let syntax = self.syntax.clone(); 3585 match kind {
3617 match syntax.kind() { 3586 PAREN_TYPE | TUPLE_TYPE | NEVER_TYPE | PATH_TYPE | POINTER_TYPE | ARRAY_TYPE
3618 PAREN_TYPE => TypeRefKind::ParenType(ParenType { syntax }), 3587 | SLICE_TYPE | REFERENCE_TYPE | PLACEHOLDER_TYPE | FN_POINTER_TYPE | FOR_TYPE
3619 TUPLE_TYPE => TypeRefKind::TupleType(TupleType { syntax }), 3588 | IMPL_TRAIT_TYPE | DYN_TRAIT_TYPE => true,
3620 NEVER_TYPE => TypeRefKind::NeverType(NeverType { syntax }), 3589 _ => false,
3621 PATH_TYPE => TypeRefKind::PathType(PathType { syntax }), 3590 }
3622 POINTER_TYPE => TypeRefKind::PointerType(PointerType { syntax }), 3591 }
3623 ARRAY_TYPE => TypeRefKind::ArrayType(ArrayType { syntax }), 3592 fn cast(syntax: SyntaxNode) -> Option<Self> {
3624 SLICE_TYPE => TypeRefKind::SliceType(SliceType { syntax }), 3593 let res = match syntax.kind() {
3625 REFERENCE_TYPE => TypeRefKind::ReferenceType(ReferenceType { syntax }), 3594 PAREN_TYPE => TypeRef::ParenType(ParenType { syntax }),
3626 PLACEHOLDER_TYPE => TypeRefKind::PlaceholderType(PlaceholderType { syntax }), 3595 TUPLE_TYPE => TypeRef::TupleType(TupleType { syntax }),
3627 FN_POINTER_TYPE => TypeRefKind::FnPointerType(FnPointerType { syntax }), 3596 NEVER_TYPE => TypeRef::NeverType(NeverType { syntax }),
3628 FOR_TYPE => TypeRefKind::ForType(ForType { syntax }), 3597 PATH_TYPE => TypeRef::PathType(PathType { syntax }),
3629 IMPL_TRAIT_TYPE => TypeRefKind::ImplTraitType(ImplTraitType { syntax }), 3598 POINTER_TYPE => TypeRef::PointerType(PointerType { syntax }),
3630 DYN_TRAIT_TYPE => TypeRefKind::DynTraitType(DynTraitType { syntax }), 3599 ARRAY_TYPE => TypeRef::ArrayType(ArrayType { syntax }),
3631 _ => unreachable!(), 3600 SLICE_TYPE => TypeRef::SliceType(SliceType { syntax }),
3601 REFERENCE_TYPE => TypeRef::ReferenceType(ReferenceType { syntax }),
3602 PLACEHOLDER_TYPE => TypeRef::PlaceholderType(PlaceholderType { syntax }),
3603 FN_POINTER_TYPE => TypeRef::FnPointerType(FnPointerType { syntax }),
3604 FOR_TYPE => TypeRef::ForType(ForType { syntax }),
3605 IMPL_TRAIT_TYPE => TypeRef::ImplTraitType(ImplTraitType { syntax }),
3606 DYN_TRAIT_TYPE => TypeRef::DynTraitType(DynTraitType { syntax }),
3607 _ => return None,
3608 };
3609 Some(res)
3610 }
3611 fn syntax(&self) -> &SyntaxNode {
3612 match self {
3613 TypeRef::ParenType(it) => &it.syntax,
3614 TypeRef::TupleType(it) => &it.syntax,
3615 TypeRef::NeverType(it) => &it.syntax,
3616 TypeRef::PathType(it) => &it.syntax,
3617 TypeRef::PointerType(it) => &it.syntax,
3618 TypeRef::ArrayType(it) => &it.syntax,
3619 TypeRef::SliceType(it) => &it.syntax,
3620 TypeRef::ReferenceType(it) => &it.syntax,
3621 TypeRef::PlaceholderType(it) => &it.syntax,
3622 TypeRef::FnPointerType(it) => &it.syntax,
3623 TypeRef::ForType(it) => &it.syntax,
3624 TypeRef::ImplTraitType(it) => &it.syntax,
3625 TypeRef::DynTraitType(it) => &it.syntax,
3632 } 3626 }
3633 } 3627 }
3634} 3628}
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 4c4e0580a..2bced1867 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -186,8 +186,8 @@ fn api_walkthrough() {
186 // Let's fetch the `foo` function. 186 // Let's fetch the `foo` function.
187 let mut func = None; 187 let mut func = None;
188 for item in file.items() { 188 for item in file.items() {
189 match item.kind() { 189 match item {
190 ast::ModuleItemKind::FnDef(f) => func = Some(f), 190 ast::ModuleItem::FnDef(f) => func = Some(f),
191 _ => unreachable!(), 191 _ => unreachable!(),
192 } 192 }
193 } 193 }
@@ -206,12 +206,12 @@ fn api_walkthrough() {
206 let block: ast::Block = func.body().unwrap(); 206 let block: ast::Block = func.body().unwrap();
207 let expr: ast::Expr = block.expr().unwrap(); 207 let expr: ast::Expr = block.expr().unwrap();
208 208
209 // "Enum"-like nodes are represented using the "kind" pattern. It allows us 209 // Enums are used to group related ast nodes together, and can be used for
210 // to match exhaustively against all flavors of nodes, while maintaining 210 // matching. However, because there are no public fields, it's possible to
211 // internal representation flexibility. The drawback is that one can't write 211 // match only the top level enum: that is the price we pay for increased API
212 // nested matches as one pattern. 212 // flexibility
213 let bin_expr: ast::BinExpr = match expr.kind() { 213 let bin_expr: &ast::BinExpr = match &expr {
214 ast::ExprKind::BinExpr(e) => e, 214 ast::Expr::BinExpr(e) => e,
215 _ => unreachable!(), 215 _ => unreachable!(),
216 }; 216 };
217 217
diff --git a/crates/ra_tools/src/boilerplate_gen.rs b/crates/ra_tools/src/boilerplate_gen.rs
index 486a3fdec..01d092d48 100644
--- a/crates/ra_tools/src/boilerplate_gen.rs
+++ b/crates/ra_tools/src/boilerplate_gen.rs
@@ -37,41 +37,72 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
37 ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>(); 37 ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
38 let name = format_ident!("{}", name); 38 let name = format_ident!("{}", name);
39 39
40 let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() } 40 let adt = if variants.is_empty() {
41 .into_iter() 41 let kind = format_ident!("{}", name.to_string().to_shouty_snake_case());
42 .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case())) 42 quote! {
43 .collect::<Vec<_>>(); 43 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
44 pub struct #name {
45 pub(crate) syntax: SyntaxNode,
46 }
44 47
45 let variants = if variants.is_empty() { 48 impl AstNode for #name {
46 None 49 fn can_cast(kind: SyntaxKind) -> bool {
50 match kind {
51 #kind => true,
52 _ => false,
53 }
54 }
55 fn cast(syntax: SyntaxNode) -> Option<Self> {
56 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
57 }
58 fn syntax(&self) -> &SyntaxNode { &self.syntax }
59 }
60 }
47 } else { 61 } else {
48 let kind_enum = format_ident!("{}Kind", name); 62 let kinds = variants
49 Some(quote!( 63 .iter()
50 pub enum #kind_enum { 64 .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
65 .collect::<Vec<_>>();
66
67 quote! {
68 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
69 pub enum #name {
51 #(#variants(#variants),)* 70 #(#variants(#variants),)*
52 } 71 }
53 72
54 #( 73 #(
55 impl From<#variants> for #name { 74 impl From<#variants> for #name {
56 fn from(node: #variants) -> #name { 75 fn from(node: #variants) -> #name {
57 #name { syntax: node.syntax } 76 #name::#variants(node)
58 } 77 }
59 } 78 }
60 )* 79 )*
61 80
62 impl #name { 81 impl AstNode for #name {
63 pub fn kind(&self) -> #kind_enum { 82 fn can_cast(kind: SyntaxKind) -> bool {
64 let syntax = self.syntax.clone(); 83 match kind {
65 match syntax.kind() { 84 #(#kinds)|* => true,
85 _ => false,
86 }
87 }
88 fn cast(syntax: SyntaxNode) -> Option<Self> {
89 let res = match syntax.kind() {
66 #( 90 #(
67 #kinds => 91 #kinds => #name::#variants(#variants { syntax }),
68 #kind_enum::#variants(#variants { syntax }), 92 )*
93 _ => return None,
94 };
95 Some(res)
96 }
97 fn syntax(&self) -> &SyntaxNode {
98 match self {
99 #(
100 #name::#variants(it) => &it.syntax,
69 )* 101 )*
70 _ => unreachable!(),
71 } 102 }
72 } 103 }
73 } 104 }
74 )) 105 }
75 }; 106 };
76 107
77 let traits = ast_node.traits.iter().map(|trait_name| { 108 let traits = ast_node.traits.iter().map(|trait_name| {
@@ -105,25 +136,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
105 }); 136 });
106 137
107 quote! { 138 quote! {
108 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 139 #adt
109 pub struct #name {
110 pub(crate) syntax: SyntaxNode,
111 }
112
113 impl AstNode for #name {
114 fn can_cast(kind: SyntaxKind) -> bool {
115 match kind {
116 #(#kinds)|* => true,
117 _ => false,
118 }
119 }
120 fn cast(syntax: SyntaxNode) -> Option<Self> {
121 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
122 }
123 fn syntax(&self) -> &SyntaxNode { &self.syntax }
124 }
125
126 #variants
127 140
128 #(#traits)* 141 #(#traits)*
129 142