From 4e2b2822f18577edb614bdc3ec6808a0587662e5 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 9 Jun 2019 19:55:15 -0400 Subject: [PATCH] inferred array size of array literals works --- src/codegen.cpp | 27 +-------------------------- src/ir.cpp | 41 +++++++++++++++++------------------------ 2 files changed, 18 insertions(+), 50 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 763e1e1c509617cb25107ce868700ed5ffb8b39e..b28f04139103efbb607225010acff99ab9b157c3 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -5072,30 +5072,6 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I return instruction->tmp_ptr; } -static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *executable, - IrInstructionContainerInitList *instruction) -{ - ZigType *array_type = instruction->base.value.type; - assert(array_type->id == ZigTypeIdArray); - LLVMValueRef tmp_array_ptr = instruction->tmp_ptr; - assert(tmp_array_ptr); - - size_t field_count = instruction->item_count; - - ZigType *child_type = array_type->data.array.child_type; - for (size_t i = 0; i < field_count; i += 1) { - LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]); - LLVMValueRef indices[] = { - LLVMConstNull(g->builtin_types.entry_usize->llvm_type), - LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false), - }; - LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, ""); - gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val); - } - - return tmp_array_ptr; -} - static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) { gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope)); return nullptr; @@ -5586,6 +5562,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, case IrInstructionIdAllocaGen: case IrInstructionIdImplicitCast: case IrInstructionIdResolveResult: + case IrInstructionIdContainerInitList: zig_unreachable(); case IrInstructionIdDeclVarGen: @@ -5700,8 +5677,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_int_to_err(g, executable, (IrInstructionIntToErr *)instruction); case IrInstructionIdErrToInt: return ir_render_err_to_int(g, executable, (IrInstructionErrToInt *)instruction); - case IrInstructionIdContainerInitList: - return ir_render_container_init_list(g, executable, (IrInstructionContainerInitList *)instruction); case IrInstructionIdPanic: return ir_render_panic(g, executable, (IrInstructionPanic *)instruction); case IrInstructionIdTagName: diff --git a/src/ir.cpp b/src/ir.cpp index 84c7dc63420a4abedd19fef93df30ebd7c136301..b29af2a1e2e4d33e330bfc73c0b33c58db30ffb1 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1484,17 +1484,15 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour } static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node, - IrInstruction *container_type, IrInstruction *elem_type, size_t item_count, IrInstruction **items) + IrInstruction *container_type, size_t item_count, IrInstruction **items) { IrInstructionContainerInitList *container_init_list_instruction = ir_build_instruction(irb, scope, source_node); container_init_list_instruction->container_type = container_type; - container_init_list_instruction->elem_type = elem_type; container_init_list_instruction->item_count = item_count; container_init_list_instruction->items = items; - if (container_type != nullptr) ir_ref_instruction(container_type, irb->current_basic_block); - if (elem_type != nullptr) ir_ref_instruction(elem_type, irb->current_basic_block); + ir_ref_instruction(container_type, irb->current_basic_block); for (size_t i = 0; i < item_count; i += 1) { ir_ref_instruction(items[i], irb->current_basic_block); } @@ -5620,11 +5618,17 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A src_assert(result_loc->scope_elide == nullptr, node); result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope); - src_assert(result_loc != nullptr, node); - IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base, - node, result_loc, container_type); - size_t item_count = container_init_expr->entries.length; + + if (container_type == nullptr) { + IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count); + container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type); + } + + src_assert(result_loc != nullptr, node); + IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base, + node, result_loc, container_type); + IrInstruction **values = allocate(item_count); for (size_t i = 0; i < item_count; i += 1) { AstNode *expr_node = container_init_expr->entries.at(i); @@ -5644,7 +5648,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A values[i] = expr_value; } - IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type, elem_type, + IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type, item_count, values); return ir_lval_wrap(irb, scope, init_list, lval, result_loc); } @@ -18538,23 +18542,12 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira, { Error err; + ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child); + if (type_is_invalid(container_type)) + return ira->codegen->invalid_instruction; + size_t elem_count = instruction->item_count; - ZigType *container_type; - if (instruction->container_type != nullptr) { - container_type = ir_resolve_type(ira, instruction->container_type->child); - if (type_is_invalid(container_type)) - return ira->codegen->invalid_instruction; - } else { - ZigType *elem_type = ir_resolve_type(ira, instruction->elem_type->child); - if (type_is_invalid(elem_type)) - return ira->codegen->invalid_instruction; - if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown))) { - return ira->codegen->invalid_instruction; - } - container_type = get_array_type(ira->codegen, elem_type, elem_count); - } - if (is_slice(container_type)) { ir_add_error(ira, &instruction->base, buf_sprintf("expected array type or [_], found slice")); -- 2.54.0