]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: derive: Add common comparison derive class
authorArthur Cohen <arthur.cohen@embecosm.com>
Fri, 18 Apr 2025 16:18:40 +0000 (18:18 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:47 +0000 (16:36 +0200)
gcc/rust/ChangeLog:

* expand/rust-derive-cmp-common.cc: New file.
* expand/rust-derive-cmp-common.h: New file.
* Make-lang.in: Compile them.

gcc/rust/Make-lang.in
gcc/rust/expand/rust-derive-cmp-common.cc [new file with mode: 0644]
gcc/rust/expand/rust-derive-cmp-common.h [new file with mode: 0644]

index c919b3d43e25393d12d589b72f4700791fbe27cc..85614b3e22466dd801ded6e82562c793266b48b2 100644 (file)
@@ -94,6 +94,7 @@ GRS_OBJS = \
     rust/rust-ast-builder.o \
     rust/rust-ast-builder-type.o \
     rust/rust-derive.o \
+    rust/rust-derive-cmp-common.o \
     rust/rust-derive-clone.o \
     rust/rust-derive-copy.o \
     rust/rust-derive-debug.o \
diff --git a/gcc/rust/expand/rust-derive-cmp-common.cc b/gcc/rust/expand/rust-derive-cmp-common.cc
new file mode 100644 (file)
index 0000000..ba260c5
--- /dev/null
@@ -0,0 +1,69 @@
+// 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
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-derive-cmp-common.h"
+#include "rust-ast-builder.h"
+
+namespace Rust {
+namespace AST {
+
+SelfOther
+SelfOther::index (Builder builder, int idx)
+{
+  return SelfOther{
+    builder.tuple_idx ("self", idx),
+    builder.tuple_idx ("other", idx),
+  };
+}
+
+std::vector<SelfOther>
+SelfOther::indexes (Builder builder, int limit)
+{
+  std::vector<SelfOther> vec;
+
+  for (int i = 0; i < limit; i++)
+    {
+      vec.emplace_back (SelfOther::index (builder, i));
+    }
+
+  return vec;
+}
+
+SelfOther
+SelfOther::field (Builder builder, const std::string &field_name)
+{
+  return SelfOther{
+    builder.field_access (builder.identifier ("self"), field_name),
+    builder.field_access (builder.identifier ("other"), field_name),
+  };
+}
+
+std::vector<SelfOther>
+SelfOther::fields (Builder builder, const std::vector<StructField> &fields)
+{
+  std::vector<SelfOther> vec;
+
+  for (auto &field : fields)
+    vec.emplace_back (
+      SelfOther::field (builder, field.get_field_name ().as_string ()));
+
+  return vec;
+}
+
+} // namespace AST
+} // namespace Rust
diff --git a/gcc/rust/expand/rust-derive-cmp-common.h b/gcc/rust/expand/rust-derive-cmp-common.h
new file mode 100644 (file)
index 0000000..2353c71
--- /dev/null
@@ -0,0 +1,50 @@
+// 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
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_DERIVE_CMP_COMMON_H
+#define RUST_DERIVE_CMP_COMMON_H
+
+#include "rust-ast.h"
+#include "rust-ast-builder.h"
+
+namespace Rust {
+namespace AST {
+
+/**
+ * A pair of two expressions from each instance being compared. E.g. this
+ * could be `self.0` and `other.0`, or `self.field` and `other.field`
+ */
+struct SelfOther
+{
+  std::unique_ptr<Expr> self_expr;
+  std::unique_ptr<Expr> other_expr;
+
+  /* Create a <self.i> and an <other.i> expression */
+  static SelfOther index (Builder builder, int idx);
+  static std::vector<SelfOther> indexes (Builder builder, int limit);
+
+  /* Create a <self.field> and an <other.field> expression */
+  static SelfOther field (Builder builder, const std::string &field_name);
+  static std::vector<SelfOther> fields (Builder builder,
+                                       const std::vector<StructField> &fields);
+};
+
+} // namespace AST
+} // namespace Rust
+
+#endif // ! RUST_DERIVE_CMP_COMMON_H