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 55101 : ASTLowerTypePath::translate (AST::TypePath &type)
31 : {
32 55101 : ASTLowerTypePath resolver;
33 55101 : type.accept_vis (resolver);
34 55101 : rust_assert (resolver.translated != nullptr);
35 55101 : return resolver.translated;
36 55101 : }
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 : mapping, std::move (ident), segment.get_separating_scope_resolution (),
66 120 : std::move (function_path), segment.get_locus ());
67 30 : }
68 :
69 : void
70 54379 : ASTLowerTypePath::visit (AST::TypePathSegment &segment)
71 : {
72 54379 : auto crate_num = mappings.get_current_crate ();
73 54379 : auto hirid = mappings.get_next_hir_id (crate_num);
74 54379 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
75 54379 : UNKNOWN_LOCAL_DEFID);
76 :
77 54379 : if (segment.is_lang_item ())
78 : {
79 341 : translated_segment = new HIR::TypePathSegment (std::move (mapping),
80 : segment.get_lang_item (),
81 341 : segment.get_locus ());
82 : }
83 : else
84 : {
85 108076 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
86 54038 : translated_segment
87 54038 : = new HIR::TypePathSegment (std::move (mapping), ident,
88 : segment.get_separating_scope_resolution (),
89 162114 : segment.get_locus ());
90 54038 : }
91 54379 : }
92 :
93 : void
94 2790 : ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
95 : {
96 2790 : std::vector<HIR::GenericArgsBinding> binding_args; // TODO
97 :
98 2790 : auto generic_args = lower_generic_args (segment.get_generic_args ());
99 :
100 2790 : auto crate_num = mappings.get_current_crate ();
101 2790 : auto hirid = mappings.get_next_hir_id (crate_num);
102 2790 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
103 2790 : UNKNOWN_LOCAL_DEFID);
104 :
105 2790 : 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 2741 : std::string segment_name = segment.get_ident_segment ().as_string ();
115 2741 : bool has_separating_scope_resolution
116 2741 : = segment.get_separating_scope_resolution ();
117 :
118 2741 : translated_segment
119 5482 : = new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name,
120 : has_separating_scope_resolution,
121 8223 : generic_args, segment.get_locus ());
122 2741 : }
123 2790 : }
124 :
125 : void
126 55101 : ASTLowerTypePath::visit (AST::TypePath &path)
127 : {
128 55101 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
129 :
130 112058 : for (auto &seg : path.get_segments ())
131 : {
132 56957 : translated_segment = nullptr;
133 56957 : seg->accept_vis (*this);
134 56957 : rust_assert (translated_segment != nullptr);
135 :
136 113914 : translated_segments.push_back (
137 56957 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
138 : }
139 :
140 55101 : auto crate_num = mappings.get_current_crate ();
141 55101 : auto hirid = mappings.get_next_hir_id (crate_num);
142 110202 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
143 55101 : mappings.get_next_localdef_id (crate_num));
144 :
145 55101 : translated
146 55101 : = new HIR::TypePath (std::move (mapping), std::move (translated_segments),
147 : path.get_locus (),
148 55101 : path.has_opening_scope_resolution_op ());
149 55101 : }
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 62286 : ASTLoweringType::ASTLoweringType (bool default_to_static_lifetime,
213 : bool impl_trait_allowed)
214 62286 : : ASTLoweringBase (), default_to_static_lifetime (default_to_static_lifetime),
215 62286 : impl_trait_allowed (impl_trait_allowed), translated (nullptr)
216 62286 : {}
217 :
218 : HIR::Type *
219 62286 : ASTLoweringType::translate (AST::Type &type, bool default_to_static_lifetime,
220 : bool impl_trait_allowed)
221 : {
222 62286 : ASTLoweringType resolver (default_to_static_lifetime, impl_trait_allowed);
223 62286 : type.accept_vis (resolver);
224 :
225 62286 : rust_assert (resolver.translated != nullptr);
226 62286 : resolver.mappings.insert_hir_type (resolver.translated);
227 62286 : resolver.mappings.insert_location (
228 62286 : resolver.translated->get_mappings ().get_hirid (),
229 62286 : resolver.translated->get_locus ());
230 :
231 62286 : return resolver.translated;
232 62286 : }
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 : default_to_static_lifetime,
271 : 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 : default_to_static_lifetime,
284 : 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 394 : ASTLoweringType::visit (AST::TupleType &tuple)
300 : {
301 394 : std::vector<std::unique_ptr<HIR::Type>> elems;
302 1005 : for (auto &e : tuple.get_elems ())
303 : {
304 611 : HIR::Type *t = ASTLoweringType::translate (*e, default_to_static_lifetime,
305 : impl_trait_allowed);
306 611 : elems.push_back (std::unique_ptr<HIR::Type> (t));
307 : }
308 :
309 394 : auto crate_num = mappings.get_current_crate ();
310 788 : Analysis::NodeMapping mapping (crate_num, tuple.get_node_id (),
311 394 : mappings.get_next_hir_id (crate_num),
312 394 : mappings.get_next_localdef_id (crate_num));
313 :
314 788 : translated = new HIR::TupleType (std::move (mapping), std::move (elems),
315 394 : tuple.get_locus ());
316 394 : }
317 :
318 : void
319 48155 : ASTLoweringType::visit (AST::TypePath &path)
320 : {
321 48155 : translated = ASTLowerTypePath::translate (path);
322 48155 : }
323 :
324 : void
325 242 : ASTLoweringType::visit (AST::QualifiedPathInType &path)
326 : {
327 242 : translated = ASTLowerQualifiedPathInType::translate (path);
328 242 : }
329 :
330 : void
331 683 : ASTLoweringType::visit (AST::ArrayType &type)
332 : {
333 683 : HIR::Type *translated_type
334 683 : = ASTLoweringType::translate (type.get_elem_type (),
335 : default_to_static_lifetime,
336 : impl_trait_allowed);
337 683 : HIR::Expr *array_size = ASTLoweringExpr::translate (type.get_size_expr ());
338 :
339 683 : auto crate_num = mappings.get_current_crate ();
340 1366 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
341 683 : mappings.get_next_hir_id (crate_num),
342 683 : mappings.get_next_localdef_id (crate_num));
343 :
344 683 : translated
345 683 : = new HIR::ArrayType (mapping, std::unique_ptr<HIR::Type> (translated_type),
346 1366 : std::unique_ptr<HIR::Expr> (array_size),
347 683 : type.get_locus ());
348 683 : }
349 :
350 : void
351 4444 : ASTLoweringType::visit (AST::ReferenceType &type)
352 : {
353 4444 : HIR::Lifetime lifetime
354 4444 : = lower_lifetime (type.get_lifetime (), default_to_static_lifetime);
355 :
356 4444 : HIR::Type *base_type = ASTLoweringType::translate (type.get_base_type (),
357 : default_to_static_lifetime,
358 : impl_trait_allowed);
359 :
360 4444 : auto crate_num = mappings.get_current_crate ();
361 8888 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
362 4444 : mappings.get_next_hir_id (crate_num),
363 4444 : mappings.get_next_localdef_id (crate_num));
364 :
365 8888 : translated = new HIR::ReferenceType (mapping,
366 4444 : type.get_has_mut () ? Mutability::Mut
367 : : Mutability::Imm,
368 8888 : std::unique_ptr<HIR::Type> (base_type),
369 13026 : type.get_locus (), lifetime);
370 4444 : }
371 :
372 : void
373 6770 : ASTLoweringType::visit (AST::RawPointerType &type)
374 : {
375 6770 : HIR::Type *base_type
376 6770 : = ASTLoweringType::translate (type.get_type_pointed_to (),
377 : default_to_static_lifetime,
378 : impl_trait_allowed);
379 :
380 6770 : auto crate_num = mappings.get_current_crate ();
381 13540 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
382 6770 : mappings.get_next_hir_id (crate_num),
383 6770 : mappings.get_next_localdef_id (crate_num));
384 :
385 6770 : translated
386 6770 : = new HIR::RawPointerType (mapping,
387 6770 : type.get_pointer_type ()
388 : == AST::RawPointerType::PointerType::MUT
389 : ? Mutability::Mut
390 : : Mutability::Imm,
391 13540 : std::unique_ptr<HIR::Type> (base_type),
392 12803 : type.get_locus ());
393 6770 : }
394 :
395 : void
396 878 : ASTLoweringType::visit (AST::SliceType &type)
397 : {
398 878 : HIR::Type *base_type = ASTLoweringType::translate (type.get_elem_type (),
399 : default_to_static_lifetime,
400 : impl_trait_allowed);
401 :
402 878 : auto crate_num = mappings.get_current_crate ();
403 1756 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
404 878 : mappings.get_next_hir_id (crate_num),
405 878 : mappings.get_next_localdef_id (crate_num));
406 :
407 878 : translated
408 878 : = new HIR::SliceType (mapping, std::unique_ptr<HIR::Type> (base_type),
409 878 : type.get_locus ());
410 878 : }
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 206 : ASTLoweringType::visit (AST::TraitObjectTypeOneBound &type)
436 : {
437 206 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
438 206 : HIR::TypeParamBound *translated_bound
439 206 : = ASTLoweringTypeBounds::translate (type.get_trait_bound ());
440 206 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (translated_bound));
441 :
442 206 : auto crate_num = mappings.get_current_crate ();
443 412 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
444 206 : mappings.get_next_hir_id (crate_num),
445 206 : mappings.get_next_localdef_id (crate_num));
446 :
447 412 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
448 206 : type.get_locus (), type.is_dyn ());
449 206 : }
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 : default_to_static_lifetime,
478 : 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 8884 : ASTLowerGenericParam::translate (AST::GenericParam ¶m)
545 : {
546 8884 : ASTLowerGenericParam resolver;
547 8884 : param.accept_vis (resolver);
548 :
549 8884 : rust_assert (resolver.translated != nullptr);
550 8884 : resolver.mappings.insert_location (
551 8884 : resolver.translated->get_mappings ().get_hirid (), param.get_locus ());
552 8884 : resolver.mappings.insert_hir_generic_param (resolver.translated);
553 :
554 8884 : return resolver.translated;
555 8884 : }
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 94 : ASTLowerGenericParam::visit (AST::ConstGenericParam ¶m)
576 : {
577 94 : auto crate_num = mappings.get_current_crate ();
578 94 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
579 94 : mappings.get_next_hir_id (crate_num),
580 94 : mappings.get_next_localdef_id (crate_num));
581 :
582 94 : auto type = ASTLoweringType::translate (param.get_type ());
583 :
584 94 : HIR::Expr *default_expr = nullptr;
585 94 : if (param.has_default_value ())
586 15 : default_expr = ASTLoweringExpr::translate (
587 15 : param.get_default_value_unchecked ().get_expression ());
588 :
589 94 : translated = new HIR::ConstGenericParam (param.get_name ().as_string (),
590 282 : std::unique_ptr<Type> (type),
591 188 : std::unique_ptr<Expr> (default_expr),
592 282 : mapping, param.get_locus ());
593 94 : }
594 :
595 : void
596 8593 : ASTLowerGenericParam::visit (AST::TypeParam ¶m)
597 : {
598 8593 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
599 8593 : if (param.has_type_param_bounds ())
600 : {
601 1635 : for (auto &bound : param.get_type_param_bounds ())
602 : {
603 845 : HIR::TypeParamBound *lowered_bound = lower_bound (*bound);
604 845 : type_param_bounds.push_back (
605 845 : std::unique_ptr<HIR::TypeParamBound> (lowered_bound));
606 : }
607 : }
608 :
609 8593 : tl::optional<std::unique_ptr<HIR::Type>> type = tl::nullopt;
610 8593 : 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 8593 : auto crate_num = mappings.get_current_crate ();
616 8593 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
617 8593 : mappings.get_next_hir_id (crate_num),
618 8593 : mappings.get_next_localdef_id (crate_num));
619 :
620 8593 : translated
621 8593 : = new HIR::TypeParam (mapping, param.get_type_representation (),
622 : param.get_locus (), std::move (type_param_bounds),
623 8593 : std::move (type), param.get_outer_attrs (),
624 26139 : param.from_impl_trait ());
625 8593 : }
626 :
627 : HIR::TypeParamBound *
628 1895 : ASTLoweringTypeBounds::translate (AST::TypeParamBound &type)
629 : {
630 1895 : ASTLoweringTypeBounds resolver;
631 1895 : type.accept_vis (resolver);
632 :
633 1895 : rust_assert (resolver.translated != nullptr);
634 1895 : resolver.mappings.insert_location (
635 1895 : resolver.translated->get_mappings ().get_hirid (),
636 1895 : resolver.translated->get_locus ());
637 :
638 1895 : return resolver.translated;
639 1895 : }
640 :
641 : void
642 1883 : ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
643 : {
644 1883 : std::vector<HIR::LifetimeParam> for_lifetimes;
645 1890 : 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 1883 : AST::TypePath &ast_trait_path = bound.get_type_path ();
653 1883 : HIR::TypePath *trait_path = ASTLowerTypePath::translate (ast_trait_path);
654 :
655 1883 : auto crate_num = mappings.get_current_crate ();
656 1883 : Analysis::NodeMapping mapping (crate_num, bound.get_node_id (),
657 1883 : mappings.get_next_hir_id (crate_num),
658 1883 : UNKNOWN_LOCAL_DEFID);
659 :
660 1883 : BoundPolarity polarity = bound.has_opening_question_mark ()
661 1883 : ? BoundPolarity::AntiBound
662 1515 : : BoundPolarity::RegularBound;
663 1883 : translated
664 1883 : = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
665 3766 : bound.is_in_parens (), polarity, for_lifetimes);
666 1883 : }
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
|