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 :
@@ -352,6 +353,19 @@ ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::Type> &type)
352353 expander.pop_context ();
353354}
354355
356+ void
357+ ExpandVisitor::maybe_expand_pattern (std::unique_ptr<AST ::Pattern> &pattern)
358+ {
359+ expander.push_context (MacroExpander::ContextType::PATTERN );
360+
361+ pattern->accept_vis (*this );
362+ auto final_fragment = expander.take_expanded_fragment ();
363+ if (final_fragment.should_expand () && final_fragment.is_pattern_fragment ())
364+ pattern = final_fragment.take_pattern_fragment ();
365+
366+ expander.pop_context ();
367+ }
368+
355369// FIXME: Can this be refactored into a `scoped` method? Which takes a
356370// ContextType as parameter and a lambda? And maybe just an std::vector<T>&?
357371void
@@ -424,6 +438,8 @@ ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> ¶ms)
424438{
425439 for (auto ¶m : params)
426440 {
441+ maybe_expand_pattern (param.get_pattern_ptr ());
442+
427443 if (param.has_type_given ())
428444 maybe_expand_type (param.get_type_ptr ());
429445 }
@@ -686,7 +702,7 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
686702 auto &arm = match_case.get_arm ();
687703
688704 for (auto &pattern : arm.get_patterns ())
689- visit (pattern);
705+ maybe_expand_pattern (pattern);
690706
691707 if (arm.has_match_arm_guard ())
692708 maybe_expand_expr (arm.get_guard_expr_ptr ());
@@ -695,6 +711,13 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
695711 }
696712}
697713
714+ void
715+ ExpandVisitor::visit (AST ::TupleExpr &expr)
716+ {
717+ for (auto &sub : expr.get_tuple_elems ())
718+ maybe_expand_expr (sub);
719+ }
720+
698721void
699722ExpandVisitor::visit (AST ::TypeParam ¶m)
700723{
@@ -970,13 +993,70 @@ ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
970993void
971994ExpandVisitor::visit (AST ::GroupedPattern &pattern)
972995{
973- visit (pattern.get_pattern_in_parens ());
996+ maybe_expand_pattern (pattern.get_pattern_in_parens_ptr ());
997+ }
998+
999+ void
1000+ ExpandVisitor::visit (AST ::SlicePatternItemsNoRest &items)
1001+ {
1002+ for (auto &sub : items.get_patterns ())
1003+ maybe_expand_pattern (sub);
1004+ }
1005+
1006+ void
1007+ ExpandVisitor::visit (AST ::SlicePatternItemsHasRest &items)
1008+ {
1009+ for (auto &sub : items.get_lower_patterns ())
1010+ maybe_expand_pattern (sub);
1011+ for (auto &sub : items.get_upper_patterns ())
1012+ maybe_expand_pattern (sub);
1013+ }
1014+
1015+ void
1016+ ExpandVisitor::visit (AST ::AltPattern &pattern)
1017+ {
1018+ for (auto &alt : pattern.get_alts ())
1019+ maybe_expand_pattern (alt);
1020+ }
1021+
1022+ void
1023+ ExpandVisitor::visit (AST ::TupleStructItemsNoRange &tuple_items)
1024+ {
1025+ for (auto &sub : tuple_items.get_patterns ())
1026+ maybe_expand_pattern (sub);
1027+ }
1028+
1029+ void
1030+ ExpandVisitor::visit (AST ::TupleStructItemsRange &tuple_items)
1031+ {
1032+ for (auto &sub : tuple_items.get_lower_patterns ())
1033+ maybe_expand_pattern (sub);
1034+
1035+ for (auto &sub : tuple_items.get_upper_patterns ())
1036+ maybe_expand_pattern (sub);
1037+ }
1038+
1039+ void
1040+ ExpandVisitor::visit (AST ::TuplePatternItemsMultiple &tuple_items)
1041+ {
1042+ for (auto &sub : tuple_items.get_patterns ())
1043+ maybe_expand_pattern (sub);
1044+ }
1045+
1046+ void
1047+ ExpandVisitor::visit (AST ::TuplePatternItemsRanged &tuple_items)
1048+ {
1049+ for (auto &sub : tuple_items.get_lower_patterns ())
1050+ maybe_expand_pattern (sub);
1051+
1052+ for (auto &sub : tuple_items.get_upper_patterns ())
1053+ maybe_expand_pattern (sub);
9741054}
9751055
9761056void
9771057ExpandVisitor::visit (AST ::LetStmt &stmt)
9781058{
979- visit (stmt.get_pattern ());
1059+ maybe_expand_pattern (stmt.get_pattern_ptr ());
9801060
9811061 if (stmt.has_type ())
9821062 maybe_expand_type (stmt.get_type_ptr ());
@@ -1006,9 +1086,17 @@ ExpandVisitor::visit (AST::BareFunctionType &type)
10061086void
10071087ExpandVisitor::visit (AST ::FunctionParam ¶m)
10081088{
1089+ maybe_expand_pattern (param.get_pattern_ptr ());
10091090 maybe_expand_type (param.get_type_ptr ());
10101091}
10111092
1093+ void
1094+ ExpandVisitor::visit (AST ::VariadicParam ¶m)
1095+ {
1096+ if (param.has_pattern ())
1097+ maybe_expand_pattern (param.get_pattern_ptr ());
1098+ }
1099+
10121100void
10131101ExpandVisitor::visit (AST ::SelfParam ¶m)
10141102{
0 commit comments