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 44 : auto self_variant_path = builder.variant_path (enum_path, variant_path);
100 44 : auto other_variant_path = self_variant_path.reconstruct ();
101 :
102 44 : auto self_pattern_items = std::unique_ptr<TupleStructItems> (
103 44 : new TupleStructItemsNoRest (std::move (self_patterns)));
104 44 : auto other_pattern_items = std::unique_ptr<TupleStructItems> (
105 44 : new TupleStructItemsNoRest (std::move (other_patterns)));
106 :
107 44 : auto self_pattern = std::unique_ptr<Pattern> (
108 44 : new ReferencePattern (std::unique_ptr<Pattern> (new TupleStructPattern (
109 88 : self_variant_path, std::move (self_pattern_items))),
110 88 : false, false, builder.loc));
111 44 : auto other_pattern = std::unique_ptr<Pattern> (new ReferencePattern (
112 44 : std::unique_ptr<Pattern> (new TupleStructPattern (
113 88 : *other_variant_path, std::move (other_pattern_items))),
114 88 : false, false, builder.loc));
115 :
116 44 : auto tuple_items = std::make_unique<TuplePatternItemsNoRest> (
117 44 : vec (std::move (self_pattern), std::move (other_pattern)));
118 :
119 44 : auto pattern
120 44 : = std::make_unique<TuplePattern> (std::move (tuple_items), builder.loc);
121 :
122 44 : auto expr = fn (std::move (self_other_exprs));
123 :
124 44 : return builder.match_case (std::move (pattern), std::move (expr));
125 44 : }
126 :
127 : MatchCase
128 23 : EnumMatchBuilder::strukt (EnumItem &variant_raw)
129 : {
130 23 : auto &variant = static_cast<EnumItemStruct &> (variant_raw);
131 :
132 23 : auto self_fields = std::vector<std::unique_ptr<StructPatternField>> ();
133 23 : auto other_fields = std::vector<std::unique_ptr<StructPatternField>> ();
134 :
135 23 : auto self_other_exprs = std::vector<SelfOther> ();
136 :
137 69 : for (auto &field : variant.get_struct_fields ())
138 : {
139 : // The patterns we're creating for each field are `self_<field>` and
140 : // `other_<field>` where `field` is the name of the field. It doesn't
141 : // actually matter what we use, as long as it's ordered, unique, and that
142 : // we can reuse it in the match case's return expression to check that
143 : // they are equal.
144 :
145 92 : auto field_name = field.get_field_name ().as_string ();
146 :
147 46 : auto self_pattern_str = "__self_" + field_name;
148 46 : auto other_pattern_str = "__other_" + field_name;
149 :
150 46 : self_fields.emplace_back (builder.struct_pattern_ident_pattern (
151 138 : field_name, builder.identifier_pattern (self_pattern_str)));
152 46 : other_fields.emplace_back (builder.struct_pattern_ident_pattern (
153 138 : field_name, builder.identifier_pattern (other_pattern_str)));
154 :
155 92 : self_other_exprs.emplace_back (SelfOther{
156 138 : builder.identifier (self_pattern_str),
157 138 : builder.identifier (other_pattern_str),
158 : });
159 46 : }
160 :
161 23 : auto self_variant_path = builder.variant_path (enum_path, variant_path);
162 23 : auto other_variant_path = self_variant_path.reconstruct ();
163 :
164 23 : auto self_elts = StructPatternElements (std::move (self_fields));
165 23 : auto other_elts = StructPatternElements (std::move (other_fields));
166 :
167 23 : auto self_pattern = std::unique_ptr<Pattern> (new ReferencePattern (
168 23 : std::unique_ptr<Pattern> (new StructPattern (self_variant_path, builder.loc,
169 46 : std::move (self_elts))),
170 46 : false, false, builder.loc));
171 23 : auto other_pattern = std::unique_ptr<Pattern> (
172 23 : new ReferencePattern (std::unique_ptr<Pattern> (
173 23 : new StructPattern (*other_variant_path, builder.loc,
174 46 : std::move (other_elts))),
175 46 : false, false, builder.loc));
176 :
177 23 : auto tuple_items = std::make_unique<TuplePatternItemsNoRest> (
178 23 : vec (std::move (self_pattern), std::move (other_pattern)));
179 :
180 23 : auto pattern
181 23 : = std::make_unique<TuplePattern> (std::move (tuple_items), builder.loc);
182 :
183 23 : auto expr = fn (std::move (self_other_exprs));
184 :
185 23 : return builder.match_case (std::move (pattern), std::move (expr));
186 69 : }
187 :
188 : } // namespace AST
189 : } // namespace Rust
|