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] hir: add a helper function for visit
Date: Tue, 14 Mar 2023 11:44:09 +0000 (GMT)	[thread overview]
Message-ID: <20230314114409.CFA5B385841C@sourceware.org> (raw)

https://gcc.gnu.org/g:6f9938c712d6d8645714ee21d5850342281022e1

commit 6f9938c712d6d8645714ee21d5850342281022e1
Author: vincent <jfan30@u.rochester.edu>
Date:   Tue Mar 7 20:32:04 2023 +0000

    hir: add a helper function for visit
    
    This commit adds a helper function for TypeCheckPattern::visit
    (HIR::RangePattern &pattern) to remove redundancy
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-pattern.cc
            (TypeCheckPattern::visit): rewrite part code to helper function
            (TypeCheckPattern::typecheck_range_pattern_bound): helper function
            * typecheck/rust-hir-type-check-pattern.h
            (TypeCheckPattern::typecheck_range_pattern_bound):
            change the parameter of the function
    
    Signed-off-by: Jiakun Fan <jfan30@u.rochester.edu>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 110 +++++++++-------------
 gcc/rust/typecheck/rust-hir-type-check-pattern.h  |   7 +-
 2 files changed, 47 insertions(+), 70 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index e7ed54250a0..cd0dbef40fe 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
@@ -321,73 +321,13 @@ TypeCheckPattern::visit (HIR::RangePattern &pattern)
   // Resolve the upper and lower bounds, and ensure they are compatible types
   TyTy::BaseType *upper = nullptr, *lower = nullptr;
 
-  // TODO: It would be nice to factor this out into a helper since the logic for
-  // both bounds is exactly the same...
-  switch (pattern.get_upper_bound ()->get_bound_type ())
-    {
-      case HIR::RangePatternBound::RangePatternBoundType::LITERAL: {
-	HIR::RangePatternBoundLiteral &ref
-	  = *static_cast<HIR::RangePatternBoundLiteral *> (
-	    pattern.get_upper_bound ().get ());
-
-	HIR::Literal lit = ref.get_literal ();
-
-	upper = resolve_literal (pattern.get_pattern_mappings (), lit,
-				 pattern.get_locus ());
-      }
-      break;
-
-      case HIR::RangePatternBound::RangePatternBoundType::PATH: {
-	HIR::RangePatternBoundPath &ref
-	  = *static_cast<HIR::RangePatternBoundPath *> (
-	    pattern.get_upper_bound ().get ());
-
-	upper = TypeCheckExpr::Resolve (&ref.get_path ());
-      }
-      break;
-
-      case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: {
-	HIR::RangePatternBoundQualPath &ref
-	  = *static_cast<HIR::RangePatternBoundQualPath *> (
-	    pattern.get_upper_bound ().get ());
-
-	upper = TypeCheckExpr::Resolve (&ref.get_qualified_path ());
-      }
-      break;
-    }
-
-  switch (pattern.get_lower_bound ()->get_bound_type ())
-    {
-      case HIR::RangePatternBound::RangePatternBoundType::LITERAL: {
-	HIR::RangePatternBoundLiteral &ref
-	  = *static_cast<HIR::RangePatternBoundLiteral *> (
-	    pattern.get_lower_bound ().get ());
-
-	HIR::Literal lit = ref.get_literal ();
-
-	lower = resolve_literal (pattern.get_pattern_mappings (), lit,
-				 pattern.get_locus ());
-      }
-      break;
-
-      case HIR::RangePatternBound::RangePatternBoundType::PATH: {
-	HIR::RangePatternBoundPath &ref
-	  = *static_cast<HIR::RangePatternBoundPath *> (
-	    pattern.get_lower_bound ().get ());
-
-	lower = TypeCheckExpr::Resolve (&ref.get_path ());
-      }
-      break;
+  upper = typecheck_range_pattern_bound (pattern.get_upper_bound (),
+					 pattern.get_pattern_mappings (),
+					 pattern.get_locus ());
 
-      case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: {
-	HIR::RangePatternBoundQualPath &ref
-	  = *static_cast<HIR::RangePatternBoundQualPath *> (
-	    pattern.get_lower_bound ().get ());
-
-	lower = TypeCheckExpr::Resolve (&ref.get_qualified_path ());
-      }
-      break;
-    }
+  lower = typecheck_range_pattern_bound (pattern.get_lower_bound (),
+					 pattern.get_pattern_mappings (),
+					 pattern.get_locus ());
 
   infered = unify_site (pattern.get_pattern_mappings ().get_hirid (),
 			TyTy::TyWithLocation (upper),
@@ -442,6 +382,44 @@ TypeCheckPattern::emit_pattern_size_error (const HIR::Pattern &pattern,
 		 got_field_count == 1 ? "element" : "elements");
 }
 
+TyTy::BaseType *
+TypeCheckPattern::typecheck_range_pattern_bound (
+  std::unique_ptr<Rust::HIR::RangePatternBound> &bound,
+  Analysis::NodeMapping mappings, Location locus)
+{
+  TyTy::BaseType *resolved_bound = nullptr;
+  switch (bound->get_bound_type ())
+    {
+      case HIR::RangePatternBound::RangePatternBoundType::LITERAL: {
+	HIR::RangePatternBoundLiteral &ref
+	  = *static_cast<HIR::RangePatternBoundLiteral *> (bound.get ());
+
+	HIR::Literal lit = ref.get_literal ();
+
+	resolved_bound = resolve_literal (mappings, lit, locus);
+      }
+      break;
+
+      case HIR::RangePatternBound::RangePatternBoundType::PATH: {
+	HIR::RangePatternBoundPath &ref
+	  = *static_cast<HIR::RangePatternBoundPath *> (bound.get ());
+
+	resolved_bound = TypeCheckExpr::Resolve (&ref.get_path ());
+      }
+      break;
+
+      case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: {
+	HIR::RangePatternBoundQualPath &ref
+	  = *static_cast<HIR::RangePatternBoundQualPath *> (bound.get ());
+
+	resolved_bound = TypeCheckExpr::Resolve (&ref.get_qualified_path ());
+      }
+      break;
+    }
+
+  return resolved_bound;
+}
+
 void
 TypeCheckPattern::visit (HIR::AltPattern &pattern)
 {
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.h b/gcc/rust/typecheck/rust-hir-type-check-pattern.h
index 3b392b552d5..d5d0fd00de5 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.h
@@ -47,10 +47,9 @@ public:
 private:
   TypeCheckPattern (TyTy::BaseType *parent);
 
-  static TyTy::BaseType *
-  typecheck_range_pattern_bound (HIR::RangePatternBound *bound,
-				 Analysis::NodeMapping mappings,
-				 Location locus);
+  TyTy::BaseType *typecheck_range_pattern_bound (
+    std::unique_ptr<Rust::HIR::RangePatternBound> &bound,
+    Analysis::NodeMapping mappings, Location locus);
 
   void emit_pattern_size_error (const HIR::Pattern &pattern,
 				size_t expected_field_count,

                 reply	other threads:[~2023-03-14 11:44 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=20230314114409.CFA5B385841C@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).