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 8213 : ASTLowerImplItem::translate (AST::AssociatedItem &item, HirId parent_impl_id)
33 : {
34 8213 : ASTLowerImplItem resolver;
35 8213 : item.accept_vis (resolver);
36 :
37 8213 : if (resolver.translated != nullptr)
38 : {
39 8213 : rust_assert (resolver.item_cast != nullptr);
40 :
41 8213 : auto id = resolver.translated->get_impl_mappings ().get_hirid ();
42 8213 : auto defid = resolver.translated->get_impl_mappings ().get_defid ();
43 8213 : auto locus = resolver.translated->get_locus ();
44 :
45 8213 : resolver.handle_outer_attributes (*resolver.item_cast);
46 8213 : resolver.mappings.insert_hir_implitem (parent_impl_id,
47 : resolver.translated);
48 8213 : resolver.mappings.insert_location (id, locus);
49 8213 : resolver.mappings.insert_defid_mapping (defid, resolver.item_cast);
50 : }
51 :
52 8213 : return resolver.translated;
53 8213 : }
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 6958 : ASTLowerImplItem::visit (AST::Function &function)
111 : {
112 : // ignore for now and leave empty
113 6958 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
114 6959 : 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 6958 : HIR::WhereClause where_clause (std::move (where_clause_items));
122 6958 : HIR::FunctionQualifiers qualifiers
123 6958 : = lower_qualifiers (function.get_qualifiers ());
124 6958 : HIR::Visibility vis = translate_visibility (function.get_visibility ());
125 :
126 : // need
127 6958 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
128 6958 : if (function.has_generics ())
129 : {
130 112 : generic_params = lower_generic_params (function.get_generic_params ());
131 : }
132 6958 : Identifier function_name = function.get_function_name ();
133 6958 : location_t locus = function.get_locus ();
134 :
135 6958 : tl::optional<HIR::SelfParam> self_param = tl::nullopt;
136 6958 : if (function.has_self_param ())
137 5828 : self_param = lower_self (function.get_self_param ());
138 :
139 6958 : std::unique_ptr<HIR::Type> return_type
140 6958 : = 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 6958 : : nullptr;
144 :
145 6958 : Defaultness defaultness
146 6958 : = function.is_default () ? Defaultness::Default : Defaultness::Final;
147 :
148 6958 : std::vector<HIR::FunctionParam> function_params;
149 17209 : for (auto &p : function.get_function_params ())
150 : {
151 10251 : if (p->is_self () || p->is_variadic ())
152 5828 : continue;
153 4423 : auto param = static_cast<AST::FunctionParam &> (*p);
154 :
155 4423 : auto translated_pattern = std::unique_ptr<HIR::Pattern> (
156 4423 : ASTLoweringPattern::translate (param.get_pattern ()));
157 4423 : auto translated_type = std::unique_ptr<HIR::Type> (
158 4423 : ASTLoweringType::translate (param.get_type ()));
159 :
160 4423 : auto crate_num = mappings.get_current_crate ();
161 4423 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
162 4423 : mappings.get_next_hir_id (crate_num),
163 4423 : UNKNOWN_LOCAL_DEFID);
164 :
165 4423 : function_params.emplace_back (mapping, std::move (translated_pattern),
166 : std::move (translated_type),
167 4423 : param.get_locus ());
168 4423 : }
169 :
170 6958 : bool terminated = false;
171 6958 : std::unique_ptr<HIR::BlockExpr> function_body
172 : = std::unique_ptr<HIR::BlockExpr> (
173 6958 : ASTLoweringBlock::translate (*function.get_definition ().value (),
174 6958 : &terminated));
175 :
176 6958 : auto crate_num = mappings.get_current_crate ();
177 13916 : Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
178 6958 : mappings.get_next_hir_id (crate_num),
179 6958 : mappings.get_next_localdef_id (crate_num));
180 :
181 6958 : mappings.insert_location (function_body->get_mappings ().get_hirid (),
182 : function.get_locus ());
183 :
184 6958 : 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 6958 : std::move (vis), function.get_outer_attrs (),
190 6958 : std::move (self_param), defaultness, locus);
191 :
192 6958 : if (fn->is_method ())
193 : {
194 : // insert mappings for self
195 5828 : mappings.insert_hir_self_param (&fn->get_self_param_unchecked ());
196 11656 : mappings.insert_location (
197 11656 : fn->get_self_param_unchecked ().get_mappings ().get_hirid (),
198 5828 : fn->get_self_param_unchecked ().get_locus ());
199 : }
200 :
201 : // add the mappings for the function params at the end
202 11381 : for (auto ¶m : fn->get_function_params ())
203 : {
204 4423 : mappings.insert_hir_param (¶m);
205 4423 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
206 : }
207 :
208 6958 : translated = fn;
209 6958 : item_cast = fn;
210 12786 : }
211 :
212 : HIR::TraitItem *
213 3375 : ASTLowerTraitItem::translate (AST::AssociatedItem &item)
214 : {
215 3375 : ASTLowerTraitItem resolver;
216 3375 : item.accept_vis (resolver);
217 :
218 3375 : if (resolver.translated != nullptr)
219 : {
220 3375 : auto id = resolver.translated->get_mappings ().get_hirid ();
221 3375 : auto defid = resolver.translated->get_mappings ().get_defid ();
222 3375 : auto locus = resolver.translated->get_trait_locus ();
223 :
224 3375 : resolver.handle_outer_attributes (*resolver.translated);
225 3375 : resolver.mappings.insert_hir_trait_item (resolver.translated);
226 3375 : resolver.mappings.insert_location (id, locus);
227 3375 : resolver.mappings.insert_defid_mapping (defid, resolver.translated);
228 : }
229 :
230 3375 : return resolver.translated;
231 3375 : }
232 :
233 : void
234 2595 : ASTLowerTraitItem::visit (AST::Function &func)
235 : {
236 2595 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
237 2595 : HIR::WhereClause where_clause (std::move (where_clause_items));
238 2595 : HIR::FunctionQualifiers qualifiers
239 2595 : = lower_qualifiers (func.get_qualifiers ());
240 :
241 2595 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
242 2595 : if (func.has_generics ())
243 26 : generic_params = lower_generic_params (func.get_generic_params ());
244 :
245 2595 : std::unique_ptr<HIR::Type> return_type
246 2595 : = func.has_return_type () ? std::unique_ptr<HIR::Type> (
247 : ASTLoweringType::translate (func.get_return_type ()))
248 2595 : : nullptr;
249 :
250 : // set self parameter to error if this is a method
251 : // else lower to hir
252 2595 : tl::optional<HIR::SelfParam> self_param = tl::nullopt;
253 2595 : if (func.has_self_param ())
254 2278 : self_param = lower_self (func.get_self_param ());
255 :
256 2595 : std::vector<HIR::FunctionParam> function_params;
257 6522 : for (auto &p : func.get_function_params ())
258 : {
259 3927 : if (p->is_variadic () || p->is_self ())
260 2278 : 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 2595 : if (func.has_self_param ())
280 : {
281 : // insert mappings for self
282 : // TODO: Is this correct ? Looks fishy
283 2278 : mappings.insert_hir_self_param (&*self_param);
284 2278 : mappings.insert_location (self_param->get_mappings ().get_hirid (),
285 : self_param->get_locus ());
286 : }
287 :
288 2595 : 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 4873 : std::move (where_clause));
295 2595 : bool terminated = false;
296 2595 : std::unique_ptr<HIR::BlockExpr> block_expr
297 2595 : = func.has_body () ? std::unique_ptr<HIR::BlockExpr> (
298 862 : ASTLoweringBlock::translate (*func.get_definition ().value (),
299 : &terminated))
300 2595 : : nullptr;
301 :
302 2595 : auto crate_num = mappings.get_current_crate ();
303 5190 : Analysis::NodeMapping mapping (crate_num, func.get_node_id (),
304 2595 : mappings.get_next_hir_id (crate_num),
305 2595 : mappings.get_next_localdef_id (crate_num));
306 :
307 2595 : auto *trait_item
308 : = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr),
309 2595 : func.get_outer_attrs (), func.get_locus ());
310 2595 : translated = trait_item;
311 :
312 : // add the mappings for the function params at the end
313 4244 : 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 4873 : }
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
|