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