Line data Source code
1 : // Copyright (C) 2020-2026 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-ord.h"
26 : #include "rust-derive-partial-eq.h"
27 : #include "rust-derive-hash.h"
28 : #include "rust-system.h"
29 :
30 : namespace Rust {
31 : namespace AST {
32 :
33 290 : DeriveVisitor::DeriveVisitor (location_t loc)
34 290 : : loc (loc), builder (Builder (loc))
35 290 : {}
36 :
37 : std::vector<std::unique_ptr<Item>>
38 291 : DeriveVisitor::derive (Item &item, const Attribute &attr,
39 : BuiltinMacro to_derive)
40 : {
41 291 : auto loc = attr.get_locus ();
42 :
43 291 : using Kind = AST::Item::Kind;
44 291 : auto item_kind = item.get_item_kind ();
45 291 : if (item_kind != Kind::Enum && item_kind != Kind::Struct
46 3 : && item_kind != Kind::Union)
47 : {
48 1 : rust_error_at (loc,
49 : "derive may only be applied to structs, enums and unions");
50 1 : return {};
51 : }
52 :
53 290 : switch (to_derive)
54 : {
55 38 : case BuiltinMacro::Clone:
56 38 : return vec (DeriveClone (loc).go (item));
57 15 : case BuiltinMacro::Copy:
58 15 : return vec (DeriveCopy (loc).go (item));
59 3 : case BuiltinMacro::Debug:
60 3 : rust_warning_at (
61 : loc, 0,
62 : "derive(Debug) is not fully implemented yet and has no effect - only a "
63 : "stub implementation will be generated");
64 3 : return vec (DeriveDebug (loc).go (item));
65 15 : case BuiltinMacro::Default:
66 15 : return vec (DeriveDefault (loc).go (item));
67 47 : case BuiltinMacro::Eq:
68 47 : return DeriveEq (loc).go (item);
69 94 : case BuiltinMacro::PartialEq:
70 94 : return DerivePartialEq (loc).go (item);
71 3 : case BuiltinMacro::Hash:
72 3 : return vec (DeriveHash (loc).go (item));
73 37 : case BuiltinMacro::Ord:
74 37 : return vec (DeriveOrd (DeriveOrd::Ordering::Total, loc).go (item));
75 38 : case BuiltinMacro::PartialOrd:
76 38 : return vec (DeriveOrd (DeriveOrd::Ordering::Partial, loc).go (item));
77 0 : default:
78 0 : rust_unreachable ();
79 : };
80 : }
81 :
82 : DeriveVisitor::ImplGenerics
83 431 : DeriveVisitor::setup_impl_generics (
84 : const std::string &type_name,
85 : const std::vector<std::unique_ptr<GenericParam>> &type_generics,
86 : tl::optional<std::function<std::unique_ptr<TypeParamBound> ()>> &&extra_bound)
87 : const
88 : {
89 431 : std::vector<Lifetime> lifetime_args;
90 431 : std::vector<GenericArg> generic_args;
91 431 : std::vector<std::unique_ptr<GenericParam>> impl_generics;
92 433 : for (const auto &generic : type_generics)
93 : {
94 2 : switch (generic->get_kind ())
95 : {
96 0 : case GenericParam::Kind::Lifetime:
97 0 : {
98 0 : LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
99 :
100 0 : Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
101 0 : lifetime_args.push_back (std::move (l));
102 :
103 0 : auto impl_lifetime_param
104 0 : = builder.new_lifetime_param (lifetime_param);
105 0 : impl_generics.push_back (std::move (impl_lifetime_param));
106 0 : }
107 0 : break;
108 :
109 2 : case GenericParam::Kind::Type:
110 2 : {
111 2 : TypeParam &type_param = (TypeParam &) *generic.get ();
112 :
113 2 : std::unique_ptr<Type> associated_type = builder.single_type_path (
114 4 : type_param.get_type_representation ().as_string ());
115 :
116 2 : GenericArg type_arg
117 2 : = GenericArg::create_type (std::move (associated_type));
118 2 : generic_args.push_back (std::move (type_arg));
119 :
120 2 : std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
121 :
122 2 : if (extra_bound)
123 2 : extra_bounds.emplace_back (extra_bound.value () ());
124 :
125 2 : auto impl_type_param
126 2 : = builder.new_type_param (type_param, std::move (extra_bounds));
127 :
128 2 : impl_generics.push_back (std::move (impl_type_param));
129 2 : }
130 2 : break;
131 :
132 0 : case GenericParam::Kind::Const:
133 0 : {
134 0 : ConstGenericParam &const_param
135 0 : = (ConstGenericParam &) *generic.get ();
136 :
137 0 : std::unique_ptr<Type> associated_type
138 0 : = builder.single_type_path (const_param.get_name ().as_string ());
139 :
140 0 : GenericArg type_arg
141 0 : = GenericArg::create_type (std::move (associated_type));
142 0 : generic_args.push_back (std::move (type_arg));
143 :
144 0 : auto impl_const_param = builder.new_const_param (const_param);
145 0 : impl_generics.push_back (std::move (impl_const_param));
146 0 : }
147 0 : break;
148 : }
149 : }
150 :
151 431 : auto generic_args_for_self
152 431 : = GenericArgs (lifetime_args, generic_args, {} /*binding args*/, loc);
153 :
154 431 : std::unique_ptr<Type> self_type_path
155 431 : = impl_generics.empty ()
156 860 : ? builder.single_type_path (type_name)
157 1293 : : builder.single_generic_type_path (type_name, generic_args_for_self);
158 :
159 431 : return ImplGenerics{std::move (self_type_path), std::move (impl_generics)};
160 431 : }
161 :
162 : } // namespace AST
163 : } // namespace Rust
|