aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-02-24 14:30:15 +0000
committerPhilip Herron <philip.herron@embecosm.com>2022-02-24 14:30:15 +0000
commit0033df1a52615529f567a5a89d40ff97b5650136 (patch)
tree7399f82ab585829e2ab28f0237afc066dfb89663 /gcc
parent833c439a501a02c6c3c5b34f8e4c84706e8b62f2 (diff)
downloadgcc-0033df1a52615529f567a5a89d40ff97b5650136.zip
gcc-0033df1a52615529f567a5a89d40ff97b5650136.tar.gz
gcc-0033df1a52615529f567a5a89d40ff97b5650136.tar.bz2
Refactor ArrayIndexExpr typechecking into cc impl file
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.cc47
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.h47
2 files changed, 48 insertions, 46 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index e99432a..bd05c28 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -242,5 +242,52 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr)
infered = SubstMapperInternal::Resolve (adt, subst);
}
+void
+TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
+{
+ TyTy::BaseType *size_ty;
+ if (!context->lookup_builtin ("usize", &size_ty))
+ {
+ rust_error_at (
+ expr.get_locus (),
+ "Failure looking up size type for index in ArrayIndexExpr");
+ return;
+ }
+
+ auto resolved_index_expr
+ = size_ty->unify (TypeCheckExpr::Resolve (expr.get_index_expr (), false));
+ if (resolved_index_expr->get_kind () != TyTy::TypeKind::ERROR)
+ {
+ // allow the index expr to fail lets just continue on
+ context->insert_type (expr.get_index_expr ()->get_mappings (),
+ resolved_index_expr);
+ }
+
+ auto array_expr_ty
+ = TypeCheckExpr::Resolve (expr.get_array_expr (), inside_loop);
+ if (array_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
+ return;
+ else if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
+ {
+ // lets try and deref it since rust allows this
+ auto ref = static_cast<TyTy::ReferenceType *> (array_expr_ty);
+ auto base = ref->get_base ();
+ if (base->get_kind () == TyTy::TypeKind::ARRAY)
+ array_expr_ty = base;
+ }
+
+ if (array_expr_ty->get_kind () != TyTy::TypeKind::ARRAY)
+ {
+ rust_error_at (expr.get_index_expr ()->get_locus (),
+ "expected an ArrayType got [%s]",
+ infered->as_string ().c_str ());
+ infered = nullptr;
+ return;
+ }
+
+ TyTy::ArrayType *array_type = static_cast<TyTy::ArrayType *> (array_expr_ty);
+ infered = array_type->get_element_type ()->clone ();
+}
+
} // namespace Resolver
} // namespace Rust
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index 0c3d229..5b96854f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -879,52 +879,7 @@ public:
= TypeCheckExpr::Resolve (expr.get_block_expr ().get (), inside_loop);
}
- void visit (HIR::ArrayIndexExpr &expr) override
- {
- TyTy::BaseType *size_ty;
- if (!context->lookup_builtin ("usize", &size_ty))
- {
- rust_error_at (
- expr.get_locus (),
- "Failure looking up size type for index in ArrayIndexExpr");
- return;
- }
-
- auto resolved_index_expr
- = size_ty->unify (TypeCheckExpr::Resolve (expr.get_index_expr (), false));
- if (resolved_index_expr->get_kind () != TyTy::TypeKind::ERROR)
- {
- // allow the index expr to fail lets just continue on
- context->insert_type (expr.get_index_expr ()->get_mappings (),
- resolved_index_expr);
- }
-
- auto array_expr_ty
- = TypeCheckExpr::Resolve (expr.get_array_expr (), inside_loop);
- if (array_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
- return;
- else if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
- {
- // lets try and deref it since rust allows this
- auto ref = static_cast<TyTy::ReferenceType *> (array_expr_ty);
- auto base = ref->get_base ();
- if (base->get_kind () == TyTy::TypeKind::ARRAY)
- array_expr_ty = base;
- }
-
- if (array_expr_ty->get_kind () != TyTy::TypeKind::ARRAY)
- {
- rust_error_at (expr.get_index_expr ()->get_locus (),
- "expected an ArrayType got [%s]",
- infered->as_string ().c_str ());
- infered = nullptr;
- return;
- }
-
- TyTy::ArrayType *array_type
- = static_cast<TyTy::ArrayType *> (array_expr_ty);
- infered = array_type->get_element_type ()->clone ();
- }
+ void visit (HIR::ArrayIndexExpr &expr) override;
void visit (HIR::ArrayExpr &expr) override
{