19
19
#ifndef RUST_AST_STATEMENT_H
20
20
#define RUST_AST_STATEMENT_H
21
21
22
+ #include " optional.h"
22
23
#include " rust-ast.h"
23
24
#include " rust-path.h"
24
25
#include " rust-expr.h"
@@ -72,6 +73,8 @@ class LetStmt : public Stmt
72
73
// bool has_init_expr;
73
74
std::unique_ptr<Expr> init_expr;
74
75
76
+ tl::optional<std::unique_ptr<Expr>> else_expr;
77
+
75
78
location_t locus;
76
79
77
80
public:
@@ -85,15 +88,18 @@ class LetStmt : public Stmt
85
88
86
89
// Returns whether let statement has an initialisation expression.
87
90
bool has_init_expr () const { return init_expr != nullptr ; }
91
+ bool has_else_expr () const { return else_expr.has_value (); }
88
92
89
93
std::string as_string () const override ;
90
94
91
95
LetStmt (std::unique_ptr<Pattern> variables_pattern,
92
96
std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type,
97
+ tl::optional<std::unique_ptr<Expr>> else_expr,
93
98
std::vector<Attribute> outer_attrs, location_t locus)
94
99
: outer_attrs (std::move (outer_attrs)),
95
100
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)
97
103
{}
98
104
99
105
// Copy constructor with clone
@@ -107,6 +113,9 @@ class LetStmt : public Stmt
107
113
// guard to prevent null dereference (always required)
108
114
if (other.init_expr != nullptr )
109
115
init_expr = other.init_expr ->clone_expr ();
116
+ if (other.else_expr .has_value ())
117
+ else_expr = other.else_expr .value ()->clone_expr ();
118
+
110
119
if (other.type != nullptr )
111
120
type = other.type ->clone_type ();
112
121
}
@@ -128,6 +137,12 @@ class LetStmt : public Stmt
128
137
init_expr = other.init_expr ->clone_expr ();
129
138
else
130
139
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
+
131
146
if (other.type != nullptr )
132
147
type = other.type ->clone_type ();
133
148
else
@@ -162,12 +177,24 @@ class LetStmt : public Stmt
162
177
return *init_expr;
163
178
}
164
179
180
+ Expr &get_else_expr ()
181
+ {
182
+ rust_assert (has_else_expr ());
183
+ return *else_expr.value ();
184
+ }
185
+
165
186
std::unique_ptr<Expr> &get_init_expr_ptr ()
166
187
{
167
188
rust_assert (has_init_expr ());
168
189
return init_expr;
169
190
}
170
191
192
+ std::unique_ptr<Expr> &get_else_expr_ptr ()
193
+ {
194
+ rust_assert (has_else_expr ());
195
+ return else_expr.value ();
196
+ }
197
+
171
198
Pattern &get_pattern ()
172
199
{
173
200
rust_assert (variables_pattern != nullptr );
0 commit comments