Branch data Line data Source code
1 : : // Copyright (C) 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-desugar-try-block.h"
20 : : #include "rust-ast-builder.h"
21 : : #include "rust-expr.h"
22 : :
23 : : namespace Rust {
24 : : namespace AST {
25 : :
26 : 1 : DesugarTryBlock::DesugarTryBlock () {}
27 : :
28 : : void
29 : 1 : DesugarTryBlock::go (std::unique_ptr<Expr> &ptr)
30 : : {
31 : 1 : rust_assert (ptr->get_expr_kind () == Expr::Kind::Try);
32 : :
33 : 1 : auto original = static_cast<TryExpr &> (*ptr);
34 : 1 : auto desugared = DesugarTryBlock ().desugar (original);
35 : :
36 : 1 : ptr = std::move (desugared);
37 : 1 : }
38 : :
39 : : std::unique_ptr<Expr>
40 : 1 : DesugarTryBlock::desugar (TryExpr &expr)
41 : : {
42 : 1 : auto builder = Builder (expr.get_locus ());
43 : 1 : auto &block = expr.get_block_expr ();
44 : :
45 : 1 : if (block.has_statements ())
46 : 0 : rust_sorry_at (expr.get_locus (),
47 : : "cannot desugar try-blocks with statements");
48 : :
49 : 1 : auto tail_expr = builder.tuple ();
50 : :
51 : 1 : if (block.has_tail_expr ())
52 : 1 : tail_expr = block.get_tail_expr ().clone_expr ();
53 : :
54 : : // Wrap in Try::from_ok call
55 : 1 : auto from_ok = builder.path_in_expression (LangItem::Kind::TRY_FROM_OK);
56 : 2 : auto call = builder.call (ptrify (from_ok), std::move (tail_expr));
57 : :
58 : 1 : return builder.block (std::move (call));
59 : 1 : }
60 : :
61 : : } // namespace AST
62 : : } // namespace Rust
|