1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-default-resolver.h"
20 #include "rust-ast-full.h"
21 #include "rust-ast-visitor.h"
22 #include "rust-item.h"
25 namespace Resolver2_0
{
28 DefaultResolver::visit (AST::BlockExpr
&expr
)
30 // extracting the lambda from the `scoped` call otherwise the code looks like
31 // a hot turd thanks to our .clang-format
33 auto inner_fn
= [this, &expr
] () {
34 for (auto &stmt
: expr
.get_statements ())
35 stmt
->accept_vis (*this);
37 if (expr
.has_tail_expr ())
38 expr
.get_tail_expr ().accept_vis (*this);
41 ctx
.scoped (Rib::Kind::Normal
, expr
.get_node_id (), inner_fn
);
45 DefaultResolver::visit (AST::Module
&module
)
47 auto item_fn
= [this, &module
] () {
48 for (auto &item
: module
.get_items ())
49 item
->accept_vis (*this);
52 ctx
.scoped (Rib::Kind::Module
, module
.get_node_id (), item_fn
,
57 DefaultResolver::visit (AST::Function
&function
)
59 auto def_fn
= [this, &function
] () {
60 for (auto &p
: function
.get_function_params ())
62 if (p
->is_variadic ())
64 auto ¶m
= static_cast<AST::VariadicParam
&> (*p
);
65 if (param
.has_pattern ())
66 param
.get_pattern ().accept_vis (*this);
68 else if (p
->is_self ())
70 auto ¶m
= static_cast<AST::SelfParam
&> (*p
);
71 param
.get_type ().accept_vis (*this);
72 param
.get_lifetime ().accept_vis (*this);
76 auto ¶m
= static_cast<AST::FunctionParam
&> (*p
);
77 param
.get_pattern ().accept_vis (*this);
78 param
.get_type ().accept_vis (*this);
82 if (function
.has_return_type ())
83 visit (function
.get_return_type ());
85 if (function
.has_body ())
86 function
.get_definition ().value ()->accept_vis (*this);
89 ctx
.scoped (Rib::Kind::Function
, function
.get_node_id (), def_fn
);
93 DefaultResolver::visit (AST::ForLoopExpr
&expr
)
95 ctx
.scoped (Rib::Kind::Normal
, expr
.get_node_id (), [this, &expr
] () {
96 expr
.get_pattern ().accept_vis (*this);
97 expr
.get_iterator_expr ().accept_vis (*this);
98 expr
.get_loop_block ().accept_vis (*this);
103 DefaultResolver::visit (AST::Trait
&trait
)
105 auto inner_fn
= [this, &trait
] () {
106 for (auto &item
: trait
.get_trait_items ())
107 item
->accept_vis (*this);
110 ctx
.scoped (Rib::Kind::TraitOrImpl
, trait
.get_node_id (), inner_fn
,
111 trait
.get_identifier () /* FIXME: Is that valid?*/);
115 DefaultResolver::visit (AST::InherentImpl
&impl
)
117 auto inner_fn
= [this, &impl
] () {
118 visit (impl
.get_type ());
119 for (auto &item
: impl
.get_impl_items ())
120 item
->accept_vis (*this);
123 ctx
.scoped (Rib::Kind::TraitOrImpl
, impl
.get_node_id (), inner_fn
);
127 DefaultResolver::visit (AST::TraitImpl
&impl
)
129 auto inner_fn
= [this, &impl
] () {
130 for (auto &item
: impl
.get_impl_items ())
131 item
->accept_vis (*this);
134 ctx
.scoped (Rib::Kind::TraitOrImpl
, impl
.get_node_id (), inner_fn
);
138 DefaultResolver::visit (AST::StructStruct
&type
)
140 // do we need to scope anything here? no, right?
142 // we also can't visit `StructField`s by default, so there's nothing to do -
143 // correct? or should we do something like
145 AST::DefaultASTVisitor::visit (type
);
151 DefaultResolver::visit (AST::Enum
&type
)
153 // FIXME: Do we need to scope anything by default?
155 auto variant_fn
= [this, &type
] () {
156 for (auto &variant
: type
.get_variants ())
157 variant
->accept_vis (*this);
160 ctx
.scoped (Rib::Kind::Item
/* FIXME: Correct? */, type
.get_node_id (),
161 variant_fn
, type
.get_identifier ());
165 DefaultResolver::visit (AST::StructExprFieldIdentifierValue
&)
169 DefaultResolver::visit (AST::StructExprFieldIndexValue
&)
173 DefaultResolver::visit (AST::ClosureExprInner
&expr
)
175 if (expr
.is_marked_for_strip ())
178 for (auto ¶m
: expr
.get_params ())
180 if (param
.is_error ())
183 param
.get_pattern ().accept_vis (*this);
184 if (param
.has_type_given ())
185 param
.get_type ().accept_vis (*this);
188 expr
.get_definition_expr ().accept_vis (*this);
192 DefaultResolver::visit (AST::ClosureExprInnerTyped
&expr
)
194 if (expr
.is_marked_for_strip ())
197 for (auto ¶m
: expr
.get_params ())
199 if (param
.is_error ())
202 param
.get_pattern ().accept_vis (*this);
203 if (param
.has_type_given ())
204 param
.get_type ().accept_vis (*this);
207 expr
.get_definition_block ().accept_vis (*this);
208 expr
.get_return_type ().accept_vis (*this);
212 DefaultResolver::visit (AST::ContinueExpr
&expr
)
216 DefaultResolver::visit (AST::RangeFromToExpr
&expr
)
220 DefaultResolver::visit (AST::RangeFromExpr
&expr
)
224 DefaultResolver::visit (AST::RangeToExpr
&expr
)
228 DefaultResolver::visit (AST::RangeFromToInclExpr
&expr
)
232 DefaultResolver::visit (AST::RangeToInclExpr
&expr
)
236 DefaultResolver::visit (AST::ReturnExpr
&expr
)
240 DefaultResolver::visit (AST::LoopExpr
&expr
)
244 DefaultResolver::visit (AST::WhileLoopExpr
&expr
)
248 DefaultResolver::visit (AST::WhileLetLoopExpr
&expr
)
252 DefaultResolver::visit (AST::IfExpr
&expr
)
254 expr
.get_condition_expr ().accept_vis (*this);
255 expr
.get_if_block ().accept_vis (*this);
259 DefaultResolver::visit (AST::IfExprConseqElse
&expr
)
261 expr
.get_condition_expr ().accept_vis (*this);
262 expr
.get_if_block ().accept_vis (*this);
263 expr
.get_else_block ().accept_vis (*this);
267 DefaultResolver::visit (AST::IfLetExpr
&expr
)
271 DefaultResolver::visit (AST::IfLetExprConseqElse
&)
275 DefaultResolver::visit (AST::MatchExpr
&expr
)
277 if (expr
.is_marked_for_strip ())
280 expr
.get_scrutinee_expr ().accept_vis (*this);
281 for (auto &arm
: expr
.get_match_cases ())
283 arm
.get_expr ().accept_vis (*this);
284 for (auto &pat
: arm
.get_arm ().get_patterns ())
285 pat
->accept_vis (*this);
286 if (arm
.get_arm ().has_match_arm_guard ())
287 arm
.get_arm ().get_guard_expr ().accept_vis (*this);
292 DefaultResolver::visit (AST::AwaitExpr
&expr
)
296 DefaultResolver::visit (AST::AsyncBlockExpr
&expr
)
300 DefaultResolver::visit (AST::DelimTokenTree
&)
304 DefaultResolver::visit (AST::AttrInputMetaItemContainer
&)
308 DefaultResolver::visit (AST::IdentifierExpr
&expr
)
312 DefaultResolver::visit (AST::LifetimeParam
&)
316 DefaultResolver::visit (AST::ConstGenericParam
&)
320 DefaultResolver::visit (AST::PathInExpression
&expr
)
322 for (auto &seg
: expr
.get_segments ())
323 if (seg
.has_generic_args ())
325 auto &args
= seg
.get_generic_args ();
326 for (auto &arg
: args
.get_generic_args ())
327 arg
.accept_vis (*this);
328 for (auto &arg
: args
.get_binding_args ())
329 if (!arg
.is_error ())
330 arg
.get_type ().accept_vis (*this);
331 for (auto &arg
: args
.get_lifetime_args ())
332 arg
.accept_vis (*this);
337 DefaultResolver::visit (AST::TypePathSegmentGeneric
&)
341 DefaultResolver::visit (AST::TypePathSegmentFunction
&)
345 DefaultResolver::visit (AST::TypePath
&)
349 DefaultResolver::visit (AST::QualifiedPathInExpression
&)
353 DefaultResolver::visit (AST::QualifiedPathInType
&)
357 DefaultResolver::visit (AST::LiteralExpr
&expr
)
361 DefaultResolver::visit (AST::AttrInputLiteral
&)
365 DefaultResolver::visit (AST::AttrInputMacro
&)
369 DefaultResolver::visit (AST::MetaItemLitExpr
&expr
)
373 DefaultResolver::visit (AST::MetaItemPathLit
&)
377 DefaultResolver::visit (AST::StructExprStruct
&)
381 DefaultResolver::visit (AST::StructExprStructFields
&)
385 DefaultResolver::visit (AST::StructExprStructBase
&)
389 DefaultResolver::visit (AST::TypeParam
&)
393 DefaultResolver::visit (AST::LifetimeWhereClauseItem
&)
397 DefaultResolver::visit (AST::TypeBoundWhereClauseItem
&)
401 DefaultResolver::visit (AST::ExternCrate
&)
405 DefaultResolver::visit (AST::UseTreeGlob
&)
409 DefaultResolver::visit (AST::UseTreeList
&)
413 DefaultResolver::visit (AST::UseTreeRebind
&)
417 DefaultResolver::visit (AST::UseDeclaration
&)
421 DefaultResolver::visit (AST::TypeAlias
&)
425 DefaultResolver::visit (AST::EnumItem
&)
429 DefaultResolver::visit (AST::EnumItemTuple
&item
)
431 for (auto &field
: item
.get_tuple_fields ())
432 field
.get_field_type ().accept_vis (*this);
436 DefaultResolver::visit (AST::EnumItemStruct
&item
)
438 for (auto &field
: item
.get_struct_fields ())
439 field
.get_field_type ().accept_vis (*this);
443 DefaultResolver::visit (AST::EnumItemDiscriminant
&item
)
445 if (item
.has_expr ())
446 item
.get_expr ().accept_vis (*this);
450 DefaultResolver::visit (AST::ConstantItem
&item
)
452 auto expr_vis
= [this, &item
] () {
453 item
.get_expr ().accept_vis (*this);
454 visit (item
.get_type ());
457 // FIXME: Why do we need a Rib here?
458 ctx
.scoped (Rib::Kind::Item
, item
.get_node_id (), expr_vis
);
462 DefaultResolver::visit (AST::StaticItem
&item
)
464 auto expr_vis
= [this, &item
] () { item
.get_expr ().accept_vis (*this); };
466 // FIXME: Why do we need a Rib here?
467 ctx
.scoped (Rib::Kind::ConstantItem
, item
.get_node_id (), expr_vis
);
471 DefaultResolver::visit (AST::TraitItemConst
&)
475 DefaultResolver::visit (AST::TraitItemType
&)
479 DefaultResolver::visit (AST::ExternalTypeItem
&)
483 DefaultResolver::visit (AST::ExternalStaticItem
&)
487 DefaultResolver::visit (AST::MacroMatchRepetition
&)
491 DefaultResolver::visit (AST::MacroMatcher
&)
495 DefaultResolver::visit (AST::MacroRulesDefinition
&)
499 DefaultResolver::visit (AST::MacroInvocation
&)
503 DefaultResolver::visit (AST::MetaItemPath
&)
507 DefaultResolver::visit (AST::MetaItemSeq
&)
511 DefaultResolver::visit (AST::MetaListPaths
&)
515 DefaultResolver::visit (AST::MetaListNameValueStr
&)
519 DefaultResolver::visit (AST::RangePatternBoundPath
&)
523 DefaultResolver::visit (AST::RangePatternBoundQualPath
&)
527 DefaultResolver::visit (AST::RangePattern
&)
531 DefaultResolver::visit (AST::ReferencePattern
&)
535 DefaultResolver::visit (AST::StructPatternFieldTuplePat
&)
539 DefaultResolver::visit (AST::StructPatternFieldIdentPat
&)
543 DefaultResolver::visit (AST::StructPatternFieldIdent
&)
547 DefaultResolver::visit (AST::StructPattern
&)
551 DefaultResolver::visit (AST::TupleStructItemsNoRange
&)
555 DefaultResolver::visit (AST::TupleStructItemsRange
&)
559 DefaultResolver::visit (AST::TupleStructPattern
&)
563 DefaultResolver::visit (AST::TuplePatternItemsMultiple
&)
567 DefaultResolver::visit (AST::TuplePatternItemsRanged
&)
571 DefaultResolver::visit (AST::TuplePattern
&)
575 DefaultResolver::visit (AST::GroupedPattern
&)
579 DefaultResolver::visit (AST::SlicePattern
&)
583 DefaultResolver::visit (AST::AltPattern
&)
587 DefaultResolver::visit (AST::EmptyStmt
&)
591 DefaultResolver::visit (AST::TraitBound
&)
595 DefaultResolver::visit (AST::ImplTraitType
&)
599 DefaultResolver::visit (AST::TraitObjectType
&)
603 DefaultResolver::visit (AST::ParenthesisedType
&)
607 DefaultResolver::visit (AST::ImplTraitTypeOneBound
&)
611 DefaultResolver::visit (AST::TraitObjectTypeOneBound
&)
615 DefaultResolver::visit (AST::TupleType
&)
619 DefaultResolver::visit (AST::ReferenceType
&)
623 DefaultResolver::visit (AST::ArrayType
&)
627 DefaultResolver::visit (AST::SliceType
&)
631 DefaultResolver::visit (AST::BareFunctionType
&)
635 DefaultResolver::visit (AST::SelfParam
&)
639 DefaultResolver::visit (AST::FunctionParam
&)
643 DefaultResolver::visit (AST::VariadicParam
&)
646 } // namespace Resolver2_0