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) {
3434 return first_executing_node(node->data.field_access_expr.struct_expr);
3535 case NodeTypeSwitchRange:
3636 return first_executing_node(node->data.switch_range.start);
37 case NodeTypeContainerInitExpr:
38 return first_executing_node(node->data.container_init_expr.type);
3937 case NodeTypeRoot:
4038 case NodeTypeRootExportDecl:
4139 case NodeTypeFnProto:
......@@ -72,6 +70,7 @@ static AstNode *first_executing_node(AstNode *node) {
7270 case NodeTypeSwitchExpr:
7371 case NodeTypeSwitchProng:
7472 case NodeTypeArrayType:
73 case NodeTypeContainerInitExpr:
7574 return node;
7675 }
7776 zig_unreachable();
......@@ -1586,9 +1585,22 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
15861585 assert(pointer_type->id == TypeTableEntryIdPointer);
15871586 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
15891592 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 }
15921604 }
15931605
15941606 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
21052105 return tag_value;
21062106 } else {
21072107 zig_panic("TODO");
2108 /*
2109 LLVMValueRef fields[] = {
2110 tag_value,
2111 union_value,
2112 };
2113 return LLVMConstStruct(fields, 2, false);
2114 */
21152108 }
21162109 } else if (type_entry->id == TypeTableEntryIdFn) {
21172110 return const_val->data.x_fn->fn_value;
......@@ -2197,10 +2190,11 @@ static void do_code_gen(CodeGen *g) {
21972190 } else {
21982191 init_val = LLVMConstNull(var->type->type_ref);
21992192 }
2200 LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_val), "");
2193 LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_val), buf_ptr(&var->name));
22012194 LLVMSetInitializer(global_value, init_val);
22022195 LLVMSetGlobalConstant(global_value, var->is_const);
22032196 LLVMSetUnnamedAddr(global_value, true);
2197 LLVMSetLinkage(global_value, LLVMInternalLinkage);
22042198
22052199 var->value_ref = global_value;
22062200 }
std/std.zig+3-3
......@@ -12,14 +12,14 @@ pub var stdin = InStream {
1212
1313pub var stdout = OutStream {
1414 .fd = stdout_fileno,
15 .buffer = uninitialized,
15 .buffer = undefined,
1616 .index = 0,
1717 .buffered = true,
1818};
1919
2020pub var stderr = OutStream {
2121 .fd = stderr_fileno,
22 .buffer = uninitialized,
22 .buffer = undefined,
2323 .index = 0,
2424 .buffered = false,
2525};
......@@ -34,7 +34,7 @@ pub %.BadPerm;
3434pub %.PipeFail;
3535*/
3636
37//const buffer_size: u16 = 4 * 1024;
37//const buffer_size = 4 * 1024;
3838const max_u64_base10_digits: isize = 20;
3939
4040/*
test/run_tests.cpp+17-1
......@@ -1244,6 +1244,20 @@ pub fn main(args: [][]u8) i32 => {
12441244}
12451245 )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
12471261}
12481262
12491263
......@@ -1498,12 +1512,14 @@ struct A {
14981512 z : i32,
14991513}
15001514fn f() => {
1515 // we want the error on the '{' not the 'A' because
1516 // the A could be a complicated expression
15011517 const a = A {
15021518 .z = 4,
15031519 .y = 2,
15041520 };
15051521}
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
15081524 add_compile_fail_case("invalid field in struct value expression", R"SOURCE(
15091525struct A {