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