public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add HIR Lowering for TuplePattern
@ 2022-06-08 12:33 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:33 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c18257c7265470a071f7ed9fe29899ece839fcf4

commit c18257c7265470a071f7ed9fe29899ece839fcf4
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Wed Apr 20 18:04:23 2022 +0100

    Add HIR Lowering for TuplePattern

Diff:
---
 gcc/rust/hir/rust-ast-lower-base.cc               | 75 +++++++++++++++++++++++
 gcc/rust/hir/rust-ast-lower-base.h                |  8 +++
 gcc/rust/hir/rust-ast-lower-expr.h                | 36 ++---------
 gcc/rust/hir/rust-ast-lower-pattern.cc            | 45 ++++++++++++++
 gcc/rust/hir/rust-ast-lower-pattern.h             |  4 ++
 gcc/rust/hir/tree/rust-hir-expr.h                 |  4 +-
 gcc/rust/typecheck/rust-hir-type-check-enumitem.h |  6 +-
 gcc/rust/typecheck/rust-hir-type-check-expr.h     |  4 +-
 8 files changed, 143 insertions(+), 39 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc
index fa7c700bda0..28895ba197d 100644
--- a/gcc/rust/hir/rust-ast-lower-base.cc
+++ b/gcc/rust/hir/rust-ast-lower-base.cc
@@ -18,6 +18,7 @@
 
 #include "rust-ast-lower-base.h"
 #include "rust-ast-lower-type.h"
+#include "rust-ast-lower-pattern.h"
 
 namespace Rust {
 namespace HIR {
@@ -885,5 +886,79 @@ ASTLoweringBase::attribute_handled_in_another_pass (
   return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
 }
 
+std::unique_ptr<HIR::TuplePatternItems>
+ASTLoweringBase::lower_tuple_pattern_multiple (
+  AST::TuplePatternItemsMultiple &pattern)
+{
+  std::vector<std::unique_ptr<HIR::Pattern> > patterns;
+  for (auto &p : pattern.get_patterns ())
+    {
+      HIR::Pattern *translated = ASTLoweringPattern::translate (p.get ());
+      patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
+    }
+
+  return std::unique_ptr<HIR::TuplePatternItems> (
+    new HIR::TuplePatternItemsMultiple (std::move (patterns)));
+}
+
+std::unique_ptr<TuplePatternItems>
+ASTLoweringBase::lower_tuple_pattern_ranged (
+  AST::TuplePatternItemsRanged &pattern)
+{
+  std::vector<std::unique_ptr<HIR::Pattern> > lower_patterns;
+  std::vector<std::unique_ptr<HIR::Pattern> > upper_patterns;
+
+  for (auto &p : pattern.get_lower_patterns ())
+    {
+      HIR::Pattern *translated = ASTLoweringPattern::translate (p.get ());
+      lower_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
+    }
+
+  for (auto &p : pattern.get_upper_patterns ())
+    {
+      HIR::Pattern *translated = ASTLoweringPattern::translate (p.get ());
+      upper_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
+    }
+
+  return std::unique_ptr<HIR::TuplePatternItems> (
+    new HIR::TuplePatternItemsRanged (std::move (lower_patterns),
+				      std::move (upper_patterns)));
+}
+
+HIR::Literal
+ASTLoweringBase::lower_literal (const AST::Literal &literal)
+{
+  HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
+  switch (literal.get_lit_type ())
+    {
+    case AST::Literal::LitType::CHAR:
+      type = HIR::Literal::LitType::CHAR;
+      break;
+    case AST::Literal::LitType::STRING:
+      type = HIR::Literal::LitType::STRING;
+      break;
+    case AST::Literal::LitType::BYTE:
+      type = HIR::Literal::LitType::BYTE;
+      break;
+    case AST::Literal::LitType::BYTE_STRING:
+      type = HIR::Literal::LitType::BYTE_STRING;
+      break;
+    case AST::Literal::LitType::INT:
+      type = HIR::Literal::LitType::INT;
+      break;
+    case AST::Literal::LitType::FLOAT:
+      type = HIR::Literal::LitType::FLOAT;
+      break;
+    case AST::Literal::LitType::BOOL:
+      type = HIR::Literal::LitType::BOOL;
+      break;
+    case AST::Literal::LitType::ERROR:
+      gcc_unreachable ();
+      break;
+    }
+
+  return HIR::Literal (literal.as_string (), type, literal.get_type_hint ());
+}
+
 } // namespace HIR
 } // namespace Rust
diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h
index 185f4672499..33009ae8c2d 100644
--- a/gcc/rust/hir/rust-ast-lower-base.h
+++ b/gcc/rust/hir/rust-ast-lower-base.h
@@ -272,6 +272,14 @@ protected:
 
   bool
   attribute_handled_in_another_pass (const std::string &attribute_path) const;
+
+  std::unique_ptr<TuplePatternItems>
+  lower_tuple_pattern_multiple (AST::TuplePatternItemsMultiple &pattern);
+
+  std::unique_ptr<TuplePatternItems>
+  lower_tuple_pattern_ranged (AST::TuplePatternItemsRanged &pattern);
+
+  HIR::Literal lower_literal (const AST::Literal &literal);
 };
 
 } // namespace HIR
diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h
index 022002e18e2..5ae538621aa 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.h
+++ b/gcc/rust/hir/rust-ast-lower-expr.h
@@ -327,43 +327,15 @@ public:
 
   void visit (AST::LiteralExpr &expr) override
   {
-    HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
-    switch (expr.get_lit_type ())
-      {
-      case AST::Literal::LitType::CHAR:
-	type = HIR::Literal::LitType::CHAR;
-	break;
-      case AST::Literal::LitType::STRING:
-	type = HIR::Literal::LitType::STRING;
-	break;
-      case AST::Literal::LitType::BYTE:
-	type = HIR::Literal::LitType::BYTE;
-	break;
-      case AST::Literal::LitType::BYTE_STRING:
-	type = HIR::Literal::LitType::BYTE_STRING;
-	break;
-      case AST::Literal::LitType::INT:
-	type = HIR::Literal::LitType::INT;
-	break;
-      case AST::Literal::LitType::FLOAT:
-	type = HIR::Literal::LitType::FLOAT;
-	break;
-      case AST::Literal::LitType::BOOL:
-	type = HIR::Literal::LitType::BOOL;
-	break;
-	// Error literals should have been stripped during expansion
-      case AST::Literal::LitType::ERROR:
-	gcc_unreachable ();
-	break;
-      }
     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::LiteralExpr (mapping, expr.as_string (), type,
-				       expr.get_literal ().get_type_hint (),
-				       expr.get_locus ());
+    HIR::Literal l = lower_literal (expr.get_literal ());
+    translated
+      = new HIR::LiteralExpr (mapping, std::move (l), expr.get_locus (),
+			      expr.get_outer_attrs ());
   }
 
   void visit (AST::ArithmeticOrLogicalExpr &expr) override
diff --git a/gcc/rust/hir/rust-ast-lower-pattern.cc b/gcc/rust/hir/rust-ast-lower-pattern.cc
index 9d733f35c12..957f8cd003b 100644
--- a/gcc/rust/hir/rust-ast-lower-pattern.cc
+++ b/gcc/rust/hir/rust-ast-lower-pattern.cc
@@ -163,5 +163,50 @@ ASTLoweringPattern::visit (AST::WildcardPattern &pattern)
   translated = new HIR::WildcardPattern (mapping, pattern.get_locus ());
 }
 
+void
+ASTLoweringPattern::visit (AST::TuplePattern &pattern)
+{
+  std::unique_ptr<HIR::TuplePatternItems> items;
+  switch (pattern.get_items ()->get_pattern_type ())
+    {
+      case AST::TuplePatternItems::TuplePatternItemType::MULTIPLE: {
+	AST::TuplePatternItemsMultiple &ref
+	  = *static_cast<AST::TuplePatternItemsMultiple *> (
+	    pattern.get_items ().get ());
+	items = lower_tuple_pattern_multiple (ref);
+      }
+      break;
+
+      case AST::TuplePatternItems::TuplePatternItemType::RANGED: {
+	AST::TuplePatternItemsRanged &ref
+	  = *static_cast<AST::TuplePatternItemsRanged *> (
+	    pattern.get_items ().get ());
+	items = lower_tuple_pattern_ranged (ref);
+      }
+      break;
+    }
+
+  auto crate_num = mappings->get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (),
+				 mappings->get_next_hir_id (crate_num),
+				 UNKNOWN_LOCAL_DEFID);
+
+  translated
+    = new HIR::TuplePattern (mapping, std::move (items), pattern.get_locus ());
+}
+
+void
+ASTLoweringPattern::visit (AST::LiteralPattern &pattern)
+{
+  auto crate_num = mappings->get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (),
+				 mappings->get_next_hir_id (crate_num),
+				 UNKNOWN_LOCAL_DEFID);
+
+  HIR::Literal l = lower_literal (pattern.get_literal ());
+  translated
+    = new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus ());
+}
+
 } // namespace HIR
 } // namespace Rust
