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-item.h"
20 : : #include "rust-diagnostics.h"
21 : : #include "rust-ast-lower.h"
22 : : #include "rust-ast-lower-base.h"
23 : : #include "rust-ast-lower-enumitem.h"
24 : : #include "rust-ast-lower-type.h"
25 : : #include "rust-ast-lower-implitem.h"
26 : : #include "rust-ast-lower-expr.h"
27 : : #include "rust-ast-lower-pattern.h"
28 : : #include "rust-ast-lower-block.h"
29 : : #include "rust-item.h"
30 : :
31 : : namespace Rust {
32 : : namespace HIR {
33 : :
34 : : HIR::Item *
35 : 24210 : ASTLoweringItem::translate (AST::Item &item)
36 : : {
37 : 24210 : ASTLoweringItem resolver;
38 : 24210 : item.accept_vis (resolver);
39 : :
40 : 24210 : if (resolver.translated != nullptr)
41 : : {
42 : 22667 : auto id = resolver.translated->get_mappings ().get_hirid ();
43 : 22667 : auto defid = resolver.translated->get_mappings ().get_defid ();
44 : 22667 : auto locus = resolver.translated->get_locus ();
45 : :
46 : 22667 : resolver.handle_outer_attributes (*resolver.translated);
47 : 22667 : resolver.mappings.insert_ast_item (&item);
48 : 22667 : resolver.mappings.insert_hir_item (resolver.translated);
49 : 22667 : resolver.mappings.insert_location (id, locus);
50 : 22667 : resolver.mappings.insert_defid_mapping (defid, resolver.translated);
51 : : }
52 : :
53 : 24210 : return resolver.translated;
54 : 24210 : }
55 : :
56 : : void
57 : 999 : ASTLoweringItem::visit (AST::Module &module)
58 : : {
59 : 999 : auto crate_num = mappings.get_current_crate ();
60 : 999 : Analysis::NodeMapping mapping (crate_num, module.get_node_id (),
61 : 999 : mappings.get_next_hir_id (crate_num),
62 : 999 : mappings.get_next_localdef_id (crate_num));
63 : :
64 : : // should be lowered from module.get_vis()
65 : 999 : HIR::Visibility vis = translate_visibility (module.get_visibility ());
66 : :
67 : 999 : auto items = std::vector<std::unique_ptr<Item>> ();
68 : :
69 : 4901 : for (auto &item : module.get_items ())
70 : : {
71 : 3902 : auto transitem = translate (*item);
72 : : // The item may be null if it doesn't need to live in the HIR - for
73 : : // example, macro rules definitions
74 : 3902 : if (transitem)
75 : 3507 : items.push_back (std::unique_ptr<Item> (transitem));
76 : : }
77 : :
78 : : // should be lowered/copied from module.get_in/outer_attrs()
79 : 999 : AST::AttrVec inner_attrs = module.get_inner_attrs ();
80 : 999 : AST::AttrVec outer_attrs = module.get_outer_attrs ();
81 : :
82 : 999 : translated
83 : 999 : = new HIR::Module (mapping, module.get_name (), module.get_locus (),
84 : : std::move (items), std::move (vis),
85 : 1998 : std::move (inner_attrs), std::move (outer_attrs));
86 : 999 : mappings.insert_module (static_cast<Module *> (translated));
87 : 999 : }
88 : :
89 : : void
90 : 50 : ASTLoweringItem::visit (AST::TypeAlias &alias)
91 : : {
92 : 50 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
93 : 50 : for (auto &item : alias.get_where_clause ().get_items ())
94 : : {
95 : 0 : HIR::WhereClauseItem *i
96 : 0 : = ASTLowerWhereClauseItem::translate (*item.get ());
97 : 0 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
98 : : }
99 : :
100 : 50 : HIR::WhereClause where_clause (std::move (where_clause_items));
101 : 50 : HIR::Visibility vis = translate_visibility (alias.get_visibility ());
102 : :
103 : 50 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
104 : 50 : if (alias.has_generics ())
105 : 8 : generic_params = lower_generic_params (alias.get_generic_params ());
106 : :
107 : 50 : HIR::Type *existing_type
108 : 50 : = ASTLoweringType::translate (alias.get_type_aliased ());
109 : :
110 : 50 : auto crate_num = mappings.get_current_crate ();
111 : 50 : Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
112 : 50 : mappings.get_next_hir_id (crate_num),
113 : 50 : mappings.get_next_localdef_id (crate_num));
114 : :
115 : 50 : translated
116 : 50 : = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
117 : : std::move (generic_params), std::move (where_clause),
118 : 100 : std::unique_ptr<HIR::Type> (existing_type),
119 : 50 : std::move (vis), alias.get_outer_attrs (),
120 : 150 : alias.get_locus ());
121 : 50 : }
122 : :
123 : : void
124 : 1081 : ASTLoweringItem::visit (AST::TupleStruct &struct_decl)
125 : : {
126 : 1081 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
127 : 1081 : if (struct_decl.has_generics ())
128 : : {
129 : 406 : generic_params = lower_generic_params (struct_decl.get_generic_params ());
130 : : }
131 : :
132 : 1081 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
133 : 1081 : for (auto &item : struct_decl.get_where_clause ().get_items ())
134 : : {
135 : 0 : HIR::WhereClauseItem *i
136 : 0 : = ASTLowerWhereClauseItem::translate (*item.get ());
137 : 0 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
138 : : }
139 : :
140 : 1081 : HIR::WhereClause where_clause (std::move (where_clause_items));
141 : 1081 : HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
142 : :
143 : 1081 : std::vector<HIR::TupleField> fields;
144 : 2936 : for (AST::TupleField &field : struct_decl.get_fields ())
145 : : {
146 : 1855 : if (field.get_field_type ().is_marked_for_strip ())
147 : 0 : continue;
148 : :
149 : : // FIXME: How do we get the visibility from here?
150 : 1855 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
151 : 1855 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
152 : :
153 : 1855 : auto crate_num = mappings.get_current_crate ();
154 : 1855 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
155 : 1855 : mappings.get_next_hir_id (crate_num),
156 : 1855 : mappings.get_next_localdef_id (crate_num));
157 : :
158 : 1855 : HIR::TupleField translated_field (mapping,
159 : 1855 : std::unique_ptr<HIR::Type> (type), vis,
160 : : field.get_locus (),
161 : 3710 : field.get_outer_attrs ());
162 : 1855 : fields.push_back (std::move (translated_field));
163 : 1855 : }
164 : :
165 : 1081 : auto crate_num = mappings.get_current_crate ();
166 : 1081 : Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
167 : 1081 : mappings.get_next_hir_id (crate_num),
168 : 1081 : mappings.get_next_localdef_id (crate_num));
169 : :
170 : 2162 : translated = new HIR::TupleStruct (mapping, std::move (fields),
171 : 1081 : struct_decl.get_identifier (),
172 : : std::move (generic_params),
173 : : std::move (where_clause), vis,
174 : 1081 : struct_decl.get_outer_attrs (),
175 : 1081 : struct_decl.get_locus ());
176 : 1081 : }
177 : :
178 : : void
179 : 1422 : ASTLoweringItem::visit (AST::StructStruct &struct_decl)
180 : : {
181 : 1422 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
182 : 1422 : if (struct_decl.has_generics ())
183 : : {
184 : 344 : generic_params = lower_generic_params (struct_decl.get_generic_params ());
185 : : }
186 : :
187 : 1422 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
188 : 1430 : for (auto &item : struct_decl.get_where_clause ().get_items ())
189 : : {
190 : 8 : HIR::WhereClauseItem *i
191 : 8 : = ASTLowerWhereClauseItem::translate (*item.get ());
192 : 8 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
193 : : }
194 : :
195 : 1422 : HIR::WhereClause where_clause (std::move (where_clause_items));
196 : :
197 : 1422 : HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
198 : :
199 : 1422 : bool is_unit = struct_decl.is_unit_struct ();
200 : 1422 : std::vector<HIR::StructField> fields;
201 : 3243 : for (AST::StructField &field : struct_decl.get_fields ())
202 : : {
203 : 1825 : if (field.get_field_type ().is_marked_for_strip ())
204 : 0 : continue;
205 : :
206 : 1825 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
207 : 1825 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
208 : :
209 : 1825 : auto crate_num = mappings.get_current_crate ();
210 : 1825 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
211 : 1825 : mappings.get_next_hir_id (crate_num),
212 : 1825 : mappings.get_next_localdef_id (crate_num));
213 : :
214 : 1825 : HIR::StructField translated_field (mapping, field.get_field_name (),
215 : 3650 : std::unique_ptr<HIR::Type> (type), vis,
216 : : field.get_locus (),
217 : 3650 : field.get_outer_attrs ());
218 : :
219 : 1825 : if (struct_field_name_exists (fields, translated_field))
220 : : break;
221 : :
222 : 1821 : fields.push_back (std::move (translated_field));
223 : 1825 : }
224 : :
225 : 1422 : auto crate_num = mappings.get_current_crate ();
226 : 1422 : Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
227 : 1422 : mappings.get_next_hir_id (crate_num),
228 : 1422 : mappings.get_next_localdef_id (crate_num));
229 : :
230 : 2844 : translated = new HIR::StructStruct (mapping, std::move (fields),
231 : 1422 : struct_decl.get_identifier (),
232 : : std::move (generic_params),
233 : : std::move (where_clause), is_unit, vis,
234 : 1422 : struct_decl.get_outer_attrs (),
235 : 1422 : struct_decl.get_locus ());
236 : 1422 : }
237 : :
238 : : void
239 : 399 : ASTLoweringItem::visit (AST::Enum &enum_decl)
240 : : {
241 : 399 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
242 : 399 : if (enum_decl.has_generics ())
243 : : {
244 : 171 : generic_params = lower_generic_params (enum_decl.get_generic_params ());
245 : : }
246 : :
247 : 399 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
248 : 399 : for (auto &item : enum_decl.get_where_clause ().get_items ())
249 : : {
250 : 0 : HIR::WhereClauseItem *i
251 : 0 : = ASTLowerWhereClauseItem::translate (*item.get ());
252 : 0 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
253 : : }
254 : :
255 : 399 : HIR::WhereClause where_clause (std::move (where_clause_items));
256 : 399 : HIR::Visibility vis = translate_visibility (enum_decl.get_visibility ());
257 : :
258 : : // bool is_unit = enum_decl.is_zero_variant ();
259 : 399 : std::vector<std::unique_ptr<HIR::EnumItem>> items;
260 : 1325 : for (auto &variant : enum_decl.get_variants ())
261 : : {
262 : 926 : if (variant->is_marked_for_strip ())
263 : 0 : continue;
264 : :
265 : 926 : HIR::EnumItem *hir_item = ASTLoweringEnumItem::translate (variant.get ());
266 : 926 : items.push_back (std::unique_ptr<HIR::EnumItem> (hir_item));
267 : : }
268 : :
269 : 399 : auto crate_num = mappings.get_current_crate ();
270 : 399 : Analysis::NodeMapping mapping (crate_num, enum_decl.get_node_id (),
271 : 399 : mappings.get_next_hir_id (crate_num),
272 : 399 : mappings.get_next_localdef_id (crate_num));
273 : :
274 : 399 : HIR::Enum *hir_enum
275 : 399 : = new HIR::Enum (mapping, enum_decl.get_identifier (), vis,
276 : : std::move (generic_params), std::move (where_clause),
277 : 399 : std::move (items), enum_decl.get_outer_attrs (),
278 : 798 : enum_decl.get_locus ());
279 : 399 : translated = hir_enum;
280 : 1325 : for (auto &variant : hir_enum->get_variants ())
281 : : {
282 : 926 : mappings.insert_hir_enumitem (hir_enum, variant.get ());
283 : : }
284 : 399 : }
285 : :
286 : : void
287 : 114 : ASTLoweringItem::visit (AST::Union &union_decl)
288 : : {
289 : 114 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
290 : 114 : if (union_decl.has_generics ())
291 : : {
292 : 76 : generic_params = lower_generic_params (union_decl.get_generic_params ());
293 : : }
294 : :
295 : 114 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
296 : 114 : for (auto &item : union_decl.get_where_clause ().get_items ())
297 : : {
298 : 0 : HIR::WhereClauseItem *i
299 : 0 : = ASTLowerWhereClauseItem::translate (*item.get ());
300 : 0 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
301 : : }
302 : 114 : HIR::WhereClause where_clause (std::move (where_clause_items));
303 : 114 : HIR::Visibility vis = translate_visibility (union_decl.get_visibility ());
304 : :
305 : 114 : std::vector<HIR::StructField> variants;
306 : 447 : for (AST::StructField &variant : union_decl.get_variants ())
307 : : {
308 : 337 : if (variant.get_field_type ().is_marked_for_strip ())
309 : 0 : continue;
310 : :
311 : : // FIXME: Does visibility apply here?
312 : 337 : HIR::Visibility vis = translate_visibility (variant.get_visibility ());
313 : 337 : HIR::Type *type = ASTLoweringType::translate (variant.get_field_type ());
314 : :
315 : 337 : auto crate_num = mappings.get_current_crate ();
316 : 337 : Analysis::NodeMapping mapping (crate_num, variant.get_node_id (),
317 : 337 : mappings.get_next_hir_id (crate_num),
318 : 337 : mappings.get_next_localdef_id (crate_num));
319 : :
320 : 337 : HIR::StructField translated_variant (mapping, variant.get_field_name (),
321 : 674 : std::unique_ptr<HIR::Type> (type),
322 : : vis, variant.get_locus (),
323 : 674 : variant.get_outer_attrs ());
324 : :
325 : 337 : if (struct_field_name_exists (variants, translated_variant))
326 : : break;
327 : :
328 : 333 : variants.push_back (std::move (translated_variant));
329 : 337 : }
330 : :
331 : 114 : auto crate_num = mappings.get_current_crate ();
332 : 114 : Analysis::NodeMapping mapping (crate_num, union_decl.get_node_id (),
333 : 114 : mappings.get_next_hir_id (crate_num),
334 : 114 : mappings.get_next_localdef_id (crate_num));
335 : :
336 : 114 : translated
337 : 114 : = new HIR::Union (mapping, union_decl.get_identifier (), vis,
338 : : std::move (generic_params), std::move (where_clause),
339 : 114 : std::move (variants), union_decl.get_outer_attrs (),
340 : 228 : union_decl.get_locus ());
341 : 114 : }
342 : :
343 : : void
344 : 81 : ASTLoweringItem::visit (AST::StaticItem &var)
345 : : {
346 : 81 : HIR::Visibility vis = translate_visibility (var.get_visibility ());
347 : :
348 : 81 : HIR::Type *type = ASTLoweringType::translate (var.get_type (), true);
349 : 81 : HIR::Expr *expr = ASTLoweringExpr::translate (var.get_expr ());
350 : :
351 : 81 : auto crate_num = mappings.get_current_crate ();
352 : 81 : Analysis::NodeMapping mapping (crate_num, var.get_node_id (),
353 : 81 : mappings.get_next_hir_id (crate_num),
354 : 81 : mappings.get_next_localdef_id (crate_num));
355 : :
356 : 81 : translated = new HIR::StaticItem (mapping, var.get_identifier (),
357 : 81 : var.is_mutable () ? Mutability::Mut
358 : : : Mutability::Imm,
359 : 81 : std::unique_ptr<HIR::Type> (type),
360 : 162 : std::unique_ptr<HIR::Expr> (expr), vis,
361 : 243 : var.get_outer_attrs (), var.get_locus ());
362 : 81 : }
363 : :
364 : : void
365 : 547 : ASTLoweringItem::visit (AST::ConstantItem &constant)
366 : : {
367 : 547 : HIR::Visibility vis = translate_visibility (constant.get_visibility ());
368 : :
369 : 547 : HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
370 : 547 : HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ());
371 : :
372 : 547 : auto crate_num = mappings.get_current_crate ();
373 : 547 : Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
374 : 547 : mappings.get_next_hir_id (crate_num),
375 : 547 : mappings.get_next_localdef_id (crate_num));
376 : :
377 : 1094 : translated = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
378 : 1641 : std::unique_ptr<HIR::Type> (type),
379 : 1094 : std::unique_ptr<HIR::Expr> (expr),
380 : 547 : constant.get_outer_attrs (),
381 : 2188 : constant.get_locus ());
382 : 547 : }
383 : :
384 : : void
385 : 7337 : ASTLoweringItem::visit (AST::Function &function)
386 : : {
387 : 7337 : if (function.is_marked_for_strip ())
388 : 0 : return;
389 : :
390 : 7337 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
391 : 7349 : for (auto &item : function.get_where_clause ().get_items ())
392 : : {
393 : 12 : HIR::WhereClauseItem *i
394 : 12 : = ASTLowerWhereClauseItem::translate (*item.get ());
395 : 12 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
396 : : }
397 : :
398 : 7337 : HIR::WhereClause where_clause (std::move (where_clause_items));
399 : 7337 : HIR::FunctionQualifiers qualifiers
400 : 7337 : = lower_qualifiers (function.get_qualifiers ());
401 : 7337 : HIR::Visibility vis = translate_visibility (function.get_visibility ());
402 : :
403 : : // need
404 : 7337 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
405 : 7337 : if (function.has_generics ())
406 : : {
407 : 650 : generic_params = lower_generic_params (function.get_generic_params ());
408 : : }
409 : 7337 : Identifier function_name = function.get_function_name ();
410 : 7337 : location_t locus = function.get_locus ();
411 : :
412 : 7337 : std::unique_ptr<HIR::Type> return_type
413 : 7337 : = function.has_return_type () ? std::unique_ptr<HIR::Type> (
414 : : ASTLoweringType::translate (function.get_return_type ()))
415 : 7337 : : nullptr;
416 : :
417 : 7337 : std::vector<HIR::FunctionParam> function_params;
418 : 9422 : for (auto &p : function.get_function_params ())
419 : : {
420 : 2085 : if (p->is_variadic () || p->is_self ())
421 : 0 : continue;
422 : 2085 : auto param = static_cast<AST::FunctionParam &> (*p);
423 : :
424 : 2085 : auto translated_pattern = std::unique_ptr<HIR::Pattern> (
425 : 2085 : ASTLoweringPattern::translate (param.get_pattern ()));
426 : 2085 : auto translated_type = std::unique_ptr<HIR::Type> (
427 : 2085 : ASTLoweringType::translate (param.get_type ()));
428 : :
429 : 2085 : auto crate_num = mappings.get_current_crate ();
430 : 2085 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
431 : 2085 : mappings.get_next_hir_id (crate_num),
432 : 2085 : UNKNOWN_LOCAL_DEFID);
433 : :
434 : 2085 : auto hir_param
435 : : = HIR::FunctionParam (mapping, std::move (translated_pattern),
436 : 2085 : std::move (translated_type), param.get_locus ());
437 : 2085 : function_params.push_back (std::move (hir_param));
438 : 2085 : }
439 : :
440 : 7337 : bool terminated = false;
441 : 7337 : std::unique_ptr<HIR::BlockExpr> function_body
442 : : = std::unique_ptr<HIR::BlockExpr> (
443 : 7337 : ASTLoweringBlock::translate (*function.get_definition ().value (),
444 : 7337 : &terminated));
445 : :
446 : 7337 : auto crate_num = mappings.get_current_crate ();
447 : 14674 : Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
448 : 7337 : mappings.get_next_hir_id (crate_num),
449 : 7337 : mappings.get_next_localdef_id (crate_num));
450 : :
451 : 7337 : mappings.insert_location (function_body->get_mappings ().get_hirid (),
452 : : function.get_locus ());
453 : :
454 : 7337 : Defaultness defaultness
455 : 7337 : = function.is_default () ? Defaultness::Default : Defaultness::Final;
456 : :
457 : 7337 : auto fn
458 : : = new HIR::Function (mapping, std::move (function_name),
459 : : std::move (qualifiers), std::move (generic_params),
460 : : std::move (function_params), std::move (return_type),
461 : : std::move (where_clause), std::move (function_body),
462 : 7337 : std::move (vis), function.get_outer_attrs (),
463 : 7337 : tl::nullopt, defaultness, locus);
464 : :
465 : : // add the mappings for the function params at the end
466 : 9422 : for (auto ¶m : fn->get_function_params ())
467 : : {
468 : 2085 : mappings.insert_hir_param (¶m);
469 : 2085 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
470 : : }
471 : :
472 : 7337 : translated = fn;
473 : 7337 : }
474 : :
475 : : void
476 : 1043 : ASTLoweringItem::visit (AST::InherentImpl &impl_block)
477 : : {
478 : 1043 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
479 : 1043 : for (auto &item : impl_block.get_where_clause ().get_items ())
480 : : {
481 : 0 : HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item);
482 : 0 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
483 : : }
484 : :
485 : 1043 : HIR::WhereClause where_clause (std::move (where_clause_items));
486 : 1043 : HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
487 : :
488 : 1043 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
489 : 1043 : if (impl_block.has_generics ())
490 : : {
491 : 360 : generic_params = lower_generic_params (impl_block.get_generic_params ());
492 : :
493 : 722 : for (auto &generic_param : generic_params)
494 : : {
495 : 362 : switch (generic_param->get_kind ())
496 : : {
497 : 349 : case HIR::GenericParam::GenericKind::TYPE: {
498 : 349 : const HIR::TypeParam &t
499 : 349 : = static_cast<const HIR::TypeParam &> (*generic_param);
500 : :
501 : 349 : if (t.has_type ())
502 : : {
503 : 2 : rich_location rich_locus (line_table, t.get_locus ());
504 : 2 : rich_locus.add_fixit_replace (
505 : : t.get_locus (),
506 : : "for more information, see issue #36887 "
507 : : "<https://github.com/rust-lang/rust/issues/36887>");
508 : 2 : rust_error_at (rich_locus,
509 : : "defaults for type parameters are only "
510 : : "allowed in %<struct%>, %<enum%>, %<type%>, "
511 : : "or %<trait%> definitions");
512 : 2 : }
513 : : }
514 : : break;
515 : :
516 : : default:
517 : : break;
518 : : }
519 : : }
520 : : }
521 : :
522 : 1043 : HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
523 : :
524 : 1043 : auto crate_num = mappings.get_current_crate ();
525 : 1043 : Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
526 : 1043 : mappings.get_next_hir_id (crate_num),
527 : 1043 : mappings.get_next_localdef_id (crate_num));
528 : :
529 : 1043 : std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
530 : 1043 : std::vector<HirId> impl_item_ids;
531 : 4009 : for (auto &impl_item : impl_block.get_impl_items ())
532 : : {
533 : 2966 : if (impl_item->is_marked_for_strip ())
534 : 0 : continue;
535 : :
536 : 2966 : HIR::ImplItem *lowered
537 : 2966 : = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
538 : 2966 : rust_assert (lowered != nullptr);
539 : 2966 : impl_items.push_back (std::unique_ptr<HIR::ImplItem> (lowered));
540 : 2966 : impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
541 : : }
542 : :
543 : 1043 : BoundPolarity polarity = BoundPolarity::RegularBound;
544 : 1043 : HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
545 : : mapping, std::move (impl_items), std::move (generic_params),
546 : 1043 : std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
547 : 2086 : vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
548 : 1043 : impl_block.get_locus (), false);
549 : 1043 : translated = hir_impl_block;
550 : :
551 : 1043 : mappings.insert_hir_impl_block (hir_impl_block);
552 : 4009 : for (auto &impl_item_id : impl_item_ids)
553 : : {
554 : 2966 : mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
555 : : }
556 : 1043 : }
557 : :
558 : : void
559 : 3570 : ASTLoweringItem::visit (AST::Trait &trait)
560 : : {
561 : 3570 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
562 : 3578 : for (auto &item : trait.get_where_clause ().get_items ())
563 : : {
564 : 8 : HIR::WhereClauseItem *i
565 : 8 : = ASTLowerWhereClauseItem::translate (*item.get ());
566 : 8 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
567 : : }
568 : 3570 : HIR::WhereClause where_clause (std::move (where_clause_items));
569 : :
570 : 3570 : HIR::Visibility vis = translate_visibility (trait.get_visibility ());
571 : :
572 : 3570 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
573 : 3570 : if (trait.has_generics ())
574 : : {
575 : 510 : generic_params = lower_generic_params (trait.get_generic_params ());
576 : : }
577 : :
578 : : // TODO: separate "Self" from normal generic parameters
579 : : // in HIR as well as in AST?
580 : 3570 : HIR::GenericParam *self_param
581 : 3570 : = ASTLowerGenericParam::translate (trait.get_implicit_self ());
582 : 3570 : generic_params.emplace (generic_params.begin (), self_param);
583 : :
584 : 3570 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
585 : 3570 : if (trait.has_type_param_bounds ())
586 : : {
587 : 786 : for (auto &bound : trait.get_type_param_bounds ())
588 : : {
589 : 394 : HIR::TypeParamBound *b = lower_bound (*bound);
590 : 394 : type_param_bounds.push_back (
591 : 394 : std::unique_ptr<HIR::TypeParamBound> (b));
592 : : }
593 : : }
594 : :
595 : 3570 : std::vector<std::unique_ptr<HIR::TraitItem>> trait_items;
596 : 3570 : std::vector<HirId> trait_item_ids;
597 : 6365 : for (auto &item : trait.get_trait_items ())
598 : : {
599 : 2795 : if (item->is_marked_for_strip ())
600 : 0 : continue;
601 : :
602 : 2795 : HIR::TraitItem *lowered = ASTLowerTraitItem::translate (*item);
603 : 2795 : trait_items.push_back (std::unique_ptr<HIR::TraitItem> (lowered));
604 : 2795 : trait_item_ids.push_back (lowered->get_mappings ().get_hirid ());
605 : : }
606 : :
607 : 3570 : auto crate_num = mappings.get_current_crate ();
608 : 3570 : Analysis::NodeMapping mapping (crate_num, trait.get_node_id (),
609 : 3570 : mappings.get_next_hir_id (crate_num),
610 : 3570 : mappings.get_next_localdef_id (crate_num));
611 : :
612 : 3570 : auto trait_unsafety
613 : 3570 : = trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
614 : :
615 : 3570 : HIR::Trait *hir_trait
616 : 7140 : = new HIR::Trait (mapping, trait.get_identifier (), trait_unsafety,
617 : : std::move (generic_params), std::move (type_param_bounds),
618 : : where_clause, std::move (trait_items), vis,
619 : 3570 : trait.get_outer_attrs (), trait.get_locus ());
620 : :
621 : 3570 : if (trait.is_auto ())
622 : 24 : mappings.insert_auto_trait (hir_trait);
623 : :
624 : 3570 : translated = hir_trait;
625 : :
626 : 6365 : for (auto trait_item_id : trait_item_ids)
627 : : {
628 : 2795 : mappings.insert_trait_item_mapping (trait_item_id, hir_trait);
629 : : }
630 : 3570 : }
631 : :
632 : : void
633 : 4567 : ASTLoweringItem::visit (AST::TraitImpl &impl_block)
634 : : {
635 : 4567 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
636 : 4567 : bool unsafe = impl_block.is_unsafe ();
637 : 4656 : for (auto &item : impl_block.get_where_clause ().get_items ())
638 : : {
639 : 89 : HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item);
640 : 89 : where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i));
641 : : }
642 : 4567 : HIR::WhereClause where_clause (std::move (where_clause_items));
643 : 4567 : HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
644 : :
645 : 4567 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
646 : 4567 : if (impl_block.has_generics ())
647 : : {
648 : 773 : generic_params = lower_generic_params (impl_block.get_generic_params ());
649 : :
650 : 1683 : for (auto &generic_param : generic_params)
651 : : {
652 : 910 : switch (generic_param->get_kind ())
653 : : {
654 : 787 : case HIR::GenericParam::GenericKind::TYPE: {
655 : 787 : const HIR::TypeParam &t
656 : 787 : = static_cast<const HIR::TypeParam &> (*generic_param);
657 : :
658 : 787 : if (t.has_type ())
659 : : {
660 : 0 : rich_location rich_locus (line_table, t.get_locus ());
661 : 0 : rich_locus.add_fixit_replace (
662 : : t.get_locus (), "for more information, see issue #36887 "
663 : : "<https://github.com/rust-lang/rust/"
664 : : "issues/36887>");
665 : 0 : rust_error_at (rich_locus,
666 : : "defaults for type parameters are only "
667 : : "allowed in %<struct%>, %<enum%>, %<type%>, "
668 : : "or %<trait%> definitions");
669 : 0 : }
670 : : }
671 : : break;
672 : :
673 : : default:
674 : : break;
675 : : }
676 : : }
677 : : }
678 : :
679 : 4567 : HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
680 : 4567 : HIR::TypePath *trait_ref
681 : 4567 : = ASTLowerTypePath::translate (impl_block.get_trait_path ());
682 : :
683 : 4567 : auto crate_num = mappings.get_current_crate ();
684 : 4567 : Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
685 : 4567 : mappings.get_next_hir_id (crate_num),
686 : 4567 : mappings.get_next_localdef_id (crate_num));
687 : :
688 : 4567 : std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
689 : 4567 : std::vector<HirId> impl_item_ids;
690 : 9348 : for (auto &impl_item : impl_block.get_impl_items ())
691 : : {
692 : 4781 : if (impl_item->is_marked_for_strip ())
693 : 0 : continue;
694 : :
695 : 4781 : HIR::ImplItem *lowered
696 : 4781 : = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
697 : 4781 : rust_assert (lowered != nullptr);
698 : 4781 : impl_items.push_back (std::unique_ptr<HIR::ImplItem> (lowered));
699 : 4781 : impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
700 : : }
701 : :
702 : 4567 : BoundPolarity polarity = impl_block.is_exclam ()
703 : 4567 : ? BoundPolarity::NegativeBound
704 : 4567 : : BoundPolarity::RegularBound;
705 : 4567 : HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
706 : : mapping, std::move (impl_items), std::move (generic_params),
707 : 13701 : std::unique_ptr<HIR::Type> (impl_type),
708 : 9134 : std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, polarity, vis,
709 : 9134 : impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
710 : 9134 : impl_block.get_locus (), unsafe);
711 : 4567 : translated = hir_impl_block;
712 : :
713 : 4567 : mappings.insert_hir_impl_block (hir_impl_block);
714 : 9348 : for (auto &impl_item_id : impl_item_ids)
715 : : {
716 : 4781 : mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
717 : : }
718 : 4567 : }
719 : :
720 : : void
721 : 1457 : ASTLoweringItem::visit (AST::ExternBlock &extern_block)
722 : : {
723 : 1457 : translated = lower_extern_block (extern_block);
724 : 1457 : }
725 : :
726 : : void
727 : 1233 : ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
728 : : {
729 : 1233 : lower_macro_definition (def);
730 : 1233 : }
731 : :
732 : : HIR::SimplePath
733 : 28 : ASTLoweringSimplePath::translate (const AST::SimplePath &path)
734 : : {
735 : 28 : ASTLoweringSimplePath resolver;
736 : :
737 : 28 : return resolver.lower (path);
738 : 28 : }
739 : :
740 : : HIR::SimplePathSegment
741 : 43 : ASTLoweringSimplePath::lower (const AST::SimplePathSegment &segment)
742 : : {
743 : 43 : auto crate_num = mappings.get_current_crate ();
744 : 43 : auto node_id = segment.get_node_id ();
745 : :
746 : 43 : auto mapping = Analysis::NodeMapping (crate_num, node_id,
747 : 43 : mappings.get_next_hir_id (crate_num),
748 : 43 : UNKNOWN_LOCAL_DEFID);
749 : :
750 : 43 : auto hir_seg = HIR::SimplePathSegment (mapping);
751 : :
752 : 43 : mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
753 : : // mappings.insert_simple_path_segment (crate_num, node_id, &segment);
754 : :
755 : 43 : return hir_seg;
756 : : }
757 : :
758 : : HIR::SimplePath
759 : 28 : ASTLoweringSimplePath::lower (const AST::SimplePath &path)
760 : : {
761 : 28 : auto segments = std::vector<HIR::SimplePathSegment> ();
762 : 71 : for (auto &segment : path.get_segments ())
763 : 43 : segments.emplace_back (lower (segment));
764 : :
765 : 28 : auto crate_num = mappings.get_current_crate ();
766 : 28 : auto node_id = path.get_node_id ();
767 : :
768 : 28 : auto mapping = Analysis::NodeMapping (crate_num, node_id,
769 : 28 : mappings.get_next_hir_id (crate_num),
770 : 28 : UNKNOWN_LOCAL_DEFID);
771 : :
772 : 28 : auto lowered
773 : 28 : = HIR::SimplePath (std::move (segments), mapping, path.get_locus ());
774 : :
775 : 28 : mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
776 : : // mappings.insert_simple_path (crate_num, node_id, &path);
777 : :
778 : 28 : return lowered;
779 : 28 : }
780 : :
781 : : } // namespace HIR
782 : : } // namespace Rust
|