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 : 48565 : ASTLowerTypePath::translate (AST::TypePath &type)
31 : : {
32 : 48565 : ASTLowerTypePath resolver;
33 : 48565 : type.accept_vis (resolver);
34 : 48565 : rust_assert (resolver.translated != nullptr);
35 : 48565 : return resolver.translated;
36 : 48565 : }
37 : :
38 : : void
39 : 44 : ASTLowerTypePath::visit (AST::TypePathSegmentFunction &segment)
40 : : {
41 : 44 : auto crate_num = mappings.get_current_crate ();
42 : 44 : auto hirid = mappings.get_next_hir_id (crate_num);
43 : 44 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
44 : 44 : UNKNOWN_LOCAL_DEFID);
45 : :
46 : 44 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
47 : :
48 : 44 : AST::TypePathFunction &fn = segment.get_type_path_function ();
49 : 44 : std::vector<std::unique_ptr<HIR::Type>> inputs;
50 : 92 : for (auto ¶m : fn.get_params ())
51 : : {
52 : 48 : HIR::Type *hir_type = ASTLoweringType::translate (*param);
53 : 48 : inputs.push_back (std::unique_ptr<HIR::Type> (hir_type));
54 : : }
55 : :
56 : 44 : HIR::Type *result_type
57 : 44 : = fn.has_return_type () ? ASTLoweringType::translate (fn.get_return_type ())
58 : 44 : : nullptr;
59 : :
60 : 44 : HIR::TypePathFunction function_path (std::move (inputs),
61 : 88 : std::unique_ptr<HIR::Type> (
62 : 44 : result_type));
63 : :
64 : 44 : translated_segment = new HIR::TypePathSegmentFunction (
65 : 44 : mapping, std::move (ident), segment.get_separating_scope_resolution (),
66 : 132 : std::move (function_path), segment.get_locus ());
67 : 44 : }
68 : :
69 : : void
70 : 47942 : ASTLowerTypePath::visit (AST::TypePathSegment &segment)
71 : : {
72 : 47942 : auto crate_num = mappings.get_current_crate ();
73 : 47942 : auto hirid = mappings.get_next_hir_id (crate_num);
74 : 47942 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
75 : 47942 : UNKNOWN_LOCAL_DEFID);
76 : :
77 : 47942 : if (segment.is_lang_item ())
78 : : {
79 : 134 : translated_segment = new HIR::TypePathSegment (std::move (mapping),
80 : : segment.get_lang_item (),
81 : 134 : segment.get_locus ());
82 : : }
83 : : else
84 : : {
85 : 47808 : HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
86 : 47808 : translated_segment
87 : 47808 : = new HIR::TypePathSegment (std::move (mapping), ident,
88 : 47808 : segment.get_separating_scope_resolution (),
89 : 95616 : segment.get_locus ());
90 : 47808 : }
91 : 47942 : }
92 : :
93 : : void
94 : 2264 : ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
95 : : {
96 : 2264 : std::vector<HIR::GenericArgsBinding> binding_args; // TODO
97 : :
98 : 2264 : auto generic_args = lower_generic_args (segment.get_generic_args ());
99 : :
100 : 2264 : auto crate_num = mappings.get_current_crate ();
101 : 2264 : auto hirid = mappings.get_next_hir_id (crate_num);
102 : 2264 : Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
103 : 2264 : UNKNOWN_LOCAL_DEFID);
104 : :
105 : 2264 : 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 : 2259 : std::string segment_name = segment.get_ident_segment ().as_string ();
115 : 2259 : bool has_separating_scope_resolution
116 : 2259 : = segment.get_separating_scope_resolution ();
117 : :
118 : 2259 : translated_segment
119 : 2259 : = new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name,
120 : : has_separating_scope_resolution,
121 : 4518 : generic_args, segment.get_locus ());
122 : 2259 : }
123 : 2264 : }
124 : :
125 : : void
126 : 48565 : ASTLowerTypePath::visit (AST::TypePath &path)
127 : : {
128 : 48565 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
129 : :
130 : 98541 : for (auto &seg : path.get_segments ())
131 : : {
132 : 49976 : translated_segment = nullptr;
133 : 49976 : seg->accept_vis (*this);
134 : 49976 : rust_assert (translated_segment != nullptr);
135 : :
136 : 99952 : translated_segments.push_back (
137 : 49976 : std::unique_ptr<HIR::TypePathSegment> (translated_segment));
138 : : }
139 : :
140 : 48565 : auto crate_num = mappings.get_current_crate ();
141 : 48565 : auto hirid = mappings.get_next_hir_id (crate_num);
142 : 48565 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
143 : 48565 : mappings.get_next_localdef_id (crate_num));
144 : :
145 : 48565 : translated
146 : 48565 : = new HIR::TypePath (std::move (mapping), std::move (translated_segments),
147 : : path.get_locus (),
148 : 48565 : path.has_opening_scope_resolution_op ());
149 : 48565 : }
150 : :
151 : : HIR::QualifiedPathInType *
152 : 274 : ASTLowerQualifiedPathInType::translate (AST::QualifiedPathInType &type)
153 : : {
154 : 274 : ASTLowerQualifiedPathInType resolver;
155 : 274 : type.accept_vis (resolver);
156 : 274 : rust_assert (resolver.translated != nullptr);
157 : 274 : return resolver.translated;
158 : 274 : }
159 : :
160 : : void
161 : 274 : ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path)
162 : : {
163 : 274 : auto crate_num = mappings.get_current_crate ();
164 : 274 : auto hirid = mappings.get_next_hir_id (crate_num);
165 : 274 : Analysis::NodeMapping qual_mappings (
166 : 274 : crate_num, path.get_qualified_path_type ().get_node_id (), hirid,
167 : 274 : UNKNOWN_LOCAL_DEFID);
168 : :
169 : 274 : HIR::Type *qual_type
170 : 274 : = ASTLoweringType::translate (path.get_qualified_path_type ().get_type ());
171 : :
172 : 274 : HIR::TypePath *qual_trait = nullptr;
173 : 274 : if (!path.get_qualified_path_type ().is_error ())
174 : : {
175 : 274 : AST::QualifiedPathType &qualifier = path.get_qualified_path_type ();
176 : 274 : if (qualifier.has_as_clause ())
177 : 272 : qual_trait
178 : 272 : = ASTLowerTypePath::translate (qualifier.get_as_type_path ());
179 : : }
180 : :
181 : 274 : HIR::QualifiedPathType qual_path_type (
182 : 274 : qual_mappings, std::unique_ptr<HIR::Type> (qual_type),
183 : 548 : std::unique_ptr<HIR::TypePath> (qual_trait),
184 : 274 : path.get_qualified_path_type ().get_locus ());
185 : :
186 : 274 : translated_segment = nullptr;
187 : 274 : path.get_associated_segment ()->accept_vis (*this);
188 : 274 : rust_assert (translated_segment != nullptr);
189 : :
190 : 274 : std::unique_ptr<HIR::TypePathSegment> associated_segment (translated_segment);
191 : :
192 : 274 : std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments;
193 : 274 : 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 : 274 : Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid,
204 : 274 : mappings.get_next_localdef_id (crate_num));
205 : 274 : translated = new HIR::QualifiedPathInType (std::move (mapping),
206 : : std::move (qual_path_type),
207 : : std::move (associated_segment),
208 : : std::move (translated_segments),
209 : 548 : path.get_locus ());
210 : 274 : }
211 : :
212 : : HIR::Type *
213 : 55015 : ASTLoweringType::translate (AST::Type &type, bool default_to_static_lifetime)
214 : : {
215 : 55015 : ASTLoweringType resolver (default_to_static_lifetime);
216 : 55015 : type.accept_vis (resolver);
217 : :
218 : 55015 : rust_assert (resolver.translated != nullptr);
219 : 55015 : resolver.mappings.insert_hir_type (resolver.translated);
220 : 55015 : resolver.mappings.insert_location (
221 : 55015 : resolver.translated->get_mappings ().get_hirid (),
222 : 55015 : resolver.translated->get_locus ());
223 : :
224 : 55015 : return resolver.translated;
225 : 55015 : }
226 : :
227 : : void
228 : 66 : ASTLoweringType::visit (AST::BareFunctionType &fntype)
229 : : {
230 : 66 : bool is_variadic = false;
231 : 66 : std::vector<HIR::LifetimeParam> lifetime_params;
232 : 72 : 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 : 66 : HIR::FunctionQualifiers qualifiers
240 : 66 : = lower_qualifiers (fntype.get_function_qualifiers ());
241 : :
242 : 66 : std::vector<HIR::MaybeNamedParam> named_params;
243 : 126 : for (auto ¶m : fntype.get_function_params ())
244 : : {
245 : 60 : HIR::MaybeNamedParam::ParamKind kind;
246 : 60 : 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 : 60 : HIR::Type *param_type
262 : 60 : = ASTLoweringType::translate (param.get_type (),
263 : 60 : default_to_static_lifetime);
264 : :
265 : 60 : HIR::MaybeNamedParam p (param.get_name (), kind,
266 : 120 : std::unique_ptr<HIR::Type> (param_type),
267 : 120 : param.get_locus ());
268 : 60 : named_params.push_back (std::move (p));
269 : : }
270 : :
271 : 66 : HIR::Type *return_type = nullptr;
272 : 66 : if (fntype.has_return_type ())
273 : : {
274 : 44 : return_type = ASTLoweringType::translate (fntype.get_return_type (),
275 : 44 : default_to_static_lifetime);
276 : : }
277 : :
278 : 66 : auto crate_num = mappings.get_current_crate ();
279 : 66 : Analysis::NodeMapping mapping (crate_num, fntype.get_node_id (),
280 : 66 : mappings.get_next_hir_id (crate_num),
281 : 66 : mappings.get_next_localdef_id (crate_num));
282 : :
283 : 132 : translated = new HIR::BareFunctionType (
284 : : std::move (mapping), std::move (lifetime_params), std::move (qualifiers),
285 : : std::move (named_params), is_variadic,
286 : 66 : std::unique_ptr<HIR::Type> (return_type), fntype.get_locus ());
287 : 66 : }
288 : :
289 : : void
290 : 405 : ASTLoweringType::visit (AST::TupleType &tuple)
291 : : {
292 : 405 : std::vector<std::unique_ptr<HIR::Type>> elems;
293 : 1040 : for (auto &e : tuple.get_elems ())
294 : : {
295 : 635 : HIR::Type *t
296 : 635 : = ASTLoweringType::translate (*e, default_to_static_lifetime);
297 : 635 : elems.push_back (std::unique_ptr<HIR::Type> (t));
298 : : }
299 : :
300 : 405 : auto crate_num = mappings.get_current_crate ();
301 : 405 : Analysis::NodeMapping mapping (crate_num, tuple.get_node_id (),
302 : 405 : mappings.get_next_hir_id (crate_num),
303 : 405 : mappings.get_next_localdef_id (crate_num));
304 : :
305 : 810 : translated = new HIR::TupleType (std::move (mapping), std::move (elems),
306 : 405 : tuple.get_locus ());
307 : 405 : }
308 : :
309 : : void
310 : 42853 : ASTLoweringType::visit (AST::TypePath &path)
311 : : {
312 : 42853 : translated = ASTLowerTypePath::translate (path);
313 : 42853 : }
314 : :
315 : : void
316 : 274 : ASTLoweringType::visit (AST::QualifiedPathInType &path)
317 : : {
318 : 274 : translated = ASTLowerQualifiedPathInType::translate (path);
319 : 274 : }
320 : :
321 : : void
322 : 691 : ASTLoweringType::visit (AST::ArrayType &type)
323 : : {
324 : 691 : HIR::Type *translated_type
325 : 691 : = ASTLoweringType::translate (type.get_elem_type (),
326 : 691 : default_to_static_lifetime);
327 : 691 : HIR::Expr *array_size = ASTLoweringExpr::translate (type.get_size_expr ());
328 : :
329 : 691 : auto crate_num = mappings.get_current_crate ();
330 : 691 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
331 : 691 : mappings.get_next_hir_id (crate_num),
332 : 691 : mappings.get_next_localdef_id (crate_num));
333 : :
334 : 691 : translated
335 : 691 : = new HIR::ArrayType (mapping, std::unique_ptr<HIR::Type> (translated_type),
336 : 1382 : std::unique_ptr<HIR::Expr> (array_size),
337 : 691 : type.get_locus ());
338 : 691 : }
339 : :
340 : : void
341 : 2857 : ASTLoweringType::visit (AST::ReferenceType &type)
342 : : {
343 : 2857 : HIR::Lifetime lifetime
344 : 2857 : = lower_lifetime (type.get_lifetime (), default_to_static_lifetime);
345 : :
346 : 2857 : HIR::Type *base_type
347 : 5714 : = ASTLoweringType::translate (type.get_base_type (),
348 : 2857 : default_to_static_lifetime);
349 : :
350 : 2857 : auto crate_num = mappings.get_current_crate ();
351 : 2857 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
352 : 2857 : mappings.get_next_hir_id (crate_num),
353 : 2857 : mappings.get_next_localdef_id (crate_num));
354 : :
355 : 5714 : translated = new HIR::ReferenceType (mapping,
356 : 2857 : type.get_has_mut () ? Mutability::Mut
357 : : : Mutability::Imm,
358 : 5714 : std::unique_ptr<HIR::Type> (base_type),
359 : 8252 : type.get_locus (), lifetime);
360 : 2857 : }
361 : :
362 : : void
363 : 6527 : ASTLoweringType::visit (AST::RawPointerType &type)
364 : : {
365 : 6527 : HIR::Type *base_type
366 : 6527 : = ASTLoweringType::translate (type.get_type_pointed_to (),
367 : 6527 : default_to_static_lifetime);
368 : :
369 : 6527 : auto crate_num = mappings.get_current_crate ();
370 : 6527 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
371 : 6527 : mappings.get_next_hir_id (crate_num),
372 : 6527 : mappings.get_next_localdef_id (crate_num));
373 : :
374 : 6527 : translated
375 : 6527 : = new HIR::RawPointerType (mapping,
376 : 6527 : type.get_pointer_type ()
377 : : == AST::RawPointerType::PointerType::MUT
378 : : ? Mutability::Mut
379 : : : Mutability::Imm,
380 : 13054 : std::unique_ptr<HIR::Type> (base_type),
381 : 12240 : type.get_locus ());
382 : 6527 : }
383 : :
384 : : void
385 : 797 : ASTLoweringType::visit (AST::SliceType &type)
386 : : {
387 : 797 : HIR::Type *base_type
388 : 797 : = ASTLoweringType::translate (type.get_elem_type (),
389 : 797 : default_to_static_lifetime);
390 : :
391 : 797 : auto crate_num = mappings.get_current_crate ();
392 : 797 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
393 : 797 : mappings.get_next_hir_id (crate_num),
394 : 797 : mappings.get_next_localdef_id (crate_num));
395 : :
396 : 797 : translated
397 : 797 : = new HIR::SliceType (mapping, std::unique_ptr<HIR::Type> (base_type),
398 : 797 : type.get_locus ());
399 : 797 : }
400 : :
401 : : void
402 : 198 : ASTLoweringType::visit (AST::InferredType &type)
403 : : {
404 : 198 : auto crate_num = mappings.get_current_crate ();
405 : 198 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
406 : 198 : mappings.get_next_hir_id (crate_num),
407 : 198 : mappings.get_next_localdef_id (crate_num));
408 : :
409 : 198 : translated = new HIR::InferredType (mapping, type.get_locus ());
410 : 198 : }
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 : 263 : ASTLoweringType::visit (AST::TraitObjectTypeOneBound &type)
425 : : {
426 : 263 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
427 : 263 : HIR::TypeParamBound *translated_bound
428 : 263 : = ASTLoweringTypeBounds::translate (type.get_trait_bound ());
429 : 263 : bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (translated_bound));
430 : :
431 : 263 : auto crate_num = mappings.get_current_crate ();
432 : 263 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
433 : 263 : mappings.get_next_hir_id (crate_num),
434 : 263 : mappings.get_next_localdef_id (crate_num));
435 : :
436 : 526 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
437 : 263 : type.get_locus (), type.is_dyn ());
438 : 263 : }
439 : :
440 : : void
441 : 22 : ASTLoweringType::visit (AST::TraitObjectType &type)
442 : : {
443 : 22 : std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds;
444 : :
445 : 72 : for (auto &bound : type.get_type_param_bounds ())
446 : : {
447 : 50 : HIR::TypeParamBound *translated_bound
448 : 50 : = ASTLoweringTypeBounds::translate (*bound);
449 : 50 : bounds.push_back (
450 : 50 : std::unique_ptr<HIR::TypeParamBound> (translated_bound));
451 : : }
452 : :
453 : 22 : auto crate_num = mappings.get_current_crate ();
454 : 22 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
455 : 22 : mappings.get_next_hir_id (crate_num),
456 : 22 : mappings.get_next_localdef_id (crate_num));
457 : :
458 : 44 : translated = new HIR::TraitObjectType (mapping, std::move (bounds),
459 : 22 : type.get_locus (), type.is_dyn ());
460 : 22 : }
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 : 8023 : ASTLowerGenericParam::translate (AST::GenericParam ¶m)
518 : : {
519 : 8023 : ASTLowerGenericParam resolver;
520 : 8023 : param.accept_vis (resolver);
521 : :
522 : 8023 : rust_assert (resolver.translated != nullptr);
523 : 8023 : resolver.mappings.insert_location (
524 : 8023 : resolver.translated->get_mappings ().get_hirid (), param.get_locus ());
525 : 8023 : resolver.mappings.insert_hir_generic_param (resolver.translated);
526 : :
527 : 8023 : return resolver.translated;
528 : 8023 : }
529 : :
530 : : void
531 : 328 : ASTLowerGenericParam::visit (AST::LifetimeParam ¶m)
532 : : {
533 : 328 : auto crate_num = mappings.get_current_crate ();
534 : 328 : AST::Lifetime lifetime = param.get_lifetime ();
535 : 328 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
536 : 328 : mappings.get_next_hir_id (crate_num),
537 : 328 : mappings.get_next_localdef_id (crate_num));
538 : :
539 : 328 : HIR::Lifetime lt (mapping, lifetime.get_lifetime_type (),
540 : 328 : lifetime.get_lifetime_name (), lifetime.get_locus ());
541 : :
542 : 328 : translated = new HIR::LifetimeParam (mapping, lt, param.get_locus (),
543 : 328 : std::vector<Lifetime> (),
544 : 656 : param.get_outer_attrs ());
545 : 328 : }
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 ().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 : 68 : mapping, param.get_locus ());
566 : 34 : }
567 : :
568 : : void
569 : 7661 : ASTLowerGenericParam::visit (AST::TypeParam ¶m)
570 : : {
571 : 7661 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
572 : 7661 : if (param.has_type_param_bounds ())
573 : : {
574 : 893 : for (auto &bound : param.get_type_param_bounds ())
575 : : {
576 : 453 : HIR::TypeParamBound *lowered_bound = lower_bound (*bound);
577 : 453 : type_param_bounds.push_back (
578 : 453 : std::unique_ptr<HIR::TypeParamBound> (lowered_bound));
579 : : }
580 : : }
581 : :
582 : 7661 : tl::optional<std::unique_ptr<HIR::Type>> type = tl::nullopt;
583 : 7661 : if (param.has_type ())
584 : 188 : type
585 : 188 : = tl::optional<std::unique_ptr<HIR::Type>> (std::unique_ptr<HIR::Type> (
586 : 188 : ASTLoweringType::translate (param.get_type ())));
587 : :
588 : 7661 : auto crate_num = mappings.get_current_crate ();
589 : 7661 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
590 : 7661 : mappings.get_next_hir_id (crate_num),
591 : 7661 : mappings.get_next_localdef_id (crate_num));
592 : :
593 : 7661 : translated
594 : 7661 : = new HIR::TypeParam (mapping, param.get_type_representation (),
595 : : param.get_locus (), std::move (type_param_bounds),
596 : 23171 : std::move (type), param.get_outer_attrs ());
597 : 7661 : }
598 : :
599 : : HIR::TypeParamBound *
600 : 1267 : ASTLoweringTypeBounds::translate (AST::TypeParamBound &type)
601 : : {
602 : 1267 : ASTLoweringTypeBounds resolver;
603 : 1267 : type.accept_vis (resolver);
604 : :
605 : 1267 : rust_assert (resolver.translated != nullptr);
606 : 1267 : resolver.mappings.insert_location (
607 : 1267 : resolver.translated->get_mappings ().get_hirid (),
608 : 1267 : resolver.translated->get_locus ());
609 : :
610 : 1267 : return resolver.translated;
611 : 1267 : }
612 : :
613 : : void
614 : 1245 : ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
615 : : {
616 : 1245 : std::vector<HIR::LifetimeParam> for_lifetimes;
617 : 1259 : for (auto &lifetime_param : bound.get_for_lifetimes ())
618 : : {
619 : 14 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
620 : 14 : for_lifetimes.push_back (
621 : : *static_cast<HIR::LifetimeParam *> (generic_param));
622 : : }
623 : :
624 : 1245 : AST::TypePath &ast_trait_path = bound.get_type_path ();
625 : 1245 : HIR::TypePath *trait_path = ASTLowerTypePath::translate (ast_trait_path);
626 : :
627 : 1245 : auto crate_num = mappings.get_current_crate ();
628 : 1245 : Analysis::NodeMapping mapping (crate_num, bound.get_node_id (),
629 : 1245 : mappings.get_next_hir_id (crate_num),
630 : 1245 : UNKNOWN_LOCAL_DEFID);
631 : :
632 : 1245 : BoundPolarity polarity = bound.has_opening_question_mark ()
633 : 1245 : ? BoundPolarity::AntiBound
634 : 1245 : : BoundPolarity::RegularBound;
635 : 1245 : translated
636 : 1245 : = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
637 : 2490 : bound.is_in_parens (), polarity, for_lifetimes);
638 : 1245 : }
639 : :
640 : : void
641 : 22 : ASTLoweringTypeBounds::visit (AST::Lifetime &bound)
642 : : {
643 : 22 : HIR::Lifetime lifetime = lower_lifetime (bound);
644 : 22 : translated = new HIR::Lifetime (lifetime);
645 : 22 : }
646 : :
647 : : HIR::WhereClauseItem *
648 : 120 : ASTLowerWhereClauseItem::translate (AST::WhereClauseItem &item)
649 : : {
650 : 120 : ASTLowerWhereClauseItem compiler;
651 : 120 : item.accept_vis (compiler);
652 : :
653 : 120 : 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 : 120 : return compiler.translated;
660 : 120 : }
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 : 118 : ASTLowerWhereClauseItem::visit (AST::TypeBoundWhereClauseItem &item)
685 : : {
686 : : // FIXME
687 : 118 : std::vector<HIR::LifetimeParam> for_lifetimes;
688 : :
689 : 136 : for (auto &lifetime_param : item.get_for_lifetimes ())
690 : : {
691 : 18 : auto generic_param = ASTLowerGenericParam::translate (lifetime_param);
692 : 18 : for_lifetimes.push_back (
693 : : *static_cast<HIR::LifetimeParam *> (generic_param));
694 : : }
695 : :
696 : 118 : std::unique_ptr<HIR::Type> bound_type = std::unique_ptr<HIR::Type> (
697 : 118 : ASTLoweringType::translate (item.get_type ()));
698 : :
699 : 118 : std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
700 : 236 : for (auto &bound : item.get_type_param_bounds ())
701 : : {
702 : 118 : HIR::TypeParamBound *b = ASTLoweringTypeBounds::translate (*bound);
703 : 118 : type_param_bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b));
704 : : }
705 : :
706 : 118 : auto crate_num = mappings.get_current_crate ();
707 : 118 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
708 : 118 : mappings.get_next_hir_id (crate_num),
709 : 118 : UNKNOWN_LOCAL_DEFID);
710 : :
711 : 118 : translated
712 : 118 : = new HIR::TypeBoundWhereClauseItem (mapping, std::move (for_lifetimes),
713 : : std::move (bound_type),
714 : : std::move (type_param_bounds),
715 : 118 : item.get_locus ());
716 : 118 : }
717 : :
718 : : } // namespace HIR
719 : : } // namespace Rust
|