diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-08-18 16:28:49 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-08-19 21:59:00 +0100 |
commit | fdbf789c2bb93baebd449744309d7c42bf2b36b8 (patch) | |
tree | cb4de70f42cb25876ff40cafa1ed7907b866eb25 /gcc/rust/backend | |
parent | 4f039ff9f6f18d15e32ddb54e3a6124802c45b7f (diff) | |
download | gcc-fdbf789c2bb93baebd449744309d7c42bf2b36b8.zip gcc-fdbf789c2bb93baebd449744309d7c42bf2b36b8.tar.gz gcc-fdbf789c2bb93baebd449744309d7c42bf2b36b8.tar.bz2 |
Redo coercion site code
This gets rid of the old visitor method and brings us much closer to the
Rustc rules which from the algo mentioned in the comment's do the checks
in a very specific order which we need to match.
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.h | 7 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 40 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 10 |
3 files changed, 47 insertions, 10 deletions
diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index 5a0ac8f..4c20933 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -48,7 +48,6 @@ protected: Location rvalue_locus); tree coerce_to_dyn_object (tree compiled_ref, const TyTy::BaseType *actual, - const TyTy::BaseType *expected, const TyTy::DynamicObjectType *ty, Location locus); tree compute_address_for_trait_item ( @@ -78,6 +77,12 @@ protected: tree resolve_unsized_adjustment (Resolver::Adjustment &adjustment, tree expression, Location locus); + tree resolve_unsized_slice_adjustment (Resolver::Adjustment &adjustment, + tree expression, Location locus); + + tree resolve_unsized_dyn_adjustment (Resolver::Adjustment &adjustment, + tree expression, Location locus); + static void setup_fndecl (tree fndecl, bool is_main_entry_point, bool is_generic_fn, HIR::Visibility &visibility, const HIR::FunctionQualifiers &qualifiers, diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index bfaa7fc..cf82cf4 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1774,6 +1774,27 @@ tree HIRCompileBase::resolve_unsized_adjustment (Resolver::Adjustment &adjustment, tree expression, Location locus) { + bool expect_slice + = adjustment.get_expected ()->get_kind () == TyTy::TypeKind::SLICE; + bool expect_dyn + = adjustment.get_expected ()->get_kind () == TyTy::TypeKind::DYNAMIC; + + // assumes this is an array + tree expr_type = TREE_TYPE (expression); + if (expect_slice) + { + rust_assert (TREE_CODE (expr_type) == ARRAY_TYPE); + return resolve_unsized_slice_adjustment (adjustment, expression, locus); + } + + rust_assert (expect_dyn); + return resolve_unsized_dyn_adjustment (adjustment, expression, locus); +} + +tree +HIRCompileBase::resolve_unsized_slice_adjustment ( + Resolver::Adjustment &adjustment, tree expression, Location locus) +{ // assumes this is an array tree expr_type = TREE_TYPE (expression); rust_assert (TREE_CODE (expr_type) == ARRAY_TYPE); @@ -1802,6 +1823,25 @@ HIRCompileBase::resolve_unsized_adjustment (Resolver::Adjustment &adjustment, {data, size}, -1, locus); } +tree +HIRCompileBase::resolve_unsized_dyn_adjustment ( + Resolver::Adjustment &adjustment, tree expression, Location locus) +{ + tree rvalue = expression; + Location rvalue_locus = locus; + + const TyTy::BaseType *actual = adjustment.get_actual (); + const TyTy::BaseType *expected = adjustment.get_expected (); + + const TyTy::DynamicObjectType *dyn + = static_cast<const TyTy::DynamicObjectType *> (expected); + + rust_debug ("resolve_unsized_dyn_adjustment actual={%s} dyn={%s}", + actual->debug_str ().c_str (), dyn->debug_str ().c_str ()); + + return coerce_to_dyn_object (rvalue, actual, dyn, rvalue_locus); +} + void CompileExpr::visit (HIR::RangeFromToExpr &expr) { diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index c4100c4..bf34bb6 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -312,13 +312,6 @@ HIRCompileBase::coercion_site1 (tree rvalue, const TyTy::BaseType *rval, lvalue_locus, rvalue_locus)) return error_mark_node; } - else if (expected->get_kind () == TyTy::TypeKind::DYNAMIC - && actual->get_kind () != TyTy::TypeKind::DYNAMIC) - { - const TyTy::DynamicObjectType *dyn - = static_cast<const TyTy::DynamicObjectType *> (expected); - return coerce_to_dyn_object (rvalue, actual, expected, dyn, rvalue_locus); - } else if (expected->get_kind () == TyTy::TypeKind::SLICE) { // bad coercion @@ -333,7 +326,7 @@ HIRCompileBase::coercion_site1 (tree rvalue, const TyTy::BaseType *rval, // return an unsized coercion Resolver::Adjustment unsize_adj ( - Resolver::Adjustment::AdjustmentType::UNSIZE, expected); + Resolver::Adjustment::AdjustmentType::UNSIZE, actual, expected); return resolve_unsized_adjustment (unsize_adj, rvalue, rvalue_locus); } @@ -343,7 +336,6 @@ HIRCompileBase::coercion_site1 (tree rvalue, const TyTy::BaseType *rval, tree HIRCompileBase::coerce_to_dyn_object (tree compiled_ref, const TyTy::BaseType *actual, - const TyTy::BaseType *expected, const TyTy::DynamicObjectType *ty, Location locus) { |