]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/rust/resolve/rust-early-name-resolver.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / resolve / rust-early-name-resolver.cc
CommitLineData
a945c346 1// Copyright (C) 2020-2024 Free Software Foundation, Inc.
52219746
AC
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-early-name-resolver.h"
20#include "rust-ast-full.h"
21#include "rust-name-resolver.h"
22
23namespace Rust {
24namespace Resolver {
25
26EarlyNameResolver::EarlyNameResolver ()
38216691
AC
27 : current_scope (UNKNOWN_NODEID), resolver (*Resolver::get ()),
28 mappings (*Analysis::Mappings::get ())
52219746
AC
29{}
30
31void
32EarlyNameResolver::go (AST::Crate &crate)
33{
38216691
AC
34 scoped (crate.get_node_id (), [&crate, this] () {
35 for (auto &item : crate.items)
36 item->accept_vis (*this);
37 });
52219746
AC
38}
39
40void
41EarlyNameResolver::resolve_generic_args (AST::GenericArgs &generic_args)
42{
43 for (auto &arg : generic_args.get_generic_args ())
44 arg.accept_vis (*this);
45
46 for (auto &arg : generic_args.get_binding_args ())
47 arg.get_type ()->accept_vis (*this);
48}
49
50void
51EarlyNameResolver::resolve_qualified_path_type (AST::QualifiedPathType &path)
52{
53 path.get_type ()->accept_vis (*this);
54
55 if (path.has_as_clause ())
56 path.get_as_type_path ().accept_vis (*this);
57}
58
59void
9f455ed8 60EarlyNameResolver::visit (AST::Token &)
52219746
AC
61{}
62
63void
9f455ed8 64EarlyNameResolver::visit (AST::DelimTokenTree &)
52219746
AC
65{}
66
67void
9f455ed8 68EarlyNameResolver::visit (AST::AttrInputMetaItemContainer &)
52219746
AC
69{}
70
71void
9f455ed8 72EarlyNameResolver::visit (AST::IdentifierExpr &)
52219746
AC
73{}
74
75void
9f455ed8 76EarlyNameResolver::visit (AST::Lifetime &)
52219746
AC
77{}
78
79void
9f455ed8 80EarlyNameResolver::visit (AST::LifetimeParam &)
52219746
AC
81{}
82
83void
9f455ed8 84EarlyNameResolver::visit (AST::ConstGenericParam &)
52219746
AC
85{}
86
87// FIXME: ARTHUR: Do we need to perform macro resolution for paths as well?
88// std::arch::asm!()?
89void
90EarlyNameResolver::visit (AST::PathInExpression &path)
91{
92 for (auto &segment : path.get_segments ())
93 if (segment.has_generic_args ())
94 resolve_generic_args (segment.get_generic_args ());
95}
96
97void
9f455ed8 98EarlyNameResolver::visit (AST::TypePathSegment &)
52219746
AC
99{}
100
101void
102EarlyNameResolver::visit (AST::TypePathSegmentGeneric &segment)
103{
104 if (segment.has_generic_args ())
105 resolve_generic_args (segment.get_generic_args ());
106}
107
108void
109EarlyNameResolver::visit (AST::TypePathSegmentFunction &segment)
110{
111 for (auto &type : segment.get_type_path_function ().get_params ())
112 type->accept_vis (*this);
113
114 segment.get_type_path_function ().get_return_type ()->accept_vis (*this);
115}
116
117void
118EarlyNameResolver::visit (AST::TypePath &path)
119{
120 for (auto &seg : path.get_segments ())
121 seg->accept_vis (*this);
122}
123
124void
125EarlyNameResolver::visit (AST::QualifiedPathInExpression &path)
126{
127 resolve_qualified_path_type (path.get_qualified_path_type ());
128
129 for (auto &segment : path.get_segments ())
130 if (segment.has_generic_args ())
131 resolve_generic_args (segment.get_generic_args ());
132}
133
134void
135EarlyNameResolver::visit (AST::QualifiedPathInType &path)
136{
137 resolve_qualified_path_type (path.get_qualified_path_type ());
138
139 for (auto &segment : path.get_segments ())
140 segment->accept_vis (*this);
141}
142
143void
9f455ed8 144EarlyNameResolver::visit (AST::LiteralExpr &)
52219746
AC
145{}
146
147void
9f455ed8 148EarlyNameResolver::visit (AST::AttrInputLiteral &)
52219746
AC
149{}
150
151void
9f455ed8 152EarlyNameResolver::visit (AST::MetaItemLitExpr &)
52219746
AC
153{}
154
155void
9f455ed8 156EarlyNameResolver::visit (AST::MetaItemPathLit &)
52219746
AC
157{}
158
159void
160EarlyNameResolver::visit (AST::BorrowExpr &expr)
161{
162 expr.get_borrowed_expr ()->accept_vis (*this);
163}
164
165void
166EarlyNameResolver::visit (AST::DereferenceExpr &expr)
167{
168 expr.get_dereferenced_expr ()->accept_vis (*this);
169}
170
171void
172EarlyNameResolver::visit (AST::ErrorPropagationExpr &expr)
173{
174 expr.get_propagating_expr ()->accept_vis (*this);
175}
176
177void
178EarlyNameResolver::visit (AST::NegationExpr &expr)
179{
180 expr.get_negated_expr ()->accept_vis (*this);
181}
182
183void
184EarlyNameResolver::visit (AST::ArithmeticOrLogicalExpr &expr)
185{
186 expr.get_left_expr ()->accept_vis (*this);
187 expr.get_right_expr ()->accept_vis (*this);
188}
189
190void
191EarlyNameResolver::visit (AST::ComparisonExpr &expr)
192{
193 expr.get_left_expr ()->accept_vis (*this);
194 expr.get_right_expr ()->accept_vis (*this);
195}
196
197void
198EarlyNameResolver::visit (AST::LazyBooleanExpr &expr)
199{
200 expr.get_left_expr ()->accept_vis (*this);
201 expr.get_right_expr ()->accept_vis (*this);
202}
203
204void
205EarlyNameResolver::visit (AST::TypeCastExpr &expr)
206{
207 expr.get_casted_expr ()->accept_vis (*this);
208 expr.get_type_to_cast_to ()->accept_vis (*this);
209}
210
211void
212EarlyNameResolver::visit (AST::AssignmentExpr &expr)
213{
214 expr.get_left_expr ()->accept_vis (*this);
215 expr.get_right_expr ()->accept_vis (*this);
216}
217
218void
219EarlyNameResolver::visit (AST::CompoundAssignmentExpr &expr)
220{
221 expr.get_left_expr ()->accept_vis (*this);
222 expr.get_right_expr ()->accept_vis (*this);
223}
224
225void
226EarlyNameResolver::visit (AST::GroupedExpr &expr)
227{
228 expr.get_expr_in_parens ()->accept_vis (*this);
229}
230
231void
232EarlyNameResolver::visit (AST::ArrayElemsValues &elems)
233{
234 for (auto &expr : elems.get_values ())
235 expr->accept_vis (*this);
236}
237
238void
239EarlyNameResolver::visit (AST::ArrayElemsCopied &elems)
240{
241 elems.get_elem_to_copy ()->accept_vis (*this);
242}
243
244void
245EarlyNameResolver::visit (AST::ArrayExpr &expr)
246{
247 expr.get_array_elems ()->accept_vis (*this);
248}
249
250void
251EarlyNameResolver::visit (AST::ArrayIndexExpr &expr)
252{
253 expr.get_array_expr ()->accept_vis (*this);
254 expr.get_index_expr ()->accept_vis (*this);
255}
256
257void
258EarlyNameResolver::visit (AST::TupleExpr &expr)
259{
260 for (auto &elem : expr.get_tuple_elems ())
261 elem->accept_vis (*this);
262}
263
264void
265EarlyNameResolver::visit (AST::TupleIndexExpr &expr)
266{
267 expr.get_tuple_expr ()->accept_vis (*this);
268}
269
270void
9f455ed8 271EarlyNameResolver::visit (AST::StructExprStruct &)
52219746
AC
272{}
273
274void
9f455ed8 275EarlyNameResolver::visit (AST::StructExprFieldIdentifier &)
52219746
AC
276{}
277
278void
279EarlyNameResolver::visit (AST::StructExprFieldIdentifierValue &field)
280{
281 field.get_value ()->accept_vis (*this);
282}
283
284void
285EarlyNameResolver::visit (AST::StructExprFieldIndexValue &field)
286{
287 field.get_value ()->accept_vis (*this);
288}
289
290void
291EarlyNameResolver::visit (AST::StructExprStructFields &expr)
292{
293 for (auto &field : expr.get_fields ())
294 field->accept_vis (*this);
295}
296
297void
9f455ed8 298EarlyNameResolver::visit (AST::StructExprStructBase &)
52219746
AC
299{}
300
301void
302EarlyNameResolver::visit (AST::CallExpr &expr)
303{
304 expr.get_function_expr ()->accept_vis (*this);
305 for (auto &param : expr.get_params ())
306 param->accept_vis (*this);
307}
308
309void
310EarlyNameResolver::visit (AST::MethodCallExpr &expr)
311{
312 expr.get_receiver_expr ()->accept_vis (*this);
313 for (auto &param : expr.get_params ())
314 param->accept_vis (*this);
315}
316
317void
318EarlyNameResolver::visit (AST::FieldAccessExpr &expr)
319{
320 expr.get_receiver_expr ()->accept_vis (*this);
321}
322
323void
324EarlyNameResolver::visit (AST::ClosureExprInner &expr)
325{
326 expr.get_definition_expr ()->accept_vis (*this);
327
328 for (auto &param : expr.get_params ())
329 param.get_type ()->accept_vis (*this);
330}
331
332void
333EarlyNameResolver::visit (AST::BlockExpr &expr)
334{
38216691
AC
335 scoped (expr.get_node_id (), [&expr, this] () {
336 for (auto &stmt : expr.get_statements ())
337 stmt->accept_vis (*this);
52219746 338
38216691
AC
339 if (expr.has_tail_expr ())
340 expr.get_tail_expr ()->accept_vis (*this);
341 });
52219746
AC
342}
343
344void
345EarlyNameResolver::visit (AST::ClosureExprInnerTyped &expr)
346{
347 expr.get_definition_block ()->accept_vis (*this);
348
349 for (auto &param : expr.get_params ())
350 param.get_type ()->accept_vis (*this);
351}
352
353void
9f455ed8 354EarlyNameResolver::visit (AST::ContinueExpr &)
52219746
AC
355{}
356
357void
358EarlyNameResolver::visit (AST::BreakExpr &expr)
359{
360 if (expr.has_break_expr ())
361 expr.get_break_expr ()->accept_vis (*this);
362}
363
364void
365EarlyNameResolver::visit (AST::RangeFromToExpr &expr)
366{
367 expr.get_from_expr ()->accept_vis (*this);
368 expr.get_to_expr ()->accept_vis (*this);
369}
370
371void
372EarlyNameResolver::visit (AST::RangeFromExpr &expr)
373{
374 expr.get_from_expr ()->accept_vis (*this);
375}
376
377void
378EarlyNameResolver::visit (AST::RangeToExpr &expr)
379{
380 expr.get_to_expr ()->accept_vis (*this);
381}
382
383void
9f455ed8 384EarlyNameResolver::visit (AST::RangeFullExpr &)
52219746
AC
385{}
386
387void
388EarlyNameResolver::visit (AST::RangeFromToInclExpr &expr)
389{
390 expr.get_from_expr ()->accept_vis (*this);
391 expr.get_to_expr ()->accept_vis (*this);
392}
393
394void
395EarlyNameResolver::visit (AST::RangeToInclExpr &expr)
396{
397 expr.get_to_expr ()->accept_vis (*this);
398}
399
400void
401EarlyNameResolver::visit (AST::ReturnExpr &expr)
402{
403 if (expr.has_returned_expr ())
404 expr.get_returned_expr ()->accept_vis (*this);
405}
406
407void
408EarlyNameResolver::visit (AST::UnsafeBlockExpr &expr)
409{
410 expr.get_block_expr ()->accept_vis (*this);
411}
412
413void
414EarlyNameResolver::visit (AST::LoopExpr &expr)
415{
416 expr.get_loop_block ()->accept_vis (*this);
417}
418
419void
420EarlyNameResolver::visit (AST::WhileLoopExpr &expr)
421{
422 expr.get_predicate_expr ()->accept_vis (*this);
423 expr.get_loop_block ()->accept_vis (*this);
424}
425
426void
427EarlyNameResolver::visit (AST::WhileLetLoopExpr &expr)
428{
429 expr.get_scrutinee_expr ()->accept_vis (*this);
430 expr.get_loop_block ()->accept_vis (*this);
431}
432
433void
434EarlyNameResolver::visit (AST::ForLoopExpr &expr)
435{
38216691
AC
436 scoped (expr.get_node_id (), [&expr, this] () {
437 expr.get_pattern ()->accept_vis (*this);
438 expr.get_iterator_expr ()->accept_vis (*this);
439 expr.get_loop_block ()->accept_vis (*this);
440 });
52219746
AC
441}
442
443void
444EarlyNameResolver::visit (AST::IfExpr &expr)
445{
446 expr.get_condition_expr ()->accept_vis (*this);
447 expr.get_if_block ()->accept_vis (*this);
448}
449
450void
451EarlyNameResolver::visit (AST::IfExprConseqElse &expr)
452{
453 expr.get_condition_expr ()->accept_vis (*this);
454 expr.get_if_block ()->accept_vis (*this);
455 expr.get_else_block ()->accept_vis (*this);
456}
457
458void
459EarlyNameResolver::visit (AST::IfExprConseqIf &expr)
460{
461 expr.get_condition_expr ()->accept_vis (*this);
462 expr.get_if_block ()->accept_vis (*this);
463 expr.get_conseq_if_expr ()->accept_vis (*this);
464}
465
466void
467EarlyNameResolver::visit (AST::IfExprConseqIfLet &expr)
468{
469 expr.get_condition_expr ()->accept_vis (*this);
470 expr.get_if_block ()->accept_vis (*this);
471 expr.get_conseq_if_let_expr ()->accept_vis (*this);
472}
473
474void
475EarlyNameResolver::visit (AST::IfLetExpr &expr)
476{
477 expr.get_value_expr ()->accept_vis (*this);
38216691
AC
478
479 scoped (expr.get_node_id (),
480 [&expr, this] () { expr.get_if_block ()->accept_vis (*this); });
52219746
AC
481}
482
483void
484EarlyNameResolver::visit (AST::IfLetExprConseqElse &expr)
485{
486 expr.get_value_expr ()->accept_vis (*this);
487 expr.get_if_block ()->accept_vis (*this);
488 expr.get_else_block ()->accept_vis (*this);
489}
490
491void
492EarlyNameResolver::visit (AST::IfLetExprConseqIf &expr)
493{
494 expr.get_value_expr ()->accept_vis (*this);
495 expr.get_if_block ()->accept_vis (*this);
496 expr.get_conseq_if_expr ()->accept_vis (*this);
497}
498
499void
500EarlyNameResolver::visit (AST::IfLetExprConseqIfLet &expr)
501{
502 expr.get_value_expr ()->accept_vis (*this);
503 expr.get_if_block ()->accept_vis (*this);
504 expr.get_conseq_if_let_expr ()->accept_vis (*this);
505}
506
507void
508EarlyNameResolver::visit (AST::MatchExpr &expr)
509{
510 expr.get_scrutinee_expr ()->accept_vis (*this);
52219746 511
38216691
AC
512 scoped (expr.get_node_id (), [&expr, this] () {
513 for (auto &arm : expr.get_match_cases ())
514 {
515 scoped (arm.get_node_id (), [&arm, this] () {
516 if (arm.get_arm ().has_match_arm_guard ())
517 arm.get_arm ().get_guard_expr ()->accept_vis (*this);
52219746 518
38216691
AC
519 for (auto &pattern : arm.get_arm ().get_patterns ())
520 pattern->accept_vis (*this);
521
522 arm.get_expr ()->accept_vis (*this);
523 });
524 }
525 });
52219746
AC
526}
527
528void
529EarlyNameResolver::visit (AST::AwaitExpr &expr)
530{
531 expr.get_awaited_expr ()->accept_vis (*this);
532}
533
534void
535EarlyNameResolver::visit (AST::AsyncBlockExpr &expr)
536{
537 expr.get_block_expr ()->accept_vis (*this);
538}
539
540void
541EarlyNameResolver::visit (AST::TypeParam &param)
542{
543 for (auto &bound : param.get_type_param_bounds ())
544 bound->accept_vis (*this);
545
546 if (param.has_type ())
547 param.get_type ()->accept_vis (*this);
548}
549
550void
9f455ed8 551EarlyNameResolver::visit (AST::LifetimeWhereClauseItem &)
52219746
AC
552{}
553
554void
555EarlyNameResolver::visit (AST::TypeBoundWhereClauseItem &item)
556{
557 for (auto &bound : item.get_type_param_bounds ())
558 bound->accept_vis (*this);
559}
560
561void
562EarlyNameResolver::visit (AST::Method &method)
563{
564 if (method.has_generics ())
565 for (auto &generic : method.get_generic_params ())
566 generic->accept_vis (*this);
567
568 if (method.get_self_param ().has_type ())
569 method.get_self_param ().get_type ()->accept_vis (*this);
570
571 for (auto &param : method.get_function_params ())
572 param.get_type ()->accept_vis (*this);
573
574 if (method.has_return_type ())
575 method.get_return_type ()->accept_vis (*this);
576
577 method.get_definition ()->accept_vis (*this);
578}
579
580void
581EarlyNameResolver::visit (AST::Module &module)
582{
38216691
AC
583 scoped (module.get_node_id (), [&module, this] () {
584 for (auto &item : module.get_items ())
585 item->accept_vis (*this);
586 });
52219746
AC
587}
588
589void
9f455ed8 590EarlyNameResolver::visit (AST::ExternCrate &)
52219746
AC
591{}
592
593void
9f455ed8 594EarlyNameResolver::visit (AST::UseTreeGlob &)
52219746
AC
595{}
596
597void
9f455ed8 598EarlyNameResolver::visit (AST::UseTreeList &)
52219746
AC
599{}
600
601void
9f455ed8 602EarlyNameResolver::visit (AST::UseTreeRebind &)
52219746
AC
603{}
604
605void
9f455ed8 606EarlyNameResolver::visit (AST::UseDeclaration &)
52219746
AC
607{}
608
609void
610EarlyNameResolver::visit (AST::Function &function)
611{
612 if (function.has_generics ())
613 for (auto &generic : function.get_generic_params ())
614 generic->accept_vis (*this);
615
616 for (auto &param : function.get_function_params ())
617 param.get_type ()->accept_vis (*this);
618
619 if (function.has_return_type ())
620 function.get_return_type ()->accept_vis (*this);
621
622 function.get_definition ()->accept_vis (*this);
623}
624
625void
626EarlyNameResolver::visit (AST::TypeAlias &type_alias)
627{
628 type_alias.get_type_aliased ()->accept_vis (*this);
629}
630
631void
632EarlyNameResolver::visit (AST::StructStruct &struct_item)
633{
634 for (auto &field : struct_item.get_fields ())
635 field.get_field_type ()->accept_vis (*this);
636}
637
638void
639EarlyNameResolver::visit (AST::TupleStruct &tuple_struct)
640{
641 for (auto &field : tuple_struct.get_fields ())
642 field.get_field_type ()->accept_vis (*this);
643}
644
645void
9f455ed8 646EarlyNameResolver::visit (AST::EnumItem &)
52219746
AC
647{}
648
649void
9f455ed8 650EarlyNameResolver::visit (AST::EnumItemTuple &)
52219746
AC
651{}
652
653void
9f455ed8 654EarlyNameResolver::visit (AST::EnumItemStruct &)
52219746
AC
655{}
656
657void
9f455ed8 658EarlyNameResolver::visit (AST::EnumItemDiscriminant &)
52219746
AC
659{}
660
661void
9f455ed8 662EarlyNameResolver::visit (AST::Enum &)
52219746
AC
663{}
664
665void
9f455ed8 666EarlyNameResolver::visit (AST::Union &)
52219746
AC
667{}
668
669void
670EarlyNameResolver::visit (AST::ConstantItem &const_item)
671{
672 const_item.get_type ()->accept_vis (*this);
673 const_item.get_expr ()->accept_vis (*this);
674}
675
676void
677EarlyNameResolver::visit (AST::StaticItem &static_item)
678{
679 static_item.get_type ()->accept_vis (*this);
680 static_item.get_expr ()->accept_vis (*this);
681}
682
683void
684EarlyNameResolver::visit (AST::TraitItemFunc &item)
685{
686 auto &decl = item.get_trait_function_decl ();
687
688 if (decl.has_return_type ())
689 decl.get_return_type ()->accept_vis (*this);
690
691 for (auto &generic : decl.get_generic_params ())
692 generic->accept_vis (*this);
693
694 for (auto &param : decl.get_function_params ())
695 param.get_type ()->accept_vis (*this);
696
697 if (item.has_definition ())
698 item.get_definition ()->accept_vis (*this);
699}
700
701void
702EarlyNameResolver::visit (AST::TraitItemMethod &item)
703{
704 // FIXME: Can we factor this with the above function?
705 auto &decl = item.get_trait_method_decl ();
706
707 if (decl.has_return_type ())
708 decl.get_return_type ()->accept_vis (*this);
709
710 for (auto &generic : decl.get_generic_params ())
711 generic->accept_vis (*this);
712
713 for (auto &param : decl.get_function_params ())
714 param.get_type ()->accept_vis (*this);
715
716 if (item.has_definition ())
717 item.get_definition ()->accept_vis (*this);
718}
719
720void
721EarlyNameResolver::visit (AST::TraitItemConst &item)
722{
723 item.get_type ()->accept_vis (*this);
724
725 if (item.has_expr ())
726 item.get_expr ()->accept_vis (*this);
727}
728
729void
9f455ed8 730EarlyNameResolver::visit (AST::TraitItemType &)
52219746
AC
731{}
732
733void
734EarlyNameResolver::visit (AST::Trait &trait)
735{
38216691
AC
736 for (auto &generic : trait.get_generic_params ())
737 generic->accept_vis (*this);
738
739 scoped (trait.get_node_id (), [&trait, this] () {
740 for (auto &item : trait.get_trait_items ())
741 item->accept_vis (*this);
742 });
52219746
AC
743}
744
745void
746EarlyNameResolver::visit (AST::InherentImpl &impl)
747{
748 impl.get_type ()->accept_vis (*this);
749
750 for (auto &generic : impl.get_generic_params ())
751 generic->accept_vis (*this);
752
38216691
AC
753 scoped (impl.get_node_id (), [&impl, this] () {
754 for (auto &item : impl.get_impl_items ())
755 item->accept_vis (*this);
756 });
52219746
AC
757}
758
759void
760EarlyNameResolver::visit (AST::TraitImpl &impl)
761{
762 impl.get_type ()->accept_vis (*this);
763
764 for (auto &generic : impl.get_generic_params ())
765 generic->accept_vis (*this);
766
38216691
AC
767 scoped (impl.get_node_id (), [&impl, this] () {
768 for (auto &item : impl.get_impl_items ())
769 item->accept_vis (*this);
770 });
52219746
AC
771}
772
773void
774EarlyNameResolver::visit (AST::ExternalStaticItem &item)
775{
776 item.get_type ()->accept_vis (*this);
777}
778
779void
780EarlyNameResolver::visit (AST::ExternalFunctionItem &item)
781{
782 for (auto &generic : item.get_generic_params ())
783 generic->accept_vis (*this);
784
785 for (auto &param : item.get_function_params ())
786 param.get_type ()->accept_vis (*this);
787
788 if (item.has_return_type ())
789 item.get_return_type ()->accept_vis (*this);
790}
791
792void
793EarlyNameResolver::visit (AST::ExternBlock &block)
794{
38216691
AC
795 scoped (block.get_node_id (), [&block, this] () {
796 for (auto &item : block.get_extern_items ())
797 item->accept_vis (*this);
798 });
52219746
AC
799}
800
801void
9f455ed8 802EarlyNameResolver::visit (AST::MacroMatchFragment &)
52219746
AC
803{}
804
805void
9f455ed8 806EarlyNameResolver::visit (AST::MacroMatchRepetition &)
52219746
AC
807{}
808
809void
9f455ed8 810EarlyNameResolver::visit (AST::MacroMatcher &)
52219746
AC
811{}
812
813void
814EarlyNameResolver::visit (AST::MacroRulesDefinition &rules_def)
815{
816 auto path = CanonicalPath::new_seg (rules_def.get_node_id (),
817 rules_def.get_rule_name ());
818 resolver.get_macro_scope ().insert (path, rules_def.get_node_id (),
819 rules_def.get_locus ());
38216691
AC
820
821 /* Since the EarlyNameResolver runs multiple time (fixed point algorithm)
822 * we could be inserting the same macro def over and over again until we
823 * implement some optimizations */
824 // FIXME: ARTHUR: Remove that lookup and add proper optimizations instead
825 AST::MacroRulesDefinition *tmp = nullptr;
826 if (mappings.lookup_macro_def (rules_def.get_node_id (), &tmp))
827 return;
828
52219746
AC
829 mappings.insert_macro_def (&rules_def);
830 rust_debug_loc (rules_def.get_locus (), "inserting macro def: [%s]",
831 path.get ().c_str ());
832}
833
834void
835EarlyNameResolver::visit (AST::MacroInvocation &invoc)
836{
837 auto &invoc_data = invoc.get_invoc_data ();
838 auto has_semicolon = invoc.has_semicolon ();
839
38216691
AC
840 if (invoc.get_kind () == AST::MacroInvocation::InvocKind::Builtin)
841 for (auto &pending_invoc : invoc.get_pending_eager_invocations ())
842 pending_invoc->accept_vis (*this);
843
52219746
AC
844 // ??
845 // switch on type of macro:
846 // - '!' syntax macro (inner switch)
847 // - procedural macro - "A token-based function-like macro"
848 // - 'macro_rules' (by example/pattern-match) macro? or not? "an
849 // AST-based function-like macro"
850 // - else is unreachable
851 // - attribute syntax macro (inner switch)
852 // - procedural macro attribute syntax - "A token-based attribute
853 // macro"
854 // - legacy macro attribute syntax? - "an AST-based attribute macro"
855 // - non-macro attribute: mark known
856 // - else is unreachable
857 // - derive macro (inner switch)
858 // - derive or legacy derive - "token-based" vs "AST-based"
859 // - else is unreachable
860 // - derive container macro - unreachable
861
862 // lookup the rules for this macro
863 NodeId resolved_node = UNKNOWN_NODEID;
864 NodeId source_node = UNKNOWN_NODEID;
865 if (has_semicolon)
866 source_node = invoc.get_macro_node_id ();
867 else
868 source_node = invoc.get_pattern_node_id ();
869 auto seg
870 = CanonicalPath::new_seg (source_node, invoc_data.get_path ().as_string ());
871
872 bool found = resolver.get_macro_scope ().lookup (seg, &resolved_node);
873 if (!found)
874 {
875 rust_error_at (invoc.get_locus (), "unknown macro: [%s]",
876 seg.get ().c_str ());
877 return;
878 }
879
880 // lookup the rules
881 AST::MacroRulesDefinition *rules_def = nullptr;
882 bool ok = mappings.lookup_macro_def (resolved_node, &rules_def);
883 rust_assert (ok);
884
38216691
AC
885 auto &outer_attrs = rules_def->get_outer_attrs ();
886 bool is_builtin
887 = std::any_of (outer_attrs.begin (), outer_attrs.end (),
888 [] (AST::Attribute attr) {
889 return attr.get_path () == "rustc_builtin_macro";
890 });
891
892 if (is_builtin)
893 {
894 auto builtin_kind
895 = AST::builtin_macro_from_string (rules_def->get_rule_name ());
896 invoc.map_to_builtin (builtin_kind);
897 }
898
899 auto attributes = rules_def->get_outer_attrs ();
900
901 /* Since the EarlyNameResolver runs multiple time (fixed point algorithm)
902 * we could be inserting the same macro def over and over again until we
903 * implement some optimizations */
904 // FIXME: ARTHUR: Remove that lookup and add proper optimizations instead
905 AST::MacroRulesDefinition *tmp_def = nullptr;
906 if (mappings.lookup_macro_invocation (invoc, &tmp_def))
907 return;
908
52219746
AC
909 mappings.insert_macro_invocation (invoc, rules_def);
910}
911
912// FIXME: ARTHUR: Do we need to resolve these as well here?
913
914void
9f455ed8 915EarlyNameResolver::visit (AST::MetaItemPath &)
52219746
AC
916{}
917
918void
9f455ed8 919EarlyNameResolver::visit (AST::MetaItemSeq &)
52219746
AC
920{}
921
922void
9f455ed8 923EarlyNameResolver::visit (AST::MetaWord &)
52219746
AC
924{}
925
926void
9f455ed8 927EarlyNameResolver::visit (AST::MetaNameValueStr &)
52219746
AC
928{}
929
930void
9f455ed8 931EarlyNameResolver::visit (AST::MetaListPaths &)
52219746
AC
932{}
933
934void
9f455ed8 935EarlyNameResolver::visit (AST::MetaListNameValueStr &)
52219746
AC
936{}
937
938void
9f455ed8 939EarlyNameResolver::visit (AST::LiteralPattern &)
52219746
AC
940{}
941
942void
943EarlyNameResolver::visit (AST::IdentifierPattern &pattern)
944{
945 if (pattern.has_pattern_to_bind ())
946 pattern.get_pattern_to_bind ()->accept_vis (*this);
947}
948
949void
9f455ed8 950EarlyNameResolver::visit (AST::WildcardPattern &)
52219746
AC
951{}
952
953void
9f455ed8 954EarlyNameResolver::visit (AST::RangePatternBoundLiteral &)
52219746
AC
955{}
956
957void
9f455ed8 958EarlyNameResolver::visit (AST::RangePatternBoundPath &)
52219746
AC
959{}
960
961void
9f455ed8 962EarlyNameResolver::visit (AST::RangePatternBoundQualPath &)
52219746
AC
963{}
964
965void
966EarlyNameResolver::visit (AST::RangePattern &pattern)
967{
968 pattern.get_lower_bound ()->accept_vis (*this);
969 pattern.get_upper_bound ()->accept_vis (*this);
970}
971
972void
973EarlyNameResolver::visit (AST::ReferencePattern &pattern)
974{
975 pattern.get_referenced_pattern ()->accept_vis (*this);
976}
977
978void
979EarlyNameResolver::visit (AST::StructPatternFieldTuplePat &field)
980{
981 field.get_index_pattern ()->accept_vis (*this);
982}
983
984void
985EarlyNameResolver::visit (AST::StructPatternFieldIdentPat &field)
986{
987 field.get_ident_pattern ()->accept_vis (*this);
988}
989
990void
9f455ed8 991EarlyNameResolver::visit (AST::StructPatternFieldIdent &)
52219746
AC
992{}
993
994void
9f455ed8 995EarlyNameResolver::visit (AST::StructPattern &)
52219746
AC
996{}
997
998void
999EarlyNameResolver::visit (AST::TupleStructItemsNoRange &tuple_items)
1000{
1001 for (auto &pattern : tuple_items.get_patterns ())
1002 pattern->accept_vis (*this);
1003}
1004
1005void
1006EarlyNameResolver::visit (AST::TupleStructItemsRange &tuple_items)
1007{
1008 for (auto &pattern : tuple_items.get_lower_patterns ())
1009 pattern->accept_vis (*this);
1010 for (auto &pattern : tuple_items.get_upper_patterns ())
1011 pattern->accept_vis (*this);
1012}
1013
1014void
1015EarlyNameResolver::visit (AST::TupleStructPattern &pattern)
1016{
1017 pattern.get_items ()->accept_vis (*this);
1018}
1019
1020void
1021EarlyNameResolver::visit (AST::TuplePatternItemsMultiple &tuple_items)
1022{
1023 for (auto &pattern : tuple_items.get_patterns ())
1024 pattern->accept_vis (*this);
1025}
1026
1027void
1028EarlyNameResolver::visit (AST::TuplePatternItemsRanged &tuple_items)
1029{
1030 for (auto &pattern : tuple_items.get_lower_patterns ())
1031 pattern->accept_vis (*this);
1032 for (auto &pattern : tuple_items.get_upper_patterns ())
1033 pattern->accept_vis (*this);
1034}
1035
1036void
1037EarlyNameResolver::visit (AST::TuplePattern &pattern)
1038{
1039 pattern.get_items ()->accept_vis (*this);
1040}
1041
1042void
1043EarlyNameResolver::visit (AST::GroupedPattern &pattern)
1044{
1045 pattern.get_pattern_in_parens ()->accept_vis (*this);
1046}
1047
1048void
1049EarlyNameResolver::visit (AST::SlicePattern &pattern)
1050{
1051 for (auto &item : pattern.get_items ())
1052 item->accept_vis (*this);
1053}
1054
8628486f
OA
1055void
1056EarlyNameResolver::visit (AST::AltPattern &pattern)
1057{
1058 for (auto &alt : pattern.get_alts ())
1059 alt->accept_vis (*this);
1060}
1061
52219746 1062void
9f455ed8 1063EarlyNameResolver::visit (AST::EmptyStmt &)
52219746
AC
1064{}
1065
1066void
1067EarlyNameResolver::visit (AST::LetStmt &stmt)
1068{
1069 if (stmt.has_type ())
1070 stmt.get_type ()->accept_vis (*this);
1071
1072 if (stmt.has_init_expr ())
1073 stmt.get_init_expr ()->accept_vis (*this);
1074
1075 stmt.get_pattern ()->accept_vis (*this);
1076}
1077
1078void
1079EarlyNameResolver::visit (AST::ExprStmtWithoutBlock &stmt)
1080{
1081 stmt.get_expr ()->accept_vis (*this);
1082}
1083
1084void
1085EarlyNameResolver::visit (AST::ExprStmtWithBlock &stmt)
1086{
1087 stmt.get_expr ()->accept_vis (*this);
1088}
1089
1090void
9f455ed8 1091EarlyNameResolver::visit (AST::TraitBound &)
52219746
AC
1092{}
1093
1094void
9f455ed8 1095EarlyNameResolver::visit (AST::ImplTraitType &)
52219746
AC
1096{}
1097
1098void
9f455ed8 1099EarlyNameResolver::visit (AST::TraitObjectType &)
52219746
AC
1100{}
1101
1102void
9f455ed8 1103EarlyNameResolver::visit (AST::ParenthesisedType &)
52219746
AC
1104{}
1105
1106void
9f455ed8 1107EarlyNameResolver::visit (AST::ImplTraitTypeOneBound &)
52219746
AC
1108{}
1109
1110void
9f455ed8 1111EarlyNameResolver::visit (AST::TraitObjectTypeOneBound &)
52219746
AC
1112{}
1113
1114void
9f455ed8 1115EarlyNameResolver::visit (AST::TupleType &)
52219746
AC
1116{}
1117
1118void
9f455ed8 1119EarlyNameResolver::visit (AST::NeverType &)
52219746
AC
1120{}
1121
1122void
9f455ed8 1123EarlyNameResolver::visit (AST::RawPointerType &)
52219746
AC
1124{}
1125
1126void
9f455ed8 1127EarlyNameResolver::visit (AST::ReferenceType &)
52219746
AC
1128{}
1129
1130void
9f455ed8 1131EarlyNameResolver::visit (AST::ArrayType &)
52219746
AC
1132{}
1133
1134void
9f455ed8 1135EarlyNameResolver::visit (AST::SliceType &)
52219746
AC
1136{}
1137
1138void
9f455ed8 1139EarlyNameResolver::visit (AST::InferredType &)
52219746
AC
1140{}
1141
1142void
1143EarlyNameResolver::visit (AST::BareFunctionType &type)
1144{
1145 for (auto &param : type.get_function_params ())
1146 param.get_type ()->accept_vis (*this);
1147
1148 if (type.has_return_type ())
1149 type.get_return_type ()->accept_vis (*this);
1150}
1151
1152} // namespace Resolver
1153} // namespace Rust