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-cmp-common.h"
20 : #include "rust-ast-builder.h"
21 : #include "rust-item.h"
22 :
23 : namespace Rust {
24 : namespace AST {
25 :
26 : SelfOther
27 9 : SelfOther::index (Builder builder, int idx)
28 : {
29 9 : return SelfOther{
30 9 : builder.tuple_idx ("self", idx),
31 18 : builder.tuple_idx ("other", idx),
32 9 : };
33 : }
34 :
35 : std::vector<SelfOther>
36 9 : SelfOther::indexes (Builder builder, const std::vector<TupleField> &fields)
37 : {
38 9 : std::vector<SelfOther> vec;
39 :
40 18 : for (size_t i = 0; i < fields.size (); i++)
41 18 : vec.emplace_back (SelfOther::index (builder, i));
42 :
43 9 : return vec;
44 : }
45 :
46 : SelfOther
47 200 : SelfOther::field (Builder builder, const std::string &field_name)
48 : {
49 200 : return SelfOther{
50 400 : builder.field_access (builder.identifier ("self"), field_name),
51 400 : builder.field_access (builder.identifier ("other"), field_name),
52 400 : };
53 : }
54 :
55 : std::vector<SelfOther>
56 116 : SelfOther::fields (Builder builder, const std::vector<StructField> &fields)
57 : {
58 116 : std::vector<SelfOther> vec;
59 :
60 316 : for (const auto &field : fields)
61 400 : vec.emplace_back (
62 400 : SelfOther::field (builder, field.get_field_name ().as_string ()));
63 :
64 116 : return vec;
65 : }
66 :
67 : MatchCase
68 44 : EnumMatchBuilder::tuple (EnumItem &variant_raw)
69 : {
70 44 : auto &variant = static_cast<EnumItemTuple &> (variant_raw);
71 :
72 44 : auto self_patterns = std::vector<std::unique_ptr<Pattern>> ();
73 44 : auto other_patterns = std::vector<std::unique_ptr<Pattern>> ();
74 :
75 44 : auto self_other_exprs = std::vector<SelfOther> ();
76 :
77 127 : for (size_t i = 0; i < variant.get_tuple_fields ().size (); i++)
78 : {
79 : // The patterns we're creating for each field are `self_<i>` and
80 : // `other_<i>` where `i` is the index of the field. It doesn't actually
81 : // matter what we use, as long as it's ordered, unique, and that we can
82 : // reuse it in the match case's return expression to check that they are
83 : // equal.
84 :
85 83 : auto self_pattern_str = "__self_" + std::to_string (i);
86 83 : auto other_pattern_str = "__other_" + std::to_string (i);
87 :
88 83 : self_patterns.emplace_back (
89 249 : builder.identifier_pattern (self_pattern_str));
90 83 : other_patterns.emplace_back (
91 249 : builder.identifier_pattern (other_pattern_str));
92 :
93 166 : self_other_exprs.emplace_back (SelfOther{
94 249 : builder.identifier (self_pattern_str),
95 249 : builder.identifier (other_pattern_str),
96 : });
97 83 : }
98 :
99 : // TODO: Replace with `reconstruct()` instead of building these twice
100 44 : auto self_variant_path = builder.variant_path (enum_path, variant_path);
101 44 : auto other_variant_path = builder.variant_path (enum_path, variant_path);
102 :
103 44 : auto self_pattern_items = std::unique_ptr<TupleStructItems> (
104 44 : new TupleStructItemsNoRest (std::move (self_patterns)));
105 44 : auto other_pattern_items = std::unique_ptr<TupleStructItems> (
106 44 : new TupleStructItemsNoRest (std::move (other_patterns)));
107 :
108 44 : auto self_pattern = std::unique_ptr<Pattern> (
109 44 : new ReferencePattern (std::unique_ptr<Pattern> (new TupleStructPattern (
110 88 : self_variant_path, std::move (self_pattern_items))),
111 88 : false, false, builder.loc));
112 44 : auto other_pattern = std::unique_ptr<Pattern> (new ReferencePattern (
113 44 : std::unique_ptr<Pattern> (new TupleStructPattern (
114 88 : other_variant_path, std::move (other_pattern_items))),
115 88 : false, false, builder.loc));
116 :
117 44 : auto tuple_items = std::make_unique<TuplePatternItemsNoRest> (
118 44 : vec (std::move (self_pattern), std::move (other_pattern)));
119 :
120 44 : auto pattern
121 44 : = std::make_unique<TuplePattern> (std::move (tuple_items), builder.loc);
122 :
123 44 : auto expr = fn (std::move (self_other_exprs));
124 :
125 44 : return builder.match_case (std::move (pattern), std::move (expr));
126 44 : }
127 :
128 : MatchCase
129 23 : EnumMatchBuilder::strukt (EnumItem &variant_raw)
130 : {
131 23 : auto &variant = static_cast<EnumItemStruct &> (variant_raw);
132 :
133 23 : auto self_fields = std::vector<std::unique_ptr<StructPatternField>> ();
134 23 : auto other_fields = std::vector<std::unique_ptr<StructPatternField>> ();
135 :
136 23 : auto self_other_exprs = std::vector<SelfOther> ();
137 :
138 69 : for (auto &field : variant.get_struct_fields ())
139 : {
140 : // The patterns we're creating for each field are `self_<field>` and
141 : // `other_<field>` where `field` is the name of the field. It doesn't
142 : // actually matter what we use, as long as it's ordered, unique, and that
143 : // we can reuse it in the match case's return expression to check that
144 : // they are equal.
145 :
146 92 : auto field_name = field.get_field_name ().as_string ();
147 :
148 46 : auto self_pattern_str = "__self_" + field_name;
149 46 : auto other_pattern_str = "__other_" + field_name;
150 :
151 46 : self_fields.emplace_back (builder.struct_pattern_ident_pattern (
152 138 : field_name, builder.identifier_pattern (self_pattern_str)));
153 46 : other_fields.emplace_back (builder.struct_pattern_ident_pattern (
154 138 : field_name, builder.identifier_pattern (other_pattern_str)));
155 :
156 92 : self_other_exprs.emplace_back (SelfOther{
157 138 : builder.identifier (self_pattern_str),
158 138 : builder.identifier (other_pattern_str),
159 : });
160 46 : }
161 :
162 : // TODO: Replace with `reconstruct()` instead of building these twice
163 23 : auto self_variant_path = builder.variant_path (enum_path, variant_path);
164 23 : auto other_variant_path = builder.variant_path (enum_path, variant_path);
165 :
166 23 : auto self_elts = StructPatternElements (std::move (self_fields));
167 23 : auto other_elts = StructPatternElements (std::move (other_fields));
168 :
169 23 : auto self_pattern = std::unique_ptr<Pattern> (new ReferencePattern (
170 23 : std::unique_ptr<Pattern> (new StructPattern (self_variant_path, builder.loc,
171 46 : std::move (self_elts))),
172 46 : false, false, builder.loc));
173 23 : auto other_pattern = std::unique_ptr<Pattern> (
174 23 : new ReferencePattern (std::unique_ptr<Pattern> (
175 : new StructPattern (other_variant_path, builder.loc,
176 46 : std::move (other_elts))),
177 46 : false, false, builder.loc));
178 :
179 23 : auto tuple_items = std::make_unique<TuplePatternItemsNoRest> (
180 23 : vec (std::move (self_pattern), std::move (other_pattern)));
181 :
182 23 : auto pattern
183 23 : = std::make_unique<TuplePattern> (std::move (tuple_items), builder.loc);
184 :
185 23 : auto expr = fn (std::move (self_other_exprs));
186 :
187 23 : return builder.match_case (std::move (pattern), std::move (expr));
188 69 : }
189 :
190 : } // namespace AST
191 : } // namespace Rust
|