diff options
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 16 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 18 |
2 files changed, 22 insertions, 12 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 8866575..226cec6 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -27,6 +27,7 @@ #include "rust-compile-tyty.h" #include "rust-ast-full.h" #include "rust-hir-full.h" +#include "rust-hir-const-fold-ctx.h" namespace Rust { namespace Compile { @@ -43,7 +44,8 @@ public: Context (::Backend *backend) : backend (backend), resolver (Resolver::Resolver::get ()), tyctx (Resolver::TypeCheckContext::get ()), - mappings (Analysis::Mappings::get ()) + mappings (Analysis::Mappings::get ()), + const_ctx (ConstFold::Context::get ()) { // insert the builtins auto builtins = resolver->get_builtin_types (); @@ -104,6 +106,7 @@ public: Resolver::Resolver *get_resolver () { return resolver; } Resolver::TypeCheckContext *get_tyctx () { return tyctx; } Analysis::Mappings *get_mappings () { return mappings; } + ConstFold::Context *get_const_ctx () { return const_ctx; } void push_block (Bblock *scope) { @@ -260,6 +263,7 @@ private: Resolver::Resolver *resolver; Resolver::TypeCheckContext *tyctx; Analysis::Mappings *mappings; + ConstFold::Context *const_ctx; // state std::vector<fncontext> fn_stack; @@ -420,16 +424,10 @@ public: void visit (TyTy::ArrayType &type) override { - mpz_t ival; - mpz_init_set_ui (ival, type.get_capacity ()); - - Btype *capacity_type = ctx->get_backend ()->integer_type (true, 32); - Bexpression *length - = ctx->get_backend ()->integer_constant_expression (capacity_type, ival); - Btype *element_type = TyTyResolveCompile::compile (ctx, type.get_element_type ()); - translated = ctx->get_backend ()->array_type (element_type, length); + translated + = ctx->get_backend ()->array_type (element_type, type.get_capacity ()); } void visit (TyTy::BoolType &type) override diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index f98ebc6..59ae815 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -275,7 +275,12 @@ public: return; } - Btype *array_type = TyTyResolveCompile::compile (ctx, tyty); + rust_assert (tyty->get_kind () == TyTy::TypeKind::ARRAY); + TyTy::ArrayType *array_tyty = static_cast<TyTy::ArrayType *> (tyty); + capacity_expr = array_tyty->get_capacity (); + + Btype *array_type = TyTyResolveCompile::compile (ctx, array_tyty); + rust_assert (array_type != nullptr); expr.get_internal_elements ()->accept_vis (*this); std::vector<unsigned long> indexes; @@ -302,7 +307,11 @@ public: Bexpression *translated_expr = CompileExpr::Compile (elems.get_elem_to_copy (), ctx); - for (size_t i = 0; i < elems.get_num_elements (); ++i) + size_t capacity; + bool ok = ctx->get_backend ()->const_size_cast (capacity_expr, &capacity); + rust_assert (ok); + + for (size_t i = 0; i < capacity; ++i) constructor.push_back (translated_expr); } @@ -786,9 +795,12 @@ public: } private: - CompileExpr (Context *ctx) : HIRCompileBase (ctx), translated (nullptr) {} + CompileExpr (Context *ctx) + : HIRCompileBase (ctx), translated (nullptr), capacity_expr (nullptr) + {} Bexpression *translated; + Bexpression *capacity_expr; std::vector<Bexpression *> constructor; }; |