From: Arthur Cohen <arthur.co...@embecosm.com>

gcc/rust/ChangeLog:

        * ast/rust-ast-builder.cc (Builder::new_const_param): New function.
        * ast/rust-ast-builder.h (vec): New function for creating 3 elts vector.
        * expand/rust-derive.cc: Use the new_const_param builder.
        * ast/rust-path.h: Add get_default_value() method.
---
 gcc/rust/ast/rust-ast-builder.cc | 10 ++++++++++
 gcc/rust/ast/rust-ast-builder.h  | 16 ++++++++++++++++
 gcc/rust/ast/rust-path.h         |  7 +++++++
 gcc/rust/expand/rust-derive.cc   | 30 +++++++++++++++---------------
 4 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index fbc8f273d81..7e017d53629 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -554,6 +554,16 @@ Builder::new_lifetime_param (LifetimeParam &param)
   return std::unique_ptr<GenericParam> (p);
 }
 
+std::unique_ptr<GenericParam>
+Builder::new_const_param (ConstGenericParam &param) const
+{
+  return std::make_unique<ConstGenericParam> (param.get_name (),
+                                             param.get_type ().clone_type (),
+                                             param.get_default_value (),
+                                             param.get_outer_attrs (),
+                                             param.get_locus ());
+}
+
 std::unique_ptr<GenericParam>
 Builder::new_type_param (
   TypeParam &param, std::vector<std::unique_ptr<TypeParamBound>> extra_bounds)
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index a5115b685ac..d1b6529599f 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -51,6 +51,19 @@ vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2)
   return v;
 }
 
+template <typename T>
+std::vector<std::unique_ptr<T>>
+vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2, std::unique_ptr<T> &&t3)
+{
+  auto v = std::vector<std::unique_ptr<T>> ();
+
+  v.emplace_back (std::move (t1));
+  v.emplace_back (std::move (t2));
+  v.emplace_back (std::move (t3));
+
+  return v;
+}
+
 /* Pointer-ify something */
 template <typename T>
 static std::unique_ptr<T>
@@ -294,6 +307,9 @@ public:
   static std::unique_ptr<GenericParam>
   new_lifetime_param (LifetimeParam &param);
 
+  std::unique_ptr<GenericParam>
+  new_const_param (ConstGenericParam &param) const;
+
   static std::unique_ptr<GenericParam> new_type_param (
     TypeParam &param,
     std::vector<std::unique_ptr<TypeParamBound>> extra_trait_bounds = {});
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index 11f72480fe0..de895a2f838 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -391,6 +391,13 @@ public:
     return default_value.value ();
   }
 
+  tl::optional<GenericArg> &get_default_value () { return default_value; }
+
+  const tl::optional<GenericArg> &get_default_value () const
+  {
+    return default_value;
+  }
+
   std::string as_string () const override;
 
   void accept_vis (ASTVisitor &vis) override;
diff --git a/gcc/rust/expand/rust-derive.cc b/gcc/rust/expand/rust-derive.cc
index 69081db29ad..8b4327ac365 100644
--- a/gcc/rust/expand/rust-derive.cc
+++ b/gcc/rust/expand/rust-derive.cc
@@ -81,8 +81,7 @@ DeriveVisitor::setup_impl_generics (
     {
       switch (generic->get_kind ())
        {
-       case GenericParam::Kind::Lifetime:
-         {
+         case GenericParam::Kind::Lifetime: {
            LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
 
            Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
@@ -94,8 +93,7 @@ DeriveVisitor::setup_impl_generics (
          }
          break;
 
-       case GenericParam::Kind::Type:
-         {
+         case GenericParam::Kind::Type: {
            TypeParam &type_param = (TypeParam &) *generic.get ();
 
            std::unique_ptr<Type> associated_type = builder.single_type_path (
@@ -108,7 +106,8 @@ DeriveVisitor::setup_impl_generics (
            std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
 
            if (extra_bound)
-             extra_bounds.emplace_back (std::move (*extra_bound));
+             extra_bounds.emplace_back (
+               extra_bound.value ()->clone_type_param_bound ());
 
            auto impl_type_param
              = builder.new_type_param (type_param, std::move (extra_bounds));
@@ -117,18 +116,19 @@ DeriveVisitor::setup_impl_generics (
          }
          break;
 
-       case GenericParam::Kind::Const:
-         {
-           rust_unreachable ();
+         case GenericParam::Kind::Const: {
+           ConstGenericParam &const_param
+             = (ConstGenericParam &) *generic.get ();
 
-           // TODO
-           // const ConstGenericParam *const_param
-           //   = (const ConstGenericParam *) generic.get ();
-           // std::unique_ptr<Expr> const_expr = nullptr;
+           std::unique_ptr<Type> associated_type
+             = builder.single_type_path (const_param.get_name ().as_string ());
 
-           // GenericArg type_arg
-           //   = GenericArg::create_const (std::move (const_expr));
-           // generic_args.push_back (std::move (type_arg));
+           GenericArg type_arg
+             = GenericArg::create_type (std::move (associated_type));
+           generic_args.push_back (std::move (type_arg));
+
+           auto impl_const_param = builder.new_const_param (const_param);
+           impl_generics.push_back (std::move (impl_const_param));
          }
          break;
        }
-- 
2.49.0

Reply via email to