Debian Codegraph @

gcc/rust/ast/rust-desugar-question-mark.cc

raw
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-question-mark.h"
20 #include "rust-ast-builder.h"
21 #include "rust-ast-visitor.h"
22
23 namespace Rust {
24 namespace AST {
25
26 DesugarQuestionMark::DesugarQuestionMark () {}
27
28 void
29 DesugarQuestionMark::go (AST::Crate &crate)
30 {
31 DesugarQuestionMark::visit (crate);
32 }
33
34 void
35 DesugarQuestionMark::visit (ExprStmt &stmt)
36 {
37 if (stmt.get_expr ().get_expr_kind () == Expr::Kind::ErrorPropagation)
38 desugar_and_replace (stmt.get_expr_ptr ());
39
40 DefaultASTVisitor::visit (stmt);
41 }
42
43 void
44 DesugarQuestionMark::visit (CallExpr &call)
45 {
46 if (call.get_function_expr ().get_expr_kind ()
47 == Expr::Kind::ErrorPropagation)
48 desugar_and_replace (call.get_function_expr_ptr ());
49
50 for (auto &arg : call.get_params ())
51 if (arg->get_expr_kind () == Expr::Kind::ErrorPropagation)
52 desugar_and_replace (arg);
53
54 DefaultASTVisitor::visit (call);
55 }
56
57 void
58 DesugarQuestionMark::visit (LetStmt &stmt)
59 {
60 if (stmt.has_init_expr ()
61 && stmt.get_init_expr ().get_expr_kind () == Expr::Kind::ErrorPropagation)
62 desugar_and_replace (stmt.get_init_expr_ptr ());
63
64 DefaultASTVisitor::visit (stmt);
65 }
66
67 MatchArm
68 make_match_arm (std::unique_ptr<Pattern> &&pattern)
69 {
70 auto loc = pattern->get_locus ();
71
72 auto patterns = std::vector<std::unique_ptr<Pattern>> ();
73 patterns.emplace_back (std::move (pattern));
74
75 return MatchArm (std::move (patterns), loc);
76 }
77
78 MatchCase
79 ok_case (Builder &builder)
80 {
81 auto val = builder.identifier_pattern ("val");
82
83 auto patterns = std::vector<std::unique_ptr<Pattern>> ();
84 patterns.emplace_back (std::move (val));
85
86 auto pattern_item = std::unique_ptr<TupleStructItems> (
87 new TupleStructItemsNoRange (std::move (patterns)));
88 auto pattern = std::unique_ptr<Pattern> (new TupleStructPattern (
89 builder.path_in_expression (LangItem::Kind::RESULT_OK),
90 std::move (pattern_item)));
91
92 auto arm = make_match_arm (std::move (pattern));
93
94 auto ret_val = builder.identifier ("val");
95
96 return MatchCase (std::move (arm), std::move (ret_val));
97 }
98
99 MatchCase
100 err_case (Builder &builder)
101 {
102 auto val = builder.identifier_pattern ("err");
103
104 auto patterns = std::vector<std::unique_ptr<Pattern>> ();
105 patterns.emplace_back (std::move (val));
106
107 auto pattern_item = std::unique_ptr<TupleStructItems> (
108 new TupleStructItemsNoRange (std::move (patterns)));
109 auto pattern = std::unique_ptr<Pattern> (new TupleStructPattern (
110 builder.path_in_expression (LangItem::Kind::RESULT_ERR),
111 std::move (pattern_item)));
112
113 auto arm = make_match_arm (std::move (pattern));
114
115 auto try_from_err = std::make_unique<PathInExpression> (
116 builder.path_in_expression (LangItem::Kind::TRY_FROM_ERROR));
117 auto from_from = std::make_unique<PathInExpression> (
118 builder.path_in_expression (LangItem::Kind::FROM_FROM));
119
120 auto early_return = builder.return_expr (
121 builder.call (std::move (try_from_err),
122 builder.call (std::move (from_from),
123 builder.identifier ("err"))));
124
125 return MatchCase (std::move (arm), std::move (early_return));
126 }
127
128 std::unique_ptr<Expr>
129 DesugarQuestionMark::desugar (ErrorPropagationExpr &expr)
130 {
131 auto builder = Builder (expr.get_locus ());
132
133 // Try::into_result(<expr>)
134 auto try_into = std::make_unique<PathInExpression> (
135 builder.path_in_expression (LangItem::Kind::TRY_INTO_RESULT));
136 auto call = builder.call (std::move (try_into),
137 expr.get_propagating_expr ().clone_expr ());
138
139 // Ok(val) => val,
140 auto ok_match_case = ok_case (builder);
141 // Err(err) => return Try::from_error(From::from(err)),
142 auto err_match_case = err_case (builder);
143
144 auto cases = std::vector<MatchCase> ();
145 cases.emplace_back (ok_match_case);
146 cases.emplace_back (err_match_case);
147
148 // match <call> {
149 // <ok_arm>
150 // <err_arm>
151 // }
152 return std::unique_ptr<MatchExpr> (new MatchExpr (std::move (call),
153 std::move (cases), {}, {},
154 expr.get_locus ()));
155 }
156
157 void
158 DesugarQuestionMark::desugar_and_replace (std::unique_ptr<Expr> &ptr)
159 {
160 auto original = static_cast<ErrorPropagationExpr &> (*ptr);
161 auto desugared = desugar (original);
162
163 ptr = std::move (desugared);
164 }
165
166 } // namespace AST
167 } // namespace Rust