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
signature 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...@@ -5072,30 +5072,6 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
5072 return instruction->tmp_ptr;5072 return instruction->tmp_ptr;
5073}5073}
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
5099static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {5075static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {
5100 gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope));5076 gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope));
5101 return nullptr;5077 return nullptr;
...@@ -5586,6 +5562,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -5586,6 +5562,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
5586 case IrInstructionIdAllocaGen:5562 case IrInstructionIdAllocaGen:
5587 case IrInstructionIdImplicitCast:5563 case IrInstructionIdImplicitCast:
5588 case IrInstructionIdResolveResult:5564 case IrInstructionIdResolveResult:
5565 case IrInstructionIdContainerInitList:
5589 zig_unreachable();5566 zig_unreachable();
55905567
5591 case IrInstructionIdDeclVarGen:5568 case IrInstructionIdDeclVarGen:
...@@ -5700,8 +5677,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -5700,8 +5677,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
5700 return ir_render_int_to_err(g, executable, (IrInstructionIntToErr *)instruction);5677 return ir_render_int_to_err(g, executable, (IrInstructionIntToErr *)instruction);
5701 case IrInstructionIdErrToInt:5678 case IrInstructionIdErrToInt:
5702 return ir_render_err_to_int(g, executable, (IrInstructionErrToInt *)instruction);5679 return ir_render_err_to_int(g, executable, (IrInstructionErrToInt *)instruction);
5703 case IrInstructionIdContainerInitList:
5704 return ir_render_container_init_list(g, executable, (IrInstructionContainerInitList *)instruction);
5705 case IrInstructionIdPanic:5680 case IrInstructionIdPanic:
5706 return ir_render_panic(g, executable, (IrInstructionPanic *)instruction);5681 return ir_render_panic(g, executable, (IrInstructionPanic *)instruction);
5707 case IrInstructionIdTagName:5682 case IrInstructionIdTagName:
src/ir.cpp+14-21
...@@ -1484,17 +1484,15 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour...@@ -1484,17 +1484,15 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
1484}1484}
14851485
1486static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,1486static 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)
1488{1488{
1489 IrInstructionContainerInitList *container_init_list_instruction =1489 IrInstructionContainerInitList *container_init_list_instruction =
1490 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);1490 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);
1491 container_init_list_instruction->container_type = container_type;1491 container_init_list_instruction->container_type = container_type;
1492 container_init_list_instruction->elem_type = elem_type;
1493 container_init_list_instruction->item_count = item_count;1492 container_init_list_instruction->item_count = item_count;
1494 container_init_list_instruction->items = items;1493 container_init_list_instruction->items = items;
14951494
1496 if (container_type != nullptr) ir_ref_instruction(container_type, irb->current_basic_block);1495 ir_ref_instruction(container_type, irb->current_basic_block);
1497 if (elem_type != nullptr) ir_ref_instruction(elem_type, irb->current_basic_block);
1498 for (size_t i = 0; i < item_count; i += 1) {1496 for (size_t i = 0; i < item_count; i += 1) {
1499 ir_ref_instruction(items[i], irb->current_basic_block);1497 ir_ref_instruction(items[i], irb->current_basic_block);
1500 }1498 }
...@@ -5620,11 +5618,17 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -5620,11 +5618,17 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
5620 src_assert(result_loc->scope_elide == nullptr, node);5618 src_assert(result_loc->scope_elide == nullptr, node);
5621 result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope);5619 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
5623 src_assert(result_loc != nullptr, node);5628 src_assert(result_loc != nullptr, node);
5624 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,5629 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,
5625 node, result_loc, container_type);5630 node, result_loc, container_type);
56265631
5627 size_t item_count = container_init_expr->entries.length;
5628 IrInstruction **values = allocate<IrInstruction *>(item_count);5632 IrInstruction **values = allocate<IrInstruction *>(item_count);
5629 for (size_t i = 0; i < item_count; i += 1) {5633 for (size_t i = 0; i < item_count; i += 1) {
5630 AstNode *expr_node = container_init_expr->entries.at(i);5634 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...@@ -5644,7 +5648,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
56445648
5645 values[i] = expr_value;5649 values[i] = expr_value;
5646 }5650 }
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,
5648 item_count, values);5652 item_count, values);
5649 return ir_lval_wrap(irb, scope, init_list, lval, result_loc);5653 return ir_lval_wrap(irb, scope, init_list, lval, result_loc);
5650 }5654 }
...@@ -18538,22 +18542,11 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -18538,22 +18542,11 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
18538{18542{
18539 Error err;18543 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;18549 size_t elem_count = instruction->item_count;
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 }
1855718550
18558 if (is_slice(container_type)) {18551 if (is_slice(container_type)) {
18559 ir_add_error(ira, &instruction->base,18552 ir_add_error(ira, &instruction->base,