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