public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] typecheck: Refactor cast_site
@ 2023-02-28 22:36 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-02-28 22:36 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0d4a4475e9fbd972bdbcf8e0bcf6f3be3db2f00f

commit 0d4a4475e9fbd972bdbcf8e0bcf6f3be3db2f00f
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Thu Feb 23 19:00:07 2023 +0100

    typecheck: Refactor cast_site
    
    Refactor cast_site to be a simple function in rust-type-util.h.
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-base.cc (TypeCheckBase::cast_site):
            Remove cast_site.
            * typecheck/rust-hir-type-check-base.h: Remove cast_site
            prototype.
            * typecheck/rust-type-util.cc (cast_site): Add cast_site.
            * typecheck/rust-type-util.h (cast_site): Add cast_site
            prototype.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-base.cc | 35 --------------------------
 gcc/rust/typecheck/rust-hir-type-check-base.h  |  4 ---
 gcc/rust/typecheck/rust-type-util.cc           | 35 ++++++++++++++++++++++++++
 gcc/rust/typecheck/rust-type-util.h            |  4 +++
 4 files changed, 39 insertions(+), 39 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 0c54a05ced1..b87ed017583 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -17,7 +17,6 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-hir-type-check-base.h"
-#include "rust-casts.h"
 #include "rust-hir-type-check-expr.h"
 #include "rust-hir-type-check-implitem.h"
 #include "rust-hir-type-check-item.h"
@@ -348,40 +347,6 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, Location locus)
   return repr;
 }
 
-TyTy::BaseType *
-TypeCheckBase::cast_site (HirId id, TyTy::TyWithLocation from,
-			  TyTy::TyWithLocation to, Location cast_locus)
-{
-  rust_debug ("cast_site id={%u} from={%s} to={%s}", id,
-	      from.get_ty ()->debug_str ().c_str (),
-	      to.get_ty ()->debug_str ().c_str ());
-
-  auto context = TypeCheckContext::get ();
-  if (from.get_ty ()->get_kind () == TyTy::TypeKind::ERROR
-      || to.get_ty ()->get_kind () == TyTy::TypeKind::ERROR)
-    return to.get_ty ();
-
-  // do the cast
-  auto result = TypeCastRules::resolve (cast_locus, from, to);
-
-  // we assume error has already been emitted
-  if (result.is_error ())
-    return to.get_ty ();
-
-  // the result needs to be unified
-  TyTy::BaseType *casted_result = result.tyty;
-  rust_debug ("cast_default_unify(a={%s}, b={%s})",
-	      casted_result->debug_str ().c_str (),
-	      to.get_ty ()->debug_str ().c_str ());
-
-  TyTy::BaseType *casted
-    = unify_site (id, to,
-		  TyTy::TyWithLocation (casted_result, from.get_locus ()),
-		  cast_locus);
-  context->insert_cast_autoderef_mappings (id, std::move (result.adjustments));
-  return casted;
-}
-
 void
 TypeCheckBase::resolve_generic_params (
   const std::vector<std::unique_ptr<HIR::GenericParam> > &generic_params,
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.h b/gcc/rust/typecheck/rust-hir-type-check-base.h
index e6f0e3c4305..fe25a4285a0 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.h
@@ -34,10 +34,6 @@ class TypeCheckBase
 public:
   virtual ~TypeCheckBase () {}
 
-  static TyTy::BaseType *cast_site (HirId id, TyTy::TyWithLocation from,
-				    TyTy::TyWithLocation to,
-				    Location cast_locus);
-
 protected:
   TypeCheckBase ();
 
diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc
index 16b0a240589..dbd10366fd7 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -24,6 +24,7 @@
 #include "rust-hir-type-check.h"
 #include "rust-hir-visitor.h"
 #include "rust-name-resolver.h"
+#include "rust-casts.h"
 #include "rust-unify.h"
 #include "rust-coercion.h"
 
@@ -154,5 +155,39 @@ coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
   return coerced;
 }
 
+TyTy::BaseType *
+cast_site (HirId id, TyTy::TyWithLocation from, TyTy::TyWithLocation to,
+	   Location cast_locus)
+{
+  rust_debug ("cast_site id={%u} from={%s} to={%s}", id,
+	      from.get_ty ()->debug_str ().c_str (),
+	      to.get_ty ()->debug_str ().c_str ());
+
+  auto context = TypeCheckContext::get ();
+  if (from.get_ty ()->get_kind () == TyTy::TypeKind::ERROR
+      || to.get_ty ()->get_kind () == TyTy::TypeKind::ERROR)
+    return to.get_ty ();
+
+  // do the cast
+  auto result = TypeCastRules::resolve (cast_locus, from, to);
+
+  // we assume error has already been emitted
+  if (result.is_error ())
+    return to.get_ty ();
+
+  // the result needs to be unified
+  TyTy::BaseType *casted_result = result.tyty;
+  rust_debug ("cast_default_unify(a={%s}, b={%s})",
+	      casted_result->debug_str ().c_str (),
+	      to.get_ty ()->debug_str ().c_str ());
+
+  TyTy::BaseType *casted
+    = unify_site (id, to,
+		  TyTy::TyWithLocation (casted_result, from.get_locus ()),
+		  cast_locus);
+  context->insert_cast_autoderef_mappings (id, std::move (result.adjustments));
+  return casted;
+}
+
 } // namespace Resolver
 } // namespace Rust
diff --git a/gcc/rust/typecheck/rust-type-util.h b/gcc/rust/typecheck/rust-type-util.h
index 764ec4d7b81..01333d8b984 100644
--- a/gcc/rust/typecheck/rust-type-util.h
+++ b/gcc/rust/typecheck/rust-type-util.h
@@ -41,6 +41,10 @@ TyTy::BaseType *
 coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
 	       Location coercion_locus);
 
+TyTy::BaseType *
+cast_site (HirId id, TyTy::TyWithLocation from, TyTy::TyWithLocation to,
+	   Location cast_locus);
+
 } // namespace Resolver
 } // namespace Rust

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

only message in thread, other threads:[~2023-02-28 22:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 22:36 [gcc/devel/rust/master] typecheck: Refactor cast_site 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).