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