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 293 : DeriveVisitor::derive (Item &item, const Attribute &attr,
39 : BuiltinMacro to_derive)
40 : {
41 293 : auto loc = attr.get_locus ();
42 :
43 293 : using Kind = AST::Item::Kind;
44 293 : auto item_kind = item.get_item_kind ();
45 293 : 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 292 : 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 2 : case BuiltinMacro::RustcEncodable:
78 2 : case BuiltinMacro::RustcDecodable:
79 3 : rust_sorry_at (loc, "derive(%s) is not yet implemented",
80 : to_derive == BuiltinMacro::RustcEncodable
81 : ? "RustcEncodable"
82 : : "RustcDecodable");
83 2 : return {};
84 0 : default:
85 0 : rust_unreachable ();
86 : };
87 : }
88 :
89 : DeriveVisitor::ImplGenerics
90 431 : DeriveVisitor::setup_impl_generics (
91 : const std::string &type_name,
92 : const std::vector<std::unique_ptr<GenericParam>> &type_generics,
93 : tl::optional<std::function<std::unique_ptr<TypeParamBound> ()>> &&extra_bound)
94 : const
95 : {
96 431 : std::vector<Lifetime> lifetime_args;
97 431 : std::vector<GenericArg> generic_args;
98 431 : std::vector<std::unique_ptr<GenericParam>> impl_generics;
99 433 : for (const auto &generic : type_generics)
100 : {
101 2 : switch (generic->get_kind ())
102 : {
103 0 : case GenericParam::Kind::Lifetime:
104 0 : {
105 0 : LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
106 :
107 0 : Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
108 0 : lifetime_args.push_back (std::move (l));
109 :
110 0 : auto impl_lifetime_param
111 0 : = builder.new_lifetime_param (lifetime_param);
112 0 : impl_generics.push_back (std::move (impl_lifetime_param));
113 0 : }
114 0 : break;
115 :
116 2 : case GenericParam::Kind::Type:
117 2 : {
118 2 : TypeParam &type_param = (TypeParam &) *generic.get ();
119 :
120 2 : std::unique_ptr<Type> associated_type = builder.single_type_path (
121 4 : type_param.get_type_representation ().as_string ());
122 :
123 2 : GenericArg type_arg
124 2 : = GenericArg::create_type (std::move (associated_type));
125 2 : generic_args.push_back (std::move (type_arg));
126 :
127 2 : std::vector<std::unique_ptr<TypeParamBound>> extra_bounds;
128 :
129 2 : if (extra_bound)
130 2 : extra_bounds.emplace_back (extra_bound.value () ());
131 :
132 2 : auto impl_type_param
133 2 : = builder.new_type_param (type_param, std::move (extra_bounds));
134 :
135 2 : impl_generics.push_back (std::move (impl_type_param));
136 2 : }
137 2 : break;
138 :
139 0 : case GenericParam::Kind::Const:
140 0 : {
141 0 : ConstGenericParam &const_param
142 0 : = (ConstGenericParam &) *generic.get ();
143 :
144 0 : std::unique_ptr<Type> associated_type
145 0 : = builder.single_type_path (const_param.get_name ().as_string ());
146 :
147 0 : GenericArg type_arg
148 0 : = GenericArg::create_type (std::move (associated_type));
149 0 : generic_args.push_back (std::move (type_arg));
150 :
151 0 : auto impl_const_param = builder.new_const_param (const_param);
152 0 : impl_generics.push_back (std::move (impl_const_param));
153 0 : }
154 0 : break;
155 : }
156 : }
157 :
158 431 : auto generic_args_for_self
159 431 : = GenericArgs (lifetime_args, generic_args, {} /*binding args*/, loc);
160 :
161 431 : std::unique_ptr<Type> self_type_path
162 431 : = impl_generics.empty ()
163 860 : ? builder.single_type_path (type_name)
164 1293 : : builder.single_generic_type_path (type_name, generic_args_for_self);
165 :
166 431 : return ImplGenerics{std::move (self_type_path), std::move (impl_generics)};
167 431 : }
168 :
169 : } // namespace AST
170 : } // namespace Rust
|