public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Lower IfLet expressions.
Date: Wed,  8 Jun 2022 12:45:14 +0000 (GMT)	[thread overview]
Message-ID: <20220608124514.CCB643895FE4@sourceware.org> (raw)

https://gcc.gnu.org/g:245215bf8d11025dfca3c7003ce10151aa7ba4e4

commit 245215bf8d11025dfca3c7003ce10151aa7ba4e4
Author: antego <antego@users.noreply.github.com>
Date:   Thu May 5 23:00:48 2022 +1000

    Lower IfLet expressions.
    
    Addresses #1177.

Diff:
---
 gcc/rust/hir/rust-ast-lower-block.h | 34 ++++++++++++++++++++++++++++++++++
 gcc/rust/hir/rust-ast-lower.cc      | 28 ++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/gcc/rust/hir/rust-ast-lower-block.h b/gcc/rust/hir/rust-ast-lower-block.h
index 512f4709c6d..5c328b53446 100644
--- a/gcc/rust/hir/rust-ast-lower-block.h
+++ b/gcc/rust/hir/rust-ast-lower-block.h
@@ -120,6 +120,35 @@ private:
   bool terminated;
 };
 
+class ASTLoweringIfLetBlock : public ASTLoweringBase
+{
+  using Rust::HIR::ASTLoweringBase::visit;
+
+public:
+  static HIR::IfLetExpr *translate (AST::IfLetExpr *expr)
+  {
+    ASTLoweringIfLetBlock resolver;
+    expr->accept_vis (resolver);
+    if (resolver.translated != nullptr)
+      {
+	resolver.mappings->insert_hir_expr (
+	  resolver.translated->get_mappings ().get_crate_num (),
+	  resolver.translated->get_mappings ().get_hirid (),
+	  resolver.translated);
+      }
+    return resolver.translated;
+  }
+
+  ~ASTLoweringIfLetBlock () {}
+
+  void visit (AST::IfLetExpr &expr) override;
+
+private:
+  ASTLoweringIfLetBlock () : ASTLoweringBase (), translated (nullptr) {}
+
+  HIR::IfLetExpr *translated;
+};
+
 class ASTLoweringExprWithBlock : public ASTLoweringBase
 {
   using Rust::HIR::ASTLoweringBase::visit;
@@ -159,6 +188,11 @@ public:
     translated = ASTLoweringIfBlock::translate (&expr, &terminated);
   }
 
+  void visit (AST::IfLetExpr &expr) override
+  {
+    translated = ASTLoweringIfLetBlock::translate (&expr);
+  }
+
   void visit (AST::BlockExpr &expr) override
   {
     translated = ASTLoweringBlock::translate (&expr, &terminated);
diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index d861207ebe8..d8a04883232 100644
--- a/gcc/rust/hir/rust-ast-lower.cc
+++ b/gcc/rust/hir/rust-ast-lower.cc
@@ -223,6 +223,34 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqIf &expr)
 			       expr.get_locus ());
 }
 
+void
+ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
+{
+  std::vector<std::unique_ptr<HIR::Pattern> > patterns;
+  for (auto &pattern : expr.get_patterns ())
+    {
+      HIR::Pattern *ptrn = ASTLoweringPattern::translate (pattern.get ());
+      patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
+    }
+  HIR::Expr *value_ptr
+    = ASTLoweringExpr::translate (expr.get_value_expr ().get ());
+
+  bool ignored_terminated = false;
+  HIR::BlockExpr *block
+    = ASTLoweringBlock::translate (expr.get_if_block ().get (),
+				   &ignored_terminated);
+
+  auto crate_num = mappings->get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
+				 mappings->get_next_hir_id (crate_num),
+				 UNKNOWN_LOCAL_DEFID);
+
+  translated = new HIR::IfLetExpr (mapping, std::move (patterns),
+				   std::unique_ptr<HIR::Expr> (value_ptr),
+				   std::unique_ptr<HIR::BlockExpr> (block),
+				   expr.get_locus ());
+}
+
 // rust-ast-lower-struct-field-expr.h
 
 void


                 reply	other threads:[~2022-06-08 12:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608124514.CCB643895FE4@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).