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