From 7626215ab7a7d02a12f6f9229dc13977e1029640 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Mon, 21 Jul 2025 19:31:49 -0400 Subject: [PATCH] Use X macros to generate DefaultASTVisitor This should help us reduce mismatches between DefaultASTVisitor and ExpandVisitor later. clang-format is disabled for "ast/rust-ast-visitable-inner.h", as it doesn't seem to handle the macro shenanagins used very well. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Remove explicit definitions, use X macros. * ast/rust-ast-visitable-inner.h: New file. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-ast-visitable-inner.h | 1067 ++++++++++++++++ gcc/rust/ast/rust-ast-visitor.cc | 1492 +---------------------- 2 files changed, 1099 insertions(+), 1460 deletions(-) create mode 100644 gcc/rust/ast/rust-ast-visitable-inner.h diff --git a/gcc/rust/ast/rust-ast-visitable-inner.h b/gcc/rust/ast/rust-ast-visitable-inner.h new file mode 100644 index 000000000000..8b08dc6e0932 --- /dev/null +++ b/gcc/rust/ast/rust-ast-visitable-inner.h @@ -0,0 +1,1067 @@ +// Copyright (C) 2025 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +// This header is used to generate visitors for AST nodes + +// The following is a description of the various x macros +// used in this file. This file attempts to use these x macros +// in a fairly well defined way, so that consumers of this file +// can be creative in their definitions of these macros. +// +// You must define: +// VISIT_DEF (T, parameter_name, ...) +// can assume T is a type +// can assume varargs parameter consists only of +// comma separated "visit statement" macros +// can assume varargs parameter is not empty +// expected to define a visitor which binds a node +// as parameter_name, iteratively applying the visit +// statement macros given as varargs +// VISIT_DEF_STUB (T) +// can assume T is a type +// expected to define a no-op visitor +// VISIT_UNESCAPE (vs) +// can assume vs is a "visit statement" +// only present inside the body of a VISIT_ESCAPE +// used to reenter "visit statement" mode +// must expand to a single statement (including any semicolon) +// +// As well as the following "visit statement" macros: +// +// VISIT_INNER_ATTRS (node) +// node could be any expression +// expected to visit node's inner attributes +// VISIT_OUTER_ATTRS (node) +// node could be any expression +// expected to visit node's outer attributes +// VISIT (node) +// node could be any expression +// expected to visit node +// VISIT_COND (cond, vs) +// cond could be any expression +// can assume vs is a "visit statement" macro +// expected to run vs only if cond is true +// VISIT_VEC (vec) +// vec could be any expression +// expected to iteratively visit entries in vec +// VISIT_FN_PARAMS (fn) +// used as an alternative to VISIT_VEC (fn.get_function_params ()) +// makes hooking the visit of function parameters easier +// VISIT_ESCAPE (...) +// run C++ code provided in varargs variable +// escape hatch for *cough* InlineAsm *cough* +// reentry to "visit statement* macros using +// VISIT_UNESCAPE +// +// In the special case of VISIT_DEF ignoring its varargs +// parameter, "visit statement" macros don't have to be +// defined. +// +// If VISIT_ESCAPE ignores its argument or is left undefined +// (see prior paragraph) VISIT_UNESCAPE doesn't have to +// be defined. + +// clang doesn't format this very well +// TODO: use https://clang.llvm.org/docs/ClangFormatStyleOptions.html#macros +// when we move to clang-format-17 +// clang-format off + +VISIT_DEF (Crate, crate, + VISIT_INNER_ATTRS (crate), + VISIT_VEC (crate.items) +) + +VISIT_DEF_STUB (Token) + +VISIT_DEF (DelimTokenTree, delim_tok_tree, + VISIT_VEC (delim_tok_tree.get_token_trees ()) +) + +VISIT_DEF (AttrInputMetaItemContainer, input, + VISIT_VEC (input.get_items ()) +) + +VISIT_DEF (IdentifierExpr, ident_expr, + VISIT_OUTER_ATTRS (ident_expr) +) + +VISIT_DEF_STUB (Lifetime) + +VISIT_DEF (LifetimeParam, lifetime_param, + VISIT_OUTER_ATTRS (lifetime_param), + VISIT (lifetime_param.get_lifetime ()), + VISIT_VEC (lifetime_param.get_lifetime_bounds ()) +) + +VISIT_DEF (ConstGenericParam, const_param, + VISIT_OUTER_ATTRS (const_param), + VISIT_COND (const_param.has_type (), VISIT (const_param.get_type ())), + VISIT_COND (const_param.has_default_value (), VISIT (const_param.get_default_value_unchecked ())) +) + +VISIT_DEF (PathInExpression, path, + VISIT_OUTER_ATTRS (path), + VISIT_COND (!path.is_lang_item (), + VISIT_VEC (path.get_segments ())) +) + +VISIT_DEF_STUB (TypePathSegment) + +VISIT_DEF (GenericArgsBinding, binding, + VISIT (binding.get_type ()) +) + +VISIT_DEF (TypePathSegmentGeneric, segment, + VISIT_COND (segment.has_generic_args (), + VISIT (segment.get_generic_args ())) +) + +VISIT_DEF (TypePathFunction, tpf, + VISIT_VEC (tpf.get_params ()), + VISIT_COND (tpf.has_return_type (), + VISIT (tpf.get_return_type ())) +) + +VISIT_DEF_STUB (PathIdentSegment) + +VISIT_DEF (TypePathSegmentFunction, segment, + VISIT (segment.get_type_path_function ()), + VISIT (segment.get_ident_segment ()) +) + +VISIT_DEF (GenericArgs, args, + VISIT_VEC (args.get_lifetime_args ()), + VISIT_VEC (args.get_generic_args ()), + VISIT_VEC (args.get_binding_args ()) +) + +VISIT_DEF (PathExprSegment, segment, + VISIT (segment.get_ident_segment ()), + VISIT_COND (segment.has_generic_args (), + VISIT (segment.get_generic_args ())) +) + +VISIT_DEF (TypePath, path, + VISIT_VEC (path.get_segments ()) +) + +VISIT_DEF (QualifiedPathInExpression, path, + VISIT_OUTER_ATTRS (path), + VISIT (path.get_qualified_path_type ()), + VISIT_VEC (path.get_segments ()) +) + +VISIT_DEF (QualifiedPathType, path, + VISIT (path.get_type ()), + VISIT_COND (path.has_as_clause (), VISIT (path.get_as_type_path ())) +) + +VISIT_DEF (QualifiedPathInType, path, + VISIT (path.get_qualified_path_type ()), + VISIT (path.get_associated_segment ()), + VISIT_VEC (path.get_segments ()) +) + +VISIT_DEF (LiteralExpr, expr, + VISIT_OUTER_ATTRS (expr) +) + +VISIT_DEF (AttrInputLiteral, attr_input, + VISIT (attr_input.get_literal ()) +) + +VISIT_DEF (AttrInputMacro, attr_input, + VISIT (attr_input.get_macro ()) +) + +VISIT_DEF (MetaItemLitExpr, meta_item, + VISIT (meta_item.get_literal ()) +) + +VISIT_DEF_STUB (SimplePathSegment) + +VISIT_DEF (SimplePath, path, + VISIT_VEC (path.get_segments ()) +) + +VISIT_DEF (MetaItemPathLit, meta_item, + VISIT (meta_item.get_path ()), + VISIT (meta_item.get_literal ()) +) + +VISIT_DEF (BorrowExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_borrowed_expr ()) +) + +VISIT_DEF (DereferenceExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_dereferenced_expr ()) +) + +VISIT_DEF (ErrorPropagationExpr, expr, + VISIT_OUTER_ATTRS (expr) + // TODO: visit more child nodes +) + +VISIT_DEF (NegationExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_negated_expr ()) +) + +VISIT_DEF (ArithmeticOrLogicalExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_left_expr ()), + VISIT (expr.get_right_expr ()) +) + +VISIT_DEF (ComparisonExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_left_expr ()), + VISIT (expr.get_right_expr ()) +) + +VISIT_DEF (LazyBooleanExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_left_expr ()), + VISIT (expr.get_right_expr ()) +) + +VISIT_DEF (TypeCastExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_casted_expr ()), + VISIT (expr.get_type_to_cast_to ()) +) + +VISIT_DEF (AssignmentExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_left_expr ()), + VISIT (expr.get_right_expr ()) +) + +VISIT_DEF (CompoundAssignmentExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_left_expr ()), + VISIT (expr.get_right_expr ()) +) + +VISIT_DEF (GroupedExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT (expr.get_expr_in_parens ()) +) + +VISIT_DEF (ArrayElemsValues, elems, + VISIT_VEC (elems.get_values ()) +) + +VISIT_DEF (ArrayElemsCopied, elems, + VISIT (elems.get_elem_to_copy ()), + VISIT (elems.get_num_copies ()) +) + +VISIT_DEF (ArrayExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT (expr.get_array_elems ()) +) + +VISIT_DEF (ArrayIndexExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_array_expr ()), + VISIT (expr.get_index_expr ()) +) + +VISIT_DEF (TupleExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT_VEC (expr.get_tuple_elems ()) +) + +VISIT_DEF (TupleIndexExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_tuple_expr ()) +) + +VISIT_DEF (StructExprStruct, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT (expr.get_struct_name ()) +) + +VISIT_DEF_STUB (StructExprFieldIdentifier) + +VISIT_DEF (StructExprFieldIdentifierValue, field, + VISIT (field.get_value ()) +) + +VISIT_DEF (StructExprFieldIndexValue, field, + VISIT (field.get_value ()) +) + +VISIT_DEF (StructBase, base, + VISIT (base.get_base_struct ()) +) + +VISIT_DEF (StructExprStructFields, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT (expr.get_struct_name ()), + VISIT_COND (expr.has_struct_base (), VISIT (expr.get_struct_base ())), + VISIT_VEC (expr.get_fields ()) +) + +VISIT_DEF (StructExprStructBase, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT (expr.get_struct_name ()), + VISIT (expr.get_struct_base ()) +) + +VISIT_DEF (CallExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_function_expr ()), + VISIT_VEC (expr.get_params ()) +) + +VISIT_DEF (MethodCallExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_receiver_expr ()), + VISIT (expr.get_method_name ()), + VISIT_VEC (expr.get_params ()) +) + +VISIT_DEF (FieldAccessExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_receiver_expr ()) +) + +VISIT_DEF (ClosureExprInner, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_VEC (expr.get_params ()), + VISIT (expr.get_definition_expr ()) +) + +VISIT_DEF (BlockExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT_COND (expr.has_label (), + VISIT (expr.get_label ())), + VISIT_VEC (expr.get_statements ()), + VISIT_COND (expr.has_tail_expr (), + VISIT (expr.get_tail_expr ())) +) + +VISIT_DEF (ConstBlock, expr, + VISIT (expr.get_const_expr ()) +) + +VISIT_DEF (AnonConst, expr, + VISIT_COND (!expr.is_deferred (), VISIT (expr.get_inner_expr ())) +) + +VISIT_DEF (ClosureExprInnerTyped, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_VEC (expr.get_params ()), + VISIT (expr.get_return_type ()), + VISIT (expr.get_definition_expr ()) +) + +VISIT_DEF (ClosureParam, param, + VISIT_OUTER_ATTRS (param), + VISIT (param.get_pattern ()), + VISIT_COND (param.has_type_given (), + VISIT (param.get_type ())) +) + +VISIT_DEF (ContinueExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_COND (expr.has_label (), + VISIT (expr.get_label_unchecked ())) +) + +VISIT_DEF (BreakExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_COND (expr.has_label (), + VISIT (expr.get_label_unchecked ())), + VISIT_COND (expr.has_break_expr (), + VISIT (expr.get_break_expr ())) +) + +VISIT_DEF (RangeFromToExpr, expr, + VISIT (expr.get_from_expr ()), + VISIT (expr.get_to_expr ()) +) + +VISIT_DEF (RangeFromExpr, expr, + VISIT (expr.get_from_expr ()) +) + +VISIT_DEF (RangeToExpr, expr, + VISIT (expr.get_to_expr ()) +) + +VISIT_DEF_STUB (RangeFullExpr) + +VISIT_DEF (RangeFromToInclExpr, expr, + VISIT (expr.get_from_expr ()), + VISIT (expr.get_to_expr ()) +) + +VISIT_DEF (RangeToInclExpr, expr, + VISIT (expr.get_to_expr ()) +) + +VISIT_DEF (ReturnExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_COND (expr.has_returned_expr (), + VISIT (expr.get_returned_expr ())) +) + +VISIT_DEF (TryExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_block_expr ()) +) + +VISIT_DEF (BoxExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_boxed_expr ()) +) + +VISIT_DEF (UnsafeBlockExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_block_expr ()) +) + +VISIT_DEF (LoopLabel, label, + VISIT (label.get_lifetime ()) +) + +VISIT_DEF (LoopExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_COND (expr.has_loop_label (), + VISIT (expr.get_loop_label ())), + VISIT (expr.get_loop_block ()) +) + +VISIT_DEF (WhileLoopExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_COND (expr.has_loop_label (), + VISIT (expr.get_loop_label ())), + VISIT (expr.get_predicate_expr ()), + VISIT (expr.get_loop_block ()) +) + +VISIT_DEF (WhileLetLoopExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_VEC (expr.get_patterns ()), + VISIT_COND (expr.has_loop_label (), + VISIT (expr.get_loop_label ())), + VISIT (expr.get_scrutinee_expr ()), + VISIT (expr.get_loop_block ()) +) + +VISIT_DEF (ForLoopExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_pattern ()), + VISIT (expr.get_iterator_expr ()), + VISIT_COND (expr.has_loop_label (), + VISIT (expr.get_loop_label ())), + VISIT (expr.get_loop_block ()) +) + +VISIT_DEF (IfExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_condition_expr ()), + VISIT (expr.get_if_block ()) +) + +VISIT_DEF (IfExprConseqElse, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_condition_expr ()), + VISIT (expr.get_if_block ()), + VISIT (expr.get_else_block ()) +) + +VISIT_DEF (IfLetExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_VEC (expr.get_patterns ()), + VISIT (expr.get_value_expr ()), + VISIT (expr.get_if_block ()) +) + +VISIT_DEF (IfLetExprConseqElse, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_VEC (expr.get_patterns ()), + VISIT (expr.get_value_expr ()), + VISIT (expr.get_if_block ()), + VISIT (expr.get_else_block ()) +) + +VISIT_DEF (MatchArm, arm, + VISIT_OUTER_ATTRS (arm), + VISIT_VEC (arm.get_patterns ()), + VISIT_COND (arm.has_match_arm_guard (), + VISIT (arm.get_guard_expr ())) +) + +VISIT_DEF (MatchCase, arm, + VISIT (arm.get_arm ()), + VISIT (arm.get_expr ()) +) + +VISIT_DEF (MatchExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_INNER_ATTRS (expr), + VISIT (expr.get_scrutinee_expr ()), + VISIT_VEC (expr.get_match_cases ()) +) + +VISIT_DEF (AwaitExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_awaited_expr ()) +) + +VISIT_DEF (AsyncBlockExpr, expr, + VISIT_OUTER_ATTRS (expr), + VISIT (expr.get_block_expr ()) +) + +VISIT_DEF (InlineAsm, expr, + VISIT_OUTER_ATTRS (expr), + VISIT_ESCAPE ({ + using RegisterType = AST::InlineAsmOperand::RegisterType; + for (auto &operand : expr.get_operands ()) + { + switch (operand.get_register_type ()) + { + case RegisterType::In: + { + VISIT_UNESCAPE (VISIT (operand.get_in ().expr)) + break; + } + case RegisterType::Out: + { + VISIT_UNESCAPE (VISIT (operand.get_out ().expr)) + break; + } + case RegisterType::InOut: + { + VISIT_UNESCAPE (VISIT (operand.get_in_out ().expr)) + break; + } + case RegisterType::SplitInOut: + { + auto split = operand.get_split_in_out (); + VISIT_UNESCAPE (VISIT (split.in_expr)) + VISIT_UNESCAPE (VISIT (split.out_expr)) + break; + } + case RegisterType::Const: + { + VISIT_UNESCAPE (VISIT (operand.get_const ().anon_const.get_inner_expr ())) + break; + } + case RegisterType::Sym: + { + VISIT_UNESCAPE (VISIT (operand.get_sym ().expr)) + break; + } + case RegisterType::Label: + { + VISIT_UNESCAPE (VISIT (operand.get_label ().expr)) + break; + } + } + } + }) +) + +VISIT_DEF (LlvmInlineAsm, expr, + VISIT_ESCAPE ( + for (auto &output : expr.get_outputs ()) + VISIT_UNESCAPE (VISIT (output.expr)) + for (auto &input : expr.get_inputs ()) + VISIT_UNESCAPE (VISIT (input.expr)) + ) +) + +VISIT_DEF (TypeParam, param, + VISIT_OUTER_ATTRS (param), + VISIT_VEC (param.get_type_param_bounds ()), + VISIT_COND (param.has_type (), + VISIT (param.get_type ())) +) + +VISIT_DEF (LifetimeWhereClauseItem, item, + VISIT (item.get_lifetime ()), + VISIT_VEC (item.get_lifetime_bounds ()) +) + +VISIT_DEF (TypeBoundWhereClauseItem, item, + VISIT_VEC (item.get_for_lifetimes ()), + VISIT (item.get_type ()), + VISIT_VEC (item.get_type_param_bounds ()) +) + +VISIT_DEF (Visibility, vis, + VISIT_COND (vis.has_path (), + VISIT (vis.get_path ())) +) + +VISIT_DEF_STUB (FunctionQualifiers) + +VISIT_DEF (WhereClause, where, + VISIT_VEC (where.get_items ()) +) + +VISIT_DEF (FunctionParam, param, + VISIT_OUTER_ATTRS (param), + VISIT_COND (param.has_name (), + VISIT (param.get_pattern ())), + VISIT (param.get_type ()) +) + +VISIT_DEF (SelfParam, param, + VISIT_OUTER_ATTRS (param), + VISIT_COND (param.has_lifetime (), VISIT (param.get_lifetime ())), + VISIT_COND (param.has_type (), VISIT (param.get_type ())) +) + +VISIT_DEF (Module, module, + VISIT_OUTER_ATTRS (module), + VISIT (module.get_visibility ()), + VISIT_INNER_ATTRS (module), + VISIT_VEC (module.get_items ()) +) + +VISIT_DEF (ExternCrate, crate, + VISIT_OUTER_ATTRS (crate), + VISIT (crate.get_visibility ()) +) + +VISIT_DEF (UseTreeGlob, use_tree, + VISIT (use_tree.get_path ()) +) + +VISIT_DEF (UseTreeList, use_tree, + VISIT (use_tree.get_path ()) +) + +VISIT_DEF (UseTreeRebind, use_tree, + VISIT (use_tree.get_path ()) +) + +VISIT_DEF (UseDeclaration, use_decl, + VISIT (use_decl.get_visibility ()), + VISIT (use_decl.get_tree ()) +) + +VISIT_DEF (Function, function, + VISIT_OUTER_ATTRS (function), + VISIT (function.get_visibility ()), + VISIT (function.get_qualifiers ()), + VISIT_VEC (function.get_generic_params ()), + VISIT_FN_PARAMS (function), + VISIT_COND (function.has_return_type (), VISIT (function.get_return_type ())), + VISIT_COND (function.has_where_clause (), VISIT (function.get_where_clause ())), + VISIT_COND (function.has_body (), VISIT (*function.get_definition ())) +) + +VISIT_DEF (TypeAlias, type_alias, + VISIT_OUTER_ATTRS (type_alias), + VISIT (type_alias.get_visibility ()), + VISIT_VEC (type_alias.get_generic_params ()), + VISIT_COND (type_alias.has_where_clause (), VISIT (type_alias.get_where_clause ())), + VISIT (type_alias.get_type_aliased ()) +) + +VISIT_DEF (StructField, field, + VISIT_OUTER_ATTRS (field), + VISIT (field.get_visibility ()), + VISIT (field.get_field_type ()) +) + +VISIT_DEF (StructStruct, struct_item, + VISIT_OUTER_ATTRS (struct_item), + VISIT (struct_item.get_visibility ()), + VISIT_VEC (struct_item.get_generic_params ()), + VISIT_COND (struct_item.has_where_clause (), VISIT (struct_item.get_where_clause ())), + VISIT_VEC (struct_item.get_fields ()) +) + +VISIT_DEF (TupleField, field, + VISIT_OUTER_ATTRS (field), + VISIT (field.get_visibility ()), + VISIT (field.get_field_type ()) +) + +VISIT_DEF (TupleStruct, tuple_struct, + VISIT_OUTER_ATTRS (tuple_struct), + VISIT (tuple_struct.get_visibility ()), + VISIT_VEC (tuple_struct.get_generic_params ()), + VISIT_COND (tuple_struct.has_where_clause (), VISIT (tuple_struct.get_where_clause ())), + VISIT_VEC (tuple_struct.get_fields ()) +) + +VISIT_DEF (EnumItem, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_visibility ()) +) + +VISIT_DEF (EnumItemTuple, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_visibility ()), + VISIT_VEC (item.get_tuple_fields ()) +) + +VISIT_DEF (EnumItemStruct, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_visibility ()), + VISIT_VEC (item.get_struct_fields ()) +) + +VISIT_DEF (EnumItemDiscriminant, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_visibility ()), + VISIT (item.get_expr ()) +) + +VISIT_DEF (Enum, enum_item, + VISIT_OUTER_ATTRS (enum_item), + VISIT (enum_item.get_visibility ()), + VISIT_VEC (enum_item.get_generic_params ()), + VISIT_COND (enum_item.has_where_clause (), VISIT (enum_item.get_where_clause ())), + VISIT_VEC (enum_item.get_variants ()) +) + +VISIT_DEF (Union, union_item, + VISIT_OUTER_ATTRS (union_item), + VISIT (union_item.get_visibility ()), + VISIT_VEC (union_item.get_generic_params ()), + VISIT_COND (union_item.has_where_clause (), VISIT (union_item.get_where_clause ())), + VISIT_VEC (union_item.get_variants ()) +) + +VISIT_DEF (ConstantItem, const_item, + VISIT_OUTER_ATTRS (const_item), + VISIT (const_item.get_visibility ()), + VISIT (const_item.get_type ()), + VISIT_COND (const_item.has_expr (), VISIT (const_item.get_expr ())) +) + +VISIT_DEF (StaticItem, static_item, + VISIT_OUTER_ATTRS (static_item), + VISIT (static_item.get_visibility ()), + VISIT (static_item.get_type ()), + VISIT (static_item.get_expr ()) +) + +VISIT_DEF (TraitItemConst, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_type ()), + VISIT_COND (item.has_expr (), VISIT (item.get_expr ())) +) + +VISIT_DEF (TraitItemType, item, + VISIT_OUTER_ATTRS (item), + VISIT_VEC (item.get_type_param_bounds ()) +) + +VISIT_DEF (Trait, trait, + VISIT_OUTER_ATTRS (trait), + VISIT (trait.get_visibility ()), + VISIT_INNER_ATTRS (trait), + VISIT (trait.get_implicit_self ()), + VISIT_VEC (trait.get_generic_params ()), + VISIT_COND (trait.has_where_clause (), VISIT (trait.get_where_clause ())), + VISIT_VEC (trait.get_type_param_bounds ()), + VISIT_VEC (trait.get_trait_items ()) +) + +VISIT_DEF (InherentImpl, impl, + VISIT_OUTER_ATTRS (impl), + VISIT (impl.get_visibility ()), + VISIT_VEC (impl.get_generic_params ()), + VISIT_COND (impl.has_where_clause (), VISIT (impl.get_where_clause ())), + VISIT (impl.get_type ()), + VISIT_INNER_ATTRS (impl), + VISIT_VEC (impl.get_impl_items ()) +) + +VISIT_DEF (TraitImpl, impl, + VISIT_OUTER_ATTRS (impl), + VISIT (impl.get_visibility ()), + VISIT_VEC (impl.get_generic_params ()), + VISIT_COND (impl.has_where_clause (), VISIT (impl.get_where_clause ())), + VISIT (impl.get_type ()), + VISIT (impl.get_trait_path ()), + VISIT_INNER_ATTRS (impl), + VISIT_VEC (impl.get_impl_items ()) +) + +VISIT_DEF (ExternalTypeItem, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_visibility ()) +) + +VISIT_DEF (ExternalStaticItem, item, + VISIT_OUTER_ATTRS (item), + VISIT (item.get_visibility ()), + VISIT (item.get_type ()) +) + +VISIT_DEF (ExternBlock, block, + VISIT_OUTER_ATTRS (block), + VISIT (block.get_visibility ()), + VISIT_INNER_ATTRS (block), + VISIT_VEC (block.get_extern_items ()) +) + +VISIT_DEF_STUB (MacroMatchFragment) + +VISIT_DEF (MacroMatchRepetition, match, + VISIT_VEC (match.get_matches ()) +) + +VISIT_DEF (MacroMatcher, matcher, + VISIT_VEC (matcher.get_matches ()) +) + +VISIT_DEF (MacroTranscriber, transcriber, + VISIT (transcriber.get_token_tree ()) +) + +VISIT_DEF (MacroRule, rule, + VISIT (rule.get_matcher ()), + VISIT (rule.get_transcriber ()) +) + +VISIT_DEF (MacroRulesDefinition, rules_def, + VISIT_OUTER_ATTRS (rules_def), + VISIT_VEC (rules_def.get_macro_rules ()) +) + +VISIT_DEF (MacroInvocData, data, + VISIT (data.get_path ()), + VISIT (data.get_delim_tok_tree ()) +) + +VISIT_DEF (MacroInvocation, macro_invoc, + VISIT_OUTER_ATTRS (macro_invoc), + VISIT (macro_invoc.get_invoc_data ()) +) + +VISIT_DEF (MetaItemPath, meta_item, + VISIT (meta_item.get_path ()) +) + +VISIT_DEF (MetaItemSeq, meta_item, + VISIT (meta_item.get_path ()), + VISIT_VEC (meta_item.get_seq ()) +) + +VISIT_DEF_STUB (MetaWord) + +VISIT_DEF_STUB (MetaNameValueStr) + +VISIT_DEF (MetaListPaths, meta_item, + VISIT_VEC (meta_item.get_paths ()) +) + +VISIT_DEF (MetaListNameValueStr, meta_item, + VISIT_VEC (meta_item.get_values ()) +) + +VISIT_DEF_STUB (LiteralPattern) + +VISIT_DEF (IdentifierPattern, pattern, + VISIT_COND (pattern.has_subpattern (), VISIT (pattern.get_subpattern ())) +) + +VISIT_DEF_STUB (WildcardPattern) + +VISIT_DEF_STUB (RestPattern) + +VISIT_DEF_STUB (RangePatternBoundLiteral) + +VISIT_DEF (RangePatternBoundPath, bound, + VISIT (bound.get_path ()) +) + +VISIT_DEF (RangePatternBoundQualPath, bound, + VISIT (bound.get_qualified_path ()) +) + +VISIT_DEF (RangePattern, pattern, + VISIT_COND (pattern.get_has_lower_bound (), VISIT (pattern.get_lower_bound ())), + VISIT_COND (pattern.get_has_upper_bound (), VISIT (pattern.get_upper_bound ())) +) + +VISIT_DEF (ReferencePattern, pattern, + VISIT (pattern.get_referenced_pattern ()) +) + +VISIT_DEF (StructPatternFieldTuplePat, field, + VISIT_OUTER_ATTRS (field), + VISIT (field.get_index_pattern ()) +) + +VISIT_DEF (StructPatternFieldIdentPat, field, + VISIT_OUTER_ATTRS (field), + VISIT (field.get_ident_pattern ()) +) + +VISIT_DEF (StructPatternFieldIdent, field, + VISIT_OUTER_ATTRS (field) +) + +VISIT_DEF (StructPatternElements, spe, + VISIT_VEC (spe.get_struct_pattern_fields ()), + VISIT_VEC (spe.get_etc_outer_attrs ()) // TODO: look into this +) + +VISIT_DEF (StructPattern, pattern, + VISIT (pattern.get_path ()), + VISIT (pattern.get_struct_pattern_elems ()) +) + +VISIT_DEF (TupleStructItemsNoRange, tuple_items, + VISIT_VEC (tuple_items.get_patterns ()) +) + +VISIT_DEF (TupleStructItemsRange, tuple_items, + VISIT_VEC (tuple_items.get_lower_patterns ()), + VISIT_VEC (tuple_items.get_upper_patterns ()) +) + +VISIT_DEF (TupleStructPattern, pattern, + VISIT (pattern.get_path ()), + VISIT (pattern.get_items ()) +) + +VISIT_DEF (TuplePatternItemsMultiple, tuple_items, + VISIT_VEC (tuple_items.get_patterns ()) +) + +VISIT_DEF (TuplePatternItemsRanged, tuple_items, + VISIT_VEC (tuple_items.get_lower_patterns ()), + VISIT_VEC (tuple_items.get_upper_patterns ()) +) + +VISIT_DEF (TuplePattern, pattern, + VISIT (pattern.get_items ()) +) + +VISIT_DEF (GroupedPattern, pattern, + VISIT (pattern.get_pattern_in_parens ()) +) + +VISIT_DEF (SlicePattern, pattern, + VISIT_VEC (pattern.get_items ()) +) + +VISIT_DEF (AltPattern, pattern, + VISIT_VEC (pattern.get_alts ()) +) + +VISIT_DEF_STUB (EmptyStmt) + +VISIT_DEF (LetStmt, stmt, + VISIT_OUTER_ATTRS (stmt), + VISIT (stmt.get_pattern ()), + VISIT_COND (stmt.has_type (), VISIT (stmt.get_type ())), + VISIT_COND (stmt.has_init_expr (), VISIT (stmt.get_init_expr ())) +) + +VISIT_DEF (ExprStmt, stmt, + VISIT (stmt.get_expr ()) +) + +VISIT_DEF (TraitBound, bound, + VISIT_VEC (bound.get_for_lifetimes ()), + VISIT (bound.get_type_path ()) +) + +VISIT_DEF (ImplTraitType, type, + VISIT_VEC (type.get_type_param_bounds ()) +) + +VISIT_DEF (TraitObjectType, type, + VISIT_VEC (type.get_type_param_bounds ()) +) + +VISIT_DEF (ParenthesisedType, type, + VISIT (type.get_type_in_parens ()) +) + +VISIT_DEF (ImplTraitTypeOneBound, type, + VISIT (type.get_trait_bound ()) +) + +VISIT_DEF (TraitObjectTypeOneBound, type, + VISIT (type.get_trait_bound ()) +) + +VISIT_DEF (TupleType, type, + VISIT_VEC (type.get_elems ()) +) + +VISIT_DEF_STUB (NeverType) + +VISIT_DEF (RawPointerType, type, + VISIT (type.get_type_pointed_to ()) +) + +VISIT_DEF (ReferenceType, type, + VISIT (type.get_lifetime ()), + VISIT (type.get_base_type ()) +) + +VISIT_DEF (ArrayType, type, + VISIT (type.get_elem_type ()), + VISIT (type.get_size_expr ()) +) + +VISIT_DEF (SliceType, type, + VISIT (type.get_elem_type ()) +) + +VISIT_DEF_STUB (InferredType) + +VISIT_DEF (MaybeNamedParam, param, + VISIT_OUTER_ATTRS (param), + VISIT (param.get_type ()) +) + +VISIT_DEF (BareFunctionType, type, + VISIT_VEC (type.get_for_lifetimes ()), + VISIT (type.get_function_qualifiers ()), + VISIT_VEC (type.get_function_params ()), + VISIT_COND (type.is_variadic (), VISIT_VEC (type.get_variadic_attr ())), // TODO: look into this + VISIT_COND (type.has_return_type (), VISIT (type.get_return_type ())) +) + +// TODO: visit subnodes? +VISIT_DEF_STUB (FormatArgs) + +VISIT_DEF (VariadicParam, param, + VISIT_COND (param.has_pattern (), VISIT (param.get_pattern ())) +) + +// clang-format on diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index 7be2ecc1b48d..17f5d0978cfc 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -31,1470 +31,42 @@ namespace Rust { namespace AST { void -DefaultASTVisitor::visit (AST::Crate &crate) -{ - visit_inner_attrs (crate); - for (auto &item : crate.items) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::Token &tok) -{} - -void -DefaultASTVisitor::visit (AST::DelimTokenTree &delim_tok_tree) -{ - for (auto &token : delim_tok_tree.get_token_trees ()) - visit (token); -} - -void -DefaultASTVisitor::visit (AST::AttrInputMetaItemContainer &input) -{ - for (auto &item : input.get_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::IdentifierExpr &ident_expr) -{ - visit_outer_attrs (ident_expr); -} - -void -DefaultASTVisitor::visit (AST::Lifetime &lifetime) -{} - -void -DefaultASTVisitor::visit (AST::LifetimeParam &lifetime_param) -{ - visit_outer_attrs (lifetime_param); - visit (lifetime_param.get_lifetime ()); - for (auto &lifetime_bound : lifetime_param.get_lifetime_bounds ()) - visit (lifetime_bound); -} - -void -DefaultASTVisitor::visit (AST::ConstGenericParam &const_param) -{ - visit_outer_attrs (const_param); - if (const_param.has_type ()) - visit (const_param.get_type ()); - if (const_param.has_default_value ()) - visit (const_param.get_default_value_unchecked ()); -} - -void -DefaultASTVisitor::visit (AST::PathInExpression &path) -{ - visit_outer_attrs (path); - - if (!path.is_lang_item ()) - for (auto &segment : path.get_segments ()) - visit (segment); -} - -void -DefaultASTVisitor::visit (AST::TypePathSegment &segment) -{} - -void -DefaultASTVisitor::visit (GenericArgsBinding &binding) -{ - visit (binding.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::TypePathSegmentGeneric &segment) -{ - if (segment.has_generic_args ()) - visit (segment.get_generic_args ()); -} - -void -DefaultASTVisitor::visit (AST::TypePathFunction &tpf) -{ - for (auto &input : tpf.get_params ()) - visit (input); - if (tpf.has_return_type ()) - visit (tpf.get_return_type ()); -} - -void -DefaultASTVisitor::visit (AST::PathIdentSegment &segment) -{} - -void -DefaultASTVisitor::visit (AST::TypePathSegmentFunction &segment) -{ - visit (segment.get_type_path_function ()); - visit (segment.get_ident_segment ()); -} - -void -DefaultASTVisitor::visit (AST::GenericArgs &args) -{ - for (auto &lifetime : args.get_lifetime_args ()) - visit (lifetime); - - for (auto &generic : args.get_generic_args ()) - visit (generic); - - for (auto &binding : args.get_binding_args ()) - visit (binding); -} - -void -DefaultASTVisitor::visit (AST::PathExprSegment &segment) -{ - visit (segment.get_ident_segment ()); - if (segment.has_generic_args ()) - visit (segment.get_generic_args ()); -} -void -DefaultASTVisitor::visit (AST::TypePath &path) -{ - for (auto &segment : path.get_segments ()) - visit (segment); -} - -void -DefaultASTVisitor::visit (AST::QualifiedPathInExpression &path) -{ - visit_outer_attrs (path); - visit (path.get_qualified_path_type ()); - - for (auto &segment : path.get_segments ()) - visit (segment); -} - -void -DefaultASTVisitor::visit (AST::QualifiedPathType &path) -{ - visit (path.get_type ()); - if (path.has_as_clause ()) - visit (path.get_as_type_path ()); -} - -void -DefaultASTVisitor::visit (AST::QualifiedPathInType &path) -{ - visit (path.get_qualified_path_type ()); - visit (path.get_associated_segment ()); - - for (auto &segment : path.get_segments ()) - visit (segment); -} - -void -DefaultASTVisitor::visit (AST::LiteralExpr &expr) -{ - visit_outer_attrs (expr); -} - -void -DefaultASTVisitor::visit (AST::AttrInputLiteral &attr_input) -{ - visit (attr_input.get_literal ()); -} - -void -DefaultASTVisitor::visit (AST::AttrInputMacro &attr_input) -{ - visit (attr_input.get_macro ()); -} - -void -DefaultASTVisitor::visit (AST::MetaItemLitExpr &meta_item) -{ - visit (meta_item.get_literal ()); -} - -void -DefaultASTVisitor::visit (AST::SimplePathSegment &segment) -{} - -void -DefaultASTVisitor::visit (AST::SimplePath &path) -{ - for (auto &segment : path.get_segments ()) - visit (segment); -} - -void -DefaultASTVisitor::visit (AST::MetaItemPathLit &meta_item) -{ - visit (meta_item.get_path ()); - visit (meta_item.get_literal ()); -} - -void -DefaultASTVisitor::visit (AST::BorrowExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_borrowed_expr ()); -} - -void -DefaultASTVisitor::visit (AST::DereferenceExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_dereferenced_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ErrorPropagationExpr &expr) -{ - visit_outer_attrs (expr); -} - -void -DefaultASTVisitor::visit (AST::NegationExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_negated_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ArithmeticOrLogicalExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_left_expr ()); - visit (expr.get_right_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ComparisonExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_left_expr ()); - visit (expr.get_right_expr ()); -} - -void -DefaultASTVisitor::visit (AST::LazyBooleanExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_left_expr ()); - visit (expr.get_right_expr ()); -} - -void -DefaultASTVisitor::visit (AST::TypeCastExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_casted_expr ()); - visit (expr.get_type_to_cast_to ()); -} - -void -DefaultASTVisitor::visit (AST::AssignmentExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_left_expr ()); - visit (expr.get_right_expr ()); -} - -void -DefaultASTVisitor::visit (AST::CompoundAssignmentExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_left_expr ()); - visit (expr.get_right_expr ()); -} - -void -DefaultASTVisitor::visit (AST::GroupedExpr &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - visit (expr.get_expr_in_parens ()); -} - -void -DefaultASTVisitor::visit (AST::ArrayElemsValues &elems) -{ - for (auto &value : elems.get_values ()) - visit (value); -} - -void -DefaultASTVisitor::visit (AST::ArrayElemsCopied &elems) -{ - visit (elems.get_elem_to_copy ()); - visit (elems.get_num_copies ()); -} - -void -DefaultASTVisitor::visit (AST::ArrayExpr &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - visit (expr.get_array_elems ()); -} - -void -DefaultASTVisitor::visit (AST::ArrayIndexExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_array_expr ()); - visit (expr.get_index_expr ()); -} - -void -DefaultASTVisitor::visit (AST::TupleExpr &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - for (auto &elem : expr.get_tuple_elems ()) - visit (elem); -} - -void -DefaultASTVisitor::visit (AST::TupleIndexExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_tuple_expr ()); -} - -void -DefaultASTVisitor::visit (AST::StructExprStruct &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - visit (expr.get_struct_name ()); -} - -void -DefaultASTVisitor::visit (AST::StructExprFieldIdentifier &field) -{} - -void -DefaultASTVisitor::visit (AST::StructExprFieldIdentifierValue &field) -{ - visit (field.get_value ()); -} - -void -DefaultASTVisitor::visit (AST::StructExprFieldIndexValue &field) -{ - visit (field.get_value ()); -} - -void -DefaultASTVisitor::visit (AST::StructBase &base) -{ - visit (base.get_base_struct ()); -} - -void -DefaultASTVisitor::visit (AST::StructExprStructFields &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - visit (expr.get_struct_name ()); - if (expr.has_struct_base ()) - visit (expr.get_struct_base ()); - for (auto &field : expr.get_fields ()) - visit (field); -} - -void -DefaultASTVisitor::visit (AST::StructExprStructBase &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - visit (expr.get_struct_name ()); - visit (expr.get_struct_base ()); -} - -void -DefaultASTVisitor::visit (AST::CallExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_function_expr ()); - for (auto ¶m : expr.get_params ()) - visit (param); -} - -void -DefaultASTVisitor::visit (AST::MethodCallExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_receiver_expr ()); - visit (expr.get_method_name ()); - for (auto ¶m : expr.get_params ()) - visit (param); -} - -void -DefaultASTVisitor::visit (AST::FieldAccessExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_receiver_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ClosureExprInner &expr) -{ - visit_outer_attrs (expr); - for (auto ¶m : expr.get_params ()) - visit (param); - visit (expr.get_definition_expr ()); -} - -void -DefaultASTVisitor::visit (AST::BlockExpr &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - - if (expr.has_label ()) - visit (expr.get_label ()); - - for (auto &stmt : expr.get_statements ()) - visit (stmt); - - if (expr.has_tail_expr ()) - visit (expr.get_tail_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ConstBlock &expr) -{ - visit (expr.get_const_expr ()); -} - -void -DefaultASTVisitor::visit (AST::AnonConst &expr) -{ - if (!expr.is_deferred ()) - visit (expr.get_inner_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ClosureExprInnerTyped &expr) -{ - visit_outer_attrs (expr); - for (auto ¶m : expr.get_params ()) - visit (param); - visit (expr.get_return_type ()); - visit (expr.get_definition_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ClosureParam ¶m) -{ - visit_outer_attrs (param); - visit (param.get_pattern ()); - if (param.has_type_given ()) - visit (param.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::ContinueExpr &expr) -{ - visit_outer_attrs (expr); - if (expr.has_label ()) - visit (expr.get_label_unchecked ()); -} - -void -DefaultASTVisitor::visit (AST::BreakExpr &expr) -{ - visit_outer_attrs (expr); - if (expr.has_label ()) - visit (expr.get_label_unchecked ()); - - if (expr.has_break_expr ()) - visit (expr.get_break_expr ()); -} - -void -DefaultASTVisitor::visit (AST::RangeFromToExpr &expr) -{ - visit (expr.get_from_expr ()); - visit (expr.get_to_expr ()); -} - -void -DefaultASTVisitor::visit (AST::RangeFromExpr &expr) -{ - visit (expr.get_from_expr ()); -} - -void -DefaultASTVisitor::visit (AST::RangeToExpr &expr) -{ - visit (expr.get_to_expr ()); -} - -void -DefaultASTVisitor::visit (AST::RangeFullExpr &expr) -{} - -void -DefaultASTVisitor::visit (AST::RangeFromToInclExpr &expr) -{ - visit (expr.get_from_expr ()); - visit (expr.get_to_expr ()); -} - -void -DefaultASTVisitor::visit (AST::RangeToInclExpr &expr) -{ - visit (expr.get_to_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ReturnExpr &expr) -{ - visit_outer_attrs (expr); - if (expr.has_returned_expr ()) - visit (expr.get_returned_expr ()); -} - -void -DefaultASTVisitor::visit (AST::TryExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_block_expr ()); -} - -void -DefaultASTVisitor::visit (AST::BoxExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_boxed_expr ()); -} - -void -DefaultASTVisitor::visit (AST::UnsafeBlockExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_block_expr ()); -} - -void -DefaultASTVisitor::visit (AST::LoopLabel &label) -{ - visit (label.get_lifetime ()); -} - -void -DefaultASTVisitor::visit (AST::LoopExpr &expr) -{ - visit_outer_attrs (expr); - if (expr.has_loop_label ()) - visit (expr.get_loop_label ()); - visit (expr.get_loop_block ()); -} - -void -DefaultASTVisitor::visit (AST::WhileLoopExpr &expr) -{ - visit_outer_attrs (expr); - if (expr.has_loop_label ()) - visit (expr.get_loop_label ()); - visit (expr.get_predicate_expr ()); - visit (expr.get_loop_block ()); -} - -void -DefaultASTVisitor::visit (AST::WhileLetLoopExpr &expr) -{ - visit_outer_attrs (expr); - for (auto &pattern : expr.get_patterns ()) - visit (pattern); - - if (expr.has_loop_label ()) - visit (expr.get_loop_label ()); - - visit (expr.get_scrutinee_expr ()); - visit (expr.get_loop_block ()); -} - -void -DefaultASTVisitor::visit (AST::ForLoopExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_pattern ()); - visit (expr.get_iterator_expr ()); - if (expr.has_loop_label ()) - visit (expr.get_loop_label ()); - visit (expr.get_loop_block ()); -} - -void -DefaultASTVisitor::visit (AST::IfExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_condition_expr ()); - visit (expr.get_if_block ()); -} - -void -DefaultASTVisitor::visit (AST::IfExprConseqElse &expr) -{ - visit (reinterpret_cast (expr)); - visit (expr.get_else_block ()); -} - -void -DefaultASTVisitor::visit (AST::IfLetExpr &expr) -{ - visit_outer_attrs (expr); - for (auto &pattern : expr.get_patterns ()) - visit (pattern); - visit (expr.get_value_expr ()); - visit (expr.get_if_block ()); -} - -void -DefaultASTVisitor::visit (AST::IfLetExprConseqElse &expr) -{ - visit (reinterpret_cast (expr)); - visit (expr.get_else_block ()); -} - -void -DefaultASTVisitor::visit (AST::MatchArm &arm) -{ - visit_outer_attrs (arm); - for (auto &pattern : arm.get_patterns ()) - visit (pattern); - if (arm.has_match_arm_guard ()) - visit (arm.get_guard_expr ()); -} - -void -DefaultASTVisitor::visit (AST::MatchCase &arm) -{ - visit (arm.get_arm ()); - visit (arm.get_expr ()); -} - -void -DefaultASTVisitor::visit (AST::MatchExpr &expr) -{ - visit_outer_attrs (expr); - visit_inner_attrs (expr); - visit (expr.get_scrutinee_expr ()); - for (auto &arm : expr.get_match_cases ()) - visit (arm); -} - -void -DefaultASTVisitor::visit (AST::AwaitExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_awaited_expr ()); -} - -void -DefaultASTVisitor::visit (AST::AsyncBlockExpr &expr) -{ - visit_outer_attrs (expr); - visit (expr.get_block_expr ()); -} - -void -DefaultASTVisitor::visit (AST::InlineAsm &expr) -{ - visit_outer_attrs (expr); - using RegisterType = AST::InlineAsmOperand::RegisterType; - for (auto &operand : expr.get_operands ()) - { - switch (operand.get_register_type ()) - { - case RegisterType::In: - { - visit (operand.get_in ().expr); - break; - } - case RegisterType::Out: - { - visit (operand.get_out ().expr); - break; - } - case RegisterType::InOut: - { - visit (operand.get_in_out ().expr); - break; - } - case RegisterType::SplitInOut: - { - auto split = operand.get_split_in_out (); - visit (split.in_expr); - visit (split.out_expr); - break; - } - case RegisterType::Const: - { - visit (operand.get_const ().anon_const.get_inner_expr ()); - break; - } - case RegisterType::Sym: - { - visit (operand.get_sym ().expr); - break; - } - case RegisterType::Label: - { - visit (operand.get_label ().expr); - break; - } - } - } -} - -void -DefaultASTVisitor::visit (AST::LlvmInlineAsm &expr) -{ - for (auto &output : expr.get_outputs ()) - visit (output.expr); - - for (auto &input : expr.get_inputs ()) - visit (input.expr); -} - -void -DefaultASTVisitor::visit (AST::TypeParam ¶m) -{ - visit_outer_attrs (param); - for (auto &bound : param.get_type_param_bounds ()) - visit (bound); - if (param.has_type ()) - visit (param.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::LifetimeWhereClauseItem &item) -{ - visit (item.get_lifetime ()); - for (auto &bound : item.get_lifetime_bounds ()) - visit (bound); -} - -void -DefaultASTVisitor::visit (AST::TypeBoundWhereClauseItem &item) -{ - for (auto &lifetime : item.get_for_lifetimes ()) - visit (lifetime); - visit (item.get_type ()); - for (auto ¶m : item.get_type_param_bounds ()) - visit (param); -} - -void -DefaultASTVisitor::visit (AST::Visibility &vis) -{ - if (vis.has_path ()) - visit (vis.get_path ()); -} - -void -DefaultASTVisitor::visit (AST::FunctionQualifiers &qualifiers) -{} - -void -DefaultASTVisitor::visit (AST::WhereClause &where) -{ - for (auto &item : where.get_items ()) - visit (item); -} -void -DefaultASTVisitor::visit (AST::FunctionParam ¶m) -{ - visit_outer_attrs (param); - if (param.has_name ()) - visit (param.get_pattern ()); - - visit (param.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::SelfParam ¶m) -{ - visit_outer_attrs (param); - - if (param.has_lifetime ()) - visit (param.get_lifetime ()); - - if (param.has_type ()) - visit (param.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::Module &module) -{ - visit_outer_attrs (module); - visit (module.get_visibility ()); - visit_inner_attrs (module); - for (auto &item : module.get_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::ExternCrate &crate) -{ - visit_outer_attrs (crate); - visit (crate.get_visibility ()); -} - -void -DefaultASTVisitor::visit (AST::UseTreeGlob &use_tree) -{ - visit (use_tree.get_path ()); -} - -void -DefaultASTVisitor::visit (AST::UseTreeList &use_tree) -{ - visit (use_tree.get_path ()); -} - -void -DefaultASTVisitor::visit (AST::UseTreeRebind &use_tree) -{ - visit (use_tree.get_path ()); -} - -void -DefaultASTVisitor::visit (AST::UseDeclaration &use_decl) -{ - visit (use_decl.get_visibility ()); - visit (use_decl.get_tree ()); -} - -void -DefaultASTVisitor::visit_function_params (AST::Function &function) -{ - for (auto ¶m : function.get_function_params ()) - visit (param); -} - -void -DefaultASTVisitor::visit (AST::Function &function) -{ - visit_outer_attrs (function); - visit (function.get_visibility ()); - visit (function.get_qualifiers ()); - for (auto &generic : function.get_generic_params ()) - visit (generic); - - visit_function_params (function); - - if (function.has_return_type ()) - visit (function.get_return_type ()); - if (function.has_where_clause ()) - visit (function.get_where_clause ()); - if (function.has_body ()) - visit (*function.get_definition ()); -} - -void -DefaultASTVisitor::visit (AST::TypeAlias &type_alias) -{ - visit_outer_attrs (type_alias); - visit (type_alias.get_visibility ()); - for (auto &generic : type_alias.get_generic_params ()) - visit (generic); - if (type_alias.has_where_clause ()) - visit (type_alias.get_where_clause ()); - visit (type_alias.get_type_aliased ()); -} - -void -DefaultASTVisitor::visit (AST::StructField &field) -{ - visit_outer_attrs (field); - visit (field.get_visibility ()); - visit (field.get_field_type ()); -} - -void -DefaultASTVisitor::visit (AST::StructStruct &struct_item) -{ - visit_outer_attrs (struct_item); - visit (struct_item.get_visibility ()); - for (auto &generic : struct_item.get_generic_params ()) - visit (generic); - if (struct_item.has_where_clause ()) - visit (struct_item.get_where_clause ()); - for (auto &field : struct_item.get_fields ()) - visit (field); -} - -void -DefaultASTVisitor::visit (AST::TupleField &field) -{ - visit_outer_attrs (field); - visit (field.get_visibility ()); - visit (field.get_field_type ()); -} - -void -DefaultASTVisitor::visit (AST::TupleStruct &tuple_struct) -{ - visit_outer_attrs (tuple_struct); - visit (tuple_struct.get_visibility ()); - for (auto &generic : tuple_struct.get_generic_params ()) - visit (generic); - if (tuple_struct.has_where_clause ()) - visit (tuple_struct.get_where_clause ()); - for (auto &field : tuple_struct.get_fields ()) - visit (field); -} - -void -DefaultASTVisitor::visit (AST::EnumItem &item) -{ - visit_outer_attrs (item); - visit (item.get_visibility ()); -} - -void -DefaultASTVisitor::visit (AST::EnumItemTuple &item) -{ - DefaultASTVisitor::visit (static_cast (item)); - for (auto &field : item.get_tuple_fields ()) - visit (field); -} - -void -DefaultASTVisitor::visit (AST::EnumItemStruct &item) -{ - DefaultASTVisitor::visit (static_cast (item)); - for (auto &field : item.get_struct_fields ()) - visit (field); -} - -void -DefaultASTVisitor::visit (AST::EnumItemDiscriminant &item) -{ - DefaultASTVisitor::visit (static_cast (item)); - visit (item.get_expr ()); -} - -void -DefaultASTVisitor::visit (AST::Enum &enum_item) -{ - visit_outer_attrs (enum_item); - visit (enum_item.get_visibility ()); - for (auto &generic : enum_item.get_generic_params ()) - visit (generic); - if (enum_item.has_where_clause ()) - visit (enum_item.get_where_clause ()); - for (auto &item : enum_item.get_variants ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::Union &union_item) -{ - visit_outer_attrs (union_item); - visit (union_item.get_visibility ()); - for (auto &generic : union_item.get_generic_params ()) - visit (generic); - if (union_item.has_where_clause ()) - visit (union_item.get_where_clause ()); - for (auto &variant : union_item.get_variants ()) - visit (variant); -} - -void -DefaultASTVisitor::visit (AST::ConstantItem &const_item) -{ - visit_outer_attrs (const_item); - visit (const_item.get_visibility ()); - visit (const_item.get_type ()); - if (const_item.has_expr ()) - visit (const_item.get_expr ()); -} - -void -DefaultASTVisitor::visit (AST::StaticItem &static_item) -{ - visit_outer_attrs (static_item); - visit (static_item.get_visibility ()); - visit (static_item.get_type ()); - visit (static_item.get_expr ()); -} - -void -DefaultASTVisitor::visit (AST::TraitItemConst &item) -{ - visit_outer_attrs (item); - visit (item.get_type ()); - if (item.has_expr ()) - visit (item.get_expr ()); -} - -void -DefaultASTVisitor::visit (AST::TraitItemType &item) -{ - visit_outer_attrs (item); - for (auto &bound : item.get_type_param_bounds ()) - visit (bound); -} - -void -DefaultASTVisitor::visit (AST::Trait &trait) -{ - visit_outer_attrs (trait); - visit (trait.get_visibility ()); - - visit_inner_attrs (trait); - - visit (trait.get_implicit_self ()); - - for (auto &generic : trait.get_generic_params ()) - visit (generic); - - if (trait.has_where_clause ()) - visit (trait.get_where_clause ()); - - for (auto &bound : trait.get_type_param_bounds ()) - visit (bound); - - for (auto &item : trait.get_trait_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::InherentImpl &impl) -{ - visit_outer_attrs (impl); - visit (impl.get_visibility ()); - - for (auto &generic : impl.get_generic_params ()) - visit (generic); - if (impl.has_where_clause ()) - visit (impl.get_where_clause ()); - visit (impl.get_type ()); - visit_inner_attrs (impl); - for (auto &item : impl.get_impl_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::TraitImpl &impl) -{ - visit_outer_attrs (impl); - visit (impl.get_visibility ()); - - for (auto &generic : impl.get_generic_params ()) - visit (generic); - if (impl.has_where_clause ()) - visit (impl.get_where_clause ()); - visit (impl.get_type ()); - visit (impl.get_trait_path ()); - visit_inner_attrs (impl); - for (auto &item : impl.get_impl_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::ExternalTypeItem &item) -{ - visit_outer_attrs (item); - visit (item.get_visibility ()); -} - -void -DefaultASTVisitor::visit (AST::ExternalStaticItem &item) -{ - visit_outer_attrs (item); - visit (item.get_visibility ()); - visit (item.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::ExternBlock &block) -{ - visit_outer_attrs (block); - visit (block.get_visibility ()); - visit_inner_attrs (block); - for (auto &item : block.get_extern_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::MacroMatchFragment &match) -{} - -void -DefaultASTVisitor::visit (AST::MacroMatchRepetition &match) -{ - for (auto &m : match.get_matches ()) - visit (m); -} - -void -DefaultASTVisitor::visit (AST::MacroMatcher &matcher) -{ - for (auto &m : matcher.get_matches ()) - visit (m); -} - -void -DefaultASTVisitor::visit (AST::MacroTranscriber &transcriber) -{ - visit (transcriber.get_token_tree ()); -} - -void -DefaultASTVisitor::visit (AST::MacroRule &rule) -{ - visit (rule.get_matcher ()); - visit (rule.get_transcriber ()); -} -void -DefaultASTVisitor::visit (AST::MacroRulesDefinition &rules_def) -{ - visit_outer_attrs (rules_def); - for (auto &rule : rules_def.get_macro_rules ()) - visit (rule); -} - -void -DefaultASTVisitor::visit (AST::MacroInvocData &data) -{ - visit (data.get_path ()); - visit (data.get_delim_tok_tree ()); -} -void -DefaultASTVisitor::visit (AST::MacroInvocation ¯o_invoc) -{ - visit_outer_attrs (macro_invoc); - visit (macro_invoc.get_invoc_data ()); -} - -void -DefaultASTVisitor::visit (AST::MetaItemPath &meta_item) -{ - visit (meta_item.get_path ()); -} - -void -DefaultASTVisitor::visit (AST::MetaItemSeq &meta_item) -{ - visit (meta_item.get_path ()); - for (auto &inner : meta_item.get_seq ()) - visit (inner); -} - -void -DefaultASTVisitor::visit (AST::MetaWord &meta_item) -{} - -void -DefaultASTVisitor::visit (AST::MetaNameValueStr &meta_item) -{} - -void -DefaultASTVisitor::visit (AST::MetaListPaths &meta_item) -{ - for (auto &path : meta_item.get_paths ()) - visit (path); -} - -void -DefaultASTVisitor::visit (AST::MetaListNameValueStr &meta_item) -{ - for (auto &str : meta_item.get_values ()) - visit (str); -} - -void -DefaultASTVisitor::visit (AST::LiteralPattern &pattern) -{} - -void -DefaultASTVisitor::visit (AST::IdentifierPattern &pattern) -{ - if (pattern.has_subpattern ()) - visit (pattern.get_subpattern ()); -} - -void -DefaultASTVisitor::visit (AST::WildcardPattern &pattern) -{} - -void -DefaultASTVisitor::visit (AST::RestPattern &pattern) -{} - -void -DefaultASTVisitor::visit (AST::RangePatternBoundLiteral &bound) -{} - -void -DefaultASTVisitor::visit (AST::RangePatternBoundPath &bound) -{ - visit (bound.get_path ()); -} - -void -DefaultASTVisitor::visit (AST::RangePatternBoundQualPath &bound) -{ - visit (bound.get_qualified_path ()); -} - -void -DefaultASTVisitor::visit (AST::RangePattern &pattern) -{ - if (pattern.get_has_lower_bound ()) - visit (pattern.get_lower_bound ()); - if (pattern.get_has_upper_bound ()) - visit (pattern.get_upper_bound ()); -} - -void -DefaultASTVisitor::visit (AST::ReferencePattern &pattern) -{ - visit (pattern.get_referenced_pattern ()); -} - -void -DefaultASTVisitor::visit (AST::StructPatternFieldTuplePat &field) -{ - visit_outer_attrs (field); - visit (field.get_index_pattern ()); -} - -void -DefaultASTVisitor::visit (AST::StructPatternFieldIdentPat &field) -{ - visit_outer_attrs (field); - visit (field.get_ident_pattern ()); -} - -void -DefaultASTVisitor::visit (AST::StructPatternFieldIdent &field) -{ - visit_outer_attrs (field); -} - -void -DefaultASTVisitor::visit (AST::StructPatternElements &spe) -{ - for (auto &field : spe.get_struct_pattern_fields ()) - visit (field); - for (auto &attribute : spe.get_etc_outer_attrs ()) - visit (attribute); -} - -void -DefaultASTVisitor::visit (AST::StructPattern &pattern) -{ - visit (pattern.get_path ()); - visit (pattern.get_struct_pattern_elems ()); -} - -void -DefaultASTVisitor::visit (AST::TupleStructItemsNoRange &tuple_items) -{ - for (auto &pattern : tuple_items.get_patterns ()) - visit (pattern); -} - -void -DefaultASTVisitor::visit (AST::TupleStructItemsRange &tuple_items) -{ - for (auto &lower : tuple_items.get_lower_patterns ()) - visit (lower); - for (auto &upper : tuple_items.get_upper_patterns ()) - visit (upper); -} - -void -DefaultASTVisitor::visit (AST::TupleStructPattern &pattern) -{ - visit (pattern.get_path ()); - visit (pattern.get_items ()); -} - -void -DefaultASTVisitor::visit (AST::TuplePatternItemsMultiple &tuple_items) -{ - for (auto &pattern : tuple_items.get_patterns ()) - visit (pattern); -} - -void -DefaultASTVisitor::visit (AST::TuplePatternItemsRanged &tuple_items) -{ - for (auto &lower : tuple_items.get_lower_patterns ()) - visit (lower); - for (auto &upper : tuple_items.get_upper_patterns ()) - visit (upper); -} - -void -DefaultASTVisitor::visit (AST::TuplePattern &pattern) -{ - visit (pattern.get_items ()); -} - -void -DefaultASTVisitor::visit (AST::GroupedPattern &pattern) -{ - visit (pattern.get_pattern_in_parens ()); -} - -void -DefaultASTVisitor::visit (AST::SlicePattern &pattern) -{ - for (auto &item : pattern.get_items ()) - visit (item); -} - -void -DefaultASTVisitor::visit (AST::AltPattern &pattern) -{ - for (auto &alt : pattern.get_alts ()) - visit (alt); -} - -void -DefaultASTVisitor::visit (AST::EmptyStmt &stmt) -{} - -void -DefaultASTVisitor::visit (AST::LetStmt &stmt) -{ - visit_outer_attrs (stmt); - visit (stmt.get_pattern ()); - if (stmt.has_type ()) - visit (stmt.get_type ()); - if (stmt.has_init_expr ()) - visit (stmt.get_init_expr ()); -} - -void -DefaultASTVisitor::visit (AST::ExprStmt &stmt) -{ - visit (stmt.get_expr ()); -} - -void -DefaultASTVisitor::visit (AST::TraitBound &bound) -{ - for (auto &lifetime : bound.get_for_lifetimes ()) - visit (lifetime); - visit (bound.get_type_path ()); -} - -void -DefaultASTVisitor::visit (AST::ImplTraitType &type) -{ - for (auto &bound : type.get_type_param_bounds ()) - visit (bound); -} - -void -DefaultASTVisitor::visit (AST::TraitObjectType &type) -{ - for (auto &bound : type.get_type_param_bounds ()) - visit (bound); -} - -void -DefaultASTVisitor::visit (AST::ParenthesisedType &type) -{ - visit (type.get_type_in_parens ()); -} - -void -DefaultASTVisitor::visit (AST::ImplTraitTypeOneBound &type) -{ - visit (type.get_trait_bound ()); -} - -void -DefaultASTVisitor::visit (AST::TraitObjectTypeOneBound &type) -{ - visit (type.get_trait_bound ()); -} - -void -DefaultASTVisitor::visit (AST::TupleType &type) -{ - for (auto &elem : type.get_elems ()) - visit (elem); -} - -void -DefaultASTVisitor::visit (AST::NeverType &type) -{} - -void -DefaultASTVisitor::visit (AST::RawPointerType &type) -{ - visit (type.get_type_pointed_to ()); -} - -void -DefaultASTVisitor::visit (AST::ReferenceType &type) -{ - visit (type.get_lifetime ()); - visit (type.get_base_type ()); -} - -void -DefaultASTVisitor::visit (AST::ArrayType &type) -{ - visit (type.get_elem_type ()); - visit (type.get_size_expr ()); -} - -void -DefaultASTVisitor::visit (AST::SliceType &type) -{ - visit (type.get_elem_type ()); -} - -void -DefaultASTVisitor::visit (AST::InferredType &type) -{} - -void -DefaultASTVisitor::visit (AST::MaybeNamedParam ¶m) -{ - visit_outer_attrs (param); - visit (param.get_type ()); -} - -void -DefaultASTVisitor::visit (AST::BareFunctionType &type) +DefaultASTVisitor::visit_function_params (AST::Function &function) { - for (auto &lifetime : type.get_for_lifetimes ()) - visit (lifetime); - visit (type.get_function_qualifiers ()); - for (auto ¶m : type.get_function_params ()) + for (auto ¶m : function.get_function_params ()) visit (param); - if (type.is_variadic ()) - for (auto attr : type.get_variadic_attr ()) - visit (attr); - if (type.has_return_type ()) - visit (type.get_return_type ()); -} - -void -DefaultASTVisitor::visit (AST::FormatArgs &) -{ - // FIXME: Do we have anything to do? any subnodes to visit? Probably, right? } -void -DefaultASTVisitor::visit (AST::VariadicParam ¶m) -{ - if (param.has_pattern ()) - visit (param.get_pattern ()); -} +#define VISIT_DEF(T, param_name, ...) \ + void DefaultASTVisitor::visit (T ¶m_name) { __VA_ARGS__; } +#define VISIT_DEF_STUB(T) \ + void DefaultASTVisitor::visit (T &) {} +#define VISIT_UNESCAPE(vs) \ + { \ + vs; \ + } +#define VISIT_INNER_ATTRS(node) visit_inner_attrs (node) +#define VISIT_OUTER_ATTRS(node) visit_outer_attrs (node) +#define VISIT(node) visit (node) +#define VISIT_COND(cond, vs) ((cond) ? vs : (void) 0) +#define VISIT_ESCAPE(...) (([&, this] () { __VA_ARGS__ }) ()) +#define VISIT_VEC(vec) \ + VISIT_ESCAPE (for (auto &visit_vec_internal \ + : (vec)) visit (visit_vec_internal);) +#define VISIT_FN_PARAMS(fn) visit_function_params (fn) + +#include "rust-ast-visitable-inner.h" + +#undef VISIT_DEF +#undef VISIT_DEF_STUB +#undef VISIT_UNESCAPE +#undef VISIT_INNER_ATTRS +#undef VISIT_OUTER_ATTRS +#undef VISIT +#undef VISIT_COND +#undef VISIT_ESCAPE +#undef VISIT_VEC +#undef VISIT_FN_PARAMS void ContextualASTVisitor::visit (AST::Crate &crate)