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