aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-expr.h
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-11-24 16:31:49 +0000
committerPhilip Herron <philip.herron@embecosm.com>2021-11-24 16:31:49 +0000
commite7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3 (patch)
treef75154244536a76f62a6cc4a89046ffd95bda6c7 /gcc/rust/backend/rust-compile-expr.h
parentf967dd27790a16837b55e6d2c5ee01dcba096f53 (diff)
downloadgcc-e7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3.zip
gcc-e7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3.tar.gz
gcc-e7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3.tar.bz2
Allow references to arrays for ArrayIndexExpr accessor's
When we have an array-index expr rust allows the array reference to be a reference and the compiler is meant to add in the required implicit indirection. This checks for this senario and injects the indirection during code-generation. Fixes #815
Diffstat (limited to 'gcc/rust/backend/rust-compile-expr.h')
-rw-r--r--gcc/rust/backend/rust-compile-expr.h26
1 files changed, 24 insertions, 2 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 2bf969b..46d501a 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -390,10 +390,32 @@ public:
void visit (HIR::ArrayIndexExpr &expr) override
{
- tree array = CompileExpr::Compile (expr.get_array_expr (), ctx);
+ tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);
+
+ // lets check if the array is a reference type then we can add an
+ // indirection if required
+ TyTy::BaseType *array_expr_ty = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (
+ expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty);
+ rust_assert (ok);
+
+ // do we need to add an indirect reference
+ if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
+ {
+ TyTy::ReferenceType *r
+ = static_cast<TyTy::ReferenceType *> (array_expr_ty);
+ TyTy::BaseType *tuple_type = r->get_base ();
+ tree array_tyty = TyTyResolveCompile::compile (ctx, tuple_type);
+
+ array_reference
+ = ctx->get_backend ()->indirect_expression (array_tyty,
+ array_reference, true,
+ expr.get_locus ());
+ }
+
translated
- = ctx->get_backend ()->array_index_expression (array, index,
+ = ctx->get_backend ()->array_index_expression (array_reference, index,
expr.get_locus ());
}