1818
1919#include " rust-expand-visitor.h"
2020#include " rust-ast-fragment.h"
21+ #include " rust-item.h"
2122#include " rust-proc-macro.h"
2223#include " rust-attributes.h"
2324#include " rust-ast.h"
@@ -62,7 +63,7 @@ derive_item (AST::Item &item, AST::SimplePath &to_derive,
6263 {
6364 switch (node.get_kind ())
6465 {
65- case AST::SingleASTNode::ITEM :
66+ case AST::SingleASTNode::Kind::Item :
6667 result.push_back (node.take_item ());
6768 break ;
6869 default :
@@ -85,7 +86,7 @@ expand_item_attribute (AST::Item &item, AST::SimplePath &name,
8586 {
8687 switch (node.get_kind ())
8788 {
88- case AST::SingleASTNode::ITEM :
89+ case AST::SingleASTNode::Kind::Item :
8990 result.push_back (node.take_item ());
9091 break ;
9192 default :
@@ -114,7 +115,7 @@ expand_stmt_attribute (T &statement, AST::SimplePath &attribute,
114115 {
115116 switch (node.get_kind ())
116117 {
117- case AST::SingleASTNode::STMT :
118+ case AST::SingleASTNode::Kind::Stmt :
118119 result.push_back (node.take_stmt ());
119120 break ;
120121 default :
@@ -380,6 +381,23 @@ ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::TypeNoBounds> &type)
380381 final_fragment.take_type_fragment (), BUILTINS_LOCATION);
381382}
382383
384+ void
385+ ExpandVisitor::maybe_expand_pattern (std::unique_ptr<AST::Pattern> &pattern)
386+ {
387+ NodeId old_expect = pattern->get_node_id ();
388+ std::swap (macro_invoc_expect_id, old_expect);
389+
390+ expander.push_context (MacroExpander::ContextType::PATTERN);
391+ pattern->accept_vis (*this );
392+ expander.pop_context ();
393+
394+ std::swap (macro_invoc_expect_id, old_expect);
395+
396+ auto final_fragment = expander.take_expanded_fragment ();
397+ if (final_fragment.should_expand () && final_fragment.is_pattern_fragment ())
398+ pattern = final_fragment.take_pattern_fragment ();
399+ }
400+
383401// FIXME: Can this be refactored into a `scoped` method? Which takes a
384402// ContextType as parameter and a lambda? And maybe just an std::vector<T>&?
385403void
@@ -452,6 +470,8 @@ ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> ¶ms)
452470{
453471 for (auto ¶m : params)
454472 {
473+ maybe_expand_pattern (param.get_pattern_ptr ());
474+
455475 if (param.has_type_given ())
456476 maybe_expand_type (param.get_type_ptr ());
457477 }
@@ -729,7 +749,7 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
729749 auto &arm = match_case.get_arm ();
730750
731751 for (auto &pattern : arm.get_patterns ())
732- visit (pattern);
752+ maybe_expand_pattern (pattern);
733753
734754 if (arm.has_match_arm_guard ())
735755 maybe_expand_expr (arm.get_guard_expr_ptr ());
@@ -738,6 +758,13 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
738758 }
739759}
740760
761+ void
762+ ExpandVisitor::visit (AST::TupleExpr &expr)
763+ {
764+ for (auto &sub : expr.get_tuple_elems ())
765+ maybe_expand_expr (sub);
766+ }
767+
741768void
742769ExpandVisitor::visit (AST::TypeParam ¶m)
743770{
@@ -1013,13 +1040,70 @@ ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
10131040void
10141041ExpandVisitor::visit (AST::GroupedPattern &pattern)
10151042{
1016- visit (pattern.get_pattern_in_parens ());
1043+ maybe_expand_pattern (pattern.get_pattern_in_parens_ptr ());
1044+ }
1045+
1046+ void
1047+ ExpandVisitor::visit (AST::SlicePatternItemsNoRest &items)
1048+ {
1049+ for (auto &sub : items.get_patterns ())
1050+ maybe_expand_pattern (sub);
1051+ }
1052+
1053+ void
1054+ ExpandVisitor::visit (AST::SlicePatternItemsHasRest &items)
1055+ {
1056+ for (auto &sub : items.get_lower_patterns ())
1057+ maybe_expand_pattern (sub);
1058+ for (auto &sub : items.get_upper_patterns ())
1059+ maybe_expand_pattern (sub);
1060+ }
1061+
1062+ void
1063+ ExpandVisitor::visit (AST::AltPattern &pattern)
1064+ {
1065+ for (auto &alt : pattern.get_alts ())
1066+ maybe_expand_pattern (alt);
1067+ }
1068+
1069+ void
1070+ ExpandVisitor::visit (AST::TupleStructItemsNoRange &tuple_items)
1071+ {
1072+ for (auto &sub : tuple_items.get_patterns ())
1073+ maybe_expand_pattern (sub);
1074+ }
1075+
1076+ void
1077+ ExpandVisitor::visit (AST::TupleStructItemsRange &tuple_items)
1078+ {
1079+ for (auto &sub : tuple_items.get_lower_patterns ())
1080+ maybe_expand_pattern (sub);
1081+
1082+ for (auto &sub : tuple_items.get_upper_patterns ())
1083+ maybe_expand_pattern (sub);
1084+ }
1085+
1086+ void
1087+ ExpandVisitor::visit (AST::TuplePatternItemsMultiple &tuple_items)
1088+ {
1089+ for (auto &sub : tuple_items.get_patterns ())
1090+ maybe_expand_pattern (sub);
1091+ }
1092+
1093+ void
1094+ ExpandVisitor::visit (AST::TuplePatternItemsRanged &tuple_items)
1095+ {
1096+ for (auto &sub : tuple_items.get_lower_patterns ())
1097+ maybe_expand_pattern (sub);
1098+
1099+ for (auto &sub : tuple_items.get_upper_patterns ())
1100+ maybe_expand_pattern (sub);
10171101}
10181102
10191103void
10201104ExpandVisitor::visit (AST::LetStmt &stmt)
10211105{
1022- visit (stmt.get_pattern ());
1106+ maybe_expand_pattern (stmt.get_pattern_ptr ());
10231107
10241108 if (stmt.has_type ())
10251109 maybe_expand_type (stmt.get_type_ptr ());
@@ -1049,9 +1133,17 @@ ExpandVisitor::visit (AST::BareFunctionType &type)
10491133void
10501134ExpandVisitor::visit (AST::FunctionParam ¶m)
10511135{
1136+ maybe_expand_pattern (param.get_pattern_ptr ());
10521137 maybe_expand_type (param.get_type_ptr ());
10531138}
10541139
1140+ void
1141+ ExpandVisitor::visit (AST::VariadicParam ¶m)
1142+ {
1143+ if (param.has_pattern ())
1144+ maybe_expand_pattern (param.get_pattern_ptr ());
1145+ }
1146+
10551147void
10561148ExpandVisitor::visit (AST::SelfParam ¶m)
10571149{
0 commit comments