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