Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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 : :
21 : : namespace Rust {
22 : : namespace HIR {
23 : :
24 : : HIR::TypePath *
25 : 30218 : ASTLowerTypePath::translate (AST::TypePath &type)
26 : : {
27 : 30218 : ASTLowerTypePath resolver;
28 : 30218 : type.accept_vis (resolver);
29 : 30218 : rust_assert (resolver.translated != nullptr);
30 : 30218 : return resolver.translated;
31 : 30218 : }
32 : :
33 : : void
34 : 19 : ASTLowerTypePath::visit (AST::TypePathSegmentFunction &segment)
35 : : {
36 : 19 : auto crate_num = mappings->get_current_crate ();
37 : 19 : auto hirid = mappings->get_next_hir_id (crate_num);
38 : 19 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
39 : 19 : UNKNOWN_LOCAL_DEFID);
40 : :
41 : 19 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
42 : :
43 : 19 : AST::TypePathFunction &fn = segment.get_type_path_function ();
44 : 19 : std::vector<std::unique_ptr<HIR::Type>> inputs;
45 : 38 : for (auto ¶m : fn.get_params ())
46 : : {
47 : 19 : HIR::Type *hir_type = ASTLoweringType::translate (param.get ());
48 : 19 : inputs.push_back (std::unique_ptr<HIR::Type> (hir_type));
49 : : }
50 : :
51 : 19 : HIR::Type *result_type
52 : 19 : = fn.has_return_type ()
53 : 19 : ? ASTLoweringType::translate (fn.get_return_type ().get ())
54 : 19 : : nullptr;
55 : :
56 : 19 : HIR::TypePathFunction function_path (std::move (inputs),
57 : 19 : std::unique_ptr<HIR::Type> (
58 : 19 : result_type));
59 : :
60 : 19 : translated_segment = new HIR::TypePathSegmentFunction (
61 : 19 : mapping, std::move (ident), segment.get_separating_scope_resolution (),
62 : 57 : std::move (function_path), segment.get_locus ());
63 : 19 : }
64 : :
65 : : void
66 : 29879 : ASTLowerTypePath::visit (AST::TypePathSegment &segment)
67 : : {
68 : 29879 : auto crate_num = mappings->get_current_crate ();
69 : 29879 : auto hirid = mappings->get_next_hir_id (crate_num);
70 : 29879 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
71 : 29879 : UNKNOWN_LOCAL_DEFID);
72 : :
73 : 29879 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
74 : 29879 : translated_segment
75 : 29879 : = new HIR::TypePathSegment (std::move (mapping), ident,
76 : 29879 : segment.get_separating_scope_resolution (),
77 : 29879 : segment.get_locus ());
78 : 29879 : }
79 : :
80 : : void
81 : 1349 : ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
82 : : {
83 : 1349 : std::vector<HIR::GenericArgsBinding> binding_args; // TODO
84 : :
85 : 1349 : std::string segment_name = segment.get_ident_segment ().as_string ();
86 : 1349 : bool has_separating_scope_resolution
87 : 1349 : = segment.get_separating_scope_resolution ();
88 : :
89 : 1349 : auto generic_args = lower_generic_args (segment.get_generic_args ());
90 : :
91 : 1349 : auto crate_num = mappings->get_current_crate ();
92 : 1349 : auto hirid = mappings->get_next_hir_id (crate_num);
93 : 1349 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
94 : 1349 : UNKNOWN_LOCAL_DEFID);
95 : :
96 : 1349 : translated_segment
97 : 1349 : = new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name,
98 : : has_separating_scope_resolution,
99 : 2698 : generic_args, segment.get_locus ());
100 : 1349 : }
101 : :
102 : : void
103 : 30218 : ASTLowerTypePath::visit (AST::TypePath &path)
104 : : {
105 : 30218 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
106 : :
107 : 61306 : for (auto &seg : path.get_segments ())
108 : : {
109 : 31088 : translated_segment = nullptr;
110 : 31088 : seg->accept_vis (*this);
111 : 31088 : rust_assert (translated_segment != nullptr);
112 : :
113 : 62176 : translated_segments.push_back (
114 : 31088 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
115 : : }
116 : :
117 : 30218 : auto crate_num = mappings->get_current_crate ();
118 : 30218 : auto hirid = mappings->get_next_hir_id (crate_num);
119 : 30218 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
120 : 30218 : mappings->get_next_localdef_id (crate_num));
121 : :
122 : 30218 : translated
123 : 30218 : = new HIR::TypePath (std::move (mapping), std::move (translated_segments),
124 : : path.get_locus (),
125 : 30218 : path.has_opening_scope_resolution_op ());
126 : 30218 : }
127 : :
128 : : HIR::QualifiedPathInType *
129 : 159 : ASTLowerQualifiedPathInType::translate (AST::QualifiedPathInType &type)
130 : : {
131 : 159 : ASTLowerQualifiedPathInType resolver;
132 : 159 : type.accept_vis (resolver);
133 : 159 : rust_assert (resolver.translated != nullptr);
134 : 159 : return resolver.translated;
135 : 159 : }
136 : :
137 : : void
138 : 159 : ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path)
139 : : {
140 : 159 : auto crate_num = mappings->get_current_crate ();
141 : 159 : auto hirid = mappings->get_next_hir_id (crate_num);
142 : 159 : Analysis::NodeMapping qual_mappings (
143 : 159 : crate_num, path.get_qualified_path_type ().get_node_id (), hirid,
144 : 159 : UNKNOWN_LOCAL_DEFID);
145 : :
146 : 159 : HIR::Type *qual_type = ASTLoweringType::translate (
147 : 159 : path.get_qualified_path_type ().get_type ().get ());
148 : 159 : HIR::TypePath *qual_trait = ASTLowerTypePath::translate (
149 : 159 : path.get_qualified_path_type ().get_as_type_path ());
150 : :
151 : 159 : HIR::QualifiedPathType qual_path_type (
152 : 318 : qual_mappings, std::unique_ptr<HIR::Type> (qual_type),
153 : 159 : std::unique_ptr<HIR::TypePath> (qual_trait),
154 : 159 : path.get_qualified_path_type ().get_locus ());
155 : :
156 : 159 : translated_segment = nullptr;
157 : 159 : path.get_associated_segment ()->accept_vis (*this);
158 : 159 : rust_assert (translated_segment != nullptr);
159 : :
160 : 159 : std::unique_ptr<HIR::TypePathSegment> associated_segment (translated_segment);
161 : :
162 : 159 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
163 : 159 : for (auto &seg : path.get_segments ())
164 : : {
165 : 0 : translated_segment = nullptr;
166 : 0 : seg->accept_vis (*this);
167 : 0 : rust_assert (translated_segment != nullptr);
168 : :
169 : 0 : translated_segments.push_back (
170 : 0 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
171 : : }
172 : :
173 : 159 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
174 : 159 : mappings->get_next_localdef_id (crate_num));
175 : 159 : translated = new HIR::QualifiedPathInType (std::move (mapping),
176 : : std::move (qual_path_type),
177 : : std::move (associated_segment),
178 : : std::move (translated_segments),
179 : 318 : path.get_locus ());
180 : 159 : }
181 : :
182 : : HIR::Type *
183 : 35414 : ASTLoweringType::translate (AST::Type *type, bool default_to_static_lifetime)
184 : : {
185 : 35414 : ASTLoweringType resolver (default_to_static_lifetime);
186 : 35414 : type->accept_vis (resolver);
187 : :
188 : 35414 : rust_assert (resolver.translated != nullptr);
189 : 35414 : resolver.mappings->insert_hir_type (resolver.translated);
190 : 35414 : resolver.mappings->insert_location (
191 : 35414 : resolver.translated->get_mappings ().get_hirid (),
192 : 35414 : resolver.translated->get_locus ());
193 : :
194 : 35414 : return resolver.translated;
195 : 35414 : }
196 : :
197 : : void
198 : 36 : ASTLoweringType::visit (AST::BareFunctionType &fntype)
199 : : {
200 : 36 : bool is_variadic = false;
201 : 36 : std::vector<HIR::LifetimeParam> lifetime_params;
202 : 38 : for (auto &lifetime_param : fntype.get_for_lifetimes ())
203 : : {
204 : 2 : auto generic_param = ASTLowerGenericParam::translate (&lifetime_param);
205 : 2 : lifetime_params.push_back (
206 : : *static_cast<HIR::LifetimeParam *> (generic_param));
207 : : }
208 : :
209 : 36 : HIR::FunctionQualifiers qualifiers
210 : 36 : = lower_qualifiers (fntype.get_function_qualifiers ());
211 : :
212 : 36 : std::vector<HIR::MaybeNamedParam> named_params;
213 : 69 : for (auto ¶m : fntype.get_function_params ())
214 : : {
215 : 33 : HIR::MaybeNamedParam::ParamKind kind;
216 : 33 : switch (param.get_param_kind ())
217 : : {
218 : : case AST::MaybeNamedParam::ParamKind::UNNAMED:
219 : : kind = HIR::MaybeNamedParam::ParamKind::UNNAMED;
220 : : break;
221 : : case AST::MaybeNamedParam::ParamKind::IDENTIFIER:
222 : : kind = HIR::MaybeNamedParam::ParamKind::IDENTIFIER;
223 : : break;
224 : : case AST::MaybeNamedParam::ParamKind::WILDCARD:
225 : : kind = HIR::MaybeNamedParam::ParamKind::WILDCARD;
226 : : break;
227 : 0 : default:
228 : 0 : rust_unreachable ();
229 : : }
230 : :
231 : 33 : HIR::Type *param_type
232 : 33 : = ASTLoweringType::translate (param.get_type ().get (),
233 : 33 : default_to_static_lifetime);
234 : :
235 : 33 : HIR::MaybeNamedParam p (param.get_name (), kind,
236 : 66 : std::unique_ptr<HIR::Type> (param_type),
237 : 99 : param.get_locus ());
238 : 33 : named_params.push_back (std::move (p));
239 : : }
240 : :
241 : 36 : HIR::Type *return_type = nullptr;
242 : 36 : if (fntype.has_return_type ())
243 : : {
244 : 32 : return_type
245 : 32 : = ASTLoweringType::translate (fntype.get_return_type ().get (),
246 : 32 : default_to_static_lifetime);
247 : : }
248 : :
249 : 36 : auto crate_num = mappings->get_current_crate ();
250 : 36 : Analysis::NodeMapping mapping (crate_num, fntype.get_node_id (),
251 : 36 : mappings->get_next_hir_id (crate_num),
252 : 36 : mappings->get_next_localdef_id (crate_num));
253 : :
254 : 72 : translated = new HIR::BareFunctionType (
255 : : std::move (mapping), std::move (lifetime_params), std::move (qualifiers),
256 : : std::move (named_params), is_variadic,
257 : 36 : std::unique_ptr<HIR::Type> (return_type), fntype.get_locus ());
258 : 36 : }
259 : :
260 : : void
261 : 230 : ASTLoweringType::visit (AST::TupleType &tuple)
262 : : {
263 : 230 : std::vector<std::unique_ptr<HIR::Type>> elems;
264 : 580 : for (auto &e : tuple.get_elems ())
265 : : {
266 : 350 : HIR::Type *t
267 : 350 : = ASTLoweringType::translate (e.get (), default_to_static_lifetime);
268 : 350 : elems.push_back (std::unique_ptr<HIR::Type> (t));
269 : : }
270 : :
271 : 230 : auto crate_num = mappings->get_current_crate ();
272 : 230 : Analysis::NodeMapping mapping (crate_num, tuple.get_node_id (),
273 : 230 : mappings->get_next_hir_id (crate_num),
274 : 230 : mappings->get_next_localdef_id (crate_num));
275 : :
276 : 230 : translated = new HIR::TupleType (std::move (mapping), std::move (elems),
277 : 230 : tuple.get_locus ());
278 : 230 : }
279 : :
280 : : void
281 : 27092 : ASTLoweringType::visit (AST::TypePath &path)
282 : : {
283 : 27092 : translated = ASTLowerTypePath::translate (path);
284 : 27092 : }
285 : :
286 : : void
287 : 159 : ASTLoweringType::visit (AST::QualifiedPathInType &path)
288 : : {
289 : 159 : translated = ASTLowerQualifiedPathInType::translate (path);
290 : 159 : }
291 : :
292 : : void
293 : 392 : ASTLoweringType::visit (AST::ArrayType &type)
294 : : {
295 : 392 : HIR::Type *translated_type
296 : 392 : = ASTLoweringType::translate (type.get_elem_type ().get (),
297 : 392 : default_to_static_lifetime);
298 : 392 : HIR::Expr *array_size
299 : 392 : = ASTLoweringExpr::translate (type.get_size_expr ().get ());
300 : :
301 : 392 : auto crate_num = mappings->get_current_crate ();
302 : 392 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
303 : 392 : mappings->get_next_hir_id (crate_num),
304 : 392 : mappings->get_next_localdef_id (crate_num));
305 : :
306 : 392 : translated
307 : 784 : = new HIR::ArrayType (mapping, std::unique_ptr<HIR::Type> (translated_type),
308 : 392 : std::unique_ptr<HIR::Expr> (array_size),
309 : 392 : type.get_locus ());
310 : 392 : }
311 : :
312 : : void
313 : 1808 : ASTLoweringType::visit (AST::ReferenceType &type)
314 : : {
315 : 1808 : HIR::Lifetime lifetime
316 : 1808 : = lower_lifetime (type.get_lifetime (), default_to_static_lifetime);
317 : :
318 : 1808 : HIR::Type *base_type
319 : 3616 : = ASTLoweringType::translate (type.get_base_type ().get (),
320 : 1808 : default_to_static_lifetime);
321 : :
322 : 1808 : auto crate_num = mappings->get_current_crate ();
323 : 1808 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
324 : 1808 : mappings->get_next_hir_id (crate_num),
325 : 1808 : mappings->get_next_localdef_id (crate_num));
326 : :
327 : 3616 : translated = new HIR::ReferenceType (mapping,
328 : 1808 : type.get_has_mut () ? Mutability::Mut
329 : : : Mutability::Imm,
330 : 3616 : std::unique_ptr<HIR::Type> (base_type),
331 : 5424 : type.get_locus (), lifetime);
332 : 1808 : }
333 : :
334 : : void
335 : 4687 : ASTLoweringType::visit (AST::RawPointerType &type)
336 : : {
337 : 4687 : HIR::Type *base_type
338 : 4687 : = ASTLoweringType::translate (type.get_type_pointed_to ().get (),
339 : 4687 : default_to_static_lifetime);
340 : :
341 : 4687 : auto crate_num = mappings->get_current_crate ();
342 : 4687 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
343 : 4687 : mappings->get_next_hir_id (crate_num),
344 : 4687 : mappings->get_next_localdef_id (crate_num));
345 : :
346 : 4687 : translated
347 : 4687 : = new HIR::RawPointerType (mapping,
348 : 4687 : type.get_pointer_type ()
349 : : == AST::RawPointerType::PointerType::MUT
350 : : ? Mutability::Mut
351 : : : Mutability::Imm,
352 : 4687 : std::unique_ptr<HIR::Type> (base_type),
353 : 9014 : type.get_locus ());
354 : 4687 : }
355 : :
356 : : void
357 : 746 : ASTLoweringType::visit (AST::SliceType &type)
358 : : {
359 : 746 : HIR::Type *base_type
360 : 746 : = ASTLoweringType::translate (type.get_elem_type ().get (),
361 : 746 : default_to_static_lifetime);
362 : :
363 : 746 : auto crate_num = mappings->get_current_crate ();
364 : 746 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
365 : 746 : mappings->get_next_hir_id (crate_num),
366 : 746 : mappings->get_next_localdef_id (crate_num));
367 : :
368 : 746 : translated
369 : 746 : = new HIR::SliceType (mapping, std::unique_ptr<HIR::Type> (base_type),
370 : 746 : type.get_locus ());
371 : 746 : }
372 : :
373 : : void
374 : 131 : ASTLoweringType::visit (AST::InferredType &type)
375 : : {
376 : 131 : auto crate_num = mappings->get_current_crate ();
377 : 131 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
378 : 131 : mappings->get_next_hir_id (crate_num),
379 : 131 : mappings->get_next_localdef_id (crate_num));
380 : :
381 : 131 : translated = new HIR::InferredType (mapping, type.get_locus ());
382 : 131 : }
383 : :
384 : : void
385 : 23 : ASTLoweringType::visit (AST::NeverType &type)
386 : : {
387 : 23 : auto crate_num = mappings->get_current_crate ();
388 : 23 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
389 : 23 : mappings->get_next_hir_id (crate_num),
390 : 23 : mappings->get_next_localdef_id (crate_num));
391 : :
392 : 23 : translated = new HIR::NeverType (mapping, type.get_locus ());
393 : 23 : }
394 : :
395 : : void
396 : 103 : ASTLoweringType::visit (AST::TraitObjectTypeOneBound &type)
397 : : {
398 : 103 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
399 : 103 : HIR::TypeParamBound *translated_bound
400 : 103 : = ASTLoweringTypeBounds::translate (&type.get_trait_bound ());
401 : 103 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (translated_bound));
402 : :
403 : 103 : auto crate_num = mappings->get_current_crate ();
404 : 103 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
405 : 103 : mappings->get_next_hir_id (crate_num),
406 : 103 : mappings->get_next_localdef_id (crate_num));
407 : :
408 : 103 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
409 : 103 : type.get_locus (), type.is_dyn ());
410 : 103 : }
411 : :
412 : : void
413 : 7 : ASTLoweringType::visit (AST::TraitObjectType &type)
414 : : {
415 : 7 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
416 : :
417 : 21 : for (auto &bound : type.get_type_param_bounds ())
418 : : {
419 : 14 : HIR::TypeParamBound *translated_bound
420 : 14 : = ASTLoweringTypeBounds::translate (bound.get ());
421 : 14 : bounds.push_back (
422 : 14 : std::unique_ptr<HIR::TypeParamBound> (translated_bound));
423 : : }
424 : :
425 : 7 : auto crate_num = mappings->get_current_crate ();
426 : 7 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
427 : 7 : mappings->get_next_hir_id (crate_num),
428 : 7 : mappings->get_next_localdef_id (crate_num));
429 : :
430 : 7 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
431 : 7 : type.get_locus (), type.is_dyn ());
432 : 7 : }
433 : :
434 : : HIR::GenericParam *
435 : 4998 : ASTLowerGenericParam::translate (AST::GenericParam *param)
436 : : {
437 : 4998 : ASTLowerGenericParam resolver;
438 : 4998 : param->accept_vis (resolver);
439 : :
440 : 4998 : rust_assert (resolver.translated != nullptr);
441 : 4998 : resolver.mappings->insert_location (
442 : 4998 : resolver.translated->get_mappings ().get_hirid (), param->get_locus ());
443 : 4998 : resolver.mappings->insert_hir_generic_param (resolver.translated);
444 : :
445 : 4998 : return resolver.translated;
446 : 4998 : }
447 : :
448 : : void
449 : 143 : ASTLowerGenericParam::visit (AST::LifetimeParam ¶m)
450 : : {
451 : 143 : auto crate_num = mappings->get_current_crate ();
452 : 143 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
453 : 143 : mappings->get_next_hir_id (crate_num),
454 : 143 : mappings->get_next_localdef_id (crate_num));
455 : :
456 : 143 : HIR::Lifetime lt (mapping, param.get_lifetime ().get_lifetime_type (),
457 : 143 : param.get_lifetime ().get_lifetime_name (),
458 : 143 : param.get_lifetime ().get_locus ());
459 : :
460 : 143 : translated = new HIR::LifetimeParam (mapping, lt, param.get_locus (),
461 : 429 : std::vector<Lifetime> ());
462 : 143 : }
463 : :
464 : : void
465 : 16 : ASTLowerGenericParam::visit (AST::ConstGenericParam ¶m)
466 : : {
467 : 16 : auto crate_num = mappings->get_current_crate ();
468 : 16 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
469 : 16 : mappings->get_next_hir_id (crate_num),
470 : 16 : mappings->get_next_localdef_id (crate_num));
471 : :
472 : 16 : auto type = ASTLoweringType::translate (param.get_type ().get ());
473 : :
474 : 16 : HIR::Expr *default_expr = nullptr;
475 : 16 : if (param.has_default_value ())
476 : 12 : default_expr = ASTLoweringExpr::translate (
477 : 12 : param.get_default_value ().get_expression ().get ());
478 : :
479 : 16 : translated = new HIR::ConstGenericParam (param.get_name ().as_string (),
480 : 48 : std::unique_ptr<Type> (type),
481 : 32 : std::unique_ptr<Expr> (default_expr),
482 : 16 : mapping, param.get_locus ());
483 : 16 : }
484 : :
485 : : void
486 : 4839 : ASTLowerGenericParam::visit (AST::TypeParam ¶m)
487 : : {
488 : 4839 : AST::Attribute outer_attr = AST::Attribute::create_empty ();
489 : 4839 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
490 : 4839 : if (param.has_type_param_bounds ())
491 : : {
492 : 502 : for (auto &bound : param.get_type_param_bounds ())
493 : : {
494 : 252 : HIR::TypeParamBound *lowered_bound = lower_bound (bound.get ());
495 : 252 : type_param_bounds.push_back (
496 : 252 : std::unique_ptr<HIR::TypeParamBound> (lowered_bound));
497 : : }
498 : : }
499 : :
500 : 4839 : HIR::Type *type = param.has_type ()
501 : 4839 : ? ASTLoweringType::translate (param.get_type ().get ())
502 : 4839 : : nullptr;
503 : :
504 : 4839 : auto crate_num = mappings->get_current_crate ();
505 : 4839 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
506 : 4839 : mappings->get_next_hir_id (crate_num),
507 : 4839 : mappings->get_next_localdef_id (crate_num));
508 : :
509 : 4839 : translated
510 : 4839 : = new HIR::TypeParam (mapping, param.get_type_representation (),
511 : : param.get_locus (), std::move (type_param_bounds),
512 : 9678 : std::unique_ptr<Type> (type), std::move (outer_attr));
513 : 4839 : }
514 : :
515 : : HIR::TypeParamBound *
516 : 597 : ASTLoweringTypeBounds::translate (AST::TypeParamBound *type)
517 : : {
518 : 597 : ASTLoweringTypeBounds resolver;
519 : 597 : type->accept_vis (resolver);
520 : :
521 : 597 : rust_assert (resolver.translated != nullptr);
522 : 597 : resolver.mappings->insert_location (
523 : 597 : resolver.translated->get_mappings ().get_hirid (),
524 : 597 : resolver.translated->get_locus ());
525 : :
526 : 597 : return resolver.translated;
527 : 597 : }
528 : :
529 : : void
530 : 585 : ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
531 : : {
532 : 585 : std::vector<HIR::LifetimeParam> for_lifetimes;
533 : 592 : for (auto &lifetime_param : bound.get_for_lifetimes ())
534 : : {
535 : 7 : auto generic_param = ASTLowerGenericParam::translate (&lifetime_param);
536 : 7 : for_lifetimes.push_back (
537 : : *static_cast<HIR::LifetimeParam *> (generic_param));
538 : : }
539 : :
540 : 585 : AST::TypePath &ast_trait_path = bound.get_type_path ();
541 : 585 : HIR::TypePath *trait_path = ASTLowerTypePath::translate (ast_trait_path);
542 : :
543 : 585 : auto crate_num = mappings->get_current_crate ();
544 : 585 : Analysis::NodeMapping mapping (crate_num, bound.get_node_id (),
545 : 585 : mappings->get_next_hir_id (crate_num),
546 : 585 : UNKNOWN_LOCAL_DEFID);
547 : :
548 : 585 : BoundPolarity polarity = bound.has_opening_question_mark ()
549 : 585 : ? BoundPolarity::AntiBound
550 : 585 : : BoundPolarity::RegularBound;
551 : 585 : translated
552 : 585 : = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
553 : 1170 : bound.is_in_parens (), polarity, for_lifetimes);
554 : 585 : }
555 : :
556 : : void
557 : 12 : ASTLoweringTypeBounds::visit (AST::Lifetime &bound)
558 : : {
559 : 12 : HIR::Lifetime lifetime = lower_lifetime (bound);
560 : 12 : translated = new HIR::Lifetime (lifetime);
561 : 12 : }
562 : :
563 : : HIR::WhereClauseItem *
564 : 81 : ASTLowerWhereClauseItem::translate (AST::WhereClauseItem &item)
565 : : {
566 : 81 : ASTLowerWhereClauseItem compiler;
567 : 81 : item.accept_vis (compiler);
568 : :
569 : 81 : rust_assert (compiler.translated != nullptr);
570 : : // FIXME
571 : : // compiler.mappings->insert_location (
572 : : // compiler.translated->get_mappings ().get_hirid (),
573 : : // compiler.translated->get_locus ());
574 : :
575 : 81 : return compiler.translated;
576 : 81 : }
577 : :
578 : : void
579 : 0 : ASTLowerWhereClauseItem::visit (AST::LifetimeWhereClauseItem &item)
580 : : {
581 : 0 : HIR::Lifetime l = lower_lifetime (item.get_lifetime ());
582 : 0 : std::vector<HIR::Lifetime> lifetime_bounds;
583 : 0 : for (auto &lifetime_bound : item.get_lifetime_bounds ())
584 : : {
585 : 0 : HIR::Lifetime ll = lower_lifetime (lifetime_bound);
586 : 0 : lifetime_bounds.push_back (std::move (ll));
587 : 0 : }
588 : :
589 : 0 : auto crate_num = mappings->get_current_crate ();
590 : 0 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
591 : 0 : mappings->get_next_hir_id (crate_num),
592 : 0 : UNKNOWN_LOCAL_DEFID);
593 : :
594 : 0 : translated = new HIR::LifetimeWhereClauseItem (mapping, std::move (l),
595 : : std::move (lifetime_bounds),
596 : 0 : item.get_locus ());
597 : 0 : }
598 : :
599 : : void
600 : 81 : ASTLowerWhereClauseItem::visit (AST::TypeBoundWhereClauseItem &item)
601 : : {
602 : : // FIXME
603 : 81 : std::vector<HIR::LifetimeParam> for_lifetimes;
604 : :
605 : 90 : for (auto &lifetime_param : item.get_for_lifetimes ())
606 : : {
607 : 9 : auto generic_param = ASTLowerGenericParam::translate (&lifetime_param);
608 : 9 : for_lifetimes.push_back (
609 : : *static_cast<HIR::LifetimeParam *> (generic_param));
610 : : }
611 : :
612 : 81 : std::unique_ptr<HIR::Type> bound_type = std::unique_ptr<HIR::Type> (
613 : 81 : ASTLoweringType::translate (item.get_type ().get ()));
614 : :
615 : 81 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
616 : 162 : for (auto &bound : item.get_type_param_bounds ())
617 : : {
618 : 81 : HIR::TypeParamBound *b = ASTLoweringTypeBounds::translate (bound.get ());
619 : 81 : type_param_bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
620 : : }
621 : :
622 : 81 : auto crate_num = mappings->get_current_crate ();
623 : 81 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
624 : 81 : mappings->get_next_hir_id (crate_num),
625 : 81 : UNKNOWN_LOCAL_DEFID);
626 : :
627 : 81 : translated
628 : 81 : = new HIR::TypeBoundWhereClauseItem (mapping, std::move (for_lifetimes),
629 : : std::move (bound_type),
630 : : std::move (type_param_bounds),
631 : 81 : item.get_locus ());
632 : 81 : }
633 : :
634 : : } // namespace HIR
635 : : } // namespace Rust
|