| author | |
| committer | |
| log | 523e3b86af44b97bcf68e3eb0956ef297421ee10 |
| tree | 61b3743aced3715ebc8f811cc3477fca45d0ad20 |
| parent | 21fc5a6402c527675900397ea60f107730985f1c |
4 files changed, 38 insertions(+), 16 deletions(-)
src/analyze.cpp+16-4| ... | ... | @@ -34,8 +34,6 @@ static AstNode *first_executing_node(AstNode *node) { |
| 34 | 34 | return first_executing_node(node->data.field_access_expr.struct_expr); |
| 35 | 35 | case NodeTypeSwitchRange: |
| 36 | 36 | return first_executing_node(node->data.switch_range.start); |
| 37 | case NodeTypeContainerInitExpr: | |
| 38 | return first_executing_node(node->data.container_init_expr.type); | |
| 39 | 37 | case NodeTypeRoot: |
| 40 | 38 | case NodeTypeRootExportDecl: |
| 41 | 39 | case NodeTypeFnProto: |
| ... | ... | @@ -72,6 +70,7 @@ static AstNode *first_executing_node(AstNode *node) { |
| 72 | 70 | case NodeTypeSwitchExpr: |
| 73 | 71 | case NodeTypeSwitchProng: |
| 74 | 72 | case NodeTypeArrayType: |
| 73 | case NodeTypeContainerInitExpr: | |
| 75 | 74 | return node; |
| 76 | 75 | } |
| 77 | 76 | zig_unreachable(); |
| ... | ... | @@ -1586,9 +1585,22 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry |
| 1586 | 1585 | assert(pointer_type->id == TypeTableEntryIdPointer); |
| 1587 | 1586 | TypeTableEntry *child_type = pointer_type->data.pointer.child_type; |
| 1588 | 1587 | |
| 1588 | ConstExprValue *const_val = &get_resolved_expr(node)->const_val; | |
| 1589 | const_val->ok = true; | |
| 1590 | const_val->data.x_array.fields = allocate<ConstExprValue*>(elem_count); | |
| 1591 | ||
| 1589 | 1592 | for (int i = 0; i < elem_count; i += 1) { |
| 1590 | AstNode *elem_node = container_init_expr->entries.at(i); | |
| 1591 | analyze_expression(g, import, context, child_type, elem_node); | |
| 1593 | AstNode **elem_node = &container_init_expr->entries.at(i); | |
| 1594 | analyze_expression(g, import, context, child_type, *elem_node); | |
| 1595 | ||
| 1596 | if (const_val->ok) { | |
| 1597 | ConstExprValue *elem_const_val = &get_resolved_expr(*elem_node)->const_val; | |
| 1598 | if (elem_const_val->ok) { | |
| 1599 | const_val->data.x_array.fields[i] = elem_const_val; | |
| 1600 | } else { | |
| 1601 | const_val->ok = false; | |
| 1602 | } | |
| 1603 | } | |
| 1592 | 1604 | } |
| 1593 | 1605 | |
| 1594 | 1606 | TypeTableEntry *fixed_size_array_type = get_array_type(g, child_type, elem_count); |
src/codegen.cpp+2-8| ... | ... | @@ -2105,13 +2105,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE |
| 2105 | 2105 | return tag_value; |
| 2106 | 2106 | } else { |
| 2107 | 2107 | zig_panic("TODO"); |
| 2108 | /* | |
| 2109 | LLVMValueRef fields[] = { | |
| 2110 | tag_value, | |
| 2111 | union_value, | |
| 2112 | }; | |
| 2113 | return LLVMConstStruct(fields, 2, false); | |
| 2114 | */ | |
| 2115 | 2108 | } |
| 2116 | 2109 | } else if (type_entry->id == TypeTableEntryIdFn) { |
| 2117 | 2110 | return const_val->data.x_fn->fn_value; |
| ... | ... | @@ -2197,10 +2190,11 @@ static void do_code_gen(CodeGen *g) { |
| 2197 | 2190 | } else { |
| 2198 | 2191 | init_val = LLVMConstNull(var->type->type_ref); |
| 2199 | 2192 | } |
| 2200 | LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_val), ""); | |
| 2193 | LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_val), buf_ptr(&var->name)); | |
| 2201 | 2194 | LLVMSetInitializer(global_value, init_val); |
| 2202 | 2195 | LLVMSetGlobalConstant(global_value, var->is_const); |
| 2203 | 2196 | LLVMSetUnnamedAddr(global_value, true); |
| 2197 | LLVMSetLinkage(global_value, LLVMInternalLinkage); | |
| 2204 | 2198 | |
| 2205 | 2199 | var->value_ref = global_value; |
| 2206 | 2200 | } |
std/std.zig+3-3| ... | ... | @@ -12,14 +12,14 @@ pub var stdin = InStream { |
| 12 | 12 | |
| 13 | 13 | pub var stdout = OutStream { |
| 14 | 14 | .fd = stdout_fileno, |
| 15 | .buffer = uninitialized, | |
| 15 | .buffer = undefined, | |
| 16 | 16 | .index = 0, |
| 17 | 17 | .buffered = true, |
| 18 | 18 | }; |
| 19 | 19 | |
| 20 | 20 | pub var stderr = OutStream { |
| 21 | 21 | .fd = stderr_fileno, |
| 22 | .buffer = uninitialized, | |
| 22 | .buffer = undefined, | |
| 23 | 23 | .index = 0, |
| 24 | 24 | .buffered = false, |
| 25 | 25 | }; |
| ... | ... | @@ -34,7 +34,7 @@ pub %.BadPerm; |
| 34 | 34 | pub %.PipeFail; |
| 35 | 35 | */ |
| 36 | 36 | |
| 37 | //const buffer_size: u16 = 4 * 1024; | |
| 37 | //const buffer_size = 4 * 1024; | |
| 38 | 38 | const max_u64_base10_digits: isize = 20; |
| 39 | 39 | |
| 40 | 40 | /* |
test/run_tests.cpp+17-1| ... | ... | @@ -1244,6 +1244,20 @@ pub fn main(args: [][]u8) i32 => { |
| 1244 | 1244 | } |
| 1245 | 1245 | )SOURCE", "OK\n"); |
| 1246 | 1246 | |
| 1247 | add_simple_case("statically initialized array literal", R"SOURCE( | |
| 1248 | import "std.zig"; | |
| 1249 | const x = []u8{1,2,3,4}; | |
| 1250 | pub fn main(args: [][]u8) i32 => { | |
| 1251 | const y : [4]u8 = x; | |
| 1252 | if (y[3] != 4) { | |
| 1253 | print_str("BAD\n"); | |
| 1254 | } | |
| 1255 | ||
| 1256 | print_str("OK\n"); | |
| 1257 | return 0; | |
| 1258 | } | |
| 1259 | )SOURCE", "OK\n"); | |
| 1260 | ||
| 1247 | 1261 | } |
| 1248 | 1262 | |
| 1249 | 1263 | |
| ... | ... | @@ -1498,12 +1512,14 @@ struct A { |
| 1498 | 1512 | z : i32, |
| 1499 | 1513 | } |
| 1500 | 1514 | fn f() => { |
| 1515 | // we want the error on the '{' not the 'A' because | |
| 1516 | // the A could be a complicated expression | |
| 1501 | 1517 | const a = A { |
| 1502 | 1518 | .z = 4, |
| 1503 | 1519 | .y = 2, |
| 1504 | 1520 | }; |
| 1505 | 1521 | } |
| 1506 | )SOURCE", 1, ".tmp_source.zig:8:17: error: missing field: 'x'"); | |
| 1522 | )SOURCE", 1, ".tmp_source.zig:10:17: error: missing field: 'x'"); | |
| 1507 | 1523 | |
| 1508 | 1524 | add_compile_fail_case("invalid field in struct value expression", R"SOURCE( |
| 1509 | 1525 | struct A { |