blob: e4c513f461fd6342a3d5791e70f70ce89bf6c7f2 (
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// 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/>.
#ifndef RUST_DESUGAR_QUESTION_MARK
#define RUST_DESUGAR_QUESTION_MARK
#include "rust-ast-visitor.h"
#include "rust-expr.h"
#include "rust-stmt.h"
namespace Rust {
namespace AST {
// NOTE: One more complexity compare to desugaring for-loops is that we need to
// desugar every possible expression... should we do that during lowering
// instead? but would it get resolved and expanded etc? Not sure...
// The goal of this desugar is to go from this:
//
// ```
// <expr>?
// ```
//
// to this:
//
// ```
// match Try::into_result(<expr>) {
// Ok(val) => val,
// Err(err) => return Try::from_err(From::from(err))
// }
// ```
//
// We use lang items for almost everything, so the actual desugared code looks
// more like this:
//
// ```
// match #[lang = "into_result"](<expr>) {
// #[lang = "Ok"](val) => val,
// #[lang = "Err"](err) => {
// return #[lang = "from_error"](#[lang ="from"](err))
// }
// }
// ```
class DesugarQuestionMark : public DefaultASTVisitor
{
using DefaultASTVisitor::visit;
public:
DesugarQuestionMark ();
void go (AST::Crate &);
private:
void desugar_and_replace (std::unique_ptr<Expr> &ptr);
std::unique_ptr<Expr> desugar (ErrorPropagationExpr &);
void visit (AST::ExprStmt &) override;
void visit (AST::CallExpr &) override;
void visit (AST::LetStmt &) override;
};
} // namespace AST
} // namespace Rust
#endif // ! RUST_DESUGAR_QUESTION_MARK
|