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,...@@ -29,6 +29,8 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,
29static TypeTableEntry *resolve_expr_const_val_as_void(CodeGen *g, AstNode *node);29static TypeTableEntry *resolve_expr_const_val_as_void(CodeGen *g, AstNode *node);
30static TypeTableEntry *resolve_expr_const_val_as_fn(CodeGen *g, AstNode *node, FnTableEntry *fn);30static TypeTableEntry *resolve_expr_const_val_as_fn(CodeGen *g, AstNode *node, FnTableEntry *fn);
31static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node, TypeTableEntry *type);31static 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);
32static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, AstNode *node);34static void detect_top_level_decl_deps(CodeGen *g, ImportTableEntry *import, AstNode *node);
33static void analyze_top_level_decls_root(CodeGen *g, ImportTableEntry *import, AstNode *node);35static 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...@@ -2229,7 +2231,7 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
2229}2231}
22302232
2231static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,2233static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
2232 AstNode *node)2234 TypeTableEntry *expected_type, AstNode *node)
2233{2235{
2234 assert(node->type == NodeTypeFieldAccessExpr);2236 assert(node->type == NodeTypeFieldAccessExpr);
22352237
...@@ -2266,7 +2268,8 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i...@@ -2266,7 +2268,8 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
2266 }2268 }
2267 } else if (struct_type->id == TypeTableEntryIdArray) {2269 } else if (struct_type->id == TypeTableEntryIdArray) {
2268 if (buf_eql_str(field_name, "len")) {2270 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);
2270 } else {2273 } else {
2271 add_node_error(g, node,2274 add_node_error(g, node,
2272 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),2275 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...@@ -2671,7 +2674,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
2671 } else if (lhs_node->type == NodeTypeArrayAccessExpr) {2674 } else if (lhs_node->type == NodeTypeArrayAccessExpr) {
2672 expected_rhs_type = analyze_array_access_expr(g, import, block_context, lhs_node);2675 expected_rhs_type = analyze_array_access_expr(g, import, block_context, lhs_node);
2673 } else if (lhs_node->type == NodeTypeFieldAccessExpr) {2676 } 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);
2675 } else if (lhs_node->type == NodeTypePrefixOpExpr &&2678 } else if (lhs_node->type == NodeTypePrefixOpExpr &&
2676 lhs_node->data.prefix_op_expr.prefix_op == PrefixOpDereference)2679 lhs_node->data.prefix_op_expr.prefix_op == PrefixOpDereference)
2677 {2680 {
...@@ -4849,7 +4852,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -4849,7 +4852,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,
4849 return_type = analyze_slice_expr(g, import, context, node);4852 return_type = analyze_slice_expr(g, import, context, node);
4850 break;4853 break;
4851 case NodeTypeFieldAccessExpr:4854 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);
4853 break;4856 break;
4854 case NodeTypeContainerInitExpr:4857 case NodeTypeContainerInitExpr:
4855 return_type = analyze_container_init_expr(g, import, context, node);4858 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 {...@@ -201,7 +201,7 @@ pub fn buf_print_i64(out_buf: []u8, x: i64) -> isize {
201pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {201pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {
202 var buf: [max_u64_base10_digits]u8 = undefined;202 var buf: [max_u64_base10_digits]u8 = undefined;
203 var a = x;203 var a = x;
204 var index = buf.len;204 var index: isize = buf.len;
205205
206 while (true) {206 while (true) {
207 const digit = a % 10;207 const digit = a % 10;
test/run_tests.cpp+2-4
...@@ -1043,11 +1043,9 @@ import "std.zig";...@@ -1043,11 +1043,9 @@ import "std.zig";
10431043
1044pub fn main(args: [][]u8) -> %void {1044pub fn main(args: [][]u8) -> %void {
1045 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};1045 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
1046 var i: @typeof(array_of_strings.len) = 0;1046 for (array_of_strings) |str| {
1047 while (i < array_of_strings.len) {1047 %%stdout.printf(str);
1048 %%stdout.printf(array_of_strings[i]);
1049 %%stdout.printf("\n");1048 %%stdout.printf("\n");
1050 i += 1;
1051 }1049 }
1052}1050}
1053 )SOURCE", "hello\nthis\nis\nmy\nthing\n");1051 )SOURCE", "hello\nthis\nis\nmy\nthing\n");
test/self_hosted.zig+8
...@@ -269,3 +269,11 @@ fn memcpy_and_memset_intrinsics() {...@@ -269,3 +269,11 @@ fn memcpy_and_memset_intrinsics() {
269269
270 if (bar[11] != 'A') unreachable{};270 if (bar[11] != 'A') unreachable{};
271}271}
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};