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 : 8077 : ASTLowerImplItem::translate (AST::AssociatedItem &item, HirId parent_impl_id)
33 : : {
34 : 8077 : ASTLowerImplItem resolver;
35 : 8077 : item.accept_vis (resolver);
36 : :
37 : 8077 : if (resolver.translated != nullptr)
38 : : {
39 : 8077 : rust_assert (resolver.item_cast != nullptr);
40 : :
41 : 8077 : auto id = resolver.translated->get_impl_mappings ().get_hirid ();
42 : 8077 : auto defid = resolver.translated->get_impl_mappings ().get_defid ();
43 : 8077 : auto locus = resolver.translated->get_locus ();
44 : :
45 : 8077 : resolver.handle_outer_attributes (*resolver.item_cast);
46 : 8077 : resolver.mappings.insert_hir_implitem (parent_impl_id,
47 : : resolver.translated);
48 : 8077 : resolver.mappings.insert_location (id, locus);
49 : 8077 : resolver.mappings.insert_defid_mapping (defid, resolver.item_cast);
50 : : }
51 : :
52 : 8077 : return resolver.translated;
53 : 8077 : }
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 : 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 : 6842 : ASTLowerImplItem::visit (AST::Function &function)
111 : : {
112 : : // ignore for now and leave empty
113 : 6842 : std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
114 : 6843 : 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 : 6842 : HIR::WhereClause where_clause (std::move (where_clause_items));
122 : 6842 : HIR::FunctionQualifiers qualifiers
123 : 6842 : = lower_qualifiers (function.get_qualifiers ());
124 : 6842 : HIR::Visibility vis = translate_visibility (function.get_visibility ());
125 : :
126 : : // need
127 : 6842 : std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
128 : 6842 : if (function.has_generics ())
129 : : {
130 : 103 : generic_params = lower_generic_params (function.get_generic_params ());
131 : : }
132 : 6842 : Identifier function_name = function.get_function_name ();
133 : 6842 : location_t locus = function.get_locus ();
134 : :
135 : 6842 : tl::optional<HIR::SelfParam> self_param = tl::nullopt;
136 : 6842 : if (function.has_self_param ())
137 : 5723 : self_param = lower_self (function.get_self_param ());
138 : :
139 : 6842 : std::unique_ptr<HIR::Type> return_type
140 : 6842 : = 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 : 6842 : : nullptr;
144 : :
145 : 6842 : Defaultness defaultness
146 : 6842 : = function.is_default () ? Defaultness::Default : Defaultness::Final;
147 : :
148 : 6842 : std::vector<HIR::FunctionParam> function_params;
149 : 16947 : for (auto &p : function.get_function_params ())
150 : : {
151 : 10105 : if (p->is_self () || p->is_variadic ())
152 : 5723 : continue;
153 : 4382 : auto param = static_cast<AST::FunctionParam &> (*p);
154 : :
155 : 4382 : auto translated_pattern = std::unique_ptr<HIR::Pattern> (
156 : 4382 : ASTLoweringPattern::translate (param.get_pattern ()));
157 : 4382 : auto translated_type = std::unique_ptr<HIR::Type> (
158 : 4382 : ASTLoweringType::translate (param.get_type ()));
159 : :
160 : 4382 : auto crate_num = mappings.get_current_crate ();
161 : 4382 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
162 : 4382 : mappings.get_next_hir_id (crate_num),
163 : 4382 : UNKNOWN_LOCAL_DEFID);
164 : :
165 : 4382 : function_params.emplace_back (mapping, std::move (translated_pattern),
166 : : std::move (translated_type),
167 : 4382 : param.get_locus ());
168 : 4382 : }
169 : :
170 : 6842 : bool terminated = false;
171 : 6842 : std::unique_ptr<HIR::BlockExpr> function_body
172 : : = std::unique_ptr<HIR::BlockExpr> (
173 : 6842 : ASTLoweringBlock::translate (*function.get_definition ().value (),
174 : 6842 : &terminated));
175 : :
176 : 6842 : auto crate_num = mappings.get_current_crate ();
177 : 13684 : Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
178 : 6842 : mappings.get_next_hir_id (crate_num),
179 : 6842 : mappings.get_next_localdef_id (crate_num));
180 : :
181 : 6842 : mappings.insert_location (function_body->get_mappings ().get_hirid (),
182 : : function.get_locus ());
183 : :
184 : 6842 : 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 : 6842 : std::move (vis), function.get_outer_attrs (),
190 : 6842 : std::move (self_param), defaultness, locus);
191 : :
192 : 6842 : 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 : 11224 : for (auto ¶m : fn->get_function_params ())
203 : : {
204 : 4382 : mappings.insert_hir_param (¶m);
205 : 4382 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
206 : : }
207 : :
208 : 6842 : translated = fn;
209 : 6842 : item_cast = fn;
210 : 12565 : }
211 : :
212 : : HIR::TraitItem *
213 : 3310 : ASTLowerTraitItem::translate (AST::AssociatedItem &item)
214 : : {
215 : 3310 : ASTLowerTraitItem resolver;
216 : 3310 : item.accept_vis (resolver);
217 : :
218 : 3310 : if (resolver.translated != nullptr)
219 : : {
220 : 3310 : auto id = resolver.translated->get_mappings ().get_hirid ();
221 : 3310 : auto defid = resolver.translated->get_mappings ().get_defid ();
222 : 3310 : auto locus = resolver.translated->get_trait_locus ();
223 : :
224 : 3310 : resolver.handle_outer_attributes (*resolver.translated);
225 : 3310 : resolver.mappings.insert_hir_trait_item (resolver.translated);
226 : 3310 : resolver.mappings.insert_location (id, locus);
227 : 3310 : resolver.mappings.insert_defid_mapping (defid, resolver.translated);
228 : : }
229 : :
230 : 3310 : return resolver.translated;
231 : 3310 : }
232 : :
233 : : void
234 : 2544 : ASTLowerTraitItem::visit (AST::Function &func)
235 : : {
236 : 2544 : std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
237 : 2544 : HIR::WhereClause where_clause (std::move (where_clause_items));
238 : 2544 : HIR::FunctionQualifiers qualifiers
239 : 2544 : = lower_qualifiers (func.get_qualifiers ());
240 : :
241 : 2544 : std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
242 : 2544 : if (func.has_generics ())
243 : 16 : generic_params = lower_generic_params (func.get_generic_params ());
244 : :
245 : 2544 : std::unique_ptr<HIR::Type> return_type
246 : 2544 : = func.has_return_type () ? std::unique_ptr<HIR::Type> (
247 : : ASTLoweringType::translate (func.get_return_type ()))
248 : 2544 : : nullptr;
249 : :
250 : : // set self parameter to error if this is a method
251 : : // else lower to hir
252 : 2544 : tl::optional<HIR::SelfParam> self_param = tl::nullopt;
253 : 2544 : if (func.has_self_param ())
254 : 2232 : self_param = lower_self (func.get_self_param ());
255 : :
256 : 2544 : std::vector<HIR::FunctionParam> function_params;
257 : 6415 : for (auto &p : func.get_function_params ())
258 : : {
259 : 3871 : if (p->is_variadic () || p->is_self ())
260 : 2232 : continue;
261 : :
262 : 1639 : auto param = static_cast<AST::FunctionParam &> (*p);
263 : :
264 : 1639 : auto translated_pattern = std::unique_ptr<HIR::Pattern> (
265 : 1639 : ASTLoweringPattern::translate (param.get_pattern ()));
266 : 1639 : auto translated_type = std::unique_ptr<HIR::Type> (
267 : 1639 : ASTLoweringType::translate (param.get_type ()));
268 : :
269 : 1639 : auto crate_num = mappings.get_current_crate ();
270 : 1639 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
271 : 1639 : mappings.get_next_hir_id (crate_num),
272 : 1639 : UNKNOWN_LOCAL_DEFID);
273 : :
274 : 1639 : function_params.emplace_back (mapping, std::move (translated_pattern),
275 : : std::move (translated_type),
276 : 1639 : param.get_locus ());
277 : 1639 : }
278 : :
279 : 2544 : 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 : 2544 : 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 : 4776 : std::move (where_clause));
295 : 2544 : bool terminated = false;
296 : 2544 : std::unique_ptr<HIR::BlockExpr> block_expr
297 : 2544 : = func.has_body () ? std::unique_ptr<HIR::BlockExpr> (
298 : 857 : ASTLoweringBlock::translate (*func.get_definition ().value (),
299 : : &terminated))
300 : 2544 : : nullptr;
301 : :
302 : 2544 : auto crate_num = mappings.get_current_crate ();
303 : 5088 : Analysis::NodeMapping mapping (crate_num, func.get_node_id (),
304 : 2544 : mappings.get_next_hir_id (crate_num),
305 : 2544 : mappings.get_next_localdef_id (crate_num));
306 : :
307 : 2544 : auto *trait_item
308 : : = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr),
309 : 2544 : func.get_outer_attrs (), func.get_locus ());
310 : 2544 : translated = trait_item;
311 : :
312 : : // add the mappings for the function params at the end
313 : 4183 : for (auto ¶m : trait_item->get_decl ().get_function_params ())
314 : : {
315 : 1639 : mappings.insert_hir_param (¶m);
316 : 1639 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
317 : : }
318 : 4776 : }
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 : 728 : ASTLowerTraitItem::visit (AST::TraitItemType &type)
344 : : {
345 : 728 : std::vector<std::unique_ptr<HIR::TypeParamBound> > type_param_bounds;
346 : 728 : auto crate_num = mappings.get_current_crate ();
347 : 1456 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
348 : 728 : mappings.get_next_hir_id (crate_num),
349 : 728 : mappings.get_next_localdef_id (crate_num));
350 : :
351 : 728 : HIR::TraitItemType *trait_item
352 : 1456 : = new HIR::TraitItemType (mapping, type.get_identifier (),
353 : : std::move (type_param_bounds),
354 : 728 : type.get_outer_attrs (), type.get_locus ());
355 : 728 : translated = trait_item;
356 : 728 : }
357 : :
358 : : } // namespace HIR
359 : : } // namespace Rust
|