Line data Source code
1 : // Copyright (C) 2020-2026 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 171441 : ASTLoweringItem::translate (AST::Item &item)
36 : {
37 171441 : ASTLoweringItem resolver;
38 171441 : item.accept_vis (resolver);
39 :
40 171433 : if (resolver.translated != nullptr)
41 : {
42 158775 : auto id = resolver.translated->get_mappings ().get_hirid ();
43 158775 : auto defid = resolver.translated->get_mappings ().get_defid ();
44 158775 : auto locus = resolver.translated->get_locus ();
45 :
46 158775 : resolver.handle_outer_attributes (*resolver.translated);
47 158775 : resolver.mappings.insert_ast_item (&item);
48 158775 : resolver.mappings.hir.items.insert (resolver.translated);
49 158775 : resolver.mappings.insert_location (id, locus);
50 158775 : resolver.mappings.insert_defid_mapping (defid, resolver.translated);
51 : }
52 :
53 171433 : return resolver.translated;
54 171433 : }
55 :
56 : void
57 4341 : ASTLoweringItem::visit (AST::Module &module)
58 : {
59 4341 : auto crate_num = mappings.crate.get_current_crate ();
60 8682 : Analysis::NodeMapping mapping (crate_num, module.get_node_id (),
61 4341 : mappings.get_next_hir_id (crate_num),
62 4341 : mappings.get_next_localdef_id (crate_num));
63 :
64 : // should be lowered from module.get_vis()
65 4341 : HIR::Visibility vis = translate_visibility (module.get_visibility ());
66 :
67 4341 : auto items = std::vector<std::unique_ptr<Item>> ();
68 :
69 145887 : for (auto &item : module.get_items ())
70 : {
71 141546 : 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 141546 : if (transitem)
75 130153 : items.emplace_back (transitem);
76 : }
77 :
78 : // should be lowered/copied from module.get_in/outer_attrs()
79 4341 : AST::AttrVec inner_attrs = module.get_inner_attrs ();
80 4341 : AST::AttrVec outer_attrs = module.get_outer_attrs ();
81 :
82 4341 : translated
83 4341 : = new HIR::Module (mapping, module.get_name (), module.get_locus (),
84 : std::move (items), std::move (vis),
85 8682 : std::move (inner_attrs), std::move (outer_attrs));
86 4341 : mappings.hir.modules.insert (static_cast<Module *> (translated));
87 4341 : }
88 :
89 : void
90 185 : ASTLoweringItem::visit (AST::TypeAlias &alias)
91 : {
92 185 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
93 185 : where_clause_items.reserve (alias.get_where_clause ().get_items ().size ());
94 :
95 185 : for (auto &item : alias.get_where_clause ().get_items ())
96 0 : where_clause_items.emplace_back (
97 0 : ASTLowerWhereClauseItem::translate (*item.get ()));
98 :
99 185 : HIR::WhereClause where_clause (std::move (where_clause_items));
100 185 : HIR::Visibility vis = translate_visibility (alias.get_visibility ());
101 :
102 185 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
103 185 : if (alias.has_generics ())
104 10 : generic_params = lower_generic_params (alias.get_generic_params ());
105 :
106 185 : HIR::Type *existing_type
107 185 : = ASTLoweringType::translate (alias.get_type_aliased ());
108 :
109 185 : auto crate_num = mappings.crate.get_current_crate ();
110 185 : Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
111 185 : mappings.get_next_hir_id (crate_num),
112 185 : mappings.get_next_localdef_id (crate_num));
113 :
114 185 : translated
115 185 : = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
116 : std::move (generic_params), std::move (where_clause),
117 370 : std::unique_ptr<HIR::Type> (existing_type),
118 185 : std::move (vis), alias.get_outer_attrs (),
119 555 : alias.get_locus ());
120 185 : }
121 :
122 : void
123 0 : ASTLoweringItem::visit (AST::TraitAlias &alias)
124 : {
125 0 : rust_sorry_at (alias.get_locus (), "trait alias lowering");
126 0 : }
127 :
128 : void
129 2311 : ASTLoweringItem::visit (AST::TupleStruct &struct_decl)
130 : {
131 2311 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
132 2311 : if (struct_decl.has_generics ())
133 : {
134 667 : generic_params = lower_generic_params (struct_decl.get_generic_params ());
135 : }
136 :
137 2311 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
138 2311 : where_clause_items.reserve (
139 2311 : struct_decl.get_where_clause ().get_items ().size ());
140 :
141 2324 : for (auto &item : struct_decl.get_where_clause ().get_items ())
142 13 : where_clause_items.emplace_back (
143 13 : ASTLowerWhereClauseItem::translate (*item.get ()));
144 :
145 2311 : HIR::WhereClause where_clause (std::move (where_clause_items));
146 2311 : HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
147 :
148 2311 : std::vector<HIR::TupleField> fields;
149 2311 : fields.reserve (struct_decl.get_fields ().size ());
150 :
151 9663 : for (AST::TupleField &field : struct_decl.get_fields ())
152 : {
153 7352 : if (field.get_field_type ().is_marked_for_strip ())
154 0 : continue;
155 :
156 : // FIXME: How do we get the visibility from here?
157 7352 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
158 7352 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
159 :
160 7352 : auto crate_num = mappings.crate.get_current_crate ();
161 7352 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
162 7352 : mappings.get_next_hir_id (crate_num),
163 7352 : mappings.get_next_localdef_id (crate_num));
164 :
165 7352 : fields.emplace_back (mapping, std::unique_ptr<HIR::Type> (type), vis,
166 14704 : field.get_locus (), field.get_outer_attrs ());
167 7352 : }
168 :
169 2311 : auto crate_num = mappings.crate.get_current_crate ();
170 4622 : Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
171 2311 : mappings.get_next_hir_id (crate_num),
172 2311 : mappings.get_next_localdef_id (crate_num));
173 :
174 4622 : translated = new HIR::TupleStruct (mapping, std::move (fields),
175 2311 : struct_decl.get_identifier (),
176 : std::move (generic_params),
177 : std::move (where_clause), vis,
178 2311 : struct_decl.get_outer_attrs (),
179 2311 : struct_decl.get_locus ());
180 2311 : }
181 :
182 : void
183 8991 : ASTLoweringItem::visit (AST::StructStruct &struct_decl)
184 : {
185 8991 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
186 8991 : if (struct_decl.has_generics ())
187 : {
188 2898 : generic_params = lower_generic_params (struct_decl.get_generic_params ());
189 : }
190 :
191 8991 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
192 8991 : where_clause_items.reserve (
193 8991 : struct_decl.get_where_clause ().get_items ().size ());
194 :
195 9148 : for (auto &item : struct_decl.get_where_clause ().get_items ())
196 157 : where_clause_items.emplace_back (
197 157 : ASTLowerWhereClauseItem::translate (*item.get ()));
198 :
199 8991 : HIR::WhereClause where_clause (std::move (where_clause_items));
200 :
201 8991 : HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
202 :
203 8991 : bool is_unit = struct_decl.is_unit_struct ();
204 8991 : std::vector<HIR::StructField> fields;
205 15981 : for (AST::StructField &field : struct_decl.get_fields ())
206 : {
207 6990 : if (field.get_field_type ().is_marked_for_strip ())
208 4 : continue;
209 :
210 6990 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
211 6990 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
212 :
213 6990 : auto crate_num = mappings.crate.get_current_crate ();
214 6990 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
215 6990 : mappings.get_next_hir_id (crate_num),
216 6990 : mappings.get_next_localdef_id (crate_num));
217 :
218 6990 : HIR::StructField translated_field (mapping, field.get_field_name (),
219 13980 : std::unique_ptr<HIR::Type> (type), vis,
220 : field.get_locus (),
221 13980 : field.get_outer_attrs ());
222 :
223 6990 : if (struct_field_name_exists (fields, translated_field))
224 4 : continue;
225 :
226 6986 : fields.push_back (std::move (translated_field));
227 6990 : }
228 :
229 8991 : auto crate_num = mappings.crate.get_current_crate ();
230 17982 : Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
231 8991 : mappings.get_next_hir_id (crate_num),
232 8991 : mappings.get_next_localdef_id (crate_num));
233 :
234 17982 : translated = new HIR::StructStruct (mapping, std::move (fields),
235 8991 : struct_decl.get_identifier (),
236 : std::move (generic_params),
237 : std::move (where_clause), is_unit, vis,
238 8991 : struct_decl.get_outer_attrs (),
239 8991 : struct_decl.get_locus ());
240 8991 : }
241 :
242 : void
243 1016 : ASTLoweringItem::visit (AST::Enum &enum_decl)
244 : {
245 1016 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
246 1016 : if (enum_decl.has_generics ())
247 : {
248 358 : generic_params = lower_generic_params (enum_decl.get_generic_params ());
249 : }
250 :
251 1016 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
252 1016 : where_clause_items.reserve (
253 1016 : enum_decl.get_where_clause ().get_items ().size ());
254 :
255 1017 : for (auto &item : enum_decl.get_where_clause ().get_items ())
256 1 : where_clause_items.emplace_back (
257 1 : ASTLowerWhereClauseItem::translate (*item.get ()));
258 :
259 1016 : HIR::WhereClause where_clause (std::move (where_clause_items));
260 1016 : HIR::Visibility vis = translate_visibility (enum_decl.get_visibility ());
261 :
262 : // bool is_unit = enum_decl.is_zero_variant ();
263 1016 : std::vector<std::unique_ptr<HIR::EnumItem>> items;
264 1016 : items.reserve (enum_decl.get_variants ().size ());
265 :
266 3533 : for (auto &variant : enum_decl.get_variants ())
267 : {
268 2517 : if (variant->is_marked_for_strip ())
269 0 : continue;
270 :
271 2517 : items.emplace_back (ASTLoweringEnumItem::translate (variant.get ()));
272 : }
273 :
274 1016 : auto crate_num = mappings.crate.get_current_crate ();
275 2032 : Analysis::NodeMapping mapping (crate_num, enum_decl.get_node_id (),
276 1016 : mappings.get_next_hir_id (crate_num),
277 1016 : mappings.get_next_localdef_id (crate_num));
278 :
279 1016 : HIR::Enum *hir_enum
280 1016 : = new HIR::Enum (mapping, enum_decl.get_identifier (), vis,
281 : std::move (generic_params), std::move (where_clause),
282 1016 : std::move (items), enum_decl.get_outer_attrs (),
283 2032 : enum_decl.get_locus ());
284 1016 : translated = hir_enum;
285 3533 : for (auto &variant : hir_enum->get_variants ())
286 : {
287 2517 : mappings.insert_hir_enumitem (hir_enum, variant.get ());
288 : }
289 1016 : }
290 :
291 : void
292 133 : ASTLoweringItem::visit (AST::Union &union_decl)
293 : {
294 133 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
295 133 : if (union_decl.has_generics ())
296 101 : generic_params = lower_generic_params (union_decl.get_generic_params ());
297 :
298 133 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
299 133 : where_clause_items.reserve (
300 133 : union_decl.get_where_clause ().get_items ().size ());
301 :
302 133 : for (auto &item : union_decl.get_where_clause ().get_items ())
303 0 : where_clause_items.emplace_back (
304 0 : ASTLowerWhereClauseItem::translate (*item.get ()));
305 :
306 133 : HIR::WhereClause where_clause (std::move (where_clause_items));
307 133 : HIR::Visibility vis = translate_visibility (union_decl.get_visibility ());
308 :
309 133 : std::vector<HIR::StructField> variants;
310 508 : for (AST::StructField &variant : union_decl.get_variants ())
311 : {
312 377 : if (variant.get_field_type ().is_marked_for_strip ())
313 0 : continue;
314 :
315 : // FIXME: Does visibility apply here?
316 377 : HIR::Visibility vis = translate_visibility (variant.get_visibility ());
317 377 : HIR::Type *type = ASTLoweringType::translate (variant.get_field_type ());
318 :
319 377 : auto crate_num = mappings.crate.get_current_crate ();
320 377 : Analysis::NodeMapping mapping (crate_num, variant.get_node_id (),
321 377 : mappings.get_next_hir_id (crate_num),
322 377 : mappings.get_next_localdef_id (crate_num));
323 :
324 377 : HIR::StructField translated_variant (mapping, variant.get_field_name (),
325 754 : std::unique_ptr<HIR::Type> (type),
326 : vis, variant.get_locus (),
327 754 : variant.get_outer_attrs ());
328 :
329 377 : if (struct_field_name_exists (variants, translated_variant))
330 : break;
331 :
332 375 : variants.push_back (std::move (translated_variant));
333 377 : }
334 :
335 133 : auto crate_num = mappings.crate.get_current_crate ();
336 266 : Analysis::NodeMapping mapping (crate_num, union_decl.get_node_id (),
337 133 : mappings.get_next_hir_id (crate_num),
338 133 : mappings.get_next_localdef_id (crate_num));
339 :
340 133 : translated
341 133 : = new HIR::Union (mapping, union_decl.get_identifier (), vis,
342 : std::move (generic_params), std::move (where_clause),
343 133 : std::move (variants), union_decl.get_outer_attrs (),
344 266 : union_decl.get_locus ());
345 133 : }
346 :
347 : void
348 496 : ASTLoweringItem::visit (AST::StaticItem &var)
349 : {
350 496 : HIR::Visibility vis = translate_visibility (var.get_visibility ());
351 :
352 496 : HIR::Type *type = ASTLoweringType::translate (var.get_type (), true);
353 496 : HIR::Expr *expr = ASTLoweringExpr::translate (var.get_expr ());
354 :
355 496 : auto crate_num = mappings.crate.get_current_crate ();
356 992 : Analysis::NodeMapping mapping (crate_num, var.get_node_id (),
357 496 : mappings.get_next_hir_id (crate_num),
358 496 : mappings.get_next_localdef_id (crate_num));
359 :
360 496 : translated = new HIR::StaticItem (mapping, var.get_identifier (),
361 496 : var.is_mutable () ? Mutability::Mut
362 : : Mutability::Imm,
363 496 : std::unique_ptr<HIR::Type> (type),
364 992 : std::unique_ptr<HIR::Expr> (expr), vis,
365 1488 : var.get_outer_attrs (), var.get_locus ());
366 496 : }
367 :
368 : void
369 7057 : ASTLoweringItem::visit (AST::ConstantItem &constant)
370 : {
371 7057 : HIR::Visibility vis = translate_visibility (constant.get_visibility ());
372 :
373 7057 : HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
374 7057 : HIR::Expr *expr = nullptr;
375 7057 : if (constant.has_expr ())
376 7056 : expr = ASTLoweringExpr::translate (constant.get_expr ());
377 :
378 7057 : auto crate_num = mappings.crate.get_current_crate ();
379 7057 : Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
380 7057 : mappings.get_next_hir_id (crate_num),
381 7057 : mappings.get_next_localdef_id (crate_num));
382 :
383 7057 : translated = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
384 21171 : std::unique_ptr<HIR::Type> (type),
385 14114 : std::unique_ptr<HIR::Expr> (expr),
386 7057 : constant.get_outer_attrs (),
387 21171 : constant.get_locus ());
388 7057 : }
389 :
390 : void
391 32094 : ASTLoweringItem::visit (AST::Function &function)
392 : {
393 32094 : if (function.is_marked_for_strip ())
394 0 : return;
395 :
396 32094 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
397 32094 : where_clause_items.reserve (
398 32094 : function.get_where_clause ().get_items ().size ());
399 :
400 32667 : for (auto &item : function.get_where_clause ().get_items ())
401 573 : where_clause_items.emplace_back (
402 573 : ASTLowerWhereClauseItem::translate (*item.get ()));
403 :
404 32094 : HIR::WhereClause where_clause (std::move (where_clause_items));
405 32094 : HIR::FunctionQualifiers qualifiers
406 32094 : = lower_qualifiers (function.get_qualifiers ());
407 32094 : HIR::Visibility vis = translate_visibility (function.get_visibility ());
408 :
409 : // need
410 32094 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
411 32094 : if (function.has_generics ())
412 : {
413 3235 : generic_params = lower_generic_params (function.get_generic_params ());
414 : }
415 32094 : Identifier function_name = function.get_function_name ();
416 32094 : location_t locus = function.get_locus ();
417 :
418 32094 : std::unique_ptr<HIR::Type> return_type
419 32094 : = function.has_return_type () ? std::unique_ptr<HIR::Type> (
420 : ASTLoweringType::translate (function.get_return_type (), false,
421 : ASTLoweringType::ImplTrait::Allow))
422 32094 : : nullptr;
423 :
424 32094 : std::vector<HIR::FunctionParam> function_params;
425 32094 : function_params.reserve (function.get_function_params ().size ());
426 :
427 32094 : auto crate_num = mappings.crate.get_current_crate ();
428 94527 : for (auto &p : function.get_function_params ())
429 : {
430 62433 : if (p->is_variadic ())
431 2 : continue;
432 62433 : if (p->is_self ())
433 : {
434 0 : rich_location r (line_table, p->get_locus ());
435 0 : r.add_range (function.get_locus ());
436 0 : rust_error_at (
437 : r, "%<self%> parameter is only allowed in associated functions");
438 :
439 : // rustc creates a synthetic regular fn-param here pointing to a
440 : // generic Self as far as i can see but that seems over the top for
441 : // now.
442 : //
443 : // see this example (invalid code):
444 : //
445 : // pub trait X {
446 : // fn x() {
447 : // fn f(&mut self) {}
448 : // f();
449 : // }
450 : // }
451 : //
452 : // without a synthetic param we wont get the number of args error as
453 : // well but i think this is fine for now.
454 : //
455 : // problem is what we make the param type to become...
456 :
457 0 : continue;
458 0 : }
459 :
460 62433 : auto ¶m = static_cast<AST::FunctionParam &> (*p);
461 :
462 62433 : auto translated_pattern = std::unique_ptr<HIR::Pattern> (
463 62433 : ASTLoweringPattern::translate (param.get_pattern ()));
464 :
465 62433 : switch (param.get_pattern ().get_pattern_kind ())
466 : {
467 2 : case AST::Pattern::Kind::Literal:
468 2 : rust_error_at (param.get_locus (),
469 : "refutable pattern in function argument");
470 2 : continue;
471 62431 : default:
472 : // defer checking for when we have type information after lowering
473 62431 : break;
474 : }
475 :
476 62431 : auto translated_type = std::unique_ptr<HIR::Type> (
477 62431 : ASTLoweringType::translate (param.get_type ()));
478 :
479 62431 : auto crate_num = mappings.crate.get_current_crate ();
480 62431 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
481 62431 : mappings.get_next_hir_id (crate_num),
482 62431 : UNKNOWN_LOCAL_DEFID);
483 :
484 62431 : function_params.emplace_back (mapping, std::move (translated_pattern),
485 : std::move (translated_type),
486 62431 : param.get_locus ());
487 62433 : }
488 :
489 32094 : bool terminated = false;
490 32094 : std::unique_ptr<HIR::BlockExpr> function_body
491 : = std::unique_ptr<HIR::BlockExpr> (
492 32094 : ASTLoweringBlock::translate (*function.get_definition ().value (),
493 32094 : &terminated));
494 :
495 64188 : Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
496 32094 : mappings.get_next_hir_id (crate_num),
497 32094 : mappings.get_next_localdef_id (crate_num));
498 :
499 32094 : mappings.insert_location (function_body->get_mappings ().get_hirid (),
500 : function.get_locus ());
501 :
502 32094 : Defaultness defaultness
503 32094 : = function.is_default () ? Defaultness::Default : Defaultness::Final;
504 :
505 32094 : auto fn
506 : = new HIR::Function (mapping, std::move (function_name),
507 : std::move (qualifiers), std::move (generic_params),
508 : std::move (function_params), std::move (return_type),
509 : std::move (where_clause), std::move (function_body),
510 32094 : std::move (vis), function.get_outer_attrs (),
511 32094 : tl::nullopt, defaultness, locus);
512 :
513 : // add the mappings for the function params at the end
514 94525 : for (auto ¶m : fn->get_function_params ())
515 : {
516 62431 : mappings.hir.function_params.insert (¶m);
517 62431 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
518 : }
519 :
520 32094 : translated = fn;
521 32094 : }
522 :
523 : void
524 4707 : ASTLoweringItem::visit (AST::InherentImpl &impl_block)
525 : {
526 4707 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
527 4707 : where_clause_items.reserve (
528 4707 : impl_block.get_where_clause ().get_items ().size ());
529 :
530 4743 : for (auto &item : impl_block.get_where_clause ().get_items ())
531 36 : where_clause_items.emplace_back (
532 36 : ASTLowerWhereClauseItem::translate (*item));
533 :
534 4707 : HIR::WhereClause where_clause (std::move (where_clause_items));
535 4707 : HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
536 :
537 4707 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
538 4707 : if (impl_block.has_generics ())
539 : {
540 2489 : generic_params = lower_generic_params (impl_block.get_generic_params ());
541 :
542 6354 : for (auto &generic_param : generic_params)
543 : {
544 3865 : switch (generic_param->get_kind ())
545 : {
546 2826 : case HIR::GenericParam::GenericKind::TYPE:
547 2826 : {
548 2826 : const HIR::TypeParam &t
549 2826 : = static_cast<const HIR::TypeParam &> (*generic_param);
550 :
551 2826 : if (t.has_type ())
552 : {
553 1 : rich_location rich_locus (line_table, t.get_locus ());
554 1 : rich_locus.add_fixit_replace (
555 : t.get_locus (),
556 : "for more information, see issue #36887 "
557 : "<https://github.com/rust-lang/rust/issues/36887>");
558 1 : rust_error_at (rich_locus,
559 : "defaults for type parameters are only "
560 : "allowed in %<struct%>, %<enum%>, %<type%>, "
561 : "or %<trait%> definitions");
562 1 : }
563 : }
564 : break;
565 :
566 : default:
567 : break;
568 : }
569 : }
570 : }
571 :
572 4707 : HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
573 :
574 4707 : auto crate_num = mappings.crate.get_current_crate ();
575 9414 : Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
576 4707 : mappings.get_next_hir_id (crate_num),
577 4707 : mappings.get_next_localdef_id (crate_num));
578 :
579 9414 : std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
580 4707 : std::vector<HirId> impl_item_ids;
581 34752 : for (auto &impl_item : impl_block.get_impl_items ())
582 : {
583 30045 : if (impl_item->is_marked_for_strip ())
584 0 : continue;
585 :
586 30045 : HIR::ImplItem *lowered
587 30045 : = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
588 30045 : rust_assert (lowered != nullptr);
589 30045 : impl_items.emplace_back (lowered);
590 30045 : impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
591 : }
592 :
593 4707 : BoundPolarity polarity = BoundPolarity::RegularBound;
594 4707 : HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
595 : mapping, std::move (impl_items), std::move (generic_params),
596 4707 : std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
597 9414 : vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
598 4707 : impl_block.get_locus (), false);
599 4707 : translated = hir_impl_block;
600 :
601 4707 : mappings.insert_hir_impl_block (hir_impl_block);
602 34752 : for (auto &impl_item_id : impl_item_ids)
603 : {
604 30045 : mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
605 : }
606 4707 : }
607 :
608 : void
609 6236 : ASTLoweringItem::visit (AST::Trait &trait)
610 : {
611 6236 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
612 6236 : where_clause_items.reserve (trait.get_where_clause ().get_items ().size ());
613 :
614 6243 : for (auto &item : trait.get_where_clause ().get_items ())
615 7 : where_clause_items.emplace_back (
616 7 : ASTLowerWhereClauseItem::translate (*item.get ()));
617 :
618 6236 : HIR::WhereClause where_clause (std::move (where_clause_items));
619 :
620 6236 : HIR::Visibility vis = translate_visibility (trait.get_visibility ());
621 :
622 6236 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
623 6236 : if (trait.has_generics ())
624 : {
625 1451 : generic_params = lower_generic_params (trait.get_generic_params ());
626 : }
627 :
628 : // TODO: separate "Self" from normal generic parameters
629 : // in HIR as well as in AST?
630 6236 : HIR::GenericParam *self_param
631 6236 : = ASTLowerGenericParam::translate (trait.get_implicit_self ());
632 6236 : generic_params.emplace (generic_params.begin (), self_param);
633 :
634 6236 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
635 6236 : if (trait.has_type_param_bounds ())
636 : {
637 3015 : for (auto &bound : trait.get_type_param_bounds ())
638 1654 : type_param_bounds.emplace_back (lower_bound (*bound));
639 : }
640 :
641 6236 : auto trait_item_size = trait.get_trait_items ().size ();
642 :
643 6236 : std::vector<std::unique_ptr<HIR::TraitItem>> trait_items;
644 6236 : trait_items.reserve (trait_item_size);
645 6236 : std::vector<HirId> trait_item_ids;
646 6236 : trait_item_ids.reserve (trait_item_size);
647 :
648 14680 : for (auto &item : trait.get_trait_items ())
649 : {
650 8444 : if (item->is_marked_for_strip ())
651 0 : continue;
652 :
653 8444 : HIR::TraitItem *lowered = ASTLowerTraitItem::translate (*item);
654 8444 : trait_item_ids.push_back (lowered->get_mappings ().get_hirid ());
655 8444 : trait_items.emplace_back (lowered);
656 : }
657 :
658 6236 : auto crate_num = mappings.crate.get_current_crate ();
659 12472 : Analysis::NodeMapping mapping (crate_num, trait.get_node_id (),
660 6236 : mappings.get_next_hir_id (crate_num),
661 6236 : mappings.get_next_localdef_id (crate_num));
662 :
663 6236 : auto trait_unsafety
664 6236 : = trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
665 :
666 6236 : HIR::Trait *hir_trait
667 12472 : = new HIR::Trait (mapping, trait.get_identifier (), trait_unsafety,
668 : std::move (generic_params), std::move (type_param_bounds),
669 : where_clause, std::move (trait_items), vis,
670 6236 : trait.get_outer_attrs (), trait.get_locus ());
671 :
672 6236 : if (trait.is_auto ())
673 70 : mappings.insert_auto_trait (hir_trait);
674 :
675 6236 : translated = hir_trait;
676 :
677 14680 : for (auto trait_item_id : trait_item_ids)
678 : {
679 8444 : mappings.insert_trait_item_mapping (trait_item_id, hir_trait);
680 : }
681 6236 : }
682 :
683 : void
684 88896 : ASTLoweringItem::visit (AST::TraitImpl &impl_block)
685 : {
686 88896 : bool unsafe = impl_block.is_unsafe ();
687 :
688 88896 : std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
689 88896 : where_clause_items.reserve (
690 88896 : impl_block.get_where_clause ().get_items ().size ());
691 :
692 94089 : for (auto &item : impl_block.get_where_clause ().get_items ())
693 5193 : where_clause_items.emplace_back (
694 5193 : ASTLowerWhereClauseItem::translate (*item));
695 :
696 88896 : HIR::WhereClause where_clause (std::move (where_clause_items));
697 88896 : HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
698 :
699 88896 : std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
700 88896 : if (impl_block.has_generics ())
701 : {
702 29364 : generic_params = lower_generic_params (impl_block.get_generic_params ());
703 :
704 111081 : for (auto &generic_param : generic_params)
705 : {
706 81717 : switch (generic_param->get_kind ())
707 : {
708 69833 : case HIR::GenericParam::GenericKind::TYPE:
709 69833 : {
710 69833 : const HIR::TypeParam &t
711 69833 : = static_cast<const HIR::TypeParam &> (*generic_param);
712 :
713 69833 : if (t.has_type ())
714 : {
715 0 : rich_location rich_locus (line_table, t.get_locus ());
716 0 : rich_locus.add_fixit_replace (
717 : t.get_locus (), "for more information, see issue #36887 "
718 : "<https://github.com/rust-lang/rust/"
719 : "issues/36887>");
720 0 : rust_error_at (rich_locus,
721 : "defaults for type parameters are only "
722 : "allowed in %<struct%>, %<enum%>, %<type%>, "
723 : "or %<trait%> definitions");
724 0 : }
725 : }
726 : break;
727 :
728 : default:
729 : break;
730 : }
731 : }
732 : }
733 :
734 88896 : HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
735 88896 : HIR::TypePath *trait_ref
736 88896 : = ASTLowerTypePath::translate (impl_block.get_trait_path ());
737 :
738 88896 : auto crate_num = mappings.crate.get_current_crate ();
739 177792 : Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
740 88896 : mappings.get_next_hir_id (crate_num),
741 88896 : mappings.get_next_localdef_id (crate_num));
742 :
743 88896 : auto impl_items_size = impl_block.get_impl_items ().size ();
744 :
745 177792 : std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
746 88896 : impl_items.reserve (impl_items_size);
747 88896 : std::vector<HirId> impl_item_ids;
748 88896 : impl_item_ids.reserve (impl_items_size);
749 :
750 213116 : for (auto &impl_item : impl_block.get_impl_items ())
751 : {
752 124220 : if (impl_item->is_marked_for_strip ())
753 0 : continue;
754 :
755 124220 : HIR::ImplItem *lowered
756 124220 : = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
757 124220 : rust_assert (lowered != nullptr);
758 124220 : impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
759 124220 : impl_items.emplace_back (lowered);
760 : }
761 :
762 88896 : BoundPolarity polarity = impl_block.is_exclam ()
763 88896 : ? BoundPolarity::NegativeBound
764 88716 : : BoundPolarity::RegularBound;
765 88896 : HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
766 : mapping, std::move (impl_items), std::move (generic_params),
767 266688 : std::unique_ptr<HIR::Type> (impl_type),
768 177792 : std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, polarity, vis,
769 177792 : impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
770 177792 : impl_block.get_locus (), unsafe);
771 88896 : translated = hir_impl_block;
772 :
773 88896 : mappings.insert_hir_impl_block (hir_impl_block);
774 :
775 213116 : for (auto &impl_item_id : impl_item_ids)
776 : {
777 124220 : mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
778 : }
779 88896 : }
780 :
781 : void
782 2312 : ASTLoweringItem::visit (AST::ExternBlock &extern_block)
783 : {
784 2312 : translated = lower_extern_block (extern_block);
785 2312 : }
786 :
787 : void
788 3636 : ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
789 : {
790 3636 : lower_macro_definition (def);
791 3636 : }
792 :
793 : void
794 54 : ASTLoweringItem::visit (AST::ExternCrate &extern_crate)
795 : {
796 54 : if (extern_crate.references_self ())
797 : return;
798 :
799 54 : auto &mappings = Analysis::Mappings::get ();
800 54 : auto &crate_mapping = mappings.crate;
801 :
802 54 : CrateNum num
803 54 : = crate_mapping.lookup_crate_name (extern_crate.get_referenced_crate ())
804 54 : .value ();
805 54 : AST::Crate &crate = mappings.crate.get_ast_crate (num);
806 :
807 54 : auto saved_crate_num = mappings.crate.get_current_crate ();
808 54 : crate_mapping.set_current_crate (num);
809 54 : auto lowered = ASTLowering::Resolve (crate);
810 54 : mappings.insert_hir_crate (std::move (lowered));
811 46 : crate_mapping.set_current_crate (saved_crate_num);
812 46 : }
813 :
814 : HIR::SimplePath
815 3852 : ASTLoweringSimplePath::translate (const AST::SimplePath &path)
816 : {
817 3852 : ASTLoweringSimplePath resolver;
818 :
819 3852 : return resolver.lower (path);
820 3852 : }
821 :
822 : HIR::SimplePathSegment
823 3948 : ASTLoweringSimplePath::lower (const AST::SimplePathSegment &segment)
824 : {
825 3948 : auto crate_num = mappings.crate.get_current_crate ();
826 3948 : auto node_id = segment.get_node_id ();
827 :
828 3948 : auto mapping = Analysis::NodeMapping (crate_num, node_id,
829 3948 : mappings.get_next_hir_id (crate_num),
830 3948 : UNKNOWN_LOCAL_DEFID);
831 :
832 3948 : auto hir_seg = HIR::SimplePathSegment (mapping);
833 :
834 3948 : mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
835 : // mappings.insert_simple_path_segment (crate_num, node_id, &segment);
836 :
837 3948 : return hir_seg;
838 : }
839 :
840 : HIR::SimplePath
841 3852 : ASTLoweringSimplePath::lower (const AST::SimplePath &path)
842 : {
843 3852 : auto segments = std::vector<HIR::SimplePathSegment> ();
844 7800 : for (auto &segment : path.get_segments ())
845 3948 : segments.emplace_back (lower (segment));
846 :
847 3852 : auto crate_num = mappings.crate.get_current_crate ();
848 3852 : auto node_id = path.get_node_id ();
849 :
850 3852 : auto mapping = Analysis::NodeMapping (crate_num, node_id,
851 3852 : mappings.get_next_hir_id (crate_num),
852 3852 : UNKNOWN_LOCAL_DEFID);
853 :
854 3852 : auto lowered
855 3852 : = HIR::SimplePath (std::move (segments), mapping, path.get_locus ());
856 :
857 3852 : mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
858 : // mappings.insert_simple_path (crate_num, node_id, &path);
859 :
860 3852 : return lowered;
861 3852 : }
862 :
863 : } // namespace HIR
864 : } // namespace Rust
|