Line data Source code
1 : // Copyright (C) 2025-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-eq.h"
20 : #include "rust-ast.h"
21 : #include "rust-expr.h"
22 : #include "rust-item.h"
23 : #include "rust-path.h"
24 : #include "rust-pattern.h"
25 : #include "rust-system.h"
26 :
27 : namespace Rust {
28 : namespace AST {
29 :
30 : static TypePath
31 1737 : get_eq_trait_path (Builder &builder)
32 : {
33 8685 : return builder.type_path ({builder.get_path_start (), "cmp", "Eq"},
34 3474 : builder.has_opening_scope ());
35 : }
36 :
37 771 : DeriveEq::DeriveEq (location_t loc, Builder::Source item_source)
38 771 : : DeriveVisitor (loc, item_source)
39 771 : {}
40 :
41 : std::vector<std::unique_ptr<AST::Item>>
42 771 : DeriveEq::go (Item &item)
43 : {
44 771 : item.accept_vis (*this);
45 :
46 771 : return std::move (expanded);
47 : }
48 :
49 : std::unique_ptr<AssociatedItem>
50 771 : DeriveEq::assert_receiver_is_total_eq_fn (
51 : std::vector<std::unique_ptr<Type>> &&types)
52 : {
53 771 : auto stmts = std::vector<std::unique_ptr<Stmt>> ();
54 :
55 771 : stmts.emplace_back (assert_param_is_eq ());
56 :
57 1750 : for (auto &&type : types)
58 979 : stmts.emplace_back (assert_type_is_eq (std::move (type)));
59 :
60 771 : auto block = std::unique_ptr<BlockExpr> (
61 771 : new BlockExpr (std::move (stmts), nullptr, {}, {}, tl::nullopt, loc, loc));
62 :
63 771 : auto self = builder.self_ref_param ();
64 :
65 3084 : return builder.function ("assert_receiver_is_total_eq",
66 2313 : vec (std::move (self)), {}, std::move (block));
67 771 : }
68 :
69 : std::unique_ptr<Stmt>
70 771 : DeriveEq::assert_param_is_eq ()
71 : {
72 771 : auto eq_bound = std::unique_ptr<TypeParamBound> (
73 771 : new TraitBound (get_eq_trait_path (builder), loc));
74 :
75 771 : auto sized_bound = std::unique_ptr<TypeParamBound> (
76 1542 : new TraitBound (builder.type_path (LangItem::Kind::SIZED), loc, false,
77 771 : true /* opening_question_mark */));
78 :
79 771 : auto bounds = vec (std::move (eq_bound), std::move (sized_bound));
80 :
81 771 : auto assert_param_is_eq = "AssertParamIsEq";
82 :
83 771 : auto t = std::unique_ptr<GenericParam> (
84 1542 : new TypeParam (Identifier ("T"), loc, std::move (bounds)));
85 :
86 2313 : return builder.struct_struct (
87 1542 : assert_param_is_eq, vec (std::move (t)),
88 : {StructField (
89 2313 : Identifier ("_t"),
90 1542 : builder.single_generic_type_path (
91 : LangItem::Kind::PHANTOM_DATA,
92 771 : GenericArgs (
93 2313 : {}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
94 2313 : Visibility::create_private (), loc)});
95 771 : }
96 :
97 : std::unique_ptr<Stmt>
98 979 : DeriveEq::assert_type_is_eq (std::unique_ptr<Type> &&type)
99 : {
100 979 : auto assert_param_is_eq = "AssertParamIsEq";
101 :
102 : // AssertParamIsCopy::<Self>
103 979 : auto assert_param_is_eq_ty
104 : = std::unique_ptr<TypePathSegment> (new TypePathSegmentGeneric (
105 1958 : PathIdentSegment (assert_param_is_eq, loc), false,
106 2937 : GenericArgs ({}, {GenericArg::create_type (std::move (type))}, {}, loc),
107 1958 : loc));
108 :
109 : // TODO: Improve this, it's really ugly
110 979 : auto type_paths = std::vector<std::unique_ptr<TypePathSegment>> ();
111 979 : type_paths.emplace_back (std::move (assert_param_is_eq_ty));
112 :
113 979 : auto full_path
114 979 : = std::unique_ptr<Type> (new TypePath ({std::move (type_paths)}, loc));
115 :
116 979 : return builder.let (builder.wildcard (), std::move (full_path));
117 979 : }
118 :
119 : std::vector<std::unique_ptr<Item>>
120 771 : DeriveEq::eq_impls (
121 : std::unique_ptr<AssociatedItem> &&fn, std::string name,
122 : const std::vector<std::unique_ptr<GenericParam>> &type_generics)
123 : {
124 771 : auto eq = [this] () { return get_eq_trait_path (builder); };
125 195 : auto eq_bound = [&, this] () { return builder.trait_bound (eq ()); };
126 :
127 771 : auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ);
128 :
129 771 : auto trait_items = vec (std::move (fn));
130 :
131 771 : auto eq_generics = setup_impl_generics (name, type_generics, eq_bound);
132 771 : auto steq_generics = setup_impl_generics (name, type_generics);
133 :
134 1542 : auto eq_impl = builder.trait_impl (eq (), std::move (eq_generics.self_type),
135 : std::move (trait_items),
136 771 : std::move (eq_generics.impl));
137 :
138 : // StructuralEq is a marker trait
139 771 : decltype (trait_items) steq_trait_items = {};
140 :
141 771 : auto steq_impl
142 1542 : = builder.trait_impl (steq, std::move (steq_generics.self_type),
143 : std::move (steq_trait_items),
144 771 : std::move (steq_generics.impl));
145 :
146 771 : return vec (std::move (eq_impl), std::move (steq_impl));
147 2313 : }
148 :
149 : void
150 194 : DeriveEq::visit_tuple (TupleStruct &item)
151 : {
152 194 : auto types = std::vector<std::unique_ptr<Type>> ();
153 :
154 388 : for (auto &field : item.get_fields ())
155 194 : types.emplace_back (field.get_field_type ().reconstruct ());
156 :
157 388 : expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
158 194 : item.get_identifier ().as_string (),
159 388 : item.get_generic_params ());
160 194 : }
161 :
162 : void
163 369 : DeriveEq::visit_struct (StructStruct &item)
164 : {
165 369 : auto types = std::vector<std::unique_ptr<Type>> ();
166 :
167 929 : for (auto &field : item.get_fields ())
168 560 : types.emplace_back (field.get_field_type ().reconstruct ());
169 :
170 738 : expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
171 369 : item.get_identifier ().as_string (),
172 738 : item.get_generic_params ());
173 369 : }
174 :
175 : void
176 208 : DeriveEq::visit_enum (Enum &item)
177 : {
178 208 : auto types = std::vector<std::unique_ptr<Type>> ();
179 :
180 871 : for (auto &variant : item.get_variants ())
181 : {
182 663 : switch (variant->get_enum_item_kind ())
183 : {
184 465 : case EnumItem::Kind::Identifier:
185 465 : case EnumItem::Kind::Discriminant:
186 : // nothing to do as they contain no inner types
187 465 : continue;
188 197 : case EnumItem::Kind::Tuple:
189 197 : {
190 197 : auto &tuple = static_cast<EnumItemTuple &> (*variant);
191 :
192 420 : for (auto &field : tuple.get_tuple_fields ())
193 223 : types.emplace_back (field.get_field_type ().reconstruct ());
194 : break;
195 : }
196 1 : case EnumItem::Kind::Struct:
197 1 : {
198 1 : auto &tuple = static_cast<EnumItemStruct &> (*variant);
199 :
200 3 : for (auto &field : tuple.get_struct_fields ())
201 2 : types.emplace_back (field.get_field_type ().reconstruct ());
202 :
203 : break;
204 : }
205 465 : }
206 : }
207 :
208 624 : expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
209 208 : item.get_identifier ().as_string (),
210 416 : item.get_generic_params ());
211 208 : }
212 :
213 : void
214 0 : DeriveEq::visit_union (Union &item)
215 : {
216 0 : auto types = std::vector<std::unique_ptr<Type>> ();
217 :
218 0 : for (auto &field : item.get_variants ())
219 0 : types.emplace_back (field.get_field_type ().reconstruct ());
220 :
221 0 : expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
222 0 : item.get_identifier ().as_string (),
223 0 : item.get_generic_params ());
224 0 : }
225 :
226 : } // namespace AST
227 : } // namespace Rust
|