authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-09 19:55:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-09 19:55:15-04:00
log4e2b2822f18577edb614bdc3ec6808a0587662e5
treeb7895f69e45851462acb9e3390605214cdebd8d5
parent3a4b749c8a1124734c3202ec0d6f4f6566e7dc7e
signaturelock-open Commit is signed but in an unrecognized format.

inferred array size of array literals works


2 files changed, 15 insertions(+), 47 deletions(-)

src/codegen.cpp+1-26
......@@ -5072,30 +5072,6 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
50725072 return instruction->tmp_ptr;
50735073}
50745074
5075static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *executable,
5076 IrInstructionContainerInitList *instruction)
5077{
5078 ZigType *array_type = instruction->base.value.type;
5079 assert(array_type->id == ZigTypeIdArray);
5080 LLVMValueRef tmp_array_ptr = instruction->tmp_ptr;
5081 assert(tmp_array_ptr);
5082
5083 size_t field_count = instruction->item_count;
5084
5085 ZigType *child_type = array_type->data.array.child_type;
5086 for (size_t i = 0; i < field_count; i += 1) {
5087 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
5088 LLVMValueRef indices[] = {
5089 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5090 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false),
5091 };
5092 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
5093 gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val);
5094 }
5095
5096 return tmp_array_ptr;
5097}
5098
50995075static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {
51005076 gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope));
51015077 return nullptr;
......@@ -5586,6 +5562,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
55865562 case IrInstructionIdAllocaGen:
55875563 case IrInstructionIdImplicitCast:
55885564 case IrInstructionIdResolveResult:
5565 case IrInstructionIdContainerInitList:
55895566 zig_unreachable();
55905567
55915568 case IrInstructionIdDeclVarGen:
......@@ -5700,8 +5677,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
57005677 return ir_render_int_to_err(g, executable, (IrInstructionIntToErr *)instruction);
57015678 case IrInstructionIdErrToInt:
57025679 return ir_render_err_to_int(g, executable, (IrInstructionErrToInt *)instruction);
5703 case IrInstructionIdContainerInitList:
5704 return ir_render_container_init_list(g, executable, (IrInstructionContainerInitList *)instruction);
57055680 case IrInstructionIdPanic:
57065681 return ir_render_panic(g, executable, (IrInstructionPanic *)instruction);
57075682 case IrInstructionIdTagName:
src/ir.cpp+14-21
......@@ -1484,17 +1484,15 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
14841484}
14851485
14861486static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,
1487 IrInstruction *container_type, IrInstruction *elem_type, size_t item_count, IrInstruction **items)
1487 IrInstruction *container_type, size_t item_count, IrInstruction **items)
14881488{
14891489 IrInstructionContainerInitList *container_init_list_instruction =
14901490 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);
14911491 container_init_list_instruction->container_type = container_type;
1492 container_init_list_instruction->elem_type = elem_type;
14931492 container_init_list_instruction->item_count = item_count;
14941493 container_init_list_instruction->items = items;
14951494
1496 if (container_type != nullptr) ir_ref_instruction(container_type, irb->current_basic_block);
1497 if (elem_type != nullptr) ir_ref_instruction(elem_type, irb->current_basic_block);
1495 ir_ref_instruction(container_type, irb->current_basic_block);
14981496 for (size_t i = 0; i < item_count; i += 1) {
14991497 ir_ref_instruction(items[i], irb->current_basic_block);
15001498 }
......@@ -5620,11 +5618,17 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
56205618 src_assert(result_loc->scope_elide == nullptr, node);
56215619 result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope);
56225620
5621 size_t item_count = container_init_expr->entries.length;
5622
5623 if (container_type == nullptr) {
5624 IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count);
5625 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type);
5626 }
5627
56235628 src_assert(result_loc != nullptr, node);
56245629 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,
56255630 node, result_loc, container_type);
56265631
5627 size_t item_count = container_init_expr->entries.length;
56285632 IrInstruction **values = allocate<IrInstruction *>(item_count);
56295633 for (size_t i = 0; i < item_count; i += 1) {
56305634 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
56445648
56455649 values[i] = expr_value;
56465650 }
5647 IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type, elem_type,
5651 IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type,
56485652 item_count, values);
56495653 return ir_lval_wrap(irb, scope, init_list, lval, result_loc);
56505654 }
......@@ -18538,22 +18542,11 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1853818542{
1853918543 Error err;
1854018544
18541 size_t elem_count = instruction->item_count;
18545 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);
18546 if (type_is_invalid(container_type))
18547 return ira->codegen->invalid_instruction;
1854218548
18543 ZigType *container_type;
18544 if (instruction->container_type != nullptr) {
18545 container_type = ir_resolve_type(ira, instruction->container_type->child);
18546 if (type_is_invalid(container_type))
18547 return ira->codegen->invalid_instruction;
18548 } else {
18549 ZigType *elem_type = ir_resolve_type(ira, instruction->elem_type->child);
18550 if (type_is_invalid(elem_type))
18551 return ira->codegen->invalid_instruction;
18552 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown))) {
18553 return ira->codegen->invalid_instruction;
18554 }
18555 container_type = get_array_type(ira->codegen, elem_type, elem_count);
18556 }
18549 size_t elem_count = instruction->item_count;
1855718550
1855818551 if (is_slice(container_type)) {
1855918552 ir_add_error(ira, &instruction->base,