diff options
Diffstat (limited to 'gcc/rust/expand/rust-derive-copy.cc')
-rw-r--r-- | gcc/rust/expand/rust-derive-copy.cc | 93 |
1 files changed, 12 insertions, 81 deletions
diff --git a/gcc/rust/expand/rust-derive-copy.cc b/gcc/rust/expand/rust-derive-copy.cc index 1de7290..b2971ad 100644 --- a/gcc/rust/expand/rust-derive-copy.cc +++ b/gcc/rust/expand/rust-derive-copy.cc @@ -17,7 +17,8 @@ // <http://www.gnu.org/licenses/>. #include "rust-derive-copy.h" -#include "rust-ast-full.h" +#include "rust-hir-map.h" +#include "rust-path.h" namespace Rust { namespace AST { @@ -41,86 +42,16 @@ DeriveCopy::copy_impl ( std::string name, const std::vector<std::unique_ptr<GenericParam>> &type_generics) { - // `$crate::core::marker::Copy` instead - auto segments = std::vector<std::unique_ptr<TypePathSegment>> (); - segments.emplace_back (builder.type_path_segment ("Copy")); - auto copy = TypePath (std::move (segments), loc); - - // 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: Copy> { ... } - // - // 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)); - - auto impl_type_param = builder.new_type_param (type_param); - 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 (copy, /* unsafe */ false, - /* exclam */ false, /* trait items */ {}, - std::move (impl_generics), std::move (self_type_path), - WhereClause::create_empty (), Visibility::create_private (), - {}, {}, loc)); + // we should have two of these, so we don't run into issues with + // two paths sharing a node id + auto copy_bound = builder.type_path (LangItem::Kind::COPY); + auto copy_trait_path = builder.type_path (LangItem::Kind::COPY); + + auto generics = setup_impl_generics (name, type_generics, + builder.trait_bound (copy_bound)); + + return builder.trait_impl (copy_trait_path, std::move (generics.self_type), + {}, std::move (generics.impl)); } void |