public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] hir: add a helper function for visit
@ 2023-03-14 11:44 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-03-14 11:44 UTC (permalink / raw)
  To: gcc-cvs

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,

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

only message in thread, other threads:[~2023-03-14 11:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 11:44 [gcc/devel/rust/master] hir: add a helper function for visit 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).