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-type.h"
20 : #include "rust-hir-map.h"
21 : #include "rust-hir-path.h"
22 : #include "rust-hir-type.h"
23 : #include "rust-path.h"
24 : #include "rust-pattern.h"
25 :
26 : namespace Rust {
27 : namespace HIR {
28 :
29 : HIR::TypePath *
30 54704 : ASTLowerTypePath::translate (AST::TypePath &type)
31 : {
32 54704 : ASTLowerTypePath resolver;
33 54704 : type.accept_vis (resolver);
34 54704 : rust_assert (resolver.translated != nullptr);
35 54704 : return resolver.translated;
36 54704 : }
37 :
38 : void
39 30 : ASTLowerTypePath::visit (AST::TypePathSegmentFunction &segment)
40 : {
41 30 : auto crate_num = mappings.get_current_crate ();
42 30 : auto hirid = mappings.get_next_hir_id (crate_num);
43 30 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
44 30 : UNKNOWN_LOCAL_DEFID);
45 :
46 60 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
47 :
48 30 : AST::TypePathFunction &fn = segment.get_type_path_function ();
49 30 : std::vector<std::unique_ptr<HIR::Type>> inputs;
50 62 : for (auto ¶m : fn.get_params ())
51 : {
52 32 : HIR::Type *hir_type = ASTLoweringType::translate (*param);
53 32 : inputs.push_back (std::unique_ptr<HIR::Type> (hir_type));
54 : }
55 :
56 30 : HIR::Type *result_type
57 30 : = fn.has_return_type () ? ASTLoweringType::translate (fn.get_return_type ())
58 : : nullptr;
59 :
60 30 : HIR::TypePathFunction function_path (std::move (inputs),
61 60 : std::unique_ptr<HIR::Type> (
62 30 : result_type));
63 :
64 30 : translated_segment = new HIR::TypePathSegmentFunction (
65 30 : mapping, std::move (ident), segment.get_separating_scope_resolution (),
66 120 : std::move (function_path), segment.get_locus ());
67 30 : }
68 :
69 : void
70 53996 : ASTLowerTypePath::visit (AST::TypePathSegment &segment)
71 : {
72 53996 : auto crate_num = mappings.get_current_crate ();
73 53996 : auto hirid = mappings.get_next_hir_id (crate_num);
74 53996 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
75 53996 : UNKNOWN_LOCAL_DEFID);
76 :
77 53996 : if (segment.is_lang_item ())
78 : {
79 339 : translated_segment = new HIR::TypePathSegment (std::move (mapping),
80 : segment.get_lang_item (),
81 339 : segment.get_locus ());
82 : }
83 : else
84 : {
85 107314 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
86 53657 : translated_segment
87 53657 : = new HIR::TypePathSegment (std::move (mapping), ident,
88 53657 : segment.get_separating_scope_resolution (),
89 160971 : segment.get_locus ());
90 53657 : }
91 53996 : }
92 :
93 : void
94 2774 : ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
95 : {
96 2774 : std::vector<HIR::GenericArgsBinding> binding_args; // TODO
97 :
98 2774 : auto generic_args = lower_generic_args (segment.get_generic_args ());
99 :
100 2774 : auto crate_num = mappings.get_current_crate ();
101 2774 : auto hirid = mappings.get_next_hir_id (crate_num);
102 2774 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
103 2774 : UNKNOWN_LOCAL_DEFID);
104 :
105 2774 : if (segment.is_lang_item ())
106 : {
107 49 : translated_segment
108 49 : = new HIR::TypePathSegmentGeneric (std::move (mapping),
109 : segment.get_lang_item (),
110 49 : generic_args, segment.get_locus ());
111 : }
112 : else
113 : {
114 2725 : std::string segment_name = segment.get_ident_segment ().as_string ();
115 2725 : bool has_separating_scope_resolution
116 2725 : = segment.get_separating_scope_resolution ();
117 :
118 2725 : translated_segment
119 5450 : = new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name,
120 : has_separating_scope_resolution,
121 8175 : generic_args, segment.get_locus ());
122 2725 : }
123 2774 : }
124 :
125 : void
126 54704 : ASTLowerTypePath::visit (AST::TypePath &path)
127 : {
128 54704 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
129 :
130 111262 : for (auto &seg : path.get_segments ())
131 : {
132 56558 : translated_segment = nullptr;
133 56558 : seg->accept_vis (*this);
134 56558 : rust_assert (translated_segment != nullptr);
135 :
136 113116 : translated_segments.push_back (
137 56558 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
138 : }
139 :
140 54704 : auto crate_num = mappings.get_current_crate ();
141 54704 : auto hirid = mappings.get_next_hir_id (crate_num);
142 109408 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
143 54704 : mappings.get_next_localdef_id (crate_num));
144 :
145 54704 : translated
146 54704 : = new HIR::TypePath (std::move (mapping), std::move (translated_segments),
147 : path.get_locus (),
148 54704 : path.has_opening_scope_resolution_op ());
149 54704 : }
150 :
151 : HIR::QualifiedPathInType *
152 242 : ASTLowerQualifiedPathInType::translate (AST::QualifiedPathInType &type)
153 : {
154 242 : ASTLowerQualifiedPathInType resolver;
155 242 : type.accept_vis (resolver);
156 242 : rust_assert (resolver.translated != nullptr);
157 242 : return resolver.translated;
158 242 : }
159 :
160 : void
161 242 : ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path)
162 : {
163 242 : auto crate_num = mappings.get_current_crate ();
164 242 : auto hirid = mappings.get_next_hir_id (crate_num);
165 242 : Analysis::NodeMapping qual_mappings (
166 242 : crate_num, path.get_qualified_path_type ().get_node_id (), hirid,
167 242 : UNKNOWN_LOCAL_DEFID);
168 :
169 242 : HIR::Type *qual_type
170 242 : = ASTLoweringType::translate (path.get_qualified_path_type ().get_type ());
171 :
172 242 : HIR::TypePath *qual_trait = nullptr;
173 242 : if (!path.get_qualified_path_type ().is_error ())
174 : {
175 242 : AST::QualifiedPathType &qualifier = path.get_qualified_path_type ();
176 242 : if (qualifier.has_as_clause ())
177 241 : qual_trait
178 241 : = ASTLowerTypePath::translate (qualifier.get_as_type_path ());
179 : }
180 :
181 242 : HIR::QualifiedPathType qual_path_type (
182 242 : qual_mappings, std::unique_ptr<HIR::Type> (qual_type),
183 484 : std::unique_ptr<HIR::TypePath> (qual_trait),
184 242 : path.get_qualified_path_type ().get_locus ());
185 :
186 242 : translated_segment = nullptr;
187 242 : path.get_associated_segment ()->accept_vis (*this);
188 242 : rust_assert (translated_segment != nullptr);
189 :
190 242 : std::unique_ptr<HIR::TypePathSegment> associated_segment (translated_segment);
191 :
192 242 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
193 242 : for (auto &seg : path.get_segments ())
194 : {
195 0 : translated_segment = nullptr;
196 0 : seg->accept_vis (*this);
197 0 : rust_assert (translated_segment != nullptr);
198 :
199 0 : translated_segments.push_back (
200 0 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
201 : }
202 :
203 484 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
204 242 : mappings.get_next_localdef_id (crate_num));
205 242 : translated = new HIR::QualifiedPathInType (std::move (mapping),
206 : std::move (qual_path_type),
207 : std::move (associated_segment),
208 : std::move (translated_segments),
209 484 : path.get_locus ());
210 242 : }
211 :
212 61763 : ASTLoweringType::ASTLoweringType (bool default_to_static_lifetime,
213 61763 : bool impl_trait_allowed)
214 61763 : : ASTLoweringBase (), default_to_static_lifetime (default_to_static_lifetime),
215 61763 : impl_trait_allowed (impl_trait_allowed), translated (nullptr)
216 61763 : {}
217 :
218 : HIR::Type *
219 61763 : ASTLoweringType::translate (AST::Type &type, bool default_to_static_lifetime,
220 : bool impl_trait_allowed)
221 : {
222 61763 : ASTLoweringType resolver (default_to_static_lifetime, impl_trait_allowed);
223 61763 : type.accept_vis (resolver);
224 :
225 61763 : rust_assert (resolver.translated != nullptr);
226 61763 : resolver.mappings.insert_hir_type (resolver.translated);
227 61763 : resolver.mappings.insert_location (
228 61763 : resolver.translated->get_mappings ().get_hirid (),
229 61763 : resolver.translated->get_locus ());
230 :
231 61763 : return resolver.translated;
232 61763 : }
233 :
234 : void
235 65 : ASTLoweringType::visit (AST::BareFunctionType &fntype)
236 : {
237 65 : bool is_variadic = false;
238 65 : std::vector<HIR::LifetimeParam> lifetime_params;
239 69 : for (auto &lifetime_param : fntype.get_for_lifetimes ())
240 : {
241 4 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
242 4 : lifetime_params.push_back (
243 : *static_cast<HIR::LifetimeParam *> (generic_param));
244 : }
245 :
246 65 : HIR::FunctionQualifiers qualifiers
247 65 : = lower_qualifiers (fntype.get_function_qualifiers ());
248 :
249 65 : std::vector<HIR::MaybeNamedParam> named_params;
250 115 : for (auto ¶m : fntype.get_function_params ())
251 : {
252 50 : HIR::MaybeNamedParam::ParamKind kind;
253 50 : switch (param.get_param_kind ())
254 : {
255 : case AST::MaybeNamedParam::ParamKind::UNNAMED:
256 : kind = HIR::MaybeNamedParam::ParamKind::UNNAMED;
257 : break;
258 : case AST::MaybeNamedParam::ParamKind::IDENTIFIER:
259 : kind = HIR::MaybeNamedParam::ParamKind::IDENTIFIER;
260 : break;
261 : case AST::MaybeNamedParam::ParamKind::WILDCARD:
262 : kind = HIR::MaybeNamedParam::ParamKind::WILDCARD;
263 : break;
264 0 : default:
265 0 : rust_unreachable ();
266 : }
267 :
268 50 : HIR::Type *param_type
269 50 : = ASTLoweringType::translate (param.get_type (),
270 50 : default_to_static_lifetime,
271 50 : impl_trait_allowed);
272 :
273 50 : HIR::MaybeNamedParam p (param.get_name (), kind,
274 100 : std::unique_ptr<HIR::Type> (param_type),
275 100 : param.get_locus ());
276 50 : named_params.push_back (std::move (p));
277 : }
278 :
279 65 : HIR::Type *return_type = nullptr;
280 65 : if (fntype.has_return_type ())
281 : {
282 47 : return_type = ASTLoweringType::translate (fntype.get_return_type (),
283 47 : default_to_static_lifetime,
284 47 : impl_trait_allowed);
285 : }
286 :
287 65 : auto crate_num = mappings.get_current_crate ();
288 130 : Analysis::NodeMapping mapping (crate_num, fntype.get_node_id (),
289 65 : mappings.get_next_hir_id (crate_num),
290 65 : mappings.get_next_localdef_id (crate_num));
291 :
292 130 : translated = new HIR::BareFunctionType (
293 : std::move (mapping), std::move (lifetime_params), std::move (qualifiers),
294 : std::move (named_params), is_variadic,
295 65 : std::unique_ptr<HIR::Type> (return_type), fntype.get_locus ());
296 65 : }
297 :
298 : void
299 373 : ASTLoweringType::visit (AST::TupleType &tuple)
300 : {
301 373 : std::vector<std::unique_ptr<HIR::Type>> elems;
302 942 : for (auto &e : tuple.get_elems ())
303 : {
304 1138 : HIR::Type *t = ASTLoweringType::translate (*e, default_to_static_lifetime,
305 569 : impl_trait_allowed);
306 569 : elems.push_back (std::unique_ptr<HIR::Type> (t));
307 : }
308 :
309 373 : auto crate_num = mappings.get_current_crate ();
310 746 : Analysis::NodeMapping mapping (crate_num, tuple.get_node_id (),
311 373 : mappings.get_next_hir_id (crate_num),
312 373 : mappings.get_next_localdef_id (crate_num));
313 :
314 746 : translated = new HIR::TupleType (std::move (mapping), std::move (elems),
315 373 : tuple.get_locus ());
316 373 : }
317 :
318 : void
319 47779 : ASTLoweringType::visit (AST::TypePath &path)
320 : {
321 47779 : translated = ASTLowerTypePath::translate (path);
322 47779 : }
323 :
324 : void
325 242 : ASTLoweringType::visit (AST::QualifiedPathInType &path)
326 : {
327 242 : translated = ASTLowerQualifiedPathInType::translate (path);
328 242 : }
329 :
330 : void
331 674 : ASTLoweringType::visit (AST::ArrayType &type)
332 : {
333 674 : HIR::Type *translated_type
334 674 : = ASTLoweringType::translate (type.get_elem_type (),
335 674 : default_to_static_lifetime,
336 674 : impl_trait_allowed);
337 674 : HIR::Expr *array_size = ASTLoweringExpr::translate (type.get_size_expr ());
338 :
339 674 : auto crate_num = mappings.get_current_crate ();
340 1348 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
341 674 : mappings.get_next_hir_id (crate_num),
342 674 : mappings.get_next_localdef_id (crate_num));
343 :
344 674 : translated
345 674 : = new HIR::ArrayType (mapping, std::unique_ptr<HIR::Type> (translated_type),
346 1348 : std::unique_ptr<HIR::Expr> (array_size),
347 674 : type.get_locus ());
348 674 : }
349 :
350 : void
351 4432 : ASTLoweringType::visit (AST::ReferenceType &type)
352 : {
353 4432 : HIR::Lifetime lifetime
354 4432 : = lower_lifetime (type.get_lifetime (), default_to_static_lifetime);
355 :
356 8864 : HIR::Type *base_type = ASTLoweringType::translate (type.get_base_type (),
357 4432 : default_to_static_lifetime,
358 4432 : impl_trait_allowed);
359 :
360 4432 : auto crate_num = mappings.get_current_crate ();
361 8864 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
362 4432 : mappings.get_next_hir_id (crate_num),
363 4432 : mappings.get_next_localdef_id (crate_num));
364 :
365 8864 : translated = new HIR::ReferenceType (mapping,
366 4432 : type.get_has_mut () ? Mutability::Mut
367 : : Mutability::Imm,
368 8864 : std::unique_ptr<HIR::Type> (base_type),
369 12990 : type.get_locus (), lifetime);
370 4432 : }
371 :
372 : void
373 6667 : ASTLoweringType::visit (AST::RawPointerType &type)
374 : {
375 6667 : HIR::Type *base_type
376 6667 : = ASTLoweringType::translate (type.get_type_pointed_to (),
377 6667 : default_to_static_lifetime,
378 6667 : impl_trait_allowed);
379 :
380 6667 : auto crate_num = mappings.get_current_crate ();
381 13334 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
382 6667 : mappings.get_next_hir_id (crate_num),
383 6667 : mappings.get_next_localdef_id (crate_num));
384 :
385 6667 : translated
386 6667 : = new HIR::RawPointerType (mapping,
387 6667 : type.get_pointer_type ()
388 : == AST::RawPointerType::PointerType::MUT
389 : ? Mutability::Mut
390 : : Mutability::Imm,
391 13334 : std::unique_ptr<HIR::Type> (base_type),
392 12598 : type.get_locus ());
393 6667 : }
394 :
395 : void
396 877 : ASTLoweringType::visit (AST::SliceType &type)
397 : {
398 877 : HIR::Type *base_type = ASTLoweringType::translate (type.get_elem_type (),
399 877 : default_to_static_lifetime,
400 877 : impl_trait_allowed);
401 :
402 877 : auto crate_num = mappings.get_current_crate ();
403 1754 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
404 877 : mappings.get_next_hir_id (crate_num),
405 877 : mappings.get_next_localdef_id (crate_num));
406 :
407 877 : translated
408 877 : = new HIR::SliceType (mapping, std::unique_ptr<HIR::Type> (base_type),
409 877 : type.get_locus ());
410 877 : }
411 :
412 : void
413 211 : ASTLoweringType::visit (AST::InferredType &type)
414 : {
415 211 : auto crate_num = mappings.get_current_crate ();
416 422 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
417 211 : mappings.get_next_hir_id (crate_num),
418 211 : mappings.get_next_localdef_id (crate_num));
419 :
420 211 : translated = new HIR::InferredType (mapping, type.get_locus ());
421 211 : }
422 :
423 : void
424 189 : ASTLoweringType::visit (AST::NeverType &type)
425 : {
426 189 : auto crate_num = mappings.get_current_crate ();
427 378 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
428 189 : mappings.get_next_hir_id (crate_num),
429 189 : mappings.get_next_localdef_id (crate_num));
430 :
431 189 : translated = new HIR::NeverType (mapping, type.get_locus ());
432 189 : }
433 :
434 : void
435 205 : ASTLoweringType::visit (AST::TraitObjectTypeOneBound &type)
436 : {
437 205 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
438 205 : HIR::TypeParamBound *translated_bound
439 205 : = ASTLoweringTypeBounds::translate (type.get_trait_bound ());
440 205 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (translated_bound));
441 :
442 205 : auto crate_num = mappings.get_current_crate ();
443 410 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
444 205 : mappings.get_next_hir_id (crate_num),
445 205 : mappings.get_next_localdef_id (crate_num));
446 :
447 410 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
448 205 : type.get_locus (), type.is_dyn ());
449 205 : }
450 :
451 : void
452 11 : ASTLoweringType::visit (AST::TraitObjectType &type)
453 : {
454 11 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
455 :
456 36 : for (auto &bound : type.get_type_param_bounds ())
457 : {
458 25 : HIR::TypeParamBound *translated_bound
459 25 : = ASTLoweringTypeBounds::translate (*bound);
460 25 : bounds.push_back (
461 25 : std::unique_ptr<HIR::TypeParamBound> (translated_bound));
462 : }
463 :
464 11 : auto crate_num = mappings.get_current_crate ();
465 22 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
466 11 : mappings.get_next_hir_id (crate_num),
467 11 : mappings.get_next_localdef_id (crate_num));
468 :
469 22 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
470 11 : type.get_locus (), type.is_dyn ());
471 11 : }
472 :
473 : void
474 6 : ASTLoweringType::visit (AST::ParenthesisedType &type)
475 : {
476 6 : auto *inner = ASTLoweringType::translate (*type.get_type_in_parens (),
477 6 : default_to_static_lifetime,
478 6 : impl_trait_allowed);
479 :
480 6 : auto crate_num = mappings.get_current_crate ();
481 12 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
482 6 : mappings.get_next_hir_id (crate_num),
483 6 : mappings.get_next_localdef_id (crate_num));
484 :
485 : // FIXME: Do we actually need to know if a type is parenthesized in the HIR?
486 : // or can we just use the type in parens?
487 6 : translated
488 6 : = new HIR::ParenthesisedType (mapping, std::unique_ptr<HIR::Type> (inner),
489 6 : type.get_locus ());
490 6 : }
491 :
492 : void
493 0 : ASTLoweringType::visit (AST::ImplTraitType &type)
494 : {
495 0 : if (!impl_trait_allowed)
496 0 : emit_impl_trait_error (type.get_locus ());
497 :
498 0 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
499 0 : for (auto &bound : type.get_type_param_bounds ())
500 : {
501 0 : auto b = ASTLoweringTypeBounds::translate (*bound.get ());
502 0 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
503 : }
504 :
505 0 : auto crate_num = mappings.get_current_crate ();
506 0 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
507 0 : mappings.get_next_hir_id (crate_num),
508 0 : mappings.get_next_localdef_id (crate_num));
509 :
510 0 : translated
511 0 : = new HIR::ImplTraitType (mapping, std::move (bounds), type.get_locus ());
512 0 : }
513 :
514 : void
515 32 : ASTLoweringType::visit (AST::ImplTraitTypeOneBound &type)
516 : {
517 32 : if (!impl_trait_allowed)
518 3 : emit_impl_trait_error (type.get_locus ());
519 :
520 32 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
521 :
522 32 : auto b = ASTLoweringTypeBounds::translate (*type.get_trait_bound ().get ());
523 32 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
524 :
525 32 : auto crate_num = mappings.get_current_crate ();
526 64 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
527 32 : mappings.get_next_hir_id (crate_num),
528 32 : mappings.get_next_localdef_id (crate_num));
529 :
530 32 : translated
531 32 : = new HIR::ImplTraitType (mapping, std::move (bounds), type.get_locus ());
532 32 : }
533 :
534 : void
535 3 : ASTLoweringType::emit_impl_trait_error (location_t locus)
536 : {
537 3 : rich_location r (line_table, locus);
538 3 : rust_error_at (r, ErrorCode::E0562,
539 : "%<impl Trait%> not allowed outside of function and inherent "
540 : "method return types");
541 3 : }
542 :
543 : HIR::GenericParam *
544 8831 : ASTLowerGenericParam::translate (AST::GenericParam ¶m)
545 : {
546 8831 : ASTLowerGenericParam resolver;
547 8831 : param.accept_vis (resolver);
548 :
549 8831 : rust_assert (resolver.translated != nullptr);
550 8831 : resolver.mappings.insert_location (
551 8831 : resolver.translated->get_mappings ().get_hirid (), param.get_locus ());
552 8831 : resolver.mappings.insert_hir_generic_param (resolver.translated);
553 :
554 8831 : return resolver.translated;
555 8831 : }
556 :
557 : void
558 197 : ASTLowerGenericParam::visit (AST::LifetimeParam ¶m)
559 : {
560 197 : auto crate_num = mappings.get_current_crate ();
561 197 : AST::Lifetime lifetime = param.get_lifetime ();
562 197 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
563 197 : mappings.get_next_hir_id (crate_num),
564 197 : mappings.get_next_localdef_id (crate_num));
565 :
566 197 : HIR::Lifetime lt (mapping, lifetime.get_lifetime_type (),
567 394 : lifetime.get_lifetime_name (), lifetime.get_locus ());
568 :
569 197 : translated = new HIR::LifetimeParam (mapping, lt, param.get_locus (),
570 197 : std::vector<Lifetime> (),
571 394 : param.get_outer_attrs ());
572 197 : }
573 :
574 : void
575 91 : ASTLowerGenericParam::visit (AST::ConstGenericParam ¶m)
576 : {
577 91 : auto crate_num = mappings.get_current_crate ();
578 91 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
579 91 : mappings.get_next_hir_id (crate_num),
580 91 : mappings.get_next_localdef_id (crate_num));
581 :
582 91 : auto type = ASTLoweringType::translate (param.get_type ());
583 :
584 91 : HIR::Expr *default_expr = nullptr;
585 91 : if (param.has_default_value ())
586 15 : default_expr = ASTLoweringExpr::translate (
587 15 : param.get_default_value_unchecked ().get_expression ());
588 :
589 91 : translated = new HIR::ConstGenericParam (param.get_name ().as_string (),
590 273 : std::unique_ptr<Type> (type),
591 182 : std::unique_ptr<Expr> (default_expr),
592 273 : mapping, param.get_locus ());
593 91 : }
594 :
595 : void
596 8543 : ASTLowerGenericParam::visit (AST::TypeParam ¶m)
597 : {
598 8543 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
599 8543 : if (param.has_type_param_bounds ())
600 : {
601 1621 : for (auto &bound : param.get_type_param_bounds ())
602 : {
603 838 : HIR::TypeParamBound *lowered_bound = lower_bound (*bound);
604 838 : type_param_bounds.push_back (
605 838 : std::unique_ptr<HIR::TypeParamBound> (lowered_bound));
606 : }
607 : }
608 :
609 8543 : tl::optional<std::unique_ptr<HIR::Type>> type = tl::nullopt;
610 8543 : if (param.has_type ())
611 360 : type
612 360 : = tl::optional<std::unique_ptr<HIR::Type>> (std::unique_ptr<HIR::Type> (
613 360 : ASTLoweringType::translate (param.get_type ())));
614 :
615 8543 : auto crate_num = mappings.get_current_crate ();
616 8543 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
617 8543 : mappings.get_next_hir_id (crate_num),
618 8543 : mappings.get_next_localdef_id (crate_num));
619 :
620 8543 : translated
621 8543 : = new HIR::TypeParam (mapping, param.get_type_representation (),
622 : param.get_locus (), std::move (type_param_bounds),
623 8543 : std::move (type), param.get_outer_attrs (),
624 25989 : param.from_impl_trait ());
625 8543 : }
626 :
627 : HIR::TypeParamBound *
628 1887 : ASTLoweringTypeBounds::translate (AST::TypeParamBound &type)
629 : {
630 1887 : ASTLoweringTypeBounds resolver;
631 1887 : type.accept_vis (resolver);
632 :
633 1887 : rust_assert (resolver.translated != nullptr);
634 1887 : resolver.mappings.insert_location (
635 1887 : resolver.translated->get_mappings ().get_hirid (),
636 1887 : resolver.translated->get_locus ());
637 :
638 1887 : return resolver.translated;
639 1887 : }
640 :
641 : void
642 1875 : ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
643 : {
644 1875 : std::vector<HIR::LifetimeParam> for_lifetimes;
645 1882 : for (auto &lifetime_param : bound.get_for_lifetimes ())
646 : {
647 7 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
648 7 : for_lifetimes.push_back (
649 : *static_cast<HIR::LifetimeParam *> (generic_param));
650 : }
651 :
652 1875 : AST::TypePath &ast_trait_path = bound.get_type_path ();
653 1875 : HIR::TypePath *trait_path = ASTLowerTypePath::translate (ast_trait_path);
654 :
655 1875 : auto crate_num = mappings.get_current_crate ();
656 1875 : Analysis::NodeMapping mapping (crate_num, bound.get_node_id (),
657 1875 : mappings.get_next_hir_id (crate_num),
658 1875 : UNKNOWN_LOCAL_DEFID);
659 :
660 1875 : BoundPolarity polarity = bound.has_opening_question_mark ()
661 1875 : ? BoundPolarity::AntiBound
662 1512 : : BoundPolarity::RegularBound;
663 1875 : translated
664 1875 : = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
665 3750 : bound.is_in_parens (), polarity, for_lifetimes);
666 1875 : }
667 :
668 : void
669 12 : ASTLoweringTypeBounds::visit (AST::Lifetime &bound)
670 : {
671 12 : HIR::Lifetime lifetime = lower_lifetime (bound);
672 12 : translated = new HIR::Lifetime (lifetime);
673 12 : }
674 :
675 : HIR::WhereClauseItem *
676 145 : ASTLowerWhereClauseItem::translate (AST::WhereClauseItem &item)
677 : {
678 145 : ASTLowerWhereClauseItem compiler;
679 145 : item.accept_vis (compiler);
680 :
681 145 : rust_assert (compiler.translated != nullptr);
682 : // FIXME
683 : // compiler.mappings.insert_location (
684 : // compiler.translated->get_mappings ().get_hirid (),
685 : // compiler.translated->get_locus ());
686 :
687 145 : return compiler.translated;
688 145 : }
689 :
690 : void
691 2 : ASTLowerWhereClauseItem::visit (AST::LifetimeWhereClauseItem &item)
692 : {
693 2 : HIR::Lifetime l = lower_lifetime (item.get_lifetime ());
694 2 : std::vector<HIR::Lifetime> lifetime_bounds;
695 4 : for (auto &lifetime_bound : item.get_lifetime_bounds ())
696 : {
697 2 : HIR::Lifetime ll = lower_lifetime (lifetime_bound);
698 2 : lifetime_bounds.push_back (std::move (ll));
699 2 : }
700 :
701 2 : auto crate_num = mappings.get_current_crate ();
702 2 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
703 2 : mappings.get_next_hir_id (crate_num),
704 2 : UNKNOWN_LOCAL_DEFID);
705 :
706 2 : translated = new HIR::LifetimeWhereClauseItem (mapping, std::move (l),
707 : std::move (lifetime_bounds),
708 4 : item.get_locus ());
709 2 : }
710 :
711 : void
712 143 : ASTLowerWhereClauseItem::visit (AST::TypeBoundWhereClauseItem &item)
713 : {
714 : // FIXME
715 143 : std::vector<HIR::LifetimeParam> for_lifetimes;
716 :
717 152 : for (auto &lifetime_param : item.get_for_lifetimes ())
718 : {
719 9 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
720 9 : for_lifetimes.push_back (
721 : *static_cast<HIR::LifetimeParam *> (generic_param));
722 : }
723 :
724 143 : std::unique_ptr<HIR::Type> bound_type = std::unique_ptr<HIR::Type> (
725 143 : ASTLoweringType::translate (item.get_type ()));
726 :
727 143 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
728 286 : for (auto &bound : item.get_type_param_bounds ())
729 : {
730 143 : HIR::TypeParamBound *b = ASTLoweringTypeBounds::translate (*bound);
731 143 : type_param_bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
732 : }
733 :
734 143 : auto crate_num = mappings.get_current_crate ();
735 143 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
736 143 : mappings.get_next_hir_id (crate_num),
737 143 : UNKNOWN_LOCAL_DEFID);
738 :
739 143 : translated
740 143 : = new HIR::TypeBoundWhereClauseItem (mapping, std::move (for_lifetimes),
741 : std::move (bound_type),
742 : std::move (type_param_bounds),
743 143 : item.get_locus ());
744 143 : }
745 :
746 : } // namespace HIR
747 : } // namespace Rust
|