diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-02-22 10:44:16 +0100 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2023-02-23 09:58:25 +0000 |
commit | 918851ad2c965cef63dd0cb0be01f80a80d6c9c7 (patch) | |
tree | f040a3a9e916c5edf7850d58107a3fc784e902fa /gcc | |
parent | 1145917c6b82ed475b84ae0dd6de241bd018721d (diff) | |
download | gcc-918851ad2c965cef63dd0cb0be01f80a80d6c9c7.zip gcc-918851ad2c965cef63dd0cb0be01f80a80d6c9c7.tar.gz gcc-918851ad2c965cef63dd0cb0be01f80a80d6c9c7.tar.bz2 |
typecheck: Refactor coercion_site
Refactor coercion_site to be a simple function in rust-type-util.h
instead of a static function in TypeCheckBase.
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-resolve.cc (TraitItemReference::resolve_item):
Remove TypeCheckBase namespace qualifier.
* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::coercion_site):
Remove coercion_site function.
* typecheck/rust-hir-type-check-base.h: Remove coercion_site
prototype.
* typecheck/rust-type-util.cc (coercion_site): Add coercion_site
function.
* typecheck/rust-type-util.h (coercion_site): Add coercion_site
prototype.
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
Remove TypeCheckBase namespace qualifier.
(TypeCheckMethodCallExpr::check): Remove TypeCheckBase namespace
qualifier.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/typecheck/rust-hir-trait-resolve.cc | 8 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-base.cc | 35 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-base.h | 4 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-type-util.cc | 35 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-type-util.h | 4 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty-call.cc | 21 |
6 files changed, 54 insertions, 53 deletions
diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index cb8d4d6..5a4d966 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -349,11 +349,9 @@ TraitItemReference::resolve_item (HIR::TraitItemFunc &func) ? func.get_decl ().get_return_type ()->get_locus () : func.get_locus (); - TypeCheckBase::coercion_site (func.get_mappings ().get_hirid (), - TyTy::TyWithLocation (expected_ret_tyty, - fn_return_locus), - TyTy::TyWithLocation (block_expr_ty), - func.get_locus ()); + coercion_site (func.get_mappings ().get_hirid (), + TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus), + TyTy::TyWithLocation (block_expr_ty), func.get_locus ()); context->pop_return_type (); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index e401e83..0c54a05 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -18,7 +18,6 @@ #include "rust-hir-type-check-base.h" #include "rust-casts.h" -#include "rust-coercion.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-implitem.h" #include "rust-hir-type-check-item.h" @@ -350,40 +349,6 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, Location locus) } TyTy::BaseType * -TypeCheckBase::coercion_site (HirId id, TyTy::TyWithLocation lhs, - TyTy::TyWithLocation rhs, Location locus) -{ - TyTy::BaseType *expected = lhs.get_ty (); - TyTy::BaseType *expr = rhs.get_ty (); - - rust_debug ("coercion_site id={%u} expected={%s} expr={%s}", id, - expected->debug_str ().c_str (), expr->debug_str ().c_str ()); - - auto context = TypeCheckContext::get (); - if (expected->get_kind () == TyTy::TypeKind::ERROR - || expr->get_kind () == TyTy::TypeKind::ERROR) - return expr; - - // can we autoderef it? - auto result = TypeCoercionRules::Coerce (expr, expected, locus); - - // the result needs to be unified - TyTy::BaseType *receiver = expr; - if (!result.is_error ()) - { - receiver = result.tyty; - } - - rust_debug ("coerce_default_unify(a={%s}, b={%s})", - receiver->debug_str ().c_str (), expected->debug_str ().c_str ()); - TyTy::BaseType *coerced - = unify_site (id, lhs, TyTy::TyWithLocation (receiver, rhs.get_locus ()), - locus); - context->insert_autoderef_mappings (id, std::move (result.adjustments)); - return coerced; -} - -TyTy::BaseType * TypeCheckBase::cast_site (HirId id, TyTy::TyWithLocation from, TyTy::TyWithLocation to, Location cast_locus) { diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.h b/gcc/rust/typecheck/rust-hir-type-check-base.h index 133c91e..e6f0e3c 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 *coercion_site (HirId id, TyTy::TyWithLocation lhs, - TyTy::TyWithLocation rhs, - Location coercion_locus); - static TyTy::BaseType *cast_site (HirId id, TyTy::TyWithLocation from, TyTy::TyWithLocation to, Location cast_locus); diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc index 19935eb..16b0a24 100644 --- a/gcc/rust/typecheck/rust-type-util.cc +++ b/gcc/rust/typecheck/rust-type-util.cc @@ -25,6 +25,7 @@ #include "rust-hir-visitor.h" #include "rust-name-resolver.h" #include "rust-unify.h" +#include "rust-coercion.h" namespace Rust { namespace Resolver { @@ -119,5 +120,39 @@ unify_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, true /*emit_error*/); } +TyTy::BaseType * +coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, + Location locus) +{ + TyTy::BaseType *expected = lhs.get_ty (); + TyTy::BaseType *expr = rhs.get_ty (); + + rust_debug ("coercion_site id={%u} expected={%s} expr={%s}", id, + expected->debug_str ().c_str (), expr->debug_str ().c_str ()); + + auto context = TypeCheckContext::get (); + if (expected->get_kind () == TyTy::TypeKind::ERROR + || expr->get_kind () == TyTy::TypeKind::ERROR) + return expr; + + // can we autoderef it? + auto result = TypeCoercionRules::Coerce (expr, expected, locus); + + // the result needs to be unified + TyTy::BaseType *receiver = expr; + if (!result.is_error ()) + { + receiver = result.tyty; + } + + rust_debug ("coerce_default_unify(a={%s}, b={%s})", + receiver->debug_str ().c_str (), expected->debug_str ().c_str ()); + TyTy::BaseType *coerced + = unify_site (id, lhs, TyTy::TyWithLocation (receiver, rhs.get_locus ()), + locus); + context->insert_autoderef_mappings (id, std::move (result.adjustments)); + return coerced; +} + } // namespace Resolver } // namespace Rust diff --git a/gcc/rust/typecheck/rust-type-util.h b/gcc/rust/typecheck/rust-type-util.h index e504082..764ec4d 100644 --- a/gcc/rust/typecheck/rust-type-util.h +++ b/gcc/rust/typecheck/rust-type-util.h @@ -37,6 +37,10 @@ TyTy::BaseType * unify_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, Location unify_locus); +TyTy::BaseType * +coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, + Location coercion_locus); + } // namespace Resolver } // namespace Rust diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 3d5fc65..b50c809 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -60,9 +60,10 @@ TypeCheckCallExpr::visit (ADTType &type) } HirId coercion_side_id = argument->get_mappings ().get_hirid (); - auto res = Resolver::TypeCheckBase::coercion_site ( - coercion_side_id, TyWithLocation (field_tyty), - TyWithLocation (arg, arg_locus), argument->get_locus ()); + auto res = Resolver::coercion_site (coercion_side_id, + TyWithLocation (field_tyty), + TyWithLocation (arg, arg_locus), + argument->get_locus ()); if (res->get_kind () == TyTy::TypeKind::ERROR) { return; @@ -134,10 +135,12 @@ TypeCheckCallExpr::visit (FnType &type) : fn_param_pattern->get_locus (); HirId coercion_side_id = argument->get_mappings ().get_hirid (); - auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site ( - coercion_side_id, TyWithLocation (param_ty, param_locus), - TyWithLocation (argument_expr_tyty, arg_locus), - argument->get_locus ()); + auto resolved_argument_type + = Resolver::coercion_site (coercion_side_id, + TyWithLocation (param_ty, param_locus), + TyWithLocation (argument_expr_tyty, + arg_locus), + argument->get_locus ()); if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) { return; @@ -240,7 +243,7 @@ TypeCheckCallExpr::visit (FnPtr &type) return; } - auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site ( + auto resolved_argument_type = Resolver::coercion_site ( argument->get_mappings ().get_hirid (), TyWithLocation (fnparam), TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ()); if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) @@ -347,7 +350,7 @@ TypeCheckMethodCallExpr::check (FnType &type) auto argument_expr_tyty = argument.get_argument_type (); HirId coercion_side_id = argument.get_mappings ().get_hirid (); - auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site ( + auto resolved_argument_type = Resolver::coercion_site ( coercion_side_id, TyWithLocation (param_ty, param_locus), TyWithLocation (argument_expr_tyty, arg_locus), arg_locus); if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) |