18
18
19
19
#include " rust-expand-visitor.h"
20
20
#include " rust-ast-fragment.h"
21
+ #include " rust-item.h"
21
22
#include " rust-proc-macro.h"
22
23
#include " rust-attributes.h"
23
24
#include " rust-ast.h"
@@ -62,7 +63,7 @@ derive_item (AST::Item &item, AST::SimplePath &to_derive,
62
63
{
63
64
switch (node.get_kind ())
64
65
{
65
- case AST::SingleASTNode::ITEM :
66
+ case AST::SingleASTNode::Kind::Item :
66
67
result.push_back (node.take_item ());
67
68
break ;
68
69
default :
@@ -85,7 +86,7 @@ expand_item_attribute (AST::Item &item, AST::SimplePath &name,
85
86
{
86
87
switch (node.get_kind ())
87
88
{
88
- case AST::SingleASTNode::ITEM :
89
+ case AST::SingleASTNode::Kind::Item :
89
90
result.push_back (node.take_item ());
90
91
break ;
91
92
default :
@@ -114,7 +115,7 @@ expand_stmt_attribute (T &statement, AST::SimplePath &attribute,
114
115
{
115
116
switch (node.get_kind ())
116
117
{
117
- case AST::SingleASTNode::STMT :
118
+ case AST::SingleASTNode::Kind::Stmt :
118
119
result.push_back (node.take_stmt ());
119
120
break ;
120
121
default :
@@ -380,6 +381,23 @@ ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::TypeNoBounds> &type)
380
381
final_fragment.take_type_fragment (), BUILTINS_LOCATION);
381
382
}
382
383
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
+
383
401
// FIXME: Can this be refactored into a `scoped` method? Which takes a
384
402
// ContextType as parameter and a lambda? And maybe just an std::vector<T>&?
385
403
void
@@ -452,6 +470,8 @@ ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> ¶ms)
452
470
{
453
471
for (auto ¶m : params)
454
472
{
473
+ maybe_expand_pattern (param.get_pattern_ptr ());
474
+
455
475
if (param.has_type_given ())
456
476
maybe_expand_type (param.get_type_ptr ());
457
477
}
@@ -729,7 +749,7 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
729
749
auto &arm = match_case.get_arm ();
730
750
731
751
for (auto &pattern : arm.get_patterns ())
732
- visit (pattern);
752
+ maybe_expand_pattern (pattern);
733
753
734
754
if (arm.has_match_arm_guard ())
735
755
maybe_expand_expr (arm.get_guard_expr_ptr ());
@@ -738,6 +758,13 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
738
758
}
739
759
}
740
760
761
+ void
762
+ ExpandVisitor::visit (AST::TupleExpr &expr)
763
+ {
764
+ for (auto &sub : expr.get_tuple_elems ())
765
+ maybe_expand_expr (sub);
766
+ }
767
+
741
768
void
742
769
ExpandVisitor::visit (AST::TypeParam ¶m)
743
770
{
@@ -1013,13 +1040,70 @@ ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
1013
1040
void
1014
1041
ExpandVisitor::visit (AST::GroupedPattern &pattern)
1015
1042
{
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);
1017
1101
}
1018
1102
1019
1103
void
1020
1104
ExpandVisitor::visit (AST::LetStmt &stmt)
1021
1105
{
1022
- visit (stmt.get_pattern ());
1106
+ maybe_expand_pattern (stmt.get_pattern_ptr ());
1023
1107
1024
1108
if (stmt.has_type ())
1025
1109
maybe_expand_type (stmt.get_type_ptr ());
@@ -1049,9 +1133,17 @@ ExpandVisitor::visit (AST::BareFunctionType &type)
1049
1133
void
1050
1134
ExpandVisitor::visit (AST::FunctionParam ¶m)
1051
1135
{
1136
+ maybe_expand_pattern (param.get_pattern_ptr ());
1052
1137
maybe_expand_type (param.get_type_ptr ());
1053
1138
}
1054
1139
1140
+ void
1141
+ ExpandVisitor::visit (AST::VariadicParam ¶m)
1142
+ {
1143
+ if (param.has_pattern ())
1144
+ maybe_expand_pattern (param.get_pattern_ptr ());
1145
+ }
1146
+
1055
1147
void
1056
1148
ExpandVisitor::visit (AST::SelfParam ¶m)
1057
1149
{
0 commit comments