Debian Codegraph @

gcc/rust/hir/rust-ast-lower-implitem.cc

raw
1 // Copyright (C) 2020-2025 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-ast-lower-implitem.h"
20 #include "rust-ast-lower.h"
21 #include "rust-ast-lower-type.h"
22 #include "rust-ast-lower-expr.h"
23 #include "rust-ast-lower-pattern.h"
24 #include "rust-ast-lower-block.h"
25 #include "rust-hir-item.h"
26 #include "rust-item.h"
27
28 namespace Rust {
29 namespace HIR {
30
31 HIR::ImplItem *
32 ASTLowerImplItem::translate (AST::AssociatedItem &item, HirId parent_impl_id)
33 {
34 ASTLowerImplItem resolver;
35 item.accept_vis (resolver);
36
37 if (resolver.translated != nullptr)
38 {
39 rust_assert (resolver.item_cast != nullptr);
40
41 auto id = resolver.translated->get_impl_mappings ().get_hirid ();
42 auto defid = resolver.translated->get_impl_mappings ().get_defid ();
43 auto locus = resolver.translated->get_locus ();
44
45 resolver.handle_outer_attributes (*resolver.item_cast);
46 resolver.mappings.insert_hir_implitem (parent_impl_id,
47 resolver.translated);
48 resolver.mappings.insert_location (id, locus);
49 resolver.mappings.insert_defid_mapping (defid, resolver.item_cast);
50 }
51
52 return resolver.translated;
53 }
54
55 void
56 ASTLowerImplItem::visit (AST::TypeAlias &alias)
57 {
58 std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
59 HIR::WhereClause where_clause (std::move (where_clause_items));
60 HIR::Visibility vis = translate_visibility (alias.get_visibility ());
61
62 std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
63 if (alias.has_generics ())
64 generic_params = lower_generic_params (alias.get_generic_params ());
65
66 HIR::Type *existing_type
67 = ASTLoweringType::translate (alias.get_type_aliased ());
68
69 auto crate_num = mappings.get_current_crate ();
70 Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
71 mappings.get_next_hir_id (crate_num),
72 mappings.get_next_localdef_id (crate_num));
73
74 auto type_alias
75 = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
76 std::move (generic_params), std::move (where_clause),
77 std::unique_ptr<HIR::Type> (existing_type),
78 std::move (vis), alias.get_outer_attrs (),
79 alias.get_locus ());
80
81 translated = type_alias;
82 item_cast = type_alias;
83 }
84
85 void
86 ASTLowerImplItem::visit (AST::ConstantItem &constant)
87 {
88 HIR::Visibility vis = translate_visibility (constant.get_visibility ());
89
90 HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
91 HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ());
92
93 auto crate_num = mappings.get_current_crate ();
94 Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
95 mappings.get_next_hir_id (crate_num),
96 mappings.get_next_localdef_id (crate_num));
97
98 auto translated_constant
99 = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
100 std::unique_ptr<HIR::Type> (type),
101 std::unique_ptr<HIR::Expr> (expr),
102 constant.get_outer_attrs (),
103 constant.get_locus ());
104
105 translated = translated_constant;
106 item_cast = translated_constant;
107 }
108
109 void
110 ASTLowerImplItem::visit (AST::Function &function)
111 {
112 // ignore for now and leave empty
113 std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
114 for (auto &item : function.get_where_clause ().get_items ())
115 {
116 HIR::WhereClauseItem *i
117 = ASTLowerWhereClauseItem::translate (*item.get ());
118 where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
119 }
120
121 HIR::WhereClause where_clause (std::move (where_clause_items));
122 HIR::FunctionQualifiers qualifiers
123 = lower_qualifiers (function.get_qualifiers ());
124 HIR::Visibility vis = translate_visibility (function.get_visibility ());
125
126 // need
127 std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
128 if (function.has_generics ())
129 {
130 generic_params = lower_generic_params (function.get_generic_params ());
131 }
132 Identifier function_name = function.get_function_name ();
133 location_t locus = function.get_locus ();
134
135 tl::optional<HIR::SelfParam> self_param = tl::nullopt;
136 if (function.has_self_param ())
137 self_param = lower_self (function.get_self_param ());
138
139 std::unique_ptr<HIR::Type> return_type
140 = function.has_return_type () ? std::unique_ptr<HIR::Type> (
141 ASTLoweringType::translate (function.get_return_type ()))
142 : nullptr;
143
144 Defaultness defaultness
145 = function.is_default () ? Defaultness::Default : Defaultness::Final;
146
147 std::vector<HIR::FunctionParam> function_params;
148 for (auto &p : function.get_function_params ())
149 {
150 if (p->is_self () || p->is_variadic ())
151 continue;
152 auto param = static_cast<AST::FunctionParam &> (*p);
153
154 auto translated_pattern = std::unique_ptr<HIR::Pattern> (
155 ASTLoweringPattern::translate (param.get_pattern ()));
156 auto translated_type = std::unique_ptr<HIR::Type> (
157 ASTLoweringType::translate (param.get_type ()));
158
159 auto crate_num = mappings.get_current_crate ();
160 Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
161 mappings.get_next_hir_id (crate_num),
162 UNKNOWN_LOCAL_DEFID);
163
164 auto hir_param
165 = HIR::FunctionParam (mapping, std::move (translated_pattern),
166 std::move (translated_type), param.get_locus ());
167 function_params.push_back (std::move (hir_param));
168 }
169
170 bool terminated = false;
171 std::unique_ptr<HIR::BlockExpr> function_body
172 = std::unique_ptr<HIR::BlockExpr> (
173 ASTLoweringBlock::translate (*function.get_definition ().value (),
174 &terminated));
175
176 auto crate_num = mappings.get_current_crate ();
177 Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
178 mappings.get_next_hir_id (crate_num),
179 mappings.get_next_localdef_id (crate_num));
180
181 mappings.insert_location (function_body->get_mappings ().get_hirid (),
182 function.get_locus ());
183
184 auto fn
185 = new HIR::Function (mapping, std::move (function_name),
186 std::move (qualifiers), std::move (generic_params),
187 std::move (function_params), std::move (return_type),
188 std::move (where_clause), std::move (function_body),
189 std::move (vis), function.get_outer_attrs (),
190 std::move (self_param), defaultness, locus);
191
192 if (fn->is_method ())
193 {
194 // insert mappings for self
195 mappings.insert_hir_self_param (&fn->get_self_param_unchecked ());
196 mappings.insert_location (
197 fn->get_self_param_unchecked ().get_mappings ().get_hirid (),
198 fn->get_self_param_unchecked ().get_locus ());
199 }
200
201 // add the mappings for the function params at the end
202 for (auto &param : fn->get_function_params ())
203 {
204 mappings.insert_hir_param (&param);
205 mappings.insert_location (mapping.get_hirid (), param.get_locus ());
206 }
207
208 translated = fn;
209 item_cast = fn;
210 }
211
212 HIR::TraitItem *
213 ASTLowerTraitItem::translate (AST::AssociatedItem &item)
214 {
215 ASTLowerTraitItem resolver;
216 item.accept_vis (resolver);
217
218 if (resolver.translated != nullptr)
219 {
220 auto id = resolver.translated->get_mappings ().get_hirid ();
221 auto defid = resolver.translated->get_mappings ().get_defid ();
222 auto locus = resolver.translated->get_trait_locus ();
223
224 resolver.handle_outer_attributes (*resolver.translated);
225 resolver.mappings.insert_hir_trait_item (resolver.translated);
226 resolver.mappings.insert_location (id, locus);
227 resolver.mappings.insert_defid_mapping (defid, resolver.translated);
228 }
229
230 return resolver.translated;
231 }
232
233 void
234 ASTLowerTraitItem::visit (AST::Function &func)
235 {
236 std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
237 HIR::WhereClause where_clause (std::move (where_clause_items));
238 HIR::FunctionQualifiers qualifiers
239 = lower_qualifiers (func.get_qualifiers ());
240
241 std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
242 if (func.has_generics ())
243 generic_params = lower_generic_params (func.get_generic_params ());
244
245 std::unique_ptr<HIR::Type> return_type
246 = func.has_return_type () ? std::unique_ptr<HIR::Type> (
247 ASTLoweringType::translate (func.get_return_type ()))
248 : nullptr;
249
250 // set self parameter to error if this is a method
251 // else lower to hir
252 tl::optional<HIR::SelfParam> self_param = tl::nullopt;
253 if (func.has_self_param ())
254 self_param = lower_self (func.get_self_param ());
255
256 std::vector<HIR::FunctionParam> function_params;
257 for (auto &p : func.get_function_params ())
258 {
259 if (p->is_variadic () || p->is_self ())
260 continue;
261
262 auto param = static_cast<AST::FunctionParam &> (*p);
263
264 auto translated_pattern = std::unique_ptr<HIR::Pattern> (
265 ASTLoweringPattern::translate (param.get_pattern ()));
266 auto translated_type = std::unique_ptr<HIR::Type> (
267 ASTLoweringType::translate (param.get_type ()));
268
269 auto crate_num = mappings.get_current_crate ();
270 Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
271 mappings.get_next_hir_id (crate_num),
272 UNKNOWN_LOCAL_DEFID);
273
274 auto hir_param
275 = HIR::FunctionParam (mapping, std::move (translated_pattern),
276 std::move (translated_type), param.get_locus ());
277 function_params.push_back (hir_param);
278 }
279
280 HIR::TraitFunctionDecl decl (func.get_function_name (),
281 std::move (qualifiers),
282 std::move (generic_params),
283 std::move (self_param),
284 std::move (function_params),
285 std::move (return_type),
286 std::move (where_clause));
287 bool terminated = false;
288 std::unique_ptr<HIR::BlockExpr> block_expr
289 = func.has_body () ? std::unique_ptr<HIR::BlockExpr> (
290 ASTLoweringBlock::translate (*func.get_definition ().value (),
291 &terminated))
292 : nullptr;
293
294 auto crate_num = mappings.get_current_crate ();
295 Analysis::NodeMapping mapping (crate_num, func.get_node_id (),
296 mappings.get_next_hir_id (crate_num),
297 mappings.get_next_localdef_id (crate_num));
298
299 auto *trait_item
300 = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr),
301 func.get_outer_attrs (), func.get_locus ());
302 translated = trait_item;
303 if (func.has_self_param ())
304 {
305 // insert mappings for self
306 // TODO: Is this correct ? Looks fishy
307 mappings.insert_hir_self_param (&*self_param);
308 mappings.insert_location (self_param->get_mappings ().get_hirid (),
309 self_param->get_locus ());
310 }
311
312 // add the mappings for the function params at the end
313 for (auto &param : trait_item->get_decl ().get_function_params ())
314 {
315 mappings.insert_hir_param (&param);
316 mappings.insert_location (mapping.get_hirid (), param.get_locus ());
317 }
318 }
319
320 void
321 ASTLowerTraitItem::visit (AST::TraitItemConst &constant)
322 {
323 HIR::Type *type = ASTLoweringType::translate (constant.get_type ());
324 HIR::Expr *expr = constant.has_expression ()
325 ? ASTLoweringExpr::translate (constant.get_expr ())
326 : nullptr;
327
328 auto crate_num = mappings.get_current_crate ();
329 Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
330 mappings.get_next_hir_id (crate_num),
331 mappings.get_next_localdef_id (crate_num));
332
333 HIR::TraitItemConst *trait_item
334 = new HIR::TraitItemConst (mapping, constant.get_identifier (),
335 std::unique_ptr<HIR::Type> (type),
336 std::unique_ptr<HIR::Expr> (expr),
337 constant.get_outer_attrs (),
338 constant.get_locus ());
339 translated = trait_item;
340 }
341
342 void
343 ASTLowerTraitItem::visit (AST::TraitItemType &type)
344 {
345 std::vector<std::unique_ptr<HIR::TypeParamBound> > type_param_bounds;
346 auto crate_num = mappings.get_current_crate ();
347 Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
348 mappings.get_next_hir_id (crate_num),
349 mappings.get_next_localdef_id (crate_num));
350
351 HIR::TraitItemType *trait_item
352 = new HIR::TraitItemType (mapping, type.get_identifier (),
353 std::move (type_param_bounds),
354 type.get_outer_attrs (), type.get_locus ());
355 translated = trait_item;
356 }
357
358 } // namespace HIR
359 } // namespace Rust