diff --git a/gcc/rust/hir/rust-ast-lower-pattern.h b/gcc/rust/hir/rust-ast-lower-pattern.h
index 80ff97ade19..32e9dacbbd4 100644
--- a/gcc/rust/hir/rust-ast-lower-pattern.h
+++ b/gcc/rust/hir/rust-ast-lower-pattern.h
@@ -58,6 +58,10 @@ public:
 
   void visit (AST::WildcardPattern &pattern) override;
 
+  void visit (AST::TuplePattern &pattern) override;
+
+  void visit (AST::LiteralPattern &pattern) override;
+
 private:
   ASTLoweringPattern () : translated (nullptr) {}
 
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index 40c9fef8ebb..e585dd2a331 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -77,13 +77,13 @@ public:
 
   LiteralExpr (Analysis::NodeMapping mappings, std::string value_as_string,
 	       Literal::LitType type, PrimitiveCoreType type_hint,
-	       Location locus, AST::AttrVec outer_attrs = AST::AttrVec ())
+	       Location locus, AST::AttrVec outer_attrs)
     : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)),
       literal (std::move (value_as_string), type, type_hint), locus (locus)
   {}
 
   LiteralExpr (Analysis::NodeMapping mappings, Literal literal, Location locus,
-	       AST::AttrVec outer_attrs = AST::AttrVec ())
+	       AST::AttrVec outer_attrs)
     : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)),
       literal (std::move (literal)), locus (locus)
   {}
diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.h b/gcc/rust/typecheck/rust-hir-type-check-enumitem.h
index 9701a20b1e2..d6baff11056 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.h
@@ -57,7 +57,7 @@ public:
       = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
 			      HIR::Literal::LitType::INT,
 			      PrimitiveCoreType::CORETYPE_I64,
-			      item.get_locus ());
+			      item.get_locus (), {});
 
     TyTy::BaseType *isize = nullptr;
     bool ok = context->lookup_builtin ("isize", &isize);
@@ -135,7 +135,7 @@ public:
       = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
 			      HIR::Literal::LitType::INT,
 			      PrimitiveCoreType::CORETYPE_I64,
-			      item.get_locus ());
+			      item.get_locus (), {});
 
     TyTy::BaseType *isize = nullptr;
     bool ok = context->lookup_builtin ("isize", &isize);
@@ -182,7 +182,7 @@ public:
       = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
 			      HIR::Literal::LitType::INT,
 			      PrimitiveCoreType::CORETYPE_I64,
-			      item.get_locus ());
+			      item.get_locus (), {});
 
     TyTy::BaseType *isize = nullptr;
     bool ok = context->lookup_builtin ("isize", &isize);
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index 560581d588b..b24ad8b5e99 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -670,7 +670,7 @@ public:
 	    = new HIR::LiteralExpr (capacity_mapping, capacity_str,
 				    HIR::Literal::LitType::INT,
 				    PrimitiveCoreType::CORETYPE_USIZE,
-				    expr.get_locus ());
+				    expr.get_locus (), expr.get_outer_attrs ());
 
 	  // mark the type for this implicit node
 	  TyTy::BaseType *expected_ty = nullptr;
@@ -929,7 +929,7 @@ public:
 	    = new HIR::LiteralExpr (mapping, capacity_str,
 				    HIR::Literal::LitType::INT,
 				    PrimitiveCoreType::CORETYPE_USIZE,
-				    Location ());
+				    Location (), {});
 
 	  // mark the type for this implicit node
 	  TyTy::BaseType *expected_ty = nullptr;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:33 [gcc/devel/rust/master] Add HIR Lowering for TuplePattern Thomas Schwinge

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).