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