| author | |
| committer | |
| log | 42fe4e3cc8a05206e86a5b4cc2edb0dc4871a2c0 |
| tree | 1d830201fa2b24d9b0c2ebaadc7b4cfce74784b6 |
| parent | 36cf9f0c721a9b1be04490d06dffae9e070fd724 |
use &array[0] instead6 files changed, 34 insertions(+), 60 deletions(-)
src/analyze.cpp-3| ... | @@ -2267,9 +2267,6 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i | ... | @@ -2267,9 +2267,6 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i |
| 2267 | } else if (struct_type->id == TypeTableEntryIdArray) { | 2267 | } else if (struct_type->id == TypeTableEntryIdArray) { |
| 2268 | if (buf_eql_str(field_name, "len")) { | 2268 | if (buf_eql_str(field_name, "len")) { |
| 2269 | return g->builtin_types.entry_isize; | 2269 | return g->builtin_types.entry_isize; |
| 2270 | } else if (buf_eql_str(field_name, "ptr")) { | ||
| 2271 | // TODO determine whether the pointer should be const | ||
| 2272 | return get_pointer_to_type(g, struct_type->data.array.child_type, false); | ||
| 2273 | } else { | 2270 | } else { |
| 2274 | add_node_error(g, node, | 2271 | add_node_error(g, node, |
| 2275 | buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), | 2272 | buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), |
src/codegen.cpp-8| ... | @@ -866,14 +866,6 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva | ... | @@ -866,14 +866,6 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva |
| 866 | if (buf_eql_str(name, "len")) { | 866 | if (buf_eql_str(name, "len")) { |
| 867 | return LLVMConstInt(g->builtin_types.entry_isize->type_ref, | 867 | return LLVMConstInt(g->builtin_types.entry_isize->type_ref, |
| 868 | struct_type->data.array.len, false); | 868 | struct_type->data.array.len, false); |
| 869 | } else if (buf_eql_str(name, "ptr")) { | ||
| 870 | LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr); | ||
| 871 | LLVMValueRef indices[] = { | ||
| 872 | LLVMConstNull(g->builtin_types.entry_isize->type_ref), | ||
| 873 | LLVMConstNull(g->builtin_types.entry_isize->type_ref), | ||
| 874 | }; | ||
| 875 | add_debug_source_node(g, node); | ||
| 876 | return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, ""); | ||
| 877 | } else { | 869 | } else { |
| 878 | zig_panic("gen_field_access_expr bad array field"); | 870 | zig_panic("gen_field_access_expr bad array field"); |
| 879 | } | 871 | } |
std/rand.zig+2-2| ... | @@ -28,7 +28,7 @@ pub struct Rand { | ... | @@ -28,7 +28,7 @@ pub struct Rand { |
| 28 | var bytes_left = r.get_bytes_aligned(buf); | 28 | var bytes_left = r.get_bytes_aligned(buf); |
| 29 | if (bytes_left > 0) { | 29 | if (bytes_left > 0) { |
| 30 | var rand_val_array : [@sizeof(u32)]u8 = undefined; | 30 | var rand_val_array : [@sizeof(u32)]u8 = undefined; |
| 31 | *((&u32)(rand_val_array.ptr)) = r.get_u32(); | 31 | *((&u32)(&rand_val_array[0])) = r.get_u32(); |
| 32 | while (bytes_left > 0) { | 32 | while (bytes_left > 0) { |
| 33 | buf[buf.len - bytes_left] = rand_val_array[@sizeof(u32) - bytes_left]; | 33 | buf[buf.len - bytes_left] = rand_val_array[@sizeof(u32) - bytes_left]; |
| 34 | bytes_left -= 1; | 34 | bytes_left -= 1; |
| ... | @@ -46,7 +46,7 @@ pub struct Rand { | ... | @@ -46,7 +46,7 @@ pub struct Rand { |
| 46 | 46 | ||
| 47 | while (true) { | 47 | while (true) { |
| 48 | r.get_bytes_aligned(rand_val_array); | 48 | r.get_bytes_aligned(rand_val_array); |
| 49 | const rand_val = *(&u64)(rand_val_array.ptr); | 49 | const rand_val = *(&u64)(&rand_val_array[0]); |
| 50 | if (rand_val < upper_bound) { | 50 | if (rand_val < upper_bound) { |
| 51 | return start + (rand_val % range); | 51 | return start + (rand_val % range); |
| 52 | } | 52 | } |
std/std.zig+2-2| ... | @@ -105,7 +105,7 @@ pub struct OutStream { | ... | @@ -105,7 +105,7 @@ pub struct OutStream { |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | pub fn flush(os: &OutStream) -> %void { | 107 | pub fn flush(os: &OutStream) -> %void { |
| 108 | const amt_written = write(os.fd, os.buffer.ptr, os.index); | 108 | const amt_written = write(os.fd, &os.buffer[0], os.index); |
| 109 | os.index = 0; | 109 | os.index = 0; |
| 110 | if (amt_written < 0) { | 110 | if (amt_written < 0) { |
| 111 | return switch (-amt_written) { | 111 | return switch (-amt_written) { |
| ... | @@ -127,7 +127,7 @@ pub struct InStream { | ... | @@ -127,7 +127,7 @@ pub struct InStream { |
| 127 | fd: isize, | 127 | fd: isize, |
| 128 | 128 | ||
| 129 | pub fn read(is: &InStream, buf: []u8) -> %isize { | 129 | pub fn read(is: &InStream, buf: []u8) -> %isize { |
| 130 | const amt_read = read(is.fd, buf.ptr, buf.len); | 130 | const amt_read = read(is.fd, &buf[0], buf.len); |
| 131 | if (amt_read < 0) { | 131 | if (amt_read < 0) { |
| 132 | return switch (-amt_read) { | 132 | return switch (-amt_read) { |
| 133 | EINVAL => unreachable{}, | 133 | EINVAL => unreachable{}, |
test/run_tests.cpp+1-45| ... | @@ -921,33 +921,6 @@ pub fn main(args: [][]u8) -> %void { | ... | @@ -921,33 +921,6 @@ pub fn main(args: [][]u8) -> %void { |
| 921 | "min i64: -9223372036854775808\n"); | 921 | "min i64: -9223372036854775808\n"); |
| 922 | 922 | ||
| 923 | 923 | ||
| 924 | add_simple_case("slicing", R"SOURCE( | ||
| 925 | import "std.zig"; | ||
| 926 | pub fn main(args: [][]u8) -> %void { | ||
| 927 | var array : [20]i32 = undefined; | ||
| 928 | |||
| 929 | array[5] = 1234; | ||
| 930 | |||
| 931 | var slice = array[5...10]; | ||
| 932 | |||
| 933 | if (slice.len != 5) { | ||
| 934 | %%stdout.printf("BAD\n"); | ||
| 935 | } | ||
| 936 | |||
| 937 | if (slice.ptr[0] != 1234) { | ||
| 938 | %%stdout.printf("BAD\n"); | ||
| 939 | } | ||
| 940 | |||
| 941 | var slice_rest = array[10...]; | ||
| 942 | if (slice_rest.len != 10) { | ||
| 943 | %%stdout.printf("BAD\n"); | ||
| 944 | } | ||
| 945 | |||
| 946 | %%stdout.printf("OK\n"); | ||
| 947 | } | ||
| 948 | )SOURCE", "OK\n"); | ||
| 949 | |||
| 950 | |||
| 951 | add_simple_case("else if expression", R"SOURCE( | 924 | add_simple_case("else if expression", R"SOURCE( |
| 952 | import "std.zig"; | 925 | import "std.zig"; |
| 953 | pub fn main(args: [][]u8) -> %void { | 926 | pub fn main(args: [][]u8) -> %void { |
| ... | @@ -983,23 +956,6 @@ pub fn main(args: [][]u8) -> %void { | ... | @@ -983,23 +956,6 @@ pub fn main(args: [][]u8) -> %void { |
| 983 | } | 956 | } |
| 984 | )SOURCE", "OK\n"); | 957 | )SOURCE", "OK\n"); |
| 985 | 958 | ||
| 986 | add_simple_case("memcpy and memset intrinsics", R"SOURCE( | ||
| 987 | import "std.zig"; | ||
| 988 | pub fn main(args: [][]u8) -> %void { | ||
| 989 | var foo : [20]u8 = undefined; | ||
| 990 | var bar : [20]u8 = undefined; | ||
| 991 | |||
| 992 | @memset(foo.ptr, 'A', foo.len); | ||
| 993 | @memcpy(bar.ptr, foo.ptr, bar.len); | ||
| 994 | |||
| 995 | if (bar[11] != 'A') { | ||
| 996 | %%stdout.printf("BAD\n"); | ||
| 997 | } | ||
| 998 | |||
| 999 | %%stdout.printf("OK\n"); | ||
| 1000 | } | ||
| 1001 | )SOURCE", "OK\n"); | ||
| 1002 | |||
| 1003 | add_simple_case("order-independent declarations", R"SOURCE( | 959 | add_simple_case("order-independent declarations", R"SOURCE( |
| 1004 | import "std.zig"; | 960 | import "std.zig"; |
| 1005 | const z = stdin_fileno; | 961 | const z = stdin_fileno; |
| ... | @@ -1413,7 +1369,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int { | ... | @@ -1413,7 +1369,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int { |
| 1413 | export fn main(args: c_int, argv: &&u8) -> c_int { | 1369 | export fn main(args: c_int, argv: &&u8) -> c_int { |
| 1414 | var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 }; | 1370 | var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 }; |
| 1415 | 1371 | ||
| 1416 | qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn); | 1372 | qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn); |
| 1417 | 1373 | ||
| 1418 | for (array) |item, i| { | 1374 | for (array) |item, i| { |
| 1419 | if (item != i) { | 1375 | if (item != i) { |
test/self_hosted.zig+29| ... | @@ -240,3 +240,32 @@ fn builtin_const_eval() { | ... | @@ -240,3 +240,32 @@ fn builtin_const_eval() { |
| 240 | const x : i32 = @const_eval(1 + 2 + 3); | 240 | const x : i32 = @const_eval(1 + 2 + 3); |
| 241 | if (x != @const_eval(6)) unreachable{}; | 241 | if (x != @const_eval(6)) unreachable{}; |
| 242 | } | 242 | } |
| 243 | |||
| 244 | #attribute("test") | ||
| 245 | fn slicing() { | ||
| 246 | var array : [20]i32 = undefined; | ||
| 247 | |||
| 248 | array[5] = 1234; | ||
| 249 | |||
| 250 | var slice = array[5...10]; | ||
| 251 | |||
| 252 | if (slice.len != 5) unreachable{}; | ||
| 253 | |||
| 254 | const ptr = &slice[0]; | ||
| 255 | if (ptr[0] != 1234) unreachable{}; | ||
| 256 | |||
| 257 | var slice_rest = array[10...]; | ||
| 258 | if (slice_rest.len != 10) unreachable{}; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | #attribute("test") | ||
| 263 | fn memcpy_and_memset_intrinsics() { | ||
| 264 | var foo : [20]u8 = undefined; | ||
| 265 | var bar : [20]u8 = undefined; | ||
| 266 | |||
| 267 | @memset(&foo[0], 'A', foo.len); | ||
| 268 | @memcpy(&bar[0], &foo[0], bar.len); | ||
| 269 | |||
| 270 | if (bar[11] != 'A') unreachable{}; | ||
| 271 | } |