Commit 42fe4e3cc8

Andrew Kelley <superjoe30@gmail.com>
2016-02-07 23:43:19
remove ptr field access of arrays
use &array[0] instead
1 parent 36cf9f0
src/analyze.cpp
@@ -2267,9 +2267,6 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
     } else if (struct_type->id == TypeTableEntryIdArray) {
         if (buf_eql_str(field_name, "len")) {
             return g->builtin_types.entry_isize;
-        } else if (buf_eql_str(field_name, "ptr")) {
-            // TODO determine whether the pointer should be const
-            return get_pointer_to_type(g, struct_type->data.array.child_type, false);
         } else {
             add_node_error(g, node,
                 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
src/codegen.cpp
@@ -866,14 +866,6 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva
         if (buf_eql_str(name, "len")) {
             return LLVMConstInt(g->builtin_types.entry_isize->type_ref,
                     struct_type->data.array.len, false);
-        } else if (buf_eql_str(name, "ptr")) {
-            LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr);
-            LLVMValueRef indices[] = {
-                LLVMConstNull(g->builtin_types.entry_isize->type_ref),
-                LLVMConstNull(g->builtin_types.entry_isize->type_ref),
-            };
-            add_debug_source_node(g, node);
-            return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, "");
         } else {
             zig_panic("gen_field_access_expr bad array field");
         }
std/rand.zig
@@ -28,7 +28,7 @@ pub struct Rand {
         var bytes_left = r.get_bytes_aligned(buf);
         if (bytes_left > 0) {
             var rand_val_array : [@sizeof(u32)]u8 = undefined;
-            *((&u32)(rand_val_array.ptr)) = r.get_u32();
+            *((&u32)(&rand_val_array[0])) = r.get_u32();
             while (bytes_left > 0) {
                 buf[buf.len - bytes_left] = rand_val_array[@sizeof(u32) - bytes_left];
                 bytes_left -= 1;
@@ -46,7 +46,7 @@ pub struct Rand {
 
         while (true) {
             r.get_bytes_aligned(rand_val_array);
-            const rand_val = *(&u64)(rand_val_array.ptr);
+            const rand_val = *(&u64)(&rand_val_array[0]);
             if (rand_val < upper_bound) {
                 return start + (rand_val % range);
             }
std/std.zig
@@ -105,7 +105,7 @@ pub struct OutStream {
     }
 
     pub fn flush(os: &OutStream) -> %void {
-        const amt_written = write(os.fd, os.buffer.ptr, os.index);
+        const amt_written = write(os.fd, &os.buffer[0], os.index);
         os.index = 0;
         if (amt_written < 0) {
             return switch (-amt_written) {
@@ -127,7 +127,7 @@ pub struct InStream {
     fd: isize,
 
     pub fn read(is: &InStream, buf: []u8) -> %isize {
-        const amt_read = read(is.fd, buf.ptr, buf.len);
+        const amt_read = read(is.fd, &buf[0], buf.len);
         if (amt_read < 0) {
             return switch (-amt_read) {
                 EINVAL => unreachable{},
test/run_tests.cpp
@@ -921,33 +921,6 @@ pub fn main(args: [][]u8) -> %void {
         "min i64: -9223372036854775808\n");
 
 
-    add_simple_case("slicing", R"SOURCE(
-import "std.zig";
-pub fn main(args: [][]u8) -> %void {
-    var array : [20]i32 = undefined;
-
-    array[5] = 1234;
-
-    var slice = array[5...10];
-
-    if (slice.len != 5) {
-        %%stdout.printf("BAD\n");
-    }
-
-    if (slice.ptr[0] != 1234) {
-        %%stdout.printf("BAD\n");
-    }
-
-    var slice_rest = array[10...];
-    if (slice_rest.len != 10) {
-        %%stdout.printf("BAD\n");
-    }
-
-    %%stdout.printf("OK\n");
-}
-    )SOURCE", "OK\n");
-
-
     add_simple_case("else if expression", R"SOURCE(
 import "std.zig";
 pub fn main(args: [][]u8) -> %void {
@@ -983,23 +956,6 @@ pub fn main(args: [][]u8) -> %void {
 }
     )SOURCE", "OK\n");
 
-    add_simple_case("memcpy and memset intrinsics", R"SOURCE(
-import "std.zig";
-pub fn main(args: [][]u8) -> %void {
-    var foo : [20]u8 = undefined;
-    var bar : [20]u8 = undefined;
-
-    @memset(foo.ptr, 'A', foo.len);
-    @memcpy(bar.ptr, foo.ptr, bar.len);
-
-    if (bar[11] != 'A') {
-        %%stdout.printf("BAD\n");
-    }
-
-    %%stdout.printf("OK\n");
-}
-    )SOURCE", "OK\n");
-
     add_simple_case("order-independent declarations", R"SOURCE(
 import "std.zig";
 const z = stdin_fileno;
@@ -1413,7 +1369,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
 export fn main(args: c_int, argv: &&u8) -> c_int {
     var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
 
-    qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn);
+    qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);
 
     for (array) |item, i| {
         if (item != i) {
test/self_hosted.zig
@@ -240,3 +240,32 @@ fn builtin_const_eval() {
     const x : i32 = @const_eval(1 + 2 + 3);
     if (x != @const_eval(6)) unreachable{};
 }
+
+#attribute("test")
+fn slicing() {
+    var array : [20]i32 = undefined;
+
+    array[5] = 1234;
+
+    var slice = array[5...10];
+
+    if (slice.len != 5) unreachable{};
+
+    const ptr = &slice[0];
+    if (ptr[0] != 1234) unreachable{};
+
+    var slice_rest = array[10...];
+    if (slice_rest.len != 10) unreachable{};
+}
+
+
+#attribute("test")
+fn memcpy_and_memset_intrinsics() {
+    var foo : [20]u8 = undefined;
+    var bar : [20]u8 = undefined;
+
+    @memset(&foo[0], 'A', foo.len);
+    @memcpy(&bar[0], &foo[0], bar.len);
+
+    if (bar[11] != 'A') unreachable{};
+}