diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-08-22 15:54:14 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-08-23 14:06:40 +0100 |
commit | 36a9255b1b6683bb060021c637997de396445049 (patch) | |
tree | 64543a9781cb47d426120f92ea05720256089395 /gcc/rust/backend/rust-compile-expr.cc | |
parent | 0f4ec11e8c2399ca20f80b4006e294794f9b2e0f (diff) | |
download | gcc-36a9255b1b6683bb060021c637997de396445049.zip gcc-36a9255b1b6683bb060021c637997de396445049.tar.gz gcc-36a9255b1b6683bb060021c637997de396445049.tar.bz2 |
Refactor our casts to follow the Rustc implemention
This gets rid of our old visitor system for cast type checking. Casts
depend on type coercions as they are meant to attempt a type coercion
before trying a simple cast. This explicitly defines the rules which should
be allowed for simple casts. In rustc they use match expressions to write
a list of casts which should not be allowed. We have likely missed some
rules of what should be allowed but this is at least the start of how
to implement this.
Fixes #1496
Diffstat (limited to 'gcc/rust/backend/rust-compile-expr.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 412ca09..865ad25 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -237,16 +237,34 @@ CompileExpr::visit (HIR::LazyBooleanExpr &expr) void CompileExpr::visit (HIR::TypeCastExpr &expr) { - TyTy::BaseType *tyty = nullptr; + TyTy::BaseType *type_to_cast_to_ty = nullptr; if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), - &tyty)) + &type_to_cast_to_ty)) { translated = error_mark_node; return; } - auto type_to_cast_to = TyTyResolveCompile::compile (ctx, tyty); + TyTy::BaseType *casted_tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + expr.get_casted_expr ()->get_mappings ().get_hirid (), &casted_tyty)) + { + translated = error_mark_node; + return; + } + + auto type_to_cast_to = TyTyResolveCompile::compile (ctx, type_to_cast_to_ty); auto casted_expr = CompileExpr::Compile (expr.get_casted_expr ().get (), ctx); + + std::vector<Resolver::Adjustment> *adjustments = nullptr; + bool ok = ctx->get_tyctx ()->lookup_cast_autoderef_mappings ( + expr.get_mappings ().get_hirid (), &adjustments); + if (ok) + { + casted_expr + = resolve_adjustements (*adjustments, casted_expr, expr.get_locus ()); + } + translated = type_cast_expression (type_to_cast_to, casted_expr, expr.get_locus ()); } |