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