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 9500971 : is_builtin (AST::Attribute &attr)
33 : {
34 9500971 : auto &segments = attr.get_path ().get_segments ();
35 9500971 : return !segments.empty ()
36 9500971 : && !Analysis::BuiltinAttributeMappings::get ()
37 9500971 : ->lookup_builtin (segments[0].get_segment_name ())
38 9500971 : .is_error ();
39 : }
40 :
41 : /* Expand all of the macro invocations currently contained in a crate */
42 : void
43 12237 : ExpandVisitor::go (AST::Crate &crate)
44 : {
45 12237 : visit (crate);
46 12237 : }
47 :
48 : static std::vector<std::unique_ptr<AST::Item>>
49 6625 : builtin_derive_item (AST::Item &item, const AST::Attribute &derive,
50 : BuiltinMacro to_derive, MacroExpander &expander)
51 : {
52 6625 : auto &mappings = Analysis::Mappings::get ();
53 6625 : auto item_source = AST::Builder::get_item_source (expander.crate);
54 :
55 6625 : auto items
56 6625 : = AST::DeriveVisitor::derive (item, derive, to_derive, item_source);
57 :
58 14792 : for (auto it = items.begin (); it != items.end ();)
59 : {
60 8167 : if (*it == nullptr)
61 11 : it = items.erase (it);
62 : else
63 : {
64 8156 : mappings.add_derived_node ((*it)->get_node_id ());
65 8156 : ++it;
66 : }
67 : }
68 :
69 6625 : 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 4530403 : expand_tail_expr (AST::BlockExpr &block_expr, MacroExpander &expander)
151 : {
152 4530403 : if (block_expr.has_tail_expr ())
153 : {
154 3997946 : auto tail = block_expr.take_tail_expr ();
155 3997946 : auto attrs = tail->get_outer_attrs ();
156 3997946 : bool changed = false;
157 4012576 : for (auto it = attrs.begin (); it != attrs.end ();)
158 : {
159 14630 : auto current = *it;
160 14630 : if (is_builtin (current))
161 : {
162 14630 : 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 14630 : }
176 3997946 : if (changed)
177 0 : block_expr.normalize_tail_expr ();
178 : else
179 3997946 : block_expr.set_tail_expr (std::move (tail));
180 3997946 : }
181 4530403 : }
182 :
183 : void
184 82560 : ExpandVisitor::expand_inner_items (
185 : std::vector<std::unique_ptr<AST::Item>> &items)
186 : {
187 82560 : expander.push_context (MacroExpander::ContextType::ITEM);
188 :
189 3202505 : for (auto it = items.begin (); it != items.end (); it++)
190 : {
191 3119945 : Rust::AST::Item &item = **it;
192 3119945 : if (item.has_outer_attrs ())
193 : {
194 2317909 : auto &attrs = item.get_outer_attrs ();
195 :
196 11691458 : for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
197 : /* erase => No increment*/)
198 : {
199 9373550 : auto current = *attr_it;
200 :
201 9373550 : if (current.is_derive ())
202 : {
203 2143 : current.parse_attr_to_meta_item ();
204 2143 : attr_it = attrs.erase (attr_it);
205 : // Get traits to derive in the current attribute
206 2143 : auto traits_to_derive = current.get_traits_to_derive ();
207 8771 : for (auto &to_derive : traits_to_derive)
208 : {
209 6628 : auto maybe_builtin = MacroBuiltin::builtins.lookup (
210 6628 : to_derive.get ().as_string ());
211 6628 : if (maybe_builtin.has_value ())
212 : {
213 6623 : auto new_items
214 : = builtin_derive_item (item, current,
215 6623 : maybe_builtin.value (),
216 6623 : expander);
217 :
218 14777 : for (auto &&new_item : new_items)
219 8154 : it = items.insert (it, std::move (new_item));
220 6623 : }
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 2143 : }
232 : else /* Attribute */
233 : {
234 9371407 : if (is_builtin (current))
235 : {
236 9371406 : visit (*attr_it);
237 9371406 : 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 9373550 : }
256 : }
257 : }
258 :
259 82560 : expand_macro_children (items, &AST::SingleASTNode::take_item);
260 :
261 82560 : expander.pop_context ();
262 82560 : }
263 :
264 : void
265 4530403 : ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
266 : {
267 4530403 : auto &stmts = expr.get_statements ();
268 4530403 : expander.push_context (MacroExpander::ContextType::STMT);
269 :
270 6937292 : for (auto it = stmts.begin (); it != stmts.end (); it++)
271 : {
272 2406889 : auto &stmt = *it;
273 :
274 : // skip all non-item statements
275 2406889 : if (stmt->get_stmt_kind () != AST::Stmt::Kind::Item)
276 2108131 : continue;
277 :
278 298758 : auto &item = static_cast<AST::Item &> (*stmt.get ());
279 :
280 298758 : if (item.has_outer_attrs ())
281 : {
282 108467 : auto &attrs = item.get_outer_attrs ();
283 :
284 223121 : for (auto attr_it = attrs.begin (); attr_it != attrs.end ();
285 : /* erase => No increment*/)
286 : {
287 114655 : auto current = *attr_it;
288 :
289 114655 : 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 114654 : if (is_builtin (current))
322 : {
323 114653 : visit (*attr_it);
324 114653 : 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 114655 : }
343 : }
344 : }
345 :
346 4530403 : if (!expr.has_tail_expr ())
347 532724 : expr.normalize_tail_expr ();
348 :
349 4530403 : expand_macro_children (stmts, &AST::SingleASTNode::take_stmt);
350 :
351 4530403 : expander.pop_context ();
352 4530403 : }
353 :
354 : void
355 25182277 : ExpandVisitor::visit (AST::Attribute &attr)
356 : {
357 : // An attribute input containing a macro may have been expanded to a literal
358 25182277 : if (attr.has_attr_input ()
359 25182277 : && attr.get_attr_input ().get_attr_input_type ()
360 : == AST::AttrInput::AttrInputType::EXPR)
361 : {
362 32162 : auto &expr = static_cast<AST::AttrInputExpr &> (attr.get_attr_input ());
363 32162 : if (expr.get_expr ().is_literal ())
364 : {
365 10593 : auto &lit = static_cast<AST::LiteralExpr &> (expr.get_expr ());
366 10593 : attr.set_attr_input (std::make_unique<AST::AttrInputLiteral> (lit));
367 : }
368 : }
369 25182277 : AST::DefaultASTVisitor::visit (attr);
370 25182277 : }
371 :
372 : void
373 142316935 : ExpandVisitor::maybe_expand_expr (std::unique_ptr<AST::Expr> &expr)
374 : {
375 142316935 : NodeId old_expect = expr->get_node_id ();
376 142316935 : std::swap (macro_invoc_expect_id, old_expect);
377 :
378 142316935 : expander.push_context (MacroExpander::ContextType::EXPR);
379 142316935 : expr->accept_vis (*this);
380 142316935 : expander.pop_context ();
381 :
382 142316935 : std::swap (macro_invoc_expect_id, old_expect);
383 :
384 142316935 : auto final_fragment = expander.take_expanded_fragment ();
385 142316935 : if (final_fragment.should_expand ()
386 142316935 : && final_fragment.is_expression_fragment ())
387 405259 : expr = final_fragment.take_expression_fragment ();
388 142316935 : }
389 :
390 : void
391 14414757 : ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::Type> &type)
392 : {
393 14414757 : NodeId old_expect = type->get_node_id ();
394 14414757 : std::swap (macro_invoc_expect_id, old_expect);
395 :
396 14414757 : expander.push_context (MacroExpander::ContextType::TYPE);
397 14414757 : type->accept_vis (*this);
398 14414757 : expander.pop_context ();
399 :
400 14414757 : std::swap (macro_invoc_expect_id, old_expect);
401 :
402 14414757 : auto final_fragment = expander.take_expanded_fragment ();
403 14414757 : if (final_fragment.should_expand () && final_fragment.is_type_fragment ())
404 3772 : type = final_fragment.take_type_fragment ();
405 14414757 : }
406 :
407 : // HACK: maybe we shouldn't have TypeNoBounds as a base class
408 : void
409 3316295 : ExpandVisitor::maybe_expand_type (std::unique_ptr<AST::TypeNoBounds> &type)
410 : {
411 3316295 : NodeId old_expect = type->get_node_id ();
412 3316295 : std::swap (macro_invoc_expect_id, old_expect);
413 :
414 3316295 : expander.push_context (MacroExpander::ContextType::TYPE);
415 3316295 : type->accept_vis (*this);
416 3316295 : expander.pop_context ();
417 :
418 3316295 : std::swap (macro_invoc_expect_id, old_expect);
419 :
420 3316295 : auto final_fragment = expander.take_expanded_fragment ();
421 3316295 : 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 3316295 : }
425 :
426 : void
427 19893886 : ExpandVisitor::maybe_expand_pattern (std::unique_ptr<AST::Pattern> &pattern)
428 : {
429 19893886 : NodeId old_expect = pattern->get_node_id ();
430 19893886 : std::swap (macro_invoc_expect_id, old_expect);
431 :
432 19893886 : expander.push_context (MacroExpander::ContextType::PATTERN);
433 19893886 : pattern->accept_vis (*this);
434 19893886 : expander.pop_context ();
435 :
436 19893886 : std::swap (macro_invoc_expect_id, old_expect);
437 :
438 19893886 : auto final_fragment = expander.take_expanded_fragment ();
439 19893886 : if (final_fragment.should_expand () && final_fragment.is_pattern_fragment ())
440 3 : pattern = final_fragment.take_pattern_fragment ();
441 19893886 : }
442 :
443 : // FIXME: This can definitely be refactored with the method above
444 : void
445 3371564 : ExpandVisitor::expand_function_params (
446 : std::vector<std::unique_ptr<AST::Param>> ¶ms)
447 : {
448 9627136 : for (auto &p : params)
449 6255572 : visit (p);
450 3371564 : }
451 :
452 : void
453 3911750 : ExpandVisitor::expand_generic_args (AST::GenericArgs &args)
454 : {
455 7730265 : for (auto &arg : args.get_generic_args ())
456 : {
457 3818515 : switch (arg.get_kind ())
458 : {
459 1187331 : case AST::GenericArg::Kind::Type:
460 1187331 : maybe_expand_type (arg.get_type_ptr ());
461 1187331 : break;
462 1564 : case AST::GenericArg::Kind::Const:
463 1564 : maybe_expand_expr (arg.get_expression_ptr ());
464 1564 : 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 4004388 : for (auto &binding : args.get_binding_args ())
479 92638 : maybe_expand_type (binding.get_type_ptr ());
480 3911750 : }
481 :
482 : void
483 846832 : ExpandVisitor::expand_qualified_path_type (AST::QualifiedPathType &path_type)
484 : {
485 846832 : maybe_expand_type (path_type.get_type_ptr ());
486 :
487 : // FIXME: ARTHUR: Can we do macro expansion in there? Needs a test!
488 846832 : if (path_type.has_as_clause ())
489 826408 : path_type.get_as_type_path ().accept_vis (*this);
490 846832 : }
491 :
492 : void
493 83342 : ExpandVisitor::expand_closure_params (std::vector<AST::ClosureParam> ¶ms)
494 : {
495 183198 : for (auto ¶m : params)
496 : {
497 99856 : maybe_expand_pattern (param.get_pattern_ptr ());
498 :
499 99856 : if (param.has_type_given ())
500 4898 : maybe_expand_type (param.get_type_ptr ());
501 : }
502 83342 : }
503 :
504 : void
505 216687 : ExpandVisitor::expand_where_clause (AST::WhereClause &where_clause)
506 : {
507 512143 : for (auto &item : where_clause.get_items ())
508 295456 : visit (item);
509 216687 : }
510 :
511 : void
512 12237 : ExpandVisitor::visit (AST::Crate &crate)
513 : {
514 12237 : expand_inner_items (crate.items);
515 12237 : }
516 :
517 : void
518 4938133 : ExpandVisitor::visit (AST::DelimTokenTree &)
519 4938133 : {}
520 :
521 : void
522 29570 : ExpandVisitor::visit (AST::AttrInputMetaItemContainer &)
523 29570 : {}
524 :
525 : void
526 31839203 : ExpandVisitor::visit (AST::IdentifierExpr &ident_expr)
527 31839203 : {}
528 :
529 : void
530 320956 : ExpandVisitor::visit (AST::LifetimeParam &)
531 320956 : {}
532 :
533 : void
534 21302 : ExpandVisitor::visit (AST::ConstGenericParam &)
535 21302 : {}
536 :
537 : void
538 450131 : ExpandVisitor::visit (AST::MacroInvocation ¯o_invoc)
539 : {
540 450131 : 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 859593 : expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ()
550 : ? AST::InvocKind::Semicoloned
551 : : AST::InvocKind::Expr);
552 450131 : }
553 :
554 : void
555 19585535 : ExpandVisitor::visit (AST::PathInExpression &path)
556 : {
557 19585535 : if (!path.is_lang_item ())
558 41949717 : for (auto &segment : path.get_segments ())
559 44829702 : if (segment.has_generic_args ())
560 118002 : expand_generic_args (segment.get_generic_args ());
561 19585535 : }
562 :
563 : void
564 3793750 : ExpandVisitor::visit (AST::TypePathSegmentGeneric &segment)
565 : {
566 3793750 : if (segment.has_generic_args ())
567 3793748 : expand_generic_args (segment.get_generic_args ());
568 3793750 : }
569 :
570 : void
571 141272 : ExpandVisitor::visit (AST::TypePathSegmentFunction &segment)
572 : {
573 141272 : auto &type_path_function = segment.get_type_path_function ();
574 :
575 337714 : for (auto &type : type_path_function.get_params ())
576 196442 : visit (type);
577 :
578 141272 : if (type_path_function.has_return_type ())
579 136512 : maybe_expand_type (type_path_function.get_return_type_ptr ());
580 141272 : }
581 :
582 : void
583 27343 : ExpandVisitor::visit (AST::QualifiedPathInExpression &path)
584 : {
585 27343 : expand_qualified_path_type (path.get_qualified_path_type ());
586 :
587 54690 : for (auto &segment : path.get_segments ())
588 54694 : if (segment.has_generic_args ())
589 0 : expand_generic_args (segment.get_generic_args ());
590 27343 : }
591 :
592 : void
593 819489 : ExpandVisitor::visit (AST::QualifiedPathInType &path)
594 : {
595 819489 : expand_qualified_path_type (path.get_qualified_path_type ());
596 :
597 : // this shouldn't strip any segments, but can strip inside them
598 819489 : for (auto &segment : path.get_segments ())
599 0 : visit (segment);
600 819489 : }
601 :
602 : void
603 46555877 : ExpandVisitor::visit (AST::LiteralExpr &expr)
604 46555877 : {}
605 :
606 : void
607 17017434 : ExpandVisitor::visit (AST::AttrInputLiteral &)
608 17017434 : {}
609 :
610 : void
611 21569 : ExpandVisitor::visit (AST::AttrInputExpr &attr_input)
612 : {
613 21569 : maybe_expand_expr (attr_input.get_expr_ptr ());
614 21569 : }
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 6886 : ExpandVisitor::visit (AST::StructExprStruct &expr)
626 6886 : {}
627 :
628 : void
629 83002 : ExpandVisitor::visit (AST::ClosureExprInner &expr)
630 : {
631 83002 : expand_closure_params (expr.get_params ());
632 :
633 83002 : maybe_expand_expr (expr.get_definition_expr_ptr ());
634 83002 : }
635 :
636 : void
637 4530403 : ExpandVisitor::visit (AST::BlockExpr &expr)
638 : {
639 4530403 : expand_inner_stmts (expr);
640 :
641 4530403 : expand_tail_expr (expr, expander);
642 4530403 : if (expr.has_tail_expr ())
643 3997946 : maybe_expand_expr (expr.get_tail_expr_ptr ());
644 4530403 : }
645 :
646 : void
647 340 : ExpandVisitor::visit (AST::ClosureExprInnerTyped &expr)
648 : {
649 340 : expand_closure_params (expr.get_params ());
650 :
651 340 : maybe_expand_type (expr.get_return_type_ptr ());
652 :
653 340 : visit (expr.get_definition_expr ());
654 340 : }
655 :
656 : void
657 178636 : ExpandVisitor::visit (AST::IfExpr &expr)
658 : {
659 178636 : maybe_expand_expr (expr.get_condition_expr_ptr ());
660 :
661 178636 : visit (expr.get_if_block ());
662 178636 : }
663 :
664 : void
665 295437 : ExpandVisitor::visit (AST::IfExprConseqElse &expr)
666 : {
667 295437 : maybe_expand_expr (expr.get_condition_expr_ptr ());
668 :
669 295437 : visit (expr.get_if_block ());
670 295437 : visit (expr.get_else_block ());
671 295437 : }
672 :
673 : void
674 17136 : ExpandVisitor::visit (AST::IfLetExpr &expr)
675 : {
676 17136 : maybe_expand_expr (expr.get_value_expr_ptr ());
677 :
678 17136 : visit (expr.get_if_block ());
679 17136 : }
680 :
681 : void
682 8556 : ExpandVisitor::visit (AST::IfLetExprConseqElse &expr)
683 : {
684 8556 : maybe_expand_expr (expr.get_value_expr_ptr ());
685 :
686 8556 : visit (expr.get_if_block ());
687 8556 : visit (expr.get_else_block ());
688 8556 : }
689 :
690 : void
691 2039054 : ExpandVisitor::visit (AST::TypeParam ¶m)
692 : {
693 2648199 : for (auto &bound : param.get_type_param_bounds ())
694 609145 : visit (bound);
695 :
696 2039054 : if (param.has_type ())
697 8682 : maybe_expand_type (param.get_type_ptr ());
698 2039054 : }
699 :
700 : void
701 284 : ExpandVisitor::visit (AST::LifetimeWhereClauseItem &)
702 284 : {}
703 :
704 : void
705 298624 : ExpandVisitor::visit (AST::TypeBoundWhereClauseItem &item)
706 : {
707 298624 : maybe_expand_type (item.get_type_ptr ());
708 :
709 611328 : for (auto &bound : item.get_type_param_bounds ())
710 312704 : visit (bound);
711 298624 : }
712 :
713 : void
714 70323 : ExpandVisitor::visit (AST::Module &module)
715 : {
716 70323 : expand_inner_items (module.get_items ());
717 70323 : }
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 193376 : ExpandVisitor::visit (AST::UseDeclaration &use_decl)
737 193376 : {}
738 :
739 : void
740 3371564 : ExpandVisitor::visit (AST::Function &function)
741 : {
742 3371564 : visit_outer_attrs (function);
743 : // TODO: handle body inner attributes more regularly?
744 : // apparently, they should be applied to this function
745 3371564 : if (function.has_body ())
746 3083386 : visit_inner_using_attrs (
747 3083386 : function, function.get_definition ().value ()->get_inner_attrs ());
748 3790719 : for (auto ¶m : function.get_generic_params ())
749 419155 : visit (param);
750 :
751 3371564 : expand_function_params (function.get_function_params ());
752 :
753 3371564 : if (function.has_return_type ())
754 2931588 : maybe_expand_type (function.get_return_type_ptr ());
755 :
756 3371564 : if (function.has_where_clause ())
757 117744 : expand_where_clause (function.get_where_clause ());
758 :
759 3371564 : if (function.has_body ())
760 3083386 : visit (*function.get_definition ());
761 3371564 : }
762 :
763 : void
764 17787 : ExpandVisitor::visit (AST::EnumItem &item)
765 17787 : {}
766 :
767 : void
768 1560 : ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
769 : {
770 1560 : maybe_expand_expr (item.get_expr_ptr ());
771 1560 : }
772 :
773 : void
774 48085 : ExpandVisitor::visit (AST::Trait &trait)
775 : {
776 66699 : for (auto &generic : trait.get_generic_params ())
777 18614 : visit (generic);
778 :
779 70291 : for (auto &bound : trait.get_type_param_bounds ())
780 22206 : visit (bound);
781 :
782 48085 : if (trait.has_where_clause ())
783 21 : expand_where_clause (trait.get_where_clause ());
784 :
785 48085 : expander.push_context (MacroExpander::ContextType::TRAIT);
786 :
787 48085 : expand_macro_children (MacroExpander::ContextType::TRAIT,
788 : trait.get_trait_items (),
789 : &AST::SingleASTNode::take_assoc_item);
790 :
791 48085 : expander.pop_context ();
792 48085 : }
793 :
794 : void
795 83719 : ExpandVisitor::visit (AST::InherentImpl &impl)
796 : {
797 167438 : visit_inner_attrs (impl);
798 : // just expand sub-stuff - can't actually strip generic params themselves
799 159376 : for (auto &generic : impl.get_generic_params ())
800 75657 : visit (generic);
801 :
802 : // FIXME: Is that correct? How do we test that?
803 83719 : expander.push_context (MacroExpander::ContextType::ITEM);
804 :
805 83719 : maybe_expand_type (impl.get_type_ptr ());
806 :
807 83719 : expander.pop_context ();
808 :
809 83719 : if (impl.has_where_clause ())
810 842 : expand_where_clause (impl.get_where_clause ());
811 :
812 83719 : expand_macro_children (MacroExpander::ContextType::IMPL,
813 : impl.get_impl_items (),
814 : &AST::SingleASTNode::take_assoc_item);
815 83719 : }
816 :
817 : void
818 1806292 : ExpandVisitor::visit (AST::TraitImpl &impl)
819 : {
820 3612584 : visit_inner_attrs (impl);
821 : // just expand sub-stuff - can't actually strip generic params themselves
822 3582390 : for (auto ¶m : impl.get_generic_params ())
823 1776098 : visit (param);
824 :
825 : // FIXME: Is that correct? How do we test that?
826 1806292 : expander.push_context (MacroExpander::ContextType::ITEM);
827 :
828 1806292 : maybe_expand_type (impl.get_type_ptr ());
829 :
830 1806292 : expander.pop_context ();
831 :
832 1806292 : visit (impl.get_trait_path ());
833 :
834 1806292 : if (impl.has_where_clause ())
835 98080 : expand_where_clause (impl.get_where_clause ());
836 :
837 1806292 : expand_macro_children (MacroExpander::ContextType::TRAIT_IMPL,
838 : impl.get_impl_items (),
839 : &AST::SingleASTNode::take_assoc_item);
840 1806292 : }
841 :
842 : void
843 288 : ExpandVisitor::visit (AST::ExternalTypeItem &item)
844 288 : {}
845 :
846 : void
847 16272 : ExpandVisitor::visit (AST::ExternBlock &block)
848 : {
849 32544 : visit_inner_attrs (block);
850 :
851 16272 : expand_macro_children (MacroExpander::ContextType::EXTERN,
852 : block.get_extern_items (),
853 : &AST::SingleASTNode::take_external_item);
854 16272 : }
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 205329 : ExpandVisitor::visit (AST::MacroRulesDefinition &rules_def)
866 205329 : {}
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 3795 : ExpandVisitor::visit (AST::StructPatternFieldIdent &field)
886 3795 : {}
887 :
888 : void
889 144815 : ExpandVisitor::visit (AST::BareFunctionType &type)
890 : {
891 1014859 : for (auto ¶m : type.get_function_params ())
892 : {
893 870044 : maybe_expand_type (param.get_type_ptr ());
894 : }
895 :
896 144815 : if (type.has_return_type ())
897 142519 : visit (type.get_return_type ());
898 144815 : }
899 :
900 : void
901 4037607 : ExpandVisitor::visit (AST::FunctionParam ¶m)
902 : {
903 4037607 : maybe_expand_pattern (param.get_pattern_ptr ());
904 4037607 : maybe_expand_type (param.get_type_ptr ());
905 4037607 : }
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 2216050 : ExpandVisitor::visit (AST::SelfParam ¶m)
916 : {
917 : /* TODO: maybe check for invariants being violated - e.g. both type and
918 : * lifetime? */
919 2216050 : if (param.has_type ())
920 3694 : maybe_expand_type (param.get_type_ptr ());
921 2216050 : }
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 4989669 : ExpandVisitor::visit_inner_using_attrs (T &item,
934 : std::vector<AST::Attribute> &attrs)
935 : {
936 4990229 : for (auto it = attrs.begin (); it != attrs.end (); /* erase => No increment*/)
937 : {
938 280 : auto current = *it;
939 :
940 280 : 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 280 : it++;
948 : }
949 : }
950 4989669 : }
951 :
952 : template <typename T>
953 : void
954 16272 : ExpandVisitor::visit_inner_attrs (T &item)
955 : {
956 1906283 : visit_inner_using_attrs (item, item.get_inner_attrs ());
957 : }
958 :
959 : } // namespace Rust
|