authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-07 15:52:52-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-07 15:52:52-07:00
log6b3ce918db0bd73b5b1973b1baf358b6114810ed
treea5f9959c7bc1408f4f8c9add0b9c0987ae563821
parent42fe4e3cc8a05206e86a5b4cc2edb0dc4871a2c0

array.len generates a constant number literal expression


4 files changed, 18 insertions(+), 9 deletions(-)

src/analyze.cpp+7-4
......@@ -29,6 +29,8 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,
2929static TypeTableEntry *resolve_expr_const_val_as_void(CodeGen *g, AstNode *node);
3030static TypeTableEntry *resolve_expr_const_val_as_fn(CodeGen *g, AstNode *node, FnTableEntry *fn);
3131static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node, TypeTableEntry *type);
32static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, AstNode *node,
33 TypeTableEntry *expected_type, uint64_t x);
3234static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, AstNode *node);
3335static void analyze_top_level_decls_root(CodeGen *g, ImportTableEntry *import, AstNode *node);
3436
......@@ -2229,7 +2231,7 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
22292231}
22302232
22312233static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
2232 AstNode *node)
2234 TypeTableEntry *expected_type, AstNode *node)
22332235{
22342236 assert(node->type == NodeTypeFieldAccessExpr);
22352237
......@@ -2266,7 +2268,8 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
22662268 }
22672269 } else if (struct_type->id == TypeTableEntryIdArray) {
22682270 if (buf_eql_str(field_name, "len")) {
2269 return g->builtin_types.entry_isize;
2271 return resolve_expr_const_val_as_unsigned_num_lit(g, node, expected_type,
2272 struct_type->data.array.len);
22702273 } else {
22712274 add_node_error(g, node,
22722275 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
......@@ -2671,7 +2674,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
26712674 } else if (lhs_node->type == NodeTypeArrayAccessExpr) {
26722675 expected_rhs_type = analyze_array_access_expr(g, import, block_context, lhs_node);
26732676 } else if (lhs_node->type == NodeTypeFieldAccessExpr) {
2674 expected_rhs_type = analyze_field_access_expr(g, import, block_context, lhs_node);
2677 expected_rhs_type = analyze_field_access_expr(g, import, block_context, nullptr, lhs_node);
26752678 } else if (lhs_node->type == NodeTypePrefixOpExpr &&
26762679 lhs_node->data.prefix_op_expr.prefix_op == PrefixOpDereference)
26772680 {
......@@ -4849,7 +4852,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,
48494852 return_type = analyze_slice_expr(g, import, context, node);
48504853 break;
48514854 case NodeTypeFieldAccessExpr:
4852 return_type = analyze_field_access_expr(g, import, context, node);
4855 return_type = analyze_field_access_expr(g, import, context, expected_type, node);
48534856 break;
48544857 case NodeTypeContainerInitExpr:
48554858 return_type = analyze_container_init_expr(g, import, context, node);
std/std.zig+1-1
......@@ -201,7 +201,7 @@ pub fn buf_print_i64(out_buf: []u8, x: i64) -> isize {
201201pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {
202202 var buf: [max_u64_base10_digits]u8 = undefined;
203203 var a = x;
204 var index = buf.len;
204 var index: isize = buf.len;
205205
206206 while (true) {
207207 const digit = a % 10;
test/run_tests.cpp+2-4
......@@ -1043,11 +1043,9 @@ import "std.zig";
10431043
10441044pub fn main(args: [][]u8) -> %void {
10451045 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
1046 var i: @typeof(array_of_strings.len) = 0;
1047 while (i < array_of_strings.len) {
1048 %%stdout.printf(array_of_strings[i]);
1046 for (array_of_strings) |str| {
1047 %%stdout.printf(str);
10491048 %%stdout.printf("\n");
1050 i += 1;
10511049 }
10521050}
10531051 )SOURCE", "hello\nthis\nis\nmy\nthing\n");
test/self_hosted.zig+8
......@@ -269,3 +269,11 @@ fn memcpy_and_memset_intrinsics() {
269269
270270 if (bar[11] != 'A') unreachable{};
271271}
272
273
274#attribute("test")
275fn array_dot_len_const_expr() { }
276struct ArrayDotLenConstExpr {
277 y: [@const_eval(some_array.len)]u8,
278}
279const some_array = []u8 {0, 1, 2, 3};