]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
typecheck: Refactor cast_site
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 23 Feb 2023 18:00:07 +0000 (19:00 +0100)
committerPhilip Herron <philip.herron@embecosm.com>
Fri, 24 Feb 2023 14:59:12 +0000 (14:59 +0000)
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>
gcc/rust/typecheck/rust-hir-type-check-base.cc
gcc/rust/typecheck/rust-hir-type-check-base.h
gcc/rust/typecheck/rust-type-util.cc
gcc/rust/typecheck/rust-type-util.h

index 0c54a05ced115f49712008693f494ac443fab837..b87ed01758357a9620aa38608875b996fe2cfa35 100644 (file)
@@ -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,
index e6f0e3c4305730718e85eabf43372a68ebcc4232..fe25a4285a0c70de66f6c52c9964137363c15fbe 100644 (file)
@@ -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 ();
 
index 16b0a240589366019a1ca134930c61876b6a325b..dbd10366fd7257f973d5733a7cdf57d8fea24824 100644 (file)
@@ -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
index 764ec4d7b811d6c94b365b4f20e8f902afce96a4..01333d8b984cadfe83cb3fdd327c51e0c50a781f 100644 (file)
@@ -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