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