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