]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: derive(Ord, PartialOrd): Finish implementation
authorArthur Cohen <arthur.cohen@embecosm.com>
Tue, 22 Apr 2025 20:22:29 +0000 (22:22 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:48 +0000 (16:36 +0200)
gcc/rust/ChangeLog:

* expand/rust-derive-ord.cc: Finish implementation for enums.
* expand/rust-derive-ord.h: Likewise.

gcc/rust/expand/rust-derive-ord.cc
gcc/rust/expand/rust-derive-ord.h

index 1f39c94d87bb80f6becfa1896f9d2196866272ce..68a9c53ab1c9b77f276ca00bf8cc6ec8832ef08c 100644 (file)
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-derive-ord.h"
-#include "rust-ast-dump.h"
 #include "rust-ast.h"
+#include "rust-derive-cmp-common.h"
 #include "rust-derive.h"
 #include "rust-item.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace AST {
@@ -34,8 +35,6 @@ DeriveOrd::go (Item &item)
 {
   item.accept_vis (*this);
 
-  AST::Dump::debug (*expanded);
-
   return std::move (expanded);
 }
 
@@ -234,25 +233,38 @@ DeriveOrd::visit_tuple (TupleStruct &item)
 void
 DeriveOrd::visit_enum (Enum &item)
 {
+  // NOTE: We can factor this even further with DerivePartialEq, but this is
+  // getting out of scope for this PR surely
+
   auto cases = std::vector<MatchCase> ();
   auto type_name = item.get_identifier ().as_string ();
 
   auto let_sd = builder.discriminant_value (DeriveOrd::self_discr, "self");
-  auto other_sd = builder.discriminant_value (DeriveOrd::other_discr, "other");
+  auto let_od = builder.discriminant_value (DeriveOrd::other_discr, "other");
 
   auto discr_cmp = cmp_call (builder.identifier (DeriveOrd::self_discr),
                             builder.identifier (DeriveOrd::other_discr));
 
+  auto recursive_match_fn = [this] (std::vector<SelfOther> &&fields) {
+    return recursive_match (std::move (fields));
+  };
+
   for (auto &variant : item.get_variants ())
     {
       auto variant_path
        = builder.variant_path (type_name,
                                variant->get_identifier ().as_string ());
+      auto enum_builder
+       = EnumMatchBuilder (variant_path, recursive_match_fn, builder);
 
       switch (variant->get_enum_item_kind ())
        {
-       case EnumItem::Kind::Tuple:
        case EnumItem::Kind::Struct:
+         cases.emplace_back (enum_builder.strukt (*variant));
+         break;
+       case EnumItem::Kind::Tuple:
+         cases.emplace_back (enum_builder.tuple (*variant));
+         break;
        case EnumItem::Kind::Identifier:
        case EnumItem::Kind::Discriminant:
          // We don't need to do anything for these, as they are handled by the
@@ -272,7 +284,7 @@ DeriveOrd::visit_enum (Enum &item)
                     std::move (cases));
 
   expanded
-    = cmp_impl (builder.block (vec (std::move (let_sd), std::move (other_sd)),
+    = cmp_impl (builder.block (vec (std::move (let_sd), std::move (let_od)),
                               std::move (match)),
                type_name, item.get_generic_params ());
 }
index a360dd26d974f413de79d6802bbd33fa2d789697..20086afe9b7f1d790bec4f06e73e34244fded03d 100644 (file)
@@ -91,11 +91,6 @@ private:
    */
   std::pair<MatchArm, MatchArm> make_cmp_arms ();
 
-  MatchCase match_enum_tuple (PathInExpression variant_path,
-                             const EnumItemTuple &variant);
-  MatchCase match_enum_struct (PathInExpression variant_path,
-                              const EnumItemStruct &variant);
-
   /**
    * Generate a call to the proper trait function, based on the ordering, in
    * order to compare two given expressions