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