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