From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 83174386CE52; Wed, 8 Jun 2022 12:41:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 83174386CE52 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Code deduplication for expression and type expansions. X-Act-Checkin: gcc X-Git-Author: antego X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: f38bf60c3ac7f039db5ff5c7c0587325097892c8 X-Git-Newrev: 5fe52462aa360852a33af4a40f0bd52e1709cca2 Message-Id: <20220608124141.83174386CE52@sourceware.org> Date: Wed, 8 Jun 2022 12:41:41 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:41:41 -0000 https://gcc.gnu.org/g:5fe52462aa360852a33af4a40f0bd52e1709cca2 commit 5fe52462aa360852a33af4a40f0bd52e1709cca2 Author: antego Date: Sun May 1 11:26:24 2022 +1000 Code deduplication for expression and type expansions. Follow-up for #1161 Diff: --- gcc/rust/expand/rust-attribute-visitor.cc | 174 ++++++++++-------------------- gcc/rust/expand/rust-attribute-visitor.h | 2 + 2 files changed, 56 insertions(+), 120 deletions(-) diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc index 842fbdf680a..771cd996191 100644 --- a/gcc/rust/expand/rust-attribute-visitor.cc +++ b/gcc/rust/expand/rust-attribute-visitor.cc @@ -41,9 +41,7 @@ AttrVisitor::expand_struct_fields (std::vector &fields) auto &type = field.get_field_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -110,9 +108,7 @@ AttrVisitor::expand_function_params (std::vector ¶ms) auto &type = param.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -137,9 +133,7 @@ AttrVisitor::expand_generic_args (AST::GenericArgs &args) for (auto &type : args.get_type_args ()) { type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -169,9 +163,7 @@ AttrVisitor::expand_qualified_path_type (AST::QualifiedPathType &path_type) auto &type = path_type.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); expander.pop_context (); @@ -216,9 +208,7 @@ AttrVisitor::AttrVisitor::expand_closure_params ( auto &type = param.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -241,9 +231,7 @@ AttrVisitor::expand_self_param (AST::SelfParam &self_param) auto &type = self_param.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -281,9 +269,7 @@ AttrVisitor::expand_trait_function_decl (AST::TraitFunctionDecl &decl) auto &return_type = decl.get_return_type (); return_type->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - return_type = r_fragment.take_type_fragment (); + maybe_expand_type (return_type); if (return_type->is_marked_for_strip ()) rust_error_at (return_type->get_locus (), @@ -319,9 +305,7 @@ AttrVisitor::expand_trait_method_decl (AST::TraitMethodDecl &decl) auto &return_type = decl.get_return_type (); return_type->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - return_type = r_fragment.take_type_fragment (); + maybe_expand_type (return_type); if (return_type->is_marked_for_strip ()) rust_error_at (return_type->get_locus (), @@ -445,9 +429,7 @@ AttrVisitor::visit (AST::TypePathSegmentFunction &segment) auto &return_type = type_path_function.get_return_type (); return_type->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - return_type = r_fragment.take_type_fragment (); + maybe_expand_type (return_type); if (return_type->is_marked_for_strip ()) rust_error_at (return_type->get_locus (), @@ -612,17 +594,13 @@ AttrVisitor::visit (AST::ArithmeticOrLogicalExpr &expr) * with outer expr */ auto &l_expr = expr.get_left_expr (); l_expr->accept_vis (*this); - auto l_fragment = expander.take_expanded_fragment (*this); - if (l_fragment.should_expand ()) - l_expr = l_fragment.take_expression_fragment (); + maybe_expand_expr (l_expr); /* should syntactically not have outer attributes, though this may * not have worked in practice */ auto &r_expr = expr.get_right_expr (); r_expr->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - r_expr = r_fragment.take_expression_fragment (); + maybe_expand_expr (r_expr); // ensure that they are not marked for strip if (expr.get_left_expr ()->is_marked_for_strip ()) @@ -645,17 +623,13 @@ AttrVisitor::visit (AST::ComparisonExpr &expr) * with outer expr */ auto &l_expr = expr.get_left_expr (); l_expr->accept_vis (*this); - auto l_fragment = expander.take_expanded_fragment (*this); - if (l_fragment.should_expand ()) - l_expr = l_fragment.take_expression_fragment (); + maybe_expand_expr (l_expr); /* should syntactically not have outer attributes, though this may * not have worked in practice */ auto &r_expr = expr.get_right_expr (); r_expr->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - r_expr = r_fragment.take_expression_fragment (); + maybe_expand_expr (r_expr); // ensure that they are not marked for strip if (expr.get_left_expr ()->is_marked_for_strip ()) @@ -678,17 +652,13 @@ AttrVisitor::visit (AST::LazyBooleanExpr &expr) * with outer expr */ auto &l_expr = expr.get_left_expr (); l_expr->accept_vis (*this); - auto l_fragment = expander.take_expanded_fragment (*this); - if (l_fragment.should_expand ()) - l_expr = l_fragment.take_expression_fragment (); + maybe_expand_expr (l_expr); /* should syntactically not have outer attributes, though this may * not have worked in practice */ auto &r_expr = expr.get_right_expr (); r_expr->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - r_expr = r_fragment.take_expression_fragment (); + maybe_expand_expr (r_expr); // ensure that they are not marked for strip if (expr.get_left_expr ()->is_marked_for_strip ()) @@ -738,17 +708,13 @@ AttrVisitor::visit (AST::AssignmentExpr &expr) * with outer expr */ auto &l_expr = expr.get_left_expr (); l_expr->accept_vis (*this); - auto l_fragment = expander.take_expanded_fragment (*this); - if (l_fragment.should_expand ()) - l_expr = l_fragment.take_expression_fragment (); + maybe_expand_expr (l_expr); /* should syntactically not have outer attributes, though this may * not have worked in practice */ auto &r_expr = expr.get_right_expr (); r_expr->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - r_expr = r_fragment.take_expression_fragment (); + maybe_expand_expr (r_expr); // ensure that they are not marked for strip if (expr.get_left_expr ()->is_marked_for_strip ()) @@ -771,17 +737,13 @@ AttrVisitor::visit (AST::CompoundAssignmentExpr &expr) * with outer expr */ auto &l_expr = expr.get_left_expr (); l_expr->accept_vis (*this); - auto l_frag = expander.take_expanded_fragment (*this); - if (l_frag.should_expand ()) - l_expr = l_frag.take_expression_fragment (); + maybe_expand_expr (l_expr); /* should syntactically not have outer attributes, though this may * not have worked in practice */ auto &r_expr = expr.get_right_expr (); r_expr->accept_vis (*this); - auto r_frag = expander.take_expanded_fragment (*this); - if (r_frag.should_expand ()) - r_expr = r_frag.take_expression_fragment (); + maybe_expand_expr (r_expr); // ensure that they are not marked for strip if (expr.get_left_expr ()->is_marked_for_strip ()) @@ -1259,9 +1221,7 @@ AttrVisitor::visit (AST::BlockExpr &expr) auto &tail_expr = expr.get_tail_expr (); tail_expr->accept_vis (*this); - auto fragment = expander.take_expanded_fragment (*this); - if (fragment.should_expand ()) - tail_expr = fragment.take_expression_fragment (); + maybe_expand_expr (tail_expr); if (tail_expr->is_marked_for_strip ()) expr.strip_tail_expr (); @@ -1290,9 +1250,7 @@ AttrVisitor::visit (AST::ClosureExprInnerTyped &expr) auto &type = expr.get_return_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -1630,9 +1588,7 @@ AttrVisitor::visit (AST::IfExpr &expr) // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); condition_expr->accept_vis (*this); - auto cond_fragment = expander.take_expanded_fragment (*this); - if (cond_fragment.should_expand ()) - condition_expr = cond_fragment.take_expression_fragment (); + maybe_expand_expr (condition_expr); if (condition_expr->is_marked_for_strip ()) rust_error_at (condition_expr->get_locus (), "cannot strip expression in this position - outer " @@ -1660,9 +1616,7 @@ AttrVisitor::visit (AST::IfExprConseqElse &expr) // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); condition_expr->accept_vis (*this); - auto cond_fragment = expander.take_expanded_fragment (*this); - if (cond_fragment.should_expand ()) - condition_expr = cond_fragment.take_expression_fragment (); + maybe_expand_expr (condition_expr); if (condition_expr->is_marked_for_strip ()) rust_error_at (condition_expr->get_locus (), "cannot strip expression in this position - outer " @@ -1698,9 +1652,7 @@ AttrVisitor::visit (AST::IfExprConseqIf &expr) // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); condition_expr->accept_vis (*this); - auto cond_fragment = expander.take_expanded_fragment (*this); - if (cond_fragment.should_expand ()) - condition_expr = cond_fragment.take_expression_fragment (); + maybe_expand_expr (condition_expr); if (condition_expr->is_marked_for_strip ()) rust_error_at (condition_expr->get_locus (), "cannot strip expression in this position - outer " @@ -1736,9 +1688,7 @@ AttrVisitor::visit (AST::IfExprConseqIfLet &expr) // can't strip condition expr itself, but can strip sub-expressions auto &condition_expr = expr.get_condition_expr (); condition_expr->accept_vis (*this); - auto cond_fragment = expander.take_expanded_fragment (*this); - if (cond_fragment.should_expand ()) - condition_expr = cond_fragment.take_expression_fragment (); + maybe_expand_expr (condition_expr); if (condition_expr->is_marked_for_strip ()) rust_error_at (condition_expr->get_locus (), "cannot strip expression in this position - outer " @@ -2061,9 +2011,7 @@ AttrVisitor::visit (AST::TypeParam ¶m) auto &type = param.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -2087,9 +2035,7 @@ AttrVisitor::visit (AST::TypeBoundWhereClauseItem &item) auto &type = item.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2131,9 +2077,7 @@ AttrVisitor::visit (AST::Method &method) auto &return_type = method.get_return_type (); return_type->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - return_type = r_fragment.take_type_fragment (); + maybe_expand_type (return_type); if (return_type->is_marked_for_strip ()) rust_error_at (return_type->get_locus (), @@ -2253,9 +2197,7 @@ AttrVisitor::visit (AST::Function &function) auto &return_type = function.get_return_type (); return_type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - return_type = t_fragment.take_type_fragment (); + maybe_expand_type (return_type); if (return_type->is_marked_for_strip ()) rust_error_at (return_type->get_locus (), @@ -2467,9 +2409,7 @@ AttrVisitor::visit (AST::ConstantItem &const_item) auto &type = const_item.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2503,9 +2443,7 @@ AttrVisitor::visit (AST::StaticItem &static_item) auto &type = static_item.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2591,9 +2529,7 @@ AttrVisitor::visit (AST::TraitItemConst &item) auto &type = item.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2698,9 +2634,7 @@ AttrVisitor::visit (AST::InherentImpl &impl) auto &type = impl.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2744,9 +2678,7 @@ AttrVisitor::visit (AST::TraitImpl &impl) auto &type = impl.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2785,9 +2717,7 @@ AttrVisitor::visit (AST::ExternalStaticItem &item) auto &type = item.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), "cannot strip type in this position"); @@ -2829,9 +2759,7 @@ AttrVisitor::visit (AST::ExternalFunctionItem &item) auto &type = param.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -2856,9 +2784,7 @@ AttrVisitor::visit (AST::ExternalFunctionItem &item) auto &return_type = item.get_return_type (); return_type->accept_vis (*this); - auto r_fragment = expander.take_expanded_fragment (*this); - if (r_fragment.should_expand ()) - return_type = r_fragment.take_type_fragment (); + maybe_expand_type (return_type); if (return_type->is_marked_for_strip ()) rust_error_at (return_type->get_locus (), @@ -3233,9 +3159,7 @@ AttrVisitor::visit (AST::LetStmt &stmt) auto &type = stmt.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -3257,9 +3181,7 @@ AttrVisitor::visit (AST::LetStmt &stmt) "cannot strip expression in this position - outer " "attributes not allowed"); - auto fragment = expander.take_expanded_fragment (*this); - if (fragment.should_expand ()) - init_expr = fragment.take_expression_fragment (); + maybe_expand_expr (init_expr); } } void @@ -3441,9 +3363,7 @@ AttrVisitor::visit (AST::BareFunctionType &type) auto &type = param.get_type (); type->accept_vis (*this); - auto t_fragment = expander.take_expanded_fragment (*this); - if (t_fragment.should_expand ()) - type = t_fragment.take_type_fragment (); + maybe_expand_type (type); if (type->is_marked_for_strip ()) rust_error_at (type->get_locus (), @@ -3472,4 +3392,18 @@ AttrVisitor::visit (AST::BareFunctionType &type) // no where clause, apparently } +void +AttrVisitor::maybe_expand_expr (std::unique_ptr &expr) +{ + auto fragment = expander.take_expanded_fragment (*this); + if (fragment.should_expand ()) + expr = fragment.take_expression_fragment (); +} +void +AttrVisitor::maybe_expand_type (std::unique_ptr &type) +{ + auto fragment = expander.take_expanded_fragment (*this); + if (fragment.should_expand ()) + type = fragment.take_type_fragment (); +} } // namespace Rust diff --git a/gcc/rust/expand/rust-attribute-visitor.h b/gcc/rust/expand/rust-attribute-visitor.h index 1c6410d2304..4b42d78cf89 100644 --- a/gcc/rust/expand/rust-attribute-visitor.h +++ b/gcc/rust/expand/rust-attribute-visitor.h @@ -25,6 +25,8 @@ class AttrVisitor : public AST::ASTVisitor { private: MacroExpander &expander; + void maybe_expand_expr (std::unique_ptr &expr); + void maybe_expand_type (std::unique_ptr &expr); public: AttrVisitor (MacroExpander &expander) : expander (expander) {}