| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-derive.h" |
| 20 |
#include "rust-derive-clone.h" |
| 21 |
#include "rust-derive-copy.h" |
| 22 |
#include "rust-derive-debug.h" |
| 23 |
#include "rust-derive-default.h" |
| 24 |
#include "rust-derive-eq.h" |
| 25 |
#include "rust-derive-partial-eq.h" |
| 26 |
#include "rust-derive-hash.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace AST { |
| 30 |
|
| 31 |
DeriveVisitor::DeriveVisitor (location_t loc) |
| 32 |
: loc (loc), builder (Builder (loc)) |
| 33 |
{} |
| 34 |
|
| 35 |
std::vector<std::unique_ptr<Item>> |
| 36 |
DeriveVisitor::derive (Item &item, const Attribute &attr, |
| 37 |
BuiltinMacro to_derive) |
| 38 |
{ |
| 39 |
auto loc = attr.get_locus (); |
| 40 |
|
| 41 |
switch (to_derive) |
| 42 |
{ |
| 43 |
case BuiltinMacro::Clone: |
| 44 |
return vec (DeriveClone (loc).go (item)); |
| 45 |
case BuiltinMacro::Copy: |
| 46 |
return vec (DeriveCopy (loc).go (item)); |
| 47 |
case BuiltinMacro::Debug: |
| 48 |
rust_warning_at ( |
| 49 |
loc, 0, |
| 50 |
"derive(Debug) is not fully implemented yet and has no effect - only a " |
| 51 |
"stub implementation will be generated"); |
| 52 |
return vec (DeriveDebug (loc).go (item)); |
| 53 |
case BuiltinMacro::Default: |
| 54 |
return vec (DeriveDefault (loc).go (item)); |
| 55 |
case BuiltinMacro::Eq: |
| 56 |
return DeriveEq (loc).go (item); |
| 57 |
case BuiltinMacro::PartialEq: |
| 58 |
return DerivePartialEq (loc).go (item); |
| 59 |
case BuiltinMacro::Hash: |
| 60 |
return vec (DeriveHash (loc).go (item)); |
| 61 |
case BuiltinMacro::Ord: |
| 62 |
case BuiltinMacro::PartialOrd: |
| 63 |
default: |
| 64 |
rust_sorry_at (loc, "unimplemented builtin derive macro"); |
| 65 |
return {}; |
| 66 |
}; |
| 67 |
} |
| 68 |
|
| 69 |
DeriveVisitor::ImplGenerics |
| 70 |
DeriveVisitor::setup_impl_generics ( |
| 71 |
const std::string &type_name, |
| 72 |
const std::vector<std::unique_ptr<GenericParam>> &type_generics, |
| 73 |
tl::optional<std::unique_ptr<TypeParamBound>> &&extra_bound) const |
| 74 |
{ |
| 75 |
std::vector<Lifetime> lifetime_args; |
| 76 |
std::vector<GenericArg> generic_args; |
| 77 |
std::vector<std::unique_ptr<GenericParam>> impl_generics; |
| 78 |
for (const auto &generic : type_generics) |
| 79 |
{ |
| 80 |
switch (generic->get_kind ()) |
| 81 |
{ |
| 82 |
case GenericParam::Kind::Lifetime: { |
| 83 |
LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get (); |
| 84 |
|
| 85 |
Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ()); |
| 86 |
lifetime_args.push_back (std::move (l)); |
| 87 |
|
| 88 |
auto impl_lifetime_param |
| 89 |
= builder.new_lifetime_param (lifetime_param); |
| 90 |
impl_generics.push_back (std::move (impl_lifetime_param)); |
| 91 |
} |
| 92 |
break; |
| 93 |
|
| 94 |
case GenericParam::Kind::Type: { |
| 95 |
TypeParam &type_param = (TypeParam &) *generic.get (); |
| 96 |
|
| 97 |
std::unique_ptr<Type> associated_type = builder.single_type_path ( |
| 98 |
type_param.get_type_representation ().as_string ()); |
| 99 |
|
| 100 |
GenericArg type_arg |
| 101 |
= GenericArg::create_type (std::move (associated_type)); |
| 102 |
generic_args.push_back (std::move (type_arg)); |
| 103 |
|
| 104 |
std::vector<std::unique_ptr<TypeParamBound>> extra_bounds; |
| 105 |
|
| 106 |
if (extra_bound) |
| 107 |
extra_bounds.emplace_back (std::move (*extra_bound)); |
| 108 |
|
| 109 |
auto impl_type_param |
| 110 |
= builder.new_type_param (type_param, std::move (extra_bounds)); |
| 111 |
|
| 112 |
impl_generics.push_back (std::move (impl_type_param)); |
| 113 |
} |
| 114 |
break; |
| 115 |
|
| 116 |
case GenericParam::Kind::Const: { |
| 117 |
rust_unreachable (); |
| 118 |
|
| 119 |
// TODO |
| 120 |
// const ConstGenericParam *const_param |
| 121 |
// = (const ConstGenericParam *) generic.get (); |
| 122 |
// std::unique_ptr<Expr> const_expr = nullptr; |
| 123 |
|
| 124 |
// GenericArg type_arg |
| 125 |
// = GenericArg::create_const (std::move (const_expr)); |
| 126 |
// generic_args.push_back (std::move (type_arg)); |
| 127 |
} |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
auto generic_args_for_self |
| 133 |
= GenericArgs (lifetime_args, generic_args, {} /*binding args*/, loc); |
| 134 |
|
| 135 |
std::unique_ptr<Type> self_type_path |
| 136 |
= impl_generics.empty () |
| 137 |
? builder.single_type_path (type_name) |
| 138 |
: builder.single_generic_type_path (type_name, generic_args_for_self); |
| 139 |
|
| 140 |
return ImplGenerics{std::move (self_type_path), std::move (impl_generics)}; |
| 141 |
} |
| 142 |
|
| 143 |
} // namespace AST |
| 144 |
} // namespace Rust |