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