Line data Source code
1 : // Copyright (C) 2020-2026 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-hir-map.h"
22 : #include "rust-item.h"
23 : #include "rust-proc-macro.h"
24 : #include "rust-attributes.h"
25 : #include "rust-ast.h"
26 : #include "rust-type.h"
27 : #include "rust-derive.h"
28 :
29 : namespace Rust {
30 :
31 : bool
32 14237951 : is_builtin (AST::Attribute &attr)
33 : {
34 14237951 : auto &segments = attr.get_path ().get_segments ();
35 14237951 : return !segments.empty ()
36 14237951 : && !Analysis::BuiltinAttributeMappings::get ()
37 14237951 : ->lookup_builtin (segments[0].get_segment_name ())
38 14237951 : .is_error ();
39 : }
40 :
41 : /* Expand all of the macro invocations currently contained in a crate */
42 : void
43 12377 : ExpandVisitor::go (AST::Crate &crate)
44 : {
45 12377 : visit (crate);
46 12377 : }
47 :
48 : static std::vector<std::unique_ptr<AST::Item>>
49 9649 : builtin_derive_item (AST::Item &item, const AST::Attribute &derive,
50 : BuiltinMacro to_derive, MacroExpander &expander)
51 : {
52 9649 : auto &mappings = Analysis::Mappings::get ();
53 9649 : auto item_source = AST::Builder::get_item_source (expander.crate);
54 :
55 9649 : auto items
56 9649 : = AST::DeriveVisitor::derive (item, derive, to_derive, item_source);
57 :
58 21512 : for (auto it = items.begin (); it != items.end ();)
59 : {
60 11863 : if (*it == nullptr)
61 11 : it = items.erase (it);
62 : else
63 : {
64 11852 : mappings.add_derived_node ((*it)->get_node_id ());
65 11852 : ++it;
66 : }
67 : }
68 :
69 9649 : return items;
70 : }
71 :
72 : static std::vector<std::unique_ptr<AST::Item>>
73 5 : derive_item (AST::Item &item, AST::SimplePath &to_derive,
74 : MacroExpander &expander)
75 : {
76 5 : std::vector<std::unique_ptr<AST::Item>> result;
77 5 : auto frag = expander.expand_derive_proc_macro (item, to_derive);
78 5 : if (!frag.is_error ())
79 : {
80 0 : for (auto &node : frag.get_nodes ())
81 : {
82 0 : switch (node.get_kind ())
83 : {
84 0 : case AST::SingleASTNode::Kind::Item:
85 0 : Analysis::Mappings::get ().add_derived_node (
86 0 : node.get_item ()->get_node_id ());
87 0 : result.push_back (node.take_item ());
88 0 : break;
89 0 : default:
90 0 : rust_unreachable ();
91 : }
92 : }
93 : }
94 5 : return result;
95 5 : }
96 :
97 : static std::vector<std::unique_ptr<AST::Item>>
98 1 : expand_item_attribute (AST::Item &item, AST::SimplePath &name,
99 : MacroExpander &expander)
100 : {
101 1 : std::vector<std::unique_ptr<AST::Item>> result;
102 1 : auto frag = expander.expand_attribute_proc_macro (item, name);
103 1 : if (!frag.is_error ())
104 : {
105 0 : for (auto &node : frag.get_nodes ())
106 : {
107 0 : switch (node.get_kind ())
108 : {
109 0 : case AST::SingleASTNode::Kind::Item:
110 0 : result.push_back (node.take_item ());
111 0 : break;
112 0 : default:
113 0 : rust_unreachable ();
114 : }
115 : }
116 : }
117 1 : return result;
118 1 : }
119 :
120 : /* Helper function to expand a given attribute on a statement and collect back
121 : * statements.
122 : * T should be anything that can be used as a statement accepting outer
123 : * attributes.
124 : */
125 : template <typename T>
126 : static std::vector<std::unique_ptr<AST::Stmt>>
127 1 : expand_stmt_attribute (T &statement, AST::SimplePath &attribute,
128 : MacroExpander &expander)
129 : {
130 1 : std::vector<std::unique_ptr<AST::Stmt>> result;
131 1 : auto frag = expander.expand_attribute_proc_macro (statement, attribute);
132 1 : if (!frag.is_error ())
133 : {
134 0 : for (auto &node : frag.get_nodes ())
135 : {
136 0 : switch (node.get_kind ())
137 : {
138 0 : case AST::SingleASTNode::Kind::Stmt:
139 0 : result.push_back (node.take_stmt ());
140 : break;
141 0 : default:
142 0 : rust_unreachable ();
143 : }
144 : }
145 : }
146 1 : return result;
147 1 : }
148 :
149 : void
150 6755917 : expand_tail_expr (AST::BlockExpr &block_expr, MacroExpander &expander)
151 : {
152 6755917 : if (block_expr.has_tail_expr ())
153 : {
154 5967630 : auto tail = block_expr.take_tail_expr ();
155 5967630 : auto attrs = tail->get_outer_attrs ();
156 5967630 : bool changed = false;
157 5989016 : for (auto it = attrs.begin (); it != attrs.end ();)
158 : {
159 21386 : auto current = *it;
160 21386 : if (is_builtin (current))
161 : {
162 21386 : it++;
163 : }
164 : else
165 : {
166 0 : it = attrs.erase (it);
167 0 : changed = true;
168 0 : auto new_stmts
169 : = expand_stmt_attribute (block_expr, current.get_path (),
170 0 : expander);
171 0 : auto &stmts = block_expr.get_statements ();
172 0 : std::move (new_stmts.begin (), new_stmts.end (),
173 : std::inserter (stmts, stmts.end ()));
174 0 : }
175 21386 : }
176 5967630 : if (changed)
177 0 : block_expr.normalize_tail_expr ();
178 : else
179 5967630 : block_expr.set_tail_expr (std::move (tail));
180 5967630 : }
181 6755917 : }
182 :
183 : void
184 115740 : ExpandVisitor::expand_inner_items (
185 : std::vector<std::unique_ptr<AST::Item>> &items)
186 : {
187 115740 : expander.push_context (MacroExpander::ContextType::ITEM);
188 :
189 4755625 : for (auto it = items.begin (); it != items.end (); it++)
190 : {
191 4639885 : Rust::AST::Item &item = **it;
192 4639885 : if (item.has_outer_attrs ())
193 : {
194 3469533 : auto &attrs = item.get_outer_attrs ();
195 :
196 17516814 : for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
197 : /* erase => No increment*/)
198 : {
199 14047282 : auto current = *attr_it;
200 :
201 14047282 : if (current.is_derive ())
202 : {
203 3075 : current.parse_attr_to_meta_item ();
204 3075 : attr_it = attrs.erase (attr_it);
205 : // Get traits to derive in the current attribute
206 3075 : auto traits_to_derive = current.get_traits_to_derive ();
207 12727 : for (auto &to_derive : traits_to_derive)
208 : {
209 9652 : auto maybe_builtin = MacroBuiltin::builtins.lookup (
210 9652 : to_derive.get ().as_string ());
211 9652 : if (maybe_builtin.has_value ())
212 : {
213 9647 : auto new_items
214 : = builtin_derive_item (item, current,
215 9647 : maybe_builtin.value (),
216 9647 : expander);
217 :
218 21497 : for (auto &&new_item : new_items)
219 11850 : it = items.insert (it, std::move (new_item));
220 9647 : }
221 : else
222 : {
223 : // Macro is not a builtin, so it must be a
224 : // user-defined derive macro.
225 5 : auto new_items
226 5 : = derive_item (item, to_derive, expander);
227 5 : std::move (new_items.begin (), new_items.end (),
228 : std::inserter (items, it));
229 5 : }
230 : }
231 3075 : }
232 : else /* Attribute */
233 : {
234 14044207 : if (is_builtin (current))
235 : {
236 14044206 : visit (*attr_it);
237 14044206 : attr_it++;
238 : }
239 : else
240 : {
241 1 : attr_it = attrs.erase (attr_it);
242 1 : auto new_items
243 : = expand_item_attribute (item, current.get_path (),
244 1 : expander);
245 1 : it = items.erase (it);
246 1 : std::move (new_items.begin (), new_items.end (),
247 : std::inserter (items, it));
248 : // TODO: Improve this ?
249 : // item is invalid since it refers to now deleted,
250 : // cancel the loop increment and break.
251 1 : it--;
252 1 : break;
253 1 : }
254 : }
255 14047282 : }
256 : }
257 : }
258 :
259 115740 : expand_macro_children (items, &AST::SingleASTNode::take_item);
260 :
261 115740 : expander.pop_context ();
262 115740 : }
263 :
264 : void
265 6755917 : ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
266 : {
267 6755917 : auto &stmts = expr.get_statements ();
268 6755917 : expander.push_context (MacroExpander::ContextType::STMT);
269 :
270 10330256 : for (auto it = stmts.begin (); it != stmts.end (); it++)
271 : {
272 3574339 : auto &stmt = *it;
273 :
274 : // skip all non-item statements
275 3574339 : if (stmt->get_stmt_kind () != AST::Stmt::Kind::Item)
276 3127329 : continue;
277 :
278 447010 : auto &item = static_cast<AST::Item &> (*stmt.get ());
279 :
280 447010 : if (item.has_outer_attrs ())
281 : {
282 162671 : auto &attrs = item.get_outer_attrs ();
283 :
284 334609 : for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
285 : /* erase => No increment*/)
286 : {
287 171939 : auto current = *attr_it;
288 :
289 171939 : if (current.is_derive ())
290 : {
291 1 : attr_it = attrs.erase (attr_it);
292 : // Get traits to derive in the current attribute
293 1 : auto traits_to_derive = current.get_traits_to_derive ();
294 3 : for (auto &to_derive : traits_to_derive)
295 : {
296 2 : auto maybe_builtin = MacroBuiltin::builtins.lookup (
297 2 : to_derive.get ().as_string ());
298 2 : if (maybe_builtin.has_value ())
299 : {
300 2 : auto new_items
301 : = builtin_derive_item (item, current,
302 2 : maybe_builtin.value (),
303 2 : expander);
304 :
305 : // this inserts the derive *before* the item - is it a
306 : // problem?
307 4 : for (auto &&new_item : new_items)
308 2 : it = stmts.insert (it, std::move (new_item));
309 2 : }
310 : else
311 : {
312 0 : auto new_items
313 0 : = derive_item (item, to_derive, expander);
314 0 : std::move (new_items.begin (), new_items.end (),
315 : std::inserter (stmts, it));
316 0 : }
317 : }
318 1 : }
319 : else /* Attribute */
320 : {
321 171938 : if (is_builtin (current))
322 : {
323 171937 : visit (*attr_it);
324 171937 : attr_it++;
325 : }
326 : else
327 : {
328 1 : attr_it = attrs.erase (attr_it);
329 1 : auto new_items
330 : = expand_stmt_attribute (item, current.get_path (),
331 1 : expander);
332 1 : it = stmts.erase (it);
333 1 : std::move (new_items.begin (), new_items.end (),
334 : std::inserter (stmts, it));
335 : // TODO: Improve this ?
336 : // item is invalid since it refers to now deleted,
337 : // cancel the loop increment and break.
338 1 : it--;
339 1 : break;
340 1 : }
341 : }
342 171939 : }
343 : }
344 : }
345 :
346 6755917 : if (!expr.has_tail_expr ())
347 788658 : expr.normalize_tail_expr ();
348 :
349 6755917 : expand_macro_children (stmts, &AST::SingleASTNode::take_stmt);
350 :
351 6755917 : expander.pop_context ();
352 6755917 : }
353 :
354 : void
355 37721177 : ExpandVisitor::visit (AST::Attribute &attr)
356 : {
357 : // An attribute input containing a macro may have been expanded to a literal
358 37721177 : if (attr.has_attr_input ()
359 37721177 : && attr.get_attr_input ().get_attr_input_type ()
360 : == AST::AttrInput::AttrInputType::EXPR)
361 : {
362 48242 : auto &expr = static_cast<AST::AttrInputExpr &> (attr.get_attr_input ());
363 48242 : if (expr.get_expr ().is_literal ())
364 : {
365 15889 : auto &lit = static_cast<AST::LiteralExpr &> (expr.get_expr ());
366 15889 : attr.set_attr_input (std::make_unique<AST::AttrInputLiteral> (lit));
367 : }
368 : }
369 37721177 : AST::DefaultASTVisitor::visit (attr);
370 37721177 : }
371 :
372 : void
373 212737271 : ExpandVisitor::maybe_expand_expr (std::unique_ptr<AST::Expr> &expr)
374 : {
375 212737271 : NodeId old_expect = expr->get_node_id ();
376 212737271 : std::swap (macro_invoc_expect_id, old_expect);
377 :
378 212737271 : expander.push_context (MacroExpander::ContextType::EXPR);
379 212737271 : expr->accept_vis (*this);
380 212737271 : expander.pop_context ();
381 :
382 212737271 : std::swap (macro_invoc_expect_id, old_expect);
383 :
384 212737271 : auto final_fragment = expander.take_expanded_fragment ();
385 212737271 : if (final_fragment.should_expand ()
386 212737271 : && final_fragment.is_expression_fragment ())
387 606923 : expr = final_fragment.take_expression_fragment ();
388 212737271 : }
389 :
390 : void
391 21542709 : ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::Type> &type)
392 : {
393 21542709 : NodeId old_expect = type->get_node_id ();
394 21542709 : std::swap (macro_invoc_expect_id, old_expect);
395 :
396 21542709 : expander.push_context (MacroExpander::ContextType::TYPE);
397 21542709 : type->accept_vis (*this);
398 21542709 : expander.pop_context ();
399 :
400 21542709 : std::swap (macro_invoc_expect_id, old_expect);
401 :
402 21542709 : auto final_fragment = expander.take_expanded_fragment ();
403 21542709 : if (final_fragment.should_expand () && final_fragment.is_type_fragment ())
404 5644 : type = final_fragment.take_type_fragment ();
405 21542709 : }
406 :
407 : // HACK: maybe we shouldn't have TypeNoBounds as a base class
408 : void
409 4949659 : ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::TypeNoBounds> &type)
410 : {
411 4949659 : NodeId old_expect = type->get_node_id ();
412 4949659 : std::swap (macro_invoc_expect_id, old_expect);
413 :
414 4949659 : expander.push_context (MacroExpander::ContextType::TYPE);
415 4949659 : type->accept_vis (*this);
416 4949659 : expander.pop_context ();
417 :
418 4949659 : std::swap (macro_invoc_expect_id, old_expect);
419 :
420 4949659 : auto final_fragment = expander.take_expanded_fragment ();
421 4949659 : if (final_fragment.should_expand () && final_fragment.is_type_fragment ())
422 2 : type = std::make_unique<AST::ParenthesisedType> (
423 3 : final_fragment.take_type_fragment (), BUILTINS_LOCATION);
424 4949659 : }
425 :
426 : void
427 29791732 : ExpandVisitor::maybe_expand_pattern (std::unique_ptr<AST::Pattern> &pattern)
428 : {
429 29791732 : NodeId old_expect = pattern->get_node_id ();
430 29791732 : std::swap (macro_invoc_expect_id, old_expect);
431 :
432 29791732 : expander.push_context (MacroExpander::ContextType::PATTERN);
433 29791732 : pattern->accept_vis (*this);
434 29791732 : expander.pop_context ();
435 :
436 29791732 : std::swap (macro_invoc_expect_id, old_expect);
437 :
438 29791732 : auto final_fragment = expander.take_expanded_fragment ();
439 29791732 : if (final_fragment.should_expand () && final_fragment.is_pattern_fragment ())
440 3 : pattern = final_fragment.take_pattern_fragment ();
441 29791732 : }
442 :
443 : // FIXME: This can definitely be refactored with the method above
444 : void
445 5026256 : ExpandVisitor::expand_function_params (
446 : std::vector<std::unique_ptr<AST::Param>> ¶ms)
447 : {
448 14373368 : for (auto &p : params)
449 9347112 : visit (p);
450 5026256 : }
451 :
452 : void
453 5856126 : ExpandVisitor::expand_generic_args (AST::GenericArgs &args)
454 : {
455 11570749 : for (auto &arg : args.get_generic_args ())
456 : {
457 5714623 : switch (arg.get_kind ())
458 : {
459 1775907 : case AST::GenericArg::Kind::Type:
460 1775907 : maybe_expand_type (arg.get_type_ptr ());
461 1775907 : break;
462 2264 : case AST::GenericArg::Kind::Const:
463 2264 : maybe_expand_expr (arg.get_expression_ptr ());
464 2264 : break;
465 : default:
466 : break;
467 : // FIXME: Figure out what to do here if there is ambiguity. Since the
468 : // resolver comes after the expansion, we need to figure out a way to
469 : // strip ambiguous values here
470 : // TODO: ARTHUR: Probably add a `mark_as_strip` method to `GenericArg`
471 : // or something. This would clean up this whole thing
472 : }
473 : }
474 :
475 : // FIXME: Can we have macro invocations in generic type bindings?
476 : // expand binding args - strip sub-types only
477 : // FIXME: ARTHUR: This needs a test! Foo<Item = macro!()>
478 5994792 : for (auto &binding : args.get_binding_args ())
479 138666 : maybe_expand_type (binding.get_type_ptr ());
480 5856126 : }
481 :
482 : void
483 1269656 : ExpandVisitor::expand_qualified_path_type (AST::QualifiedPathType &path_type)
484 : {
485 1269656 : maybe_expand_type (path_type.get_type_ptr ());
486 :
487 : // FIXME: ARTHUR: Can we do macro expansion in there? Needs a test!
488 1269656 : if (path_type.has_as_clause ())
489 1239084 : path_type.get_as_type_path ().accept_vis (*this);
490 1269656 : }
491 :
492 : void
493 124642 : ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> ¶ms)
494 : {
495 274058 : for (auto ¶m : params)
496 : {
497 149416 : maybe_expand_pattern (param.get_pattern_ptr ());
498 :
499 149416 : if (param.has_type_given ())
500 7278 : maybe_expand_type (param.get_type_ptr ());
501 : }
502 124642 : }
503 :
504 : void
505 324299 : ExpandVisitor::expand_where_clause (AST::WhereClause &where_clause)
506 : {
507 766623 : for (auto &item : where_clause.get_items ())
508 442324 : visit (item);
509 324299 : }
510 :
511 : void
512 12377 : ExpandVisitor::visit (AST::Crate &crate)
513 : {
514 12377 : expand_inner_items (crate.items);
515 12377 : }
516 :
517 : void
518 7400453 : ExpandVisitor::visit (AST::DelimTokenTree &)
519 7400453 : {}
520 :
521 : void
522 44226 : ExpandVisitor::visit (AST::AttrInputMetaItemContainer &)
523 44226 : {}
524 :
525 : void
526 47715823 : ExpandVisitor::visit (AST::IdentifierExpr &ident_expr)
527 47715823 : {}
528 :
529 : void
530 480416 : ExpandVisitor::visit (AST::LifetimeParam &)
531 480416 : {}
532 :
533 : void
534 31802 : ExpandVisitor::visit (AST::ConstGenericParam &)
535 31802 : {}
536 :
537 : void
538 673571 : ExpandVisitor::visit (AST::MacroInvocation ¯o_invoc)
539 : {
540 673571 : if (macro_invoc_expect_id != macro_invoc.get_node_id ())
541 : {
542 0 : rust_internal_error_at (
543 : macro_invoc.get_locus (),
544 : "attempting to expand node with id %d into position with node id %d",
545 0 : (int) macro_invoc.get_node_id (), (int) macro_invoc_expect_id);
546 : }
547 :
548 : // TODO: Can we do the AST fragment replacing here? Probably not, right?
549 1286761 : expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ()
550 : ? AST::InvocKind::Semicoloned
551 : : AST::InvocKind::Expr);
552 673571 : }
553 :
554 : void
555 29331773 : ExpandVisitor::visit (AST::PathInExpression &path)
556 : {
557 29331773 : if (!path.is_lang_item ())
558 62813173 : for (auto &segment : path.get_segments ())
559 67114394 : if (segment.has_generic_args ())
560 175558 : expand_generic_args (segment.get_generic_args ());
561 29331773 : }
562 :
563 : void
564 5680570 : ExpandVisitor::visit (AST::TypePathSegmentGeneric &segment)
565 : {
566 5680570 : if (segment.has_generic_args ())
567 5680568 : expand_generic_args (segment.get_generic_args ());
568 5680570 : }
569 :
570 : void
571 211716 : ExpandVisitor::visit (AST::TypePathSegmentFunction &segment)
572 : {
573 211716 : auto &type_path_function = segment.get_type_path_function ();
574 :
575 506154 : for (auto &type : type_path_function.get_params ())
576 294438 : visit (type);
577 :
578 211716 : if (type_path_function.has_return_type ())
579 204584 : maybe_expand_type (type_path_function.get_return_type_ptr ());
580 211716 : }
581 :
582 : void
583 40827 : ExpandVisitor::visit (AST::QualifiedPathInExpression &path)
584 : {
585 40827 : expand_qualified_path_type (path.get_qualified_path_type ());
586 :
587 81658 : for (auto &segment : path.get_segments ())
588 81662 : if (segment.has_generic_args ())
589 0 : expand_generic_args (segment.get_generic_args ());
590 40827 : }
591 :
592 : void
593 1228829 : ExpandVisitor::visit (AST::QualifiedPathInType &path)
594 : {
595 1228829 : expand_qualified_path_type (path.get_qualified_path_type ());
596 :
597 : // this shouldn't strip any segments, but can strip inside them
598 1228829 : for (auto &segment : path.get_segments ())
599 0 : visit (segment);
600 1228829 : }
601 :
602 : void
603 69543467 : ExpandVisitor::visit (AST::LiteralExpr &expr)
604 69543467 : {}
605 :
606 : void
607 25484282 : ExpandVisitor::visit (AST::AttrInputLiteral &)
608 25484282 : {}
609 :
610 : void
611 32353 : ExpandVisitor::visit (AST::AttrInputExpr &attr_input)
612 : {
613 32353 : maybe_expand_expr (attr_input.get_expr_ptr ());
614 32353 : }
615 :
616 : void
617 0 : ExpandVisitor::visit (AST::MetaItemLitExpr &)
618 0 : {}
619 :
620 : void
621 0 : ExpandVisitor::visit (AST::MetaItemPathExpr &)
622 0 : {}
623 :
624 : void
625 10210 : ExpandVisitor::visit (AST::StructExprStruct &expr)
626 10210 : {}
627 :
628 : void
629 124162 : ExpandVisitor::visit (AST::ClosureExprInner &expr)
630 : {
631 124162 : expand_closure_params (expr.get_params ());
632 :
633 124162 : maybe_expand_expr (expr.get_definition_expr_ptr ());
634 124162 : }
635 :
636 : void
637 6755917 : ExpandVisitor::visit (AST::BlockExpr &expr)
638 : {
639 6755917 : expand_inner_stmts (expr);
640 :
641 6755917 : expand_tail_expr (expr, expander);
642 6755917 : if (expr.has_tail_expr ())
643 5967630 : maybe_expand_expr (expr.get_tail_expr_ptr ());
644 6755917 : }
645 :
646 : void
647 480 : ExpandVisitor::visit (AST::ClosureExprInnerTyped &expr)
648 : {
649 480 : expand_closure_params (expr.get_params ());
650 :
651 480 : maybe_expand_type (expr.get_return_type_ptr ());
652 :
653 480 : visit (expr.get_definition_expr ());
654 480 : }
655 :
656 : void
657 265394 : ExpandVisitor::visit (AST::IfExpr &expr)
658 : {
659 265394 : maybe_expand_expr (expr.get_condition_expr_ptr ());
660 :
661 265394 : visit (expr.get_if_block ());
662 265394 : }
663 :
664 : void
665 441129 : ExpandVisitor::visit (AST::IfExprConseqElse &expr)
666 : {
667 441129 : maybe_expand_expr (expr.get_condition_expr_ptr ());
668 :
669 441129 : visit (expr.get_if_block ());
670 441129 : visit (expr.get_else_block ());
671 441129 : }
672 :
673 : void
674 25668 : ExpandVisitor::visit (AST::IfLetExpr &expr)
675 : {
676 25668 : maybe_expand_expr (expr.get_value_expr_ptr ());
677 :
678 25668 : visit (expr.get_if_block ());
679 25668 : }
680 :
681 : void
682 12756 : ExpandVisitor::visit (AST::IfLetExprConseqElse &expr)
683 : {
684 12756 : maybe_expand_expr (expr.get_value_expr_ptr ());
685 :
686 12756 : visit (expr.get_if_block ());
687 12756 : visit (expr.get_else_block ());
688 12756 : }
689 :
690 : void
691 3047966 : ExpandVisitor::visit (AST::TypeParam ¶m)
692 : {
693 3957959 : for (auto &bound : param.get_type_param_bounds ())
694 909993 : visit (bound);
695 :
696 3047966 : if (param.has_type ())
697 12462 : maybe_expand_type (param.get_type_ptr ());
698 3047966 : }
699 :
700 : void
701 424 : ExpandVisitor::visit (AST::LifetimeWhereClauseItem &)
702 424 : {}
703 :
704 : void
705 447032 : ExpandVisitor::visit (AST::TypeBoundWhereClauseItem &item)
706 : {
707 447032 : maybe_expand_type (item.get_type_ptr ());
708 :
709 915128 : for (auto &bound : item.get_type_param_bounds ())
710 468096 : visit (bound);
711 447032 : }
712 :
713 : void
714 103363 : ExpandVisitor::visit (AST::Module &module)
715 : {
716 103363 : expand_inner_items (module.get_items ());
717 103363 : }
718 :
719 : void
720 141 : ExpandVisitor::visit (AST::ExternCrate &crate)
721 141 : {}
722 :
723 : void
724 0 : ExpandVisitor::visit (AST::UseTreeGlob &)
725 0 : {}
726 :
727 : void
728 0 : ExpandVisitor::visit (AST::UseTreeList &)
729 0 : {}
730 :
731 : void
732 0 : ExpandVisitor::visit (AST::UseTreeRebind &)
733 0 : {}
734 :
735 : void
736 287928 : ExpandVisitor::visit (AST::UseDeclaration &use_decl)
737 287928 : {}
738 :
739 : void
740 5026256 : ExpandVisitor::visit (AST::Function &function)
741 : {
742 5026256 : visit_outer_attrs (function);
743 : // TODO: handle body inner attributes more regularly?
744 : // apparently, they should be applied to this function
745 5026256 : if (function.has_body ())
746 4600710 : visit_inner_using_attrs (
747 4600710 : function, function.get_definition ().value ()->get_inner_attrs ());
748 5651111 : for (auto ¶m : function.get_generic_params ())
749 624855 : visit (param);
750 :
751 5026256 : expand_function_params (function.get_function_params ());
752 :
753 5026256 : if (function.has_return_type ())
754 4373832 : maybe_expand_type (function.get_return_type_ptr ());
755 :
756 5026256 : if (function.has_where_clause ())
757 176156 : expand_where_clause (function.get_where_clause ());
758 :
759 5026256 : if (function.has_body ())
760 4600710 : visit (*function.get_definition ());
761 5026256 : }
762 :
763 : void
764 26047 : ExpandVisitor::visit (AST::EnumItem &item)
765 26047 : {}
766 :
767 : void
768 1980 : ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
769 : {
770 1980 : maybe_expand_expr (item.get_expr_ptr ());
771 1980 : }
772 :
773 : void
774 66565 : ExpandVisitor::visit (AST::Trait &trait)
775 : {
776 93439 : for (auto &generic : trait.get_generic_params ())
777 26874 : visit (generic);
778 :
779 98711 : for (auto &bound : trait.get_type_param_bounds ())
780 32146 : visit (bound);
781 :
782 66565 : if (trait.has_where_clause ())
783 21 : expand_where_clause (trait.get_where_clause ());
784 :
785 66565 : expander.push_context (MacroExpander::ContextType::TRAIT);
786 :
787 66565 : expand_macro_children (MacroExpander::ContextType::TRAIT,
788 : trait.get_trait_items (),
789 : &AST::SingleASTNode::take_assoc_item);
790 :
791 66565 : expander.pop_context ();
792 66565 : }
793 :
794 : void
795 123983 : ExpandVisitor::visit (AST::InherentImpl &impl)
796 : {
797 247966 : visit_inner_attrs (impl);
798 : // just expand sub-stuff - can't actually strip generic params themselves
799 236304 : for (auto &generic : impl.get_generic_params ())
800 112321 : visit (generic);
801 :
802 : // FIXME: Is that correct? How do we test that?
803 123983 : expander.push_context (MacroExpander::ContextType::ITEM);
804 :
805 123983 : maybe_expand_type (impl.get_type_ptr ());
806 :
807 123983 : expander.pop_context ();
808 :
809 123983 : if (impl.has_where_clause ())
810 1262 : expand_where_clause (impl.get_where_clause ());
811 :
812 123983 : expand_macro_children (MacroExpander::ContextType::IMPL,
813 : impl.get_impl_items (),
814 : &AST::SingleASTNode::take_assoc_item);
815 123983 : }
816 :
817 : void
818 2699132 : ExpandVisitor::visit (AST::TraitImpl &impl)
819 : {
820 5398264 : visit_inner_attrs (impl);
821 : // just expand sub-stuff - can't actually strip generic params themselves
822 5359974 : for (auto ¶m : impl.get_generic_params ())
823 2660842 : visit (param);
824 :
825 : // FIXME: Is that correct? How do we test that?
826 2699132 : expander.push_context (MacroExpander::ContextType::ITEM);
827 :
828 2699132 : maybe_expand_type (impl.get_type_ptr ());
829 :
830 2699132 : expander.pop_context ();
831 :
832 2699132 : visit (impl.get_trait_path ());
833 :
834 2699132 : if (impl.has_where_clause ())
835 146860 : expand_where_clause (impl.get_where_clause ());
836 :
837 2699132 : expand_macro_children (MacroExpander::ContextType::TRAIT_IMPL,
838 : impl.get_impl_items (),
839 : &AST::SingleASTNode::take_assoc_item);
840 2699132 : }
841 :
842 : void
843 428 : ExpandVisitor::visit (AST::ExternalTypeItem &item)
844 428 : {}
845 :
846 : void
847 22292 : ExpandVisitor::visit (AST::ExternBlock &block)
848 : {
849 44584 : visit_inner_attrs (block);
850 :
851 22292 : expand_macro_children (MacroExpander::ContextType::EXTERN,
852 : block.get_extern_items (),
853 : &AST::SingleASTNode::take_external_item);
854 22292 : }
855 :
856 : void
857 0 : ExpandVisitor::visit (AST::MacroMatchRepetition &)
858 0 : {}
859 :
860 : void
861 0 : ExpandVisitor::visit (AST::MacroMatcher &)
862 0 : {}
863 :
864 : void
865 305685 : ExpandVisitor::visit (AST::MacroRulesDefinition &rules_def)
866 305685 : {}
867 :
868 : void
869 0 : ExpandVisitor::visit (AST::MetaItemPath &)
870 0 : {}
871 :
872 : void
873 0 : ExpandVisitor::visit (AST::MetaItemSeq &)
874 0 : {}
875 :
876 : void
877 0 : ExpandVisitor::visit (AST::MetaListPaths &)
878 0 : {}
879 :
880 : void
881 0 : ExpandVisitor::visit (AST::MetaListNameValueStr &)
882 0 : {}
883 :
884 : void
885 5475 : ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
886 5475 : {}
887 :
888 : void
889 217139 : ExpandVisitor::visit (AST::BareFunctionType &type)
890 : {
891 1522135 : for (auto ¶m : type.get_function_params ())
892 : {
893 1304996 : maybe_expand_type (param.get_type_ptr ());
894 : }
895 :
896 217139 : if (type.has_return_type ())
897 213723 : visit (type.get_return_type ());
898 217139 : }
899 :
900 : void
901 6036703 : ExpandVisitor::visit (AST::FunctionParam ¶m)
902 : {
903 6036703 : maybe_expand_pattern (param.get_pattern_ptr ());
904 6036703 : maybe_expand_type (param.get_type_ptr ());
905 6036703 : }
906 :
907 : void
908 1915 : ExpandVisitor::visit (AST::VariadicParam ¶m)
909 : {
910 1915 : if (param.has_pattern ())
911 22 : maybe_expand_pattern (param.get_pattern_ptr ());
912 1915 : }
913 :
914 : void
915 3308494 : ExpandVisitor::visit (AST::SelfParam ¶m)
916 : {
917 : /* TODO: maybe check for invariants being violated - e.g. both type and
918 : * lifetime? */
919 3308494 : if (param.has_type ())
920 5514 : maybe_expand_type (param.get_type_ptr ());
921 3308494 : }
922 :
923 : template <typename T>
924 : void
925 0 : ExpandVisitor::expand_inner_attribute (T &item, AST::SimplePath &path)
926 : {
927 : // FIXME: Retrieve path from segments + local use statements instead of string
928 0 : expander.expand_attribute_proc_macro (item, path);
929 0 : }
930 :
931 : template <typename T>
932 : void
933 7446117 : ExpandVisitor::visit_inner_using_attrs (T &item,
934 : std::vector<AST::Attribute> &attrs)
935 : {
936 7446957 : for (auto it = attrs.begin (); it != attrs.end (); /* erase => No increment*/)
937 : {
938 420 : auto current = *it;
939 :
940 420 : if (!is_builtin (current) && !current.is_derive ())
941 : {
942 0 : it = attrs.erase (it);
943 0 : expand_inner_attribute (item, current.get_path ());
944 : }
945 : else
946 : {
947 420 : it++;
948 : }
949 : }
950 7446117 : }
951 :
952 : template <typename T>
953 : void
954 22292 : ExpandVisitor::visit_inner_attrs (T &item)
955 : {
956 2845407 : visit_inner_using_attrs (item, item.get_inner_attrs ());
957 : }
958 :
959 : } // namespace Rust
|