aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast/rust-desugar-try-block.cc
blob: 07f06aabec892724f16e362cfddf772a6ccce949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include "rust-desugar-try-block.h"
#include "rust-ast-builder.h"
#include "rust-expr.h"

namespace Rust {
namespace AST {

DesugarTryBlock::DesugarTryBlock () {}

void
DesugarTryBlock::go (std::unique_ptr<Expr> &ptr)
{
  rust_assert (ptr->get_expr_kind () == Expr::Kind::Try);

  auto original = static_cast<TryExpr &> (*ptr);
  auto desugared = DesugarTryBlock ().desugar (original);

  ptr = std::move (desugared);
}

std::unique_ptr<Expr>
DesugarTryBlock::desugar (TryExpr &expr)
{
  auto builder = Builder (expr.get_locus ());
  auto &block = expr.get_block_expr ();

  if (block.has_statements ())
    rust_sorry_at (expr.get_locus (),
		   "cannot desugar try-blocks with statements");

  auto tail_expr = builder.tuple ();

  if (block.has_tail_expr ())
    tail_expr = block.get_tail_expr ().clone_expr ();

  // Wrap in Try::from_ok call
  auto from_ok = builder.path_in_expression (LangItem::Kind::TRY_FROM_OK);
  auto call = builder.call (ptrify (from_ok), std::move (tail_expr));

  return builder.block (std::move (call));
}

} // namespace AST
} // namespace Rust