https://gcc.gnu.org/g:0a316f44a0811b54b72aa68997ee62a66ff54ebe

commit 0a316f44a0811b54b72aa68997ee62a66ff54ebe
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Wed Apr 16 16:03:26 2025 +0200

    backend: Refactor struct pattern compilation
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-pattern.h: Split struct pattern compilation 
into three functions.
            * backend/rust-compile-pattern.cc: Implement them.

Diff:
---
 gcc/rust/backend/rust-compile-pattern.cc | 105 +++++++++++++++++--------------
 gcc/rust/backend/rust-compile-pattern.h  |   9 +++
 2 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-pattern.cc 
b/gcc/rust/backend/rust-compile-pattern.cc
index 1e33c4899634..a83607294e19 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -22,6 +22,8 @@
 #include "rust-constexpr.h"
 #include "rust-compile-type.h"
 #include "print-tree.h"
+#include "rust-hir-pattern.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace Compile {
@@ -504,6 +506,57 @@ CompilePatternBindings::visit (HIR::TupleStructPattern 
&pattern)
     }
 }
 
+void
+CompilePatternBindings::handle_struct_pattern_ident (
+  HIR::StructPatternField &pat, TyTy::ADTType *adt, TyTy::VariantDef *variant,
+  int variant_index)
+{
+  HIR::StructPatternFieldIdent &ident
+    = static_cast<HIR::StructPatternFieldIdent &> (pat);
+
+  size_t offs = 0;
+  auto ok = variant->lookup_field (ident.get_identifier ().as_string (),
+                                  nullptr, &offs);
+  rust_assert (ok);
+
+  tree binding = error_mark_node;
+  if (adt->is_enum ())
+    {
+      tree payload_accessor_union
+       = Backend::struct_field_expression (match_scrutinee_expr, 1,
+                                           ident.get_locus ());
+
+      tree variant_accessor
+       = Backend::struct_field_expression (payload_accessor_union,
+                                           variant_index, ident.get_locus ());
+
+      binding = Backend::struct_field_expression (variant_accessor, offs,
+                                                 ident.get_locus ());
+    }
+  else
+    {
+      tree variant_accessor = match_scrutinee_expr;
+      binding = Backend::struct_field_expression (variant_accessor, offs,
+                                                 ident.get_locus ());
+    }
+
+  ctx->insert_pattern_binding (ident.get_mappings ().get_hirid (), binding);
+}
+
+void
+CompilePatternBindings::handle_struct_pattern_ident_pat (
+  HIR::StructPatternField &pat)
+{
+  rust_unreachable ();
+}
+
+void
+CompilePatternBindings::handle_struct_pattern_tuple_pat (
+  HIR::StructPatternField &pat)
+{
+  rust_unreachable ();
+}
+
 void
 CompilePatternBindings::visit (HIR::StructPattern &pattern)
 {
@@ -539,54 +592,14 @@ CompilePatternBindings::visit (HIR::StructPattern 
&pattern)
     {
       switch (field->get_item_type ())
        {
-         case HIR::StructPatternField::ItemType::TUPLE_PAT: {
-           // TODO
-           rust_unreachable ();
-         }
+       case HIR::StructPatternField::ItemType::TUPLE_PAT:
+         handle_struct_pattern_tuple_pat (*field);
          break;
-
-         case HIR::StructPatternField::ItemType::IDENT_PAT: {
-           // TODO
-           rust_unreachable ();
-         }
+       case HIR::StructPatternField::ItemType::IDENT_PAT:
+         handle_struct_pattern_ident_pat (*field);
          break;
-
-         case HIR::StructPatternField::ItemType::IDENT: {
-           HIR::StructPatternFieldIdent &ident
-             = static_cast<HIR::StructPatternFieldIdent &> (*field.get ());
-
-           size_t offs = 0;
-           ok = variant->lookup_field (ident.get_identifier ().as_string (),
-                                       nullptr, &offs);
-           rust_assert (ok);
-
-           tree binding = error_mark_node;
-           if (adt->is_enum ())
-             {
-               tree payload_accessor_union
-                 = Backend::struct_field_expression (match_scrutinee_expr, 1,
-                                                     ident.get_locus ());
-
-               tree variant_accessor
-                 = Backend::struct_field_expression (payload_accessor_union,
-                                                     variant_index,
-                                                     ident.get_locus ());
-
-               binding
-                 = Backend::struct_field_expression (variant_accessor, offs,
-                                                     ident.get_locus ());
-             }
-           else
-             {
-               tree variant_accessor = match_scrutinee_expr;
-               binding
-                 = Backend::struct_field_expression (variant_accessor, offs,
-                                                     ident.get_locus ());
-             }
-
-           ctx->insert_pattern_binding (ident.get_mappings ().get_hirid (),
-                                        binding);
-         }
+       case HIR::StructPatternField::ItemType::IDENT:
+         handle_struct_pattern_ident (*field, adt, variant, variant_index);
          break;
        }
     }
diff --git a/gcc/rust/backend/rust-compile-pattern.h 
b/gcc/rust/backend/rust-compile-pattern.h
index fc12859a4646..dea63e3ca346 100644
--- a/gcc/rust/backend/rust-compile-pattern.h
+++ b/gcc/rust/backend/rust-compile-pattern.h
@@ -17,7 +17,9 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-compile-base.h"
+#include "rust-hir-pattern.h"
 #include "rust-hir-visitor.h"
+#include "rust-tyty.h"
 
 namespace Rust {
 namespace Compile {
@@ -78,6 +80,13 @@ public:
     pattern.accept_vis (compiler);
   }
 
+  void handle_struct_pattern_ident (HIR::StructPatternField &pat,
+                                   TyTy::ADTType *adt,
+                                   TyTy::VariantDef *variant,
+                                   int variant_index);
+  void handle_struct_pattern_ident_pat (HIR::StructPatternField &pat);
+  void handle_struct_pattern_tuple_pat (HIR::StructPatternField &pat);
+
   void visit (HIR::StructPattern &pattern) override;
   void visit (HIR::TupleStructPattern &pattern) override;
   void visit (HIR::ReferencePattern &pattern) override;

Reply via email to