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,19 @@ 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+ expander.push_context (MacroExpander::ContextType::PATTERN );
388+
389+ pattern->accept_vis (*this );
390+ auto final_fragment = expander.take_expanded_fragment ();
391+ if (final_fragment.should_expand () && final_fragment.is_pattern_fragment ())
392+ pattern = final_fragment.take_pattern_fragment ();
393+
394+ expander.pop_context ();
395+ }
396+
383397// FIXME: Can this be refactored into a `scoped` method? Which takes a
384398// ContextType as parameter and a lambda? And maybe just an std::vector<T>&?
385399void
@@ -452,6 +466,8 @@ ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> ¶ms)
452466{
453467 for (auto ¶m : params)
454468 {
469+ maybe_expand_pattern (param.get_pattern_ptr ());
470+
455471 if (param.has_type_given ())
456472 maybe_expand_type (param.get_type_ptr ());
457473 }
@@ -729,7 +745,7 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
729745 auto &arm = match_case.get_arm ();
730746
731747 for (auto &pattern : arm.get_patterns ())
732- visit (pattern);
748+ maybe_expand_pattern (pattern);
733749
734750 if (arm.has_match_arm_guard ())
735751 maybe_expand_expr (arm.get_guard_expr_ptr ());
@@ -738,6 +754,13 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
738754 }
739755}
740756
757+ void
758+ ExpandVisitor::visit (AST ::TupleExpr &expr)
759+ {
760+ for (auto &sub : expr.get_tuple_elems ())
761+ maybe_expand_expr (sub);
762+ }
763+
741764void
742765ExpandVisitor::visit (AST ::TypeParam ¶m)
743766{
@@ -1013,13 +1036,70 @@ ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
10131036void
10141037ExpandVisitor::visit (AST ::GroupedPattern &pattern)
10151038{
1016- visit (pattern.get_pattern_in_parens ());
1039+ maybe_expand_pattern (pattern.get_pattern_in_parens_ptr ());
1040+ }
1041+
1042+ void
1043+ ExpandVisitor::visit (AST ::SlicePatternItemsNoRest &items)
1044+ {
1045+ for (auto &sub : items.get_patterns ())
1046+ maybe_expand_pattern (sub);
1047+ }
1048+
1049+ void
1050+ ExpandVisitor::visit (AST ::SlicePatternItemsHasRest &items)
1051+ {
1052+ for (auto &sub : items.get_lower_patterns ())
1053+ maybe_expand_pattern (sub);
1054+ for (auto &sub : items.get_upper_patterns ())
1055+ maybe_expand_pattern (sub);
1056+ }
1057+
1058+ void
1059+ ExpandVisitor::visit (AST ::AltPattern &pattern)
1060+ {
1061+ for (auto &alt : pattern.get_alts ())
1062+ maybe_expand_pattern (alt);
1063+ }
1064+
1065+ void
1066+ ExpandVisitor::visit (AST ::TupleStructItemsNoRange &tuple_items)
1067+ {
1068+ for (auto &sub : tuple_items.get_patterns ())
1069+ maybe_expand_pattern (sub);
1070+ }
1071+
1072+ void
1073+ ExpandVisitor::visit (AST ::TupleStructItemsRange &tuple_items)
1074+ {
1075+ for (auto &sub : tuple_items.get_lower_patterns ())
1076+ maybe_expand_pattern (sub);
1077+
1078+ for (auto &sub : tuple_items.get_upper_patterns ())
1079+ maybe_expand_pattern (sub);
1080+ }
1081+
1082+ void
1083+ ExpandVisitor::visit (AST ::TuplePatternItemsMultiple &tuple_items)
1084+ {
1085+ for (auto &sub : tuple_items.get_patterns ())
1086+ maybe_expand_pattern (sub);
1087+ }
1088+
1089+ void
1090+ ExpandVisitor::visit (AST ::TuplePatternItemsRanged &tuple_items)
1091+ {
1092+ for (auto &sub : tuple_items.get_lower_patterns ())
1093+ maybe_expand_pattern (sub);
1094+
1095+ for (auto &sub : tuple_items.get_upper_patterns ())
1096+ maybe_expand_pattern (sub);
10171097}
10181098
10191099void
10201100ExpandVisitor::visit (AST ::LetStmt &stmt)
10211101{
1022- visit (stmt.get_pattern ());
1102+ maybe_expand_pattern (stmt.get_pattern_ptr ());
10231103
10241104 if (stmt.has_type ())
10251105 maybe_expand_type (stmt.get_type_ptr ());
@@ -1049,9 +1129,17 @@ ExpandVisitor::visit (AST::BareFunctionType &type)
10491129void
10501130ExpandVisitor::visit (AST ::FunctionParam ¶m)
10511131{
1132+ maybe_expand_pattern (param.get_pattern_ptr ());
10521133 maybe_expand_type (param.get_type_ptr ());
10531134}
10541135
1136+ void
1137+ ExpandVisitor::visit (AST ::VariadicParam ¶m)
1138+ {
1139+ if (param.has_pattern ())
1140+ maybe_expand_pattern (param.get_pattern_ptr ());
1141+ }
1142+
10551143void
10561144ExpandVisitor::visit (AST ::SelfParam ¶m)
10571145{
0 commit comments