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.h"
20 : : #include "rust-ast-lower-item.h"
21 : : #include "rust-ast-lower-stmt.h"
22 : : #include "rust-ast-lower-expr.h"
23 : : #include "rust-ast-lower-block.h"
24 : : #include "rust-ast-lower-type.h"
25 : : #include "rust-ast-lower-pattern.h"
26 : : #include "rust-ast-lower-struct-field-expr.h"
27 : : #include "rust-expr.h"
28 : : #include "rust-hir-expr.h"
29 : :
30 : : namespace Rust {
31 : : namespace HIR {
32 : : using HIR::ClosureParam;
33 : :
34 : : Visibility
35 : 36446 : translate_visibility (const AST::Visibility &vis)
36 : : {
37 : : // FIXME: How do we create a private visibility here? Is it always private if
38 : : // the AST vis is an error?
39 : : // FIXME: We need to add a `create_private()` static function to the
40 : : // AST::Visibility class and use it when the vis is empty in the parser...
41 : 36446 : if (vis.is_error ())
42 : 0 : return Visibility::create_error ();
43 : :
44 : 36446 : switch (vis.get_vis_type ())
45 : : {
46 : 7874 : case AST::Visibility::PUB:
47 : 7874 : return Visibility (Visibility::VisType::PUBLIC);
48 : 28511 : case AST::Visibility::PRIV:
49 : 28511 : case AST::Visibility::PUB_SELF:
50 : 28511 : return Visibility (Visibility::VisType::PRIVATE);
51 : 61 : case AST::Visibility::PUB_CRATE:
52 : 61 : case AST::Visibility::PUB_SUPER:
53 : 61 : case AST::Visibility::PUB_IN_PATH:
54 : 61 : return Visibility (Visibility::VisType::RESTRICTED,
55 : 61 : ASTLoweringSimplePath::translate (vis.get_path ()),
56 : 61 : vis.get_locus ());
57 : 0 : break;
58 : : }
59 : :
60 : 0 : return Visibility::create_error ();
61 : : }
62 : :
63 : 4299 : ASTLowering::ASTLowering (AST::Crate &astCrate) : astCrate (astCrate) {}
64 : :
65 : 4299 : ASTLowering::~ASTLowering () {}
66 : :
67 : : std::unique_ptr<HIR::Crate>
68 : 4299 : ASTLowering::Resolve (AST::Crate &astCrate)
69 : : {
70 : 4299 : ASTLowering resolver (astCrate);
71 : 4299 : return resolver.go ();
72 : 4299 : }
73 : :
74 : : std::unique_ptr<HIR::Crate>
75 : 4299 : ASTLowering::go ()
76 : : {
77 : 4299 : std::vector<std::unique_ptr<HIR::Item>> items;
78 : :
79 : 22910 : for (auto &item : astCrate.items)
80 : : {
81 : 18611 : auto translated = ASTLoweringItem::translate (*item);
82 : 18611 : if (translated != nullptr)
83 : 17500 : items.emplace_back (translated);
84 : : }
85 : :
86 : 4299 : auto &mappings = Analysis::Mappings::get ();
87 : 4299 : auto crate_num = mappings.get_current_crate ();
88 : 4299 : Analysis::NodeMapping mapping (crate_num, astCrate.get_node_id (),
89 : : mappings.get_next_hir_id (crate_num),
90 : 4299 : UNKNOWN_LOCAL_DEFID);
91 : :
92 : 4299 : return std::unique_ptr<HIR::Crate> (
93 : 4299 : new HIR::Crate (std::move (items), astCrate.get_inner_attrs (), mapping));
94 : 4299 : }
95 : :
96 : : // rust-ast-lower-block.h
97 : : void
98 : 21797 : ASTLoweringBlock::visit (AST::BlockExpr &expr)
99 : : {
100 : 21797 : tl::optional<HIR::LoopLabel> label;
101 : 21797 : if (expr.has_label ())
102 : 0 : label = lower_loop_label (expr.get_label ());
103 : : else
104 : : label = tl::nullopt;
105 : :
106 : 21797 : std::vector<std::unique_ptr<HIR::Stmt>> block_stmts;
107 : 21797 : bool block_did_terminate = false;
108 : :
109 : 44105 : for (auto &s : expr.get_statements ())
110 : : {
111 : : // FIXME: We basically need to do that check for *every* single node in
112 : : // the AST. this isn't realistic and this should be turned into an
113 : : // optional, debug-visitor instead, which goes through the entire AST and
114 : : // checks if any of the nodes are macro invocations
115 : 22308 : if (s->get_stmt_kind () == AST::Stmt::Kind::MacroInvocation)
116 : 0 : rust_fatal_error (
117 : 0 : s->get_locus (),
118 : : "macro invocations should not get lowered to HIR - At "
119 : : "this point in "
120 : : "the pipeline, they should all have been expanded");
121 : :
122 : 22308 : if (block_did_terminate)
123 : 18 : rust_warning_at (s->get_locus (), 0, "unreachable statement");
124 : :
125 : 22308 : bool terminated = false;
126 : 22308 : auto translated_stmt = ASTLoweringStmt::translate (s.get (), &terminated);
127 : 22308 : block_did_terminate |= terminated;
128 : :
129 : 22308 : if (translated_stmt)
130 : 22299 : block_stmts.emplace_back (translated_stmt);
131 : : }
132 : :
133 : 21797 : if (expr.has_tail_expr () && block_did_terminate)
134 : : {
135 : : // warning unreachable tail expressions
136 : 16 : rust_warning_at (expr.get_tail_expr ().get_locus (), 0,
137 : : "unreachable expression");
138 : : }
139 : :
140 : 21797 : HIR::ExprWithoutBlock *tail_expr = nullptr;
141 : 21797 : if (expr.has_tail_expr ())
142 : : {
143 : 15894 : bool terminated = false;
144 : 15894 : tail_expr = (HIR::ExprWithoutBlock *)
145 : 15894 : ASTLoweringExpr::translate (expr.get_tail_expr (), &terminated);
146 : 15894 : block_did_terminate |= terminated;
147 : : }
148 : :
149 : 21797 : bool tail_reachable = !block_did_terminate;
150 : 21797 : auto crate_num = mappings.get_current_crate ();
151 : 43594 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
152 : 21797 : mappings.get_next_hir_id (crate_num),
153 : 21797 : UNKNOWN_LOCAL_DEFID);
154 : 21797 : translated
155 : 21797 : = new HIR::BlockExpr (mapping, std::move (block_stmts),
156 : 43594 : std::unique_ptr<HIR::ExprWithoutBlock> (tail_expr),
157 : 21797 : tail_reachable, expr.get_inner_attrs (),
158 : 21797 : expr.get_outer_attrs (), label,
159 : 43594 : expr.get_start_locus (), expr.get_end_locus ());
160 : :
161 : 21797 : terminated = block_did_terminate;
162 : 21797 : }
163 : :
164 : : void
165 : 469 : ASTLoweringIfBlock::visit (AST::IfExpr &expr)
166 : : {
167 : 469 : bool ignored_terminated = false;
168 : 469 : HIR::Expr *condition = ASTLoweringExpr::translate (expr.get_condition_expr (),
169 : : &ignored_terminated);
170 : 469 : HIR::BlockExpr *block
171 : 469 : = ASTLoweringBlock::translate (expr.get_if_block (), &ignored_terminated);
172 : :
173 : 469 : auto crate_num = mappings.get_current_crate ();
174 : 938 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
175 : 469 : mappings.get_next_hir_id (crate_num),
176 : 469 : UNKNOWN_LOCAL_DEFID);
177 : :
178 : 469 : translated = new HIR::IfExpr (mapping, std::unique_ptr<HIR::Expr> (condition),
179 : 938 : std::unique_ptr<HIR::BlockExpr> (block),
180 : 469 : expr.get_locus ());
181 : 469 : }
182 : :
183 : : void
184 : 1204 : ASTLoweringIfBlock::visit (AST::IfExprConseqElse &expr)
185 : : {
186 : 1204 : HIR::Expr *condition
187 : 1204 : = ASTLoweringExpr::translate (expr.get_condition_expr ());
188 : :
189 : 1204 : bool if_block_terminated = false;
190 : 1204 : bool else_block_termianted = false;
191 : :
192 : 1204 : HIR::BlockExpr *if_block
193 : 1204 : = ASTLoweringBlock::translate (expr.get_if_block (), &if_block_terminated);
194 : 1204 : HIR::ExprWithBlock *else_block
195 : 1204 : = ASTLoweringExprWithBlock::translate (expr.get_else_block (),
196 : : &else_block_termianted);
197 : :
198 : 1204 : terminated = if_block_terminated && else_block_termianted;
199 : :
200 : 1204 : auto crate_num = mappings.get_current_crate ();
201 : 2408 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
202 : 1204 : mappings.get_next_hir_id (crate_num),
203 : 1204 : UNKNOWN_LOCAL_DEFID);
204 : :
205 : 2408 : translated = new HIR::IfExprConseqElse (
206 : 1204 : mapping, std::unique_ptr<HIR::Expr> (condition),
207 : 2408 : std::unique_ptr<HIR::BlockExpr> (if_block),
208 : 2408 : std::unique_ptr<HIR::ExprWithBlock> (else_block), expr.get_locus ());
209 : 1204 : }
210 : :
211 : : /**
212 : : * Lowers the common part "if let 'pattern' = 'expr' { 'if_block' }" of
213 : : * IfLetExpr[ConseqElse]:
214 : : * - 'expr' is lowered into *BRANCH_VALUE
215 : : * - 'pattern' + 'if_block' are lowered and resulting ARM pushed in MATCH_ARMS
216 : : * - 'KASE_ELSE_EXPR' is the lowered HIR to be used in the else part.
217 : : *
218 : : * Looks like:
219 : : *
220 : : * match (expr) {
221 : : * pattern => {if_block}
222 : : * _ => kase_else_expr
223 : : * }
224 : : *
225 : : */
226 : : void
227 : 29 : ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr,
228 : : HIR::Expr **branch_value,
229 : : HIR::Expr *kase_else_expr,
230 : : std::vector<HIR::MatchCase> &match_arms)
231 : : {
232 : 29 : HIR::Expr *kase_expr;
233 : 29 : std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns;
234 : 29 : match_arm_patterns.reserve (expr.get_patterns ().size ());
235 : :
236 : 29 : *branch_value = ASTLoweringExpr::translate (expr.get_value_expr ());
237 : 29 : kase_expr = ASTLoweringExpr::translate (expr.get_if_block ());
238 : :
239 : : // (stable) if let only accepts a single pattern, but (unstable) if let chains
240 : : // need more than one pattern.
241 : : // We don't support if let chains, so only support a single pattern.
242 : 29 : rust_assert (expr.get_patterns ().size () == 1);
243 : :
244 : 58 : for (auto &pattern : expr.get_patterns ())
245 : 29 : match_arm_patterns.emplace_back (ASTLoweringPattern::translate (*pattern));
246 : :
247 : : // The match arm corresponding to the if let pattern when it matches.
248 : 29 : HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (), nullptr,
249 : 29 : {});
250 : :
251 : 29 : auto crate_num = mappings.get_current_crate ();
252 : 58 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
253 : 29 : mappings.get_next_hir_id (crate_num),
254 : 29 : UNKNOWN_LOCAL_DEFID);
255 : :
256 : 29 : match_arms.emplace_back (std::move (mapping), std::move (arm),
257 : 29 : std::unique_ptr<HIR::Expr> (kase_expr));
258 : :
259 : : // The default match arm when the if let pattern does not match
260 : 29 : std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard;
261 : 58 : Analysis::NodeMapping mapping_default (crate_num, expr.get_node_id (),
262 : 29 : mappings.get_next_hir_id (crate_num),
263 : 29 : UNKNOWN_LOCAL_DEFID);
264 : :
265 : 29 : match_arm_patterns_wildcard.emplace_back (
266 : 29 : new HIR::WildcardPattern (mapping_default, expr.get_locus ()));
267 : :
268 : 29 : HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard),
269 : 29 : expr.get_locus (), nullptr, {});
270 : :
271 : 29 : match_arms.emplace_back (std::move (mapping_default), std::move (arm_default),
272 : 29 : std::unique_ptr<HIR::Expr> (kase_else_expr));
273 : 29 : }
274 : :
275 : : void
276 : 17 : ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
277 : : {
278 : : // Desugar:
279 : : // if let Some(y) = some_value {
280 : : // bar();
281 : : // }
282 : : //
283 : : // into:
284 : : //
285 : : // match some_value {
286 : : // Some(y) => {bar();},
287 : : // _ => ()
288 : : // }
289 : :
290 : 17 : HIR::Expr *branch_value;
291 : :
292 : 17 : std::vector<HIR::MatchCase> match_arms;
293 : 17 : auto crate_num = mappings.get_current_crate ();
294 : 34 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
295 : 17 : mappings.get_next_hir_id (crate_num),
296 : 17 : UNKNOWN_LOCAL_DEFID);
297 : :
298 : 17 : HIR::TupleExpr *unit
299 : 17 : = new HIR::TupleExpr (mapping, {}, {}, {}, expr.get_locus ());
300 : :
301 : 17 : desugar_iflet (expr, &branch_value, unit, match_arms);
302 : :
303 : 17 : translated
304 : 17 : = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
305 : 34 : std::move (match_arms), {}, {}, expr.get_locus ());
306 : 17 : }
307 : :
308 : : void
309 : 12 : ASTLoweringIfLetBlock::visit (AST::IfLetExprConseqElse &expr)
310 : : {
311 : : // desugar:
312 : : // if let Some(y) = some_value {
313 : : // bar();
314 : : // } else {
315 : : // baz();
316 : : // }
317 : : //
318 : : // into
319 : : // match some_value {
320 : : // Some(y) => {bar();},
321 : : // _ => {baz();}
322 : : // }
323 : : //
324 : :
325 : 12 : HIR::Expr *branch_value;
326 : 12 : std::vector<HIR::MatchCase> match_arms;
327 : :
328 : 12 : HIR::Expr *kase_else_expr
329 : 12 : = ASTLoweringExpr::translate (expr.get_else_block ());
330 : :
331 : 12 : desugar_iflet (expr, &branch_value, kase_else_expr, match_arms);
332 : :
333 : 12 : auto crate_num = mappings.get_current_crate ();
334 : 24 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
335 : 12 : mappings.get_next_hir_id (crate_num),
336 : 12 : UNKNOWN_LOCAL_DEFID);
337 : :
338 : 12 : translated
339 : 12 : = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
340 : 24 : std::move (match_arms), {}, {}, expr.get_locus ());
341 : 12 : }
342 : :
343 : : // rust-ast-lower-struct-field-expr.h
344 : :
345 : : void
346 : 2022 : ASTLowerStructExprField::visit (AST::StructExprFieldIdentifierValue &field)
347 : : {
348 : 2022 : HIR::Expr *value = ASTLoweringExpr::translate (field.get_value ());
349 : :
350 : 2022 : auto crate_num = mappings.get_current_crate ();
351 : 2022 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
352 : 2022 : mappings.get_next_hir_id (crate_num),
353 : 2022 : UNKNOWN_LOCAL_DEFID);
354 : :
355 : 2022 : translated = new HIR::StructExprFieldIdentifierValue (
356 : 6066 : mapping, field.get_field_name (), std::unique_ptr<HIR::Expr> (value),
357 : 4044 : field.get_locus ());
358 : 2022 : }
359 : :
360 : : void
361 : 44 : ASTLowerStructExprField::visit (AST::StructExprFieldIndexValue &field)
362 : : {
363 : 44 : HIR::Expr *value = ASTLoweringExpr::translate (field.get_value ());
364 : :
365 : 44 : auto crate_num = mappings.get_current_crate ();
366 : 44 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
367 : 44 : mappings.get_next_hir_id (crate_num),
368 : 44 : UNKNOWN_LOCAL_DEFID);
369 : :
370 : 44 : translated
371 : 44 : = new HIR::StructExprFieldIndexValue (mapping, field.get_index (),
372 : 44 : std::unique_ptr<HIR::Expr> (value),
373 : 44 : field.get_locus ());
374 : 44 : }
375 : :
376 : : void
377 : 216 : ASTLowerStructExprField::visit (AST::StructExprFieldIdentifier &field)
378 : : {
379 : 216 : auto crate_num = mappings.get_current_crate ();
380 : 216 : Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
381 : 216 : mappings.get_next_hir_id (crate_num),
382 : 216 : UNKNOWN_LOCAL_DEFID);
383 : :
384 : 216 : translated
385 : 216 : = new HIR::StructExprFieldIdentifier (mapping, field.get_field_name (),
386 : 216 : field.get_locus ());
387 : 216 : }
388 : :
389 : : // rust-ast-lower-block.h
390 : :
391 : : void
392 : 70 : ASTLoweringExprWithBlock::visit (AST::WhileLoopExpr &expr)
393 : : {
394 : 70 : HIR::BlockExpr *loop_block
395 : 70 : = ASTLoweringBlock::translate (expr.get_loop_block (), &terminated);
396 : :
397 : 70 : tl::optional<HIR::LoopLabel> loop_label;
398 : 70 : if (expr.has_loop_label ())
399 : 2 : loop_label = lower_loop_label (expr.get_loop_label ());
400 : :
401 : 70 : HIR::Expr *loop_condition
402 : 70 : = ASTLoweringExpr::translate (expr.get_predicate_expr (), &terminated);
403 : :
404 : 70 : auto crate_num = mappings.get_current_crate ();
405 : 140 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
406 : 70 : mappings.get_next_hir_id (crate_num),
407 : 70 : UNKNOWN_LOCAL_DEFID);
408 : :
409 : 70 : translated
410 : 70 : = new HIR::WhileLoopExpr (mapping,
411 : 70 : std::unique_ptr<HIR::Expr> (loop_condition),
412 : 140 : std::unique_ptr<HIR::BlockExpr> (loop_block),
413 : : expr.get_locus (), std::move (loop_label),
414 : 140 : expr.get_outer_attrs ());
415 : 70 : }
416 : :
417 : : void
418 : 1052 : ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr)
419 : : {
420 : 1052 : HIR::Expr *branch_value
421 : 1052 : = ASTLoweringExpr::translate (expr.get_scrutinee_expr ());
422 : :
423 : 1052 : std::vector<HIR::MatchCase> match_arms;
424 : 3498 : for (auto &match_case : expr.get_match_cases ())
425 : : {
426 : 2446 : HIR::Expr *kase_expr
427 : 2446 : = ASTLoweringExpr::translate (match_case.get_expr ());
428 : :
429 : 2446 : HIR::Expr *kase_guard_expr = nullptr;
430 : 2446 : if (match_case.get_arm ().has_match_arm_guard ())
431 : : {
432 : 1 : kase_guard_expr = ASTLoweringExpr::translate (
433 : : match_case.get_arm ().get_guard_expr ());
434 : : }
435 : :
436 : 2446 : std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns;
437 : 2446 : match_arm_patterns.reserve (
438 : 2446 : match_case.get_arm ().get_patterns ().size ());
439 : :
440 : 4892 : for (auto &pattern : match_case.get_arm ().get_patterns ())
441 : 2446 : match_arm_patterns.emplace_back (
442 : 2446 : ASTLoweringPattern::translate (*pattern));
443 : :
444 : 2446 : HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (),
445 : 2446 : std::unique_ptr<HIR::Expr> (kase_guard_expr),
446 : 2446 : match_case.get_arm ().get_outer_attrs ());
447 : :
448 : 2446 : auto crate_num = mappings.get_current_crate ();
449 : 4892 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
450 : 2446 : mappings.get_next_hir_id (crate_num),
451 : 2446 : UNKNOWN_LOCAL_DEFID);
452 : :
453 : 2446 : match_arms.emplace_back (std::move (mapping), std::move (arm),
454 : 2446 : std::unique_ptr<HIR::Expr> (kase_expr));
455 : 2446 : }
456 : :
457 : 1052 : auto crate_num = mappings.get_current_crate ();
458 : 2104 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
459 : 1052 : mappings.get_next_hir_id (crate_num),
460 : 1052 : UNKNOWN_LOCAL_DEFID);
461 : :
462 : 1052 : translated
463 : 1052 : = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
464 : 1052 : std::move (match_arms), expr.get_inner_attrs (),
465 : 2104 : expr.get_outer_attrs (), expr.get_locus ());
466 : 1052 : }
467 : :
468 : : // rust-ast-lower-expr.h
469 : :
470 : : void
471 : 24087 : ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
472 : : {
473 : 24087 : auto crate_num = mappings.get_current_crate ();
474 : 48174 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
475 : 24087 : mappings.get_next_hir_id (crate_num),
476 : 24087 : UNKNOWN_LOCAL_DEFID);
477 : :
478 : 24087 : if (expr.is_lang_item ())
479 : : {
480 : 288 : translated = new HIR::PathInExpression (mapping, expr.get_lang_item (),
481 : 144 : expr.get_locus (), false);
482 : 144 : return;
483 : : }
484 : :
485 : 23943 : auto &segments = expr.get_segments ();
486 : :
487 : 23943 : std::vector<HIR::PathExprSegment> path_segments;
488 : 23943 : path_segments.reserve (segments.size ());
489 : :
490 : 56929 : for (auto &s : segments)
491 : : {
492 : 32986 : path_segments.push_back (lower_path_expr_seg ((s)));
493 : :
494 : : // insert the mappings for the segment
495 : 32986 : HIR::PathExprSegment *lowered_seg = &path_segments.back ();
496 : 32986 : mappings.insert_hir_path_expr_seg (lowered_seg);
497 : : }
498 : :
499 : 47886 : translated = new HIR::PathInExpression (mapping, std::move (path_segments),
500 : : expr.get_locus (),
501 : 23943 : expr.opening_scope_resolution ());
502 : 23943 : }
503 : :
504 : : HIR::QualifiedPathType
505 : 114 : ASTLoweringBase::lower_qual_path_type (AST::QualifiedPathType &qualified_type)
506 : : {
507 : 114 : HIR::Type *type = ASTLoweringType::translate (qualified_type.get_type ());
508 : 114 : HIR::TypePath *trait
509 : 114 : = qualified_type.has_as_clause ()
510 : 114 : ? ASTLowerTypePath::translate (qualified_type.get_as_type_path ())
511 : : : nullptr;
512 : :
513 : 114 : auto crate_num = mappings.get_current_crate ();
514 : 114 : Analysis::NodeMapping mapping (crate_num, qualified_type.get_node_id (),
515 : 114 : mappings.get_next_hir_id (crate_num),
516 : 114 : UNKNOWN_LOCAL_DEFID);
517 : :
518 : 114 : return HIR::QualifiedPathType (mapping, std::unique_ptr<HIR::Type> (type),
519 : 228 : std::unique_ptr<HIR::TypePath> (trait),
520 : 114 : qualified_type.get_locus ());
521 : : }
522 : :
523 : : void
524 : 114 : ASTLowerQualPathInExpression::visit (AST::QualifiedPathInExpression &expr)
525 : : {
526 : 114 : HIR::QualifiedPathType qual_path_type
527 : 114 : = lower_qual_path_type (expr.get_qualified_path_type ());
528 : :
529 : 114 : auto &segments = expr.get_segments ();
530 : :
531 : 114 : std::vector<HIR::PathExprSegment> path_segments;
532 : 114 : path_segments.reserve (segments.size ());
533 : :
534 : 228 : for (auto &s : segments)
535 : : {
536 : 114 : path_segments.push_back (lower_path_expr_seg ((s)));
537 : :
538 : : // insert the mappings for the segment
539 : 114 : HIR::PathExprSegment *lowered_seg = &path_segments.back ();
540 : 114 : mappings.insert_hir_path_expr_seg (lowered_seg);
541 : : }
542 : :
543 : 114 : auto crate_num = mappings.get_current_crate ();
544 : 228 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
545 : 114 : mappings.get_next_hir_id (crate_num),
546 : 114 : UNKNOWN_LOCAL_DEFID);
547 : :
548 : 114 : translated = new HIR::QualifiedPathInExpression (mapping, qual_path_type,
549 : : std::move (path_segments),
550 : : expr.get_locus (),
551 : 228 : expr.get_outer_attrs ());
552 : 114 : }
553 : :
554 : : ClosureParam
555 : 61 : ASTLoweringBase::lower_closure_param (AST::ClosureParam ¶m)
556 : : {
557 : 61 : HIR::Pattern *param_pattern
558 : 61 : = ASTLoweringPattern::translate (param.get_pattern ());
559 : :
560 : 61 : HIR::Type *param_type = param.has_type_given ()
561 : 61 : ? ASTLoweringType::translate (param.get_type ())
562 : : : nullptr;
563 : :
564 : 122 : return HIR::ClosureParam (std::unique_ptr<HIR::Pattern> (param_pattern),
565 : : param.get_locus (),
566 : 61 : param.has_type_given ()
567 : 122 : ? std::unique_ptr<HIR::Type> (param_type)
568 : : : nullptr,
569 : 183 : param.get_outer_attrs ());
570 : : }
571 : :
572 : : } // namespace HIR
573 : : } // namespace Rust
|