Debian Codegraph @

gcc/rust/expand/rust-expand-visitor.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include "rust-expand-visitor.h"
20 #include "rust-ast-fragment.h"
21 #include "rust-proc-macro.h"
22 #include "rust-attributes.h"
23 #include "rust-ast.h"
24 #include "rust-type.h"
25 #include "rust-derive.h"
26
27 namespace Rust {
28
29 bool
30 is_builtin (AST::Attribute &attr)
31 {
32 auto &segments = attr.get_path ().get_segments ();
33 return !segments.empty ()
34 && !Analysis::BuiltinAttributeMappings::get ()
35 ->lookup_builtin (segments[0].get_segment_name ())
36 .is_error ();
37 }
38
39 /* Expand all of the macro invocations currently contained in a crate */
40 void
41 ExpandVisitor::go (AST::Crate &crate)
42 {
43 visit (crate);
44 }
45
46 static std::vector<std::unique_ptr<AST::Item>>
47 builtin_derive_item (AST::Item &item, const AST::Attribute &derive,
48 BuiltinMacro to_derive)
49 {
50 return AST::DeriveVisitor::derive (item, derive, to_derive);
51 }
52
53 static std::vector<std::unique_ptr<AST::Item>>
54 derive_item (AST::Item &item, AST::SimplePath &to_derive,
55 MacroExpander &expander)
56 {
57 std::vector<std::unique_ptr<AST::Item>> result;
58 auto frag = expander.expand_derive_proc_macro (item, to_derive);
59 if (!frag.is_error ())
60 {
61 for (auto &node : frag.get_nodes ())
62 {
63 switch (node.get_kind ())
64 {
65 case AST::SingleASTNode::ITEM:
66 result.push_back (node.take_item ());
67 break;
68 default:
69 rust_unreachable ();
70 }
71 }
72 }
73 return result;
74 }
75
76 static std::vector<std::unique_ptr<AST::Item>>
77 expand_item_attribute (AST::Item &item, AST::SimplePath &name,
78 MacroExpander &expander)
79 {
80 std::vector<std::unique_ptr<AST::Item>> result;
81 auto frag = expander.expand_attribute_proc_macro (item, name);
82 if (!frag.is_error ())
83 {
84 for (auto &node : frag.get_nodes ())
85 {
86 switch (node.get_kind ())
87 {
88 case AST::SingleASTNode::ITEM:
89 result.push_back (node.take_item ());
90 break;
91 default:
92 rust_unreachable ();
93 }
94 }
95 }
96 return result;
97 }
98
99 /* Helper function to expand a given attribute on a statement and collect back
100 * statements.
101 * T should be anything that can be used as a statement accepting outer
102 * attributes.
103 */
104 template <typename T>
105 static std::vector<std::unique_ptr<AST::Stmt>>
106 expand_stmt_attribute (T &statement, AST::SimplePath &attribute,
107 MacroExpander &expander)
108 {
109 std::vector<std::unique_ptr<AST::Stmt>> result;
110 auto frag = expander.expand_attribute_proc_macro (statement, attribute);
111 if (!frag.is_error ())
112 {
113 for (auto &node : frag.get_nodes ())
114 {
115 switch (node.get_kind ())
116 {
117 case AST::SingleASTNode::STMT:
118 result.push_back (node.take_stmt ());
119 break;
120 default:
121 rust_unreachable ();
122 }
123 }
124 }
125 return result;
126 }
127
128 void
129 expand_tail_expr (AST::BlockExpr &block_expr, MacroExpander &expander)
130 {
131 if (block_expr.has_tail_expr ())
132 {
133 auto tail = block_expr.take_tail_expr ();
134 auto attrs = tail->get_outer_attrs ();
135 bool changed = false;
136 for (auto it = attrs.begin (); it != attrs.end ();)
137 {
138 auto current = *it;
139 if (is_builtin (current))
140 {
141 it++;
142 }
143 else
144 {
145 it = attrs.erase (it);
146 changed = true;
147 auto new_stmts
148 = expand_stmt_attribute (block_expr, current.get_path (),
149 expander);
150 auto &stmts = block_expr.get_statements ();
151 std::move (new_stmts.begin (), new_stmts.end (),
152 std::inserter (stmts, stmts.end ()));
153 }
154 }
155 if (changed)
156 block_expr.normalize_tail_expr ();
157 else
158 block_expr.set_tail_expr (std::move (tail));
159 }
160 }
161
162 void
163 ExpandVisitor::expand_inner_items (
164 std::vector<std::unique_ptr<AST::Item>> &items)
165 {
166 expander.push_context (MacroExpander::ContextType::ITEM);
167
168 for (auto it = items.begin (); it != items.end (); it++)
169 {
170 Rust::AST::Item &item = **it;
171 if (item.has_outer_attrs ())
172 {
173 auto &attrs = item.get_outer_attrs ();
174
175 for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
176 /* erase => No increment*/)
177 {
178 auto current = *attr_it;
179
180 if (current.is_derive ())
181 {
182 current.parse_attr_to_meta_item ();
183 attr_it = attrs.erase (attr_it);
184 // Get traits to derive in the current attribute
185 auto traits_to_derive = current.get_traits_to_derive ();
186 for (auto &to_derive : traits_to_derive)
187 {
188 auto maybe_builtin = MacroBuiltin::builtins.lookup (
189 to_derive.get ().as_string ());
190 if (maybe_builtin.has_value ())
191 {
192 auto new_items
193 = builtin_derive_item (item, current,
194 maybe_builtin.value ());
195
196 for (auto &&new_item : new_items)
197 it = items.insert (it, std::move (new_item));
198 }
199 else
200 {
201 // Macro is not a builtin, so it must be a
202 // user-defined derive macro.
203 auto new_items
204 = derive_item (item, to_derive, expander);
205 std::move (new_items.begin (), new_items.end (),
206 std::inserter (items, it));
207 }
208 }
209 }
210 else /* Attribute */
211 {
212 if (is_builtin (current))
213 {
214 attr_it++;
215 }
216 else
217 {
218 attr_it = attrs.erase (attr_it);
219 auto new_items
220 = expand_item_attribute (item, current.get_path (),
221 expander);
222 it = items.erase (it);
223 std::move (new_items.begin (), new_items.end (),
224 std::inserter (items, it));
225 // TODO: Improve this ?
226 // item is invalid since it refers to now deleted,
227 // cancel the loop increment and break.
228 it--;
229 break;
230 }
231 }
232 }
233 }
234 }
235
236 std::function<std::unique_ptr<AST::Item> (AST::SingleASTNode)> extractor
237 = [] (AST::SingleASTNode node) { return node.take_item (); };
238
239 expand_macro_children (items, extractor);
240
241 expander.pop_context ();
242 }
243
244 void
245 ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
246 {
247 auto &stmts = expr.get_statements ();
248 expander.push_context (MacroExpander::ContextType::STMT);
249
250 for (auto it = stmts.begin (); it != stmts.end (); it++)
251 {
252 auto &stmt = *it;
253
254 // skip all non-item statements
255 if (stmt->get_stmt_kind () != AST::Stmt::Kind::Item)
256 continue;
257
258 auto &item = static_cast<AST::Item &> (*stmt.get ());
259
260 if (item.has_outer_attrs ())
261 {
262 auto &attrs = item.get_outer_attrs ();
263
264 for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
265 /* erase => No increment*/)
266 {
267 auto current = *attr_it;
268
269 if (current.is_derive ())
270 {
271 attr_it = attrs.erase (attr_it);
272 // Get traits to derive in the current attribute
273 auto traits_to_derive = current.get_traits_to_derive ();
274 for (auto &to_derive : traits_to_derive)
275 {
276 auto maybe_builtin = MacroBuiltin::builtins.lookup (
277 to_derive.get ().as_string ());
278 if (maybe_builtin.has_value ())
279 {
280 auto new_items
281 = builtin_derive_item (item, current,
282 maybe_builtin.value ());
283
284 // this inserts the derive *before* the item - is it a
285 // problem?
286 for (auto &&new_item : new_items)
287 it = stmts.insert (it, std::move (new_item));
288 }
289 else
290 {
291 auto new_items
292 = derive_item (item, to_derive, expander);
293 std::move (new_items.begin (), new_items.end (),
294 std::inserter (stmts, it));
295 }
296 }
297 }
298 else /* Attribute */
299 {
300 if (is_builtin (current))
301 {
302 attr_it++;
303 }
304 else
305 {
306 attr_it = attrs.erase (attr_it);
307 auto new_items
308 = expand_stmt_attribute (item, current.get_path (),
309 expander);
310 it = stmts.erase (it);
311 std::move (new_items.begin (), new_items.end (),
312 std::inserter (stmts, it));
313 // TODO: Improve this ?
314 // item is invalid since it refers to now deleted,
315 // cancel the loop increment and break.
316 it--;
317 break;
318 }
319 }
320 }
321 }
322 }
323
324 if (!expr.has_tail_expr ())
325 expr.normalize_tail_expr ();
326
327 std::function<std::unique_ptr<AST::Stmt> (AST::SingleASTNode)> extractor
328 = [] (AST::SingleASTNode node) { return node.take_stmt (); };
329
330 expand_macro_children (stmts, extractor);
331
332 expander.pop_context ();
333 }
334
335 void
336 ExpandVisitor::maybe_expand_expr (std::unique_ptr<AST::Expr> &expr)
337 {
338 expander.push_context (MacroExpander::ContextType::EXPR);
339 expr->accept_vis (*this);
340 expander.pop_context ();
341
342 auto final_fragment = expander.take_expanded_fragment ();
343 if (final_fragment.should_expand ()
344 && final_fragment.is_expression_fragment ())
345 expr = final_fragment.take_expression_fragment ();
346 }
347
348 void
349 ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::Type> &type)
350 {
351 expander.push_context (MacroExpander::ContextType::TYPE);
352
353 type->accept_vis (*this);
354 auto final_fragment = expander.take_expanded_fragment ();
355 if (final_fragment.should_expand () && final_fragment.is_type_fragment ())
356 type = final_fragment.take_type_fragment ();
357
358 expander.pop_context ();
359 }
360
361 // FIXME: Can this be refactored into a `scoped` method? Which takes a
362 // ContextType as parameter and a lambda? And maybe just an std::vector<T>&?
363 void
364 ExpandVisitor::expand_struct_fields (std::vector<AST::StructField> &fields)
365 {
366 for (auto &field : fields)
367 {
368 maybe_expand_type (field.get_field_type_ptr ());
369 }
370 }
371
372 void
373 ExpandVisitor::expand_tuple_fields (std::vector<AST::TupleField> &fields)
374 {
375 for (auto &field : fields)
376 maybe_expand_type (field.get_field_type_ptr ());
377 }
378
379 // FIXME: This can definitely be refactored with the method above
380 void
381 ExpandVisitor::expand_function_params (
382 std::vector<std::unique_ptr<AST::Param>> &params)
383 {
384 for (auto &p : params)
385 visit (p);
386 }
387
388 void
389 ExpandVisitor::expand_generic_args (AST::GenericArgs &args)
390 {
391 for (auto &arg : args.get_generic_args ())
392 {
393 switch (arg.get_kind ())
394 {
395 case AST::GenericArg::Kind::Type:
396 maybe_expand_type (arg.get_type_ptr ());
397 break;
398 case AST::GenericArg::Kind::Const:
399 maybe_expand_expr (arg.get_expression_ptr ());
400 break;
401 default:
402 break;
403 // FIXME: Figure out what to do here if there is ambiguity. Since the
404 // resolver comes after the expansion, we need to figure out a way to
405 // strip ambiguous values here
406 // TODO: ARTHUR: Probably add a `mark_as_strip` method to `GenericArg`
407 // or something. This would clean up this whole thing
408 }
409 }
410
411 // FIXME: Can we have macro invocations in generic type bindings?
412 // expand binding args - strip sub-types only
413 // FIXME: ARTHUR: This needs a test! Foo<Item = macro!()>
414 for (auto &binding : args.get_binding_args ())
415 maybe_expand_type (binding.get_type_ptr ());
416 }
417
418 void
419 ExpandVisitor::expand_qualified_path_type (AST::QualifiedPathType &path_type)
420 {
421 maybe_expand_type (path_type.get_type_ptr ());
422
423 // FIXME: ARTHUR: Can we do macro expansion in there? Needs a test!
424 if (path_type.has_as_clause ())
425 path_type.get_as_type_path ().accept_vis (*this);
426 }
427
428 void
429 ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> &params)
430 {
431 for (auto &param : params)
432 {
433 if (param.has_type_given ())
434 maybe_expand_type (param.get_type_ptr ());
435 }
436 }
437
438 void
439 ExpandVisitor::expand_where_clause (AST::WhereClause &where_clause)
440 {
441 for (auto &item : where_clause.get_items ())
442 visit (item);
443 }
444
445 void
446 ExpandVisitor::visit (AST::Crate &crate)
447 {
448 expand_inner_items (crate.items);
449 }
450
451 void
452 ExpandVisitor::visit (AST::DelimTokenTree &)
453 {}
454
455 void
456 ExpandVisitor::visit (AST::AttrInputMetaItemContainer &)
457 {}
458
459 void
460 ExpandVisitor::visit (AST::IdentifierExpr &ident_expr)
461 {}
462
463 void
464 ExpandVisitor::visit (AST::LifetimeParam &)
465 {}
466
467 void
468 ExpandVisitor::visit (AST::ConstGenericParam &)
469 {}
470
471 void
472 ExpandVisitor::visit (AST::MacroInvocation &macro_invoc)
473 {
474 // TODO: Can we do the AST fragment replacing here? Probably not, right?
475 expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ()
476 ? AST::InvocKind::Semicoloned
477 : AST::InvocKind::Expr);
478 }
479
480 void
481 ExpandVisitor::visit (AST::PathInExpression &path)
482 {
483 if (!path.is_lang_item ())
484 for (auto &segment : path.get_segments ())
485 if (segment.has_generic_args ())
486 expand_generic_args (segment.get_generic_args ());
487 }
488
489 void
490 ExpandVisitor::visit (AST::TypePathSegmentGeneric &segment)
491 {
492 expand_generic_args (segment.get_generic_args ());
493 }
494
495 void
496 ExpandVisitor::visit (AST::TypePathSegmentFunction &segment)
497 {
498 auto &type_path_function = segment.get_type_path_function ();
499
500 for (auto &type : type_path_function.get_params ())
501 visit (type);
502
503 if (type_path_function.has_return_type ())
504 maybe_expand_type (type_path_function.get_return_type_ptr ());
505 }
506
507 void
508 ExpandVisitor::visit (AST::QualifiedPathInExpression &path)
509 {
510 expand_qualified_path_type (path.get_qualified_path_type ());
511
512 for (auto &segment : path.get_segments ())
513 if (segment.has_generic_args ())
514 expand_generic_args (segment.get_generic_args ());
515 }
516
517 void
518 ExpandVisitor::visit (AST::QualifiedPathInType &path)
519 {
520 expand_qualified_path_type (path.get_qualified_path_type ());
521
522 // this shouldn't strip any segments, but can strip inside them
523 for (auto &segment : path.get_segments ())
524 visit (segment);
525 }
526
527 void
528 ExpandVisitor::visit (AST::LiteralExpr &expr)
529 {}
530
531 void
532 ExpandVisitor::visit (AST::AttrInputLiteral &)
533 {}
534
535 void
536 ExpandVisitor::visit (AST::AttrInputMacro &macro)
537 {
538 rust_sorry_at (UNDEF_LOCATION, "macros in attributes not supported");
539 }
540
541 void
542 ExpandVisitor::visit (AST::MetaItemLitExpr &)
543 {}
544
545 void
546 ExpandVisitor::visit (AST::MetaItemPathLit &)
547 {}
548
549 void
550 ExpandVisitor::visit (AST::ErrorPropagationExpr &expr)
551 {
552 visit (expr.get_propagating_expr ());
553 }
554
555 void
556 ExpandVisitor::visit (AST::ArithmeticOrLogicalExpr &expr)
557 {
558 maybe_expand_expr (expr.get_left_expr_ptr ());
559 maybe_expand_expr (expr.get_right_expr_ptr ());
560 }
561
562 void
563 ExpandVisitor::visit (AST::ComparisonExpr &expr)
564 {
565 maybe_expand_expr (expr.get_left_expr_ptr ());
566 maybe_expand_expr (expr.get_right_expr_ptr ());
567 }
568
569 void
570 ExpandVisitor::visit (AST::LazyBooleanExpr &expr)
571 {
572 maybe_expand_expr (expr.get_left_expr_ptr ());
573 maybe_expand_expr (expr.get_right_expr_ptr ());
574 }
575
576 void
577 ExpandVisitor::visit (AST::AssignmentExpr &expr)
578 {
579 maybe_expand_expr (expr.get_left_expr_ptr ());
580 maybe_expand_expr (expr.get_right_expr_ptr ());
581 }
582
583 void
584 ExpandVisitor::visit (AST::CompoundAssignmentExpr &expr)
585 {
586 maybe_expand_expr (expr.get_left_expr_ptr ());
587 maybe_expand_expr (expr.get_right_expr_ptr ());
588 }
589
590 void
591 ExpandVisitor::visit (AST::GroupedExpr &expr)
592 {
593 maybe_expand_expr (expr.get_expr_in_parens_ptr ());
594 }
595
596 void
597 ExpandVisitor::visit (AST::StructExprStruct &expr)
598 {}
599
600 void
601 ExpandVisitor::visit (AST::CallExpr &expr)
602 {
603 visit (expr.get_function_expr ());
604
605 for (auto &param : expr.get_params ())
606 maybe_expand_expr (param);
607 }
608
609 void
610 ExpandVisitor::visit (AST::MethodCallExpr &expr)
611 {
612 visit (expr.get_receiver_expr ());
613
614 for (auto &param : expr.get_params ())
615 maybe_expand_expr (param);
616 }
617
618 void
619 ExpandVisitor::visit (AST::ClosureExprInner &expr)
620 {
621 expand_closure_params (expr.get_params ());
622
623 visit (expr.get_definition_expr ());
624 }
625
626 void
627 ExpandVisitor::visit (AST::BlockExpr &expr)
628 {
629 expand_inner_stmts (expr);
630
631 expand_tail_expr (expr, expander);
632 if (expr.has_tail_expr ())
633 maybe_expand_expr (expr.get_tail_expr_ptr ());
634 }
635
636 void
637 ExpandVisitor::visit (AST::ClosureExprInnerTyped &expr)
638 {
639 expand_closure_params (expr.get_params ());
640
641 maybe_expand_type (expr.get_return_type_ptr ());
642
643 visit (expr.get_definition_block ());
644 }
645
646 void
647 ExpandVisitor::visit (AST::ContinueExpr &expr)
648 {}
649
650 void
651 ExpandVisitor::visit (AST::IfExpr &expr)
652 {
653 maybe_expand_expr (expr.get_condition_expr_ptr ());
654
655 visit (expr.get_if_block ());
656 }
657
658 void
659 ExpandVisitor::visit (AST::IfExprConseqElse &expr)
660 {
661 maybe_expand_expr (expr.get_condition_expr_ptr ());
662
663 visit (expr.get_if_block ());
664 visit (expr.get_else_block ());
665 }
666
667 void
668 ExpandVisitor::visit (AST::IfLetExpr &expr)
669 {
670 maybe_expand_expr (expr.get_value_expr_ptr ());
671
672 visit (expr.get_if_block ());
673 }
674
675 void
676 ExpandVisitor::visit (AST::IfLetExprConseqElse &expr)
677 {
678 maybe_expand_expr (expr.get_value_expr_ptr ());
679
680 visit (expr.get_if_block ());
681 visit (expr.get_else_block ());
682 }
683
684 void
685 ExpandVisitor::visit (AST::MatchExpr &expr)
686 {
687 visit (expr.get_scrutinee_expr ());
688
689 for (auto &match_case : expr.get_match_cases ())
690 {
691 auto &arm = match_case.get_arm ();
692
693 for (auto &pattern : arm.get_patterns ())
694 visit (pattern);
695
696 if (arm.has_match_arm_guard ())
697 maybe_expand_expr (arm.get_guard_expr_ptr ());
698
699 maybe_expand_expr (match_case.get_expr_ptr ());
700 }
701 }
702
703 void
704 ExpandVisitor::visit (AST::TypeParam &param)
705 {
706 for (auto &bound : param.get_type_param_bounds ())
707 visit (bound);
708
709 if (param.has_type ())
710 maybe_expand_type (param.get_type_ptr ());
711 }
712
713 void
714 ExpandVisitor::visit (AST::LifetimeWhereClauseItem &)
715 {}
716
717 void
718 ExpandVisitor::visit (AST::TypeBoundWhereClauseItem &item)
719 {
720 maybe_expand_type (item.get_type_ptr ());
721
722 for (auto &bound : item.get_type_param_bounds ())
723 visit (bound);
724 }
725
726 void
727 ExpandVisitor::visit (AST::Module &module)
728 {
729 expand_inner_items (module.get_items ());
730 }
731
732 void
733 ExpandVisitor::visit (AST::ExternCrate &crate)
734 {}
735
736 void
737 ExpandVisitor::visit (AST::UseTreeGlob &)
738 {}
739
740 void
741 ExpandVisitor::visit (AST::UseTreeList &)
742 {}
743
744 void
745 ExpandVisitor::visit (AST::UseTreeRebind &)
746 {}
747
748 void
749 ExpandVisitor::visit (AST::UseDeclaration &use_decl)
750 {}
751
752 void
753 ExpandVisitor::visit (AST::Function &function)
754 {
755 if (function.has_body ())
756 visit_inner_using_attrs (
757 function, function.get_definition ().value ()->get_inner_attrs ());
758 for (auto &param : function.get_generic_params ())
759 visit (param);
760
761 expand_function_params (function.get_function_params ());
762
763 if (function.has_return_type ())
764 maybe_expand_type (function.get_return_type_ptr ());
765
766 if (function.has_where_clause ())
767 expand_where_clause (function.get_where_clause ());
768
769 if (function.has_body ())
770 visit (*function.get_definition ());
771 }
772
773 void
774 ExpandVisitor::visit (AST::StructStruct &struct_item)
775 {
776 for (auto &generic : struct_item.get_generic_params ())
777 visit (generic);
778
779 if (struct_item.has_where_clause ())
780 expand_where_clause (struct_item.get_where_clause ());
781
782 expand_struct_fields (struct_item.get_fields ());
783 }
784
785 void
786 ExpandVisitor::visit (AST::TupleStruct &tuple_struct)
787 {
788 for (auto &generic : tuple_struct.get_generic_params ())
789 visit (generic);
790
791 if (tuple_struct.has_where_clause ())
792 expand_where_clause (tuple_struct.get_where_clause ());
793
794 expand_tuple_fields (tuple_struct.get_fields ());
795 }
796
797 void
798 ExpandVisitor::visit (AST::EnumItem &item)
799 {}
800
801 void
802 ExpandVisitor::visit (AST::EnumItemTuple &item)
803 {
804 expand_tuple_fields (item.get_tuple_fields ());
805 }
806
807 void
808 ExpandVisitor::visit (AST::EnumItemStruct &item)
809 {
810 expand_struct_fields (item.get_struct_fields ());
811 }
812
813 void
814 ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
815 {
816 maybe_expand_expr (item.get_expr_ptr ());
817 }
818
819 void
820 ExpandVisitor::visit (AST::Union &union_item)
821 {
822 for (auto &generic : union_item.get_generic_params ())
823 visit (generic);
824
825 expand_struct_fields (union_item.get_variants ());
826 }
827
828 void
829 ExpandVisitor::visit (AST::ConstantItem &const_item)
830 {
831 maybe_expand_type (const_item.get_type_ptr ());
832
833 if (const_item.has_expr ())
834 maybe_expand_expr (const_item.get_expr_ptr ());
835 }
836
837 void
838 ExpandVisitor::visit (AST::StaticItem &static_item)
839 {
840 maybe_expand_type (static_item.get_type_ptr ());
841
842 maybe_expand_expr (static_item.get_expr_ptr ());
843 }
844
845 void
846 ExpandVisitor::visit (AST::TraitItemConst &const_item)
847 {
848 maybe_expand_type (const_item.get_type_ptr ());
849
850 if (const_item.has_expr ())
851 maybe_expand_expr (const_item.get_expr_ptr ());
852 }
853
854 void
855 ExpandVisitor::visit (AST::Trait &trait)
856 {
857 for (auto &generic : trait.get_generic_params ())
858 visit (generic);
859
860 for (auto &bound : trait.get_type_param_bounds ())
861 visit (bound);
862
863 if (trait.has_where_clause ())
864 expand_where_clause (trait.get_where_clause ());
865
866 expander.push_context (MacroExpander::ContextType::TRAIT);
867
868 std::function<std::unique_ptr<AST::AssociatedItem> (AST::SingleASTNode)>
869 extractor
870 = [] (AST::SingleASTNode node) { return node.take_assoc_item (); };
871
872 expand_macro_children (MacroExpander::ContextType::TRAIT,
873 trait.get_trait_items (), extractor);
874
875 expander.pop_context ();
876 }
877
878 void
879 ExpandVisitor::visit (AST::InherentImpl &impl)
880 {
881 visit_inner_attrs (impl);
882 // just expand sub-stuff - can't actually strip generic params themselves
883 for (auto &generic : impl.get_generic_params ())
884 visit (generic);
885
886 // FIXME: Is that correct? How do we test that?
887 expander.push_context (MacroExpander::ContextType::ITEM);
888
889 maybe_expand_type (impl.get_type_ptr ());
890
891 expander.pop_context ();
892
893 if (impl.has_where_clause ())
894 expand_where_clause (impl.get_where_clause ());
895
896 std::function<std::unique_ptr<AST::AssociatedItem> (AST::SingleASTNode)>
897 extractor
898 = [] (AST::SingleASTNode node) { return node.take_assoc_item (); };
899
900 expand_macro_children (MacroExpander::ContextType::IMPL,
901 impl.get_impl_items (), extractor);
902 }
903
904 void
905 ExpandVisitor::visit (AST::TraitImpl &impl)
906 {
907 visit_inner_attrs (impl);
908 // just expand sub-stuff - can't actually strip generic params themselves
909 for (auto &param : impl.get_generic_params ())
910 visit (param);
911
912 // FIXME: Is that correct? How do we test that?
913 expander.push_context (MacroExpander::ContextType::ITEM);
914
915 maybe_expand_type (impl.get_type_ptr ());
916
917 expander.pop_context ();
918
919 visit (impl.get_trait_path ());
920
921 if (impl.has_where_clause ())
922 expand_where_clause (impl.get_where_clause ());
923
924 std::function<std::unique_ptr<AST::AssociatedItem> (AST::SingleASTNode)>
925 extractor
926 = [] (AST::SingleASTNode node) { return node.take_assoc_item (); };
927
928 expand_macro_children (MacroExpander::ContextType::TRAIT_IMPL,
929 impl.get_impl_items (), extractor);
930 }
931
932 void
933 ExpandVisitor::visit (AST::ExternalTypeItem &item)
934 {}
935
936 void
937 ExpandVisitor::visit (AST::ExternalStaticItem &static_item)
938 {
939 maybe_expand_type (static_item.get_type_ptr ());
940 }
941
942 void
943 ExpandVisitor::visit (AST::ExternBlock &block)
944 {
945 visit_inner_attrs (block);
946 std::function<std::unique_ptr<AST::ExternalItem> (AST::SingleASTNode)>
947 extractor
948 = [] (AST::SingleASTNode node) { return node.take_external_item (); };
949
950 expand_macro_children (MacroExpander::ContextType::EXTERN,
951 block.get_extern_items (), extractor);
952 }
953
954 void
955 ExpandVisitor::visit (AST::MacroMatchRepetition &)
956 {}
957
958 void
959 ExpandVisitor::visit (AST::MacroMatcher &)
960 {}
961
962 void
963 ExpandVisitor::visit (AST::MacroRulesDefinition &rules_def)
964 {}
965
966 void
967 ExpandVisitor::visit (AST::MetaItemPath &)
968 {}
969
970 void
971 ExpandVisitor::visit (AST::MetaItemSeq &)
972 {}
973
974 void
975 ExpandVisitor::visit (AST::MetaListPaths &)
976 {}
977
978 void
979 ExpandVisitor::visit (AST::MetaListNameValueStr &)
980 {}
981
982 void
983 ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
984 {}
985
986 void
987 ExpandVisitor::visit (AST::GroupedPattern &pattern)
988 {
989 visit (pattern.get_pattern_in_parens ());
990 }
991
992 void
993 ExpandVisitor::visit (AST::LetStmt &stmt)
994 {
995 visit (stmt.get_pattern ());
996
997 if (stmt.has_type ())
998 maybe_expand_type (stmt.get_type_ptr ());
999
1000 if (stmt.has_init_expr ())
1001 maybe_expand_expr (stmt.get_init_expr_ptr ());
1002 }
1003
1004 void
1005 ExpandVisitor::visit (AST::ExprStmt &stmt)
1006 {
1007 maybe_expand_expr (stmt.get_expr_ptr ());
1008 }
1009
1010 void
1011 ExpandVisitor::visit (AST::BareFunctionType &type)
1012 {
1013 for (auto &param : type.get_function_params ())
1014 {
1015 maybe_expand_type (param.get_type_ptr ());
1016 }
1017
1018 if (type.has_return_type ())
1019 visit (type.get_return_type ());
1020 }
1021
1022 void
1023 ExpandVisitor::visit (AST::FunctionParam &param)
1024 {
1025 maybe_expand_type (param.get_type_ptr ());
1026 }
1027
1028 void
1029 ExpandVisitor::visit (AST::SelfParam &param)
1030 {
1031 /* TODO: maybe check for invariants being violated - e.g. both type and
1032 * lifetime? */
1033 if (param.has_type ())
1034 maybe_expand_type (param.get_type_ptr ());
1035 }
1036
1037 template <typename T>
1038 void
1039 ExpandVisitor::expand_inner_attribute (T &item, AST::SimplePath &path)
1040 {
1041 // FIXME: Retrieve path from segments + local use statements instead of string
1042 expander.expand_attribute_proc_macro (item, path);
1043 }
1044
1045 template <typename T>
1046 void
1047 ExpandVisitor::visit_inner_using_attrs (T &item,
1048 std::vector<AST::Attribute> &attrs)
1049 {
1050 for (auto it = attrs.begin (); it != attrs.end (); /* erase => No increment*/)
1051 {
1052 auto current = *it;
1053
1054 if (!is_builtin (current) && !current.is_derive ())
1055 {
1056 it = attrs.erase (it);
1057 expand_inner_attribute (item, current.get_path ());
1058 }
1059 else
1060 {
1061 it++;
1062 }
1063 }
1064 }
1065
1066 template <typename T>
1067 void
1068 ExpandVisitor::visit_inner_attrs (T &item)
1069 {
1070 visit_inner_using_attrs (item, item.get_inner_attrs ());
1071 }
1072
1073 } // namespace Rust