authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-22 22:02:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-22 22:02:07-07:00
log523e3b86af44b97bcf68e3eb0956ef297421ee10
tree61b3743aced3715ebc8f811cc3477fca45d0ad20
parent21fc5a6402c527675900397ea60f107730985f1c

support statically initialized array literal


4 files changed, 38 insertions(+), 16 deletions(-)

src/analyze.cpp+16-4
...@@ -34,8 +34,6 @@ static AstNode *first_executing_node(AstNode *node) {...@@ -34,8 +34,6 @@ static AstNode *first_executing_node(AstNode *node) {
34 return first_executing_node(node->data.field_access_expr.struct_expr);34 return first_executing_node(node->data.field_access_expr.struct_expr);
35 case NodeTypeSwitchRange:35 case NodeTypeSwitchRange:
36 return first_executing_node(node->data.switch_range.start);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 case NodeTypeRoot:37 case NodeTypeRoot:
40 case NodeTypeRootExportDecl:38 case NodeTypeRootExportDecl:
41 case NodeTypeFnProto:39 case NodeTypeFnProto:
...@@ -72,6 +70,7 @@ static AstNode *first_executing_node(AstNode *node) {...@@ -72,6 +70,7 @@ static AstNode *first_executing_node(AstNode *node) {
72 case NodeTypeSwitchExpr:70 case NodeTypeSwitchExpr:
73 case NodeTypeSwitchProng:71 case NodeTypeSwitchProng:
74 case NodeTypeArrayType:72 case NodeTypeArrayType:
73 case NodeTypeContainerInitExpr:
75 return node;74 return node;
76 }75 }
77 zig_unreachable();76 zig_unreachable();
...@@ -1586,9 +1585,22 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry...@@ -1586,9 +1585,22 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
1586 assert(pointer_type->id == TypeTableEntryIdPointer);1585 assert(pointer_type->id == TypeTableEntryIdPointer);
1587 TypeTableEntry *child_type = pointer_type->data.pointer.child_type;1586 TypeTableEntry *child_type = pointer_type->data.pointer.child_type;
15881587
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 for (int i = 0; i < elem_count; i += 1) {1592 for (int i = 0; i < elem_count; i += 1) {
1590 AstNode *elem_node = container_init_expr->entries.at(i);1593 AstNode **elem_node = &container_init_expr->entries.at(i);
1591 analyze_expression(g, import, context, child_type, elem_node);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 }
15931605
1594 TypeTableEntry *fixed_size_array_type = get_array_type(g, child_type, elem_count);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,13 +2105,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
2105 return tag_value;2105 return tag_value;
2106 } else {2106 } else {
2107 zig_panic("TODO");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 } else if (type_entry->id == TypeTableEntryIdFn) {2109 } else if (type_entry->id == TypeTableEntryIdFn) {
2117 return const_val->data.x_fn->fn_value;2110 return const_val->data.x_fn->fn_value;
...@@ -2197,10 +2190,11 @@ static void do_code_gen(CodeGen *g) {...@@ -2197,10 +2190,11 @@ static void do_code_gen(CodeGen *g) {
2197 } else {2190 } else {
2198 init_val = LLVMConstNull(var->type->type_ref);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 LLVMSetInitializer(global_value, init_val);2194 LLVMSetInitializer(global_value, init_val);
2202 LLVMSetGlobalConstant(global_value, var->is_const);2195 LLVMSetGlobalConstant(global_value, var->is_const);
2203 LLVMSetUnnamedAddr(global_value, true);2196 LLVMSetUnnamedAddr(global_value, true);
2197 LLVMSetLinkage(global_value, LLVMInternalLinkage);
22042198
2205 var->value_ref = global_value;2199 var->value_ref = global_value;
2206 }2200 }
std/std.zig+3-3
...@@ -12,14 +12,14 @@ pub var stdin = InStream {...@@ -12,14 +12,14 @@ pub var stdin = InStream {
1212
13pub var stdout = OutStream {13pub var stdout = OutStream {
14 .fd = stdout_fileno,14 .fd = stdout_fileno,
15 .buffer = uninitialized,15 .buffer = undefined,
16 .index = 0,16 .index = 0,
17 .buffered = true,17 .buffered = true,
18};18};
1919
20pub var stderr = OutStream {20pub var stderr = OutStream {
21 .fd = stderr_fileno,21 .fd = stderr_fileno,
22 .buffer = uninitialized,22 .buffer = undefined,
23 .index = 0,23 .index = 0,
24 .buffered = false,24 .buffered = false,
25};25};
...@@ -34,7 +34,7 @@ pub %.BadPerm;...@@ -34,7 +34,7 @@ pub %.BadPerm;
34pub %.PipeFail;34pub %.PipeFail;
35*/35*/
3636
37//const buffer_size: u16 = 4 * 1024;37//const buffer_size = 4 * 1024;
38const max_u64_base10_digits: isize = 20;38const max_u64_base10_digits: isize = 20;
3939
40/*40/*
test/run_tests.cpp+17-1
...@@ -1244,6 +1244,20 @@ pub fn main(args: [][]u8) i32 => {...@@ -1244,6 +1244,20 @@ pub fn main(args: [][]u8) i32 => {
1244}1244}
1245 )SOURCE", "OK\n");1245 )SOURCE", "OK\n");
12461246
1247 add_simple_case("statically initialized array literal", R"SOURCE(
1248import "std.zig";
1249const x = []u8{1,2,3,4};
1250pub 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}
12481262
12491263
...@@ -1498,12 +1512,14 @@ struct A {...@@ -1498,12 +1512,14 @@ struct A {
1498 z : i32,1512 z : i32,
1499}1513}
1500fn f() => {1514fn f() => {
1515 // we want the error on the '{' not the 'A' because
1516 // the A could be a complicated expression
1501 const a = A {1517 const a = A {
1502 .z = 4,1518 .z = 4,
1503 .y = 2,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'");
15071523
1508 add_compile_fail_case("invalid field in struct value expression", R"SOURCE(1524 add_compile_fail_case("invalid field in struct value expression", R"SOURCE(
1509struct A {1525struct A {