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 53465 : ASTLowerTypePath::translate (AST::TypePath &type)
31 : {
32 53465 : ASTLowerTypePath resolver;
33 53465 : type.accept_vis (resolver);
34 53465 : rust_assert (resolver.translated != nullptr);
35 53465 : return resolver.translated;
36 53465 : }
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 52770 : ASTLowerTypePath::visit (AST::TypePathSegment &segment)
71 : {
72 52770 : auto crate_num = mappings.get_current_crate ();
73 52770 : auto hirid = mappings.get_next_hir_id (crate_num);
74 52770 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
75 52770 : UNKNOWN_LOCAL_DEFID);
76 :
77 52770 : 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 104862 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
86 52431 : translated_segment
87 52431 : = new HIR::TypePathSegment (std::move (mapping), ident,
88 52431 : segment.get_separating_scope_resolution (),
89 157293 : segment.get_locus ());
90 52431 : }
91 52770 : }
92 :
93 : void
94 2736 : ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
95 : {
96 2736 : std::vector<HIR::GenericArgsBinding> binding_args; // TODO
97 :
98 2736 : auto generic_args = lower_generic_args (segment.get_generic_args ());
99 :
100 2736 : auto crate_num = mappings.get_current_crate ();
101 2736 : auto hirid = mappings.get_next_hir_id (crate_num);
102 2736 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
103 2736 : UNKNOWN_LOCAL_DEFID);
104 :
105 2736 : 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 2687 : std::string segment_name = segment.get_ident_segment ().as_string ();
115 2687 : bool has_separating_scope_resolution
116 2687 : = segment.get_separating_scope_resolution ();
117 :
118 2687 : translated_segment
119 5374 : = new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name,
120 : has_separating_scope_resolution,
121 8061 : generic_args, segment.get_locus ());
122 2687 : }
123 2736 : }
124 :
125 : void
126 53465 : ASTLowerTypePath::visit (AST::TypePath &path)
127 : {
128 53465 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
129 :
130 108759 : for (auto &seg : path.get_segments ())
131 : {
132 55294 : translated_segment = nullptr;
133 55294 : seg->accept_vis (*this);
134 55294 : rust_assert (translated_segment != nullptr);
135 :
136 110588 : translated_segments.push_back (
137 55294 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
138 : }
139 :
140 53465 : auto crate_num = mappings.get_current_crate ();
141 53465 : auto hirid = mappings.get_next_hir_id (crate_num);
142 106930 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
143 53465 : mappings.get_next_localdef_id (crate_num));
144 :
145 53465 : translated
146 53465 : = new HIR::TypePath (std::move (mapping), std::move (translated_segments),
147 : path.get_locus (),
148 53465 : path.has_opening_scope_resolution_op ());
149 53465 : }
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 60165 : ASTLoweringType::ASTLoweringType (bool default_to_static_lifetime,
213 60165 : bool impl_trait_allowed)
214 60165 : : ASTLoweringBase (), default_to_static_lifetime (default_to_static_lifetime),
215 60165 : impl_trait_allowed (impl_trait_allowed), translated (nullptr)
216 60165 : {}
217 :
218 : HIR::Type *
219 60165 : ASTLoweringType::translate (AST::Type &type, bool default_to_static_lifetime,
220 : bool impl_trait_allowed)
221 : {
222 60165 : ASTLoweringType resolver (default_to_static_lifetime, impl_trait_allowed);
223 60165 : type.accept_vis (resolver);
224 :
225 60165 : rust_assert (resolver.translated != nullptr);
226 60165 : resolver.mappings.insert_hir_type (resolver.translated);
227 60165 : resolver.mappings.insert_location (
228 60165 : resolver.translated->get_mappings ().get_hirid (),
229 60165 : resolver.translated->get_locus ());
230 :
231 60165 : return resolver.translated;
232 60165 : }
233 :
234 : void
235 62 : ASTLoweringType::visit (AST::BareFunctionType &fntype)
236 : {
237 62 : bool is_variadic = false;
238 62 : std::vector<HIR::LifetimeParam> lifetime_params;
239 65 : for (auto &lifetime_param : fntype.get_for_lifetimes ())
240 : {
241 3 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
242 3 : lifetime_params.push_back (
243 : *static_cast<HIR::LifetimeParam *> (generic_param));
244 : }
245 :
246 62 : HIR::FunctionQualifiers qualifiers
247 62 : = lower_qualifiers (fntype.get_function_qualifiers ());
248 :
249 62 : std::vector<HIR::MaybeNamedParam> named_params;
250 108 : for (auto ¶m : fntype.get_function_params ())
251 : {
252 46 : HIR::MaybeNamedParam::ParamKind kind;
253 46 : 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 46 : HIR::Type *param_type
269 46 : = ASTLoweringType::translate (param.get_type (),
270 46 : default_to_static_lifetime,
271 46 : impl_trait_allowed);
272 :
273 46 : HIR::MaybeNamedParam p (param.get_name (), kind,
274 92 : std::unique_ptr<HIR::Type> (param_type),
275 92 : param.get_locus ());
276 46 : named_params.push_back (std::move (p));
277 : }
278 :
279 62 : HIR::Type *return_type = nullptr;
280 62 : 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 62 : auto crate_num = mappings.get_current_crate ();
288 124 : Analysis::NodeMapping mapping (crate_num, fntype.get_node_id (),
289 62 : mappings.get_next_hir_id (crate_num),
290 62 : mappings.get_next_localdef_id (crate_num));
291 :
292 124 : translated = new HIR::BareFunctionType (
293 : std::move (mapping), std::move (lifetime_params), std::move (qualifiers),
294 : std::move (named_params), is_variadic,
295 62 : std::unique_ptr<HIR::Type> (return_type), fntype.get_locus ());
296 62 : }
297 :
298 : void
299 361 : ASTLoweringType::visit (AST::TupleType &tuple)
300 : {
301 361 : std::vector<std::unique_ptr<HIR::Type>> elems;
302 926 : for (auto &e : tuple.get_elems ())
303 : {
304 1130 : HIR::Type *t = ASTLoweringType::translate (*e, default_to_static_lifetime,
305 565 : impl_trait_allowed);
306 565 : elems.push_back (std::unique_ptr<HIR::Type> (t));
307 : }
308 :
309 361 : auto crate_num = mappings.get_current_crate ();
310 722 : Analysis::NodeMapping mapping (crate_num, tuple.get_node_id (),
311 361 : mappings.get_next_hir_id (crate_num),
312 361 : mappings.get_next_localdef_id (crate_num));
313 :
314 722 : translated = new HIR::TupleType (std::move (mapping), std::move (elems),
315 361 : tuple.get_locus ());
316 361 : }
317 :
318 : void
319 46671 : ASTLoweringType::visit (AST::TypePath &path)
320 : {
321 46671 : translated = ASTLowerTypePath::translate (path);
322 46671 : }
323 :
324 : void
325 242 : ASTLoweringType::visit (AST::QualifiedPathInType &path)
326 : {
327 242 : translated = ASTLowerQualifiedPathInType::translate (path);
328 242 : }
329 :
330 : void
331 663 : ASTLoweringType::visit (AST::ArrayType &type)
332 : {
333 663 : HIR::Type *translated_type
334 663 : = ASTLoweringType::translate (type.get_elem_type (),
335 663 : default_to_static_lifetime,
336 663 : impl_trait_allowed);
337 663 : HIR::Expr *array_size = ASTLoweringExpr::translate (type.get_size_expr ());
338 :
339 663 : auto crate_num = mappings.get_current_crate ();
340 1326 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
341 663 : mappings.get_next_hir_id (crate_num),
342 663 : mappings.get_next_localdef_id (crate_num));
343 :
344 663 : translated
345 663 : = new HIR::ArrayType (mapping, std::unique_ptr<HIR::Type> (translated_type),
346 1326 : std::unique_ptr<HIR::Expr> (array_size),
347 663 : type.get_locus ());
348 663 : }
349 :
350 : void
351 4379 : ASTLoweringType::visit (AST::ReferenceType &type)
352 : {
353 4379 : HIR::Lifetime lifetime
354 4379 : = lower_lifetime (type.get_lifetime (), default_to_static_lifetime);
355 :
356 8758 : HIR::Type *base_type = ASTLoweringType::translate (type.get_base_type (),
357 4379 : default_to_static_lifetime,
358 4379 : impl_trait_allowed);
359 :
360 4379 : auto crate_num = mappings.get_current_crate ();
361 8758 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
362 4379 : mappings.get_next_hir_id (crate_num),
363 4379 : mappings.get_next_localdef_id (crate_num));
364 :
365 8758 : translated = new HIR::ReferenceType (mapping,
366 4379 : type.get_has_mut () ? Mutability::Mut
367 : : Mutability::Imm,
368 8758 : std::unique_ptr<HIR::Type> (base_type),
369 12830 : type.get_locus (), lifetime);
370 4379 : }
371 :
372 : void
373 6488 : ASTLoweringType::visit (AST::RawPointerType &type)
374 : {
375 6488 : HIR::Type *base_type
376 6488 : = ASTLoweringType::translate (type.get_type_pointed_to (),
377 6488 : default_to_static_lifetime,
378 6488 : impl_trait_allowed);
379 :
380 6488 : auto crate_num = mappings.get_current_crate ();
381 12976 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
382 6488 : mappings.get_next_hir_id (crate_num),
383 6488 : mappings.get_next_localdef_id (crate_num));
384 :
385 6488 : translated
386 6488 : = new HIR::RawPointerType (mapping,
387 6488 : type.get_pointer_type ()
388 : == AST::RawPointerType::PointerType::MUT
389 : ? Mutability::Mut
390 : : Mutability::Imm,
391 12976 : std::unique_ptr<HIR::Type> (base_type),
392 12255 : type.get_locus ());
393 6488 : }
394 :
395 : void
396 841 : ASTLoweringType::visit (AST::SliceType &type)
397 : {
398 841 : HIR::Type *base_type = ASTLoweringType::translate (type.get_elem_type (),
399 841 : default_to_static_lifetime,
400 841 : impl_trait_allowed);
401 :
402 841 : auto crate_num = mappings.get_current_crate ();
403 1682 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
404 841 : mappings.get_next_hir_id (crate_num),
405 841 : mappings.get_next_localdef_id (crate_num));
406 :
407 841 : translated
408 841 : = new HIR::SliceType (mapping, std::unique_ptr<HIR::Type> (base_type),
409 841 : type.get_locus ());
410 841 : }
411 :
412 : void
413 206 : ASTLoweringType::visit (AST::InferredType &type)
414 : {
415 206 : auto crate_num = mappings.get_current_crate ();
416 412 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
417 206 : mappings.get_next_hir_id (crate_num),
418 206 : mappings.get_next_localdef_id (crate_num));
419 :
420 206 : translated = new HIR::InferredType (mapping, type.get_locus ());
421 206 : }
422 :
423 : void
424 47 : ASTLoweringType::visit (AST::NeverType &type)
425 : {
426 47 : auto crate_num = mappings.get_current_crate ();
427 94 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
428 47 : mappings.get_next_hir_id (crate_num),
429 47 : mappings.get_next_localdef_id (crate_num));
430 :
431 47 : translated = new HIR::NeverType (mapping, type.get_locus ());
432 47 : }
433 :
434 : void
435 156 : ASTLoweringType::visit (AST::TraitObjectTypeOneBound &type)
436 : {
437 156 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
438 156 : HIR::TypeParamBound *translated_bound
439 156 : = ASTLoweringTypeBounds::translate (type.get_trait_bound ());
440 156 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (translated_bound));
441 :
442 156 : auto crate_num = mappings.get_current_crate ();
443 312 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
444 156 : mappings.get_next_hir_id (crate_num),
445 156 : mappings.get_next_localdef_id (crate_num));
446 :
447 312 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
448 156 : type.get_locus (), type.is_dyn ());
449 156 : }
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 8369 : ASTLowerGenericParam::translate (AST::GenericParam ¶m)
545 : {
546 8369 : ASTLowerGenericParam resolver;
547 8369 : param.accept_vis (resolver);
548 :
549 8369 : rust_assert (resolver.translated != nullptr);
550 8369 : resolver.mappings.insert_location (
551 8369 : resolver.translated->get_mappings ().get_hirid (), param.get_locus ());
552 8369 : resolver.mappings.insert_hir_generic_param (resolver.translated);
553 :
554 8369 : return resolver.translated;
555 8369 : }
556 :
557 : void
558 207 : ASTLowerGenericParam::visit (AST::LifetimeParam ¶m)
559 : {
560 207 : auto crate_num = mappings.get_current_crate ();
561 207 : AST::Lifetime lifetime = param.get_lifetime ();
562 207 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
563 207 : mappings.get_next_hir_id (crate_num),
564 207 : mappings.get_next_localdef_id (crate_num));
565 :
566 207 : HIR::Lifetime lt (mapping, lifetime.get_lifetime_type (),
567 414 : lifetime.get_lifetime_name (), lifetime.get_locus ());
568 :
569 207 : translated = new HIR::LifetimeParam (mapping, lt, param.get_locus (),
570 207 : std::vector<Lifetime> (),
571 414 : param.get_outer_attrs ());
572 207 : }
573 :
574 : void
575 86 : ASTLowerGenericParam::visit (AST::ConstGenericParam ¶m)
576 : {
577 86 : auto crate_num = mappings.get_current_crate ();
578 86 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
579 86 : mappings.get_next_hir_id (crate_num),
580 86 : mappings.get_next_localdef_id (crate_num));
581 :
582 86 : auto type = ASTLoweringType::translate (param.get_type ());
583 :
584 86 : HIR::Expr *default_expr = nullptr;
585 86 : if (param.has_default_value ())
586 15 : default_expr = ASTLoweringExpr::translate (
587 15 : param.get_default_value_unchecked ().get_expression ());
588 :
589 86 : translated = new HIR::ConstGenericParam (param.get_name ().as_string (),
590 258 : std::unique_ptr<Type> (type),
591 172 : std::unique_ptr<Expr> (default_expr),
592 258 : mapping, param.get_locus ());
593 86 : }
594 :
595 : void
596 8076 : ASTLowerGenericParam::visit (AST::TypeParam ¶m)
597 : {
598 8076 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
599 8076 : if (param.has_type_param_bounds ())
600 : {
601 1602 : for (auto &bound : param.get_type_param_bounds ())
602 : {
603 828 : HIR::TypeParamBound *lowered_bound = lower_bound (*bound);
604 828 : type_param_bounds.push_back (
605 828 : std::unique_ptr<HIR::TypeParamBound> (lowered_bound));
606 : }
607 : }
608 :
609 8076 : tl::optional<std::unique_ptr<HIR::Type>> type = tl::nullopt;
610 8076 : if (param.has_type ())
611 359 : type
612 359 : = tl::optional<std::unique_ptr<HIR::Type>> (std::unique_ptr<HIR::Type> (
613 359 : ASTLoweringType::translate (param.get_type ())));
614 :
615 8076 : auto crate_num = mappings.get_current_crate ();
616 8076 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
617 8076 : mappings.get_next_hir_id (crate_num),
618 8076 : mappings.get_next_localdef_id (crate_num));
619 :
620 8076 : translated
621 8076 : = new HIR::TypeParam (mapping, param.get_type_representation (),
622 : param.get_locus (), std::move (type_param_bounds),
623 8076 : std::move (type), param.get_outer_attrs (),
624 24587 : param.from_impl_trait ());
625 8076 : }
626 :
627 : HIR::TypeParamBound *
628 1828 : ASTLoweringTypeBounds::translate (AST::TypeParamBound &type)
629 : {
630 1828 : ASTLoweringTypeBounds resolver;
631 1828 : type.accept_vis (resolver);
632 :
633 1828 : rust_assert (resolver.translated != nullptr);
634 1828 : resolver.mappings.insert_location (
635 1828 : resolver.translated->get_mappings ().get_hirid (),
636 1828 : resolver.translated->get_locus ());
637 :
638 1828 : return resolver.translated;
639 1828 : }
640 :
641 : void
642 1816 : ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
643 : {
644 1816 : std::vector<HIR::LifetimeParam> for_lifetimes;
645 1830 : for (auto &lifetime_param : bound.get_for_lifetimes ())
646 : {
647 14 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
648 14 : for_lifetimes.push_back (
649 : *static_cast<HIR::LifetimeParam *> (generic_param));
650 : }
651 :
652 1816 : AST::TypePath &ast_trait_path = bound.get_type_path ();
653 1816 : HIR::TypePath *trait_path = ASTLowerTypePath::translate (ast_trait_path);
654 :
655 1816 : auto crate_num = mappings.get_current_crate ();
656 1816 : Analysis::NodeMapping mapping (crate_num, bound.get_node_id (),
657 1816 : mappings.get_next_hir_id (crate_num),
658 1816 : UNKNOWN_LOCAL_DEFID);
659 :
660 1816 : BoundPolarity polarity = bound.has_opening_question_mark ()
661 1816 : ? BoundPolarity::AntiBound
662 1458 : : BoundPolarity::RegularBound;
663 1816 : translated
664 1816 : = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
665 3632 : bound.is_in_parens (), polarity, for_lifetimes);
666 1816 : }
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 143 : ASTLowerWhereClauseItem::translate (AST::WhereClauseItem &item)
677 : {
678 143 : ASTLowerWhereClauseItem compiler;
679 143 : item.accept_vis (compiler);
680 :
681 143 : 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 143 : return compiler.translated;
688 143 : }
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 141 : ASTLowerWhereClauseItem::visit (AST::TypeBoundWhereClauseItem &item)
713 : {
714 : // FIXME
715 141 : std::vector<HIR::LifetimeParam> for_lifetimes;
716 :
717 150 : 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 141 : std::unique_ptr<HIR::Type> bound_type = std::unique_ptr<HIR::Type> (
725 141 : ASTLoweringType::translate (item.get_type ()));
726 :
727 141 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
728 282 : for (auto &bound : item.get_type_param_bounds ())
729 : {
730 141 : HIR::TypeParamBound *b = ASTLoweringTypeBounds::translate (*bound);
731 141 : type_param_bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
732 : }
733 :
734 141 : auto crate_num = mappings.get_current_crate ();
735 141 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
736 141 : mappings.get_next_hir_id (crate_num),
737 141 : UNKNOWN_LOCAL_DEFID);
738 :
739 141 : translated
740 141 : = new HIR::TypeBoundWhereClauseItem (mapping, std::move (for_lifetimes),
741 : std::move (bound_type),
742 : std::move (type_param_bounds),
743 141 : item.get_locus ());
744 141 : }
745 :
746 : } // namespace HIR
747 : } // namespace Rust
|