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