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