Skip to content

Commit b257ae6

Browse files
committed
ast: Add optional diverging else to AST::LetStmt
gcc/rust/ChangeLog: * ast/rust-stmt.h (class LetStmt): Add optional expression for diverging else. * ast/rust-ast-builder.cc (Builder::let): Use new API.
1 parent b79c452 commit b257ae6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

gcc/rust/ast/rust-ast-builder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-ast-builder.h"
20+
#include "optional.h"
2021
#include "rust-ast-builder-type.h"
2122
#include "rust-ast.h"
2223
#include "rust-common.h"
@@ -352,7 +353,7 @@ Builder::let (std::unique_ptr<Pattern> &&pattern, std::unique_ptr<Type> &&type,
352353
{
353354
return std::unique_ptr<Stmt> (new LetStmt (std::move (pattern),
354355
std::move (init), std::move (type),
355-
{}, loc));
356+
tl::nullopt, {}, loc));
356357
}
357358

358359
std::unique_ptr<Expr>

gcc/rust/ast/rust-stmt.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef RUST_AST_STATEMENT_H
2020
#define RUST_AST_STATEMENT_H
2121

22+
#include "optional.h"
2223
#include "rust-ast.h"
2324
#include "rust-path.h"
2425
#include "rust-expr.h"
@@ -72,6 +73,8 @@ class LetStmt : public Stmt
7273
// bool has_init_expr;
7374
std::unique_ptr<Expr> init_expr;
7475

76+
tl::optional<std::unique_ptr<Expr>> else_expr;
77+
7578
location_t locus;
7679

7780
public:
@@ -85,15 +88,18 @@ class LetStmt : public Stmt
8588

8689
// Returns whether let statement has an initialisation expression.
8790
bool has_init_expr () const { return init_expr != nullptr; }
91+
bool has_else_expr () const { return else_expr.has_value (); }
8892

8993
std::string as_string () const override;
9094

9195
LetStmt (std::unique_ptr<Pattern> variables_pattern,
9296
std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type,
97+
tl::optional<std::unique_ptr<Expr>> else_expr,
9398
std::vector<Attribute> outer_attrs, location_t locus)
9499
: outer_attrs (std::move (outer_attrs)),
95100
variables_pattern (std::move (variables_pattern)),
96-
type (std::move (type)), init_expr (std::move (init_expr)), locus (locus)
101+
type (std::move (type)), init_expr (std::move (init_expr)),
102+
else_expr (std::move (else_expr)), locus (locus)
97103
{}
98104

99105
// Copy constructor with clone
@@ -107,6 +113,9 @@ class LetStmt : public Stmt
107113
// guard to prevent null dereference (always required)
108114
if (other.init_expr != nullptr)
109115
init_expr = other.init_expr->clone_expr ();
116+
if (other.else_expr.has_value ())
117+
else_expr = other.else_expr.value ()->clone_expr ();
118+
110119
if (other.type != nullptr)
111120
type = other.type->clone_type ();
112121
}
@@ -128,6 +137,12 @@ class LetStmt : public Stmt
128137
init_expr = other.init_expr->clone_expr ();
129138
else
130139
init_expr = nullptr;
140+
141+
if (other.else_expr != nullptr)
142+
else_expr = other.else_expr.value ()->clone_expr ();
143+
else
144+
else_expr = tl::nullopt;
145+
131146
if (other.type != nullptr)
132147
type = other.type->clone_type ();
133148
else
@@ -162,12 +177,24 @@ class LetStmt : public Stmt
162177
return *init_expr;
163178
}
164179

180+
Expr &get_else_expr ()
181+
{
182+
rust_assert (has_else_expr ());
183+
return *else_expr.value ();
184+
}
185+
165186
std::unique_ptr<Expr> &get_init_expr_ptr ()
166187
{
167188
rust_assert (has_init_expr ());
168189
return init_expr;
169190
}
170191

192+
std::unique_ptr<Expr> &get_else_expr_ptr ()
193+
{
194+
rust_assert (has_else_expr ());
195+
return else_expr.value ();
196+
}
197+
171198
Pattern &get_pattern ()
172199
{
173200
rust_assert (variables_pattern != nullptr);

0 commit comments

Comments
 (0)