]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: derive(Clone, Copy): Cleanup using new method from DeriveVisitor
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Jan 2025 10:01:33 +0000 (11:01 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 24 Mar 2025 12:07:00 +0000 (13:07 +0100)
gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc: Cleanup using DeriveVisitor::setup_impl_generics.
* expand/rust-derive-copy.cc: Likewise.

gcc/rust/expand/rust-derive-clone.cc
gcc/rust/expand/rust-derive-copy.cc

index 18f7723ddc2f6359a41f4ce3260da56f97e7a917..b1b39668f51a61ccb1cc36b9d50866d825e8b709 100644 (file)
@@ -94,89 +94,14 @@ DeriveClone::clone_impl (
 {
   auto clone = builder.type_path (LangItem::Kind::CLONE);
 
-  auto trait_items = std::vector<std::unique_ptr<AssociatedItem>> ();
-  trait_items.emplace_back (std::move (clone_fn));
-
-  // We need to build up the generics for this impl block which will be just a
-  // clone of the generics specified, with added `Clone` bounds
-  //
-  // For example with:
-  //
-  // #[derive(Clone)]
-  // struct Be<T> { ... }
-  //
-  // We need to generate the following impl block:
-  //
-  // impl<T: Clone> Clone for Be<T>
-
-  std::vector<Lifetime> lifetime_args;
-  std::vector<GenericArg> generic_args;
-  std::vector<std::unique_ptr<GenericParam>> impl_generics;
-  for (const auto &generic : type_generics)
-    {
-      switch (generic->get_kind ())
-       {
-         case GenericParam::Kind::Lifetime: {
-           LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
-
-           Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
-           lifetime_args.push_back (std::move (l));
-
-           auto impl_lifetime_param
-             = builder.new_lifetime_param (lifetime_param);
-           impl_generics.push_back (std::move (impl_lifetime_param));
-         }
-         break;
-
-         case GenericParam::Kind::Type: {
-           TypeParam &type_param = (TypeParam &) *generic.get ();
-
-           std::unique_ptr<Type> associated_type = builder.single_type_path (
-             type_param.get_type_representation ().as_string ());
-
-           GenericArg type_arg
-             = GenericArg::create_type (std::move (associated_type));
-           generic_args.push_back (std::move (type_arg));
+  auto trait_items = vec (std::move (clone_fn));
 
-           std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
-           extra_bounds.emplace_back (std::unique_ptr<TypeParamBound> (
-             new TraitBound (builder.type_path (LangItem::Kind::CLONE), loc)));
-
-           auto impl_type_param
-             = builder.new_type_param (type_param, std::move (extra_bounds));
-           impl_generics.push_back (std::move (impl_type_param));
-         }
-         break;
-
-         case GenericParam::Kind::Const: {
-           rust_unreachable ();
-
-           // TODO
-           // const ConstGenericParam *const_param
-           //   = (const ConstGenericParam *) generic.get ();
-           // std::unique_ptr<Expr> const_expr = nullptr;
-
-           // GenericArg type_arg
-           //   = GenericArg::create_const (std::move (const_expr));
-           // generic_args.push_back (std::move (type_arg));
-         }
-         break;
-       }
-    }
+  auto generics
+    = setup_impl_generics (name, type_generics, builder.trait_bound (clone));
 
-  GenericArgs generic_args_for_self (lifetime_args, generic_args,
-                                    {} /*binding args*/, loc);
-  std::unique_ptr<Type> self_type_path
-    = impl_generics.empty ()
-       ? builder.single_type_path (name)
-       : builder.single_generic_type_path (name, generic_args_for_self);
-
-  return std::unique_ptr<Item> (
-    new TraitImpl (clone, /* unsafe */ false,
-                  /* exclam */ false, std::move (trait_items),
-                  std::move (impl_generics), std::move (self_type_path),
-                  WhereClause::create_empty (), Visibility::create_private (),
-                  {}, {}, loc));
+  return builder.trait_impl (clone, std::move (generics.self_type),
+                            std::move (trait_items),
+                            std::move (generics.impl));
 }
 
 // TODO: Create new `make_qualified_call` helper function
index 1d5f72bd656c230deb89b5bf407986182c35489a..e91a3508bdee8d0e43aa828c94ea2dd17a8a4f73 100644 (file)
@@ -17,9 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-derive-copy.h"
-#include "rust-ast-full.h"
 #include "rust-hir-map.h"
-#include "rust-mapping-common.h"
 #include "rust-path.h"
 
 namespace Rust {
@@ -46,86 +44,11 @@ DeriveCopy::copy_impl (
 {
   auto copy = builder.type_path (LangItem::Kind::COPY);
 
-  // we need to build up the generics for this impl block which will be just a
-  // clone of the types specified ones
-  //
-  // for example:
-  //
-  // #[derive(Copy)]
-  // struct Be<T> { ... }
-  //
-  // we need to generate the impl block:
-  //
-  // impl<T: Copy> Clone for Be<T>
-
-  std::vector<Lifetime> lifetime_args;
-  std::vector<GenericArg> generic_args;
-  std::vector<std::unique_ptr<GenericParam>> impl_generics;
-  for (const auto &generic : type_generics)
-    {
-      switch (generic->get_kind ())
-       {
-         case GenericParam::Kind::Lifetime: {
-           LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
-
-           Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
-           lifetime_args.push_back (std::move (l));
-
-           auto impl_lifetime_param
-             = builder.new_lifetime_param (lifetime_param);
-           impl_generics.push_back (std::move (impl_lifetime_param));
-         }
-         break;
-
-         case GenericParam::Kind::Type: {
-           TypeParam &type_param = (TypeParam &) *generic.get ();
-
-           std::unique_ptr<Type> associated_type = builder.single_type_path (
-             type_param.get_type_representation ().as_string ());
-
-           GenericArg type_arg
-             = GenericArg::create_type (std::move (associated_type));
-           generic_args.push_back (std::move (type_arg));
-
-           std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
-           extra_bounds.emplace_back (std::unique_ptr<TypeParamBound> (
-             new TraitBound (builder.type_path (LangItem::Kind::COPY), loc)));
-
-           auto impl_type_param
-             = builder.new_type_param (type_param, std::move (extra_bounds));
-           impl_generics.push_back (std::move (impl_type_param));
-         }
-         break;
-
-         case GenericParam::Kind::Const: {
-           rust_unreachable ();
-
-           // TODO
-           // const ConstGenericParam *const_param
-           //   = (const ConstGenericParam *) generic.get ();
-           // std::unique_ptr<Expr> const_expr = nullptr;
-
-           // GenericArg type_arg
-           //   = GenericArg::create_const (std::move (const_expr));
-           // generic_args.push_back (std::move (type_arg));
-         }
-         break;
-       }
-    }
-
-  GenericArgs generic_args_for_self (lifetime_args, generic_args,
-                                    {} /*binding args*/, loc);
-  std::unique_ptr<Type> self_type_path
-    = impl_generics.empty ()
-       ? builder.single_type_path (name)
-       : builder.single_generic_type_path (name, generic_args_for_self);
-
-  return std::unique_ptr<Item> (
-    new TraitImpl (std::move (copy), /* unsafe */ false,
-                  /* exclam */ false, /* trait items */ {},
-                  std::move (impl_generics), std::move (self_type_path),
-                  WhereClause::create_empty (), Visibility::create_private (),
-                  {}, {}, loc));
+  auto generics
+    = setup_impl_generics (name, type_generics, builder.trait_bound (copy));
+
+  return builder.trait_impl (copy, std::move (generics.self_type), {},
+                            std::move (generics.impl));
 }
 
 void