aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-02-24 16:59:05 +0000
committerPhilip Herron <philip.herron@embecosm.com>2022-02-24 16:59:56 +0000
commit22c6bca60a9bc80c043e4da9a94cb80023dde04c (patch)
tree188edbabfaa677bf7971b44a2450f83f4ba0cfba /gcc/rust/backend
parentd8351d9168f92c997858fdb25942c05dc832f330 (diff)
downloadgcc-22c6bca60a9bc80c043e4da9a94cb80023dde04c.zip
gcc-22c6bca60a9bc80c043e4da9a94cb80023dde04c.tar.gz
gcc-22c6bca60a9bc80c043e4da9a94cb80023dde04c.tar.bz2
Add support for index lang item overloads
This reuses our code to resolve operator overloads to call into the index lang item for the array-index-expression this serves as a basis for supporting slices. Fixes #975
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc29
1 files changed, 29 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index dfe5231..bf47661 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -1490,6 +1490,35 @@ CompileExpr::visit (HIR::ArrayIndexExpr &expr)
tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);
+ // this might be an core::ops::index lang item situation
+ TyTy::FnType *fntype;
+ bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
+ expr.get_mappings ().get_hirid (), &fntype);
+ if (is_op_overload)
+ {
+ auto lang_item_type = Analysis::RustLangItem::ItemType::INDEX;
+ tree operator_overload_call
+ = resolve_operator_overload (lang_item_type, expr, array_reference,
+ index, expr.get_array_expr (),
+ expr.get_index_expr ());
+
+ // lookup the expected type for this expression
+ TyTy::BaseType *tyty = nullptr;
+ bool ok
+ = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
+ &tyty);
+ rust_assert (ok);
+ tree expected_type = TyTyResolveCompile::compile (ctx, tyty);
+
+ // rust deref always returns a reference from this overload then we can
+ // actually do the indirection
+ translated
+ = ctx->get_backend ()->indirect_expression (expected_type,
+ operator_overload_call,
+ true, expr.get_locus ());
+ return;
+ }
+
// lets check if the array is a reference type then we can add an
// indirection if required
TyTy::BaseType *array_expr_ty = nullptr;