From 921ee82c974541cdb2be72f9ea9eee82bc44ad3b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 25 Mar 2020 15:11:35 +0000 Subject: Fix range statements for enumerated arrays without an element value but with an index --- src/ir.cpp | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index fe5608a4a..8feab6419 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9282,12 +9282,14 @@ void ir_build_range_indexed(irProcedure *proc, irValue *expr, Type *val_type, ir case Type_EnumeratedArray: { if (val_type != nullptr) { val = ir_emit_load(proc, ir_emit_array_ep(proc, expr, idx)); - // NOTE(bill): Override the idx value for the enumeration - Type *index_type =expr_type->EnumeratedArray.index; - if (compare_exact_values(Token_NotEq, expr_type->EnumeratedArray.min_value, exact_value_u64(0))) { - idx = ir_emit_arith(proc, Token_Add, idx, ir_value_constant(index_type, expr_type->EnumeratedArray.min_value), index_type); - } } + // NOTE(bill): Override the idx value for the enumeration + Type *index_type =expr_type->EnumeratedArray.index; + if (compare_exact_values(Token_NotEq, expr_type->EnumeratedArray.min_value, exact_value_u64(0))) { + idx = ir_emit_arith(proc, Token_Add, idx, ir_value_constant(index_type, expr_type->EnumeratedArray.min_value), index_type); + } + + idx = ir_emit_conv(proc, idx, expr_type->EnumeratedArray.index); break; } case Type_Slice: { @@ -9565,6 +9567,12 @@ irAddr ir_store_range_stmt_val(irProcedure *proc, Ast *stmt_val, irValue *value) if (value->kind == irValue_Instr) { if (value->Instr.kind == irInstr_Load) { irValue *ptr = value->Instr.Load.address; + Type *vt = type_deref(ir_type(ptr)); + if (!are_types_identical(vt, e->type)) { + GB_ASSERT(are_types_identical(base_type(vt), base_type(e->type))); + ptr = ir_emit_conv(proc, ptr, alloc_type_pointer(e->type)); + } + ir_module_add_value(proc->module, e, ptr); return ir_addr(ptr); } @@ -9573,7 +9581,19 @@ irAddr ir_store_range_stmt_val(irProcedure *proc, Ast *stmt_val, irValue *value) // by value irAddr addr = ir_addr(ir_add_local(proc, e, nullptr, false)); - GB_ASSERT(are_types_identical(ir_type(value), e->type)); + Type *vt = ir_type(value); + Type *base_et = base_type(e->type); + Type *base_vt = base_type(vt); + if (!are_types_identical(e->type, vt)) { + if (are_types_identical(base_et, base_vt)) { + value = ir_emit_conv(proc, value, e->type); + } else { + // gb_printf_err("%s\n", expr_to_string(stmt_val)); + // gb_printf_err("Entity: %s -> Value: %s\n", type_to_string(e->type), type_to_string(vt)); + // Token tok = ast_token(stmt_val); + // gb_printf_err("%.*s(%td:%td)\n", LIT(tok.pos.file), tok.pos.line, tok.pos.column); + } + } ir_addr_store(proc, addr, value); return addr; } -- cgit v1.2.3