From: Arthur Cohen <arthur.co...@embecosm.com>

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.
---
 gcc/rust/ast/rust-ast-builder.cc |  3 ++-
 gcc/rust/ast/rust-stmt.h         | 29 ++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 86290e199c6..cdc6eec254b 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-ast-builder.h"
+#include "optional.h"
 #include "rust-ast-builder-type.h"
 #include "rust-ast.h"
 #include "rust-common.h"
@@ -352,7 +353,7 @@ Builder::let (std::unique_ptr<Pattern> &&pattern, 
std::unique_ptr<Type> &&type,
 {
   return std::unique_ptr<Stmt> (new LetStmt (std::move (pattern),
                                             std::move (init), std::move (type),
-                                            {}, loc));
+                                            tl::nullopt, {}, loc));
 }
 
 std::unique_ptr<Expr>
diff --git a/gcc/rust/ast/rust-stmt.h b/gcc/rust/ast/rust-stmt.h
index 6cbecaffd03..f843a79b3f9 100644
--- a/gcc/rust/ast/rust-stmt.h
+++ b/gcc/rust/ast/rust-stmt.h
@@ -19,6 +19,7 @@
 #ifndef RUST_AST_STATEMENT_H
 #define RUST_AST_STATEMENT_H
 
+#include "optional.h"
 #include "rust-ast.h"
 #include "rust-path.h"
 #include "rust-expr.h"
@@ -72,6 +73,8 @@ class LetStmt : public Stmt
   // bool has_init_expr;
   std::unique_ptr<Expr> init_expr;
 
+  tl::optional<std::unique_ptr<Expr>> else_expr;
+
   location_t locus;
 
 public:
@@ -85,15 +88,18 @@ public:
 
   // Returns whether let statement has an initialisation expression.
   bool has_init_expr () const { return init_expr != nullptr; }
+  bool has_else_expr () const { return else_expr.has_value (); }
 
   std::string as_string () const override;
 
   LetStmt (std::unique_ptr<Pattern> variables_pattern,
           std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type,
+          tl::optional<std::unique_ptr<Expr>> else_expr,
           std::vector<Attribute> outer_attrs, location_t locus)
     : outer_attrs (std::move (outer_attrs)),
       variables_pattern (std::move (variables_pattern)),
-      type (std::move (type)), init_expr (std::move (init_expr)), locus (locus)
+      type (std::move (type)), init_expr (std::move (init_expr)),
+      else_expr (std::move (else_expr)), locus (locus)
   {}
 
   // Copy constructor with clone
@@ -107,6 +113,9 @@ public:
     // guard to prevent null dereference (always required)
     if (other.init_expr != nullptr)
       init_expr = other.init_expr->clone_expr ();
+    if (other.else_expr.has_value ())
+      else_expr = other.else_expr.value ()->clone_expr ();
+
     if (other.type != nullptr)
       type = other.type->clone_type ();
   }
@@ -128,6 +137,12 @@ public:
       init_expr = other.init_expr->clone_expr ();
     else
       init_expr = nullptr;
+
+    if (other.else_expr != nullptr)
+      else_expr = other.else_expr.value ()->clone_expr ();
+    else
+      else_expr = tl::nullopt;
+
     if (other.type != nullptr)
       type = other.type->clone_type ();
     else
@@ -162,12 +177,24 @@ public:
     return *init_expr;
   }
 
+  Expr &get_else_expr ()
+  {
+    rust_assert (has_else_expr ());
+    return *else_expr.value ();
+  }
+
   std::unique_ptr<Expr> &get_init_expr_ptr ()
   {
     rust_assert (has_init_expr ());
     return init_expr;
   }
 
+  std::unique_ptr<Expr> &get_else_expr_ptr ()
+  {
+    rust_assert (has_else_expr ());
+    return else_expr.value ();
+  }
+
   Pattern &get_pattern ()
   {
     rust_assert (variables_pattern != nullptr);
-- 
2.49.0

Reply via email to