| 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 |
#ifndef RUST_DESUGAR_QUESTION_MARK |
| 20 |
#define RUST_DESUGAR_QUESTION_MARK |
| 21 |
|
| 22 |
#include "rust-ast-visitor.h" |
| 23 |
#include "rust-expr.h" |
| 24 |
#include "rust-stmt.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace AST { |
| 28 |
|
| 29 |
// NOTE: One more complexity compare to desugaring for-loops is that we need to |
| 30 |
// desugar every possible expression... should we do that during lowering |
| 31 |
// instead? but would it get resolved and expanded etc? Not sure... |
| 32 |
|
| 33 |
// The goal of this desugar is to go from this: |
| 34 |
// |
| 35 |
// ``` |
| 36 |
// <expr>? |
| 37 |
// ``` |
| 38 |
// |
| 39 |
// to this: |
| 40 |
// |
| 41 |
// ``` |
| 42 |
// match Try::into_result(<expr>) { |
| 43 |
// Ok(val) => val, |
| 44 |
// Err(err) => return Try::from_err(From::from(err)) |
| 45 |
// } |
| 46 |
// ``` |
| 47 |
// |
| 48 |
// We use lang items for almost everything, so the actual desugared code looks |
| 49 |
// more like this: |
| 50 |
// |
| 51 |
// ``` |
| 52 |
// match #[lang = "into_result"](<expr>) { |
| 53 |
// #[lang = "Ok"](val) => val, |
| 54 |
// #[lang = "Err"](err) => { |
| 55 |
// return #[lang = "from_error"](#[lang ="from"](err)) |
| 56 |
// } |
| 57 |
// } |
| 58 |
// ``` |
| 59 |
class DesugarQuestionMark : public DefaultASTVisitor |
| 60 |
{ |
| 61 |
using DefaultASTVisitor::visit; |
| 62 |
|
| 63 |
public: |
| 64 |
DesugarQuestionMark (); |
| 65 |
void go (AST::Crate &); |
| 66 |
|
| 67 |
private: |
| 68 |
void desugar_and_replace (std::unique_ptr<Expr> &ptr); |
| 69 |
std::unique_ptr<Expr> desugar (ErrorPropagationExpr &); |
| 70 |
|
| 71 |
void visit (AST::ExprStmt &) override; |
| 72 |
void visit (AST::CallExpr &) override; |
| 73 |
void visit (AST::LetStmt &) override; |
| 74 |
}; |
| 75 |
|
| 76 |
} // namespace AST |
| 77 |
} // namespace Rust |
| 78 |
|
| 79 |
#endif // ! RUST_DESUGAR_QUESTION_MARK |