From 4ac36d094c06b04c1c71d972d3f3e1187bccea95 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Fri, 27 Apr 2018 19:27:58 -0400
Subject: add std.atomic.Stack and std.atomic.Queue
---
std/atomic/index.zig | 7 +++++++
std/atomic/queue.zig | 37 +++++++++++++++++++++++++++++++++++++
std/atomic/stack.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 89 insertions(+)
create mode 100644 std/atomic/index.zig
create mode 100644 std/atomic/queue.zig
create mode 100644 std/atomic/stack.zig
(limited to 'std/atomic')
diff --git a/std/atomic/index.zig b/std/atomic/index.zig
new file mode 100644
index 0000000000..9d556a6415
--- /dev/null
+++ b/std/atomic/index.zig
@@ -0,0 +1,7 @@
+pub const Stack = @import("stack.zig").Stack;
+pub const Queue = @import("queue.zig").Queue;
+
+test "std.atomic" {
+ _ = @import("stack.zig").Stack;
+ _ = @import("queue.zig").Queue;
+}
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
new file mode 100644
index 0000000000..54981d4a61
--- /dev/null
+++ b/std/atomic/queue.zig
@@ -0,0 +1,37 @@
+/// Many reader, many writer, non-allocating, thread-safe, lock-free
+pub fn Queue(comptime T: type) type {
+ return struct {
+ head: &Node,
+ tail: &Node,
+ root: Node,
+
+ pub const Self = this;
+
+ pub const Node = struct {
+ next: ?&Node,
+ data: T,
+ };
+
+ // TODO: well defined copy elision
+ pub fn init(self: &Self) void {
+ self.root.next = null;
+ self.head = &self.root;
+ self.tail = &self.root;
+ }
+
+ pub fn put(self: &Self, node: &Node) void {
+ node.next = null;
+
+ const tail = @atomicRmw(&Node, &self.tail, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
+ _ = @atomicRmw(?&Node, &tail.next, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
+ }
+
+ pub fn get(self: &Self) ?&Node {
+ var head = @atomicLoad(&Node, &self.head, AtomicOrder.Acquire);
+ while (true) {
+ const node = head.next ?? return null;
+ head = @cmpxchgWeak(&Node, &self.head, head, node, AtomicOrder.Release, AtomicOrder.Acquire) ?? return node;
+ }
+ }
+ };
+}
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
new file mode 100644
index 0000000000..4ceecb7b1d
--- /dev/null
+++ b/std/atomic/stack.zig
@@ -0,0 +1,45 @@
+/// Many reader, many writer, non-allocating, thread-safe, lock-free
+pub fn Stack(comptime T: type) type {
+ return struct {
+ root: ?&Node,
+
+ pub const Self = this;
+
+ pub const Node = struct {
+ next: ?&Node,
+ data: T,
+ };
+
+ pub fn init() Self {
+ return Self {
+ .root = null,
+ };
+ }
+
+ /// push operation, but only if you are the first item in the stack. if you did not succeed in
+ /// being the first item in the stack, returns the other item that was there.
+ pub fn pushFirst(self: &Self, node: &Node) ?&Node {
+ node.next = null;
+ return @cmpxchgStrong(?&Node, &self.root, null, node, AtomicOrder.AcqRel, AtomicOrder.AcqRel);
+ }
+
+ pub fn push(self: &Self, node: &Node) void {
+ var root = @atomicLoad(?&Node, &self.root, AtomicOrder.Acquire);
+ while (true) {
+ node.next = root;
+ root = @cmpxchgWeak(?&Node, &self.root, root, node, AtomicOrder.Release, AtomicOrder.Acquire) ?? break;
+ }
+ }
+
+ pub fn pop(self: &Self) ?&Node {
+ var root = @atomicLoad(?&Node, &self.root, AtomicOrder.Acquire);
+ while (true) {
+ root = @cmpxchgWeak(?&Node, &self.root, root, (root ?? return null).next, AtomicOrder.Release, AtomicOrder.Acquire) ?? return root;
+ }
+ }
+
+ pub fn isEmpty(self: &Self) bool {
+ return @atomicLoad(?&Node, &self.root, AtomicOrder.Relaxed) == null;
+ }
+ };
+}
--
cgit v1.2.3
From 96ecb402590df7a02526009f1630f27e14a0e77c Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sat, 28 Apr 2018 17:53:06 -0400
Subject: add fuzz tests for std.atomic.Stack
---
src/ir.cpp | 5 +++
std/atomic/stack.zig | 87 +++++++++++++++++++++++++++++++++++++++++++++++++---
std/heap.zig | 64 +++++++++++++++++++++++++++++++++-----
3 files changed, 143 insertions(+), 13 deletions(-)
(limited to 'std/atomic')
diff --git a/src/ir.cpp b/src/ir.cpp
index 4bf8240472..469900bf07 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -18184,6 +18184,11 @@ static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstr
} else {
if (!ir_resolve_atomic_order(ira, instruction->ordering->other, &ordering))
return ira->codegen->builtin_types.entry_invalid;
+ if (ordering == AtomicOrderUnordered) {
+ ir_add_error(ira, instruction->ordering,
+ buf_sprintf("@atomicRmw atomic ordering must not be Unordered"));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
}
if (instr_is_comptime(casted_operand) && instr_is_comptime(casted_ptr) && casted_ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar)
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 4ceecb7b1d..a1e686155c 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -1,3 +1,6 @@
+const builtin = @import("builtin");
+const AtomicOrder = builtin.AtomicOrder;
+
/// Many reader, many writer, non-allocating, thread-safe, lock-free
pub fn Stack(comptime T: type) type {
return struct {
@@ -20,26 +23,100 @@ pub fn Stack(comptime T: type) type {
/// being the first item in the stack, returns the other item that was there.
pub fn pushFirst(self: &Self, node: &Node) ?&Node {
node.next = null;
- return @cmpxchgStrong(?&Node, &self.root, null, node, AtomicOrder.AcqRel, AtomicOrder.AcqRel);
+ return @cmpxchgStrong(?&Node, &self.root, null, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst);
}
pub fn push(self: &Self, node: &Node) void {
- var root = @atomicLoad(?&Node, &self.root, AtomicOrder.Acquire);
+ var root = @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst);
while (true) {
node.next = root;
- root = @cmpxchgWeak(?&Node, &self.root, root, node, AtomicOrder.Release, AtomicOrder.Acquire) ?? break;
+ root = @cmpxchgWeak(?&Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? break;
}
}
pub fn pop(self: &Self) ?&Node {
var root = @atomicLoad(?&Node, &self.root, AtomicOrder.Acquire);
while (true) {
- root = @cmpxchgWeak(?&Node, &self.root, root, (root ?? return null).next, AtomicOrder.Release, AtomicOrder.Acquire) ?? return root;
+ root = @cmpxchgWeak(?&Node, &self.root, root, (root ?? return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return root;
}
}
pub fn isEmpty(self: &Self) bool {
- return @atomicLoad(?&Node, &self.root, AtomicOrder.Relaxed) == null;
+ return @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst) == null;
}
};
}
+
+const std = @import("std");
+const Context = struct {
+ allocator: &std.mem.Allocator,
+ stack: &Stack(i32),
+ put_sum: isize,
+ get_sum: isize,
+ puts_done: u8, // TODO make this a bool
+};
+const puts_per_thread = 1000;
+const put_thread_count = 3;
+
+test "std.atomic.stack" {
+ var direct_allocator = std.heap.DirectAllocator.init();
+ defer direct_allocator.deinit();
+
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
+ defer direct_allocator.allocator.free(plenty_of_memory);
+
+ var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
+ var a = &fixed_buffer_allocator.allocator;
+
+ var stack = Stack(i32).init();
+ var context = Context {
+ .allocator = a,
+ .stack = &stack,
+ .put_sum = 0,
+ .get_sum = 0,
+ .puts_done = 0,
+ };
+
+ var putters: [put_thread_count]&std.os.Thread = undefined;
+ for (putters) |*t| {
+ *t = try std.os.spawnThreadAllocator(a, &context, startPuts);
+ }
+ var getters: [put_thread_count]&std.os.Thread = undefined;
+ for (getters) |*t| {
+ *t = try std.os.spawnThreadAllocator(a, &context, startGets);
+ }
+
+ for (putters) |t| t.wait();
+ _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ for (getters) |t| t.wait();
+
+ std.debug.assert(context.put_sum == context.get_sum);
+}
+
+fn startPuts(ctx: &Context) u8 {
+ var put_count: usize = puts_per_thread;
+ var r = std.rand.DefaultPrng.init(0xdeadbeef);
+ while (put_count != 0) : (put_count -= 1) {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ const x = @bitCast(i32, r.random.scalar(u32));
+ const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
+ node.data = x;
+ ctx.stack.push(node);
+ _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
+ }
+ return 0;
+}
+
+fn startGets(ctx: &Context) u8 {
+ while (true) {
+ while (ctx.stack.pop()) |node| {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
+ }
+
+ if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/std/heap.zig b/std/heap.zig
index b3a1e6bf27..d632b44cd1 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -47,13 +47,6 @@ pub const DirectAllocator = struct {
const HeapHandle = if (builtin.os == Os.windows) os.windows.HANDLE else void;
- //pub const canary_bytes = []u8 {48, 239, 128, 46, 18, 49, 147, 9, 195, 59, 203, 3, 245, 54, 9, 122};
- //pub const want_safety = switch (builtin.mode) {
- // builtin.Mode.Debug => true,
- // builtin.Mode.ReleaseSafe => true,
- // else => false,
- //};
-
pub fn init() DirectAllocator {
return DirectAllocator {
.allocator = Allocator {
@@ -298,7 +291,7 @@ pub const FixedBufferAllocator = struct {
fn alloc(allocator: &Allocator, n: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator);
- const addr = @ptrToInt(&self.buffer[self.end_index]);
+ const addr = @ptrToInt(self.buffer.ptr) + self.end_index;
const rem = @rem(addr, alignment);
const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
const adjusted_index = self.end_index + march_forward_bytes;
@@ -325,6 +318,54 @@ pub const FixedBufferAllocator = struct {
fn free(allocator: &Allocator, bytes: []u8) void { }
};
+/// lock free
+pub const ThreadSafeFixedBufferAllocator = struct {
+ allocator: Allocator,
+ end_index: usize,
+ buffer: []u8,
+
+ pub fn init(buffer: []u8) ThreadSafeFixedBufferAllocator {
+ return ThreadSafeFixedBufferAllocator {
+ .allocator = Allocator {
+ .allocFn = alloc,
+ .reallocFn = realloc,
+ .freeFn = free,
+ },
+ .buffer = buffer,
+ .end_index = 0,
+ };
+ }
+
+ fn alloc(allocator: &Allocator, n: usize, alignment: u29) ![]u8 {
+ const self = @fieldParentPtr(ThreadSafeFixedBufferAllocator, "allocator", allocator);
+ var end_index = @atomicLoad(usize, &self.end_index, builtin.AtomicOrder.SeqCst);
+ while (true) {
+ const addr = @ptrToInt(self.buffer.ptr) + end_index;
+ const rem = @rem(addr, alignment);
+ const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
+ const adjusted_index = end_index + march_forward_bytes;
+ const new_end_index = adjusted_index + n;
+ if (new_end_index > self.buffer.len) {
+ return error.OutOfMemory;
+ }
+ end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index,
+ builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) ?? return self.buffer[adjusted_index .. new_end_index];
+ }
+ }
+
+ fn realloc(allocator: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ if (new_size <= old_mem.len) {
+ return old_mem[0..new_size];
+ } else {
+ const result = try alloc(allocator, new_size, alignment);
+ mem.copy(u8, result, old_mem);
+ return result;
+ }
+ }
+
+ fn free(allocator: &Allocator, bytes: []u8) void { }
+};
+
test "c_allocator" {
@@ -363,6 +404,13 @@ test "FixedBufferAllocator" {
try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
}
+test "ThreadSafeFixedBufferAllocator" {
+ var fixed_buffer_allocator = ThreadSafeFixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]);
+
+ try testAllocator(&fixed_buffer_allocator.allocator);
+ try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
+}
+
fn testAllocator(allocator: &mem.Allocator) !void {
var slice = try allocator.alloc(&i32, 100);
--
cgit v1.2.3
From 5d6e44b3f2bf37deaf09ce4a473fa5b52a6fb760 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sat, 28 Apr 2018 18:00:51 -0400
Subject: add tests for std.atomic Queue and Stack
---
std/atomic/queue.zig | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++-
std/atomic/stack.zig | 4 +++
2 files changed, 88 insertions(+), 1 deletion(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 54981d4a61..c7ce00d6cf 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -1,3 +1,7 @@
+const builtin = @import("builtin");
+const AtomicOrder = builtin.AtomicOrder;
+const AtomicRmwOp = builtin.AtomicRmwOp;
+
/// Many reader, many writer, non-allocating, thread-safe, lock-free
pub fn Queue(comptime T: type) type {
return struct {
@@ -12,7 +16,7 @@ pub fn Queue(comptime T: type) type {
data: T,
};
- // TODO: well defined copy elision
+ // TODO: well defined copy elision: https://github.com/zig-lang/zig/issues/287
pub fn init(self: &Self) void {
self.root.next = null;
self.head = &self.root;
@@ -35,3 +39,82 @@ pub fn Queue(comptime T: type) type {
}
};
}
+
+const std = @import("std");
+const Context = struct {
+ allocator: &std.mem.Allocator,
+ queue: &Queue(i32),
+ put_sum: isize,
+ get_sum: isize,
+ get_count: usize,
+ puts_done: u8, // TODO make this a bool
+};
+const puts_per_thread = 10000;
+const put_thread_count = 3;
+
+test "std.atomic.queue" {
+ var direct_allocator = std.heap.DirectAllocator.init();
+ defer direct_allocator.deinit();
+
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
+ defer direct_allocator.allocator.free(plenty_of_memory);
+
+ var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
+ var a = &fixed_buffer_allocator.allocator;
+
+ var queue: Queue(i32) = undefined;
+ queue.init();
+ var context = Context {
+ .allocator = a,
+ .queue = &queue,
+ .put_sum = 0,
+ .get_sum = 0,
+ .puts_done = 0,
+ .get_count = 0,
+ };
+
+ var putters: [put_thread_count]&std.os.Thread = undefined;
+ for (putters) |*t| {
+ *t = try std.os.spawnThreadAllocator(a, &context, startPuts);
+ }
+ var getters: [put_thread_count]&std.os.Thread = undefined;
+ for (getters) |*t| {
+ *t = try std.os.spawnThreadAllocator(a, &context, startGets);
+ }
+
+ for (putters) |t| t.wait();
+ _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ for (getters) |t| t.wait();
+
+ std.debug.assert(context.put_sum == context.get_sum);
+ std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
+}
+
+fn startPuts(ctx: &Context) u8 {
+ var put_count: usize = puts_per_thread;
+ var r = std.rand.DefaultPrng.init(0xdeadbeef);
+ while (put_count != 0) : (put_count -= 1) {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ const x = @bitCast(i32, r.random.scalar(u32));
+ const node = ctx.allocator.create(Queue(i32).Node) catch unreachable;
+ node.data = x;
+ ctx.queue.put(node);
+ _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
+ }
+ return 0;
+}
+
+fn startGets(ctx: &Context) u8 {
+ while (true) {
+ while (ctx.queue.get()) |node| {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
+ _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
+ }
+
+ if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index a1e686155c..a53f7682b0 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -53,6 +53,7 @@ const Context = struct {
stack: &Stack(i32),
put_sum: isize,
get_sum: isize,
+ get_count: usize,
puts_done: u8, // TODO make this a bool
};
const puts_per_thread = 1000;
@@ -75,6 +76,7 @@ test "std.atomic.stack" {
.put_sum = 0,
.get_sum = 0,
.puts_done = 0,
+ .get_count = 0,
};
var putters: [put_thread_count]&std.os.Thread = undefined;
@@ -91,6 +93,7 @@ test "std.atomic.stack" {
for (getters) |t| t.wait();
std.debug.assert(context.put_sum == context.get_sum);
+ std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
}
fn startPuts(ctx: &Context) u8 {
@@ -112,6 +115,7 @@ fn startGets(ctx: &Context) u8 {
while (ctx.stack.pop()) |node| {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
+ _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
}
if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
--
cgit v1.2.3
From a10351b439769c57454e19915365b21e43f408bc Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sat, 28 Apr 2018 18:19:00 -0400
Subject: disable atomic stack and queue tests for non-linux
---
std/atomic/queue.zig | 4 ++++
std/atomic/stack.zig | 4 ++++
2 files changed, 8 insertions(+)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index c7ce00d6cf..3866bad7ce 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -53,6 +53,10 @@ const puts_per_thread = 10000;
const put_thread_count = 3;
test "std.atomic.queue" {
+ if (builtin.os != builtin.Os.linux) {
+ // TODO implement kernel threads for windows and macos
+ return;
+ }
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index a53f7682b0..12de2edaaa 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -60,6 +60,10 @@ const puts_per_thread = 1000;
const put_thread_count = 3;
test "std.atomic.stack" {
+ if (builtin.os != builtin.Os.linux) {
+ // TODO implement kernel threads for windows and macos
+ return;
+ }
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
--
cgit v1.2.3
From abf90eaa674782e092e49bb23c4c7da0f581f604 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sun, 29 Apr 2018 00:09:18 -0400
Subject: enable atomic queue and stack tests for macos
---
std/atomic/queue.zig | 4 ++--
std/atomic/stack.zig | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 3866bad7ce..dd9b869f02 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -53,8 +53,8 @@ const puts_per_thread = 10000;
const put_thread_count = 3;
test "std.atomic.queue" {
- if (builtin.os != builtin.Os.linux) {
- // TODO implement kernel threads for windows and macos
+ if (builtin.os == builtin.Os.windows) {
+ // TODO implement kernel threads for windows
return;
}
var direct_allocator = std.heap.DirectAllocator.init();
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 12de2edaaa..9f2ceacfa3 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -60,8 +60,8 @@ const puts_per_thread = 1000;
const put_thread_count = 3;
test "std.atomic.stack" {
- if (builtin.os != builtin.Os.linux) {
- // TODO implement kernel threads for windows and macos
+ if (builtin.os == builtin.Os.windows) {
+ // TODO implement kernel threads for windows
return;
}
var direct_allocator = std.heap.DirectAllocator.init();
--
cgit v1.2.3
From 6376d96824c5205ecc02b2c621bcef5dc78f1a81 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sun, 29 Apr 2018 02:40:22 -0400
Subject: support kernel threads for windows
* remove std.os.spawnThreadAllocator - windows does not support
an explicit stack, so using an allocator for a thread stack
space does not work.
* std.os.spawnThread - instead of accepting a stack argument, the
implementation will directly allocate using OS-specific APIs.
---
std/atomic/queue.zig | 8 +--
std/atomic/stack.zig | 8 +--
std/mem.zig | 1 +
std/os/index.zig | 165 ++++++++++++++++++++++++++++++-----------------
std/os/test.zig | 20 ++----
std/os/windows/index.zig | 6 ++
6 files changed, 120 insertions(+), 88 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index dd9b869f02..1acecbab2c 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -53,10 +53,6 @@ const puts_per_thread = 10000;
const put_thread_count = 3;
test "std.atomic.queue" {
- if (builtin.os == builtin.Os.windows) {
- // TODO implement kernel threads for windows
- return;
- }
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
@@ -79,11 +75,11 @@ test "std.atomic.queue" {
var putters: [put_thread_count]&std.os.Thread = undefined;
for (putters) |*t| {
- *t = try std.os.spawnThreadAllocator(a, &context, startPuts);
+ *t = try std.os.spawnThread(&context, startPuts);
}
var getters: [put_thread_count]&std.os.Thread = undefined;
for (getters) |*t| {
- *t = try std.os.spawnThreadAllocator(a, &context, startGets);
+ *t = try std.os.spawnThread(&context, startGets);
}
for (putters) |t| t.wait();
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 9f2ceacfa3..accbcc942a 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -60,10 +60,6 @@ const puts_per_thread = 1000;
const put_thread_count = 3;
test "std.atomic.stack" {
- if (builtin.os == builtin.Os.windows) {
- // TODO implement kernel threads for windows
- return;
- }
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
@@ -85,11 +81,11 @@ test "std.atomic.stack" {
var putters: [put_thread_count]&std.os.Thread = undefined;
for (putters) |*t| {
- *t = try std.os.spawnThreadAllocator(a, &context, startPuts);
+ *t = try std.os.spawnThread(&context, startPuts);
}
var getters: [put_thread_count]&std.os.Thread = undefined;
for (getters) |*t| {
- *t = try std.os.spawnThreadAllocator(a, &context, startGets);
+ *t = try std.os.spawnThread(&context, startGets);
}
for (putters) |t| t.wait();
diff --git a/std/mem.zig b/std/mem.zig
index cc3161cddd..0f66f549cc 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -32,6 +32,7 @@ pub const Allocator = struct {
freeFn: fn (self: &Allocator, old_mem: []u8) void,
fn create(self: &Allocator, comptime T: type) !&T {
+ if (@sizeOf(T) == 0) return &{};
const slice = try self.alloc(T, 1);
return &slice[0];
}
diff --git a/std/os/index.zig b/std/os/index.zig
index 85e46a1bf9..6842fd0fb3 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -2347,18 +2347,30 @@ pub fn posixGetSockOptConnectError(sockfd: i32) PosixConnectError!void {
}
pub const Thread = struct {
- pid: pid_t,
- allocator: ?&mem.Allocator,
- stack: []u8,
- pthread_handle: pthread_t,
+ data: Data,
pub const use_pthreads = is_posix and builtin.link_libc;
- const pthread_t = if (use_pthreads) c.pthread_t else void;
- const pid_t = if (!use_pthreads) i32 else void;
+ const Data = if (use_pthreads) struct {
+ handle: c.pthread_t,
+ stack_addr: usize,
+ stack_len: usize,
+ } else switch (builtin.os) {
+ builtin.Os.linux => struct {
+ pid: i32,
+ stack_addr: usize,
+ stack_len: usize,
+ },
+ builtin.Os.windows => struct {
+ handle: windows.HANDLE,
+ alloc_start: &c_void,
+ heap_handle: windows.HANDLE,
+ },
+ else => @compileError("Unsupported OS"),
+ };
pub fn wait(self: &const Thread) void {
if (use_pthreads) {
- const err = c.pthread_join(self.pthread_handle, null);
+ const err = c.pthread_join(self.data.handle, null);
switch (err) {
0 => {},
posix.EINVAL => unreachable,
@@ -2366,23 +2378,27 @@ pub const Thread = struct {
posix.EDEADLK => unreachable,
else => unreachable,
}
- } else if (builtin.os == builtin.Os.linux) {
- while (true) {
- const pid_value = @atomicLoad(i32, &self.pid, builtin.AtomicOrder.SeqCst);
- if (pid_value == 0) break;
- const rc = linux.futex_wait(@ptrToInt(&self.pid), linux.FUTEX_WAIT, pid_value, null);
- switch (linux.getErrno(rc)) {
- 0 => continue,
- posix.EINTR => continue,
- posix.EAGAIN => continue,
- else => unreachable,
+ assert(posix.munmap(self.data.stack_addr, self.data.stack_len) == 0);
+ } else switch (builtin.os) {
+ builtin.Os.linux => {
+ while (true) {
+ const pid_value = @atomicLoad(i32, &self.data.pid, builtin.AtomicOrder.SeqCst);
+ if (pid_value == 0) break;
+ const rc = linux.futex_wait(@ptrToInt(&self.data.pid), linux.FUTEX_WAIT, pid_value, null);
+ switch (linux.getErrno(rc)) {
+ 0 => continue,
+ posix.EINTR => continue,
+ posix.EAGAIN => continue,
+ else => unreachable,
+ }
}
- }
- } else {
- @compileError("Unsupported OS");
- }
- if (self.allocator) |a| {
- a.free(self.stack);
+ assert(posix.munmap(self.data.stack_addr, self.data.stack_len) == 0);
+ },
+ builtin.Os.windows => {
+ assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0);
+ assert(windows.HeapFree(self.data.heap_handle, 0, self.data.alloc_start) != 0);
+ },
+ else => @compileError("Unsupported OS"),
}
}
};
@@ -2407,52 +2423,60 @@ pub const SpawnThreadError = error {
/// be copied.
SystemResources,
- /// pthreads requires at least 16384 bytes of stack space
- StackTooSmall,
+ /// Not enough userland memory to spawn the thread.
+ OutOfMemory,
Unexpected,
};
-pub const SpawnThreadAllocatorError = SpawnThreadError || error{OutOfMemory};
-
/// caller must call wait on the returned thread
/// fn startFn(@typeOf(context)) T
/// where T is u8, noreturn, void, or !void
-pub fn spawnThreadAllocator(allocator: &mem.Allocator, context: var, comptime startFn: var) SpawnThreadAllocatorError!&Thread {
+/// caller must call wait on the returned thread
+pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread {
// TODO compile-time call graph analysis to determine stack upper bound
// https://github.com/zig-lang/zig/issues/157
const default_stack_size = 8 * 1024 * 1024;
- const stack_bytes = try allocator.alignedAlloc(u8, os.page_size, default_stack_size);
- const thread = try spawnThread(stack_bytes, context, startFn);
- thread.allocator = allocator;
- return thread;
-}
-/// stack must be big enough to store one Thread and one @typeOf(context), each with default alignment, at the end
-/// fn startFn(@typeOf(context)) T
-/// where T is u8, noreturn, void, or !void
-/// caller must call wait on the returned thread
-pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime startFn: var) SpawnThreadError!&Thread {
const Context = @typeOf(context);
comptime assert(@ArgType(@typeOf(startFn), 0) == Context);
- var stack_end: usize = @ptrToInt(stack.ptr) + stack.len;
- var arg: usize = undefined;
- if (@sizeOf(Context) != 0) {
- stack_end -= @sizeOf(Context);
- stack_end -= stack_end % @alignOf(Context);
- assert(stack_end >= @ptrToInt(stack.ptr));
- const context_ptr = @alignCast(@alignOf(Context), @intToPtr(&Context, stack_end));
- *context_ptr = context;
- arg = stack_end;
- }
+ if (builtin.os == builtin.Os.windows) {
+ const WinThread = struct {
+ const OuterContext = struct {
+ thread: Thread,
+ inner: Context,
+ };
+ extern fn threadMain(arg: windows.LPVOID) windows.DWORD {
+ if (@sizeOf(Context) == 0) {
+ return startFn({});
+ } else {
+ return startFn(*@ptrCast(&Context, @alignCast(@alignOf(Context), arg)));
+ }
+ }
+ };
- stack_end -= @sizeOf(Thread);
- stack_end -= stack_end % @alignOf(Thread);
- assert(stack_end >= @ptrToInt(stack.ptr));
- const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(&Thread, stack_end));
- thread_ptr.stack = stack;
- thread_ptr.allocator = null;
+ const heap_handle = windows.GetProcessHeap() ?? return SpawnThreadError.OutOfMemory;
+ const byte_count = @alignOf(WinThread.OuterContext) + @sizeOf(WinThread.OuterContext);
+ const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) ?? return SpawnThreadError.OutOfMemory;
+ errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
+ const bytes = @ptrCast(&u8, bytes_ptr)[0..byte_count];
+ const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable;
+ outer_context.inner = context;
+ outer_context.thread.data.heap_handle = heap_handle;
+ outer_context.thread.data.alloc_start = bytes_ptr;
+
+ const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(&c_void, &outer_context.inner);
+ outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain,
+ parameter, 0, null) ??
+ {
+ const err = windows.GetLastError();
+ return switch (err) {
+ else => os.unexpectedErrorWindows(err),
+ };
+ };
+ return &outer_context.thread;
+ }
const MainFuncs = struct {
extern fn linuxThreadMain(ctx_addr: usize) u8 {
@@ -2473,6 +2497,29 @@ pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime start
}
};
+ const stack_len = default_stack_size;
+ const stack_addr = posix.mmap(null, stack_len, posix.PROT_READ|posix.PROT_WRITE,
+ posix.MAP_PRIVATE|posix.MAP_ANONYMOUS|posix.MAP_GROWSDOWN, -1, 0);
+ if (stack_addr == posix.MAP_FAILED) return error.OutOfMemory;
+ errdefer _ = posix.munmap(stack_addr, stack_len);
+
+ var stack_end: usize = stack_addr + stack_len;
+ var arg: usize = undefined;
+ if (@sizeOf(Context) != 0) {
+ stack_end -= @sizeOf(Context);
+ stack_end -= stack_end % @alignOf(Context);
+ assert(stack_end >= stack_addr);
+ const context_ptr = @alignCast(@alignOf(Context), @intToPtr(&Context, stack_end));
+ *context_ptr = context;
+ arg = stack_end;
+ }
+
+ stack_end -= @sizeOf(Thread);
+ stack_end -= stack_end % @alignOf(Thread);
+ assert(stack_end >= stack_addr);
+ const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(&Thread, stack_end));
+
+
if (builtin.os == builtin.Os.windows) {
// use windows API directly
@compileError("TODO support spawnThread for Windows");
@@ -2484,14 +2531,12 @@ pub fn spawnThread(stack: []align(os.page_size) u8, context: var, comptime start
// align to page
stack_end -= stack_end % os.page_size;
+ assert(c.pthread_attr_setstack(&attr, @intToPtr(&c_void, stack_addr), stack_len) == 0);
- const stack_size = stack_end - @ptrToInt(stack.ptr);
- const setstack_err = c.pthread_attr_setstack(&attr, @ptrCast(&c_void, stack.ptr), stack_size);
- if (setstack_err != 0) {
- return SpawnThreadError.StackTooSmall; // pthreads requires at least 16384 bytes
- }
+ thread_ptr.data.stack_addr = stack_addr;
+ thread_ptr.data.stack_len = stack_len;
- const err = c.pthread_create(&thread_ptr.pthread_handle, &attr, MainFuncs.posixThreadMain, @intToPtr(&c_void, arg));
+ const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(&c_void, arg));
switch (err) {
0 => return thread_ptr,
posix.EAGAIN => return SpawnThreadError.SystemResources,
diff --git a/std/os/test.zig b/std/os/test.zig
index 37e5bf4bb8..56d6e8b309 100644
--- a/std/os/test.zig
+++ b/std/os/test.zig
@@ -44,24 +44,12 @@ test "access file" {
}
test "spawn threads" {
- if (builtin.os == builtin.Os.windows) {
- // TODO implement threads on windows
- return;
- }
-
- var direct_allocator = std.heap.DirectAllocator.init();
- defer direct_allocator.deinit();
-
var shared_ctx: i32 = 1;
- const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1);
- const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2);
-
- var stack1: [20 * 1024]u8 align(os.page_size) = undefined;
- var stack2: [20 * 1024]u8 align(os.page_size) = undefined;
-
- const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2);
- const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2);
+ const thread1 = try std.os.spawnThread({}, start1);
+ const thread2 = try std.os.spawnThread(&shared_ctx, start2);
+ const thread3 = try std.os.spawnThread(&shared_ctx, start2);
+ const thread4 = try std.os.spawnThread(&shared_ctx, start2);
thread1.wait();
thread2.wait();
diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig
index d6ef7205e8..e13ed0f131 100644
--- a/std/os/windows/index.zig
+++ b/std/os/windows/index.zig
@@ -28,6 +28,9 @@ pub extern "kernel32" stdcallcc fn CreateProcessA(lpApplicationName: ?LPCSTR, lp
pub extern "kernel32" stdcallcc fn CreateSymbolicLinkA(lpSymlinkFileName: LPCSTR, lpTargetFileName: LPCSTR,
dwFlags: DWORD) BOOLEAN;
+
+pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE;
+
pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) BOOL;
pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) noreturn;
@@ -318,6 +321,9 @@ pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000;
pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004;
pub const HEAP_NO_SERIALIZE = 0x00000001;
+pub const PTHREAD_START_ROUTINE = extern fn(LPVOID) DWORD;
+pub const LPTHREAD_START_ROUTINE = PTHREAD_START_ROUTINE;
+
test "import" {
_ = @import("util.zig");
}
--
cgit v1.2.3
From c186cd187e9b75ee1514a5f3371abeffb36d871a Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 2 May 2018 20:19:26 -0400
Subject: std.atomic - use AtomicOrder.SeqCst for everything
also use less memory for the tests
---
std/atomic/queue.zig | 6 +++---
std/atomic/stack.zig | 4 ++--
2 files changed, 5 insertions(+), 5 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 1acecbab2c..69c82236d1 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -31,10 +31,10 @@ pub fn Queue(comptime T: type) type {
}
pub fn get(self: &Self) ?&Node {
- var head = @atomicLoad(&Node, &self.head, AtomicOrder.Acquire);
+ var head = @atomicLoad(&Node, &self.head, AtomicOrder.SeqCst);
while (true) {
const node = head.next ?? return null;
- head = @cmpxchgWeak(&Node, &self.head, head, node, AtomicOrder.Release, AtomicOrder.Acquire) ?? return node;
+ head = @cmpxchgWeak(&Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return node;
}
}
};
@@ -56,7 +56,7 @@ test "std.atomic.queue" {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 600 * 1024);
defer direct_allocator.allocator.free(plenty_of_memory);
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index accbcc942a..96185d308b 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -35,7 +35,7 @@ pub fn Stack(comptime T: type) type {
}
pub fn pop(self: &Self) ?&Node {
- var root = @atomicLoad(?&Node, &self.root, AtomicOrder.Acquire);
+ var root = @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst);
while (true) {
root = @cmpxchgWeak(?&Node, &self.root, root, (root ?? return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return root;
}
@@ -63,7 +63,7 @@ test "std.atomic.stack" {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 600 * 1024);
defer direct_allocator.allocator.free(plenty_of_memory);
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
--
cgit v1.2.3
From 02c1b9df3bcf2e0324adf02fd6c0ed56fe58c6d3 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 2 May 2018 21:34:34 -0400
Subject: fix compiler-rt tests accidentally running std tests
also reduce the aggressiveness of std.atomic.stack
and std.atomic.queue fuzz testing. appveyor has 1 core
and 10,000 iterations is too much for 6 threads to
thrash over
---
std/atomic/queue.zig | 10 +++++--
std/atomic/stack.zig | 9 ++++--
std/special/compiler_rt/fixuint.zig | 2 +-
std/special/compiler_rt/fixunsdfdi_test.zig | 2 +-
std/special/compiler_rt/fixunsdfsi_test.zig | 2 +-
std/special/compiler_rt/fixunsdfti_test.zig | 2 +-
std/special/compiler_rt/fixunssfdi_test.zig | 2 +-
std/special/compiler_rt/fixunssfsi_test.zig | 2 +-
std/special/compiler_rt/fixunssfti_test.zig | 2 +-
std/special/compiler_rt/fixunstfdi_test.zig | 2 +-
std/special/compiler_rt/fixunstfsi_test.zig | 2 +-
std/special/compiler_rt/fixunstfti_test.zig | 2 +-
std/special/compiler_rt/index.zig | 5 ++--
std/special/compiler_rt/udivmod.zig | 2 +-
std/zig/parser_test.zig | 46 +++++++++++++++++------------
15 files changed, 56 insertions(+), 36 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 69c82236d1..e25c8e6b17 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -49,14 +49,20 @@ const Context = struct {
get_count: usize,
puts_done: u8, // TODO make this a bool
};
-const puts_per_thread = 10000;
+
+// TODO add lazy evaluated build options and then put puts_per_thread behind
+// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
+// CI we would use a less aggressive setting since at 1 core, while we still
+// want this test to pass, we need a smaller value since there is so much thrashing
+// we would also use a less aggressive setting when running in valgrind
+const puts_per_thread = 500;
const put_thread_count = 3;
test "std.atomic.queue" {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 600 * 1024);
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
defer direct_allocator.allocator.free(plenty_of_memory);
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 96185d308b..4a3dbef32b 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -56,14 +56,19 @@ const Context = struct {
get_count: usize,
puts_done: u8, // TODO make this a bool
};
-const puts_per_thread = 1000;
+// TODO add lazy evaluated build options and then put puts_per_thread behind
+// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
+// CI we would use a less aggressive setting since at 1 core, while we still
+// want this test to pass, we need a smaller value since there is so much thrashing
+// we would also use a less aggressive setting when running in valgrind
+const puts_per_thread = 500;
const put_thread_count = 3;
test "std.atomic.stack" {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 600 * 1024);
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
defer direct_allocator.allocator.free(plenty_of_memory);
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
diff --git a/std/special/compiler_rt/fixuint.zig b/std/special/compiler_rt/fixuint.zig
index b01bc48118..37cec446bc 100644
--- a/std/special/compiler_rt/fixuint.zig
+++ b/std/special/compiler_rt/fixuint.zig
@@ -1,5 +1,5 @@
const is_test = @import("builtin").is_test;
-const Log2Int = @import("../../math/index.zig").Log2Int;
+const Log2Int = @import("std").math.Log2Int;
pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t {
@setRuntimeSafety(is_test);
diff --git a/std/special/compiler_rt/fixunsdfdi_test.zig b/std/special/compiler_rt/fixunsdfdi_test.zig
index 3443a4938e..e59d09f8de 100644
--- a/std/special/compiler_rt/fixunsdfdi_test.zig
+++ b/std/special/compiler_rt/fixunsdfdi_test.zig
@@ -1,5 +1,5 @@
const __fixunsdfdi = @import("fixunsdfdi.zig").__fixunsdfdi;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunsdfdi(a: f64, expected: u64) void {
const x = __fixunsdfdi(a);
diff --git a/std/special/compiler_rt/fixunsdfsi_test.zig b/std/special/compiler_rt/fixunsdfsi_test.zig
index 3c74bc5f4c..db6e32e23d 100644
--- a/std/special/compiler_rt/fixunsdfsi_test.zig
+++ b/std/special/compiler_rt/fixunsdfsi_test.zig
@@ -1,5 +1,5 @@
const __fixunsdfsi = @import("fixunsdfsi.zig").__fixunsdfsi;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunsdfsi(a: f64, expected: u32) void {
const x = __fixunsdfsi(a);
diff --git a/std/special/compiler_rt/fixunsdfti_test.zig b/std/special/compiler_rt/fixunsdfti_test.zig
index 3cb7687887..7283b35c0e 100644
--- a/std/special/compiler_rt/fixunsdfti_test.zig
+++ b/std/special/compiler_rt/fixunsdfti_test.zig
@@ -1,5 +1,5 @@
const __fixunsdfti = @import("fixunsdfti.zig").__fixunsdfti;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunsdfti(a: f64, expected: u128) void {
const x = __fixunsdfti(a);
diff --git a/std/special/compiler_rt/fixunssfdi_test.zig b/std/special/compiler_rt/fixunssfdi_test.zig
index de27323777..e4e6c1736d 100644
--- a/std/special/compiler_rt/fixunssfdi_test.zig
+++ b/std/special/compiler_rt/fixunssfdi_test.zig
@@ -1,5 +1,5 @@
const __fixunssfdi = @import("fixunssfdi.zig").__fixunssfdi;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunssfdi(a: f32, expected: u64) void {
const x = __fixunssfdi(a);
diff --git a/std/special/compiler_rt/fixunssfsi_test.zig b/std/special/compiler_rt/fixunssfsi_test.zig
index 47ed21d4f4..614c648dfe 100644
--- a/std/special/compiler_rt/fixunssfsi_test.zig
+++ b/std/special/compiler_rt/fixunssfsi_test.zig
@@ -1,5 +1,5 @@
const __fixunssfsi = @import("fixunssfsi.zig").__fixunssfsi;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunssfsi(a: f32, expected: u32) void {
const x = __fixunssfsi(a);
diff --git a/std/special/compiler_rt/fixunssfti_test.zig b/std/special/compiler_rt/fixunssfti_test.zig
index 3033eb0def..43ad527f53 100644
--- a/std/special/compiler_rt/fixunssfti_test.zig
+++ b/std/special/compiler_rt/fixunssfti_test.zig
@@ -1,5 +1,5 @@
const __fixunssfti = @import("fixunssfti.zig").__fixunssfti;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunssfti(a: f32, expected: u128) void {
const x = __fixunssfti(a);
diff --git a/std/special/compiler_rt/fixunstfdi_test.zig b/std/special/compiler_rt/fixunstfdi_test.zig
index d1f5f6496a..dd0869195a 100644
--- a/std/special/compiler_rt/fixunstfdi_test.zig
+++ b/std/special/compiler_rt/fixunstfdi_test.zig
@@ -1,5 +1,5 @@
const __fixunstfdi = @import("fixunstfdi.zig").__fixunstfdi;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunstfdi(a: f128, expected: u64) void {
const x = __fixunstfdi(a);
diff --git a/std/special/compiler_rt/fixunstfsi_test.zig b/std/special/compiler_rt/fixunstfsi_test.zig
index 8bdf36d9d4..f682191994 100644
--- a/std/special/compiler_rt/fixunstfsi_test.zig
+++ b/std/special/compiler_rt/fixunstfsi_test.zig
@@ -1,5 +1,5 @@
const __fixunstfsi = @import("fixunstfsi.zig").__fixunstfsi;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunstfsi(a: f128, expected: u32) void {
const x = __fixunstfsi(a);
diff --git a/std/special/compiler_rt/fixunstfti_test.zig b/std/special/compiler_rt/fixunstfti_test.zig
index d9eb60e59b..9128ac6c08 100644
--- a/std/special/compiler_rt/fixunstfti_test.zig
+++ b/std/special/compiler_rt/fixunstfti_test.zig
@@ -1,5 +1,5 @@
const __fixunstfti = @import("fixunstfti.zig").__fixunstfti;
-const assert = @import("../../index.zig").debug.assert;
+const assert = @import("std").debug.assert;
fn test__fixunstfti(a: f128, expected: u128) void {
const x = __fixunstfti(a);
diff --git a/std/special/compiler_rt/index.zig b/std/special/compiler_rt/index.zig
index 6ef43c4fed..9da9c3f083 100644
--- a/std/special/compiler_rt/index.zig
+++ b/std/special/compiler_rt/index.zig
@@ -71,7 +71,8 @@ comptime {
}
}
-const assert = @import("../../index.zig").debug.assert;
+const std = @import("std");
+const assert = std.debug.assert;
const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
@@ -80,7 +81,7 @@ const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn {
@setCold(true);
if (is_test) {
- @import("std").debug.panic("{}", msg);
+ std.debug.panic("{}", msg);
} else {
unreachable;
}
diff --git a/std/special/compiler_rt/udivmod.zig b/std/special/compiler_rt/udivmod.zig
index 07eaef583c..7820c7beb0 100644
--- a/std/special/compiler_rt/udivmod.zig
+++ b/std/special/compiler_rt/udivmod.zig
@@ -9,7 +9,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
const SingleInt = @IntType(false, @divExact(DoubleInt.bit_count, 2));
const SignedDoubleInt = @IntType(true, DoubleInt.bit_count);
- const Log2SingleInt = @import("../../math/index.zig").Log2Int(SingleInt);
+ const Log2SingleInt = @import("std").math.Log2Int(SingleInt);
const n = *@ptrCast(&const [2]SingleInt, &a); // TODO issue #421
const d = *@ptrCast(&const [2]SingleInt, &b); // TODO issue #421
diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig
index 4688939485..beb3a4e0c3 100644
--- a/std/zig/parser_test.zig
+++ b/std/zig/parser_test.zig
@@ -1,3 +1,30 @@
+// TODO
+//if (sr > n_uword_bits - 1) // d > r
+// return 0;
+
+// TODO switch with no body
+// format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};
+
+
+//TODO
+//test "zig fmt: same-line comptime" {
+// try testCanonical(
+// \\test "" {
+// \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
+// \\}
+// \\
+// );
+//}
+
+
+//TODO
+//test "zig fmt: number literals" {
+// try testCanonical(
+// \\pub const f64_true_min = 4.94065645841246544177e-324;
+// \\
+// );
+//}
+
test "zig fmt: line comments in struct initializer" {
try testCanonical(
\\fn foo() void {
@@ -20,25 +47,6 @@ test "zig fmt: line comments in struct initializer" {
);
}
-//TODO
-//test "zig fmt: same-line comptime" {
-// try testCanonical(
-// \\test "" {
-// \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
-// \\}
-// \\
-// );
-//}
-
-
-//TODO
-//test "zig fmt: number literals" {
-// try testCanonical(
-// \\pub const f64_true_min = 4.94065645841246544177e-324;
-// \\
-// );
-//}
-
test "zig fmt: doc comments before struct field" {
try testCanonical(
\\pub const Allocator = struct {
--
cgit v1.2.3
From 4787127cf6418f7a819c9d6f07a9046d76e0de65 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Thu, 10 May 2018 00:29:49 -0400
Subject: partial conversion to post-fix pointer deref using zig fmt
---
std/atomic/queue.zig | 12 +-
std/atomic/stack.zig | 16 +-
std/buffer.zig | 11 +-
std/build.zig | 207 ++++++--------
std/crypto/blake2.zig | 711 +++++++++++++++++++++++++++++++----------------
std/crypto/hmac.zig | 4 +-
std/crypto/sha3.zig | 281 ++++++++++++-------
std/event.zig | 53 ++--
std/hash/crc.zig | 32 +--
std/hash_map.zig | 102 ++++---
std/json.zig | 207 ++++++++------
std/math/acos.zig | 14 +-
std/math/asin.zig | 18 +-
std/math/atan2.zig | 66 ++---
std/math/cbrt.zig | 10 +-
std/math/ceil.zig | 4 +-
std/math/cos.zig | 12 +-
std/math/floor.zig | 4 +-
std/math/fma.zig | 7 +-
std/math/hypot.zig | 8 +-
std/math/ln.zig | 6 +-
std/math/log10.zig | 18 +-
std/math/log2.zig | 7 +-
std/math/round.zig | 8 +-
std/math/sin.zig | 12 +-
std/math/tan.zig | 6 +-
std/net.zig | 40 ++-
std/os/child_process.zig | 192 +++++++------
std/segmented_list.zig | 54 ++--
std/sort.zig | 562 ++++++++++++++++++++++++++-----------
30 files changed, 1620 insertions(+), 1064 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index e25c8e6b17..288a2b3b48 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -70,7 +70,7 @@ test "std.atomic.queue" {
var queue: Queue(i32) = undefined;
queue.init();
- var context = Context {
+ var context = Context{
.allocator = a,
.queue = &queue,
.put_sum = 0,
@@ -81,16 +81,18 @@ test "std.atomic.queue" {
var putters: [put_thread_count]&std.os.Thread = undefined;
for (putters) |*t| {
- *t = try std.os.spawnThread(&context, startPuts);
+ t.* = try std.os.spawnThread(&context, startPuts);
}
var getters: [put_thread_count]&std.os.Thread = undefined;
for (getters) |*t| {
- *t = try std.os.spawnThread(&context, startGets);
+ t.* = try std.os.spawnThread(&context, startGets);
}
- for (putters) |t| t.wait();
+ for (putters) |t|
+ t.wait();
_ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
- for (getters) |t| t.wait();
+ for (getters) |t|
+ t.wait();
std.debug.assert(context.put_sum == context.get_sum);
std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 4a3dbef32b..400a1a3c4f 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -14,9 +14,7 @@ pub fn Stack(comptime T: type) type {
};
pub fn init() Self {
- return Self {
- .root = null,
- };
+ return Self{ .root = null };
}
/// push operation, but only if you are the first item in the stack. if you did not succeed in
@@ -75,7 +73,7 @@ test "std.atomic.stack" {
var a = &fixed_buffer_allocator.allocator;
var stack = Stack(i32).init();
- var context = Context {
+ var context = Context{
.allocator = a,
.stack = &stack,
.put_sum = 0,
@@ -86,16 +84,18 @@ test "std.atomic.stack" {
var putters: [put_thread_count]&std.os.Thread = undefined;
for (putters) |*t| {
- *t = try std.os.spawnThread(&context, startPuts);
+ t.* = try std.os.spawnThread(&context, startPuts);
}
var getters: [put_thread_count]&std.os.Thread = undefined;
for (getters) |*t| {
- *t = try std.os.spawnThread(&context, startGets);
+ t.* = try std.os.spawnThread(&context, startGets);
}
- for (putters) |t| t.wait();
+ for (putters) |t|
+ t.wait();
_ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
- for (getters) |t| t.wait();
+ for (getters) |t|
+ t.wait();
std.debug.assert(context.put_sum == context.get_sum);
std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
diff --git a/std/buffer.zig b/std/buffer.zig
index 041d891dec..cf530c3c9e 100644
--- a/std/buffer.zig
+++ b/std/buffer.zig
@@ -31,9 +31,7 @@ pub const Buffer = struct {
/// * ::replaceContentsBuffer
/// * ::resize
pub fn initNull(allocator: &Allocator) Buffer {
- return Buffer {
- .list = ArrayList(u8).init(allocator),
- };
+ return Buffer{ .list = ArrayList(u8).init(allocator) };
}
/// Must deinitialize with deinit.
@@ -45,9 +43,7 @@ pub const Buffer = struct {
/// allocated with `allocator`.
/// Must deinitialize with deinit.
pub fn fromOwnedSlice(allocator: &Allocator, slice: []u8) Buffer {
- var self = Buffer {
- .list = ArrayList(u8).fromOwnedSlice(allocator, slice),
- };
+ var self = Buffer{ .list = ArrayList(u8).fromOwnedSlice(allocator, slice) };
self.list.append(0);
return self;
}
@@ -57,11 +53,10 @@ pub const Buffer = struct {
pub fn toOwnedSlice(self: &Buffer) []u8 {
const allocator = self.list.allocator;
const result = allocator.shrink(u8, self.list.items, self.len());
- *self = initNull(allocator);
+ self.* = initNull(allocator);
return result;
}
-
pub fn deinit(self: &Buffer) void {
self.list.deinit();
}
diff --git a/std/build.zig b/std/build.zig
index a312b28a6f..276176c63c 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -82,10 +82,8 @@ pub const Builder = struct {
description: []const u8,
};
- pub fn init(allocator: &Allocator, zig_exe: []const u8, build_root: []const u8,
- cache_root: []const u8) Builder
- {
- var self = Builder {
+ pub fn init(allocator: &Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
+ var self = Builder{
.zig_exe = zig_exe,
.build_root = build_root,
.cache_root = os.path.relative(allocator, build_root, cache_root) catch unreachable,
@@ -112,12 +110,12 @@ pub const Builder = struct {
.lib_dir = undefined,
.exe_dir = undefined,
.installed_files = ArrayList([]const u8).init(allocator),
- .uninstall_tls = TopLevelStep {
+ .uninstall_tls = TopLevelStep{
.step = Step.init("uninstall", allocator, makeUninstall),
.description = "Remove build artifacts from prefix path",
},
.have_uninstall_step = false,
- .install_tls = TopLevelStep {
+ .install_tls = TopLevelStep{
.step = Step.initNoOp("install", allocator),
.description = "Copy build artifacts to prefix path",
},
@@ -151,9 +149,7 @@ pub const Builder = struct {
return LibExeObjStep.createObject(self, name, root_src);
}
- pub fn addSharedLibrary(self: &Builder, name: []const u8, root_src: ?[]const u8,
- ver: &const Version) &LibExeObjStep
- {
+ pub fn addSharedLibrary(self: &Builder, name: []const u8, root_src: ?[]const u8, ver: &const Version) &LibExeObjStep {
return LibExeObjStep.createSharedLibrary(self, name, root_src, ver);
}
@@ -163,7 +159,7 @@ pub const Builder = struct {
pub fn addTest(self: &Builder, root_src: []const u8) &TestStep {
const test_step = self.allocator.create(TestStep) catch unreachable;
- *test_step = TestStep.init(self, root_src);
+ test_step.* = TestStep.init(self, root_src);
return test_step;
}
@@ -190,33 +186,31 @@ pub const Builder = struct {
}
/// ::argv is copied.
- pub fn addCommand(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
- argv: []const []const u8) &CommandStep
- {
+ pub fn addCommand(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) &CommandStep {
return CommandStep.create(self, cwd, env_map, argv);
}
pub fn addWriteFile(self: &Builder, file_path: []const u8, data: []const u8) &WriteFileStep {
const write_file_step = self.allocator.create(WriteFileStep) catch unreachable;
- *write_file_step = WriteFileStep.init(self, file_path, data);
+ write_file_step.* = WriteFileStep.init(self, file_path, data);
return write_file_step;
}
pub fn addLog(self: &Builder, comptime format: []const u8, args: ...) &LogStep {
const data = self.fmt(format, args);
const log_step = self.allocator.create(LogStep) catch unreachable;
- *log_step = LogStep.init(self, data);
+ log_step.* = LogStep.init(self, data);
return log_step;
}
pub fn addRemoveDirTree(self: &Builder, dir_path: []const u8) &RemoveDirStep {
const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable;
- *remove_dir_step = RemoveDirStep.init(self, dir_path);
+ remove_dir_step.* = RemoveDirStep.init(self, dir_path);
return remove_dir_step;
}
pub fn version(self: &const Builder, major: u32, minor: u32, patch: u32) Version {
- return Version {
+ return Version{
.major = major,
.minor = minor,
.patch = patch,
@@ -254,8 +248,7 @@ pub const Builder = struct {
}
pub fn getInstallStep(self: &Builder) &Step {
- if (self.have_install_step)
- return &self.install_tls.step;
+ if (self.have_install_step) return &self.install_tls.step;
self.top_level_steps.append(&self.install_tls) catch unreachable;
self.have_install_step = true;
@@ -263,8 +256,7 @@ pub const Builder = struct {
}
pub fn getUninstallStep(self: &Builder) &Step {
- if (self.have_uninstall_step)
- return &self.uninstall_tls.step;
+ if (self.have_uninstall_step) return &self.uninstall_tls.step;
self.top_level_steps.append(&self.uninstall_tls) catch unreachable;
self.have_uninstall_step = true;
@@ -360,7 +352,7 @@ pub const Builder = struct {
pub fn option(self: &Builder, comptime T: type, name: []const u8, description: []const u8) ?T {
const type_id = comptime typeToEnum(T);
- const available_option = AvailableOption {
+ const available_option = AvailableOption{
.name = name,
.type_id = type_id,
.description = description,
@@ -413,7 +405,7 @@ pub const Builder = struct {
pub fn step(self: &Builder, name: []const u8, description: []const u8) &Step {
const step_info = self.allocator.create(TopLevelStep) catch unreachable;
- *step_info = TopLevelStep {
+ step_info.* = TopLevelStep{
.step = Step.initNoOp(name, self.allocator),
.description = description,
};
@@ -446,9 +438,9 @@ pub const Builder = struct {
}
pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) bool {
- if (self.user_input_options.put(name, UserInputOption {
+ if (self.user_input_options.put(name, UserInputOption{
.name = name,
- .value = UserValue { .Scalar = value },
+ .value = UserValue{ .Scalar = value },
.used = false,
}) catch unreachable) |*prev_value| {
// option already exists
@@ -458,18 +450,18 @@ pub const Builder = struct {
var list = ArrayList([]const u8).init(self.allocator);
list.append(s) catch unreachable;
list.append(value) catch unreachable;
- _ = self.user_input_options.put(name, UserInputOption {
+ _ = self.user_input_options.put(name, UserInputOption{
.name = name,
- .value = UserValue { .List = list },
+ .value = UserValue{ .List = list },
.used = false,
}) catch unreachable;
},
UserValue.List => |*list| {
// append to the list
list.append(value) catch unreachable;
- _ = self.user_input_options.put(name, UserInputOption {
+ _ = self.user_input_options.put(name, UserInputOption{
.name = name,
- .value = UserValue { .List = *list },
+ .value = UserValue{ .List = list.* },
.used = false,
}) catch unreachable;
},
@@ -483,9 +475,9 @@ pub const Builder = struct {
}
pub fn addUserInputFlag(self: &Builder, name: []const u8) bool {
- if (self.user_input_options.put(name, UserInputOption {
+ if (self.user_input_options.put(name, UserInputOption{
.name = name,
- .value = UserValue {.Flag = {} },
+ .value = UserValue{ .Flag = {} },
.used = false,
}) catch unreachable) |*prev_value| {
switch (prev_value.value) {
@@ -556,9 +548,7 @@ pub const Builder = struct {
warn("\n");
}
- fn spawnChildEnvMap(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
- argv: []const []const u8) !void
- {
+ fn spawnChildEnvMap(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) !void {
if (self.verbose) {
printCmd(cwd, argv);
}
@@ -617,7 +607,7 @@ pub const Builder = struct {
self.pushInstalledFile(full_dest_path);
const install_step = self.allocator.create(InstallFileStep) catch unreachable;
- *install_step = InstallFileStep.init(self, src_path, full_dest_path);
+ install_step.* = InstallFileStep.init(self, src_path, full_dest_path);
return install_step;
}
@@ -659,25 +649,23 @@ pub const Builder = struct {
if (builtin.environ == builtin.Environ.msvc) {
return "cl.exe";
} else {
- return os.getEnvVarOwned(self.allocator, "CC") catch |err|
+ return os.getEnvVarOwned(self.allocator, "CC") catch |err|
if (err == error.EnvironmentVariableNotFound)
([]const u8)("cc")
else
- debug.panic("Unable to get environment variable: {}", err)
- ;
+ debug.panic("Unable to get environment variable: {}", err);
}
}
pub fn findProgram(self: &Builder, names: []const []const u8, paths: []const []const u8) ![]const u8 {
// TODO report error for ambiguous situations
- const exe_extension = (Target { .Native = {}}).exeFileExt();
+ const exe_extension = (Target{ .Native = {} }).exeFileExt();
for (self.search_prefixes.toSliceConst()) |search_prefix| {
for (names) |name| {
if (os.path.isAbsolute(name)) {
return name;
}
- const full_path = try os.path.join(self.allocator, search_prefix, "bin",
- self.fmt("{}{}", name, exe_extension));
+ const full_path = try os.path.join(self.allocator, search_prefix, "bin", self.fmt("{}{}", name, exe_extension));
if (os.path.real(self.allocator, full_path)) |real_path| {
return real_path;
} else |_| {
@@ -761,7 +749,7 @@ pub const Target = union(enum) {
Cross: CrossTarget,
pub fn oFileExt(self: &const Target) []const u8 {
- const environ = switch (*self) {
+ const environ = switch (self.*) {
Target.Native => builtin.environ,
Target.Cross => |t| t.environ,
};
@@ -786,7 +774,7 @@ pub const Target = union(enum) {
}
pub fn getOs(self: &const Target) builtin.Os {
- return switch (*self) {
+ return switch (self.*) {
Target.Native => builtin.os,
Target.Cross => |t| t.os,
};
@@ -794,7 +782,8 @@ pub const Target = union(enum) {
pub fn isDarwin(self: &const Target) bool {
return switch (self.getOs()) {
- builtin.Os.ios, builtin.Os.macosx => true,
+ builtin.Os.ios,
+ builtin.Os.macosx => true,
else => false,
};
}
@@ -860,61 +849,57 @@ pub const LibExeObjStep = struct {
Obj,
};
- pub fn createSharedLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8,
- ver: &const Version) &LibExeObjStep
- {
+ pub fn createSharedLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8, ver: &const Version) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
+ self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
return self;
}
pub fn createCSharedLibrary(builder: &Builder, name: []const u8, version: &const Version) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initC(builder, name, Kind.Lib, version, false);
+ self.* = initC(builder, name, Kind.Lib, version, false);
return self;
}
pub fn createStaticLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
+ self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
return self;
}
pub fn createCStaticLibrary(builder: &Builder, name: []const u8) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
+ self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
return self;
}
pub fn createObject(builder: &Builder, name: []const u8, root_src: []const u8) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
+ self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
return self;
}
pub fn createCObject(builder: &Builder, name: []const u8, src: []const u8) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
+ self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
self.object_src = src;
return self;
}
pub fn createExecutable(builder: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
+ self.* = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
return self;
}
pub fn createCExecutable(builder: &Builder, name: []const u8) &LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- *self = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
+ self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
return self;
}
- fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind,
- static: bool, ver: &const Version) LibExeObjStep
- {
- var self = LibExeObjStep {
+ fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: &const Version) LibExeObjStep {
+ var self = LibExeObjStep{
.strip = false,
.builder = builder,
.verbose_link = false,
@@ -930,7 +915,7 @@ pub const LibExeObjStep = struct {
.step = Step.init(name, builder.allocator, make),
.output_path = null,
.output_h_path = null,
- .version = *ver,
+ .version = ver.*,
.out_filename = undefined,
.out_h_filename = builder.fmt("{}.h", name),
.major_only_filename = undefined,
@@ -953,11 +938,11 @@ pub const LibExeObjStep = struct {
}
fn initC(builder: &Builder, name: []const u8, kind: Kind, version: &const Version, static: bool) LibExeObjStep {
- var self = LibExeObjStep {
+ var self = LibExeObjStep{
.builder = builder,
.name = name,
.kind = kind,
- .version = *version,
+ .version = version.*,
.static = static,
.target = Target.Native,
.cflags = ArrayList([]const u8).init(builder.allocator),
@@ -1005,9 +990,9 @@ pub const LibExeObjStep = struct {
self.out_filename = self.builder.fmt("lib{}.a", self.name);
} else {
switch (self.target.getOs()) {
- builtin.Os.ios, builtin.Os.macosx => {
- self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib",
- self.name, self.version.major, self.version.minor, self.version.patch);
+ builtin.Os.ios,
+ builtin.Os.macosx => {
+ self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch);
self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major);
self.name_only_filename = self.builder.fmt("lib{}.dylib", self.name);
},
@@ -1015,8 +1000,7 @@ pub const LibExeObjStep = struct {
self.out_filename = self.builder.fmt("{}.dll", self.name);
},
else => {
- self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}",
- self.name, self.version.major, self.version.minor, self.version.patch);
+ self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}", self.name, self.version.major, self.version.minor, self.version.patch);
self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major);
self.name_only_filename = self.builder.fmt("lib{}.so", self.name);
},
@@ -1026,16 +1010,12 @@ pub const LibExeObjStep = struct {
}
}
- pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os,
- target_environ: builtin.Environ) void
- {
- self.target = Target {
- .Cross = CrossTarget {
- .arch = target_arch,
- .os = target_os,
- .environ = target_environ,
- }
- };
+ pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
+ self.target = Target{ .Cross = CrossTarget{
+ .arch = target_arch,
+ .os = target_os,
+ .environ = target_environ,
+ } };
self.computeOutFileNames();
}
@@ -1159,7 +1139,7 @@ pub const LibExeObjStep = struct {
pub fn addPackagePath(self: &LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
assert(self.is_zig);
- self.packages.append(Pkg {
+ self.packages.append(Pkg{
.name = name,
.path = pkg_index_path,
}) catch unreachable;
@@ -1343,8 +1323,7 @@ pub const LibExeObjStep = struct {
try builder.spawnChild(zig_args.toSliceConst());
if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) {
- try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename,
- self.name_only_filename);
+ try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, self.name_only_filename);
}
}
@@ -1373,7 +1352,8 @@ pub const LibExeObjStep = struct {
args.append("ssp-buffer-size=4") catch unreachable;
}
},
- builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => {
+ builtin.Mode.ReleaseFast,
+ builtin.Mode.ReleaseSmall => {
args.append("-O2") catch unreachable;
args.append("-fno-stack-protector") catch unreachable;
},
@@ -1505,8 +1485,7 @@ pub const LibExeObjStep = struct {
}
if (!is_darwin) {
- const rpath_arg = builder.fmt("-Wl,-rpath,{}",
- os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
+ const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
defer builder.allocator.free(rpath_arg);
cc_args.append(rpath_arg) catch unreachable;
@@ -1535,8 +1514,7 @@ pub const LibExeObjStep = struct {
try builder.spawnChild(cc_args.toSliceConst());
if (self.target.wantSharedLibSymLinks()) {
- try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename,
- self.name_only_filename);
+ try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, self.name_only_filename);
}
}
},
@@ -1581,8 +1559,7 @@ pub const LibExeObjStep = struct {
cc_args.append("-o") catch unreachable;
cc_args.append(output_path) catch unreachable;
- const rpath_arg = builder.fmt("-Wl,-rpath,{}",
- os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
+ const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
defer builder.allocator.free(rpath_arg);
cc_args.append(rpath_arg) catch unreachable;
@@ -1635,7 +1612,7 @@ pub const TestStep = struct {
pub fn init(builder: &Builder, root_src: []const u8) TestStep {
const step_name = builder.fmt("test {}", root_src);
- return TestStep {
+ return TestStep{
.step = Step.init(step_name, builder.allocator, make),
.builder = builder,
.root_src = root_src,
@@ -1644,7 +1621,7 @@ pub const TestStep = struct {
.name_prefix = "",
.filter = null,
.link_libs = BufSet.init(builder.allocator),
- .target = Target { .Native = {} },
+ .target = Target{ .Native = {} },
.exec_cmd_args = null,
.include_dirs = ArrayList([]const u8).init(builder.allocator),
};
@@ -1674,16 +1651,12 @@ pub const TestStep = struct {
self.filter = text;
}
- pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os,
- target_environ: builtin.Environ) void
- {
- self.target = Target {
- .Cross = CrossTarget {
- .arch = target_arch,
- .os = target_os,
- .environ = target_environ,
- }
- };
+ pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
+ self.target = Target{ .Cross = CrossTarget{
+ .arch = target_arch,
+ .os = target_os,
+ .environ = target_environ,
+ } };
}
pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) void {
@@ -1789,11 +1762,9 @@ pub const CommandStep = struct {
env_map: &const BufMap,
/// ::argv is copied.
- pub fn create(builder: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
- argv: []const []const u8) &CommandStep
- {
+ pub fn create(builder: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) &CommandStep {
const self = builder.allocator.create(CommandStep) catch unreachable;
- *self = CommandStep {
+ self.* = CommandStep{
.builder = builder,
.step = Step.init(argv[0], builder.allocator, make),
.argv = builder.allocator.alloc([]u8, argv.len) catch unreachable,
@@ -1828,7 +1799,7 @@ const InstallArtifactStep = struct {
LibExeObjStep.Kind.Exe => builder.exe_dir,
LibExeObjStep.Kind.Lib => builder.lib_dir,
};
- *self = Self {
+ self.* = Self{
.builder = builder,
.step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
.artifact = artifact,
@@ -1837,10 +1808,8 @@ const InstallArtifactStep = struct {
self.step.dependOn(&artifact.step);
builder.pushInstalledFile(self.dest_file);
if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
- builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir,
- artifact.major_only_filename) catch unreachable);
- builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir,
- artifact.name_only_filename) catch unreachable);
+ builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir, artifact.major_only_filename) catch unreachable);
+ builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir, artifact.name_only_filename) catch unreachable);
}
return self;
}
@@ -1859,8 +1828,7 @@ const InstallArtifactStep = struct {
};
try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode);
if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
- try doAtomicSymLinks(builder.allocator, self.dest_file,
- self.artifact.major_only_filename, self.artifact.name_only_filename);
+ try doAtomicSymLinks(builder.allocator, self.dest_file, self.artifact.major_only_filename, self.artifact.name_only_filename);
}
}
};
@@ -1872,7 +1840,7 @@ pub const InstallFileStep = struct {
dest_path: []const u8,
pub fn init(builder: &Builder, src_path: []const u8, dest_path: []const u8) InstallFileStep {
- return InstallFileStep {
+ return InstallFileStep{
.builder = builder,
.step = Step.init(builder.fmt("install {}", src_path), builder.allocator, make),
.src_path = src_path,
@@ -1893,7 +1861,7 @@ pub const WriteFileStep = struct {
data: []const u8,
pub fn init(builder: &Builder, file_path: []const u8, data: []const u8) WriteFileStep {
- return WriteFileStep {
+ return WriteFileStep{
.builder = builder,
.step = Step.init(builder.fmt("writefile {}", file_path), builder.allocator, make),
.file_path = file_path,
@@ -1922,7 +1890,7 @@ pub const LogStep = struct {
data: []const u8,
pub fn init(builder: &Builder, data: []const u8) LogStep {
- return LogStep {
+ return LogStep{
.builder = builder,
.step = Step.init(builder.fmt("log {}", data), builder.allocator, make),
.data = data,
@@ -1941,7 +1909,7 @@ pub const RemoveDirStep = struct {
dir_path: []const u8,
pub fn init(builder: &Builder, dir_path: []const u8) RemoveDirStep {
- return RemoveDirStep {
+ return RemoveDirStep{
.builder = builder,
.step = Step.init(builder.fmt("RemoveDir {}", dir_path), builder.allocator, make),
.dir_path = dir_path,
@@ -1966,8 +1934,8 @@ pub const Step = struct {
loop_flag: bool,
done_flag: bool,
- pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn (&Step)error!void) Step {
- return Step {
+ pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn(&Step) error!void) Step {
+ return Step{
.name = name,
.makeFn = makeFn,
.dependencies = ArrayList(&Step).init(allocator),
@@ -1980,8 +1948,7 @@ pub const Step = struct {
}
pub fn make(self: &Step) !void {
- if (self.done_flag)
- return;
+ if (self.done_flag) return;
try self.makeFn(self);
self.done_flag = true;
@@ -1994,9 +1961,7 @@ pub const Step = struct {
fn makeNoOp(self: &Step) error!void {}
};
-fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_major_only: []const u8,
- filename_name_only: []const u8) !void
-{
+fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {
const out_dir = os.path.dirname(output_path);
const out_basename = os.path.basename(output_path);
// sym link for libfoo.so.1 to libfoo.so.1.2.3
diff --git a/std/crypto/blake2.zig b/std/crypto/blake2.zig
index 99f0e629cd..18025d08eb 100644
--- a/std/crypto/blake2.zig
+++ b/std/crypto/blake2.zig
@@ -6,11 +6,23 @@ const builtin = @import("builtin");
const htest = @import("test.zig");
const RoundParam = struct {
- a: usize, b: usize, c: usize, d: usize, x: usize, y: usize,
+ a: usize,
+ b: usize,
+ c: usize,
+ d: usize,
+ x: usize,
+ y: usize,
};
fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {
- return RoundParam { .a = a, .b = b, .c = c, .d = d, .x = x, .y = y, };
+ return RoundParam{
+ .a = a,
+ .b = b,
+ .c = c,
+ .d = d,
+ .x = x,
+ .y = y,
+ };
}
/////////////////////
@@ -19,145 +31,153 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {
pub const Blake2s224 = Blake2s(224);
pub const Blake2s256 = Blake2s(256);
-fn Blake2s(comptime out_len: usize) type { return struct {
- const Self = this;
- const block_size = 64;
- const digest_size = out_len / 8;
+fn Blake2s(comptime out_len: usize) type {
+ return struct {
+ const Self = this;
+ const block_size = 64;
+ const digest_size = out_len / 8;
+
+ const iv = [8]u32{
+ 0x6A09E667,
+ 0xBB67AE85,
+ 0x3C6EF372,
+ 0xA54FF53A,
+ 0x510E527F,
+ 0x9B05688C,
+ 0x1F83D9AB,
+ 0x5BE0CD19,
+ };
- const iv = [8]u32 {
- 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
- 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
- };
+ const sigma = [10][16]u8{
+ []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
+ []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
+ []const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
+ []const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
+ []const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
+ []const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
+ []const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
+ []const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
+ []const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
+ []const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
+ };
- const sigma = [10][16]u8 {
- []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
- []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
- []const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
- []const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
- []const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
- []const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
- []const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
- []const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
- []const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
- []const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
- };
+ h: [8]u32,
+ t: u64,
+ // Streaming cache
+ buf: [64]u8,
+ buf_len: u8,
- h: [8]u32,
- t: u64,
- // Streaming cache
- buf: [64]u8,
- buf_len: u8,
-
- pub fn init() Self {
- debug.assert(8 <= out_len and out_len <= 512);
-
- var s: Self = undefined;
- s.reset();
- return s;
- }
-
- pub fn reset(d: &Self) void {
- mem.copy(u32, d.h[0..], iv[0..]);
-
- // No key plus default parameters
- d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
- d.t = 0;
- d.buf_len = 0;
- }
-
- pub fn hash(b: []const u8, out: []u8) void {
- var d = Self.init();
- d.update(b);
- d.final(out);
- }
-
- pub fn update(d: &Self, b: []const u8) void {
- var off: usize = 0;
-
- // Partial buffer exists from previous update. Copy into buffer then hash.
- if (d.buf_len != 0 and d.buf_len + b.len > 64) {
- off += 64 - d.buf_len;
- mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
- d.t += 64;
- d.round(d.buf[0..], false);
- d.buf_len = 0;
+ pub fn init() Self {
+ debug.assert(8 <= out_len and out_len <= 512);
+
+ var s: Self = undefined;
+ s.reset();
+ return s;
}
- // Full middle blocks.
- while (off + 64 <= b.len) : (off += 64) {
- d.t += 64;
- d.round(b[off..off + 64], false);
+ pub fn reset(d: &Self) void {
+ mem.copy(u32, d.h[0..], iv[0..]);
+
+ // No key plus default parameters
+ d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
+ d.t = 0;
+ d.buf_len = 0;
}
- // Copy any remainder for next pass.
- mem.copy(u8, d.buf[d.buf_len..], b[off..]);
- d.buf_len += u8(b[off..].len);
- }
+ pub fn hash(b: []const u8, out: []u8) void {
+ var d = Self.init();
+ d.update(b);
+ d.final(out);
+ }
- pub fn final(d: &Self, out: []u8) void {
- debug.assert(out.len >= out_len / 8);
+ pub fn update(d: &Self, b: []const u8) void {
+ var off: usize = 0;
- mem.set(u8, d.buf[d.buf_len..], 0);
- d.t += d.buf_len;
- d.round(d.buf[0..], true);
+ // Partial buffer exists from previous update. Copy into buffer then hash.
+ if (d.buf_len != 0 and d.buf_len + b.len > 64) {
+ off += 64 - d.buf_len;
+ mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
+ d.t += 64;
+ d.round(d.buf[0..], false);
+ d.buf_len = 0;
+ }
- const rr = d.h[0 .. out_len / 32];
+ // Full middle blocks.
+ while (off + 64 <= b.len) : (off += 64) {
+ d.t += 64;
+ d.round(b[off..off + 64], false);
+ }
- for (rr) |s, j| {
- mem.writeInt(out[4*j .. 4*j + 4], s, builtin.Endian.Little);
+ // Copy any remainder for next pass.
+ mem.copy(u8, d.buf[d.buf_len..], b[off..]);
+ d.buf_len += u8(b[off..].len);
}
- }
- fn round(d: &Self, b: []const u8, last: bool) void {
- debug.assert(b.len == 64);
+ pub fn final(d: &Self, out: []u8) void {
+ debug.assert(out.len >= out_len / 8);
- var m: [16]u32 = undefined;
- var v: [16]u32 = undefined;
+ mem.set(u8, d.buf[d.buf_len..], 0);
+ d.t += d.buf_len;
+ d.round(d.buf[0..], true);
- for (m) |*r, i| {
- *r = mem.readIntLE(u32, b[4*i .. 4*i + 4]);
- }
+ const rr = d.h[0..out_len / 32];
- var k: usize = 0;
- while (k < 8) : (k += 1) {
- v[k] = d.h[k];
- v[k+8] = iv[k];
+ for (rr) |s, j| {
+ mem.writeInt(out[4 * j..4 * j + 4], s, builtin.Endian.Little);
+ }
}
- v[12] ^= @truncate(u32, d.t);
- v[13] ^= u32(d.t >> 32);
- if (last) v[14] = ~v[14];
-
- const rounds = comptime []RoundParam {
- Rp(0, 4, 8, 12, 0, 1),
- Rp(1, 5, 9, 13, 2, 3),
- Rp(2, 6, 10, 14, 4, 5),
- Rp(3, 7, 11, 15, 6, 7),
- Rp(0, 5, 10, 15, 8, 9),
- Rp(1, 6, 11, 12, 10, 11),
- Rp(2, 7, 8, 13, 12, 13),
- Rp(3, 4, 9, 14, 14, 15),
- };
+ fn round(d: &Self, b: []const u8, last: bool) void {
+ debug.assert(b.len == 64);
- comptime var j: usize = 0;
- inline while (j < 10) : (j += 1) {
- inline for (rounds) |r| {
- v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
- v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(16));
- v[r.c] = v[r.c] +% v[r.d];
- v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(12));
- v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
- v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(8));
- v[r.c] = v[r.c] +% v[r.d];
- v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(7));
+ var m: [16]u32 = undefined;
+ var v: [16]u32 = undefined;
+
+ for (m) |*r, i| {
+ r.* = mem.readIntLE(u32, b[4 * i..4 * i + 4]);
}
- }
- for (d.h) |*r, i| {
- *r ^= v[i] ^ v[i + 8];
+ var k: usize = 0;
+ while (k < 8) : (k += 1) {
+ v[k] = d.h[k];
+ v[k + 8] = iv[k];
+ }
+
+ v[12] ^= @truncate(u32, d.t);
+ v[13] ^= u32(d.t >> 32);
+ if (last) v[14] = ~v[14];
+
+ const rounds = comptime []RoundParam{
+ Rp(0, 4, 8, 12, 0, 1),
+ Rp(1, 5, 9, 13, 2, 3),
+ Rp(2, 6, 10, 14, 4, 5),
+ Rp(3, 7, 11, 15, 6, 7),
+ Rp(0, 5, 10, 15, 8, 9),
+ Rp(1, 6, 11, 12, 10, 11),
+ Rp(2, 7, 8, 13, 12, 13),
+ Rp(3, 4, 9, 14, 14, 15),
+ };
+
+ comptime var j: usize = 0;
+ inline while (j < 10) : (j += 1) {
+ inline for (rounds) |r| {
+ v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
+ v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(16));
+ v[r.c] = v[r.c] +% v[r.d];
+ v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(12));
+ v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
+ v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(8));
+ v[r.c] = v[r.c] +% v[r.d];
+ v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(7));
+ }
+ }
+
+ for (d.h) |*r, i| {
+ r.* ^= v[i] ^ v[i + 8];
+ }
}
- }
-};}
+ };
+}
test "blake2s224 single" {
const h1 = "1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4";
@@ -230,7 +250,7 @@ test "blake2s256 streaming" {
}
test "blake2s256 aligned final" {
- var block = []u8 {0} ** Blake2s256.block_size;
+ var block = []u8{0} ** Blake2s256.block_size;
var out: [Blake2s256.digest_size]u8 = undefined;
var h = Blake2s256.init();
@@ -238,154 +258,363 @@ test "blake2s256 aligned final" {
h.final(out[0..]);
}
-
/////////////////////
// Blake2b
pub const Blake2b384 = Blake2b(384);
pub const Blake2b512 = Blake2b(512);
-fn Blake2b(comptime out_len: usize) type { return struct {
- const Self = this;
- const block_size = 128;
- const digest_size = out_len / 8;
+fn Blake2b(comptime out_len: usize) type {
+ return struct {
+ const Self = this;
+ const block_size = 128;
+ const digest_size = out_len / 8;
+
+ const iv = [8]u64{
+ 0x6a09e667f3bcc908,
+ 0xbb67ae8584caa73b,
+ 0x3c6ef372fe94f82b,
+ 0xa54ff53a5f1d36f1,
+ 0x510e527fade682d1,
+ 0x9b05688c2b3e6c1f,
+ 0x1f83d9abfb41bd6b,
+ 0x5be0cd19137e2179,
+ };
- const iv = [8]u64 {
- 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
- 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
- 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
- 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
- };
+ const sigma = [12][16]u8{
+ []const u8{
+ 0,
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ 8,
+ 9,
+ 10,
+ 11,
+ 12,
+ 13,
+ 14,
+ 15,
+ },
+ []const u8{
+ 14,
+ 10,
+ 4,
+ 8,
+ 9,
+ 15,
+ 13,
+ 6,
+ 1,
+ 12,
+ 0,
+ 2,
+ 11,
+ 7,
+ 5,
+ 3,
+ },
+ []const u8{
+ 11,
+ 8,
+ 12,
+ 0,
+ 5,
+ 2,
+ 15,
+ 13,
+ 10,
+ 14,
+ 3,
+ 6,
+ 7,
+ 1,
+ 9,
+ 4,
+ },
+ []const u8{
+ 7,
+ 9,
+ 3,
+ 1,
+ 13,
+ 12,
+ 11,
+ 14,
+ 2,
+ 6,
+ 5,
+ 10,
+ 4,
+ 0,
+ 15,
+ 8,
+ },
+ []const u8{
+ 9,
+ 0,
+ 5,
+ 7,
+ 2,
+ 4,
+ 10,
+ 15,
+ 14,
+ 1,
+ 11,
+ 12,
+ 6,
+ 8,
+ 3,
+ 13,
+ },
+ []const u8{
+ 2,
+ 12,
+ 6,
+ 10,
+ 0,
+ 11,
+ 8,
+ 3,
+ 4,
+ 13,
+ 7,
+ 5,
+ 15,
+ 14,
+ 1,
+ 9,
+ },
+ []const u8{
+ 12,
+ 5,
+ 1,
+ 15,
+ 14,
+ 13,
+ 4,
+ 10,
+ 0,
+ 7,
+ 6,
+ 3,
+ 9,
+ 2,
+ 8,
+ 11,
+ },
+ []const u8{
+ 13,
+ 11,
+ 7,
+ 14,
+ 12,
+ 1,
+ 3,
+ 9,
+ 5,
+ 0,
+ 15,
+ 4,
+ 8,
+ 6,
+ 2,
+ 10,
+ },
+ []const u8{
+ 6,
+ 15,
+ 14,
+ 9,
+ 11,
+ 3,
+ 0,
+ 8,
+ 12,
+ 2,
+ 13,
+ 7,
+ 1,
+ 4,
+ 10,
+ 5,
+ },
+ []const u8{
+ 10,
+ 2,
+ 8,
+ 4,
+ 7,
+ 6,
+ 1,
+ 5,
+ 15,
+ 11,
+ 9,
+ 14,
+ 3,
+ 12,
+ 13,
+ 0,
+ },
+ []const u8{
+ 0,
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ 8,
+ 9,
+ 10,
+ 11,
+ 12,
+ 13,
+ 14,
+ 15,
+ },
+ []const u8{
+ 14,
+ 10,
+ 4,
+ 8,
+ 9,
+ 15,
+ 13,
+ 6,
+ 1,
+ 12,
+ 0,
+ 2,
+ 11,
+ 7,
+ 5,
+ 3,
+ },
+ };
- const sigma = [12][16]u8 {
- []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
- []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
- []const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
- []const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
- []const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
- []const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
- []const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
- []const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
- []const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
- []const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
- []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
- []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
- };
+ h: [8]u64,
+ t: u128,
+ // Streaming cache
+ buf: [128]u8,
+ buf_len: u8,
- h: [8]u64,
- t: u128,
- // Streaming cache
- buf: [128]u8,
- buf_len: u8,
-
- pub fn init() Self {
- debug.assert(8 <= out_len and out_len <= 512);
-
- var s: Self = undefined;
- s.reset();
- return s;
- }
-
- pub fn reset(d: &Self) void {
- mem.copy(u64, d.h[0..], iv[0..]);
-
- // No key plus default parameters
- d.h[0] ^= 0x01010000 ^ (out_len >> 3);
- d.t = 0;
- d.buf_len = 0;
- }
-
- pub fn hash(b: []const u8, out: []u8) void {
- var d = Self.init();
- d.update(b);
- d.final(out);
- }
-
- pub fn update(d: &Self, b: []const u8) void {
- var off: usize = 0;
-
- // Partial buffer exists from previous update. Copy into buffer then hash.
- if (d.buf_len != 0 and d.buf_len + b.len > 128) {
- off += 128 - d.buf_len;
- mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
- d.t += 128;
- d.round(d.buf[0..], false);
+ pub fn init() Self {
+ debug.assert(8 <= out_len and out_len <= 512);
+
+ var s: Self = undefined;
+ s.reset();
+ return s;
+ }
+
+ pub fn reset(d: &Self) void {
+ mem.copy(u64, d.h[0..], iv[0..]);
+
+ // No key plus default parameters
+ d.h[0] ^= 0x01010000 ^ (out_len >> 3);
+ d.t = 0;
d.buf_len = 0;
}
- // Full middle blocks.
- while (off + 128 <= b.len) : (off += 128) {
- d.t += 128;
- d.round(b[off..off + 128], false);
+ pub fn hash(b: []const u8, out: []u8) void {
+ var d = Self.init();
+ d.update(b);
+ d.final(out);
}
- // Copy any remainder for next pass.
- mem.copy(u8, d.buf[d.buf_len..], b[off..]);
- d.buf_len += u8(b[off..].len);
- }
+ pub fn update(d: &Self, b: []const u8) void {
+ var off: usize = 0;
- pub fn final(d: &Self, out: []u8) void {
- mem.set(u8, d.buf[d.buf_len..], 0);
- d.t += d.buf_len;
- d.round(d.buf[0..], true);
+ // Partial buffer exists from previous update. Copy into buffer then hash.
+ if (d.buf_len != 0 and d.buf_len + b.len > 128) {
+ off += 128 - d.buf_len;
+ mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
+ d.t += 128;
+ d.round(d.buf[0..], false);
+ d.buf_len = 0;
+ }
- const rr = d.h[0 .. out_len / 64];
+ // Full middle blocks.
+ while (off + 128 <= b.len) : (off += 128) {
+ d.t += 128;
+ d.round(b[off..off + 128], false);
+ }
- for (rr) |s, j| {
- mem.writeInt(out[8*j .. 8*j + 8], s, builtin.Endian.Little);
+ // Copy any remainder for next pass.
+ mem.copy(u8, d.buf[d.buf_len..], b[off..]);
+ d.buf_len += u8(b[off..].len);
}
- }
- fn round(d: &Self, b: []const u8, last: bool) void {
- debug.assert(b.len == 128);
+ pub fn final(d: &Self, out: []u8) void {
+ mem.set(u8, d.buf[d.buf_len..], 0);
+ d.t += d.buf_len;
+ d.round(d.buf[0..], true);
- var m: [16]u64 = undefined;
- var v: [16]u64 = undefined;
+ const rr = d.h[0..out_len / 64];
- for (m) |*r, i| {
- *r = mem.readIntLE(u64, b[8*i .. 8*i + 8]);
+ for (rr) |s, j| {
+ mem.writeInt(out[8 * j..8 * j + 8], s, builtin.Endian.Little);
+ }
}
- var k: usize = 0;
- while (k < 8) : (k += 1) {
- v[k] = d.h[k];
- v[k+8] = iv[k];
- }
+ fn round(d: &Self, b: []const u8, last: bool) void {
+ debug.assert(b.len == 128);
- v[12] ^= @truncate(u64, d.t);
- v[13] ^= u64(d.t >> 64);
- if (last) v[14] = ~v[14];
-
- const rounds = comptime []RoundParam {
- Rp(0, 4, 8, 12, 0, 1),
- Rp(1, 5, 9, 13, 2, 3),
- Rp(2, 6, 10, 14, 4, 5),
- Rp(3, 7, 11, 15, 6, 7),
- Rp(0, 5, 10, 15, 8, 9),
- Rp(1, 6, 11, 12, 10, 11),
- Rp(2, 7, 8, 13, 12, 13),
- Rp(3, 4, 9, 14, 14, 15),
- };
+ var m: [16]u64 = undefined;
+ var v: [16]u64 = undefined;
- comptime var j: usize = 0;
- inline while (j < 12) : (j += 1) {
- inline for (rounds) |r| {
- v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
- v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(32));
- v[r.c] = v[r.c] +% v[r.d];
- v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(24));
- v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
- v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(16));
- v[r.c] = v[r.c] +% v[r.d];
- v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(63));
+ for (m) |*r, i| {
+ r.* = mem.readIntLE(u64, b[8 * i..8 * i + 8]);
+ }
+
+ var k: usize = 0;
+ while (k < 8) : (k += 1) {
+ v[k] = d.h[k];
+ v[k + 8] = iv[k];
}
- }
- for (d.h) |*r, i| {
- *r ^= v[i] ^ v[i + 8];
+ v[12] ^= @truncate(u64, d.t);
+ v[13] ^= u64(d.t >> 64);
+ if (last) v[14] = ~v[14];
+
+ const rounds = comptime []RoundParam{
+ Rp(0, 4, 8, 12, 0, 1),
+ Rp(1, 5, 9, 13, 2, 3),
+ Rp(2, 6, 10, 14, 4, 5),
+ Rp(3, 7, 11, 15, 6, 7),
+ Rp(0, 5, 10, 15, 8, 9),
+ Rp(1, 6, 11, 12, 10, 11),
+ Rp(2, 7, 8, 13, 12, 13),
+ Rp(3, 4, 9, 14, 14, 15),
+ };
+
+ comptime var j: usize = 0;
+ inline while (j < 12) : (j += 1) {
+ inline for (rounds) |r| {
+ v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
+ v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(32));
+ v[r.c] = v[r.c] +% v[r.d];
+ v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(24));
+ v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
+ v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(16));
+ v[r.c] = v[r.c] +% v[r.d];
+ v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(63));
+ }
+ }
+
+ for (d.h) |*r, i| {
+ r.* ^= v[i] ^ v[i + 8];
+ }
}
- }
-};}
+ };
+}
test "blake2b384 single" {
const h1 = "b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100";
@@ -458,7 +687,7 @@ test "blake2b512 streaming" {
}
test "blake2b512 aligned final" {
- var block = []u8 {0} ** Blake2b512.block_size;
+ var block = []u8{0} ** Blake2b512.block_size;
var out: [Blake2b512.digest_size]u8 = undefined;
var h = Blake2b512.init();
diff --git a/std/crypto/hmac.zig b/std/crypto/hmac.zig
index 2a36f15b71..1415e88cf4 100644
--- a/std/crypto/hmac.zig
+++ b/std/crypto/hmac.zig
@@ -29,12 +29,12 @@ pub fn Hmac(comptime H: type) type {
var o_key_pad: [H.block_size]u8 = undefined;
for (o_key_pad) |*b, i| {
- *b = scratch[i] ^ 0x5c;
+ b.* = scratch[i] ^ 0x5c;
}
var i_key_pad: [H.block_size]u8 = undefined;
for (i_key_pad) |*b, i| {
- *b = scratch[i] ^ 0x36;
+ b.* = scratch[i] ^ 0x36;
}
// HMAC(k, m) = H(o_key_pad | H(i_key_pad | message)) where | is concatenation
diff --git a/std/crypto/sha3.zig b/std/crypto/sha3.zig
index f92f56d68f..73b6415e1d 100644
--- a/std/crypto/sha3.zig
+++ b/std/crypto/sha3.zig
@@ -10,148 +10,228 @@ pub const Sha3_256 = Keccak(256, 0x06);
pub const Sha3_384 = Keccak(384, 0x06);
pub const Sha3_512 = Keccak(512, 0x06);
-fn Keccak(comptime bits: usize, comptime delim: u8) type { return struct {
- const Self = this;
- const block_size = 200;
- const digest_size = bits / 8;
-
- s: [200]u8,
- offset: usize,
- rate: usize,
-
- pub fn init() Self {
- var d: Self = undefined;
- d.reset();
- return d;
- }
+fn Keccak(comptime bits: usize, comptime delim: u8) type {
+ return struct {
+ const Self = this;
+ const block_size = 200;
+ const digest_size = bits / 8;
+
+ s: [200]u8,
+ offset: usize,
+ rate: usize,
+
+ pub fn init() Self {
+ var d: Self = undefined;
+ d.reset();
+ return d;
+ }
- pub fn reset(d: &Self) void {
- mem.set(u8, d.s[0..], 0);
- d.offset = 0;
- d.rate = 200 - (bits / 4);
- }
+ pub fn reset(d: &Self) void {
+ mem.set(u8, d.s[0..], 0);
+ d.offset = 0;
+ d.rate = 200 - (bits / 4);
+ }
- pub fn hash(b: []const u8, out: []u8) void {
- var d = Self.init();
- d.update(b);
- d.final(out);
- }
+ pub fn hash(b: []const u8, out: []u8) void {
+ var d = Self.init();
+ d.update(b);
+ d.final(out);
+ }
- pub fn update(d: &Self, b: []const u8) void {
- var ip: usize = 0;
- var len = b.len;
- var rate = d.rate - d.offset;
- var offset = d.offset;
+ pub fn update(d: &Self, b: []const u8) void {
+ var ip: usize = 0;
+ var len = b.len;
+ var rate = d.rate - d.offset;
+ var offset = d.offset;
- // absorb
- while (len >= rate) {
- for (d.s[offset .. offset + rate]) |*r, i|
- *r ^= b[ip..][i];
+ // absorb
+ while (len >= rate) {
+ for (d.s[offset..offset + rate]) |*r, i|
+ r.* ^= b[ip..][i];
- keccak_f(1600, d.s[0..]);
+ keccak_f(1600, d.s[0..]);
- ip += rate;
- len -= rate;
- rate = d.rate;
- offset = 0;
- }
+ ip += rate;
+ len -= rate;
+ rate = d.rate;
+ offset = 0;
+ }
- for (d.s[offset .. offset + len]) |*r, i|
- *r ^= b[ip..][i];
+ for (d.s[offset..offset + len]) |*r, i|
+ r.* ^= b[ip..][i];
- d.offset = offset + len;
- }
+ d.offset = offset + len;
+ }
- pub fn final(d: &Self, out: []u8) void {
- // padding
- d.s[d.offset] ^= delim;
- d.s[d.rate - 1] ^= 0x80;
+ pub fn final(d: &Self, out: []u8) void {
+ // padding
+ d.s[d.offset] ^= delim;
+ d.s[d.rate - 1] ^= 0x80;
- keccak_f(1600, d.s[0..]);
+ keccak_f(1600, d.s[0..]);
- // squeeze
- var op: usize = 0;
- var len: usize = bits / 8;
+ // squeeze
+ var op: usize = 0;
+ var len: usize = bits / 8;
- while (len >= d.rate) {
- mem.copy(u8, out[op..], d.s[0..d.rate]);
- keccak_f(1600, d.s[0..]);
- op += d.rate;
- len -= d.rate;
+ while (len >= d.rate) {
+ mem.copy(u8, out[op..], d.s[0..d.rate]);
+ keccak_f(1600, d.s[0..]);
+ op += d.rate;
+ len -= d.rate;
+ }
+
+ mem.copy(u8, out[op..], d.s[0..len]);
}
+ };
+}
- mem.copy(u8, out[op..], d.s[0..len]);
- }
-};}
-
-const RC = []const u64 {
- 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
- 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
- 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
- 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
- 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
- 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
+const RC = []const u64{
+ 0x0000000000000001,
+ 0x0000000000008082,
+ 0x800000000000808a,
+ 0x8000000080008000,
+ 0x000000000000808b,
+ 0x0000000080000001,
+ 0x8000000080008081,
+ 0x8000000000008009,
+ 0x000000000000008a,
+ 0x0000000000000088,
+ 0x0000000080008009,
+ 0x000000008000000a,
+ 0x000000008000808b,
+ 0x800000000000008b,
+ 0x8000000000008089,
+ 0x8000000000008003,
+ 0x8000000000008002,
+ 0x8000000000000080,
+ 0x000000000000800a,
+ 0x800000008000000a,
+ 0x8000000080008081,
+ 0x8000000000008080,
+ 0x0000000080000001,
+ 0x8000000080008008,
};
-const ROTC = []const usize {
- 1, 3, 6, 10, 15, 21, 28, 36,
- 45, 55, 2, 14, 27, 41, 56, 8,
- 25, 43, 62, 18, 39, 61, 20, 44
+const ROTC = []const usize{
+ 1,
+ 3,
+ 6,
+ 10,
+ 15,
+ 21,
+ 28,
+ 36,
+ 45,
+ 55,
+ 2,
+ 14,
+ 27,
+ 41,
+ 56,
+ 8,
+ 25,
+ 43,
+ 62,
+ 18,
+ 39,
+ 61,
+ 20,
+ 44,
};
-const PIL = []const usize {
- 10, 7, 11, 17, 18, 3, 5, 16,
- 8, 21, 24, 4, 15, 23, 19, 13,
- 12, 2, 20, 14, 22, 9, 6, 1
+const PIL = []const usize{
+ 10,
+ 7,
+ 11,
+ 17,
+ 18,
+ 3,
+ 5,
+ 16,
+ 8,
+ 21,
+ 24,
+ 4,
+ 15,
+ 23,
+ 19,
+ 13,
+ 12,
+ 2,
+ 20,
+ 14,
+ 22,
+ 9,
+ 6,
+ 1,
};
-const M5 = []const usize {
- 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
+const M5 = []const usize{
+ 0,
+ 1,
+ 2,
+ 3,
+ 4,
+ 0,
+ 1,
+ 2,
+ 3,
+ 4,
};
fn keccak_f(comptime F: usize, d: []u8) void {
debug.assert(d.len == F / 8);
const B = F / 25;
- const no_rounds = comptime x: { break :x 12 + 2 * math.log2(B); };
+ const no_rounds = comptime x: {
+ break :x 12 + 2 * math.log2(B);
+ };
- var s = []const u64 {0} ** 25;
- var t = []const u64 {0} ** 1;
- var c = []const u64 {0} ** 5;
+ var s = []const u64{0} ** 25;
+ var t = []const u64{0} ** 1;
+ var c = []const u64{0} ** 5;
for (s) |*r, i| {
- *r = mem.readIntLE(u64, d[8*i .. 8*i + 8]);
+ r.* = mem.readIntLE(u64, d[8 * i..8 * i + 8]);
}
comptime var x: usize = 0;
comptime var y: usize = 0;
for (RC[0..no_rounds]) |round| {
// theta
- x = 0; inline while (x < 5) : (x += 1) {
- c[x] = s[x] ^ s[x+5] ^ s[x+10] ^ s[x+15] ^ s[x+20];
+ x = 0;
+ inline while (x < 5) : (x += 1) {
+ c[x] = s[x] ^ s[x + 5] ^ s[x + 10] ^ s[x + 15] ^ s[x + 20];
}
- x = 0; inline while (x < 5) : (x += 1) {
- t[0] = c[M5[x+4]] ^ math.rotl(u64, c[M5[x+1]], usize(1));
- y = 0; inline while (y < 5) : (y += 1) {
- s[x + y*5] ^= t[0];
+ x = 0;
+ inline while (x < 5) : (x += 1) {
+ t[0] = c[M5[x + 4]] ^ math.rotl(u64, c[M5[x + 1]], usize(1));
+ y = 0;
+ inline while (y < 5) : (y += 1) {
+ s[x + y * 5] ^= t[0];
}
}
// rho+pi
t[0] = s[1];
- x = 0; inline while (x < 24) : (x += 1) {
+ x = 0;
+ inline while (x < 24) : (x += 1) {
c[0] = s[PIL[x]];
s[PIL[x]] = math.rotl(u64, t[0], ROTC[x]);
t[0] = c[0];
}
// chi
- y = 0; inline while (y < 5) : (y += 1) {
- x = 0; inline while (x < 5) : (x += 1) {
- c[x] = s[x + y*5];
+ y = 0;
+ inline while (y < 5) : (y += 1) {
+ x = 0;
+ inline while (x < 5) : (x += 1) {
+ c[x] = s[x + y * 5];
}
- x = 0; inline while (x < 5) : (x += 1) {
- s[x + y*5] = c[x] ^ (~c[M5[x+1]] & c[M5[x+2]]);
+ x = 0;
+ inline while (x < 5) : (x += 1) {
+ s[x + y * 5] = c[x] ^ (~c[M5[x + 1]] & c[M5[x + 2]]);
}
}
@@ -160,11 +240,10 @@ fn keccak_f(comptime F: usize, d: []u8) void {
}
for (s) |r, i| {
- mem.writeInt(d[8*i .. 8*i + 8], r, builtin.Endian.Little);
+ mem.writeInt(d[8 * i..8 * i + 8], r, builtin.Endian.Little);
}
}
-
test "sha3-224 single" {
htest.assertEqualHash(Sha3_224, "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", "");
htest.assertEqualHash(Sha3_224, "e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", "abc");
@@ -192,7 +271,7 @@ test "sha3-224 streaming" {
}
test "sha3-256 single" {
- htest.assertEqualHash(Sha3_256, "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a" , "");
+ htest.assertEqualHash(Sha3_256, "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", "");
htest.assertEqualHash(Sha3_256, "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", "abc");
htest.assertEqualHash(Sha3_256, "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
}
@@ -218,7 +297,7 @@ test "sha3-256 streaming" {
}
test "sha3-256 aligned final" {
- var block = []u8 {0} ** Sha3_256.block_size;
+ var block = []u8{0} ** Sha3_256.block_size;
var out: [Sha3_256.digest_size]u8 = undefined;
var h = Sha3_256.init();
@@ -228,7 +307,7 @@ test "sha3-256 aligned final" {
test "sha3-384 single" {
const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
- htest.assertEqualHash(Sha3_384, h1 , "");
+ htest.assertEqualHash(Sha3_384, h1, "");
const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";
htest.assertEqualHash(Sha3_384, h2, "abc");
const h3 = "79407d3b5916b59c3e30b09822974791c313fb9ecc849e406f23592d04f625dc8c709b98b43b3852b337216179aa7fc7";
@@ -259,7 +338,7 @@ test "sha3-384 streaming" {
test "sha3-512 single" {
const h1 = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";
- htest.assertEqualHash(Sha3_512, h1 , "");
+ htest.assertEqualHash(Sha3_512, h1, "");
const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
htest.assertEqualHash(Sha3_512, h2, "abc");
const h3 = "afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185";
@@ -289,7 +368,7 @@ test "sha3-512 streaming" {
}
test "sha3-512 aligned final" {
- var block = []u8 {0} ** Sha3_512.block_size;
+ var block = []u8{0} ** Sha3_512.block_size;
var out: [Sha3_512.digest_size]u8 = undefined;
var h = Sha3_512.init();
diff --git a/std/event.zig b/std/event.zig
index bdad7fcc18..b2e7e3ae38 100644
--- a/std/event.zig
+++ b/std/event.zig
@@ -6,7 +6,7 @@ const mem = std.mem;
const posix = std.os.posix;
pub const TcpServer = struct {
- handleRequestFn: async<&mem.Allocator> fn (&TcpServer, &const std.net.Address, &const std.os.File) void,
+ handleRequestFn: async<&mem.Allocator> fn(&TcpServer, &const std.net.Address, &const std.os.File) void,
loop: &Loop,
sockfd: i32,
@@ -18,13 +18,11 @@ pub const TcpServer = struct {
const PromiseNode = std.LinkedList(promise).Node;
pub fn init(loop: &Loop) !TcpServer {
- const sockfd = try std.os.posixSocket(posix.AF_INET,
- posix.SOCK_STREAM|posix.SOCK_CLOEXEC|posix.SOCK_NONBLOCK,
- posix.PROTO_tcp);
+ const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
errdefer std.os.close(sockfd);
// TODO can't initialize handler coroutine here because we need well defined copy elision
- return TcpServer {
+ return TcpServer{
.loop = loop,
.sockfd = sockfd,
.accept_coro = null,
@@ -34,9 +32,7 @@ pub const TcpServer = struct {
};
}
- pub fn listen(self: &TcpServer, address: &const std.net.Address,
- handleRequestFn: async<&mem.Allocator> fn (&TcpServer, &const std.net.Address, &const std.os.File)void) !void
- {
+ pub fn listen(self: &TcpServer, address: &const std.net.Address, handleRequestFn: async<&mem.Allocator> fn(&TcpServer, &const std.net.Address, &const std.os.File) void) !void {
self.handleRequestFn = handleRequestFn;
try std.os.posixBind(self.sockfd, &address.os_addr);
@@ -48,7 +44,6 @@ pub const TcpServer = struct {
try self.loop.addFd(self.sockfd, ??self.accept_coro);
errdefer self.loop.removeFd(self.sockfd);
-
}
pub fn deinit(self: &TcpServer) void {
@@ -60,9 +55,7 @@ pub const TcpServer = struct {
pub async fn handler(self: &TcpServer) void {
while (true) {
var accepted_addr: std.net.Address = undefined;
- if (std.os.posixAccept(self.sockfd, &accepted_addr.os_addr,
- posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd|
- {
+ if (std.os.posixAccept(self.sockfd, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
var socket = std.os.File.openHandle(accepted_fd);
_ = async self.handleRequestFn(self, accepted_addr, socket) catch |err| switch (err) {
error.OutOfMemory => {
@@ -110,7 +103,7 @@ pub const Loop = struct {
fn init(allocator: &mem.Allocator) !Loop {
const epollfd = try std.os.linuxEpollCreate(std.os.linux.EPOLL_CLOEXEC);
- return Loop {
+ return Loop{
.keep_running = true,
.allocator = allocator,
.epollfd = epollfd,
@@ -118,11 +111,9 @@ pub const Loop = struct {
}
pub fn addFd(self: &Loop, fd: i32, prom: promise) !void {
- var ev = std.os.linux.epoll_event {
- .events = std.os.linux.EPOLLIN|std.os.linux.EPOLLOUT|std.os.linux.EPOLLET,
- .data = std.os.linux.epoll_data {
- .ptr = @ptrToInt(prom),
- },
+ var ev = std.os.linux.epoll_event{
+ .events = std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT | std.os.linux.EPOLLET,
+ .data = std.os.linux.epoll_data{ .ptr = @ptrToInt(prom) },
};
try std.os.linuxEpollCtl(self.epollfd, std.os.linux.EPOLL_CTL_ADD, fd, &ev);
}
@@ -157,9 +148,9 @@ pub const Loop = struct {
};
pub async fn connect(loop: &Loop, _address: &const std.net.Address) !std.os.File {
- var address = *_address; // TODO https://github.com/zig-lang/zig/issues/733
+ var address = _address.*; // TODO https://github.com/zig-lang/zig/issues/733
- const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM|posix.SOCK_CLOEXEC|posix.SOCK_NONBLOCK, posix.PROTO_tcp);
+ const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
errdefer std.os.close(sockfd);
try std.os.posixConnectAsync(sockfd, &address.os_addr);
@@ -179,11 +170,9 @@ test "listen on a port, send bytes, receive bytes" {
const Self = this;
- async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address,
- _socket: &const std.os.File) void
- {
+ async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address, _socket: &const std.os.File) void {
const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
- var socket = *_socket; // TODO https://github.com/zig-lang/zig/issues/733
+ var socket = _socket.*; // TODO https://github.com/zig-lang/zig/issues/733
defer socket.close();
const next_handler = async errorableHandler(self, _addr, socket) catch |err| switch (err) {
error.OutOfMemory => @panic("unable to handle connection: out of memory"),
@@ -191,14 +180,14 @@ test "listen on a port, send bytes, receive bytes" {
(await next_handler) catch |err| {
std.debug.panic("unable to handle connection: {}\n", err);
};
- suspend |p| { cancel p; }
+ suspend |p| {
+ cancel p;
+ }
}
- async fn errorableHandler(self: &Self, _addr: &const std.net.Address,
- _socket: &const std.os.File) !void
- {
- const addr = *_addr; // TODO https://github.com/zig-lang/zig/issues/733
- var socket = *_socket; // TODO https://github.com/zig-lang/zig/issues/733
+ async fn errorableHandler(self: &Self, _addr: &const std.net.Address, _socket: &const std.os.File) !void {
+ const addr = _addr.*; // TODO https://github.com/zig-lang/zig/issues/733
+ var socket = _socket.*; // TODO https://github.com/zig-lang/zig/issues/733
var adapter = std.io.FileOutStream.init(&socket);
var stream = &adapter.stream;
@@ -210,9 +199,7 @@ test "listen on a port, send bytes, receive bytes" {
const addr = std.net.Address.initIp4(ip4addr, 0);
var loop = try Loop.init(std.debug.global_allocator);
- var server = MyServer {
- .tcp_server = try TcpServer.init(&loop),
- };
+ var server = MyServer{ .tcp_server = try TcpServer.init(&loop) };
defer server.tcp_server.deinit();
try server.tcp_server.listen(addr, MyServer.handler);
diff --git a/std/hash/crc.zig b/std/hash/crc.zig
index f88069ce3c..e4c1405a1c 100644
--- a/std/hash/crc.zig
+++ b/std/hash/crc.zig
@@ -9,9 +9,9 @@ const std = @import("../index.zig");
const debug = std.debug;
pub const Polynomial = struct {
- const IEEE = 0xedb88320;
+ const IEEE = 0xedb88320;
const Castagnoli = 0x82f63b78;
- const Koopman = 0xeb31d82e;
+ const Koopman = 0xeb31d82e;
};
// IEEE is by far the most common CRC and so is aliased by default.
@@ -27,20 +27,22 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
for (tables[0]) |*e, i| {
var crc = u32(i);
- var j: usize = 0; while (j < 8) : (j += 1) {
+ var j: usize = 0;
+ while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ poly;
} else {
crc = (crc >> 1);
}
}
- *e = crc;
+ e.* = crc;
}
var i: usize = 0;
while (i < 256) : (i += 1) {
var crc = tables[0][i];
- var j: usize = 1; while (j < 8) : (j += 1) {
+ var j: usize = 1;
+ while (j < 8) : (j += 1) {
const index = @truncate(u8, crc);
crc = tables[0][index] ^ (crc >> 8);
tables[j][i] = crc;
@@ -53,22 +55,21 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
crc: u32,
pub fn init() Self {
- return Self {
- .crc = 0xffffffff,
- };
+ return Self{ .crc = 0xffffffff };
}
pub fn update(self: &Self, input: []const u8) void {
var i: usize = 0;
while (i + 8 <= input.len) : (i += 8) {
- const p = input[i..i+8];
+ const p = input[i..i + 8];
// Unrolling this way gives ~50Mb/s increase
- self.crc ^= (u32(p[0]) << 0);
- self.crc ^= (u32(p[1]) << 8);
+ self.crc ^= (u32(p[0]) << 0);
+ self.crc ^= (u32(p[1]) << 8);
self.crc ^= (u32(p[2]) << 16);
self.crc ^= (u32(p[3]) << 24);
+
self.crc =
lookup_tables[0][p[7]] ^
lookup_tables[1][p[6]] ^
@@ -123,14 +124,15 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
for (table) |*e, i| {
var crc = u32(i * 16);
- var j: usize = 0; while (j < 8) : (j += 1) {
+ var j: usize = 0;
+ while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ poly;
} else {
crc = (crc >> 1);
}
}
- *e = crc;
+ e.* = crc;
}
break :block table;
@@ -139,9 +141,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
crc: u32,
pub fn init() Self {
- return Self {
- .crc = 0xffffffff,
- };
+ return Self{ .crc = 0xffffffff };
}
pub fn update(self: &Self, input: []const u8) void {
diff --git a/std/hash_map.zig b/std/hash_map.zig
index 2a178d9d44..e3b86f8a3b 100644
--- a/std/hash_map.zig
+++ b/std/hash_map.zig
@@ -9,10 +9,7 @@ const builtin = @import("builtin");
const want_modification_safety = builtin.mode != builtin.Mode.ReleaseFast;
const debug_u32 = if (want_modification_safety) u32 else void;
-pub fn HashMap(comptime K: type, comptime V: type,
- comptime hash: fn(key: K)u32,
- comptime eql: fn(a: K, b: K)bool) type
-{
+pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K) u32, comptime eql: fn(a: K, b: K) bool) type {
return struct {
entries: []Entry,
size: usize,
@@ -65,7 +62,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
};
pub fn init(allocator: &Allocator) Self {
- return Self {
+ return Self{
.entries = []Entry{},
.allocator = allocator,
.size = 0,
@@ -129,34 +126,36 @@ pub fn HashMap(comptime K: type, comptime V: type,
if (hm.entries.len == 0) return null;
hm.incrementModificationCount();
const start_index = hm.keyToIndex(key);
- {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
- const index = (start_index + roll_over) % hm.entries.len;
- var entry = &hm.entries[index];
-
- if (!entry.used)
- return null;
-
- if (!eql(entry.key, key)) continue;
-
- while (roll_over < hm.entries.len) : (roll_over += 1) {
- const next_index = (start_index + roll_over + 1) % hm.entries.len;
- const next_entry = &hm.entries[next_index];
- if (!next_entry.used or next_entry.distance_from_start_index == 0) {
- entry.used = false;
- hm.size -= 1;
- return entry;
+ {
+ var roll_over: usize = 0;
+ while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
+ const index = (start_index + roll_over) % hm.entries.len;
+ var entry = &hm.entries[index];
+
+ if (!entry.used) return null;
+
+ if (!eql(entry.key, key)) continue;
+
+ while (roll_over < hm.entries.len) : (roll_over += 1) {
+ const next_index = (start_index + roll_over + 1) % hm.entries.len;
+ const next_entry = &hm.entries[next_index];
+ if (!next_entry.used or next_entry.distance_from_start_index == 0) {
+ entry.used = false;
+ hm.size -= 1;
+ return entry;
+ }
+ entry.* = next_entry.*;
+ entry.distance_from_start_index -= 1;
+ entry = next_entry;
}
- *entry = *next_entry;
- entry.distance_from_start_index -= 1;
- entry = next_entry;
+ unreachable; // shifting everything in the table
}
- unreachable; // shifting everything in the table
- }}
+ }
return null;
}
pub fn iterator(hm: &const Self) Iterator {
- return Iterator {
+ return Iterator{
.hm = hm,
.count = 0,
.index = 0,
@@ -182,21 +181,23 @@ pub fn HashMap(comptime K: type, comptime V: type,
/// Returns the value that was already there.
fn internalPut(hm: &Self, orig_key: K, orig_value: &const V) ?V {
var key = orig_key;
- var value = *orig_value;
+ var value = orig_value.*;
const start_index = hm.keyToIndex(key);
var roll_over: usize = 0;
var distance_from_start_index: usize = 0;
- while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1;}) {
+ while (roll_over < hm.entries.len) : ({
+ roll_over += 1;
+ distance_from_start_index += 1;
+ }) {
const index = (start_index + roll_over) % hm.entries.len;
const entry = &hm.entries[index];
if (entry.used and !eql(entry.key, key)) {
if (entry.distance_from_start_index < distance_from_start_index) {
// robin hood to the rescue
- const tmp = *entry;
- hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index,
- distance_from_start_index);
- *entry = Entry {
+ const tmp = entry.*;
+ hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index, distance_from_start_index);
+ entry.* = Entry{
.used = true,
.distance_from_start_index = distance_from_start_index,
.key = key,
@@ -219,7 +220,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
}
hm.max_distance_from_start_index = math.max(distance_from_start_index, hm.max_distance_from_start_index);
- *entry = Entry {
+ entry.* = Entry{
.used = true,
.distance_from_start_index = distance_from_start_index,
.key = key,
@@ -232,13 +233,16 @@ pub fn HashMap(comptime K: type, comptime V: type,
fn internalGet(hm: &const Self, key: K) ?&Entry {
const start_index = hm.keyToIndex(key);
- {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
- const index = (start_index + roll_over) % hm.entries.len;
- const entry = &hm.entries[index];
-
- if (!entry.used) return null;
- if (eql(entry.key, key)) return entry;
- }}
+ {
+ var roll_over: usize = 0;
+ while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
+ const index = (start_index + roll_over) % hm.entries.len;
+ const entry = &hm.entries[index];
+
+ if (!entry.used) return null;
+ if (eql(entry.key, key)) return entry;
+ }
+ }
return null;
}
@@ -282,11 +286,19 @@ test "iterator hash map" {
assert((reset_map.put(2, 22) catch unreachable) == null);
assert((reset_map.put(3, 33) catch unreachable) == null);
- var keys = []i32 { 1, 2, 3 };
- var values = []i32 { 11, 22, 33 };
+ var keys = []i32{
+ 1,
+ 2,
+ 3,
+ };
+ var values = []i32{
+ 11,
+ 22,
+ 33,
+ };
var it = reset_map.iterator();
- var count : usize = 0;
+ var count: usize = 0;
while (it.next()) |next| {
assert(next.key == keys[count]);
assert(next.value == values[count]);
@@ -305,7 +317,7 @@ test "iterator hash map" {
}
it.reset();
- var entry = ?? it.next();
+ var entry = ??it.next();
assert(entry.key == keys[0]);
assert(entry.value == values[0]);
}
diff --git a/std/json.zig b/std/json.zig
index 6f853501ed..2ea9083e2f 100644
--- a/std/json.zig
+++ b/std/json.zig
@@ -35,7 +35,7 @@ pub const Token = struct {
};
pub fn init(id: Id, count: usize, offset: u1) Token {
- return Token {
+ return Token{
.id = id,
.offset = offset,
.string_has_escape = false,
@@ -45,7 +45,7 @@ pub const Token = struct {
}
pub fn initString(count: usize, has_unicode_escape: bool) Token {
- return Token {
+ return Token{
.id = Id.String,
.offset = 0,
.string_has_escape = has_unicode_escape,
@@ -55,7 +55,7 @@ pub const Token = struct {
}
pub fn initNumber(count: usize, number_is_integer: bool) Token {
- return Token {
+ return Token{
.id = Id.Number,
.offset = 0,
.string_has_escape = false,
@@ -66,7 +66,7 @@ pub const Token = struct {
// A marker token is a zero-length
pub fn initMarker(id: Id) Token {
- return Token {
+ return Token{
.id = id,
.offset = 0,
.string_has_escape = false,
@@ -77,7 +77,7 @@ pub const Token = struct {
// Slice into the underlying input string.
pub fn slice(self: &const Token, input: []const u8, i: usize) []const u8 {
- return input[i + self.offset - self.count .. i + self.offset];
+ return input[i + self.offset - self.count..i + self.offset];
}
};
@@ -105,8 +105,8 @@ const StreamingJsonParser = struct {
stack: u256,
stack_used: u8,
- const object_bit = 0;
- const array_bit = 1;
+ const object_bit = 0;
+ const array_bit = 1;
const max_stack_size = @maxValue(u8);
pub fn init() StreamingJsonParser {
@@ -120,7 +120,7 @@ const StreamingJsonParser = struct {
p.count = 0;
// Set before ever read in main transition function
p.after_string_state = undefined;
- p.after_value_state = State.ValueEnd; // handle end of values normally
+ p.after_value_state = State.ValueEnd; // handle end of values normally
p.stack = 0;
p.stack_used = 0;
p.complete = false;
@@ -181,7 +181,7 @@ const StreamingJsonParser = struct {
}
};
- pub const Error = error {
+ pub const Error = error{
InvalidTopLevel,
TooManyNestedItems,
TooManyClosingItems,
@@ -206,8 +206,8 @@ const StreamingJsonParser = struct {
//
// There is currently no error recovery on a bad stream.
pub fn feed(p: &StreamingJsonParser, c: u8, token1: &?Token, token2: &?Token) Error!void {
- *token1 = null;
- *token2 = null;
+ token1.* = null;
+ token2.* = null;
p.count += 1;
// unlikely
@@ -228,7 +228,7 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ObjectSeparator;
- *token = Token.initMarker(Token.Id.ObjectBegin);
+ token.* = Token.initMarker(Token.Id.ObjectBegin);
},
'[' => {
p.stack <<= 1;
@@ -238,7 +238,7 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ValueEnd;
- *token = Token.initMarker(Token.Id.ArrayBegin);
+ token.* = Token.initMarker(Token.Id.ArrayBegin);
},
'-' => {
p.number_is_integer = true;
@@ -281,7 +281,10 @@ const StreamingJsonParser = struct {
p.after_value_state = State.TopLevelEnd;
p.count = 0;
},
- 0x09, 0x0A, 0x0D, 0x20 => {
+ 0x09,
+ 0x0A,
+ 0x0D,
+ 0x20 => {
// whitespace
},
else => {
@@ -290,7 +293,10 @@ const StreamingJsonParser = struct {
},
State.TopLevelEnd => switch (c) {
- 0x09, 0x0A, 0x0D, 0x20 => {
+ 0x09,
+ 0x0A,
+ 0x0D,
+ 0x20 => {
// whitespace
},
else => {
@@ -324,7 +330,7 @@ const StreamingJsonParser = struct {
else => {},
}
- *token = Token.initMarker(Token.Id.ObjectEnd);
+ token.* = Token.initMarker(Token.Id.ObjectEnd);
},
']' => {
if (p.stack & 1 != array_bit) {
@@ -348,7 +354,7 @@ const StreamingJsonParser = struct {
else => {},
}
- *token = Token.initMarker(Token.Id.ArrayEnd);
+ token.* = Token.initMarker(Token.Id.ArrayEnd);
},
'{' => {
if (p.stack_used == max_stack_size) {
@@ -362,7 +368,7 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ObjectSeparator;
- *token = Token.initMarker(Token.Id.ObjectBegin);
+ token.* = Token.initMarker(Token.Id.ObjectBegin);
},
'[' => {
if (p.stack_used == max_stack_size) {
@@ -376,7 +382,7 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ValueEnd;
- *token = Token.initMarker(Token.Id.ArrayBegin);
+ token.* = Token.initMarker(Token.Id.ArrayBegin);
},
'-' => {
p.state = State.Number;
@@ -406,7 +412,10 @@ const StreamingJsonParser = struct {
p.state = State.NullLiteral1;
p.count = 0;
},
- 0x09, 0x0A, 0x0D, 0x20 => {
+ 0x09,
+ 0x0A,
+ 0x0D,
+ 0x20 => {
// whitespace
},
else => {
@@ -428,7 +437,7 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ObjectSeparator;
- *token = Token.initMarker(Token.Id.ObjectBegin);
+ token.* = Token.initMarker(Token.Id.ObjectBegin);
},
'[' => {
if (p.stack_used == max_stack_size) {
@@ -442,7 +451,7 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ValueEnd;
- *token = Token.initMarker(Token.Id.ArrayBegin);
+ token.* = Token.initMarker(Token.Id.ArrayBegin);
},
'-' => {
p.state = State.Number;
@@ -472,7 +481,10 @@ const StreamingJsonParser = struct {
p.state = State.NullLiteral1;
p.count = 0;
},
- 0x09, 0x0A, 0x0D, 0x20 => {
+ 0x09,
+ 0x0A,
+ 0x0D,
+ 0x20 => {
// whitespace
},
else => {
@@ -501,7 +513,7 @@ const StreamingJsonParser = struct {
p.state = State.TopLevelEnd;
}
- *token = Token.initMarker(Token.Id.ArrayEnd);
+ token.* = Token.initMarker(Token.Id.ArrayEnd);
},
'}' => {
if (p.stack_used == 0) {
@@ -519,9 +531,12 @@ const StreamingJsonParser = struct {
p.state = State.TopLevelEnd;
}
- *token = Token.initMarker(Token.Id.ObjectEnd);
+ token.* = Token.initMarker(Token.Id.ObjectEnd);
},
- 0x09, 0x0A, 0x0D, 0x20 => {
+ 0x09,
+ 0x0A,
+ 0x0D,
+ 0x20 => {
// whitespace
},
else => {
@@ -534,7 +549,10 @@ const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ValueEnd;
},
- 0x09, 0x0A, 0x0D, 0x20 => {
+ 0x09,
+ 0x0A,
+ 0x0D,
+ 0x20 => {
// whitespace
},
else => {
@@ -553,12 +571,15 @@ const StreamingJsonParser = struct {
p.complete = true;
}
- *token = Token.initString(p.count - 1, p.string_has_escape);
+ token.* = Token.initString(p.count - 1, p.string_has_escape);
},
'\\' => {
p.state = State.StringEscapeCharacter;
},
- 0x20, 0x21, 0x23 ... 0x5B, 0x5D ... 0x7F => {
+ 0x20,
+ 0x21,
+ 0x23 ... 0x5B,
+ 0x5D ... 0x7F => {
// non-control ascii
},
0xC0 ... 0xDF => {
@@ -599,7 +620,14 @@ const StreamingJsonParser = struct {
// The current JSONTestSuite tests rely on both of this behaviour being present
// however, so we default to the status quo where both are accepted until this
// is further clarified.
- '"', '\\', '/', 'b', 'f', 'n', 'r', 't' => {
+ '"',
+ '\\',
+ '/',
+ 'b',
+ 'f',
+ 'n',
+ 'r',
+ 't' => {
p.string_has_escape = true;
p.state = State.String;
},
@@ -613,28 +641,36 @@ const StreamingJsonParser = struct {
},
State.StringEscapeHexUnicode4 => switch (c) {
- '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
+ '0' ... '9',
+ 'A' ... 'F',
+ 'a' ... 'f' => {
p.state = State.StringEscapeHexUnicode3;
},
else => return error.InvalidUnicodeHexSymbol,
},
State.StringEscapeHexUnicode3 => switch (c) {
- '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
+ '0' ... '9',
+ 'A' ... 'F',
+ 'a' ... 'f' => {
p.state = State.StringEscapeHexUnicode2;
},
else => return error.InvalidUnicodeHexSymbol,
},
State.StringEscapeHexUnicode2 => switch (c) {
- '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
+ '0' ... '9',
+ 'A' ... 'F',
+ 'a' ... 'f' => {
p.state = State.StringEscapeHexUnicode1;
},
else => return error.InvalidUnicodeHexSymbol,
},
State.StringEscapeHexUnicode1 => switch (c) {
- '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
+ '0' ... '9',
+ 'A' ... 'F',
+ 'a' ... 'f' => {
p.state = State.String;
},
else => return error.InvalidUnicodeHexSymbol,
@@ -662,13 +698,14 @@ const StreamingJsonParser = struct {
p.number_is_integer = false;
p.state = State.NumberFractionalRequired;
},
- 'e', 'E' => {
+ 'e',
+ 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
else => {
p.state = p.after_value_state;
- *token = Token.initNumber(p.count, p.number_is_integer);
+ token.* = Token.initNumber(p.count, p.number_is_integer);
return true;
},
}
@@ -681,7 +718,8 @@ const StreamingJsonParser = struct {
p.number_is_integer = false;
p.state = State.NumberFractionalRequired;
},
- 'e', 'E' => {
+ 'e',
+ 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
@@ -690,7 +728,7 @@ const StreamingJsonParser = struct {
},
else => {
p.state = p.after_value_state;
- *token = Token.initNumber(p.count, p.number_is_integer);
+ token.* = Token.initNumber(p.count, p.number_is_integer);
return true;
},
}
@@ -714,13 +752,14 @@ const StreamingJsonParser = struct {
'0' ... '9' => {
// another digit
},
- 'e', 'E' => {
+ 'e',
+ 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
else => {
p.state = p.after_value_state;
- *token = Token.initNumber(p.count, p.number_is_integer);
+ token.* = Token.initNumber(p.count, p.number_is_integer);
return true;
},
}
@@ -729,20 +768,22 @@ const StreamingJsonParser = struct {
State.NumberMaybeExponent => {
p.complete = p.after_value_state == State.TopLevelEnd;
switch (c) {
- 'e', 'E' => {
+ 'e',
+ 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
else => {
p.state = p.after_value_state;
- *token = Token.initNumber(p.count, p.number_is_integer);
+ token.* = Token.initNumber(p.count, p.number_is_integer);
return true;
},
}
},
State.NumberExponent => switch (c) {
- '-', '+', => {
+ '-',
+ '+' => {
p.complete = false;
p.state = State.NumberExponentDigitsRequired;
},
@@ -773,7 +814,7 @@ const StreamingJsonParser = struct {
},
else => {
p.state = p.after_value_state;
- *token = Token.initNumber(p.count, p.number_is_integer);
+ token.* = Token.initNumber(p.count, p.number_is_integer);
return true;
},
}
@@ -793,7 +834,7 @@ const StreamingJsonParser = struct {
'e' => {
p.state = p.after_value_state;
p.complete = p.state == State.TopLevelEnd;
- *token = Token.init(Token.Id.True, p.count + 1, 1);
+ token.* = Token.init(Token.Id.True, p.count + 1, 1);
},
else => {
return error.InvalidLiteral;
@@ -819,7 +860,7 @@ const StreamingJsonParser = struct {
'e' => {
p.state = p.after_value_state;
p.complete = p.state == State.TopLevelEnd;
- *token = Token.init(Token.Id.False, p.count + 1, 1);
+ token.* = Token.init(Token.Id.False, p.count + 1, 1);
},
else => {
return error.InvalidLiteral;
@@ -840,7 +881,7 @@ const StreamingJsonParser = struct {
'l' => {
p.state = p.after_value_state;
p.complete = p.state == State.TopLevelEnd;
- *token = Token.init(Token.Id.Null, p.count + 1, 1);
+ token.* = Token.init(Token.Id.Null, p.count + 1, 1);
},
else => {
return error.InvalidLiteral;
@@ -895,7 +936,7 @@ pub const Value = union(enum) {
Object: ObjectMap,
pub fn dump(self: &const Value) void {
- switch (*self) {
+ switch (self.*) {
Value.Null => {
std.debug.warn("null");
},
@@ -950,7 +991,7 @@ pub const Value = union(enum) {
}
fn dumpIndentLevel(self: &const Value, indent: usize, level: usize) void {
- switch (*self) {
+ switch (self.*) {
Value.Null => {
std.debug.warn("null");
},
@@ -1027,7 +1068,7 @@ const JsonParser = struct {
};
pub fn init(allocator: &Allocator, copy_strings: bool) JsonParser {
- return JsonParser {
+ return JsonParser{
.allocator = allocator,
.state = State.Simple,
.copy_strings = copy_strings,
@@ -1082,7 +1123,7 @@ const JsonParser = struct {
std.debug.assert(p.stack.len == 1);
- return ValueTree {
+ return ValueTree{
.arena = arena,
.root = p.stack.at(0),
};
@@ -1115,11 +1156,11 @@ const JsonParser = struct {
switch (token.id) {
Token.Id.ObjectBegin => {
- try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
+ try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
p.state = State.ObjectKey;
},
Token.Id.ArrayBegin => {
- try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
+ try p.stack.append(Value{ .Array = ArrayList(Value).init(allocator) });
p.state = State.ArrayValue;
},
Token.Id.String => {
@@ -1133,12 +1174,12 @@ const JsonParser = struct {
p.state = State.ObjectKey;
},
Token.Id.True => {
- _ = try object.put(key, Value { .Bool = true });
+ _ = try object.put(key, Value{ .Bool = true });
_ = p.stack.pop();
p.state = State.ObjectKey;
},
Token.Id.False => {
- _ = try object.put(key, Value { .Bool = false });
+ _ = try object.put(key, Value{ .Bool = false });
_ = p.stack.pop();
p.state = State.ObjectKey;
},
@@ -1165,11 +1206,11 @@ const JsonParser = struct {
try p.pushToParent(value);
},
Token.Id.ObjectBegin => {
- try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
+ try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
p.state = State.ObjectKey;
},
Token.Id.ArrayBegin => {
- try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
+ try p.stack.append(Value{ .Array = ArrayList(Value).init(allocator) });
p.state = State.ArrayValue;
},
Token.Id.String => {
@@ -1179,10 +1220,10 @@ const JsonParser = struct {
try array.append(try p.parseNumber(token, input, i));
},
Token.Id.True => {
- try array.append(Value { .Bool = true });
+ try array.append(Value{ .Bool = true });
},
Token.Id.False => {
- try array.append(Value { .Bool = false });
+ try array.append(Value{ .Bool = false });
},
Token.Id.Null => {
try array.append(Value.Null);
@@ -1194,11 +1235,11 @@ const JsonParser = struct {
},
State.Simple => switch (token.id) {
Token.Id.ObjectBegin => {
- try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
+ try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
p.state = State.ObjectKey;
},
Token.Id.ArrayBegin => {
- try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
+ try p.stack.append(Value{ .Array = ArrayList(Value).init(allocator) });
p.state = State.ArrayValue;
},
Token.Id.String => {
@@ -1208,15 +1249,16 @@ const JsonParser = struct {
try p.stack.append(try p.parseNumber(token, input, i));
},
Token.Id.True => {
- try p.stack.append(Value { .Bool = true });
+ try p.stack.append(Value{ .Bool = true });
},
Token.Id.False => {
- try p.stack.append(Value { .Bool = false });
+ try p.stack.append(Value{ .Bool = false });
},
Token.Id.Null => {
try p.stack.append(Value.Null);
},
- Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
+ Token.Id.ObjectEnd,
+ Token.Id.ArrayEnd => {
unreachable;
},
},
@@ -1248,15 +1290,14 @@ const JsonParser = struct {
// TODO: We don't strictly have to copy values which do not contain any escape
// characters if flagged with the option.
const slice = token.slice(input, i);
- return Value { .String = try mem.dupe(p.allocator, u8, slice) };
+ return Value{ .String = try mem.dupe(p.allocator, u8, slice) };
}
fn parseNumber(p: &JsonParser, token: &const Token, input: []const u8, i: usize) !Value {
return if (token.number_is_integer)
- Value { .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
+ Value{ .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
else
- @panic("TODO: fmt.parseFloat not yet implemented")
- ;
+ @panic("TODO: fmt.parseFloat not yet implemented");
}
};
@@ -1267,21 +1308,21 @@ test "json parser dynamic" {
defer p.deinit();
const s =
- \\{
- \\ "Image": {
- \\ "Width": 800,
- \\ "Height": 600,
- \\ "Title": "View from 15th Floor",
- \\ "Thumbnail": {
- \\ "Url": "http://www.example.com/image/481989943",
- \\ "Height": 125,
- \\ "Width": 100
- \\ },
- \\ "Animated" : false,
- \\ "IDs": [116, 943, 234, 38793]
- \\ }
- \\}
- ;
+ \\{
+ \\ "Image": {
+ \\ "Width": 800,
+ \\ "Height": 600,
+ \\ "Title": "View from 15th Floor",
+ \\ "Thumbnail": {
+ \\ "Url": "http://www.example.com/image/481989943",
+ \\ "Height": 125,
+ \\ "Width": 100
+ \\ },
+ \\ "Animated" : false,
+ \\ "IDs": [116, 943, 234, 38793]
+ \\ }
+ \\}
+ ;
var tree = try p.parse(s);
defer tree.deinit();
diff --git a/std/math/acos.zig b/std/math/acos.zig
index a4f08af306..284f73fc91 100644
--- a/std/math/acos.zig
+++ b/std/math/acos.zig
@@ -16,7 +16,7 @@ pub fn acos(x: var) @typeOf(x) {
}
fn r32(z: f32) f32 {
- const pS0 = 1.6666586697e-01;
+ const pS0 = 1.6666586697e-01;
const pS1 = -4.2743422091e-02;
const pS2 = -8.6563630030e-03;
const qS1 = -7.0662963390e-01;
@@ -74,16 +74,16 @@ fn acos32(x: f32) f32 {
}
fn r64(z: f64) f64 {
- const pS0: f64 = 1.66666666666666657415e-01;
+ const pS0: f64 = 1.66666666666666657415e-01;
const pS1: f64 = -3.25565818622400915405e-01;
- const pS2: f64 = 2.01212532134862925881e-01;
+ const pS2: f64 = 2.01212532134862925881e-01;
const pS3: f64 = -4.00555345006794114027e-02;
- const pS4: f64 = 7.91534994289814532176e-04;
- const pS5: f64 = 3.47933107596021167570e-05;
+ const pS4: f64 = 7.91534994289814532176e-04;
+ const pS5: f64 = 3.47933107596021167570e-05;
const qS1: f64 = -2.40339491173441421878e+00;
- const qS2: f64 = 2.02094576023350569471e+00;
+ const qS2: f64 = 2.02094576023350569471e+00;
const qS3: f64 = -6.88283971605453293030e-01;
- const qS4: f64 = 7.70381505559019352791e-02;
+ const qS4: f64 = 7.70381505559019352791e-02;
const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
diff --git a/std/math/asin.zig b/std/math/asin.zig
index 9fa5a80ea5..2ee3aa6048 100644
--- a/std/math/asin.zig
+++ b/std/math/asin.zig
@@ -17,7 +17,7 @@ pub fn asin(x: var) @typeOf(x) {
}
fn r32(z: f32) f32 {
- const pS0 = 1.6666586697e-01;
+ const pS0 = 1.6666586697e-01;
const pS1 = -4.2743422091e-02;
const pS2 = -8.6563630030e-03;
const qS1 = -7.0662963390e-01;
@@ -37,9 +37,9 @@ fn asin32(x: f32) f32 {
if (ix >= 0x3F800000) {
// |x| >= 1
if (ix == 0x3F800000) {
- return x * pio2 + 0x1.0p-120; // asin(+-1) = +-pi/2 with inexact
+ return x * pio2 + 0x1.0p-120; // asin(+-1) = +-pi/2 with inexact
} else {
- return math.nan(f32); // asin(|x| > 1) is nan
+ return math.nan(f32); // asin(|x| > 1) is nan
}
}
@@ -66,16 +66,16 @@ fn asin32(x: f32) f32 {
}
fn r64(z: f64) f64 {
- const pS0: f64 = 1.66666666666666657415e-01;
+ const pS0: f64 = 1.66666666666666657415e-01;
const pS1: f64 = -3.25565818622400915405e-01;
- const pS2: f64 = 2.01212532134862925881e-01;
+ const pS2: f64 = 2.01212532134862925881e-01;
const pS3: f64 = -4.00555345006794114027e-02;
- const pS4: f64 = 7.91534994289814532176e-04;
- const pS5: f64 = 3.47933107596021167570e-05;
+ const pS4: f64 = 7.91534994289814532176e-04;
+ const pS5: f64 = 3.47933107596021167570e-05;
const qS1: f64 = -2.40339491173441421878e+00;
- const qS2: f64 = 2.02094576023350569471e+00;
+ const qS2: f64 = 2.02094576023350569471e+00;
const qS3: f64 = -6.88283971605453293030e-01;
- const qS4: f64 = 7.70381505559019352791e-02;
+ const qS4: f64 = 7.70381505559019352791e-02;
const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
diff --git a/std/math/atan2.zig b/std/math/atan2.zig
index 37c520da46..0892f0b438 100644
--- a/std/math/atan2.zig
+++ b/std/math/atan2.zig
@@ -31,7 +31,7 @@ pub fn atan2(comptime T: type, x: T, y: T) T {
}
fn atan2_32(y: f32, x: f32) f32 {
- const pi: f32 = 3.1415927410e+00;
+ const pi: f32 = 3.1415927410e+00;
const pi_lo: f32 = -8.7422776573e-08;
if (math.isNan(x) or math.isNan(y)) {
@@ -53,9 +53,10 @@ fn atan2_32(y: f32, x: f32) f32 {
if (iy == 0) {
switch (m) {
- 0, 1 => return y, // atan(+-0, +...)
- 2 => return pi, // atan(+0, -...)
- 3 => return -pi, // atan(-0, -...)
+ 0,
+ 1 => return y, // atan(+-0, +...)
+ 2 => return pi, // atan(+0, -...)
+ 3 => return -pi, // atan(-0, -...)
else => unreachable,
}
}
@@ -71,18 +72,18 @@ fn atan2_32(y: f32, x: f32) f32 {
if (ix == 0x7F800000) {
if (iy == 0x7F800000) {
switch (m) {
- 0 => return pi / 4, // atan(+inf, +inf)
- 1 => return -pi / 4, // atan(-inf, +inf)
- 2 => return 3*pi / 4, // atan(+inf, -inf)
- 3 => return -3*pi / 4, // atan(-inf, -inf)
+ 0 => return pi / 4, // atan(+inf, +inf)
+ 1 => return -pi / 4, // atan(-inf, +inf)
+ 2 => return 3 * pi / 4, // atan(+inf, -inf)
+ 3 => return -3 * pi / 4, // atan(-inf, -inf)
else => unreachable,
}
} else {
switch (m) {
- 0 => return 0.0, // atan(+..., +inf)
- 1 => return -0.0, // atan(-..., +inf)
- 2 => return pi, // atan(+..., -inf)
- 3 => return -pi, // atan(-...f, -inf)
+ 0 => return 0.0, // atan(+..., +inf)
+ 1 => return -0.0, // atan(-..., +inf)
+ 2 => return pi, // atan(+..., -inf)
+ 3 => return -pi, // atan(-...f, -inf)
else => unreachable,
}
}
@@ -107,16 +108,16 @@ fn atan2_32(y: f32, x: f32) f32 {
};
switch (m) {
- 0 => return z, // atan(+, +)
- 1 => return -z, // atan(-, +)
- 2 => return pi - (z - pi_lo), // atan(+, -)
- 3 => return (z - pi_lo) - pi, // atan(-, -)
+ 0 => return z, // atan(+, +)
+ 1 => return -z, // atan(-, +)
+ 2 => return pi - (z - pi_lo), // atan(+, -)
+ 3 => return (z - pi_lo) - pi, // atan(-, -)
else => unreachable,
}
}
fn atan2_64(y: f64, x: f64) f64 {
- const pi: f64 = 3.1415926535897931160E+00;
+ const pi: f64 = 3.1415926535897931160E+00;
const pi_lo: f64 = 1.2246467991473531772E-16;
if (math.isNan(x) or math.isNan(y)) {
@@ -143,9 +144,10 @@ fn atan2_64(y: f64, x: f64) f64 {
if (iy | ly == 0) {
switch (m) {
- 0, 1 => return y, // atan(+-0, +...)
- 2 => return pi, // atan(+0, -...)
- 3 => return -pi, // atan(-0, -...)
+ 0,
+ 1 => return y, // atan(+-0, +...)
+ 2 => return pi, // atan(+0, -...)
+ 3 => return -pi, // atan(-0, -...)
else => unreachable,
}
}
@@ -161,18 +163,18 @@ fn atan2_64(y: f64, x: f64) f64 {
if (ix == 0x7FF00000) {
if (iy == 0x7FF00000) {
switch (m) {
- 0 => return pi / 4, // atan(+inf, +inf)
- 1 => return -pi / 4, // atan(-inf, +inf)
- 2 => return 3*pi / 4, // atan(+inf, -inf)
- 3 => return -3*pi / 4, // atan(-inf, -inf)
+ 0 => return pi / 4, // atan(+inf, +inf)
+ 1 => return -pi / 4, // atan(-inf, +inf)
+ 2 => return 3 * pi / 4, // atan(+inf, -inf)
+ 3 => return -3 * pi / 4, // atan(-inf, -inf)
else => unreachable,
}
} else {
switch (m) {
- 0 => return 0.0, // atan(+..., +inf)
- 1 => return -0.0, // atan(-..., +inf)
- 2 => return pi, // atan(+..., -inf)
- 3 => return -pi, // atan(-...f, -inf)
+ 0 => return 0.0, // atan(+..., +inf)
+ 1 => return -0.0, // atan(-..., +inf)
+ 2 => return pi, // atan(+..., -inf)
+ 3 => return -pi, // atan(-...f, -inf)
else => unreachable,
}
}
@@ -197,10 +199,10 @@ fn atan2_64(y: f64, x: f64) f64 {
};
switch (m) {
- 0 => return z, // atan(+, +)
- 1 => return -z, // atan(-, +)
- 2 => return pi - (z - pi_lo), // atan(+, -)
- 3 => return (z - pi_lo) - pi, // atan(-, -)
+ 0 => return z, // atan(+, +)
+ 1 => return -z, // atan(-, +)
+ 2 => return pi - (z - pi_lo), // atan(+, -)
+ 3 => return (z - pi_lo) - pi, // atan(-, -)
else => unreachable,
}
}
diff --git a/std/math/cbrt.zig b/std/math/cbrt.zig
index a265392ff7..cd3b71ca8a 100644
--- a/std/math/cbrt.zig
+++ b/std/math/cbrt.zig
@@ -58,15 +58,15 @@ fn cbrt32(x: f32) f32 {
}
fn cbrt64(x: f64) f64 {
- const B1: u32 = 715094163; // (1023 - 1023 / 3 - 0.03306235651 * 2^20
- const B2: u32 = 696219795; // (1023 - 1023 / 3 - 54 / 3 - 0.03306235651 * 2^20
+ const B1: u32 = 715094163; // (1023 - 1023 / 3 - 0.03306235651 * 2^20
+ const B2: u32 = 696219795; // (1023 - 1023 / 3 - 54 / 3 - 0.03306235651 * 2^20
// |1 / cbrt(x) - p(x)| < 2^(23.5)
- const P0: f64 = 1.87595182427177009643;
+ const P0: f64 = 1.87595182427177009643;
const P1: f64 = -1.88497979543377169875;
- const P2: f64 = 1.621429720105354466140;
+ const P2: f64 = 1.621429720105354466140;
const P3: f64 = -0.758397934778766047437;
- const P4: f64 = 0.145996192886612446982;
+ const P4: f64 = 0.145996192886612446982;
var u = @bitCast(u64, x);
var hx = u32(u >> 32) & 0x7FFFFFFF;
diff --git a/std/math/ceil.zig b/std/math/ceil.zig
index 5bdb84ca00..a189bb66d2 100644
--- a/std/math/ceil.zig
+++ b/std/math/ceil.zig
@@ -56,7 +56,7 @@ fn ceil64(x: f64) f64 {
const e = (u >> 52) & 0x7FF;
var y: f64 = undefined;
- if (e >= 0x3FF+52 or x == 0) {
+ if (e >= 0x3FF + 52 or x == 0) {
return x;
}
@@ -68,7 +68,7 @@ fn ceil64(x: f64) f64 {
y = x + math.f64_toint - math.f64_toint - x;
}
- if (e <= 0x3FF-1) {
+ if (e <= 0x3FF - 1) {
math.forceEval(y);
if (u >> 63 != 0) {
return -0.0;
diff --git a/std/math/cos.zig b/std/math/cos.zig
index bb405b0d10..5e5ec4f1cb 100644
--- a/std/math/cos.zig
+++ b/std/math/cos.zig
@@ -18,20 +18,20 @@ pub fn cos(x: var) @typeOf(x) {
}
// sin polynomial coefficients
-const S0 = 1.58962301576546568060E-10;
+const S0 = 1.58962301576546568060E-10;
const S1 = -2.50507477628578072866E-8;
-const S2 = 2.75573136213857245213E-6;
+const S2 = 2.75573136213857245213E-6;
const S3 = -1.98412698295895385996E-4;
-const S4 = 8.33333333332211858878E-3;
+const S4 = 8.33333333332211858878E-3;
const S5 = -1.66666666666666307295E-1;
// cos polynomial coeffiecients
const C0 = -1.13585365213876817300E-11;
-const C1 = 2.08757008419747316778E-9;
+const C1 = 2.08757008419747316778E-9;
const C2 = -2.75573141792967388112E-7;
-const C3 = 2.48015872888517045348E-5;
+const C3 = 2.48015872888517045348E-5;
const C4 = -1.38888888888730564116E-3;
-const C5 = 4.16666666666665929218E-2;
+const C5 = 4.16666666666665929218E-2;
// NOTE: This is taken from the go stdlib. The musl implementation is much more complex.
//
diff --git a/std/math/floor.zig b/std/math/floor.zig
index 1b8e2dfeed..7d5364787f 100644
--- a/std/math/floor.zig
+++ b/std/math/floor.zig
@@ -57,7 +57,7 @@ fn floor64(x: f64) f64 {
const e = (u >> 52) & 0x7FF;
var y: f64 = undefined;
- if (e >= 0x3FF+52 or x == 0) {
+ if (e >= 0x3FF + 52 or x == 0) {
return x;
}
@@ -69,7 +69,7 @@ fn floor64(x: f64) f64 {
y = x + math.f64_toint - math.f64_toint - x;
}
- if (e <= 0x3FF-1) {
+ if (e <= 0x3FF - 1) {
math.forceEval(y);
if (u >> 63 != 0) {
return -1.0;
diff --git a/std/math/fma.zig b/std/math/fma.zig
index e8d146db34..3e9214f35b 100644
--- a/std/math/fma.zig
+++ b/std/math/fma.zig
@@ -5,7 +5,7 @@ const assert = std.debug.assert;
pub fn fma(comptime T: type, x: T, y: T, z: T) T {
return switch (T) {
f32 => fma32(x, y, z),
- f64 => fma64(x, y ,z),
+ f64 => fma64(x, y, z),
else => @compileError("fma not implemented for " ++ @typeName(T)),
};
}
@@ -71,7 +71,10 @@ fn fma64(x: f64, y: f64, z: f64) f64 {
}
}
-const dd = struct { hi: f64, lo: f64, };
+const dd = struct {
+ hi: f64,
+ lo: f64,
+};
fn dd_add(a: f64, b: f64) dd {
var ret: dd = undefined;
diff --git a/std/math/hypot.zig b/std/math/hypot.zig
index 06427d0865..fe0de3a1ea 100644
--- a/std/math/hypot.zig
+++ b/std/math/hypot.zig
@@ -39,11 +39,11 @@ fn hypot32(x: f32, y: f32) f32 {
}
var z: f32 = 1.0;
- if (ux >= (0x7F+60) << 23) {
+ if (ux >= (0x7F + 60) << 23) {
z = 0x1.0p90;
xx *= 0x1.0p-90;
yy *= 0x1.0p-90;
- } else if (uy < (0x7F-60) << 23) {
+ } else if (uy < (0x7F - 60) << 23) {
z = 0x1.0p-90;
xx *= 0x1.0p-90;
yy *= 0x1.0p-90;
@@ -57,8 +57,8 @@ fn sq(hi: &f64, lo: &f64, x: f64) void {
const xc = x * split;
const xh = x - xc + xc;
const xl = x - xh;
- *hi = x * x;
- *lo = xh * xh - *hi + 2 * xh * xl + xl * xl;
+ hi.* = x * x;
+ lo.* = xh * xh - hi.* + 2 * xh * xl + xl * xl;
}
fn hypot64(x: f64, y: f64) f64 {
diff --git a/std/math/ln.zig b/std/math/ln.zig
index d09494b998..263e5955cb 100644
--- a/std/math/ln.zig
+++ b/std/math/ln.zig
@@ -120,11 +120,9 @@ pub fn ln_64(x_: f64) f64 {
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, ix) >> 32);
- }
- else if (hx >= 0x7FF00000) {
+ } else if (hx >= 0x7FF00000) {
return x;
- }
- else if (hx == 0x3FF00000 and ix << 32 == 0) {
+ } else if (hx == 0x3FF00000 and ix << 32 == 0) {
return 0;
}
diff --git a/std/math/log10.zig b/std/math/log10.zig
index aa74caa901..d9fa1dcb02 100644
--- a/std/math/log10.zig
+++ b/std/math/log10.zig
@@ -35,10 +35,10 @@ pub fn log10(x: var) @typeOf(x) {
}
pub fn log10_32(x_: f32) f32 {
- const ivln10hi: f32 = 4.3432617188e-01;
- const ivln10lo: f32 = -3.1689971365e-05;
- const log10_2hi: f32 = 3.0102920532e-01;
- const log10_2lo: f32 = 7.9034151668e-07;
+ const ivln10hi: f32 = 4.3432617188e-01;
+ const ivln10lo: f32 = -3.1689971365e-05;
+ const log10_2hi: f32 = 3.0102920532e-01;
+ const log10_2lo: f32 = 7.9034151668e-07;
const Lg1: f32 = 0xaaaaaa.0p-24;
const Lg2: f32 = 0xccce13.0p-25;
const Lg3: f32 = 0x91e9ee.0p-25;
@@ -95,8 +95,8 @@ pub fn log10_32(x_: f32) f32 {
}
pub fn log10_64(x_: f64) f64 {
- const ivln10hi: f64 = 4.34294481878168880939e-01;
- const ivln10lo: f64 = 2.50829467116452752298e-11;
+ const ivln10hi: f64 = 4.34294481878168880939e-01;
+ const ivln10lo: f64 = 2.50829467116452752298e-11;
const log10_2hi: f64 = 3.01029995663611771306e-01;
const log10_2lo: f64 = 3.69423907715893078616e-13;
const Lg1: f64 = 6.666666666666735130e-01;
@@ -126,11 +126,9 @@ pub fn log10_64(x_: f64) f64 {
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, x) >> 32);
- }
- else if (hx >= 0x7FF00000) {
+ } else if (hx >= 0x7FF00000) {
return x;
- }
- else if (hx == 0x3FF00000 and ix << 32 == 0) {
+ } else if (hx == 0x3FF00000 and ix << 32 == 0) {
return 0;
}
diff --git a/std/math/log2.zig b/std/math/log2.zig
index d5bbe385c2..22cc8082b3 100644
--- a/std/math/log2.zig
+++ b/std/math/log2.zig
@@ -27,7 +27,10 @@ pub fn log2(x: var) @typeOf(x) {
TypeId.IntLiteral => comptime {
var result = 0;
var x_shifted = x;
- while (b: {x_shifted >>= 1; break :b x_shifted != 0;}) : (result += 1) {}
+ while (b: {
+ x_shifted >>= 1;
+ break :b x_shifted != 0;
+ }) : (result += 1) {}
return result;
},
TypeId.Int => {
@@ -38,7 +41,7 @@ pub fn log2(x: var) @typeOf(x) {
}
pub fn log2_32(x_: f32) f32 {
- const ivln2hi: f32 = 1.4428710938e+00;
+ const ivln2hi: f32 = 1.4428710938e+00;
const ivln2lo: f32 = -1.7605285393e-04;
const Lg1: f32 = 0xaaaaaa.0p-24;
const Lg2: f32 = 0xccce13.0p-25;
diff --git a/std/math/round.zig b/std/math/round.zig
index c16190da21..c8d9eb4fd4 100644
--- a/std/math/round.zig
+++ b/std/math/round.zig
@@ -24,13 +24,13 @@ fn round32(x_: f32) f32 {
const e = (u >> 23) & 0xFF;
var y: f32 = undefined;
- if (e >= 0x7F+23) {
+ if (e >= 0x7F + 23) {
return x;
}
if (u >> 31 != 0) {
x = -x;
}
- if (e < 0x7F-1) {
+ if (e < 0x7F - 1) {
math.forceEval(x + math.f32_toint);
return 0 * @bitCast(f32, u);
}
@@ -61,13 +61,13 @@ fn round64(x_: f64) f64 {
const e = (u >> 52) & 0x7FF;
var y: f64 = undefined;
- if (e >= 0x3FF+52) {
+ if (e >= 0x3FF + 52) {
return x;
}
if (u >> 63 != 0) {
x = -x;
}
- if (e < 0x3ff-1) {
+ if (e < 0x3ff - 1) {
math.forceEval(x + math.f64_toint);
return 0 * @bitCast(f64, u);
}
diff --git a/std/math/sin.zig b/std/math/sin.zig
index 5dd869545b..21c324e444 100644
--- a/std/math/sin.zig
+++ b/std/math/sin.zig
@@ -19,20 +19,20 @@ pub fn sin(x: var) @typeOf(x) {
}
// sin polynomial coefficients
-const S0 = 1.58962301576546568060E-10;
+const S0 = 1.58962301576546568060E-10;
const S1 = -2.50507477628578072866E-8;
-const S2 = 2.75573136213857245213E-6;
+const S2 = 2.75573136213857245213E-6;
const S3 = -1.98412698295895385996E-4;
-const S4 = 8.33333333332211858878E-3;
+const S4 = 8.33333333332211858878E-3;
const S5 = -1.66666666666666307295E-1;
// cos polynomial coeffiecients
const C0 = -1.13585365213876817300E-11;
-const C1 = 2.08757008419747316778E-9;
+const C1 = 2.08757008419747316778E-9;
const C2 = -2.75573141792967388112E-7;
-const C3 = 2.48015872888517045348E-5;
+const C3 = 2.48015872888517045348E-5;
const C4 = -1.38888888888730564116E-3;
-const C5 = 4.16666666666665929218E-2;
+const C5 = 4.16666666666665929218E-2;
// NOTE: This is taken from the go stdlib. The musl implementation is much more complex.
//
diff --git a/std/math/tan.zig b/std/math/tan.zig
index 11428b6e8b..f578cf8156 100644
--- a/std/math/tan.zig
+++ b/std/math/tan.zig
@@ -19,12 +19,12 @@ pub fn tan(x: var) @typeOf(x) {
}
const Tp0 = -1.30936939181383777646E4;
-const Tp1 = 1.15351664838587416140E6;
+const Tp1 = 1.15351664838587416140E6;
const Tp2 = -1.79565251976484877988E7;
-const Tq1 = 1.36812963470692954678E4;
+const Tq1 = 1.36812963470692954678E4;
const Tq2 = -1.32089234440210967447E6;
-const Tq3 = 2.50083801823357915839E7;
+const Tq3 = 2.50083801823357915839E7;
const Tq4 = -5.38695755929454629881E7;
// NOTE: This is taken from the go stdlib. The musl implementation is much more complex.
diff --git a/std/net.zig b/std/net.zig
index 8e1b8d97b2..b1e291ab92 100644
--- a/std/net.zig
+++ b/std/net.zig
@@ -19,37 +19,29 @@ pub const Address = struct {
os_addr: OsAddress,
pub fn initIp4(ip4: u32, port: u16) Address {
- return Address {
- .os_addr = posix.sockaddr {
- .in = posix.sockaddr_in {
- .family = posix.AF_INET,
- .port = std.mem.endianSwapIfLe(u16, port),
- .addr = ip4,
- .zero = []u8{0} ** 8,
- },
- },
- };
+ return Address{ .os_addr = posix.sockaddr{ .in = posix.sockaddr_in{
+ .family = posix.AF_INET,
+ .port = std.mem.endianSwapIfLe(u16, port),
+ .addr = ip4,
+ .zero = []u8{0} ** 8,
+ } } };
}
pub fn initIp6(ip6: &const Ip6Addr, port: u16) Address {
- return Address {
+ return Address{
.family = posix.AF_INET6,
- .os_addr = posix.sockaddr {
- .in6 = posix.sockaddr_in6 {
- .family = posix.AF_INET6,
- .port = std.mem.endianSwapIfLe(u16, port),
- .flowinfo = 0,
- .addr = ip6.addr,
- .scope_id = ip6.scope_id,
- },
- },
+ .os_addr = posix.sockaddr{ .in6 = posix.sockaddr_in6{
+ .family = posix.AF_INET6,
+ .port = std.mem.endianSwapIfLe(u16, port),
+ .flowinfo = 0,
+ .addr = ip6.addr,
+ .scope_id = ip6.scope_id,
+ } },
};
}
pub fn initPosix(addr: &const posix.sockaddr) Address {
- return Address {
- .os_addr = *addr,
- };
+ return Address{ .os_addr = addr.* };
}
pub fn format(self: &const Address, out_stream: var) !void {
@@ -98,7 +90,7 @@ pub fn parseIp4(buf: []const u8) !u32 {
}
} else {
return error.InvalidCharacter;
- }
+ }
}
if (index == 3 and saw_any_digits) {
out_ptr[index] = x;
diff --git a/std/os/child_process.zig b/std/os/child_process.zig
index 8bb8b2d7e7..6c0c21f64a 100644
--- a/std/os/child_process.zig
+++ b/std/os/child_process.zig
@@ -49,7 +49,7 @@ pub const ChildProcess = struct {
err_pipe: if (is_windows) void else [2]i32,
llnode: if (is_windows) void else LinkedList(&ChildProcess).Node,
- pub const SpawnError = error {
+ pub const SpawnError = error{
ProcessFdQuotaExceeded,
Unexpected,
NotDir,
@@ -88,7 +88,7 @@ pub const ChildProcess = struct {
const child = try allocator.create(ChildProcess);
errdefer allocator.destroy(child);
- *child = ChildProcess {
+ child.* = ChildProcess{
.allocator = allocator,
.argv = argv,
.pid = undefined,
@@ -99,8 +99,10 @@ pub const ChildProcess = struct {
.term = null,
.env_map = null,
.cwd = null,
- .uid = if (is_windows) {} else null,
- .gid = if (is_windows) {} else null,
+ .uid = if (is_windows) {} else
+ null,
+ .gid = if (is_windows) {} else
+ null,
.stdin = null,
.stdout = null,
.stderr = null,
@@ -193,9 +195,7 @@ pub const ChildProcess = struct {
/// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
/// If it succeeds, the caller owns result.stdout and result.stderr memory.
- pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8,
- env_map: ?&const BufMap, max_output_size: usize) !ExecResult
- {
+ pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8, env_map: ?&const BufMap, max_output_size: usize) !ExecResult {
const child = try ChildProcess.init(argv, allocator);
defer child.deinit();
@@ -218,7 +218,7 @@ pub const ChildProcess = struct {
try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);
try stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size);
- return ExecResult {
+ return ExecResult{
.term = try child.wait(),
.stdout = stdout.toOwnedSlice(),
.stderr = stderr.toOwnedSlice(),
@@ -255,9 +255,9 @@ pub const ChildProcess = struct {
self.term = (SpawnError!Term)(x: {
var exit_code: windows.DWORD = undefined;
if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
- break :x Term { .Unknown = 0 };
+ break :x Term{ .Unknown = 0 };
} else {
- break :x Term { .Exited = @bitCast(i32, exit_code)};
+ break :x Term{ .Exited = @bitCast(i32, exit_code) };
}
});
@@ -288,9 +288,18 @@ pub const ChildProcess = struct {
}
fn cleanupStreams(self: &ChildProcess) void {
- if (self.stdin) |*stdin| { stdin.close(); self.stdin = null; }
- if (self.stdout) |*stdout| { stdout.close(); self.stdout = null; }
- if (self.stderr) |*stderr| { stderr.close(); self.stderr = null; }
+ if (self.stdin) |*stdin| {
+ stdin.close();
+ self.stdin = null;
+ }
+ if (self.stdout) |*stdout| {
+ stdout.close();
+ self.stdout = null;
+ }
+ if (self.stderr) |*stderr| {
+ stderr.close();
+ self.stderr = null;
+ }
}
fn cleanupAfterWait(self: &ChildProcess, status: i32) !Term {
@@ -317,25 +326,30 @@ pub const ChildProcess = struct {
fn statusToTerm(status: i32) Term {
return if (posix.WIFEXITED(status))
- Term { .Exited = posix.WEXITSTATUS(status) }
+ Term{ .Exited = posix.WEXITSTATUS(status) }
else if (posix.WIFSIGNALED(status))
- Term { .Signal = posix.WTERMSIG(status) }
+ Term{ .Signal = posix.WTERMSIG(status) }
else if (posix.WIFSTOPPED(status))
- Term { .Stopped = posix.WSTOPSIG(status) }
+ Term{ .Stopped = posix.WSTOPSIG(status) }
else
- Term { .Unknown = status }
- ;
+ Term{ .Unknown = status };
}
fn spawnPosix(self: &ChildProcess) !void {
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
- errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ destroyPipe(stdin_pipe);
+ };
const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined;
- errdefer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
+ errdefer if (self.stdout_behavior == StdIo.Pipe) {
+ destroyPipe(stdout_pipe);
+ };
const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined;
- errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
+ errdefer if (self.stderr_behavior == StdIo.Pipe) {
+ destroyPipe(stderr_pipe);
+ };
const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
const dev_null_fd = if (any_ignore) blk: {
@@ -346,7 +360,9 @@ pub const ChildProcess = struct {
} else blk: {
break :blk undefined;
};
- defer { if (any_ignore) os.close(dev_null_fd); }
+ defer {
+ if (any_ignore) os.close(dev_null_fd);
+ }
var env_map_owned: BufMap = undefined;
var we_own_env_map: bool = undefined;
@@ -358,7 +374,9 @@ pub const ChildProcess = struct {
env_map_owned = try os.getEnvMap(self.allocator);
break :x &env_map_owned;
};
- defer { if (we_own_env_map) env_map_owned.deinit(); }
+ defer {
+ if (we_own_env_map) env_map_owned.deinit();
+ }
// This pipe is used to communicate errors between the time of fork
// and execve from the child process to the parent process.
@@ -369,23 +387,21 @@ pub const ChildProcess = struct {
const pid_err = posix.getErrno(pid_result);
if (pid_err > 0) {
return switch (pid_err) {
- posix.EAGAIN, posix.ENOMEM, posix.ENOSYS => error.SystemResources,
+ posix.EAGAIN,
+ posix.ENOMEM,
+ posix.ENOSYS => error.SystemResources,
else => os.unexpectedErrorPosix(pid_err),
};
}
if (pid_result == 0) {
// we are the child
- setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch
- |err| forkChildErrReport(err_pipe[1], err);
- setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch
- |err| forkChildErrReport(err_pipe[1], err);
- setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch
- |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
if (self.cwd) |cwd| {
- os.changeCurDir(self.allocator, cwd) catch
- |err| forkChildErrReport(err_pipe[1], err);
+ os.changeCurDir(self.allocator, cwd) catch |err| forkChildErrReport(err_pipe[1], err);
}
if (self.gid) |gid| {
@@ -396,8 +412,7 @@ pub const ChildProcess = struct {
os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
}
- os.posixExecve(self.argv, env_map, self.allocator) catch
- |err| forkChildErrReport(err_pipe[1], err);
+ os.posixExecve(self.argv, env_map, self.allocator) catch |err| forkChildErrReport(err_pipe[1], err);
}
// we are the parent
@@ -423,37 +438,41 @@ pub const ChildProcess = struct {
self.llnode = LinkedList(&ChildProcess).Node.init(self);
self.term = null;
- if (self.stdin_behavior == StdIo.Pipe) { os.close(stdin_pipe[0]); }
- if (self.stdout_behavior == StdIo.Pipe) { os.close(stdout_pipe[1]); }
- if (self.stderr_behavior == StdIo.Pipe) { os.close(stderr_pipe[1]); }
+ if (self.stdin_behavior == StdIo.Pipe) {
+ os.close(stdin_pipe[0]);
+ }
+ if (self.stdout_behavior == StdIo.Pipe) {
+ os.close(stdout_pipe[1]);
+ }
+ if (self.stderr_behavior == StdIo.Pipe) {
+ os.close(stderr_pipe[1]);
+ }
}
fn spawnWindows(self: &ChildProcess) !void {
- const saAttr = windows.SECURITY_ATTRIBUTES {
+ const saAttr = windows.SECURITY_ATTRIBUTES{
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
.bInheritHandle = windows.TRUE,
.lpSecurityDescriptor = null,
};
- const any_ignore = (self.stdin_behavior == StdIo.Ignore or
- self.stdout_behavior == StdIo.Ignore or
- self.stderr_behavior == StdIo.Ignore);
+ const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
const nul_handle = if (any_ignore) blk: {
const nul_file_path = "NUL";
var fixed_buffer_mem: [nul_file_path.len + 1]u8 = undefined;
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
- break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ,
- windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
+ break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
} else blk: {
break :blk undefined;
};
- defer { if (any_ignore) os.close(nul_handle); }
+ defer {
+ if (any_ignore) os.close(nul_handle);
+ }
if (any_ignore) {
try windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0);
}
-
var g_hChildStd_IN_Rd: ?windows.HANDLE = null;
var g_hChildStd_IN_Wr: ?windows.HANDLE = null;
switch (self.stdin_behavior) {
@@ -470,7 +489,9 @@ pub const ChildProcess = struct {
g_hChildStd_IN_Rd = null;
},
}
- errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr);
+ };
var g_hChildStd_OUT_Rd: ?windows.HANDLE = null;
var g_hChildStd_OUT_Wr: ?windows.HANDLE = null;
@@ -488,7 +509,9 @@ pub const ChildProcess = struct {
g_hChildStd_OUT_Wr = null;
},
}
- errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr);
+ };
var g_hChildStd_ERR_Rd: ?windows.HANDLE = null;
var g_hChildStd_ERR_Wr: ?windows.HANDLE = null;
@@ -506,12 +529,14 @@ pub const ChildProcess = struct {
g_hChildStd_ERR_Wr = null;
},
}
- errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr);
+ };
const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv);
defer self.allocator.free(cmd_line);
- var siStartInfo = windows.STARTUPINFOA {
+ var siStartInfo = windows.STARTUPINFOA{
.cb = @sizeOf(windows.STARTUPINFOA),
.hStdError = g_hChildStd_ERR_Wr,
.hStdOutput = g_hChildStd_OUT_Wr,
@@ -534,19 +559,11 @@ pub const ChildProcess = struct {
};
var piProcInfo: windows.PROCESS_INFORMATION = undefined;
- const cwd_slice = if (self.cwd) |cwd|
- try cstr.addNullByte(self.allocator, cwd)
- else
- null
- ;
+ const cwd_slice = if (self.cwd) |cwd| try cstr.addNullByte(self.allocator, cwd) else null;
defer if (cwd_slice) |cwd| self.allocator.free(cwd);
const cwd_ptr = if (cwd_slice) |cwd| cwd.ptr else null;
- const maybe_envp_buf = if (self.env_map) |env_map|
- try os.createWindowsEnvBlock(self.allocator, env_map)
- else
- null
- ;
+ const maybe_envp_buf = if (self.env_map) |env_map| try os.createWindowsEnvBlock(self.allocator, env_map) else null;
defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf);
const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null;
@@ -563,11 +580,8 @@ pub const ChildProcess = struct {
};
defer self.allocator.free(app_name);
- windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr,
- &siStartInfo, &piProcInfo) catch |no_path_err|
- {
- if (no_path_err != error.FileNotFound)
- return no_path_err;
+ windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| {
+ if (no_path_err != error.FileNotFound) return no_path_err;
const PATH = try os.getEnvVarOwned(self.allocator, "PATH");
defer self.allocator.free(PATH);
@@ -577,9 +591,7 @@ pub const ChildProcess = struct {
const joined_path = try os.path.join(self.allocator, search_path, app_name);
defer self.allocator.free(joined_path);
- if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr,
- &siStartInfo, &piProcInfo)) |_|
- {
+ if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, &siStartInfo, &piProcInfo)) |_| {
break;
} else |err| if (err == error.FileNotFound) {
continue;
@@ -609,9 +621,15 @@ pub const ChildProcess = struct {
self.thread_handle = piProcInfo.hThread;
self.term = null;
- if (self.stdin_behavior == StdIo.Pipe) { os.close(??g_hChildStd_IN_Rd); }
- if (self.stderr_behavior == StdIo.Pipe) { os.close(??g_hChildStd_ERR_Wr); }
- if (self.stdout_behavior == StdIo.Pipe) { os.close(??g_hChildStd_OUT_Wr); }
+ if (self.stdin_behavior == StdIo.Pipe) {
+ os.close(??g_hChildStd_IN_Rd);
+ }
+ if (self.stderr_behavior == StdIo.Pipe) {
+ os.close(??g_hChildStd_ERR_Wr);
+ }
+ if (self.stdout_behavior == StdIo.Pipe) {
+ os.close(??g_hChildStd_OUT_Wr);
+ }
}
fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) !void {
@@ -622,18 +640,14 @@ pub const ChildProcess = struct {
StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno),
}
}
-
};
-fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8,
- lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void
-{
- if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0,
- @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0)
- {
+fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8, lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void {
+ if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) {
const err = windows.GetLastError();
return switch (err) {
- windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
+ windows.ERROR.FILE_NOT_FOUND,
+ windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
windows.ERROR.INVALID_PARAMETER => unreachable,
windows.ERROR.INVALID_NAME => error.InvalidName,
else => os.unexpectedErrorWindows(err),
@@ -641,9 +655,6 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?
}
}
-
-
-
/// Caller must dealloc.
/// Guarantees a null byte at result[result.len].
fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8) ![]u8 {
@@ -651,8 +662,7 @@ fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8)
defer buf.deinit();
for (argv) |arg, arg_i| {
- if (arg_i != 0)
- try buf.appendByte(' ');
+ if (arg_i != 0) try buf.appendByte(' ');
if (mem.indexOfAny(u8, arg, " \t\n\"") == null) {
try buf.append(arg);
continue;
@@ -686,7 +696,6 @@ fn windowsDestroyPipe(rd: ?windows.HANDLE, wr: ?windows.HANDLE) void {
if (wr) |h| os.close(h);
}
-
// TODO: workaround for bug where the `const` from `&const` is dropped when the type is
// a namespace field lookup
const SECURITY_ATTRIBUTES = windows.SECURITY_ATTRIBUTES;
@@ -715,8 +724,8 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S
try windowsMakePipe(&rd_h, &wr_h, sattr);
errdefer windowsDestroyPipe(rd_h, wr_h);
try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0);
- *rd = rd_h;
- *wr = wr_h;
+ rd.* = rd_h;
+ wr.* = wr_h;
}
fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
@@ -725,8 +734,8 @@ fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const
try windowsMakePipe(&rd_h, &wr_h, sattr);
errdefer windowsDestroyPipe(rd_h, wr_h);
try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0);
- *rd = rd_h;
- *wr = wr_h;
+ rd.* = rd_h;
+ wr.* = wr_h;
}
fn makePipe() ![2]i32 {
@@ -734,7 +743,8 @@ fn makePipe() ![2]i32 {
const err = posix.getErrno(posix.pipe(&fds));
if (err > 0) {
return switch (err) {
- posix.EMFILE, posix.ENFILE => error.SystemResources,
+ posix.EMFILE,
+ posix.ENFILE => error.SystemResources,
else => os.unexpectedErrorPosix(err),
};
}
@@ -742,8 +752,8 @@ fn makePipe() ![2]i32 {
}
fn destroyPipe(pipe: &const [2]i32) void {
- os.close((*pipe)[0]);
- os.close((*pipe)[1]);
+ os.close((pipe.*)[0]);
+ os.close((pipe.*)[1]);
}
// Child of fork calls this to report an error to the fork parent.
diff --git a/std/segmented_list.zig b/std/segmented_list.zig
index 6c7c879919..e9abc7adf9 100644
--- a/std/segmented_list.zig
+++ b/std/segmented_list.zig
@@ -93,7 +93,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
/// Deinitialize with `deinit`
pub fn init(allocator: &Allocator) Self {
- return Self {
+ return Self{
.allocator = allocator,
.len = 0,
.prealloc_segment = undefined,
@@ -104,7 +104,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
pub fn deinit(self: &Self) void {
self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
self.allocator.free(self.dynamic_segments);
- *self = undefined;
+ self.* = undefined;
}
pub fn at(self: &Self, i: usize) &T {
@@ -118,7 +118,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
pub fn push(self: &Self, item: &const T) !void {
const new_item_ptr = try self.addOne();
- *new_item_ptr = *item;
+ new_item_ptr.* = item.*;
}
pub fn pushMany(self: &Self, items: []const T) !void {
@@ -128,11 +128,10 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
pub fn pop(self: &Self) ?T {
- if (self.len == 0)
- return null;
+ if (self.len == 0) return null;
const index = self.len - 1;
- const result = *self.uncheckedAt(index);
+ const result = self.uncheckedAt(index).*;
self.len = index;
return result;
}
@@ -245,8 +244,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
shelf_size: usize,
pub fn next(it: &Iterator) ?&T {
- if (it.index >= it.list.len)
- return null;
+ if (it.index >= it.list.len) return null;
if (it.index < prealloc_item_count) {
const ptr = &it.list.prealloc_segment[it.index];
it.index += 1;
@@ -270,12 +268,10 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
pub fn prev(it: &Iterator) ?&T {
- if (it.index == 0)
- return null;
+ if (it.index == 0) return null;
it.index -= 1;
- if (it.index < prealloc_item_count)
- return &it.list.prealloc_segment[it.index];
+ if (it.index < prealloc_item_count) return &it.list.prealloc_segment[it.index];
if (it.box_index == 0) {
it.shelf_index -= 1;
@@ -290,7 +286,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
};
pub fn iterator(self: &Self, start_index: usize) Iterator {
- var it = Iterator {
+ var it = Iterator{
.list = self,
.index = start_index,
.shelf_index = undefined,
@@ -324,25 +320,31 @@ fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
var list = SegmentedList(i32, prealloc).init(allocator);
defer list.deinit();
- {var i: usize = 0; while (i < 100) : (i += 1) {
- try list.push(i32(i + 1));
- assert(list.len == i + 1);
- }}
+ {
+ var i: usize = 0;
+ while (i < 100) : (i += 1) {
+ try list.push(i32(i + 1));
+ assert(list.len == i + 1);
+ }
+ }
- {var i: usize = 0; while (i < 100) : (i += 1) {
- assert(*list.at(i) == i32(i + 1));
- }}
+ {
+ var i: usize = 0;
+ while (i < 100) : (i += 1) {
+ assert(list.at(i).* == i32(i + 1));
+ }
+ }
{
var it = list.iterator(0);
var x: i32 = 0;
while (it.next()) |item| {
x += 1;
- assert(*item == x);
+ assert(item.* == x);
}
assert(x == 100);
while (it.prev()) |item| : (x -= 1) {
- assert(*item == x);
+ assert(item.* == x);
}
assert(x == 0);
}
@@ -350,14 +352,18 @@ fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
assert(??list.pop() == 100);
assert(list.len == 99);
- try list.pushMany([]i32 { 1, 2, 3 });
+ try list.pushMany([]i32{
+ 1,
+ 2,
+ 3,
+ });
assert(list.len == 102);
assert(??list.pop() == 3);
assert(??list.pop() == 2);
assert(??list.pop() == 1);
assert(list.len == 99);
- try list.pushMany([]const i32 {});
+ try list.pushMany([]const i32{});
assert(list.len == 99);
var i: i32 = 99;
diff --git a/std/sort.zig b/std/sort.zig
index 0f83df7bb4..4cc7ad503a 100644
--- a/std/sort.zig
+++ b/std/sort.zig
@@ -5,15 +5,18 @@ const math = std.math;
const builtin = @import("builtin");
/// Stable in-place sort. O(n) best case, O(pow(n, 2)) worst case. O(1) memory (no allocator required).
-pub fn insertionSort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) void {
- {var i: usize = 1; while (i < items.len) : (i += 1) {
- const x = items[i];
- var j: usize = i;
- while (j > 0 and lessThan(x, items[j - 1])) : (j -= 1) {
- items[j] = items[j - 1];
+pub fn insertionSort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) void {
+ {
+ var i: usize = 1;
+ while (i < items.len) : (i += 1) {
+ const x = items[i];
+ var j: usize = i;
+ while (j > 0 and lessThan(x, items[j - 1])) : (j -= 1) {
+ items[j] = items[j - 1];
+ }
+ items[j] = x;
}
- items[j] = x;
- }}
+ }
}
const Range = struct {
@@ -21,7 +24,10 @@ const Range = struct {
end: usize,
fn init(start: usize, end: usize) Range {
- return Range { .start = start, .end = end };
+ return Range{
+ .start = start,
+ .end = end,
+ };
}
fn length(self: &const Range) usize {
@@ -29,7 +35,6 @@ const Range = struct {
}
};
-
const Iterator = struct {
size: usize,
power_of_two: usize,
@@ -42,7 +47,7 @@ const Iterator = struct {
fn init(size2: usize, min_level: usize) Iterator {
const power_of_two = math.floorPowerOfTwo(usize, size2);
const denominator = power_of_two / min_level;
- return Iterator {
+ return Iterator{
.numerator = 0,
.decimal = 0,
.size = size2,
@@ -68,7 +73,10 @@ const Iterator = struct {
self.decimal += 1;
}
- return Range {.start = start, .end = self.decimal};
+ return Range{
+ .start = start,
+ .end = self.decimal,
+ };
}
fn finished(self: &Iterator) bool {
@@ -100,7 +108,7 @@ const Pull = struct {
/// Stable in-place sort. O(n) best case, O(n*log(n)) worst case and average case. O(1) memory (no allocator required).
/// Currently implemented as block sort.
-pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) void {
+pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) void {
// Implementation ported from https://github.com/BonzaiThePenguin/WikiSort/blob/master/WikiSort.c
var cache: [512]T = undefined;
@@ -123,7 +131,16 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// http://pages.ripco.net/~jgamble/nw.html
var iterator = Iterator.init(items.len, 4);
while (!iterator.finished()) {
- var order = []u8{0, 1, 2, 3, 4, 5, 6, 7};
+ var order = []u8{
+ 0,
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ };
const range = iterator.nextRange();
const sliced_items = items[range.start..];
@@ -149,56 +166,56 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
swap(T, sliced_items, lessThan, &order, 3, 5);
swap(T, sliced_items, lessThan, &order, 3, 4);
},
- 7 => {
- swap(T, sliced_items, lessThan, &order, 1, 2);
- swap(T, sliced_items, lessThan, &order, 3, 4);
- swap(T, sliced_items, lessThan, &order, 5, 6);
- swap(T, sliced_items, lessThan, &order, 0, 2);
- swap(T, sliced_items, lessThan, &order, 3, 5);
- swap(T, sliced_items, lessThan, &order, 4, 6);
- swap(T, sliced_items, lessThan, &order, 0, 1);
- swap(T, sliced_items, lessThan, &order, 4, 5);
- swap(T, sliced_items, lessThan, &order, 2, 6);
- swap(T, sliced_items, lessThan, &order, 0, 4);
- swap(T, sliced_items, lessThan, &order, 1, 5);
- swap(T, sliced_items, lessThan, &order, 0, 3);
- swap(T, sliced_items, lessThan, &order, 2, 5);
- swap(T, sliced_items, lessThan, &order, 1, 3);
- swap(T, sliced_items, lessThan, &order, 2, 4);
- swap(T, sliced_items, lessThan, &order, 2, 3);
- },
- 6 => {
- swap(T, sliced_items, lessThan, &order, 1, 2);
- swap(T, sliced_items, lessThan, &order, 4, 5);
- swap(T, sliced_items, lessThan, &order, 0, 2);
- swap(T, sliced_items, lessThan, &order, 3, 5);
- swap(T, sliced_items, lessThan, &order, 0, 1);
- swap(T, sliced_items, lessThan, &order, 3, 4);
- swap(T, sliced_items, lessThan, &order, 2, 5);
- swap(T, sliced_items, lessThan, &order, 0, 3);
- swap(T, sliced_items, lessThan, &order, 1, 4);
- swap(T, sliced_items, lessThan, &order, 2, 4);
- swap(T, sliced_items, lessThan, &order, 1, 3);
- swap(T, sliced_items, lessThan, &order, 2, 3);
- },
- 5 => {
- swap(T, sliced_items, lessThan, &order, 0, 1);
- swap(T, sliced_items, lessThan, &order, 3, 4);
- swap(T, sliced_items, lessThan, &order, 2, 4);
- swap(T, sliced_items, lessThan, &order, 2, 3);
- swap(T, sliced_items, lessThan, &order, 1, 4);
- swap(T, sliced_items, lessThan, &order, 0, 3);
- swap(T, sliced_items, lessThan, &order, 0, 2);
- swap(T, sliced_items, lessThan, &order, 1, 3);
- swap(T, sliced_items, lessThan, &order, 1, 2);
- },
- 4 => {
- swap(T, sliced_items, lessThan, &order, 0, 1);
- swap(T, sliced_items, lessThan, &order, 2, 3);
- swap(T, sliced_items, lessThan, &order, 0, 2);
- swap(T, sliced_items, lessThan, &order, 1, 3);
- swap(T, sliced_items, lessThan, &order, 1, 2);
- },
+ 7 => {
+ swap(T, sliced_items, lessThan, &order, 1, 2);
+ swap(T, sliced_items, lessThan, &order, 3, 4);
+ swap(T, sliced_items, lessThan, &order, 5, 6);
+ swap(T, sliced_items, lessThan, &order, 0, 2);
+ swap(T, sliced_items, lessThan, &order, 3, 5);
+ swap(T, sliced_items, lessThan, &order, 4, 6);
+ swap(T, sliced_items, lessThan, &order, 0, 1);
+ swap(T, sliced_items, lessThan, &order, 4, 5);
+ swap(T, sliced_items, lessThan, &order, 2, 6);
+ swap(T, sliced_items, lessThan, &order, 0, 4);
+ swap(T, sliced_items, lessThan, &order, 1, 5);
+ swap(T, sliced_items, lessThan, &order, 0, 3);
+ swap(T, sliced_items, lessThan, &order, 2, 5);
+ swap(T, sliced_items, lessThan, &order, 1, 3);
+ swap(T, sliced_items, lessThan, &order, 2, 4);
+ swap(T, sliced_items, lessThan, &order, 2, 3);
+ },
+ 6 => {
+ swap(T, sliced_items, lessThan, &order, 1, 2);
+ swap(T, sliced_items, lessThan, &order, 4, 5);
+ swap(T, sliced_items, lessThan, &order, 0, 2);
+ swap(T, sliced_items, lessThan, &order, 3, 5);
+ swap(T, sliced_items, lessThan, &order, 0, 1);
+ swap(T, sliced_items, lessThan, &order, 3, 4);
+ swap(T, sliced_items, lessThan, &order, 2, 5);
+ swap(T, sliced_items, lessThan, &order, 0, 3);
+ swap(T, sliced_items, lessThan, &order, 1, 4);
+ swap(T, sliced_items, lessThan, &order, 2, 4);
+ swap(T, sliced_items, lessThan, &order, 1, 3);
+ swap(T, sliced_items, lessThan, &order, 2, 3);
+ },
+ 5 => {
+ swap(T, sliced_items, lessThan, &order, 0, 1);
+ swap(T, sliced_items, lessThan, &order, 3, 4);
+ swap(T, sliced_items, lessThan, &order, 2, 4);
+ swap(T, sliced_items, lessThan, &order, 2, 3);
+ swap(T, sliced_items, lessThan, &order, 1, 4);
+ swap(T, sliced_items, lessThan, &order, 0, 3);
+ swap(T, sliced_items, lessThan, &order, 0, 2);
+ swap(T, sliced_items, lessThan, &order, 1, 3);
+ swap(T, sliced_items, lessThan, &order, 1, 2);
+ },
+ 4 => {
+ swap(T, sliced_items, lessThan, &order, 0, 1);
+ swap(T, sliced_items, lessThan, &order, 2, 3);
+ swap(T, sliced_items, lessThan, &order, 0, 2);
+ swap(T, sliced_items, lessThan, &order, 1, 3);
+ swap(T, sliced_items, lessThan, &order, 1, 2);
+ },
else => {},
}
}
@@ -273,7 +290,6 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// we merged two levels at the same time, so we're done with this level already
// (iterator.nextLevel() is called again at the bottom of this outer merge loop)
_ = iterator.nextLevel();
-
} else {
iterator.begin();
while (!iterator.finished()) {
@@ -303,7 +319,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// 8. redistribute the two internal buffers back into the items
var block_size: usize = math.sqrt(iterator.length());
- var buffer_size = iterator.length()/block_size + 1;
+ var buffer_size = iterator.length() / block_size + 1;
// as an optimization, we really only need to pull out the internal buffers once for each level of merges
// after that we can reuse the same buffers over and over, then redistribute it when we're finished with this level
@@ -316,8 +332,18 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
var start: usize = 0;
var pull_index: usize = 0;
var pull = []Pull{
- Pull {.from = 0, .to = 0, .count = 0, .range = Range.init(0, 0),},
- Pull {.from = 0, .to = 0, .count = 0, .range = Range.init(0, 0),},
+ Pull{
+ .from = 0,
+ .to = 0,
+ .count = 0,
+ .range = Range.init(0, 0),
+ },
+ Pull{
+ .from = 0,
+ .to = 0,
+ .count = 0,
+ .range = Range.init(0, 0),
+ },
};
var buffer1 = Range.init(0, 0);
@@ -355,7 +381,10 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// these values will be pulled out to the start of A
last = A.start;
count = 1;
- while (count < find) : ({last = index; count += 1;}) {
+ while (count < find) : ({
+ last = index;
+ count += 1;
+ }) {
index = findLastForward(T, items, items[last], Range.init(last + 1, A.end), lessThan, find - count);
if (index == A.end) break;
}
@@ -363,7 +392,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
if (count >= buffer_size) {
// keep track of the range within the items where we'll need to "pull out" these values to create the internal buffer
- pull[pull_index] = Pull {
+ pull[pull_index] = Pull{
.range = Range.init(A.start, B.end),
.count = count,
.from = index,
@@ -398,7 +427,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
} else if (pull_index == 0 and count > buffer1.length()) {
// keep track of the largest buffer we were able to find
buffer1 = Range.init(A.start, A.start + count);
- pull[pull_index] = Pull {
+ pull[pull_index] = Pull{
.range = Range.init(A.start, B.end),
.count = count,
.from = index,
@@ -410,7 +439,10 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// these values will be pulled out to the end of B
last = B.end - 1;
count = 1;
- while (count < find) : ({last = index - 1; count += 1;}) {
+ while (count < find) : ({
+ last = index - 1;
+ count += 1;
+ }) {
index = findFirstBackward(T, items, items[last], Range.init(B.start, last), lessThan, find - count);
if (index == B.start) break;
}
@@ -418,7 +450,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
if (count >= buffer_size) {
// keep track of the range within the items where we'll need to "pull out" these values to create the internal buffe
- pull[pull_index] = Pull {
+ pull[pull_index] = Pull{
.range = Range.init(A.start, B.end),
.count = count,
.from = index,
@@ -457,7 +489,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
} else if (pull_index == 0 and count > buffer1.length()) {
// keep track of the largest buffer we were able to find
buffer1 = Range.init(B.end - count, B.end);
- pull[pull_index] = Pull {
+ pull[pull_index] = Pull{
.range = Range.init(A.start, B.end),
.count = count,
.from = index,
@@ -496,7 +528,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// adjust block_size and buffer_size based on the values we were able to pull out
buffer_size = buffer1.length();
- block_size = iterator.length()/buffer_size + 1;
+ block_size = iterator.length() / buffer_size + 1;
// the first buffer NEEDS to be large enough to tag each of the evenly sized A blocks,
// so this was originally here to test the math for adjusting block_size above
@@ -547,7 +579,10 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// swap the first value of each A block with the value in buffer1
var indexA = buffer1.start;
index = firstA.end;
- while (index < blockA.end) : ({indexA += 1; index += block_size;}) {
+ while (index < blockA.end) : ({
+ indexA += 1;
+ index += block_size;
+ }) {
mem.swap(T, &items[indexA], &items[index]);
}
@@ -626,9 +661,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// if there are no more A blocks remaining, this step is finished!
blockA.start += block_size;
- if (blockA.length() == 0)
- break;
-
+ if (blockA.length() == 0) break;
} else if (blockB.length() < block_size) {
// move the last B block, which is unevenly sized, to before the remaining A blocks, by using a rotation
// the cache is disabled here since it might contain the contents of the previous A block
@@ -709,7 +742,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
}
// merge operation without a buffer
-fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const Range, lessThan: fn(&const T,&const T)bool) void {
+fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const Range, lessThan: fn(&const T, &const T) bool) void {
if (A_arg.length() == 0 or B_arg.length() == 0) return;
// this just repeatedly binary searches into B and rotates A into position.
@@ -730,8 +763,8 @@ fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const
// again, this is NOT a general-purpose solution – it only works well in this case!
// kind of like how the O(n^2) insertion sort is used in some places
- var A = *A_arg;
- var B = *B_arg;
+ var A = A_arg.*;
+ var B = B_arg.*;
while (true) {
// find the first place in B where the first item in A needs to be inserted
@@ -751,7 +784,7 @@ fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const
}
// merge operation using an internal buffer
-fn mergeInternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T,&const T)bool, buffer: &const Range) void {
+fn mergeInternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T, &const T) bool, buffer: &const Range) void {
// whenever we find a value to add to the final array, swap it with the value that's already in that spot
// when this algorithm is finished, 'buffer' will contain its original contents, but in a different order
var A_count: usize = 0;
@@ -787,9 +820,9 @@ fn blockSwap(comptime T: type, items: []T, start1: usize, start2: usize, block_s
// combine a linear search with a binary search to reduce the number of comparisons in situations
// where have some idea as to how many unique values there are and where the next value might be
-fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
+fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
- const skip = math.max(range.length()/unique, usize(1));
+ const skip = math.max(range.length() / unique, usize(1));
var index = range.start + skip;
while (lessThan(items[index - 1], value)) : (index += skip) {
@@ -801,9 +834,9 @@ fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const
return binaryFirst(T, items, value, Range.init(index - skip, index), lessThan);
}
-fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
+fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
- const skip = math.max(range.length()/unique, usize(1));
+ const skip = math.max(range.length() / unique, usize(1));
var index = range.end - skip;
while (index > range.start and !lessThan(items[index - 1], value)) : (index -= skip) {
@@ -815,9 +848,9 @@ fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &cons
return binaryFirst(T, items, value, Range.init(index, index + skip), lessThan);
}
-fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
+fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
- const skip = math.max(range.length()/unique, usize(1));
+ const skip = math.max(range.length() / unique, usize(1));
var index = range.start + skip;
while (!lessThan(value, items[index - 1])) : (index += skip) {
@@ -829,9 +862,9 @@ fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const
return binaryLast(T, items, value, Range.init(index - skip, index), lessThan);
}
-fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
+fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
- const skip = math.max(range.length()/unique, usize(1));
+ const skip = math.max(range.length() / unique, usize(1));
var index = range.end - skip;
while (index > range.start and lessThan(value, items[index - 1])) : (index -= skip) {
@@ -843,12 +876,12 @@ fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const
return binaryLast(T, items, value, Range.init(index, index + skip), lessThan);
}
-fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool) usize {
+fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool) usize {
var start = range.start;
var end = range.end - 1;
if (range.start >= range.end) return range.end;
while (start < end) {
- const mid = start + (end - start)/2;
+ const mid = start + (end - start) / 2;
if (lessThan(items[mid], value)) {
start = mid + 1;
} else {
@@ -861,12 +894,12 @@ fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Rang
return start;
}
-fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool) usize {
+fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool) usize {
var start = range.start;
var end = range.end - 1;
if (range.start >= range.end) return range.end;
while (start < end) {
- const mid = start + (end - start)/2;
+ const mid = start + (end - start) / 2;
if (!lessThan(value, items[mid])) {
start = mid + 1;
} else {
@@ -879,7 +912,7 @@ fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range
return start;
}
-fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, lessThan: fn(&const T,&const T)bool, into: []T) void {
+fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, lessThan: fn(&const T, &const T) bool, into: []T) void {
var A_index: usize = A.start;
var B_index: usize = B.start;
const A_last = A.end;
@@ -909,7 +942,7 @@ fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, less
}
}
-fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T,&const T)bool, cache: []T) void {
+fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T, &const T) bool, cache: []T) void {
// A fits into the cache, so use that instead of the internal buffer
var A_index: usize = 0;
var B_index: usize = B.start;
@@ -937,29 +970,27 @@ fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range,
mem.copy(T, items[insert_index..], cache[A_index..A_last]);
}
-fn swap(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool, order: &[8]u8, x: usize, y: usize) void {
- if (lessThan(items[y], items[x]) or
- ((*order)[x] > (*order)[y] and !lessThan(items[x], items[y])))
- {
+fn swap(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool, order: &[8]u8, x: usize, y: usize) void {
+ if (lessThan(items[y], items[x]) or ((order.*)[x] > (order.*)[y] and !lessThan(items[x], items[y]))) {
mem.swap(T, &items[x], &items[y]);
- mem.swap(u8, &(*order)[x], &(*order)[y]);
+ mem.swap(u8, &(order.*)[x], &(order.*)[y]);
}
}
fn i32asc(lhs: &const i32, rhs: &const i32) bool {
- return *lhs < *rhs;
+ return lhs.* < rhs.*;
}
fn i32desc(lhs: &const i32, rhs: &const i32) bool {
- return *rhs < *lhs;
+ return rhs.* < lhs.*;
}
fn u8asc(lhs: &const u8, rhs: &const u8) bool {
- return *lhs < *rhs;
+ return lhs.* < rhs.*;
}
fn u8desc(lhs: &const u8, rhs: &const u8) bool {
- return *rhs < *lhs;
+ return rhs.* < lhs.*;
}
test "stable sort" {
@@ -967,44 +998,125 @@ test "stable sort" {
comptime testStableSort();
}
fn testStableSort() void {
- var expected = []IdAndValue {
- IdAndValue{.id = 0, .value = 0},
- IdAndValue{.id = 1, .value = 0},
- IdAndValue{.id = 2, .value = 0},
- IdAndValue{.id = 0, .value = 1},
- IdAndValue{.id = 1, .value = 1},
- IdAndValue{.id = 2, .value = 1},
- IdAndValue{.id = 0, .value = 2},
- IdAndValue{.id = 1, .value = 2},
- IdAndValue{.id = 2, .value = 2},
+ var expected = []IdAndValue{
+ IdAndValue{
+ .id = 0,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 0,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 0,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 2,
+ },
};
- var cases = [][9]IdAndValue {
- []IdAndValue {
- IdAndValue{.id = 0, .value = 0},
- IdAndValue{.id = 0, .value = 1},
- IdAndValue{.id = 0, .value = 2},
- IdAndValue{.id = 1, .value = 0},
- IdAndValue{.id = 1, .value = 1},
- IdAndValue{.id = 1, .value = 2},
- IdAndValue{.id = 2, .value = 0},
- IdAndValue{.id = 2, .value = 1},
- IdAndValue{.id = 2, .value = 2},
+ var cases = [][9]IdAndValue{
+ []IdAndValue{
+ IdAndValue{
+ .id = 0,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 0,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 0,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 2,
+ },
},
- []IdAndValue {
- IdAndValue{.id = 0, .value = 2},
- IdAndValue{.id = 0, .value = 1},
- IdAndValue{.id = 0, .value = 0},
- IdAndValue{.id = 1, .value = 2},
- IdAndValue{.id = 1, .value = 1},
- IdAndValue{.id = 1, .value = 0},
- IdAndValue{.id = 2, .value = 2},
- IdAndValue{.id = 2, .value = 1},
- IdAndValue{.id = 2, .value = 0},
+ []IdAndValue{
+ IdAndValue{
+ .id = 0,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 0,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 0,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 1,
+ .value = 0,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 2,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 1,
+ },
+ IdAndValue{
+ .id = 2,
+ .value = 0,
+ },
},
};
for (cases) |*case| {
- insertionSort(IdAndValue, (*case)[0..], cmpByValue);
- for (*case) |item, i| {
+ insertionSort(IdAndValue, (case.*)[0..], cmpByValue);
+ for (case.*) |item, i| {
assert(item.id == expected[i].id);
assert(item.value == expected[i].value);
}
@@ -1019,13 +1131,31 @@ fn cmpByValue(a: &const IdAndValue, b: &const IdAndValue) bool {
}
test "std.sort" {
- const u8cases = [][]const []const u8 {
- [][]const u8{"", ""},
- [][]const u8{"a", "a"},
- [][]const u8{"az", "az"},
- [][]const u8{"za", "az"},
- [][]const u8{"asdf", "adfs"},
- [][]const u8{"one", "eno"},
+ const u8cases = [][]const []const u8{
+ [][]const u8{
+ "",
+ "",
+ },
+ [][]const u8{
+ "a",
+ "a",
+ },
+ [][]const u8{
+ "az",
+ "az",
+ },
+ [][]const u8{
+ "za",
+ "az",
+ },
+ [][]const u8{
+ "asdf",
+ "adfs",
+ },
+ [][]const u8{
+ "one",
+ "eno",
+ },
};
for (u8cases) |case| {
@@ -1036,13 +1166,59 @@ test "std.sort" {
assert(mem.eql(u8, slice, case[1]));
}
- const i32cases = [][]const []const i32 {
- [][]const i32{[]i32{}, []i32{}},
- [][]const i32{[]i32{1}, []i32{1}},
- [][]const i32{[]i32{0, 1}, []i32{0, 1}},
- [][]const i32{[]i32{1, 0}, []i32{0, 1}},
- [][]const i32{[]i32{1, -1, 0}, []i32{-1, 0, 1}},
- [][]const i32{[]i32{2, 1, 3}, []i32{1, 2, 3}},
+ const i32cases = [][]const []const i32{
+ [][]const i32{
+ []i32{},
+ []i32{},
+ },
+ [][]const i32{
+ []i32{1},
+ []i32{1},
+ },
+ [][]const i32{
+ []i32{
+ 0,
+ 1,
+ },
+ []i32{
+ 0,
+ 1,
+ },
+ },
+ [][]const i32{
+ []i32{
+ 1,
+ 0,
+ },
+ []i32{
+ 0,
+ 1,
+ },
+ },
+ [][]const i32{
+ []i32{
+ 1,
+ -1,
+ 0,
+ },
+ []i32{
+ -1,
+ 0,
+ 1,
+ },
+ },
+ [][]const i32{
+ []i32{
+ 2,
+ 1,
+ 3,
+ },
+ []i32{
+ 1,
+ 2,
+ 3,
+ },
+ },
};
for (i32cases) |case| {
@@ -1055,13 +1231,59 @@ test "std.sort" {
}
test "std.sort descending" {
- const rev_cases = [][]const []const i32 {
- [][]const i32{[]i32{}, []i32{}},
- [][]const i32{[]i32{1}, []i32{1}},
- [][]const i32{[]i32{0, 1}, []i32{1, 0}},
- [][]const i32{[]i32{1, 0}, []i32{1, 0}},
- [][]const i32{[]i32{1, -1, 0}, []i32{1, 0, -1}},
- [][]const i32{[]i32{2, 1, 3}, []i32{3, 2, 1}},
+ const rev_cases = [][]const []const i32{
+ [][]const i32{
+ []i32{},
+ []i32{},
+ },
+ [][]const i32{
+ []i32{1},
+ []i32{1},
+ },
+ [][]const i32{
+ []i32{
+ 0,
+ 1,
+ },
+ []i32{
+ 1,
+ 0,
+ },
+ },
+ [][]const i32{
+ []i32{
+ 1,
+ 0,
+ },
+ []i32{
+ 1,
+ 0,
+ },
+ },
+ [][]const i32{
+ []i32{
+ 1,
+ -1,
+ 0,
+ },
+ []i32{
+ 1,
+ 0,
+ -1,
+ },
+ },
+ [][]const i32{
+ []i32{
+ 2,
+ 1,
+ 3,
+ },
+ []i32{
+ 3,
+ 2,
+ 1,
+ },
+ },
};
for (rev_cases) |case| {
@@ -1074,10 +1296,22 @@ test "std.sort descending" {
}
test "another sort case" {
- var arr = []i32{ 5, 3, 1, 2, 4 };
+ var arr = []i32{
+ 5,
+ 3,
+ 1,
+ 2,
+ 4,
+ };
sort(i32, arr[0..], i32asc);
- assert(mem.eql(i32, arr, []i32{ 1, 2, 3, 4, 5 }));
+ assert(mem.eql(i32, arr, []i32{
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ }));
}
test "sort fuzz testing" {
@@ -1112,7 +1346,7 @@ fn fuzzTest(rng: &std.rand.Random) void {
}
}
-pub fn min(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) T {
+pub fn min(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) T {
var i: usize = 0;
var smallest = items[0];
for (items[1..]) |item| {
@@ -1123,7 +1357,7 @@ pub fn min(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const
return smallest;
}
-pub fn max(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) T {
+pub fn max(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) T {
var i: usize = 0;
var biggest = items[0];
for (items[1..]) |item| {
--
cgit v1.2.3
From 43085417bec447ab31f3454e180213f102885cc8 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Thu, 24 May 2018 21:27:44 -0400
Subject: update github.com/zig-lang to github.com/ziglang
---
README.md | 4 ++--
doc/langref.html.in | 8 ++++----
example/hello_world/hello_libc.zig | 2 +-
src/analyze.cpp | 4 ++--
src/codegen.cpp | 6 +++---
src/ir.cpp | 8 ++++----
std/atomic/queue.zig | 2 +-
std/event.zig | 8 ++++----
std/fmt/index.zig | 2 +-
std/mem.zig | 2 +-
std/os/index.zig | 6 +++---
std/os/test.zig | 2 +-
std/os/time.zig | 2 +-
std/special/compiler_rt/comparetf2.zig | 4 ++--
std/zig/ast.zig | 4 ++--
test/build_examples.zig | 2 +-
test/cases/coroutines.zig | 2 +-
test/cases/misc.zig | 2 +-
test/compare_output.zig | 4 ++--
19 files changed, 37 insertions(+), 37 deletions(-)
(limited to 'std/atomic')
diff --git a/README.md b/README.md
index cf4d8179c7..b5bf13f095 100644
--- a/README.md
+++ b/README.md
@@ -114,7 +114,7 @@ libc. Create demo games using Zig.
## Building
-[](https://travis-ci.org/zig-lang/zig)
+[](https://travis-ci.org/ziglang/zig)
[](https://ci.appveyor.com/project/andrewrk/zig-d3l86/branch/master)
### Stage 1: Build Zig from C++ Source Code
@@ -161,7 +161,7 @@ bin/zig build --build-file ../build.zig test
##### Windows
-See https://github.com/zig-lang/zig/wiki/Building-Zig-on-Windows
+See https://github.com/ziglang/zig/wiki/Building-Zig-on-Windows
### Stage 2: Build Self-Hosted Zig from Zig Source Code
diff --git a/doc/langref.html.in b/doc/langref.html.in
index c3c50b117b..d63c38d0fe 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -96,7 +96,7 @@
If you search for something specific in this documentation and do not find it,
- please file an issue or say something on IRC.
+ please file an issue or say something on IRC.
The code samples in this document are compiled and tested as part of the main test suite of Zig.
@@ -2827,7 +2827,7 @@ test "fn reflection" {
The number of unique error values across the entire compilation should determine the size of the error set type.
- However right now it is hard coded to be a u16. See #768.
+ However right now it is hard coded to be a u16. See #768.
You can implicitly cast an error from a subset to its superset:
@@ -5958,7 +5958,7 @@ pub fn main() void {
{#code_begin|exe#}
{#link_libc#}
const c = @cImport({
- // See https://github.com/zig-lang/zig/issues/515
+ // See https://github.com/ziglang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("stdio.h");
});
@@ -6301,7 +6301,7 @@ fn readU32Be() u32 {}
Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).
The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possbly the last line of the file).
- For some discussion on the rationale behind these design decisions, see issue #663
+ For some discussion on the rationale behind these design decisions, see issue #663
{#header_close#}
{#header_open|Grammar#}
Root = many(TopLevelItem) EOF
diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig
index 60123c6fd8..4a35e47b15 100644
--- a/example/hello_world/hello_libc.zig
+++ b/example/hello_world/hello_libc.zig
@@ -1,5 +1,5 @@
const c = @cImport({
- // See https://github.com/zig-lang/zig/issues/515
+ // See https://github.com/ziglang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("stdio.h");
@cInclude("string.h");
diff --git a/src/analyze.cpp b/src/analyze.cpp
index d6137a4286..c59fde8ef6 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -1007,7 +1007,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
if (fn_type_id->return_type != nullptr) {
ensure_complete_type(g, fn_type_id->return_type);
} else {
- zig_panic("TODO implement inferred return types https://github.com/zig-lang/zig/issues/447");
+ zig_panic("TODO implement inferred return types https://github.com/ziglang/zig/issues/447");
}
TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn);
@@ -1556,7 +1556,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
return g->builtin_types.entry_invalid;
}
add_node_error(g, proto_node,
- buf_sprintf("TODO implement inferred return types https://github.com/zig-lang/zig/issues/447"));
+ buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
return g->builtin_types.entry_invalid;
//return get_generic_fn_type(g, &fn_type_id);
}
diff --git a/src/codegen.cpp b/src/codegen.cpp
index f1e102392a..69542b3e67 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -582,7 +582,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) {
addLLVMArgAttr(fn_table_entry->llvm_value, (unsigned)gen_index, "nonnull");
}
// Note: byval is disabled on windows due to an LLVM bug:
- // https://github.com/zig-lang/zig/issues/536
+ // https://github.com/ziglang/zig/issues/536
if (is_byval && g->zig_target.os != OsWindows) {
addLLVMArgAttr(fn_table_entry->llvm_value, (unsigned)gen_index, "byval");
}
@@ -3067,7 +3067,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
for (size_t param_i = 0; param_i < fn_type_id->param_count; param_i += 1) {
FnGenParamInfo *gen_info = &fn_type->data.fn.gen_param_info[param_i];
// Note: byval is disabled on windows due to an LLVM bug:
- // https://github.com/zig-lang/zig/issues/536
+ // https://github.com/ziglang/zig/issues/536
if (gen_info->is_byval && g->zig_target.os != OsWindows) {
addLLVMCallsiteAttr(result, (unsigned)gen_info->gen_index, "byval");
}
@@ -6730,7 +6730,7 @@ static void init(CodeGen *g) {
const char *target_specific_features;
if (g->is_native_target) {
// LLVM creates invalid binaries on Windows sometimes.
- // See https://github.com/zig-lang/zig/issues/508
+ // See https://github.com/ziglang/zig/issues/508
// As a workaround we do not use target native features on Windows.
if (g->zig_target.os == OsWindows) {
target_specific_cpu_args = "";
diff --git a/src/ir.cpp b/src/ir.cpp
index e2cbba48a7..440063d58d 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -12130,7 +12130,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
casted_arg->value.type->id == TypeTableEntryIdNumLitFloat)
{
ir_add_error(ira, casted_arg,
- buf_sprintf("compiler bug: integer and float literals in var args function must be casted. https://github.com/zig-lang/zig/issues/557"));
+ buf_sprintf("compiler bug: integer and float literals in var args function must be casted. https://github.com/ziglang/zig/issues/557"));
return false;
}
@@ -12331,7 +12331,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
if (fn_proto_node->data.fn_proto.is_var_args) {
ir_add_error(ira, &call_instruction->base,
- buf_sprintf("compiler bug: unable to call var args function at compile time. https://github.com/zig-lang/zig/issues/313"));
+ buf_sprintf("compiler bug: unable to call var args function at compile time. https://github.com/ziglang/zig/issues/313"));
return ira->codegen->builtin_types.entry_invalid;
}
@@ -12424,7 +12424,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
}
if (call_instruction->is_async && fn_type_id->is_var_args) {
ir_add_error(ira, call_instruction->fn_ref,
- buf_sprintf("compiler bug: TODO: implement var args async functions. https://github.com/zig-lang/zig/issues/557"));
+ buf_sprintf("compiler bug: TODO: implement var args async functions. https://github.com/ziglang/zig/issues/557"));
return ira->codegen->builtin_types.entry_invalid;
}
@@ -12507,7 +12507,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
VariableTableEntry *arg_var = get_fn_var_by_index(parent_fn_entry, arg_tuple_i);
if (arg_var == nullptr) {
ir_add_error(ira, arg,
- buf_sprintf("compiler bug: var args can't handle void. https://github.com/zig-lang/zig/issues/557"));
+ buf_sprintf("compiler bug: var args can't handle void. https://github.com/ziglang/zig/issues/557"));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *arg_var_ptr_inst = ir_get_var_ptr(ira, arg, arg_var, true, false);
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 288a2b3b48..35180da8d1 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -16,7 +16,7 @@ pub fn Queue(comptime T: type) type {
data: T,
};
- // TODO: well defined copy elision: https://github.com/zig-lang/zig/issues/287
+ // TODO: well defined copy elision: https://github.com/ziglang/zig/issues/287
pub fn init(self: &Self) void {
self.root.next = null;
self.head = &self.root;
diff --git a/std/event.zig b/std/event.zig
index b2e7e3ae38..558bd2a188 100644
--- a/std/event.zig
+++ b/std/event.zig
@@ -148,7 +148,7 @@ pub const Loop = struct {
};
pub async fn connect(loop: &Loop, _address: &const std.net.Address) !std.os.File {
- var address = _address.*; // TODO https://github.com/zig-lang/zig/issues/733
+ var address = _address.*; // TODO https://github.com/ziglang/zig/issues/733
const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
errdefer std.os.close(sockfd);
@@ -172,7 +172,7 @@ test "listen on a port, send bytes, receive bytes" {
async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address, _socket: &const std.os.File) void {
const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
- var socket = _socket.*; // TODO https://github.com/zig-lang/zig/issues/733
+ var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
defer socket.close();
const next_handler = async errorableHandler(self, _addr, socket) catch |err| switch (err) {
error.OutOfMemory => @panic("unable to handle connection: out of memory"),
@@ -186,8 +186,8 @@ test "listen on a port, send bytes, receive bytes" {
}
async fn errorableHandler(self: &Self, _addr: &const std.net.Address, _socket: &const std.os.File) !void {
- const addr = _addr.*; // TODO https://github.com/zig-lang/zig/issues/733
- var socket = _socket.*; // TODO https://github.com/zig-lang/zig/issues/733
+ const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/733
+ var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
var adapter = std.io.FileOutStream.init(&socket);
var stream = &adapter.stream;
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 0af772b7dc..624751822a 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -824,7 +824,7 @@ test "fmt.format" {
try testFmt("file size: 63MiB\n", "file size: {Bi}\n", usize(63 * 1024 * 1024));
try testFmt("file size: 66.06MB\n", "file size: {B2}\n", usize(63 * 1024 * 1024));
{
- // Dummy field because of https://github.com/zig-lang/zig/issues/557.
+ // Dummy field because of https://github.com/ziglang/zig/issues/557.
const Struct = struct {
unused: u8,
};
diff --git a/std/mem.zig b/std/mem.zig
index 3ca87b35d3..617c1de2f5 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -702,7 +702,7 @@ test "std.mem.rotate" {
}));
}
-// TODO: When https://github.com/zig-lang/zig/issues/649 is solved these can be done by
+// TODO: When https://github.com/ziglang/zig/issues/649 is solved these can be done by
// endian-casting the pointer and then dereferencing
pub fn endianSwapIfLe(comptime T: type, x: T) T {
diff --git a/std/os/index.zig b/std/os/index.zig
index 7d19cd82c6..01e2092e1c 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -239,7 +239,7 @@ pub fn close(handle: FileHandle) void {
/// Calls POSIX read, and keeps trying if it gets interrupted.
pub fn posixRead(fd: i32, buf: []u8) !void {
// Linux can return EINVAL when read amount is > 0x7ffff000
- // See https://github.com/zig-lang/zig/pull/743#issuecomment-363158274
+ // See https://github.com/ziglang/zig/pull/743#issuecomment-363158274
const max_buf_len = 0x7ffff000;
var index: usize = 0;
@@ -281,7 +281,7 @@ pub const PosixWriteError = error{
/// Calls POSIX write, and keeps trying if it gets interrupted.
pub fn posixWrite(fd: i32, bytes: []const u8) !void {
// Linux can return EINVAL when write amount is > 0x7ffff000
- // See https://github.com/zig-lang/zig/pull/743#issuecomment-363165856
+ // See https://github.com/ziglang/zig/pull/743#issuecomment-363165856
const max_bytes_len = 0x7ffff000;
var index: usize = 0;
@@ -2513,7 +2513,7 @@ pub const SpawnThreadError = error{
/// caller must call wait on the returned thread
pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread {
// TODO compile-time call graph analysis to determine stack upper bound
- // https://github.com/zig-lang/zig/issues/157
+ // https://github.com/ziglang/zig/issues/157
const default_stack_size = 8 * 1024 * 1024;
const Context = @typeOf(context);
diff --git a/std/os/test.zig b/std/os/test.zig
index 56d6e8b309..4dfe76224a 100644
--- a/std/os/test.zig
+++ b/std/os/test.zig
@@ -12,7 +12,7 @@ const AtomicOrder = builtin.AtomicOrder;
test "makePath, put some files in it, deleteTree" {
if (builtin.os == builtin.Os.windows) {
// TODO implement os.Dir for windows
- // https://github.com/zig-lang/zig/issues/709
+ // https://github.com/ziglang/zig/issues/709
return;
}
try os.makePath(a, "os_test_tmp/b/c");
diff --git a/std/os/time.zig b/std/os/time.zig
index 4fd2c4e924..3af150ab6a 100644
--- a/std/os/time.zig
+++ b/std/os/time.zig
@@ -135,7 +135,7 @@ pub const Timer = struct {
//At some point we may change our minds on RAW, but for now we're
// sticking with posix standard MONOTONIC. For more information, see:
- // https://github.com/zig-lang/zig/pull/933
+ // https://github.com/ziglang/zig/pull/933
//
//const monotonic_clock_id = switch(builtin.os) {
// Os.linux => linux.CLOCK_MONOTONIC_RAW,
diff --git a/std/special/compiler_rt/comparetf2.zig b/std/special/compiler_rt/comparetf2.zig
index c189e5803b..760c3689c0 100644
--- a/std/special/compiler_rt/comparetf2.zig
+++ b/std/special/compiler_rt/comparetf2.zig
@@ -1,4 +1,4 @@
-// TODO https://github.com/zig-lang/zig/issues/305
+// TODO https://github.com/ziglang/zig/issues/305
// and then make the return types of some of these functions the enum instead of c_int
const LE_LESS = c_int(-1);
const LE_EQUAL = c_int(0);
@@ -59,7 +59,7 @@ pub extern fn __letf2(a: f128, b: f128) c_int {
;
}
-// TODO https://github.com/zig-lang/zig/issues/305
+// TODO https://github.com/ziglang/zig/issues/305
// and then make the return types of some of these functions the enum instead of c_int
const GE_LESS = c_int(-1);
const GE_EQUAL = c_int(0);
diff --git a/std/zig/ast.zig b/std/zig/ast.zig
index c1552b0220..1f15046a79 100644
--- a/std/zig/ast.zig
+++ b/std/zig/ast.zig
@@ -113,7 +113,7 @@ pub const Error = union(enum) {
pub fn render(self: &Error, tokens: &Tree.TokenList, stream: var) !void {
switch (self.*) {
- // TODO https://github.com/zig-lang/zig/issues/683
+ // TODO https://github.com/ziglang/zig/issues/683
@TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
@TagType(Error).ExpectedVarDeclOrFn => |*x| return x.render(tokens, stream),
@TagType(Error).ExpectedAggregateKw => |*x| return x.render(tokens, stream),
@@ -137,7 +137,7 @@ pub const Error = union(enum) {
pub fn loc(self: &Error) TokenIndex {
switch (self.*) {
- // TODO https://github.com/zig-lang/zig/issues/683
+ // TODO https://github.com/ziglang/zig/issues/683
@TagType(Error).InvalidToken => |x| return x.token,
@TagType(Error).ExpectedVarDeclOrFn => |x| return x.token,
@TagType(Error).ExpectedAggregateKw => |x| return x.token,
diff --git a/test/build_examples.zig b/test/build_examples.zig
index a3b44b9136..7a4c0f35d9 100644
--- a/test/build_examples.zig
+++ b/test/build_examples.zig
@@ -9,7 +9,7 @@ pub fn addCases(cases: &tests.BuildExamplesContext) void {
cases.add("example/guess_number/main.zig");
if (!is_windows) {
// TODO get this test passing on windows
- // See https://github.com/zig-lang/zig/issues/538
+ // See https://github.com/ziglang/zig/issues/538
cases.addBuildFile("example/shared_library/build.zig");
cases.addBuildFile("example/mix_o_files/build.zig");
}
diff --git a/test/cases/coroutines.zig b/test/cases/coroutines.zig
index 4aa97861ac..e983947a4c 100644
--- a/test/cases/coroutines.zig
+++ b/test/cases/coroutines.zig
@@ -204,7 +204,7 @@ test "error return trace across suspend points - async return" {
cancel p2;
}
-// TODO https://github.com/zig-lang/zig/issues/760
+// TODO https://github.com/ziglang/zig/issues/760
fn nonFailing() (promise->error!void) {
return async suspendThenFail() catch unreachable;
}
diff --git a/test/cases/misc.zig b/test/cases/misc.zig
index 66487a4946..deeeca8c3a 100644
--- a/test/cases/misc.zig
+++ b/test/cases/misc.zig
@@ -543,7 +543,7 @@ test "@typeName" {
comptime {
assert(mem.eql(u8, @typeName(i64), "i64"));
assert(mem.eql(u8, @typeName(&usize), "&usize"));
- // https://github.com/zig-lang/zig/issues/675
+ // https://github.com/ziglang/zig/issues/675
assert(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)"));
assert(mem.eql(u8, @typeName(Struct), "Struct"));
assert(mem.eql(u8, @typeName(Union), "Union"));
diff --git a/test/compare_output.zig b/test/compare_output.zig
index b01e87d4eb..905ffd37a9 100644
--- a/test/compare_output.zig
+++ b/test/compare_output.zig
@@ -131,7 +131,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\const is_windows = builtin.os == builtin.Os.windows;
\\const c = @cImport({
\\ if (is_windows) {
- \\ // See https://github.com/zig-lang/zig/issues/515
+ \\ // See https://github.com/ziglang/zig/issues/515
\\ @cDefine("_NO_CRT_STDIO_INLINE", "1");
\\ @cInclude("io.h");
\\ @cInclude("fcntl.h");
@@ -316,7 +316,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\const is_windows = builtin.os == builtin.Os.windows;
\\const c = @cImport({
\\ if (is_windows) {
- \\ // See https://github.com/zig-lang/zig/issues/515
+ \\ // See https://github.com/ziglang/zig/issues/515
\\ @cDefine("_NO_CRT_STDIO_INLINE", "1");
\\ @cInclude("io.h");
\\ @cInclude("fcntl.h");
--
cgit v1.2.3
From fcbb7426faac5e693ef195defe2d8d2a2eddadb1 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Thu, 31 May 2018 10:56:59 -0400
Subject: use * for pointer type instead of &
See #770
To help automatically translate code, see the
zig-fmt-pointer-reform-2 branch.
This will convert all & into *. Due to the syntax
ambiguity (which is why we are making this change),
even address-of & will turn into *, so you'll have
to manually fix thes instances. You will be guaranteed
to get compile errors for them - expected 'type', found 'foo'
---
build.zig | 14 +-
doc/docgen.zig | 22 +-
doc/langref.html.in | 212 ++++-----
example/cat/main.zig | 2 +-
example/hello_world/hello_libc.zig | 2 +-
example/mix_o_files/base64.zig | 2 +-
example/mix_o_files/build.zig | 2 +-
example/shared_library/build.zig | 2 +-
src-self-hosted/arg.zig | 12 +-
src-self-hosted/errmsg.zig | 14 +-
src-self-hosted/introspect.zig | 6 +-
src-self-hosted/ir.zig | 2 +-
src-self-hosted/main.zig | 36 +-
src-self-hosted/module.zig | 30 +-
src-self-hosted/scope.zig | 2 +-
src-self-hosted/target.zig | 10 +-
src/all_types.hpp | 31 +-
src/analyze.cpp | 8 +-
src/ast_render.cpp | 31 +-
src/codegen.cpp | 2 +-
src/ir.cpp | 89 ++--
src/ir_print.cpp | 6 +-
src/parser.cpp | 41 +-
src/translate_c.cpp | 33 +-
std/array_list.zig | 46 +-
std/atomic/queue.zig | 32 +-
std/atomic/stack.zig | 36 +-
std/base64.zig | 12 +-
std/buf_map.zig | 18 +-
std/buf_set.zig | 18 +-
std/buffer.zig | 40 +-
std/build.zig | 278 ++++++------
std/c/darwin.zig | 8 +-
std/c/index.zig | 72 ++--
std/c/linux.zig | 4 +-
std/c/windows.zig | 2 +-
std/crypto/blake2.zig | 16 +-
std/crypto/md5.zig | 8 +-
std/crypto/sha1.zig | 8 +-
std/crypto/sha2.zig | 16 +-
std/crypto/sha3.zig | 6 +-
std/crypto/throughput_test.zig | 4 +-
std/cstr.zig | 26 +-
std/debug/failing_allocator.zig | 10 +-
std/debug/index.zig | 106 ++---
std/elf.zig | 18 +-
std/event.zig | 34 +-
std/fmt/errol/index.zig | 14 +-
std/fmt/index.zig | 8 +-
std/hash/adler.zig | 4 +-
std/hash/crc.zig | 8 +-
std/hash/fnv.zig | 4 +-
std/hash/siphash.zig | 8 +-
std/hash_map.zig | 36 +-
std/heap.zig | 82 ++--
std/io.zig | 80 ++--
std/json.zig | 36 +-
std/linked_list.zig | 32 +-
std/macho.zig | 16 +-
std/math/complex/atan.zig | 4 +-
std/math/complex/cosh.zig | 4 +-
std/math/complex/exp.zig | 4 +-
std/math/complex/index.zig | 14 +-
std/math/complex/ldexp.zig | 8 +-
std/math/complex/pow.zig | 2 +-
std/math/complex/sinh.zig | 4 +-
std/math/complex/sqrt.zig | 4 +-
std/math/complex/tanh.zig | 4 +-
std/math/hypot.zig | 2 +-
std/math/index.zig | 4 +-
std/mem.zig | 48 +--
std/net.zig | 8 +-
std/os/child_process.zig | 62 +--
std/os/darwin.zig | 64 +--
std/os/file.zig | 32 +-
std/os/get_user_id.zig | 8 +-
std/os/index.zig | 164 +++----
std/os/linux/index.zig | 174 ++++----
std/os/linux/vdso.zig | 36 +-
std/os/linux/x86_64.zig | 8 +-
std/os/path.zig | 22 +-
std/os/test.zig | 2 +-
std/os/time.zig | 6 +-
std/os/windows/index.zig | 96 ++---
std/os/windows/util.zig | 12 +-
std/os/zen.zig | 20 +-
std/rand/index.zig | 46 +-
std/rand/ziggurat.zig | 10 +-
std/segmented_list.zig | 54 +--
std/sort.zig | 54 +--
std/special/bootstrap.zig | 20 +-
std/special/build_file_template.zig | 4 +-
std/special/build_runner.zig | 6 +-
std/special/builtin.zig | 8 +-
std/special/compiler_rt/index.zig | 4 +-
std/special/compiler_rt/udivmod.zig | 18 +-
std/special/compiler_rt/udivmoddi4.zig | 2 +-
std/special/compiler_rt/udivmodti4.zig | 4 +-
std/special/compiler_rt/udivti3.zig | 2 +-
std/special/compiler_rt/umodti3.zig | 2 +-
std/special/panic.zig | 2 +-
std/unicode.zig | 6 +-
std/zig/ast.zig | 570 ++++++++++++-------------
std/zig/bench.zig | 6 +-
std/zig/parse.zig | 156 +++----
std/zig/parser_test.zig | 2 +-
std/zig/render.zig | 56 +--
std/zig/tokenizer.zig | 8 +-
test/assemble_and_link.zig | 2 +-
test/build_examples.zig | 2 +-
test/cases/align.zig | 56 +--
test/cases/atomics.zig | 12 +-
test/cases/bugs/655.zig | 4 +-
test/cases/bugs/828.zig | 6 +-
test/cases/bugs/920.zig | 6 +-
test/cases/cast.zig | 42 +-
test/cases/const_slice_child.zig | 6 +-
test/cases/coroutines.zig | 6 +-
test/cases/enum.zig | 10 +-
test/cases/enum_with_members.zig | 2 +-
test/cases/eval.zig | 12 +-
test/cases/field_parent_ptr.zig | 4 +-
test/cases/fn_in_struct_in_comptime.zig | 6 +-
test/cases/generics.zig | 8 +-
test/cases/incomplete_struct_param_tld.zig | 4 +-
test/cases/math.zig | 18 +-
test/cases/misc.zig | 48 +--
test/cases/null.zig | 2 +-
test/cases/reflection.zig | 2 +-
test/cases/slice.zig | 2 +-
test/cases/struct.zig | 28 +-
test/cases/struct_contains_null_ptr_itself.zig | 4 +-
test/cases/switch.zig | 2 +-
test/cases/this.zig | 2 +-
test/cases/type_info.zig | 16 +-
test/cases/undefined.zig | 4 +-
test/cases/union.zig | 16 +-
test/compare_output.zig | 20 +-
test/compile_errors.zig | 122 +++---
test/gen_h.zig | 2 +-
test/runtime_safety.zig | 2 +-
test/standalone/brace_expansion/build.zig | 2 +-
test/standalone/brace_expansion/main.zig | 8 +-
test/standalone/issue_339/build.zig | 2 +-
test/standalone/issue_339/test.zig | 2 +-
test/standalone/issue_794/build.zig | 2 +-
test/standalone/pkg_import/build.zig | 2 +-
test/standalone/use_alias/build.zig | 2 +-
test/tests.zig | 136 +++---
test/translate_c.zig | 58 +--
150 files changed, 2162 insertions(+), 2143 deletions(-)
(limited to 'std/atomic')
diff --git a/build.zig b/build.zig
index a4e3dbcdfa..109a799ac9 100644
--- a/build.zig
+++ b/build.zig
@@ -10,7 +10,7 @@ const ArrayList = std.ArrayList;
const Buffer = std.Buffer;
const io = std.io;
-pub fn build(b: &Builder) !void {
+pub fn build(b: *Builder) !void {
const mode = b.standardReleaseOptions();
var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
@@ -132,7 +132,7 @@ pub fn build(b: &Builder) !void {
test_step.dependOn(tests.addGenHTests(b, test_filter));
}
-fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) void {
+fn dependOnLib(lib_exe_obj: *std.build.LibExeObjStep, dep: *const LibraryDep) void {
for (dep.libdirs.toSliceConst()) |lib_dir| {
lib_exe_obj.addLibPath(lib_dir);
}
@@ -147,7 +147,7 @@ fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) vo
}
}
-fn addCppLib(b: &Builder, lib_exe_obj: &std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) void {
+fn addCppLib(b: *Builder, lib_exe_obj: *std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) void {
const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
lib_exe_obj.addObjectFile(os.path.join(b.allocator, cmake_binary_dir, "zig_cpp", b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())) catch unreachable);
}
@@ -159,7 +159,7 @@ const LibraryDep = struct {
includes: ArrayList([]const u8),
};
-fn findLLVM(b: &Builder, llvm_config_exe: []const u8) !LibraryDep {
+fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep {
const libs_output = try b.exec([][]const u8{
llvm_config_exe,
"--libs",
@@ -217,7 +217,7 @@ fn findLLVM(b: &Builder, llvm_config_exe: []const u8) !LibraryDep {
return result;
}
-pub fn installStdLib(b: &Builder, stdlib_files: []const u8) void {
+pub fn installStdLib(b: *Builder, stdlib_files: []const u8) void {
var it = mem.split(stdlib_files, ";");
while (it.next()) |stdlib_file| {
const src_path = os.path.join(b.allocator, "std", stdlib_file) catch unreachable;
@@ -226,7 +226,7 @@ pub fn installStdLib(b: &Builder, stdlib_files: []const u8) void {
}
}
-pub fn installCHeaders(b: &Builder, c_header_files: []const u8) void {
+pub fn installCHeaders(b: *Builder, c_header_files: []const u8) void {
var it = mem.split(c_header_files, ";");
while (it.next()) |c_header_file| {
const src_path = os.path.join(b.allocator, "c_headers", c_header_file) catch unreachable;
@@ -235,7 +235,7 @@ pub fn installCHeaders(b: &Builder, c_header_files: []const u8) void {
}
}
-fn nextValue(index: &usize, build_info: []const u8) []const u8 {
+fn nextValue(index: *usize, build_info: []const u8) []const u8 {
const start = index.*;
while (true) : (index.* += 1) {
switch (build_info[index.*]) {
diff --git a/doc/docgen.zig b/doc/docgen.zig
index 7dc444f127..fed4bb8eba 100644
--- a/doc/docgen.zig
+++ b/doc/docgen.zig
@@ -104,7 +104,7 @@ const Tokenizer = struct {
};
}
- fn next(self: &Tokenizer) Token {
+ fn next(self: *Tokenizer) Token {
var result = Token{
.id = Token.Id.Eof,
.start = self.index,
@@ -196,7 +196,7 @@ const Tokenizer = struct {
line_end: usize,
};
- fn getTokenLocation(self: &Tokenizer, token: &const Token) Location {
+ fn getTokenLocation(self: *Tokenizer, token: *const Token) Location {
var loc = Location{
.line = 0,
.column = 0,
@@ -221,7 +221,7 @@ const Tokenizer = struct {
}
};
-fn parseError(tokenizer: &Tokenizer, token: &const Token, comptime fmt: []const u8, args: ...) error {
+fn parseError(tokenizer: *Tokenizer, token: *const Token, comptime fmt: []const u8, args: ...) error {
const loc = tokenizer.getTokenLocation(token);
warn("{}:{}:{}: error: " ++ fmt ++ "\n", tokenizer.source_file_name, loc.line + 1, loc.column + 1, args);
if (loc.line_start <= loc.line_end) {
@@ -244,13 +244,13 @@ fn parseError(tokenizer: &Tokenizer, token: &const Token, comptime fmt: []const
return error.ParseError;
}
-fn assertToken(tokenizer: &Tokenizer, token: &const Token, id: Token.Id) !void {
+fn assertToken(tokenizer: *Tokenizer, token: *const Token, id: Token.Id) !void {
if (token.id != id) {
return parseError(tokenizer, token, "expected {}, found {}", @tagName(id), @tagName(token.id));
}
}
-fn eatToken(tokenizer: &Tokenizer, id: Token.Id) !Token {
+fn eatToken(tokenizer: *Tokenizer, id: Token.Id) !Token {
const token = tokenizer.next();
try assertToken(tokenizer, token, id);
return token;
@@ -317,7 +317,7 @@ const Action = enum {
Close,
};
-fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
+fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
var urls = std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator);
errdefer urls.deinit();
@@ -546,7 +546,7 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
};
}
-fn urlize(allocator: &mem.Allocator, input: []const u8) ![]u8 {
+fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
var buf = try std.Buffer.initSize(allocator, 0);
defer buf.deinit();
@@ -566,7 +566,7 @@ fn urlize(allocator: &mem.Allocator, input: []const u8) ![]u8 {
return buf.toOwnedSlice();
}
-fn escapeHtml(allocator: &mem.Allocator, input: []const u8) ![]u8 {
+fn escapeHtml(allocator: *mem.Allocator, input: []const u8) ![]u8 {
var buf = try std.Buffer.initSize(allocator, 0);
defer buf.deinit();
@@ -608,7 +608,7 @@ test "term color" {
assert(mem.eql(u8, result, "AgreenB"));
}
-fn termColor(allocator: &mem.Allocator, input: []const u8) ![]u8 {
+fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
var buf = try std.Buffer.initSize(allocator, 0);
defer buf.deinit();
@@ -688,7 +688,7 @@ fn termColor(allocator: &mem.Allocator, input: []const u8) ![]u8 {
return buf.toOwnedSlice();
}
-fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var, zig_exe: []const u8) !void {
+fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {
var code_progress_index: usize = 0;
for (toc.nodes) |node| {
switch (node) {
@@ -1036,7 +1036,7 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
}
}
-fn exec(allocator: &mem.Allocator, args: []const []const u8) !os.ChildProcess.ExecResult {
+fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.ExecResult {
const result = try os.ChildProcess.exec(allocator, args, null, null, max_doc_file_size);
switch (result.term) {
os.ChildProcess.Term.Exited => |exit_code| {
diff --git a/doc/langref.html.in b/doc/langref.html.in
index d63c38d0fe..3bd1124e00 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -458,7 +458,7 @@ test "string literals" {
// A C string literal is a null terminated pointer.
const null_terminated_bytes = c"hello";
- assert(@typeOf(null_terminated_bytes) == &const u8);
+ assert(@typeOf(null_terminated_bytes) == *const u8);
assert(null_terminated_bytes[5] == 0);
}
{#code_end#}
@@ -547,7 +547,7 @@ const c_string_literal =
;
{#code_end#}
- In this example the variable c_string_literal has type &const char and
+ In this example the variable c_string_literal has type *const char and
has a terminating null byte.
{#see_also|@embedFile#}
@@ -1403,12 +1403,12 @@ test "address of syntax" {
assert(x_ptr.* == 1234);
// When you get the address of a const variable, you get a const pointer.
- assert(@typeOf(x_ptr) == &const i32);
+ assert(@typeOf(x_ptr) == *const i32);
// If you want to mutate the value, you'd need an address of a mutable variable:
var y: i32 = 5678;
const y_ptr = &y;
- assert(@typeOf(y_ptr) == &i32);
+ assert(@typeOf(y_ptr) == *i32);
y_ptr.* += 1;
assert(y_ptr.* == 5679);
}
@@ -1455,7 +1455,7 @@ comptime {
test "@ptrToInt and @intToPtr" {
// To convert an integer address into a pointer, use @intToPtr:
- const ptr = @intToPtr(&i32, 0xdeadbeef);
+ const ptr = @intToPtr(*i32, 0xdeadbeef);
// To convert a pointer to an integer, use @ptrToInt:
const addr = @ptrToInt(ptr);
@@ -1467,7 +1467,7 @@ test "@ptrToInt and @intToPtr" {
comptime {
// Zig is able to do this at compile-time, as long as
// ptr is never dereferenced.
- const ptr = @intToPtr(&i32, 0xdeadbeef);
+ const ptr = @intToPtr(*i32, 0xdeadbeef);
const addr = @ptrToInt(ptr);
assert(@typeOf(addr) == usize);
assert(addr == 0xdeadbeef);
@@ -1477,17 +1477,17 @@ test "volatile" {
// In Zig, loads and stores are assumed to not have side effects.
// If a given load or store should have side effects, such as
// Memory Mapped Input/Output (MMIO), use `volatile`:
- const mmio_ptr = @intToPtr(&volatile u8, 0x12345678);
+ const mmio_ptr = @intToPtr(*volatile u8, 0x12345678);
// Now loads and stores with mmio_ptr are guaranteed to all happen
// and in the same order as in source code.
- assert(@typeOf(mmio_ptr) == &volatile u8);
+ assert(@typeOf(mmio_ptr) == *volatile u8);
}
test "nullable pointers" {
// Pointers cannot be null. If you want a null pointer, use the nullable
// prefix `?` to make the pointer type nullable.
- var ptr: ?&i32 = null;
+ var ptr: ?*i32 = null;
var x: i32 = 1;
ptr = &x;
@@ -1496,7 +1496,7 @@ test "nullable pointers" {
// Nullable pointers are the same size as normal pointers, because pointer
// value 0 is used as the null value.
- assert(@sizeOf(?&i32) == @sizeOf(&i32));
+ assert(@sizeOf(?*i32) == @sizeOf(*i32));
}
test "pointer casting" {
@@ -1504,7 +1504,7 @@ test "pointer casting" {
// operation that Zig cannot protect you against. Use @ptrCast only when other
// conversions are not possible.
const bytes align(@alignOf(u32)) = []u8{0x12, 0x12, 0x12, 0x12};
- const u32_ptr = @ptrCast(&const u32, &bytes[0]);
+ const u32_ptr = @ptrCast(*const u32, &bytes[0]);
assert(u32_ptr.* == 0x12121212);
// Even this example is contrived - there are better ways to do the above than
@@ -1518,7 +1518,7 @@ test "pointer casting" {
test "pointer child type" {
// pointer types have a `child` field which tells you the type they point to.
- assert((&u32).Child == u32);
+ assert((*u32).Child == u32);
}
{#code_end#}
{#header_open|Alignment#}
@@ -1543,15 +1543,15 @@ const builtin = @import("builtin");
test "variable alignment" {
var x: i32 = 1234;
const align_of_i32 = @alignOf(@typeOf(x));
- assert(@typeOf(&x) == &i32);
- assert(&i32 == &align(align_of_i32) i32);
+ assert(@typeOf(&x) == *i32);
+ assert(*i32 == *align(align_of_i32) i32);
if (builtin.arch == builtin.Arch.x86_64) {
- assert((&i32).alignment == 4);
+ assert((*i32).alignment == 4);
}
}
{#code_end#}
- In the same way that a &i32 can be implicitly cast to a
- &const i32, a pointer with a larger alignment can be implicitly
+
In the same way that a *i32 can be implicitly cast to a
+ *const i32, a pointer with a larger alignment can be implicitly
cast to a pointer with a smaller alignment, but not vice versa.
@@ -1565,7 +1565,7 @@ var foo: u8 align(4) = 100;
test "global variable alignment" {
assert(@typeOf(&foo).alignment == 4);
- assert(@typeOf(&foo) == &align(4) u8);
+ assert(@typeOf(&foo) == *align(4) u8);
const slice = (&foo)[0..1];
assert(@typeOf(slice) == []align(4) u8);
}
@@ -1610,7 +1610,7 @@ fn foo(bytes: []u8) u32 {
u8 can alias any memory.
As an example, this code produces undefined behavior:
- @ptrCast(&u32, f32(12.34)).*
+ @ptrCast(*u32, f32(12.34)).*
Instead, use {#link|@bitCast#}:
@bitCast(u32, f32(12.34))
As an added benefit, the @bitcast version works at compile-time.
@@ -1736,7 +1736,7 @@ const Vec3 = struct {
};
}
- pub fn dot(self: &const Vec3, other: &const Vec3) f32 {
+ pub fn dot(self: *const Vec3, other: *const Vec3) f32 {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
};
@@ -1768,7 +1768,7 @@ test "struct namespaced variable" {
// struct field order is determined by the compiler for optimal performance.
// however, you can still calculate a struct base pointer given a field pointer:
-fn setYBasedOnX(x: &f32, y: f32) void {
+fn setYBasedOnX(x: *f32, y: f32) void {
const point = @fieldParentPtr(Point, "x", x);
point.y = y;
}
@@ -1786,13 +1786,13 @@ test "field parent pointer" {
fn LinkedList(comptime T: type) type {
return struct {
pub const Node = struct {
- prev: ?&Node,
- next: ?&Node,
+ prev: ?*Node,
+ next: ?*Node,
data: T,
};
- first: ?&Node,
- last: ?&Node,
+ first: ?*Node,
+ last: ?*Node,
len: usize,
};
}
@@ -2039,7 +2039,7 @@ const Variant = union(enum) {
Int: i32,
Bool: bool,
- fn truthy(self: &const Variant) bool {
+ fn truthy(self: *const Variant) bool {
return switch (self.*) {
Variant.Int => |x_int| x_int != 0,
Variant.Bool => |x_bool| x_bool,
@@ -2786,7 +2786,7 @@ test "pass aggregate type by value to function" {
}
{#code_end#}
- Instead, one must use &const. Zig allows implicitly casting something
+ Instead, one must use *const. Zig allows implicitly casting something
to a const pointer to it:
{#code_begin|test#}
@@ -2794,7 +2794,7 @@ const Foo = struct {
x: i32,
};
-fn bar(foo: &const Foo) void {}
+fn bar(foo: *const Foo) void {}
test "implicitly cast to const pointer" {
bar(Foo {.x = 12,});
@@ -3208,16 +3208,16 @@ struct Foo *do_a_thing(void) {
Zig code
{#code_begin|syntax#}
// malloc prototype included for reference
-extern fn malloc(size: size_t) ?&u8;
+extern fn malloc(size: size_t) ?*u8;
-fn doAThing() ?&Foo {
+fn doAThing() ?*Foo {
const ptr = malloc(1234) ?? return null;
// ...
}
{#code_end#}
Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr"
- is &u8 not ?&u8. The ?? operator
+ is *u8 not ?*u8. The ?? operator
unwrapped the nullable type and therefore ptr is guaranteed to be non-null everywhere
it is used in the function.
@@ -3237,7 +3237,7 @@ fn doAThing() ?&Foo {
In Zig you can accomplish the same thing:
{#code_begin|syntax#}
-fn doAThing(nullable_foo: ?&Foo) void {
+fn doAThing(nullable_foo: ?*Foo) void {
// do some stuff
if (nullable_foo) |foo| {
@@ -3713,7 +3713,7 @@ fn List(comptime T: type) type {
{#code_begin|syntax#}
const Node = struct {
- next: &Node,
+ next: *Node,
name: []u8,
};
{#code_end#}
@@ -3745,7 +3745,7 @@ pub fn main() void {
{#code_begin|syntax#}
/// Calls print and then flushes the buffer.
-pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) error!void {
+pub fn printf(self: *OutStream, comptime format: []const u8, args: ...) error!void {
const State = enum {
Start,
OpenBrace,
@@ -3817,7 +3817,7 @@ pub fn printf(self: &OutStream, comptime format: []const u8, args: ...) error!vo
and emits a function that actually looks like this:
{#code_begin|syntax#}
-pub fn printf(self: &OutStream, arg0: i32, arg1: []const u8) !void {
+pub fn printf(self: *OutStream, arg0: i32, arg1: []const u8) !void {
try self.write("here is a string: '");
try self.printValue(arg0);
try self.write("' here is a number: ");
@@ -3831,7 +3831,7 @@ pub fn printf(self: &OutStream, arg0: i32, arg1: []const u8) !void {
on the type:
{#code_begin|syntax#}
-pub fn printValue(self: &OutStream, value: var) !void {
+pub fn printValue(self: *OutStream, value: var) !void {
const T = @typeOf(value);
if (@isInteger(T)) {
return self.printInt(T, value);
@@ -3911,7 +3911,7 @@ pub fn main() void {
at compile time.
{#header_open|@addWithOverflow#}
- @addWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool
+ @addWithOverflow(comptime T: type, a: T, b: T, result: *T) bool
Performs result.* = a + b. If overflow or underflow occurs,
stores the overflowed bits in result and returns true.
@@ -3919,7 +3919,7 @@ pub fn main() void {
{#header_close#}
{#header_open|@ArgType#}
- @ArgType(comptime T: type, comptime n: usize) -> type
+ @ArgType(comptime T: type, comptime n: usize) type
This builtin function takes a function type and returns the type of the parameter at index n.
@@ -3931,7 +3931,7 @@ pub fn main() void {
{#header_close#}
{#header_open|@atomicLoad#}
- @atomicLoad(comptime T: type, ptr: &const T, comptime ordering: builtin.AtomicOrder) -> T
+ @atomicLoad(comptime T: type, ptr: *const T, comptime ordering: builtin.AtomicOrder) T
This builtin function atomically dereferences a pointer and returns the value.
@@ -3950,7 +3950,7 @@ pub fn main() void {
{#header_close#}
{#header_open|@atomicRmw#}
- @atomicRmw(comptime T: type, ptr: &T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) -> T
+ @atomicRmw(comptime T: type, ptr: *T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) T
This builtin function atomically modifies memory and then returns the previous value.
@@ -3969,7 +3969,7 @@ pub fn main() void {
{#header_close#}
{#header_open|@bitCast#}
- @bitCast(comptime DestType: type, value: var) -> DestType
+ @bitCast(comptime DestType: type, value: var) DestType
Converts a value of one type to another type.
@@ -4002,9 +4002,9 @@ pub fn main() void {
{#header_close#}
{#header_open|@alignCast#}
- @alignCast(comptime alignment: u29, ptr: var) -> var
+ @alignCast(comptime alignment: u29, ptr: var) var
- ptr can be &T, fn(), ?&T,
+ ptr can be *T, fn(), ?*T,
?fn(), or []T. It returns the same type as ptr
except with the alignment adjusted to the new value.
@@ -4013,7 +4013,7 @@ pub fn main() void {
{#header_close#}
{#header_open|@alignOf#}
- @alignOf(comptime T: type) -> (number literal)
+ @alignOf(comptime T: type) (number literal)
This function returns the number of bytes that this type should be aligned to
for the current target to match the C ABI. When the child type of a pointer has
@@ -4021,7 +4021,7 @@ pub fn main() void {
const assert = @import("std").debug.assert;
comptime {
- assert(&u32 == &align(@alignOf(u32)) u32);
+ assert(*u32 == *align(@alignOf(u32)) u32);
}
The result is a target-specific compile time constant. It is guaranteed to be
@@ -4049,7 +4049,7 @@ comptime {
{#see_also|Import from C Header File|@cInclude|@cImport|@cUndef|void#}
{#header_close#}
{#header_open|@cImport#}
-
@cImport(expression) -> (namespace)
+ @cImport(expression) (namespace)
This function parses C code and imports the functions, types, variables, and
compatible macro definitions into the result namespace.
@@ -4095,13 +4095,13 @@ comptime {
{#see_also|Import from C Header File|@cImport|@cDefine|@cInclude#}
{#header_close#}
{#header_open|@canImplicitCast#}
-
@canImplicitCast(comptime T: type, value) -> bool
+ @canImplicitCast(comptime T: type, value) bool
Returns whether a value can be implicitly casted to a given type.
{#header_close#}
{#header_open|@clz#}
- @clz(x: T) -> U
+ @clz(x: T) U
This function counts the number of leading zeroes in x which is an integer
type T.
@@ -4116,13 +4116,13 @@ comptime {
{#header_close#}
{#header_open|@cmpxchgStrong#}
-
@cmpxchgStrong(comptime T: type, ptr: &T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) -> ?T
+ @cmpxchgStrong(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T
This function performs a strong atomic compare exchange operation. It's the equivalent of this code,
except atomic:
{#code_begin|syntax#}
-fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_value: T) ?T {
+fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_value: T) ?T {
const old_value = ptr.*;
if (old_value == expected_value) {
ptr.* = new_value;
@@ -4143,13 +4143,13 @@ fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_v
{#see_also|Compile Variables|cmpxchgWeak#}
{#header_close#}
{#header_open|@cmpxchgWeak#}
- @cmpxchgWeak(comptime T: type, ptr: &T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) -> ?T
+ @cmpxchgWeak(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T
This function performs a weak atomic compare exchange operation. It's the equivalent of this code,
except atomic:
{#code_begin|syntax#}
-fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_value: T) ?T {
+fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_value: T) ?T {
const old_value = ptr.*;
if (old_value == expected_value and usuallyTrueButSometimesFalse()) {
ptr.* = new_value;
@@ -4237,7 +4237,7 @@ test "main" {
{#code_end#}
{#header_close#}
{#header_open|@ctz#}
- @ctz(x: T) -> U
+ @ctz(x: T) U
This function counts the number of trailing zeroes in x which is an integer
type T.
@@ -4251,7 +4251,7 @@ test "main" {
{#header_close#}
{#header_open|@divExact#}
- @divExact(numerator: T, denominator: T) -> T
+ @divExact(numerator: T, denominator: T) T
Exact division. Caller guarantees denominator != 0 and
@divTrunc(numerator, denominator) * denominator == numerator.
@@ -4264,7 +4264,7 @@ test "main" {
{#see_also|@divTrunc|@divFloor#}
{#header_close#}
{#header_open|@divFloor#}
-
@divFloor(numerator: T, denominator: T) -> T
+ @divFloor(numerator: T, denominator: T) T
Floored division. Rounds toward negative infinity. For unsigned integers it is
the same as numerator / denominator. Caller guarantees denominator != 0 and
@@ -4278,7 +4278,7 @@ test "main" {
{#see_also|@divTrunc|@divExact#}
{#header_close#}
{#header_open|@divTrunc#}
-
@divTrunc(numerator: T, denominator: T) -> T
+ @divTrunc(numerator: T, denominator: T) T
Truncated division. Rounds toward zero. For unsigned integers it is
the same as numerator / denominator. Caller guarantees denominator != 0 and
@@ -4292,7 +4292,7 @@ test "main" {
{#see_also|@divFloor|@divExact#}
{#header_close#}
{#header_open|@embedFile#}
-
@embedFile(comptime path: []const u8) -> [X]u8
+ @embedFile(comptime path: []const u8) [X]u8
This function returns a compile time constant fixed-size array with length
equal to the byte count of the file given by path. The contents of the array
@@ -4304,19 +4304,19 @@ test "main" {
{#see_also|@import#}
{#header_close#}
{#header_open|@export#}
-
@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) -> []const u8
+ @export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) []const u8
Creates a symbol in the output object file.
{#header_close#}
{#header_open|@tagName#}
- @tagName(value: var) -> []const u8
+ @tagName(value: var) []const u8
Converts an enum value or union value to a slice of bytes representing the name.
{#header_close#}
{#header_open|@TagType#}
- @TagType(T: type) -> type
+ @TagType(T: type) type
For an enum, returns the integer type that is used to store the enumeration value.
@@ -4325,7 +4325,7 @@ test "main" {
{#header_close#}
{#header_open|@errorName#}
- @errorName(err: error) -> []u8
+ @errorName(err: error) []u8
This function returns the string representation of an error. If an error
declaration is:
@@ -4341,7 +4341,7 @@ test "main" {
{#header_close#}
{#header_open|@errorReturnTrace#}
- @errorReturnTrace() -> ?&builtin.StackTrace
+ @errorReturnTrace() ?*builtin.StackTrace
If the binary is built with error return tracing, and this function is invoked in a
function that calls a function with an error or error union return type, returns a
@@ -4360,7 +4360,7 @@ test "main" {
{#header_close#}
{#header_open|@fieldParentPtr#}
@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8,
- field_ptr: &T) -> &ParentType
+ field_ptr: *T) *ParentType
Given a pointer to a field, returns the base pointer of a struct.
@@ -4380,7 +4380,7 @@ test "main" {
{#header_close#}
{#header_open|@import#}
- @import(comptime path: []u8) -> (namespace)
+ @import(comptime path: []u8) (namespace)
This function finds a zig file corresponding to path and imports all the
public top level declarations into the resulting namespace.
@@ -4400,7 +4400,7 @@ test "main" {
{#see_also|Compile Variables|@embedFile#}
{#header_close#}
{#header_open|@inlineCall#}
-
@inlineCall(function: X, args: ...) -> Y
+ @inlineCall(function: X, args: ...) Y
This calls a function, in the same way that invoking an expression with parentheses does:
@@ -4420,19 +4420,19 @@ fn add(a: i32, b: i32) i32 { return a + b; }
{#see_also|@noInlineCall#}
{#header_close#}
{#header_open|@intToPtr#}
- @intToPtr(comptime DestType: type, int: usize) -> DestType
+ @intToPtr(comptime DestType: type, int: usize) DestType
Converts an integer to a pointer. To convert the other way, use {#link|@ptrToInt#}.
{#header_close#}
{#header_open|@IntType#}
- @IntType(comptime is_signed: bool, comptime bit_count: u8) -> type
+ @IntType(comptime is_signed: bool, comptime bit_count: u8) type
This function returns an integer type with the given signness and bit count.
{#header_close#}
{#header_open|@maxValue#}
- @maxValue(comptime T: type) -> (number literal)
+ @maxValue(comptime T: type) (number literal)
This function returns the maximum value of the integer type T.
@@ -4441,7 +4441,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
{#header_close#}
{#header_open|@memberCount#}
- @memberCount(comptime T: type) -> (number literal)
+ @memberCount(comptime T: type) (number literal)
This function returns the number of members in a struct, enum, or union type.
@@ -4453,7 +4453,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
{#header_close#}
{#header_open|@memberName#}
- @memberName(comptime T: type, comptime index: usize) -> [N]u8
+ @memberName(comptime T: type, comptime index: usize) [N]u8
Returns the field name of a struct, union, or enum.
The result is a compile time constant.
@@ -4463,15 +4463,15 @@ fn add(a: i32, b: i32) i32 { return a + b; }
{#header_close#}
{#header_open|@field#}
- @field(lhs: var, comptime field_name: []const u8) -> (field)
+ @field(lhs: var, comptime field_name: []const u8) (field)
Preforms field access equivalent to lhs.->field_name-<.
{#header_close#}
{#header_open|@memberType#}
- @memberType(comptime T: type, comptime index: usize) -> type
+ @memberType(comptime T: type, comptime index: usize) type
Returns the field type of a struct or union.
{#header_close#}
{#header_open|@memcpy#}
- @memcpy(noalias dest: &u8, noalias source: &const u8, byte_count: usize)
+ @memcpy(noalias dest: *u8, noalias source: *const u8, byte_count: usize)
This function copies bytes from one region of memory to another. dest and
source are both pointers and must not overlap.
@@ -4489,7 +4489,7 @@ fn add(a: i32, b: i32) i32 { return a + b; }
mem.copy(u8, dest[0...byte_count], source[0...byte_count]);
{#header_close#}
{#header_open|@memset#}
-
@memset(dest: &u8, c: u8, byte_count: usize)
+ @memset(dest: *u8, c: u8, byte_count: usize)
This function sets a region of memory to c. dest is a pointer.
@@ -4506,7 +4506,7 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);
mem.set(u8, dest, c);
{#header_close#}
{#header_open|@minValue#}
- @minValue(comptime T: type) -> (number literal)
+ @minValue(comptime T: type) (number literal)
This function returns the minimum value of the integer type T.
@@ -4515,7 +4515,7 @@ mem.set(u8, dest, c);
{#header_close#}
{#header_open|@mod#}
- @mod(numerator: T, denominator: T) -> T
+ @mod(numerator: T, denominator: T) T
Modulus division. For unsigned integers this is the same as
numerator % denominator. Caller guarantees denominator > 0.
@@ -4528,7 +4528,7 @@ mem.set(u8, dest, c);
{#see_also|@rem#}
{#header_close#}
{#header_open|@mulWithOverflow#}
-
@mulWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool
+ @mulWithOverflow(comptime T: type, a: T, b: T, result: *T) bool
Performs result.* = a * b. If overflow or underflow occurs,
stores the overflowed bits in result and returns true.
@@ -4536,7 +4536,7 @@ mem.set(u8, dest, c);
{#header_close#}
{#header_open|@newStackCall#}
- @newStackCall(new_stack: []u8, function: var, args: ...) -> var
+ @newStackCall(new_stack: []u8, function: var, args: ...) var
This calls a function, in the same way that invoking an expression with parentheses does. However,
instead of using the same stack as the caller, the function uses the stack provided in the new_stack
@@ -4572,7 +4572,7 @@ fn targetFunction(x: i32) usize {
{#code_end#}
{#header_close#}
{#header_open|@noInlineCall#}
-
@noInlineCall(function: var, args: ...) -> var
+ @noInlineCall(function: var, args: ...) var
This calls a function, in the same way that invoking an expression with parentheses does:
@@ -4594,13 +4594,13 @@ fn add(a: i32, b: i32) i32 {
{#see_also|@inlineCall#}
{#header_close#}
{#header_open|@offsetOf#}
- @offsetOf(comptime T: type, comptime field_name: [] const u8) -> (number literal)
+ @offsetOf(comptime T: type, comptime field_name: [] const u8) (number literal)
This function returns the byte offset of a field relative to its containing struct.
{#header_close#}
{#header_open|@OpaqueType#}
- @OpaqueType() -> type
+ @OpaqueType() type
Creates a new type with an unknown size and alignment.
@@ -4608,12 +4608,12 @@ fn add(a: i32, b: i32) i32 {
This is typically used for type safety when interacting with C code that does not expose struct details.
Example:
- {#code_begin|test_err|expected type '&Derp', found '&Wat'#}
+ {#code_begin|test_err|expected type '*Derp', found '*Wat'#}
const Derp = @OpaqueType();
const Wat = @OpaqueType();
-extern fn bar(d: &Derp) void;
-export fn foo(w: &Wat) void {
+extern fn bar(d: *Derp) void;
+export fn foo(w: *Wat) void {
bar(w);
}
@@ -4623,7 +4623,7 @@ test "call foo" {
{#code_end#}
{#header_close#}
{#header_open|@panic#}
- @panic(message: []const u8) -> noreturn
+ @panic(message: []const u8) noreturn
Invokes the panic handler function. By default the panic handler function
calls the public panic function exposed in the root source file, or
@@ -4639,19 +4639,19 @@ test "call foo" {
{#see_also|Root Source File#}
{#header_close#}
{#header_open|@ptrCast#}
-
@ptrCast(comptime DestType: type, value: var) -> DestType
+ @ptrCast(comptime DestType: type, value: var) DestType
Converts a pointer of one type to a pointer of another type.
{#header_close#}
{#header_open|@ptrToInt#}
- @ptrToInt(value: var) -> usize
+ @ptrToInt(value: var) usize
Converts value to a usize which is the address of the pointer. value can be one of these types:
- &T
- ?&T
+ *T
+ ?*T
fn()
?fn()
@@ -4659,7 +4659,7 @@ test "call foo" {
{#header_close#}
{#header_open|@rem#}
- @rem(numerator: T, denominator: T) -> T
+ @rem(numerator: T, denominator: T) T
Remainder division. For unsigned integers this is the same as
numerator % denominator. Caller guarantees denominator > 0.
@@ -4776,13 +4776,13 @@ pub const FloatMode = enum {
{#see_also|Compile Variables#}
{#header_close#}
{#header_open|@setGlobalSection#}
-
@setGlobalSection(global_variable_name, comptime section_name: []const u8) -> bool
+ @setGlobalSection(global_variable_name, comptime section_name: []const u8) bool
Puts the global variable in the specified section.
{#header_close#}
{#header_open|@shlExact#}
- @shlExact(value: T, shift_amt: Log2T) -> T
+ @shlExact(value: T, shift_amt: Log2T) T
Performs the left shift operation (<<). Caller guarantees
that the shift will not shift any 1 bits out.
@@ -4794,7 +4794,7 @@ pub const FloatMode = enum {
{#see_also|@shrExact|@shlWithOverflow#}
{#header_close#}
{#header_open|@shlWithOverflow#}
-
@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: &T) -> bool
+ @shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: *T) bool
Performs result.* = a << b. If overflow or underflow occurs,
stores the overflowed bits in result and returns true.
@@ -4807,7 +4807,7 @@ pub const FloatMode = enum {
{#see_also|@shlExact|@shrExact#}
{#header_close#}
{#header_open|@shrExact#}
-
@shrExact(value: T, shift_amt: Log2T) -> T
+ @shrExact(value: T, shift_amt: Log2T) T
Performs the right shift operation (>>). Caller guarantees
that the shift will not shift any 1 bits out.
@@ -4819,7 +4819,7 @@ pub const FloatMode = enum {
{#see_also|@shlExact|@shlWithOverflow#}
{#header_close#}
{#header_open|@sizeOf#}
-
@sizeOf(comptime T: type) -> (number literal)
+ @sizeOf(comptime T: type) (number literal)
This function returns the number of bytes it takes to store T in memory.
@@ -4828,7 +4828,7 @@ pub const FloatMode = enum {
{#header_close#}
{#header_open|@sqrt#}
- @sqrt(comptime T: type, value: T) -> T
+ @sqrt(comptime T: type, value: T) T
Performs the square root of a floating point number. Uses a dedicated hardware instruction
when available. Currently only supports f32 and f64 at runtime. f128 at runtime is TODO.
@@ -4838,7 +4838,7 @@ pub const FloatMode = enum {
{#header_close#}
{#header_open|@subWithOverflow#}
- @subWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool
+ @subWithOverflow(comptime T: type, a: T, b: T, result: *T) bool
Performs result.* = a - b. If overflow or underflow occurs,
stores the overflowed bits in result and returns true.
@@ -4846,7 +4846,7 @@ pub const FloatMode = enum {
{#header_close#}
{#header_open|@truncate#}
- @truncate(comptime T: type, integer) -> T
+ @truncate(comptime T: type, integer) T
This function truncates bits from an integer type, resulting in a smaller
integer type.
@@ -4870,7 +4870,7 @@ const b: u8 = @truncate(u8, a);
{#header_close#}
{#header_open|@typeId#}
-
@typeId(comptime T: type) -> @import("builtin").TypeId
+ @typeId(comptime T: type) @import("builtin").TypeId
Returns which kind of type something is. Possible values:
@@ -4904,7 +4904,7 @@ pub const TypeId = enum {
{#code_end#}
{#header_close#}
{#header_open|@typeInfo#}
- @typeInfo(comptime T: type) -> @import("builtin").TypeInfo
+ @typeInfo(comptime T: type) @import("builtin").TypeInfo
Returns information on the type. Returns a value of the following union:
@@ -5080,14 +5080,14 @@ pub const TypeInfo = union(TypeId) {
{#code_end#}
{#header_close#}
{#header_open|@typeName#}
- @typeName(T: type) -> []u8
+ @typeName(T: type) []u8
This function returns the string representation of a type.
{#header_close#}
{#header_open|@typeOf#}
- @typeOf(expression) -> type
+ @typeOf(expression) type
This function returns a compile-time constant, which is the type of the
expression passed as an argument. The expression is evaluated.
@@ -5937,7 +5937,7 @@ pub const __zig_test_fn_slice = {}; // overwritten later
{#header_open|C String Literals#}
{#code_begin|exe#}
{#link_libc#}
-extern fn puts(&const u8) void;
+extern fn puts(*const u8) void;
pub fn main() void {
puts(c"this has a null terminator");
@@ -5996,8 +5996,8 @@ const c = @cImport({
{#code_begin|syntax#}
const base64 = @import("std").base64;
-export fn decode_base_64(dest_ptr: &u8, dest_len: usize,
- source_ptr: &const u8, source_len: usize) usize
+export fn decode_base_64(dest_ptr: *u8, dest_len: usize,
+ source_ptr: *const u8, source_len: usize) usize
{
const src = source_ptr[0..source_len];
const dest = dest_ptr[0..dest_len];
@@ -6028,7 +6028,7 @@ int main(int argc, char **argv) {
{#code_begin|syntax#}
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const obj = b.addObject("base64", "base64.zig");
const exe = b.addCExecutable("test");
diff --git a/example/cat/main.zig b/example/cat/main.zig
index de0d323bed..1b34cb22eb 100644
--- a/example/cat/main.zig
+++ b/example/cat/main.zig
@@ -41,7 +41,7 @@ fn usage(exe: []const u8) !void {
return error.Invalid;
}
-fn cat_file(stdout: &os.File, file: &os.File) !void {
+fn cat_file(stdout: *os.File, file: *os.File) !void {
var buf: [1024 * 4]u8 = undefined;
while (true) {
diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig
index 1df8f04ce4..f64beda40f 100644
--- a/example/hello_world/hello_libc.zig
+++ b/example/hello_world/hello_libc.zig
@@ -7,7 +7,7 @@ const c = @cImport({
const msg = c"Hello, world!\n";
-export fn main(argc: c_int, argv: &&u8) c_int {
+export fn main(argc: c_int, argv: **u8) c_int {
if (c.printf(msg) != c_int(c.strlen(msg))) return -1;
return 0;
diff --git a/example/mix_o_files/base64.zig b/example/mix_o_files/base64.zig
index e682a97055..35b090825b 100644
--- a/example/mix_o_files/base64.zig
+++ b/example/mix_o_files/base64.zig
@@ -1,6 +1,6 @@
const base64 = @import("std").base64;
-export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) usize {
+export fn decode_base_64(dest_ptr: *u8, dest_len: usize, source_ptr: *const u8, source_len: usize) usize {
const src = source_ptr[0..source_len];
const dest = dest_ptr[0..dest_len];
const base64_decoder = base64.standard_decoder_unsafe;
diff --git a/example/mix_o_files/build.zig b/example/mix_o_files/build.zig
index e5d2e6a446..a4e7fbbf8f 100644
--- a/example/mix_o_files/build.zig
+++ b/example/mix_o_files/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const obj = b.addObject("base64", "base64.zig");
const exe = b.addCExecutable("test");
diff --git a/example/shared_library/build.zig b/example/shared_library/build.zig
index 30c714c6c6..05648cf9eb 100644
--- a/example/shared_library/build.zig
+++ b/example/shared_library/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
const exe = b.addCExecutable("test");
diff --git a/src-self-hosted/arg.zig b/src-self-hosted/arg.zig
index fa2166e3a5..df2c04ef1f 100644
--- a/src-self-hosted/arg.zig
+++ b/src-self-hosted/arg.zig
@@ -30,7 +30,7 @@ fn argInAllowedSet(maybe_set: ?[]const []const u8, arg: []const u8) bool {
}
// Modifies the current argument index during iteration
-fn readFlagArguments(allocator: &Allocator, args: []const []const u8, required: usize, allowed_set: ?[]const []const u8, index: &usize) !FlagArg {
+fn readFlagArguments(allocator: *Allocator, args: []const []const u8, required: usize, allowed_set: ?[]const []const u8, index: *usize) !FlagArg {
switch (required) {
0 => return FlagArg{ .None = undefined }, // TODO: Required to force non-tag but value?
1 => {
@@ -79,7 +79,7 @@ pub const Args = struct {
flags: HashMapFlags,
positionals: ArrayList([]const u8),
- pub fn parse(allocator: &Allocator, comptime spec: []const Flag, args: []const []const u8) !Args {
+ pub fn parse(allocator: *Allocator, comptime spec: []const Flag, args: []const []const u8) !Args {
var parsed = Args{
.flags = HashMapFlags.init(allocator),
.positionals = ArrayList([]const u8).init(allocator),
@@ -143,18 +143,18 @@ pub const Args = struct {
return parsed;
}
- pub fn deinit(self: &Args) void {
+ pub fn deinit(self: *Args) void {
self.flags.deinit();
self.positionals.deinit();
}
// e.g. --help
- pub fn present(self: &Args, name: []const u8) bool {
+ pub fn present(self: *Args, name: []const u8) bool {
return self.flags.contains(name);
}
// e.g. --name value
- pub fn single(self: &Args, name: []const u8) ?[]const u8 {
+ pub fn single(self: *Args, name: []const u8) ?[]const u8 {
if (self.flags.get(name)) |entry| {
switch (entry.value) {
FlagArg.Single => |inner| {
@@ -168,7 +168,7 @@ pub const Args = struct {
}
// e.g. --names value1 value2 value3
- pub fn many(self: &Args, name: []const u8) ?[]const []const u8 {
+ pub fn many(self: *Args, name: []const u8) ?[]const []const u8 {
if (self.flags.get(name)) |entry| {
switch (entry.value) {
FlagArg.Many => |inner| {
diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig
index 9905b8e3a6..32d2450aac 100644
--- a/src-self-hosted/errmsg.zig
+++ b/src-self-hosted/errmsg.zig
@@ -16,18 +16,18 @@ pub const Msg = struct {
text: []u8,
first_token: TokenIndex,
last_token: TokenIndex,
- tree: &ast.Tree,
+ tree: *ast.Tree,
};
/// `path` must outlive the returned Msg
/// `tree` must outlive the returned Msg
/// Caller owns returned Msg and must free with `allocator`
pub fn createFromParseError(
- allocator: &mem.Allocator,
- parse_error: &const ast.Error,
- tree: &ast.Tree,
+ allocator: *mem.Allocator,
+ parse_error: *const ast.Error,
+ tree: *ast.Tree,
path: []const u8,
-) !&Msg {
+) !*Msg {
const loc_token = parse_error.loc();
var text_buf = try std.Buffer.initSize(allocator, 0);
defer text_buf.deinit();
@@ -47,7 +47,7 @@ pub fn createFromParseError(
return msg;
}
-pub fn printToStream(stream: var, msg: &const Msg, color_on: bool) !void {
+pub fn printToStream(stream: var, msg: *const Msg, color_on: bool) !void {
const first_token = msg.tree.tokens.at(msg.first_token);
const last_token = msg.tree.tokens.at(msg.last_token);
const start_loc = msg.tree.tokenLocationPtr(0, first_token);
@@ -76,7 +76,7 @@ pub fn printToStream(stream: var, msg: &const Msg, color_on: bool) !void {
try stream.write("\n");
}
-pub fn printToFile(file: &os.File, msg: &const Msg, color: Color) !void {
+pub fn printToFile(file: *os.File, msg: *const Msg, color: Color) !void {
const color_on = switch (color) {
Color.Auto => file.isTty(),
Color.On => true,
diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig
index adab00286b..56b56c0c78 100644
--- a/src-self-hosted/introspect.zig
+++ b/src-self-hosted/introspect.zig
@@ -7,7 +7,7 @@ const os = std.os;
const warn = std.debug.warn;
/// Caller must free result
-pub fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) ![]u8 {
+pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 {
const test_zig_dir = try os.path.join(allocator, test_path, "lib", "zig");
errdefer allocator.free(test_zig_dir);
@@ -21,7 +21,7 @@ pub fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) ![
}
/// Caller must free result
-pub fn findZigLibDir(allocator: &mem.Allocator) ![]u8 {
+pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 {
const self_exe_path = try os.selfExeDirPath(allocator);
defer allocator.free(self_exe_path);
@@ -42,7 +42,7 @@ pub fn findZigLibDir(allocator: &mem.Allocator) ![]u8 {
return error.FileNotFound;
}
-pub fn resolveZigLibDir(allocator: &mem.Allocator) ![]u8 {
+pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 {
return findZigLibDir(allocator) catch |err| {
warn(
\\Unable to find zig lib directory: {}.
diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig
index c4550b5179..3334d9511b 100644
--- a/src-self-hosted/ir.zig
+++ b/src-self-hosted/ir.zig
@@ -2,7 +2,7 @@ const Scope = @import("scope.zig").Scope;
pub const Instruction = struct {
id: Id,
- scope: &Scope,
+ scope: *Scope,
pub const Id = enum {
Br,
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index 71838503b7..80b1c3889a 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -18,8 +18,8 @@ const Target = @import("target.zig").Target;
const errmsg = @import("errmsg.zig");
var stderr_file: os.File = undefined;
-var stderr: &io.OutStream(io.FileOutStream.Error) = undefined;
-var stdout: &io.OutStream(io.FileOutStream.Error) = undefined;
+var stderr: *io.OutStream(io.FileOutStream.Error) = undefined;
+var stdout: *io.OutStream(io.FileOutStream.Error) = undefined;
const usage =
\\usage: zig [command] [options]
@@ -43,7 +43,7 @@ const usage =
const Command = struct {
name: []const u8,
- exec: fn (&Allocator, []const []const u8) error!void,
+ exec: fn (*Allocator, []const []const u8) error!void,
};
pub fn main() !void {
@@ -191,7 +191,7 @@ const missing_build_file =
\\
;
-fn cmdBuild(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdBuild(allocator: *Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_build_spec, args);
defer flags.deinit();
@@ -426,7 +426,7 @@ const args_build_generic = []Flag{
Flag.Arg1("--ver-patch"),
};
-fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Module.Kind) !void {
+fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Module.Kind) !void {
var flags = try Args.parse(allocator, args_build_generic, args);
defer flags.deinit();
@@ -661,19 +661,19 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
try stderr.print("building {}: {}\n", @tagName(out_type), in_file);
}
-fn cmdBuildExe(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdBuildExe(allocator: *Allocator, args: []const []const u8) !void {
try buildOutputType(allocator, args, Module.Kind.Exe);
}
// cmd:build-lib ///////////////////////////////////////////////////////////////////////////////////
-fn cmdBuildLib(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdBuildLib(allocator: *Allocator, args: []const []const u8) !void {
try buildOutputType(allocator, args, Module.Kind.Lib);
}
// cmd:build-obj ///////////////////////////////////////////////////////////////////////////////////
-fn cmdBuildObj(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdBuildObj(allocator: *Allocator, args: []const []const u8) !void {
try buildOutputType(allocator, args, Module.Kind.Obj);
}
@@ -700,7 +700,7 @@ const args_fmt_spec = []Flag{
}),
};
-fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_fmt_spec, args);
defer flags.deinit();
@@ -768,7 +768,7 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
// cmd:targets /////////////////////////////////////////////////////////////////////////////////////
-fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void {
try stdout.write("Architectures:\n");
{
comptime var i: usize = 0;
@@ -810,7 +810,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
// cmd:version /////////////////////////////////////////////////////////////////////////////////////
-fn cmdVersion(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING));
}
@@ -827,7 +827,7 @@ const usage_test =
const args_test_spec = []Flag{Flag.Bool("--help")};
-fn cmdTest(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdTest(allocator: *Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_build_spec, args);
defer flags.deinit();
@@ -862,7 +862,7 @@ const usage_run =
const args_run_spec = []Flag{Flag.Bool("--help")};
-fn cmdRun(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdRun(allocator: *Allocator, args: []const []const u8) !void {
var compile_args = args;
var runtime_args: []const []const u8 = []const []const u8{};
@@ -912,7 +912,7 @@ const args_translate_c_spec = []Flag{
Flag.Arg1("--output"),
};
-fn cmdTranslateC(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdTranslateC(allocator: *Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_translate_c_spec, args);
defer flags.deinit();
@@ -958,7 +958,7 @@ fn cmdTranslateC(allocator: &Allocator, args: []const []const u8) !void {
// cmd:help ////////////////////////////////////////////////////////////////////////////////////////
-fn cmdHelp(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdHelp(allocator: *Allocator, args: []const []const u8) !void {
try stderr.write(usage);
}
@@ -981,7 +981,7 @@ const info_zen =
\\
;
-fn cmdZen(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdZen(allocator: *Allocator, args: []const []const u8) !void {
try stdout.write(info_zen);
}
@@ -996,7 +996,7 @@ const usage_internal =
\\
;
-fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {
if (args.len == 0) {
try stderr.write(usage_internal);
os.exit(1);
@@ -1018,7 +1018,7 @@ fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {
try stderr.write(usage_internal);
}
-fn cmdInternalBuildInfo(allocator: &Allocator, args: []const []const u8) !void {
+fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {
try stdout.print(
\\ZIG_CMAKE_BINARY_DIR {}
\\ZIG_CXX_COMPILER {}
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index 61834eab66..a7ddf3f9e9 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -13,7 +13,7 @@ const ArrayList = std.ArrayList;
const errmsg = @import("errmsg.zig");
pub const Module = struct {
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
name: Buffer,
root_src_path: ?[]const u8,
module: llvm.ModuleRef,
@@ -53,8 +53,8 @@ pub const Module = struct {
windows_subsystem_windows: bool,
windows_subsystem_console: bool,
- link_libs_list: ArrayList(&LinkLib),
- libc_link_lib: ?&LinkLib,
+ link_libs_list: ArrayList(*LinkLib),
+ libc_link_lib: ?*LinkLib,
err_color: errmsg.Color,
@@ -106,19 +106,19 @@ pub const Module = struct {
pub const CliPkg = struct {
name: []const u8,
path: []const u8,
- children: ArrayList(&CliPkg),
- parent: ?&CliPkg,
+ children: ArrayList(*CliPkg),
+ parent: ?*CliPkg,
- pub fn init(allocator: &mem.Allocator, name: []const u8, path: []const u8, parent: ?&CliPkg) !&CliPkg {
+ pub fn init(allocator: *mem.Allocator, name: []const u8, path: []const u8, parent: ?*CliPkg) !*CliPkg {
var pkg = try allocator.create(CliPkg);
pkg.name = name;
pkg.path = path;
- pkg.children = ArrayList(&CliPkg).init(allocator);
+ pkg.children = ArrayList(*CliPkg).init(allocator);
pkg.parent = parent;
return pkg;
}
- pub fn deinit(self: &CliPkg) void {
+ pub fn deinit(self: *CliPkg) void {
for (self.children.toSliceConst()) |child| {
child.deinit();
}
@@ -126,7 +126,7 @@ pub const Module = struct {
}
};
- pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !&Module {
+ pub fn create(allocator: *mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: *const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !*Module {
var name_buffer = try Buffer.init(allocator, name);
errdefer name_buffer.deinit();
@@ -188,7 +188,7 @@ pub const Module = struct {
.link_objects = [][]const u8{},
.windows_subsystem_windows = false,
.windows_subsystem_console = false,
- .link_libs_list = ArrayList(&LinkLib).init(allocator),
+ .link_libs_list = ArrayList(*LinkLib).init(allocator),
.libc_link_lib = null,
.err_color = errmsg.Color.Auto,
.darwin_frameworks = [][]const u8{},
@@ -200,11 +200,11 @@ pub const Module = struct {
return module_ptr;
}
- fn dump(self: &Module) void {
+ fn dump(self: *Module) void {
c.LLVMDumpModule(self.module);
}
- pub fn destroy(self: &Module) void {
+ pub fn destroy(self: *Module) void {
c.LLVMDisposeBuilder(self.builder);
c.LLVMDisposeModule(self.module);
c.LLVMContextDispose(self.context);
@@ -213,7 +213,7 @@ pub const Module = struct {
self.allocator.destroy(self);
}
- pub fn build(self: &Module) !void {
+ pub fn build(self: *Module) !void {
if (self.llvm_argv.len != 0) {
var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.allocator, [][]const []const u8{
[][]const u8{"zig (LLVM option parsing)"},
@@ -259,12 +259,12 @@ pub const Module = struct {
self.dump();
}
- pub fn link(self: &Module, out_file: ?[]const u8) !void {
+ pub fn link(self: *Module, out_file: ?[]const u8) !void {
warn("TODO link");
return error.Todo;
}
- pub fn addLinkLib(self: &Module, name: []const u8, provided_explicitly: bool) !&LinkLib {
+ pub fn addLinkLib(self: *Module, name: []const u8, provided_explicitly: bool) !*LinkLib {
const is_libc = mem.eql(u8, name, "c");
if (is_libc) {
diff --git a/src-self-hosted/scope.zig b/src-self-hosted/scope.zig
index 05e586daae..b73dcb4ed3 100644
--- a/src-self-hosted/scope.zig
+++ b/src-self-hosted/scope.zig
@@ -1,6 +1,6 @@
pub const Scope = struct {
id: Id,
- parent: &Scope,
+ parent: *Scope,
pub const Id = enum {
Decls,
diff --git a/src-self-hosted/target.zig b/src-self-hosted/target.zig
index 7983a3ddec..724d99ea23 100644
--- a/src-self-hosted/target.zig
+++ b/src-self-hosted/target.zig
@@ -11,7 +11,7 @@ pub const Target = union(enum) {
Native,
Cross: CrossTarget,
- pub fn oFileExt(self: &const Target) []const u8 {
+ pub fn oFileExt(self: *const Target) []const u8 {
const environ = switch (self.*) {
Target.Native => builtin.environ,
Target.Cross => |t| t.environ,
@@ -22,28 +22,28 @@ pub const Target = union(enum) {
};
}
- pub fn exeFileExt(self: &const Target) []const u8 {
+ pub fn exeFileExt(self: *const Target) []const u8 {
return switch (self.getOs()) {
builtin.Os.windows => ".exe",
else => "",
};
}
- pub fn getOs(self: &const Target) builtin.Os {
+ pub fn getOs(self: *const Target) builtin.Os {
return switch (self.*) {
Target.Native => builtin.os,
Target.Cross => |t| t.os,
};
}
- pub fn isDarwin(self: &const Target) bool {
+ pub fn isDarwin(self: *const Target) bool {
return switch (self.getOs()) {
builtin.Os.ios, builtin.Os.macosx => true,
else => false,
};
}
- pub fn isWindows(self: &const Target) bool {
+ pub fn isWindows(self: *const Target) bool {
return switch (self.getOs()) {
builtin.Os.windows => true,
else => false,
diff --git a/src/all_types.hpp b/src/all_types.hpp
index 9c156fb58b..b9199c2757 100644
--- a/src/all_types.hpp
+++ b/src/all_types.hpp
@@ -374,7 +374,7 @@ enum NodeType {
NodeTypeCharLiteral,
NodeTypeSymbol,
NodeTypePrefixOpExpr,
- NodeTypeAddrOfExpr,
+ NodeTypePointerType,
NodeTypeFnCallExpr,
NodeTypeArrayAccessExpr,
NodeTypeSliceExpr,
@@ -616,6 +616,7 @@ enum PrefixOp {
PrefixOpNegationWrap,
PrefixOpMaybe,
PrefixOpUnwrapMaybe,
+ PrefixOpAddrOf,
};
struct AstNodePrefixOpExpr {
@@ -623,7 +624,7 @@ struct AstNodePrefixOpExpr {
AstNode *primary_expr;
};
-struct AstNodeAddrOfExpr {
+struct AstNodePointerType {
AstNode *align_expr;
BigInt *bit_offset_start;
BigInt *bit_offset_end;
@@ -899,7 +900,7 @@ struct AstNode {
AstNodeBinOpExpr bin_op_expr;
AstNodeCatchExpr unwrap_err_expr;
AstNodePrefixOpExpr prefix_op_expr;
- AstNodeAddrOfExpr addr_of_expr;
+ AstNodePointerType pointer_type;
AstNodeFnCallExpr fn_call_expr;
AstNodeArrayAccessExpr array_access_expr;
AstNodeSliceExpr slice_expr;
@@ -2053,7 +2054,7 @@ enum IrInstructionId {
IrInstructionIdTypeInfo,
IrInstructionIdTypeId,
IrInstructionIdSetEvalBranchQuota,
- IrInstructionIdPtrTypeOf,
+ IrInstructionIdPtrType,
IrInstructionIdAlignCast,
IrInstructionIdOpaqueType,
IrInstructionIdSetAlignStack,
@@ -2412,6 +2413,17 @@ struct IrInstructionArrayType {
IrInstruction *child_type;
};
+struct IrInstructionPtrType {
+ IrInstruction base;
+
+ IrInstruction *align_value;
+ IrInstruction *child_type;
+ uint32_t bit_offset_start;
+ uint32_t bit_offset_end;
+ bool is_const;
+ bool is_volatile;
+};
+
struct IrInstructionPromiseType {
IrInstruction base;
@@ -2891,17 +2903,6 @@ struct IrInstructionSetEvalBranchQuota {
IrInstruction *new_quota;
};
-struct IrInstructionPtrTypeOf {
- IrInstruction base;
-
- IrInstruction *align_value;
- IrInstruction *child_type;
- uint32_t bit_offset_start;
- uint32_t bit_offset_end;
- bool is_const;
- bool is_volatile;
-};
-
struct IrInstructionAlignCast {
IrInstruction base;
diff --git a/src/analyze.cpp b/src/analyze.cpp
index b00e18a9a1..a5011035c5 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -418,12 +418,12 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
const char *volatile_str = is_volatile ? "volatile " : "";
buf_resize(&entry->name, 0);
if (unaligned_bit_count == 0 && byte_alignment == abi_alignment) {
- buf_appendf(&entry->name, "&%s%s%s", const_str, volatile_str, buf_ptr(&child_type->name));
+ buf_appendf(&entry->name, "*%s%s%s", const_str, volatile_str, buf_ptr(&child_type->name));
} else if (unaligned_bit_count == 0) {
- buf_appendf(&entry->name, "&align(%" PRIu32 ") %s%s%s", byte_alignment,
+ buf_appendf(&entry->name, "*align(%" PRIu32 ") %s%s%s", byte_alignment,
const_str, volatile_str, buf_ptr(&child_type->name));
} else {
- buf_appendf(&entry->name, "&align(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s", byte_alignment,
+ buf_appendf(&entry->name, "*align(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s", byte_alignment,
bit_offset, bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));
}
@@ -3270,7 +3270,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
case NodeTypeThisLiteral:
case NodeTypeSymbol:
case NodeTypePrefixOpExpr:
- case NodeTypeAddrOfExpr:
+ case NodeTypePointerType:
case NodeTypeIfBoolExpr:
case NodeTypeWhileExpr:
case NodeTypeForExpr:
diff --git a/src/ast_render.cpp b/src/ast_render.cpp
index 5a1e81b36d..f356f406b0 100644
--- a/src/ast_render.cpp
+++ b/src/ast_render.cpp
@@ -68,6 +68,7 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
case PrefixOpBinNot: return "~";
case PrefixOpMaybe: return "?";
case PrefixOpUnwrapMaybe: return "??";
+ case PrefixOpAddrOf: return "&";
}
zig_unreachable();
}
@@ -185,8 +186,6 @@ static const char *node_type_str(NodeType node_type) {
return "Symbol";
case NodeTypePrefixOpExpr:
return "PrefixOpExpr";
- case NodeTypeAddrOfExpr:
- return "AddrOfExpr";
case NodeTypeUse:
return "Use";
case NodeTypeBoolLiteral:
@@ -251,6 +250,8 @@ static const char *node_type_str(NodeType node_type) {
return "Suspend";
case NodeTypePromiseType:
return "PromiseType";
+ case NodeTypePointerType:
+ return "PointerType";
}
zig_unreachable();
}
@@ -616,41 +617,41 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
fprintf(ar->f, "%s", prefix_op_str(op));
AstNode *child_node = node->data.prefix_op_expr.primary_expr;
- bool new_grouped = child_node->type == NodeTypePrefixOpExpr || child_node->type == NodeTypeAddrOfExpr;
+ bool new_grouped = child_node->type == NodeTypePrefixOpExpr || child_node->type == NodeTypePointerType;
render_node_extra(ar, child_node, new_grouped);
if (!grouped) fprintf(ar->f, ")");
break;
}
- case NodeTypeAddrOfExpr:
+ case NodeTypePointerType:
{
if (!grouped) fprintf(ar->f, "(");
- fprintf(ar->f, "&");
- if (node->data.addr_of_expr.align_expr != nullptr) {
+ fprintf(ar->f, "*");
+ if (node->data.pointer_type.align_expr != nullptr) {
fprintf(ar->f, "align(");
- render_node_grouped(ar, node->data.addr_of_expr.align_expr);
- if (node->data.addr_of_expr.bit_offset_start != nullptr) {
- assert(node->data.addr_of_expr.bit_offset_end != nullptr);
+ render_node_grouped(ar, node->data.pointer_type.align_expr);
+ if (node->data.pointer_type.bit_offset_start != nullptr) {
+ assert(node->data.pointer_type.bit_offset_end != nullptr);
Buf offset_start_buf = BUF_INIT;
buf_resize(&offset_start_buf, 0);
- bigint_append_buf(&offset_start_buf, node->data.addr_of_expr.bit_offset_start, 10);
+ bigint_append_buf(&offset_start_buf, node->data.pointer_type.bit_offset_start, 10);
Buf offset_end_buf = BUF_INIT;
buf_resize(&offset_end_buf, 0);
- bigint_append_buf(&offset_end_buf, node->data.addr_of_expr.bit_offset_end, 10);
+ bigint_append_buf(&offset_end_buf, node->data.pointer_type.bit_offset_end, 10);
fprintf(ar->f, ":%s:%s ", buf_ptr(&offset_start_buf), buf_ptr(&offset_end_buf));
}
fprintf(ar->f, ") ");
}
- if (node->data.addr_of_expr.is_const) {
+ if (node->data.pointer_type.is_const) {
fprintf(ar->f, "const ");
}
- if (node->data.addr_of_expr.is_volatile) {
+ if (node->data.pointer_type.is_volatile) {
fprintf(ar->f, "volatile ");
}
- render_node_ungrouped(ar, node->data.addr_of_expr.op_expr);
+ render_node_ungrouped(ar, node->data.pointer_type.op_expr);
if (!grouped) fprintf(ar->f, ")");
break;
}
@@ -669,7 +670,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
fprintf(ar->f, " ");
}
AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
- bool grouped = (fn_ref_node->type != NodeTypePrefixOpExpr && fn_ref_node->type != NodeTypeAddrOfExpr);
+ bool grouped = (fn_ref_node->type != NodeTypePrefixOpExpr && fn_ref_node->type != NodeTypePointerType);
render_node_extra(ar, fn_ref_node, grouped);
fprintf(ar->f, "(");
for (size_t i = 0; i < node->data.fn_call_expr.params.length; i += 1) {
diff --git a/src/codegen.cpp b/src/codegen.cpp
index 69542b3e67..d07d427729 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -4600,7 +4600,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdTypeInfo:
case IrInstructionIdTypeId:
case IrInstructionIdSetEvalBranchQuota:
- case IrInstructionIdPtrTypeOf:
+ case IrInstructionIdPtrType:
case IrInstructionIdOpaqueType:
case IrInstructionIdSetAlignStack:
case IrInstructionIdArgType:
diff --git a/src/ir.cpp b/src/ir.cpp
index 6e944a8976..b1fac9f485 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -41,10 +41,6 @@ struct IrAnalyze {
static const LVal LVAL_NONE = { false, false, false };
static const LVal LVAL_PTR = { true, false, false };
-static LVal make_lval_addr(bool is_const, bool is_volatile) {
- return { true, is_const, is_volatile };
-}
-
enum ConstCastResultId {
ConstCastResultIdOk,
ConstCastResultIdErrSet,
@@ -629,8 +625,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionSetEvalBranchQuo
return IrInstructionIdSetEvalBranchQuota;
}
-static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrTypeOf *) {
- return IrInstructionIdPtrTypeOf;
+static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrType *) {
+ return IrInstructionIdPtrType;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionAlignCast *) {
@@ -1196,11 +1192,11 @@ static IrInstruction *ir_build_br_from(IrBuilder *irb, IrInstruction *old_instru
return new_instruction;
}
-static IrInstruction *ir_build_ptr_type_of(IrBuilder *irb, Scope *scope, AstNode *source_node,
+static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
IrInstruction *child_type, bool is_const, bool is_volatile, IrInstruction *align_value,
uint32_t bit_offset_start, uint32_t bit_offset_end)
{
- IrInstructionPtrTypeOf *ptr_type_of_instruction = ir_build_instruction(irb, scope, source_node);
+ IrInstructionPtrType *ptr_type_of_instruction = ir_build_instruction(irb, scope, source_node);
ptr_type_of_instruction->align_value = align_value;
ptr_type_of_instruction->child_type = child_type;
ptr_type_of_instruction->is_const = is_const;
@@ -4609,14 +4605,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
}
static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, Scope *scope, AstNode *node, IrUnOp op_id, LVal lval) {
- AstNode *expr_node;
- if (node->type == NodeTypePrefixOpExpr) {
- expr_node = node->data.prefix_op_expr.primary_expr;
- } else if (node->type == NodeTypePtrDeref) {
- expr_node = node->data.ptr_deref_expr.target;
- } else {
- zig_unreachable();
- }
+ assert(node->type == NodeTypePrefixOpExpr);
+ AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
if (value == irb->codegen->invalid_instruction)
@@ -4640,16 +4630,12 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
return ir_build_ref(irb, scope, value->source_node, value, lval.is_const, lval.is_volatile);
}
-static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *node) {
- assert(node->type == NodeTypeAddrOfExpr);
- bool is_const = node->data.addr_of_expr.is_const;
- bool is_volatile = node->data.addr_of_expr.is_volatile;
- AstNode *expr_node = node->data.addr_of_expr.op_expr;
- AstNode *align_expr = node->data.addr_of_expr.align_expr;
-
- if (align_expr == nullptr && !is_const && !is_volatile) {
- return ir_gen_node_extra(irb, expr_node, scope, make_lval_addr(is_const, is_volatile));
- }
+static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode *node) {
+ assert(node->type == NodeTypePointerType);
+ bool is_const = node->data.pointer_type.is_const;
+ bool is_volatile = node->data.pointer_type.is_volatile;
+ AstNode *expr_node = node->data.pointer_type.op_expr;
+ AstNode *align_expr = node->data.pointer_type.align_expr;
IrInstruction *align_value;
if (align_expr != nullptr) {
@@ -4665,27 +4651,27 @@ static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *n
return child_type;
uint32_t bit_offset_start = 0;
- if (node->data.addr_of_expr.bit_offset_start != nullptr) {
- if (!bigint_fits_in_bits(node->data.addr_of_expr.bit_offset_start, 32, false)) {
+ if (node->data.pointer_type.bit_offset_start != nullptr) {
+ if (!bigint_fits_in_bits(node->data.pointer_type.bit_offset_start, 32, false)) {
Buf *val_buf = buf_alloc();
- bigint_append_buf(val_buf, node->data.addr_of_expr.bit_offset_start, 10);
+ bigint_append_buf(val_buf, node->data.pointer_type.bit_offset_start, 10);
exec_add_error_node(irb->codegen, irb->exec, node,
buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf)));
return irb->codegen->invalid_instruction;
}
- bit_offset_start = bigint_as_unsigned(node->data.addr_of_expr.bit_offset_start);
+ bit_offset_start = bigint_as_unsigned(node->data.pointer_type.bit_offset_start);
}
uint32_t bit_offset_end = 0;
- if (node->data.addr_of_expr.bit_offset_end != nullptr) {
- if (!bigint_fits_in_bits(node->data.addr_of_expr.bit_offset_end, 32, false)) {
+ if (node->data.pointer_type.bit_offset_end != nullptr) {
+ if (!bigint_fits_in_bits(node->data.pointer_type.bit_offset_end, 32, false)) {
Buf *val_buf = buf_alloc();
- bigint_append_buf(val_buf, node->data.addr_of_expr.bit_offset_end, 10);
+ bigint_append_buf(val_buf, node->data.pointer_type.bit_offset_end, 10);
exec_add_error_node(irb->codegen, irb->exec, node,
buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf)));
return irb->codegen->invalid_instruction;
}
- bit_offset_end = bigint_as_unsigned(node->data.addr_of_expr.bit_offset_end);
+ bit_offset_end = bigint_as_unsigned(node->data.pointer_type.bit_offset_end);
}
if ((bit_offset_start != 0 || bit_offset_end != 0) && bit_offset_start >= bit_offset_end) {
@@ -4694,7 +4680,7 @@ static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *n
return irb->codegen->invalid_instruction;
}
- return ir_build_ptr_type_of(irb, scope, node, child_type, is_const, is_volatile,
+ return ir_build_ptr_type(irb, scope, node, child_type, is_const, is_volatile,
align_value, bit_offset_start, bit_offset_end);
}
@@ -4761,6 +4747,10 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod
return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpMaybe), lval);
case PrefixOpUnwrapMaybe:
return ir_gen_maybe_assert_ok(irb, scope, node, lval);
+ case PrefixOpAddrOf: {
+ AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
+ return ir_lval_wrap(irb, scope, ir_gen_node_extra(irb, expr_node, scope, LVAL_PTR), lval);
+ }
}
zig_unreachable();
}
@@ -6568,8 +6558,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
return ir_lval_wrap(irb, scope, ir_gen_if_bool_expr(irb, scope, node), lval);
case NodeTypePrefixOpExpr:
return ir_gen_prefix_op_expr(irb, scope, node, lval);
- case NodeTypeAddrOfExpr:
- return ir_lval_wrap(irb, scope, ir_gen_address_of(irb, scope, node), lval);
case NodeTypeContainerInitExpr:
return ir_lval_wrap(irb, scope, ir_gen_container_init_expr(irb, scope, node), lval);
case NodeTypeVariableDeclaration:
@@ -6592,14 +6580,23 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
return ir_build_load_ptr(irb, scope, node, ptr_instruction);
}
- case NodeTypePtrDeref:
- return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpDereference, lval);
+ case NodeTypePtrDeref: {
+ assert(node->type == NodeTypePtrDeref);
+ AstNode *expr_node = node->data.ptr_deref_expr.target;
+ IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
+ if (value == irb->codegen->invalid_instruction)
+ return value;
+
+ return ir_build_un_op(irb, scope, node, IrUnOpDereference, value);
+ }
case NodeTypeThisLiteral:
return ir_lval_wrap(irb, scope, ir_gen_this_literal(irb, scope, node), lval);
case NodeTypeBoolLiteral:
return ir_lval_wrap(irb, scope, ir_gen_bool_literal(irb, scope, node), lval);
case NodeTypeArrayType:
return ir_lval_wrap(irb, scope, ir_gen_array_type(irb, scope, node), lval);
+ case NodeTypePointerType:
+ return ir_lval_wrap(irb, scope, ir_gen_pointer_type(irb, scope, node), lval);
case NodeTypePromiseType:
return ir_lval_wrap(irb, scope, ir_gen_promise_type(irb, scope, node), lval);
case NodeTypeStringLiteral:
@@ -8961,6 +8958,7 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
ConstExprValue *pointee, TypeTableEntry *pointee_type,
ConstPtrMut ptr_mut, bool ptr_is_const, bool ptr_is_volatile, uint32_t ptr_align)
{
+ // TODO remove this special case for types
if (pointee_type->id == TypeTableEntryIdMetaType) {
TypeTableEntry *type_entry = pointee->data.x_type;
if (type_entry->id == TypeTableEntryIdUnreachable) {
@@ -18778,11 +18776,16 @@ static TypeTableEntry *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstr
return usize;
}
-static TypeTableEntry *ir_analyze_instruction_ptr_type_of(IrAnalyze *ira, IrInstructionPtrTypeOf *instruction) {
+static TypeTableEntry *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtrType *instruction) {
TypeTableEntry *child_type = ir_resolve_type(ira, instruction->child_type->other);
if (type_is_invalid(child_type))
return ira->codegen->builtin_types.entry_invalid;
+ if (child_type->id == TypeTableEntryIdUnreachable) {
+ ir_add_error(ira, &instruction->base, buf_sprintf("pointer to noreturn not allowed"));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+
uint32_t align_bytes;
if (instruction->align_value != nullptr) {
if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))
@@ -19606,8 +19609,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction);
case IrInstructionIdSetEvalBranchQuota:
return ir_analyze_instruction_set_eval_branch_quota(ira, (IrInstructionSetEvalBranchQuota *)instruction);
- case IrInstructionIdPtrTypeOf:
- return ir_analyze_instruction_ptr_type_of(ira, (IrInstructionPtrTypeOf *)instruction);
+ case IrInstructionIdPtrType:
+ return ir_analyze_instruction_ptr_type(ira, (IrInstructionPtrType *)instruction);
case IrInstructionIdAlignCast:
return ir_analyze_instruction_align_cast(ira, (IrInstructionAlignCast *)instruction);
case IrInstructionIdOpaqueType:
@@ -19783,7 +19786,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdCheckStatementIsVoid:
case IrInstructionIdPanic:
case IrInstructionIdSetEvalBranchQuota:
- case IrInstructionIdPtrTypeOf:
+ case IrInstructionIdPtrType:
case IrInstructionIdSetAlignStack:
case IrInstructionIdExport:
case IrInstructionIdCancel:
diff --git a/src/ir_print.cpp b/src/ir_print.cpp
index 9678120f1d..3c177a8bbf 100644
--- a/src/ir_print.cpp
+++ b/src/ir_print.cpp
@@ -921,7 +921,7 @@ static void ir_print_can_implicit_cast(IrPrint *irp, IrInstructionCanImplicitCas
fprintf(irp->f, ")");
}
-static void ir_print_ptr_type_of(IrPrint *irp, IrInstructionPtrTypeOf *instruction) {
+static void ir_print_ptr_type(IrPrint *irp, IrInstructionPtrType *instruction) {
fprintf(irp->f, "&");
if (instruction->align_value != nullptr) {
fprintf(irp->f, "align(");
@@ -1527,8 +1527,8 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdCanImplicitCast:
ir_print_can_implicit_cast(irp, (IrInstructionCanImplicitCast *)instruction);
break;
- case IrInstructionIdPtrTypeOf:
- ir_print_ptr_type_of(irp, (IrInstructionPtrTypeOf *)instruction);
+ case IrInstructionIdPtrType:
+ ir_print_ptr_type(irp, (IrInstructionPtrType *)instruction);
break;
case IrInstructionIdDeclRef:
ir_print_decl_ref(irp, (IrInstructionDeclRef *)instruction);
diff --git a/src/parser.cpp b/src/parser.cpp
index 4763d3b987..ef390a3a2e 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -1167,20 +1167,19 @@ static PrefixOp tok_to_prefix_op(Token *token) {
case TokenIdTilde: return PrefixOpBinNot;
case TokenIdMaybe: return PrefixOpMaybe;
case TokenIdDoubleQuestion: return PrefixOpUnwrapMaybe;
+ case TokenIdAmpersand: return PrefixOpAddrOf;
default: return PrefixOpInvalid;
}
}
-static AstNode *ast_parse_addr_of(ParseContext *pc, size_t *token_index) {
- Token *ampersand_tok = ast_eat_token(pc, token_index, TokenIdAmpersand);
-
- AstNode *node = ast_create_node(pc, NodeTypeAddrOfExpr, ampersand_tok);
+static AstNode *ast_parse_pointer_type(ParseContext *pc, size_t *token_index, Token *star_tok) {
+ AstNode *node = ast_create_node(pc, NodeTypePointerType, star_tok);
Token *token = &pc->tokens->at(*token_index);
if (token->id == TokenIdKeywordAlign) {
*token_index += 1;
ast_eat_token(pc, token_index, TokenIdLParen);
- node->data.addr_of_expr.align_expr = ast_parse_expression(pc, token_index, true);
+ node->data.pointer_type.align_expr = ast_parse_expression(pc, token_index, true);
token = &pc->tokens->at(*token_index);
if (token->id == TokenIdColon) {
@@ -1189,24 +1188,24 @@ static AstNode *ast_parse_addr_of(ParseContext *pc, size_t *token_index) {
ast_eat_token(pc, token_index, TokenIdColon);
Token *bit_offset_end_tok = ast_eat_token(pc, token_index, TokenIdIntLiteral);
- node->data.addr_of_expr.bit_offset_start = token_bigint(bit_offset_start_tok);
- node->data.addr_of_expr.bit_offset_end = token_bigint(bit_offset_end_tok);
+ node->data.pointer_type.bit_offset_start = token_bigint(bit_offset_start_tok);
+ node->data.pointer_type.bit_offset_end = token_bigint(bit_offset_end_tok);
}
ast_eat_token(pc, token_index, TokenIdRParen);
token = &pc->tokens->at(*token_index);
}
if (token->id == TokenIdKeywordConst) {
*token_index += 1;
- node->data.addr_of_expr.is_const = true;
+ node->data.pointer_type.is_const = true;
token = &pc->tokens->at(*token_index);
}
if (token->id == TokenIdKeywordVolatile) {
*token_index += 1;
- node->data.addr_of_expr.is_volatile = true;
+ node->data.pointer_type.is_volatile = true;
}
- node->data.addr_of_expr.op_expr = ast_parse_prefix_op_expr(pc, token_index, true);
+ node->data.pointer_type.op_expr = ast_parse_prefix_op_expr(pc, token_index, true);
return node;
}
@@ -1216,8 +1215,17 @@ PrefixOp = "!" | "-" | "~" | ("*" option("align" "(" Expression option(":" Integ
*/
static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *token = &pc->tokens->at(*token_index);
- if (token->id == TokenIdAmpersand) {
- return ast_parse_addr_of(pc, token_index);
+ if (token->id == TokenIdStar) {
+ *token_index += 1;
+ return ast_parse_pointer_type(pc, token_index, token);
+ }
+ if (token->id == TokenIdStarStar) {
+ *token_index += 1;
+ AstNode *child_node = ast_parse_pointer_type(pc, token_index, token);
+ child_node->column += 1;
+ AstNode *parent_node = ast_create_node(pc, NodeTypePointerType, token);
+ parent_node->data.pointer_type.op_expr = child_node;
+ return parent_node;
}
if (token->id == TokenIdKeywordTry) {
return ast_parse_try_expr(pc, token_index);
@@ -1234,13 +1242,12 @@ static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index,
AstNode *node = ast_create_node(pc, NodeTypePrefixOpExpr, token);
- AstNode *parent_node = node;
AstNode *prefix_op_expr = ast_parse_error_set_expr(pc, token_index, true);
node->data.prefix_op_expr.primary_expr = prefix_op_expr;
node->data.prefix_op_expr.prefix_op = prefix_op;
- return parent_node;
+ return node;
}
@@ -3121,9 +3128,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
case NodeTypeErrorType:
// none
break;
- case NodeTypeAddrOfExpr:
- visit_field(&node->data.addr_of_expr.align_expr, visit, context);
- visit_field(&node->data.addr_of_expr.op_expr, visit, context);
+ case NodeTypePointerType:
+ visit_field(&node->data.pointer_type.align_expr, visit, context);
+ visit_field(&node->data.pointer_type.op_expr, visit, context);
break;
case NodeTypeErrorSetDecl:
visit_node_list(&node->data.err_set_decl.decls, visit, context);
diff --git a/src/translate_c.cpp b/src/translate_c.cpp
index 50ff073008..db541d34f3 100644
--- a/src/translate_c.cpp
+++ b/src/translate_c.cpp
@@ -276,11 +276,18 @@ static AstNode *maybe_suppress_result(Context *c, ResultUsed result_used, AstNod
node);
}
-static AstNode *trans_create_node_addr_of(Context *c, bool is_const, bool is_volatile, AstNode *child_node) {
- AstNode *node = trans_create_node(c, NodeTypeAddrOfExpr);
- node->data.addr_of_expr.is_const = is_const;
- node->data.addr_of_expr.is_volatile = is_volatile;
- node->data.addr_of_expr.op_expr = child_node;
+static AstNode *trans_create_node_ptr_type(Context *c, bool is_const, bool is_volatile, AstNode *child_node) {
+ AstNode *node = trans_create_node(c, NodeTypePointerType);
+ node->data.pointer_type.is_const = is_const;
+ node->data.pointer_type.is_volatile = is_volatile;
+ node->data.pointer_type.op_expr = child_node;
+ return node;
+}
+
+static AstNode *trans_create_node_addr_of(Context *c, AstNode *child_node) {
+ AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
+ node->data.prefix_op_expr.prefix_op = PrefixOpAddrOf;
+ node->data.prefix_op_expr.primary_expr = child_node;
return node;
}
@@ -848,7 +855,7 @@ static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &sou
return trans_create_node_prefix_op(c, PrefixOpMaybe, child_node);
}
- AstNode *pointer_node = trans_create_node_addr_of(c, child_qt.isConstQualified(),
+ AstNode *pointer_node = trans_create_node_ptr_type(c, child_qt.isConstQualified(),
child_qt.isVolatileQualified(), child_node);
return trans_create_node_prefix_op(c, PrefixOpMaybe, pointer_node);
}
@@ -1033,7 +1040,7 @@ static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &sou
emit_warning(c, source_loc, "unresolved array element type");
return nullptr;
}
- AstNode *pointer_node = trans_create_node_addr_of(c, child_qt.isConstQualified(),
+ AstNode *pointer_node = trans_create_node_ptr_type(c, child_qt.isConstQualified(),
child_qt.isVolatileQualified(), child_type_node);
return pointer_node;
}
@@ -1402,7 +1409,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
// const _ref = &lhs;
AstNode *lhs = trans_expr(c, ResultUsedYes, &child_scope->base, stmt->getLHS(), TransLValue);
if (lhs == nullptr) return nullptr;
- AstNode *addr_of_lhs = trans_create_node_addr_of(c, false, false, lhs);
+ AstNode *addr_of_lhs = trans_create_node_addr_of(c, lhs);
// TODO: avoid name collisions with generated variable names
Buf* tmp_var_name = buf_create_from_str("_ref");
AstNode *tmp_var_decl = trans_create_node_var_decl_local(c, true, tmp_var_name, nullptr, addr_of_lhs);
@@ -1476,7 +1483,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
// const _ref = &lhs;
AstNode *lhs = trans_expr(c, ResultUsedYes, &child_scope->base, stmt->getLHS(), TransLValue);
if (lhs == nullptr) return nullptr;
- AstNode *addr_of_lhs = trans_create_node_addr_of(c, false, false, lhs);
+ AstNode *addr_of_lhs = trans_create_node_addr_of(c, lhs);
// TODO: avoid name collisions with generated variable names
Buf* tmp_var_name = buf_create_from_str("_ref");
AstNode *tmp_var_decl = trans_create_node_var_decl_local(c, true, tmp_var_name, nullptr, addr_of_lhs);
@@ -1813,7 +1820,7 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
// const _ref = &expr;
AstNode *expr = trans_expr(c, ResultUsedYes, &child_scope->base, op_expr, TransLValue);
if (expr == nullptr) return nullptr;
- AstNode *addr_of_expr = trans_create_node_addr_of(c, false, false, expr);
+ AstNode *addr_of_expr = trans_create_node_addr_of(c, expr);
// TODO: avoid name collisions with generated variable names
Buf* ref_var_name = buf_create_from_str("_ref");
AstNode *ref_var_decl = trans_create_node_var_decl_local(c, true, ref_var_name, nullptr, addr_of_expr);
@@ -1868,7 +1875,7 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra
// const _ref = &expr;
AstNode *expr = trans_expr(c, ResultUsedYes, &child_scope->base, op_expr, TransLValue);
if (expr == nullptr) return nullptr;
- AstNode *addr_of_expr = trans_create_node_addr_of(c, false, false, expr);
+ AstNode *addr_of_expr = trans_create_node_addr_of(c, expr);
// TODO: avoid name collisions with generated variable names
Buf* ref_var_name = buf_create_from_str("_ref");
AstNode *ref_var_decl = trans_create_node_var_decl_local(c, true, ref_var_name, nullptr, addr_of_expr);
@@ -1917,7 +1924,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
AstNode *value_node = trans_expr(c, result_used, scope, stmt->getSubExpr(), TransLValue);
if (value_node == nullptr)
return value_node;
- return trans_create_node_addr_of(c, false, false, value_node);
+ return trans_create_node_addr_of(c, value_node);
}
case UO_Deref:
{
@@ -4441,7 +4448,7 @@ static AstNode *parse_ctok_suffix_op_expr(Context *c, CTokenize *ctok, size_t *t
} else if (first_tok->id == CTokIdAsterisk) {
*tok_i += 1;
- node = trans_create_node_addr_of(c, false, false, node);
+ node = trans_create_node_ptr_type(c, false, false, node);
} else {
return node;
}
diff --git a/std/array_list.zig b/std/array_list.zig
index b315194c33..07a1db6451 100644
--- a/std/array_list.zig
+++ b/std/array_list.zig
@@ -17,10 +17,10 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
/// you uninitialized memory.
items: []align(A) T,
len: usize,
- allocator: &Allocator,
+ allocator: *Allocator,
/// Deinitialize with `deinit` or use `toOwnedSlice`.
- pub fn init(allocator: &Allocator) Self {
+ pub fn init(allocator: *Allocator) Self {
return Self{
.items = []align(A) T{},
.len = 0,
@@ -28,30 +28,30 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
};
}
- pub fn deinit(l: &const Self) void {
+ pub fn deinit(l: *const Self) void {
l.allocator.free(l.items);
}
- pub fn toSlice(l: &const Self) []align(A) T {
+ pub fn toSlice(l: *const Self) []align(A) T {
return l.items[0..l.len];
}
- pub fn toSliceConst(l: &const Self) []align(A) const T {
+ pub fn toSliceConst(l: *const Self) []align(A) const T {
return l.items[0..l.len];
}
- pub fn at(l: &const Self, n: usize) T {
+ pub fn at(l: *const Self, n: usize) T {
return l.toSliceConst()[n];
}
- pub fn count(self: &const Self) usize {
+ pub fn count(self: *const Self) usize {
return self.len;
}
/// ArrayList takes ownership of the passed in slice. The slice must have been
/// allocated with `allocator`.
/// Deinitialize with `deinit` or use `toOwnedSlice`.
- pub fn fromOwnedSlice(allocator: &Allocator, slice: []align(A) T) Self {
+ pub fn fromOwnedSlice(allocator: *Allocator, slice: []align(A) T) Self {
return Self{
.items = slice,
.len = slice.len,
@@ -60,14 +60,14 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
}
/// The caller owns the returned memory. ArrayList becomes empty.
- pub fn toOwnedSlice(self: &Self) []align(A) T {
+ pub fn toOwnedSlice(self: *Self) []align(A) T {
const allocator = self.allocator;
const result = allocator.alignedShrink(T, A, self.items, self.len);
self.* = init(allocator);
return result;
}
- pub fn insert(l: &Self, n: usize, item: &const T) !void {
+ pub fn insert(l: *Self, n: usize, item: *const T) !void {
try l.ensureCapacity(l.len + 1);
l.len += 1;
@@ -75,7 +75,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
l.items[n] = item.*;
}
- pub fn insertSlice(l: &Self, n: usize, items: []align(A) const T) !void {
+ pub fn insertSlice(l: *Self, n: usize, items: []align(A) const T) !void {
try l.ensureCapacity(l.len + items.len);
l.len += items.len;
@@ -83,28 +83,28 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
mem.copy(T, l.items[n .. n + items.len], items);
}
- pub fn append(l: &Self, item: &const T) !void {
+ pub fn append(l: *Self, item: *const T) !void {
const new_item_ptr = try l.addOne();
new_item_ptr.* = item.*;
}
- pub fn appendSlice(l: &Self, items: []align(A) const T) !void {
+ pub fn appendSlice(l: *Self, items: []align(A) const T) !void {
try l.ensureCapacity(l.len + items.len);
mem.copy(T, l.items[l.len..], items);
l.len += items.len;
}
- pub fn resize(l: &Self, new_len: usize) !void {
+ pub fn resize(l: *Self, new_len: usize) !void {
try l.ensureCapacity(new_len);
l.len = new_len;
}
- pub fn shrink(l: &Self, new_len: usize) void {
+ pub fn shrink(l: *Self, new_len: usize) void {
assert(new_len <= l.len);
l.len = new_len;
}
- pub fn ensureCapacity(l: &Self, new_capacity: usize) !void {
+ pub fn ensureCapacity(l: *Self, new_capacity: usize) !void {
var better_capacity = l.items.len;
if (better_capacity >= new_capacity) return;
while (true) {
@@ -114,7 +114,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
l.items = try l.allocator.alignedRealloc(T, A, l.items, better_capacity);
}
- pub fn addOne(l: &Self) !&T {
+ pub fn addOne(l: *Self) !*T {
const new_length = l.len + 1;
try l.ensureCapacity(new_length);
const result = &l.items[l.len];
@@ -122,34 +122,34 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
return result;
}
- pub fn pop(self: &Self) T {
+ pub fn pop(self: *Self) T {
self.len -= 1;
return self.items[self.len];
}
- pub fn popOrNull(self: &Self) ?T {
+ pub fn popOrNull(self: *Self) ?T {
if (self.len == 0) return null;
return self.pop();
}
pub const Iterator = struct {
- list: &const Self,
+ list: *const Self,
// how many items have we returned
count: usize,
- pub fn next(it: &Iterator) ?T {
+ pub fn next(it: *Iterator) ?T {
if (it.count >= it.list.len) return null;
const val = it.list.at(it.count);
it.count += 1;
return val;
}
- pub fn reset(it: &Iterator) void {
+ pub fn reset(it: *Iterator) void {
it.count = 0;
}
};
- pub fn iterator(self: &const Self) Iterator {
+ pub fn iterator(self: *const Self) Iterator {
return Iterator{
.list = self,
.count = 0,
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 35180da8d1..142c958173 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -5,36 +5,36 @@ const AtomicRmwOp = builtin.AtomicRmwOp;
/// Many reader, many writer, non-allocating, thread-safe, lock-free
pub fn Queue(comptime T: type) type {
return struct {
- head: &Node,
- tail: &Node,
+ head: *Node,
+ tail: *Node,
root: Node,
pub const Self = this;
pub const Node = struct {
- next: ?&Node,
+ next: ?*Node,
data: T,
};
// TODO: well defined copy elision: https://github.com/ziglang/zig/issues/287
- pub fn init(self: &Self) void {
+ pub fn init(self: *Self) void {
self.root.next = null;
self.head = &self.root;
self.tail = &self.root;
}
- pub fn put(self: &Self, node: &Node) void {
+ pub fn put(self: *Self, node: *Node) void {
node.next = null;
- const tail = @atomicRmw(&Node, &self.tail, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
- _ = @atomicRmw(?&Node, &tail.next, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
+ const tail = @atomicRmw(*Node, &self.tail, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
+ _ = @atomicRmw(?*Node, &tail.next, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
}
- pub fn get(self: &Self) ?&Node {
- var head = @atomicLoad(&Node, &self.head, AtomicOrder.SeqCst);
+ pub fn get(self: *Self) ?*Node {
+ var head = @atomicLoad(*Node, &self.head, AtomicOrder.SeqCst);
while (true) {
const node = head.next ?? return null;
- head = @cmpxchgWeak(&Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return node;
+ head = @cmpxchgWeak(*Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return node;
}
}
};
@@ -42,8 +42,8 @@ pub fn Queue(comptime T: type) type {
const std = @import("std");
const Context = struct {
- allocator: &std.mem.Allocator,
- queue: &Queue(i32),
+ allocator: *std.mem.Allocator,
+ queue: *Queue(i32),
put_sum: isize,
get_sum: isize,
get_count: usize,
@@ -79,11 +79,11 @@ test "std.atomic.queue" {
.get_count = 0,
};
- var putters: [put_thread_count]&std.os.Thread = undefined;
+ var putters: [put_thread_count]*std.os.Thread = undefined;
for (putters) |*t| {
t.* = try std.os.spawnThread(&context, startPuts);
}
- var getters: [put_thread_count]&std.os.Thread = undefined;
+ var getters: [put_thread_count]*std.os.Thread = undefined;
for (getters) |*t| {
t.* = try std.os.spawnThread(&context, startGets);
}
@@ -98,7 +98,7 @@ test "std.atomic.queue" {
std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
}
-fn startPuts(ctx: &Context) u8 {
+fn startPuts(ctx: *Context) u8 {
var put_count: usize = puts_per_thread;
var r = std.rand.DefaultPrng.init(0xdeadbeef);
while (put_count != 0) : (put_count -= 1) {
@@ -112,7 +112,7 @@ fn startPuts(ctx: &Context) u8 {
return 0;
}
-fn startGets(ctx: &Context) u8 {
+fn startGets(ctx: *Context) u8 {
while (true) {
while (ctx.queue.get()) |node| {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 400a1a3c4f..15611188d2 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -4,12 +4,12 @@ const AtomicOrder = builtin.AtomicOrder;
/// Many reader, many writer, non-allocating, thread-safe, lock-free
pub fn Stack(comptime T: type) type {
return struct {
- root: ?&Node,
+ root: ?*Node,
pub const Self = this;
pub const Node = struct {
- next: ?&Node,
+ next: ?*Node,
data: T,
};
@@ -19,36 +19,36 @@ pub fn Stack(comptime T: type) type {
/// push operation, but only if you are the first item in the stack. if you did not succeed in
/// being the first item in the stack, returns the other item that was there.
- pub fn pushFirst(self: &Self, node: &Node) ?&Node {
+ pub fn pushFirst(self: *Self, node: *Node) ?*Node {
node.next = null;
- return @cmpxchgStrong(?&Node, &self.root, null, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst);
+ return @cmpxchgStrong(?*Node, &self.root, null, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst);
}
- pub fn push(self: &Self, node: &Node) void {
- var root = @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst);
+ pub fn push(self: *Self, node: *Node) void {
+ var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
while (true) {
node.next = root;
- root = @cmpxchgWeak(?&Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? break;
+ root = @cmpxchgWeak(?*Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? break;
}
}
- pub fn pop(self: &Self) ?&Node {
- var root = @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst);
+ pub fn pop(self: *Self) ?*Node {
+ var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
while (true) {
- root = @cmpxchgWeak(?&Node, &self.root, root, (root ?? return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return root;
+ root = @cmpxchgWeak(?*Node, &self.root, root, (root ?? return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return root;
}
}
- pub fn isEmpty(self: &Self) bool {
- return @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst) == null;
+ pub fn isEmpty(self: *Self) bool {
+ return @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst) == null;
}
};
}
const std = @import("std");
const Context = struct {
- allocator: &std.mem.Allocator,
- stack: &Stack(i32),
+ allocator: *std.mem.Allocator,
+ stack: *Stack(i32),
put_sum: isize,
get_sum: isize,
get_count: usize,
@@ -82,11 +82,11 @@ test "std.atomic.stack" {
.get_count = 0,
};
- var putters: [put_thread_count]&std.os.Thread = undefined;
+ var putters: [put_thread_count]*std.os.Thread = undefined;
for (putters) |*t| {
t.* = try std.os.spawnThread(&context, startPuts);
}
- var getters: [put_thread_count]&std.os.Thread = undefined;
+ var getters: [put_thread_count]*std.os.Thread = undefined;
for (getters) |*t| {
t.* = try std.os.spawnThread(&context, startGets);
}
@@ -101,7 +101,7 @@ test "std.atomic.stack" {
std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
}
-fn startPuts(ctx: &Context) u8 {
+fn startPuts(ctx: *Context) u8 {
var put_count: usize = puts_per_thread;
var r = std.rand.DefaultPrng.init(0xdeadbeef);
while (put_count != 0) : (put_count -= 1) {
@@ -115,7 +115,7 @@ fn startPuts(ctx: &Context) u8 {
return 0;
}
-fn startGets(ctx: &Context) u8 {
+fn startGets(ctx: *Context) u8 {
while (true) {
while (ctx.stack.pop()) |node| {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
diff --git a/std/base64.zig b/std/base64.zig
index 204628a405..d27bcbd201 100644
--- a/std/base64.zig
+++ b/std/base64.zig
@@ -32,7 +32,7 @@ pub const Base64Encoder = struct {
}
/// dest.len must be what you get from ::calcSize.
- pub fn encode(encoder: &const Base64Encoder, dest: []u8, source: []const u8) void {
+ pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) void {
assert(dest.len == Base64Encoder.calcSize(source.len));
var i: usize = 0;
@@ -107,7 +107,7 @@ pub const Base64Decoder = struct {
}
/// If the encoded buffer is detected to be invalid, returns error.InvalidPadding.
- pub fn calcSize(decoder: &const Base64Decoder, source: []const u8) !usize {
+ pub fn calcSize(decoder: *const Base64Decoder, source: []const u8) !usize {
if (source.len % 4 != 0) return error.InvalidPadding;
return calcDecodedSizeExactUnsafe(source, decoder.pad_char);
}
@@ -115,7 +115,7 @@ pub const Base64Decoder = struct {
/// dest.len must be what you get from ::calcSize.
/// invalid characters result in error.InvalidCharacter.
/// invalid padding results in error.InvalidPadding.
- pub fn decode(decoder: &const Base64Decoder, dest: []u8, source: []const u8) !void {
+ pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) !void {
assert(dest.len == (decoder.calcSize(source) catch unreachable));
assert(source.len % 4 == 0);
@@ -181,7 +181,7 @@ pub const Base64DecoderWithIgnore = struct {
/// Invalid padding results in error.InvalidPadding.
/// Decoding more data than can fit in dest results in error.OutputTooSmall. See also ::calcSizeUpperBound.
/// Returns the number of bytes writen to dest.
- pub fn decode(decoder_with_ignore: &const Base64DecoderWithIgnore, dest: []u8, source: []const u8) !usize {
+ pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8) !usize {
const decoder = &decoder_with_ignore.decoder;
var src_cursor: usize = 0;
@@ -290,13 +290,13 @@ pub const Base64DecoderUnsafe = struct {
}
/// The source buffer must be valid.
- pub fn calcSize(decoder: &const Base64DecoderUnsafe, source: []const u8) usize {
+ pub fn calcSize(decoder: *const Base64DecoderUnsafe, source: []const u8) usize {
return calcDecodedSizeExactUnsafe(source, decoder.pad_char);
}
/// dest.len must be what you get from ::calcDecodedSizeExactUnsafe.
/// invalid characters or padding will result in undefined values.
- pub fn decode(decoder: &const Base64DecoderUnsafe, dest: []u8, source: []const u8) void {
+ pub fn decode(decoder: *const Base64DecoderUnsafe, dest: []u8, source: []const u8) void {
assert(dest.len == decoder.calcSize(source));
var src_index: usize = 0;
diff --git a/std/buf_map.zig b/std/buf_map.zig
index 930fc36a78..22d821ae7b 100644
--- a/std/buf_map.zig
+++ b/std/buf_map.zig
@@ -11,12 +11,12 @@ pub const BufMap = struct {
const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);
- pub fn init(allocator: &Allocator) BufMap {
+ pub fn init(allocator: *Allocator) BufMap {
var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) };
return self;
}
- pub fn deinit(self: &const BufMap) void {
+ pub fn deinit(self: *const BufMap) void {
var it = self.hash_map.iterator();
while (true) {
const entry = it.next() ?? break;
@@ -27,7 +27,7 @@ pub const BufMap = struct {
self.hash_map.deinit();
}
- pub fn set(self: &BufMap, key: []const u8, value: []const u8) !void {
+ pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {
self.delete(key);
const key_copy = try self.copy(key);
errdefer self.free(key_copy);
@@ -36,30 +36,30 @@ pub const BufMap = struct {
_ = try self.hash_map.put(key_copy, value_copy);
}
- pub fn get(self: &const BufMap, key: []const u8) ?[]const u8 {
+ pub fn get(self: *const BufMap, key: []const u8) ?[]const u8 {
const entry = self.hash_map.get(key) ?? return null;
return entry.value;
}
- pub fn delete(self: &BufMap, key: []const u8) void {
+ pub fn delete(self: *BufMap, key: []const u8) void {
const entry = self.hash_map.remove(key) ?? return;
self.free(entry.key);
self.free(entry.value);
}
- pub fn count(self: &const BufMap) usize {
+ pub fn count(self: *const BufMap) usize {
return self.hash_map.count();
}
- pub fn iterator(self: &const BufMap) BufMapHashMap.Iterator {
+ pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator {
return self.hash_map.iterator();
}
- fn free(self: &const BufMap, value: []const u8) void {
+ fn free(self: *const BufMap, value: []const u8) void {
self.hash_map.allocator.free(value);
}
- fn copy(self: &const BufMap, value: []const u8) ![]const u8 {
+ fn copy(self: *const BufMap, value: []const u8) ![]const u8 {
return mem.dupe(self.hash_map.allocator, u8, value);
}
};
diff --git a/std/buf_set.zig b/std/buf_set.zig
index c5a80e16fb..03a050ed8b 100644
--- a/std/buf_set.zig
+++ b/std/buf_set.zig
@@ -9,12 +9,12 @@ pub const BufSet = struct {
const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);
- pub fn init(a: &Allocator) BufSet {
+ pub fn init(a: *Allocator) BufSet {
var self = BufSet{ .hash_map = BufSetHashMap.init(a) };
return self;
}
- pub fn deinit(self: &const BufSet) void {
+ pub fn deinit(self: *const BufSet) void {
var it = self.hash_map.iterator();
while (true) {
const entry = it.next() ?? break;
@@ -24,7 +24,7 @@ pub const BufSet = struct {
self.hash_map.deinit();
}
- pub fn put(self: &BufSet, key: []const u8) !void {
+ pub fn put(self: *BufSet, key: []const u8) !void {
if (self.hash_map.get(key) == null) {
const key_copy = try self.copy(key);
errdefer self.free(key_copy);
@@ -32,28 +32,28 @@ pub const BufSet = struct {
}
}
- pub fn delete(self: &BufSet, key: []const u8) void {
+ pub fn delete(self: *BufSet, key: []const u8) void {
const entry = self.hash_map.remove(key) ?? return;
self.free(entry.key);
}
- pub fn count(self: &const BufSet) usize {
+ pub fn count(self: *const BufSet) usize {
return self.hash_map.count();
}
- pub fn iterator(self: &const BufSet) BufSetHashMap.Iterator {
+ pub fn iterator(self: *const BufSet) BufSetHashMap.Iterator {
return self.hash_map.iterator();
}
- pub fn allocator(self: &const BufSet) &Allocator {
+ pub fn allocator(self: *const BufSet) *Allocator {
return self.hash_map.allocator;
}
- fn free(self: &const BufSet, value: []const u8) void {
+ fn free(self: *const BufSet, value: []const u8) void {
self.hash_map.allocator.free(value);
}
- fn copy(self: &const BufSet, value: []const u8) ![]const u8 {
+ fn copy(self: *const BufSet, value: []const u8) ![]const u8 {
const result = try self.hash_map.allocator.alloc(u8, value.len);
mem.copy(u8, result, value);
return result;
diff --git a/std/buffer.zig b/std/buffer.zig
index 90d63719e3..305746e183 100644
--- a/std/buffer.zig
+++ b/std/buffer.zig
@@ -12,14 +12,14 @@ pub const Buffer = struct {
list: ArrayList(u8),
/// Must deinitialize with deinit.
- pub fn init(allocator: &Allocator, m: []const u8) !Buffer {
+ pub fn init(allocator: *Allocator, m: []const u8) !Buffer {
var self = try initSize(allocator, m.len);
mem.copy(u8, self.list.items, m);
return self;
}
/// Must deinitialize with deinit.
- pub fn initSize(allocator: &Allocator, size: usize) !Buffer {
+ pub fn initSize(allocator: *Allocator, size: usize) !Buffer {
var self = initNull(allocator);
try self.resize(size);
return self;
@@ -30,19 +30,19 @@ pub const Buffer = struct {
/// * ::replaceContents
/// * ::replaceContentsBuffer
/// * ::resize
- pub fn initNull(allocator: &Allocator) Buffer {
+ pub fn initNull(allocator: *Allocator) Buffer {
return Buffer{ .list = ArrayList(u8).init(allocator) };
}
/// Must deinitialize with deinit.
- pub fn initFromBuffer(buffer: &const Buffer) !Buffer {
+ pub fn initFromBuffer(buffer: *const Buffer) !Buffer {
return Buffer.init(buffer.list.allocator, buffer.toSliceConst());
}
/// Buffer takes ownership of the passed in slice. The slice must have been
/// allocated with `allocator`.
/// Must deinitialize with deinit.
- pub fn fromOwnedSlice(allocator: &Allocator, slice: []u8) Buffer {
+ pub fn fromOwnedSlice(allocator: *Allocator, slice: []u8) Buffer {
var self = Buffer{ .list = ArrayList(u8).fromOwnedSlice(allocator, slice) };
self.list.append(0);
return self;
@@ -50,79 +50,79 @@ pub const Buffer = struct {
/// The caller owns the returned memory. The Buffer becomes null and
/// is safe to `deinit`.
- pub fn toOwnedSlice(self: &Buffer) []u8 {
+ pub fn toOwnedSlice(self: *Buffer) []u8 {
const allocator = self.list.allocator;
const result = allocator.shrink(u8, self.list.items, self.len());
self.* = initNull(allocator);
return result;
}
- pub fn deinit(self: &Buffer) void {
+ pub fn deinit(self: *Buffer) void {
self.list.deinit();
}
- pub fn toSlice(self: &const Buffer) []u8 {
+ pub fn toSlice(self: *const Buffer) []u8 {
return self.list.toSlice()[0..self.len()];
}
- pub fn toSliceConst(self: &const Buffer) []const u8 {
+ pub fn toSliceConst(self: *const Buffer) []const u8 {
return self.list.toSliceConst()[0..self.len()];
}
- pub fn shrink(self: &Buffer, new_len: usize) void {
+ pub fn shrink(self: *Buffer, new_len: usize) void {
assert(new_len <= self.len());
self.list.shrink(new_len + 1);
self.list.items[self.len()] = 0;
}
- pub fn resize(self: &Buffer, new_len: usize) !void {
+ pub fn resize(self: *Buffer, new_len: usize) !void {
try self.list.resize(new_len + 1);
self.list.items[self.len()] = 0;
}
- pub fn isNull(self: &const Buffer) bool {
+ pub fn isNull(self: *const Buffer) bool {
return self.list.len == 0;
}
- pub fn len(self: &const Buffer) usize {
+ pub fn len(self: *const Buffer) usize {
return self.list.len - 1;
}
- pub fn append(self: &Buffer, m: []const u8) !void {
+ pub fn append(self: *Buffer, m: []const u8) !void {
const old_len = self.len();
try self.resize(old_len + m.len);
mem.copy(u8, self.list.toSlice()[old_len..], m);
}
- pub fn appendByte(self: &Buffer, byte: u8) !void {
+ pub fn appendByte(self: *Buffer, byte: u8) !void {
const old_len = self.len();
try self.resize(old_len + 1);
self.list.toSlice()[old_len] = byte;
}
- pub fn eql(self: &const Buffer, m: []const u8) bool {
+ pub fn eql(self: *const Buffer, m: []const u8) bool {
return mem.eql(u8, self.toSliceConst(), m);
}
- pub fn startsWith(self: &const Buffer, m: []const u8) bool {
+ pub fn startsWith(self: *const Buffer, m: []const u8) bool {
if (self.len() < m.len) return false;
return mem.eql(u8, self.list.items[0..m.len], m);
}
- pub fn endsWith(self: &const Buffer, m: []const u8) bool {
+ pub fn endsWith(self: *const Buffer, m: []const u8) bool {
const l = self.len();
if (l < m.len) return false;
const start = l - m.len;
return mem.eql(u8, self.list.items[start..l], m);
}
- pub fn replaceContents(self: &const Buffer, m: []const u8) !void {
+ pub fn replaceContents(self: *const Buffer, m: []const u8) !void {
try self.resize(m.len);
mem.copy(u8, self.list.toSlice(), m);
}
/// For passing to C functions.
- pub fn ptr(self: &const Buffer) &u8 {
+ pub fn ptr(self: *const Buffer) *u8 {
return self.list.items.ptr;
}
};
diff --git a/std/build.zig b/std/build.zig
index 9a6e17f728..fed02e0815 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -20,7 +20,7 @@ pub const Builder = struct {
install_tls: TopLevelStep,
have_uninstall_step: bool,
have_install_step: bool,
- allocator: &Allocator,
+ allocator: *Allocator,
lib_paths: ArrayList([]const u8),
include_paths: ArrayList([]const u8),
rpaths: ArrayList([]const u8),
@@ -36,9 +36,9 @@ pub const Builder = struct {
verbose_cimport: bool,
invalid_user_input: bool,
zig_exe: []const u8,
- default_step: &Step,
+ default_step: *Step,
env_map: BufMap,
- top_level_steps: ArrayList(&TopLevelStep),
+ top_level_steps: ArrayList(*TopLevelStep),
prefix: []const u8,
search_prefixes: ArrayList([]const u8),
lib_dir: []const u8,
@@ -82,7 +82,7 @@ pub const Builder = struct {
description: []const u8,
};
- pub fn init(allocator: &Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
+ pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
var self = Builder{
.zig_exe = zig_exe,
.build_root = build_root,
@@ -102,7 +102,7 @@ pub const Builder = struct {
.user_input_options = UserInputOptionsMap.init(allocator),
.available_options_map = AvailableOptionsMap.init(allocator),
.available_options_list = ArrayList(AvailableOption).init(allocator),
- .top_level_steps = ArrayList(&TopLevelStep).init(allocator),
+ .top_level_steps = ArrayList(*TopLevelStep).init(allocator),
.default_step = undefined,
.env_map = os.getEnvMap(allocator) catch unreachable,
.prefix = undefined,
@@ -127,7 +127,7 @@ pub const Builder = struct {
return self;
}
- pub fn deinit(self: &Builder) void {
+ pub fn deinit(self: *Builder) void {
self.lib_paths.deinit();
self.include_paths.deinit();
self.rpaths.deinit();
@@ -135,81 +135,81 @@ pub const Builder = struct {
self.top_level_steps.deinit();
}
- pub fn setInstallPrefix(self: &Builder, maybe_prefix: ?[]const u8) void {
+ pub fn setInstallPrefix(self: *Builder, maybe_prefix: ?[]const u8) void {
self.prefix = maybe_prefix ?? "/usr/local"; // TODO better default
self.lib_dir = os.path.join(self.allocator, self.prefix, "lib") catch unreachable;
self.exe_dir = os.path.join(self.allocator, self.prefix, "bin") catch unreachable;
}
- pub fn addExecutable(self: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
+ pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
return LibExeObjStep.createExecutable(self, name, root_src);
}
- pub fn addObject(self: &Builder, name: []const u8, root_src: []const u8) &LibExeObjStep {
+ pub fn addObject(self: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {
return LibExeObjStep.createObject(self, name, root_src);
}
- pub fn addSharedLibrary(self: &Builder, name: []const u8, root_src: ?[]const u8, ver: &const Version) &LibExeObjStep {
+ pub fn addSharedLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8, ver: *const Version) *LibExeObjStep {
return LibExeObjStep.createSharedLibrary(self, name, root_src, ver);
}
- pub fn addStaticLibrary(self: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
+ pub fn addStaticLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
return LibExeObjStep.createStaticLibrary(self, name, root_src);
}
- pub fn addTest(self: &Builder, root_src: []const u8) &TestStep {
+ pub fn addTest(self: *Builder, root_src: []const u8) *TestStep {
const test_step = self.allocator.create(TestStep) catch unreachable;
test_step.* = TestStep.init(self, root_src);
return test_step;
}
- pub fn addAssemble(self: &Builder, name: []const u8, src: []const u8) &LibExeObjStep {
+ pub fn addAssemble(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
const obj_step = LibExeObjStep.createObject(self, name, null);
obj_step.addAssemblyFile(src);
return obj_step;
}
- pub fn addCStaticLibrary(self: &Builder, name: []const u8) &LibExeObjStep {
+ pub fn addCStaticLibrary(self: *Builder, name: []const u8) *LibExeObjStep {
return LibExeObjStep.createCStaticLibrary(self, name);
}
- pub fn addCSharedLibrary(self: &Builder, name: []const u8, ver: &const Version) &LibExeObjStep {
+ pub fn addCSharedLibrary(self: *Builder, name: []const u8, ver: *const Version) *LibExeObjStep {
return LibExeObjStep.createCSharedLibrary(self, name, ver);
}
- pub fn addCExecutable(self: &Builder, name: []const u8) &LibExeObjStep {
+ pub fn addCExecutable(self: *Builder, name: []const u8) *LibExeObjStep {
return LibExeObjStep.createCExecutable(self, name);
}
- pub fn addCObject(self: &Builder, name: []const u8, src: []const u8) &LibExeObjStep {
+ pub fn addCObject(self: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
return LibExeObjStep.createCObject(self, name, src);
}
/// ::argv is copied.
- pub fn addCommand(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) &CommandStep {
+ pub fn addCommand(self: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) *CommandStep {
return CommandStep.create(self, cwd, env_map, argv);
}
- pub fn addWriteFile(self: &Builder, file_path: []const u8, data: []const u8) &WriteFileStep {
+ pub fn addWriteFile(self: *Builder, file_path: []const u8, data: []const u8) *WriteFileStep {
const write_file_step = self.allocator.create(WriteFileStep) catch unreachable;
write_file_step.* = WriteFileStep.init(self, file_path, data);
return write_file_step;
}
- pub fn addLog(self: &Builder, comptime format: []const u8, args: ...) &LogStep {
+ pub fn addLog(self: *Builder, comptime format: []const u8, args: ...) *LogStep {
const data = self.fmt(format, args);
const log_step = self.allocator.create(LogStep) catch unreachable;
log_step.* = LogStep.init(self, data);
return log_step;
}
- pub fn addRemoveDirTree(self: &Builder, dir_path: []const u8) &RemoveDirStep {
+ pub fn addRemoveDirTree(self: *Builder, dir_path: []const u8) *RemoveDirStep {
const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable;
remove_dir_step.* = RemoveDirStep.init(self, dir_path);
return remove_dir_step;
}
- pub fn version(self: &const Builder, major: u32, minor: u32, patch: u32) Version {
+ pub fn version(self: *const Builder, major: u32, minor: u32, patch: u32) Version {
return Version{
.major = major,
.minor = minor,
@@ -217,20 +217,20 @@ pub const Builder = struct {
};
}
- pub fn addCIncludePath(self: &Builder, path: []const u8) void {
+ pub fn addCIncludePath(self: *Builder, path: []const u8) void {
self.include_paths.append(path) catch unreachable;
}
- pub fn addRPath(self: &Builder, path: []const u8) void {
+ pub fn addRPath(self: *Builder, path: []const u8) void {
self.rpaths.append(path) catch unreachable;
}
- pub fn addLibPath(self: &Builder, path: []const u8) void {
+ pub fn addLibPath(self: *Builder, path: []const u8) void {
self.lib_paths.append(path) catch unreachable;
}
- pub fn make(self: &Builder, step_names: []const []const u8) !void {
- var wanted_steps = ArrayList(&Step).init(self.allocator);
+ pub fn make(self: *Builder, step_names: []const []const u8) !void {
+ var wanted_steps = ArrayList(*Step).init(self.allocator);
defer wanted_steps.deinit();
if (step_names.len == 0) {
@@ -247,7 +247,7 @@ pub const Builder = struct {
}
}
- pub fn getInstallStep(self: &Builder) &Step {
+ pub fn getInstallStep(self: *Builder) *Step {
if (self.have_install_step) return &self.install_tls.step;
self.top_level_steps.append(&self.install_tls) catch unreachable;
@@ -255,7 +255,7 @@ pub const Builder = struct {
return &self.install_tls.step;
}
- pub fn getUninstallStep(self: &Builder) &Step {
+ pub fn getUninstallStep(self: *Builder) *Step {
if (self.have_uninstall_step) return &self.uninstall_tls.step;
self.top_level_steps.append(&self.uninstall_tls) catch unreachable;
@@ -263,7 +263,7 @@ pub const Builder = struct {
return &self.uninstall_tls.step;
}
- fn makeUninstall(uninstall_step: &Step) error!void {
+ fn makeUninstall(uninstall_step: *Step) error!void {
const uninstall_tls = @fieldParentPtr(TopLevelStep, "step", uninstall_step);
const self = @fieldParentPtr(Builder, "uninstall_tls", uninstall_tls);
@@ -277,7 +277,7 @@ pub const Builder = struct {
// TODO remove empty directories
}
- fn makeOneStep(self: &Builder, s: &Step) error!void {
+ fn makeOneStep(self: *Builder, s: *Step) error!void {
if (s.loop_flag) {
warn("Dependency loop detected:\n {}\n", s.name);
return error.DependencyLoopDetected;
@@ -298,7 +298,7 @@ pub const Builder = struct {
try s.make();
}
- fn getTopLevelStepByName(self: &Builder, name: []const u8) !&Step {
+ fn getTopLevelStepByName(self: *Builder, name: []const u8) !*Step {
for (self.top_level_steps.toSliceConst()) |top_level_step| {
if (mem.eql(u8, top_level_step.step.name, name)) {
return &top_level_step.step;
@@ -308,7 +308,7 @@ pub const Builder = struct {
return error.InvalidStepName;
}
- fn processNixOSEnvVars(self: &Builder) void {
+ fn processNixOSEnvVars(self: *Builder) void {
if (os.getEnvVarOwned(self.allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
var it = mem.split(nix_cflags_compile, " ");
while (true) {
@@ -350,7 +350,7 @@ pub const Builder = struct {
}
}
- pub fn option(self: &Builder, comptime T: type, name: []const u8, description: []const u8) ?T {
+ pub fn option(self: *Builder, comptime T: type, name: []const u8, description: []const u8) ?T {
const type_id = comptime typeToEnum(T);
const available_option = AvailableOption{
.name = name,
@@ -403,7 +403,7 @@ pub const Builder = struct {
}
}
- pub fn step(self: &Builder, name: []const u8, description: []const u8) &Step {
+ pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {
const step_info = self.allocator.create(TopLevelStep) catch unreachable;
step_info.* = TopLevelStep{
.step = Step.initNoOp(name, self.allocator),
@@ -413,7 +413,7 @@ pub const Builder = struct {
return &step_info.step;
}
- pub fn standardReleaseOptions(self: &Builder) builtin.Mode {
+ pub fn standardReleaseOptions(self: *Builder) builtin.Mode {
if (self.release_mode) |mode| return mode;
const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false;
@@ -429,7 +429,7 @@ pub const Builder = struct {
return mode;
}
- pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) bool {
+ pub fn addUserInputOption(self: *Builder, name: []const u8, value: []const u8) bool {
if (self.user_input_options.put(name, UserInputOption{
.name = name,
.value = UserValue{ .Scalar = value },
@@ -466,7 +466,7 @@ pub const Builder = struct {
return false;
}
- pub fn addUserInputFlag(self: &Builder, name: []const u8) bool {
+ pub fn addUserInputFlag(self: *Builder, name: []const u8) bool {
if (self.user_input_options.put(name, UserInputOption{
.name = name,
.value = UserValue{ .Flag = {} },
@@ -500,7 +500,7 @@ pub const Builder = struct {
};
}
- fn markInvalidUserInput(self: &Builder) void {
+ fn markInvalidUserInput(self: *Builder) void {
self.invalid_user_input = true;
}
@@ -514,7 +514,7 @@ pub const Builder = struct {
};
}
- pub fn validateUserInputDidItFail(self: &Builder) bool {
+ pub fn validateUserInputDidItFail(self: *Builder) bool {
// make sure all args are used
var it = self.user_input_options.iterator();
while (true) {
@@ -528,7 +528,7 @@ pub const Builder = struct {
return self.invalid_user_input;
}
- fn spawnChild(self: &Builder, argv: []const []const u8) !void {
+ fn spawnChild(self: *Builder, argv: []const []const u8) !void {
return self.spawnChildEnvMap(null, &self.env_map, argv);
}
@@ -540,7 +540,7 @@ pub const Builder = struct {
warn("\n");
}
- fn spawnChildEnvMap(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) !void {
+ fn spawnChildEnvMap(self: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) !void {
if (self.verbose) {
printCmd(cwd, argv);
}
@@ -573,28 +573,28 @@ pub const Builder = struct {
}
}
- pub fn makePath(self: &Builder, path: []const u8) !void {
+ pub fn makePath(self: *Builder, path: []const u8) !void {
os.makePath(self.allocator, self.pathFromRoot(path)) catch |err| {
warn("Unable to create path {}: {}\n", path, @errorName(err));
return err;
};
}
- pub fn installArtifact(self: &Builder, artifact: &LibExeObjStep) void {
+ pub fn installArtifact(self: *Builder, artifact: *LibExeObjStep) void {
self.getInstallStep().dependOn(&self.addInstallArtifact(artifact).step);
}
- pub fn addInstallArtifact(self: &Builder, artifact: &LibExeObjStep) &InstallArtifactStep {
+ pub fn addInstallArtifact(self: *Builder, artifact: *LibExeObjStep) *InstallArtifactStep {
return InstallArtifactStep.create(self, artifact);
}
///::dest_rel_path is relative to prefix path or it can be an absolute path
- pub fn installFile(self: &Builder, src_path: []const u8, dest_rel_path: []const u8) void {
+ pub fn installFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
self.getInstallStep().dependOn(&self.addInstallFile(src_path, dest_rel_path).step);
}
///::dest_rel_path is relative to prefix path or it can be an absolute path
- pub fn addInstallFile(self: &Builder, src_path: []const u8, dest_rel_path: []const u8) &InstallFileStep {
+ pub fn addInstallFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
const full_dest_path = os.path.resolve(self.allocator, self.prefix, dest_rel_path) catch unreachable;
self.pushInstalledFile(full_dest_path);
@@ -603,16 +603,16 @@ pub const Builder = struct {
return install_step;
}
- pub fn pushInstalledFile(self: &Builder, full_path: []const u8) void {
+ pub fn pushInstalledFile(self: *Builder, full_path: []const u8) void {
_ = self.getUninstallStep();
self.installed_files.append(full_path) catch unreachable;
}
- fn copyFile(self: &Builder, source_path: []const u8, dest_path: []const u8) !void {
+ fn copyFile(self: *Builder, source_path: []const u8, dest_path: []const u8) !void {
return self.copyFileMode(source_path, dest_path, os.default_file_mode);
}
- fn copyFileMode(self: &Builder, source_path: []const u8, dest_path: []const u8, mode: os.FileMode) !void {
+ fn copyFileMode(self: *Builder, source_path: []const u8, dest_path: []const u8, mode: os.FileMode) !void {
if (self.verbose) {
warn("cp {} {}\n", source_path, dest_path);
}
@@ -629,15 +629,15 @@ pub const Builder = struct {
};
}
- fn pathFromRoot(self: &Builder, rel_path: []const u8) []u8 {
+ fn pathFromRoot(self: *Builder, rel_path: []const u8) []u8 {
return os.path.resolve(self.allocator, self.build_root, rel_path) catch unreachable;
}
- pub fn fmt(self: &Builder, comptime format: []const u8, args: ...) []u8 {
+ pub fn fmt(self: *Builder, comptime format: []const u8, args: ...) []u8 {
return fmt_lib.allocPrint(self.allocator, format, args) catch unreachable;
}
- fn getCCExe(self: &Builder) []const u8 {
+ fn getCCExe(self: *Builder) []const u8 {
if (builtin.environ == builtin.Environ.msvc) {
return "cl.exe";
} else {
@@ -645,7 +645,7 @@ pub const Builder = struct {
}
}
- pub fn findProgram(self: &Builder, names: []const []const u8, paths: []const []const u8) ![]const u8 {
+ pub fn findProgram(self: *Builder, names: []const []const u8, paths: []const []const u8) ![]const u8 {
// TODO report error for ambiguous situations
const exe_extension = (Target{ .Native = {} }).exeFileExt();
for (self.search_prefixes.toSliceConst()) |search_prefix| {
@@ -693,7 +693,7 @@ pub const Builder = struct {
return error.FileNotFound;
}
- pub fn exec(self: &Builder, argv: []const []const u8) ![]u8 {
+ pub fn exec(self: *Builder, argv: []const []const u8) ![]u8 {
const max_output_size = 100 * 1024;
const result = try os.ChildProcess.exec(self.allocator, argv, null, null, max_output_size);
switch (result.term) {
@@ -715,7 +715,7 @@ pub const Builder = struct {
}
}
- pub fn addSearchPrefix(self: &Builder, search_prefix: []const u8) void {
+ pub fn addSearchPrefix(self: *Builder, search_prefix: []const u8) void {
self.search_prefixes.append(search_prefix) catch unreachable;
}
};
@@ -736,7 +736,7 @@ pub const Target = union(enum) {
Native: void,
Cross: CrossTarget,
- pub fn oFileExt(self: &const Target) []const u8 {
+ pub fn oFileExt(self: *const Target) []const u8 {
const environ = switch (self.*) {
Target.Native => builtin.environ,
Target.Cross => |t| t.environ,
@@ -747,49 +747,49 @@ pub const Target = union(enum) {
};
}
- pub fn exeFileExt(self: &const Target) []const u8 {
+ pub fn exeFileExt(self: *const Target) []const u8 {
return switch (self.getOs()) {
builtin.Os.windows => ".exe",
else => "",
};
}
- pub fn libFileExt(self: &const Target) []const u8 {
+ pub fn libFileExt(self: *const Target) []const u8 {
return switch (self.getOs()) {
builtin.Os.windows => ".lib",
else => ".a",
};
}
- pub fn getOs(self: &const Target) builtin.Os {
+ pub fn getOs(self: *const Target) builtin.Os {
return switch (self.*) {
Target.Native => builtin.os,
Target.Cross => |t| t.os,
};
}
- pub fn isDarwin(self: &const Target) bool {
+ pub fn isDarwin(self: *const Target) bool {
return switch (self.getOs()) {
builtin.Os.ios, builtin.Os.macosx => true,
else => false,
};
}
- pub fn isWindows(self: &const Target) bool {
+ pub fn isWindows(self: *const Target) bool {
return switch (self.getOs()) {
builtin.Os.windows => true,
else => false,
};
}
- pub fn wantSharedLibSymLinks(self: &const Target) bool {
+ pub fn wantSharedLibSymLinks(self: *const Target) bool {
return !self.isWindows();
}
};
pub const LibExeObjStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
name: []const u8,
target: Target,
link_libs: BufSet,
@@ -836,56 +836,56 @@ pub const LibExeObjStep = struct {
Obj,
};
- pub fn createSharedLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8, ver: &const Version) &LibExeObjStep {
+ pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: *const Version) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
return self;
}
- pub fn createCSharedLibrary(builder: &Builder, name: []const u8, version: &const Version) &LibExeObjStep {
+ pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: *const Version) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Lib, version, false);
return self;
}
- pub fn createStaticLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
+ pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
return self;
}
- pub fn createCStaticLibrary(builder: &Builder, name: []const u8) &LibExeObjStep {
+ pub fn createCStaticLibrary(builder: *Builder, name: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
return self;
}
- pub fn createObject(builder: &Builder, name: []const u8, root_src: []const u8) &LibExeObjStep {
+ pub fn createObject(builder: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
return self;
}
- pub fn createCObject(builder: &Builder, name: []const u8, src: []const u8) &LibExeObjStep {
+ pub fn createCObject(builder: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
self.object_src = src;
return self;
}
- pub fn createExecutable(builder: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
+ pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
return self;
}
- pub fn createCExecutable(builder: &Builder, name: []const u8) &LibExeObjStep {
+ pub fn createCExecutable(builder: *Builder, name: []const u8) *LibExeObjStep {
const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
return self;
}
- fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: &const Version) LibExeObjStep {
+ fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: *const Version) LibExeObjStep {
var self = LibExeObjStep{
.strip = false,
.builder = builder,
@@ -924,7 +924,7 @@ pub const LibExeObjStep = struct {
return self;
}
- fn initC(builder: &Builder, name: []const u8, kind: Kind, version: &const Version, static: bool) LibExeObjStep {
+ fn initC(builder: *Builder, name: []const u8, kind: Kind, version: *const Version, static: bool) LibExeObjStep {
var self = LibExeObjStep{
.builder = builder,
.name = name,
@@ -964,7 +964,7 @@ pub const LibExeObjStep = struct {
return self;
}
- fn computeOutFileNames(self: &LibExeObjStep) void {
+ fn computeOutFileNames(self: *LibExeObjStep) void {
switch (self.kind) {
Kind.Obj => {
self.out_filename = self.builder.fmt("{}{}", self.name, self.target.oFileExt());
@@ -996,7 +996,7 @@ pub const LibExeObjStep = struct {
}
}
- pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
+ pub fn setTarget(self: *LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
self.target = Target{
.Cross = CrossTarget{
.arch = target_arch,
@@ -1008,16 +1008,16 @@ pub const LibExeObjStep = struct {
}
// TODO respect this in the C args
- pub fn setLinkerScriptPath(self: &LibExeObjStep, path: []const u8) void {
+ pub fn setLinkerScriptPath(self: *LibExeObjStep, path: []const u8) void {
self.linker_script = path;
}
- pub fn linkFramework(self: &LibExeObjStep, framework_name: []const u8) void {
+ pub fn linkFramework(self: *LibExeObjStep, framework_name: []const u8) void {
assert(self.target.isDarwin());
self.frameworks.put(framework_name) catch unreachable;
}
- pub fn linkLibrary(self: &LibExeObjStep, lib: &LibExeObjStep) void {
+ pub fn linkLibrary(self: *LibExeObjStep, lib: *LibExeObjStep) void {
assert(self.kind != Kind.Obj);
assert(lib.kind == Kind.Lib);
@@ -1038,26 +1038,26 @@ pub const LibExeObjStep = struct {
}
}
- pub fn linkSystemLibrary(self: &LibExeObjStep, name: []const u8) void {
+ pub fn linkSystemLibrary(self: *LibExeObjStep, name: []const u8) void {
assert(self.kind != Kind.Obj);
self.link_libs.put(name) catch unreachable;
}
- pub fn addSourceFile(self: &LibExeObjStep, file: []const u8) void {
+ pub fn addSourceFile(self: *LibExeObjStep, file: []const u8) void {
assert(self.kind != Kind.Obj);
assert(!self.is_zig);
self.source_files.append(file) catch unreachable;
}
- pub fn setVerboseLink(self: &LibExeObjStep, value: bool) void {
+ pub fn setVerboseLink(self: *LibExeObjStep, value: bool) void {
self.verbose_link = value;
}
- pub fn setBuildMode(self: &LibExeObjStep, mode: builtin.Mode) void {
+ pub fn setBuildMode(self: *LibExeObjStep, mode: builtin.Mode) void {
self.build_mode = mode;
}
- pub fn setOutputPath(self: &LibExeObjStep, file_path: []const u8) void {
+ pub fn setOutputPath(self: *LibExeObjStep, file_path: []const u8) void {
self.output_path = file_path;
// catch a common mistake
@@ -1066,11 +1066,11 @@ pub const LibExeObjStep = struct {
}
}
- pub fn getOutputPath(self: &LibExeObjStep) []const u8 {
+ pub fn getOutputPath(self: *LibExeObjStep) []const u8 {
return if (self.output_path) |output_path| output_path else os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename) catch unreachable;
}
- pub fn setOutputHPath(self: &LibExeObjStep, file_path: []const u8) void {
+ pub fn setOutputHPath(self: *LibExeObjStep, file_path: []const u8) void {
self.output_h_path = file_path;
// catch a common mistake
@@ -1079,21 +1079,21 @@ pub const LibExeObjStep = struct {
}
}
- pub fn getOutputHPath(self: &LibExeObjStep) []const u8 {
+ pub fn getOutputHPath(self: *LibExeObjStep) []const u8 {
return if (self.output_h_path) |output_h_path| output_h_path else os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename) catch unreachable;
}
- pub fn addAssemblyFile(self: &LibExeObjStep, path: []const u8) void {
+ pub fn addAssemblyFile(self: *LibExeObjStep, path: []const u8) void {
self.assembly_files.append(path) catch unreachable;
}
- pub fn addObjectFile(self: &LibExeObjStep, path: []const u8) void {
+ pub fn addObjectFile(self: *LibExeObjStep, path: []const u8) void {
assert(self.kind != Kind.Obj);
self.object_files.append(path) catch unreachable;
}
- pub fn addObject(self: &LibExeObjStep, obj: &LibExeObjStep) void {
+ pub fn addObject(self: *LibExeObjStep, obj: *LibExeObjStep) void {
assert(obj.kind == Kind.Obj);
assert(self.kind != Kind.Obj);
@@ -1110,15 +1110,15 @@ pub const LibExeObjStep = struct {
self.include_dirs.append(self.builder.cache_root) catch unreachable;
}
- pub fn addIncludeDir(self: &LibExeObjStep, path: []const u8) void {
+ pub fn addIncludeDir(self: *LibExeObjStep, path: []const u8) void {
self.include_dirs.append(path) catch unreachable;
}
- pub fn addLibPath(self: &LibExeObjStep, path: []const u8) void {
+ pub fn addLibPath(self: *LibExeObjStep, path: []const u8) void {
self.lib_paths.append(path) catch unreachable;
}
- pub fn addPackagePath(self: &LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
+ pub fn addPackagePath(self: *LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
assert(self.is_zig);
self.packages.append(Pkg{
@@ -1127,23 +1127,23 @@ pub const LibExeObjStep = struct {
}) catch unreachable;
}
- pub fn addCompileFlags(self: &LibExeObjStep, flags: []const []const u8) void {
+ pub fn addCompileFlags(self: *LibExeObjStep, flags: []const []const u8) void {
for (flags) |flag| {
self.cflags.append(flag) catch unreachable;
}
}
- pub fn setNoStdLib(self: &LibExeObjStep, disable: bool) void {
+ pub fn setNoStdLib(self: *LibExeObjStep, disable: bool) void {
assert(!self.is_zig);
self.disable_libc = disable;
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(LibExeObjStep, "step", step);
return if (self.is_zig) self.makeZig() else self.makeC();
}
- fn makeZig(self: &LibExeObjStep) !void {
+ fn makeZig(self: *LibExeObjStep) !void {
const builder = self.builder;
assert(self.is_zig);
@@ -1309,7 +1309,7 @@ pub const LibExeObjStep = struct {
}
}
- fn appendCompileFlags(self: &LibExeObjStep, args: &ArrayList([]const u8)) void {
+ fn appendCompileFlags(self: *LibExeObjStep, args: *ArrayList([]const u8)) void {
if (!self.strip) {
args.append("-g") catch unreachable;
}
@@ -1354,7 +1354,7 @@ pub const LibExeObjStep = struct {
}
}
- fn makeC(self: &LibExeObjStep) !void {
+ fn makeC(self: *LibExeObjStep) !void {
const builder = self.builder;
const cc = builder.getCCExe();
@@ -1580,7 +1580,7 @@ pub const LibExeObjStep = struct {
pub const TestStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
root_src: []const u8,
build_mode: builtin.Mode,
verbose: bool,
@@ -1591,7 +1591,7 @@ pub const TestStep = struct {
exec_cmd_args: ?[]const ?[]const u8,
include_dirs: ArrayList([]const u8),
- pub fn init(builder: &Builder, root_src: []const u8) TestStep {
+ pub fn init(builder: *Builder, root_src: []const u8) TestStep {
const step_name = builder.fmt("test {}", root_src);
return TestStep{
.step = Step.init(step_name, builder.allocator, make),
@@ -1608,31 +1608,31 @@ pub const TestStep = struct {
};
}
- pub fn setVerbose(self: &TestStep, value: bool) void {
+ pub fn setVerbose(self: *TestStep, value: bool) void {
self.verbose = value;
}
- pub fn addIncludeDir(self: &TestStep, path: []const u8) void {
+ pub fn addIncludeDir(self: *TestStep, path: []const u8) void {
self.include_dirs.append(path) catch unreachable;
}
- pub fn setBuildMode(self: &TestStep, mode: builtin.Mode) void {
+ pub fn setBuildMode(self: *TestStep, mode: builtin.Mode) void {
self.build_mode = mode;
}
- pub fn linkSystemLibrary(self: &TestStep, name: []const u8) void {
+ pub fn linkSystemLibrary(self: *TestStep, name: []const u8) void {
self.link_libs.put(name) catch unreachable;
}
- pub fn setNamePrefix(self: &TestStep, text: []const u8) void {
+ pub fn setNamePrefix(self: *TestStep, text: []const u8) void {
self.name_prefix = text;
}
- pub fn setFilter(self: &TestStep, text: ?[]const u8) void {
+ pub fn setFilter(self: *TestStep, text: ?[]const u8) void {
self.filter = text;
}
- pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
+ pub fn setTarget(self: *TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
self.target = Target{
.Cross = CrossTarget{
.arch = target_arch,
@@ -1642,11 +1642,11 @@ pub const TestStep = struct {
};
}
- pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) void {
+ pub fn setExecCmd(self: *TestStep, args: []const ?[]const u8) void {
self.exec_cmd_args = args;
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(TestStep, "step", step);
const builder = self.builder;
@@ -1739,13 +1739,13 @@ pub const TestStep = struct {
pub const CommandStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
argv: [][]const u8,
cwd: ?[]const u8,
- env_map: &const BufMap,
+ env_map: *const BufMap,
/// ::argv is copied.
- pub fn create(builder: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) &CommandStep {
+ pub fn create(builder: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) *CommandStep {
const self = builder.allocator.create(CommandStep) catch unreachable;
self.* = CommandStep{
.builder = builder,
@@ -1759,7 +1759,7 @@ pub const CommandStep = struct {
return self;
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(CommandStep, "step", step);
const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else self.builder.build_root;
@@ -1769,13 +1769,13 @@ pub const CommandStep = struct {
const InstallArtifactStep = struct {
step: Step,
- builder: &Builder,
- artifact: &LibExeObjStep,
+ builder: *Builder,
+ artifact: *LibExeObjStep,
dest_file: []const u8,
const Self = this;
- pub fn create(builder: &Builder, artifact: &LibExeObjStep) &Self {
+ pub fn create(builder: *Builder, artifact: *LibExeObjStep) *Self {
const self = builder.allocator.create(Self) catch unreachable;
const dest_dir = switch (artifact.kind) {
LibExeObjStep.Kind.Obj => unreachable,
@@ -1797,7 +1797,7 @@ const InstallArtifactStep = struct {
return self;
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(Self, "step", step);
const builder = self.builder;
@@ -1818,11 +1818,11 @@ const InstallArtifactStep = struct {
pub const InstallFileStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
src_path: []const u8,
dest_path: []const u8,
- pub fn init(builder: &Builder, src_path: []const u8, dest_path: []const u8) InstallFileStep {
+ pub fn init(builder: *Builder, src_path: []const u8, dest_path: []const u8) InstallFileStep {
return InstallFileStep{
.builder = builder,
.step = Step.init(builder.fmt("install {}", src_path), builder.allocator, make),
@@ -1831,7 +1831,7 @@ pub const InstallFileStep = struct {
};
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(InstallFileStep, "step", step);
try self.builder.copyFile(self.src_path, self.dest_path);
}
@@ -1839,11 +1839,11 @@ pub const InstallFileStep = struct {
pub const WriteFileStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
file_path: []const u8,
data: []const u8,
- pub fn init(builder: &Builder, file_path: []const u8, data: []const u8) WriteFileStep {
+ pub fn init(builder: *Builder, file_path: []const u8, data: []const u8) WriteFileStep {
return WriteFileStep{
.builder = builder,
.step = Step.init(builder.fmt("writefile {}", file_path), builder.allocator, make),
@@ -1852,7 +1852,7 @@ pub const WriteFileStep = struct {
};
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(WriteFileStep, "step", step);
const full_path = self.builder.pathFromRoot(self.file_path);
const full_path_dir = os.path.dirname(full_path);
@@ -1869,10 +1869,10 @@ pub const WriteFileStep = struct {
pub const LogStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
data: []const u8,
- pub fn init(builder: &Builder, data: []const u8) LogStep {
+ pub fn init(builder: *Builder, data: []const u8) LogStep {
return LogStep{
.builder = builder,
.step = Step.init(builder.fmt("log {}", data), builder.allocator, make),
@@ -1880,7 +1880,7 @@ pub const LogStep = struct {
};
}
- fn make(step: &Step) error!void {
+ fn make(step: *Step) error!void {
const self = @fieldParentPtr(LogStep, "step", step);
warn("{}", self.data);
}
@@ -1888,10 +1888,10 @@ pub const LogStep = struct {
pub const RemoveDirStep = struct {
step: Step,
- builder: &Builder,
+ builder: *Builder,
dir_path: []const u8,
- pub fn init(builder: &Builder, dir_path: []const u8) RemoveDirStep {
+ pub fn init(builder: *Builder, dir_path: []const u8) RemoveDirStep {
return RemoveDirStep{
.builder = builder,
.step = Step.init(builder.fmt("RemoveDir {}", dir_path), builder.allocator, make),
@@ -1899,7 +1899,7 @@ pub const RemoveDirStep = struct {
};
}
- fn make(step: &Step) !void {
+ fn make(step: *Step) !void {
const self = @fieldParentPtr(RemoveDirStep, "step", step);
const full_path = self.builder.pathFromRoot(self.dir_path);
@@ -1912,39 +1912,39 @@ pub const RemoveDirStep = struct {
pub const Step = struct {
name: []const u8,
- makeFn: fn (self: &Step) error!void,
- dependencies: ArrayList(&Step),
+ makeFn: fn (self: *Step) error!void,
+ dependencies: ArrayList(*Step),
loop_flag: bool,
done_flag: bool,
- pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn (&Step) error!void) Step {
+ pub fn init(name: []const u8, allocator: *Allocator, makeFn: fn (*Step) error!void) Step {
return Step{
.name = name,
.makeFn = makeFn,
- .dependencies = ArrayList(&Step).init(allocator),
+ .dependencies = ArrayList(*Step).init(allocator),
.loop_flag = false,
.done_flag = false,
};
}
- pub fn initNoOp(name: []const u8, allocator: &Allocator) Step {
+ pub fn initNoOp(name: []const u8, allocator: *Allocator) Step {
return init(name, allocator, makeNoOp);
}
- pub fn make(self: &Step) !void {
+ pub fn make(self: *Step) !void {
if (self.done_flag) return;
try self.makeFn(self);
self.done_flag = true;
}
- pub fn dependOn(self: &Step, other: &Step) void {
+ pub fn dependOn(self: *Step, other: *Step) void {
self.dependencies.append(other) catch unreachable;
}
- fn makeNoOp(self: &Step) error!void {}
+ fn makeNoOp(self: *Step) error!void {}
};
-fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {
+fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {
const out_dir = os.path.dirname(output_path);
const out_basename = os.path.basename(output_path);
// sym link for libfoo.so.1 to libfoo.so.1.2.3
diff --git a/std/c/darwin.zig b/std/c/darwin.zig
index 6a33c994bf..69395e6b27 100644
--- a/std/c/darwin.zig
+++ b/std/c/darwin.zig
@@ -1,10 +1,10 @@
-extern "c" fn __error() &c_int;
-pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) c_int;
+extern "c" fn __error() *c_int;
+pub extern "c" fn _NSGetExecutablePath(buf: *u8, bufsize: *u32) c_int;
-pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: &u8, buf_len: usize, basep: &i64) usize;
+pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: *u8, buf_len: usize, basep: *i64) usize;
pub extern "c" fn mach_absolute_time() u64;
-pub extern "c" fn mach_timebase_info(tinfo: ?&mach_timebase_info_data) void;
+pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void;
pub use @import("../os/darwin_errno.zig");
diff --git a/std/c/index.zig b/std/c/index.zig
index f9704f4738..114b79cdae 100644
--- a/std/c/index.zig
+++ b/std/c/index.zig
@@ -13,49 +13,49 @@ pub extern "c" fn abort() noreturn;
pub extern "c" fn exit(code: c_int) noreturn;
pub extern "c" fn isatty(fd: c_int) c_int;
pub extern "c" fn close(fd: c_int) c_int;
-pub extern "c" fn fstat(fd: c_int, buf: &Stat) c_int;
-pub extern "c" fn @"fstat$INODE64"(fd: c_int, buf: &Stat) c_int;
+pub extern "c" fn fstat(fd: c_int, buf: *Stat) c_int;
+pub extern "c" fn @"fstat$INODE64"(fd: c_int, buf: *Stat) c_int;
pub extern "c" fn lseek(fd: c_int, offset: isize, whence: c_int) isize;
-pub extern "c" fn open(path: &const u8, oflag: c_int, ...) c_int;
+pub extern "c" fn open(path: *const u8, oflag: c_int, ...) c_int;
pub extern "c" fn raise(sig: c_int) c_int;
-pub extern "c" fn read(fd: c_int, buf: &c_void, nbyte: usize) isize;
-pub extern "c" fn stat(noalias path: &const u8, noalias buf: &Stat) c_int;
-pub extern "c" fn write(fd: c_int, buf: &const c_void, nbyte: usize) isize;
-pub extern "c" fn mmap(addr: ?&c_void, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: isize) ?&c_void;
-pub extern "c" fn munmap(addr: &c_void, len: usize) c_int;
-pub extern "c" fn unlink(path: &const u8) c_int;
-pub extern "c" fn getcwd(buf: &u8, size: usize) ?&u8;
-pub extern "c" fn waitpid(pid: c_int, stat_loc: &c_int, options: c_int) c_int;
+pub extern "c" fn read(fd: c_int, buf: *c_void, nbyte: usize) isize;
+pub extern "c" fn stat(noalias path: *const u8, noalias buf: *Stat) c_int;
+pub extern "c" fn write(fd: c_int, buf: *const c_void, nbyte: usize) isize;
+pub extern "c" fn mmap(addr: ?*c_void, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: isize) ?*c_void;
+pub extern "c" fn munmap(addr: *c_void, len: usize) c_int;
+pub extern "c" fn unlink(path: *const u8) c_int;
+pub extern "c" fn getcwd(buf: *u8, size: usize) ?*u8;
+pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int;
pub extern "c" fn fork() c_int;
-pub extern "c" fn access(path: &const u8, mode: c_uint) c_int;
-pub extern "c" fn pipe(fds: &c_int) c_int;
-pub extern "c" fn mkdir(path: &const u8, mode: c_uint) c_int;
-pub extern "c" fn symlink(existing: &const u8, new: &const u8) c_int;
-pub extern "c" fn rename(old: &const u8, new: &const u8) c_int;
-pub extern "c" fn chdir(path: &const u8) c_int;
-pub extern "c" fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) c_int;
+pub extern "c" fn access(path: *const u8, mode: c_uint) c_int;
+pub extern "c" fn pipe(fds: *c_int) c_int;
+pub extern "c" fn mkdir(path: *const u8, mode: c_uint) c_int;
+pub extern "c" fn symlink(existing: *const u8, new: *const u8) c_int;
+pub extern "c" fn rename(old: *const u8, new: *const u8) c_int;
+pub extern "c" fn chdir(path: *const u8) c_int;
+pub extern "c" fn execve(path: *const u8, argv: *const ?*const u8, envp: *const ?*const u8) c_int;
pub extern "c" fn dup(fd: c_int) c_int;
pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int;
-pub extern "c" fn readlink(noalias path: &const u8, noalias buf: &u8, bufsize: usize) isize;
-pub extern "c" fn realpath(noalias file_name: &const u8, noalias resolved_name: &u8) ?&u8;
-pub extern "c" fn sigprocmask(how: c_int, noalias set: &const sigset_t, noalias oset: ?&sigset_t) c_int;
-pub extern "c" fn gettimeofday(tv: ?&timeval, tz: ?&timezone) c_int;
-pub extern "c" fn sigaction(sig: c_int, noalias act: &const Sigaction, noalias oact: ?&Sigaction) c_int;
-pub extern "c" fn nanosleep(rqtp: &const timespec, rmtp: ?×pec) c_int;
+pub extern "c" fn readlink(noalias path: *const u8, noalias buf: *u8, bufsize: usize) isize;
+pub extern "c" fn realpath(noalias file_name: *const u8, noalias resolved_name: *u8) ?*u8;
+pub extern "c" fn sigprocmask(how: c_int, noalias set: *const sigset_t, noalias oset: ?*sigset_t) c_int;
+pub extern "c" fn gettimeofday(tv: ?*timeval, tz: ?*timezone) c_int;
+pub extern "c" fn sigaction(sig: c_int, noalias act: *const Sigaction, noalias oact: ?*Sigaction) c_int;
+pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
-pub extern "c" fn rmdir(path: &const u8) c_int;
+pub extern "c" fn rmdir(path: *const u8) c_int;
-pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?&c_void;
-pub extern "c" fn malloc(usize) ?&c_void;
-pub extern "c" fn realloc(&c_void, usize) ?&c_void;
-pub extern "c" fn free(&c_void) void;
-pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) c_int;
+pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
+pub extern "c" fn malloc(usize) ?*c_void;
+pub extern "c" fn realloc(*c_void, usize) ?*c_void;
+pub extern "c" fn free(*c_void) void;
+pub extern "c" fn posix_memalign(memptr: **c_void, alignment: usize, size: usize) c_int;
-pub extern "pthread" fn pthread_create(noalias newthread: &pthread_t, noalias attr: ?&const pthread_attr_t, start_routine: extern fn (?&c_void) ?&c_void, noalias arg: ?&c_void) c_int;
-pub extern "pthread" fn pthread_attr_init(attr: &pthread_attr_t) c_int;
-pub extern "pthread" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int;
-pub extern "pthread" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int;
-pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?&?&c_void) c_int;
+pub extern "pthread" fn pthread_create(noalias newthread: *pthread_t, noalias attr: ?*const pthread_attr_t, start_routine: extern fn (?*c_void) ?*c_void, noalias arg: ?*c_void) c_int;
+pub extern "pthread" fn pthread_attr_init(attr: *pthread_attr_t) c_int;
+pub extern "pthread" fn pthread_attr_setstack(attr: *pthread_attr_t, stackaddr: *c_void, stacksize: usize) c_int;
+pub extern "pthread" fn pthread_attr_destroy(attr: *pthread_attr_t) c_int;
+pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?*?*c_void) c_int;
-pub const pthread_t = &@OpaqueType();
+pub const pthread_t = *@OpaqueType();
diff --git a/std/c/linux.zig b/std/c/linux.zig
index 7810fec130..0ab043533e 100644
--- a/std/c/linux.zig
+++ b/std/c/linux.zig
@@ -1,7 +1,7 @@
pub use @import("../os/linux/errno.zig");
-pub extern "c" fn getrandom(buf_ptr: &u8, buf_len: usize, flags: c_uint) c_int;
-extern "c" fn __errno_location() &c_int;
+pub extern "c" fn getrandom(buf_ptr: *u8, buf_len: usize, flags: c_uint) c_int;
+extern "c" fn __errno_location() *c_int;
pub const _errno = __errno_location;
pub const pthread_attr_t = extern struct {
diff --git a/std/c/windows.zig b/std/c/windows.zig
index 6e8b17eda8..35ca217131 100644
--- a/std/c/windows.zig
+++ b/std/c/windows.zig
@@ -1 +1 @@
-pub extern "c" fn _errno() &c_int;
+pub extern "c" fn _errno() *c_int;
diff --git a/std/crypto/blake2.zig b/std/crypto/blake2.zig
index bf3193b5d9..f0a9766c00 100644
--- a/std/crypto/blake2.zig
+++ b/std/crypto/blake2.zig
@@ -75,7 +75,7 @@ fn Blake2s(comptime out_len: usize) type {
return s;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
mem.copy(u32, d.h[0..], iv[0..]);
// No key plus default parameters
@@ -90,7 +90,7 @@ fn Blake2s(comptime out_len: usize) type {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial buffer exists from previous update. Copy into buffer then hash.
@@ -113,7 +113,7 @@ fn Blake2s(comptime out_len: usize) type {
d.buf_len += u8(b[off..].len);
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
debug.assert(out.len >= out_len / 8);
mem.set(u8, d.buf[d.buf_len..], 0);
@@ -127,7 +127,7 @@ fn Blake2s(comptime out_len: usize) type {
}
}
- fn round(d: &Self, b: []const u8, last: bool) void {
+ fn round(d: *Self, b: []const u8, last: bool) void {
debug.assert(b.len == 64);
var m: [16]u32 = undefined;
@@ -310,7 +310,7 @@ fn Blake2b(comptime out_len: usize) type {
return s;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
mem.copy(u64, d.h[0..], iv[0..]);
// No key plus default parameters
@@ -325,7 +325,7 @@ fn Blake2b(comptime out_len: usize) type {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial buffer exists from previous update. Copy into buffer then hash.
@@ -348,7 +348,7 @@ fn Blake2b(comptime out_len: usize) type {
d.buf_len += u8(b[off..].len);
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
mem.set(u8, d.buf[d.buf_len..], 0);
d.t += d.buf_len;
d.round(d.buf[0..], true);
@@ -360,7 +360,7 @@ fn Blake2b(comptime out_len: usize) type {
}
}
- fn round(d: &Self, b: []const u8, last: bool) void {
+ fn round(d: *Self, b: []const u8, last: bool) void {
debug.assert(b.len == 128);
var m: [16]u64 = undefined;
diff --git a/std/crypto/md5.zig b/std/crypto/md5.zig
index 3d05597273..c0d1732d37 100644
--- a/std/crypto/md5.zig
+++ b/std/crypto/md5.zig
@@ -44,7 +44,7 @@ pub const Md5 = struct {
return d;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
d.s[0] = 0x67452301;
d.s[1] = 0xEFCDAB89;
d.s[2] = 0x98BADCFE;
@@ -59,7 +59,7 @@ pub const Md5 = struct {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial buffer exists from previous update. Copy into buffer then hash.
@@ -84,7 +84,7 @@ pub const Md5 = struct {
d.total_len +%= b.len;
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
debug.assert(out.len >= 16);
// The buffer here will never be completely full.
@@ -116,7 +116,7 @@ pub const Md5 = struct {
}
}
- fn round(d: &Self, b: []const u8) void {
+ fn round(d: *Self, b: []const u8) void {
debug.assert(b.len == 64);
var s: [16]u32 = undefined;
diff --git a/std/crypto/sha1.zig b/std/crypto/sha1.zig
index e9d8e3e132..9e46fc9239 100644
--- a/std/crypto/sha1.zig
+++ b/std/crypto/sha1.zig
@@ -43,7 +43,7 @@ pub const Sha1 = struct {
return d;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
d.s[0] = 0x67452301;
d.s[1] = 0xEFCDAB89;
d.s[2] = 0x98BADCFE;
@@ -59,7 +59,7 @@ pub const Sha1 = struct {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial buffer exists from previous update. Copy into buffer then hash.
@@ -83,7 +83,7 @@ pub const Sha1 = struct {
d.total_len += b.len;
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
debug.assert(out.len >= 20);
// The buffer here will never be completely full.
@@ -115,7 +115,7 @@ pub const Sha1 = struct {
}
}
- fn round(d: &Self, b: []const u8) void {
+ fn round(d: *Self, b: []const u8) void {
debug.assert(b.len == 64);
var s: [16]u32 = undefined;
diff --git a/std/crypto/sha2.zig b/std/crypto/sha2.zig
index aedc820f44..d1375d73e8 100644
--- a/std/crypto/sha2.zig
+++ b/std/crypto/sha2.zig
@@ -93,7 +93,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
return d;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
d.s[0] = params.iv0;
d.s[1] = params.iv1;
d.s[2] = params.iv2;
@@ -112,7 +112,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial buffer exists from previous update. Copy into buffer then hash.
@@ -136,7 +136,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
d.total_len += b.len;
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
debug.assert(out.len >= params.out_len / 8);
// The buffer here will never be completely full.
@@ -171,7 +171,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
}
}
- fn round(d: &Self, b: []const u8) void {
+ fn round(d: *Self, b: []const u8) void {
debug.assert(b.len == 64);
var s: [64]u32 = undefined;
@@ -434,7 +434,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
return d;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
d.s[0] = params.iv0;
d.s[1] = params.iv1;
d.s[2] = params.iv2;
@@ -453,7 +453,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial buffer exists from previous update. Copy into buffer then hash.
@@ -477,7 +477,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
d.total_len += b.len;
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
debug.assert(out.len >= params.out_len / 8);
// The buffer here will never be completely full.
@@ -512,7 +512,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
}
}
- fn round(d: &Self, b: []const u8) void {
+ fn round(d: *Self, b: []const u8) void {
debug.assert(b.len == 128);
var s: [80]u64 = undefined;
diff --git a/std/crypto/sha3.zig b/std/crypto/sha3.zig
index 75bec57a87..ae02d7a482 100644
--- a/std/crypto/sha3.zig
+++ b/std/crypto/sha3.zig
@@ -26,7 +26,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
return d;
}
- pub fn reset(d: &Self) void {
+ pub fn reset(d: *Self) void {
mem.set(u8, d.s[0..], 0);
d.offset = 0;
d.rate = 200 - (bits / 4);
@@ -38,7 +38,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
d.final(out);
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var ip: usize = 0;
var len = b.len;
var rate = d.rate - d.offset;
@@ -63,7 +63,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
d.offset = offset + len;
}
- pub fn final(d: &Self, out: []u8) void {
+ pub fn final(d: *Self, out: []u8) void {
// padding
d.s[d.offset] ^= delim;
d.s[d.rate - 1] ^= 0x80;
diff --git a/std/crypto/throughput_test.zig b/std/crypto/throughput_test.zig
index c5c4f9fe10..0ad6845d1a 100644
--- a/std/crypto/throughput_test.zig
+++ b/std/crypto/throughput_test.zig
@@ -15,8 +15,8 @@ const BytesToHash = 1024 * MiB;
pub fn main() !void {
var stdout_file = try std.io.getStdOut();
- var stdout_out_stream = std.io.FileOutStream.init(&stdout_file);
- const stdout = &stdout_out_stream.stream;
+ var stdout_out_stream = std.io.FileOutStream.init(*stdout_file);
+ const stdout = *stdout_out_stream.stream;
var block: [HashFunction.block_size]u8 = undefined;
std.mem.set(u8, block[0..], 0);
diff --git a/std/cstr.zig b/std/cstr.zig
index c9f3026064..dfbfb8047f 100644
--- a/std/cstr.zig
+++ b/std/cstr.zig
@@ -9,13 +9,13 @@ pub const line_sep = switch (builtin.os) {
else => "\n",
};
-pub fn len(ptr: &const u8) usize {
+pub fn len(ptr: *const u8) usize {
var count: usize = 0;
while (ptr[count] != 0) : (count += 1) {}
return count;
}
-pub fn cmp(a: &const u8, b: &const u8) i8 {
+pub fn cmp(a: *const u8, b: *const u8) i8 {
var index: usize = 0;
while (a[index] == b[index] and a[index] != 0) : (index += 1) {}
if (a[index] > b[index]) {
@@ -27,11 +27,11 @@ pub fn cmp(a: &const u8, b: &const u8) i8 {
}
}
-pub fn toSliceConst(str: &const u8) []const u8 {
+pub fn toSliceConst(str: *const u8) []const u8 {
return str[0..len(str)];
}
-pub fn toSlice(str: &u8) []u8 {
+pub fn toSlice(str: *u8) []u8 {
return str[0..len(str)];
}
@@ -47,7 +47,7 @@ fn testCStrFnsImpl() void {
/// Returns a mutable slice with 1 more byte of length which is a null byte.
/// Caller owns the returned memory.
-pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) ![]u8 {
+pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![]u8 {
const result = try allocator.alloc(u8, slice.len + 1);
mem.copy(u8, result, slice);
result[slice.len] = 0;
@@ -55,13 +55,13 @@ pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) ![]u8 {
}
pub const NullTerminated2DArray = struct {
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
byte_count: usize,
- ptr: ?&?&u8,
+ ptr: ?*?*u8,
/// Takes N lists of strings, concatenates the lists together, and adds a null terminator
/// Caller must deinit result
- pub fn fromSlices(allocator: &mem.Allocator, slices: []const []const []const u8) !NullTerminated2DArray {
+ pub fn fromSlices(allocator: *mem.Allocator, slices: []const []const []const u8) !NullTerminated2DArray {
var new_len: usize = 1; // 1 for the list null
var byte_count: usize = 0;
for (slices) |slice| {
@@ -75,11 +75,11 @@ pub const NullTerminated2DArray = struct {
const index_size = @sizeOf(usize) * new_len; // size of the ptrs
byte_count += index_size;
- const buf = try allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count);
+ const buf = try allocator.alignedAlloc(u8, @alignOf(?*u8), byte_count);
errdefer allocator.free(buf);
var write_index = index_size;
- const index_buf = ([]?&u8)(buf);
+ const index_buf = ([]?*u8)(buf);
var i: usize = 0;
for (slices) |slice| {
@@ -97,12 +97,12 @@ pub const NullTerminated2DArray = struct {
return NullTerminated2DArray{
.allocator = allocator,
.byte_count = byte_count,
- .ptr = @ptrCast(?&?&u8, buf.ptr),
+ .ptr = @ptrCast(?*?*u8, buf.ptr),
};
}
- pub fn deinit(self: &NullTerminated2DArray) void {
- const buf = @ptrCast(&u8, self.ptr);
+ pub fn deinit(self: *NullTerminated2DArray) void {
+ const buf = @ptrCast(*u8, self.ptr);
self.allocator.free(buf[0..self.byte_count]);
}
};
diff --git a/std/debug/failing_allocator.zig b/std/debug/failing_allocator.zig
index 6b5edff5bf..e16dd21db4 100644
--- a/std/debug/failing_allocator.zig
+++ b/std/debug/failing_allocator.zig
@@ -7,12 +7,12 @@ pub const FailingAllocator = struct {
allocator: mem.Allocator,
index: usize,
fail_index: usize,
- internal_allocator: &mem.Allocator,
+ internal_allocator: *mem.Allocator,
allocated_bytes: usize,
freed_bytes: usize,
deallocations: usize,
- pub fn init(allocator: &mem.Allocator, fail_index: usize) FailingAllocator {
+ pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {
return FailingAllocator{
.internal_allocator = allocator,
.fail_index = fail_index,
@@ -28,7 +28,7 @@ pub const FailingAllocator = struct {
};
}
- fn alloc(allocator: &mem.Allocator, n: usize, alignment: u29) ![]u8 {
+ fn alloc(allocator: *mem.Allocator, n: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
if (self.index == self.fail_index) {
return error.OutOfMemory;
@@ -39,7 +39,7 @@ pub const FailingAllocator = struct {
return result;
}
- fn realloc(allocator: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ fn realloc(allocator: *mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
if (new_size <= old_mem.len) {
self.freed_bytes += old_mem.len - new_size;
@@ -55,7 +55,7 @@ pub const FailingAllocator = struct {
return result;
}
- fn free(allocator: &mem.Allocator, bytes: []u8) void {
+ fn free(allocator: *mem.Allocator, bytes: []u8) void {
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
self.freed_bytes += bytes.len;
self.deallocations += 1;
diff --git a/std/debug/index.zig b/std/debug/index.zig
index 92e565b391..00d9bef121 100644
--- a/std/debug/index.zig
+++ b/std/debug/index.zig
@@ -16,12 +16,12 @@ pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator;
/// TODO atomic/multithread support
var stderr_file: os.File = undefined;
var stderr_file_out_stream: io.FileOutStream = undefined;
-var stderr_stream: ?&io.OutStream(io.FileOutStream.Error) = null;
+var stderr_stream: ?*io.OutStream(io.FileOutStream.Error) = null;
pub fn warn(comptime fmt: []const u8, args: ...) void {
const stderr = getStderrStream() catch return;
stderr.print(fmt, args) catch return;
}
-fn getStderrStream() !&io.OutStream(io.FileOutStream.Error) {
+fn getStderrStream() !*io.OutStream(io.FileOutStream.Error) {
if (stderr_stream) |st| {
return st;
} else {
@@ -33,8 +33,8 @@ fn getStderrStream() !&io.OutStream(io.FileOutStream.Error) {
}
}
-var self_debug_info: ?&ElfStackTrace = null;
-pub fn getSelfDebugInfo() !&ElfStackTrace {
+var self_debug_info: ?*ElfStackTrace = null;
+pub fn getSelfDebugInfo() !*ElfStackTrace {
if (self_debug_info) |info| {
return info;
} else {
@@ -58,7 +58,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
}
/// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.
-pub fn dumpStackTrace(stack_trace: &const builtin.StackTrace) void {
+pub fn dumpStackTrace(stack_trace: *const builtin.StackTrace) void {
const stderr = getStderrStream() catch return;
const debug_info = getSelfDebugInfo() catch |err| {
stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return;
@@ -104,7 +104,7 @@ pub fn panic(comptime format: []const u8, args: ...) noreturn {
var panicking: u8 = 0; // TODO make this a bool
-pub fn panicExtra(trace: ?&const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: ...) noreturn {
+pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: ...) noreturn {
@setCold(true);
if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) {
@@ -130,7 +130,7 @@ const WHITE = "\x1b[37;1m";
const DIM = "\x1b[2m";
const RESET = "\x1b[0m";
-pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var, allocator: &mem.Allocator, debug_info: &ElfStackTrace, tty_color: bool) !void {
+pub fn writeStackTrace(stack_trace: *const builtin.StackTrace, out_stream: var, allocator: *mem.Allocator, debug_info: *ElfStackTrace, tty_color: bool) !void {
var frame_index: usize = undefined;
var frames_left: usize = undefined;
if (stack_trace.index < stack_trace.instruction_addresses.len) {
@@ -150,7 +150,7 @@ pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var,
}
}
-pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator, debug_info: &ElfStackTrace, tty_color: bool, start_addr: ?usize) !void {
+pub fn writeCurrentStackTrace(out_stream: var, allocator: *mem.Allocator, debug_info: *ElfStackTrace, tty_color: bool, start_addr: ?usize) !void {
const AddressState = union(enum) {
NotLookingForStartAddress,
LookingForStartAddress: usize,
@@ -166,8 +166,8 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator, debug_
}
var fp = @ptrToInt(@frameAddress());
- while (fp != 0) : (fp = @intToPtr(&const usize, fp).*) {
- const return_address = @intToPtr(&const usize, fp + @sizeOf(usize)).*;
+ while (fp != 0) : (fp = @intToPtr(*const usize, fp).*) {
+ const return_address = @intToPtr(*const usize, fp + @sizeOf(usize)).*;
switch (addr_state) {
AddressState.NotLookingForStartAddress => {},
@@ -183,7 +183,7 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator, debug_
}
}
-fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: usize) !void {
+fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: usize) !void {
const ptr_hex = "0x{x}";
switch (builtin.os) {
@@ -236,7 +236,7 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us
}
}
-pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace {
+pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
switch (builtin.object_format) {
builtin.ObjectFormat.elf => {
const st = try allocator.create(ElfStackTrace);
@@ -289,7 +289,7 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace {
}
}
-fn printLineFromFile(allocator: &mem.Allocator, out_stream: var, line_info: &const LineInfo) !void {
+fn printLineFromFile(allocator: *mem.Allocator, out_stream: var, line_info: *const LineInfo) !void {
var f = try os.File.openRead(allocator, line_info.file_name);
defer f.close();
// TODO fstat and make sure that the file has the correct size
@@ -325,32 +325,32 @@ pub const ElfStackTrace = switch (builtin.os) {
builtin.Os.macosx => struct {
symbol_table: macho.SymbolTable,
- pub fn close(self: &ElfStackTrace) void {
+ pub fn close(self: *ElfStackTrace) void {
self.symbol_table.deinit();
}
},
else => struct {
self_exe_file: os.File,
elf: elf.Elf,
- debug_info: &elf.SectionHeader,
- debug_abbrev: &elf.SectionHeader,
- debug_str: &elf.SectionHeader,
- debug_line: &elf.SectionHeader,
- debug_ranges: ?&elf.SectionHeader,
+ debug_info: *elf.SectionHeader,
+ debug_abbrev: *elf.SectionHeader,
+ debug_str: *elf.SectionHeader,
+ debug_line: *elf.SectionHeader,
+ debug_ranges: ?*elf.SectionHeader,
abbrev_table_list: ArrayList(AbbrevTableHeader),
compile_unit_list: ArrayList(CompileUnit),
- pub fn allocator(self: &const ElfStackTrace) &mem.Allocator {
+ pub fn allocator(self: *const ElfStackTrace) *mem.Allocator {
return self.abbrev_table_list.allocator;
}
- pub fn readString(self: &ElfStackTrace) ![]u8 {
+ pub fn readString(self: *ElfStackTrace) ![]u8 {
var in_file_stream = io.FileInStream.init(&self.self_exe_file);
const in_stream = &in_file_stream.stream;
return readStringRaw(self.allocator(), in_stream);
}
- pub fn close(self: &ElfStackTrace) void {
+ pub fn close(self: *ElfStackTrace) void {
self.self_exe_file.close();
self.elf.close();
}
@@ -365,7 +365,7 @@ const PcRange = struct {
const CompileUnit = struct {
version: u16,
is_64: bool,
- die: &Die,
+ die: *Die,
index: usize,
pc_range: ?PcRange,
};
@@ -408,7 +408,7 @@ const Constant = struct {
payload: []u8,
signed: bool,
- fn asUnsignedLe(self: &const Constant) !u64 {
+ fn asUnsignedLe(self: *const Constant) !u64 {
if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo;
if (self.signed) return error.InvalidDebugInfo;
return mem.readInt(self.payload, u64, builtin.Endian.Little);
@@ -425,14 +425,14 @@ const Die = struct {
value: FormValue,
};
- fn getAttr(self: &const Die, id: u64) ?&const FormValue {
+ fn getAttr(self: *const Die, id: u64) ?*const FormValue {
for (self.attrs.toSliceConst()) |*attr| {
if (attr.id == id) return &attr.value;
}
return null;
}
- fn getAttrAddr(self: &const Die, id: u64) !u64 {
+ fn getAttrAddr(self: *const Die, id: u64) !u64 {
const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.Address => |value| value,
@@ -440,7 +440,7 @@ const Die = struct {
};
}
- fn getAttrSecOffset(self: &const Die, id: u64) !u64 {
+ fn getAttrSecOffset(self: *const Die, id: u64) !u64 {
const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.Const => |value| value.asUnsignedLe(),
@@ -449,7 +449,7 @@ const Die = struct {
};
}
- fn getAttrUnsignedLe(self: &const Die, id: u64) !u64 {
+ fn getAttrUnsignedLe(self: *const Die, id: u64) !u64 {
const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.Const => |value| value.asUnsignedLe(),
@@ -457,7 +457,7 @@ const Die = struct {
};
}
- fn getAttrString(self: &const Die, st: &ElfStackTrace, id: u64) ![]u8 {
+ fn getAttrString(self: *const Die, st: *ElfStackTrace, id: u64) ![]u8 {
const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.String => |value| value,
@@ -478,9 +478,9 @@ const LineInfo = struct {
line: usize,
column: usize,
file_name: []u8,
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
- fn deinit(self: &const LineInfo) void {
+ fn deinit(self: *const LineInfo) void {
self.allocator.free(self.file_name);
}
};
@@ -496,7 +496,7 @@ const LineNumberProgram = struct {
target_address: usize,
include_dirs: []const []const u8,
- file_entries: &ArrayList(FileEntry),
+ file_entries: *ArrayList(FileEntry),
prev_address: usize,
prev_file: usize,
@@ -506,7 +506,7 @@ const LineNumberProgram = struct {
prev_basic_block: bool,
prev_end_sequence: bool,
- pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: &ArrayList(FileEntry), target_address: usize) LineNumberProgram {
+ pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: *ArrayList(FileEntry), target_address: usize) LineNumberProgram {
return LineNumberProgram{
.address = 0,
.file = 1,
@@ -528,7 +528,7 @@ const LineNumberProgram = struct {
};
}
- pub fn checkLineMatch(self: &LineNumberProgram) !?LineInfo {
+ pub fn checkLineMatch(self: *LineNumberProgram) !?LineInfo {
if (self.target_address >= self.prev_address and self.target_address < self.address) {
const file_entry = if (self.prev_file == 0) {
return error.MissingDebugInfo;
@@ -562,7 +562,7 @@ const LineNumberProgram = struct {
}
};
-fn readStringRaw(allocator: &mem.Allocator, in_stream: var) ![]u8 {
+fn readStringRaw(allocator: *mem.Allocator, in_stream: var) ![]u8 {
var buf = ArrayList(u8).init(allocator);
while (true) {
const byte = try in_stream.readByte();
@@ -572,30 +572,30 @@ fn readStringRaw(allocator: &mem.Allocator, in_stream: var) ![]u8 {
return buf.toSlice();
}
-fn getString(st: &ElfStackTrace, offset: u64) ![]u8 {
+fn getString(st: *ElfStackTrace, offset: u64) ![]u8 {
const pos = st.debug_str.offset + offset;
try st.self_exe_file.seekTo(pos);
return st.readString();
}
-fn readAllocBytes(allocator: &mem.Allocator, in_stream: var, size: usize) ![]u8 {
+fn readAllocBytes(allocator: *mem.Allocator, in_stream: var, size: usize) ![]u8 {
const buf = try allocator.alloc(u8, size);
errdefer allocator.free(buf);
if ((try in_stream.read(buf)) < size) return error.EndOfFile;
return buf;
}
-fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
+fn parseFormValueBlockLen(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue {
const buf = try readAllocBytes(allocator, in_stream, size);
return FormValue{ .Block = buf };
}
-fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
+fn parseFormValueBlock(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue {
const block_len = try in_stream.readVarInt(builtin.Endian.Little, usize, size);
return parseFormValueBlockLen(allocator, in_stream, block_len);
}
-fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue {
+fn parseFormValueConstant(allocator: *mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue {
return FormValue{
.Const = Constant{
.signed = signed,
@@ -612,12 +612,12 @@ fn parseFormValueTargetAddrSize(in_stream: var) !u64 {
return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) else unreachable;
}
-fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
+fn parseFormValueRefLen(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue {
const buf = try readAllocBytes(allocator, in_stream, size);
return FormValue{ .Ref = buf };
}
-fn parseFormValueRef(allocator: &mem.Allocator, in_stream: var, comptime T: type) !FormValue {
+fn parseFormValueRef(allocator: *mem.Allocator, in_stream: var, comptime T: type) !FormValue {
const block_len = try in_stream.readIntLe(T);
return parseFormValueRefLen(allocator, in_stream, block_len);
}
@@ -632,7 +632,7 @@ const ParseFormValueError = error{
OutOfMemory,
};
-fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64: bool) ParseFormValueError!FormValue {
+fn parseFormValue(allocator: *mem.Allocator, in_stream: var, form_id: u64, is_64: bool) ParseFormValueError!FormValue {
return switch (form_id) {
DW.FORM_addr => FormValue{ .Address = try parseFormValueTargetAddrSize(in_stream) },
DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1),
@@ -682,7 +682,7 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
};
}
-fn parseAbbrevTable(st: &ElfStackTrace) !AbbrevTable {
+fn parseAbbrevTable(st: *ElfStackTrace) !AbbrevTable {
const in_file = &st.self_exe_file;
var in_file_stream = io.FileInStream.init(in_file);
const in_stream = &in_file_stream.stream;
@@ -712,7 +712,7 @@ fn parseAbbrevTable(st: &ElfStackTrace) !AbbrevTable {
/// Gets an already existing AbbrevTable given the abbrev_offset, or if not found,
/// seeks in the stream and parses it.
-fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) !&const AbbrevTable {
+fn getAbbrevTable(st: *ElfStackTrace, abbrev_offset: u64) !*const AbbrevTable {
for (st.abbrev_table_list.toSlice()) |*header| {
if (header.offset == abbrev_offset) {
return &header.table;
@@ -726,14 +726,14 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) !&const AbbrevTable {
return &st.abbrev_table_list.items[st.abbrev_table_list.len - 1].table;
}
-fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) ?&const AbbrevTableEntry {
+fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*const AbbrevTableEntry {
for (abbrev_table.toSliceConst()) |*table_entry| {
if (table_entry.abbrev_code == abbrev_code) return table_entry;
}
return null;
}
-fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) !Die {
+fn parseDie(st: *ElfStackTrace, abbrev_table: *const AbbrevTable, is_64: bool) !Die {
const in_file = &st.self_exe_file;
var in_file_stream = io.FileInStream.init(in_file);
const in_stream = &in_file_stream.stream;
@@ -755,7 +755,7 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) !
return result;
}
-fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, target_address: usize) !LineInfo {
+fn getLineNumberInfo(st: *ElfStackTrace, compile_unit: *const CompileUnit, target_address: usize) !LineInfo {
const compile_unit_cwd = try compile_unit.die.getAttrString(st, DW.AT_comp_dir);
const in_file = &st.self_exe_file;
@@ -934,7 +934,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
return error.MissingDebugInfo;
}
-fn scanAllCompileUnits(st: &ElfStackTrace) !void {
+fn scanAllCompileUnits(st: *ElfStackTrace) !void {
const debug_info_end = st.debug_info.offset + st.debug_info.size;
var this_unit_offset = st.debug_info.offset;
var cu_index: usize = 0;
@@ -1005,7 +1005,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
}
}
-fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit {
+fn findCompileUnit(st: *ElfStackTrace, target_address: u64) !*const CompileUnit {
var in_file_stream = io.FileInStream.init(&st.self_exe_file);
const in_stream = &in_file_stream.stream;
for (st.compile_unit_list.toSlice()) |*compile_unit| {
@@ -1039,7 +1039,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
return error.MissingDebugInfo;
}
-fn readInitialLength(comptime E: type, in_stream: &io.InStream(E), is_64: &bool) !u64 {
+fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool) !u64 {
const first_32_bits = try in_stream.readIntLe(u32);
is_64.* = (first_32_bits == 0xffffffff);
if (is_64.*) {
@@ -1096,10 +1096,10 @@ var global_fixed_allocator = std.heap.FixedBufferAllocator.init(global_allocator
var global_allocator_mem: [100 * 1024]u8 = undefined;
// TODO make thread safe
-var debug_info_allocator: ?&mem.Allocator = null;
+var debug_info_allocator: ?*mem.Allocator = null;
var debug_info_direct_allocator: std.heap.DirectAllocator = undefined;
var debug_info_arena_allocator: std.heap.ArenaAllocator = undefined;
-fn getDebugInfoAllocator() &mem.Allocator {
+fn getDebugInfoAllocator() *mem.Allocator {
if (debug_info_allocator) |a| return a;
debug_info_direct_allocator = std.heap.DirectAllocator.init();
diff --git a/std/elf.zig b/std/elf.zig
index 29b9473f98..50e97ab271 100644
--- a/std/elf.zig
+++ b/std/elf.zig
@@ -338,7 +338,7 @@ pub const SectionHeader = struct {
};
pub const Elf = struct {
- in_file: &os.File,
+ in_file: *os.File,
auto_close_stream: bool,
is_64: bool,
endian: builtin.Endian,
@@ -348,20 +348,20 @@ pub const Elf = struct {
program_header_offset: u64,
section_header_offset: u64,
string_section_index: u64,
- string_section: &SectionHeader,
+ string_section: *SectionHeader,
section_headers: []SectionHeader,
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
prealloc_file: os.File,
/// Call close when done.
- pub fn openPath(elf: &Elf, allocator: &mem.Allocator, path: []const u8) !void {
+ pub fn openPath(elf: *Elf, allocator: *mem.Allocator, path: []const u8) !void {
try elf.prealloc_file.open(path);
- try elf.openFile(allocator, &elf.prealloc_file);
+ try elf.openFile(allocator, *elf.prealloc_file);
elf.auto_close_stream = true;
}
/// Call close when done.
- pub fn openFile(elf: &Elf, allocator: &mem.Allocator, file: &os.File) !void {
+ pub fn openFile(elf: *Elf, allocator: *mem.Allocator, file: *os.File) !void {
elf.allocator = allocator;
elf.in_file = file;
elf.auto_close_stream = false;
@@ -503,13 +503,13 @@ pub const Elf = struct {
}
}
- pub fn close(elf: &Elf) void {
+ pub fn close(elf: *Elf) void {
elf.allocator.free(elf.section_headers);
if (elf.auto_close_stream) elf.in_file.close();
}
- pub fn findSection(elf: &Elf, name: []const u8) !?&SectionHeader {
+ pub fn findSection(elf: *Elf, name: []const u8) !?*SectionHeader {
var file_stream = io.FileInStream.init(elf.in_file);
const in = &file_stream.stream;
@@ -533,7 +533,7 @@ pub const Elf = struct {
return null;
}
- pub fn seekToSection(elf: &Elf, elf_section: &SectionHeader) !void {
+ pub fn seekToSection(elf: *Elf, elf_section: *SectionHeader) !void {
try elf.in_file.seekTo(elf_section.offset);
}
};
diff --git a/std/event.zig b/std/event.zig
index 4604eb8d02..89ab816bb6 100644
--- a/std/event.zig
+++ b/std/event.zig
@@ -6,9 +6,9 @@ const mem = std.mem;
const posix = std.os.posix;
pub const TcpServer = struct {
- handleRequestFn: async<&mem.Allocator> fn (&TcpServer, &const std.net.Address, &const std.os.File) void,
+ handleRequestFn: async<*mem.Allocator> fn (*TcpServer, *const std.net.Address, *const std.os.File) void,
- loop: &Loop,
+ loop: *Loop,
sockfd: i32,
accept_coro: ?promise,
listen_address: std.net.Address,
@@ -17,7 +17,7 @@ pub const TcpServer = struct {
const PromiseNode = std.LinkedList(promise).Node;
- pub fn init(loop: &Loop) !TcpServer {
+ pub fn init(loop: *Loop) !TcpServer {
const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
errdefer std.os.close(sockfd);
@@ -32,7 +32,7 @@ pub const TcpServer = struct {
};
}
- pub fn listen(self: &TcpServer, address: &const std.net.Address, handleRequestFn: async<&mem.Allocator> fn (&TcpServer, &const std.net.Address, &const std.os.File) void) !void {
+ pub fn listen(self: *TcpServer, address: *const std.net.Address, handleRequestFn: async<*mem.Allocator> fn (*TcpServer, *const std.net.Address, *const std.os.File) void) !void {
self.handleRequestFn = handleRequestFn;
try std.os.posixBind(self.sockfd, &address.os_addr);
@@ -46,13 +46,13 @@ pub const TcpServer = struct {
errdefer self.loop.removeFd(self.sockfd);
}
- pub fn deinit(self: &TcpServer) void {
+ pub fn deinit(self: *TcpServer) void {
self.loop.removeFd(self.sockfd);
if (self.accept_coro) |accept_coro| cancel accept_coro;
std.os.close(self.sockfd);
}
- pub async fn handler(self: &TcpServer) void {
+ pub async fn handler(self: *TcpServer) void {
while (true) {
var accepted_addr: std.net.Address = undefined;
if (std.os.posixAccept(self.sockfd, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
@@ -92,11 +92,11 @@ pub const TcpServer = struct {
};
pub const Loop = struct {
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
epollfd: i32,
keep_running: bool,
- fn init(allocator: &mem.Allocator) !Loop {
+ fn init(allocator: *mem.Allocator) !Loop {
const epollfd = try std.os.linuxEpollCreate(std.os.linux.EPOLL_CLOEXEC);
return Loop{
.keep_running = true,
@@ -105,7 +105,7 @@ pub const Loop = struct {
};
}
- pub fn addFd(self: &Loop, fd: i32, prom: promise) !void {
+ pub fn addFd(self: *Loop, fd: i32, prom: promise) !void {
var ev = std.os.linux.epoll_event{
.events = std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT | std.os.linux.EPOLLET,
.data = std.os.linux.epoll_data{ .ptr = @ptrToInt(prom) },
@@ -113,23 +113,23 @@ pub const Loop = struct {
try std.os.linuxEpollCtl(self.epollfd, std.os.linux.EPOLL_CTL_ADD, fd, &ev);
}
- pub fn removeFd(self: &Loop, fd: i32) void {
+ pub fn removeFd(self: *Loop, fd: i32) void {
std.os.linuxEpollCtl(self.epollfd, std.os.linux.EPOLL_CTL_DEL, fd, undefined) catch {};
}
- async fn waitFd(self: &Loop, fd: i32) !void {
+ async fn waitFd(self: *Loop, fd: i32) !void {
defer self.removeFd(fd);
suspend |p| {
try self.addFd(fd, p);
}
}
- pub fn stop(self: &Loop) void {
+ pub fn stop(self: *Loop) void {
// TODO make atomic
self.keep_running = false;
// TODO activate an fd in the epoll set
}
- pub fn run(self: &Loop) void {
+ pub fn run(self: *Loop) void {
while (self.keep_running) {
var events: [16]std.os.linux.epoll_event = undefined;
const count = std.os.linuxEpollWait(self.epollfd, events[0..], -1);
@@ -141,7 +141,7 @@ pub const Loop = struct {
}
};
-pub async fn connect(loop: &Loop, _address: &const std.net.Address) !std.os.File {
+pub async fn connect(loop: *Loop, _address: *const std.net.Address) !std.os.File {
var address = _address.*; // TODO https://github.com/ziglang/zig/issues/733
const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
@@ -163,7 +163,7 @@ test "listen on a port, send bytes, receive bytes" {
tcp_server: TcpServer,
const Self = this;
- async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address, _socket: &const std.os.File) void {
+ async<*mem.Allocator> fn handler(tcp_server: *TcpServer, _addr: *const std.net.Address, _socket: *const std.os.File) void {
const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
defer socket.close();
@@ -177,7 +177,7 @@ test "listen on a port, send bytes, receive bytes" {
cancel p;
}
}
- async fn errorableHandler(self: &Self, _addr: &const std.net.Address, _socket: &const std.os.File) !void {
+ async fn errorableHandler(self: *Self, _addr: *const std.net.Address, _socket: *const std.os.File) !void {
const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/733
var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
@@ -199,7 +199,7 @@ test "listen on a port, send bytes, receive bytes" {
defer cancel p;
loop.run();
}
-async fn doAsyncTest(loop: &Loop, address: &const std.net.Address) void {
+async fn doAsyncTest(loop: *Loop, address: *const std.net.Address) void {
errdefer @panic("test failure");
var socket_file = try await try async event.connect(loop, address);
diff --git a/std/fmt/errol/index.zig b/std/fmt/errol/index.zig
index 65e8d448a8..933958ac18 100644
--- a/std/fmt/errol/index.zig
+++ b/std/fmt/errol/index.zig
@@ -21,7 +21,7 @@ pub const RoundMode = enum {
/// Round a FloatDecimal as returned by errol3 to the specified fractional precision.
/// All digits after the specified precision should be considered invalid.
-pub fn roundToPrecision(float_decimal: &FloatDecimal, precision: usize, mode: RoundMode) void {
+pub fn roundToPrecision(float_decimal: *FloatDecimal, precision: usize, mode: RoundMode) void {
// The round digit refers to the index which we should look at to determine
// whether we need to round to match the specified precision.
var round_digit: usize = 0;
@@ -59,7 +59,7 @@ pub fn roundToPrecision(float_decimal: &FloatDecimal, precision: usize, mode: Ro
float_decimal.exp += 1;
// Re-size the buffer to use the reserved leading byte.
- const one_before = @intToPtr(&u8, @ptrToInt(&float_decimal.digits[0]) - 1);
+ const one_before = @intToPtr(*u8, @ptrToInt(&float_decimal.digits[0]) - 1);
float_decimal.digits = one_before[0 .. float_decimal.digits.len + 1];
float_decimal.digits[0] = '1';
return;
@@ -217,7 +217,7 @@ fn tableLowerBound(k: u64) usize {
/// @in: The HP number.
/// @val: The double.
/// &returns: The HP number.
-fn hpProd(in: &const HP, val: f64) HP {
+fn hpProd(in: *const HP, val: f64) HP {
var hi: f64 = undefined;
var lo: f64 = undefined;
split(in.val, &hi, &lo);
@@ -239,7 +239,7 @@ fn hpProd(in: &const HP, val: f64) HP {
/// @val: The double.
/// @hi: The high bits.
/// @lo: The low bits.
-fn split(val: f64, hi: &f64, lo: &f64) void {
+fn split(val: f64, hi: *f64, lo: *f64) void {
hi.* = gethi(val);
lo.* = val - hi.*;
}
@@ -252,7 +252,7 @@ fn gethi(in: f64) f64 {
/// Normalize the number by factoring in the error.
/// @hp: The float pair.
-fn hpNormalize(hp: &HP) void {
+fn hpNormalize(hp: *HP) void {
// Required to avoid segfaults causing buffer overrun during errol3 digit output termination.
@setFloatMode(this, @import("builtin").FloatMode.Strict);
@@ -264,7 +264,7 @@ fn hpNormalize(hp: &HP) void {
/// Divide the high-precision number by ten.
/// @hp: The high-precision number
-fn hpDiv10(hp: &HP) void {
+fn hpDiv10(hp: *HP) void {
var val = hp.val;
hp.val /= 10.0;
@@ -280,7 +280,7 @@ fn hpDiv10(hp: &HP) void {
/// Multiply the high-precision number by ten.
/// @hp: The high-precision number
-fn hpMul10(hp: &HP) void {
+fn hpMul10(hp: *HP) void {
const val = hp.val;
hp.val *= 10.0;
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 0ffbc59895..b522d9d37d 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -679,7 +679,7 @@ const FormatIntBuf = struct {
out_buf: []u8,
index: usize,
};
-fn formatIntCallback(context: &FormatIntBuf, bytes: []const u8) (error{}!void) {
+fn formatIntCallback(context: *FormatIntBuf, bytes: []const u8) (error{}!void) {
mem.copy(u8, context.out_buf[context.index..], bytes);
context.index += bytes.len;
}
@@ -751,7 +751,7 @@ const BufPrintContext = struct {
remaining: []u8,
};
-fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) !void {
+fn bufPrintWrite(context: *BufPrintContext, bytes: []const u8) !void {
if (context.remaining.len < bytes.len) return error.BufferTooSmall;
mem.copy(u8, context.remaining, bytes);
context.remaining = context.remaining[bytes.len..];
@@ -763,14 +763,14 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) ![]u8 {
return buf[0 .. buf.len - context.remaining.len];
}
-pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ...) ![]u8 {
+pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: ...) ![]u8 {
var size: usize = 0;
format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};
const buf = try allocator.alloc(u8, size);
return bufPrint(buf, fmt, args);
}
-fn countSize(size: &usize, bytes: []const u8) (error{}!void) {
+fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
size.* += bytes.len;
}
diff --git a/std/hash/adler.zig b/std/hash/adler.zig
index 12dab1457c..9c5966f89b 100644
--- a/std/hash/adler.zig
+++ b/std/hash/adler.zig
@@ -18,7 +18,7 @@ pub const Adler32 = struct {
// This fast variant is taken from zlib. It reduces the required modulos and unrolls longer
// buffer inputs and should be much quicker.
- pub fn update(self: &Adler32, input: []const u8) void {
+ pub fn update(self: *Adler32, input: []const u8) void {
var s1 = self.adler & 0xffff;
var s2 = (self.adler >> 16) & 0xffff;
@@ -77,7 +77,7 @@ pub const Adler32 = struct {
self.adler = s1 | (s2 << 16);
}
- pub fn final(self: &Adler32) u32 {
+ pub fn final(self: *Adler32) u32 {
return self.adler;
}
diff --git a/std/hash/crc.zig b/std/hash/crc.zig
index 45bcb70e8b..ec831cdc2e 100644
--- a/std/hash/crc.zig
+++ b/std/hash/crc.zig
@@ -58,7 +58,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
return Self{ .crc = 0xffffffff };
}
- pub fn update(self: &Self, input: []const u8) void {
+ pub fn update(self: *Self, input: []const u8) void {
var i: usize = 0;
while (i + 8 <= input.len) : (i += 8) {
const p = input[i .. i + 8];
@@ -86,7 +86,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
}
}
- pub fn final(self: &Self) u32 {
+ pub fn final(self: *Self) u32 {
return ~self.crc;
}
@@ -143,14 +143,14 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
return Self{ .crc = 0xffffffff };
}
- pub fn update(self: &Self, input: []const u8) void {
+ pub fn update(self: *Self, input: []const u8) void {
for (input) |b| {
self.crc = lookup_table[@truncate(u4, self.crc ^ (b >> 0))] ^ (self.crc >> 4);
self.crc = lookup_table[@truncate(u4, self.crc ^ (b >> 4))] ^ (self.crc >> 4);
}
}
- pub fn final(self: &Self) u32 {
+ pub fn final(self: *Self) u32 {
return ~self.crc;
}
diff --git a/std/hash/fnv.zig b/std/hash/fnv.zig
index c2439e0ebc..447c996772 100644
--- a/std/hash/fnv.zig
+++ b/std/hash/fnv.zig
@@ -21,14 +21,14 @@ fn Fnv1a(comptime T: type, comptime prime: T, comptime offset: T) type {
return Self{ .value = offset };
}
- pub fn update(self: &Self, input: []const u8) void {
+ pub fn update(self: *Self, input: []const u8) void {
for (input) |b| {
self.value ^= b;
self.value *%= prime;
}
}
- pub fn final(self: &Self) T {
+ pub fn final(self: *Self) T {
return self.value;
}
diff --git a/std/hash/siphash.zig b/std/hash/siphash.zig
index 750e23d4c8..8a90308a46 100644
--- a/std/hash/siphash.zig
+++ b/std/hash/siphash.zig
@@ -63,7 +63,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
return d;
}
- pub fn update(d: &Self, b: []const u8) void {
+ pub fn update(d: *Self, b: []const u8) void {
var off: usize = 0;
// Partial from previous.
@@ -85,7 +85,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
d.msg_len +%= @truncate(u8, b.len);
}
- pub fn final(d: &Self) T {
+ pub fn final(d: *Self) T {
// Padding
mem.set(u8, d.buf[d.buf_len..], 0);
d.buf[7] = d.msg_len;
@@ -118,7 +118,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
return (u128(b2) << 64) | b1;
}
- fn round(d: &Self, b: []const u8) void {
+ fn round(d: *Self, b: []const u8) void {
debug.assert(b.len == 8);
const m = mem.readInt(b[0..], u64, Endian.Little);
@@ -132,7 +132,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
d.v0 ^= m;
}
- fn sipRound(d: &Self) void {
+ fn sipRound(d: *Self) void {
d.v0 +%= d.v1;
d.v1 = math.rotl(u64, d.v1, u64(13));
d.v1 ^= d.v0;
diff --git a/std/hash_map.zig b/std/hash_map.zig
index f51b9c66ba..a323cdc197 100644
--- a/std/hash_map.zig
+++ b/std/hash_map.zig
@@ -14,7 +14,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
entries: []Entry,
size: usize,
max_distance_from_start_index: usize,
- allocator: &Allocator,
+ allocator: *Allocator,
// this is used to detect bugs where a hashtable is edited while an iterator is running.
modification_count: debug_u32,
@@ -28,7 +28,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
};
pub const Iterator = struct {
- hm: &const Self,
+ hm: *const Self,
// how many items have we returned
count: usize,
// iterator through the entry array
@@ -36,7 +36,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
// used to detect concurrent modification
initial_modification_count: debug_u32,
- pub fn next(it: &Iterator) ?&Entry {
+ pub fn next(it: *Iterator) ?*Entry {
if (want_modification_safety) {
assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification
}
@@ -53,7 +53,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
}
// Reset the iterator to the initial index
- pub fn reset(it: &Iterator) void {
+ pub fn reset(it: *Iterator) void {
it.count = 0;
it.index = 0;
// Resetting the modification count too
@@ -61,7 +61,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
}
};
- pub fn init(allocator: &Allocator) Self {
+ pub fn init(allocator: *Allocator) Self {
return Self{
.entries = []Entry{},
.allocator = allocator,
@@ -71,11 +71,11 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
};
}
- pub fn deinit(hm: &const Self) void {
+ pub fn deinit(hm: *const Self) void {
hm.allocator.free(hm.entries);
}
- pub fn clear(hm: &Self) void {
+ pub fn clear(hm: *Self) void {
for (hm.entries) |*entry| {
entry.used = false;
}
@@ -84,12 +84,12 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
hm.incrementModificationCount();
}
- pub fn count(hm: &const Self) usize {
+ pub fn count(hm: *const Self) usize {
return hm.size;
}
/// Returns the value that was already there.
- pub fn put(hm: &Self, key: K, value: &const V) !?V {
+ pub fn put(hm: *Self, key: K, value: *const V) !?V {
if (hm.entries.len == 0) {
try hm.initCapacity(16);
}
@@ -111,18 +111,18 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
return hm.internalPut(key, value);
}
- pub fn get(hm: &const Self, key: K) ?&Entry {
+ pub fn get(hm: *const Self, key: K) ?*Entry {
if (hm.entries.len == 0) {
return null;
}
return hm.internalGet(key);
}
- pub fn contains(hm: &const Self, key: K) bool {
+ pub fn contains(hm: *const Self, key: K) bool {
return hm.get(key) != null;
}
- pub fn remove(hm: &Self, key: K) ?&Entry {
+ pub fn remove(hm: *Self, key: K) ?*Entry {
if (hm.entries.len == 0) return null;
hm.incrementModificationCount();
const start_index = hm.keyToIndex(key);
@@ -154,7 +154,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
return null;
}
- pub fn iterator(hm: &const Self) Iterator {
+ pub fn iterator(hm: *const Self) Iterator {
return Iterator{
.hm = hm,
.count = 0,
@@ -163,7 +163,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
};
}
- fn initCapacity(hm: &Self, capacity: usize) !void {
+ fn initCapacity(hm: *Self, capacity: usize) !void {
hm.entries = try hm.allocator.alloc(Entry, capacity);
hm.size = 0;
hm.max_distance_from_start_index = 0;
@@ -172,14 +172,14 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
}
}
- fn incrementModificationCount(hm: &Self) void {
+ fn incrementModificationCount(hm: *Self) void {
if (want_modification_safety) {
hm.modification_count +%= 1;
}
}
/// Returns the value that was already there.
- fn internalPut(hm: &Self, orig_key: K, orig_value: &const V) ?V {
+ fn internalPut(hm: *Self, orig_key: K, orig_value: *const V) ?V {
var key = orig_key;
var value = orig_value.*;
const start_index = hm.keyToIndex(key);
@@ -231,7 +231,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
unreachable; // put into a full map
}
- fn internalGet(hm: &const Self, key: K) ?&Entry {
+ fn internalGet(hm: *const Self, key: K) ?*Entry {
const start_index = hm.keyToIndex(key);
{
var roll_over: usize = 0;
@@ -246,7 +246,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
return null;
}
- fn keyToIndex(hm: &const Self, key: K) usize {
+ fn keyToIndex(hm: *const Self, key: K) usize {
return usize(hash(key)) % hm.entries.len;
}
};
diff --git a/std/heap.zig b/std/heap.zig
index 8d4938a7c3..81d6f25282 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -16,15 +16,15 @@ var c_allocator_state = Allocator{
.freeFn = cFree,
};
-fn cAlloc(self: &Allocator, n: usize, alignment: u29) ![]u8 {
+fn cAlloc(self: *Allocator, n: usize, alignment: u29) ![]u8 {
assert(alignment <= @alignOf(c_longdouble));
- return if (c.malloc(n)) |buf| @ptrCast(&u8, buf)[0..n] else error.OutOfMemory;
+ return if (c.malloc(n)) |buf| @ptrCast(*u8, buf)[0..n] else error.OutOfMemory;
}
-fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
- const old_ptr = @ptrCast(&c_void, old_mem.ptr);
+fn cRealloc(self: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ const old_ptr = @ptrCast(*c_void, old_mem.ptr);
if (c.realloc(old_ptr, new_size)) |buf| {
- return @ptrCast(&u8, buf)[0..new_size];
+ return @ptrCast(*u8, buf)[0..new_size];
} else if (new_size <= old_mem.len) {
return old_mem[0..new_size];
} else {
@@ -32,8 +32,8 @@ fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![
}
}
-fn cFree(self: &Allocator, old_mem: []u8) void {
- const old_ptr = @ptrCast(&c_void, old_mem.ptr);
+fn cFree(self: *Allocator, old_mem: []u8) void {
+ const old_ptr = @ptrCast(*c_void, old_mem.ptr);
c.free(old_ptr);
}
@@ -55,7 +55,7 @@ pub const DirectAllocator = struct {
};
}
- pub fn deinit(self: &DirectAllocator) void {
+ pub fn deinit(self: *DirectAllocator) void {
switch (builtin.os) {
Os.windows => if (self.heap_handle) |heap_handle| {
_ = os.windows.HeapDestroy(heap_handle);
@@ -64,7 +64,7 @@ pub const DirectAllocator = struct {
}
}
- fn alloc(allocator: &Allocator, n: usize, alignment: u29) ![]u8 {
+ fn alloc(allocator: *Allocator, n: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
switch (builtin.os) {
@@ -74,7 +74,7 @@ pub const DirectAllocator = struct {
const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0);
if (addr == p.MAP_FAILED) return error.OutOfMemory;
- if (alloc_size == n) return @intToPtr(&u8, addr)[0..n];
+ if (alloc_size == n) return @intToPtr(*u8, addr)[0..n];
var aligned_addr = addr & ~usize(alignment - 1);
aligned_addr += alignment;
@@ -93,7 +93,7 @@ pub const DirectAllocator = struct {
//It is impossible that there is an unoccupied page at the top of our
// mmap.
- return @intToPtr(&u8, aligned_addr)[0..n];
+ return @intToPtr(*u8, aligned_addr)[0..n];
},
Os.windows => {
const amt = n + alignment + @sizeOf(usize);
@@ -108,14 +108,14 @@ pub const DirectAllocator = struct {
const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
const adjusted_addr = root_addr + march_forward_bytes;
const record_addr = adjusted_addr + n;
- @intToPtr(&align(1) usize, record_addr).* = root_addr;
- return @intToPtr(&u8, adjusted_addr)[0..n];
+ @intToPtr(*align(1) usize, record_addr).* = root_addr;
+ return @intToPtr(*u8, adjusted_addr)[0..n];
},
else => @compileError("Unsupported OS"),
}
}
- fn realloc(allocator: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ fn realloc(allocator: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
switch (builtin.os) {
@@ -139,13 +139,13 @@ pub const DirectAllocator = struct {
Os.windows => {
const old_adjusted_addr = @ptrToInt(old_mem.ptr);
const old_record_addr = old_adjusted_addr + old_mem.len;
- const root_addr = @intToPtr(&align(1) usize, old_record_addr).*;
+ const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;
const old_ptr = @intToPtr(os.windows.LPVOID, root_addr);
const amt = new_size + alignment + @sizeOf(usize);
const new_ptr = os.windows.HeapReAlloc(??self.heap_handle, 0, old_ptr, amt) ?? blk: {
if (new_size > old_mem.len) return error.OutOfMemory;
const new_record_addr = old_record_addr - new_size + old_mem.len;
- @intToPtr(&align(1) usize, new_record_addr).* = root_addr;
+ @intToPtr(*align(1) usize, new_record_addr).* = root_addr;
return old_mem[0..new_size];
};
const offset = old_adjusted_addr - root_addr;
@@ -153,14 +153,14 @@ pub const DirectAllocator = struct {
const new_adjusted_addr = new_root_addr + offset;
assert(new_adjusted_addr % alignment == 0);
const new_record_addr = new_adjusted_addr + new_size;
- @intToPtr(&align(1) usize, new_record_addr).* = new_root_addr;
- return @intToPtr(&u8, new_adjusted_addr)[0..new_size];
+ @intToPtr(*align(1) usize, new_record_addr).* = new_root_addr;
+ return @intToPtr(*u8, new_adjusted_addr)[0..new_size];
},
else => @compileError("Unsupported OS"),
}
}
- fn free(allocator: &Allocator, bytes: []u8) void {
+ fn free(allocator: *Allocator, bytes: []u8) void {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
switch (builtin.os) {
@@ -169,7 +169,7 @@ pub const DirectAllocator = struct {
},
Os.windows => {
const record_addr = @ptrToInt(bytes.ptr) + bytes.len;
- const root_addr = @intToPtr(&align(1) usize, record_addr).*;
+ const root_addr = @intToPtr(*align(1) usize, record_addr).*;
const ptr = @intToPtr(os.windows.LPVOID, root_addr);
_ = os.windows.HeapFree(??self.heap_handle, 0, ptr);
},
@@ -183,13 +183,13 @@ pub const DirectAllocator = struct {
pub const ArenaAllocator = struct {
pub allocator: Allocator,
- child_allocator: &Allocator,
+ child_allocator: *Allocator,
buffer_list: std.LinkedList([]u8),
end_index: usize,
const BufNode = std.LinkedList([]u8).Node;
- pub fn init(child_allocator: &Allocator) ArenaAllocator {
+ pub fn init(child_allocator: *Allocator) ArenaAllocator {
return ArenaAllocator{
.allocator = Allocator{
.allocFn = alloc,
@@ -202,7 +202,7 @@ pub const ArenaAllocator = struct {
};
}
- pub fn deinit(self: &ArenaAllocator) void {
+ pub fn deinit(self: *ArenaAllocator) void {
var it = self.buffer_list.first;
while (it) |node| {
// this has to occur before the free because the free frees node
@@ -212,7 +212,7 @@ pub const ArenaAllocator = struct {
}
}
- fn createNode(self: &ArenaAllocator, prev_len: usize, minimum_size: usize) !&BufNode {
+ fn createNode(self: *ArenaAllocator, prev_len: usize, minimum_size: usize) !*BufNode {
const actual_min_size = minimum_size + @sizeOf(BufNode);
var len = prev_len;
while (true) {
@@ -233,7 +233,7 @@ pub const ArenaAllocator = struct {
return buf_node;
}
- fn alloc(allocator: &Allocator, n: usize, alignment: u29) ![]u8 {
+ fn alloc(allocator: *Allocator, n: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(ArenaAllocator, "allocator", allocator);
var cur_node = if (self.buffer_list.last) |last_node| last_node else try self.createNode(0, n + alignment);
@@ -254,7 +254,7 @@ pub const ArenaAllocator = struct {
}
}
- fn realloc(allocator: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ fn realloc(allocator: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
if (new_size <= old_mem.len) {
return old_mem[0..new_size];
} else {
@@ -264,7 +264,7 @@ pub const ArenaAllocator = struct {
}
}
- fn free(allocator: &Allocator, bytes: []u8) void {}
+ fn free(allocator: *Allocator, bytes: []u8) void {}
};
pub const FixedBufferAllocator = struct {
@@ -284,7 +284,7 @@ pub const FixedBufferAllocator = struct {
};
}
- fn alloc(allocator: &Allocator, n: usize, alignment: u29) ![]u8 {
+ fn alloc(allocator: *Allocator, n: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator);
const addr = @ptrToInt(self.buffer.ptr) + self.end_index;
const rem = @rem(addr, alignment);
@@ -300,7 +300,7 @@ pub const FixedBufferAllocator = struct {
return result;
}
- fn realloc(allocator: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ fn realloc(allocator: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
if (new_size <= old_mem.len) {
return old_mem[0..new_size];
} else {
@@ -310,7 +310,7 @@ pub const FixedBufferAllocator = struct {
}
}
- fn free(allocator: &Allocator, bytes: []u8) void {}
+ fn free(allocator: *Allocator, bytes: []u8) void {}
};
/// lock free
@@ -331,7 +331,7 @@ pub const ThreadSafeFixedBufferAllocator = struct {
};
}
- fn alloc(allocator: &Allocator, n: usize, alignment: u29) ![]u8 {
+ fn alloc(allocator: *Allocator, n: usize, alignment: u29) ![]u8 {
const self = @fieldParentPtr(ThreadSafeFixedBufferAllocator, "allocator", allocator);
var end_index = @atomicLoad(usize, &self.end_index, builtin.AtomicOrder.SeqCst);
while (true) {
@@ -343,11 +343,11 @@ pub const ThreadSafeFixedBufferAllocator = struct {
if (new_end_index > self.buffer.len) {
return error.OutOfMemory;
}
- end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) ?? return self.buffer[adjusted_index..new_end_index];
+ end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst,) ?? return self.buffer[adjusted_index..new_end_index];
}
}
- fn realloc(allocator: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
+ fn realloc(allocator: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
if (new_size <= old_mem.len) {
return old_mem[0..new_size];
} else {
@@ -357,7 +357,7 @@ pub const ThreadSafeFixedBufferAllocator = struct {
}
}
- fn free(allocator: &Allocator, bytes: []u8) void {}
+ fn free(allocator: *Allocator, bytes: []u8) void {}
};
test "c_allocator" {
@@ -403,8 +403,8 @@ test "ThreadSafeFixedBufferAllocator" {
try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator);
}
-fn testAllocator(allocator: &mem.Allocator) !void {
- var slice = try allocator.alloc(&i32, 100);
+fn testAllocator(allocator: *mem.Allocator) !void {
+ var slice = try allocator.alloc(*i32, 100);
for (slice) |*item, i| {
item.* = try allocator.create(i32);
@@ -415,15 +415,15 @@ fn testAllocator(allocator: &mem.Allocator) !void {
allocator.destroy(item);
}
- slice = try allocator.realloc(&i32, slice, 20000);
- slice = try allocator.realloc(&i32, slice, 50);
- slice = try allocator.realloc(&i32, slice, 25);
- slice = try allocator.realloc(&i32, slice, 10);
+ slice = try allocator.realloc(*i32, slice, 20000);
+ slice = try allocator.realloc(*i32, slice, 50);
+ slice = try allocator.realloc(*i32, slice, 25);
+ slice = try allocator.realloc(*i32, slice, 10);
allocator.free(slice);
}
-fn testAllocatorLargeAlignment(allocator: &mem.Allocator) mem.Allocator.Error!void {
+fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
//Maybe a platform's page_size is actually the same as or
// very near usize?
if (os.page_size << 2 > @maxValue(usize)) return;
diff --git a/std/io.zig b/std/io.zig
index 39d319159e..e20a284e4e 100644
--- a/std/io.zig
+++ b/std/io.zig
@@ -34,20 +34,20 @@ pub fn getStdIn() GetStdIoErrs!File {
/// Implementation of InStream trait for File
pub const FileInStream = struct {
- file: &File,
+ file: *File,
stream: Stream,
pub const Error = @typeOf(File.read).ReturnType.ErrorSet;
pub const Stream = InStream(Error);
- pub fn init(file: &File) FileInStream {
+ pub fn init(file: *File) FileInStream {
return FileInStream{
.file = file,
.stream = Stream{ .readFn = readFn },
};
}
- fn readFn(in_stream: &Stream, buffer: []u8) Error!usize {
+ fn readFn(in_stream: *Stream, buffer: []u8) Error!usize {
const self = @fieldParentPtr(FileInStream, "stream", in_stream);
return self.file.read(buffer);
}
@@ -55,20 +55,20 @@ pub const FileInStream = struct {
/// Implementation of OutStream trait for File
pub const FileOutStream = struct {
- file: &File,
+ file: *File,
stream: Stream,
pub const Error = File.WriteError;
pub const Stream = OutStream(Error);
- pub fn init(file: &File) FileOutStream {
+ pub fn init(file: *File) FileOutStream {
return FileOutStream{
.file = file,
.stream = Stream{ .writeFn = writeFn },
};
}
- fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
+ fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
const self = @fieldParentPtr(FileOutStream, "stream", out_stream);
return self.file.write(bytes);
}
@@ -82,12 +82,12 @@ pub fn InStream(comptime ReadError: type) type {
/// Return the number of bytes read. If the number read is smaller than buf.len, it
/// means the stream reached the end. Reaching the end of a stream is not an error
/// condition.
- readFn: fn (self: &Self, buffer: []u8) Error!usize,
+ readFn: fn (self: *Self, buffer: []u8) Error!usize,
/// Replaces `buffer` contents by reading from the stream until it is finished.
/// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and
/// the contents read from the stream are lost.
- pub fn readAllBuffer(self: &Self, buffer: &Buffer, max_size: usize) !void {
+ pub fn readAllBuffer(self: *Self, buffer: *Buffer, max_size: usize) !void {
try buffer.resize(0);
var actual_buf_len: usize = 0;
@@ -111,7 +111,7 @@ pub fn InStream(comptime ReadError: type) type {
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
/// Caller owns returned memory.
/// If this function returns an error, the contents from the stream read so far are lost.
- pub fn readAllAlloc(self: &Self, allocator: &mem.Allocator, max_size: usize) ![]u8 {
+ pub fn readAllAlloc(self: *Self, allocator: *mem.Allocator, max_size: usize) ![]u8 {
var buf = Buffer.initNull(allocator);
defer buf.deinit();
@@ -123,7 +123,7 @@ pub fn InStream(comptime ReadError: type) type {
/// Does not include the delimiter in the result.
/// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents
/// read from the stream so far are lost.
- pub fn readUntilDelimiterBuffer(self: &Self, buffer: &Buffer, delimiter: u8, max_size: usize) !void {
+ pub fn readUntilDelimiterBuffer(self: *Self, buffer: *Buffer, delimiter: u8, max_size: usize) !void {
try buffer.resize(0);
while (true) {
@@ -145,7 +145,7 @@ pub fn InStream(comptime ReadError: type) type {
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
/// Caller owns returned memory.
/// If this function returns an error, the contents from the stream read so far are lost.
- pub fn readUntilDelimiterAlloc(self: &Self, allocator: &mem.Allocator, delimiter: u8, max_size: usize) ![]u8 {
+ pub fn readUntilDelimiterAlloc(self: *Self, allocator: *mem.Allocator, delimiter: u8, max_size: usize) ![]u8 {
var buf = Buffer.initNull(allocator);
defer buf.deinit();
@@ -156,43 +156,43 @@ pub fn InStream(comptime ReadError: type) type {
/// Returns the number of bytes read. If the number read is smaller than buf.len, it
/// means the stream reached the end. Reaching the end of a stream is not an error
/// condition.
- pub fn read(self: &Self, buffer: []u8) !usize {
+ pub fn read(self: *Self, buffer: []u8) !usize {
return self.readFn(self, buffer);
}
/// Same as `read` but end of stream returns `error.EndOfStream`.
- pub fn readNoEof(self: &Self, buf: []u8) !void {
+ pub fn readNoEof(self: *Self, buf: []u8) !void {
const amt_read = try self.read(buf);
if (amt_read < buf.len) return error.EndOfStream;
}
/// Reads 1 byte from the stream or returns `error.EndOfStream`.
- pub fn readByte(self: &Self) !u8 {
+ pub fn readByte(self: *Self) !u8 {
var result: [1]u8 = undefined;
try self.readNoEof(result[0..]);
return result[0];
}
/// Same as `readByte` except the returned byte is signed.
- pub fn readByteSigned(self: &Self) !i8 {
+ pub fn readByteSigned(self: *Self) !i8 {
return @bitCast(i8, try self.readByte());
}
- pub fn readIntLe(self: &Self, comptime T: type) !T {
+ pub fn readIntLe(self: *Self, comptime T: type) !T {
return self.readInt(builtin.Endian.Little, T);
}
- pub fn readIntBe(self: &Self, comptime T: type) !T {
+ pub fn readIntBe(self: *Self, comptime T: type) !T {
return self.readInt(builtin.Endian.Big, T);
}
- pub fn readInt(self: &Self, endian: builtin.Endian, comptime T: type) !T {
+ pub fn readInt(self: *Self, endian: builtin.Endian, comptime T: type) !T {
var bytes: [@sizeOf(T)]u8 = undefined;
try self.readNoEof(bytes[0..]);
return mem.readInt(bytes, T, endian);
}
- pub fn readVarInt(self: &Self, endian: builtin.Endian, comptime T: type, size: usize) !T {
+ pub fn readVarInt(self: *Self, endian: builtin.Endian, comptime T: type, size: usize) !T {
assert(size <= @sizeOf(T));
assert(size <= 8);
var input_buf: [8]u8 = undefined;
@@ -208,22 +208,22 @@ pub fn OutStream(comptime WriteError: type) type {
const Self = this;
pub const Error = WriteError;
- writeFn: fn (self: &Self, bytes: []const u8) Error!void,
+ writeFn: fn (self: *Self, bytes: []const u8) Error!void,
- pub fn print(self: &Self, comptime format: []const u8, args: ...) !void {
+ pub fn print(self: *Self, comptime format: []const u8, args: ...) !void {
return std.fmt.format(self, Error, self.writeFn, format, args);
}
- pub fn write(self: &Self, bytes: []const u8) !void {
+ pub fn write(self: *Self, bytes: []const u8) !void {
return self.writeFn(self, bytes);
}
- pub fn writeByte(self: &Self, byte: u8) !void {
+ pub fn writeByte(self: *Self, byte: u8) !void {
const slice = (&byte)[0..1];
return self.writeFn(self, slice);
}
- pub fn writeByteNTimes(self: &Self, byte: u8, n: usize) !void {
+ pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) !void {
const slice = (&byte)[0..1];
var i: usize = 0;
while (i < n) : (i += 1) {
@@ -234,14 +234,14 @@ pub fn OutStream(comptime WriteError: type) type {
}
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
-pub fn writeFile(allocator: &mem.Allocator, path: []const u8, data: []const u8) !void {
+pub fn writeFile(allocator: *mem.Allocator, path: []const u8, data: []const u8) !void {
var file = try File.openWrite(allocator, path);
defer file.close();
try file.write(data);
}
/// On success, caller owns returned buffer.
-pub fn readFileAlloc(allocator: &mem.Allocator, path: []const u8) ![]u8 {
+pub fn readFileAlloc(allocator: *mem.Allocator, path: []const u8) ![]u8 {
var file = try File.openRead(allocator, path);
defer file.close();
@@ -265,13 +265,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
pub stream: Stream,
- unbuffered_in_stream: &Stream,
+ unbuffered_in_stream: *Stream,
buffer: [buffer_size]u8,
start_index: usize,
end_index: usize,
- pub fn init(unbuffered_in_stream: &Stream) Self {
+ pub fn init(unbuffered_in_stream: *Stream) Self {
return Self{
.unbuffered_in_stream = unbuffered_in_stream,
.buffer = undefined,
@@ -287,7 +287,7 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
};
}
- fn readFn(in_stream: &Stream, dest: []u8) !usize {
+ fn readFn(in_stream: *Stream, dest: []u8) !usize {
const self = @fieldParentPtr(Self, "stream", in_stream);
var dest_index: usize = 0;
@@ -338,12 +338,12 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
pub stream: Stream,
- unbuffered_out_stream: &Stream,
+ unbuffered_out_stream: *Stream,
buffer: [buffer_size]u8,
index: usize,
- pub fn init(unbuffered_out_stream: &Stream) Self {
+ pub fn init(unbuffered_out_stream: *Stream) Self {
return Self{
.unbuffered_out_stream = unbuffered_out_stream,
.buffer = undefined,
@@ -352,12 +352,12 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
};
}
- pub fn flush(self: &Self) !void {
+ pub fn flush(self: *Self) !void {
try self.unbuffered_out_stream.write(self.buffer[0..self.index]);
self.index = 0;
}
- fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
+ fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
const self = @fieldParentPtr(Self, "stream", out_stream);
if (bytes.len >= self.buffer.len) {
@@ -383,20 +383,20 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
/// Implementation of OutStream trait for Buffer
pub const BufferOutStream = struct {
- buffer: &Buffer,
+ buffer: *Buffer,
stream: Stream,
pub const Error = error{OutOfMemory};
pub const Stream = OutStream(Error);
- pub fn init(buffer: &Buffer) BufferOutStream {
+ pub fn init(buffer: *Buffer) BufferOutStream {
return BufferOutStream{
.buffer = buffer,
.stream = Stream{ .writeFn = writeFn },
};
}
- fn writeFn(out_stream: &Stream, bytes: []const u8) !void {
+ fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
const self = @fieldParentPtr(BufferOutStream, "stream", out_stream);
return self.buffer.append(bytes);
}
@@ -407,7 +407,7 @@ pub const BufferedAtomicFile = struct {
file_stream: FileOutStream,
buffered_stream: BufferedOutStream(FileOutStream.Error),
- pub fn create(allocator: &mem.Allocator, dest_path: []const u8) !&BufferedAtomicFile {
+ pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile {
// TODO with well defined copy elision we don't need this allocation
var self = try allocator.create(BufferedAtomicFile);
errdefer allocator.destroy(self);
@@ -427,18 +427,18 @@ pub const BufferedAtomicFile = struct {
}
/// always call destroy, even after successful finish()
- pub fn destroy(self: &BufferedAtomicFile) void {
+ pub fn destroy(self: *BufferedAtomicFile) void {
const allocator = self.atomic_file.allocator;
self.atomic_file.deinit();
allocator.destroy(self);
}
- pub fn finish(self: &BufferedAtomicFile) !void {
+ pub fn finish(self: *BufferedAtomicFile) !void {
try self.buffered_stream.flush();
try self.atomic_file.finish();
}
- pub fn stream(self: &BufferedAtomicFile) &OutStream(FileOutStream.Error) {
+ pub fn stream(self: *BufferedAtomicFile) *OutStream(FileOutStream.Error) {
return &self.buffered_stream.stream;
}
};
diff --git a/std/json.zig b/std/json.zig
index 9de8f0b53e..c8aef7688b 100644
--- a/std/json.zig
+++ b/std/json.zig
@@ -76,7 +76,7 @@ pub const Token = struct {
}
// Slice into the underlying input string.
- pub fn slice(self: &const Token, input: []const u8, i: usize) []const u8 {
+ pub fn slice(self: *const Token, input: []const u8, i: usize) []const u8 {
return input[i + self.offset - self.count .. i + self.offset];
}
};
@@ -115,7 +115,7 @@ pub const StreamingJsonParser = struct {
return p;
}
- pub fn reset(p: &StreamingJsonParser) void {
+ pub fn reset(p: *StreamingJsonParser) void {
p.state = State.TopLevelBegin;
p.count = 0;
// Set before ever read in main transition function
@@ -205,7 +205,7 @@ pub const StreamingJsonParser = struct {
// tokens. token2 is always null if token1 is null.
//
// There is currently no error recovery on a bad stream.
- pub fn feed(p: &StreamingJsonParser, c: u8, token1: &?Token, token2: &?Token) Error!void {
+ pub fn feed(p: *StreamingJsonParser, c: u8, token1: *?Token, token2: *?Token) Error!void {
token1.* = null;
token2.* = null;
p.count += 1;
@@ -217,7 +217,7 @@ pub const StreamingJsonParser = struct {
}
// Perform a single transition on the state machine and return any possible token.
- fn transition(p: &StreamingJsonParser, c: u8, token: &?Token) Error!bool {
+ fn transition(p: *StreamingJsonParser, c: u8, token: *?Token) Error!bool {
switch (p.state) {
State.TopLevelBegin => switch (c) {
'{' => {
@@ -861,7 +861,7 @@ pub fn validate(s: []const u8) bool {
var token1: ?Token = undefined;
var token2: ?Token = undefined;
- p.feed(c, &token1, &token2) catch |err| {
+ p.feed(c, *token1, *token2) catch |err| {
return false;
};
}
@@ -878,7 +878,7 @@ pub const ValueTree = struct {
arena: ArenaAllocator,
root: Value,
- pub fn deinit(self: &ValueTree) void {
+ pub fn deinit(self: *ValueTree) void {
self.arena.deinit();
}
};
@@ -894,7 +894,7 @@ pub const Value = union(enum) {
Array: ArrayList(Value),
Object: ObjectMap,
- pub fn dump(self: &const Value) void {
+ pub fn dump(self: *const Value) void {
switch (self.*) {
Value.Null => {
std.debug.warn("null");
@@ -941,7 +941,7 @@ pub const Value = union(enum) {
}
}
- pub fn dumpIndent(self: &const Value, indent: usize) void {
+ pub fn dumpIndent(self: *const Value, indent: usize) void {
if (indent == 0) {
self.dump();
} else {
@@ -949,7 +949,7 @@ pub const Value = union(enum) {
}
}
- fn dumpIndentLevel(self: &const Value, indent: usize, level: usize) void {
+ fn dumpIndentLevel(self: *const Value, indent: usize, level: usize) void {
switch (self.*) {
Value.Null => {
std.debug.warn("null");
@@ -1013,7 +1013,7 @@ pub const Value = union(enum) {
// A non-stream JSON parser which constructs a tree of Value's.
pub const JsonParser = struct {
- allocator: &Allocator,
+ allocator: *Allocator,
state: State,
copy_strings: bool,
// Stores parent nodes and un-combined Values.
@@ -1026,7 +1026,7 @@ pub const JsonParser = struct {
Simple,
};
- pub fn init(allocator: &Allocator, copy_strings: bool) JsonParser {
+ pub fn init(allocator: *Allocator, copy_strings: bool) JsonParser {
return JsonParser{
.allocator = allocator,
.state = State.Simple,
@@ -1035,16 +1035,16 @@ pub const JsonParser = struct {
};
}
- pub fn deinit(p: &JsonParser) void {
+ pub fn deinit(p: *JsonParser) void {
p.stack.deinit();
}
- pub fn reset(p: &JsonParser) void {
+ pub fn reset(p: *JsonParser) void {
p.state = State.Simple;
p.stack.shrink(0);
}
- pub fn parse(p: &JsonParser, input: []const u8) !ValueTree {
+ pub fn parse(p: *JsonParser, input: []const u8) !ValueTree {
var mp = StreamingJsonParser.init();
var arena = ArenaAllocator.init(p.allocator);
@@ -1090,7 +1090,7 @@ pub const JsonParser = struct {
// Even though p.allocator exists, we take an explicit allocator so that allocation state
// can be cleaned up on error correctly during a `parse` on call.
- fn transition(p: &JsonParser, allocator: &Allocator, input: []const u8, i: usize, token: &const Token) !void {
+ fn transition(p: *JsonParser, allocator: *Allocator, input: []const u8, i: usize, token: *const Token) !void {
switch (p.state) {
State.ObjectKey => switch (token.id) {
Token.Id.ObjectEnd => {
@@ -1223,7 +1223,7 @@ pub const JsonParser = struct {
}
}
- fn pushToParent(p: &JsonParser, value: &const Value) !void {
+ fn pushToParent(p: *JsonParser, value: *const Value) !void {
switch (p.stack.at(p.stack.len - 1)) {
// Object Parent -> [ ..., object, , value ]
Value.String => |key| {
@@ -1244,14 +1244,14 @@ pub const JsonParser = struct {
}
}
- fn parseString(p: &JsonParser, allocator: &Allocator, token: &const Token, input: []const u8, i: usize) !Value {
+ fn parseString(p: *JsonParser, allocator: *Allocator, token: *const Token, input: []const u8, i: usize) !Value {
// TODO: We don't strictly have to copy values which do not contain any escape
// characters if flagged with the option.
const slice = token.slice(input, i);
return Value{ .String = try mem.dupe(p.allocator, u8, slice) };
}
- fn parseNumber(p: &JsonParser, token: &const Token, input: []const u8, i: usize) !Value {
+ fn parseNumber(p: *JsonParser, token: *const Token, input: []const u8, i: usize) !Value {
return if (token.number_is_integer)
Value{ .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
else
diff --git a/std/linked_list.zig b/std/linked_list.zig
index c6be08171e..fbc0a0c42a 100644
--- a/std/linked_list.zig
+++ b/std/linked_list.zig
@@ -21,11 +21,11 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// Node inside the linked list wrapping the actual data.
pub const Node = struct {
- prev: ?&Node,
- next: ?&Node,
+ prev: ?*Node,
+ next: ?*Node,
data: T,
- pub fn init(value: &const T) Node {
+ pub fn init(value: *const T) Node {
return Node{
.prev = null,
.next = null,
@@ -38,14 +38,14 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
return Node.init({});
}
- pub fn toData(node: &Node) &ParentType {
+ pub fn toData(node: *Node) *ParentType {
comptime assert(isIntrusive());
return @fieldParentPtr(ParentType, field_name, node);
}
};
- first: ?&Node,
- last: ?&Node,
+ first: ?*Node,
+ last: ?*Node,
len: usize,
/// Initialize a linked list.
@@ -69,7 +69,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// Arguments:
/// node: Pointer to a node in the list.
/// new_node: Pointer to the new node to insert.
- pub fn insertAfter(list: &Self, node: &Node, new_node: &Node) void {
+ pub fn insertAfter(list: *Self, node: *Node, new_node: *Node) void {
new_node.prev = node;
if (node.next) |next_node| {
// Intermediate node.
@@ -90,7 +90,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// Arguments:
/// node: Pointer to a node in the list.
/// new_node: Pointer to the new node to insert.
- pub fn insertBefore(list: &Self, node: &Node, new_node: &Node) void {
+ pub fn insertBefore(list: *Self, node: *Node, new_node: *Node) void {
new_node.next = node;
if (node.prev) |prev_node| {
// Intermediate node.
@@ -110,7 +110,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Arguments:
/// new_node: Pointer to the new node to insert.
- pub fn append(list: &Self, new_node: &Node) void {
+ pub fn append(list: *Self, new_node: *Node) void {
if (list.last) |last| {
// Insert after last.
list.insertAfter(last, new_node);
@@ -124,7 +124,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Arguments:
/// new_node: Pointer to the new node to insert.
- pub fn prepend(list: &Self, new_node: &Node) void {
+ pub fn prepend(list: *Self, new_node: *Node) void {
if (list.first) |first| {
// Insert before first.
list.insertBefore(first, new_node);
@@ -143,7 +143,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Arguments:
/// node: Pointer to the node to be removed.
- pub fn remove(list: &Self, node: &Node) void {
+ pub fn remove(list: *Self, node: *Node) void {
if (node.prev) |prev_node| {
// Intermediate node.
prev_node.next = node.next;
@@ -168,7 +168,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Returns:
/// A pointer to the last node in the list.
- pub fn pop(list: &Self) ?&Node {
+ pub fn pop(list: *Self) ?*Node {
const last = list.last ?? return null;
list.remove(last);
return last;
@@ -178,7 +178,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Returns:
/// A pointer to the first node in the list.
- pub fn popFirst(list: &Self) ?&Node {
+ pub fn popFirst(list: *Self) ?*Node {
const first = list.first ?? return null;
list.remove(first);
return first;
@@ -191,7 +191,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Returns:
/// A pointer to the new node.
- pub fn allocateNode(list: &Self, allocator: &Allocator) !&Node {
+ pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node {
comptime assert(!isIntrusive());
return allocator.create(Node);
}
@@ -201,7 +201,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// Arguments:
/// node: Pointer to the node to deallocate.
/// allocator: Dynamic memory allocator.
- pub fn destroyNode(list: &Self, node: &Node, allocator: &Allocator) void {
+ pub fn destroyNode(list: *Self, node: *Node, allocator: *Allocator) void {
comptime assert(!isIntrusive());
allocator.destroy(node);
}
@@ -214,7 +214,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
///
/// Returns:
/// A pointer to the new node.
- pub fn createNode(list: &Self, data: &const T, allocator: &Allocator) !&Node {
+ pub fn createNode(list: *Self, data: *const T, allocator: *Allocator) !*Node {
comptime assert(!isIntrusive());
var node = try list.allocateNode(allocator);
node.* = Node.init(data);
diff --git a/std/macho.zig b/std/macho.zig
index 615569e4b4..e71ac76b1a 100644
--- a/std/macho.zig
+++ b/std/macho.zig
@@ -42,13 +42,13 @@ pub const Symbol = struct {
name: []const u8,
address: u64,
- fn addressLessThan(lhs: &const Symbol, rhs: &const Symbol) bool {
+ fn addressLessThan(lhs: *const Symbol, rhs: *const Symbol) bool {
return lhs.address < rhs.address;
}
};
pub const SymbolTable = struct {
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
symbols: []const Symbol,
strings: []const u8,
@@ -56,7 +56,7 @@ pub const SymbolTable = struct {
// Ideally we'd use _mh_execute_header because it's always at 0x100000000
// in the image but as it's located in a different section than executable
// code, its displacement is different.
- pub fn deinit(self: &SymbolTable) void {
+ pub fn deinit(self: *SymbolTable) void {
self.allocator.free(self.symbols);
self.symbols = []const Symbol{};
@@ -64,7 +64,7 @@ pub const SymbolTable = struct {
self.strings = []const u8{};
}
- pub fn search(self: &const SymbolTable, address: usize) ?&const Symbol {
+ pub fn search(self: *const SymbolTable, address: usize) ?*const Symbol {
var min: usize = 0;
var max: usize = self.symbols.len - 1; // Exclude sentinel.
while (min < max) {
@@ -83,7 +83,7 @@ pub const SymbolTable = struct {
}
};
-pub fn loadSymbols(allocator: &mem.Allocator, in: &io.FileInStream) !SymbolTable {
+pub fn loadSymbols(allocator: *mem.Allocator, in: *io.FileInStream) !SymbolTable {
var file = in.file;
try file.seekTo(0);
@@ -160,13 +160,13 @@ pub fn loadSymbols(allocator: &mem.Allocator, in: &io.FileInStream) !SymbolTable
};
}
-fn readNoEof(in: &io.FileInStream, comptime T: type, result: []T) !void {
+fn readNoEof(in: *io.FileInStream, comptime T: type, result: []T) !void {
return in.stream.readNoEof(([]u8)(result));
}
-fn readOneNoEof(in: &io.FileInStream, comptime T: type, result: &T) !void {
+fn readOneNoEof(in: *io.FileInStream, comptime T: type, result: *T) !void {
return readNoEof(in, T, result[0..1]);
}
-fn isSymbol(sym: &const Nlist64) bool {
+fn isSymbol(sym: *const Nlist64) bool {
return sym.n_value != 0 and sym.n_desc == 0;
}
diff --git a/std/math/complex/atan.zig b/std/math/complex/atan.zig
index b7bbf930eb..9bfe5fe724 100644
--- a/std/math/complex/atan.zig
+++ b/std/math/complex/atan.zig
@@ -29,7 +29,7 @@ fn redupif32(x: f32) f32 {
return ((x - u * DP1) - u * DP2) - t * DP3;
}
-fn atan32(z: &const Complex(f32)) Complex(f32) {
+fn atan32(z: *const Complex(f32)) Complex(f32) {
const maxnum = 1.0e38;
const x = z.re;
@@ -78,7 +78,7 @@ fn redupif64(x: f64) f64 {
return ((x - u * DP1) - u * DP2) - t * DP3;
}
-fn atan64(z: &const Complex(f64)) Complex(f64) {
+fn atan64(z: *const Complex(f64)) Complex(f64) {
const maxnum = 1.0e308;
const x = z.re;
diff --git a/std/math/complex/cosh.zig b/std/math/complex/cosh.zig
index 96eac68556..c2f9a47b8d 100644
--- a/std/math/complex/cosh.zig
+++ b/std/math/complex/cosh.zig
@@ -15,7 +15,7 @@ pub fn cosh(z: var) Complex(@typeOf(z.re)) {
};
}
-fn cosh32(z: &const Complex(f32)) Complex(f32) {
+fn cosh32(z: *const Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -78,7 +78,7 @@ fn cosh32(z: &const Complex(f32)) Complex(f32) {
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
}
-fn cosh64(z: &const Complex(f64)) Complex(f64) {
+fn cosh64(z: *const Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
diff --git a/std/math/complex/exp.zig b/std/math/complex/exp.zig
index 8fe069a43d..44c354f246 100644
--- a/std/math/complex/exp.zig
+++ b/std/math/complex/exp.zig
@@ -16,7 +16,7 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
};
}
-fn exp32(z: &const Complex(f32)) Complex(f32) {
+fn exp32(z: *const Complex(f32)) Complex(f32) {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
@@ -63,7 +63,7 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
}
}
-fn exp64(z: &const Complex(f64)) Complex(f64) {
+fn exp64(z: *const Complex(f64)) Complex(f64) {
const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
diff --git a/std/math/complex/index.zig b/std/math/complex/index.zig
index 5902ffaa19..b00296beda 100644
--- a/std/math/complex/index.zig
+++ b/std/math/complex/index.zig
@@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
};
}
- pub fn add(self: &const Self, other: &const Self) Self {
+ pub fn add(self: *const Self, other: *const Self) Self {
return Self{
.re = self.re + other.re,
.im = self.im + other.im,
};
}
- pub fn sub(self: &const Self, other: &const Self) Self {
+ pub fn sub(self: *const Self, other: *const Self) Self {
return Self{
.re = self.re - other.re,
.im = self.im - other.im,
};
}
- pub fn mul(self: &const Self, other: &const Self) Self {
+ pub fn mul(self: *const Self, other: *const Self) Self {
return Self{
.re = self.re * other.re - self.im * other.im,
.im = self.im * other.re + self.re * other.im,
};
}
- pub fn div(self: &const Self, other: &const Self) Self {
+ pub fn div(self: *const Self, other: *const Self) Self {
const re_num = self.re * other.re + self.im * other.im;
const im_num = self.im * other.re - self.re * other.im;
const den = other.re * other.re + other.im * other.im;
@@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
};
}
- pub fn conjugate(self: &const Self) Self {
+ pub fn conjugate(self: *const Self) Self {
return Self{
.re = self.re,
.im = -self.im,
};
}
- pub fn reciprocal(self: &const Self) Self {
+ pub fn reciprocal(self: *const Self) Self {
const m = self.re * self.re + self.im * self.im;
return Self{
.re = self.re / m,
@@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
};
}
- pub fn magnitude(self: &const Self) T {
+ pub fn magnitude(self: *const Self) T {
return math.sqrt(self.re * self.re + self.im * self.im);
}
};
diff --git a/std/math/complex/ldexp.zig b/std/math/complex/ldexp.zig
index 7ebefff40c..a56c2ef2eb 100644
--- a/std/math/complex/ldexp.zig
+++ b/std/math/complex/ldexp.zig
@@ -14,7 +14,7 @@ pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
};
}
-fn frexp_exp32(x: f32, expt: &i32) f32 {
+fn frexp_exp32(x: f32, expt: *i32) f32 {
const k = 235; // reduction constant
const kln2 = 162.88958740; // k * ln2
@@ -24,7 +24,7 @@ fn frexp_exp32(x: f32, expt: &i32) f32 {
return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
}
-fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) {
+fn ldexp_cexp32(z: *const Complex(f32), expt: i32) Complex(f32) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp32(z.re, &ex_expt);
const exptf = expt + ex_expt;
@@ -38,7 +38,7 @@ fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) {
return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
}
-fn frexp_exp64(x: f64, expt: &i32) f64 {
+fn frexp_exp64(x: f64, expt: *i32) f64 {
const k = 1799; // reduction constant
const kln2 = 1246.97177782734161156; // k * ln2
@@ -54,7 +54,7 @@ fn frexp_exp64(x: f64, expt: &i32) f64 {
return @bitCast(f64, (u64(high_word) << 32) | lx);
}
-fn ldexp_cexp64(z: &const Complex(f64), expt: i32) Complex(f64) {
+fn ldexp_cexp64(z: *const Complex(f64), expt: i32) Complex(f64) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp64(z.re, &ex_expt);
const exptf = i64(expt + ex_expt);
diff --git a/std/math/complex/pow.zig b/std/math/complex/pow.zig
index bef9fde542..4c2cd9cf34 100644
--- a/std/math/complex/pow.zig
+++ b/std/math/complex/pow.zig
@@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
-pub fn pow(comptime T: type, z: &const T, c: &const T) T {
+pub fn pow(comptime T: type, z: *const T, c: *const T) T {
const p = cmath.log(z);
const q = c.mul(p);
return cmath.exp(q);
diff --git a/std/math/complex/sinh.zig b/std/math/complex/sinh.zig
index 09a62ca058..3d196bfd50 100644
--- a/std/math/complex/sinh.zig
+++ b/std/math/complex/sinh.zig
@@ -15,7 +15,7 @@ pub fn sinh(z: var) Complex(@typeOf(z.re)) {
};
}
-fn sinh32(z: &const Complex(f32)) Complex(f32) {
+fn sinh32(z: *const Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -78,7 +78,7 @@ fn sinh32(z: &const Complex(f32)) Complex(f32) {
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
}
-fn sinh64(z: &const Complex(f64)) Complex(f64) {
+fn sinh64(z: *const Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
diff --git a/std/math/complex/sqrt.zig b/std/math/complex/sqrt.zig
index afda69f7c9..d4f5a67528 100644
--- a/std/math/complex/sqrt.zig
+++ b/std/math/complex/sqrt.zig
@@ -15,7 +15,7 @@ pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
};
}
-fn sqrt32(z: &const Complex(f32)) Complex(f32) {
+fn sqrt32(z: *const Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -57,7 +57,7 @@ fn sqrt32(z: &const Complex(f32)) Complex(f32) {
}
}
-fn sqrt64(z: &const Complex(f64)) Complex(f64) {
+fn sqrt64(z: *const Complex(f64)) Complex(f64) {
// may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
const threshold = 0x1.a827999fcef32p+1022;
diff --git a/std/math/complex/tanh.zig b/std/math/complex/tanh.zig
index 34250b1b4a..1d754838a3 100644
--- a/std/math/complex/tanh.zig
+++ b/std/math/complex/tanh.zig
@@ -13,7 +13,7 @@ pub fn tanh(z: var) Complex(@typeOf(z.re)) {
};
}
-fn tanh32(z: &const Complex(f32)) Complex(f32) {
+fn tanh32(z: *const Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -51,7 +51,7 @@ fn tanh32(z: &const Complex(f32)) Complex(f32) {
return Complex(f32).new((beta * rho * s) / den, t / den);
}
-fn tanh64(z: &const Complex(f64)) Complex(f64) {
+fn tanh64(z: *const Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
diff --git a/std/math/hypot.zig b/std/math/hypot.zig
index fe0de3a1ea..494df22ba6 100644
--- a/std/math/hypot.zig
+++ b/std/math/hypot.zig
@@ -52,7 +52,7 @@ fn hypot32(x: f32, y: f32) f32 {
return z * math.sqrt(f32(f64(x) * x + f64(y) * y));
}
-fn sq(hi: &f64, lo: &f64, x: f64) void {
+fn sq(hi: *f64, lo: *f64, x: f64) void {
const split: f64 = 0x1.0p27 + 1.0;
const xc = x * split;
const xh = x - xc + xc;
diff --git a/std/math/index.zig b/std/math/index.zig
index 847e972500..33bc1082f7 100644
--- a/std/math/index.zig
+++ b/std/math/index.zig
@@ -46,12 +46,12 @@ pub fn forceEval(value: var) void {
switch (T) {
f32 => {
var x: f32 = undefined;
- const p = @ptrCast(&volatile f32, &x);
+ const p = @ptrCast(*volatile f32, &x);
p.* = x;
},
f64 => {
var x: f64 = undefined;
- const p = @ptrCast(&volatile f64, &x);
+ const p = @ptrCast(*volatile f64, &x);
p.* = x;
},
else => {
diff --git a/std/mem.zig b/std/mem.zig
index f4696cff9f..aec24e8491 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -13,7 +13,7 @@ pub const Allocator = struct {
/// The returned newly allocated memory is undefined.
/// `alignment` is guaranteed to be >= 1
/// `alignment` is guaranteed to be a power of 2
- allocFn: fn (self: &Allocator, byte_count: usize, alignment: u29) Error![]u8,
+ allocFn: fn (self: *Allocator, byte_count: usize, alignment: u29) Error![]u8,
/// If `new_byte_count > old_mem.len`:
/// * `old_mem.len` is the same as what was returned from allocFn or reallocFn.
@@ -26,22 +26,22 @@ pub const Allocator = struct {
/// The returned newly allocated memory is undefined.
/// `alignment` is guaranteed to be >= 1
/// `alignment` is guaranteed to be a power of 2
- reallocFn: fn (self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8,
+ reallocFn: fn (self: *Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8,
/// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
- freeFn: fn (self: &Allocator, old_mem: []u8) void,
+ freeFn: fn (self: *Allocator, old_mem: []u8) void,
- fn create(self: &Allocator, comptime T: type) !&T {
- if (@sizeOf(T) == 0) return &{};
+ fn create(self: *Allocator, comptime T: type) !*T {
+ if (@sizeOf(T) == 0) return *{};
const slice = try self.alloc(T, 1);
return &slice[0];
}
// TODO once #733 is solved, this will replace create
- fn construct(self: &Allocator, init: var) t: {
+ fn construct(self: *Allocator, init: var) t: {
// TODO this is a workaround for type getting parsed as Error!&const T
const T = @typeOf(init).Child;
- break :t Error!&T;
+ break :t Error!*T;
} {
const T = @typeOf(init).Child;
if (@sizeOf(T) == 0) return &{};
@@ -51,17 +51,17 @@ pub const Allocator = struct {
return ptr;
}
- fn destroy(self: &Allocator, ptr: var) void {
+ fn destroy(self: *Allocator, ptr: var) void {
self.free(ptr[0..1]);
}
- fn alloc(self: &Allocator, comptime T: type, n: usize) ![]T {
+ fn alloc(self: *Allocator, comptime T: type, n: usize) ![]T {
return self.alignedAlloc(T, @alignOf(T), n);
}
- fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29, n: usize) ![]align(alignment) T {
+ fn alignedAlloc(self: *Allocator, comptime T: type, comptime alignment: u29, n: usize) ![]align(alignment) T {
if (n == 0) {
- return (&align(alignment) T)(undefined)[0..0];
+ return (*align(alignment) T)(undefined)[0..0];
}
const byte_count = math.mul(usize, @sizeOf(T), n) catch return Error.OutOfMemory;
const byte_slice = try self.allocFn(self, byte_count, alignment);
@@ -73,17 +73,17 @@ pub const Allocator = struct {
return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
}
- fn realloc(self: &Allocator, comptime T: type, old_mem: []T, n: usize) ![]T {
+ fn realloc(self: *Allocator, comptime T: type, old_mem: []T, n: usize) ![]T {
return self.alignedRealloc(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
}
- fn alignedRealloc(self: &Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
+ fn alignedRealloc(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
if (old_mem.len == 0) {
return self.alloc(T, n);
}
if (n == 0) {
self.free(old_mem);
- return (&align(alignment) T)(undefined)[0..0];
+ return (*align(alignment) T)(undefined)[0..0];
}
const old_byte_slice = ([]u8)(old_mem);
@@ -102,11 +102,11 @@ pub const Allocator = struct {
/// Reallocate, but `n` must be less than or equal to `old_mem.len`.
/// Unlike `realloc`, this function cannot fail.
/// Shrinking to 0 is the same as calling `free`.
- fn shrink(self: &Allocator, comptime T: type, old_mem: []T, n: usize) []T {
+ fn shrink(self: *Allocator, comptime T: type, old_mem: []T, n: usize) []T {
return self.alignedShrink(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
}
- fn alignedShrink(self: &Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) []align(alignment) T {
+ fn alignedShrink(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) []align(alignment) T {
if (n == 0) {
self.free(old_mem);
return old_mem[0..0];
@@ -123,10 +123,10 @@ pub const Allocator = struct {
return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
}
- fn free(self: &Allocator, memory: var) void {
+ fn free(self: *Allocator, memory: var) void {
const bytes = ([]const u8)(memory);
if (bytes.len == 0) return;
- const non_const_ptr = @intToPtr(&u8, @ptrToInt(bytes.ptr));
+ const non_const_ptr = @intToPtr(*u8, @ptrToInt(bytes.ptr));
self.freeFn(self, non_const_ptr[0..bytes.len]);
}
};
@@ -186,7 +186,7 @@ pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool {
}
/// Copies ::m to newly allocated memory. Caller is responsible to free it.
-pub fn dupe(allocator: &Allocator, comptime T: type, m: []const T) ![]T {
+pub fn dupe(allocator: *Allocator, comptime T: type, m: []const T) ![]T {
const new_buf = try allocator.alloc(T, m.len);
copy(T, new_buf, m);
return new_buf;
@@ -457,7 +457,7 @@ pub const SplitIterator = struct {
split_bytes: []const u8,
index: usize,
- pub fn next(self: &SplitIterator) ?[]const u8 {
+ pub fn next(self: *SplitIterator) ?[]const u8 {
// move to beginning of token
while (self.index < self.buffer.len and self.isSplitByte(self.buffer[self.index])) : (self.index += 1) {}
const start = self.index;
@@ -473,14 +473,14 @@ pub const SplitIterator = struct {
}
/// Returns a slice of the remaining bytes. Does not affect iterator state.
- pub fn rest(self: &const SplitIterator) []const u8 {
+ pub fn rest(self: *const SplitIterator) []const u8 {
// move to beginning of token
var index: usize = self.index;
while (index < self.buffer.len and self.isSplitByte(self.buffer[index])) : (index += 1) {}
return self.buffer[index..];
}
- fn isSplitByte(self: &const SplitIterator, byte: u8) bool {
+ fn isSplitByte(self: *const SplitIterator, byte: u8) bool {
for (self.split_bytes) |split_byte| {
if (byte == split_byte) {
return true;
@@ -492,7 +492,7 @@ pub const SplitIterator = struct {
/// Naively combines a series of strings with a separator.
/// Allocates memory for the result, which must be freed by the caller.
-pub fn join(allocator: &Allocator, sep: u8, strings: ...) ![]u8 {
+pub fn join(allocator: *Allocator, sep: u8, strings: ...) ![]u8 {
comptime assert(strings.len >= 1);
var total_strings_len: usize = strings.len; // 1 sep per string
{
@@ -649,7 +649,7 @@ test "mem.max" {
assert(max(u8, "abcdefg") == 'g');
}
-pub fn swap(comptime T: type, a: &T, b: &T) void {
+pub fn swap(comptime T: type, a: *T, b: *T) void {
const tmp = a.*;
a.* = b.*;
b.* = tmp;
diff --git a/std/net.zig b/std/net.zig
index 3af4e0b525..bfe4b1c2a0 100644
--- a/std/net.zig
+++ b/std/net.zig
@@ -31,7 +31,7 @@ pub const Address = struct {
};
}
- pub fn initIp6(ip6: &const Ip6Addr, port: u16) Address {
+ pub fn initIp6(ip6: *const Ip6Addr, port: u16) Address {
return Address{
.family = posix.AF_INET6,
.os_addr = posix.sockaddr{
@@ -46,15 +46,15 @@ pub const Address = struct {
};
}
- pub fn initPosix(addr: &const posix.sockaddr) Address {
+ pub fn initPosix(addr: *const posix.sockaddr) Address {
return Address{ .os_addr = addr.* };
}
- pub fn format(self: &const Address, out_stream: var) !void {
+ pub fn format(self: *const Address, out_stream: var) !void {
switch (self.os_addr.in.family) {
posix.AF_INET => {
const native_endian_port = std.mem.endianSwapIfLe(u16, self.os_addr.in.port);
- const bytes = ([]const u8)((&self.os_addr.in.addr)[0..1]);
+ const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]);
try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port);
},
posix.AF_INET6 => {
diff --git a/std/os/child_process.zig b/std/os/child_process.zig
index 51f1bd96e5..30a2fd1408 100644
--- a/std/os/child_process.zig
+++ b/std/os/child_process.zig
@@ -20,7 +20,7 @@ pub const ChildProcess = struct {
pub handle: if (is_windows) windows.HANDLE else void,
pub thread_handle: if (is_windows) windows.HANDLE else void,
- pub allocator: &mem.Allocator,
+ pub allocator: *mem.Allocator,
pub stdin: ?os.File,
pub stdout: ?os.File,
@@ -31,7 +31,7 @@ pub const ChildProcess = struct {
pub argv: []const []const u8,
/// Leave as null to use the current env map using the supplied allocator.
- pub env_map: ?&const BufMap,
+ pub env_map: ?*const BufMap,
pub stdin_behavior: StdIo,
pub stdout_behavior: StdIo,
@@ -47,7 +47,7 @@ pub const ChildProcess = struct {
pub cwd: ?[]const u8,
err_pipe: if (is_windows) void else [2]i32,
- llnode: if (is_windows) void else LinkedList(&ChildProcess).Node,
+ llnode: if (is_windows) void else LinkedList(*ChildProcess).Node,
pub const SpawnError = error{
ProcessFdQuotaExceeded,
@@ -84,7 +84,7 @@ pub const ChildProcess = struct {
/// First argument in argv is the executable.
/// On success must call deinit.
- pub fn init(argv: []const []const u8, allocator: &mem.Allocator) !&ChildProcess {
+ pub fn init(argv: []const []const u8, allocator: *mem.Allocator) !*ChildProcess {
const child = try allocator.create(ChildProcess);
errdefer allocator.destroy(child);
@@ -114,14 +114,14 @@ pub const ChildProcess = struct {
return child;
}
- pub fn setUserName(self: &ChildProcess, name: []const u8) !void {
+ pub fn setUserName(self: *ChildProcess, name: []const u8) !void {
const user_info = try os.getUserInfo(name);
self.uid = user_info.uid;
self.gid = user_info.gid;
}
/// On success must call `kill` or `wait`.
- pub fn spawn(self: &ChildProcess) !void {
+ pub fn spawn(self: *ChildProcess) !void {
if (is_windows) {
return self.spawnWindows();
} else {
@@ -129,13 +129,13 @@ pub const ChildProcess = struct {
}
}
- pub fn spawnAndWait(self: &ChildProcess) !Term {
+ pub fn spawnAndWait(self: *ChildProcess) !Term {
try self.spawn();
return self.wait();
}
/// Forcibly terminates child process and then cleans up all resources.
- pub fn kill(self: &ChildProcess) !Term {
+ pub fn kill(self: *ChildProcess) !Term {
if (is_windows) {
return self.killWindows(1);
} else {
@@ -143,7 +143,7 @@ pub const ChildProcess = struct {
}
}
- pub fn killWindows(self: &ChildProcess, exit_code: windows.UINT) !Term {
+ pub fn killWindows(self: *ChildProcess, exit_code: windows.UINT) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
@@ -159,7 +159,7 @@ pub const ChildProcess = struct {
return ??self.term;
}
- pub fn killPosix(self: &ChildProcess) !Term {
+ pub fn killPosix(self: *ChildProcess) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
@@ -179,7 +179,7 @@ pub const ChildProcess = struct {
}
/// Blocks until child process terminates and then cleans up all resources.
- pub fn wait(self: &ChildProcess) !Term {
+ pub fn wait(self: *ChildProcess) !Term {
if (is_windows) {
return self.waitWindows();
} else {
@@ -195,7 +195,7 @@ pub const ChildProcess = struct {
/// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
/// If it succeeds, the caller owns result.stdout and result.stderr memory.
- pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8, env_map: ?&const BufMap, max_output_size: usize) !ExecResult {
+ pub fn exec(allocator: *mem.Allocator, argv: []const []const u8, cwd: ?[]const u8, env_map: ?*const BufMap, max_output_size: usize) !ExecResult {
const child = try ChildProcess.init(argv, allocator);
defer child.deinit();
@@ -225,7 +225,7 @@ pub const ChildProcess = struct {
};
}
- fn waitWindows(self: &ChildProcess) !Term {
+ fn waitWindows(self: *ChildProcess) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
@@ -235,7 +235,7 @@ pub const ChildProcess = struct {
return ??self.term;
}
- fn waitPosix(self: &ChildProcess) !Term {
+ fn waitPosix(self: *ChildProcess) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
@@ -245,16 +245,16 @@ pub const ChildProcess = struct {
return ??self.term;
}
- pub fn deinit(self: &ChildProcess) void {
+ pub fn deinit(self: *ChildProcess) void {
self.allocator.destroy(self);
}
- fn waitUnwrappedWindows(self: &ChildProcess) !void {
+ fn waitUnwrappedWindows(self: *ChildProcess) !void {
const result = os.windowsWaitSingle(self.handle, windows.INFINITE);
self.term = (SpawnError!Term)(x: {
var exit_code: windows.DWORD = undefined;
- if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
+ if (windows.GetExitCodeProcess(self.handle, *exit_code) == 0) {
break :x Term{ .Unknown = 0 };
} else {
break :x Term{ .Exited = @bitCast(i32, exit_code) };
@@ -267,7 +267,7 @@ pub const ChildProcess = struct {
return result;
}
- fn waitUnwrapped(self: &ChildProcess) void {
+ fn waitUnwrapped(self: *ChildProcess) void {
var status: i32 = undefined;
while (true) {
const err = posix.getErrno(posix.waitpid(self.pid, &status, 0));
@@ -283,11 +283,11 @@ pub const ChildProcess = struct {
}
}
- fn handleWaitResult(self: &ChildProcess, status: i32) void {
+ fn handleWaitResult(self: *ChildProcess, status: i32) void {
self.term = self.cleanupAfterWait(status);
}
- fn cleanupStreams(self: &ChildProcess) void {
+ fn cleanupStreams(self: *ChildProcess) void {
if (self.stdin) |*stdin| {
stdin.close();
self.stdin = null;
@@ -302,7 +302,7 @@ pub const ChildProcess = struct {
}
}
- fn cleanupAfterWait(self: &ChildProcess, status: i32) !Term {
+ fn cleanupAfterWait(self: *ChildProcess, status: i32) !Term {
defer {
os.close(self.err_pipe[0]);
os.close(self.err_pipe[1]);
@@ -335,7 +335,7 @@ pub const ChildProcess = struct {
Term{ .Unknown = status };
}
- fn spawnPosix(self: &ChildProcess) !void {
+ fn spawnPosix(self: *ChildProcess) !void {
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
errdefer if (self.stdin_behavior == StdIo.Pipe) {
destroyPipe(stdin_pipe);
@@ -432,7 +432,7 @@ pub const ChildProcess = struct {
self.pid = pid;
self.err_pipe = err_pipe;
- self.llnode = LinkedList(&ChildProcess).Node.init(self);
+ self.llnode = LinkedList(*ChildProcess).Node.init(self);
self.term = null;
if (self.stdin_behavior == StdIo.Pipe) {
@@ -446,7 +446,7 @@ pub const ChildProcess = struct {
}
}
- fn spawnWindows(self: &ChildProcess) !void {
+ fn spawnWindows(self: *ChildProcess) !void {
const saAttr = windows.SECURITY_ATTRIBUTES{
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
.bInheritHandle = windows.TRUE,
@@ -639,8 +639,8 @@ pub const ChildProcess = struct {
}
};
-fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8, lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void {
- if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) {
+fn windowsCreateProcess(app_name: *u8, cmd_line: *u8, envp_ptr: ?*u8, cwd_ptr: ?*u8, lpStartupInfo: *windows.STARTUPINFOA, lpProcessInformation: *windows.PROCESS_INFORMATION) !void {
+ if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?*c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
@@ -653,7 +653,7 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?
/// Caller must dealloc.
/// Guarantees a null byte at result[result.len].
-fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8) ![]u8 {
+fn windowsCreateCommandLine(allocator: *mem.Allocator, argv: []const []const u8) ![]u8 {
var buf = try Buffer.initSize(allocator, 0);
defer buf.deinit();
@@ -698,7 +698,7 @@ fn windowsDestroyPipe(rd: ?windows.HANDLE, wr: ?windows.HANDLE) void {
// a namespace field lookup
const SECURITY_ATTRIBUTES = windows.SECURITY_ATTRIBUTES;
-fn windowsMakePipe(rd: &windows.HANDLE, wr: &windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
+fn windowsMakePipe(rd: *windows.HANDLE, wr: *windows.HANDLE, sattr: *const SECURITY_ATTRIBUTES) !void {
if (windows.CreatePipe(rd, wr, sattr, 0) == 0) {
const err = windows.GetLastError();
return switch (err) {
@@ -716,7 +716,7 @@ fn windowsSetHandleInfo(h: windows.HANDLE, mask: windows.DWORD, flags: windows.D
}
}
-fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
+fn windowsMakePipeIn(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const SECURITY_ATTRIBUTES) !void {
var rd_h: windows.HANDLE = undefined;
var wr_h: windows.HANDLE = undefined;
try windowsMakePipe(&rd_h, &wr_h, sattr);
@@ -726,7 +726,7 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S
wr.* = wr_h;
}
-fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
+fn windowsMakePipeOut(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const SECURITY_ATTRIBUTES) !void {
var rd_h: windows.HANDLE = undefined;
var wr_h: windows.HANDLE = undefined;
try windowsMakePipe(&rd_h, &wr_h, sattr);
@@ -748,7 +748,7 @@ fn makePipe() ![2]i32 {
return fds;
}
-fn destroyPipe(pipe: &const [2]i32) void {
+fn destroyPipe(pipe: *const [2]i32) void {
os.close((pipe.*)[0]);
os.close((pipe.*)[1]);
}
diff --git a/std/os/darwin.zig b/std/os/darwin.zig
index a3fc230ac5..77e8b6bb6a 100644
--- a/std/os/darwin.zig
+++ b/std/os/darwin.zig
@@ -309,7 +309,7 @@ pub fn isatty(fd: i32) bool {
return c.isatty(fd) != 0;
}
-pub fn fstat(fd: i32, buf: &c.Stat) usize {
+pub fn fstat(fd: i32, buf: *c.Stat) usize {
return errnoWrap(c.@"fstat$INODE64"(fd, buf));
}
@@ -317,7 +317,7 @@ pub fn lseek(fd: i32, offset: isize, whence: c_int) usize {
return errnoWrap(c.lseek(fd, offset, whence));
}
-pub fn open(path: &const u8, flags: u32, mode: usize) usize {
+pub fn open(path: *const u8, flags: u32, mode: usize) usize {
return errnoWrap(c.open(path, @bitCast(c_int, flags), mode));
}
@@ -325,79 +325,79 @@ pub fn raise(sig: i32) usize {
return errnoWrap(c.raise(sig));
}
-pub fn read(fd: i32, buf: &u8, nbyte: usize) usize {
- return errnoWrap(c.read(fd, @ptrCast(&c_void, buf), nbyte));
+pub fn read(fd: i32, buf: *u8, nbyte: usize) usize {
+ return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte));
}
-pub fn stat(noalias path: &const u8, noalias buf: &stat) usize {
+pub fn stat(noalias path: *const u8, noalias buf: *stat) usize {
return errnoWrap(c.stat(path, buf));
}
-pub fn write(fd: i32, buf: &const u8, nbyte: usize) usize {
- return errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte));
+pub fn write(fd: i32, buf: *const u8, nbyte: usize) usize {
+ return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte));
}
-pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
- const ptr_result = c.mmap(@ptrCast(&c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
+pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
+ const ptr_result = c.mmap(@ptrCast(*c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
return errnoWrap(isize_result);
}
pub fn munmap(address: usize, length: usize) usize {
- return errnoWrap(c.munmap(@intToPtr(&c_void, address), length));
+ return errnoWrap(c.munmap(@intToPtr(*c_void, address), length));
}
-pub fn unlink(path: &const u8) usize {
+pub fn unlink(path: *const u8) usize {
return errnoWrap(c.unlink(path));
}
-pub fn getcwd(buf: &u8, size: usize) usize {
+pub fn getcwd(buf: *u8, size: usize) usize {
return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
}
-pub fn waitpid(pid: i32, status: &i32, options: u32) usize {
+pub fn waitpid(pid: i32, status: *i32, options: u32) usize {
comptime assert(i32.bit_count == c_int.bit_count);
- return errnoWrap(c.waitpid(pid, @ptrCast(&c_int, status), @bitCast(c_int, options)));
+ return errnoWrap(c.waitpid(pid, @ptrCast(*c_int, status), @bitCast(c_int, options)));
}
pub fn fork() usize {
return errnoWrap(c.fork());
}
-pub fn access(path: &const u8, mode: u32) usize {
+pub fn access(path: *const u8, mode: u32) usize {
return errnoWrap(c.access(path, mode));
}
-pub fn pipe(fds: &[2]i32) usize {
+pub fn pipe(fds: *[2]i32) usize {
comptime assert(i32.bit_count == c_int.bit_count);
- return errnoWrap(c.pipe(@ptrCast(&c_int, fds)));
+ return errnoWrap(c.pipe(@ptrCast(*c_int, fds)));
}
-pub fn getdirentries64(fd: i32, buf_ptr: &u8, buf_len: usize, basep: &i64) usize {
+pub fn getdirentries64(fd: i32, buf_ptr: *u8, buf_len: usize, basep: *i64) usize {
return errnoWrap(@bitCast(isize, c.__getdirentries64(fd, buf_ptr, buf_len, basep)));
}
-pub fn mkdir(path: &const u8, mode: u32) usize {
+pub fn mkdir(path: *const u8, mode: u32) usize {
return errnoWrap(c.mkdir(path, mode));
}
-pub fn symlink(existing: &const u8, new: &const u8) usize {
+pub fn symlink(existing: *const u8, new: *const u8) usize {
return errnoWrap(c.symlink(existing, new));
}
-pub fn rename(old: &const u8, new: &const u8) usize {
+pub fn rename(old: *const u8, new: *const u8) usize {
return errnoWrap(c.rename(old, new));
}
-pub fn rmdir(path: &const u8) usize {
+pub fn rmdir(path: *const u8) usize {
return errnoWrap(c.rmdir(path));
}
-pub fn chdir(path: &const u8) usize {
+pub fn chdir(path: *const u8) usize {
return errnoWrap(c.chdir(path));
}
-pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize {
+pub fn execve(path: *const u8, argv: *const ?*const u8, envp: *const ?*const u8) usize {
return errnoWrap(c.execve(path, argv, envp));
}
@@ -405,19 +405,19 @@ pub fn dup2(old: i32, new: i32) usize {
return errnoWrap(c.dup2(old, new));
}
-pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize {
+pub fn readlink(noalias path: *const u8, noalias buf_ptr: *u8, buf_len: usize) usize {
return errnoWrap(c.readlink(path, buf_ptr, buf_len));
}
-pub fn gettimeofday(tv: ?&timeval, tz: ?&timezone) usize {
+pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) usize {
return errnoWrap(c.gettimeofday(tv, tz));
}
-pub fn nanosleep(req: &const timespec, rem: ?×pec) usize {
+pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
return errnoWrap(c.nanosleep(req, rem));
}
-pub fn realpath(noalias filename: &const u8, noalias resolved_name: &u8) usize {
+pub fn realpath(noalias filename: *const u8, noalias resolved_name: *u8) usize {
return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
}
@@ -429,11 +429,11 @@ pub fn setregid(rgid: u32, egid: u32) usize {
return errnoWrap(c.setregid(rgid, egid));
}
-pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize {
+pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
return errnoWrap(c.sigprocmask(@bitCast(c_int, flags), set, oldset));
}
-pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
+pub fn sigaction(sig: u5, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize {
assert(sig != SIGKILL);
assert(sig != SIGSTOP);
var cact = c.Sigaction{
@@ -442,7 +442,7 @@ pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigacti
.sa_mask = act.mask,
};
var coact: c.Sigaction = undefined;
- const result = errnoWrap(c.sigaction(sig, &cact, &coact));
+ const result = errnoWrap(c.sigaction(sig, *cact, *coact));
if (result != 0) {
return result;
}
@@ -473,7 +473,7 @@ pub const Sigaction = struct {
flags: u32,
};
-pub fn sigaddset(set: &sigset_t, signo: u5) void {
+pub fn sigaddset(set: *sigset_t, signo: u5) void {
set.* |= u32(1) << (signo - 1);
}
diff --git a/std/os/file.zig b/std/os/file.zig
index c07e2c5c8b..d943da30ca 100644
--- a/std/os/file.zig
+++ b/std/os/file.zig
@@ -19,7 +19,7 @@ pub const File = struct {
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
/// Call close to clean up.
- pub fn openRead(allocator: &mem.Allocator, path: []const u8) OpenError!File {
+ pub fn openRead(allocator: *mem.Allocator, path: []const u8) OpenError!File {
if (is_posix) {
const flags = posix.O_LARGEFILE | posix.O_RDONLY;
const fd = try os.posixOpen(allocator, path, flags, 0);
@@ -40,7 +40,7 @@ pub const File = struct {
}
/// Calls `openWriteMode` with os.default_file_mode for the mode.
- pub fn openWrite(allocator: &mem.Allocator, path: []const u8) OpenError!File {
+ pub fn openWrite(allocator: *mem.Allocator, path: []const u8) OpenError!File {
return openWriteMode(allocator, path, os.default_file_mode);
}
@@ -48,7 +48,7 @@ pub const File = struct {
/// If a file already exists in the destination it will be truncated.
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
/// Call close to clean up.
- pub fn openWriteMode(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
+ pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
if (is_posix) {
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
const fd = try os.posixOpen(allocator, path, flags, file_mode);
@@ -72,7 +72,7 @@ pub const File = struct {
/// If a file already exists in the destination this returns OpenError.PathAlreadyExists
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
/// Call close to clean up.
- pub fn openWriteNoClobber(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
+ pub fn openWriteNoClobber(allocator: *mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
if (is_posix) {
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_EXCL;
const fd = try os.posixOpen(allocator, path, flags, file_mode);
@@ -96,7 +96,7 @@ pub const File = struct {
return File{ .handle = handle };
}
- pub fn access(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) !bool {
+ pub fn access(allocator: *mem.Allocator, path: []const u8, file_mode: os.FileMode) !bool {
const path_with_null = try std.cstr.addNullByte(allocator, path);
defer allocator.free(path_with_null);
@@ -140,17 +140,17 @@ pub const File = struct {
/// Upon success, the stream is in an uninitialized state. To continue using it,
/// you must use the open() function.
- pub fn close(self: &File) void {
+ pub fn close(self: *File) void {
os.close(self.handle);
self.handle = undefined;
}
/// Calls `os.isTty` on `self.handle`.
- pub fn isTty(self: &File) bool {
+ pub fn isTty(self: *File) bool {
return os.isTty(self.handle);
}
- pub fn seekForward(self: &File, amount: isize) !void {
+ pub fn seekForward(self: *File, amount: isize) !void {
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios => {
const result = posix.lseek(self.handle, amount, posix.SEEK_CUR);
@@ -179,7 +179,7 @@ pub const File = struct {
}
}
- pub fn seekTo(self: &File, pos: usize) !void {
+ pub fn seekTo(self: *File, pos: usize) !void {
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios => {
const ipos = try math.cast(isize, pos);
@@ -210,7 +210,7 @@ pub const File = struct {
}
}
- pub fn getPos(self: &File) !usize {
+ pub fn getPos(self: *File) !usize {
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios => {
const result = posix.lseek(self.handle, 0, posix.SEEK_CUR);
@@ -229,7 +229,7 @@ pub const File = struct {
},
Os.windows => {
var pos: windows.LARGE_INTEGER = undefined;
- if (windows.SetFilePointerEx(self.handle, 0, &pos, windows.FILE_CURRENT) == 0) {
+ if (windows.SetFilePointerEx(self.handle, 0, *pos, windows.FILE_CURRENT) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.INVALID_PARAMETER => error.BadFd,
@@ -250,7 +250,7 @@ pub const File = struct {
}
}
- pub fn getEndPos(self: &File) !usize {
+ pub fn getEndPos(self: *File) !usize {
if (is_posix) {
var stat: posix.Stat = undefined;
const err = posix.getErrno(posix.fstat(self.handle, &stat));
@@ -285,7 +285,7 @@ pub const File = struct {
Unexpected,
};
- fn mode(self: &File) ModeError!os.FileMode {
+ fn mode(self: *File) ModeError!os.FileMode {
if (is_posix) {
var stat: posix.Stat = undefined;
const err = posix.getErrno(posix.fstat(self.handle, &stat));
@@ -309,7 +309,7 @@ pub const File = struct {
pub const ReadError = error{};
- pub fn read(self: &File, buffer: []u8) !usize {
+ pub fn read(self: *File, buffer: []u8) !usize {
if (is_posix) {
var index: usize = 0;
while (index < buffer.len) {
@@ -334,7 +334,7 @@ pub const File = struct {
while (index < buffer.len) {
const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
var amt_read: windows.DWORD = undefined;
- if (windows.ReadFile(self.handle, @ptrCast(&c_void, &buffer[index]), want_read_count, &amt_read, null) == 0) {
+ if (windows.ReadFile(self.handle, @ptrCast(*c_void, &buffer[index]), want_read_count, &amt_read, null) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.OPERATION_ABORTED => continue,
@@ -353,7 +353,7 @@ pub const File = struct {
pub const WriteError = os.WindowsWriteError || os.PosixWriteError;
- fn write(self: &File, bytes: []const u8) WriteError!void {
+ fn write(self: *File, bytes: []const u8) WriteError!void {
if (is_posix) {
try os.posixWrite(self.handle, bytes);
} else if (is_windows) {
diff --git a/std/os/get_user_id.zig b/std/os/get_user_id.zig
index 2a15e1d495..c0c1b1cc4b 100644
--- a/std/os/get_user_id.zig
+++ b/std/os/get_user_id.zig
@@ -77,8 +77,8 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
'0'...'9' => byte - '0',
else => return error.CorruptPasswordFile,
};
- if (@mulWithOverflow(u32, uid, 10, &uid)) return error.CorruptPasswordFile;
- if (@addWithOverflow(u32, uid, digit, &uid)) return error.CorruptPasswordFile;
+ if (@mulWithOverflow(u32, uid, 10, *uid)) return error.CorruptPasswordFile;
+ if (@addWithOverflow(u32, uid, digit, *uid)) return error.CorruptPasswordFile;
},
},
State.ReadGroupId => switch (byte) {
@@ -93,8 +93,8 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
'0'...'9' => byte - '0',
else => return error.CorruptPasswordFile,
};
- if (@mulWithOverflow(u32, gid, 10, &gid)) return error.CorruptPasswordFile;
- if (@addWithOverflow(u32, gid, digit, &gid)) return error.CorruptPasswordFile;
+ if (@mulWithOverflow(u32, gid, 10, *gid)) return error.CorruptPasswordFile;
+ if (@addWithOverflow(u32, gid, digit, *gid)) return error.CorruptPasswordFile;
},
},
}
diff --git a/std/os/index.zig b/std/os/index.zig
index 70e654bcd9..ff638c670b 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -321,14 +321,14 @@ pub const PosixOpenError = error{
/// ::file_path needs to be copied in memory to add a null terminating byte.
/// Calls POSIX open, keeps trying if it gets interrupted, and translates
/// the return value into zig errors.
-pub fn posixOpen(allocator: &Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 {
+pub fn posixOpen(allocator: *Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 {
const path_with_null = try cstr.addNullByte(allocator, file_path);
defer allocator.free(path_with_null);
return posixOpenC(path_with_null.ptr, flags, perm);
}
-pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) !i32 {
+pub fn posixOpenC(file_path: *const u8, flags: u32, perm: usize) !i32 {
while (true) {
const result = posix.open(file_path, flags, perm);
const err = posix.getErrno(result);
@@ -374,10 +374,10 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) !void {
}
}
-pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap) ![]?&u8 {
+pub fn createNullDelimitedEnvMap(allocator: *Allocator, env_map: *const BufMap) ![]?*u8 {
const envp_count = env_map.count();
- const envp_buf = try allocator.alloc(?&u8, envp_count + 1);
- mem.set(?&u8, envp_buf, null);
+ const envp_buf = try allocator.alloc(?*u8, envp_count + 1);
+ mem.set(?*u8, envp_buf, null);
errdefer freeNullDelimitedEnvMap(allocator, envp_buf);
{
var it = env_map.iterator();
@@ -397,7 +397,7 @@ pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap)
return envp_buf;
}
-pub fn freeNullDelimitedEnvMap(allocator: &Allocator, envp_buf: []?&u8) void {
+pub fn freeNullDelimitedEnvMap(allocator: *Allocator, envp_buf: []?*u8) void {
for (envp_buf) |env| {
const env_buf = if (env) |ptr| ptr[0 .. cstr.len(ptr) + 1] else break;
allocator.free(env_buf);
@@ -410,9 +410,9 @@ pub fn freeNullDelimitedEnvMap(allocator: &Allocator, envp_buf: []?&u8) void {
/// pointers after the args and after the environment variables.
/// `argv[0]` is the executable path.
/// This function also uses the PATH environment variable to get the full path to the executable.
-pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, allocator: &Allocator) !void {
- const argv_buf = try allocator.alloc(?&u8, argv.len + 1);
- mem.set(?&u8, argv_buf, null);
+pub fn posixExecve(argv: []const []const u8, env_map: *const BufMap, allocator: *Allocator) !void {
+ const argv_buf = try allocator.alloc(?*u8, argv.len + 1);
+ mem.set(?*u8, argv_buf, null);
defer {
for (argv_buf) |arg| {
const arg_buf = if (arg) |ptr| cstr.toSlice(ptr) else break;
@@ -494,10 +494,10 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
}
pub var linux_aux_raw = []usize{0} ** 38;
-pub var posix_environ_raw: []&u8 = undefined;
+pub var posix_environ_raw: []*u8 = undefined;
/// Caller must free result when done.
-pub fn getEnvMap(allocator: &Allocator) !BufMap {
+pub fn getEnvMap(allocator: *Allocator) !BufMap {
var result = BufMap.init(allocator);
errdefer result.deinit();
@@ -557,7 +557,7 @@ pub fn getEnvPosix(key: []const u8) ?[]const u8 {
}
/// Caller must free returned memory.
-pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) ![]u8 {
+pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) ![]u8 {
if (is_windows) {
const key_with_null = try cstr.addNullByte(allocator, key);
defer allocator.free(key_with_null);
@@ -591,7 +591,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) ![]u8 {
}
/// Caller must free the returned memory.
-pub fn getCwd(allocator: &Allocator) ![]u8 {
+pub fn getCwd(allocator: *Allocator) ![]u8 {
switch (builtin.os) {
Os.windows => {
var buf = try allocator.alloc(u8, 256);
@@ -640,7 +640,7 @@ test "os.getCwd" {
pub const SymLinkError = PosixSymLinkError || WindowsSymLinkError;
-pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) SymLinkError!void {
+pub fn symLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) SymLinkError!void {
if (is_windows) {
return symLinkWindows(allocator, existing_path, new_path);
} else {
@@ -653,7 +653,7 @@ pub const WindowsSymLinkError = error{
Unexpected,
};
-pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) WindowsSymLinkError!void {
+pub fn symLinkWindows(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) WindowsSymLinkError!void {
const existing_with_null = try cstr.addNullByte(allocator, existing_path);
defer allocator.free(existing_with_null);
const new_with_null = try cstr.addNullByte(allocator, new_path);
@@ -683,7 +683,7 @@ pub const PosixSymLinkError = error{
Unexpected,
};
-pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) PosixSymLinkError!void {
+pub fn symLinkPosix(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) PosixSymLinkError!void {
const full_buf = try allocator.alloc(u8, existing_path.len + new_path.len + 2);
defer allocator.free(full_buf);
@@ -718,7 +718,7 @@ pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path:
// here we replace the standard +/ with -_ so that it can be used in a file name
const b64_fs_encoder = base64.Base64Encoder.init("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", base64.standard_pad_char);
-pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) !void {
+pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) !void {
if (symLink(allocator, existing_path, new_path)) {
return;
} else |err| switch (err) {
@@ -746,7 +746,7 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
}
}
-pub fn deleteFile(allocator: &Allocator, file_path: []const u8) !void {
+pub fn deleteFile(allocator: *Allocator, file_path: []const u8) !void {
if (builtin.os == Os.windows) {
return deleteFileWindows(allocator, file_path);
} else {
@@ -754,7 +754,7 @@ pub fn deleteFile(allocator: &Allocator, file_path: []const u8) !void {
}
}
-pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) !void {
+pub fn deleteFileWindows(allocator: *Allocator, file_path: []const u8) !void {
const buf = try allocator.alloc(u8, file_path.len + 1);
defer allocator.free(buf);
@@ -772,7 +772,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) !void {
}
}
-pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) !void {
+pub fn deleteFilePosix(allocator: *Allocator, file_path: []const u8) !void {
const buf = try allocator.alloc(u8, file_path.len + 1);
defer allocator.free(buf);
@@ -803,7 +803,7 @@ pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) !void {
/// there is a possibility of power loss or application termination leaving temporary files present
/// in the same directory as dest_path.
/// Destination file will have the same mode as the source file.
-pub fn copyFile(allocator: &Allocator, source_path: []const u8, dest_path: []const u8) !void {
+pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []const u8) !void {
var in_file = try os.File.openRead(allocator, source_path);
defer in_file.close();
@@ -825,7 +825,7 @@ pub fn copyFile(allocator: &Allocator, source_path: []const u8, dest_path: []con
/// Guaranteed to be atomic. However until https://patchwork.kernel.org/patch/9636735/ is
/// merged and readily available,
/// there is a possibility of power loss or application termination leaving temporary files present
-pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: []const u8, mode: FileMode) !void {
+pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: []const u8, mode: FileMode) !void {
var in_file = try os.File.openRead(allocator, source_path);
defer in_file.close();
@@ -843,7 +843,7 @@ pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: [
}
pub const AtomicFile = struct {
- allocator: &Allocator,
+ allocator: *Allocator,
file: os.File,
tmp_path: []u8,
dest_path: []const u8,
@@ -851,7 +851,7 @@ pub const AtomicFile = struct {
/// dest_path must remain valid for the lifetime of AtomicFile
/// call finish to atomically replace dest_path with contents
- pub fn init(allocator: &Allocator, dest_path: []const u8, mode: FileMode) !AtomicFile {
+ pub fn init(allocator: *Allocator, dest_path: []const u8, mode: FileMode) !AtomicFile {
const dirname = os.path.dirname(dest_path);
var rand_buf: [12]u8 = undefined;
@@ -888,7 +888,7 @@ pub const AtomicFile = struct {
}
/// always call deinit, even after successful finish()
- pub fn deinit(self: &AtomicFile) void {
+ pub fn deinit(self: *AtomicFile) void {
if (!self.finished) {
self.file.close();
deleteFile(self.allocator, self.tmp_path) catch {};
@@ -897,7 +897,7 @@ pub const AtomicFile = struct {
}
}
- pub fn finish(self: &AtomicFile) !void {
+ pub fn finish(self: *AtomicFile) !void {
assert(!self.finished);
self.file.close();
try rename(self.allocator, self.tmp_path, self.dest_path);
@@ -906,7 +906,7 @@ pub const AtomicFile = struct {
}
};
-pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8) !void {
+pub fn rename(allocator: *Allocator, old_path: []const u8, new_path: []const u8) !void {
const full_buf = try allocator.alloc(u8, old_path.len + new_path.len + 2);
defer allocator.free(full_buf);
@@ -951,7 +951,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
}
}
-pub fn makeDir(allocator: &Allocator, dir_path: []const u8) !void {
+pub fn makeDir(allocator: *Allocator, dir_path: []const u8) !void {
if (is_windows) {
return makeDirWindows(allocator, dir_path);
} else {
@@ -959,7 +959,7 @@ pub fn makeDir(allocator: &Allocator, dir_path: []const u8) !void {
}
}
-pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) !void {
+pub fn makeDirWindows(allocator: *Allocator, dir_path: []const u8) !void {
const path_buf = try cstr.addNullByte(allocator, dir_path);
defer allocator.free(path_buf);
@@ -973,7 +973,7 @@ pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) !void {
}
}
-pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) !void {
+pub fn makeDirPosix(allocator: *Allocator, dir_path: []const u8) !void {
const path_buf = try cstr.addNullByte(allocator, dir_path);
defer allocator.free(path_buf);
@@ -999,7 +999,7 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) !void {
/// Calls makeDir recursively to make an entire path. Returns success if the path
/// already exists and is a directory.
-pub fn makePath(allocator: &Allocator, full_path: []const u8) !void {
+pub fn makePath(allocator: *Allocator, full_path: []const u8) !void {
const resolved_path = try path.resolve(allocator, full_path);
defer allocator.free(resolved_path);
@@ -1033,7 +1033,7 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) !void {
/// Returns ::error.DirNotEmpty if the directory is not empty.
/// To delete a directory recursively, see ::deleteTree
-pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) !void {
+pub fn deleteDir(allocator: *Allocator, dir_path: []const u8) !void {
const path_buf = try allocator.alloc(u8, dir_path.len + 1);
defer allocator.free(path_buf);
@@ -1084,7 +1084,7 @@ const DeleteTreeError = error{
DirNotEmpty,
Unexpected,
};
-pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!void {
+pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!void {
start_over: while (true) {
var got_access_denied = false;
// First, try deleting the item as a file. This way we don't follow sym links.
@@ -1153,7 +1153,7 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
pub const Dir = struct {
fd: i32,
darwin_seek: darwin_seek_t,
- allocator: &Allocator,
+ allocator: *Allocator,
buf: []u8,
index: usize,
end_index: usize,
@@ -1180,7 +1180,7 @@ pub const Dir = struct {
};
};
- pub fn open(allocator: &Allocator, dir_path: []const u8) !Dir {
+ pub fn open(allocator: *Allocator, dir_path: []const u8) !Dir {
const fd = switch (builtin.os) {
Os.windows => @compileError("TODO support Dir.open for windows"),
Os.linux => try posixOpen(allocator, dir_path, posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, 0),
@@ -1206,14 +1206,14 @@ pub const Dir = struct {
};
}
- pub fn close(self: &Dir) void {
+ pub fn close(self: *Dir) void {
self.allocator.free(self.buf);
os.close(self.fd);
}
/// Memory such as file names referenced in this returned entry becomes invalid
/// with subsequent calls to next, as well as when this ::Dir is deinitialized.
- pub fn next(self: &Dir) !?Entry {
+ pub fn next(self: *Dir) !?Entry {
switch (builtin.os) {
Os.linux => return self.nextLinux(),
Os.macosx, Os.ios => return self.nextDarwin(),
@@ -1222,7 +1222,7 @@ pub const Dir = struct {
}
}
- fn nextDarwin(self: &Dir) !?Entry {
+ fn nextDarwin(self: *Dir) !?Entry {
start_over: while (true) {
if (self.index >= self.end_index) {
if (self.buf.len == 0) {
@@ -1248,7 +1248,7 @@ pub const Dir = struct {
break;
}
}
- const darwin_entry = @ptrCast(&align(1) posix.dirent, &self.buf[self.index]);
+ const darwin_entry = @ptrCast(*align(1) posix.dirent, &self.buf[self.index]);
const next_index = self.index + darwin_entry.d_reclen;
self.index = next_index;
@@ -1277,11 +1277,11 @@ pub const Dir = struct {
}
}
- fn nextWindows(self: &Dir) !?Entry {
+ fn nextWindows(self: *Dir) !?Entry {
@compileError("TODO support Dir.next for windows");
}
- fn nextLinux(self: &Dir) !?Entry {
+ fn nextLinux(self: *Dir) !?Entry {
start_over: while (true) {
if (self.index >= self.end_index) {
if (self.buf.len == 0) {
@@ -1307,7 +1307,7 @@ pub const Dir = struct {
break;
}
}
- const linux_entry = @ptrCast(&align(1) posix.dirent, &self.buf[self.index]);
+ const linux_entry = @ptrCast(*align(1) posix.dirent, &self.buf[self.index]);
const next_index = self.index + linux_entry.d_reclen;
self.index = next_index;
@@ -1337,7 +1337,7 @@ pub const Dir = struct {
}
};
-pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) !void {
+pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void {
const path_buf = try allocator.alloc(u8, dir_path.len + 1);
defer allocator.free(path_buf);
@@ -1361,7 +1361,7 @@ pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) !void {
}
/// Read value of a symbolic link.
-pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 {
+pub fn readLink(allocator: *Allocator, pathname: []const u8) ![]u8 {
const path_buf = try allocator.alloc(u8, pathname.len + 1);
defer allocator.free(path_buf);
@@ -1468,7 +1468,7 @@ pub const ArgIteratorPosix = struct {
};
}
- pub fn next(self: &ArgIteratorPosix) ?[]const u8 {
+ pub fn next(self: *ArgIteratorPosix) ?[]const u8 {
if (self.index == self.count) return null;
const s = raw[self.index];
@@ -1476,7 +1476,7 @@ pub const ArgIteratorPosix = struct {
return cstr.toSlice(s);
}
- pub fn skip(self: &ArgIteratorPosix) bool {
+ pub fn skip(self: *ArgIteratorPosix) bool {
if (self.index == self.count) return false;
self.index += 1;
@@ -1485,12 +1485,12 @@ pub const ArgIteratorPosix = struct {
/// This is marked as public but actually it's only meant to be used
/// internally by zig's startup code.
- pub var raw: []&u8 = undefined;
+ pub var raw: []*u8 = undefined;
};
pub const ArgIteratorWindows = struct {
index: usize,
- cmd_line: &const u8,
+ cmd_line: *const u8,
in_quote: bool,
quote_count: usize,
seen_quote_count: usize,
@@ -1501,7 +1501,7 @@ pub const ArgIteratorWindows = struct {
return initWithCmdLine(windows.GetCommandLineA());
}
- pub fn initWithCmdLine(cmd_line: &const u8) ArgIteratorWindows {
+ pub fn initWithCmdLine(cmd_line: *const u8) ArgIteratorWindows {
return ArgIteratorWindows{
.index = 0,
.cmd_line = cmd_line,
@@ -1512,7 +1512,7 @@ pub const ArgIteratorWindows = struct {
}
/// You must free the returned memory when done.
- pub fn next(self: &ArgIteratorWindows, allocator: &Allocator) ?(NextError![]u8) {
+ pub fn next(self: *ArgIteratorWindows, allocator: *Allocator) ?(NextError![]u8) {
// march forward over whitespace
while (true) : (self.index += 1) {
const byte = self.cmd_line[self.index];
@@ -1526,7 +1526,7 @@ pub const ArgIteratorWindows = struct {
return self.internalNext(allocator);
}
- pub fn skip(self: &ArgIteratorWindows) bool {
+ pub fn skip(self: *ArgIteratorWindows) bool {
// march forward over whitespace
while (true) : (self.index += 1) {
const byte = self.cmd_line[self.index];
@@ -1565,7 +1565,7 @@ pub const ArgIteratorWindows = struct {
}
}
- fn internalNext(self: &ArgIteratorWindows, allocator: &Allocator) NextError![]u8 {
+ fn internalNext(self: *ArgIteratorWindows, allocator: *Allocator) NextError![]u8 {
var buf = try Buffer.initSize(allocator, 0);
defer buf.deinit();
@@ -1609,14 +1609,14 @@ pub const ArgIteratorWindows = struct {
}
}
- fn emitBackslashes(self: &ArgIteratorWindows, buf: &Buffer, emit_count: usize) !void {
+ fn emitBackslashes(self: *ArgIteratorWindows, buf: *Buffer, emit_count: usize) !void {
var i: usize = 0;
while (i < emit_count) : (i += 1) {
try buf.appendByte('\\');
}
}
- fn countQuotes(cmd_line: &const u8) usize {
+ fn countQuotes(cmd_line: *const u8) usize {
var result: usize = 0;
var backslash_count: usize = 0;
var index: usize = 0;
@@ -1649,7 +1649,7 @@ pub const ArgIterator = struct {
pub const NextError = ArgIteratorWindows.NextError;
/// You must free the returned memory when done.
- pub fn next(self: &ArgIterator, allocator: &Allocator) ?(NextError![]u8) {
+ pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) {
if (builtin.os == Os.windows) {
return self.inner.next(allocator);
} else {
@@ -1658,13 +1658,13 @@ pub const ArgIterator = struct {
}
/// If you only are targeting posix you can call this and not need an allocator.
- pub fn nextPosix(self: &ArgIterator) ?[]const u8 {
+ pub fn nextPosix(self: *ArgIterator) ?[]const u8 {
return self.inner.next();
}
/// Parse past 1 argument without capturing it.
/// Returns `true` if skipped an arg, `false` if we are at the end.
- pub fn skip(self: &ArgIterator) bool {
+ pub fn skip(self: *ArgIterator) bool {
return self.inner.skip();
}
};
@@ -1674,7 +1674,7 @@ pub fn args() ArgIterator {
}
/// Caller must call freeArgs on result.
-pub fn argsAlloc(allocator: &mem.Allocator) ![]const []u8 {
+pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 {
// TODO refactor to only make 1 allocation.
var it = args();
var contents = try Buffer.initSize(allocator, 0);
@@ -1711,12 +1711,12 @@ pub fn argsAlloc(allocator: &mem.Allocator) ![]const []u8 {
return result_slice_list;
}
-pub fn argsFree(allocator: &mem.Allocator, args_alloc: []const []u8) void {
+pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void {
var total_bytes: usize = 0;
for (args_alloc) |arg| {
total_bytes += @sizeOf([]u8) + arg.len;
}
- const unaligned_allocated_buf = @ptrCast(&const u8, args_alloc.ptr)[0..total_bytes];
+ const unaligned_allocated_buf = @ptrCast(*const u8, args_alloc.ptr)[0..total_bytes];
const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf);
return allocator.free(aligned_allocated_buf);
}
@@ -1765,7 +1765,7 @@ test "windows arg parsing" {
});
}
-fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const u8) void {
+fn testWindowsCmdLine(input_cmd_line: *const u8, expected_args: []const []const u8) void {
var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);
for (expected_args) |expected_arg| {
const arg = ??it.next(debug.global_allocator) catch unreachable;
@@ -1832,7 +1832,7 @@ test "openSelfExe" {
/// This function may return an error if the current executable
/// was deleted after spawning.
/// Caller owns returned memory.
-pub fn selfExePath(allocator: &mem.Allocator) ![]u8 {
+pub fn selfExePath(allocator: *mem.Allocator) ![]u8 {
switch (builtin.os) {
Os.linux => {
// If the currently executing binary has been deleted,
@@ -1875,7 +1875,7 @@ pub fn selfExePath(allocator: &mem.Allocator) ![]u8 {
/// Get the directory path that contains the current executable.
/// Caller owns returned memory.
-pub fn selfExeDirPath(allocator: &mem.Allocator) ![]u8 {
+pub fn selfExeDirPath(allocator: *mem.Allocator) ![]u8 {
switch (builtin.os) {
Os.linux => {
// If the currently executing binary has been deleted,
@@ -2001,7 +2001,7 @@ pub const PosixBindError = error{
};
/// addr is `&const T` where T is one of the sockaddr
-pub fn posixBind(fd: i32, addr: &const posix.sockaddr) PosixBindError!void {
+pub fn posixBind(fd: i32, addr: *const posix.sockaddr) PosixBindError!void {
const rc = posix.bind(fd, addr, @sizeOf(posix.sockaddr));
const err = posix.getErrno(rc);
switch (err) {
@@ -2096,7 +2096,7 @@ pub const PosixAcceptError = error{
Unexpected,
};
-pub fn posixAccept(fd: i32, addr: &posix.sockaddr, flags: u32) PosixAcceptError!i32 {
+pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!i32 {
while (true) {
var sockaddr_size = u32(@sizeOf(posix.sockaddr));
const rc = posix.accept4(fd, addr, &sockaddr_size, flags);
@@ -2195,7 +2195,7 @@ pub const LinuxEpollCtlError = error{
Unexpected,
};
-pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: &linux.epoll_event) LinuxEpollCtlError!void {
+pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: *linux.epoll_event) LinuxEpollCtlError!void {
const rc = posix.epoll_ctl(epfd, op, fd, event);
const err = posix.getErrno(rc);
switch (err) {
@@ -2288,7 +2288,7 @@ pub const PosixConnectError = error{
Unexpected,
};
-pub fn posixConnect(sockfd: i32, sockaddr: &const posix.sockaddr) PosixConnectError!void {
+pub fn posixConnect(sockfd: i32, sockaddr: *const posix.sockaddr) PosixConnectError!void {
while (true) {
const rc = posix.connect(sockfd, sockaddr, @sizeOf(posix.sockaddr));
const err = posix.getErrno(rc);
@@ -2319,7 +2319,7 @@ pub fn posixConnect(sockfd: i32, sockaddr: &const posix.sockaddr) PosixConnectEr
/// Same as posixConnect except it is for blocking socket file descriptors.
/// It expects to receive EINPROGRESS.
-pub fn posixConnectAsync(sockfd: i32, sockaddr: &const posix.sockaddr) PosixConnectError!void {
+pub fn posixConnectAsync(sockfd: i32, sockaddr: *const posix.sockaddr) PosixConnectError!void {
while (true) {
const rc = posix.connect(sockfd, sockaddr, @sizeOf(posix.sockaddr));
const err = posix.getErrno(rc);
@@ -2350,7 +2350,7 @@ pub fn posixConnectAsync(sockfd: i32, sockaddr: &const posix.sockaddr) PosixConn
pub fn posixGetSockOptConnectError(sockfd: i32) PosixConnectError!void {
var err_code: i32 = undefined;
var size: u32 = @sizeOf(i32);
- const rc = posix.getsockopt(sockfd, posix.SOL_SOCKET, posix.SO_ERROR, @ptrCast(&u8, &err_code), &size);
+ const rc = posix.getsockopt(sockfd, posix.SOL_SOCKET, posix.SO_ERROR, @ptrCast(*u8, &err_code), &size);
assert(size == 4);
const err = posix.getErrno(rc);
switch (err) {
@@ -2401,13 +2401,13 @@ pub const Thread = struct {
},
builtin.Os.windows => struct {
handle: windows.HANDLE,
- alloc_start: &c_void,
+ alloc_start: *c_void,
heap_handle: windows.HANDLE,
},
else => @compileError("Unsupported OS"),
};
- pub fn wait(self: &const Thread) void {
+ pub fn wait(self: *const Thread) void {
if (use_pthreads) {
const err = c.pthread_join(self.data.handle, null);
switch (err) {
@@ -2473,7 +2473,7 @@ pub const SpawnThreadError = error{
/// fn startFn(@typeOf(context)) T
/// where T is u8, noreturn, void, or !void
/// caller must call wait on the returned thread
-pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread {
+pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread {
// TODO compile-time call graph analysis to determine stack upper bound
// https://github.com/ziglang/zig/issues/157
const default_stack_size = 8 * 1024 * 1024;
@@ -2491,7 +2491,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
if (@sizeOf(Context) == 0) {
return startFn({});
} else {
- return startFn(@ptrCast(&Context, @alignCast(@alignOf(Context), arg)).*);
+ return startFn(@ptrCast(*Context, @alignCast(@alignOf(Context), arg)).*);
}
}
};
@@ -2500,13 +2500,13 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
const byte_count = @alignOf(WinThread.OuterContext) + @sizeOf(WinThread.OuterContext);
const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) ?? return SpawnThreadError.OutOfMemory;
errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
- const bytes = @ptrCast(&u8, bytes_ptr)[0..byte_count];
+ const bytes = @ptrCast(*u8, bytes_ptr)[0..byte_count];
const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable;
outer_context.inner = context;
outer_context.thread.data.heap_handle = heap_handle;
outer_context.thread.data.alloc_start = bytes_ptr;
- const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(&c_void, &outer_context.inner);
+ const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(*c_void, &outer_context.inner);
outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) ?? {
const err = windows.GetLastError();
return switch (err) {
@@ -2521,15 +2521,15 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
if (@sizeOf(Context) == 0) {
return startFn({});
} else {
- return startFn(@intToPtr(&const Context, ctx_addr).*);
+ return startFn(@intToPtr(*const Context, ctx_addr).*);
}
}
- extern fn posixThreadMain(ctx: ?&c_void) ?&c_void {
+ extern fn posixThreadMain(ctx: ?*c_void) ?*c_void {
if (@sizeOf(Context) == 0) {
_ = startFn({});
return null;
} else {
- _ = startFn(@ptrCast(&const Context, @alignCast(@alignOf(Context), ctx)).*);
+ _ = startFn(@ptrCast(*const Context, @alignCast(@alignOf(Context), ctx)).*);
return null;
}
}
@@ -2548,7 +2548,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
stack_end -= @sizeOf(Context);
stack_end -= stack_end % @alignOf(Context);
assert(stack_end >= stack_addr);
- const context_ptr = @alignCast(@alignOf(Context), @intToPtr(&Context, stack_end));
+ const context_ptr = @alignCast(@alignOf(Context), @intToPtr(*Context, stack_end));
context_ptr.* = context;
arg = stack_end;
}
@@ -2556,7 +2556,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
stack_end -= @sizeOf(Thread);
stack_end -= stack_end % @alignOf(Thread);
assert(stack_end >= stack_addr);
- const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(&Thread, stack_end));
+ const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, stack_end));
thread_ptr.data.stack_addr = stack_addr;
thread_ptr.data.stack_len = mmap_len;
@@ -2572,9 +2572,9 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
// align to page
stack_end -= stack_end % os.page_size;
- assert(c.pthread_attr_setstack(&attr, @intToPtr(&c_void, stack_addr), stack_end - stack_addr) == 0);
+ assert(c.pthread_attr_setstack(&attr, @intToPtr(*c_void, stack_addr), stack_end - stack_addr) == 0);
- const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(&c_void, arg));
+ const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg));
switch (err) {
0 => return thread_ptr,
posix.EAGAIN => return SpawnThreadError.SystemResources,
diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig
index 5186ff32d3..3e7b836ac7 100644
--- a/std/os/linux/index.zig
+++ b/std/os/linux/index.zig
@@ -665,15 +665,15 @@ pub fn dup2(old: i32, new: i32) usize {
return syscall2(SYS_dup2, usize(old), usize(new));
}
-pub fn chdir(path: &const u8) usize {
+pub fn chdir(path: *const u8) usize {
return syscall1(SYS_chdir, @ptrToInt(path));
}
-pub fn chroot(path: &const u8) usize {
+pub fn chroot(path: *const u8) usize {
return syscall1(SYS_chroot, @ptrToInt(path));
}
-pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) usize {
+pub fn execve(path: *const u8, argv: *const ?*const u8, envp: *const ?*const u8) usize {
return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));
}
@@ -681,15 +681,15 @@ pub fn fork() usize {
return syscall0(SYS_fork);
}
-pub fn futex_wait(uaddr: usize, futex_op: u32, val: i32, timeout: ?×pec) usize {
+pub fn futex_wait(uaddr: usize, futex_op: u32, val: i32, timeout: ?*timespec) usize {
return syscall4(SYS_futex, uaddr, futex_op, @bitCast(u32, val), @ptrToInt(timeout));
}
-pub fn getcwd(buf: &u8, size: usize) usize {
+pub fn getcwd(buf: *u8, size: usize) usize {
return syscall2(SYS_getcwd, @ptrToInt(buf), size);
}
-pub fn getdents(fd: i32, dirp: &u8, count: usize) usize {
+pub fn getdents(fd: i32, dirp: *u8, count: usize) usize {
return syscall3(SYS_getdents, usize(fd), @ptrToInt(dirp), count);
}
@@ -698,27 +698,27 @@ pub fn isatty(fd: i32) bool {
return syscall3(SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
}
-pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) usize {
+pub fn readlink(noalias path: *const u8, noalias buf_ptr: *u8, buf_len: usize) usize {
return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
}
-pub fn mkdir(path: &const u8, mode: u32) usize {
+pub fn mkdir(path: *const u8, mode: u32) usize {
return syscall2(SYS_mkdir, @ptrToInt(path), mode);
}
-pub fn mount(special: &const u8, dir: &const u8, fstype: &const u8, flags: usize, data: usize) usize {
+pub fn mount(special: *const u8, dir: *const u8, fstype: *const u8, flags: usize, data: usize) usize {
return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
}
-pub fn umount(special: &const u8) usize {
+pub fn umount(special: *const u8) usize {
return syscall2(SYS_umount2, @ptrToInt(special), 0);
}
-pub fn umount2(special: &const u8, flags: u32) usize {
+pub fn umount2(special: *const u8, flags: u32) usize {
return syscall2(SYS_umount2, @ptrToInt(special), flags);
}
-pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
+pub fn mmap(address: ?*u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), @bitCast(usize, offset));
}
@@ -726,60 +726,60 @@ pub fn munmap(address: usize, length: usize) usize {
return syscall2(SYS_munmap, address, length);
}
-pub fn read(fd: i32, buf: &u8, count: usize) usize {
+pub fn read(fd: i32, buf: *u8, count: usize) usize {
return syscall3(SYS_read, usize(fd), @ptrToInt(buf), count);
}
-pub fn rmdir(path: &const u8) usize {
+pub fn rmdir(path: *const u8) usize {
return syscall1(SYS_rmdir, @ptrToInt(path));
}
-pub fn symlink(existing: &const u8, new: &const u8) usize {
+pub fn symlink(existing: *const u8, new: *const u8) usize {
return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
}
-pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize {
+pub fn pread(fd: i32, buf: *u8, count: usize, offset: usize) usize {
return syscall4(SYS_pread, usize(fd), @ptrToInt(buf), count, offset);
}
-pub fn access(path: &const u8, mode: u32) usize {
+pub fn access(path: *const u8, mode: u32) usize {
return syscall2(SYS_access, @ptrToInt(path), mode);
}
-pub fn pipe(fd: &[2]i32) usize {
+pub fn pipe(fd: *[2]i32) usize {
return pipe2(fd, 0);
}
-pub fn pipe2(fd: &[2]i32, flags: usize) usize {
+pub fn pipe2(fd: *[2]i32, flags: usize) usize {
return syscall2(SYS_pipe2, @ptrToInt(fd), flags);
}
-pub fn write(fd: i32, buf: &const u8, count: usize) usize {
+pub fn write(fd: i32, buf: *const u8, count: usize) usize {
return syscall3(SYS_write, usize(fd), @ptrToInt(buf), count);
}
-pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) usize {
+pub fn pwrite(fd: i32, buf: *const u8, count: usize, offset: usize) usize {
return syscall4(SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset);
}
-pub fn rename(old: &const u8, new: &const u8) usize {
+pub fn rename(old: *const u8, new: *const u8) usize {
return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new));
}
-pub fn open(path: &const u8, flags: u32, perm: usize) usize {
+pub fn open(path: *const u8, flags: u32, perm: usize) usize {
return syscall3(SYS_open, @ptrToInt(path), flags, perm);
}
-pub fn create(path: &const u8, perm: usize) usize {
+pub fn create(path: *const u8, perm: usize) usize {
return syscall2(SYS_creat, @ptrToInt(path), perm);
}
-pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize {
+pub fn openat(dirfd: i32, path: *const u8, flags: usize, mode: usize) usize {
return syscall4(SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode);
}
/// See also `clone` (from the arch-specific include)
-pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: &i32, child_tid: &i32, newtls: usize) usize {
+pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize {
return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls);
}
@@ -801,7 +801,7 @@ pub fn exit(status: i32) noreturn {
unreachable;
}
-pub fn getrandom(buf: &u8, count: usize, flags: u32) usize {
+pub fn getrandom(buf: *u8, count: usize, flags: u32) usize {
return syscall3(SYS_getrandom, @ptrToInt(buf), count, usize(flags));
}
@@ -809,15 +809,15 @@ pub fn kill(pid: i32, sig: i32) usize {
return syscall2(SYS_kill, @bitCast(usize, isize(pid)), usize(sig));
}
-pub fn unlink(path: &const u8) usize {
+pub fn unlink(path: *const u8) usize {
return syscall1(SYS_unlink, @ptrToInt(path));
}
-pub fn waitpid(pid: i32, status: &i32, options: i32) usize {
+pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
}
-pub fn clock_gettime(clk_id: i32, tp: ×pec) usize {
+pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
if (VDSO_CGT_SYM.len != 0) {
const f = @atomicLoad(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, builtin.AtomicOrder.Unordered);
if (@ptrToInt(f) != 0) {
@@ -831,7 +831,7 @@ pub fn clock_gettime(clk_id: i32, tp: ×pec) usize {
return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
}
var vdso_clock_gettime = init_vdso_clock_gettime;
-extern fn init_vdso_clock_gettime(clk: i32, ts: ×pec) usize {
+extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize {
const addr = vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM);
var f = @intToPtr(@typeOf(init_vdso_clock_gettime), addr);
_ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f, builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic);
@@ -839,23 +839,23 @@ extern fn init_vdso_clock_gettime(clk: i32, ts: ×pec) usize {
return f(clk, ts);
}
-pub fn clock_getres(clk_id: i32, tp: ×pec) usize {
+pub fn clock_getres(clk_id: i32, tp: *timespec) usize {
return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
}
-pub fn clock_settime(clk_id: i32, tp: &const timespec) usize {
+pub fn clock_settime(clk_id: i32, tp: *const timespec) usize {
return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
}
-pub fn gettimeofday(tv: &timeval, tz: &timezone) usize {
+pub fn gettimeofday(tv: *timeval, tz: *timezone) usize {
return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz));
}
-pub fn settimeofday(tv: &const timeval, tz: &const timezone) usize {
+pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize {
return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz));
}
-pub fn nanosleep(req: &const timespec, rem: ?×pec) usize {
+pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
}
@@ -899,11 +899,11 @@ pub fn setegid(egid: u32) usize {
return syscall1(SYS_setegid, egid);
}
-pub fn getresuid(ruid: &u32, euid: &u32, suid: &u32) usize {
+pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize {
return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid));
}
-pub fn getresgid(rgid: &u32, egid: &u32, sgid: &u32) usize {
+pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize {
return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid));
}
@@ -915,11 +915,11 @@ pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize {
return syscall3(SYS_setresgid, rgid, egid, sgid);
}
-pub fn getgroups(size: usize, list: &u32) usize {
+pub fn getgroups(size: usize, list: *u32) usize {
return syscall2(SYS_getgroups, size, @ptrToInt(list));
}
-pub fn setgroups(size: usize, list: &const u32) usize {
+pub fn setgroups(size: usize, list: *const u32) usize {
return syscall2(SYS_setgroups, size, @ptrToInt(list));
}
@@ -927,11 +927,11 @@ pub fn getpid() i32 {
return @bitCast(i32, u32(syscall0(SYS_getpid)));
}
-pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize {
+pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
}
-pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
+pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize {
assert(sig >= 1);
assert(sig != SIGKILL);
assert(sig != SIGSTOP);
@@ -942,8 +942,8 @@ pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigacti
.restorer = @ptrCast(extern fn () void, restore_rt),
};
var ksa_old: k_sigaction = undefined;
- @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8);
- const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask)));
+ @memcpy(@ptrCast(*u8, *ksa.mask), @ptrCast(*const u8, *act.mask), 8);
+ const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(*ksa), @ptrToInt(*ksa_old), @sizeOf(@typeOf(ksa.mask)));
const err = getErrno(result);
if (err != 0) {
return result;
@@ -951,7 +951,7 @@ pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigacti
if (oact) |old| {
old.handler = ksa_old.handler;
old.flags = @truncate(u32, ksa_old.flags);
- @memcpy(@ptrCast(&u8, &old.mask), @ptrCast(&const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask)));
+ @memcpy(@ptrCast(*u8, *old.mask), @ptrCast(*const u8, *ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask)));
}
return 0;
}
@@ -989,24 +989,24 @@ pub fn raise(sig: i32) usize {
return ret;
}
-fn blockAllSignals(set: &sigset_t) void {
+fn blockAllSignals(set: *sigset_t) void {
_ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8);
}
-fn blockAppSignals(set: &sigset_t) void {
+fn blockAppSignals(set: *sigset_t) void {
_ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8);
}
-fn restoreSignals(set: &sigset_t) void {
+fn restoreSignals(set: *sigset_t) void {
_ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8);
}
-pub fn sigaddset(set: &sigset_t, sig: u6) void {
+pub fn sigaddset(set: *sigset_t, sig: u6) void {
const s = sig - 1;
(set.*)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
}
-pub fn sigismember(set: &const sigset_t, sig: u6) bool {
+pub fn sigismember(set: *const sigset_t, sig: u6) bool {
const s = sig - 1;
return ((set.*)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
}
@@ -1036,15 +1036,15 @@ pub const sockaddr_in6 = extern struct {
};
pub const iovec = extern struct {
- iov_base: &u8,
+ iov_base: *u8,
iov_len: usize,
};
-pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
+pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return syscall3(SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len));
}
-pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
+pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return syscall3(SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len));
}
@@ -1052,27 +1052,27 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
return syscall3(SYS_socket, domain, socket_type, protocol);
}
-pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: &const u8, optlen: socklen_t) usize {
+pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: *const u8, optlen: socklen_t) usize {
return syscall5(SYS_setsockopt, usize(fd), level, optname, usize(optval), @ptrToInt(optlen));
}
-pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: &u8, noalias optlen: &socklen_t) usize {
+pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: *u8, noalias optlen: *socklen_t) usize {
return syscall5(SYS_getsockopt, usize(fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
}
-pub fn sendmsg(fd: i32, msg: &const msghdr, flags: u32) usize {
+pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize {
return syscall3(SYS_sendmsg, usize(fd), @ptrToInt(msg), flags);
}
-pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) usize {
+pub fn connect(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
return syscall3(SYS_connect, usize(fd), @ptrToInt(addr), usize(len));
}
-pub fn recvmsg(fd: i32, msg: &msghdr, flags: u32) usize {
+pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
return syscall3(SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
}
-pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize {
+pub fn recvfrom(fd: i32, noalias buf: *u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
return syscall6(SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
}
@@ -1080,7 +1080,7 @@ pub fn shutdown(fd: i32, how: i32) usize {
return syscall2(SYS_shutdown, usize(fd), usize(how));
}
-pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) usize {
+pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
return syscall3(SYS_bind, usize(fd), @ptrToInt(addr), usize(len));
}
@@ -1088,79 +1088,79 @@ pub fn listen(fd: i32, backlog: u32) usize {
return syscall2(SYS_listen, usize(fd), backlog);
}
-pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) usize {
+pub fn sendto(fd: i32, buf: *const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
return syscall6(SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen));
}
pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
- return syscall4(SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0]));
+ return syscall4(SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(*fd[0]));
}
-pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) usize {
+pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return accept4(fd, addr, len, 0);
}
-pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) usize {
+pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
return syscall4(SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags);
}
-pub fn fstat(fd: i32, stat_buf: &Stat) usize {
+pub fn fstat(fd: i32, stat_buf: *Stat) usize {
return syscall2(SYS_fstat, usize(fd), @ptrToInt(stat_buf));
}
-pub fn stat(pathname: &const u8, statbuf: &Stat) usize {
+pub fn stat(pathname: *const u8, statbuf: *Stat) usize {
return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf));
}
-pub fn lstat(pathname: &const u8, statbuf: &Stat) usize {
+pub fn lstat(pathname: *const u8, statbuf: *Stat) usize {
return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf));
}
-pub fn listxattr(path: &const u8, list: &u8, size: usize) usize {
+pub fn listxattr(path: *const u8, list: *u8, size: usize) usize {
return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size);
}
-pub fn llistxattr(path: &const u8, list: &u8, size: usize) usize {
+pub fn llistxattr(path: *const u8, list: *u8, size: usize) usize {
return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size);
}
-pub fn flistxattr(fd: usize, list: &u8, size: usize) usize {
+pub fn flistxattr(fd: usize, list: *u8, size: usize) usize {
return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size);
}
-pub fn getxattr(path: &const u8, name: &const u8, value: &void, size: usize) usize {
+pub fn getxattr(path: *const u8, name: *const u8, value: *void, size: usize) usize {
return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size);
}
-pub fn lgetxattr(path: &const u8, name: &const u8, value: &void, size: usize) usize {
+pub fn lgetxattr(path: *const u8, name: *const u8, value: *void, size: usize) usize {
return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size);
}
-pub fn fgetxattr(fd: usize, name: &const u8, value: &void, size: usize) usize {
+pub fn fgetxattr(fd: usize, name: *const u8, value: *void, size: usize) usize {
return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size);
}
-pub fn setxattr(path: &const u8, name: &const u8, value: &const void, size: usize, flags: usize) usize {
+pub fn setxattr(path: *const u8, name: *const u8, value: *const void, size: usize, flags: usize) usize {
return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
}
-pub fn lsetxattr(path: &const u8, name: &const u8, value: &const void, size: usize, flags: usize) usize {
+pub fn lsetxattr(path: *const u8, name: *const u8, value: *const void, size: usize, flags: usize) usize {
return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
}
-pub fn fsetxattr(fd: usize, name: &const u8, value: &const void, size: usize, flags: usize) usize {
+pub fn fsetxattr(fd: usize, name: *const u8, value: *const void, size: usize, flags: usize) usize {
return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags);
}
-pub fn removexattr(path: &const u8, name: &const u8) usize {
+pub fn removexattr(path: *const u8, name: *const u8) usize {
return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name));
}
-pub fn lremovexattr(path: &const u8, name: &const u8) usize {
+pub fn lremovexattr(path: *const u8, name: *const u8) usize {
return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name));
}
-pub fn fremovexattr(fd: usize, name: &const u8) usize {
+pub fn fremovexattr(fd: usize, name: *const u8) usize {
return syscall2(SYS_fremovexattr, fd, @ptrToInt(name));
}
@@ -1184,11 +1184,11 @@ pub fn epoll_create1(flags: usize) usize {
return syscall1(SYS_epoll_create1, flags);
}
-pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: &epoll_event) usize {
+pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
return syscall4(SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev));
}
-pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: u32, timeout: i32) usize {
+pub fn epoll_wait(epoll_fd: i32, events: *epoll_event, maxevents: u32, timeout: i32) usize {
return syscall4(SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout));
}
@@ -1201,11 +1201,11 @@ pub const itimerspec = extern struct {
it_value: timespec,
};
-pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize {
+pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
return syscall2(SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value));
}
-pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) usize {
+pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
return syscall4(SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
}
@@ -1300,8 +1300,8 @@ pub fn CAP_TO_INDEX(cap: u8) u8 {
}
pub const cap_t = extern struct {
- hdrp: &cap_user_header_t,
- datap: &cap_user_data_t,
+ hdrp: *cap_user_header_t,
+ datap: *cap_user_data_t,
};
pub const cap_user_header_t = extern struct {
@@ -1319,11 +1319,11 @@ pub fn unshare(flags: usize) usize {
return syscall1(SYS_unshare, usize(flags));
}
-pub fn capget(hdrp: &cap_user_header_t, datap: &cap_user_data_t) usize {
+pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap));
}
-pub fn capset(hdrp: &cap_user_header_t, datap: &const cap_user_data_t) usize {
+pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize {
return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap));
}
diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig
index 8e0a285841..1317da6388 100644
--- a/std/os/linux/vdso.zig
+++ b/std/os/linux/vdso.zig
@@ -8,11 +8,11 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
const vdso_addr = std.os.linux_aux_raw[std.elf.AT_SYSINFO_EHDR];
if (vdso_addr == 0) return 0;
- const eh = @intToPtr(&elf.Ehdr, vdso_addr);
+ const eh = @intToPtr(*elf.Ehdr, vdso_addr);
var ph_addr: usize = vdso_addr + eh.e_phoff;
- const ph = @intToPtr(&elf.Phdr, ph_addr);
+ const ph = @intToPtr(*elf.Phdr, ph_addr);
- var maybe_dynv: ?&usize = null;
+ var maybe_dynv: ?*usize = null;
var base: usize = @maxValue(usize);
{
var i: usize = 0;
@@ -20,10 +20,10 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
i += 1;
ph_addr += eh.e_phentsize;
}) {
- const this_ph = @intToPtr(&elf.Phdr, ph_addr);
+ const this_ph = @intToPtr(*elf.Phdr, ph_addr);
switch (this_ph.p_type) {
elf.PT_LOAD => base = vdso_addr + this_ph.p_offset - this_ph.p_vaddr,
- elf.PT_DYNAMIC => maybe_dynv = @intToPtr(&usize, vdso_addr + this_ph.p_offset),
+ elf.PT_DYNAMIC => maybe_dynv = @intToPtr(*usize, vdso_addr + this_ph.p_offset),
else => {},
}
}
@@ -31,22 +31,22 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
const dynv = maybe_dynv ?? return 0;
if (base == @maxValue(usize)) return 0;
- var maybe_strings: ?&u8 = null;
- var maybe_syms: ?&elf.Sym = null;
- var maybe_hashtab: ?&linux.Elf_Symndx = null;
- var maybe_versym: ?&u16 = null;
- var maybe_verdef: ?&elf.Verdef = null;
+ var maybe_strings: ?*u8 = null;
+ var maybe_syms: ?*elf.Sym = null;
+ var maybe_hashtab: ?*linux.Elf_Symndx = null;
+ var maybe_versym: ?*u16 = null;
+ var maybe_verdef: ?*elf.Verdef = null;
{
var i: usize = 0;
while (dynv[i] != 0) : (i += 2) {
const p = base + dynv[i + 1];
switch (dynv[i]) {
- elf.DT_STRTAB => maybe_strings = @intToPtr(&u8, p),
- elf.DT_SYMTAB => maybe_syms = @intToPtr(&elf.Sym, p),
- elf.DT_HASH => maybe_hashtab = @intToPtr(&linux.Elf_Symndx, p),
- elf.DT_VERSYM => maybe_versym = @intToPtr(&u16, p),
- elf.DT_VERDEF => maybe_verdef = @intToPtr(&elf.Verdef, p),
+ elf.DT_STRTAB => maybe_strings = @intToPtr(*u8, p),
+ elf.DT_SYMTAB => maybe_syms = @intToPtr(*elf.Sym, p),
+ elf.DT_HASH => maybe_hashtab = @intToPtr(*linux.Elf_Symndx, p),
+ elf.DT_VERSYM => maybe_versym = @intToPtr(*u16, p),
+ elf.DT_VERDEF => maybe_verdef = @intToPtr(*elf.Verdef, p),
else => {},
}
}
@@ -76,7 +76,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
return 0;
}
-fn checkver(def_arg: &elf.Verdef, vsym_arg: i32, vername: []const u8, strings: &u8) bool {
+fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: *u8) bool {
var def = def_arg;
const vsym = @bitCast(u32, vsym_arg) & 0x7fff;
while (true) {
@@ -84,8 +84,8 @@ fn checkver(def_arg: &elf.Verdef, vsym_arg: i32, vername: []const u8, strings: &
break;
if (def.vd_next == 0)
return false;
- def = @intToPtr(&elf.Verdef, @ptrToInt(def) + def.vd_next);
+ def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next);
}
- const aux = @intToPtr(&elf.Verdaux, @ptrToInt(def) + def.vd_aux);
+ const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux);
return mem.eql(u8, vername, cstr.toSliceConst(&strings[aux.vda_name]));
}
diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig
index b43a642038..9a90e64757 100644
--- a/std/os/linux/x86_64.zig
+++ b/std/os/linux/x86_64.zig
@@ -463,7 +463,7 @@ pub fn syscall6(
}
/// This matches the libc clone function.
-pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: usize, arg: usize, ptid: &i32, tls: usize, ctid: &i32) usize;
+pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub nakedcc fn restore_rt() void {
return asm volatile ("syscall"
@@ -474,12 +474,12 @@ pub nakedcc fn restore_rt() void {
}
pub const msghdr = extern struct {
- msg_name: &u8,
+ msg_name: *u8,
msg_namelen: socklen_t,
- msg_iov: &iovec,
+ msg_iov: *iovec,
msg_iovlen: i32,
__pad1: i32,
- msg_control: &u8,
+ msg_control: *u8,
msg_controllen: socklen_t,
__pad2: socklen_t,
msg_flags: i32,
diff --git a/std/os/path.zig b/std/os/path.zig
index 162faffc42..4df6179bf5 100644
--- a/std/os/path.zig
+++ b/std/os/path.zig
@@ -32,7 +32,7 @@ pub fn isSep(byte: u8) bool {
/// Naively combines a series of paths with the native path seperator.
/// Allocates memory for the result, which must be freed by the caller.
-pub fn join(allocator: &Allocator, paths: ...) ![]u8 {
+pub fn join(allocator: *Allocator, paths: ...) ![]u8 {
if (is_windows) {
return joinWindows(allocator, paths);
} else {
@@ -40,11 +40,11 @@ pub fn join(allocator: &Allocator, paths: ...) ![]u8 {
}
}
-pub fn joinWindows(allocator: &Allocator, paths: ...) ![]u8 {
+pub fn joinWindows(allocator: *Allocator, paths: ...) ![]u8 {
return mem.join(allocator, sep_windows, paths);
}
-pub fn joinPosix(allocator: &Allocator, paths: ...) ![]u8 {
+pub fn joinPosix(allocator: *Allocator, paths: ...) ![]u8 {
return mem.join(allocator, sep_posix, paths);
}
@@ -310,7 +310,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
}
/// Converts the command line arguments into a slice and calls `resolveSlice`.
-pub fn resolve(allocator: &Allocator, args: ...) ![]u8 {
+pub fn resolve(allocator: *Allocator, args: ...) ![]u8 {
var paths: [args.len][]const u8 = undefined;
comptime var arg_i = 0;
inline while (arg_i < args.len) : (arg_i += 1) {
@@ -320,7 +320,7 @@ pub fn resolve(allocator: &Allocator, args: ...) ![]u8 {
}
/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
-pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) ![]u8 {
+pub fn resolveSlice(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (is_windows) {
return resolveWindows(allocator, paths);
} else {
@@ -334,7 +334,7 @@ pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) ![]u8 {
/// If all paths are relative it uses the current working directory as a starting point.
/// Each drive has its own current working directory.
/// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters.
-pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
+pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (paths.len == 0) {
assert(is_windows); // resolveWindows called on non windows can't use getCwd
return os.getCwd(allocator);
@@ -513,7 +513,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
/// It resolves "." and "..".
/// The result does not have a trailing path separator.
/// If all paths are relative it uses the current working directory as a starting point.
-pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) ![]u8 {
+pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (paths.len == 0) {
assert(!is_windows); // resolvePosix called on windows can't use getCwd
return os.getCwd(allocator);
@@ -883,7 +883,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {
/// resolve to the same path (after calling `resolve` on each), a zero-length
/// string is returned.
/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.
-pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
+pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
if (is_windows) {
return relativeWindows(allocator, from, to);
} else {
@@ -891,7 +891,7 @@ pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
}
}
-pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
+pub fn relativeWindows(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
const resolved_from = try resolveWindows(allocator, [][]const u8{from});
defer allocator.free(resolved_from);
@@ -964,7 +964,7 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8)
return []u8{};
}
-pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
+pub fn relativePosix(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
const resolved_from = try resolvePosix(allocator, [][]const u8{from});
defer allocator.free(resolved_from);
@@ -1063,7 +1063,7 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons
/// Expands all symbolic links and resolves references to `.`, `..`, and
/// extra `/` characters in ::pathname.
/// Caller must deallocate result.
-pub fn real(allocator: &Allocator, pathname: []const u8) ![]u8 {
+pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 {
switch (builtin.os) {
Os.windows => {
const pathname_buf = try allocator.alloc(u8, pathname.len + 1);
diff --git a/std/os/test.zig b/std/os/test.zig
index 4dfe76224a..4aa3535829 100644
--- a/std/os/test.zig
+++ b/std/os/test.zig
@@ -63,7 +63,7 @@ fn start1(ctx: void) u8 {
return 0;
}
-fn start2(ctx: &i32) u8 {
+fn start2(ctx: *i32) u8 {
_ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
return 0;
}
diff --git a/std/os/time.zig b/std/os/time.zig
index 9a7c682483..8629504323 100644
--- a/std/os/time.zig
+++ b/std/os/time.zig
@@ -200,7 +200,7 @@ pub const Timer = struct {
}
/// Reads the timer value since start or the last reset in nanoseconds
- pub fn read(self: &Timer) u64 {
+ pub fn read(self: *Timer) u64 {
var clock = clockNative() - self.start_time;
return switch (builtin.os) {
Os.windows => @divFloor(clock * ns_per_s, self.frequency),
@@ -211,12 +211,12 @@ pub const Timer = struct {
}
/// Resets the timer value to 0/now.
- pub fn reset(self: &Timer) void {
+ pub fn reset(self: *Timer) void {
self.start_time = clockNative();
}
/// Returns the current value of the timer in nanoseconds, then resets it
- pub fn lap(self: &Timer) u64 {
+ pub fn lap(self: *Timer) u64 {
var now = clockNative();
var lap_time = self.read();
self.start_time = now;
diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig
index 264ea391c4..85f69836d5 100644
--- a/std/os/windows/index.zig
+++ b/std/os/windows/index.zig
@@ -1,7 +1,7 @@
pub const ERROR = @import("error.zig");
pub extern "advapi32" stdcallcc fn CryptAcquireContextA(
- phProv: &HCRYPTPROV,
+ phProv: *HCRYPTPROV,
pszContainer: ?LPCSTR,
pszProvider: ?LPCSTR,
dwProvType: DWORD,
@@ -10,13 +10,13 @@ pub extern "advapi32" stdcallcc fn CryptAcquireContextA(
pub extern "advapi32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) BOOL;
-pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) BOOL;
+pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: *BYTE) BOOL;
pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL;
pub extern "kernel32" stdcallcc fn CreateDirectoryA(
lpPathName: LPCSTR,
- lpSecurityAttributes: ?&SECURITY_ATTRIBUTES,
+ lpSecurityAttributes: ?*SECURITY_ATTRIBUTES,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateFileA(
@@ -30,23 +30,23 @@ pub extern "kernel32" stdcallcc fn CreateFileA(
) HANDLE;
pub extern "kernel32" stdcallcc fn CreatePipe(
- hReadPipe: &HANDLE,
- hWritePipe: &HANDLE,
- lpPipeAttributes: &const SECURITY_ATTRIBUTES,
+ hReadPipe: *HANDLE,
+ hWritePipe: *HANDLE,
+ lpPipeAttributes: *const SECURITY_ATTRIBUTES,
nSize: DWORD,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateProcessA(
lpApplicationName: ?LPCSTR,
lpCommandLine: LPSTR,
- lpProcessAttributes: ?&SECURITY_ATTRIBUTES,
- lpThreadAttributes: ?&SECURITY_ATTRIBUTES,
+ lpProcessAttributes: ?*SECURITY_ATTRIBUTES,
+ lpThreadAttributes: ?*SECURITY_ATTRIBUTES,
bInheritHandles: BOOL,
dwCreationFlags: DWORD,
- lpEnvironment: ?&c_void,
+ lpEnvironment: ?*c_void,
lpCurrentDirectory: ?LPCSTR,
- lpStartupInfo: &STARTUPINFOA,
- lpProcessInformation: &PROCESS_INFORMATION,
+ lpStartupInfo: *STARTUPINFOA,
+ lpProcessInformation: *PROCESS_INFORMATION,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateSymbolicLinkA(
@@ -65,7 +65,7 @@ pub extern "kernel32" stdcallcc fn FreeEnvironmentStringsA(penv: LPCH) BOOL;
pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR;
-pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: &DWORD) BOOL;
+pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) BOOL;
pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD;
@@ -73,9 +73,9 @@ pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?LPCH;
pub extern "kernel32" stdcallcc fn GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: LPSTR, nSize: DWORD) DWORD;
-pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: &DWORD) BOOL;
+pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: *DWORD) BOOL;
-pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: &LARGE_INTEGER) BOOL;
+pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LARGE_INTEGER) BOOL;
pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: LPSTR, nSize: DWORD) DWORD;
@@ -84,7 +84,7 @@ pub extern "kernel32" stdcallcc fn GetLastError() DWORD;
pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(
in_hFile: HANDLE,
in_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
- out_lpFileInformation: &c_void,
+ out_lpFileInformation: *c_void,
in_dwBufferSize: DWORD,
) BOOL;
@@ -97,21 +97,21 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(
pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE;
-pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(?&FILETIME) void;
+pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(?*FILETIME) void;
pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE;
pub extern "kernel32" stdcallcc fn HeapDestroy(hHeap: HANDLE) BOOL;
-pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: &c_void, dwBytes: SIZE_T) ?&c_void;
-pub extern "kernel32" stdcallcc fn HeapSize(hHeap: HANDLE, dwFlags: DWORD, lpMem: &const c_void) SIZE_T;
-pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: &const c_void) BOOL;
+pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: *c_void, dwBytes: SIZE_T) ?*c_void;
+pub extern "kernel32" stdcallcc fn HeapSize(hHeap: HANDLE, dwFlags: DWORD, lpMem: *const c_void) SIZE_T;
+pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: *const c_void) BOOL;
pub extern "kernel32" stdcallcc fn HeapCompact(hHeap: HANDLE, dwFlags: DWORD) SIZE_T;
pub extern "kernel32" stdcallcc fn HeapSummary(hHeap: HANDLE, dwFlags: DWORD, lpSummary: LPHEAP_SUMMARY) BOOL;
pub extern "kernel32" stdcallcc fn GetStdHandle(in_nStdHandle: DWORD) ?HANDLE;
-pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) ?&c_void;
+pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) ?*c_void;
-pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: &c_void) BOOL;
+pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: *c_void) BOOL;
pub extern "kernel32" stdcallcc fn MoveFileExA(
lpExistingFileName: LPCSTR,
@@ -119,24 +119,24 @@ pub extern "kernel32" stdcallcc fn MoveFileExA(
dwFlags: DWORD,
) BOOL;
-pub extern "kernel32" stdcallcc fn QueryPerformanceCounter(lpPerformanceCount: &LARGE_INTEGER) BOOL;
+pub extern "kernel32" stdcallcc fn QueryPerformanceCounter(lpPerformanceCount: *LARGE_INTEGER) BOOL;
-pub extern "kernel32" stdcallcc fn QueryPerformanceFrequency(lpFrequency: &LARGE_INTEGER) BOOL;
+pub extern "kernel32" stdcallcc fn QueryPerformanceFrequency(lpFrequency: *LARGE_INTEGER) BOOL;
pub extern "kernel32" stdcallcc fn PathFileExists(pszPath: ?LPCTSTR) BOOL;
pub extern "kernel32" stdcallcc fn ReadFile(
in_hFile: HANDLE,
- out_lpBuffer: &c_void,
+ out_lpBuffer: *c_void,
in_nNumberOfBytesToRead: DWORD,
- out_lpNumberOfBytesRead: &DWORD,
- in_out_lpOverlapped: ?&OVERLAPPED,
+ out_lpNumberOfBytesRead: *DWORD,
+ in_out_lpOverlapped: ?*OVERLAPPED,
) BOOL;
pub extern "kernel32" stdcallcc fn SetFilePointerEx(
in_fFile: HANDLE,
in_liDistanceToMove: LARGE_INTEGER,
- out_opt_ldNewFilePointer: ?&LARGE_INTEGER,
+ out_opt_ldNewFilePointer: ?*LARGE_INTEGER,
in_dwMoveMethod: DWORD,
) BOOL;
@@ -150,10 +150,10 @@ pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMillis
pub extern "kernel32" stdcallcc fn WriteFile(
in_hFile: HANDLE,
- in_lpBuffer: &const c_void,
+ in_lpBuffer: *const c_void,
in_nNumberOfBytesToWrite: DWORD,
- out_lpNumberOfBytesWritten: ?&DWORD,
- in_out_lpOverlapped: ?&OVERLAPPED,
+ out_lpNumberOfBytesWritten: ?*DWORD,
+ in_out_lpOverlapped: ?*OVERLAPPED,
) BOOL;
//TODO: call unicode versions instead of relying on ANSI code page
@@ -171,23 +171,23 @@ pub const BYTE = u8;
pub const CHAR = u8;
pub const DWORD = u32;
pub const FLOAT = f32;
-pub const HANDLE = &c_void;
+pub const HANDLE = *c_void;
pub const HCRYPTPROV = ULONG_PTR;
-pub const HINSTANCE = &@OpaqueType();
-pub const HMODULE = &@OpaqueType();
+pub const HINSTANCE = *@OpaqueType();
+pub const HMODULE = *@OpaqueType();
pub const INT = c_int;
-pub const LPBYTE = &BYTE;
-pub const LPCH = &CHAR;
-pub const LPCSTR = &const CHAR;
-pub const LPCTSTR = &const TCHAR;
-pub const LPCVOID = &const c_void;
-pub const LPDWORD = &DWORD;
-pub const LPSTR = &CHAR;
+pub const LPBYTE = *BYTE;
+pub const LPCH = *CHAR;
+pub const LPCSTR = *const CHAR;
+pub const LPCTSTR = *const TCHAR;
+pub const LPCVOID = *const c_void;
+pub const LPDWORD = *DWORD;
+pub const LPSTR = *CHAR;
pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR;
-pub const LPVOID = &c_void;
-pub const LPWSTR = &WCHAR;
-pub const PVOID = &c_void;
-pub const PWSTR = &WCHAR;
+pub const LPVOID = *c_void;
+pub const LPWSTR = *WCHAR;
+pub const PVOID = *c_void;
+pub const PWSTR = *WCHAR;
pub const SIZE_T = usize;
pub const TCHAR = if (UNICODE) WCHAR else u8;
pub const UINT = c_uint;
@@ -218,7 +218,7 @@ pub const OVERLAPPED = extern struct {
Pointer: PVOID,
hEvent: HANDLE,
};
-pub const LPOVERLAPPED = &OVERLAPPED;
+pub const LPOVERLAPPED = *OVERLAPPED;
pub const MAX_PATH = 260;
@@ -271,11 +271,11 @@ pub const VOLUME_NAME_NT = 0x2;
pub const SECURITY_ATTRIBUTES = extern struct {
nLength: DWORD,
- lpSecurityDescriptor: ?&c_void,
+ lpSecurityDescriptor: ?*c_void,
bInheritHandle: BOOL,
};
-pub const PSECURITY_ATTRIBUTES = &SECURITY_ATTRIBUTES;
-pub const LPSECURITY_ATTRIBUTES = &SECURITY_ATTRIBUTES;
+pub const PSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES;
+pub const LPSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES;
pub const GENERIC_READ = 0x80000000;
pub const GENERIC_WRITE = 0x40000000;
diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig
index 2bd8a157e4..7170346108 100644
--- a/std/os/windows/util.zig
+++ b/std/os/windows/util.zig
@@ -42,7 +42,7 @@ pub const WriteError = error{
};
pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) WriteError!void {
- if (windows.WriteFile(handle, @ptrCast(&const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
+ if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources,
@@ -68,11 +68,11 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
const size = @sizeOf(windows.FILE_NAME_INFO);
var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH);
- if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(&c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) {
+ if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(*c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) {
return true;
}
- const name_info = @ptrCast(&const windows.FILE_NAME_INFO, &name_info_bytes[0]);
+ const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]);
const name_bytes = name_info_bytes[size .. size + usize(name_info.FileNameLength)];
const name_wide = ([]u16)(name_bytes);
return mem.indexOf(u16, name_wide, []u16{ 'm', 's', 'y', 's', '-' }) != null or
@@ -91,7 +91,7 @@ pub const OpenError = error{
/// `file_path` needs to be copied in memory to add a null terminating byte, hence the allocator.
pub fn windowsOpen(
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
file_path: []const u8,
desired_access: windows.DWORD,
share_mode: windows.DWORD,
@@ -119,7 +119,7 @@ pub fn windowsOpen(
}
/// Caller must free result.
-pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) ![]u8 {
+pub fn createWindowsEnvBlock(allocator: *mem.Allocator, env_map: *const BufMap) ![]u8 {
// count bytes needed
const bytes_needed = x: {
var bytes_needed: usize = 1; // 1 for the final null byte
@@ -150,7 +150,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
return result;
}
-pub fn windowsLoadDll(allocator: &mem.Allocator, dll_path: []const u8) !windows.HMODULE {
+pub fn windowsLoadDll(allocator: *mem.Allocator, dll_path: []const u8) !windows.HMODULE {
const padded_buff = try cstr.addNullByte(allocator, dll_path);
defer allocator.free(padded_buff);
return windows.LoadLibraryA(padded_buff.ptr) ?? error.DllNotFound;
diff --git a/std/os/zen.zig b/std/os/zen.zig
index 2411c5363e..2312b36dea 100644
--- a/std/os/zen.zig
+++ b/std/os/zen.zig
@@ -8,7 +8,7 @@ pub const Message = struct {
type: usize,
payload: usize,
- pub fn from(mailbox_id: &const MailboxId) Message {
+ pub fn from(mailbox_id: *const MailboxId) Message {
return Message{
.sender = MailboxId.Undefined,
.receiver = *mailbox_id,
@@ -17,7 +17,7 @@ pub const Message = struct {
};
}
- pub fn to(mailbox_id: &const MailboxId, msg_type: usize) Message {
+ pub fn to(mailbox_id: *const MailboxId, msg_type: usize) Message {
return Message{
.sender = MailboxId.This,
.receiver = *mailbox_id,
@@ -26,7 +26,7 @@ pub const Message = struct {
};
}
- pub fn withData(mailbox_id: &const MailboxId, msg_type: usize, payload: usize) Message {
+ pub fn withData(mailbox_id: *const MailboxId, msg_type: usize, payload: usize) Message {
return Message{
.sender = MailboxId.This,
.receiver = *mailbox_id,
@@ -67,7 +67,7 @@ pub const getErrno = @import("linux/index.zig").getErrno;
use @import("linux/errno.zig");
// TODO: implement this correctly.
-pub fn read(fd: i32, buf: &u8, count: usize) usize {
+pub fn read(fd: i32, buf: *u8, count: usize) usize {
switch (fd) {
STDIN_FILENO => {
var i: usize = 0;
@@ -75,7 +75,7 @@ pub fn read(fd: i32, buf: &u8, count: usize) usize {
send(Message.to(Server.Keyboard, 0));
var message = Message.from(MailboxId.This);
- receive(&message);
+ receive(*message);
buf[i] = u8(message.payload);
}
@@ -86,7 +86,7 @@ pub fn read(fd: i32, buf: &u8, count: usize) usize {
}
// TODO: implement this correctly.
-pub fn write(fd: i32, buf: &const u8, count: usize) usize {
+pub fn write(fd: i32, buf: *const u8, count: usize) usize {
switch (fd) {
STDOUT_FILENO, STDERR_FILENO => {
var i: usize = 0;
@@ -126,22 +126,22 @@ pub fn exit(status: i32) noreturn {
unreachable;
}
-pub fn createPort(mailbox_id: &const MailboxId) void {
+pub fn createPort(mailbox_id: *const MailboxId) void {
_ = switch (*mailbox_id) {
MailboxId.Port => |id| syscall1(Syscall.createPort, id),
else => unreachable,
};
}
-pub fn send(message: &const Message) void {
+pub fn send(message: *const Message) void {
_ = syscall1(Syscall.send, @ptrToInt(message));
}
-pub fn receive(destination: &Message) void {
+pub fn receive(destination: *Message) void {
_ = syscall1(Syscall.receive, @ptrToInt(destination));
}
-pub fn subscribeIRQ(irq: u8, mailbox_id: &const MailboxId) void {
+pub fn subscribeIRQ(irq: u8, mailbox_id: *const MailboxId) void {
_ = syscall2(Syscall.subscribeIRQ, irq, @ptrToInt(mailbox_id));
}
diff --git a/std/rand/index.zig b/std/rand/index.zig
index c32309a0fd..3a1a559cd9 100644
--- a/std/rand/index.zig
+++ b/std/rand/index.zig
@@ -28,15 +28,15 @@ pub const DefaultPrng = Xoroshiro128;
pub const DefaultCsprng = Isaac64;
pub const Random = struct {
- fillFn: fn (r: &Random, buf: []u8) void,
+ fillFn: fn (r: *Random, buf: []u8) void,
/// Read random bytes into the specified buffer until fill.
- pub fn bytes(r: &Random, buf: []u8) void {
+ pub fn bytes(r: *Random, buf: []u8) void {
r.fillFn(r, buf);
}
/// Return a random integer/boolean type.
- pub fn scalar(r: &Random, comptime T: type) T {
+ pub fn scalar(r: *Random, comptime T: type) T {
var rand_bytes: [@sizeOf(T)]u8 = undefined;
r.bytes(rand_bytes[0..]);
@@ -50,7 +50,7 @@ pub const Random = struct {
/// Get a random unsigned integer with even distribution between `start`
/// inclusive and `end` exclusive.
- pub fn range(r: &Random, comptime T: type, start: T, end: T) T {
+ pub fn range(r: *Random, comptime T: type, start: T, end: T) T {
assert(start <= end);
if (T.is_signed) {
const uint = @IntType(false, T.bit_count);
@@ -92,7 +92,7 @@ pub const Random = struct {
}
/// Return a floating point value evenly distributed in the range [0, 1).
- pub fn float(r: &Random, comptime T: type) T {
+ pub fn float(r: *Random, comptime T: type) T {
// Generate a uniform value between [1, 2) and scale down to [0, 1).
// Note: The lowest mantissa bit is always set to 0 so we only use half the available range.
switch (T) {
@@ -113,7 +113,7 @@ pub const Random = struct {
/// Return a floating point value normally distributed with mean = 0, stddev = 1.
///
/// To use different parameters, use: floatNorm(...) * desiredStddev + desiredMean.
- pub fn floatNorm(r: &Random, comptime T: type) T {
+ pub fn floatNorm(r: *Random, comptime T: type) T {
const value = ziggurat.next_f64(r, ziggurat.NormDist);
switch (T) {
f32 => return f32(value),
@@ -125,7 +125,7 @@ pub const Random = struct {
/// Return an exponentially distributed float with a rate parameter of 1.
///
/// To use a different rate parameter, use: floatExp(...) / desiredRate.
- pub fn floatExp(r: &Random, comptime T: type) T {
+ pub fn floatExp(r: *Random, comptime T: type) T {
const value = ziggurat.next_f64(r, ziggurat.ExpDist);
switch (T) {
f32 => return f32(value),
@@ -135,7 +135,7 @@ pub const Random = struct {
}
/// Shuffle a slice into a random order.
- pub fn shuffle(r: &Random, comptime T: type, buf: []T) void {
+ pub fn shuffle(r: *Random, comptime T: type, buf: []T) void {
if (buf.len < 2) {
return;
}
@@ -159,7 +159,7 @@ const SplitMix64 = struct {
return SplitMix64{ .s = seed };
}
- pub fn next(self: &SplitMix64) u64 {
+ pub fn next(self: *SplitMix64) u64 {
self.s +%= 0x9e3779b97f4a7c15;
var z = self.s;
@@ -208,7 +208,7 @@ pub const Pcg = struct {
return pcg;
}
- fn next(self: &Pcg) u32 {
+ fn next(self: *Pcg) u32 {
const l = self.s;
self.s = l *% default_multiplier +% (self.i | 1);
@@ -218,13 +218,13 @@ pub const Pcg = struct {
return (xor_s >> u5(rot)) | (xor_s << u5((0 -% rot) & 31));
}
- fn seed(self: &Pcg, init_s: u64) void {
+ fn seed(self: *Pcg, init_s: u64) void {
// Pcg requires 128-bits of seed.
var gen = SplitMix64.init(init_s);
self.seedTwo(gen.next(), gen.next());
}
- fn seedTwo(self: &Pcg, init_s: u64, init_i: u64) void {
+ fn seedTwo(self: *Pcg, init_s: u64, init_i: u64) void {
self.s = 0;
self.i = (init_s << 1) | 1;
self.s = self.s *% default_multiplier +% self.i;
@@ -232,7 +232,7 @@ pub const Pcg = struct {
self.s = self.s *% default_multiplier +% self.i;
}
- fn fill(r: &Random, buf: []u8) void {
+ fn fill(r: *Random, buf: []u8) void {
const self = @fieldParentPtr(Pcg, "random", r);
var i: usize = 0;
@@ -297,7 +297,7 @@ pub const Xoroshiro128 = struct {
return x;
}
- fn next(self: &Xoroshiro128) u64 {
+ fn next(self: *Xoroshiro128) u64 {
const s0 = self.s[0];
var s1 = self.s[1];
const r = s0 +% s1;
@@ -310,7 +310,7 @@ pub const Xoroshiro128 = struct {
}
// Skip 2^64 places ahead in the sequence
- fn jump(self: &Xoroshiro128) void {
+ fn jump(self: *Xoroshiro128) void {
var s0: u64 = 0;
var s1: u64 = 0;
@@ -334,7 +334,7 @@ pub const Xoroshiro128 = struct {
self.s[1] = s1;
}
- fn seed(self: &Xoroshiro128, init_s: u64) void {
+ fn seed(self: *Xoroshiro128, init_s: u64) void {
// Xoroshiro requires 128-bits of seed.
var gen = SplitMix64.init(init_s);
@@ -342,7 +342,7 @@ pub const Xoroshiro128 = struct {
self.s[1] = gen.next();
}
- fn fill(r: &Random, buf: []u8) void {
+ fn fill(r: *Random, buf: []u8) void {
const self = @fieldParentPtr(Xoroshiro128, "random", r);
var i: usize = 0;
@@ -435,7 +435,7 @@ pub const Isaac64 = struct {
return isaac;
}
- fn step(self: &Isaac64, mix: u64, base: usize, comptime m1: usize, comptime m2: usize) void {
+ fn step(self: *Isaac64, mix: u64, base: usize, comptime m1: usize, comptime m2: usize) void {
const x = self.m[base + m1];
self.a = mix +% self.m[base + m2];
@@ -446,7 +446,7 @@ pub const Isaac64 = struct {
self.r[self.r.len - 1 - base - m1] = self.b;
}
- fn refill(self: &Isaac64) void {
+ fn refill(self: *Isaac64) void {
const midpoint = self.r.len / 2;
self.c +%= 1;
@@ -475,7 +475,7 @@ pub const Isaac64 = struct {
self.i = 0;
}
- fn next(self: &Isaac64) u64 {
+ fn next(self: *Isaac64) u64 {
if (self.i >= self.r.len) {
self.refill();
}
@@ -485,7 +485,7 @@ pub const Isaac64 = struct {
return value;
}
- fn seed(self: &Isaac64, init_s: u64, comptime rounds: usize) void {
+ fn seed(self: *Isaac64, init_s: u64, comptime rounds: usize) void {
// We ignore the multi-pass requirement since we don't currently expose full access to
// seeding the self.m array completely.
mem.set(u64, self.m[0..], 0);
@@ -551,7 +551,7 @@ pub const Isaac64 = struct {
self.i = self.r.len; // trigger refill on first value
}
- fn fill(r: &Random, buf: []u8) void {
+ fn fill(r: *Random, buf: []u8) void {
const self = @fieldParentPtr(Isaac64, "random", r);
var i: usize = 0;
@@ -666,7 +666,7 @@ test "Random range" {
testRange(&prng.random, 10, 14);
}
-fn testRange(r: &Random, start: i32, end: i32) void {
+fn testRange(r: *Random, start: i32, end: i32) void {
const count = usize(end - start);
var values_buffer = []bool{false} ** 20;
const values = values_buffer[0..count];
diff --git a/std/rand/ziggurat.zig b/std/rand/ziggurat.zig
index 7daeb59165..774d3bd52a 100644
--- a/std/rand/ziggurat.zig
+++ b/std/rand/ziggurat.zig
@@ -12,7 +12,7 @@ const std = @import("../index.zig");
const math = std.math;
const Random = std.rand.Random;
-pub fn next_f64(random: &Random, comptime tables: &const ZigTable) f64 {
+pub fn next_f64(random: *Random, comptime tables: *const ZigTable) f64 {
while (true) {
// We manually construct a float from parts as we can avoid an extra random lookup here by
// using the unused exponent for the lookup table entry.
@@ -60,7 +60,7 @@ pub const ZigTable = struct {
// whether the distribution is symmetric
is_symmetric: bool,
// fallback calculation in the case we are in the 0 block
- zero_case: fn (&Random, f64) f64,
+ zero_case: fn (*Random, f64) f64,
};
// zigNorInit
@@ -70,7 +70,7 @@ fn ZigTableGen(
comptime v: f64,
comptime f: fn (f64) f64,
comptime f_inv: fn (f64) f64,
- comptime zero_case: fn (&Random, f64) f64,
+ comptime zero_case: fn (*Random, f64) f64,
) ZigTable {
var tables: ZigTable = undefined;
@@ -110,7 +110,7 @@ fn norm_f(x: f64) f64 {
fn norm_f_inv(y: f64) f64 {
return math.sqrt(-2.0 * math.ln(y));
}
-fn norm_zero_case(random: &Random, u: f64) f64 {
+fn norm_zero_case(random: *Random, u: f64) f64 {
var x: f64 = 1;
var y: f64 = 0;
@@ -149,7 +149,7 @@ fn exp_f(x: f64) f64 {
fn exp_f_inv(y: f64) f64 {
return -math.ln(y);
}
-fn exp_zero_case(random: &Random, _: f64) f64 {
+fn exp_zero_case(random: *Random, _: f64) f64 {
return exp_r - math.ln(random.float(f64));
}
diff --git a/std/segmented_list.zig b/std/segmented_list.zig
index d755135fe8..be9a2071a0 100644
--- a/std/segmented_list.zig
+++ b/std/segmented_list.zig
@@ -87,49 +87,49 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
const ShelfIndex = std.math.Log2Int(usize);
prealloc_segment: [prealloc_item_count]T,
- dynamic_segments: []&T,
- allocator: &Allocator,
+ dynamic_segments: []*T,
+ allocator: *Allocator,
len: usize,
pub const prealloc_count = prealloc_item_count;
/// Deinitialize with `deinit`
- pub fn init(allocator: &Allocator) Self {
+ pub fn init(allocator: *Allocator) Self {
return Self{
.allocator = allocator,
.len = 0,
.prealloc_segment = undefined,
- .dynamic_segments = []&T{},
+ .dynamic_segments = []*T{},
};
}
- pub fn deinit(self: &Self) void {
+ pub fn deinit(self: *Self) void {
self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
self.allocator.free(self.dynamic_segments);
self.* = undefined;
}
- pub fn at(self: &Self, i: usize) &T {
+ pub fn at(self: *Self, i: usize) *T {
assert(i < self.len);
return self.uncheckedAt(i);
}
- pub fn count(self: &const Self) usize {
+ pub fn count(self: *const Self) usize {
return self.len;
}
- pub fn push(self: &Self, item: &const T) !void {
+ pub fn push(self: *Self, item: *const T) !void {
const new_item_ptr = try self.addOne();
new_item_ptr.* = item.*;
}
- pub fn pushMany(self: &Self, items: []const T) !void {
+ pub fn pushMany(self: *Self, items: []const T) !void {
for (items) |item| {
try self.push(item);
}
}
- pub fn pop(self: &Self) ?T {
+ pub fn pop(self: *Self) ?T {
if (self.len == 0) return null;
const index = self.len - 1;
@@ -138,7 +138,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
return result;
}
- pub fn addOne(self: &Self) !&T {
+ pub fn addOne(self: *Self) !*T {
const new_length = self.len + 1;
try self.growCapacity(new_length);
const result = self.uncheckedAt(self.len);
@@ -147,7 +147,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
/// Grows or shrinks capacity to match usage.
- pub fn setCapacity(self: &Self, new_capacity: usize) !void {
+ pub fn setCapacity(self: *Self, new_capacity: usize) !void {
if (new_capacity <= usize(1) << (prealloc_exp + self.dynamic_segments.len)) {
return self.shrinkCapacity(new_capacity);
} else {
@@ -156,15 +156,15 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
/// Only grows capacity, or retains current capacity
- pub fn growCapacity(self: &Self, new_capacity: usize) !void {
+ pub fn growCapacity(self: *Self, new_capacity: usize) !void {
const new_cap_shelf_count = shelfCount(new_capacity);
const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
if (new_cap_shelf_count > old_shelf_count) {
- self.dynamic_segments = try self.allocator.realloc(&T, self.dynamic_segments, new_cap_shelf_count);
+ self.dynamic_segments = try self.allocator.realloc(*T, self.dynamic_segments, new_cap_shelf_count);
var i = old_shelf_count;
errdefer {
self.freeShelves(i, old_shelf_count);
- self.dynamic_segments = self.allocator.shrink(&T, self.dynamic_segments, old_shelf_count);
+ self.dynamic_segments = self.allocator.shrink(*T, self.dynamic_segments, old_shelf_count);
}
while (i < new_cap_shelf_count) : (i += 1) {
self.dynamic_segments[i] = (try self.allocator.alloc(T, shelfSize(i))).ptr;
@@ -173,12 +173,12 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
/// Only shrinks capacity or retains current capacity
- pub fn shrinkCapacity(self: &Self, new_capacity: usize) void {
+ pub fn shrinkCapacity(self: *Self, new_capacity: usize) void {
if (new_capacity <= prealloc_item_count) {
const len = ShelfIndex(self.dynamic_segments.len);
self.freeShelves(len, 0);
self.allocator.free(self.dynamic_segments);
- self.dynamic_segments = []&T{};
+ self.dynamic_segments = []*T{};
return;
}
@@ -190,10 +190,10 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
self.freeShelves(old_shelf_count, new_cap_shelf_count);
- self.dynamic_segments = self.allocator.shrink(&T, self.dynamic_segments, new_cap_shelf_count);
+ self.dynamic_segments = self.allocator.shrink(*T, self.dynamic_segments, new_cap_shelf_count);
}
- pub fn uncheckedAt(self: &Self, index: usize) &T {
+ pub fn uncheckedAt(self: *Self, index: usize) *T {
if (index < prealloc_item_count) {
return &self.prealloc_segment[index];
}
@@ -230,7 +230,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
return list_index + prealloc_item_count - (usize(1) << ((prealloc_exp + 1) + shelf_index));
}
- fn freeShelves(self: &Self, from_count: ShelfIndex, to_count: ShelfIndex) void {
+ fn freeShelves(self: *Self, from_count: ShelfIndex, to_count: ShelfIndex) void {
var i = from_count;
while (i != to_count) {
i -= 1;
@@ -239,13 +239,13 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
pub const Iterator = struct {
- list: &Self,
+ list: *Self,
index: usize,
box_index: usize,
shelf_index: ShelfIndex,
shelf_size: usize,
- pub fn next(it: &Iterator) ?&T {
+ pub fn next(it: *Iterator) ?*T {
if (it.index >= it.list.len) return null;
if (it.index < prealloc_item_count) {
const ptr = &it.list.prealloc_segment[it.index];
@@ -269,7 +269,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
return ptr;
}
- pub fn prev(it: &Iterator) ?&T {
+ pub fn prev(it: *Iterator) ?*T {
if (it.index == 0) return null;
it.index -= 1;
@@ -286,7 +286,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
return &it.list.dynamic_segments[it.shelf_index][it.box_index];
}
- pub fn peek(it: &Iterator) ?&T {
+ pub fn peek(it: *Iterator) ?*T {
if (it.index >= it.list.len)
return null;
if (it.index < prealloc_item_count)
@@ -295,7 +295,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
return &it.list.dynamic_segments[it.shelf_index][it.box_index];
}
- pub fn set(it: &Iterator, index: usize) void {
+ pub fn set(it: *Iterator, index: usize) void {
it.index = index;
if (index < prealloc_item_count) return;
it.shelf_index = shelfIndex(index);
@@ -304,7 +304,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
};
- pub fn iterator(self: &Self, start_index: usize) Iterator {
+ pub fn iterator(self: *Self, start_index: usize) Iterator {
var it = Iterator{
.list = self,
.index = undefined,
@@ -331,7 +331,7 @@ test "std.SegmentedList" {
try testSegmentedList(16, a);
}
-fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
+fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
var list = SegmentedList(i32, prealloc).init(allocator);
defer list.deinit();
diff --git a/std/sort.zig b/std/sort.zig
index 4e17718241..1b44c18dd9 100644
--- a/std/sort.zig
+++ b/std/sort.zig
@@ -5,7 +5,7 @@ const math = std.math;
const builtin = @import("builtin");
/// Stable in-place sort. O(n) best case, O(pow(n, 2)) worst case. O(1) memory (no allocator required).
-pub fn insertionSort(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &const T) bool) void {
+pub fn insertionSort(comptime T: type, items: []T, lessThan: fn (lhs: *const T, rhs: *const T) bool) void {
{
var i: usize = 1;
while (i < items.len) : (i += 1) {
@@ -30,7 +30,7 @@ const Range = struct {
};
}
- fn length(self: &const Range) usize {
+ fn length(self: *const Range) usize {
return self.end - self.start;
}
};
@@ -58,12 +58,12 @@ const Iterator = struct {
};
}
- fn begin(self: &Iterator) void {
+ fn begin(self: *Iterator) void {
self.numerator = 0;
self.decimal = 0;
}
- fn nextRange(self: &Iterator) Range {
+ fn nextRange(self: *Iterator) Range {
const start = self.decimal;
self.decimal += self.decimal_step;
@@ -79,11 +79,11 @@ const Iterator = struct {
};
}
- fn finished(self: &Iterator) bool {
+ fn finished(self: *Iterator) bool {
return self.decimal >= self.size;
}
- fn nextLevel(self: &Iterator) bool {
+ fn nextLevel(self: *Iterator) bool {
self.decimal_step += self.decimal_step;
self.numerator_step += self.numerator_step;
if (self.numerator_step >= self.denominator) {
@@ -94,7 +94,7 @@ const Iterator = struct {
return (self.decimal_step < self.size);
}
- fn length(self: &Iterator) usize {
+ fn length(self: *Iterator) usize {
return self.decimal_step;
}
};
@@ -108,7 +108,7 @@ const Pull = struct {
/// Stable in-place sort. O(n) best case, O(n*log(n)) worst case and average case. O(1) memory (no allocator required).
/// Currently implemented as block sort.
-pub fn sort(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &const T) bool) void {
+pub fn sort(comptime T: type, items: []T, lessThan: fn (lhs: *const T, rhs: *const T) bool) void {
// Implementation ported from https://github.com/BonzaiThePenguin/WikiSort/blob/master/WikiSort.c
var cache: [512]T = undefined;
@@ -741,7 +741,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &con
}
// merge operation without a buffer
-fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const Range, lessThan: fn (&const T, &const T) bool) void {
+fn mergeInPlace(comptime T: type, items: []T, A_arg: *const Range, B_arg: *const Range, lessThan: fn (*const T, *const T) bool) void {
if (A_arg.length() == 0 or B_arg.length() == 0) return;
// this just repeatedly binary searches into B and rotates A into position.
@@ -783,7 +783,7 @@ fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const
}
// merge operation using an internal buffer
-fn mergeInternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn (&const T, &const T) bool, buffer: &const Range) void {
+fn mergeInternal(comptime T: type, items: []T, A: *const Range, B: *const Range, lessThan: fn (*const T, *const T) bool, buffer: *const Range) void {
// whenever we find a value to add to the final array, swap it with the value that's already in that spot
// when this algorithm is finished, 'buffer' will contain its original contents, but in a different order
var A_count: usize = 0;
@@ -819,7 +819,7 @@ fn blockSwap(comptime T: type, items: []T, start1: usize, start2: usize, block_s
// combine a linear search with a binary search to reduce the number of comparisons in situations
// where have some idea as to how many unique values there are and where the next value might be
-fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn (&const T, &const T) bool, unique: usize) usize {
+fn findFirstForward(comptime T: type, items: []T, value: *const T, range: *const Range, lessThan: fn (*const T, *const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
const skip = math.max(range.length() / unique, usize(1));
@@ -833,7 +833,7 @@ fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const
return binaryFirst(T, items, value, Range.init(index - skip, index), lessThan);
}
-fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn (&const T, &const T) bool, unique: usize) usize {
+fn findFirstBackward(comptime T: type, items: []T, value: *const T, range: *const Range, lessThan: fn (*const T, *const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
const skip = math.max(range.length() / unique, usize(1));
@@ -847,7 +847,7 @@ fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &cons
return binaryFirst(T, items, value, Range.init(index, index + skip), lessThan);
}
-fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn (&const T, &const T) bool, unique: usize) usize {
+fn findLastForward(comptime T: type, items: []T, value: *const T, range: *const Range, lessThan: fn (*const T, *const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
const skip = math.max(range.length() / unique, usize(1));
@@ -861,7 +861,7 @@ fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const
return binaryLast(T, items, value, Range.init(index - skip, index), lessThan);
}
-fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn (&const T, &const T) bool, unique: usize) usize {
+fn findLastBackward(comptime T: type, items: []T, value: *const T, range: *const Range, lessThan: fn (*const T, *const T) bool, unique: usize) usize {
if (range.length() == 0) return range.start;
const skip = math.max(range.length() / unique, usize(1));
@@ -875,7 +875,7 @@ fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const
return binaryLast(T, items, value, Range.init(index, index + skip), lessThan);
}
-fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn (&const T, &const T) bool) usize {
+fn binaryFirst(comptime T: type, items: []T, value: *const T, range: *const Range, lessThan: fn (*const T, *const T) bool) usize {
var start = range.start;
var end = range.end - 1;
if (range.start >= range.end) return range.end;
@@ -893,7 +893,7 @@ fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Rang
return start;
}
-fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn (&const T, &const T) bool) usize {
+fn binaryLast(comptime T: type, items: []T, value: *const T, range: *const Range, lessThan: fn (*const T, *const T) bool) usize {
var start = range.start;
var end = range.end - 1;
if (range.start >= range.end) return range.end;
@@ -911,7 +911,7 @@ fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range
return start;
}
-fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, lessThan: fn (&const T, &const T) bool, into: []T) void {
+fn mergeInto(comptime T: type, from: []T, A: *const Range, B: *const Range, lessThan: fn (*const T, *const T) bool, into: []T) void {
var A_index: usize = A.start;
var B_index: usize = B.start;
const A_last = A.end;
@@ -941,7 +941,7 @@ fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, less
}
}
-fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn (&const T, &const T) bool, cache: []T) void {
+fn mergeExternal(comptime T: type, items: []T, A: *const Range, B: *const Range, lessThan: fn (*const T, *const T) bool, cache: []T) void {
// A fits into the cache, so use that instead of the internal buffer
var A_index: usize = 0;
var B_index: usize = B.start;
@@ -969,26 +969,26 @@ fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range,
mem.copy(T, items[insert_index..], cache[A_index..A_last]);
}
-fn swap(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &const T) bool, order: &[8]u8, x: usize, y: usize) void {
+fn swap(comptime T: type, items: []T, lessThan: fn (lhs: *const T, rhs: *const T) bool, order: *[8]u8, x: usize, y: usize) void {
if (lessThan(items[y], items[x]) or ((order.*)[x] > (order.*)[y] and !lessThan(items[x], items[y]))) {
mem.swap(T, &items[x], &items[y]);
mem.swap(u8, &(order.*)[x], &(order.*)[y]);
}
}
-fn i32asc(lhs: &const i32, rhs: &const i32) bool {
+fn i32asc(lhs: *const i32, rhs: *const i32) bool {
return lhs.* < rhs.*;
}
-fn i32desc(lhs: &const i32, rhs: &const i32) bool {
+fn i32desc(lhs: *const i32, rhs: *const i32) bool {
return rhs.* < lhs.*;
}
-fn u8asc(lhs: &const u8, rhs: &const u8) bool {
+fn u8asc(lhs: *const u8, rhs: *const u8) bool {
return lhs.* < rhs.*;
}
-fn u8desc(lhs: &const u8, rhs: &const u8) bool {
+fn u8desc(lhs: *const u8, rhs: *const u8) bool {
return rhs.* < lhs.*;
}
@@ -1125,7 +1125,7 @@ const IdAndValue = struct {
id: usize,
value: i32,
};
-fn cmpByValue(a: &const IdAndValue, b: &const IdAndValue) bool {
+fn cmpByValue(a: *const IdAndValue, b: *const IdAndValue) bool {
return i32asc(a.value, b.value);
}
@@ -1324,7 +1324,7 @@ test "sort fuzz testing" {
var fixed_buffer_mem: [100 * 1024]u8 = undefined;
-fn fuzzTest(rng: &std.rand.Random) void {
+fn fuzzTest(rng: *std.rand.Random) void {
const array_size = rng.range(usize, 0, 1000);
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable;
@@ -1345,7 +1345,7 @@ fn fuzzTest(rng: &std.rand.Random) void {
}
}
-pub fn min(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &const T) bool) T {
+pub fn min(comptime T: type, items: []T, lessThan: fn (lhs: *const T, rhs: *const T) bool) T {
var i: usize = 0;
var smallest = items[0];
for (items[1..]) |item| {
@@ -1356,7 +1356,7 @@ pub fn min(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &cons
return smallest;
}
-pub fn max(comptime T: type, items: []T, lessThan: fn (lhs: &const T, rhs: &const T) bool) T {
+pub fn max(comptime T: type, items: []T, lessThan: fn (lhs: *const T, rhs: *const T) bool) T {
var i: usize = 0;
var biggest = items[0];
for (items[1..]) |item| {
diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig
index c10f4aa806..5ed7874ca5 100644
--- a/std/special/bootstrap.zig
+++ b/std/special/bootstrap.zig
@@ -5,7 +5,7 @@ const root = @import("@root");
const std = @import("std");
const builtin = @import("builtin");
-var argc_ptr: &usize = undefined;
+var argc_ptr: *usize = undefined;
comptime {
const strong_linkage = builtin.GlobalLinkage.Strong;
@@ -28,12 +28,12 @@ nakedcc fn _start() noreturn {
switch (builtin.arch) {
builtin.Arch.x86_64 => {
argc_ptr = asm ("lea (%%rsp), %[argc]"
- : [argc] "=r" (-> &usize)
+ : [argc] "=r" (-> *usize)
);
},
builtin.Arch.i386 => {
argc_ptr = asm ("lea (%%esp), %[argc]"
- : [argc] "=r" (-> &usize)
+ : [argc] "=r" (-> *usize)
);
},
else => @compileError("unsupported arch"),
@@ -51,13 +51,13 @@ extern fn WinMainCRTStartup() noreturn {
fn posixCallMainAndExit() noreturn {
const argc = argc_ptr.*;
- const argv = @ptrCast(&&u8, &argc_ptr[1]);
- const envp_nullable = @ptrCast(&?&u8, &argv[argc + 1]);
+ const argv = @ptrCast(**u8, &argc_ptr[1]);
+ const envp_nullable = @ptrCast(*?*u8, &argv[argc + 1]);
var envp_count: usize = 0;
while (envp_nullable[envp_count]) |_| : (envp_count += 1) {}
- const envp = @ptrCast(&&u8, envp_nullable)[0..envp_count];
+ const envp = @ptrCast(**u8, envp_nullable)[0..envp_count];
if (builtin.os == builtin.Os.linux) {
- const auxv = &@ptrCast(&usize, envp.ptr)[envp_count + 1];
+ const auxv = &@ptrCast(*usize, envp.ptr)[envp_count + 1];
var i: usize = 0;
while (auxv[i] != 0) : (i += 2) {
if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i + 1];
@@ -68,16 +68,16 @@ fn posixCallMainAndExit() noreturn {
std.os.posix.exit(callMainWithArgs(argc, argv, envp));
}
-fn callMainWithArgs(argc: usize, argv: &&u8, envp: []&u8) u8 {
+fn callMainWithArgs(argc: usize, argv: **u8, envp: []*u8) u8 {
std.os.ArgIteratorPosix.raw = argv[0..argc];
std.os.posix_environ_raw = envp;
return callMain();
}
-extern fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) i32 {
+extern fn main(c_argc: i32, c_argv: **u8, c_envp: *?*u8) i32 {
var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {}
- const envp = @ptrCast(&&u8, c_envp)[0..env_count];
+ const envp = @ptrCast(**u8, c_envp)[0..env_count];
return callMainWithArgs(usize(c_argc), c_argv, envp);
}
diff --git a/std/special/build_file_template.zig b/std/special/build_file_template.zig
index 1c06c93cdc..1e3eb01136 100644
--- a/std/special/build_file_template.zig
+++ b/std/special/build_file_template.zig
@@ -1,10 +1,10 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("YOUR_NAME_HERE", "src/main.zig");
exe.setBuildMode(mode);
- b.default_step.dependOn(&exe.step);
+ b.default_step.dependOn(*exe.step);
b.installArtifact(exe);
}
diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig
index 3ff11bbee4..3471d6ed21 100644
--- a/std/special/build_runner.zig
+++ b/std/special/build_runner.zig
@@ -129,7 +129,7 @@ pub fn main() !void {
};
}
-fn runBuild(builder: &Builder) error!void {
+fn runBuild(builder: *Builder) error!void {
switch (@typeId(@typeOf(root.build).ReturnType)) {
builtin.TypeId.Void => root.build(builder),
builtin.TypeId.ErrorUnion => try root.build(builder),
@@ -137,7 +137,7 @@ fn runBuild(builder: &Builder) error!void {
}
}
-fn usage(builder: &Builder, already_ran_build: bool, out_stream: var) !void {
+fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
// run the build script to collect the options
if (!already_ran_build) {
builder.setInstallPrefix(null);
@@ -195,7 +195,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: var) !void {
);
}
-fn usageAndErr(builder: &Builder, already_ran_build: bool, out_stream: var) error {
+fn usageAndErr(builder: *Builder, already_ran_build: bool, out_stream: var) error {
usage(builder, already_ran_build, out_stream) catch {};
return error.InvalidArgs;
}
diff --git a/std/special/builtin.zig b/std/special/builtin.zig
index 63149d5161..9c9cd35103 100644
--- a/std/special/builtin.zig
+++ b/std/special/builtin.zig
@@ -5,7 +5,7 @@ const builtin = @import("builtin");
// Avoid dragging in the runtime safety mechanisms into this .o file,
// unless we're trying to test this file.
-pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn {
+pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
if (builtin.is_test) {
@setCold(true);
@import("std").debug.panic("{}", msg);
@@ -14,7 +14,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn
}
}
-export fn memset(dest: ?&u8, c: u8, n: usize) ?&u8 {
+export fn memset(dest: ?*u8, c: u8, n: usize) ?*u8 {
@setRuntimeSafety(false);
var index: usize = 0;
@@ -24,7 +24,7 @@ export fn memset(dest: ?&u8, c: u8, n: usize) ?&u8 {
return dest;
}
-export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) ?&u8 {
+export fn memcpy(noalias dest: ?*u8, noalias src: ?*const u8, n: usize) ?*u8 {
@setRuntimeSafety(false);
var index: usize = 0;
@@ -34,7 +34,7 @@ export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) ?&u8 {
return dest;
}
-export fn memmove(dest: ?&u8, src: ?&const u8, n: usize) ?&u8 {
+export fn memmove(dest: ?*u8, src: ?*const u8, n: usize) ?*u8 {
@setRuntimeSafety(false);
if (@ptrToInt(dest) < @ptrToInt(src)) {
diff --git a/std/special/compiler_rt/index.zig b/std/special/compiler_rt/index.zig
index 3e014d4d16..d328324320 100644
--- a/std/special/compiler_rt/index.zig
+++ b/std/special/compiler_rt/index.zig
@@ -78,7 +78,7 @@ const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
// Avoid dragging in the runtime safety mechanisms into this .o file,
// unless we're trying to test this file.
-pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn {
+pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
@setCold(true);
if (is_test) {
std.debug.panic("{}", msg);
@@ -284,7 +284,7 @@ nakedcc fn ___chkstk_ms() align(4) void {
);
}
-extern fn __udivmodsi4(a: u32, b: u32, rem: &u32) u32 {
+extern fn __udivmodsi4(a: u32, b: u32, rem: *u32) u32 {
@setRuntimeSafety(is_test);
const d = __udivsi3(a, b);
diff --git a/std/special/compiler_rt/udivmod.zig b/std/special/compiler_rt/udivmod.zig
index 0dee5e45f6..894dd02239 100644
--- a/std/special/compiler_rt/udivmod.zig
+++ b/std/special/compiler_rt/udivmod.zig
@@ -7,15 +7,15 @@ const low = switch (builtin.endian) {
};
const high = 1 - low;
-pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) DoubleInt {
+pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?*DoubleInt) DoubleInt {
@setRuntimeSafety(is_test);
const SingleInt = @IntType(false, @divExact(DoubleInt.bit_count, 2));
const SignedDoubleInt = @IntType(true, DoubleInt.bit_count);
const Log2SingleInt = @import("std").math.Log2Int(SingleInt);
- const n = @ptrCast(&const [2]SingleInt, &a).*; // TODO issue #421
- const d = @ptrCast(&const [2]SingleInt, &b).*; // TODO issue #421
+ const n = @ptrCast(*const [2]SingleInt, &a).*; // TODO issue #421
+ const d = @ptrCast(*const [2]SingleInt, &b).*; // TODO issue #421
var q: [2]SingleInt = undefined;
var r: [2]SingleInt = undefined;
var sr: c_uint = undefined;
@@ -57,7 +57,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
if (maybe_rem) |rem| {
r[high] = n[high] % d[high];
r[low] = 0;
- rem.* = @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
+ rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
}
return n[high] / d[high];
}
@@ -69,7 +69,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
if (maybe_rem) |rem| {
r[low] = n[low];
r[high] = n[high] & (d[high] - 1);
- rem.* = @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
+ rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
}
return n[high] >> Log2SingleInt(@ctz(d[high]));
}
@@ -109,7 +109,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
sr = @ctz(d[low]);
q[high] = n[high] >> Log2SingleInt(sr);
q[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
- return @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
+ return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
}
// K X
// ---
@@ -183,13 +183,13 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
// r.all -= b;
// carry = 1;
// }
- r_all = @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
+ r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
const s: SignedDoubleInt = SignedDoubleInt(b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
carry = u32(s & 1);
r_all -= b & @bitCast(DoubleInt, s);
- r = @ptrCast(&[2]SingleInt, &r_all).*; // TODO issue #421
+ r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421
}
- const q_all = ((@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &q[0]).*) << 1) | carry; // TODO issue #421
+ const q_all = ((@ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*) << 1) | carry; // TODO issue #421
if (maybe_rem) |rem| {
rem.* = r_all;
}
diff --git a/std/special/compiler_rt/udivmoddi4.zig b/std/special/compiler_rt/udivmoddi4.zig
index 6cc54bb6bf..de86c845e5 100644
--- a/std/special/compiler_rt/udivmoddi4.zig
+++ b/std/special/compiler_rt/udivmoddi4.zig
@@ -1,7 +1,7 @@
const udivmod = @import("udivmod.zig").udivmod;
const builtin = @import("builtin");
-pub extern fn __udivmoddi4(a: u64, b: u64, maybe_rem: ?&u64) u64 {
+pub extern fn __udivmoddi4(a: u64, b: u64, maybe_rem: ?*u64) u64 {
@setRuntimeSafety(builtin.is_test);
return udivmod(u64, a, b, maybe_rem);
}
diff --git a/std/special/compiler_rt/udivmodti4.zig b/std/special/compiler_rt/udivmodti4.zig
index 816f82b900..3fa596442f 100644
--- a/std/special/compiler_rt/udivmodti4.zig
+++ b/std/special/compiler_rt/udivmodti4.zig
@@ -2,12 +2,12 @@ const udivmod = @import("udivmod.zig").udivmod;
const builtin = @import("builtin");
const compiler_rt = @import("index.zig");
-pub extern fn __udivmodti4(a: u128, b: u128, maybe_rem: ?&u128) u128 {
+pub extern fn __udivmodti4(a: u128, b: u128, maybe_rem: ?*u128) u128 {
@setRuntimeSafety(builtin.is_test);
return udivmod(u128, a, b, maybe_rem);
}
-pub extern fn __udivmodti4_windows_x86_64(a: &const u128, b: &const u128, maybe_rem: ?&u128) void {
+pub extern fn __udivmodti4_windows_x86_64(a: *const u128, b: *const u128, maybe_rem: ?*u128) void {
@setRuntimeSafety(builtin.is_test);
compiler_rt.setXmm0(u128, udivmod(u128, a.*, b.*, maybe_rem));
}
diff --git a/std/special/compiler_rt/udivti3.zig b/std/special/compiler_rt/udivti3.zig
index ad0f09e733..510e21ac1d 100644
--- a/std/special/compiler_rt/udivti3.zig
+++ b/std/special/compiler_rt/udivti3.zig
@@ -6,7 +6,7 @@ pub extern fn __udivti3(a: u128, b: u128) u128 {
return udivmodti4.__udivmodti4(a, b, null);
}
-pub extern fn __udivti3_windows_x86_64(a: &const u128, b: &const u128) void {
+pub extern fn __udivti3_windows_x86_64(a: *const u128, b: *const u128) void {
@setRuntimeSafety(builtin.is_test);
udivmodti4.__udivmodti4_windows_x86_64(a, b, null);
}
diff --git a/std/special/compiler_rt/umodti3.zig b/std/special/compiler_rt/umodti3.zig
index 11e2955bb3..9551e63a6f 100644
--- a/std/special/compiler_rt/umodti3.zig
+++ b/std/special/compiler_rt/umodti3.zig
@@ -9,7 +9,7 @@ pub extern fn __umodti3(a: u128, b: u128) u128 {
return r;
}
-pub extern fn __umodti3_windows_x86_64(a: &const u128, b: &const u128) void {
+pub extern fn __umodti3_windows_x86_64(a: *const u128, b: *const u128) void {
@setRuntimeSafety(builtin.is_test);
compiler_rt.setXmm0(u128, __umodti3(a.*, b.*));
}
diff --git a/std/special/panic.zig b/std/special/panic.zig
index 8f933ddd97..ca1caea73c 100644
--- a/std/special/panic.zig
+++ b/std/special/panic.zig
@@ -6,7 +6,7 @@
const builtin = @import("builtin");
const std = @import("std");
-pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn {
+pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
@setCold(true);
switch (builtin.os) {
// TODO: fix panic in zen.
diff --git a/std/unicode.zig b/std/unicode.zig
index 36f04778f4..3d1bebdb55 100644
--- a/std/unicode.zig
+++ b/std/unicode.zig
@@ -196,7 +196,7 @@ pub const Utf8View = struct {
}
}
- pub fn iterator(s: &const Utf8View) Utf8Iterator {
+ pub fn iterator(s: *const Utf8View) Utf8Iterator {
return Utf8Iterator{
.bytes = s.bytes,
.i = 0,
@@ -208,7 +208,7 @@ const Utf8Iterator = struct {
bytes: []const u8,
i: usize,
- pub fn nextCodepointSlice(it: &Utf8Iterator) ?[]const u8 {
+ pub fn nextCodepointSlice(it: *Utf8Iterator) ?[]const u8 {
if (it.i >= it.bytes.len) {
return null;
}
@@ -219,7 +219,7 @@ const Utf8Iterator = struct {
return it.bytes[it.i - cp_len .. it.i];
}
- pub fn nextCodepoint(it: &Utf8Iterator) ?u32 {
+ pub fn nextCodepoint(it: *Utf8Iterator) ?u32 {
const slice = it.nextCodepointSlice() ?? return null;
switch (slice.len) {
diff --git a/std/zig/ast.zig b/std/zig/ast.zig
index 56d4f9c393..4d25ceb7db 100644
--- a/std/zig/ast.zig
+++ b/std/zig/ast.zig
@@ -9,26 +9,26 @@ pub const TokenIndex = usize;
pub const Tree = struct {
source: []const u8,
tokens: TokenList,
- root_node: &Node.Root,
+ root_node: *Node.Root,
arena_allocator: std.heap.ArenaAllocator,
errors: ErrorList,
pub const TokenList = SegmentedList(Token, 64);
pub const ErrorList = SegmentedList(Error, 0);
- pub fn deinit(self: &Tree) void {
+ pub fn deinit(self: *Tree) void {
self.arena_allocator.deinit();
}
- pub fn renderError(self: &Tree, parse_error: &Error, stream: var) !void {
+ pub fn renderError(self: *Tree, parse_error: *Error, stream: var) !void {
return parse_error.render(&self.tokens, stream);
}
- pub fn tokenSlice(self: &Tree, token_index: TokenIndex) []const u8 {
+ pub fn tokenSlice(self: *Tree, token_index: TokenIndex) []const u8 {
return self.tokenSlicePtr(self.tokens.at(token_index));
}
- pub fn tokenSlicePtr(self: &Tree, token: &const Token) []const u8 {
+ pub fn tokenSlicePtr(self: *Tree, token: *const Token) []const u8 {
return self.source[token.start..token.end];
}
@@ -39,7 +39,7 @@ pub const Tree = struct {
line_end: usize,
};
- pub fn tokenLocationPtr(self: &Tree, start_index: usize, token: &const Token) Location {
+ pub fn tokenLocationPtr(self: *Tree, start_index: usize, token: *const Token) Location {
var loc = Location{
.line = 0,
.column = 0,
@@ -64,24 +64,24 @@ pub const Tree = struct {
return loc;
}
- pub fn tokenLocation(self: &Tree, start_index: usize, token_index: TokenIndex) Location {
+ pub fn tokenLocation(self: *Tree, start_index: usize, token_index: TokenIndex) Location {
return self.tokenLocationPtr(start_index, self.tokens.at(token_index));
}
- pub fn tokensOnSameLine(self: &Tree, token1_index: TokenIndex, token2_index: TokenIndex) bool {
+ pub fn tokensOnSameLine(self: *Tree, token1_index: TokenIndex, token2_index: TokenIndex) bool {
return self.tokensOnSameLinePtr(self.tokens.at(token1_index), self.tokens.at(token2_index));
}
- pub fn tokensOnSameLinePtr(self: &Tree, token1: &const Token, token2: &const Token) bool {
+ pub fn tokensOnSameLinePtr(self: *Tree, token1: *const Token, token2: *const Token) bool {
return mem.indexOfScalar(u8, self.source[token1.end..token2.start], '\n') == null;
}
- pub fn dump(self: &Tree) void {
+ pub fn dump(self: *Tree) void {
self.root_node.base.dump(0);
}
/// Skips over comments
- pub fn prevToken(self: &Tree, token_index: TokenIndex) TokenIndex {
+ pub fn prevToken(self: *Tree, token_index: TokenIndex) TokenIndex {
var index = token_index - 1;
while (self.tokens.at(index).id == Token.Id.LineComment) {
index -= 1;
@@ -90,7 +90,7 @@ pub const Tree = struct {
}
/// Skips over comments
- pub fn nextToken(self: &Tree, token_index: TokenIndex) TokenIndex {
+ pub fn nextToken(self: *Tree, token_index: TokenIndex) TokenIndex {
var index = token_index + 1;
while (self.tokens.at(index).id == Token.Id.LineComment) {
index += 1;
@@ -120,7 +120,7 @@ pub const Error = union(enum) {
ExpectedToken: ExpectedToken,
ExpectedCommaOrEnd: ExpectedCommaOrEnd,
- pub fn render(self: &const Error, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const Error, tokens: *Tree.TokenList, stream: var) !void {
switch (self.*) {
// TODO https://github.com/ziglang/zig/issues/683
@TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
@@ -145,7 +145,7 @@ pub const Error = union(enum) {
}
}
- pub fn loc(self: &const Error) TokenIndex {
+ pub fn loc(self: *const Error) TokenIndex {
switch (self.*) {
// TODO https://github.com/ziglang/zig/issues/683
@TagType(Error).InvalidToken => |x| return x.token,
@@ -188,17 +188,17 @@ pub const Error = union(enum) {
pub const ExtraVolatileQualifier = SimpleError("Extra volatile qualifier");
pub const ExpectedCall = struct {
- node: &Node,
+ node: *Node,
- pub fn render(self: &const ExpectedCall, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const ExpectedCall, tokens: *Tree.TokenList, stream: var) !void {
return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}", @tagName(self.node.id));
}
};
pub const ExpectedCallOrFnProto = struct {
- node: &Node,
+ node: *Node,
- pub fn render(self: &const ExpectedCallOrFnProto, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const ExpectedCallOrFnProto, tokens: *Tree.TokenList, stream: var) !void {
return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++ @tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
}
};
@@ -207,7 +207,7 @@ pub const Error = union(enum) {
token: TokenIndex,
expected_id: @TagType(Token.Id),
- pub fn render(self: &const ExpectedToken, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const ExpectedToken, tokens: *Tree.TokenList, stream: var) !void {
const token_name = @tagName(tokens.at(self.token).id);
return stream.print("expected {}, found {}", @tagName(self.expected_id), token_name);
}
@@ -217,7 +217,7 @@ pub const Error = union(enum) {
token: TokenIndex,
end_id: @TagType(Token.Id),
- pub fn render(self: &const ExpectedCommaOrEnd, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const ExpectedCommaOrEnd, tokens: *Tree.TokenList, stream: var) !void {
const token_name = @tagName(tokens.at(self.token).id);
return stream.print("expected ',' or {}, found {}", @tagName(self.end_id), token_name);
}
@@ -229,7 +229,7 @@ pub const Error = union(enum) {
token: TokenIndex,
- pub fn render(self: &const ThisError, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const ThisError, tokens: *Tree.TokenList, stream: var) !void {
const token_name = @tagName(tokens.at(self.token).id);
return stream.print(msg, token_name);
}
@@ -242,7 +242,7 @@ pub const Error = union(enum) {
token: TokenIndex,
- pub fn render(self: &const ThisError, tokens: &Tree.TokenList, stream: var) !void {
+ pub fn render(self: *const ThisError, tokens: *Tree.TokenList, stream: var) !void {
return stream.write(msg);
}
};
@@ -320,14 +320,14 @@ pub const Node = struct {
FieldInitializer,
};
- pub fn cast(base: &Node, comptime T: type) ?&T {
+ pub fn cast(base: *Node, comptime T: type) ?*T {
if (base.id == comptime typeToId(T)) {
return @fieldParentPtr(T, "base", base);
}
return null;
}
- pub fn iterate(base: &Node, index: usize) ?&Node {
+ pub fn iterate(base: *Node, index: usize) ?*Node {
comptime var i = 0;
inline while (i < @memberCount(Id)) : (i += 1) {
if (base.id == @field(Id, @memberName(Id, i))) {
@@ -338,7 +338,7 @@ pub const Node = struct {
unreachable;
}
- pub fn firstToken(base: &Node) TokenIndex {
+ pub fn firstToken(base: *Node) TokenIndex {
comptime var i = 0;
inline while (i < @memberCount(Id)) : (i += 1) {
if (base.id == @field(Id, @memberName(Id, i))) {
@@ -349,7 +349,7 @@ pub const Node = struct {
unreachable;
}
- pub fn lastToken(base: &Node) TokenIndex {
+ pub fn lastToken(base: *Node) TokenIndex {
comptime var i = 0;
inline while (i < @memberCount(Id)) : (i += 1) {
if (base.id == @field(Id, @memberName(Id, i))) {
@@ -370,7 +370,7 @@ pub const Node = struct {
unreachable;
}
- pub fn requireSemiColon(base: &const Node) bool {
+ pub fn requireSemiColon(base: *const Node) bool {
var n = base;
while (true) {
switch (n.id) {
@@ -443,7 +443,7 @@ pub const Node = struct {
}
}
- pub fn dump(self: &Node, indent: usize) void {
+ pub fn dump(self: *Node, indent: usize) void {
{
var i: usize = 0;
while (i < indent) : (i += 1) {
@@ -460,44 +460,44 @@ pub const Node = struct {
pub const Root = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
decls: DeclList,
eof_token: TokenIndex,
- pub const DeclList = SegmentedList(&Node, 4);
+ pub const DeclList = SegmentedList(*Node, 4);
- pub fn iterate(self: &Root, index: usize) ?&Node {
+ pub fn iterate(self: *Root, index: usize) ?*Node {
if (index < self.decls.len) {
return self.decls.at(index).*;
}
return null;
}
- pub fn firstToken(self: &Root) TokenIndex {
+ pub fn firstToken(self: *Root) TokenIndex {
return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
}
- pub fn lastToken(self: &Root) TokenIndex {
+ pub fn lastToken(self: *Root) TokenIndex {
return if (self.decls.len == 0) self.eof_token else (self.decls.at(self.decls.len - 1).*).lastToken();
}
};
pub const VarDecl = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
visib_token: ?TokenIndex,
name_token: TokenIndex,
eq_token: TokenIndex,
mut_token: TokenIndex,
comptime_token: ?TokenIndex,
extern_export_token: ?TokenIndex,
- lib_name: ?&Node,
- type_node: ?&Node,
- align_node: ?&Node,
- init_node: ?&Node,
+ lib_name: ?*Node,
+ type_node: ?*Node,
+ align_node: ?*Node,
+ init_node: ?*Node,
semicolon_token: TokenIndex,
- pub fn iterate(self: &VarDecl, index: usize) ?&Node {
+ pub fn iterate(self: *VarDecl, index: usize) ?*Node {
var i = index;
if (self.type_node) |type_node| {
@@ -518,7 +518,7 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &VarDecl) TokenIndex {
+ pub fn firstToken(self: *VarDecl) TokenIndex {
if (self.visib_token) |visib_token| return visib_token;
if (self.comptime_token) |comptime_token| return comptime_token;
if (self.extern_export_token) |extern_export_token| return extern_export_token;
@@ -526,20 +526,20 @@ pub const Node = struct {
return self.mut_token;
}
- pub fn lastToken(self: &VarDecl) TokenIndex {
+ pub fn lastToken(self: *VarDecl) TokenIndex {
return self.semicolon_token;
}
};
pub const Use = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
visib_token: ?TokenIndex,
use_token: TokenIndex,
- expr: &Node,
+ expr: *Node,
semicolon_token: TokenIndex,
- pub fn iterate(self: &Use, index: usize) ?&Node {
+ pub fn iterate(self: *Use, index: usize) ?*Node {
var i = index;
if (i < 1) return self.expr;
@@ -548,12 +548,12 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Use) TokenIndex {
+ pub fn firstToken(self: *Use) TokenIndex {
if (self.visib_token) |visib_token| return visib_token;
return self.use_token;
}
- pub fn lastToken(self: &Use) TokenIndex {
+ pub fn lastToken(self: *Use) TokenIndex {
return self.semicolon_token;
}
};
@@ -564,9 +564,9 @@ pub const Node = struct {
decls: DeclList,
rbrace_token: TokenIndex,
- pub const DeclList = SegmentedList(&Node, 2);
+ pub const DeclList = SegmentedList(*Node, 2);
- pub fn iterate(self: &ErrorSetDecl, index: usize) ?&Node {
+ pub fn iterate(self: *ErrorSetDecl, index: usize) ?*Node {
var i = index;
if (i < self.decls.len) return self.decls.at(i).*;
@@ -575,11 +575,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &ErrorSetDecl) TokenIndex {
+ pub fn firstToken(self: *ErrorSetDecl) TokenIndex {
return self.error_token;
}
- pub fn lastToken(self: &ErrorSetDecl) TokenIndex {
+ pub fn lastToken(self: *ErrorSetDecl) TokenIndex {
return self.rbrace_token;
}
};
@@ -597,11 +597,11 @@ pub const Node = struct {
const InitArg = union(enum) {
None,
- Enum: ?&Node,
- Type: &Node,
+ Enum: ?*Node,
+ Type: *Node,
};
- pub fn iterate(self: &ContainerDecl, index: usize) ?&Node {
+ pub fn iterate(self: *ContainerDecl, index: usize) ?*Node {
var i = index;
switch (self.init_arg_expr) {
@@ -618,26 +618,26 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &ContainerDecl) TokenIndex {
+ pub fn firstToken(self: *ContainerDecl) TokenIndex {
if (self.layout_token) |layout_token| {
return layout_token;
}
return self.kind_token;
}
- pub fn lastToken(self: &ContainerDecl) TokenIndex {
+ pub fn lastToken(self: *ContainerDecl) TokenIndex {
return self.rbrace_token;
}
};
pub const StructField = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
visib_token: ?TokenIndex,
name_token: TokenIndex,
- type_expr: &Node,
+ type_expr: *Node,
- pub fn iterate(self: &StructField, index: usize) ?&Node {
+ pub fn iterate(self: *StructField, index: usize) ?*Node {
var i = index;
if (i < 1) return self.type_expr;
@@ -646,24 +646,24 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &StructField) TokenIndex {
+ pub fn firstToken(self: *StructField) TokenIndex {
if (self.visib_token) |visib_token| return visib_token;
return self.name_token;
}
- pub fn lastToken(self: &StructField) TokenIndex {
+ pub fn lastToken(self: *StructField) TokenIndex {
return self.type_expr.lastToken();
}
};
pub const UnionTag = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
name_token: TokenIndex,
- type_expr: ?&Node,
- value_expr: ?&Node,
+ type_expr: ?*Node,
+ value_expr: ?*Node,
- pub fn iterate(self: &UnionTag, index: usize) ?&Node {
+ pub fn iterate(self: *UnionTag, index: usize) ?*Node {
var i = index;
if (self.type_expr) |type_expr| {
@@ -679,11 +679,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &UnionTag) TokenIndex {
+ pub fn firstToken(self: *UnionTag) TokenIndex {
return self.name_token;
}
- pub fn lastToken(self: &UnionTag) TokenIndex {
+ pub fn lastToken(self: *UnionTag) TokenIndex {
if (self.value_expr) |value_expr| {
return value_expr.lastToken();
}
@@ -697,11 +697,11 @@ pub const Node = struct {
pub const EnumTag = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
name_token: TokenIndex,
- value: ?&Node,
+ value: ?*Node,
- pub fn iterate(self: &EnumTag, index: usize) ?&Node {
+ pub fn iterate(self: *EnumTag, index: usize) ?*Node {
var i = index;
if (self.value) |value| {
@@ -712,11 +712,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &EnumTag) TokenIndex {
+ pub fn firstToken(self: *EnumTag) TokenIndex {
return self.name_token;
}
- pub fn lastToken(self: &EnumTag) TokenIndex {
+ pub fn lastToken(self: *EnumTag) TokenIndex {
if (self.value) |value| {
return value.lastToken();
}
@@ -727,25 +727,25 @@ pub const Node = struct {
pub const ErrorTag = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
name_token: TokenIndex,
- pub fn iterate(self: &ErrorTag, index: usize) ?&Node {
+ pub fn iterate(self: *ErrorTag, index: usize) ?*Node {
var i = index;
if (self.doc_comments) |comments| {
- if (i < 1) return &comments.base;
+ if (i < 1) return *comments.base;
i -= 1;
}
return null;
}
- pub fn firstToken(self: &ErrorTag) TokenIndex {
+ pub fn firstToken(self: *ErrorTag) TokenIndex {
return self.name_token;
}
- pub fn lastToken(self: &ErrorTag) TokenIndex {
+ pub fn lastToken(self: *ErrorTag) TokenIndex {
return self.name_token;
}
};
@@ -754,15 +754,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &Identifier, index: usize) ?&Node {
+ pub fn iterate(self: *Identifier, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &Identifier) TokenIndex {
+ pub fn firstToken(self: *Identifier) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &Identifier) TokenIndex {
+ pub fn lastToken(self: *Identifier) TokenIndex {
return self.token;
}
};
@@ -770,10 +770,10 @@ pub const Node = struct {
pub const AsyncAttribute = struct {
base: Node,
async_token: TokenIndex,
- allocator_type: ?&Node,
+ allocator_type: ?*Node,
rangle_bracket: ?TokenIndex,
- pub fn iterate(self: &AsyncAttribute, index: usize) ?&Node {
+ pub fn iterate(self: *AsyncAttribute, index: usize) ?*Node {
var i = index;
if (self.allocator_type) |allocator_type| {
@@ -784,11 +784,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &AsyncAttribute) TokenIndex {
+ pub fn firstToken(self: *AsyncAttribute) TokenIndex {
return self.async_token;
}
- pub fn lastToken(self: &AsyncAttribute) TokenIndex {
+ pub fn lastToken(self: *AsyncAttribute) TokenIndex {
if (self.rangle_bracket) |rangle_bracket| {
return rangle_bracket;
}
@@ -799,7 +799,7 @@ pub const Node = struct {
pub const FnProto = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
visib_token: ?TokenIndex,
fn_token: TokenIndex,
name_token: ?TokenIndex,
@@ -808,19 +808,19 @@ pub const Node = struct {
var_args_token: ?TokenIndex,
extern_export_inline_token: ?TokenIndex,
cc_token: ?TokenIndex,
- async_attr: ?&AsyncAttribute,
- body_node: ?&Node,
- lib_name: ?&Node, // populated if this is an extern declaration
- align_expr: ?&Node, // populated if align(A) is present
+ async_attr: ?*AsyncAttribute,
+ body_node: ?*Node,
+ lib_name: ?*Node, // populated if this is an extern declaration
+ align_expr: ?*Node, // populated if align(A) is present
- pub const ParamList = SegmentedList(&Node, 2);
+ pub const ParamList = SegmentedList(*Node, 2);
pub const ReturnType = union(enum) {
- Explicit: &Node,
- InferErrorSet: &Node,
+ Explicit: *Node,
+ InferErrorSet: *Node,
};
- pub fn iterate(self: &FnProto, index: usize) ?&Node {
+ pub fn iterate(self: *FnProto, index: usize) ?*Node {
var i = index;
if (self.lib_name) |lib_name| {
@@ -856,7 +856,7 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &FnProto) TokenIndex {
+ pub fn firstToken(self: *FnProto) TokenIndex {
if (self.visib_token) |visib_token| return visib_token;
if (self.extern_export_inline_token) |extern_export_inline_token| return extern_export_inline_token;
assert(self.lib_name == null);
@@ -864,7 +864,7 @@ pub const Node = struct {
return self.fn_token;
}
- pub fn lastToken(self: &FnProto) TokenIndex {
+ pub fn lastToken(self: *FnProto) TokenIndex {
if (self.body_node) |body_node| return body_node.lastToken();
switch (self.return_type) {
// TODO allow this and next prong to share bodies since the types are the same
@@ -881,10 +881,10 @@ pub const Node = struct {
pub const Result = struct {
arrow_token: TokenIndex,
- return_type: &Node,
+ return_type: *Node,
};
- pub fn iterate(self: &PromiseType, index: usize) ?&Node {
+ pub fn iterate(self: *PromiseType, index: usize) ?*Node {
var i = index;
if (self.result) |result| {
@@ -895,11 +895,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &PromiseType) TokenIndex {
+ pub fn firstToken(self: *PromiseType) TokenIndex {
return self.promise_token;
}
- pub fn lastToken(self: &PromiseType) TokenIndex {
+ pub fn lastToken(self: *PromiseType) TokenIndex {
if (self.result) |result| return result.return_type.lastToken();
return self.promise_token;
}
@@ -910,10 +910,10 @@ pub const Node = struct {
comptime_token: ?TokenIndex,
noalias_token: ?TokenIndex,
name_token: ?TokenIndex,
- type_node: &Node,
+ type_node: *Node,
var_args_token: ?TokenIndex,
- pub fn iterate(self: &ParamDecl, index: usize) ?&Node {
+ pub fn iterate(self: *ParamDecl, index: usize) ?*Node {
var i = index;
if (i < 1) return self.type_node;
@@ -922,14 +922,14 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &ParamDecl) TokenIndex {
+ pub fn firstToken(self: *ParamDecl) TokenIndex {
if (self.comptime_token) |comptime_token| return comptime_token;
if (self.noalias_token) |noalias_token| return noalias_token;
if (self.name_token) |name_token| return name_token;
return self.type_node.firstToken();
}
- pub fn lastToken(self: &ParamDecl) TokenIndex {
+ pub fn lastToken(self: *ParamDecl) TokenIndex {
if (self.var_args_token) |var_args_token| return var_args_token;
return self.type_node.lastToken();
}
@@ -944,7 +944,7 @@ pub const Node = struct {
pub const StatementList = Root.DeclList;
- pub fn iterate(self: &Block, index: usize) ?&Node {
+ pub fn iterate(self: *Block, index: usize) ?*Node {
var i = index;
if (i < self.statements.len) return self.statements.at(i).*;
@@ -953,7 +953,7 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Block) TokenIndex {
+ pub fn firstToken(self: *Block) TokenIndex {
if (self.label) |label| {
return label;
}
@@ -961,7 +961,7 @@ pub const Node = struct {
return self.lbrace;
}
- pub fn lastToken(self: &Block) TokenIndex {
+ pub fn lastToken(self: *Block) TokenIndex {
return self.rbrace;
}
};
@@ -970,14 +970,14 @@ pub const Node = struct {
base: Node,
defer_token: TokenIndex,
kind: Kind,
- expr: &Node,
+ expr: *Node,
const Kind = enum {
Error,
Unconditional,
};
- pub fn iterate(self: &Defer, index: usize) ?&Node {
+ pub fn iterate(self: *Defer, index: usize) ?*Node {
var i = index;
if (i < 1) return self.expr;
@@ -986,22 +986,22 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Defer) TokenIndex {
+ pub fn firstToken(self: *Defer) TokenIndex {
return self.defer_token;
}
- pub fn lastToken(self: &Defer) TokenIndex {
+ pub fn lastToken(self: *Defer) TokenIndex {
return self.expr.lastToken();
}
};
pub const Comptime = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
comptime_token: TokenIndex,
- expr: &Node,
+ expr: *Node,
- pub fn iterate(self: &Comptime, index: usize) ?&Node {
+ pub fn iterate(self: *Comptime, index: usize) ?*Node {
var i = index;
if (i < 1) return self.expr;
@@ -1010,11 +1010,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Comptime) TokenIndex {
+ pub fn firstToken(self: *Comptime) TokenIndex {
return self.comptime_token;
}
- pub fn lastToken(self: &Comptime) TokenIndex {
+ pub fn lastToken(self: *Comptime) TokenIndex {
return self.expr.lastToken();
}
};
@@ -1022,10 +1022,10 @@ pub const Node = struct {
pub const Payload = struct {
base: Node,
lpipe: TokenIndex,
- error_symbol: &Node,
+ error_symbol: *Node,
rpipe: TokenIndex,
- pub fn iterate(self: &Payload, index: usize) ?&Node {
+ pub fn iterate(self: *Payload, index: usize) ?*Node {
var i = index;
if (i < 1) return self.error_symbol;
@@ -1034,11 +1034,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Payload) TokenIndex {
+ pub fn firstToken(self: *Payload) TokenIndex {
return self.lpipe;
}
- pub fn lastToken(self: &Payload) TokenIndex {
+ pub fn lastToken(self: *Payload) TokenIndex {
return self.rpipe;
}
};
@@ -1047,10 +1047,10 @@ pub const Node = struct {
base: Node,
lpipe: TokenIndex,
ptr_token: ?TokenIndex,
- value_symbol: &Node,
+ value_symbol: *Node,
rpipe: TokenIndex,
- pub fn iterate(self: &PointerPayload, index: usize) ?&Node {
+ pub fn iterate(self: *PointerPayload, index: usize) ?*Node {
var i = index;
if (i < 1) return self.value_symbol;
@@ -1059,11 +1059,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &PointerPayload) TokenIndex {
+ pub fn firstToken(self: *PointerPayload) TokenIndex {
return self.lpipe;
}
- pub fn lastToken(self: &PointerPayload) TokenIndex {
+ pub fn lastToken(self: *PointerPayload) TokenIndex {
return self.rpipe;
}
};
@@ -1072,11 +1072,11 @@ pub const Node = struct {
base: Node,
lpipe: TokenIndex,
ptr_token: ?TokenIndex,
- value_symbol: &Node,
- index_symbol: ?&Node,
+ value_symbol: *Node,
+ index_symbol: ?*Node,
rpipe: TokenIndex,
- pub fn iterate(self: &PointerIndexPayload, index: usize) ?&Node {
+ pub fn iterate(self: *PointerIndexPayload, index: usize) ?*Node {
var i = index;
if (i < 1) return self.value_symbol;
@@ -1090,11 +1090,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &PointerIndexPayload) TokenIndex {
+ pub fn firstToken(self: *PointerIndexPayload) TokenIndex {
return self.lpipe;
}
- pub fn lastToken(self: &PointerIndexPayload) TokenIndex {
+ pub fn lastToken(self: *PointerIndexPayload) TokenIndex {
return self.rpipe;
}
};
@@ -1102,10 +1102,10 @@ pub const Node = struct {
pub const Else = struct {
base: Node,
else_token: TokenIndex,
- payload: ?&Node,
- body: &Node,
+ payload: ?*Node,
+ body: *Node,
- pub fn iterate(self: &Else, index: usize) ?&Node {
+ pub fn iterate(self: *Else, index: usize) ?*Node {
var i = index;
if (self.payload) |payload| {
@@ -1119,11 +1119,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Else) TokenIndex {
+ pub fn firstToken(self: *Else) TokenIndex {
return self.else_token;
}
- pub fn lastToken(self: &Else) TokenIndex {
+ pub fn lastToken(self: *Else) TokenIndex {
return self.body.lastToken();
}
};
@@ -1131,15 +1131,15 @@ pub const Node = struct {
pub const Switch = struct {
base: Node,
switch_token: TokenIndex,
- expr: &Node,
+ expr: *Node,
/// these must be SwitchCase nodes
cases: CaseList,
rbrace: TokenIndex,
- pub const CaseList = SegmentedList(&Node, 2);
+ pub const CaseList = SegmentedList(*Node, 2);
- pub fn iterate(self: &Switch, index: usize) ?&Node {
+ pub fn iterate(self: *Switch, index: usize) ?*Node {
var i = index;
if (i < 1) return self.expr;
@@ -1151,11 +1151,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Switch) TokenIndex {
+ pub fn firstToken(self: *Switch) TokenIndex {
return self.switch_token;
}
- pub fn lastToken(self: &Switch) TokenIndex {
+ pub fn lastToken(self: *Switch) TokenIndex {
return self.rbrace;
}
};
@@ -1164,12 +1164,12 @@ pub const Node = struct {
base: Node,
items: ItemList,
arrow_token: TokenIndex,
- payload: ?&Node,
- expr: &Node,
+ payload: ?*Node,
+ expr: *Node,
- pub const ItemList = SegmentedList(&Node, 1);
+ pub const ItemList = SegmentedList(*Node, 1);
- pub fn iterate(self: &SwitchCase, index: usize) ?&Node {
+ pub fn iterate(self: *SwitchCase, index: usize) ?*Node {
var i = index;
if (i < self.items.len) return self.items.at(i).*;
@@ -1186,11 +1186,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &SwitchCase) TokenIndex {
+ pub fn firstToken(self: *SwitchCase) TokenIndex {
return (self.items.at(0).*).firstToken();
}
- pub fn lastToken(self: &SwitchCase) TokenIndex {
+ pub fn lastToken(self: *SwitchCase) TokenIndex {
return self.expr.lastToken();
}
};
@@ -1199,15 +1199,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &SwitchElse, index: usize) ?&Node {
+ pub fn iterate(self: *SwitchElse, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &SwitchElse) TokenIndex {
+ pub fn firstToken(self: *SwitchElse) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &SwitchElse) TokenIndex {
+ pub fn lastToken(self: *SwitchElse) TokenIndex {
return self.token;
}
};
@@ -1217,13 +1217,13 @@ pub const Node = struct {
label: ?TokenIndex,
inline_token: ?TokenIndex,
while_token: TokenIndex,
- condition: &Node,
- payload: ?&Node,
- continue_expr: ?&Node,
- body: &Node,
- @"else": ?&Else,
+ condition: *Node,
+ payload: ?*Node,
+ continue_expr: ?*Node,
+ body: *Node,
+ @"else": ?*Else,
- pub fn iterate(self: &While, index: usize) ?&Node {
+ pub fn iterate(self: *While, index: usize) ?*Node {
var i = index;
if (i < 1) return self.condition;
@@ -1243,14 +1243,14 @@ pub const Node = struct {
i -= 1;
if (self.@"else") |@"else"| {
- if (i < 1) return &@"else".base;
+ if (i < 1) return *@"else".base;
i -= 1;
}
return null;
}
- pub fn firstToken(self: &While) TokenIndex {
+ pub fn firstToken(self: *While) TokenIndex {
if (self.label) |label| {
return label;
}
@@ -1262,7 +1262,7 @@ pub const Node = struct {
return self.while_token;
}
- pub fn lastToken(self: &While) TokenIndex {
+ pub fn lastToken(self: *While) TokenIndex {
if (self.@"else") |@"else"| {
return @"else".body.lastToken();
}
@@ -1276,12 +1276,12 @@ pub const Node = struct {
label: ?TokenIndex,
inline_token: ?TokenIndex,
for_token: TokenIndex,
- array_expr: &Node,
- payload: ?&Node,
- body: &Node,
- @"else": ?&Else,
+ array_expr: *Node,
+ payload: ?*Node,
+ body: *Node,
+ @"else": ?*Else,
- pub fn iterate(self: &For, index: usize) ?&Node {
+ pub fn iterate(self: *For, index: usize) ?*Node {
var i = index;
if (i < 1) return self.array_expr;
@@ -1296,14 +1296,14 @@ pub const Node = struct {
i -= 1;
if (self.@"else") |@"else"| {
- if (i < 1) return &@"else".base;
+ if (i < 1) return *@"else".base;
i -= 1;
}
return null;
}
- pub fn firstToken(self: &For) TokenIndex {
+ pub fn firstToken(self: *For) TokenIndex {
if (self.label) |label| {
return label;
}
@@ -1315,7 +1315,7 @@ pub const Node = struct {
return self.for_token;
}
- pub fn lastToken(self: &For) TokenIndex {
+ pub fn lastToken(self: *For) TokenIndex {
if (self.@"else") |@"else"| {
return @"else".body.lastToken();
}
@@ -1327,12 +1327,12 @@ pub const Node = struct {
pub const If = struct {
base: Node,
if_token: TokenIndex,
- condition: &Node,
- payload: ?&Node,
- body: &Node,
- @"else": ?&Else,
+ condition: *Node,
+ payload: ?*Node,
+ body: *Node,
+ @"else": ?*Else,
- pub fn iterate(self: &If, index: usize) ?&Node {
+ pub fn iterate(self: *If, index: usize) ?*Node {
var i = index;
if (i < 1) return self.condition;
@@ -1347,18 +1347,18 @@ pub const Node = struct {
i -= 1;
if (self.@"else") |@"else"| {
- if (i < 1) return &@"else".base;
+ if (i < 1) return *@"else".base;
i -= 1;
}
return null;
}
- pub fn firstToken(self: &If) TokenIndex {
+ pub fn firstToken(self: *If) TokenIndex {
return self.if_token;
}
- pub fn lastToken(self: &If) TokenIndex {
+ pub fn lastToken(self: *If) TokenIndex {
if (self.@"else") |@"else"| {
return @"else".body.lastToken();
}
@@ -1370,9 +1370,9 @@ pub const Node = struct {
pub const InfixOp = struct {
base: Node,
op_token: TokenIndex,
- lhs: &Node,
+ lhs: *Node,
op: Op,
- rhs: &Node,
+ rhs: *Node,
pub const Op = union(enum) {
Add,
@@ -1401,7 +1401,7 @@ pub const Node = struct {
BitXor,
BoolAnd,
BoolOr,
- Catch: ?&Node,
+ Catch: ?*Node,
Div,
EqualEqual,
ErrorUnion,
@@ -1420,7 +1420,7 @@ pub const Node = struct {
UnwrapMaybe,
};
- pub fn iterate(self: &InfixOp, index: usize) ?&Node {
+ pub fn iterate(self: *InfixOp, index: usize) ?*Node {
var i = index;
if (i < 1) return self.lhs;
@@ -1485,11 +1485,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &InfixOp) TokenIndex {
+ pub fn firstToken(self: *InfixOp) TokenIndex {
return self.lhs.firstToken();
}
- pub fn lastToken(self: &InfixOp) TokenIndex {
+ pub fn lastToken(self: *InfixOp) TokenIndex {
return self.rhs.lastToken();
}
};
@@ -1498,11 +1498,11 @@ pub const Node = struct {
base: Node,
op_token: TokenIndex,
op: Op,
- rhs: &Node,
+ rhs: *Node,
pub const Op = union(enum) {
AddrOf: AddrOfInfo,
- ArrayType: &Node,
+ ArrayType: *Node,
Await,
BitNot,
BoolNot,
@@ -1523,17 +1523,17 @@ pub const Node = struct {
volatile_token: ?TokenIndex,
pub const Align = struct {
- node: &Node,
+ node: *Node,
bit_range: ?BitRange,
pub const BitRange = struct {
- start: &Node,
- end: &Node,
+ start: *Node,
+ end: *Node,
};
};
};
- pub fn iterate(self: &PrefixOp, index: usize) ?&Node {
+ pub fn iterate(self: *PrefixOp, index: usize) ?*Node {
var i = index;
switch (self.op) {
@@ -1573,11 +1573,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &PrefixOp) TokenIndex {
+ pub fn firstToken(self: *PrefixOp) TokenIndex {
return self.op_token;
}
- pub fn lastToken(self: &PrefixOp) TokenIndex {
+ pub fn lastToken(self: *PrefixOp) TokenIndex {
return self.rhs.lastToken();
}
};
@@ -1586,9 +1586,9 @@ pub const Node = struct {
base: Node,
period_token: TokenIndex,
name_token: TokenIndex,
- expr: &Node,
+ expr: *Node,
- pub fn iterate(self: &FieldInitializer, index: usize) ?&Node {
+ pub fn iterate(self: *FieldInitializer, index: usize) ?*Node {
var i = index;
if (i < 1) return self.expr;
@@ -1597,45 +1597,45 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &FieldInitializer) TokenIndex {
+ pub fn firstToken(self: *FieldInitializer) TokenIndex {
return self.period_token;
}
- pub fn lastToken(self: &FieldInitializer) TokenIndex {
+ pub fn lastToken(self: *FieldInitializer) TokenIndex {
return self.expr.lastToken();
}
};
pub const SuffixOp = struct {
base: Node,
- lhs: &Node,
+ lhs: *Node,
op: Op,
rtoken: TokenIndex,
pub const Op = union(enum) {
Call: Call,
- ArrayAccess: &Node,
+ ArrayAccess: *Node,
Slice: Slice,
ArrayInitializer: InitList,
StructInitializer: InitList,
Deref,
- pub const InitList = SegmentedList(&Node, 2);
+ pub const InitList = SegmentedList(*Node, 2);
pub const Call = struct {
params: ParamList,
- async_attr: ?&AsyncAttribute,
+ async_attr: ?*AsyncAttribute,
- pub const ParamList = SegmentedList(&Node, 2);
+ pub const ParamList = SegmentedList(*Node, 2);
};
pub const Slice = struct {
- start: &Node,
- end: ?&Node,
+ start: *Node,
+ end: ?*Node,
};
};
- pub fn iterate(self: &SuffixOp, index: usize) ?&Node {
+ pub fn iterate(self: *SuffixOp, index: usize) ?*Node {
var i = index;
if (i < 1) return self.lhs;
@@ -1673,7 +1673,7 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &SuffixOp) TokenIndex {
+ pub fn firstToken(self: *SuffixOp) TokenIndex {
switch (self.op) {
@TagType(Op).Call => |*call_info| if (call_info.async_attr) |async_attr| return async_attr.firstToken(),
else => {},
@@ -1681,7 +1681,7 @@ pub const Node = struct {
return self.lhs.firstToken();
}
- pub fn lastToken(self: &SuffixOp) TokenIndex {
+ pub fn lastToken(self: *SuffixOp) TokenIndex {
return self.rtoken;
}
};
@@ -1689,10 +1689,10 @@ pub const Node = struct {
pub const GroupedExpression = struct {
base: Node,
lparen: TokenIndex,
- expr: &Node,
+ expr: *Node,
rparen: TokenIndex,
- pub fn iterate(self: &GroupedExpression, index: usize) ?&Node {
+ pub fn iterate(self: *GroupedExpression, index: usize) ?*Node {
var i = index;
if (i < 1) return self.expr;
@@ -1701,11 +1701,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &GroupedExpression) TokenIndex {
+ pub fn firstToken(self: *GroupedExpression) TokenIndex {
return self.lparen;
}
- pub fn lastToken(self: &GroupedExpression) TokenIndex {
+ pub fn lastToken(self: *GroupedExpression) TokenIndex {
return self.rparen;
}
};
@@ -1714,15 +1714,15 @@ pub const Node = struct {
base: Node,
ltoken: TokenIndex,
kind: Kind,
- rhs: ?&Node,
+ rhs: ?*Node,
const Kind = union(enum) {
- Break: ?&Node,
- Continue: ?&Node,
+ Break: ?*Node,
+ Continue: ?*Node,
Return,
};
- pub fn iterate(self: &ControlFlowExpression, index: usize) ?&Node {
+ pub fn iterate(self: *ControlFlowExpression, index: usize) ?*Node {
var i = index;
switch (self.kind) {
@@ -1749,11 +1749,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &ControlFlowExpression) TokenIndex {
+ pub fn firstToken(self: *ControlFlowExpression) TokenIndex {
return self.ltoken;
}
- pub fn lastToken(self: &ControlFlowExpression) TokenIndex {
+ pub fn lastToken(self: *ControlFlowExpression) TokenIndex {
if (self.rhs) |rhs| {
return rhs.lastToken();
}
@@ -1780,10 +1780,10 @@ pub const Node = struct {
base: Node,
label: ?TokenIndex,
suspend_token: TokenIndex,
- payload: ?&Node,
- body: ?&Node,
+ payload: ?*Node,
+ body: ?*Node,
- pub fn iterate(self: &Suspend, index: usize) ?&Node {
+ pub fn iterate(self: *Suspend, index: usize) ?*Node {
var i = index;
if (self.payload) |payload| {
@@ -1799,12 +1799,12 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &Suspend) TokenIndex {
+ pub fn firstToken(self: *Suspend) TokenIndex {
if (self.label) |label| return label;
return self.suspend_token;
}
- pub fn lastToken(self: &Suspend) TokenIndex {
+ pub fn lastToken(self: *Suspend) TokenIndex {
if (self.body) |body| {
return body.lastToken();
}
@@ -1821,15 +1821,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &IntegerLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *IntegerLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &IntegerLiteral) TokenIndex {
+ pub fn firstToken(self: *IntegerLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &IntegerLiteral) TokenIndex {
+ pub fn lastToken(self: *IntegerLiteral) TokenIndex {
return self.token;
}
};
@@ -1838,15 +1838,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &FloatLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *FloatLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &FloatLiteral) TokenIndex {
+ pub fn firstToken(self: *FloatLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &FloatLiteral) TokenIndex {
+ pub fn lastToken(self: *FloatLiteral) TokenIndex {
return self.token;
}
};
@@ -1857,9 +1857,9 @@ pub const Node = struct {
params: ParamList,
rparen_token: TokenIndex,
- pub const ParamList = SegmentedList(&Node, 2);
+ pub const ParamList = SegmentedList(*Node, 2);
- pub fn iterate(self: &BuiltinCall, index: usize) ?&Node {
+ pub fn iterate(self: *BuiltinCall, index: usize) ?*Node {
var i = index;
if (i < self.params.len) return self.params.at(i).*;
@@ -1868,11 +1868,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &BuiltinCall) TokenIndex {
+ pub fn firstToken(self: *BuiltinCall) TokenIndex {
return self.builtin_token;
}
- pub fn lastToken(self: &BuiltinCall) TokenIndex {
+ pub fn lastToken(self: *BuiltinCall) TokenIndex {
return self.rparen_token;
}
};
@@ -1881,15 +1881,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &StringLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *StringLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &StringLiteral) TokenIndex {
+ pub fn firstToken(self: *StringLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &StringLiteral) TokenIndex {
+ pub fn lastToken(self: *StringLiteral) TokenIndex {
return self.token;
}
};
@@ -1900,15 +1900,15 @@ pub const Node = struct {
pub const LineList = SegmentedList(TokenIndex, 4);
- pub fn iterate(self: &MultilineStringLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *MultilineStringLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &MultilineStringLiteral) TokenIndex {
+ pub fn firstToken(self: *MultilineStringLiteral) TokenIndex {
return self.lines.at(0).*;
}
- pub fn lastToken(self: &MultilineStringLiteral) TokenIndex {
+ pub fn lastToken(self: *MultilineStringLiteral) TokenIndex {
return self.lines.at(self.lines.len - 1).*;
}
};
@@ -1917,15 +1917,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &CharLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *CharLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &CharLiteral) TokenIndex {
+ pub fn firstToken(self: *CharLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &CharLiteral) TokenIndex {
+ pub fn lastToken(self: *CharLiteral) TokenIndex {
return self.token;
}
};
@@ -1934,15 +1934,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &BoolLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *BoolLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &BoolLiteral) TokenIndex {
+ pub fn firstToken(self: *BoolLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &BoolLiteral) TokenIndex {
+ pub fn lastToken(self: *BoolLiteral) TokenIndex {
return self.token;
}
};
@@ -1951,15 +1951,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &NullLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *NullLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &NullLiteral) TokenIndex {
+ pub fn firstToken(self: *NullLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &NullLiteral) TokenIndex {
+ pub fn lastToken(self: *NullLiteral) TokenIndex {
return self.token;
}
};
@@ -1968,15 +1968,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &UndefinedLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *UndefinedLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &UndefinedLiteral) TokenIndex {
+ pub fn firstToken(self: *UndefinedLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &UndefinedLiteral) TokenIndex {
+ pub fn lastToken(self: *UndefinedLiteral) TokenIndex {
return self.token;
}
};
@@ -1985,15 +1985,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &ThisLiteral, index: usize) ?&Node {
+ pub fn iterate(self: *ThisLiteral, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &ThisLiteral) TokenIndex {
+ pub fn firstToken(self: *ThisLiteral) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &ThisLiteral) TokenIndex {
+ pub fn lastToken(self: *ThisLiteral) TokenIndex {
return self.token;
}
};
@@ -2001,17 +2001,17 @@ pub const Node = struct {
pub const AsmOutput = struct {
base: Node,
lbracket: TokenIndex,
- symbolic_name: &Node,
- constraint: &Node,
+ symbolic_name: *Node,
+ constraint: *Node,
kind: Kind,
rparen: TokenIndex,
const Kind = union(enum) {
- Variable: &Identifier,
- Return: &Node,
+ Variable: *Identifier,
+ Return: *Node,
};
- pub fn iterate(self: &AsmOutput, index: usize) ?&Node {
+ pub fn iterate(self: *AsmOutput, index: usize) ?*Node {
var i = index;
if (i < 1) return self.symbolic_name;
@@ -2022,7 +2022,7 @@ pub const Node = struct {
switch (self.kind) {
Kind.Variable => |variable_name| {
- if (i < 1) return &variable_name.base;
+ if (i < 1) return *variable_name.base;
i -= 1;
},
Kind.Return => |return_type| {
@@ -2034,11 +2034,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &AsmOutput) TokenIndex {
+ pub fn firstToken(self: *AsmOutput) TokenIndex {
return self.lbracket;
}
- pub fn lastToken(self: &AsmOutput) TokenIndex {
+ pub fn lastToken(self: *AsmOutput) TokenIndex {
return self.rparen;
}
};
@@ -2046,12 +2046,12 @@ pub const Node = struct {
pub const AsmInput = struct {
base: Node,
lbracket: TokenIndex,
- symbolic_name: &Node,
- constraint: &Node,
- expr: &Node,
+ symbolic_name: *Node,
+ constraint: *Node,
+ expr: *Node,
rparen: TokenIndex,
- pub fn iterate(self: &AsmInput, index: usize) ?&Node {
+ pub fn iterate(self: *AsmInput, index: usize) ?*Node {
var i = index;
if (i < 1) return self.symbolic_name;
@@ -2066,11 +2066,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &AsmInput) TokenIndex {
+ pub fn firstToken(self: *AsmInput) TokenIndex {
return self.lbracket;
}
- pub fn lastToken(self: &AsmInput) TokenIndex {
+ pub fn lastToken(self: *AsmInput) TokenIndex {
return self.rparen;
}
};
@@ -2079,33 +2079,33 @@ pub const Node = struct {
base: Node,
asm_token: TokenIndex,
volatile_token: ?TokenIndex,
- template: &Node,
+ template: *Node,
outputs: OutputList,
inputs: InputList,
clobbers: ClobberList,
rparen: TokenIndex,
- const OutputList = SegmentedList(&AsmOutput, 2);
- const InputList = SegmentedList(&AsmInput, 2);
+ const OutputList = SegmentedList(*AsmOutput, 2);
+ const InputList = SegmentedList(*AsmInput, 2);
const ClobberList = SegmentedList(TokenIndex, 2);
- pub fn iterate(self: &Asm, index: usize) ?&Node {
+ pub fn iterate(self: *Asm, index: usize) ?*Node {
var i = index;
- if (i < self.outputs.len) return &(self.outputs.at(index).*).base;
+ if (i < self.outputs.len) return *(self.outputs.at(index).*).base;
i -= self.outputs.len;
- if (i < self.inputs.len) return &(self.inputs.at(index).*).base;
+ if (i < self.inputs.len) return *(self.inputs.at(index).*).base;
i -= self.inputs.len;
return null;
}
- pub fn firstToken(self: &Asm) TokenIndex {
+ pub fn firstToken(self: *Asm) TokenIndex {
return self.asm_token;
}
- pub fn lastToken(self: &Asm) TokenIndex {
+ pub fn lastToken(self: *Asm) TokenIndex {
return self.rparen;
}
};
@@ -2114,15 +2114,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &Unreachable, index: usize) ?&Node {
+ pub fn iterate(self: *Unreachable, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &Unreachable) TokenIndex {
+ pub fn firstToken(self: *Unreachable) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &Unreachable) TokenIndex {
+ pub fn lastToken(self: *Unreachable) TokenIndex {
return self.token;
}
};
@@ -2131,15 +2131,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &ErrorType, index: usize) ?&Node {
+ pub fn iterate(self: *ErrorType, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &ErrorType) TokenIndex {
+ pub fn firstToken(self: *ErrorType) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &ErrorType) TokenIndex {
+ pub fn lastToken(self: *ErrorType) TokenIndex {
return self.token;
}
};
@@ -2148,15 +2148,15 @@ pub const Node = struct {
base: Node,
token: TokenIndex,
- pub fn iterate(self: &VarType, index: usize) ?&Node {
+ pub fn iterate(self: *VarType, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &VarType) TokenIndex {
+ pub fn firstToken(self: *VarType) TokenIndex {
return self.token;
}
- pub fn lastToken(self: &VarType) TokenIndex {
+ pub fn lastToken(self: *VarType) TokenIndex {
return self.token;
}
};
@@ -2167,27 +2167,27 @@ pub const Node = struct {
pub const LineList = SegmentedList(TokenIndex, 4);
- pub fn iterate(self: &DocComment, index: usize) ?&Node {
+ pub fn iterate(self: *DocComment, index: usize) ?*Node {
return null;
}
- pub fn firstToken(self: &DocComment) TokenIndex {
+ pub fn firstToken(self: *DocComment) TokenIndex {
return self.lines.at(0).*;
}
- pub fn lastToken(self: &DocComment) TokenIndex {
+ pub fn lastToken(self: *DocComment) TokenIndex {
return self.lines.at(self.lines.len - 1).*;
}
};
pub const TestDecl = struct {
base: Node,
- doc_comments: ?&DocComment,
+ doc_comments: ?*DocComment,
test_token: TokenIndex,
- name: &Node,
- body_node: &Node,
+ name: *Node,
+ body_node: *Node,
- pub fn iterate(self: &TestDecl, index: usize) ?&Node {
+ pub fn iterate(self: *TestDecl, index: usize) ?*Node {
var i = index;
if (i < 1) return self.body_node;
@@ -2196,11 +2196,11 @@ pub const Node = struct {
return null;
}
- pub fn firstToken(self: &TestDecl) TokenIndex {
+ pub fn firstToken(self: *TestDecl) TokenIndex {
return self.test_token;
}
- pub fn lastToken(self: &TestDecl) TokenIndex {
+ pub fn lastToken(self: *TestDecl) TokenIndex {
return self.body_node.lastToken();
}
};
diff --git a/std/zig/bench.zig b/std/zig/bench.zig
index c3b6b0d3d3..59392889a6 100644
--- a/std/zig/bench.zig
+++ b/std/zig/bench.zig
@@ -24,15 +24,15 @@ pub fn main() !void {
const mb_per_sec = bytes_per_sec / (1024 * 1024);
var stdout_file = try std.io.getStdOut();
- const stdout = &std.io.FileOutStream.init(&stdout_file).stream;
+ const stdout = *std.io.FileOutStream.init(*stdout_file).stream;
try stdout.print("{.3} MB/s, {} KB used \n", mb_per_sec, memory_used / 1024);
}
fn testOnce() usize {
var fixed_buf_alloc = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
- var allocator = &fixed_buf_alloc.allocator;
+ var allocator = *fixed_buf_alloc.allocator;
var tokenizer = Tokenizer.init(source);
- var parser = Parser.init(&tokenizer, allocator, "(memory buffer)");
+ var parser = Parser.init(*tokenizer, allocator, "(memory buffer)");
_ = parser.parse() catch @panic("parse failure");
return fixed_buf_alloc.end_index;
}
diff --git a/std/zig/parse.zig b/std/zig/parse.zig
index 05554f5d34..6d29300aed 100644
--- a/std/zig/parse.zig
+++ b/std/zig/parse.zig
@@ -9,7 +9,7 @@ const Error = ast.Error;
/// Result should be freed with tree.deinit() when there are
/// no more references to any of the tokens or nodes.
-pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
+pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
var tree_arena = std.heap.ArenaAllocator.init(allocator);
errdefer tree_arena.deinit();
@@ -2754,16 +2754,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
}
const AnnotatedToken = struct {
- ptr: &Token,
+ ptr: *Token,
index: TokenIndex,
};
const TopLevelDeclCtx = struct {
- decls: &ast.Node.Root.DeclList,
+ decls: *ast.Node.Root.DeclList,
visib_token: ?TokenIndex,
extern_export_inline_token: ?AnnotatedToken,
- lib_name: ?&ast.Node,
- comments: ?&ast.Node.DocComment,
+ lib_name: ?*ast.Node,
+ comments: ?*ast.Node.DocComment,
};
const VarDeclCtx = struct {
@@ -2771,21 +2771,21 @@ const VarDeclCtx = struct {
visib_token: ?TokenIndex,
comptime_token: ?TokenIndex,
extern_export_token: ?TokenIndex,
- lib_name: ?&ast.Node,
- list: &ast.Node.Root.DeclList,
- comments: ?&ast.Node.DocComment,
+ lib_name: ?*ast.Node,
+ list: *ast.Node.Root.DeclList,
+ comments: ?*ast.Node.DocComment,
};
const TopLevelExternOrFieldCtx = struct {
visib_token: TokenIndex,
- container_decl: &ast.Node.ContainerDecl,
- comments: ?&ast.Node.DocComment,
+ container_decl: *ast.Node.ContainerDecl,
+ comments: ?*ast.Node.DocComment,
};
const ExternTypeCtx = struct {
opt_ctx: OptionalCtx,
extern_token: TokenIndex,
- comments: ?&ast.Node.DocComment,
+ comments: ?*ast.Node.DocComment,
};
const ContainerKindCtx = struct {
@@ -2795,24 +2795,24 @@ const ContainerKindCtx = struct {
const ExpectTokenSave = struct {
id: @TagType(Token.Id),
- ptr: &TokenIndex,
+ ptr: *TokenIndex,
};
const OptionalTokenSave = struct {
id: @TagType(Token.Id),
- ptr: &?TokenIndex,
+ ptr: *?TokenIndex,
};
const ExprListCtx = struct {
- list: &ast.Node.SuffixOp.Op.InitList,
+ list: *ast.Node.SuffixOp.Op.InitList,
end: Token.Id,
- ptr: &TokenIndex,
+ ptr: *TokenIndex,
};
fn ListSave(comptime List: type) type {
return struct {
- list: &List,
- ptr: &TokenIndex,
+ list: *List,
+ ptr: *TokenIndex,
};
}
@@ -2841,7 +2841,7 @@ const LoopCtx = struct {
const AsyncEndCtx = struct {
ctx: OptionalCtx,
- attribute: &ast.Node.AsyncAttribute,
+ attribute: *ast.Node.AsyncAttribute,
};
const ErrorTypeOrSetDeclCtx = struct {
@@ -2850,21 +2850,21 @@ const ErrorTypeOrSetDeclCtx = struct {
};
const ParamDeclEndCtx = struct {
- fn_proto: &ast.Node.FnProto,
- param_decl: &ast.Node.ParamDecl,
+ fn_proto: *ast.Node.FnProto,
+ param_decl: *ast.Node.ParamDecl,
};
const ComptimeStatementCtx = struct {
comptime_token: TokenIndex,
- block: &ast.Node.Block,
+ block: *ast.Node.Block,
};
const OptionalCtx = union(enum) {
- Optional: &?&ast.Node,
- RequiredNull: &?&ast.Node,
- Required: &&ast.Node,
+ Optional: *?*ast.Node,
+ RequiredNull: *?*ast.Node,
+ Required: **ast.Node,
- pub fn store(self: &const OptionalCtx, value: &ast.Node) void {
+ pub fn store(self: *const OptionalCtx, value: *ast.Node) void {
switch (self.*) {
OptionalCtx.Optional => |ptr| ptr.* = value,
OptionalCtx.RequiredNull => |ptr| ptr.* = value,
@@ -2872,7 +2872,7 @@ const OptionalCtx = union(enum) {
}
}
- pub fn get(self: &const OptionalCtx) ?&ast.Node {
+ pub fn get(self: *const OptionalCtx) ?*ast.Node {
switch (self.*) {
OptionalCtx.Optional => |ptr| return ptr.*,
OptionalCtx.RequiredNull => |ptr| return ??ptr.*,
@@ -2880,7 +2880,7 @@ const OptionalCtx = union(enum) {
}
}
- pub fn toRequired(self: &const OptionalCtx) OptionalCtx {
+ pub fn toRequired(self: *const OptionalCtx) OptionalCtx {
switch (self.*) {
OptionalCtx.Optional => |ptr| {
return OptionalCtx{ .RequiredNull = ptr };
@@ -2892,8 +2892,8 @@ const OptionalCtx = union(enum) {
};
const AddCommentsCtx = struct {
- node_ptr: &&ast.Node,
- comments: ?&ast.Node.DocComment,
+ node_ptr: **ast.Node,
+ comments: ?*ast.Node.DocComment,
};
const State = union(enum) {
@@ -2904,67 +2904,67 @@ const State = union(enum) {
TopLevelExternOrField: TopLevelExternOrFieldCtx,
ContainerKind: ContainerKindCtx,
- ContainerInitArgStart: &ast.Node.ContainerDecl,
- ContainerInitArg: &ast.Node.ContainerDecl,
- ContainerDecl: &ast.Node.ContainerDecl,
+ ContainerInitArgStart: *ast.Node.ContainerDecl,
+ ContainerInitArg: *ast.Node.ContainerDecl,
+ ContainerDecl: *ast.Node.ContainerDecl,
VarDecl: VarDeclCtx,
- VarDeclAlign: &ast.Node.VarDecl,
- VarDeclEq: &ast.Node.VarDecl,
- VarDeclSemiColon: &ast.Node.VarDecl,
-
- FnDef: &ast.Node.FnProto,
- FnProto: &ast.Node.FnProto,
- FnProtoAlign: &ast.Node.FnProto,
- FnProtoReturnType: &ast.Node.FnProto,
-
- ParamDecl: &ast.Node.FnProto,
- ParamDeclAliasOrComptime: &ast.Node.ParamDecl,
- ParamDeclName: &ast.Node.ParamDecl,
+ VarDeclAlign: *ast.Node.VarDecl,
+ VarDeclEq: *ast.Node.VarDecl,
+ VarDeclSemiColon: *ast.Node.VarDecl,
+
+ FnDef: *ast.Node.FnProto,
+ FnProto: *ast.Node.FnProto,
+ FnProtoAlign: *ast.Node.FnProto,
+ FnProtoReturnType: *ast.Node.FnProto,
+
+ ParamDecl: *ast.Node.FnProto,
+ ParamDeclAliasOrComptime: *ast.Node.ParamDecl,
+ ParamDeclName: *ast.Node.ParamDecl,
ParamDeclEnd: ParamDeclEndCtx,
- ParamDeclComma: &ast.Node.FnProto,
+ ParamDeclComma: *ast.Node.FnProto,
MaybeLabeledExpression: MaybeLabeledExpressionCtx,
LabeledExpression: LabelCtx,
Inline: InlineCtx,
While: LoopCtx,
- WhileContinueExpr: &?&ast.Node,
+ WhileContinueExpr: *?*ast.Node,
For: LoopCtx,
- Else: &?&ast.Node.Else,
+ Else: *?*ast.Node.Else,
- Block: &ast.Node.Block,
- Statement: &ast.Node.Block,
+ Block: *ast.Node.Block,
+ Statement: *ast.Node.Block,
ComptimeStatement: ComptimeStatementCtx,
- Semicolon: &&ast.Node,
+ Semicolon: **ast.Node,
- AsmOutputItems: &ast.Node.Asm.OutputList,
- AsmOutputReturnOrType: &ast.Node.AsmOutput,
- AsmInputItems: &ast.Node.Asm.InputList,
- AsmClobberItems: &ast.Node.Asm.ClobberList,
+ AsmOutputItems: *ast.Node.Asm.OutputList,
+ AsmOutputReturnOrType: *ast.Node.AsmOutput,
+ AsmInputItems: *ast.Node.Asm.InputList,
+ AsmClobberItems: *ast.Node.Asm.ClobberList,
ExprListItemOrEnd: ExprListCtx,
ExprListCommaOrEnd: ExprListCtx,
FieldInitListItemOrEnd: ListSave(ast.Node.SuffixOp.Op.InitList),
FieldInitListCommaOrEnd: ListSave(ast.Node.SuffixOp.Op.InitList),
- FieldListCommaOrEnd: &ast.Node.ContainerDecl,
+ FieldListCommaOrEnd: *ast.Node.ContainerDecl,
FieldInitValue: OptionalCtx,
ErrorTagListItemOrEnd: ListSave(ast.Node.ErrorSetDecl.DeclList),
ErrorTagListCommaOrEnd: ListSave(ast.Node.ErrorSetDecl.DeclList),
SwitchCaseOrEnd: ListSave(ast.Node.Switch.CaseList),
SwitchCaseCommaOrEnd: ListSave(ast.Node.Switch.CaseList),
- SwitchCaseFirstItem: &ast.Node.SwitchCase,
- SwitchCaseItemCommaOrEnd: &ast.Node.SwitchCase,
- SwitchCaseItemOrEnd: &ast.Node.SwitchCase,
+ SwitchCaseFirstItem: *ast.Node.SwitchCase,
+ SwitchCaseItemCommaOrEnd: *ast.Node.SwitchCase,
+ SwitchCaseItemOrEnd: *ast.Node.SwitchCase,
- SuspendBody: &ast.Node.Suspend,
- AsyncAllocator: &ast.Node.AsyncAttribute,
+ SuspendBody: *ast.Node.Suspend,
+ AsyncAllocator: *ast.Node.AsyncAttribute,
AsyncEnd: AsyncEndCtx,
ExternType: ExternTypeCtx,
- SliceOrArrayAccess: &ast.Node.SuffixOp,
- SliceOrArrayType: &ast.Node.PrefixOp,
- AddrOfModifiers: &ast.Node.PrefixOp.AddrOfInfo,
- AlignBitRange: &ast.Node.PrefixOp.AddrOfInfo.Align,
+ SliceOrArrayAccess: *ast.Node.SuffixOp,
+ SliceOrArrayType: *ast.Node.PrefixOp,
+ AddrOfModifiers: *ast.Node.PrefixOp.AddrOfInfo,
+ AlignBitRange: *ast.Node.PrefixOp.AddrOfInfo.Align,
Payload: OptionalCtx,
PointerPayload: OptionalCtx,
@@ -3007,7 +3007,7 @@ const State = union(enum) {
ErrorTypeOrSetDecl: ErrorTypeOrSetDeclCtx,
StringLiteral: OptionalCtx,
Identifier: OptionalCtx,
- ErrorTag: &&ast.Node,
+ ErrorTag: **ast.Node,
IfToken: @TagType(Token.Id),
IfTokenSave: ExpectTokenSave,
@@ -3016,7 +3016,7 @@ const State = union(enum) {
OptionalTokenSave: OptionalTokenSave,
};
-fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void {
+fn pushDocComment(arena: *mem.Allocator, line_comment: TokenIndex, result: *?*ast.Node.DocComment) !void {
const node = blk: {
if (result.*) |comment_node| {
break :blk comment_node;
@@ -3032,8 +3032,8 @@ fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&as
try node.lines.push(line_comment);
}
-fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment {
- var result: ?&ast.Node.DocComment = null;
+fn eatDocComments(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree) !?*ast.Node.DocComment {
+ var result: ?*ast.Node.DocComment = null;
while (true) {
if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| {
try pushDocComment(arena, line_comment, &result);
@@ -3044,7 +3044,7 @@ fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, t
return result;
}
-fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, token_ptr: &const Token, token_index: TokenIndex, tree: &ast.Tree) !?&ast.Node {
+fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterator, token_ptr: *const Token, token_index: TokenIndex, tree: *ast.Tree) !?*ast.Node {
switch (token_ptr.id) {
Token.Id.StringLiteral => {
return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base;
@@ -3071,11 +3071,11 @@ fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterato
},
// TODO: We shouldn't need a cast, but:
// zig: /home/jc/Documents/zig/src/ir.cpp:7962: TypeTableEntry* ir_resolve_peer_types(IrAnalyze*, AstNode*, IrInstruction**, size_t): Assertion `err_set_type != nullptr' failed.
- else => return (?&ast.Node)(null),
+ else => return (?*ast.Node)(null),
}
}
-fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &const OptionalCtx, token_ptr: &const Token, token_index: TokenIndex) !bool {
+fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *const OptionalCtx, token_ptr: *const Token, token_index: TokenIndex) !bool {
switch (token_ptr.id) {
Token.Id.Keyword_suspend => {
const node = try arena.construct(ast.Node.Suspend{
@@ -3189,7 +3189,7 @@ const ExpectCommaOrEndResult = union(enum) {
parse_error: Error,
};
-fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end: @TagType(Token.Id)) ExpectCommaOrEndResult {
+fn expectCommaOrEnd(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree, end: @TagType(Token.Id)) ExpectCommaOrEndResult {
const token = nextToken(tok_it, tree);
const token_index = token.index;
const token_ptr = token.ptr;
@@ -3212,7 +3212,7 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:
}
}
-fn tokenIdToAssignment(id: &const Token.Id) ?ast.Node.InfixOp.Op {
+fn tokenIdToAssignment(id: *const Token.Id) ?ast.Node.InfixOp.Op {
// TODO: We have to cast all cases because of this:
// error: expected type '?InfixOp', found '?@TagType(InfixOp)'
return switch (id.*) {
@@ -3307,21 +3307,21 @@ fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
};
}
-fn createLiteral(arena: &mem.Allocator, comptime T: type, token_index: TokenIndex) !&T {
+fn createLiteral(arena: *mem.Allocator, comptime T: type, token_index: TokenIndex) !*T {
return arena.construct(T{
.base = ast.Node{ .id = ast.Node.typeToId(T) },
.token = token_index,
});
}
-fn createToCtxLiteral(arena: &mem.Allocator, opt_ctx: &const OptionalCtx, comptime T: type, token_index: TokenIndex) !&T {
+fn createToCtxLiteral(arena: *mem.Allocator, opt_ctx: *const OptionalCtx, comptime T: type, token_index: TokenIndex) !*T {
const node = try createLiteral(arena, T, token_index);
opt_ctx.store(&node.base);
return node;
}
-fn eatToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, id: @TagType(Token.Id)) ?TokenIndex {
+fn eatToken(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree, id: @TagType(Token.Id)) ?TokenIndex {
const token = ??tok_it.peek();
if (token.id == id) {
@@ -3331,7 +3331,7 @@ fn eatToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, id: @TagType(
return null;
}
-fn nextToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) AnnotatedToken {
+fn nextToken(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree) AnnotatedToken {
const result = AnnotatedToken{
.index = tok_it.index,
.ptr = ??tok_it.next(),
@@ -3345,7 +3345,7 @@ fn nextToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) AnnotatedTok
}
}
-fn prevToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) void {
+fn prevToken(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree) void {
while (true) {
const prev_tok = tok_it.prev() ?? return;
if (prev_tok.id == Token.Id.LineComment) continue;
diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig
index 69903bc3fd..8507470bcc 100644
--- a/std/zig/parser_test.zig
+++ b/std/zig/parser_test.zig
@@ -1803,7 +1803,7 @@ const io = std.io;
var fixed_buffer_mem: [100 * 1024]u8 = undefined;
-fn testParse(source: []const u8, allocator: &mem.Allocator, anything_changed: &bool) ![]u8 {
+fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *bool) ![]u8 {
var stderr_file = try io.getStdErr();
var stderr = &io.FileOutStream.init(&stderr_file).stream;
diff --git a/std/zig/render.zig b/std/zig/render.zig
index ac07917ff1..07e01241b7 100644
--- a/std/zig/render.zig
+++ b/std/zig/render.zig
@@ -13,7 +13,7 @@ pub const Error = error{
};
/// Returns whether anything changed
-pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(stream).Child.Error || Error)!bool {
+pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@typeOf(stream).Child.Error || Error)!bool {
comptime assert(@typeId(@typeOf(stream)) == builtin.TypeId.Pointer);
var anything_changed: bool = false;
@@ -24,13 +24,13 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
const StreamError = @typeOf(stream).Child.Error;
const Stream = std.io.OutStream(StreamError);
- anything_changed_ptr: &bool,
+ anything_changed_ptr: *bool,
child_stream: @typeOf(stream),
stream: Stream,
source_index: usize,
source: []const u8,
- fn write(iface_stream: &Stream, bytes: []const u8) StreamError!void {
+ fn write(iface_stream: *Stream, bytes: []const u8) StreamError!void {
const self = @fieldParentPtr(MyStream, "stream", iface_stream);
if (!self.anything_changed_ptr.*) {
@@ -63,9 +63,9 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
}
fn renderRoot(
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
stream: var,
- tree: &ast.Tree,
+ tree: *ast.Tree,
) (@typeOf(stream).Child.Error || Error)!void {
// render all the line comments at the beginning of the file
var tok_it = tree.tokens.iterator(0);
@@ -90,7 +90,7 @@ fn renderRoot(
}
}
-fn renderExtraNewline(tree: &ast.Tree, stream: var, start_col: &usize, node: &ast.Node) !void {
+fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *ast.Node) !void {
const first_token = node.firstToken();
var prev_token = first_token;
while (tree.tokens.at(prev_token - 1).id == Token.Id.DocComment) {
@@ -104,7 +104,7 @@ fn renderExtraNewline(tree: &ast.Tree, stream: var, start_col: &usize, node: &as
}
}
-fn renderTopLevelDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, indent: usize, start_col: &usize, decl: &ast.Node) (@typeOf(stream).Child.Error || Error)!void {
+fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node) (@typeOf(stream).Child.Error || Error)!void {
switch (decl.id) {
ast.Node.Id.FnProto => {
const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
@@ -214,12 +214,12 @@ fn renderTopLevelDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, i
}
fn renderExpression(
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
stream: var,
- tree: &ast.Tree,
+ tree: *ast.Tree,
indent: usize,
- start_col: &usize,
- base: &ast.Node,
+ start_col: *usize,
+ base: *ast.Node,
space: Space,
) (@typeOf(stream).Child.Error || Error)!void {
switch (base.id) {
@@ -1640,12 +1640,12 @@ fn renderExpression(
}
fn renderVarDecl(
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
stream: var,
- tree: &ast.Tree,
+ tree: *ast.Tree,
indent: usize,
- start_col: &usize,
- var_decl: &ast.Node.VarDecl,
+ start_col: *usize,
+ var_decl: *ast.Node.VarDecl,
) (@typeOf(stream).Child.Error || Error)!void {
if (var_decl.visib_token) |visib_token| {
try renderToken(tree, stream, visib_token, indent, start_col, Space.Space); // pub
@@ -1696,12 +1696,12 @@ fn renderVarDecl(
}
fn renderParamDecl(
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
stream: var,
- tree: &ast.Tree,
+ tree: *ast.Tree,
indent: usize,
- start_col: &usize,
- base: &ast.Node,
+ start_col: *usize,
+ base: *ast.Node,
space: Space,
) (@typeOf(stream).Child.Error || Error)!void {
const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", base);
@@ -1724,12 +1724,12 @@ fn renderParamDecl(
}
fn renderStatement(
- allocator: &mem.Allocator,
+ allocator: *mem.Allocator,
stream: var,
- tree: &ast.Tree,
+ tree: *ast.Tree,
indent: usize,
- start_col: &usize,
- base: &ast.Node,
+ start_col: *usize,
+ base: *ast.Node,
) (@typeOf(stream).Child.Error || Error)!void {
switch (base.id) {
ast.Node.Id.VarDecl => {
@@ -1761,7 +1761,7 @@ const Space = enum {
BlockStart,
};
-fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: &usize, space: Space) (@typeOf(stream).Child.Error || Error)!void {
+fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space) (@typeOf(stream).Child.Error || Error)!void {
if (space == Space.BlockStart) {
if (start_col.* < indent + indent_delta)
return renderToken(tree, stream, token_index, indent, start_col, Space.Space);
@@ -1928,11 +1928,11 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
}
fn renderDocComments(
- tree: &ast.Tree,
+ tree: *ast.Tree,
stream: var,
node: var,
indent: usize,
- start_col: &usize,
+ start_col: *usize,
) (@typeOf(stream).Child.Error || Error)!void {
const comment = node.doc_comments ?? return;
var it = comment.lines.iterator(0);
@@ -1949,7 +1949,7 @@ fn renderDocComments(
}
}
-fn nodeIsBlock(base: &const ast.Node) bool {
+fn nodeIsBlock(base: *const ast.Node) bool {
return switch (base.id) {
ast.Node.Id.Block,
ast.Node.Id.If,
@@ -1961,7 +1961,7 @@ fn nodeIsBlock(base: &const ast.Node) bool {
};
}
-fn nodeCausesSliceOpSpace(base: &ast.Node) bool {
+fn nodeCausesSliceOpSpace(base: *ast.Node) bool {
const infix_op = base.cast(ast.Node.InfixOp) ?? return false;
return switch (infix_op.op) {
ast.Node.InfixOp.Op.Period => false,
diff --git a/std/zig/tokenizer.zig b/std/zig/tokenizer.zig
index 7c3b3210fb..8378a9011d 100644
--- a/std/zig/tokenizer.zig
+++ b/std/zig/tokenizer.zig
@@ -200,7 +200,7 @@ pub const Tokenizer = struct {
pending_invalid_token: ?Token,
/// For debugging purposes
- pub fn dump(self: &Tokenizer, token: &const Token) void {
+ pub fn dump(self: *Tokenizer, token: *const Token) void {
std.debug.warn("{} \"{}\"\n", @tagName(token.id), self.buffer[token.start..token.end]);
}
@@ -265,7 +265,7 @@ pub const Tokenizer = struct {
SawAtSign,
};
- pub fn next(self: &Tokenizer) Token {
+ pub fn next(self: *Tokenizer) Token {
if (self.pending_invalid_token) |token| {
self.pending_invalid_token = null;
return token;
@@ -1089,7 +1089,7 @@ pub const Tokenizer = struct {
return result;
}
- fn checkLiteralCharacter(self: &Tokenizer) void {
+ fn checkLiteralCharacter(self: *Tokenizer) void {
if (self.pending_invalid_token != null) return;
const invalid_length = self.getInvalidCharacterLength();
if (invalid_length == 0) return;
@@ -1100,7 +1100,7 @@ pub const Tokenizer = struct {
};
}
- fn getInvalidCharacterLength(self: &Tokenizer) u3 {
+ fn getInvalidCharacterLength(self: *Tokenizer) u3 {
const c0 = self.buffer[self.index];
if (c0 < 0x80) {
if (c0 < 0x20 or c0 == 0x7f) {
diff --git a/test/assemble_and_link.zig b/test/assemble_and_link.zig
index 2593f3306a..8c727e87b5 100644
--- a/test/assemble_and_link.zig
+++ b/test/assemble_and_link.zig
@@ -1,7 +1,7 @@
const builtin = @import("builtin");
const tests = @import("tests.zig");
-pub fn addCases(cases: &tests.CompareOutputContext) void {
+pub fn addCases(cases: *tests.CompareOutputContext) void {
if (builtin.os == builtin.Os.linux and builtin.arch == builtin.Arch.x86_64) {
cases.addAsm("hello world linux x86_64",
\\.text
diff --git a/test/build_examples.zig b/test/build_examples.zig
index 7a4c0f35d9..1ba0ca46cf 100644
--- a/test/build_examples.zig
+++ b/test/build_examples.zig
@@ -2,7 +2,7 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");
const is_windows = builtin.os == builtin.Os.windows;
-pub fn addCases(cases: &tests.BuildExamplesContext) void {
+pub fn addCases(cases: *tests.BuildExamplesContext) void {
cases.add("example/hello_world/hello.zig");
cases.addC("example/hello_world/hello_libc.zig");
cases.add("example/cat/main.zig");
diff --git a/test/cases/align.zig b/test/cases/align.zig
index 582063766f..99bdcdf940 100644
--- a/test/cases/align.zig
+++ b/test/cases/align.zig
@@ -5,7 +5,7 @@ var foo: u8 align(4) = 100;
test "global variable alignment" {
assert(@typeOf(&foo).alignment == 4);
- assert(@typeOf(&foo) == &align(4) u8);
+ assert(@typeOf(&foo) == *align(4) u8);
const slice = (&foo)[0..1];
assert(@typeOf(slice) == []align(4) u8);
}
@@ -30,7 +30,7 @@ var baz: packed struct {
} = undefined;
test "packed struct alignment" {
- assert(@typeOf(&baz.b) == &align(1) u32);
+ assert(@typeOf(&baz.b) == *align(1) u32);
}
const blah: packed struct {
@@ -40,11 +40,11 @@ const blah: packed struct {
} = undefined;
test "bit field alignment" {
- assert(@typeOf(&blah.b) == &align(1:3:6) const u3);
+ assert(@typeOf(&blah.b) == *align(1:3:6) const u3);
}
test "default alignment allows unspecified in type syntax" {
- assert(&u32 == &align(@alignOf(u32)) u32);
+ assert(*u32 == *align(@alignOf(u32)) u32);
}
test "implicitly decreasing pointer alignment" {
@@ -53,7 +53,7 @@ test "implicitly decreasing pointer alignment" {
assert(addUnaligned(&a, &b) == 7);
}
-fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) u32 {
+fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
return a.* + b.*;
}
@@ -76,7 +76,7 @@ fn testBytesAlign(b: u8) void {
b,
b,
};
- const ptr = @ptrCast(&u32, &bytes[0]);
+ const ptr = @ptrCast(*u32, &bytes[0]);
assert(ptr.* == 0x33333333);
}
@@ -99,10 +99,10 @@ test "@alignCast pointers" {
expectsOnly1(&x);
assert(x == 2);
}
-fn expectsOnly1(x: &align(1) u32) void {
+fn expectsOnly1(x: *align(1) u32) void {
expects4(@alignCast(4, x));
}
-fn expects4(x: &align(4) u32) void {
+fn expects4(x: *align(4) u32) void {
x.* += 1;
}
@@ -163,8 +163,8 @@ fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
test "@ptrCast preserves alignment of bigger source" {
var x: u32 align(16) = 1234;
- const ptr = @ptrCast(&u8, &x);
- assert(@typeOf(ptr) == &align(16) u8);
+ const ptr = @ptrCast(*u8, &x);
+ assert(@typeOf(ptr) == *align(16) u8);
}
test "compile-time known array index has best alignment possible" {
@@ -175,10 +175,10 @@ test "compile-time known array index has best alignment possible" {
3,
4,
};
- assert(@typeOf(&array[0]) == &align(4) u8);
- assert(@typeOf(&array[1]) == &u8);
- assert(@typeOf(&array[2]) == &align(2) u8);
- assert(@typeOf(&array[3]) == &u8);
+ assert(@typeOf(&array[0]) == *align(4) u8);
+ assert(@typeOf(&array[1]) == *u8);
+ assert(@typeOf(&array[2]) == *align(2) u8);
+ assert(@typeOf(&array[3]) == *u8);
// because align is too small but we still figure out to use 2
var bigger align(2) = []u64{
@@ -187,10 +187,10 @@ test "compile-time known array index has best alignment possible" {
3,
4,
};
- assert(@typeOf(&bigger[0]) == &align(2) u64);
- assert(@typeOf(&bigger[1]) == &align(2) u64);
- assert(@typeOf(&bigger[2]) == &align(2) u64);
- assert(@typeOf(&bigger[3]) == &align(2) u64);
+ assert(@typeOf(&bigger[0]) == *align(2) u64);
+ assert(@typeOf(&bigger[1]) == *align(2) u64);
+ assert(@typeOf(&bigger[2]) == *align(2) u64);
+ assert(@typeOf(&bigger[3]) == *align(2) u64);
// because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
var smaller align(2) = []u32{
@@ -199,21 +199,21 @@ test "compile-time known array index has best alignment possible" {
3,
4,
};
- testIndex(&smaller[0], 0, &align(2) u32);
- testIndex(&smaller[0], 1, &align(2) u32);
- testIndex(&smaller[0], 2, &align(2) u32);
- testIndex(&smaller[0], 3, &align(2) u32);
+ testIndex(&smaller[0], 0, *align(2) u32);
+ testIndex(&smaller[0], 1, *align(2) u32);
+ testIndex(&smaller[0], 2, *align(2) u32);
+ testIndex(&smaller[0], 3, *align(2) u32);
// has to use ABI alignment because index known at runtime only
- testIndex2(&array[0], 0, &u8);
- testIndex2(&array[0], 1, &u8);
- testIndex2(&array[0], 2, &u8);
- testIndex2(&array[0], 3, &u8);
+ testIndex2(&array[0], 0, *u8);
+ testIndex2(&array[0], 1, *u8);
+ testIndex2(&array[0], 2, *u8);
+ testIndex2(&array[0], 3, *u8);
}
-fn testIndex(smaller: &align(2) u32, index: usize, comptime T: type) void {
+fn testIndex(smaller: *align(2) u32, index: usize, comptime T: type) void {
assert(@typeOf(&smaller[index]) == T);
}
-fn testIndex2(ptr: &align(4) u8, index: usize, comptime T: type) void {
+fn testIndex2(ptr: *align(4) u8, index: usize, comptime T: type) void {
assert(@typeOf(&ptr[index]) == T);
}
diff --git a/test/cases/atomics.zig b/test/cases/atomics.zig
index d406285d29..67c9ab3dd1 100644
--- a/test/cases/atomics.zig
+++ b/test/cases/atomics.zig
@@ -34,7 +34,7 @@ test "atomicrmw and atomicload" {
testAtomicLoad(&data);
}
-fn testAtomicRmw(ptr: &u8) void {
+fn testAtomicRmw(ptr: *u8) void {
const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst);
assert(prev_value == 200);
comptime {
@@ -45,7 +45,7 @@ fn testAtomicRmw(ptr: &u8) void {
}
}
-fn testAtomicLoad(ptr: &u8) void {
+fn testAtomicLoad(ptr: *u8) void {
const x = @atomicLoad(u8, ptr, AtomicOrder.SeqCst);
assert(x == 42);
}
@@ -54,18 +54,18 @@ test "cmpxchg with ptr" {
var data1: i32 = 1234;
var data2: i32 = 5678;
var data3: i32 = 9101;
- var x: &i32 = &data1;
- if (@cmpxchgWeak(&i32, &x, &data2, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
+ var x: *i32 = &data1;
+ if (@cmpxchgWeak(*i32, &x, &data2, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
assert(x1 == &data1);
} else {
@panic("cmpxchg should have failed");
}
- while (@cmpxchgWeak(&i32, &x, &data1, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
+ while (@cmpxchgWeak(*i32, &x, &data1, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
assert(x1 == &data1);
}
assert(x == &data3);
- assert(@cmpxchgStrong(&i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null);
+ assert(@cmpxchgStrong(*i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null);
assert(x == &data2);
}
diff --git a/test/cases/bugs/655.zig b/test/cases/bugs/655.zig
index 4431767d5c..50374d4e6d 100644
--- a/test/cases/bugs/655.zig
+++ b/test/cases/bugs/655.zig
@@ -3,10 +3,10 @@ const other_file = @import("655_other_file.zig");
test "function with &const parameter with type dereferenced by namespace" {
const x: other_file.Integer = 1234;
- comptime std.debug.assert(@typeOf(&x) == &const other_file.Integer);
+ comptime std.debug.assert(@typeOf(&x) == *const other_file.Integer);
foo(x);
}
-fn foo(x: &const other_file.Integer) void {
+fn foo(x: *const other_file.Integer) void {
std.debug.assert(x.* == 1234);
}
diff --git a/test/cases/bugs/828.zig b/test/cases/bugs/828.zig
index 10d7370b90..50ae0fd279 100644
--- a/test/cases/bugs/828.zig
+++ b/test/cases/bugs/828.zig
@@ -3,7 +3,7 @@ const CountBy = struct {
const One = CountBy{ .a = 1 };
- pub fn counter(self: &const CountBy) Counter {
+ pub fn counter(self: *const CountBy) Counter {
return Counter{ .i = 0 };
}
};
@@ -11,13 +11,13 @@ const CountBy = struct {
const Counter = struct {
i: usize,
- pub fn count(self: &Counter) bool {
+ pub fn count(self: *Counter) bool {
self.i += 1;
return self.i <= 10;
}
};
-fn constCount(comptime cb: &const CountBy, comptime unused: u32) void {
+fn constCount(comptime cb: *const CountBy, comptime unused: u32) void {
comptime {
var cnt = cb.counter();
if (cnt.i != 0) @compileError("Counter instance reused!");
diff --git a/test/cases/bugs/920.zig b/test/cases/bugs/920.zig
index c315206072..2903f05a29 100644
--- a/test/cases/bugs/920.zig
+++ b/test/cases/bugs/920.zig
@@ -9,10 +9,10 @@ const ZigTable = struct {
pdf: fn (f64) f64,
is_symmetric: bool,
- zero_case: fn (&Random, f64) f64,
+ zero_case: fn (*Random, f64) f64,
};
-fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn (f64) f64, comptime f_inv: fn (f64) f64, comptime zero_case: fn (&Random, f64) f64) ZigTable {
+fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn (f64) f64, comptime f_inv: fn (f64) f64, comptime zero_case: fn (*Random, f64) f64) ZigTable {
var tables: ZigTable = undefined;
tables.is_symmetric = is_symmetric;
@@ -45,7 +45,7 @@ fn norm_f(x: f64) f64 {
fn norm_f_inv(y: f64) f64 {
return math.sqrt(-2.0 * math.ln(y));
}
-fn norm_zero_case(random: &Random, u: f64) f64 {
+fn norm_zero_case(random: *Random, u: f64) f64 {
return 0.0;
}
diff --git a/test/cases/cast.zig b/test/cases/cast.zig
index e37451ea93..7358a4ffd8 100644
--- a/test/cases/cast.zig
+++ b/test/cases/cast.zig
@@ -3,20 +3,20 @@ const mem = @import("std").mem;
test "int to ptr cast" {
const x = usize(13);
- const y = @intToPtr(&u8, x);
+ const y = @intToPtr(*u8, x);
const z = @ptrToInt(y);
assert(z == 13);
}
test "integer literal to pointer cast" {
- const vga_mem = @intToPtr(&u16, 0xB8000);
+ const vga_mem = @intToPtr(*u16, 0xB8000);
assert(@ptrToInt(vga_mem) == 0xB8000);
}
test "pointer reinterpret const float to int" {
const float: f64 = 5.99999999999994648725e-01;
const float_ptr = &float;
- const int_ptr = @ptrCast(&const i32, float_ptr);
+ const int_ptr = @ptrCast(*const i32, float_ptr);
const int_val = int_ptr.*;
assert(int_val == 858993411);
}
@@ -28,7 +28,7 @@ test "implicitly cast a pointer to a const pointer of it" {
assert(x == 2);
}
-fn funcWithConstPtrPtr(x: &const &i32) void {
+fn funcWithConstPtrPtr(x: *const *i32) void {
x.*.* += 1;
}
@@ -66,11 +66,11 @@ fn Struct(comptime T: type) type {
const Self = this;
x: T,
- fn pointer(self: &const Self) Self {
+ fn pointer(self: *const Self) Self {
return self.*;
}
- fn maybePointer(self: ?&const Self) Self {
+ fn maybePointer(self: ?*const Self) Self {
const none = Self{ .x = if (T == void) void{} else 0 };
return (self ?? &none).*;
}
@@ -80,11 +80,11 @@ fn Struct(comptime T: type) type {
const Union = union {
x: u8,
- fn pointer(self: &const Union) Union {
+ fn pointer(self: *const Union) Union {
return self.*;
}
- fn maybePointer(self: ?&const Union) Union {
+ fn maybePointer(self: ?*const Union) Union {
const none = Union{ .x = 0 };
return (self ?? &none).*;
}
@@ -94,11 +94,11 @@ const Enum = enum {
None,
Some,
- fn pointer(self: &const Enum) Enum {
+ fn pointer(self: *const Enum) Enum {
return self.*;
}
- fn maybePointer(self: ?&const Enum) Enum {
+ fn maybePointer(self: ?*const Enum) Enum {
return (self ?? &Enum.None).*;
}
};
@@ -107,16 +107,16 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
const S = struct {
const Self = this;
x: u8,
- fn constConst(p: &const &const Self) u8 {
+ fn constConst(p: *const *const Self) u8 {
return (p.*).x;
}
- fn maybeConstConst(p: ?&const &const Self) u8 {
+ fn maybeConstConst(p: ?*const *const Self) u8 {
return ((??p).*).x;
}
- fn constConstConst(p: &const &const &const Self) u8 {
+ fn constConstConst(p: *const *const *const Self) u8 {
return (p.*.*).x;
}
- fn maybeConstConstConst(p: ?&const &const &const Self) u8 {
+ fn maybeConstConstConst(p: ?*const *const *const Self) u8 {
return ((??p).*.*).x;
}
};
@@ -166,12 +166,12 @@ fn testPeerResolveArrayConstSlice(b: bool) void {
}
test "integer literal to &const int" {
- const x: &const i32 = 3;
+ const x: *const i32 = 3;
assert(x.* == 3);
}
test "string literal to &const []const u8" {
- const x: &const []const u8 = "hello";
+ const x: *const []const u8 = "hello";
assert(mem.eql(u8, x.*, "hello"));
}
@@ -209,11 +209,11 @@ test "return null from fn() error!?&T" {
const b = returnNullLitFromMaybeTypeErrorRef();
assert((try a) == null and (try b) == null);
}
-fn returnNullFromMaybeTypeErrorRef() error!?&A {
- const a: ?&A = null;
+fn returnNullFromMaybeTypeErrorRef() error!?*A {
+ const a: ?*A = null;
return a;
}
-fn returnNullLitFromMaybeTypeErrorRef() error!?&A {
+fn returnNullLitFromMaybeTypeErrorRef() error!?*A {
return null;
}
@@ -312,7 +312,7 @@ test "implicit cast from &const [N]T to []const T" {
fn testCastConstArrayRefToConstSlice() void {
const blah = "aoeu";
const const_array_ref = &blah;
- assert(@typeOf(const_array_ref) == &const [4]u8);
+ assert(@typeOf(const_array_ref) == *const [4]u8);
const slice: []const u8 = const_array_ref;
assert(mem.eql(u8, slice, "aoeu"));
}
@@ -322,7 +322,7 @@ test "var args implicitly casts by value arg to const ref" {
}
fn foo(args: ...) void {
- assert(@typeOf(args[0]) == &const [5]u8);
+ assert(@typeOf(args[0]) == *const [5]u8);
}
test "peer type resolution: error and [N]T" {
diff --git a/test/cases/const_slice_child.zig b/test/cases/const_slice_child.zig
index a92c589186..e012c729a0 100644
--- a/test/cases/const_slice_child.zig
+++ b/test/cases/const_slice_child.zig
@@ -1,10 +1,10 @@
const debug = @import("std").debug;
const assert = debug.assert;
-var argv: &const &const u8 = undefined;
+var argv: *const *const u8 = undefined;
test "const slice child" {
- const strs = ([]&const u8){
+ const strs = ([]*const u8){
c"one",
c"two",
c"three",
@@ -29,7 +29,7 @@ fn bar(argc: usize) void {
foo(args);
}
-fn strlen(ptr: &const u8) usize {
+fn strlen(ptr: *const u8) usize {
var count: usize = 0;
while (ptr[count] != 0) : (count += 1) {}
return count;
diff --git a/test/cases/coroutines.zig b/test/cases/coroutines.zig
index 8a071c6aad..4d2aa54a69 100644
--- a/test/cases/coroutines.zig
+++ b/test/cases/coroutines.zig
@@ -154,7 +154,7 @@ test "async function with dot syntax" {
test "async fn pointer in a struct field" {
var data: i32 = 1;
const Foo = struct {
- bar: async<&std.mem.Allocator> fn (&i32) void,
+ bar: async<*std.mem.Allocator> fn (*i32) void,
};
var foo = Foo{ .bar = simpleAsyncFn2 };
const p = (async foo.bar(&data)) catch unreachable;
@@ -162,7 +162,7 @@ test "async fn pointer in a struct field" {
cancel p;
assert(data == 4);
}
-async<&std.mem.Allocator> fn simpleAsyncFn2(y: &i32) void {
+async<*std.mem.Allocator> fn simpleAsyncFn2(y: *i32) void {
defer y.* += 2;
y.* += 1;
suspend;
@@ -220,7 +220,7 @@ test "break from suspend" {
cancel p;
std.debug.assert(my_result == 2);
}
-async fn testBreakFromSuspend(my_result: &i32) void {
+async fn testBreakFromSuspend(my_result: *i32) void {
s: suspend |p| {
break :s;
}
diff --git a/test/cases/enum.zig b/test/cases/enum.zig
index cbcbc5e306..ae9f04869b 100644
--- a/test/cases/enum.zig
+++ b/test/cases/enum.zig
@@ -56,14 +56,14 @@ test "constant enum with payload" {
shouldBeNotEmpty(full);
}
-fn shouldBeEmpty(x: &const AnEnumWithPayload) void {
+fn shouldBeEmpty(x: *const AnEnumWithPayload) void {
switch (x.*) {
AnEnumWithPayload.Empty => {},
else => unreachable,
}
}
-fn shouldBeNotEmpty(x: &const AnEnumWithPayload) void {
+fn shouldBeNotEmpty(x: *const AnEnumWithPayload) void {
switch (x.*) {
AnEnumWithPayload.Empty => unreachable,
else => {},
@@ -750,15 +750,15 @@ test "bit field access with enum fields" {
assert(data.b == B.Four3);
}
-fn getA(data: &const BitFieldOfEnums) A {
+fn getA(data: *const BitFieldOfEnums) A {
return data.a;
}
-fn getB(data: &const BitFieldOfEnums) B {
+fn getB(data: *const BitFieldOfEnums) B {
return data.b;
}
-fn getC(data: &const BitFieldOfEnums) C {
+fn getC(data: *const BitFieldOfEnums) C {
return data.c;
}
diff --git a/test/cases/enum_with_members.zig b/test/cases/enum_with_members.zig
index 8fafa70b02..18174186a9 100644
--- a/test/cases/enum_with_members.zig
+++ b/test/cases/enum_with_members.zig
@@ -6,7 +6,7 @@ const ET = union(enum) {
SINT: i32,
UINT: u32,
- pub fn print(a: &const ET, buf: []u8) error!usize {
+ pub fn print(a: *const ET, buf: []u8) error!usize {
return switch (a.*) {
ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
diff --git a/test/cases/eval.zig b/test/cases/eval.zig
index 8a6dc25bd8..6c8bcfdbab 100644
--- a/test/cases/eval.zig
+++ b/test/cases/eval.zig
@@ -282,7 +282,7 @@ fn fnWithFloatMode() f32 {
const SimpleStruct = struct {
field: i32,
- fn method(self: &const SimpleStruct) i32 {
+ fn method(self: *const SimpleStruct) i32 {
return self.field + 3;
}
};
@@ -367,7 +367,7 @@ test "const global shares pointer with other same one" {
assertEqualPtrs(&hi1[0], &hi2[0]);
comptime assert(&hi1[0] == &hi2[0]);
}
-fn assertEqualPtrs(ptr1: &const u8, ptr2: &const u8) void {
+fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) void {
assert(ptr1 == ptr2);
}
@@ -418,9 +418,9 @@ test "string literal used as comptime slice is memoized" {
}
test "comptime slice of undefined pointer of length 0" {
- const slice1 = (&i32)(undefined)[0..0];
+ const slice1 = (*i32)(undefined)[0..0];
assert(slice1.len == 0);
- const slice2 = (&i32)(undefined)[100..100];
+ const slice2 = (*i32)(undefined)[100..100];
assert(slice2.len == 0);
}
@@ -472,7 +472,7 @@ test "comptime function with mutable pointer is not memoized" {
}
}
-fn increment(value: &i32) void {
+fn increment(value: *i32) void {
value.* += 1;
}
@@ -517,7 +517,7 @@ test "comptime slice of pointer preserves comptime var" {
const SingleFieldStruct = struct {
x: i32,
- fn read_x(self: &const SingleFieldStruct) i32 {
+ fn read_x(self: *const SingleFieldStruct) i32 {
return self.x;
}
};
diff --git a/test/cases/field_parent_ptr.zig b/test/cases/field_parent_ptr.zig
index 1a7de9ce35..00d4e0f367 100644
--- a/test/cases/field_parent_ptr.zig
+++ b/test/cases/field_parent_ptr.zig
@@ -24,7 +24,7 @@ const foo = Foo{
.d = -10,
};
-fn testParentFieldPtr(c: &const i32) void {
+fn testParentFieldPtr(c: *const i32) void {
assert(c == &foo.c);
const base = @fieldParentPtr(Foo, "c", c);
@@ -32,7 +32,7 @@ fn testParentFieldPtr(c: &const i32) void {
assert(&base.c == c);
}
-fn testParentFieldPtrFirst(a: &const bool) void {
+fn testParentFieldPtrFirst(a: *const bool) void {
assert(a == &foo.a);
const base = @fieldParentPtr(Foo, "a", a);
diff --git a/test/cases/fn_in_struct_in_comptime.zig b/test/cases/fn_in_struct_in_comptime.zig
index c22da71940..fabb57e9cb 100644
--- a/test/cases/fn_in_struct_in_comptime.zig
+++ b/test/cases/fn_in_struct_in_comptime.zig
@@ -1,9 +1,9 @@
const assert = @import("std").debug.assert;
-fn get_foo() fn (&u8) usize {
+fn get_foo() fn (*u8) usize {
comptime {
return struct {
- fn func(ptr: &u8) usize {
+ fn func(ptr: *u8) usize {
var u = @ptrToInt(ptr);
return u;
}
@@ -13,5 +13,5 @@ fn get_foo() fn (&u8) usize {
test "define a function in an anonymous struct in comptime" {
const foo = get_foo();
- assert(foo(@intToPtr(&u8, 12345)) == 12345);
+ assert(foo(@intToPtr(*u8, 12345)) == 12345);
}
diff --git a/test/cases/generics.zig b/test/cases/generics.zig
index 37cd1b89e4..a76990e2a1 100644
--- a/test/cases/generics.zig
+++ b/test/cases/generics.zig
@@ -96,8 +96,8 @@ test "generic struct" {
fn GenNode(comptime T: type) type {
return struct {
value: T,
- next: ?&GenNode(T),
- fn getVal(n: &const GenNode(T)) T {
+ next: ?*GenNode(T),
+ fn getVal(n: *const GenNode(T)) T {
return n.value;
}
};
@@ -126,11 +126,11 @@ test "generic fn with implicit cast" {
13,
}) == 0);
}
-fn getByte(ptr: ?&const u8) u8 {
+fn getByte(ptr: ?*const u8) u8 {
return (??ptr).*;
}
fn getFirstByte(comptime T: type, mem: []const T) u8 {
- return getByte(@ptrCast(&const u8, &mem[0]));
+ return getByte(@ptrCast(*const u8, &mem[0]));
}
const foos = []fn (var) bool{
diff --git a/test/cases/incomplete_struct_param_tld.zig b/test/cases/incomplete_struct_param_tld.zig
index a2f57743d0..552d6ef185 100644
--- a/test/cases/incomplete_struct_param_tld.zig
+++ b/test/cases/incomplete_struct_param_tld.zig
@@ -11,12 +11,12 @@ const B = struct {
const C = struct {
x: i32,
- fn d(c: &const C) i32 {
+ fn d(c: *const C) i32 {
return c.x;
}
};
-fn foo(a: &const A) i32 {
+fn foo(a: *const A) i32 {
return a.b.c.d();
}
diff --git a/test/cases/math.zig b/test/cases/math.zig
index 0b4622702f..5f16e903b2 100644
--- a/test/cases/math.zig
+++ b/test/cases/math.zig
@@ -28,13 +28,13 @@ fn testDivision() void {
assert(divTrunc(f32, -5.0, 3.0) == -1.0);
comptime {
- assert(1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600);
- assert(@rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600);
- assert(1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2);
- assert(@divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2);
- assert(@divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2);
- assert(@divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2);
- assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1);
+ assert(1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,);
+ assert(@rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,);
+ assert(1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,);
+ assert(@divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,);
+ assert(@divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,);
+ assert(@divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,);
+ assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1,);
}
}
fn div(comptime T: type, a: T, b: T) T {
@@ -324,8 +324,8 @@ test "big number addition" {
test "big number multiplication" {
comptime {
- assert(45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567);
- assert(594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016);
+ assert(45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567,);
+ assert(594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016,);
}
}
diff --git a/test/cases/misc.zig b/test/cases/misc.zig
index b6b2da8de5..919b978f9f 100644
--- a/test/cases/misc.zig
+++ b/test/cases/misc.zig
@@ -252,20 +252,20 @@ test "multiline C string" {
}
test "type equality" {
- assert(&const u8 != &u8);
+ assert(*const u8 != *u8);
}
const global_a: i32 = 1234;
-const global_b: &const i32 = &global_a;
-const global_c: &const f32 = @ptrCast(&const f32, global_b);
+const global_b: *const i32 = &global_a;
+const global_c: *const f32 = @ptrCast(*const f32, global_b);
test "compile time global reinterpret" {
- const d = @ptrCast(&const i32, global_c);
+ const d = @ptrCast(*const i32, global_c);
assert(d.* == 1234);
}
test "explicit cast maybe pointers" {
- const a: ?&i32 = undefined;
- const b: ?&f32 = @ptrCast(?&f32, a);
+ const a: ?*i32 = undefined;
+ const b: ?*f32 = @ptrCast(?*f32, a);
}
test "generic malloc free" {
@@ -274,7 +274,7 @@ test "generic malloc free" {
}
var some_mem: [100]u8 = undefined;
fn memAlloc(comptime T: type, n: usize) error![]T {
- return @ptrCast(&T, &some_mem[0])[0..n];
+ return @ptrCast(*T, &some_mem[0])[0..n];
}
fn memFree(comptime T: type, memory: []T) void {}
@@ -357,7 +357,7 @@ const test3_foo = Test3Foo{
},
};
const test3_bar = Test3Foo{ .Two = 13 };
-fn test3_1(f: &const Test3Foo) void {
+fn test3_1(f: *const Test3Foo) void {
switch (f.*) {
Test3Foo.Three => |pt| {
assert(pt.x == 3);
@@ -366,7 +366,7 @@ fn test3_1(f: &const Test3Foo) void {
else => unreachable,
}
}
-fn test3_2(f: &const Test3Foo) void {
+fn test3_2(f: *const Test3Foo) void {
switch (f.*) {
Test3Foo.Two => |x| {
assert(x == 13);
@@ -393,7 +393,7 @@ test "pointer comparison" {
const b = &a;
assert(ptrEql(b, b));
}
-fn ptrEql(a: &const []const u8, b: &const []const u8) bool {
+fn ptrEql(a: *const []const u8, b: *const []const u8) bool {
return a == b;
}
@@ -446,13 +446,13 @@ fn testPointerToVoidReturnType() error!void {
return a.*;
}
const test_pointer_to_void_return_type_x = void{};
-fn testPointerToVoidReturnType2() &const void {
+fn testPointerToVoidReturnType2() *const void {
return &test_pointer_to_void_return_type_x;
}
test "non const ptr to aliased type" {
const int = i32;
- assert(?&int == ?&i32);
+ assert(?*int == ?*i32);
}
test "array 2D const double ptr" {
@@ -463,7 +463,7 @@ test "array 2D const double ptr" {
testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
}
-fn testArray2DConstDoublePtr(ptr: &const f32) void {
+fn testArray2DConstDoublePtr(ptr: *const f32) void {
assert(ptr[0] == 1.0);
assert(ptr[1] == 2.0);
}
@@ -497,7 +497,7 @@ test "@typeId" {
assert(@typeId(u64) == Tid.Int);
assert(@typeId(f32) == Tid.Float);
assert(@typeId(f64) == Tid.Float);
- assert(@typeId(&f32) == Tid.Pointer);
+ assert(@typeId(*f32) == Tid.Pointer);
assert(@typeId([2]u8) == Tid.Array);
assert(@typeId(AStruct) == Tid.Struct);
assert(@typeId(@typeOf(1)) == Tid.IntLiteral);
@@ -540,7 +540,7 @@ test "@typeName" {
};
comptime {
assert(mem.eql(u8, @typeName(i64), "i64"));
- assert(mem.eql(u8, @typeName(&usize), "&usize"));
+ assert(mem.eql(u8, @typeName(*usize), "*usize"));
// https://github.com/ziglang/zig/issues/675
assert(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)"));
assert(mem.eql(u8, @typeName(Struct), "Struct"));
@@ -555,7 +555,7 @@ fn TypeFromFn(comptime T: type) type {
test "volatile load and store" {
var number: i32 = 1234;
- const ptr = (&volatile i32)(&number);
+ const ptr = (*volatile i32)(&number);
ptr.* += 1;
assert(ptr.* == 1235);
}
@@ -587,28 +587,28 @@ var global_ptr = &gdt[0];
// can't really run this test but we can make sure it has no compile error
// and generates code
-const vram = @intToPtr(&volatile u8, 0x20000000)[0..0x8000];
+const vram = @intToPtr(*volatile u8, 0x20000000)[0..0x8000];
export fn writeToVRam() void {
vram[0] = 'X';
}
test "pointer child field" {
- assert((&u32).Child == u32);
+ assert((*u32).Child == u32);
}
const OpaqueA = @OpaqueType();
const OpaqueB = @OpaqueType();
test "@OpaqueType" {
- assert(&OpaqueA != &OpaqueB);
+ assert(*OpaqueA != *OpaqueB);
assert(mem.eql(u8, @typeName(OpaqueA), "OpaqueA"));
assert(mem.eql(u8, @typeName(OpaqueB), "OpaqueB"));
}
test "variable is allowed to be a pointer to an opaque type" {
var x: i32 = 1234;
- _ = hereIsAnOpaqueType(@ptrCast(&OpaqueA, &x));
+ _ = hereIsAnOpaqueType(@ptrCast(*OpaqueA, &x));
}
-fn hereIsAnOpaqueType(ptr: &OpaqueA) &OpaqueA {
+fn hereIsAnOpaqueType(ptr: *OpaqueA) *OpaqueA {
var a = ptr;
return a;
}
@@ -692,7 +692,7 @@ test "packed struct, enum, union parameters in extern function" {
}, PackedUnion{ .a = 1 }, PackedEnum.A);
}
-export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {}
+export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion, c: PackedEnum) void {}
test "slicing zero length array" {
const s1 = ""[0..];
@@ -703,8 +703,8 @@ test "slicing zero length array" {
assert(mem.eql(u32, s2, []u32{}));
}
-const addr1 = @ptrCast(&const u8, emptyFn);
+const addr1 = @ptrCast(*const u8, emptyFn);
test "comptime cast fn to ptr" {
- const addr2 = @ptrCast(&const u8, emptyFn);
+ const addr2 = @ptrCast(*const u8, emptyFn);
comptime assert(addr1 == addr2);
}
diff --git a/test/cases/null.zig b/test/cases/null.zig
index 936e5fafbd..bd78990ff4 100644
--- a/test/cases/null.zig
+++ b/test/cases/null.zig
@@ -65,7 +65,7 @@ test "if var maybe pointer" {
.d = 1,
}) == 15);
}
-fn shouldBeAPlus1(p: &const Particle) u64 {
+fn shouldBeAPlus1(p: *const Particle) u64 {
var maybe_particle: ?Particle = p.*;
if (maybe_particle) |*particle| {
particle.a += 1;
diff --git a/test/cases/reflection.zig b/test/cases/reflection.zig
index b82ce6340f..48fcc9ef03 100644
--- a/test/cases/reflection.zig
+++ b/test/cases/reflection.zig
@@ -5,7 +5,7 @@ const reflection = this;
test "reflection: array, pointer, nullable, error union type child" {
comptime {
assert(([10]u8).Child == u8);
- assert((&u8).Child == u8);
+ assert((*u8).Child == u8);
assert((error!u8).Payload == u8);
assert((?u8).Child == u8);
}
diff --git a/test/cases/slice.zig b/test/cases/slice.zig
index eae6fa895e..24e5239e2d 100644
--- a/test/cases/slice.zig
+++ b/test/cases/slice.zig
@@ -1,7 +1,7 @@
const assert = @import("std").debug.assert;
const mem = @import("std").mem;
-const x = @intToPtr(&i32, 0x1000)[0..0x500];
+const x = @intToPtr(*i32, 0x1000)[0..0x500];
const y = x[0x100..];
test "compile time slice of pointer to hard coded address" {
assert(@ptrToInt(x.ptr) == 0x1000);
diff --git a/test/cases/struct.zig b/test/cases/struct.zig
index d4a1c7fbe3..0712e508de 100644
--- a/test/cases/struct.zig
+++ b/test/cases/struct.zig
@@ -43,7 +43,7 @@ const VoidStructFieldsFoo = struct {
test "structs" {
var foo: StructFoo = undefined;
- @memset(@ptrCast(&u8, &foo), 0, @sizeOf(StructFoo));
+ @memset(@ptrCast(*u8, &foo), 0, @sizeOf(StructFoo));
foo.a += 1;
foo.b = foo.a == 1;
testFoo(foo);
@@ -55,16 +55,16 @@ const StructFoo = struct {
b: bool,
c: f32,
};
-fn testFoo(foo: &const StructFoo) void {
+fn testFoo(foo: *const StructFoo) void {
assert(foo.b);
}
-fn testMutation(foo: &StructFoo) void {
+fn testMutation(foo: *StructFoo) void {
foo.c = 100;
}
const Node = struct {
val: Val,
- next: &Node,
+ next: *Node,
};
const Val = struct {
@@ -112,7 +112,7 @@ fn aFunc() i32 {
return 13;
}
-fn callStructField(foo: &const Foo) i32 {
+fn callStructField(foo: *const Foo) i32 {
return foo.ptr();
}
@@ -124,7 +124,7 @@ test "store member function in variable" {
}
const MemberFnTestFoo = struct {
x: i32,
- fn member(foo: &const MemberFnTestFoo) i32 {
+ fn member(foo: *const MemberFnTestFoo) i32 {
return foo.x;
}
};
@@ -141,7 +141,7 @@ test "member functions" {
}
const MemberFnRand = struct {
seed: u32,
- pub fn getSeed(r: &const MemberFnRand) u32 {
+ pub fn getSeed(r: *const MemberFnRand) u32 {
return r.seed;
}
};
@@ -166,7 +166,7 @@ test "empty struct method call" {
assert(es.method() == 1234);
}
const EmptyStruct = struct {
- fn method(es: &const EmptyStruct) i32 {
+ fn method(es: *const EmptyStruct) i32 {
return 1234;
}
};
@@ -228,15 +228,15 @@ test "bit field access" {
assert(data.b == 3);
}
-fn getA(data: &const BitField1) u3 {
+fn getA(data: *const BitField1) u3 {
return data.a;
}
-fn getB(data: &const BitField1) u3 {
+fn getB(data: *const BitField1) u3 {
return data.b;
}
-fn getC(data: &const BitField1) u2 {
+fn getC(data: *const BitField1) u2 {
return data.c;
}
@@ -396,8 +396,8 @@ const Bitfields = packed struct {
test "native bit field understands endianness" {
var all: u64 = 0x7765443322221111;
var bytes: [8]u8 = undefined;
- @memcpy(&bytes[0], @ptrCast(&u8, &all), 8);
- var bitfields = @ptrCast(&Bitfields, &bytes[0]).*;
+ @memcpy(&bytes[0], @ptrCast(*u8, &all), 8);
+ var bitfields = @ptrCast(*Bitfields, &bytes[0]).*;
assert(bitfields.f1 == 0x1111);
assert(bitfields.f2 == 0x2222);
@@ -415,7 +415,7 @@ test "align 1 field before self referential align 8 field as slice return type"
const Expr = union(enum) {
Literal: u8,
- Question: &Expr,
+ Question: *Expr,
};
fn alloc(comptime T: type) []T {
diff --git a/test/cases/struct_contains_null_ptr_itself.zig b/test/cases/struct_contains_null_ptr_itself.zig
index b6cb1a94cc..21175974b3 100644
--- a/test/cases/struct_contains_null_ptr_itself.zig
+++ b/test/cases/struct_contains_null_ptr_itself.zig
@@ -2,13 +2,13 @@ const std = @import("std");
const assert = std.debug.assert;
test "struct contains null pointer which contains original struct" {
- var x: ?&NodeLineComment = null;
+ var x: ?*NodeLineComment = null;
assert(x == null);
}
pub const Node = struct {
id: Id,
- comment: ?&NodeLineComment,
+ comment: ?*NodeLineComment,
pub const Id = enum {
Root,
diff --git a/test/cases/switch.zig b/test/cases/switch.zig
index 495fa9f3ed..c6a4b60f09 100644
--- a/test/cases/switch.zig
+++ b/test/cases/switch.zig
@@ -90,7 +90,7 @@ const SwitchProngWithVarEnum = union(enum) {
Two: f32,
Meh: void,
};
-fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) void {
+fn switchProngWithVarFn(a: *const SwitchProngWithVarEnum) void {
switch (a.*) {
SwitchProngWithVarEnum.One => |x| {
assert(x == 13);
diff --git a/test/cases/this.zig b/test/cases/this.zig
index 5e433b5037..ba51d0ac90 100644
--- a/test/cases/this.zig
+++ b/test/cases/this.zig
@@ -8,7 +8,7 @@ fn Point(comptime T: type) type {
x: T,
y: T,
- fn addOne(self: &Self) void {
+ fn addOne(self: *Self) void {
self.x += 1;
self.y += 1;
}
diff --git a/test/cases/type_info.zig b/test/cases/type_info.zig
index 2561d70865..921ff785a7 100644
--- a/test/cases/type_info.zig
+++ b/test/cases/type_info.zig
@@ -37,7 +37,7 @@ test "type info: pointer type info" {
}
fn testPointer() void {
- const u32_ptr_info = @typeInfo(&u32);
+ const u32_ptr_info = @typeInfo(*u32);
assert(TypeId(u32_ptr_info) == TypeId.Pointer);
assert(u32_ptr_info.Pointer.is_const == false);
assert(u32_ptr_info.Pointer.is_volatile == false);
@@ -169,14 +169,14 @@ fn testUnion() void {
assert(notag_union_info.Union.fields[1].field_type == u32);
const TestExternUnion = extern union {
- foo: &c_void,
+ foo: *c_void,
};
const extern_union_info = @typeInfo(TestExternUnion);
assert(extern_union_info.Union.layout == TypeInfo.ContainerLayout.Extern);
assert(extern_union_info.Union.tag_type == @typeOf(undefined));
assert(extern_union_info.Union.fields[0].enum_field == null);
- assert(extern_union_info.Union.fields[0].field_type == &c_void);
+ assert(extern_union_info.Union.fields[0].field_type == *c_void);
}
test "type info: struct info" {
@@ -190,13 +190,13 @@ fn testStruct() void {
assert(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed);
assert(struct_info.Struct.fields.len == 3);
assert(struct_info.Struct.fields[1].offset == null);
- assert(struct_info.Struct.fields[2].field_type == &TestStruct);
+ assert(struct_info.Struct.fields[2].field_type == *TestStruct);
assert(struct_info.Struct.defs.len == 2);
assert(struct_info.Struct.defs[0].is_pub);
assert(!struct_info.Struct.defs[0].data.Fn.is_extern);
assert(struct_info.Struct.defs[0].data.Fn.lib_name == null);
assert(struct_info.Struct.defs[0].data.Fn.return_type == void);
- assert(struct_info.Struct.defs[0].data.Fn.fn_type == fn (&const TestStruct) void);
+ assert(struct_info.Struct.defs[0].data.Fn.fn_type == fn (*const TestStruct) void);
}
const TestStruct = packed struct {
@@ -204,9 +204,9 @@ const TestStruct = packed struct {
fieldA: usize,
fieldB: void,
- fieldC: &Self,
+ fieldC: *Self,
- pub fn foo(self: &const Self) void {}
+ pub fn foo(self: *const Self) void {}
};
test "type info: function type info" {
@@ -227,7 +227,7 @@ fn testFunction() void {
const test_instance: TestStruct = undefined;
const bound_fn_info = @typeInfo(@typeOf(test_instance.foo));
assert(TypeId(bound_fn_info) == TypeId.BoundFn);
- assert(bound_fn_info.BoundFn.args[0].arg_type == &const TestStruct);
+ assert(bound_fn_info.BoundFn.args[0].arg_type == *const TestStruct);
}
fn foo(comptime a: usize, b: bool, args: ...) usize {
diff --git a/test/cases/undefined.zig b/test/cases/undefined.zig
index f1af10e532..83c620d211 100644
--- a/test/cases/undefined.zig
+++ b/test/cases/undefined.zig
@@ -27,12 +27,12 @@ test "init static array to undefined" {
const Foo = struct {
x: i32,
- fn setFooXMethod(foo: &Foo) void {
+ fn setFooXMethod(foo: *Foo) void {
foo.x = 3;
}
};
-fn setFooX(foo: &Foo) void {
+fn setFooX(foo: *Foo) void {
foo.x = 2;
}
diff --git a/test/cases/union.zig b/test/cases/union.zig
index 005ad08e6a..bdcbbdb452 100644
--- a/test/cases/union.zig
+++ b/test/cases/union.zig
@@ -68,11 +68,11 @@ test "init union with runtime value" {
assert(foo.int == 42);
}
-fn setFloat(foo: &Foo, x: f64) void {
+fn setFloat(foo: *Foo, x: f64) void {
foo.* = Foo{ .float = x };
}
-fn setInt(foo: &Foo, x: i32) void {
+fn setInt(foo: *Foo, x: i32) void {
foo.* = Foo{ .int = x };
}
@@ -108,7 +108,7 @@ fn doTest() void {
assert(bar(Payload{ .A = 1234 }) == -10);
}
-fn bar(value: &const Payload) i32 {
+fn bar(value: *const Payload) i32 {
assert(Letter(value.*) == Letter.A);
return switch (value.*) {
Payload.A => |x| return x - 1244,
@@ -147,7 +147,7 @@ test "union(enum(u32)) with specified and unspecified tag values" {
comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
}
-fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: &const MultipleChoice2) void {
+fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: *const MultipleChoice2) void {
assert(u32(@TagType(MultipleChoice2)(x.*)) == 60);
assert(1123 == switch (x.*) {
MultipleChoice2.A => 1,
@@ -163,7 +163,7 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: &const MultipleChoice2) void
}
const ExternPtrOrInt = extern union {
- ptr: &u8,
+ ptr: *u8,
int: u64,
};
test "extern union size" {
@@ -171,7 +171,7 @@ test "extern union size" {
}
const PackedPtrOrInt = packed union {
- ptr: &u8,
+ ptr: *u8,
int: u64,
};
test "extern union size" {
@@ -206,7 +206,7 @@ test "cast union to tag type of union" {
comptime testCastUnionToTagType(TheUnion{ .B = 1234 });
}
-fn testCastUnionToTagType(x: &const TheUnion) void {
+fn testCastUnionToTagType(x: *const TheUnion) void {
assert(TheTag(x.*) == TheTag.B);
}
@@ -243,7 +243,7 @@ const TheUnion2 = union(enum) {
Item2: i32,
};
-fn assertIsTheUnion2Item1(value: &const TheUnion2) void {
+fn assertIsTheUnion2Item1(value: *const TheUnion2) void {
assert(value.* == TheUnion2.Item1);
}
diff --git a/test/compare_output.zig b/test/compare_output.zig
index 0170477b8b..00ad4a709b 100644
--- a/test/compare_output.zig
+++ b/test/compare_output.zig
@@ -3,10 +3,10 @@ const std = @import("std");
const os = std.os;
const tests = @import("tests.zig");
-pub fn addCases(cases: &tests.CompareOutputContext) void {
+pub fn addCases(cases: *tests.CompareOutputContext) void {
cases.addC("hello world with libc",
\\const c = @cImport(@cInclude("stdio.h"));
- \\export fn main(argc: c_int, argv: &&u8) c_int {
+ \\export fn main(argc: c_int, argv: **u8) c_int {
\\ _ = c.puts(c"Hello, world!");
\\ return 0;
\\}
@@ -139,7 +139,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\ @cInclude("stdio.h");
\\});
\\
- \\export fn main(argc: c_int, argv: &&u8) c_int {
+ \\export fn main(argc: c_int, argv: **u8) c_int {
\\ if (is_windows) {
\\ // we want actual \n, not \r\n
\\ _ = c._setmode(1, c._O_BINARY);
@@ -284,9 +284,9 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
cases.addC("expose function pointer to C land",
\\const c = @cImport(@cInclude("stdlib.h"));
\\
- \\export fn compare_fn(a: ?&const c_void, b: ?&const c_void) c_int {
- \\ const a_int = @ptrCast(&align(1) const i32, a ?? unreachable);
- \\ const b_int = @ptrCast(&align(1) const i32, b ?? unreachable);
+ \\export fn compare_fn(a: ?*const c_void, b: ?*const c_void) c_int {
+ \\ const a_int = @ptrCast(*align(1) const i32, a ?? unreachable);
+ \\ const b_int = @ptrCast(*align(1) const i32, b ?? unreachable);
\\ if (a_int.* < b_int.*) {
\\ return -1;
\\ } else if (a_int.* > b_int.*) {
@@ -299,7 +299,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\export fn main() c_int {
\\ var array = []u32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
\\
- \\ c.qsort(@ptrCast(&c_void, &array[0]), c_ulong(array.len), @sizeOf(i32), compare_fn);
+ \\ c.qsort(@ptrCast(*c_void, &array[0]), c_ulong(array.len), @sizeOf(i32), compare_fn);
\\
\\ for (array) |item, i| {
\\ if (item != i) {
@@ -324,7 +324,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\ @cInclude("stdio.h");
\\});
\\
- \\export fn main(argc: c_int, argv: &&u8) c_int {
+ \\export fn main(argc: c_int, argv: **u8) c_int {
\\ if (is_windows) {
\\ // we want actual \n, not \r\n
\\ _ = c._setmode(1, c._O_BINARY);
@@ -344,13 +344,13 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\const Foo = struct {
\\ field1: Bar,
\\
- \\ fn method(a: &const Foo) bool { return true; }
+ \\ fn method(a: *const Foo) bool { return true; }
\\};
\\
\\const Bar = struct {
\\ field2: i32,
\\
- \\ fn method(b: &const Bar) bool { return true; }
+ \\ fn method(b: *const Bar) bool { return true; }
\\};
\\
\\pub fn main() void {
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 5215953d0a..1297ed29ab 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -1,6 +1,6 @@
const tests = @import("tests.zig");
-pub fn addCases(cases: &tests.CompileErrorContext) void {
+pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"invalid deref on switch target",
\\comptime {
@@ -109,7 +109,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
"@ptrCast discards const qualifier",
\\export fn entry() void {
\\ const x: i32 = 1234;
- \\ const y = @ptrCast(&i32, &x);
+ \\ const y = @ptrCast(*i32, &x);
\\}
,
".tmp_source.zig:3:15: error: cast discards const qualifier",
@@ -118,7 +118,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"comptime slice of undefined pointer non-zero len",
\\export fn entry() void {
- \\ const slice = (&i32)(undefined)[0..1];
+ \\ const slice = (*i32)(undefined)[0..1];
\\}
,
".tmp_source.zig:2:36: error: non-zero length slice of undefined pointer",
@@ -126,7 +126,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"type checking function pointers",
- \\fn a(b: fn (&const u8) void) void {
+ \\fn a(b: fn (*const u8) void) void {
\\ b('a');
\\}
\\fn c(d: u8) void {
@@ -136,7 +136,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ a(c);
\\}
,
- ".tmp_source.zig:8:7: error: expected type 'fn(&const u8) void', found 'fn(u8) void'",
+ ".tmp_source.zig:8:7: error: expected type 'fn(*const u8) void', found 'fn(u8) void'",
);
cases.add(
@@ -594,15 +594,15 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"attempt to use 0 bit type in extern fn",
- \\extern fn foo(ptr: extern fn(&void) void) void;
+ \\extern fn foo(ptr: extern fn(*void) void) void;
\\
\\export fn entry() void {
\\ foo(bar);
\\}
\\
- \\extern fn bar(x: &void) void { }
+ \\extern fn bar(x: *void) void { }
,
- ".tmp_source.zig:7:18: error: parameter of type '&void' has 0 bits; not allowed in function with calling convention 'ccc'",
+ ".tmp_source.zig:7:18: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'ccc'",
);
cases.add(
@@ -911,10 +911,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"pointer to noreturn",
- \\fn a() &noreturn {}
+ \\fn a() *noreturn {}
\\export fn entry() void { _ = a(); }
,
- ".tmp_source.zig:1:9: error: pointer to noreturn not allowed",
+ ".tmp_source.zig:1:8: error: pointer to noreturn not allowed",
);
cases.add(
@@ -985,7 +985,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ return a;
\\}
,
- ".tmp_source.zig:3:12: error: expected type 'i32', found '&const u8'",
+ ".tmp_source.zig:3:12: error: expected type 'i32', found '*const u8'",
);
cases.add(
@@ -1446,7 +1446,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"switch expression - switch on pointer type with no else",
- \\fn foo(x: &u8) void {
+ \\fn foo(x: *u8) void {
\\ switch (x) {
\\ &y => {},
\\ }
@@ -1454,7 +1454,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\const y: u8 = 100;
\\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
,
- ".tmp_source.zig:2:5: error: else prong required when switching on type '&u8'",
+ ".tmp_source.zig:2:5: error: else prong required when switching on type '*u8'",
);
cases.add(
@@ -1501,10 +1501,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
"address of number literal",
\\const x = 3;
\\const y = &x;
- \\fn foo() &const i32 { return y; }
+ \\fn foo() *const i32 { return y; }
\\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
,
- ".tmp_source.zig:3:30: error: expected type '&const i32', found '&const (integer literal)'",
+ ".tmp_source.zig:3:30: error: expected type '*const i32', found '*const (integer literal)'",
);
cases.add(
@@ -1529,10 +1529,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ a: i32,
\\ b: i32,
\\
- \\ fn member_a(foo: &const Foo) i32 {
+ \\ fn member_a(foo: *const Foo) i32 {
\\ return foo.a;
\\ }
- \\ fn member_b(foo: &const Foo) i32 {
+ \\ fn member_b(foo: *const Foo) i32 {
\\ return foo.b;
\\ }
\\};
@@ -1543,7 +1543,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ Foo.member_b,
\\};
\\
- \\fn f(foo: &const Foo, index: usize) void {
+ \\fn f(foo: *const Foo, index: usize) void {
\\ const result = members[index]();
\\}
\\
@@ -1692,11 +1692,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"assign null to non-nullable pointer",
- \\const a: &u8 = null;
+ \\const a: *u8 = null;
\\
\\export fn entry() usize { return @sizeOf(@typeOf(a)); }
,
- ".tmp_source.zig:1:16: error: expected type '&u8', found '(null)'",
+ ".tmp_source.zig:1:16: error: expected type '*u8', found '(null)'",
);
cases.add(
@@ -1806,7 +1806,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ One: void,
\\ Two: i32,
\\};
- \\fn bad_eql_2(a: &const EnumWithData, b: &const EnumWithData) bool {
+ \\fn bad_eql_2(a: *const EnumWithData, b: *const EnumWithData) bool {
\\ return a.* == b.*;
\\}
\\
@@ -2011,9 +2011,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"wrong number of arguments for method fn call",
\\const Foo = struct {
- \\ fn method(self: &const Foo, a: i32) void {}
+ \\ fn method(self: *const Foo, a: i32) void {}
\\};
- \\fn f(foo: &const Foo) void {
+ \\fn f(foo: *const Foo) void {
\\
\\ foo.method(1, 2);
\\}
@@ -2062,7 +2062,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"misspelled type with pointer only reference",
\\const JasonHM = u8;
- \\const JasonList = &JsonNode;
+ \\const JasonList = *JsonNode;
\\
\\const JsonOA = union(enum) {
\\ JSONArray: JsonList,
@@ -2113,16 +2113,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ derp.init();
\\}
,
- ".tmp_source.zig:14:5: error: expected type 'i32', found '&const Foo'",
+ ".tmp_source.zig:14:5: error: expected type 'i32', found '*const Foo'",
);
cases.add(
"method call with first arg type wrong container",
\\pub const List = struct {
\\ len: usize,
- \\ allocator: &Allocator,
+ \\ allocator: *Allocator,
\\
- \\ pub fn init(allocator: &Allocator) List {
+ \\ pub fn init(allocator: *Allocator) List {
\\ return List {
\\ .len = 0,
\\ .allocator = allocator,
@@ -2143,7 +2143,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ x.init();
\\}
,
- ".tmp_source.zig:23:5: error: expected type '&Allocator', found '&List'",
+ ".tmp_source.zig:23:5: error: expected type '*Allocator', found '*List'",
);
cases.add(
@@ -2308,17 +2308,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ c: u2,
\\};
\\
- \\fn foo(bit_field: &const BitField) u3 {
+ \\fn foo(bit_field: *const BitField) u3 {
\\ return bar(&bit_field.b);
\\}
\\
- \\fn bar(x: &const u3) u3 {
+ \\fn bar(x: *const u3) u3 {
\\ return x.*;
\\}
\\
\\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
,
- ".tmp_source.zig:8:26: error: expected type '&const u3', found '&align(1:3:6) const u3'",
+ ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(1:3:6) const u3'",
);
cases.add(
@@ -2441,13 +2441,13 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ const b = &a;
\\ return ptrEql(b, b);
\\}
- \\fn ptrEql(a: &[]const u8, b: &[]const u8) bool {
+ \\fn ptrEql(a: *[]const u8, b: *[]const u8) bool {
\\ return true;
\\}
\\
\\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
,
- ".tmp_source.zig:4:19: error: expected type '&[]const u8', found '&const []const u8'",
+ ".tmp_source.zig:4:19: error: expected type '*[]const u8', found '*const []const u8'",
);
cases.addCase(x: {
@@ -2493,7 +2493,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"ptrcast to non-pointer",
- \\export fn entry(a: &i32) usize {
+ \\export fn entry(a: *i32) usize {
\\ return @ptrCast(usize, a);
\\}
,
@@ -2542,16 +2542,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
"int to ptr of 0 bits",
\\export fn foo() void {
\\ var x: usize = 0x1000;
- \\ var y: &void = @intToPtr(&void, x);
+ \\ var y: *void = @intToPtr(*void, x);
\\}
,
- ".tmp_source.zig:3:31: error: type '&void' has 0 bits and cannot store information",
+ ".tmp_source.zig:3:30: error: type '*void' has 0 bits and cannot store information",
);
cases.add(
"@fieldParentPtr - non struct",
\\const Foo = i32;
- \\export fn foo(a: &i32) &Foo {
+ \\export fn foo(a: *i32) *Foo {
\\ return @fieldParentPtr(Foo, "a", a);
\\}
,
@@ -2563,7 +2563,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\const Foo = extern struct {
\\ derp: i32,
\\};
- \\export fn foo(a: &i32) &Foo {
+ \\export fn foo(a: *i32) *Foo {
\\ return @fieldParentPtr(Foo, "a", a);
\\}
,
@@ -2575,7 +2575,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\const Foo = extern struct {
\\ a: i32,
\\};
- \\export fn foo(a: i32) &Foo {
+ \\export fn foo(a: i32) *Foo {
\\ return @fieldParentPtr(Foo, "a", a);
\\}
,
@@ -2591,7 +2591,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\const foo = Foo { .a = 1, .b = 2, };
\\
\\comptime {
- \\ const field_ptr = @intToPtr(&i32, 0x1234);
+ \\ const field_ptr = @intToPtr(*i32, 0x1234);
\\ const another_foo_ptr = @fieldParentPtr(Foo, "b", field_ptr);
\\}
,
@@ -2682,7 +2682,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"returning address of local variable - simple",
- \\export fn foo() &i32 {
+ \\export fn foo() *i32 {
\\ var a: i32 = undefined;
\\ return &a;
\\}
@@ -2692,7 +2692,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"returning address of local variable - phi",
- \\export fn foo(c: bool) &i32 {
+ \\export fn foo(c: bool) *i32 {
\\ var a: i32 = undefined;
\\ var b: i32 = undefined;
\\ return if (c) &a else &b;
@@ -3086,11 +3086,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ bar(&foo.b);
\\}
\\
- \\fn bar(x: &u32) void {
+ \\fn bar(x: *u32) void {
\\ x.* += 1;
\\}
,
- ".tmp_source.zig:8:13: error: expected type '&u32', found '&align(1) u32'",
+ ".tmp_source.zig:8:13: error: expected type '*u32', found '*align(1) u32'",
);
cases.add(
@@ -3117,13 +3117,13 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
"increase pointer alignment in @ptrCast",
\\export fn entry() u32 {
\\ var bytes: [4]u8 = []u8{0x01, 0x02, 0x03, 0x04};
- \\ const ptr = @ptrCast(&u32, &bytes[0]);
+ \\ const ptr = @ptrCast(*u32, &bytes[0]);
\\ return ptr.*;
\\}
,
".tmp_source.zig:3:17: error: cast increases pointer alignment",
- ".tmp_source.zig:3:38: note: '&u8' has alignment 1",
- ".tmp_source.zig:3:27: note: '&u32' has alignment 4",
+ ".tmp_source.zig:3:38: note: '*u8' has alignment 1",
+ ".tmp_source.zig:3:26: note: '*u32' has alignment 4",
);
cases.add(
@@ -3169,7 +3169,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ return x == 5678;
\\}
,
- ".tmp_source.zig:4:32: error: expected type '&i32', found '&align(1) i32'",
+ ".tmp_source.zig:4:32: error: expected type '*i32', found '*align(1) i32'",
);
cases.add(
@@ -3198,20 +3198,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add(
"wrong pointer implicitly casted to pointer to @OpaqueType()",
\\const Derp = @OpaqueType();
- \\extern fn bar(d: &Derp) void;
+ \\extern fn bar(d: *Derp) void;
\\export fn foo() void {
\\ var x = u8(1);
- \\ bar(@ptrCast(&c_void, &x));
+ \\ bar(@ptrCast(*c_void, &x));
\\}
,
- ".tmp_source.zig:5:9: error: expected type '&Derp', found '&c_void'",
+ ".tmp_source.zig:5:9: error: expected type '*Derp', found '*c_void'",
);
cases.add(
"non-const variables of things that require const variables",
\\const Opaque = @OpaqueType();
\\
- \\export fn entry(opaque: &Opaque) void {
+ \\export fn entry(opaque: *Opaque) void {
\\ var m2 = &2;
\\ const y: u32 = m2.*;
\\
@@ -3229,10 +3229,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\}
\\
\\const Foo = struct {
- \\ fn bar(self: &const Foo) void {}
+ \\ fn bar(self: *const Foo) void {}
\\};
,
- ".tmp_source.zig:4:4: error: variable of type '&const (integer literal)' must be const or comptime",
+ ".tmp_source.zig:4:4: error: variable of type '*const (integer literal)' must be const or comptime",
".tmp_source.zig:7:4: error: variable of type '(undefined)' must be const or comptime",
".tmp_source.zig:8:4: error: variable of type '(integer literal)' must be const or comptime",
".tmp_source.zig:9:4: error: variable of type '(float literal)' must be const or comptime",
@@ -3241,7 +3241,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
".tmp_source.zig:12:4: error: variable of type 'Opaque' must be const or comptime",
".tmp_source.zig:13:4: error: variable of type 'type' must be const or comptime",
".tmp_source.zig:14:4: error: variable of type '(namespace)' must be const or comptime",
- ".tmp_source.zig:15:4: error: variable of type '(bound fn(&const Foo) void)' must be const or comptime",
+ ".tmp_source.zig:15:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
".tmp_source.zig:17:4: error: unreachable code",
);
@@ -3397,14 +3397,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\
\\export fn entry() bool {
\\ var x: i32 = 1;
- \\ return bar(@ptrCast(&MyType, &x));
+ \\ return bar(@ptrCast(*MyType, &x));
\\}
\\
- \\fn bar(x: &MyType) bool {
+ \\fn bar(x: *MyType) bool {
\\ return x.blah;
\\}
,
- ".tmp_source.zig:9:13: error: type '&MyType' does not support field access",
+ ".tmp_source.zig:9:13: error: type '*MyType' does not support field access",
);
cases.add(
@@ -3535,9 +3535,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\export fn entry() void {
\\ foo("hello",);
\\}
- \\pub extern fn foo(format: &const u8, ...) void;
+ \\pub extern fn foo(format: *const u8, ...) void;
,
- ".tmp_source.zig:2:9: error: expected type '&const u8', found '[5]u8'",
+ ".tmp_source.zig:2:9: error: expected type '*const u8', found '[5]u8'",
);
cases.add(
@@ -3902,7 +3902,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
\\ const a = Payload { .A = 1234 };
\\ foo(a);
\\}
- \\fn foo(a: &const Payload) void {
+ \\fn foo(a: *const Payload) void {
\\ switch (a.*) {
\\ Payload.A => {},
\\ else => unreachable,
diff --git a/test/gen_h.zig b/test/gen_h.zig
index 2def39bed7..9559c3395c 100644
--- a/test/gen_h.zig
+++ b/test/gen_h.zig
@@ -1,6 +1,6 @@
const tests = @import("tests.zig");
-pub fn addCases(cases: &tests.GenHContext) void {
+pub fn addCases(cases: *tests.GenHContext) void {
cases.add("declare enum",
\\const Foo = extern enum { A, B, C };
\\export fn entry(foo: Foo) void { }
diff --git a/test/runtime_safety.zig b/test/runtime_safety.zig
index 1fea6347ab..71d1d68764 100644
--- a/test/runtime_safety.zig
+++ b/test/runtime_safety.zig
@@ -1,6 +1,6 @@
const tests = @import("tests.zig");
-pub fn addCases(cases: &tests.CompareOutputContext) void {
+pub fn addCases(cases: *tests.CompareOutputContext) void {
cases.addRuntimeSafety("calling panic",
\\pub fn panic(message: []const u8, stack_trace: ?&@import("builtin").StackTrace) noreturn {
\\ @import("std").os.exit(126);
diff --git a/test/standalone/brace_expansion/build.zig b/test/standalone/brace_expansion/build.zig
index 7752f599df..64f3c08583 100644
--- a/test/standalone/brace_expansion/build.zig
+++ b/test/standalone/brace_expansion/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const main = b.addTest("main.zig");
main.setBuildMode(b.standardReleaseOptions());
diff --git a/test/standalone/brace_expansion/main.zig b/test/standalone/brace_expansion/main.zig
index c96cc2cbb9..ccb4f6dd45 100644
--- a/test/standalone/brace_expansion/main.zig
+++ b/test/standalone/brace_expansion/main.zig
@@ -14,7 +14,7 @@ const Token = union(enum) {
Eof,
};
-var global_allocator: &mem.Allocator = undefined;
+var global_allocator: *mem.Allocator = undefined;
fn tokenize(input: []const u8) !ArrayList(Token) {
const State = enum {
@@ -73,7 +73,7 @@ const ParseError = error{
OutOfMemory,
};
-fn parse(tokens: &const ArrayList(Token), token_index: &usize) ParseError!Node {
+fn parse(tokens: *const ArrayList(Token), token_index: *usize) ParseError!Node {
const first_token = tokens.items[token_index.*];
token_index.* += 1;
@@ -109,7 +109,7 @@ fn parse(tokens: &const ArrayList(Token), token_index: &usize) ParseError!Node {
}
}
-fn expandString(input: []const u8, output: &Buffer) !void {
+fn expandString(input: []const u8, output: *Buffer) !void {
const tokens = try tokenize(input);
if (tokens.len == 1) {
return output.resize(0);
@@ -139,7 +139,7 @@ fn expandString(input: []const u8, output: &Buffer) !void {
const ExpandNodeError = error{OutOfMemory};
-fn expandNode(node: &const Node, output: &ArrayList(Buffer)) ExpandNodeError!void {
+fn expandNode(node: *const Node, output: *ArrayList(Buffer)) ExpandNodeError!void {
assert(output.len == 0);
switch (node.*) {
Node.Scalar => |scalar| {
diff --git a/test/standalone/issue_339/build.zig b/test/standalone/issue_339/build.zig
index f3ab327006..733b3729c1 100644
--- a/test/standalone/issue_339/build.zig
+++ b/test/standalone/issue_339/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const obj = b.addObject("test", "test.zig");
const test_step = b.step("test", "Test the program");
diff --git a/test/standalone/issue_339/test.zig b/test/standalone/issue_339/test.zig
index da0747b8e6..f4068dcfac 100644
--- a/test/standalone/issue_339/test.zig
+++ b/test/standalone/issue_339/test.zig
@@ -1,5 +1,5 @@
const StackTrace = @import("builtin").StackTrace;
-pub fn panic(msg: []const u8, stack_trace: ?&StackTrace) noreturn {
+pub fn panic(msg: []const u8, stack_trace: ?*StackTrace) noreturn {
@breakpoint();
while (true) {}
}
diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig
index 4f5dcd7ff4..06c37a83a3 100644
--- a/test/standalone/issue_794/build.zig
+++ b/test/standalone/issue_794/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const test_artifact = b.addTest("main.zig");
test_artifact.addIncludeDir("a_directory");
diff --git a/test/standalone/pkg_import/build.zig b/test/standalone/pkg_import/build.zig
index bb9416d3c4..e0b3885dc3 100644
--- a/test/standalone/pkg_import/build.zig
+++ b/test/standalone/pkg_import/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
const exe = b.addExecutable("test", "test.zig");
exe.addPackagePath("my_pkg", "pkg.zig");
diff --git a/test/standalone/use_alias/build.zig b/test/standalone/use_alias/build.zig
index ecbba297d8..c700d43db9 100644
--- a/test/standalone/use_alias/build.zig
+++ b/test/standalone/use_alias/build.zig
@@ -1,6 +1,6 @@
const Builder = @import("std").build.Builder;
-pub fn build(b: &Builder) void {
+pub fn build(b: *Builder) void {
b.addCIncludePath(".");
const main = b.addTest("main.zig");
diff --git a/test/tests.zig b/test/tests.zig
index b59b954122..cc562331fe 100644
--- a/test/tests.zig
+++ b/test/tests.zig
@@ -47,7 +47,7 @@ const test_targets = []TestTarget{
const max_stdout_size = 1 * 1024 * 1024; // 1 MB
-pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(CompareOutputContext) catch unreachable;
cases.* = CompareOutputContext{
.b = b,
@@ -61,7 +61,7 @@ pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) &build
return cases.step;
}
-pub fn addRuntimeSafetyTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(CompareOutputContext) catch unreachable;
cases.* = CompareOutputContext{
.b = b,
@@ -75,7 +75,7 @@ pub fn addRuntimeSafetyTests(b: &build.Builder, test_filter: ?[]const u8) &build
return cases.step;
}
-pub fn addCompileErrorTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(CompileErrorContext) catch unreachable;
cases.* = CompileErrorContext{
.b = b,
@@ -89,7 +89,7 @@ pub fn addCompileErrorTests(b: &build.Builder, test_filter: ?[]const u8) &build.
return cases.step;
}
-pub fn addBuildExampleTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(BuildExamplesContext) catch unreachable;
cases.* = BuildExamplesContext{
.b = b,
@@ -103,7 +103,7 @@ pub fn addBuildExampleTests(b: &build.Builder, test_filter: ?[]const u8) &build.
return cases.step;
}
-pub fn addAssembleAndLinkTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(CompareOutputContext) catch unreachable;
cases.* = CompareOutputContext{
.b = b,
@@ -117,7 +117,7 @@ pub fn addAssembleAndLinkTests(b: &build.Builder, test_filter: ?[]const u8) &bui
return cases.step;
}
-pub fn addTranslateCTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(TranslateCContext) catch unreachable;
cases.* = TranslateCContext{
.b = b,
@@ -131,7 +131,7 @@ pub fn addTranslateCTests(b: &build.Builder, test_filter: ?[]const u8) &build.St
return cases.step;
}
-pub fn addGenHTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
+pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
const cases = b.allocator.create(GenHContext) catch unreachable;
cases.* = GenHContext{
.b = b,
@@ -145,7 +145,7 @@ pub fn addGenHTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
return cases.step;
}
-pub fn addPkgTests(b: &build.Builder, test_filter: ?[]const u8, root_src: []const u8, name: []const u8, desc: []const u8, with_lldb: bool) &build.Step {
+pub fn addPkgTests(b: *build.Builder, test_filter: ?[]const u8, root_src: []const u8, name: []const u8, desc: []const u8, with_lldb: bool) *build.Step {
const step = b.step(b.fmt("test-{}", name), desc);
for (test_targets) |test_target| {
const is_native = (test_target.os == builtin.os and test_target.arch == builtin.arch);
@@ -193,8 +193,8 @@ pub fn addPkgTests(b: &build.Builder, test_filter: ?[]const u8, root_src: []cons
}
pub const CompareOutputContext = struct {
- b: &build.Builder,
- step: &build.Step,
+ b: *build.Builder,
+ step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
@@ -217,28 +217,28 @@ pub const CompareOutputContext = struct {
source: []const u8,
};
- pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
+ pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
self.sources.append(SourceFile{
.filename = filename,
.source = source,
}) catch unreachable;
}
- pub fn setCommandLineArgs(self: &TestCase, args: []const []const u8) void {
+ pub fn setCommandLineArgs(self: *TestCase, args: []const []const u8) void {
self.cli_args = args;
}
};
const RunCompareOutputStep = struct {
step: build.Step,
- context: &CompareOutputContext,
+ context: *CompareOutputContext,
exe_path: []const u8,
name: []const u8,
expected_output: []const u8,
test_index: usize,
cli_args: []const []const u8,
- pub fn create(context: &CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) &RunCompareOutputStep {
+ pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) *RunCompareOutputStep {
const allocator = context.b.allocator;
const ptr = allocator.create(RunCompareOutputStep) catch unreachable;
ptr.* = RunCompareOutputStep{
@@ -254,7 +254,7 @@ pub const CompareOutputContext = struct {
return ptr;
}
- fn make(step: &build.Step) !void {
+ fn make(step: *build.Step) !void {
const self = @fieldParentPtr(RunCompareOutputStep, "step", step);
const b = self.context.b;
@@ -321,12 +321,12 @@ pub const CompareOutputContext = struct {
const RuntimeSafetyRunStep = struct {
step: build.Step,
- context: &CompareOutputContext,
+ context: *CompareOutputContext,
exe_path: []const u8,
name: []const u8,
test_index: usize,
- pub fn create(context: &CompareOutputContext, exe_path: []const u8, name: []const u8) &RuntimeSafetyRunStep {
+ pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8) *RuntimeSafetyRunStep {
const allocator = context.b.allocator;
const ptr = allocator.create(RuntimeSafetyRunStep) catch unreachable;
ptr.* = RuntimeSafetyRunStep{
@@ -340,7 +340,7 @@ pub const CompareOutputContext = struct {
return ptr;
}
- fn make(step: &build.Step) !void {
+ fn make(step: *build.Step) !void {
const self = @fieldParentPtr(RuntimeSafetyRunStep, "step", step);
const b = self.context.b;
@@ -382,7 +382,7 @@ pub const CompareOutputContext = struct {
}
};
- pub fn createExtra(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8, special: Special) TestCase {
+ pub fn createExtra(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8, special: Special) TestCase {
var tc = TestCase{
.name = name,
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
@@ -396,32 +396,32 @@ pub const CompareOutputContext = struct {
return tc;
}
- pub fn create(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) TestCase {
+ pub fn create(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) TestCase {
return createExtra(self, name, source, expected_output, Special.None);
}
- pub fn addC(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
+ pub fn addC(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
var tc = self.create(name, source, expected_output);
tc.link_libc = true;
self.addCase(tc);
}
- pub fn add(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
+ pub fn add(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
const tc = self.create(name, source, expected_output);
self.addCase(tc);
}
- pub fn addAsm(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
+ pub fn addAsm(self: *CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) void {
const tc = self.createExtra(name, source, expected_output, Special.Asm);
self.addCase(tc);
}
- pub fn addRuntimeSafety(self: &CompareOutputContext, name: []const u8, source: []const u8) void {
+ pub fn addRuntimeSafety(self: *CompareOutputContext, name: []const u8, source: []const u8) void {
const tc = self.createExtra(name, source, undefined, Special.RuntimeSafety);
self.addCase(tc);
}
- pub fn addCase(self: &CompareOutputContext, case: &const TestCase) void {
+ pub fn addCase(self: *CompareOutputContext, case: *const TestCase) void {
const b = self.b;
const root_src = os.path.join(b.allocator, b.cache_root, case.sources.items[0].filename) catch unreachable;
@@ -504,8 +504,8 @@ pub const CompareOutputContext = struct {
};
pub const CompileErrorContext = struct {
- b: &build.Builder,
- step: &build.Step,
+ b: *build.Builder,
+ step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
@@ -521,27 +521,27 @@ pub const CompileErrorContext = struct {
source: []const u8,
};
- pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
+ pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
self.sources.append(SourceFile{
.filename = filename,
.source = source,
}) catch unreachable;
}
- pub fn addExpectedError(self: &TestCase, text: []const u8) void {
+ pub fn addExpectedError(self: *TestCase, text: []const u8) void {
self.expected_errors.append(text) catch unreachable;
}
};
const CompileCmpOutputStep = struct {
step: build.Step,
- context: &CompileErrorContext,
+ context: *CompileErrorContext,
name: []const u8,
test_index: usize,
- case: &const TestCase,
+ case: *const TestCase,
build_mode: Mode,
- pub fn create(context: &CompileErrorContext, name: []const u8, case: &const TestCase, build_mode: Mode) &CompileCmpOutputStep {
+ pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep {
const allocator = context.b.allocator;
const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
ptr.* = CompileCmpOutputStep{
@@ -556,7 +556,7 @@ pub const CompileErrorContext = struct {
return ptr;
}
- fn make(step: &build.Step) !void {
+ fn make(step: *build.Step) !void {
const self = @fieldParentPtr(CompileCmpOutputStep, "step", step);
const b = self.context.b;
@@ -661,7 +661,7 @@ pub const CompileErrorContext = struct {
warn("\n");
}
- pub fn create(self: &CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) &TestCase {
+ pub fn create(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
const tc = self.b.allocator.create(TestCase) catch unreachable;
tc.* = TestCase{
.name = name,
@@ -678,24 +678,24 @@ pub const CompileErrorContext = struct {
return tc;
}
- pub fn addC(self: &CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn addC(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
var tc = self.create(name, source, expected_lines);
tc.link_libc = true;
self.addCase(tc);
}
- pub fn addExe(self: &CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn addExe(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
var tc = self.create(name, source, expected_lines);
tc.is_exe = true;
self.addCase(tc);
}
- pub fn add(self: &CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn add(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) void {
const tc = self.create(name, source, expected_lines);
self.addCase(tc);
}
- pub fn addCase(self: &CompileErrorContext, case: &const TestCase) void {
+ pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
const b = self.b;
for ([]Mode{
@@ -720,20 +720,20 @@ pub const CompileErrorContext = struct {
};
pub const BuildExamplesContext = struct {
- b: &build.Builder,
- step: &build.Step,
+ b: *build.Builder,
+ step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
- pub fn addC(self: &BuildExamplesContext, root_src: []const u8) void {
+ pub fn addC(self: *BuildExamplesContext, root_src: []const u8) void {
self.addAllArgs(root_src, true);
}
- pub fn add(self: &BuildExamplesContext, root_src: []const u8) void {
+ pub fn add(self: *BuildExamplesContext, root_src: []const u8) void {
self.addAllArgs(root_src, false);
}
- pub fn addBuildFile(self: &BuildExamplesContext, build_file: []const u8) void {
+ pub fn addBuildFile(self: *BuildExamplesContext, build_file: []const u8) void {
const b = self.b;
const annotated_case_name = b.fmt("build {} (Debug)", build_file);
@@ -763,7 +763,7 @@ pub const BuildExamplesContext = struct {
self.step.dependOn(&log_step.step);
}
- pub fn addAllArgs(self: &BuildExamplesContext, root_src: []const u8, link_libc: bool) void {
+ pub fn addAllArgs(self: *BuildExamplesContext, root_src: []const u8, link_libc: bool) void {
const b = self.b;
for ([]Mode{
@@ -792,8 +792,8 @@ pub const BuildExamplesContext = struct {
};
pub const TranslateCContext = struct {
- b: &build.Builder,
- step: &build.Step,
+ b: *build.Builder,
+ step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
@@ -808,26 +808,26 @@ pub const TranslateCContext = struct {
source: []const u8,
};
- pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
+ pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
self.sources.append(SourceFile{
.filename = filename,
.source = source,
}) catch unreachable;
}
- pub fn addExpectedLine(self: &TestCase, text: []const u8) void {
+ pub fn addExpectedLine(self: *TestCase, text: []const u8) void {
self.expected_lines.append(text) catch unreachable;
}
};
const TranslateCCmpOutputStep = struct {
step: build.Step,
- context: &TranslateCContext,
+ context: *TranslateCContext,
name: []const u8,
test_index: usize,
- case: &const TestCase,
+ case: *const TestCase,
- pub fn create(context: &TranslateCContext, name: []const u8, case: &const TestCase) &TranslateCCmpOutputStep {
+ pub fn create(context: *TranslateCContext, name: []const u8, case: *const TestCase) *TranslateCCmpOutputStep {
const allocator = context.b.allocator;
const ptr = allocator.create(TranslateCCmpOutputStep) catch unreachable;
ptr.* = TranslateCCmpOutputStep{
@@ -841,7 +841,7 @@ pub const TranslateCContext = struct {
return ptr;
}
- fn make(step: &build.Step) !void {
+ fn make(step: *build.Step) !void {
const self = @fieldParentPtr(TranslateCCmpOutputStep, "step", step);
const b = self.context.b;
@@ -935,7 +935,7 @@ pub const TranslateCContext = struct {
warn("\n");
}
- pub fn create(self: &TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) &TestCase {
+ pub fn create(self: *TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
const tc = self.b.allocator.create(TestCase) catch unreachable;
tc.* = TestCase{
.name = name,
@@ -951,22 +951,22 @@ pub const TranslateCContext = struct {
return tc;
}
- pub fn add(self: &TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn add(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
const tc = self.create(false, "source.h", name, source, expected_lines);
self.addCase(tc);
}
- pub fn addC(self: &TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn addC(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
const tc = self.create(false, "source.c", name, source, expected_lines);
self.addCase(tc);
}
- pub fn addAllowWarnings(self: &TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn addAllowWarnings(self: *TranslateCContext, name: []const u8, source: []const u8, expected_lines: ...) void {
const tc = self.create(true, "source.h", name, source, expected_lines);
self.addCase(tc);
}
- pub fn addCase(self: &TranslateCContext, case: &const TestCase) void {
+ pub fn addCase(self: *TranslateCContext, case: *const TestCase) void {
const b = self.b;
const annotated_case_name = fmt.allocPrint(self.b.allocator, "translate-c {}", case.name) catch unreachable;
@@ -986,8 +986,8 @@ pub const TranslateCContext = struct {
};
pub const GenHContext = struct {
- b: &build.Builder,
- step: &build.Step,
+ b: *build.Builder,
+ step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
@@ -1001,27 +1001,27 @@ pub const GenHContext = struct {
source: []const u8,
};
- pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
+ pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
self.sources.append(SourceFile{
.filename = filename,
.source = source,
}) catch unreachable;
}
- pub fn addExpectedLine(self: &TestCase, text: []const u8) void {
+ pub fn addExpectedLine(self: *TestCase, text: []const u8) void {
self.expected_lines.append(text) catch unreachable;
}
};
const GenHCmpOutputStep = struct {
step: build.Step,
- context: &GenHContext,
+ context: *GenHContext,
h_path: []const u8,
name: []const u8,
test_index: usize,
- case: &const TestCase,
+ case: *const TestCase,
- pub fn create(context: &GenHContext, h_path: []const u8, name: []const u8, case: &const TestCase) &GenHCmpOutputStep {
+ pub fn create(context: *GenHContext, h_path: []const u8, name: []const u8, case: *const TestCase) *GenHCmpOutputStep {
const allocator = context.b.allocator;
const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
ptr.* = GenHCmpOutputStep{
@@ -1036,7 +1036,7 @@ pub const GenHContext = struct {
return ptr;
}
- fn make(step: &build.Step) !void {
+ fn make(step: *build.Step) !void {
const self = @fieldParentPtr(GenHCmpOutputStep, "step", step);
const b = self.context.b;
@@ -1069,7 +1069,7 @@ pub const GenHContext = struct {
warn("\n");
}
- pub fn create(self: &GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) &TestCase {
+ pub fn create(self: *GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
const tc = self.b.allocator.create(TestCase) catch unreachable;
tc.* = TestCase{
.name = name,
@@ -1084,12 +1084,12 @@ pub const GenHContext = struct {
return tc;
}
- pub fn add(self: &GenHContext, name: []const u8, source: []const u8, expected_lines: ...) void {
+ pub fn add(self: *GenHContext, name: []const u8, source: []const u8, expected_lines: ...) void {
const tc = self.create("test.zig", name, source, expected_lines);
self.addCase(tc);
}
- pub fn addCase(self: &GenHContext, case: &const TestCase) void {
+ pub fn addCase(self: *GenHContext, case: *const TestCase) void {
const b = self.b;
const root_src = os.path.join(b.allocator, b.cache_root, case.sources.items[0].filename) catch unreachable;
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 4cf1e047fa..9a07bc343d 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -1,6 +1,6 @@
const tests = @import("tests.zig");
-pub fn addCases(cases: &tests.TranslateCContext) void {
+pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("double define struct",
\\typedef struct Bar Bar;
\\typedef struct Foo Foo;
@@ -14,11 +14,11 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\};
,
\\pub const struct_Foo = extern struct {
- \\ a: ?&Foo,
+ \\ a: ?*Foo,
\\};
\\pub const Foo = struct_Foo;
\\pub const struct_Bar = extern struct {
- \\ a: ?&Foo,
+ \\ a: ?*Foo,
\\};
);
@@ -99,7 +99,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
cases.add("restrict -> noalias",
\\void foo(void *restrict bar, void *restrict);
,
- \\pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void) void;
+ \\pub extern fn foo(noalias bar: ?*c_void, noalias arg1: ?*c_void) void;
);
cases.add("simple struct",
@@ -110,7 +110,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
,
\\const struct_Foo = extern struct {
\\ x: c_int,
- \\ y: ?&u8,
+ \\ y: ?*u8,
\\};
,
\\pub const Foo = struct_Foo;
@@ -141,7 +141,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
,
\\pub const BarB = enum_Bar.B;
,
- \\pub extern fn func(a: ?&struct_Foo, b: ?&(?&enum_Bar)) void;
+ \\pub extern fn func(a: ?*struct_Foo, b: ?*(?*enum_Bar)) void;
,
\\pub const Foo = struct_Foo;
,
@@ -151,7 +151,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
cases.add("constant size array",
\\void func(int array[20]);
,
- \\pub extern fn func(array: ?&c_int) void;
+ \\pub extern fn func(array: ?*c_int) void;
);
cases.add("self referential struct with function pointer",
@@ -160,7 +160,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\};
,
\\pub const struct_Foo = extern struct {
- \\ derp: ?extern fn(?&struct_Foo) void,
+ \\ derp: ?extern fn(?*struct_Foo) void,
\\};
,
\\pub const Foo = struct_Foo;
@@ -172,7 +172,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
,
\\pub const struct_Foo = @OpaqueType();
,
- \\pub extern fn some_func(foo: ?&struct_Foo, x: c_int) ?&struct_Foo;
+ \\pub extern fn some_func(foo: ?*struct_Foo, x: c_int) ?*struct_Foo;
,
\\pub const Foo = struct_Foo;
);
@@ -219,11 +219,11 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\};
,
\\pub const struct_Bar = extern struct {
- \\ next: ?&struct_Foo,
+ \\ next: ?*struct_Foo,
\\};
,
\\pub const struct_Foo = extern struct {
- \\ next: ?&struct_Bar,
+ \\ next: ?*struct_Bar,
\\};
);
@@ -233,7 +233,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
,
\\pub const Foo = c_void;
,
- \\pub extern fn fun(a: ?&Foo) Foo;
+ \\pub extern fn fun(a: ?*Foo) Foo;
);
cases.add("generate inline func for #define global extern fn",
@@ -505,7 +505,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return 6;
\\}
,
- \\pub export fn and_or_none_bool(a: c_int, b: f32, c: ?&c_void) c_int {
+ \\pub export fn and_or_none_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ if ((a != 0) and (b != 0)) return 0;
\\ if ((b != 0) and (c != null)) return 1;
\\ if ((a != 0) and (c != null)) return 2;
@@ -607,7 +607,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\pub const struct_Foo = extern struct {
\\ field: c_int,
\\};
- \\pub export fn read_field(foo: ?&struct_Foo) c_int {
+ \\pub export fn read_field(foo: ?*struct_Foo) c_int {
\\ return (??foo).field;
\\}
);
@@ -653,8 +653,8 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return x;
\\}
,
- \\pub export fn foo(x: ?&c_ushort) ?&c_void {
- \\ return @ptrCast(?&c_void, x);
+ \\pub export fn foo(x: ?*c_ushort) ?*c_void {
+ \\ return @ptrCast(?*c_void, x);
\\}
);
@@ -674,7 +674,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return 0;
\\}
,
- \\pub export fn foo() ?&c_int {
+ \\pub export fn foo() ?*c_int {
\\ return null;
\\}
);
@@ -983,7 +983,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ *x = 1;
\\}
,
- \\pub export fn foo(x: ?&c_int) void {
+ \\pub export fn foo(x: ?*c_int) void {
\\ (??x).* = 1;
\\}
);
@@ -1011,7 +1011,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
,
\\pub fn foo() c_int {
\\ var x: c_int = 1234;
- \\ var ptr: ?&c_int = &x;
+ \\ var ptr: ?*c_int = &x;
\\ return (??ptr).*;
\\}
);
@@ -1021,7 +1021,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return "bar";
\\}
,
- \\pub fn foo() ?&const u8 {
+ \\pub fn foo() ?*const u8 {
\\ return c"bar";
\\}
);
@@ -1150,8 +1150,8 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return (float *)a;
\\}
,
- \\fn ptrcast(a: ?&c_int) ?&f32 {
- \\ return @ptrCast(?&f32, a);
+ \\fn ptrcast(a: ?*c_int) ?*f32 {
+ \\ return @ptrCast(?*f32, a);
\\}
);
@@ -1173,7 +1173,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return !c;
\\}
,
- \\pub fn foo(a: c_int, b: f32, c: ?&c_void) c_int {
+ \\pub fn foo(a: c_int, b: f32, c: ?*c_void) c_int {
\\ return !(a == 0);
\\ return !(a != 0);
\\ return !(b != 0);
@@ -1194,7 +1194,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
cases.add("const ptr initializer",
\\static const char *v0 = "0.0.0";
,
- \\pub var v0: ?&const u8 = c"0.0.0";
+ \\pub var v0: ?*const u8 = c"0.0.0";
);
cases.add("static incomplete array inside function",
@@ -1203,14 +1203,14 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\}
,
\\pub fn foo() void {
- \\ const v2: &const u8 = c"2.2.2";
+ \\ const v2: *const u8 = c"2.2.2";
\\}
);
cases.add("macro pointer cast",
\\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
,
- \\pub const NRF_GPIO = if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Pointer) @ptrCast(&NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Int) @intToPtr(&NRF_GPIO_Type, NRF_GPIO_BASE) else (&NRF_GPIO_Type)(NRF_GPIO_BASE);
+ \\pub const NRF_GPIO = if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Pointer) @ptrCast(*NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Int) @intToPtr(*NRF_GPIO_Type, NRF_GPIO_BASE) else (*NRF_GPIO_Type)(NRF_GPIO_BASE);
);
cases.add("if on none bool",
@@ -1231,7 +1231,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ B,
\\ C,
\\};
- \\pub fn if_none_bool(a: c_int, b: f32, c: ?&c_void, d: enum_SomeEnum) c_int {
+ \\pub fn if_none_bool(a: c_int, b: f32, c: ?*c_void, d: enum_SomeEnum) c_int {
\\ if (a != 0) return 0;
\\ if (b != 0) return 1;
\\ if (c != null) return 2;
@@ -1248,7 +1248,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return 3;
\\}
,
- \\pub fn while_none_bool(a: c_int, b: f32, c: ?&c_void) c_int {
+ \\pub fn while_none_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ while (a != 0) return 0;
\\ while (b != 0) return 1;
\\ while (c != null) return 2;
@@ -1264,7 +1264,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return 3;
\\}
,
- \\pub fn for_none_bool(a: c_int, b: f32, c: ?&c_void) c_int {
+ \\pub fn for_none_bool(a: c_int, b: f32, c: ?*c_void) c_int {
\\ while (a != 0) return 0;
\\ while (b != 0) return 1;
\\ while (c != null) return 2;
--
cgit v1.2.3
From 77678b2cbc7ac9ba2d5d4725241f6a9f7ac64fa4 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sun, 10 Jun 2018 01:13:51 -0400
Subject: breaking syntax change: orelse keyword instead of ?? (#1096)
use the `zig-fmt-optional-default` branch to have zig fmt
automatically do the changes.
closes #1023
---
build.zig | 6 +++---
doc/docgen.zig | 6 +++---
doc/langref.html.in | 16 +++++++--------
src-self-hosted/main.zig | 14 ++++++-------
src-self-hosted/module.zig | 8 ++++----
src/all_types.hpp | 7 ++++++-
src/analyze.cpp | 1 +
src/ast_render.cpp | 12 +++++++++--
src/ir.cpp | 31 ++++++++++++-----------------
src/parser.cpp | 13 ++++++------
src/tokenizer.cpp | 27 ++++++-------------------
src/tokenizer.hpp | 2 +-
src/translate_c.cpp | 16 ++++++++-------
std/atomic/queue.zig | 4 ++--
std/atomic/stack.zig | 4 ++--
std/buf_map.zig | 6 +++---
std/buf_set.zig | 4 ++--
std/build.zig | 24 +++++++++++-----------
std/debug/index.zig | 20 +++++++++----------
std/heap.zig | 10 +++++-----
std/linked_list.zig | 4 ++--
std/os/index.zig | 14 ++++++-------
std/os/linux/vdso.zig | 8 ++++----
std/os/path.zig | 12 +++++------
std/os/windows/util.zig | 2 +-
std/special/build_runner.zig | 10 +++++-----
std/unicode.zig | 2 +-
std/zig/parse.zig | 47 ++++++++++++++++++++++----------------------
std/zig/render.zig | 8 ++++----
test/cases/cast.zig | 6 +++---
test/cases/null.zig | 10 +++++-----
test/compile_errors.zig | 2 +-
test/translate_c.zig | 20 +++++++++----------
33 files changed, 187 insertions(+), 189 deletions(-)
(limited to 'std/atomic')
diff --git a/build.zig b/build.zig
index eada37816c..fd154c7504 100644
--- a/build.zig
+++ b/build.zig
@@ -102,11 +102,11 @@ pub fn build(b: *Builder) !void {
b.default_step.dependOn(&exe.step);
- const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") ?? false;
+ const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") orelse false;
if (!skip_self_hosted) {
test_step.dependOn(&exe.step);
}
- const verbose_link_exe = b.option(bool, "verbose-link", "Print link command for self hosted compiler") ?? false;
+ const verbose_link_exe = b.option(bool, "verbose-link", "Print link command for self hosted compiler") orelse false;
exe.setVerboseLink(verbose_link_exe);
b.installArtifact(exe);
@@ -114,7 +114,7 @@ pub fn build(b: *Builder) !void {
installCHeaders(b, c_header_files);
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
- const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") ?? false;
+ const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") orelse false;
test_step.dependOn(docs_step);
diff --git a/doc/docgen.zig b/doc/docgen.zig
index ed0e1be273..3283d146b0 100644
--- a/doc/docgen.zig
+++ b/doc/docgen.zig
@@ -25,13 +25,13 @@ pub fn main() !void {
if (!args_it.skip()) @panic("expected self arg");
- const zig_exe = try (args_it.next(allocator) ?? @panic("expected zig exe arg"));
+ const zig_exe = try (args_it.next(allocator) orelse @panic("expected zig exe arg"));
defer allocator.free(zig_exe);
- const in_file_name = try (args_it.next(allocator) ?? @panic("expected input arg"));
+ const in_file_name = try (args_it.next(allocator) orelse @panic("expected input arg"));
defer allocator.free(in_file_name);
- const out_file_name = try (args_it.next(allocator) ?? @panic("expected output arg"));
+ const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg"));
defer allocator.free(out_file_name);
var in_file = try os.File.openRead(allocator, in_file_name);
diff --git a/doc/langref.html.in b/doc/langref.html.in
index 4c4a637095..0ada8a5196 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -985,7 +985,7 @@ a ^= b
- a ?? b
|
+ a orelse b
|
- {#link|Optionals#}
@@ -998,7 +998,7 @@ a ^= b |
const value: ?u32 = null;
-const unwrapped = value ?? 1234;
+const unwrapped = value orelse 1234;
unwrapped == 1234
|
@@ -1011,7 +1011,7 @@ unwrapped == 1234
Equivalent to:
- a ?? unreachable
+ a orelse unreachable
|
const value: ?u32 = 5678;
@@ -1278,7 +1278,7 @@ x{} x.* x.?
== != < > <= >=
and
or
-?? catch
+orelse catch
= *= /= %= += -= <<= >>= &= ^= |=
{#header_close#}
{#header_close#}
@@ -3062,7 +3062,7 @@ fn createFoo(param: i32) !Foo {
// but we want to return it if the function succeeds.
errdefer deallocateFoo(foo);
- const tmp_buf = allocateTmpBuffer() ?? return error.OutOfMemory;
+ const tmp_buf = allocateTmpBuffer() orelse return error.OutOfMemory;
// tmp_buf is truly a temporary resource, and we for sure want to clean it up
// before this block leaves scope
defer deallocateTmpBuffer(tmp_buf);
@@ -3219,13 +3219,13 @@ struct Foo *do_a_thing(void) {
extern fn malloc(size: size_t) ?*u8;
fn doAThing() ?*Foo {
- const ptr = malloc(1234) ?? return null;
+ const ptr = malloc(1234) orelse return null;
// ...
}
{#code_end#}
Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr"
- is *u8 not ?*u8. The ?? operator
+ is *u8 not ?*u8. The orelse keyword
unwrapped the optional type and therefore ptr is guaranteed to be non-null everywhere
it is used in the function.
@@ -5941,7 +5941,7 @@ AsmClobbers= ":" list(String, ",")
UnwrapExpression = BoolOrExpression (UnwrapOptional | UnwrapError) | BoolOrExpression
-UnwrapOptional = "??" Expression
+UnwrapOptional = "orelse" Expression
UnwrapError = "catch" option("|" Symbol "|") Expression
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index 64734f077a..1c91ab9cbe 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -212,7 +212,7 @@ fn cmdBuild(allocator: *Allocator, args: []const []const u8) !void {
const build_runner_path = try os.path.join(allocator, special_dir, "build_runner.zig");
defer allocator.free(build_runner_path);
- const build_file = flags.single("build-file") ?? "build.zig";
+ const build_file = flags.single("build-file") orelse "build.zig";
const build_file_abs = try os.path.resolve(allocator, ".", build_file);
defer allocator.free(build_file_abs);
@@ -516,7 +516,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
const basename = os.path.basename(in_file.?);
var it = mem.split(basename, ".");
- const root_name = it.next() ?? {
+ const root_name = it.next() orelse {
try stderr.write("file name cannot be empty\n");
os.exit(1);
};
@@ -535,7 +535,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
const zig_root_source_file = in_file;
- const full_cache_dir = os.path.resolve(allocator, ".", flags.single("cache-dir") ?? "zig-cache"[0..]) catch {
+ const full_cache_dir = os.path.resolve(allocator, ".", flags.single("cache-dir") orelse "zig-cache"[0..]) catch {
os.exit(1);
};
defer allocator.free(full_cache_dir);
@@ -555,9 +555,9 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
);
defer module.destroy();
- module.version_major = try std.fmt.parseUnsigned(u32, flags.single("ver-major") ?? "0", 10);
- module.version_minor = try std.fmt.parseUnsigned(u32, flags.single("ver-minor") ?? "0", 10);
- module.version_patch = try std.fmt.parseUnsigned(u32, flags.single("ver-patch") ?? "0", 10);
+ module.version_major = try std.fmt.parseUnsigned(u32, flags.single("ver-major") orelse "0", 10);
+ module.version_minor = try std.fmt.parseUnsigned(u32, flags.single("ver-minor") orelse "0", 10);
+ module.version_patch = try std.fmt.parseUnsigned(u32, flags.single("ver-patch") orelse "0", 10);
module.is_test = false;
@@ -652,7 +652,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
}
try module.build();
- try module.link(flags.single("out-file") ?? null);
+ try module.link(flags.single("out-file") orelse null);
if (flags.present("print-timing-info")) {
// codegen_print_timing_info(g, stderr);
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index a7ddf3f9e9..575105f25f 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -130,13 +130,13 @@ pub const Module = struct {
var name_buffer = try Buffer.init(allocator, name);
errdefer name_buffer.deinit();
- const context = c.LLVMContextCreate() ?? return error.OutOfMemory;
+ const context = c.LLVMContextCreate() orelse return error.OutOfMemory;
errdefer c.LLVMContextDispose(context);
- const module = c.LLVMModuleCreateWithNameInContext(name_buffer.ptr(), context) ?? return error.OutOfMemory;
+ const module = c.LLVMModuleCreateWithNameInContext(name_buffer.ptr(), context) orelse return error.OutOfMemory;
errdefer c.LLVMDisposeModule(module);
- const builder = c.LLVMCreateBuilderInContext(context) ?? return error.OutOfMemory;
+ const builder = c.LLVMCreateBuilderInContext(context) orelse return error.OutOfMemory;
errdefer c.LLVMDisposeBuilder(builder);
const module_ptr = try allocator.create(Module);
@@ -223,7 +223,7 @@ pub const Module = struct {
c.ZigLLVMParseCommandLineOptions(self.llvm_argv.len + 1, c_compatible_args.ptr);
}
- const root_src_path = self.root_src_path ?? @panic("TODO handle null root src path");
+ const root_src_path = self.root_src_path orelse @panic("TODO handle null root src path");
const root_src_real_path = os.path.real(self.allocator, root_src_path) catch |err| {
try printError("unable to get real path '{}': {}", root_src_path, err);
return err;
diff --git a/src/all_types.hpp b/src/all_types.hpp
index 2a5a0ad740..ab219e4e56 100644
--- a/src/all_types.hpp
+++ b/src/all_types.hpp
@@ -387,6 +387,7 @@ enum NodeType {
NodeTypeSliceExpr,
NodeTypeFieldAccessExpr,
NodeTypePtrDeref,
+ NodeTypeUnwrapOptional,
NodeTypeUse,
NodeTypeBoolLiteral,
NodeTypeNullLiteral,
@@ -575,6 +576,10 @@ struct AstNodeCatchExpr {
AstNode *op2;
};
+struct AstNodeUnwrapOptional {
+ AstNode *expr;
+};
+
enum CastOp {
CastOpNoCast, // signifies the function call expression is not a cast
CastOpNoop, // fn call expr is a cast, but does nothing
@@ -624,7 +629,6 @@ enum PrefixOp {
PrefixOpNegation,
PrefixOpNegationWrap,
PrefixOpOptional,
- PrefixOpUnwrapOptional,
PrefixOpAddrOf,
};
@@ -909,6 +913,7 @@ struct AstNode {
AstNodeTestDecl test_decl;
AstNodeBinOpExpr bin_op_expr;
AstNodeCatchExpr unwrap_err_expr;
+ AstNodeUnwrapOptional unwrap_optional;
AstNodePrefixOpExpr prefix_op_expr;
AstNodePointerType pointer_type;
AstNodeFnCallExpr fn_call_expr;
diff --git a/src/analyze.cpp b/src/analyze.cpp
index ed261148ea..0aa5ea5dcb 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -3308,6 +3308,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
case NodeTypeAsmExpr:
case NodeTypeFieldAccessExpr:
case NodeTypePtrDeref:
+ case NodeTypeUnwrapOptional:
case NodeTypeStructField:
case NodeTypeContainerInitExpr:
case NodeTypeStructValueField:
diff --git a/src/ast_render.cpp b/src/ast_render.cpp
index 2c8c03b226..2ace00885d 100644
--- a/src/ast_render.cpp
+++ b/src/ast_render.cpp
@@ -50,7 +50,7 @@ static const char *bin_op_str(BinOpType bin_op) {
case BinOpTypeAssignBitXor: return "^=";
case BinOpTypeAssignBitOr: return "|=";
case BinOpTypeAssignMergeErrorSets: return "||=";
- case BinOpTypeUnwrapOptional: return "??";
+ case BinOpTypeUnwrapOptional: return "orelse";
case BinOpTypeArrayCat: return "++";
case BinOpTypeArrayMult: return "**";
case BinOpTypeErrorUnion: return "!";
@@ -67,7 +67,6 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
case PrefixOpBoolNot: return "!";
case PrefixOpBinNot: return "~";
case PrefixOpOptional: return "?";
- case PrefixOpUnwrapOptional: return "??";
case PrefixOpAddrOf: return "&";
}
zig_unreachable();
@@ -222,6 +221,8 @@ static const char *node_type_str(NodeType node_type) {
return "FieldAccessExpr";
case NodeTypePtrDeref:
return "PtrDerefExpr";
+ case NodeTypeUnwrapOptional:
+ return "UnwrapOptional";
case NodeTypeContainerDecl:
return "ContainerDecl";
case NodeTypeStructField:
@@ -711,6 +712,13 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
fprintf(ar->f, ".*");
break;
}
+ case NodeTypeUnwrapOptional:
+ {
+ AstNode *lhs = node->data.unwrap_optional.expr;
+ render_node_ungrouped(ar, lhs);
+ fprintf(ar->f, ".?");
+ break;
+ }
case NodeTypeUndefinedLiteral:
fprintf(ar->f, "undefined");
break;
diff --git a/src/ir.cpp b/src/ir.cpp
index 02606fc4aa..96eb5f7434 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -4661,21 +4661,6 @@ static IrInstruction *ir_gen_err_assert_ok(IrBuilder *irb, Scope *scope, AstNode
return ir_build_load_ptr(irb, scope, source_node, payload_ptr);
}
-static IrInstruction *ir_gen_maybe_assert_ok(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
- assert(node->type == NodeTypePrefixOpExpr);
- AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
-
- IrInstruction *maybe_ptr = ir_gen_node_extra(irb, expr_node, scope, LVAL_PTR);
- if (maybe_ptr == irb->codegen->invalid_instruction)
- return irb->codegen->invalid_instruction;
-
- IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, scope, node, maybe_ptr, true);
- if (lval.is_ptr)
- return unwrapped_ptr;
-
- return ir_build_load_ptr(irb, scope, node, unwrapped_ptr);
-}
-
static IrInstruction *ir_gen_bool_not(IrBuilder *irb, Scope *scope, AstNode *node) {
assert(node->type == NodeTypePrefixOpExpr);
AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
@@ -4705,8 +4690,6 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod
return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegationWrap), lval);
case PrefixOpOptional:
return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpOptional), lval);
- case PrefixOpUnwrapOptional:
- return ir_gen_maybe_assert_ok(irb, scope, node, lval);
case PrefixOpAddrOf: {
AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
return ir_lval_wrap(irb, scope, ir_gen_node_extra(irb, expr_node, scope, LVAL_PTR), lval);
@@ -6541,7 +6524,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
return ir_build_load_ptr(irb, scope, node, ptr_instruction);
}
case NodeTypePtrDeref: {
- assert(node->type == NodeTypePtrDeref);
AstNode *expr_node = node->data.ptr_deref_expr.target;
IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
if (value == irb->codegen->invalid_instruction)
@@ -6549,6 +6531,19 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
return ir_build_un_op(irb, scope, node, IrUnOpDereference, value);
}
+ case NodeTypeUnwrapOptional: {
+ AstNode *expr_node = node->data.unwrap_optional.expr;
+
+ IrInstruction *maybe_ptr = ir_gen_node_extra(irb, expr_node, scope, LVAL_PTR);
+ if (maybe_ptr == irb->codegen->invalid_instruction)
+ return irb->codegen->invalid_instruction;
+
+ IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, scope, node, maybe_ptr, true);
+ if (lval.is_ptr)
+ return unwrapped_ptr;
+
+ return ir_build_load_ptr(irb, scope, node, unwrapped_ptr);
+ }
case NodeTypeThisLiteral:
return ir_lval_wrap(irb, scope, ir_gen_this_literal(irb, scope, node), lval);
case NodeTypeBoolLiteral:
diff --git a/src/parser.cpp b/src/parser.cpp
index 2ee69f81ab..adb1633f5d 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -1151,9 +1151,8 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
} else if (token->id == TokenIdQuestion) {
*token_index += 1;
- AstNode *node = ast_create_node(pc, NodeTypePrefixOpExpr, first_token);
- node->data.prefix_op_expr.prefix_op = PrefixOpUnwrapOptional;
- node->data.prefix_op_expr.primary_expr = primary_expr;
+ AstNode *node = ast_create_node(pc, NodeTypeUnwrapOptional, first_token);
+ node->data.unwrap_optional.expr = primary_expr;
primary_expr = node;
} else {
@@ -1173,7 +1172,6 @@ static PrefixOp tok_to_prefix_op(Token *token) {
case TokenIdMinusPercent: return PrefixOpNegationWrap;
case TokenIdTilde: return PrefixOpBinNot;
case TokenIdQuestion: return PrefixOpOptional;
- case TokenIdDoubleQuestion: return PrefixOpUnwrapOptional;
case TokenIdAmpersand: return PrefixOpAddrOf;
default: return PrefixOpInvalid;
}
@@ -2312,7 +2310,7 @@ static BinOpType ast_parse_ass_op(ParseContext *pc, size_t *token_index, bool ma
/*
UnwrapExpression : BoolOrExpression (UnwrapOptional | UnwrapError) | BoolOrExpression
-UnwrapOptional : "??" BoolOrExpression
+UnwrapOptional = "orelse" Expression
UnwrapError = "catch" option("|" Symbol "|") Expression
*/
static AstNode *ast_parse_unwrap_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
@@ -2322,7 +2320,7 @@ static AstNode *ast_parse_unwrap_expr(ParseContext *pc, size_t *token_index, boo
Token *token = &pc->tokens->at(*token_index);
- if (token->id == TokenIdDoubleQuestion) {
+ if (token->id == TokenIdKeywordOrElse) {
*token_index += 1;
AstNode *rhs = ast_parse_expression(pc, token_index, true);
@@ -3035,6 +3033,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
case NodeTypePtrDeref:
visit_field(&node->data.ptr_deref_expr.target, visit, context);
break;
+ case NodeTypeUnwrapOptional:
+ visit_field(&node->data.unwrap_optional.expr, visit, context);
+ break;
case NodeTypeUse:
visit_field(&node->data.use.expr, visit, context);
break;
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
index cfabdf11ad..2950b4eb49 100644
--- a/src/tokenizer.cpp
+++ b/src/tokenizer.cpp
@@ -134,6 +134,7 @@ static const struct ZigKeyword zig_keywords[] = {
{"noalias", TokenIdKeywordNoAlias},
{"null", TokenIdKeywordNull},
{"or", TokenIdKeywordOr},
+ {"orelse", TokenIdKeywordOrElse},
{"packed", TokenIdKeywordPacked},
{"promise", TokenIdKeywordPromise},
{"pub", TokenIdKeywordPub},
@@ -215,7 +216,6 @@ enum TokenizeState {
TokenizeStateSawGreaterThanGreaterThan,
TokenizeStateSawDot,
TokenizeStateSawDotDot,
- TokenizeStateSawQuestionMark,
TokenizeStateSawAtSign,
TokenizeStateCharCode,
TokenizeStateError,
@@ -532,6 +532,10 @@ void tokenize(Buf *buf, Tokenization *out) {
begin_token(&t, TokenIdComma);
end_token(&t);
break;
+ case '?':
+ begin_token(&t, TokenIdQuestion);
+ end_token(&t);
+ break;
case '{':
begin_token(&t, TokenIdLBrace);
end_token(&t);
@@ -624,28 +628,10 @@ void tokenize(Buf *buf, Tokenization *out) {
begin_token(&t, TokenIdDot);
t.state = TokenizeStateSawDot;
break;
- case '?':
- begin_token(&t, TokenIdQuestion);
- t.state = TokenizeStateSawQuestionMark;
- break;
default:
invalid_char_error(&t, c);
}
break;
- case TokenizeStateSawQuestionMark:
- switch (c) {
- case '?':
- set_token_id(&t, t.cur_tok, TokenIdDoubleQuestion);
- end_token(&t);
- t.state = TokenizeStateStart;
- break;
- default:
- t.pos -= 1;
- end_token(&t);
- t.state = TokenizeStateStart;
- continue;
- }
- break;
case TokenizeStateSawDot:
switch (c) {
case '.':
@@ -1480,7 +1466,6 @@ void tokenize(Buf *buf, Tokenization *out) {
case TokenizeStateSawGreaterThan:
case TokenizeStateSawGreaterThanGreaterThan:
case TokenizeStateSawDot:
- case TokenizeStateSawQuestionMark:
case TokenizeStateSawAtSign:
case TokenizeStateSawStarPercent:
case TokenizeStateSawPlusPercent:
@@ -1545,7 +1530,6 @@ const char * token_name(TokenId id) {
case TokenIdDash: return "-";
case TokenIdDivEq: return "/=";
case TokenIdDot: return ".";
- case TokenIdDoubleQuestion: return "??";
case TokenIdEllipsis2: return "..";
case TokenIdEllipsis3: return "...";
case TokenIdEof: return "EOF";
@@ -1582,6 +1566,7 @@ const char * token_name(TokenId id) {
case TokenIdKeywordNoAlias: return "noalias";
case TokenIdKeywordNull: return "null";
case TokenIdKeywordOr: return "or";
+ case TokenIdKeywordOrElse: return "orelse";
case TokenIdKeywordPacked: return "packed";
case TokenIdKeywordPromise: return "promise";
case TokenIdKeywordPub: return "pub";
diff --git a/src/tokenizer.hpp b/src/tokenizer.hpp
index 7c617f85c6..75c7feb476 100644
--- a/src/tokenizer.hpp
+++ b/src/tokenizer.hpp
@@ -41,7 +41,6 @@ enum TokenId {
TokenIdDash,
TokenIdDivEq,
TokenIdDot,
- TokenIdDoubleQuestion,
TokenIdEllipsis2,
TokenIdEllipsis3,
TokenIdEof,
@@ -76,6 +75,7 @@ enum TokenId {
TokenIdKeywordNoAlias,
TokenIdKeywordNull,
TokenIdKeywordOr,
+ TokenIdKeywordOrElse,
TokenIdKeywordPacked,
TokenIdKeywordPromise,
TokenIdKeywordPub,
diff --git a/src/translate_c.cpp b/src/translate_c.cpp
index aaaf5a1edb..db46d31c5b 100644
--- a/src/translate_c.cpp
+++ b/src/translate_c.cpp
@@ -260,6 +260,12 @@ static AstNode *trans_create_node_prefix_op(Context *c, PrefixOp op, AstNode *ch
return node;
}
+static AstNode *trans_create_node_unwrap_null(Context *c, AstNode *child_node) {
+ AstNode *node = trans_create_node(c, NodeTypeUnwrapOptional);
+ node->data.unwrap_optional.expr = child_node;
+ return node;
+}
+
static AstNode *trans_create_node_bin_op(Context *c, AstNode *lhs_node, BinOpType op, AstNode *rhs_node) {
AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
node->data.bin_op_expr.op1 = lhs_node;
@@ -382,7 +388,7 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r
fn_def->data.fn_def.fn_proto = fn_proto;
fn_proto->data.fn_proto.fn_def_node = fn_def;
- AstNode *unwrap_node = trans_create_node_prefix_op(c, PrefixOpUnwrapOptional, ref_node);
+ AstNode *unwrap_node = trans_create_node_unwrap_null(c, ref_node);
AstNode *fn_call_node = trans_create_node(c, NodeTypeFnCallExpr);
fn_call_node->data.fn_call_expr.fn_ref_expr = unwrap_node;
@@ -409,10 +415,6 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r
return fn_def;
}
-static AstNode *trans_create_node_unwrap_null(Context *c, AstNode *child) {
- return trans_create_node_prefix_op(c, PrefixOpUnwrapOptional, child);
-}
-
static AstNode *get_global(Context *c, Buf *name) {
{
auto entry = c->global_table.maybe_get(name);
@@ -1963,7 +1965,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
bool is_fn_ptr = qual_type_is_fn_ptr(stmt->getSubExpr()->getType());
if (is_fn_ptr)
return value_node;
- AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapOptional, value_node);
+ AstNode *unwrapped = trans_create_node_unwrap_null(c, value_node);
return trans_create_node_ptr_deref(c, unwrapped);
}
case UO_Plus:
@@ -2587,7 +2589,7 @@ static AstNode *trans_call_expr(Context *c, ResultUsed result_used, TransScope *
}
}
if (callee_node == nullptr) {
- callee_node = trans_create_node_prefix_op(c, PrefixOpUnwrapOptional, callee_raw_node);
+ callee_node = trans_create_node_unwrap_null(c, callee_raw_node);
}
} else {
callee_node = callee_raw_node;
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 142c958173..4f856d9e01 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -33,8 +33,8 @@ pub fn Queue(comptime T: type) type {
pub fn get(self: *Self) ?*Node {
var head = @atomicLoad(*Node, &self.head, AtomicOrder.SeqCst);
while (true) {
- const node = head.next ?? return null;
- head = @cmpxchgWeak(*Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return node;
+ const node = head.next orelse return null;
+ head = @cmpxchgWeak(*Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return node;
}
}
};
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 15611188d2..77fa1a9100 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -28,14 +28,14 @@ pub fn Stack(comptime T: type) type {
var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
while (true) {
node.next = root;
- root = @cmpxchgWeak(?*Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? break;
+ root = @cmpxchgWeak(?*Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse break;
}
}
pub fn pop(self: *Self) ?*Node {
var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
while (true) {
- root = @cmpxchgWeak(?*Node, &self.root, root, (root ?? return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return root;
+ root = @cmpxchgWeak(?*Node, &self.root, root, (root orelse return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return root;
}
}
diff --git a/std/buf_map.zig b/std/buf_map.zig
index 0d4f3a6d5e..a82d1b731a 100644
--- a/std/buf_map.zig
+++ b/std/buf_map.zig
@@ -19,7 +19,7 @@ pub const BufMap = struct {
pub fn deinit(self: *const BufMap) void {
var it = self.hash_map.iterator();
while (true) {
- const entry = it.next() ?? break;
+ const entry = it.next() orelse break;
self.free(entry.key);
self.free(entry.value);
}
@@ -37,12 +37,12 @@ pub const BufMap = struct {
}
pub fn get(self: *const BufMap, key: []const u8) ?[]const u8 {
- const entry = self.hash_map.get(key) ?? return null;
+ const entry = self.hash_map.get(key) orelse return null;
return entry.value;
}
pub fn delete(self: *BufMap, key: []const u8) void {
- const entry = self.hash_map.remove(key) ?? return;
+ const entry = self.hash_map.remove(key) orelse return;
self.free(entry.key);
self.free(entry.value);
}
diff --git a/std/buf_set.zig b/std/buf_set.zig
index 03a050ed8b..ab2d8e7c34 100644
--- a/std/buf_set.zig
+++ b/std/buf_set.zig
@@ -17,7 +17,7 @@ pub const BufSet = struct {
pub fn deinit(self: *const BufSet) void {
var it = self.hash_map.iterator();
while (true) {
- const entry = it.next() ?? break;
+ const entry = it.next() orelse break;
self.free(entry.key);
}
@@ -33,7 +33,7 @@ pub const BufSet = struct {
}
pub fn delete(self: *BufSet, key: []const u8) void {
- const entry = self.hash_map.remove(key) ?? return;
+ const entry = self.hash_map.remove(key) orelse return;
self.free(entry.key);
}
diff --git a/std/build.zig b/std/build.zig
index fed02e0815..5733aec17d 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -136,7 +136,7 @@ pub const Builder = struct {
}
pub fn setInstallPrefix(self: *Builder, maybe_prefix: ?[]const u8) void {
- self.prefix = maybe_prefix ?? "/usr/local"; // TODO better default
+ self.prefix = maybe_prefix orelse "/usr/local"; // TODO better default
self.lib_dir = os.path.join(self.allocator, self.prefix, "lib") catch unreachable;
self.exe_dir = os.path.join(self.allocator, self.prefix, "bin") catch unreachable;
}
@@ -312,9 +312,9 @@ pub const Builder = struct {
if (os.getEnvVarOwned(self.allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
var it = mem.split(nix_cflags_compile, " ");
while (true) {
- const word = it.next() ?? break;
+ const word = it.next() orelse break;
if (mem.eql(u8, word, "-isystem")) {
- const include_path = it.next() ?? {
+ const include_path = it.next() orelse {
warn("Expected argument after -isystem in NIX_CFLAGS_COMPILE\n");
break;
};
@@ -330,9 +330,9 @@ pub const Builder = struct {
if (os.getEnvVarOwned(self.allocator, "NIX_LDFLAGS")) |nix_ldflags| {
var it = mem.split(nix_ldflags, " ");
while (true) {
- const word = it.next() ?? break;
+ const word = it.next() orelse break;
if (mem.eql(u8, word, "-rpath")) {
- const rpath = it.next() ?? {
+ const rpath = it.next() orelse {
warn("Expected argument after -rpath in NIX_LDFLAGS\n");
break;
};
@@ -362,7 +362,7 @@ pub const Builder = struct {
}
self.available_options_list.append(available_option) catch unreachable;
- const entry = self.user_input_options.get(name) ?? return null;
+ const entry = self.user_input_options.get(name) orelse return null;
entry.value.used = true;
switch (type_id) {
TypeId.Bool => switch (entry.value.value) {
@@ -416,9 +416,9 @@ pub const Builder = struct {
pub fn standardReleaseOptions(self: *Builder) builtin.Mode {
if (self.release_mode) |mode| return mode;
- const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false;
- const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false;
- const release_small = self.option(bool, "release-small", "size optimizations on and safety off") ?? false;
+ const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") orelse false;
+ const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") orelse false;
+ const release_small = self.option(bool, "release-small", "size optimizations on and safety off") orelse false;
const mode = if (release_safe and !release_fast and !release_small) builtin.Mode.ReleaseSafe else if (release_fast and !release_safe and !release_small) builtin.Mode.ReleaseFast else if (release_small and !release_fast and !release_safe) builtin.Mode.ReleaseSmall else if (!release_fast and !release_safe and !release_small) builtin.Mode.Debug else x: {
warn("Multiple release modes (of -Drelease-safe, -Drelease-fast and -Drelease-small)");
@@ -518,7 +518,7 @@ pub const Builder = struct {
// make sure all args are used
var it = self.user_input_options.iterator();
while (true) {
- const entry = it.next() ?? break;
+ const entry = it.next() orelse break;
if (!entry.value.used) {
warn("Invalid option: -D{}\n\n", entry.key);
self.markInvalidUserInput();
@@ -1246,7 +1246,7 @@ pub const LibExeObjStep = struct {
{
var it = self.link_libs.iterator();
while (true) {
- const entry = it.next() ?? break;
+ const entry = it.next() orelse break;
zig_args.append("--library") catch unreachable;
zig_args.append(entry.key) catch unreachable;
}
@@ -1696,7 +1696,7 @@ pub const TestStep = struct {
{
var it = self.link_libs.iterator();
while (true) {
- const entry = it.next() ?? break;
+ const entry = it.next() orelse break;
try zig_args.append("--library");
try zig_args.append(entry.key);
}
diff --git a/std/debug/index.zig b/std/debug/index.zig
index be47ab76bc..25f7a58b25 100644
--- a/std/debug/index.zig
+++ b/std/debug/index.zig
@@ -208,7 +208,7 @@ fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: us
.name = "???",
.address = address,
};
- const symbol = debug_info.symbol_table.search(address) ?? &unknown;
+ const symbol = debug_info.symbol_table.search(address) orelse &unknown;
try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address);
},
else => {
@@ -268,10 +268,10 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
try st.elf.openFile(allocator, &st.self_exe_file);
errdefer st.elf.close();
- st.debug_info = (try st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
- st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
- st.debug_str = (try st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo;
- st.debug_line = (try st.elf.findSection(".debug_line")) ?? return error.MissingDebugInfo;
+ st.debug_info = (try st.elf.findSection(".debug_info")) orelse return error.MissingDebugInfo;
+ st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) orelse return error.MissingDebugInfo;
+ st.debug_str = (try st.elf.findSection(".debug_str")) orelse return error.MissingDebugInfo;
+ st.debug_line = (try st.elf.findSection(".debug_line")) orelse return error.MissingDebugInfo;
st.debug_ranges = (try st.elf.findSection(".debug_ranges"));
try scanAllCompileUnits(st);
return st;
@@ -443,7 +443,7 @@ const Die = struct {
}
fn getAttrAddr(self: *const Die, id: u64) !u64 {
- const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
+ const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.Address => |value| value,
else => error.InvalidDebugInfo,
@@ -451,7 +451,7 @@ const Die = struct {
}
fn getAttrSecOffset(self: *const Die, id: u64) !u64 {
- const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
+ const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.Const => |value| value.asUnsignedLe(),
FormValue.SecOffset => |value| value,
@@ -460,7 +460,7 @@ const Die = struct {
}
fn getAttrUnsignedLe(self: *const Die, id: u64) !u64 {
- const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
+ const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.Const => |value| value.asUnsignedLe(),
else => error.InvalidDebugInfo,
@@ -468,7 +468,7 @@ const Die = struct {
}
fn getAttrString(self: *const Die, st: *ElfStackTrace, id: u64) ![]u8 {
- const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
+ const form_value = self.getAttr(id) orelse return error.MissingDebugInfo;
return switch (form_value.*) {
FormValue.String => |value| value,
FormValue.StrPtr => |offset| getString(st, offset),
@@ -748,7 +748,7 @@ fn parseDie(st: *ElfStackTrace, abbrev_table: *const AbbrevTable, is_64: bool) !
var in_file_stream = io.FileInStream.init(in_file);
const in_stream = &in_file_stream.stream;
const abbrev_code = try readULeb128(in_stream);
- const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo;
+ const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) orelse return error.InvalidDebugInfo;
var result = Die{
.tag_id = table_entry.tag_id,
diff --git a/std/heap.zig b/std/heap.zig
index d1fbf9ca0a..172bc24118 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -97,12 +97,12 @@ pub const DirectAllocator = struct {
},
Os.windows => {
const amt = n + alignment + @sizeOf(usize);
- const heap_handle = self.heap_handle ?? blk: {
- const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) ?? return error.OutOfMemory;
+ const heap_handle = self.heap_handle orelse blk: {
+ const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) orelse return error.OutOfMemory;
self.heap_handle = hh;
break :blk hh;
};
- const ptr = os.windows.HeapAlloc(heap_handle, 0, amt) ?? return error.OutOfMemory;
+ const ptr = os.windows.HeapAlloc(heap_handle, 0, amt) orelse return error.OutOfMemory;
const root_addr = @ptrToInt(ptr);
const rem = @rem(root_addr, alignment);
const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
@@ -142,7 +142,7 @@ pub const DirectAllocator = struct {
const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;
const old_ptr = @intToPtr(*c_void, root_addr);
const amt = new_size + alignment + @sizeOf(usize);
- const new_ptr = os.windows.HeapReAlloc(self.heap_handle.?, 0, old_ptr, amt) ?? blk: {
+ const new_ptr = os.windows.HeapReAlloc(self.heap_handle.?, 0, old_ptr, amt) orelse blk: {
if (new_size > old_mem.len) return error.OutOfMemory;
const new_record_addr = old_record_addr - new_size + old_mem.len;
@intToPtr(*align(1) usize, new_record_addr).* = root_addr;
@@ -343,7 +343,7 @@ pub const ThreadSafeFixedBufferAllocator = struct {
if (new_end_index > self.buffer.len) {
return error.OutOfMemory;
}
- end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) ?? return self.buffer[adjusted_index..new_end_index];
+ end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) orelse return self.buffer[adjusted_index..new_end_index];
}
}
diff --git a/std/linked_list.zig b/std/linked_list.zig
index 536c6d24d0..9e32b7d9da 100644
--- a/std/linked_list.zig
+++ b/std/linked_list.zig
@@ -169,7 +169,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// Returns:
/// A pointer to the last node in the list.
pub fn pop(list: *Self) ?*Node {
- const last = list.last ?? return null;
+ const last = list.last orelse return null;
list.remove(last);
return last;
}
@@ -179,7 +179,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// Returns:
/// A pointer to the first node in the list.
pub fn popFirst(list: *Self) ?*Node {
- const first = list.first ?? return null;
+ const first = list.first orelse return null;
list.remove(first);
return first;
}
diff --git a/std/os/index.zig b/std/os/index.zig
index 807b2c398b..6a13ff94d4 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -425,7 +425,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: *const BufMap, allocator:
return posixExecveErrnoToErr(posix.getErrno(posix.execve(argv_buf[0].?, argv_buf.ptr, envp_buf.ptr)));
}
- const PATH = getEnvPosix("PATH") ?? "/usr/local/bin:/bin/:/usr/bin";
+ const PATH = getEnvPosix("PATH") orelse "/usr/local/bin:/bin/:/usr/bin";
// PATH.len because it is >= the largest search_path
// +1 for the / to join the search path and exe_path
// +1 for the null terminating byte
@@ -490,7 +490,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
errdefer result.deinit();
if (is_windows) {
- const ptr = windows.GetEnvironmentStringsA() ?? return error.OutOfMemory;
+ const ptr = windows.GetEnvironmentStringsA() orelse return error.OutOfMemory;
defer assert(windows.FreeEnvironmentStringsA(ptr) != 0);
var i: usize = 0;
@@ -573,7 +573,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) ![]u8 {
return allocator.shrink(u8, buf, result);
}
} else {
- const result = getEnvPosix(key) ?? return error.EnvironmentVariableNotFound;
+ const result = getEnvPosix(key) orelse return error.EnvironmentVariableNotFound;
return mem.dupe(allocator, u8, result);
}
}
@@ -1641,7 +1641,7 @@ pub const ArgIterator = struct {
if (builtin.os == Os.windows) {
return self.inner.next(allocator);
} else {
- return mem.dupe(allocator, u8, self.inner.next() ?? return null);
+ return mem.dupe(allocator, u8, self.inner.next() orelse return null);
}
}
@@ -2457,9 +2457,9 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
}
};
- const heap_handle = windows.GetProcessHeap() ?? return SpawnThreadError.OutOfMemory;
+ const heap_handle = windows.GetProcessHeap() orelse return SpawnThreadError.OutOfMemory;
const byte_count = @alignOf(WinThread.OuterContext) + @sizeOf(WinThread.OuterContext);
- const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) ?? return SpawnThreadError.OutOfMemory;
+ const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) orelse return SpawnThreadError.OutOfMemory;
errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count];
const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable;
@@ -2468,7 +2468,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
outer_context.thread.data.alloc_start = bytes_ptr;
const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(*c_void, &outer_context.inner);
- outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) ?? {
+ outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) orelse {
const err = windows.GetLastError();
return switch (err) {
else => os.unexpectedErrorWindows(err),
diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig
index 1414b8185b..cbd0cd1df5 100644
--- a/std/os/linux/vdso.zig
+++ b/std/os/linux/vdso.zig
@@ -28,7 +28,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
}
}
}
- const dynv = maybe_dynv ?? return 0;
+ const dynv = maybe_dynv orelse return 0;
if (base == @maxValue(usize)) return 0;
var maybe_strings: ?[*]u8 = null;
@@ -52,9 +52,9 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
}
}
- const strings = maybe_strings ?? return 0;
- const syms = maybe_syms ?? return 0;
- const hashtab = maybe_hashtab ?? return 0;
+ const strings = maybe_strings orelse return 0;
+ const syms = maybe_syms orelse return 0;
+ const hashtab = maybe_hashtab orelse return 0;
if (maybe_verdef == null) maybe_versym = null;
const OK_TYPES = (1 << elf.STT_NOTYPE | 1 << elf.STT_OBJECT | 1 << elf.STT_FUNC | 1 << elf.STT_COMMON);
diff --git a/std/os/path.zig b/std/os/path.zig
index 430dda2934..a3ad23b1a9 100644
--- a/std/os/path.zig
+++ b/std/os/path.zig
@@ -182,8 +182,8 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
}
var it = mem.split(path, []u8{this_sep});
- _ = (it.next() ?? return relative_path);
- _ = (it.next() ?? return relative_path);
+ _ = (it.next() orelse return relative_path);
+ _ = (it.next() orelse return relative_path);
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.NetworkShare,
@@ -200,8 +200,8 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
}
var it = mem.split(path, []u8{this_sep});
- _ = (it.next() ?? return relative_path);
- _ = (it.next() ?? return relative_path);
+ _ = (it.next() orelse return relative_path);
+ _ = (it.next() orelse return relative_path);
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.NetworkShare,
@@ -923,7 +923,7 @@ pub fn relativeWindows(allocator: *Allocator, from: []const u8, to: []const u8)
var from_it = mem.split(resolved_from, "/\\");
var to_it = mem.split(resolved_to, "/\\");
while (true) {
- const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
+ const from_component = from_it.next() orelse return mem.dupe(allocator, u8, to_it.rest());
const to_rest = to_it.rest();
if (to_it.next()) |to_component| {
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
@@ -974,7 +974,7 @@ pub fn relativePosix(allocator: *Allocator, from: []const u8, to: []const u8) ![
var from_it = mem.split(resolved_from, "/");
var to_it = mem.split(resolved_to, "/");
while (true) {
- const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
+ const from_component = from_it.next() orelse return mem.dupe(allocator, u8, to_it.rest());
const to_rest = to_it.rest();
if (to_it.next()) |to_component| {
if (mem.eql(u8, from_component, to_component))
diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig
index 7170346108..f93a673be0 100644
--- a/std/os/windows/util.zig
+++ b/std/os/windows/util.zig
@@ -153,7 +153,7 @@ pub fn createWindowsEnvBlock(allocator: *mem.Allocator, env_map: *const BufMap)
pub fn windowsLoadDll(allocator: *mem.Allocator, dll_path: []const u8) !windows.HMODULE {
const padded_buff = try cstr.addNullByte(allocator, dll_path);
defer allocator.free(padded_buff);
- return windows.LoadLibraryA(padded_buff.ptr) ?? error.DllNotFound;
+ return windows.LoadLibraryA(padded_buff.ptr) orelse error.DllNotFound;
}
pub fn windowsUnloadDll(hModule: windows.HMODULE) void {
diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig
index 3471d6ed21..e4f04df6d0 100644
--- a/std/special/build_runner.zig
+++ b/std/special/build_runner.zig
@@ -27,15 +27,15 @@ pub fn main() !void {
// skip my own exe name
_ = arg_it.skip();
- const zig_exe = try unwrapArg(arg_it.next(allocator) ?? {
+ const zig_exe = try unwrapArg(arg_it.next(allocator) orelse {
warn("Expected first argument to be path to zig compiler\n");
return error.InvalidArgs;
});
- const build_root = try unwrapArg(arg_it.next(allocator) ?? {
+ const build_root = try unwrapArg(arg_it.next(allocator) orelse {
warn("Expected second argument to be build root directory path\n");
return error.InvalidArgs;
});
- const cache_root = try unwrapArg(arg_it.next(allocator) ?? {
+ const cache_root = try unwrapArg(arg_it.next(allocator) orelse {
warn("Expected third argument to be cache root directory path\n");
return error.InvalidArgs;
});
@@ -84,12 +84,12 @@ pub fn main() !void {
} else if (mem.eql(u8, arg, "--help")) {
return usage(&builder, false, try stdout_stream);
} else if (mem.eql(u8, arg, "--prefix")) {
- prefix = try unwrapArg(arg_it.next(allocator) ?? {
+ prefix = try unwrapArg(arg_it.next(allocator) orelse {
warn("Expected argument after --prefix\n\n");
return usageAndErr(&builder, false, try stderr_stream);
});
} else if (mem.eql(u8, arg, "--search-prefix")) {
- const search_prefix = try unwrapArg(arg_it.next(allocator) ?? {
+ const search_prefix = try unwrapArg(arg_it.next(allocator) orelse {
warn("Expected argument after --search-prefix\n\n");
return usageAndErr(&builder, false, try stderr_stream);
});
diff --git a/std/unicode.zig b/std/unicode.zig
index 21ae12f59c..ec808ca4fe 100644
--- a/std/unicode.zig
+++ b/std/unicode.zig
@@ -220,7 +220,7 @@ const Utf8Iterator = struct {
}
pub fn nextCodepoint(it: *Utf8Iterator) ?u32 {
- const slice = it.nextCodepointSlice() ?? return null;
+ const slice = it.nextCodepointSlice() orelse return null;
switch (slice.len) {
1 => return u32(slice[0]),
diff --git a/std/zig/parse.zig b/std/zig/parse.zig
index 9f8ef3c3d6..5752f69409 100644
--- a/std/zig/parse.zig
+++ b/std/zig/parse.zig
@@ -43,7 +43,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
// skip over line comments at the top of the file
while (true) {
- const next_tok = tok_it.peek() ?? break;
+ const next_tok = tok_it.peek() orelse break;
if (next_tok.id != Token.Id.LineComment) break;
_ = tok_it.next();
}
@@ -197,7 +197,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lib_name_token = nextToken(&tok_it, &tree);
const lib_name_token_index = lib_name_token.index;
const lib_name_token_ptr = lib_name_token.ptr;
- break :blk (try parseStringLiteral(arena, &tok_it, lib_name_token_ptr, lib_name_token_index, &tree)) ?? {
+ break :blk (try parseStringLiteral(arena, &tok_it, lib_name_token_ptr, lib_name_token_index, &tree)) orelse {
prevToken(&tok_it, &tree);
break :blk null;
};
@@ -1434,13 +1434,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
try stack.append(State{
.ExpectTokenSave = ExpectTokenSave{
.id = Token.Id.AngleBracketRight,
- .ptr = &async_node.rangle_bracket.? },
+ .ptr = &async_node.rangle_bracket.?,
+ },
});
try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &async_node.allocator_type } });
continue;
},
State.AsyncEnd => |ctx| {
- const node = ctx.ctx.get() ?? continue;
+ const node = ctx.ctx.get() orelse continue;
switch (node.id) {
ast.Node.Id.FnProto => {
@@ -1813,7 +1814,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
State.RangeExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -1835,7 +1836,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.AssignmentExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -1865,7 +1866,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.UnwrapExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -1900,7 +1901,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.BoolOrExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -1924,7 +1925,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.BoolAndExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -1948,7 +1949,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.ComparisonExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -1978,7 +1979,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.BinaryOrExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -2002,7 +2003,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.BinaryXorExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -2026,7 +2027,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.BinaryAndExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -2050,7 +2051,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.BitShiftExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -2080,7 +2081,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.AdditionExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -2110,7 +2111,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.MultiplyExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -2141,7 +2142,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.CurlySuffixExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (tok_it.peek().?.id == Token.Id.Period) {
const node = try arena.construct(ast.Node.SuffixOp{
@@ -2189,7 +2190,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.TypeExprEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| {
const node = try arena.construct(ast.Node.InfixOp{
@@ -2269,7 +2270,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.SuffixOpExpressionEnd => |opt_ctx| {
- const lhs = opt_ctx.get() ?? continue;
+ const lhs = opt_ctx.get() orelse continue;
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
@@ -2418,7 +2419,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.StringLiteral, Token.Id.MultilineStringLiteralLine => {
- opt_ctx.store((try parseStringLiteral(arena, &tok_it, token.ptr, token.index, &tree)) ?? unreachable);
+ opt_ctx.store((try parseStringLiteral(arena, &tok_it, token.ptr, token.index, &tree)) orelse unreachable);
continue;
},
Token.Id.LParen => {
@@ -2648,7 +2649,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
const token_ptr = token.ptr;
- opt_ctx.store((try parseStringLiteral(arena, &tok_it, token_ptr, token_index, &tree)) ?? {
+ opt_ctx.store((try parseStringLiteral(arena, &tok_it, token_ptr, token_index, &tree)) orelse {
prevToken(&tok_it, &tree);
if (opt_ctx != OptionalCtx.Optional) {
((try tree.errors.addOne())).* = Error{ .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr{ .token = token_index } };
@@ -3348,7 +3349,7 @@ fn nextToken(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree) AnnotatedTok
assert(result.ptr.id != Token.Id.LineComment);
while (true) {
- const next_tok = tok_it.peek() ?? return result;
+ const next_tok = tok_it.peek() orelse return result;
if (next_tok.id != Token.Id.LineComment) return result;
_ = tok_it.next();
}
@@ -3356,7 +3357,7 @@ fn nextToken(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree) AnnotatedTok
fn prevToken(tok_it: *ast.Tree.TokenList.Iterator, tree: *ast.Tree) void {
while (true) {
- const prev_tok = tok_it.prev() ?? return;
+ const prev_tok = tok_it.prev() orelse return;
if (prev_tok.id == Token.Id.LineComment) continue;
return;
}
diff --git a/std/zig/render.zig b/std/zig/render.zig
index 0b8e4d1453..bc45768fa3 100644
--- a/std/zig/render.zig
+++ b/std/zig/render.zig
@@ -83,7 +83,7 @@ fn renderRoot(
var start_col: usize = 0;
var it = tree.root_node.decls.iterator(0);
while (true) {
- var decl = (it.next() ?? return).*;
+ var decl = (it.next() orelse return).*;
// look for zig fmt: off comment
var start_token_index = decl.firstToken();
zig_fmt_loop: while (start_token_index != 0) {
@@ -112,7 +112,7 @@ fn renderRoot(
const start = tree.tokens.at(start_token_index + 1).start;
try stream.print("{}\n", tree.source[start..end_token.end]);
while (tree.tokens.at(decl.firstToken()).start < end_token.end) {
- decl = (it.next() ?? return).*;
+ decl = (it.next() orelse return).*;
}
break :zig_fmt_loop;
}
@@ -1993,7 +1993,7 @@ fn renderDocComments(
indent: usize,
start_col: *usize,
) (@typeOf(stream).Child.Error || Error)!void {
- const comment = node.doc_comments ?? return;
+ const comment = node.doc_comments orelse return;
var it = comment.lines.iterator(0);
const first_token = node.firstToken();
while (it.next()) |line_token_index| {
@@ -2021,7 +2021,7 @@ fn nodeIsBlock(base: *const ast.Node) bool {
}
fn nodeCausesSliceOpSpace(base: *ast.Node) bool {
- const infix_op = base.cast(ast.Node.InfixOp) ?? return false;
+ const infix_op = base.cast(ast.Node.InfixOp) orelse return false;
return switch (infix_op.op) {
ast.Node.InfixOp.Op.Period => false,
else => true,
diff --git a/test/cases/cast.zig b/test/cases/cast.zig
index a56c470408..ade1cf78aa 100644
--- a/test/cases/cast.zig
+++ b/test/cases/cast.zig
@@ -73,7 +73,7 @@ fn Struct(comptime T: type) type {
fn maybePointer(self: ?*const Self) Self {
const none = Self{ .x = if (T == void) void{} else 0 };
- return (self ?? &none).*;
+ return (self orelse &none).*;
}
};
}
@@ -87,7 +87,7 @@ const Union = union {
fn maybePointer(self: ?*const Union) Union {
const none = Union{ .x = 0 };
- return (self ?? &none).*;
+ return (self orelse &none).*;
}
};
@@ -100,7 +100,7 @@ const Enum = enum {
}
fn maybePointer(self: ?*const Enum) Enum {
- return (self ?? &Enum.None).*;
+ return (self orelse &Enum.None).*;
}
};
diff --git a/test/cases/null.zig b/test/cases/null.zig
index 62565784ac..cdcfd23efb 100644
--- a/test/cases/null.zig
+++ b/test/cases/null.zig
@@ -15,13 +15,13 @@ test "optional type" {
const next_x: ?i32 = null;
- const z = next_x ?? 1234;
+ const z = next_x orelse 1234;
assert(z == 1234);
const final_x: ?i32 = 13;
- const num = final_x ?? unreachable;
+ const num = final_x orelse unreachable;
assert(num == 13);
}
@@ -38,7 +38,7 @@ test "test maybe object and get a pointer to the inner value" {
test "rhs maybe unwrap return" {
const x: ?bool = true;
- const y = x ?? return;
+ const y = x orelse return;
}
test "maybe return" {
@@ -53,7 +53,7 @@ fn maybeReturnImpl() void {
}
fn foo(x: ?i32) ?bool {
- const value = x ?? return null;
+ const value = x orelse return null;
return value > 1234;
}
@@ -140,6 +140,6 @@ test "unwrap optional which is field of global var" {
}
test "null with default unwrap" {
- const x: i32 = null ?? 1;
+ const x: i32 = null orelse 1;
assert(x == 1);
}
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 1c737a59e7..5ec2759032 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2296,7 +2296,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\
\\ defer try canFail();
\\
- \\ const a = maybeInt() ?? return;
+ \\ const a = maybeInt() orelse return;
\\}
\\
\\fn canFail() error!void { }
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 3489f9da21..417171d2c2 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -246,13 +246,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub extern var fn_ptr: ?extern fn() void;
,
\\pub inline fn foo() void {
- \\ return (??fn_ptr)();
+ \\ return fn_ptr.?();
\\}
,
\\pub extern var fn_ptr2: ?extern fn(c_int, f32) u8;
,
\\pub inline fn bar(arg0: c_int, arg1: f32) u8 {
- \\ return (??fn_ptr2)(arg0, arg1);
+ \\ return fn_ptr2.?(arg0, arg1);
\\}
);
@@ -608,7 +608,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ field: c_int,
\\};
\\pub export fn read_field(foo: ?[*]struct_Foo) c_int {
- \\ return (??foo).field;
+ \\ return foo.?.field;
\\}
);
@@ -969,11 +969,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub export fn bar() void {
\\ var f: ?extern fn() void = foo;
\\ var b: ?extern fn() c_int = baz;
- \\ (??f)();
- \\ (??f)();
+ \\ f.?();
+ \\ f.?();
\\ foo();
- \\ _ = (??b)();
- \\ _ = (??b)();
+ \\ _ = b.?();
+ \\ _ = b.?();
\\ _ = baz();
\\}
);
@@ -984,7 +984,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
,
\\pub export fn foo(x: ?[*]c_int) void {
- \\ (??x).* = 1;
+ \\ x.?.* = 1;
\\}
);
@@ -1012,7 +1012,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub fn foo() c_int {
\\ var x: c_int = 1234;
\\ var ptr: ?[*]c_int = &x;
- \\ return (??ptr).*;
+ \\ return ptr.?.*;
\\}
);
@@ -1119,7 +1119,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const glClearPFN = PFNGLCLEARPROC;
,
\\pub inline fn glClearUnion(arg0: GLbitfield) void {
- \\ return (??glProcs.gl.Clear)(arg0);
+ \\ return glProcs.gl.Clear.?(arg0);
\\}
,
\\pub const OpenGLProcs = union_OpenGLProcs;
--
cgit v1.2.3
From fdd9cf09287b5e397ad9b2c960c833a7de075e4c Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Tue, 12 Jun 2018 15:14:32 -0400
Subject: better debugging for CI failures of std.atomic
---
std/atomic/queue.zig | 14 ++++++++++++--
std/atomic/stack.zig | 14 ++++++++++++--
2 files changed, 24 insertions(+), 4 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 4f856d9e01..2a48407383 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -94,8 +94,18 @@ test "std.atomic.queue" {
for (getters) |t|
t.wait();
- std.debug.assert(context.put_sum == context.get_sum);
- std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
+ if (context.put_sum != context.get_sum) {
+ std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
+ }
+
+ if (context.get_count != puts_per_thread * put_thread_count) {
+ std.debug.panic(
+ "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
+ context.get_count,
+ u32(puts_per_thread),
+ u32(put_thread_count),
+ );
+ }
}
fn startPuts(ctx: *Context) u8 {
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 77fa1a9100..c6b368b990 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -97,8 +97,18 @@ test "std.atomic.stack" {
for (getters) |t|
t.wait();
- std.debug.assert(context.put_sum == context.get_sum);
- std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
+ if (context.put_sum != context.get_sum) {
+ std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
+ }
+
+ if (context.get_count != puts_per_thread * put_thread_count) {
+ std.debug.panic(
+ "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
+ context.get_count,
+ u32(puts_per_thread),
+ u32(put_thread_count),
+ );
+ }
}
fn startPuts(ctx: *Context) u8 {
--
cgit v1.2.3
From fc87f6e417d206a88b581b77d3a5494ae4c978dd Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 13 Jun 2018 11:57:57 -0400
Subject: fix race condition bug in test harness of std.atomic
---
std/atomic/queue.zig | 7 +++----
std/atomic/stack.zig | 7 +++----
2 files changed, 6 insertions(+), 8 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 2a48407383..3dc64dbea2 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -124,15 +124,14 @@ fn startPuts(ctx: *Context) u8 {
fn startGets(ctx: *Context) u8 {
while (true) {
+ const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
+
while (ctx.queue.get()) |node| {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
}
- if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
- break;
- }
+ if (last) return 0;
}
- return 0;
}
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index c6b368b990..9e81d89257 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -127,15 +127,14 @@ fn startPuts(ctx: *Context) u8 {
fn startGets(ctx: *Context) u8 {
while (true) {
+ const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
+
while (ctx.stack.pop()) |node| {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
}
- if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
- break;
- }
+ if (last) return 0;
}
- return 0;
}
--
cgit v1.2.3
From 71db8df5480f4d849480267574cd5491066e3868 Mon Sep 17 00:00:00 2001
From: kristopher tate
Date: Thu, 21 Jun 2018 00:40:21 +0900
Subject: std: update stdlib to match updated allocator create signature; ref
#733
---
src-self-hosted/module.zig | 24 +++++++-------
std/atomic/queue.zig | 6 ++--
std/atomic/stack.zig | 6 ++--
std/build.zig | 55 +++++++++++--------------------
std/debug/index.zig | 17 ++++------
std/heap.zig | 2 +-
std/io.zig | 8 ++---
std/linked_list.zig | 6 +++-
std/os/child_process.zig | 9 ++---
std/os/index.zig | 7 ++--
test/tests.zig | 82 +++++++++++++++++++++-------------------------
11 files changed, 100 insertions(+), 122 deletions(-)
(limited to 'std/atomic')
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index 575105f25f..997ef5eed2 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -110,11 +110,12 @@ pub const Module = struct {
parent: ?*CliPkg,
pub fn init(allocator: *mem.Allocator, name: []const u8, path: []const u8, parent: ?*CliPkg) !*CliPkg {
- var pkg = try allocator.create(CliPkg);
- pkg.name = name;
- pkg.path = path;
- pkg.children = ArrayList(*CliPkg).init(allocator);
- pkg.parent = parent;
+ var pkg = try allocator.create(CliPkg{
+ .name = name,
+ .path = path,
+ .children = ArrayList(*CliPkg).init(allocator),
+ .parent = parent
+ });
return pkg;
}
@@ -139,10 +140,7 @@ pub const Module = struct {
const builder = c.LLVMCreateBuilderInContext(context) orelse return error.OutOfMemory;
errdefer c.LLVMDisposeBuilder(builder);
- const module_ptr = try allocator.create(Module);
- errdefer allocator.destroy(module_ptr);
-
- module_ptr.* = Module{
+ const module_ptr = try allocator.create(Module{
.allocator = allocator,
.name = name_buffer,
.root_src_path = root_src_path,
@@ -196,7 +194,8 @@ pub const Module = struct {
.test_filters = [][]const u8{},
.test_name_prefix = null,
.emit_file_type = Emit.Binary,
- };
+ });
+ errdefer allocator.destroy(module_ptr);
return module_ptr;
}
@@ -279,13 +278,12 @@ pub const Module = struct {
}
}
- const link_lib = try self.allocator.create(LinkLib);
- link_lib.* = LinkLib{
+ const link_lib = try self.allocator.create(LinkLib{
.name = name,
.path = null,
.provided_explicitly = provided_explicitly,
.symbols = ArrayList([]u8).init(self.allocator),
- };
+ });
try self.link_libs_list.append(link_lib);
if (is_libc) {
self.libc_link_lib = link_lib;
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 3dc64dbea2..5b810f95ac 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -114,8 +114,10 @@ fn startPuts(ctx: *Context) u8 {
while (put_count != 0) : (put_count -= 1) {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
- const node = ctx.allocator.create(Queue(i32).Node) catch unreachable;
- node.data = x;
+ const node = ctx.allocator.create(Queue(i32).Node{
+ .next = null,
+ .data = x
+ }) catch unreachable;
ctx.queue.put(node);
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
}
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 9e81d89257..2272be4a92 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -117,8 +117,10 @@ fn startPuts(ctx: *Context) u8 {
while (put_count != 0) : (put_count -= 1) {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
- const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
- node.data = x;
+ const node = ctx.allocator.create(Stack(i32).Node{
+ .next = null,
+ .data = x
+ }) catch unreachable;
ctx.stack.push(node);
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
}
diff --git a/std/build.zig b/std/build.zig
index 92454a183a..99de9b5197 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -158,8 +158,7 @@ pub const Builder = struct {
}
pub fn addTest(self: *Builder, root_src: []const u8) *TestStep {
- const test_step = self.allocator.create(TestStep) catch unreachable;
- test_step.* = TestStep.init(self, root_src);
+ const test_step = self.allocator.create(TestStep.init(self, root_src)) catch unreachable;
return test_step;
}
@@ -191,21 +190,18 @@ pub const Builder = struct {
}
pub fn addWriteFile(self: *Builder, file_path: []const u8, data: []const u8) *WriteFileStep {
- const write_file_step = self.allocator.create(WriteFileStep) catch unreachable;
- write_file_step.* = WriteFileStep.init(self, file_path, data);
+ const write_file_step = self.allocator.create(WriteFileStep.init(self, file_path, data)) catch unreachable;
return write_file_step;
}
pub fn addLog(self: *Builder, comptime format: []const u8, args: ...) *LogStep {
const data = self.fmt(format, args);
- const log_step = self.allocator.create(LogStep) catch unreachable;
- log_step.* = LogStep.init(self, data);
+ const log_step = self.allocator.create(LogStep.init(self, data)) catch unreachable;
return log_step;
}
pub fn addRemoveDirTree(self: *Builder, dir_path: []const u8) *RemoveDirStep {
- const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable;
- remove_dir_step.* = RemoveDirStep.init(self, dir_path);
+ const remove_dir_step = self.allocator.create(RemoveDirStep.init(self, dir_path)) catch unreachable;
return remove_dir_step;
}
@@ -404,11 +400,10 @@ pub const Builder = struct {
}
pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {
- const step_info = self.allocator.create(TopLevelStep) catch unreachable;
- step_info.* = TopLevelStep{
+ const step_info = self.allocator.create(TopLevelStep{
.step = Step.initNoOp(name, self.allocator),
.description = description,
- };
+ }) catch unreachable;
self.top_level_steps.append(step_info) catch unreachable;
return &step_info.step;
}
@@ -598,8 +593,7 @@ pub const Builder = struct {
const full_dest_path = os.path.resolve(self.allocator, self.prefix, dest_rel_path) catch unreachable;
self.pushInstalledFile(full_dest_path);
- const install_step = self.allocator.create(InstallFileStep) catch unreachable;
- install_step.* = InstallFileStep.init(self, src_path, full_dest_path);
+ const install_step = self.allocator.create(InstallFileStep.init(self, src_path, full_dest_path)) catch unreachable;
return install_step;
}
@@ -837,51 +831,43 @@ pub const LibExeObjStep = struct {
};
pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: *const Version) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
+ const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, false, ver)) catch unreachable;
return self;
}
pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: *const Version) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initC(builder, name, Kind.Lib, version, false);
+ const self = builder.allocator.create(initC(builder, name, Kind.Lib, version, false)) catch unreachable;
return self;
}
pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
+ const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0))) catch unreachable;
return self;
}
pub fn createCStaticLibrary(builder: *Builder, name: []const u8) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
+ const self = builder.allocator.create(initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true)) catch unreachable;
return self;
}
pub fn createObject(builder: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
+ const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0))) catch unreachable;
return self;
}
pub fn createCObject(builder: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
+ const self = builder.allocator.create(initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false)) catch unreachable;
self.object_src = src;
return self;
}
pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
+ const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0))) catch unreachable;
return self;
}
pub fn createCExecutable(builder: *Builder, name: []const u8) *LibExeObjStep {
- const self = builder.allocator.create(LibExeObjStep) catch unreachable;
- self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
+ const self = builder.allocator.create(initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false)) catch unreachable;
return self;
}
@@ -1748,14 +1734,14 @@ pub const CommandStep = struct {
/// ::argv is copied.
pub fn create(builder: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) *CommandStep {
- const self = builder.allocator.create(CommandStep) catch unreachable;
- self.* = CommandStep{
+ const self = builder.allocator.create(CommandStep{
.builder = builder,
.step = Step.init(argv[0], builder.allocator, make),
.argv = builder.allocator.alloc([]u8, argv.len) catch unreachable,
.cwd = cwd,
.env_map = env_map,
- };
+ }) catch unreachable;
+
mem.copy([]const u8, self.argv, argv);
self.step.name = self.argv[0];
return self;
@@ -1778,18 +1764,17 @@ const InstallArtifactStep = struct {
const Self = this;
pub fn create(builder: *Builder, artifact: *LibExeObjStep) *Self {
- const self = builder.allocator.create(Self) catch unreachable;
const dest_dir = switch (artifact.kind) {
LibExeObjStep.Kind.Obj => unreachable,
LibExeObjStep.Kind.Exe => builder.exe_dir,
LibExeObjStep.Kind.Lib => builder.lib_dir,
};
- self.* = Self{
+ const self = builder.allocator.create(Self{
.builder = builder,
.step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
.artifact = artifact,
.dest_file = os.path.join(builder.allocator, dest_dir, artifact.out_filename) catch unreachable,
- };
+ }) catch unreachable;
self.step.dependOn(&artifact.step);
builder.pushInstalledFile(self.dest_file);
if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
diff --git a/std/debug/index.zig b/std/debug/index.zig
index 198e0f90f6..19cee3c65d 100644
--- a/std/debug/index.zig
+++ b/std/debug/index.zig
@@ -249,9 +249,7 @@ fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: us
pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
switch (builtin.object_format) {
builtin.ObjectFormat.elf => {
- const st = try allocator.create(ElfStackTrace);
- errdefer allocator.destroy(st);
- st.* = ElfStackTrace{
+ const st = try allocator.create(ElfStackTrace{
.self_exe_file = undefined,
.elf = undefined,
.debug_info = undefined,
@@ -261,7 +259,8 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
.debug_ranges = null,
.abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator),
.compile_unit_list = ArrayList(CompileUnit).init(allocator),
- };
+ });
+ errdefer allocator.destroy(st);
st.self_exe_file = try os.openSelfExe();
errdefer st.self_exe_file.close();
@@ -280,11 +279,10 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
var exe_file = try os.openSelfExe();
defer exe_file.close();
- const st = try allocator.create(ElfStackTrace);
+ const st = try allocator.create(ElfStackTrace{
+ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file))
+ });
errdefer allocator.destroy(st);
-
- st.* = ElfStackTrace{ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)) };
-
return st;
},
builtin.ObjectFormat.coff => {
@@ -974,8 +972,7 @@ fn scanAllCompileUnits(st: *ElfStackTrace) !void {
try st.self_exe_file.seekTo(compile_unit_pos);
- const compile_unit_die = try st.allocator().create(Die);
- compile_unit_die.* = try parseDie(st, abbrev_table, is_64);
+ const compile_unit_die = try st.allocator().create( try parseDie(st, abbrev_table, is_64) );
if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo;
diff --git a/std/heap.zig b/std/heap.zig
index c948818e3d..7fc00cd0a4 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -407,7 +407,7 @@ fn testAllocator(allocator: *mem.Allocator) !void {
var slice = try allocator.alloc(*i32, 100);
for (slice) |*item, i| {
- item.* = try allocator.create(i32);
+ item.* = try allocator.create(i32(0));
item.*.* = @intCast(i32, i);
}
diff --git a/std/io.zig b/std/io.zig
index cfe1a7f585..1c468f6f4f 100644
--- a/std/io.zig
+++ b/std/io.zig
@@ -414,14 +414,12 @@ pub const BufferedAtomicFile = struct {
pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile {
// TODO with well defined copy elision we don't need this allocation
- var self = try allocator.create(BufferedAtomicFile);
- errdefer allocator.destroy(self);
-
- self.* = BufferedAtomicFile{
+ var self = try allocator.create(BufferedAtomicFile{
.atomic_file = undefined,
.file_stream = undefined,
.buffered_stream = undefined,
- };
+ });
+ errdefer allocator.destroy(self);
self.atomic_file = try os.AtomicFile.init(allocator, dest_path, os.default_file_mode);
errdefer self.atomic_file.deinit();
diff --git a/std/linked_list.zig b/std/linked_list.zig
index 9e32b7d9da..f4f7aab752 100644
--- a/std/linked_list.zig
+++ b/std/linked_list.zig
@@ -193,7 +193,11 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// A pointer to the new node.
pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node {
comptime assert(!isIntrusive());
- return allocator.create(Node);
+ return allocator.create(Node{
+ .prev = null,
+ .next = null,
+ .data = undefined
+ });
}
/// Deallocate a node.
diff --git a/std/os/child_process.zig b/std/os/child_process.zig
index da5e708555..693129eea8 100644
--- a/std/os/child_process.zig
+++ b/std/os/child_process.zig
@@ -85,10 +85,7 @@ pub const ChildProcess = struct {
/// First argument in argv is the executable.
/// On success must call deinit.
pub fn init(argv: []const []const u8, allocator: *mem.Allocator) !*ChildProcess {
- const child = try allocator.create(ChildProcess);
- errdefer allocator.destroy(child);
-
- child.* = ChildProcess{
+ const child = try allocator.create(ChildProcess{
.allocator = allocator,
.argv = argv,
.pid = undefined,
@@ -109,8 +106,8 @@ pub const ChildProcess = struct {
.stdin_behavior = StdIo.Inherit,
.stdout_behavior = StdIo.Inherit,
.stderr_behavior = StdIo.Inherit,
- };
-
+ });
+ errdefer allocator.destroy(child);
return child;
}
diff --git a/std/os/index.zig b/std/os/index.zig
index dd0d4e2ea1..7b69bd0b36 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -2582,8 +2582,11 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) orelse return SpawnThreadError.OutOfMemory;
errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count];
- const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable;
- outer_context.inner = context;
+ const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext{
+ .thread = undefined,
+ .inner = context
+ }) catch unreachable;
+
outer_context.thread.data.heap_handle = heap_handle;
outer_context.thread.data.alloc_start = bytes_ptr;
diff --git a/test/tests.zig b/test/tests.zig
index b66441f628..66eb2d93a0 100644
--- a/test/tests.zig
+++ b/test/tests.zig
@@ -48,13 +48,12 @@ const test_targets = []TestTarget{
const max_stdout_size = 1 * 1024 * 1024; // 1 MB
pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(CompareOutputContext) catch unreachable;
- cases.* = CompareOutputContext{
+ const cases = b.allocator.create(CompareOutputContext{
.b = b,
.step = b.step("test-compare-output", "Run the compare output tests"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
compare_output.addCases(cases);
@@ -62,13 +61,12 @@ pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8) *build
}
pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(CompareOutputContext) catch unreachable;
- cases.* = CompareOutputContext{
+ const cases = b.allocator.create(CompareOutputContext{
.b = b,
.step = b.step("test-runtime-safety", "Run the runtime safety tests"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
runtime_safety.addCases(cases);
@@ -76,13 +74,12 @@ pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8) *build
}
pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(CompileErrorContext) catch unreachable;
- cases.* = CompileErrorContext{
+ const cases = b.allocator.create(CompileErrorContext{
.b = b,
.step = b.step("test-compile-errors", "Run the compile error tests"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
compile_errors.addCases(cases);
@@ -90,13 +87,12 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8) *build.
}
pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(BuildExamplesContext) catch unreachable;
- cases.* = BuildExamplesContext{
+ const cases = b.allocator.create(BuildExamplesContext{
.b = b,
.step = b.step("test-build-examples", "Build the examples"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
build_examples.addCases(cases);
@@ -104,13 +100,12 @@ pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8) *build.
}
pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(CompareOutputContext) catch unreachable;
- cases.* = CompareOutputContext{
+ const cases = b.allocator.create(CompareOutputContext{
.b = b,
.step = b.step("test-asm-link", "Run the assemble and link tests"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
assemble_and_link.addCases(cases);
@@ -118,13 +113,12 @@ pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8) *bui
}
pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(TranslateCContext) catch unreachable;
- cases.* = TranslateCContext{
+ const cases = b.allocator.create(TranslateCContext{
.b = b,
.step = b.step("test-translate-c", "Run the C transation tests"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
translate_c.addCases(cases);
@@ -132,13 +126,12 @@ pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.St
}
pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
- const cases = b.allocator.create(GenHContext) catch unreachable;
- cases.* = GenHContext{
+ const cases = b.allocator.create(GenHContext{
.b = b,
.step = b.step("test-gen-h", "Run the C header file generation tests"),
.test_index = 0,
.test_filter = test_filter,
- };
+ }) catch unreachable;
gen_h.addCases(cases);
@@ -240,8 +233,7 @@ pub const CompareOutputContext = struct {
pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) *RunCompareOutputStep {
const allocator = context.b.allocator;
- const ptr = allocator.create(RunCompareOutputStep) catch unreachable;
- ptr.* = RunCompareOutputStep{
+ const ptr = allocator.create(RunCompareOutputStep{
.context = context,
.exe_path = exe_path,
.name = name,
@@ -249,7 +241,7 @@ pub const CompareOutputContext = struct {
.test_index = context.test_index,
.step = build.Step.init("RunCompareOutput", allocator, make),
.cli_args = cli_args,
- };
+ }) catch unreachable;
context.test_index += 1;
return ptr;
}
@@ -328,14 +320,14 @@ pub const CompareOutputContext = struct {
pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8) *RuntimeSafetyRunStep {
const allocator = context.b.allocator;
- const ptr = allocator.create(RuntimeSafetyRunStep) catch unreachable;
- ptr.* = RuntimeSafetyRunStep{
+ const ptr = allocator.create(RuntimeSafetyRunStep{
.context = context,
.exe_path = exe_path,
.name = name,
.test_index = context.test_index,
.step = build.Step.init("RuntimeSafetyRun", allocator, make),
- };
+ }) catch unreachable;
+
context.test_index += 1;
return ptr;
}
@@ -543,15 +535,15 @@ pub const CompileErrorContext = struct {
pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep {
const allocator = context.b.allocator;
- const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
- ptr.* = CompileCmpOutputStep{
+ const ptr = allocator.create(CompileCmpOutputStep{
.step = build.Step.init("CompileCmpOutput", allocator, make),
.context = context,
.name = name,
.test_index = context.test_index,
.case = case,
.build_mode = build_mode,
- };
+ }) catch unreachable;
+
context.test_index += 1;
return ptr;
}
@@ -662,14 +654,14 @@ pub const CompileErrorContext = struct {
}
pub fn create(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
- const tc = self.b.allocator.create(TestCase) catch unreachable;
- tc.* = TestCase{
+ const tc = self.b.allocator.create(TestCase{
.name = name,
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
.expected_errors = ArrayList([]const u8).init(self.b.allocator),
.link_libc = false,
.is_exe = false,
- };
+ }) catch unreachable;
+
tc.addSourceFile(".tmp_source.zig", source);
comptime var arg_i = 0;
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
@@ -829,14 +821,14 @@ pub const TranslateCContext = struct {
pub fn create(context: *TranslateCContext, name: []const u8, case: *const TestCase) *TranslateCCmpOutputStep {
const allocator = context.b.allocator;
- const ptr = allocator.create(TranslateCCmpOutputStep) catch unreachable;
- ptr.* = TranslateCCmpOutputStep{
+ const ptr = allocator.create(TranslateCCmpOutputStep{
.step = build.Step.init("ParseCCmpOutput", allocator, make),
.context = context,
.name = name,
.test_index = context.test_index,
.case = case,
- };
+ }) catch unreachable;
+
context.test_index += 1;
return ptr;
}
@@ -936,13 +928,13 @@ pub const TranslateCContext = struct {
}
pub fn create(self: *TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
- const tc = self.b.allocator.create(TestCase) catch unreachable;
- tc.* = TestCase{
+ const tc = self.b.allocator.create(TestCase{
.name = name,
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
.expected_lines = ArrayList([]const u8).init(self.b.allocator),
.allow_warnings = allow_warnings,
- };
+ }) catch unreachable;
+
tc.addSourceFile(filename, source);
comptime var arg_i = 0;
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
@@ -1023,15 +1015,15 @@ pub const GenHContext = struct {
pub fn create(context: *GenHContext, h_path: []const u8, name: []const u8, case: *const TestCase) *GenHCmpOutputStep {
const allocator = context.b.allocator;
- const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
- ptr.* = GenHCmpOutputStep{
+ const ptr = allocator.create(GenHCmpOutputStep{
.step = build.Step.init("ParseCCmpOutput", allocator, make),
.context = context,
.h_path = h_path,
.name = name,
.test_index = context.test_index,
.case = case,
- };
+ }) catch unreachable;
+
context.test_index += 1;
return ptr;
}
@@ -1070,12 +1062,12 @@ pub const GenHContext = struct {
}
pub fn create(self: *GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
- const tc = self.b.allocator.create(TestCase) catch unreachable;
- tc.* = TestCase{
+ const tc = self.b.allocator.create(TestCase{
.name = name,
.sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
.expected_lines = ArrayList([]const u8).init(self.b.allocator),
- };
+ }) catch unreachable;
+
tc.addSourceFile(filename, source);
comptime var arg_i = 0;
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
--
cgit v1.2.3
From e891f9cd9dc52b4bc63ae63115822fbf5e724348 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 20 Jun 2018 17:16:27 -0400
Subject: zig fmt
---
std/atomic/queue.zig | 2 +-
std/atomic/stack.zig | 2 +-
std/debug/index.zig | 6 ++----
std/linked_list.zig | 6 +++---
std/mem.zig | 2 +-
std/os/index.zig | 4 ++--
test/cases/null.zig | 2 +-
7 files changed, 11 insertions(+), 13 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 5b810f95ac..308cd6c736 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -116,7 +116,7 @@ fn startPuts(ctx: *Context) u8 {
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Queue(i32).Node{
.next = null,
- .data = x
+ .data = x,
}) catch unreachable;
ctx.queue.put(node);
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 2272be4a92..011ab3254f 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -119,7 +119,7 @@ fn startPuts(ctx: *Context) u8 {
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Stack(i32).Node{
.next = null,
- .data = x
+ .data = x,
}) catch unreachable;
ctx.stack.push(node);
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
diff --git a/std/debug/index.zig b/std/debug/index.zig
index 19cee3c65d..57b2dfc300 100644
--- a/std/debug/index.zig
+++ b/std/debug/index.zig
@@ -279,9 +279,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
var exe_file = try os.openSelfExe();
defer exe_file.close();
- const st = try allocator.create(ElfStackTrace{
- .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file))
- });
+ const st = try allocator.create(ElfStackTrace{ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)) });
errdefer allocator.destroy(st);
return st;
},
@@ -972,7 +970,7 @@ fn scanAllCompileUnits(st: *ElfStackTrace) !void {
try st.self_exe_file.seekTo(compile_unit_pos);
- const compile_unit_die = try st.allocator().create( try parseDie(st, abbrev_table, is_64) );
+ const compile_unit_die = try st.allocator().create(try parseDie(st, abbrev_table, is_64));
if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo;
diff --git a/std/linked_list.zig b/std/linked_list.zig
index f4f7aab752..128ccdf4b1 100644
--- a/std/linked_list.zig
+++ b/std/linked_list.zig
@@ -194,9 +194,9 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node {
comptime assert(!isIntrusive());
return allocator.create(Node{
- .prev = null,
- .next = null,
- .data = undefined
+ .prev = null,
+ .next = null,
+ .data = undefined,
});
}
diff --git a/std/mem.zig b/std/mem.zig
index 18fc386b47..29f5f924c8 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -44,7 +44,7 @@ pub const Allocator = struct {
/// Alias of `create`
/// Call `destroy` with the result
pub fn construct(self: *Allocator, init: var) Error!*@typeOf(init) {
- return self.create(init);
+ return self.create(init);
}
/// `ptr` should be the return value of `construct` or `create`
diff --git a/std/os/index.zig b/std/os/index.zig
index 7b69bd0b36..ce29106810 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -2583,8 +2583,8 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count];
const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext{
- .thread = undefined,
- .inner = context
+ .thread = undefined,
+ .inner = context,
}) catch unreachable;
outer_context.thread.data.heap_handle = heap_handle;
diff --git a/test/cases/null.zig b/test/cases/null.zig
index d2a9aaed55..c86dd34b06 100644
--- a/test/cases/null.zig
+++ b/test/cases/null.zig
@@ -146,7 +146,7 @@ test "null with default unwrap" {
test "optional types" {
comptime {
- const opt_type_struct = StructWithOptionalType { .t=u8, };
+ const opt_type_struct = StructWithOptionalType{ .t = u8 };
assert(opt_type_struct.t != null and opt_type_struct.t.? == u8);
}
}
--
cgit v1.2.3
From 85f928f8bff8c033f6ef0104d68b033669cb36e4 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 20 Jun 2018 17:33:29 -0400
Subject: remove std.mem.Allocator.construct and other fixups
---
src-self-hosted/errmsg.zig | 2 +-
src-self-hosted/main.zig | 2 +-
src-self-hosted/module.zig | 13 +++-
std/atomic/queue.zig | 2 +-
std/atomic/stack.zig | 2 +-
std/heap.zig | 3 +-
std/linked_list.zig | 6 +-
std/mem.zig | 8 +--
std/os/index.zig | 13 ++--
std/zig/parse.zig | 150 ++++++++++++++++++++++-----------------------
10 files changed, 101 insertions(+), 100 deletions(-)
(limited to 'std/atomic')
diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig
index 32d2450aac..b6fd78d8f6 100644
--- a/src-self-hosted/errmsg.zig
+++ b/src-self-hosted/errmsg.zig
@@ -35,7 +35,7 @@ pub fn createFromParseError(
var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
try parse_error.render(&tree.tokens, out_stream);
- const msg = try allocator.construct(Msg{
+ const msg = try allocator.create(Msg{
.tree = tree,
.path = path,
.text = text_buf.toOwnedSlice(),
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index f7f38130b5..45e6bb742a 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -707,7 +707,7 @@ const Fmt = struct {
// file_path must outlive Fmt
fn addToQueue(self: *Fmt, file_path: []const u8) !void {
- const new_node = try self.seen.allocator.construct(std.LinkedList([]const u8).Node{
+ const new_node = try self.seen.allocator.create(std.LinkedList([]const u8).Node{
.prev = undefined,
.next = undefined,
.data = file_path,
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index 997ef5eed2..5f02f1a832 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -114,7 +114,7 @@ pub const Module = struct {
.name = name,
.path = path,
.children = ArrayList(*CliPkg).init(allocator),
- .parent = parent
+ .parent = parent,
});
return pkg;
}
@@ -127,7 +127,16 @@ pub const Module = struct {
}
};
- pub fn create(allocator: *mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: *const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !*Module {
+ pub fn create(
+ allocator: *mem.Allocator,
+ name: []const u8,
+ root_src_path: ?[]const u8,
+ target: *const Target,
+ kind: Kind,
+ build_mode: builtin.Mode,
+ zig_lib_dir: []const u8,
+ cache_dir: []const u8,
+ ) !*Module {
var name_buffer = try Buffer.init(allocator, name);
errdefer name_buffer.deinit();
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 308cd6c736..16dc9f6cc3 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -115,7 +115,7 @@ fn startPuts(ctx: *Context) u8 {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Queue(i32).Node{
- .next = null,
+ .next = undefined,
.data = x,
}) catch unreachable;
ctx.queue.put(node);
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index 011ab3254f..d74bee8e8b 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -118,7 +118,7 @@ fn startPuts(ctx: *Context) u8 {
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const node = ctx.allocator.create(Stack(i32).Node{
- .next = null,
+ .next = undefined,
.data = x,
}) catch unreachable;
ctx.stack.push(node);
diff --git a/std/heap.zig b/std/heap.zig
index 7fc00cd0a4..41d7802fdd 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -407,8 +407,7 @@ fn testAllocator(allocator: *mem.Allocator) !void {
var slice = try allocator.alloc(*i32, 100);
for (slice) |*item, i| {
- item.* = try allocator.create(i32(0));
- item.*.* = @intCast(i32, i);
+ item.* = try allocator.create(@intCast(i32, i));
}
for (slice) |item, i| {
diff --git a/std/linked_list.zig b/std/linked_list.zig
index 128ccdf4b1..62cd5ca2bb 100644
--- a/std/linked_list.zig
+++ b/std/linked_list.zig
@@ -193,11 +193,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
/// A pointer to the new node.
pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node {
comptime assert(!isIntrusive());
- return allocator.create(Node{
- .prev = null,
- .next = null,
- .data = undefined,
- });
+ return allocator.create(Node(undefined));
}
/// Deallocate a node.
diff --git a/std/mem.zig b/std/mem.zig
index 29f5f924c8..ba59faf711 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -41,13 +41,7 @@ pub const Allocator = struct {
return ptr;
}
- /// Alias of `create`
- /// Call `destroy` with the result
- pub fn construct(self: *Allocator, init: var) Error!*@typeOf(init) {
- return self.create(init);
- }
-
- /// `ptr` should be the return value of `construct` or `create`
+ /// `ptr` should be the return value of `create`
pub fn destroy(self: *Allocator, ptr: var) void {
const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr));
self.freeFn(self, non_const_ptr[0..@sizeOf(@typeOf(ptr).Child)]);
diff --git a/std/os/index.zig b/std/os/index.zig
index ce29106810..52b36c351c 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -2468,7 +2468,7 @@ pub const Thread = struct {
data: Data,
pub const use_pthreads = is_posix and builtin.link_libc;
- const Data = if (use_pthreads)
+ pub const Data = if (use_pthreads)
struct {
handle: c.pthread_t,
stack_addr: usize,
@@ -2583,13 +2583,16 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count];
const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext{
- .thread = undefined,
+ .thread = Thread{
+ .data = Thread.Data{
+ .heap_handle = heap_handle,
+ .alloc_start = bytes_ptr,
+ .handle = undefined,
+ },
+ },
.inner = context,
}) catch unreachable;
- outer_context.thread.data.heap_handle = heap_handle;
- outer_context.thread.data.alloc_start = bytes_ptr;
-
const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(*c_void, &outer_context.inner);
outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) orelse {
const err = windows.GetLastError();
diff --git a/std/zig/parse.zig b/std/zig/parse.zig
index 877b81c527..9f0371d4da 100644
--- a/std/zig/parse.zig
+++ b/std/zig/parse.zig
@@ -17,7 +17,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
defer stack.deinit();
const arena = &tree_arena.allocator;
- const root_node = try arena.construct(ast.Node.Root{
+ const root_node = try arena.create(ast.Node.Root{
.base = ast.Node{ .id = ast.Node.Id.Root },
.decls = ast.Node.Root.DeclList.init(arena),
.doc_comments = null,
@@ -65,14 +65,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
Token.Id.Keyword_test => {
stack.append(State.TopLevel) catch unreachable;
- const block = try arena.construct(ast.Node.Block{
+ const block = try arena.create(ast.Node.Block{
.base = ast.Node{ .id = ast.Node.Id.Block },
.label = null,
.lbrace = undefined,
.statements = ast.Node.Block.StatementList.init(arena),
.rbrace = undefined,
});
- const test_node = try arena.construct(ast.Node.TestDecl{
+ const test_node = try arena.create(ast.Node.TestDecl{
.base = ast.Node{ .id = ast.Node.Id.TestDecl },
.doc_comments = comments,
.test_token = token_index,
@@ -109,14 +109,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_comptime => {
- const block = try arena.construct(ast.Node.Block{
+ const block = try arena.create(ast.Node.Block{
.base = ast.Node{ .id = ast.Node.Id.Block },
.label = null,
.lbrace = undefined,
.statements = ast.Node.Block.StatementList.init(arena),
.rbrace = undefined,
});
- const node = try arena.construct(ast.Node.Comptime{
+ const node = try arena.create(ast.Node.Comptime{
.base = ast.Node{ .id = ast.Node.Id.Comptime },
.comptime_token = token_index,
.expr = &block.base,
@@ -225,7 +225,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
return tree;
}
- const node = try arena.construct(ast.Node.Use{
+ const node = try arena.create(ast.Node.Use{
.base = ast.Node{ .id = ast.Node.Id.Use },
.use_token = token_index,
.visib_token = ctx.visib_token,
@@ -266,7 +266,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc, Token.Id.Keyword_async => {
- const fn_proto = try arena.construct(ast.Node.FnProto{
+ const fn_proto = try arena.create(ast.Node.FnProto{
.base = ast.Node{ .id = ast.Node.Id.FnProto },
.doc_comments = ctx.comments,
.visib_token = ctx.visib_token,
@@ -298,7 +298,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_async => {
- const async_node = try arena.construct(ast.Node.AsyncAttribute{
+ const async_node = try arena.create(ast.Node.AsyncAttribute{
.base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
.async_token = token_index,
.allocator_type = null,
@@ -330,7 +330,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.TopLevelExternOrField => |ctx| {
if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |identifier| {
- const node = try arena.construct(ast.Node.StructField{
+ const node = try arena.create(ast.Node.StructField{
.base = ast.Node{ .id = ast.Node.Id.StructField },
.doc_comments = ctx.comments,
.visib_token = ctx.visib_token,
@@ -375,7 +375,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
const token_ptr = token.ptr;
- const node = try arena.construct(ast.Node.ContainerDecl{
+ const node = try arena.create(ast.Node.ContainerDecl{
.base = ast.Node{ .id = ast.Node.Id.ContainerDecl },
.layout_token = ctx.layout_token,
.kind_token = switch (token_ptr.id) {
@@ -448,7 +448,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
Token.Id.Identifier => {
switch (tree.tokens.at(container_decl.kind_token).id) {
Token.Id.Keyword_struct => {
- const node = try arena.construct(ast.Node.StructField{
+ const node = try arena.create(ast.Node.StructField{
.base = ast.Node{ .id = ast.Node.Id.StructField },
.doc_comments = comments,
.visib_token = null,
@@ -464,7 +464,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_union => {
- const node = try arena.construct(ast.Node.UnionTag{
+ const node = try arena.create(ast.Node.UnionTag{
.base = ast.Node{ .id = ast.Node.Id.UnionTag },
.name_token = token_index,
.type_expr = null,
@@ -480,7 +480,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_enum => {
- const node = try arena.construct(ast.Node.EnumTag{
+ const node = try arena.create(ast.Node.EnumTag{
.base = ast.Node{ .id = ast.Node.Id.EnumTag },
.name_token = token_index,
.value = null,
@@ -562,7 +562,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.VarDecl => |ctx| {
- const var_decl = try arena.construct(ast.Node.VarDecl{
+ const var_decl = try arena.create(ast.Node.VarDecl{
.base = ast.Node{ .id = ast.Node.Id.VarDecl },
.doc_comments = ctx.comments,
.visib_token = ctx.visib_token,
@@ -660,7 +660,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_ptr = token.ptr;
switch (token_ptr.id) {
Token.Id.LBrace => {
- const block = try arena.construct(ast.Node.Block{
+ const block = try arena.create(ast.Node.Block{
.base = ast.Node{ .id = ast.Node.Id.Block },
.label = null,
.lbrace = token_index,
@@ -712,7 +712,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
// TODO: this is a special case. Remove this when #760 is fixed
if (token_ptr.id == Token.Id.Keyword_error) {
if (tok_it.peek().?.id == Token.Id.LBrace) {
- const error_type_node = try arena.construct(ast.Node.ErrorType{
+ const error_type_node = try arena.create(ast.Node.ErrorType{
.base = ast.Node{ .id = ast.Node.Id.ErrorType },
.token = token_index,
});
@@ -733,7 +733,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| {
continue;
}
- const param_decl = try arena.construct(ast.Node.ParamDecl{
+ const param_decl = try arena.create(ast.Node.ParamDecl{
.base = ast.Node{ .id = ast.Node.Id.ParamDecl },
.comptime_token = null,
.noalias_token = null,
@@ -819,7 +819,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_ptr = token.ptr;
switch (token_ptr.id) {
Token.Id.LBrace => {
- const block = try arena.construct(ast.Node.Block{
+ const block = try arena.create(ast.Node.Block{
.base = ast.Node{ .id = ast.Node.Id.Block },
.label = ctx.label,
.lbrace = token_index,
@@ -853,7 +853,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_suspend => {
- const node = try arena.construct(ast.Node.Suspend{
+ const node = try arena.create(ast.Node.Suspend{
.base = ast.Node{ .id = ast.Node.Id.Suspend },
.label = ctx.label,
.suspend_token = token_index,
@@ -925,7 +925,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
}
},
State.While => |ctx| {
- const node = try arena.construct(ast.Node.While{
+ const node = try arena.create(ast.Node.While{
.base = ast.Node{ .id = ast.Node.Id.While },
.label = ctx.label,
.inline_token = ctx.inline_token,
@@ -954,7 +954,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
State.For => |ctx| {
- const node = try arena.construct(ast.Node.For{
+ const node = try arena.create(ast.Node.For{
.base = ast.Node{ .id = ast.Node.Id.For },
.label = ctx.label,
.inline_token = ctx.inline_token,
@@ -975,7 +975,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
State.Else => |dest| {
if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
- const node = try arena.construct(ast.Node.Else{
+ const node = try arena.create(ast.Node.Else{
.base = ast.Node{ .id = ast.Node.Id.Else },
.else_token = else_token,
.payload = null,
@@ -1038,7 +1038,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => {
- const node = try arena.construct(ast.Node.Defer{
+ const node = try arena.create(ast.Node.Defer{
.base = ast.Node{ .id = ast.Node.Id.Defer },
.defer_token = token_index,
.kind = switch (token_ptr.id) {
@@ -1056,7 +1056,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.LBrace => {
- const inner_block = try arena.construct(ast.Node.Block{
+ const inner_block = try arena.create(ast.Node.Block{
.base = ast.Node{ .id = ast.Node.Id.Block },
.label = null,
.lbrace = token_index,
@@ -1124,7 +1124,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.AsmOutput{
+ const node = try arena.create(ast.Node.AsmOutput{
.base = ast.Node{ .id = ast.Node.Id.AsmOutput },
.lbracket = lbracket_index,
.symbolic_name = undefined,
@@ -1178,7 +1178,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.AsmInput{
+ const node = try arena.create(ast.Node.AsmInput{
.base = ast.Node{ .id = ast.Node.Id.AsmInput },
.lbracket = lbracket_index,
.symbolic_name = undefined,
@@ -1243,7 +1243,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.FieldInitializer{
+ const node = try arena.create(ast.Node.FieldInitializer{
.base = ast.Node{ .id = ast.Node.Id.FieldInitializer },
.period_token = undefined,
.name_token = undefined,
@@ -1332,7 +1332,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
}
const comments = try eatDocComments(arena, &tok_it, &tree);
- const node = try arena.construct(ast.Node.SwitchCase{
+ const node = try arena.create(ast.Node.SwitchCase{
.base = ast.Node{ .id = ast.Node.Id.SwitchCase },
.items = ast.Node.SwitchCase.ItemList.init(arena),
.payload = null,
@@ -1369,7 +1369,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (token_ptr.id == Token.Id.Keyword_else) {
- const else_node = try arena.construct(ast.Node.SwitchElse{
+ const else_node = try arena.create(ast.Node.SwitchElse{
.base = ast.Node{ .id = ast.Node.Id.SwitchElse },
.token = token_index,
});
@@ -1468,7 +1468,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
State.ExternType => |ctx| {
if (eatToken(&tok_it, &tree, Token.Id.Keyword_fn)) |fn_token| {
- const fn_proto = try arena.construct(ast.Node.FnProto{
+ const fn_proto = try arena.create(ast.Node.FnProto{
.base = ast.Node{ .id = ast.Node.Id.FnProto },
.doc_comments = ctx.comments,
.visib_token = null,
@@ -1641,7 +1641,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.Payload{
+ const node = try arena.create(ast.Node.Payload{
.base = ast.Node{ .id = ast.Node.Id.Payload },
.lpipe = token_index,
.error_symbol = undefined,
@@ -1677,7 +1677,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.PointerPayload{
+ const node = try arena.create(ast.Node.PointerPayload{
.base = ast.Node{ .id = ast.Node.Id.PointerPayload },
.lpipe = token_index,
.ptr_token = null,
@@ -1720,7 +1720,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.PointerIndexPayload{
+ const node = try arena.create(ast.Node.PointerIndexPayload{
.base = ast.Node{ .id = ast.Node.Id.PointerIndexPayload },
.lpipe = token_index,
.ptr_token = null,
@@ -1754,7 +1754,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_ptr = token.ptr;
switch (token_ptr.id) {
Token.Id.Keyword_return, Token.Id.Keyword_break, Token.Id.Keyword_continue => {
- const node = try arena.construct(ast.Node.ControlFlowExpression{
+ const node = try arena.create(ast.Node.ControlFlowExpression{
.base = ast.Node{ .id = ast.Node.Id.ControlFlowExpression },
.ltoken = token_index,
.kind = undefined,
@@ -1783,7 +1783,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => {
- const node = try arena.construct(ast.Node.PrefixOp{
+ const node = try arena.create(ast.Node.PrefixOp{
.base = ast.Node{ .id = ast.Node.Id.PrefixOp },
.op_token = token_index,
.op = switch (token_ptr.id) {
@@ -1817,7 +1817,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = ellipsis3,
@@ -1842,7 +1842,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToAssignment(token_ptr.id)) |ass_id| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -1872,7 +1872,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToUnwrapExpr(token_ptr.id)) |unwrap_id| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -1904,7 +1904,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = or_token,
@@ -1928,7 +1928,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = and_token,
@@ -1955,7 +1955,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToComparison(token_ptr.id)) |comp_id| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -1982,7 +1982,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = pipe,
@@ -2006,7 +2006,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = caret,
@@ -2030,7 +2030,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = ampersand,
@@ -2057,7 +2057,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToBitShift(token_ptr.id)) |bitshift_id| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -2087,7 +2087,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToAddition(token_ptr.id)) |add_id| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -2117,7 +2117,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToMultiply(token_ptr.id)) |mult_id| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -2145,7 +2145,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (tok_it.peek().?.id == Token.Id.Period) {
- const node = try arena.construct(ast.Node.SuffixOp{
+ const node = try arena.create(ast.Node.SuffixOp{
.base = ast.Node{ .id = ast.Node.Id.SuffixOp },
.lhs = lhs,
.op = ast.Node.SuffixOp.Op{ .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
@@ -2164,7 +2164,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.SuffixOp{
+ const node = try arena.create(ast.Node.SuffixOp{
.base = ast.Node{ .id = ast.Node.Id.SuffixOp },
.lhs = lhs,
.op = ast.Node.SuffixOp.Op{ .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
@@ -2193,7 +2193,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const lhs = opt_ctx.get() orelse continue;
if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| {
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = bang,
@@ -2212,7 +2212,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_index = token.index;
const token_ptr = token.ptr;
if (tokenIdToPrefixOp(token_ptr.id)) |prefix_id| {
- var node = try arena.construct(ast.Node.PrefixOp{
+ var node = try arena.create(ast.Node.PrefixOp{
.base = ast.Node{ .id = ast.Node.Id.PrefixOp },
.op_token = token_index,
.op = prefix_id,
@@ -2222,7 +2222,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
// Treat '**' token as two pointer types
if (token_ptr.id == Token.Id.AsteriskAsterisk) {
- const child = try arena.construct(ast.Node.PrefixOp{
+ const child = try arena.create(ast.Node.PrefixOp{
.base = ast.Node{ .id = ast.Node.Id.PrefixOp },
.op_token = token_index,
.op = prefix_id,
@@ -2246,7 +2246,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
State.SuffixOpExpressionBegin => |opt_ctx| {
if (eatToken(&tok_it, &tree, Token.Id.Keyword_async)) |async_token| {
- const async_node = try arena.construct(ast.Node.AsyncAttribute{
+ const async_node = try arena.create(ast.Node.AsyncAttribute{
.base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
.async_token = async_token,
.allocator_type = null,
@@ -2277,7 +2277,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token_ptr = token.ptr;
switch (token_ptr.id) {
Token.Id.LParen => {
- const node = try arena.construct(ast.Node.SuffixOp{
+ const node = try arena.create(ast.Node.SuffixOp{
.base = ast.Node{ .id = ast.Node.Id.SuffixOp },
.lhs = lhs,
.op = ast.Node.SuffixOp.Op{
@@ -2301,7 +2301,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.LBracket => {
- const node = try arena.construct(ast.Node.SuffixOp{
+ const node = try arena.create(ast.Node.SuffixOp{
.base = ast.Node{ .id = ast.Node.Id.SuffixOp },
.lhs = lhs,
.op = ast.Node.SuffixOp.Op{ .ArrayAccess = undefined },
@@ -2316,7 +2316,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
},
Token.Id.Period => {
if (eatToken(&tok_it, &tree, Token.Id.Asterisk)) |asterisk_token| {
- const node = try arena.construct(ast.Node.SuffixOp{
+ const node = try arena.create(ast.Node.SuffixOp{
.base = ast.Node{ .id = ast.Node.Id.SuffixOp },
.lhs = lhs,
.op = ast.Node.SuffixOp.Op.Deref,
@@ -2327,7 +2327,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
if (eatToken(&tok_it, &tree, Token.Id.QuestionMark)) |question_token| {
- const node = try arena.construct(ast.Node.SuffixOp{
+ const node = try arena.create(ast.Node.SuffixOp{
.base = ast.Node{ .id = ast.Node.Id.SuffixOp },
.lhs = lhs,
.op = ast.Node.SuffixOp.Op.UnwrapOptional,
@@ -2337,7 +2337,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
continue;
}
- const node = try arena.construct(ast.Node.InfixOp{
+ const node = try arena.create(ast.Node.InfixOp{
.base = ast.Node{ .id = ast.Node.Id.InfixOp },
.lhs = lhs,
.op_token = token_index,
@@ -2397,7 +2397,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_promise => {
- const node = try arena.construct(ast.Node.PromiseType{
+ const node = try arena.create(ast.Node.PromiseType{
.base = ast.Node{ .id = ast.Node.Id.PromiseType },
.promise_token = token.index,
.result = null,
@@ -2423,7 +2423,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.LParen => {
- const node = try arena.construct(ast.Node.GroupedExpression{
+ const node = try arena.create(ast.Node.GroupedExpression{
.base = ast.Node{ .id = ast.Node.Id.GroupedExpression },
.lparen = token.index,
.expr = undefined,
@@ -2441,7 +2441,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Builtin => {
- const node = try arena.construct(ast.Node.BuiltinCall{
+ const node = try arena.create(ast.Node.BuiltinCall{
.base = ast.Node{ .id = ast.Node.Id.BuiltinCall },
.builtin_token = token.index,
.params = ast.Node.BuiltinCall.ParamList.init(arena),
@@ -2460,7 +2460,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.LBracket => {
- const node = try arena.construct(ast.Node.PrefixOp{
+ const node = try arena.create(ast.Node.PrefixOp{
.base = ast.Node{ .id = ast.Node.Id.PrefixOp },
.op_token = token.index,
.op = undefined,
@@ -2519,7 +2519,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_fn => {
- const fn_proto = try arena.construct(ast.Node.FnProto{
+ const fn_proto = try arena.create(ast.Node.FnProto{
.base = ast.Node{ .id = ast.Node.Id.FnProto },
.doc_comments = null,
.visib_token = null,
@@ -2540,7 +2540,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {
- const fn_proto = try arena.construct(ast.Node.FnProto{
+ const fn_proto = try arena.create(ast.Node.FnProto{
.base = ast.Node{ .id = ast.Node.Id.FnProto },
.doc_comments = null,
.visib_token = null,
@@ -2567,7 +2567,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
Token.Id.Keyword_asm => {
- const node = try arena.construct(ast.Node.Asm{
+ const node = try arena.create(ast.Node.Asm{
.base = ast.Node{ .id = ast.Node.Id.Asm },
.asm_token = token.index,
.volatile_token = null,
@@ -2629,7 +2629,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
}
- const node = try arena.construct(ast.Node.ErrorSetDecl{
+ const node = try arena.create(ast.Node.ErrorSetDecl{
.base = ast.Node{ .id = ast.Node.Id.ErrorSetDecl },
.error_token = ctx.error_token,
.decls = ast.Node.ErrorSetDecl.DeclList.init(arena),
@@ -2695,7 +2695,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
return tree;
}
- const node = try arena.construct(ast.Node.ErrorTag{
+ const node = try arena.create(ast.Node.ErrorTag{
.base = ast.Node{ .id = ast.Node.Id.ErrorTag },
.doc_comments = comments,
.name_token = ident_token_index,
@@ -3032,7 +3032,7 @@ fn pushDocComment(arena: *mem.Allocator, line_comment: TokenIndex, result: *?*as
if (result.*) |comment_node| {
break :blk comment_node;
} else {
- const comment_node = try arena.construct(ast.Node.DocComment{
+ const comment_node = try arena.create(ast.Node.DocComment{
.base = ast.Node{ .id = ast.Node.Id.DocComment },
.lines = ast.Node.DocComment.LineList.init(arena),
});
@@ -3061,7 +3061,7 @@ fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterato
return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base;
},
Token.Id.MultilineStringLiteralLine => {
- const node = try arena.construct(ast.Node.MultilineStringLiteral{
+ const node = try arena.create(ast.Node.MultilineStringLiteral{
.base = ast.Node{ .id = ast.Node.Id.MultilineStringLiteral },
.lines = ast.Node.MultilineStringLiteral.LineList.init(arena),
});
@@ -3089,7 +3089,7 @@ fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterato
fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *const OptionalCtx, token_ptr: *const Token, token_index: TokenIndex) !bool {
switch (token_ptr.id) {
Token.Id.Keyword_suspend => {
- const node = try arena.construct(ast.Node.Suspend{
+ const node = try arena.create(ast.Node.Suspend{
.base = ast.Node{ .id = ast.Node.Id.Suspend },
.label = null,
.suspend_token = token_index,
@@ -3103,7 +3103,7 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *con
return true;
},
Token.Id.Keyword_if => {
- const node = try arena.construct(ast.Node.If{
+ const node = try arena.create(ast.Node.If{
.base = ast.Node{ .id = ast.Node.Id.If },
.if_token = token_index,
.condition = undefined,
@@ -3144,7 +3144,7 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *con
return true;
},
Token.Id.Keyword_switch => {
- const node = try arena.construct(ast.Node.Switch{
+ const node = try arena.create(ast.Node.Switch{
.base = ast.Node{ .id = ast.Node.Id.Switch },
.switch_token = token_index,
.expr = undefined,
@@ -3166,7 +3166,7 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *con
return true;
},
Token.Id.Keyword_comptime => {
- const node = try arena.construct(ast.Node.Comptime{
+ const node = try arena.create(ast.Node.Comptime{
.base = ast.Node{ .id = ast.Node.Id.Comptime },
.comptime_token = token_index,
.expr = undefined,
@@ -3178,7 +3178,7 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *con
return true;
},
Token.Id.LBrace => {
- const block = try arena.construct(ast.Node.Block{
+ const block = try arena.create(ast.Node.Block{
.base = ast.Node{ .id = ast.Node.Id.Block },
.label = null,
.lbrace = token_index,
@@ -3318,7 +3318,7 @@ fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
}
fn createLiteral(arena: *mem.Allocator, comptime T: type, token_index: TokenIndex) !*T {
- return arena.construct(T{
+ return arena.create(T{
.base = ast.Node{ .id = ast.Node.typeToId(T) },
.token = token_index,
});
--
cgit v1.2.3
From 0874a5ba77a1d049a0e9e7f9f249605c109a731c Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Fri, 29 Jun 2018 14:45:01 -0400
Subject: std.atomic.queue - document limitation and add MPSC queue
---
CMakeLists.txt | 3 +-
std/atomic/index.zig | 8 +-
std/atomic/queue.zig | 139 ------------------------------
std/atomic/queue_mpmc.zig | 214 ++++++++++++++++++++++++++++++++++++++++++++++
std/atomic/queue_mpsc.zig | 143 +++++++++++++++++++++++++++++++
5 files changed, 364 insertions(+), 143 deletions(-)
delete mode 100644 std/atomic/queue.zig
create mode 100644 std/atomic/queue_mpmc.zig
create mode 100644 std/atomic/queue_mpsc.zig
(limited to 'std/atomic')
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 789da4a8a6..4838aeb797 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -431,7 +431,8 @@ set(ZIG_CPP_SOURCES
set(ZIG_STD_FILES
"array_list.zig"
"atomic/index.zig"
- "atomic/queue.zig"
+ "atomic/queue_mpmc.zig"
+ "atomic/queue_mpsc.zig"
"atomic/stack.zig"
"base64.zig"
"buf_map.zig"
diff --git a/std/atomic/index.zig b/std/atomic/index.zig
index 9d556a6415..c0ea5be183 100644
--- a/std/atomic/index.zig
+++ b/std/atomic/index.zig
@@ -1,7 +1,9 @@
pub const Stack = @import("stack.zig").Stack;
-pub const Queue = @import("queue.zig").Queue;
+pub const QueueMpsc = @import("queue_mpsc.zig").QueueMpsc;
+pub const QueueMpmc = @import("queue_mpmc.zig").QueueMpmc;
test "std.atomic" {
- _ = @import("stack.zig").Stack;
- _ = @import("queue.zig").Queue;
+ _ = @import("stack.zig");
+ _ = @import("queue_mpsc.zig");
+ _ = @import("queue_mpmc.zig");
}
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
deleted file mode 100644
index 16dc9f6cc3..0000000000
--- a/std/atomic/queue.zig
+++ /dev/null
@@ -1,139 +0,0 @@
-const builtin = @import("builtin");
-const AtomicOrder = builtin.AtomicOrder;
-const AtomicRmwOp = builtin.AtomicRmwOp;
-
-/// Many reader, many writer, non-allocating, thread-safe, lock-free
-pub fn Queue(comptime T: type) type {
- return struct {
- head: *Node,
- tail: *Node,
- root: Node,
-
- pub const Self = this;
-
- pub const Node = struct {
- next: ?*Node,
- data: T,
- };
-
- // TODO: well defined copy elision: https://github.com/ziglang/zig/issues/287
- pub fn init(self: *Self) void {
- self.root.next = null;
- self.head = &self.root;
- self.tail = &self.root;
- }
-
- pub fn put(self: *Self, node: *Node) void {
- node.next = null;
-
- const tail = @atomicRmw(*Node, &self.tail, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
- _ = @atomicRmw(?*Node, &tail.next, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
- }
-
- pub fn get(self: *Self) ?*Node {
- var head = @atomicLoad(*Node, &self.head, AtomicOrder.SeqCst);
- while (true) {
- const node = head.next orelse return null;
- head = @cmpxchgWeak(*Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return node;
- }
- }
- };
-}
-
-const std = @import("std");
-const Context = struct {
- allocator: *std.mem.Allocator,
- queue: *Queue(i32),
- put_sum: isize,
- get_sum: isize,
- get_count: usize,
- puts_done: u8, // TODO make this a bool
-};
-
-// TODO add lazy evaluated build options and then put puts_per_thread behind
-// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
-// CI we would use a less aggressive setting since at 1 core, while we still
-// want this test to pass, we need a smaller value since there is so much thrashing
-// we would also use a less aggressive setting when running in valgrind
-const puts_per_thread = 500;
-const put_thread_count = 3;
-
-test "std.atomic.queue" {
- var direct_allocator = std.heap.DirectAllocator.init();
- defer direct_allocator.deinit();
-
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
- defer direct_allocator.allocator.free(plenty_of_memory);
-
- var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
- var a = &fixed_buffer_allocator.allocator;
-
- var queue: Queue(i32) = undefined;
- queue.init();
- var context = Context{
- .allocator = a,
- .queue = &queue,
- .put_sum = 0,
- .get_sum = 0,
- .puts_done = 0,
- .get_count = 0,
- };
-
- var putters: [put_thread_count]*std.os.Thread = undefined;
- for (putters) |*t| {
- t.* = try std.os.spawnThread(&context, startPuts);
- }
- var getters: [put_thread_count]*std.os.Thread = undefined;
- for (getters) |*t| {
- t.* = try std.os.spawnThread(&context, startGets);
- }
-
- for (putters) |t|
- t.wait();
- _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
- for (getters) |t|
- t.wait();
-
- if (context.put_sum != context.get_sum) {
- std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
- }
-
- if (context.get_count != puts_per_thread * put_thread_count) {
- std.debug.panic(
- "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
- context.get_count,
- u32(puts_per_thread),
- u32(put_thread_count),
- );
- }
-}
-
-fn startPuts(ctx: *Context) u8 {
- var put_count: usize = puts_per_thread;
- var r = std.rand.DefaultPrng.init(0xdeadbeef);
- while (put_count != 0) : (put_count -= 1) {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
- const x = @bitCast(i32, r.random.scalar(u32));
- const node = ctx.allocator.create(Queue(i32).Node{
- .next = undefined,
- .data = x,
- }) catch unreachable;
- ctx.queue.put(node);
- _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
- }
- return 0;
-}
-
-fn startGets(ctx: *Context) u8 {
- while (true) {
- const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
-
- while (ctx.queue.get()) |node| {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
- _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
- _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
- }
-
- if (last) return 0;
- }
-}
diff --git a/std/atomic/queue_mpmc.zig b/std/atomic/queue_mpmc.zig
new file mode 100644
index 0000000000..7ffc9f9ccb
--- /dev/null
+++ b/std/atomic/queue_mpmc.zig
@@ -0,0 +1,214 @@
+const builtin = @import("builtin");
+const AtomicOrder = builtin.AtomicOrder;
+const AtomicRmwOp = builtin.AtomicRmwOp;
+
+/// Many producer, many consumer, non-allocating, thread-safe, lock-free
+/// This implementation has a crippling limitation - it hangs onto node
+/// memory for 1 extra get() and 1 extra put() operation - when get() returns a node, that
+/// node must not be freed until both the next get() and the next put() completes.
+pub fn QueueMpmc(comptime T: type) type {
+ return struct {
+ head: *Node,
+ tail: *Node,
+ root: Node,
+
+ pub const Self = this;
+
+ pub const Node = struct {
+ next: ?*Node,
+ data: T,
+ };
+
+ /// TODO: well defined copy elision: https://github.com/ziglang/zig/issues/287
+ pub fn init(self: *Self) void {
+ self.root.next = null;
+ self.head = &self.root;
+ self.tail = &self.root;
+ }
+
+ pub fn put(self: *Self, node: *Node) void {
+ node.next = null;
+
+ const tail = @atomicRmw(*Node, &self.tail, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
+ _ = @atomicRmw(?*Node, &tail.next, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
+ }
+
+ /// node must not be freed until both the next get() and the next put() complete
+ pub fn get(self: *Self) ?*Node {
+ var head = @atomicLoad(*Node, &self.head, AtomicOrder.SeqCst);
+ while (true) {
+ const node = head.next orelse return null;
+ head = @cmpxchgWeak(*Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return node;
+ }
+ }
+
+ ///// This is a debug function that is not thread-safe.
+ pub fn dump(self: *Self) void {
+ std.debug.warn("head: ");
+ dumpRecursive(self.head, 0);
+ std.debug.warn("tail: ");
+ dumpRecursive(self.tail, 0);
+ }
+
+ fn dumpRecursive(optional_node: ?*Node, indent: usize) void {
+ var stderr_file = std.io.getStdErr() catch return;
+ const stderr = &std.io.FileOutStream.init(&stderr_file).stream;
+ stderr.writeByteNTimes(' ', indent) catch return;
+ if (optional_node) |node| {
+ std.debug.warn("0x{x}={}\n", @ptrToInt(node), node.data);
+ dumpRecursive(node.next, indent + 1);
+ } else {
+ std.debug.warn("(null)\n");
+ }
+ }
+ };
+}
+
+const std = @import("std");
+const assert = std.debug.assert;
+
+const Context = struct {
+ allocator: *std.mem.Allocator,
+ queue: *QueueMpmc(i32),
+ put_sum: isize,
+ get_sum: isize,
+ get_count: usize,
+ puts_done: u8, // TODO make this a bool
+};
+
+// TODO add lazy evaluated build options and then put puts_per_thread behind
+// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
+// CI we would use a less aggressive setting since at 1 core, while we still
+// want this test to pass, we need a smaller value since there is so much thrashing
+// we would also use a less aggressive setting when running in valgrind
+const puts_per_thread = 500;
+const put_thread_count = 3;
+
+test "std.atomic.queue_mpmc" {
+ var direct_allocator = std.heap.DirectAllocator.init();
+ defer direct_allocator.deinit();
+
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
+ defer direct_allocator.allocator.free(plenty_of_memory);
+
+ var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
+ var a = &fixed_buffer_allocator.allocator;
+
+ var queue: QueueMpmc(i32) = undefined;
+ queue.init();
+ var context = Context{
+ .allocator = a,
+ .queue = &queue,
+ .put_sum = 0,
+ .get_sum = 0,
+ .puts_done = 0,
+ .get_count = 0,
+ };
+
+ var putters: [put_thread_count]*std.os.Thread = undefined;
+ for (putters) |*t| {
+ t.* = try std.os.spawnThread(&context, startPuts);
+ }
+ var getters: [put_thread_count]*std.os.Thread = undefined;
+ for (getters) |*t| {
+ t.* = try std.os.spawnThread(&context, startGets);
+ }
+
+ for (putters) |t|
+ t.wait();
+ _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ for (getters) |t|
+ t.wait();
+
+ if (context.put_sum != context.get_sum) {
+ std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
+ }
+
+ if (context.get_count != puts_per_thread * put_thread_count) {
+ std.debug.panic(
+ "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
+ context.get_count,
+ u32(puts_per_thread),
+ u32(put_thread_count),
+ );
+ }
+}
+
+fn startPuts(ctx: *Context) u8 {
+ var put_count: usize = puts_per_thread;
+ var r = std.rand.DefaultPrng.init(0xdeadbeef);
+ while (put_count != 0) : (put_count -= 1) {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ const x = @bitCast(i32, r.random.scalar(u32));
+ const node = ctx.allocator.create(QueueMpmc(i32).Node{
+ .next = undefined,
+ .data = x,
+ }) catch unreachable;
+ ctx.queue.put(node);
+ _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
+ }
+ return 0;
+}
+
+fn startGets(ctx: *Context) u8 {
+ while (true) {
+ const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
+
+ while (ctx.queue.get()) |node| {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
+ _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
+ }
+
+ if (last) return 0;
+ }
+}
+
+test "std.atomic.queue_mpmc single-threaded" {
+ var queue: QueueMpmc(i32) = undefined;
+ queue.init();
+
+ var node_0 = QueueMpmc(i32).Node{
+ .data = 0,
+ .next = undefined,
+ };
+ queue.put(&node_0);
+
+ var node_1 = QueueMpmc(i32).Node{
+ .data = 1,
+ .next = undefined,
+ };
+ queue.put(&node_1);
+
+ assert(queue.get().?.data == 0);
+
+ var node_2 = QueueMpmc(i32).Node{
+ .data = 2,
+ .next = undefined,
+ };
+ queue.put(&node_2);
+
+ var node_3 = QueueMpmc(i32).Node{
+ .data = 3,
+ .next = undefined,
+ };
+ queue.put(&node_3);
+
+ assert(queue.get().?.data == 1);
+
+ assert(queue.get().?.data == 2);
+
+ var node_4 = QueueMpmc(i32).Node{
+ .data = 4,
+ .next = undefined,
+ };
+ queue.put(&node_4);
+
+ assert(queue.get().?.data == 3);
+ // if we were to set node_3.next to null here, it would cause this test
+ // to fail. this demonstrates the limitation of hanging on to extra memory.
+
+ assert(queue.get().?.data == 4);
+
+ assert(queue.get() == null);
+}
diff --git a/std/atomic/queue_mpsc.zig b/std/atomic/queue_mpsc.zig
new file mode 100644
index 0000000000..66eb4573df
--- /dev/null
+++ b/std/atomic/queue_mpsc.zig
@@ -0,0 +1,143 @@
+const std = @import("std");
+const assert = std.debug.assert;
+const builtin = @import("builtin");
+const AtomicOrder = builtin.AtomicOrder;
+const AtomicRmwOp = builtin.AtomicRmwOp;
+
+/// Many producer, single consumer, non-allocating, thread-safe, lock-free
+pub fn QueueMpsc(comptime T: type) type {
+ return struct {
+ inboxes: [2]std.atomic.Stack(T),
+ outbox: std.atomic.Stack(T),
+ inbox_index: usize,
+
+ pub const Self = this;
+
+ pub const Node = std.atomic.Stack(T).Node;
+
+ pub fn init() Self {
+ return Self{
+ .inboxes = []std.atomic.Stack(T){
+ std.atomic.Stack(T).init(),
+ std.atomic.Stack(T).init(),
+ },
+ .outbox = std.atomic.Stack(T).init(),
+ .inbox_index = 0,
+ };
+ }
+
+ pub fn put(self: *Self, node: *Node) void {
+ const inbox_index = @atomicLoad(usize, &self.inbox_index, AtomicOrder.SeqCst);
+ const inbox = &self.inboxes[inbox_index];
+ inbox.push(node);
+ }
+
+ pub fn get(self: *Self) ?*Node {
+ if (self.outbox.pop()) |node| {
+ return node;
+ }
+ const prev_inbox_index = @atomicRmw(usize, &self.inbox_index, AtomicRmwOp.Xor, 0x1, AtomicOrder.SeqCst);
+ const prev_inbox = &self.inboxes[prev_inbox_index];
+ while (prev_inbox.pop()) |node| {
+ self.outbox.push(node);
+ }
+ return self.outbox.pop();
+ }
+ };
+}
+
+const Context = struct {
+ allocator: *std.mem.Allocator,
+ queue: *QueueMpsc(i32),
+ put_sum: isize,
+ get_sum: isize,
+ get_count: usize,
+ puts_done: u8, // TODO make this a bool
+};
+
+// TODO add lazy evaluated build options and then put puts_per_thread behind
+// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
+// CI we would use a less aggressive setting since at 1 core, while we still
+// want this test to pass, we need a smaller value since there is so much thrashing
+// we would also use a less aggressive setting when running in valgrind
+const puts_per_thread = 500;
+const put_thread_count = 3;
+
+test "std.atomic.queue_mpsc" {
+ var direct_allocator = std.heap.DirectAllocator.init();
+ defer direct_allocator.deinit();
+
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
+ defer direct_allocator.allocator.free(plenty_of_memory);
+
+ var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
+ var a = &fixed_buffer_allocator.allocator;
+
+ var queue = QueueMpsc(i32).init();
+ var context = Context{
+ .allocator = a,
+ .queue = &queue,
+ .put_sum = 0,
+ .get_sum = 0,
+ .puts_done = 0,
+ .get_count = 0,
+ };
+
+ var putters: [put_thread_count]*std.os.Thread = undefined;
+ for (putters) |*t| {
+ t.* = try std.os.spawnThread(&context, startPuts);
+ }
+ var getters: [1]*std.os.Thread = undefined;
+ for (getters) |*t| {
+ t.* = try std.os.spawnThread(&context, startGets);
+ }
+
+ for (putters) |t|
+ t.wait();
+ _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ for (getters) |t|
+ t.wait();
+
+ if (context.put_sum != context.get_sum) {
+ std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
+ }
+
+ if (context.get_count != puts_per_thread * put_thread_count) {
+ std.debug.panic(
+ "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
+ context.get_count,
+ u32(puts_per_thread),
+ u32(put_thread_count),
+ );
+ }
+}
+
+fn startPuts(ctx: *Context) u8 {
+ var put_count: usize = puts_per_thread;
+ var r = std.rand.DefaultPrng.init(0xdeadbeef);
+ while (put_count != 0) : (put_count -= 1) {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ const x = @bitCast(i32, r.random.scalar(u32));
+ const node = ctx.allocator.create(QueueMpsc(i32).Node{
+ .next = undefined,
+ .data = x,
+ }) catch unreachable;
+ ctx.queue.put(node);
+ _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
+ }
+ return 0;
+}
+
+fn startGets(ctx: *Context) u8 {
+ while (true) {
+ const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
+
+ while (ctx.queue.get()) |node| {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
+ _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
+ }
+
+ if (last) return 0;
+ }
+}
--
cgit v1.2.3
From a3f55aaf34f0a459c8aec4b35e55ad4534eaca30 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Fri, 29 Jun 2018 15:39:55 -0400
Subject: add event loop Channel abstraction
This is akin to channels in Go, except:
* implemented in userland
* they are lock-free and thread-safe
* they integrate with the userland event loop
The self hosted compiler is changed to use a channel for events,
and made to stay alive, watching files and performing builds when
things change, however the main.zig file exits after 1 build.
Note that nothing is actually built yet, it just parses the input
and then declares that the build succeeded.
Next items to do:
* add windows and macos support for std.event.Loop
* improve the event loop stop() operation
* make the event loop multiplex coroutines onto kernel threads
* watch source file for updates, and provide AST diffs
(at least list the top level declaration changes)
* top level declaration analysis
---
src-self-hosted/main.zig | 37 +++++-
src-self-hosted/module.zig | 135 ++++++++++++++++------
std/atomic/queue_mpsc.zig | 2 +-
std/event.zig | 279 ++++++++++++++++++++++++++++++++++++++++++++-
std/fmt/index.zig | 3 +
std/heap.zig | 1 +
6 files changed, 416 insertions(+), 41 deletions(-)
(limited to 'std/atomic')
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index 6dabddaefb..d17fc94c82 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
+const event = std.event;
const os = std.os;
const io = std.io;
const mem = std.mem;
@@ -43,6 +44,9 @@ const Command = struct {
};
pub fn main() !void {
+ // This allocator needs to be thread-safe because we use it for the event.Loop
+ // which multiplexes coroutines onto kernel threads.
+ // libc allocator is guaranteed to have this property.
const allocator = std.heap.c_allocator;
var stdout_file = try std.io.getStdOut();
@@ -380,8 +384,10 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);
defer allocator.free(zig_lib_dir);
+ var loop = try event.Loop.init(allocator);
+
var module = try Module.create(
- allocator,
+ &loop,
root_name,
root_source_file,
Target.Native,
@@ -471,9 +477,35 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
module.emit_file_type = emit_type;
module.link_objects = link_objects;
module.assembly_files = assembly_files;
+ module.link_out_file = flags.single("out-file");
try module.build();
- try module.link(flags.single("out-file"));
+ const process_build_events_handle = try async processBuildEvents(module, true);
+ defer cancel process_build_events_handle;
+ loop.run();
+}
+
+async fn processBuildEvents(module: *Module, watch: bool) void {
+ while (watch) {
+ // TODO directly awaiting async should guarantee memory allocation elision
+ const build_event = await (async module.events.get() catch unreachable);
+
+ switch (build_event) {
+ Module.Event.Ok => {
+ std.debug.warn("Build succeeded\n");
+ // for now we stop after 1
+ module.loop.stop();
+ return;
+ },
+ Module.Event.Error => |err| {
+ std.debug.warn("build failed: {}\n", @errorName(err));
+ @panic("TODO error return trace");
+ },
+ Module.Event.Fail => |errs| {
+ @panic("TODO print compile error messages");
+ },
+ }
+ }
}
fn cmdBuildExe(allocator: *Allocator, args: []const []const u8) !void {
@@ -780,4 +812,3 @@ const CliPkg = struct {
self.children.deinit();
}
};
-
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index 4da46cd38c..4fac760790 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -11,9 +11,11 @@ const warn = std.debug.warn;
const Token = std.zig.Token;
const ArrayList = std.ArrayList;
const errmsg = @import("errmsg.zig");
+const ast = std.zig.ast;
+const event = std.event;
pub const Module = struct {
- allocator: *mem.Allocator,
+ loop: *event.Loop,
name: Buffer,
root_src_path: ?[]const u8,
module: llvm.ModuleRef,
@@ -76,6 +78,50 @@ pub const Module = struct {
kind: Kind,
+ link_out_file: ?[]const u8,
+ events: *event.Channel(Event),
+
+ // TODO handle some of these earlier and report them in a way other than error codes
+ pub const BuildError = error{
+ OutOfMemory,
+ EndOfStream,
+ BadFd,
+ Io,
+ IsDir,
+ Unexpected,
+ SystemResources,
+ SharingViolation,
+ PathAlreadyExists,
+ FileNotFound,
+ AccessDenied,
+ PipeBusy,
+ FileTooBig,
+ SymLinkLoop,
+ ProcessFdQuotaExceeded,
+ NameTooLong,
+ SystemFdQuotaExceeded,
+ NoDevice,
+ PathNotFound,
+ NoSpaceLeft,
+ NotDir,
+ FileSystem,
+ OperationAborted,
+ IoPending,
+ BrokenPipe,
+ WouldBlock,
+ FileClosed,
+ DestinationAddressRequired,
+ DiskQuota,
+ InputOutput,
+ NoStdHandles,
+ };
+
+ pub const Event = union(enum) {
+ Ok,
+ Fail: []errmsg.Msg,
+ Error: BuildError,
+ };
+
pub const DarwinVersionMin = union(enum) {
None,
MacOS: []const u8,
@@ -104,7 +150,7 @@ pub const Module = struct {
};
pub fn create(
- allocator: *mem.Allocator,
+ loop: *event.Loop,
name: []const u8,
root_src_path: ?[]const u8,
target: *const Target,
@@ -113,7 +159,7 @@ pub const Module = struct {
zig_lib_dir: []const u8,
cache_dir: []const u8,
) !*Module {
- var name_buffer = try Buffer.init(allocator, name);
+ var name_buffer = try Buffer.init(loop.allocator, name);
errdefer name_buffer.deinit();
const context = c.LLVMContextCreate() orelse return error.OutOfMemory;
@@ -125,8 +171,12 @@ pub const Module = struct {
const builder = c.LLVMCreateBuilderInContext(context) orelse return error.OutOfMemory;
errdefer c.LLVMDisposeBuilder(builder);
- const module_ptr = try allocator.create(Module{
- .allocator = allocator,
+ const events = try event.Channel(Event).create(loop, 0);
+ errdefer events.destroy();
+
+ return loop.allocator.create(Module{
+ .loop = loop,
+ .events = events,
.name = name_buffer,
.root_src_path = root_src_path,
.module = module,
@@ -171,7 +221,7 @@ pub const Module = struct {
.link_objects = [][]const u8{},
.windows_subsystem_windows = false,
.windows_subsystem_console = false,
- .link_libs_list = ArrayList(*LinkLib).init(allocator),
+ .link_libs_list = ArrayList(*LinkLib).init(loop.allocator),
.libc_link_lib = null,
.err_color = errmsg.Color.Auto,
.darwin_frameworks = [][]const u8{},
@@ -179,9 +229,8 @@ pub const Module = struct {
.test_filters = [][]const u8{},
.test_name_prefix = null,
.emit_file_type = Emit.Binary,
+ .link_out_file = null,
});
- errdefer allocator.destroy(module_ptr);
- return module_ptr;
}
fn dump(self: *Module) void {
@@ -189,58 +238,70 @@ pub const Module = struct {
}
pub fn destroy(self: *Module) void {
+ self.events.destroy();
c.LLVMDisposeBuilder(self.builder);
c.LLVMDisposeModule(self.module);
c.LLVMContextDispose(self.context);
self.name.deinit();
- self.allocator.destroy(self);
+ self.a().destroy(self);
}
pub fn build(self: *Module) !void {
if (self.llvm_argv.len != 0) {
- var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.allocator, [][]const []const u8{
+ var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.a(), [][]const []const u8{
[][]const u8{"zig (LLVM option parsing)"},
self.llvm_argv,
});
defer c_compatible_args.deinit();
+ // TODO this sets global state
c.ZigLLVMParseCommandLineOptions(self.llvm_argv.len + 1, c_compatible_args.ptr);
}
+ _ = try async self.buildAsync();
+ }
+
+ async fn buildAsync(self: *Module) void {
+ while (true) {
+ // TODO directly awaiting async should guarantee memory allocation elision
+ // TODO also async before suspending should guarantee memory allocation elision
+ (await (async self.addRootSrc() catch unreachable)) catch |err| {
+ await (async self.events.put(Event{ .Error = err }) catch unreachable);
+ return;
+ };
+ await (async self.events.put(Event.Ok) catch unreachable);
+ }
+ }
+
+ async fn addRootSrc(self: *Module) !void {
const root_src_path = self.root_src_path orelse @panic("TODO handle null root src path");
- const root_src_real_path = os.path.real(self.allocator, root_src_path) catch |err| {
+ const root_src_real_path = os.path.real(self.a(), root_src_path) catch |err| {
try printError("unable to get real path '{}': {}", root_src_path, err);
return err;
};
- errdefer self.allocator.free(root_src_real_path);
+ errdefer self.a().free(root_src_real_path);
- const source_code = io.readFileAlloc(self.allocator, root_src_real_path) catch |err| {
+ const source_code = io.readFileAlloc(self.a(), root_src_real_path) catch |err| {
try printError("unable to open '{}': {}", root_src_real_path, err);
return err;
};
- errdefer self.allocator.free(source_code);
-
- warn("====input:====\n");
-
- warn("{}", source_code);
+ errdefer self.a().free(source_code);
- warn("====parse:====\n");
-
- var tree = try std.zig.parse(self.allocator, source_code);
+ var tree = try std.zig.parse(self.a(), source_code);
defer tree.deinit();
- var stderr_file = try std.io.getStdErr();
- var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
- const out_stream = &stderr_file_out_stream.stream;
-
- warn("====fmt:====\n");
- _ = try std.zig.render(self.allocator, out_stream, &tree);
-
- warn("====ir:====\n");
- warn("TODO\n\n");
-
- warn("====llvm ir:====\n");
- self.dump();
+ //var it = tree.root_node.decls.iterator();
+ //while (it.next()) |decl_ptr| {
+ // const decl = decl_ptr.*;
+ // switch (decl.id) {
+ // ast.Node.Comptime => @panic("TODO"),
+ // ast.Node.VarDecl => @panic("TODO"),
+ // ast.Node.UseDecl => @panic("TODO"),
+ // ast.Node.FnDef => @panic("TODO"),
+ // ast.Node.TestDecl => @panic("TODO"),
+ // else => unreachable,
+ // }
+ //}
}
pub fn link(self: *Module, out_file: ?[]const u8) !void {
@@ -263,11 +324,11 @@ pub const Module = struct {
}
}
- const link_lib = try self.allocator.create(LinkLib{
+ const link_lib = try self.a().create(LinkLib{
.name = name,
.path = null,
.provided_explicitly = provided_explicitly,
- .symbols = ArrayList([]u8).init(self.allocator),
+ .symbols = ArrayList([]u8).init(self.a()),
});
try self.link_libs_list.append(link_lib);
if (is_libc) {
@@ -275,6 +336,10 @@ pub const Module = struct {
}
return link_lib;
}
+
+ fn a(self: Module) *mem.Allocator {
+ return self.loop.allocator;
+ }
};
fn printError(comptime format: []const u8, args: ...) !void {
diff --git a/std/atomic/queue_mpsc.zig b/std/atomic/queue_mpsc.zig
index 66eb4573df..8030565d7a 100644
--- a/std/atomic/queue_mpsc.zig
+++ b/std/atomic/queue_mpsc.zig
@@ -1,4 +1,4 @@
-const std = @import("std");
+const std = @import("../index.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
const AtomicOrder = builtin.AtomicOrder;
diff --git a/std/event.zig b/std/event.zig
index 0821c789b7..7f823bc732 100644
--- a/std/event.zig
+++ b/std/event.zig
@@ -4,6 +4,8 @@ const assert = std.debug.assert;
const event = this;
const mem = std.mem;
const posix = std.os.posix;
+const AtomicRmwOp = builtin.AtomicRmwOp;
+const AtomicOrder = builtin.AtomicOrder;
pub const TcpServer = struct {
handleRequestFn: async<*mem.Allocator> fn (*TcpServer, *const std.net.Address, *const std.os.File) void,
@@ -95,16 +97,29 @@ pub const Loop = struct {
allocator: *mem.Allocator,
epollfd: i32,
keep_running: bool,
+ next_tick_queue: std.atomic.QueueMpsc(promise),
- fn init(allocator: *mem.Allocator) !Loop {
+ pub const NextTickNode = std.atomic.QueueMpsc(promise).Node;
+
+ /// The allocator must be thread-safe because we use it for multiplexing
+ /// coroutines onto kernel threads.
+ pub fn init(allocator: *mem.Allocator) !Loop {
const epollfd = try std.os.linuxEpollCreate(std.os.linux.EPOLL_CLOEXEC);
+ errdefer std.os.close(epollfd);
+
return Loop{
.keep_running = true,
.allocator = allocator,
.epollfd = epollfd,
+ .next_tick_queue = std.atomic.QueueMpsc(promise).init(),
};
}
+ /// must call stop before deinit
+ pub fn deinit(self: *Loop) void {
+ std.os.close(self.epollfd);
+ }
+
pub fn addFd(self: *Loop, fd: i32, prom: promise) !void {
var ev = std.os.linux.epoll_event{
.events = std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT | std.os.linux.EPOLLET,
@@ -126,11 +141,21 @@ pub const Loop = struct {
pub fn stop(self: *Loop) void {
// TODO make atomic
self.keep_running = false;
- // TODO activate an fd in the epoll set
+ // TODO activate an fd in the epoll set which should cancel all the promises
+ }
+
+ /// bring your own linked list node. this means it can't fail.
+ pub fn onNextTick(self: *Loop, node: *NextTickNode) void {
+ self.next_tick_queue.put(node);
}
pub fn run(self: *Loop) void {
while (self.keep_running) {
+ // TODO multiplex the next tick queue and the epoll event results onto a thread pool
+ while (self.next_tick_queue.get()) |node| {
+ resume node.data;
+ }
+ if (!self.keep_running) break;
var events: [16]std.os.linux.epoll_event = undefined;
const count = std.os.linuxEpollWait(self.epollfd, events[0..], -1);
for (events[0..count]) |ev| {
@@ -141,6 +166,215 @@ pub const Loop = struct {
}
};
+/// many producer, many consumer, thread-safe, lock-free, runtime configurable buffer size
+/// when buffer is empty, consumers suspend and are resumed by producers
+/// when buffer is full, producers suspend and are resumed by consumers
+pub fn Channel(comptime T: type) type {
+ return struct {
+ loop: *Loop,
+
+ getters: std.atomic.QueueMpsc(GetNode),
+ putters: std.atomic.QueueMpsc(PutNode),
+ get_count: usize,
+ put_count: usize,
+ dispatch_lock: u8, // TODO make this a bool
+ need_dispatch: u8, // TODO make this a bool
+
+ // simple fixed size ring buffer
+ buffer_nodes: []T,
+ buffer_index: usize,
+ buffer_len: usize,
+
+ const SelfChannel = this;
+ const GetNode = struct {
+ ptr: *T,
+ tick_node: *Loop.NextTickNode,
+ };
+ const PutNode = struct {
+ data: T,
+ tick_node: *Loop.NextTickNode,
+ };
+
+ /// call destroy when done
+ pub fn create(loop: *Loop, capacity: usize) !*SelfChannel {
+ const buffer_nodes = try loop.allocator.alloc(T, capacity);
+ errdefer loop.allocator.free(buffer_nodes);
+
+ const self = try loop.allocator.create(SelfChannel{
+ .loop = loop,
+ .buffer_len = 0,
+ .buffer_nodes = buffer_nodes,
+ .buffer_index = 0,
+ .dispatch_lock = 0,
+ .need_dispatch = 0,
+ .getters = std.atomic.QueueMpsc(GetNode).init(),
+ .putters = std.atomic.QueueMpsc(PutNode).init(),
+ .get_count = 0,
+ .put_count = 0,
+ });
+ errdefer loop.allocator.destroy(self);
+
+ return self;
+ }
+
+ /// must be called when all calls to put and get have suspended and no more calls occur
+ pub fn destroy(self: *SelfChannel) void {
+ while (self.getters.get()) |get_node| {
+ cancel get_node.data.tick_node.data;
+ }
+ while (self.putters.get()) |put_node| {
+ cancel put_node.data.tick_node.data;
+ }
+ self.loop.allocator.free(self.buffer_nodes);
+ self.loop.allocator.destroy(self);
+ }
+
+ /// puts a data item in the channel. The promise completes when the value has been added to the
+ /// buffer, or in the case of a zero size buffer, when the item has been retrieved by a getter.
+ pub async fn put(self: *SelfChannel, data: T) void {
+ // TODO should be able to group memory allocation failure before first suspend point
+ // so that the async invocation catches it
+ var dispatch_tick_node_ptr: *Loop.NextTickNode = undefined;
+ _ = async self.dispatch(&dispatch_tick_node_ptr) catch unreachable;
+
+ suspend |handle| {
+ var my_tick_node = Loop.NextTickNode{
+ .next = undefined,
+ .data = handle,
+ };
+ var queue_node = std.atomic.QueueMpsc(PutNode).Node{
+ .data = PutNode{
+ .tick_node = &my_tick_node,
+ .data = data,
+ },
+ .next = undefined,
+ };
+ self.putters.put(&queue_node);
+ _ = @atomicRmw(usize, &self.put_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+
+ self.loop.onNextTick(dispatch_tick_node_ptr);
+ }
+ }
+
+ /// await this function to get an item from the channel. If the buffer is empty, the promise will
+ /// complete when the next item is put in the channel.
+ pub async fn get(self: *SelfChannel) T {
+ // TODO should be able to group memory allocation failure before first suspend point
+ // so that the async invocation catches it
+ var dispatch_tick_node_ptr: *Loop.NextTickNode = undefined;
+ _ = async self.dispatch(&dispatch_tick_node_ptr) catch unreachable;
+
+ // TODO integrate this function with named return values
+ // so we can get rid of this extra result copy
+ var result: T = undefined;
+ var debug_handle: usize = undefined;
+ suspend |handle| {
+ debug_handle = @ptrToInt(handle);
+ var my_tick_node = Loop.NextTickNode{
+ .next = undefined,
+ .data = handle,
+ };
+ var queue_node = std.atomic.QueueMpsc(GetNode).Node{
+ .data = GetNode{
+ .ptr = &result,
+ .tick_node = &my_tick_node,
+ },
+ .next = undefined,
+ };
+ self.getters.put(&queue_node);
+ _ = @atomicRmw(usize, &self.get_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+
+ self.loop.onNextTick(dispatch_tick_node_ptr);
+ }
+ return result;
+ }
+
+ async fn dispatch(self: *SelfChannel, tick_node_ptr: **Loop.NextTickNode) void {
+ // resumed by onNextTick
+ suspend |handle| {
+ var tick_node = Loop.NextTickNode{
+ .data = handle,
+ .next = undefined,
+ };
+ tick_node_ptr.* = &tick_node;
+ }
+
+ // set the "need dispatch" flag
+ _ = @atomicRmw(u8, &self.need_dispatch, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+
+ lock: while (true) {
+ // set the lock flag
+ const prev_lock = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ if (prev_lock != 0) return;
+
+ // clear the need_dispatch flag since we're about to do it
+ _ = @atomicRmw(u8, &self.need_dispatch, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+
+ while (true) {
+ one_dispatch: {
+ // later we correct these extra subtractions
+ var get_count = @atomicRmw(usize, &self.get_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ var put_count = @atomicRmw(usize, &self.put_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+
+ // transfer self.buffer to self.getters
+ while (self.buffer_len != 0) {
+ if (get_count == 0) break :one_dispatch;
+
+ const get_node = &self.getters.get().?.data;
+ get_node.ptr.* = self.buffer_nodes[self.buffer_index -% self.buffer_len];
+ self.loop.onNextTick(get_node.tick_node);
+ self.buffer_len -= 1;
+
+ get_count = @atomicRmw(usize, &self.get_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+
+ // direct transfer self.putters to self.getters
+ while (get_count != 0 and put_count != 0) {
+ const get_node = &self.getters.get().?.data;
+ const put_node = &self.putters.get().?.data;
+
+ get_node.ptr.* = put_node.data;
+ self.loop.onNextTick(get_node.tick_node);
+ self.loop.onNextTick(put_node.tick_node);
+
+ get_count = @atomicRmw(usize, &self.get_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ put_count = @atomicRmw(usize, &self.put_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+
+ // transfer self.putters to self.buffer
+ while (self.buffer_len != self.buffer_nodes.len and put_count != 0) {
+ const put_node = &self.putters.get().?.data;
+
+ self.buffer_nodes[self.buffer_index] = put_node.data;
+ self.loop.onNextTick(put_node.tick_node);
+ self.buffer_index +%= 1;
+ self.buffer_len += 1;
+
+ put_count = @atomicRmw(usize, &self.put_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+ }
+
+ // undo the extra subtractions
+ _ = @atomicRmw(usize, &self.get_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+ _ = @atomicRmw(usize, &self.put_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+
+ // clear need-dispatch flag
+ const need_dispatch = @atomicRmw(u8, &self.need_dispatch, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ if (need_dispatch != 0) continue;
+
+ const my_lock = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ assert(my_lock != 0);
+
+ // we have to check again now that we unlocked
+ if (@atomicLoad(u8, &self.need_dispatch, AtomicOrder.SeqCst) != 0) continue :lock;
+
+ return;
+ }
+ }
+ }
+ };
+}
+
pub async fn connect(loop: *Loop, _address: *const std.net.Address) !std.os.File {
var address = _address.*; // TODO https://github.com/ziglang/zig/issues/733
@@ -199,6 +433,7 @@ test "listen on a port, send bytes, receive bytes" {
defer cancel p;
loop.run();
}
+
async fn doAsyncTest(loop: *Loop, address: *const std.net.Address) void {
errdefer @panic("test failure");
@@ -211,3 +446,43 @@ async fn doAsyncTest(loop: *Loop, address: *const std.net.Address) void {
assert(mem.eql(u8, msg, "hello from server\n"));
loop.stop();
}
+
+test "std.event.Channel" {
+ var da = std.heap.DirectAllocator.init();
+ defer da.deinit();
+
+ const allocator = &da.allocator;
+
+ var loop = try Loop.init(allocator);
+ defer loop.deinit();
+
+ const channel = try Channel(i32).create(&loop, 0);
+ defer channel.destroy();
+
+ const handle = try async testChannelGetter(&loop, channel);
+ defer cancel handle;
+
+ const putter = try async testChannelPutter(channel);
+ defer cancel putter;
+
+ loop.run();
+}
+
+async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void {
+ errdefer @panic("test failed");
+
+ const value1_promise = try async channel.get();
+ const value1 = await value1_promise;
+ assert(value1 == 1234);
+
+ const value2_promise = try async channel.get();
+ const value2 = await value2_promise;
+ assert(value2 == 4567);
+
+ loop.stop();
+}
+
+async fn testChannelPutter(channel: *Channel(i32)) void {
+ await (async channel.put(1234) catch @panic("out of memory"));
+ await (async channel.put(4567) catch @panic("out of memory"));
+}
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index bf12e86fef..c3c17f5322 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -130,6 +130,9 @@ pub fn formatType(
try output(context, "error.");
return output(context, @errorName(value));
},
+ builtin.TypeId.Promise => {
+ return format(context, Errors, output, "promise@{x}", @ptrToInt(value));
+ },
builtin.TypeId.Pointer => |ptr_info| switch (ptr_info.size) {
builtin.TypeInfo.Pointer.Size.One => switch (@typeInfo(ptr_info.child)) {
builtin.TypeId.Array => |info| {
diff --git a/std/heap.zig b/std/heap.zig
index 41d7802fdd..2e02733da1 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -38,6 +38,7 @@ fn cFree(self: *Allocator, old_mem: []u8) void {
}
/// This allocator makes a syscall directly for every allocation and free.
+/// TODO make this thread-safe. The windows implementation will need some atomics.
pub const DirectAllocator = struct {
allocator: Allocator,
heap_handle: ?HeapHandle,
--
cgit v1.2.3
From eb326e15530dd6dca4ccbe7dbfde7bf048de813e Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Thu, 5 Jul 2018 15:09:02 -0400
Subject: M:N threading
* add std.atomic.QueueMpsc.isEmpty
* make std.debug.global_allocator thread-safe
* std.event.Loop: now you have to choose between
- initSingleThreaded
- initMultiThreaded
* std.event.Loop multiplexes coroutines onto kernel threads
* Remove std.event.Loop.stop. Instead the event loop run() function
returns once there are no pending coroutines.
* fix crash in ir.cpp for calling methods under some conditions
* small progress self-hosted compiler, analyzing top level declarations
* Introduce std.event.Lock for synchronizing coroutines
* introduce std.event.Locked(T) for data that only 1 coroutine should
modify at once.
* make the self hosted compiler use multi threaded event loop
* make std.heap.DirectAllocator thread-safe
See #174
TODO:
* call sched_getaffinity instead of hard coding thread pool size 4
* support for Windows and MacOS
* #1194
* #1197
---
src-self-hosted/main.zig | 5 +-
src-self-hosted/module.zig | 257 ++++++++++++++++++--
src/ir.cpp | 2 +-
std/atomic/queue_mpsc.zig | 17 ++
std/debug/index.zig | 7 +-
std/event.zig | 580 +++++++++++++++++++++++++++++++++++++++------
std/heap.zig | 30 +--
std/mem.zig | 2 +-
std/os/index.zig | 39 ++-
std/os/linux/index.zig | 8 +
10 files changed, 833 insertions(+), 114 deletions(-)
(limited to 'std/atomic')
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index d17fc94c82..fe94a4460a 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -384,7 +384,8 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);
defer allocator.free(zig_lib_dir);
- var loop = try event.Loop.init(allocator);
+ var loop: event.Loop = undefined;
+ try loop.initMultiThreaded(allocator);
var module = try Module.create(
&loop,
@@ -493,8 +494,6 @@ async fn processBuildEvents(module: *Module, watch: bool) void {
switch (build_event) {
Module.Event.Ok => {
std.debug.warn("Build succeeded\n");
- // for now we stop after 1
- module.loop.stop();
return;
},
Module.Event.Error => |err| {
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index cf27c826c8..5ce1a7965a 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -2,6 +2,7 @@ const std = @import("std");
const os = std.os;
const io = std.io;
const mem = std.mem;
+const Allocator = mem.Allocator;
const Buffer = std.Buffer;
const llvm = @import("llvm.zig");
const c = @import("c.zig");
@@ -13,6 +14,7 @@ const ArrayList = std.ArrayList;
const errmsg = @import("errmsg.zig");
const ast = std.zig.ast;
const event = std.event;
+const assert = std.debug.assert;
pub const Module = struct {
loop: *event.Loop,
@@ -81,6 +83,8 @@ pub const Module = struct {
link_out_file: ?[]const u8,
events: *event.Channel(Event),
+ exported_symbol_names: event.Locked(Decl.Table),
+
// TODO handle some of these earlier and report them in a way other than error codes
pub const BuildError = error{
OutOfMemory,
@@ -232,6 +236,7 @@ pub const Module = struct {
.test_name_prefix = null,
.emit_file_type = Emit.Binary,
.link_out_file = null,
+ .exported_symbol_names = event.Locked(Decl.Table).init(loop, Decl.Table.init(loop.allocator)),
});
}
@@ -272,38 +277,91 @@ pub const Module = struct {
return;
};
await (async self.events.put(Event.Ok) catch unreachable);
+ // for now we stop after 1
+ return;
}
}
async fn addRootSrc(self: *Module) !void {
const root_src_path = self.root_src_path orelse @panic("TODO handle null root src path");
+ // TODO async/await os.path.real
const root_src_real_path = os.path.real(self.a(), root_src_path) catch |err| {
try printError("unable to get real path '{}': {}", root_src_path, err);
return err;
};
errdefer self.a().free(root_src_real_path);
+ // TODO async/await readFileAlloc()
const source_code = io.readFileAlloc(self.a(), root_src_real_path) catch |err| {
try printError("unable to open '{}': {}", root_src_real_path, err);
return err;
};
errdefer self.a().free(source_code);
- var tree = try std.zig.parse(self.a(), source_code);
- defer tree.deinit();
-
- //var it = tree.root_node.decls.iterator();
- //while (it.next()) |decl_ptr| {
- // const decl = decl_ptr.*;
- // switch (decl.id) {
- // ast.Node.Comptime => @panic("TODO"),
- // ast.Node.VarDecl => @panic("TODO"),
- // ast.Node.UseDecl => @panic("TODO"),
- // ast.Node.FnDef => @panic("TODO"),
- // ast.Node.TestDecl => @panic("TODO"),
- // else => unreachable,
- // }
- //}
+ var parsed_file = ParsedFile{
+ .tree = try std.zig.parse(self.a(), source_code),
+ .realpath = root_src_real_path,
+ };
+ errdefer parsed_file.tree.deinit();
+
+ const tree = &parsed_file.tree;
+
+ // create empty struct for it
+ const decls = try Scope.Decls.create(self.a(), null);
+ errdefer decls.destroy();
+
+ var it = tree.root_node.decls.iterator(0);
+ while (it.next()) |decl_ptr| {
+ const decl = decl_ptr.*;
+ switch (decl.id) {
+ ast.Node.Id.Comptime => @panic("TODO"),
+ ast.Node.Id.VarDecl => @panic("TODO"),
+ ast.Node.Id.FnProto => {
+ const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
+
+ const name = if (fn_proto.name_token) |name_token| tree.tokenSlice(name_token) else {
+ @panic("TODO add compile error");
+ //try self.addCompileError(
+ // &parsed_file,
+ // fn_proto.fn_token,
+ // fn_proto.fn_token + 1,
+ // "missing function name",
+ //);
+ continue;
+ };
+
+ const fn_decl = try self.a().create(Decl.Fn{
+ .base = Decl{
+ .id = Decl.Id.Fn,
+ .name = name,
+ .visib = parseVisibToken(tree, fn_proto.visib_token),
+ .resolution = Decl.Resolution.Unresolved,
+ },
+ .value = Decl.Fn.Val{ .Unresolved = {} },
+ .fn_proto = fn_proto,
+ });
+ errdefer self.a().destroy(fn_decl);
+
+ // TODO make this parallel
+ try await try async self.addTopLevelDecl(tree, &fn_decl.base);
+ },
+ ast.Node.Id.TestDecl => @panic("TODO"),
+ else => unreachable,
+ }
+ }
+ }
+
+ async fn addTopLevelDecl(self: *Module, tree: *ast.Tree, decl: *Decl) !void {
+ const is_export = decl.isExported(tree);
+
+ {
+ const exported_symbol_names = await try async self.exported_symbol_names.acquire();
+ defer exported_symbol_names.release();
+
+ if (try exported_symbol_names.value.put(decl.name, decl)) |other_decl| {
+ @panic("TODO report compile error");
+ }
+ }
}
pub fn link(self: *Module, out_file: ?[]const u8) !void {
@@ -350,3 +408,172 @@ fn printError(comptime format: []const u8, args: ...) !void {
const out_stream = &stderr_file_out_stream.stream;
try out_stream.print(format, args);
}
+
+fn parseVisibToken(tree: *ast.Tree, optional_token_index: ?ast.TokenIndex) Visib {
+ if (optional_token_index) |token_index| {
+ const token = tree.tokens.at(token_index);
+ assert(token.id == Token.Id.Keyword_pub);
+ return Visib.Pub;
+ } else {
+ return Visib.Private;
+ }
+}
+
+pub const Scope = struct {
+ id: Id,
+ parent: ?*Scope,
+
+ pub const Id = enum {
+ Decls,
+ Block,
+ };
+
+ pub const Decls = struct {
+ base: Scope,
+ table: Decl.Table,
+
+ pub fn create(a: *Allocator, parent: ?*Scope) !*Decls {
+ const self = try a.create(Decls{
+ .base = Scope{
+ .id = Id.Decls,
+ .parent = parent,
+ },
+ .table = undefined,
+ });
+ errdefer a.destroy(self);
+
+ self.table = Decl.Table.init(a);
+ errdefer self.table.deinit();
+
+ return self;
+ }
+
+ pub fn destroy(self: *Decls) void {
+ self.table.deinit();
+ self.table.allocator.destroy(self);
+ self.* = undefined;
+ }
+ };
+
+ pub const Block = struct {
+ base: Scope,
+ };
+};
+
+pub const Visib = enum {
+ Private,
+ Pub,
+};
+
+pub const Decl = struct {
+ id: Id,
+ name: []const u8,
+ visib: Visib,
+ resolution: Resolution,
+
+ pub const Table = std.HashMap([]const u8, *Decl, mem.hash_slice_u8, mem.eql_slice_u8);
+
+ pub fn isExported(base: *const Decl, tree: *ast.Tree) bool {
+ switch (base.id) {
+ Id.Fn => {
+ const fn_decl = @fieldParentPtr(Fn, "base", base);
+ return fn_decl.isExported(tree);
+ },
+ else => return false,
+ }
+ }
+
+ pub const Resolution = enum {
+ Unresolved,
+ InProgress,
+ Invalid,
+ Ok,
+ };
+
+ pub const Id = enum {
+ Var,
+ Fn,
+ CompTime,
+ };
+
+ pub const Var = struct {
+ base: Decl,
+ };
+
+ pub const Fn = struct {
+ base: Decl,
+ value: Val,
+ fn_proto: *const ast.Node.FnProto,
+
+ // TODO https://github.com/ziglang/zig/issues/683 and then make this anonymous
+ pub const Val = union {
+ Unresolved: void,
+ Ok: *Value.Fn,
+ };
+
+ pub fn externLibName(self: Fn, tree: *ast.Tree) ?[]const u8 {
+ return if (self.fn_proto.extern_export_inline_token) |tok_index| x: {
+ const token = tree.tokens.at(tok_index);
+ break :x switch (token.id) {
+ Token.Id.Extern => tree.tokenSlicePtr(token),
+ else => null,
+ };
+ } else null;
+ }
+
+ pub fn isExported(self: Fn, tree: *ast.Tree) bool {
+ if (self.fn_proto.extern_export_inline_token) |tok_index| {
+ const token = tree.tokens.at(tok_index);
+ return token.id == Token.Id.Keyword_export;
+ } else {
+ return false;
+ }
+ }
+ };
+
+ pub const CompTime = struct {
+ base: Decl,
+ };
+};
+
+pub const Value = struct {
+ pub const Fn = struct {};
+};
+
+pub const Type = struct {
+ id: Id,
+
+ pub const Id = enum {
+ Type,
+ Void,
+ Bool,
+ NoReturn,
+ Int,
+ Float,
+ Pointer,
+ Array,
+ Struct,
+ ComptimeFloat,
+ ComptimeInt,
+ Undefined,
+ Null,
+ Optional,
+ ErrorUnion,
+ ErrorSet,
+ Enum,
+ Union,
+ Fn,
+ Opaque,
+ Promise,
+ };
+
+ pub const Struct = struct {
+ base: Type,
+ decls: *Scope.Decls,
+ };
+};
+
+pub const ParsedFile = struct {
+ tree: ast.Tree,
+ realpath: []const u8,
+};
diff --git a/src/ir.cpp b/src/ir.cpp
index 98b1bd85ad..3fc8306339 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -13278,7 +13278,7 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
FnTableEntry *fn_table_entry = fn_ref->value.data.x_bound_fn.fn;
IrInstruction *first_arg_ptr = fn_ref->value.data.x_bound_fn.first_arg;
return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
- nullptr, first_arg_ptr, is_comptime, call_instruction->fn_inline);
+ fn_ref, first_arg_ptr, is_comptime, call_instruction->fn_inline);
} else {
ir_add_error_node(ira, fn_ref->source_node,
buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value.type->name)));
diff --git a/std/atomic/queue_mpsc.zig b/std/atomic/queue_mpsc.zig
index 8030565d7a..bc0a94258b 100644
--- a/std/atomic/queue_mpsc.zig
+++ b/std/atomic/queue_mpsc.zig
@@ -15,6 +15,8 @@ pub fn QueueMpsc(comptime T: type) type {
pub const Node = std.atomic.Stack(T).Node;
+ /// Not thread-safe. The call to init() must complete before any other functions are called.
+ /// No deinitialization required.
pub fn init() Self {
return Self{
.inboxes = []std.atomic.Stack(T){
@@ -26,12 +28,15 @@ pub fn QueueMpsc(comptime T: type) type {
};
}
+ /// Fully thread-safe. put() may be called from any thread at any time.
pub fn put(self: *Self, node: *Node) void {
const inbox_index = @atomicLoad(usize, &self.inbox_index, AtomicOrder.SeqCst);
const inbox = &self.inboxes[inbox_index];
inbox.push(node);
}
+ /// Must be called by only 1 consumer at a time. Every call to get() and isEmpty() must complete before
+ /// the next call to get().
pub fn get(self: *Self) ?*Node {
if (self.outbox.pop()) |node| {
return node;
@@ -43,6 +48,18 @@ pub fn QueueMpsc(comptime T: type) type {
}
return self.outbox.pop();
}
+
+ /// Must be called by only 1 consumer at a time. Every call to get() and isEmpty() must complete before
+ /// the next call to isEmpty().
+ pub fn isEmpty(self: *Self) bool {
+ if (!self.outbox.isEmpty()) return false;
+ const prev_inbox_index = @atomicRmw(usize, &self.inbox_index, AtomicRmwOp.Xor, 0x1, AtomicOrder.SeqCst);
+ const prev_inbox = &self.inboxes[prev_inbox_index];
+ while (prev_inbox.pop()) |node| {
+ self.outbox.push(node);
+ }
+ return self.outbox.isEmpty();
+ }
};
}
diff --git a/std/debug/index.zig b/std/debug/index.zig
index 57b2dfc300..a5e1c313f0 100644
--- a/std/debug/index.zig
+++ b/std/debug/index.zig
@@ -11,6 +11,11 @@ const builtin = @import("builtin");
pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator;
+pub const runtime_safety = switch (builtin.mode) {
+ builtin.Mode.Debug, builtin.Mode.ReleaseSafe => true,
+ builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => false,
+};
+
/// Tries to write to stderr, unbuffered, and ignores any error returned.
/// Does not append a newline.
/// TODO atomic/multithread support
@@ -1098,7 +1103,7 @@ fn readILeb128(in_stream: var) !i64 {
/// This should only be used in temporary test programs.
pub const global_allocator = &global_fixed_allocator.allocator;
-var global_fixed_allocator = std.heap.FixedBufferAllocator.init(global_allocator_mem[0..]);
+var global_fixed_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(global_allocator_mem[0..]);
var global_allocator_mem: [100 * 1024]u8 = undefined;
// TODO make thread safe
diff --git a/std/event.zig b/std/event.zig
index c6ac04a9d0..2d69d0cb16 100644
--- a/std/event.zig
+++ b/std/event.zig
@@ -11,53 +11,69 @@ pub const TcpServer = struct {
handleRequestFn: async<*mem.Allocator> fn (*TcpServer, *const std.net.Address, *const std.os.File) void,
loop: *Loop,
- sockfd: i32,
+ sockfd: ?i32,
accept_coro: ?promise,
listen_address: std.net.Address,
waiting_for_emfile_node: PromiseNode,
+ listen_resume_node: event.Loop.ResumeNode,
const PromiseNode = std.LinkedList(promise).Node;
- pub fn init(loop: *Loop) !TcpServer {
- const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
- errdefer std.os.close(sockfd);
-
+ pub fn init(loop: *Loop) TcpServer {
// TODO can't initialize handler coroutine here because we need well defined copy elision
return TcpServer{
.loop = loop,
- .sockfd = sockfd,
+ .sockfd = null,
.accept_coro = null,
.handleRequestFn = undefined,
.waiting_for_emfile_node = undefined,
.listen_address = undefined,
+ .listen_resume_node = event.Loop.ResumeNode{
+ .id = event.Loop.ResumeNode.Id.Basic,
+ .handle = undefined,
+ },
};
}
- pub fn listen(self: *TcpServer, address: *const std.net.Address, handleRequestFn: async<*mem.Allocator> fn (*TcpServer, *const std.net.Address, *const std.os.File) void) !void {
+ pub fn listen(
+ self: *TcpServer,
+ address: *const std.net.Address,
+ handleRequestFn: async<*mem.Allocator> fn (*TcpServer, *const std.net.Address, *const std.os.File) void,
+ ) !void {
self.handleRequestFn = handleRequestFn;
- try std.os.posixBind(self.sockfd, &address.os_addr);
- try std.os.posixListen(self.sockfd, posix.SOMAXCONN);
- self.listen_address = std.net.Address.initPosix(try std.os.posixGetSockName(self.sockfd));
+ const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
+ errdefer std.os.close(sockfd);
+ self.sockfd = sockfd;
+
+ try std.os.posixBind(sockfd, &address.os_addr);
+ try std.os.posixListen(sockfd, posix.SOMAXCONN);
+ self.listen_address = std.net.Address.initPosix(try std.os.posixGetSockName(sockfd));
self.accept_coro = try async TcpServer.handler(self);
errdefer cancel self.accept_coro.?;
- try self.loop.addFd(self.sockfd, self.accept_coro.?);
- errdefer self.loop.removeFd(self.sockfd);
+ self.listen_resume_node.handle = self.accept_coro.?;
+ try self.loop.addFd(sockfd, &self.listen_resume_node);
+ errdefer self.loop.removeFd(sockfd);
+ }
+
+ /// Stop listening
+ pub fn close(self: *TcpServer) void {
+ self.loop.removeFd(self.sockfd.?);
+ std.os.close(self.sockfd.?);
}
pub fn deinit(self: *TcpServer) void {
- self.loop.removeFd(self.sockfd);
if (self.accept_coro) |accept_coro| cancel accept_coro;
- std.os.close(self.sockfd);
+ if (self.sockfd) |sockfd| std.os.close(sockfd);
}
pub async fn handler(self: *TcpServer) void {
while (true) {
var accepted_addr: std.net.Address = undefined;
- if (std.os.posixAccept(self.sockfd, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
+ if (std.os.posixAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
var socket = std.os.File.openHandle(accepted_fd);
_ = async self.handleRequestFn(self, accepted_addr, socket) catch |err| switch (err) {
error.OutOfMemory => {
@@ -95,32 +111,65 @@ pub const TcpServer = struct {
pub const Loop = struct {
allocator: *mem.Allocator,
- keep_running: bool,
next_tick_queue: std.atomic.QueueMpsc(promise),
os_data: OsData,
+ dispatch_lock: u8, // TODO make this a bool
+ pending_event_count: usize,
+ extra_threads: []*std.os.Thread,
+ final_resume_node: ResumeNode,
- const OsData = switch (builtin.os) {
- builtin.Os.linux => struct {
- epollfd: i32,
- },
- else => struct {},
+ pub const NextTickNode = std.atomic.QueueMpsc(promise).Node;
+
+ pub const ResumeNode = struct {
+ id: Id,
+ handle: promise,
+
+ pub const Id = enum {
+ Basic,
+ Stop,
+ EventFd,
+ };
+
+ pub const EventFd = struct {
+ base: ResumeNode,
+ eventfd: i32,
+ };
};
- pub const NextTickNode = std.atomic.QueueMpsc(promise).Node;
+ /// After initialization, call run().
+ /// TODO copy elision / named return values so that the threads referencing *Loop
+ /// have the correct pointer value.
+ fn initSingleThreaded(self: *Loop, allocator: *mem.Allocator) !void {
+ return self.initInternal(allocator, 1);
+ }
/// The allocator must be thread-safe because we use it for multiplexing
/// coroutines onto kernel threads.
- pub fn init(allocator: *mem.Allocator) !Loop {
- var self = Loop{
- .keep_running = true,
+ /// After initialization, call run().
+ /// TODO copy elision / named return values so that the threads referencing *Loop
+ /// have the correct pointer value.
+ fn initMultiThreaded(self: *Loop, allocator: *mem.Allocator) !void {
+ // TODO check the actual cpu core count
+ return self.initInternal(allocator, 4);
+ }
+
+ /// Thread count is the total thread count. The thread pool size will be
+ /// max(thread_count - 1, 0)
+ fn initInternal(self: *Loop, allocator: *mem.Allocator, thread_count: usize) !void {
+ self.* = Loop{
+ .pending_event_count = 0,
.allocator = allocator,
.os_data = undefined,
.next_tick_queue = std.atomic.QueueMpsc(promise).init(),
+ .dispatch_lock = 1, // start locked so threads go directly into epoll wait
+ .extra_threads = undefined,
+ .final_resume_node = ResumeNode{
+ .id = ResumeNode.Id.Stop,
+ .handle = undefined,
+ },
};
- try self.initOsData();
+ try self.initOsData(thread_count);
errdefer self.deinitOsData();
-
- return self;
}
/// must call stop before deinit
@@ -128,13 +177,70 @@ pub const Loop = struct {
self.deinitOsData();
}
- const InitOsDataError = std.os.LinuxEpollCreateError;
+ const InitOsDataError = std.os.LinuxEpollCreateError || mem.Allocator.Error || std.os.LinuxEventFdError ||
+ std.os.SpawnThreadError || std.os.LinuxEpollCtlError;
+
+ const wakeup_bytes = []u8{0x1} ** 8;
- fn initOsData(self: *Loop) InitOsDataError!void {
+ fn initOsData(self: *Loop, thread_count: usize) InitOsDataError!void {
switch (builtin.os) {
builtin.Os.linux => {
- self.os_data.epollfd = try std.os.linuxEpollCreate(std.os.linux.EPOLL_CLOEXEC);
+ const extra_thread_count = thread_count - 1;
+ self.os_data.available_eventfd_resume_nodes = std.atomic.Stack(ResumeNode.EventFd).init();
+ self.os_data.eventfd_resume_nodes = try self.allocator.alloc(
+ std.atomic.Stack(ResumeNode.EventFd).Node,
+ extra_thread_count,
+ );
+ errdefer self.allocator.free(self.os_data.eventfd_resume_nodes);
+
+ errdefer {
+ while (self.os_data.available_eventfd_resume_nodes.pop()) |node| std.os.close(node.data.eventfd);
+ }
+ for (self.os_data.eventfd_resume_nodes) |*eventfd_node| {
+ eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{
+ .data = ResumeNode.EventFd{
+ .base = ResumeNode{
+ .id = ResumeNode.Id.EventFd,
+ .handle = undefined,
+ },
+ .eventfd = try std.os.linuxEventFd(1, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK),
+ },
+ .next = undefined,
+ };
+ self.os_data.available_eventfd_resume_nodes.push(eventfd_node);
+ }
+
+ self.os_data.epollfd = try std.os.linuxEpollCreate(posix.EPOLL_CLOEXEC);
errdefer std.os.close(self.os_data.epollfd);
+
+ self.os_data.final_eventfd = try std.os.linuxEventFd(0, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK);
+ errdefer std.os.close(self.os_data.final_eventfd);
+
+ self.os_data.final_eventfd_event = posix.epoll_event{
+ .events = posix.EPOLLIN,
+ .data = posix.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) },
+ };
+ try std.os.linuxEpollCtl(
+ self.os_data.epollfd,
+ posix.EPOLL_CTL_ADD,
+ self.os_data.final_eventfd,
+ &self.os_data.final_eventfd_event,
+ );
+ self.extra_threads = try self.allocator.alloc(*std.os.Thread, extra_thread_count);
+ errdefer self.allocator.free(self.extra_threads);
+
+ var extra_thread_index: usize = 0;
+ errdefer {
+ while (extra_thread_index != 0) {
+ extra_thread_index -= 1;
+ // writing 8 bytes to an eventfd cannot fail
+ std.os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable;
+ self.extra_threads[extra_thread_index].wait();
+ }
+ }
+ while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
+ self.extra_threads[extra_thread_index] = try std.os.spawnThread(self, workerRun);
+ }
},
else => {},
}
@@ -142,65 +248,154 @@ pub const Loop = struct {
fn deinitOsData(self: *Loop) void {
switch (builtin.os) {
- builtin.Os.linux => std.os.close(self.os_data.epollfd),
+ builtin.Os.linux => {
+ std.os.close(self.os_data.final_eventfd);
+ while (self.os_data.available_eventfd_resume_nodes.pop()) |node| std.os.close(node.data.eventfd);
+ std.os.close(self.os_data.epollfd);
+ self.allocator.free(self.os_data.eventfd_resume_nodes);
+ self.allocator.free(self.extra_threads);
+ },
else => {},
}
}
- pub fn addFd(self: *Loop, fd: i32, prom: promise) !void {
+ /// resume_node must live longer than the promise that it holds a reference to.
+ pub fn addFd(self: *Loop, fd: i32, resume_node: *ResumeNode) !void {
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+ errdefer {
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+ try self.addFdNoCounter(fd, resume_node);
+ }
+
+ fn addFdNoCounter(self: *Loop, fd: i32, resume_node: *ResumeNode) !void {
var ev = std.os.linux.epoll_event{
.events = std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT | std.os.linux.EPOLLET,
- .data = std.os.linux.epoll_data{ .ptr = @ptrToInt(prom) },
+ .data = std.os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) },
};
try std.os.linuxEpollCtl(self.os_data.epollfd, std.os.linux.EPOLL_CTL_ADD, fd, &ev);
}
pub fn removeFd(self: *Loop, fd: i32) void {
+ self.removeFdNoCounter(fd);
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+
+ fn removeFdNoCounter(self: *Loop, fd: i32) void {
std.os.linuxEpollCtl(self.os_data.epollfd, std.os.linux.EPOLL_CTL_DEL, fd, undefined) catch {};
}
- async fn waitFd(self: *Loop, fd: i32) !void {
+
+ pub async fn waitFd(self: *Loop, fd: i32) !void {
defer self.removeFd(fd);
+ var resume_node = ResumeNode{
+ .id = ResumeNode.Id.Basic,
+ .handle = undefined,
+ };
suspend |p| {
- try self.addFd(fd, p);
+ resume_node.handle = p;
+ try self.addFd(fd, &resume_node);
}
+ var a = &resume_node; // TODO better way to explicitly put memory in coro frame
}
- pub fn stop(self: *Loop) void {
- // TODO make atomic
- self.keep_running = false;
- // TODO activate an fd in the epoll set which should cancel all the promises
- }
-
- /// bring your own linked list node. this means it can't fail.
+ /// Bring your own linked list node. This means it can't fail.
pub fn onNextTick(self: *Loop, node: *NextTickNode) void {
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
self.next_tick_queue.put(node);
}
pub fn run(self: *Loop) void {
- while (self.keep_running) {
- // TODO multiplex the next tick queue and the epoll event results onto a thread pool
- while (self.next_tick_queue.get()) |node| {
- resume node.data;
- }
- if (!self.keep_running) break;
-
- self.dispatchOsEvents();
+ _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ self.workerRun();
+ for (self.extra_threads) |extra_thread| {
+ extra_thread.wait();
}
}
- fn dispatchOsEvents(self: *Loop) void {
- switch (builtin.os) {
- builtin.Os.linux => {
- var events: [16]std.os.linux.epoll_event = undefined;
- const count = std.os.linuxEpollWait(self.os_data.epollfd, events[0..], -1);
- for (events[0..count]) |ev| {
- const p = @intToPtr(promise, ev.data.ptr);
- resume p;
+ fn workerRun(self: *Loop) void {
+ start_over: while (true) {
+ if (@atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) == 0) {
+ while (self.next_tick_queue.get()) |next_tick_node| {
+ const handle = next_tick_node.data;
+ if (self.next_tick_queue.isEmpty()) {
+ // last node, just resume it
+ _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ resume handle;
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ continue :start_over;
+ }
+
+ // non-last node, stick it in the epoll set so that
+ // other threads can get to it
+ if (self.os_data.available_eventfd_resume_nodes.pop()) |resume_stack_node| {
+ const eventfd_node = &resume_stack_node.data;
+ eventfd_node.base.handle = handle;
+ // the pending count is already accounted for
+ self.addFdNoCounter(eventfd_node.eventfd, &eventfd_node.base) catch |_| {
+ // fine, we didn't need it anyway
+ _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ self.os_data.available_eventfd_resume_nodes.push(resume_stack_node);
+ resume handle;
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ continue :start_over;
+ };
+ } else {
+ // threads are too busy, can't add another eventfd to wake one up
+ _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ resume handle;
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ continue :start_over;
+ }
}
- },
- else => {},
+
+ const pending_event_count = @atomicLoad(usize, &self.pending_event_count, AtomicOrder.SeqCst);
+ if (pending_event_count == 0) {
+ // cause all the threads to stop
+ // writing 8 bytes to an eventfd cannot fail
+ std.os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable;
+ return;
+ }
+
+ _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ }
+
+ // only process 1 event so we don't steal from other threads
+ var events: [1]std.os.linux.epoll_event = undefined;
+ const count = std.os.linuxEpollWait(self.os_data.epollfd, events[0..], -1);
+ for (events[0..count]) |ev| {
+ const resume_node = @intToPtr(*ResumeNode, ev.data.ptr);
+ const handle = resume_node.handle;
+ const resume_node_id = resume_node.id;
+ switch (resume_node_id) {
+ ResumeNode.Id.Basic => {},
+ ResumeNode.Id.Stop => return,
+ ResumeNode.Id.EventFd => {
+ const event_fd_node = @fieldParentPtr(ResumeNode.EventFd, "base", resume_node);
+ self.removeFdNoCounter(event_fd_node.eventfd);
+ const stack_node = @fieldParentPtr(std.atomic.Stack(ResumeNode.EventFd).Node, "data", event_fd_node);
+ self.os_data.available_eventfd_resume_nodes.push(stack_node);
+ },
+ }
+ resume handle;
+ if (resume_node_id == ResumeNode.Id.EventFd) {
+ _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+ }
}
}
+
+ const OsData = switch (builtin.os) {
+ builtin.Os.linux => struct {
+ epollfd: i32,
+ // pre-allocated eventfds. all permanently active.
+ // this is how we send promises to be resumed on other threads.
+ available_eventfd_resume_nodes: std.atomic.Stack(ResumeNode.EventFd),
+ eventfd_resume_nodes: []std.atomic.Stack(ResumeNode.EventFd).Node,
+ final_eventfd: i32,
+ final_eventfd_event: posix.epoll_event,
+ },
+ else => struct {},
+ };
};
/// many producer, many consumer, thread-safe, lock-free, runtime configurable buffer size
@@ -304,9 +499,7 @@ pub fn Channel(comptime T: type) type {
// TODO integrate this function with named return values
// so we can get rid of this extra result copy
var result: T = undefined;
- var debug_handle: usize = undefined;
suspend |handle| {
- debug_handle = @ptrToInt(handle);
var my_tick_node = Loop.NextTickNode{
.next = undefined,
.data = handle,
@@ -438,9 +631,8 @@ test "listen on a port, send bytes, receive bytes" {
const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
defer socket.close();
- const next_handler = async errorableHandler(self, _addr, socket) catch |err| switch (err) {
- error.OutOfMemory => @panic("unable to handle connection: out of memory"),
- };
+ // TODO guarantee elision of this allocation
+ const next_handler = async errorableHandler(self, _addr, socket) catch unreachable;
(await next_handler) catch |err| {
std.debug.panic("unable to handle connection: {}\n", err);
};
@@ -461,17 +653,18 @@ test "listen on a port, send bytes, receive bytes" {
const ip4addr = std.net.parseIp4("127.0.0.1") catch unreachable;
const addr = std.net.Address.initIp4(ip4addr, 0);
- var loop = try Loop.init(std.debug.global_allocator);
- var server = MyServer{ .tcp_server = try TcpServer.init(&loop) };
+ var loop: Loop = undefined;
+ try loop.initSingleThreaded(std.debug.global_allocator);
+ var server = MyServer{ .tcp_server = TcpServer.init(&loop) };
defer server.tcp_server.deinit();
try server.tcp_server.listen(addr, MyServer.handler);
- const p = try async doAsyncTest(&loop, server.tcp_server.listen_address);
+ const p = try async doAsyncTest(&loop, server.tcp_server.listen_address, &server.tcp_server);
defer cancel p;
loop.run();
}
-async fn doAsyncTest(loop: *Loop, address: *const std.net.Address) void {
+async fn doAsyncTest(loop: *Loop, address: *const std.net.Address, server: *TcpServer) void {
errdefer @panic("test failure");
var socket_file = try await try async event.connect(loop, address);
@@ -481,7 +674,7 @@ async fn doAsyncTest(loop: *Loop, address: *const std.net.Address) void {
const amt_read = try socket_file.read(buf[0..]);
const msg = buf[0..amt_read];
assert(mem.eql(u8, msg, "hello from server\n"));
- loop.stop();
+ server.close();
}
test "std.event.Channel" {
@@ -490,7 +683,9 @@ test "std.event.Channel" {
const allocator = &da.allocator;
- var loop = try Loop.init(allocator);
+ var loop: Loop = undefined;
+ // TODO make a multi threaded test
+ try loop.initSingleThreaded(allocator);
defer loop.deinit();
const channel = try Channel(i32).create(&loop, 0);
@@ -515,11 +710,248 @@ async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void {
const value2_promise = try async channel.get();
const value2 = await value2_promise;
assert(value2 == 4567);
-
- loop.stop();
}
async fn testChannelPutter(channel: *Channel(i32)) void {
await (async channel.put(1234) catch @panic("out of memory"));
await (async channel.put(4567) catch @panic("out of memory"));
}
+
+/// Thread-safe async/await lock.
+/// Does not make any syscalls - coroutines which are waiting for the lock are suspended, and
+/// are resumed when the lock is released, in order.
+pub const Lock = struct {
+ loop: *Loop,
+ shared_bit: u8, // TODO make this a bool
+ queue: Queue,
+ queue_empty_bit: u8, // TODO make this a bool
+
+ const Queue = std.atomic.QueueMpsc(promise);
+
+ pub const Held = struct {
+ lock: *Lock,
+
+ pub fn release(self: Held) void {
+ // Resume the next item from the queue.
+ if (self.lock.queue.get()) |node| {
+ self.lock.loop.onNextTick(node);
+ return;
+ }
+
+ // We need to release the lock.
+ _ = @atomicRmw(u8, &self.lock.queue_empty_bit, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ _ = @atomicRmw(u8, &self.lock.shared_bit, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+
+ // There might be a queue item. If we know the queue is empty, we can be done,
+ // because the other actor will try to obtain the lock.
+ // But if there's a queue item, we are the actor which must loop and attempt
+ // to grab the lock again.
+ if (@atomicLoad(u8, &self.lock.queue_empty_bit, AtomicOrder.SeqCst) == 1) {
+ return;
+ }
+
+ while (true) {
+ const old_bit = @atomicRmw(u8, &self.lock.shared_bit, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ if (old_bit != 0) {
+ // We did not obtain the lock. Great, the queue is someone else's problem.
+ return;
+ }
+
+ // Resume the next item from the queue.
+ if (self.lock.queue.get()) |node| {
+ self.lock.loop.onNextTick(node);
+ return;
+ }
+
+ // Release the lock again.
+ _ = @atomicRmw(u8, &self.lock.queue_empty_bit, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ _ = @atomicRmw(u8, &self.lock.shared_bit, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+
+ // Find out if we can be done.
+ if (@atomicLoad(u8, &self.lock.queue_empty_bit, AtomicOrder.SeqCst) == 1) {
+ return;
+ }
+ }
+ }
+ };
+
+ pub fn init(loop: *Loop) Lock {
+ return Lock{
+ .loop = loop,
+ .shared_bit = 0,
+ .queue = Queue.init(),
+ .queue_empty_bit = 1,
+ };
+ }
+
+ /// Must be called when not locked. Not thread safe.
+ /// All calls to acquire() and release() must complete before calling deinit().
+ pub fn deinit(self: *Lock) void {
+ assert(self.shared_bit == 0);
+ while (self.queue.get()) |node| cancel node.data;
+ }
+
+ pub async fn acquire(self: *Lock) Held {
+ var my_tick_node: Loop.NextTickNode = undefined;
+
+ s: suspend |handle| {
+ my_tick_node.data = handle;
+ self.queue.put(&my_tick_node);
+
+ // At this point, we are in the queue, so we might have already been resumed and this coroutine
+ // frame might be destroyed. For the rest of the suspend block we cannot access the coroutine frame.
+
+ // We set this bit so that later we can rely on the fact, that if queue_empty_bit is 1, some actor
+ // will attempt to grab the lock.
+ _ = @atomicRmw(u8, &self.queue_empty_bit, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+
+ while (true) {
+ const old_bit = @atomicRmw(u8, &self.shared_bit, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ if (old_bit != 0) {
+ // We did not obtain the lock. Trust that our queue entry will resume us, and allow
+ // suspend to complete.
+ break;
+ }
+ // We got the lock. However we might have already been resumed from the queue.
+ if (self.queue.get()) |node| {
+ // Whether this node is us or someone else, we tail resume it.
+ resume node.data;
+ break;
+ } else {
+ // We already got resumed, and there are none left in the queue, which means that
+ // we aren't even supposed to hold the lock right now.
+ _ = @atomicRmw(u8, &self.queue_empty_bit, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ _ = @atomicRmw(u8, &self.shared_bit, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+
+ // There might be a queue item. If we know the queue is empty, we can be done,
+ // because the other actor will try to obtain the lock.
+ // But if there's a queue item, we are the actor which must loop and attempt
+ // to grab the lock again.
+ if (@atomicLoad(u8, &self.queue_empty_bit, AtomicOrder.SeqCst) == 1) {
+ break;
+ } else {
+ continue;
+ }
+ }
+ unreachable;
+ }
+ }
+
+ // TODO this workaround to force my_tick_node to be in the coroutine frame should
+ // not be necessary
+ var trash1 = &my_tick_node;
+
+ return Held{ .lock = self };
+ }
+};
+
+/// Thread-safe async/await lock that protects one piece of data.
+/// Does not make any syscalls - coroutines which are waiting for the lock are suspended, and
+/// are resumed when the lock is released, in order.
+pub fn Locked(comptime T: type) type {
+ return struct {
+ lock: Lock,
+ private_data: T,
+
+ const Self = this;
+
+ pub const HeldLock = struct {
+ value: *T,
+ held: Lock.Held,
+
+ pub fn release(self: HeldLock) void {
+ self.held.release();
+ }
+ };
+
+ pub fn init(loop: *Loop, data: T) Self {
+ return Self{
+ .lock = Lock.init(loop),
+ .private_data = data,
+ };
+ }
+
+ pub fn deinit(self: *Self) void {
+ self.lock.deinit();
+ }
+
+ pub async fn acquire(self: *Self) HeldLock {
+ return HeldLock{
+ // TODO guaranteed allocation elision
+ .held = await (async self.lock.acquire() catch unreachable),
+ .value = &self.private_data,
+ };
+ }
+ };
+}
+
+test "std.event.Lock" {
+ var da = std.heap.DirectAllocator.init();
+ defer da.deinit();
+
+ const allocator = &da.allocator;
+
+ var loop: Loop = undefined;
+ try loop.initMultiThreaded(allocator);
+ defer loop.deinit();
+
+ var lock = Lock.init(&loop);
+ defer lock.deinit();
+
+ const handle = try async testLock(&loop, &lock);
+ defer cancel handle;
+ loop.run();
+
+ assert(mem.eql(i32, shared_test_data, [1]i32{3 * 10} ** 10));
+}
+
+async fn testLock(loop: *Loop, lock: *Lock) void {
+ const handle1 = async lockRunner(lock) catch @panic("out of memory");
+ var tick_node1 = Loop.NextTickNode{
+ .next = undefined,
+ .data = handle1,
+ };
+ loop.onNextTick(&tick_node1);
+
+ const handle2 = async lockRunner(lock) catch @panic("out of memory");
+ var tick_node2 = Loop.NextTickNode{
+ .next = undefined,
+ .data = handle2,
+ };
+ loop.onNextTick(&tick_node2);
+
+ const handle3 = async lockRunner(lock) catch @panic("out of memory");
+ var tick_node3 = Loop.NextTickNode{
+ .next = undefined,
+ .data = handle3,
+ };
+ loop.onNextTick(&tick_node3);
+
+ await handle1;
+ await handle2;
+ await handle3;
+
+ // TODO this is to force tick node memory to be in the coro frame
+ // there should be a way to make it explicit where the memory is
+ var a = &tick_node1;
+ var b = &tick_node2;
+ var c = &tick_node3;
+}
+
+var shared_test_data = [1]i32{0} ** 10;
+var shared_test_index: usize = 0;
+
+async fn lockRunner(lock: *Lock) void {
+ suspend; // resumed by onNextTick
+
+ var i: usize = 0;
+ while (i < 10) : (i += 1) {
+ const handle = await (async lock.acquire() catch @panic("out of memory"));
+ defer handle.release();
+
+ shared_test_index = 0;
+ while (shared_test_index < shared_test_data.len) : (shared_test_index += 1) {
+ shared_test_data[shared_test_index] = shared_test_data[shared_test_index] + 1;
+ }
+ }
+}
diff --git a/std/heap.zig b/std/heap.zig
index 2e02733da1..bcace34afe 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -38,7 +38,7 @@ fn cFree(self: *Allocator, old_mem: []u8) void {
}
/// This allocator makes a syscall directly for every allocation and free.
-/// TODO make this thread-safe. The windows implementation will need some atomics.
+/// Thread-safe and lock-free.
pub const DirectAllocator = struct {
allocator: Allocator,
heap_handle: ?HeapHandle,
@@ -74,34 +74,34 @@ pub const DirectAllocator = struct {
const alloc_size = if (alignment <= os.page_size) n else n + alignment;
const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0);
if (addr == p.MAP_FAILED) return error.OutOfMemory;
-
if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n];
- var aligned_addr = addr & ~usize(alignment - 1);
- aligned_addr += alignment;
+ const aligned_addr = (addr & ~usize(alignment - 1)) + alignment;
- //We can unmap the unused portions of our mmap, but we must only
- // pass munmap bytes that exist outside our allocated pages or it
- // will happily eat us too
+ // We can unmap the unused portions of our mmap, but we must only
+ // pass munmap bytes that exist outside our allocated pages or it
+ // will happily eat us too.
- //Since alignment > page_size, we are by definition on a page boundry
+ // Since alignment > page_size, we are by definition on a page boundary.
const unused_start = addr;
const unused_len = aligned_addr - 1 - unused_start;
- var err = p.munmap(unused_start, unused_len);
- debug.assert(p.getErrno(err) == 0);
+ const err = p.munmap(unused_start, unused_len);
+ assert(p.getErrno(err) == 0);
- //It is impossible that there is an unoccupied page at the top of our
- // mmap.
+ // It is impossible that there is an unoccupied page at the top of our
+ // mmap.
return @intToPtr([*]u8, aligned_addr)[0..n];
},
Os.windows => {
const amt = n + alignment + @sizeOf(usize);
- const heap_handle = self.heap_handle orelse blk: {
+ const optional_heap_handle = @atomicLoad(?HeapHandle, ?self.heap_handle, builtin.AtomicOrder.SeqCst);
+ const heap_handle = optional_heap_handle orelse blk: {
const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) orelse return error.OutOfMemory;
- self.heap_handle = hh;
- break :blk hh;
+ const other_hh = @cmpxchgStrong(?HeapHandle, &self.heap_handle, null, hh, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) orelse break :blk hh;
+ _ = os.windows.HeapDestroy(hh);
+ break :blk other_hh;
};
const ptr = os.windows.HeapAlloc(heap_handle, 0, amt) orelse return error.OutOfMemory;
const root_addr = @ptrToInt(ptr);
diff --git a/std/mem.zig b/std/mem.zig
index b52d3e9f68..555e1e249d 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -6,7 +6,7 @@ const builtin = @import("builtin");
const mem = this;
pub const Allocator = struct {
- const Error = error{OutOfMemory};
+ pub const Error = error{OutOfMemory};
/// Allocate byte_count bytes and return them in a slice, with the
/// slice's pointer aligned at least to alignment bytes.
diff --git a/std/os/index.zig b/std/os/index.zig
index 52b36c351c..74a1b64f6e 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -2309,6 +2309,30 @@ pub fn linuxEpollWait(epfd: i32, events: []linux.epoll_event, timeout: i32) usiz
}
}
+pub const LinuxEventFdError = error{
+ InvalidFlagValue,
+ SystemResources,
+ ProcessFdQuotaExceeded,
+ SystemFdQuotaExceeded,
+
+ Unexpected,
+};
+
+pub fn linuxEventFd(initval: u32, flags: u32) LinuxEventFdError!i32 {
+ const rc = posix.eventfd(initval, flags);
+ const err = posix.getErrno(rc);
+ switch (err) {
+ 0 => return @intCast(i32, rc),
+ else => return unexpectedErrorPosix(err),
+
+ posix.EINVAL => return LinuxEventFdError.InvalidFlagValue,
+ posix.EMFILE => return LinuxEventFdError.ProcessFdQuotaExceeded,
+ posix.ENFILE => return LinuxEventFdError.SystemFdQuotaExceeded,
+ posix.ENODEV => return LinuxEventFdError.SystemResources,
+ posix.ENOMEM => return LinuxEventFdError.SystemResources,
+ }
+}
+
pub const PosixGetSockNameError = error{
/// Insufficient resources were available in the system to perform the operation.
SystemResources,
@@ -2605,10 +2629,17 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
const MainFuncs = struct {
extern fn linuxThreadMain(ctx_addr: usize) u8 {
- if (@sizeOf(Context) == 0) {
- return startFn({});
- } else {
- return startFn(@intToPtr(*const Context, ctx_addr).*);
+ const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*;
+
+ switch (@typeId(@typeOf(startFn).ReturnType)) {
+ builtin.TypeId.Int => {
+ return startFn(arg);
+ },
+ builtin.TypeId.Void => {
+ startFn(arg);
+ return 0;
+ },
+ else => @compileError("expected return type of startFn to be 'u8', 'noreturn', 'void', or '!void'"),
}
}
extern fn posixThreadMain(ctx: ?*c_void) ?*c_void {
diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig
index 65aa659c82..1c15be4887 100644
--- a/std/os/linux/index.zig
+++ b/std/os/linux/index.zig
@@ -523,6 +523,10 @@ pub const CLONE_NEWPID = 0x20000000;
pub const CLONE_NEWNET = 0x40000000;
pub const CLONE_IO = 0x80000000;
+pub const EFD_SEMAPHORE = 1;
+pub const EFD_CLOEXEC = O_CLOEXEC;
+pub const EFD_NONBLOCK = O_NONBLOCK;
+
pub const MS_RDONLY = 1;
pub const MS_NOSUID = 2;
pub const MS_NODEV = 4;
@@ -1221,6 +1225,10 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout
return syscall4(SYS_epoll_wait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout));
}
+pub fn eventfd(count: u32, flags: u32) usize {
+ return syscall2(SYS_eventfd2, count, flags);
+}
+
pub fn timerfd_create(clockid: i32, flags: u32) usize {
return syscall2(SYS_timerfd_create, @intCast(usize, clockid), @intCast(usize, flags));
}
--
cgit v1.2.3
From c89aac85c440ea4cbccf1abdbd6acf84a33077e3 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Mon, 9 Jul 2018 21:21:59 -0400
Subject: better workaround for guaranteeing memory in coroutine frame
See #1194
---
std/atomic/queue_mpsc.zig | 25 +++++++++++++++++++++++++
std/event.zig | 40 ++++++++++++++++++----------------------
2 files changed, 43 insertions(+), 22 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue_mpsc.zig b/std/atomic/queue_mpsc.zig
index bc0a94258b..978e189453 100644
--- a/std/atomic/queue_mpsc.zig
+++ b/std/atomic/queue_mpsc.zig
@@ -60,6 +60,31 @@ pub fn QueueMpsc(comptime T: type) type {
}
return self.outbox.isEmpty();
}
+
+ /// For debugging only. No API guarantees about what this does.
+ pub fn dump(self: *Self) void {
+ {
+ var it = self.outbox.root;
+ while (it) |node| {
+ std.debug.warn("0x{x} -> ", @ptrToInt(node));
+ it = node.next;
+ }
+ }
+ const inbox_index = self.inbox_index;
+ const inboxes = []*std.atomic.Stack(T){
+ &self.inboxes[self.inbox_index],
+ &self.inboxes[1 - self.inbox_index],
+ };
+ for (inboxes) |inbox| {
+ var it = inbox.root;
+ while (it) |node| {
+ std.debug.warn("0x{x} -> ", @ptrToInt(node));
+ it = node.next;
+ }
+ }
+
+ std.debug.warn("null\n");
+ }
};
}
diff --git a/std/event.zig b/std/event.zig
index a72db05e78..de51f8c87e 100644
--- a/std/event.zig
+++ b/std/event.zig
@@ -439,15 +439,14 @@ pub const Loop = struct {
pub async fn waitFd(self: *Loop, fd: i32) !void {
defer self.removeFd(fd);
- var resume_node = ResumeNode{
- .id = ResumeNode.Id.Basic,
- .handle = undefined,
- };
suspend |p| {
- resume_node.handle = p;
+ // TODO explicitly put this memory in the coroutine frame #1194
+ var resume_node = ResumeNode{
+ .id = ResumeNode.Id.Basic,
+ .handle = p,
+ };
try self.addFd(fd, &resume_node);
}
- var a = &resume_node; // TODO better way to explicitly put memory in coro frame
}
/// Bring your own linked list node. This means it can't fail.
@@ -618,8 +617,7 @@ pub const Loop = struct {
while (true) {
var nbytes: windows.DWORD = undefined;
var overlapped: ?*windows.OVERLAPPED = undefined;
- switch (std.os.windowsGetQueuedCompletionStatus(self.os_data.io_port, &nbytes, &completion_key,
- &overlapped, windows.INFINITE)) {
+ switch (std.os.windowsGetQueuedCompletionStatus(self.os_data.io_port, &nbytes, &completion_key, &overlapped, windows.INFINITE)) {
std.os.WindowsWaitResult.Aborted => return,
std.os.WindowsWaitResult.Normal => {},
}
@@ -1062,10 +1060,13 @@ pub const Lock = struct {
}
pub async fn acquire(self: *Lock) Held {
- var my_tick_node: Loop.NextTickNode = undefined;
-
s: suspend |handle| {
- my_tick_node.data = handle;
+ // TODO explicitly put this memory in the coroutine frame #1194
+ var my_tick_node = Loop.NextTickNode{
+ .data = handle,
+ .next = undefined,
+ };
+
self.queue.put(&my_tick_node);
// At this point, we are in the queue, so we might have already been resumed and this coroutine
@@ -1107,10 +1108,6 @@ pub const Lock = struct {
}
}
- // TODO this workaround to force my_tick_node to be in the coroutine frame should
- // not be necessary
- var trash1 = &my_tick_node;
-
return Held{ .lock = self };
}
};
@@ -1176,6 +1173,10 @@ test "std.event.Lock" {
}
async fn testLock(loop: *Loop, lock: *Lock) void {
+ // TODO explicitly put next tick node memory in the coroutine frame #1194
+ suspend |p| {
+ resume p;
+ }
const handle1 = async lockRunner(lock) catch @panic("out of memory");
var tick_node1 = Loop.NextTickNode{
.next = undefined,
@@ -1200,12 +1201,6 @@ async fn testLock(loop: *Loop, lock: *Lock) void {
await handle1;
await handle2;
await handle3;
-
- // TODO this is to force tick node memory to be in the coro frame
- // there should be a way to make it explicit where the memory is
- var a = &tick_node1;
- var b = &tick_node2;
- var c = &tick_node3;
}
var shared_test_data = [1]i32{0} ** 10;
@@ -1216,7 +1211,8 @@ async fn lockRunner(lock: *Lock) void {
var i: usize = 0;
while (i < shared_test_data.len) : (i += 1) {
- const handle = await (async lock.acquire() catch @panic("out of memory"));
+ const lock_promise = async lock.acquire() catch @panic("out of memory");
+ const handle = await lock_promise;
defer handle.release();
shared_test_index = 0;
--
cgit v1.2.3
From 574e31f0a046aa6e6fad73fff2cbbb3617fe1bae Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Tue, 10 Jul 2018 20:18:43 -0400
Subject: self-hosted: first passing test
* introduce std.atomic.Int
* add src-self-hosted/test.zig which is tested by the main test suite
- it fully utilizes the multithreaded async/await event loop so the
tests should Go Fast
* `stage2/bin/zig build-obj test.zig` is able to spit out an error if 2 exported
functions collide
* ability for `zig test` to accept `--object` and `--assembly`
arguments
* std.build: TestStep supports addLibPath and addObjectFile
---
CMakeLists.txt | 1 +
build.zig | 152 ++++++++++++++++++++---------------
src-self-hosted/errmsg.zig | 18 +++--
src-self-hosted/introspect.zig | 5 ++
src-self-hosted/main.zig | 36 ++++-----
src-self-hosted/module.zig | 98 +++++++++++++++++++----
src-self-hosted/test.zig | 176 +++++++++++++++++++++++++++++++++++++++++
src/main.cpp | 10 ++-
std/atomic/index.zig | 2 +
std/atomic/int.zig | 19 +++++
std/build.zig | 22 ++++++
11 files changed, 432 insertions(+), 107 deletions(-)
create mode 100644 src-self-hosted/test.zig
create mode 100644 std/atomic/int.zig
(limited to 'std/atomic')
diff --git a/CMakeLists.txt b/CMakeLists.txt
index eeb0ec2058..559b3b6964 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -431,6 +431,7 @@ set(ZIG_CPP_SOURCES
set(ZIG_STD_FILES
"array_list.zig"
"atomic/index.zig"
+ "atomic/int.zig"
"atomic/queue_mpmc.zig"
"atomic/queue_mpsc.zig"
"atomic/stack.zig"
diff --git a/build.zig b/build.zig
index fd154c7504..273048d458 100644
--- a/build.zig
+++ b/build.zig
@@ -35,70 +35,27 @@ pub fn build(b: *Builder) !void {
"BUILD_INFO",
});
var index: usize = 0;
- const cmake_binary_dir = nextValue(&index, build_info);
- const cxx_compiler = nextValue(&index, build_info);
- const llvm_config_exe = nextValue(&index, build_info);
- const lld_include_dir = nextValue(&index, build_info);
- const lld_libraries = nextValue(&index, build_info);
- const std_files = nextValue(&index, build_info);
- const c_header_files = nextValue(&index, build_info);
- const dia_guids_lib = nextValue(&index, build_info);
+ var ctx = Context{
+ .cmake_binary_dir = nextValue(&index, build_info),
+ .cxx_compiler = nextValue(&index, build_info),
+ .llvm_config_exe = nextValue(&index, build_info),
+ .lld_include_dir = nextValue(&index, build_info),
+ .lld_libraries = nextValue(&index, build_info),
+ .std_files = nextValue(&index, build_info),
+ .c_header_files = nextValue(&index, build_info),
+ .dia_guids_lib = nextValue(&index, build_info),
+ .llvm = undefined,
+ };
+ ctx.llvm = try findLLVM(b, ctx.llvm_config_exe);
- const llvm = findLLVM(b, llvm_config_exe) catch unreachable;
+ var test_stage2 = b.addTest("src-self-hosted/test.zig");
+ test_stage2.setBuildMode(builtin.Mode.Debug);
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
exe.setBuildMode(mode);
- // This is for finding /lib/libz.a on alpine linux.
- // TODO turn this into -Dextra-lib-path=/lib option
- exe.addLibPath("/lib");
-
- exe.addIncludeDir("src");
- exe.addIncludeDir(cmake_binary_dir);
- addCppLib(b, exe, cmake_binary_dir, "zig_cpp");
- if (lld_include_dir.len != 0) {
- exe.addIncludeDir(lld_include_dir);
- var it = mem.split(lld_libraries, ";");
- while (it.next()) |lib| {
- exe.addObjectFile(lib);
- }
- } else {
- addCppLib(b, exe, cmake_binary_dir, "embedded_lld_wasm");
- addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf");
- addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff");
- addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib");
- }
- dependOnLib(exe, llvm);
-
- if (exe.target.getOs() == builtin.Os.linux) {
- const libstdcxx_path_padded = try b.exec([][]const u8{
- cxx_compiler,
- "-print-file-name=libstdc++.a",
- });
- const libstdcxx_path = mem.split(libstdcxx_path_padded, "\r\n").next().?;
- if (mem.eql(u8, libstdcxx_path, "libstdc++.a")) {
- warn(
- \\Unable to determine path to libstdc++.a
- \\On Fedora, install libstdc++-static and try again.
- \\
- );
- return error.RequiredLibraryNotFound;
- }
- exe.addObjectFile(libstdcxx_path);
-
- exe.linkSystemLibrary("pthread");
- } else if (exe.target.isDarwin()) {
- exe.linkSystemLibrary("c++");
- }
-
- if (dia_guids_lib.len != 0) {
- exe.addObjectFile(dia_guids_lib);
- }
-
- if (exe.target.getOs() != builtin.Os.windows) {
- exe.linkSystemLibrary("xml2");
- }
- exe.linkSystemLibrary("c");
+ try configureStage2(b, test_stage2, ctx);
+ try configureStage2(b, exe, ctx);
b.default_step.dependOn(&exe.step);
@@ -110,12 +67,16 @@ pub fn build(b: *Builder) !void {
exe.setVerboseLink(verbose_link_exe);
b.installArtifact(exe);
- installStdLib(b, std_files);
- installCHeaders(b, c_header_files);
+ installStdLib(b, ctx.std_files);
+ installCHeaders(b, ctx.c_header_files);
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") orelse false;
+ const test_stage2_step = b.step("test-stage2", "Run the stage2 compiler tests");
+ test_stage2_step.dependOn(&test_stage2.step);
+ test_step.dependOn(test_stage2_step);
+
test_step.dependOn(docs_step);
test_step.dependOn(tests.addPkgTests(b, test_filter, "test/behavior.zig", "behavior", "Run the behavior tests", with_lldb));
@@ -133,7 +94,7 @@ pub fn build(b: *Builder) !void {
test_step.dependOn(tests.addGenHTests(b, test_filter));
}
-fn dependOnLib(lib_exe_obj: *std.build.LibExeObjStep, dep: *const LibraryDep) void {
+fn dependOnLib(lib_exe_obj: var, dep: *const LibraryDep) void {
for (dep.libdirs.toSliceConst()) |lib_dir| {
lib_exe_obj.addLibPath(lib_dir);
}
@@ -148,7 +109,7 @@ fn dependOnLib(lib_exe_obj: *std.build.LibExeObjStep, dep: *const LibraryDep) vo
}
}
-fn addCppLib(b: *Builder, lib_exe_obj: *std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) void {
+fn addCppLib(b: *Builder, lib_exe_obj: var, cmake_binary_dir: []const u8, lib_name: []const u8) void {
const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
lib_exe_obj.addObjectFile(os.path.join(b.allocator, cmake_binary_dir, "zig_cpp", b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())) catch unreachable);
}
@@ -254,3 +215,68 @@ fn nextValue(index: *usize, build_info: []const u8) []const u8 {
}
}
}
+
+fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
+ // This is for finding /lib/libz.a on alpine linux.
+ // TODO turn this into -Dextra-lib-path=/lib option
+ exe.addLibPath("/lib");
+
+ exe.addIncludeDir("src");
+ exe.addIncludeDir(ctx.cmake_binary_dir);
+ addCppLib(b, exe, ctx.cmake_binary_dir, "zig_cpp");
+ if (ctx.lld_include_dir.len != 0) {
+ exe.addIncludeDir(ctx.lld_include_dir);
+ var it = mem.split(ctx.lld_libraries, ";");
+ while (it.next()) |lib| {
+ exe.addObjectFile(lib);
+ }
+ } else {
+ addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_wasm");
+ addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_elf");
+ addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_coff");
+ addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_lib");
+ }
+ dependOnLib(exe, ctx.llvm);
+
+ if (exe.target.getOs() == builtin.Os.linux) {
+ const libstdcxx_path_padded = try b.exec([][]const u8{
+ ctx.cxx_compiler,
+ "-print-file-name=libstdc++.a",
+ });
+ const libstdcxx_path = mem.split(libstdcxx_path_padded, "\r\n").next().?;
+ if (mem.eql(u8, libstdcxx_path, "libstdc++.a")) {
+ warn(
+ \\Unable to determine path to libstdc++.a
+ \\On Fedora, install libstdc++-static and try again.
+ \\
+ );
+ return error.RequiredLibraryNotFound;
+ }
+ exe.addObjectFile(libstdcxx_path);
+
+ exe.linkSystemLibrary("pthread");
+ } else if (exe.target.isDarwin()) {
+ exe.linkSystemLibrary("c++");
+ }
+
+ if (ctx.dia_guids_lib.len != 0) {
+ exe.addObjectFile(ctx.dia_guids_lib);
+ }
+
+ if (exe.target.getOs() != builtin.Os.windows) {
+ exe.linkSystemLibrary("xml2");
+ }
+ exe.linkSystemLibrary("c");
+}
+
+const Context = struct {
+ cmake_binary_dir: []const u8,
+ cxx_compiler: []const u8,
+ llvm_config_exe: []const u8,
+ lld_include_dir: []const u8,
+ lld_libraries: []const u8,
+ std_files: []const u8,
+ c_header_files: []const u8,
+ dia_guids_lib: []const u8,
+ llvm: LibraryDep,
+};
diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig
index b6fd78d8f6..a92b5145ce 100644
--- a/src-self-hosted/errmsg.zig
+++ b/src-self-hosted/errmsg.zig
@@ -11,11 +11,15 @@ pub const Color = enum {
On,
};
+pub const Span = struct {
+ first: ast.TokenIndex,
+ last: ast.TokenIndex,
+};
+
pub const Msg = struct {
path: []const u8,
text: []u8,
- first_token: TokenIndex,
- last_token: TokenIndex,
+ span: Span,
tree: *ast.Tree,
};
@@ -39,8 +43,10 @@ pub fn createFromParseError(
.tree = tree,
.path = path,
.text = text_buf.toOwnedSlice(),
- .first_token = loc_token,
- .last_token = loc_token,
+ .span = Span{
+ .first = loc_token,
+ .last = loc_token,
+ },
});
errdefer allocator.destroy(msg);
@@ -48,8 +54,8 @@ pub fn createFromParseError(
}
pub fn printToStream(stream: var, msg: *const Msg, color_on: bool) !void {
- const first_token = msg.tree.tokens.at(msg.first_token);
- const last_token = msg.tree.tokens.at(msg.last_token);
+ const first_token = msg.tree.tokens.at(msg.span.first);
+ const last_token = msg.tree.tokens.at(msg.span.last);
const start_loc = msg.tree.tokenLocationPtr(0, first_token);
const end_loc = msg.tree.tokenLocationPtr(first_token.end, last_token);
if (!color_on) {
diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig
index 74084b48c6..ecd04c4467 100644
--- a/src-self-hosted/introspect.zig
+++ b/src-self-hosted/introspect.zig
@@ -53,3 +53,8 @@ pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 {
return error.ZigLibDirNotFound;
};
}
+
+/// Caller must free result
+pub fn resolveZigCacheDir(allocator: *mem.Allocator) ![]u8 {
+ return std.mem.dupe(allocator, u8, "zig-cache");
+}
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index fe94a4460a..d7ead0ba32 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -481,29 +481,29 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
module.link_out_file = flags.single("out-file");
try module.build();
- const process_build_events_handle = try async processBuildEvents(module, true);
+ const process_build_events_handle = try async processBuildEvents(module, color);
defer cancel process_build_events_handle;
loop.run();
}
-async fn processBuildEvents(module: *Module, watch: bool) void {
- while (watch) {
- // TODO directly awaiting async should guarantee memory allocation elision
- const build_event = await (async module.events.get() catch unreachable);
+async fn processBuildEvents(module: *Module, color: errmsg.Color) void {
+ // TODO directly awaiting async should guarantee memory allocation elision
+ const build_event = await (async module.events.get() catch unreachable);
- switch (build_event) {
- Module.Event.Ok => {
- std.debug.warn("Build succeeded\n");
- return;
- },
- Module.Event.Error => |err| {
- std.debug.warn("build failed: {}\n", @errorName(err));
- @panic("TODO error return trace");
- },
- Module.Event.Fail => |errs| {
- @panic("TODO print compile error messages");
- },
- }
+ switch (build_event) {
+ Module.Event.Ok => {
+ std.debug.warn("Build succeeded\n");
+ return;
+ },
+ Module.Event.Error => |err| {
+ std.debug.warn("build failed: {}\n", @errorName(err));
+ @panic("TODO error return trace");
+ },
+ Module.Event.Fail => |msgs| {
+ for (msgs) |msg| {
+ errmsg.printToFile(&stderr_file, msg, color) catch os.exit(1);
+ }
+ },
}
}
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index 24be228eb8..44954e4cd1 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -89,12 +89,9 @@ pub const Module = struct {
/// the build is complete.
build_group: event.Group(BuildError!void),
- const BuildErrorsList = std.SegmentedList(BuildErrorDesc, 1);
+ compile_errors: event.Locked(CompileErrList),
- pub const BuildErrorDesc = struct {
- code: BuildError,
- text: []const u8,
- };
+ const CompileErrList = std.ArrayList(*errmsg.Msg);
// TODO handle some of these earlier and report them in a way other than error codes
pub const BuildError = error{
@@ -131,11 +128,12 @@ pub const Module = struct {
NoStdHandles,
Overflow,
NotSupported,
+ BufferTooSmall,
};
pub const Event = union(enum) {
Ok,
- Fail: []errmsg.Msg,
+ Fail: []*errmsg.Msg,
Error: BuildError,
};
@@ -249,6 +247,7 @@ pub const Module = struct {
.link_out_file = null,
.exported_symbol_names = event.Locked(Decl.Table).init(loop, Decl.Table.init(loop.allocator)),
.build_group = event.Group(BuildError!void).init(loop),
+ .compile_errors = event.Locked(CompileErrList).init(loop, CompileErrList.init(loop.allocator)),
});
}
@@ -288,7 +287,17 @@ pub const Module = struct {
await (async self.events.put(Event{ .Error = err }) catch unreachable);
return;
};
- await (async self.events.put(Event.Ok) catch unreachable);
+ const compile_errors = blk: {
+ const held = await (async self.compile_errors.acquire() catch unreachable);
+ defer held.release();
+ break :blk held.value.toOwnedSlice();
+ };
+
+ if (compile_errors.len == 0) {
+ await (async self.events.put(Event.Ok) catch unreachable);
+ } else {
+ await (async self.events.put(Event{ .Fail = compile_errors }) catch unreachable);
+ }
// for now we stop after 1
return;
}
@@ -310,10 +319,13 @@ pub const Module = struct {
};
errdefer self.a().free(source_code);
- var parsed_file = ParsedFile{
- .tree = try std.zig.parse(self.a(), source_code),
+ const parsed_file = try self.a().create(ParsedFile{
+ .tree = undefined,
.realpath = root_src_real_path,
- };
+ });
+ errdefer self.a().destroy(parsed_file);
+
+ parsed_file.tree = try std.zig.parse(self.a(), source_code);
errdefer parsed_file.tree.deinit();
const tree = &parsed_file.tree;
@@ -337,7 +349,7 @@ pub const Module = struct {
const name = if (fn_proto.name_token) |name_token| tree.tokenSlice(name_token) else {
@panic("TODO add compile error");
//try self.addCompileError(
- // &parsed_file,
+ // parsed_file,
// fn_proto.fn_token,
// fn_proto.fn_token + 1,
// "missing function name",
@@ -357,7 +369,7 @@ pub const Module = struct {
});
errdefer self.a().destroy(fn_decl);
- try decl_group.call(addTopLevelDecl, self, tree, &fn_decl.base);
+ try decl_group.call(addTopLevelDecl, self, parsed_file, &fn_decl.base);
},
ast.Node.Id.TestDecl => @panic("TODO"),
else => unreachable,
@@ -367,20 +379,56 @@ pub const Module = struct {
try await (async self.build_group.wait() catch unreachable);
}
- async fn addTopLevelDecl(self: *Module, tree: *ast.Tree, decl: *Decl) !void {
- const is_export = decl.isExported(tree);
+ async fn addTopLevelDecl(self: *Module, parsed_file: *ParsedFile, decl: *Decl) !void {
+ const is_export = decl.isExported(&parsed_file.tree);
if (is_export) {
- try self.build_group.call(verifyUniqueSymbol, self, decl);
+ try self.build_group.call(verifyUniqueSymbol, self, parsed_file, decl);
}
}
- async fn verifyUniqueSymbol(self: *Module, decl: *Decl) !void {
+ fn addCompileError(self: *Module, parsed_file: *ParsedFile, span: errmsg.Span, comptime fmt: []const u8, args: ...) !void {
+ const text = try std.fmt.allocPrint(self.loop.allocator, fmt, args);
+ errdefer self.loop.allocator.free(text);
+
+ try self.build_group.call(addCompileErrorAsync, self, parsed_file, span.first, span.last, text);
+ }
+
+ async fn addCompileErrorAsync(
+ self: *Module,
+ parsed_file: *ParsedFile,
+ first_token: ast.TokenIndex,
+ last_token: ast.TokenIndex,
+ text: []u8,
+ ) !void {
+ const msg = try self.loop.allocator.create(errmsg.Msg{
+ .path = parsed_file.realpath,
+ .text = text,
+ .span = errmsg.Span{
+ .first = first_token,
+ .last = last_token,
+ },
+ .tree = &parsed_file.tree,
+ });
+ errdefer self.loop.allocator.destroy(msg);
+
+ const compile_errors = await (async self.compile_errors.acquire() catch unreachable);
+ defer compile_errors.release();
+
+ try compile_errors.value.append(msg);
+ }
+
+ async fn verifyUniqueSymbol(self: *Module, parsed_file: *ParsedFile, decl: *Decl) !void {
const exported_symbol_names = await (async self.exported_symbol_names.acquire() catch unreachable);
defer exported_symbol_names.release();
if (try exported_symbol_names.value.put(decl.name, decl)) |other_decl| {
- @panic("TODO report compile error");
+ try self.addCompileError(
+ parsed_file,
+ decl.getSpan(),
+ "exported symbol collision: '{}'",
+ decl.name,
+ );
}
}
@@ -503,6 +551,22 @@ pub const Decl = struct {
}
}
+ pub fn getSpan(base: *const Decl) errmsg.Span {
+ switch (base.id) {
+ Id.Fn => {
+ const fn_decl = @fieldParentPtr(Fn, "base", base);
+ const fn_proto = fn_decl.fn_proto;
+ const start = fn_proto.fn_token;
+ const end = fn_proto.name_token orelse start;
+ return errmsg.Span{
+ .first = start,
+ .last = end + 1,
+ };
+ },
+ else => @panic("TODO"),
+ }
+ }
+
pub const Resolution = enum {
Unresolved,
InProgress,
diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig
new file mode 100644
index 0000000000..7ce7cf6ee3
--- /dev/null
+++ b/src-self-hosted/test.zig
@@ -0,0 +1,176 @@
+const std = @import("std");
+const mem = std.mem;
+const builtin = @import("builtin");
+const Target = @import("target.zig").Target;
+const Module = @import("module.zig").Module;
+const introspect = @import("introspect.zig");
+const assertOrPanic = std.debug.assertOrPanic;
+const errmsg = @import("errmsg.zig");
+
+test "compile errors" {
+ var ctx: TestContext = undefined;
+ try ctx.init();
+ defer ctx.deinit();
+
+ try ctx.testCompileError(
+ \\export fn entry() void {}
+ \\export fn entry() void {}
+ , file1, 2, 8, "exported symbol collision: 'entry'");
+
+ try ctx.run();
+}
+
+const file1 = "1.zig";
+
+const TestContext = struct {
+ loop: std.event.Loop,
+ zig_lib_dir: []u8,
+ direct_allocator: std.heap.DirectAllocator,
+ arena: std.heap.ArenaAllocator,
+ zig_cache_dir: []u8,
+ file_index: std.atomic.Int(usize),
+ group: std.event.Group(error!void),
+ any_err: error!void,
+
+ const tmp_dir_name = "stage2_test_tmp";
+
+ fn init(self: *TestContext) !void {
+ self.* = TestContext{
+ .any_err = {},
+ .direct_allocator = undefined,
+ .arena = undefined,
+ .loop = undefined,
+ .zig_lib_dir = undefined,
+ .zig_cache_dir = undefined,
+ .group = undefined,
+ .file_index = std.atomic.Int(usize).init(0),
+ };
+
+ self.direct_allocator = std.heap.DirectAllocator.init();
+ errdefer self.direct_allocator.deinit();
+
+ self.arena = std.heap.ArenaAllocator.init(&self.direct_allocator.allocator);
+ errdefer self.arena.deinit();
+
+ // TODO faster allocator for coroutines that is thread-safe/lock-free
+ try self.loop.initMultiThreaded(&self.direct_allocator.allocator);
+ errdefer self.loop.deinit();
+
+ self.group = std.event.Group(error!void).init(&self.loop);
+ errdefer self.group.cancelAll();
+
+ self.zig_lib_dir = try introspect.resolveZigLibDir(&self.arena.allocator);
+ errdefer self.arena.allocator.free(self.zig_lib_dir);
+
+ self.zig_cache_dir = try introspect.resolveZigCacheDir(&self.arena.allocator);
+ errdefer self.arena.allocator.free(self.zig_cache_dir);
+
+ try std.os.makePath(&self.arena.allocator, tmp_dir_name);
+ errdefer std.os.deleteTree(&self.arena.allocator, tmp_dir_name) catch {};
+ }
+
+ fn deinit(self: *TestContext) void {
+ std.os.deleteTree(&self.arena.allocator, tmp_dir_name) catch {};
+ self.arena.allocator.free(self.zig_cache_dir);
+ self.arena.allocator.free(self.zig_lib_dir);
+ self.loop.deinit();
+ self.arena.deinit();
+ self.direct_allocator.deinit();
+ }
+
+ fn run(self: *TestContext) !void {
+ const handle = try self.loop.call(waitForGroup, self);
+ defer cancel handle;
+ self.loop.run();
+ return self.any_err;
+ }
+
+ async fn waitForGroup(self: *TestContext) void {
+ self.any_err = await (async self.group.wait() catch unreachable);
+ }
+
+ fn testCompileError(
+ self: *TestContext,
+ source: []const u8,
+ path: []const u8,
+ line: usize,
+ column: usize,
+ msg: []const u8,
+ ) !void {
+ var file_index_buf: [20]u8 = undefined;
+ const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.next());
+ const file1_path = try std.os.path.join(&self.arena.allocator, tmp_dir_name, file_index, file1);
+
+ if (std.os.path.dirname(file1_path)) |dirname| {
+ try std.os.makePath(&self.arena.allocator, dirname);
+ }
+
+ // TODO async I/O
+ try std.io.writeFile(&self.arena.allocator, file1_path, source);
+
+ var module = try Module.create(
+ &self.loop,
+ "test",
+ file1_path,
+ Target.Native,
+ Module.Kind.Obj,
+ builtin.Mode.Debug,
+ self.zig_lib_dir,
+ self.zig_cache_dir,
+ );
+ errdefer module.destroy();
+
+ try module.build();
+
+ try self.group.call(getModuleEvent, module, source, path, line, column, msg);
+ }
+
+ async fn getModuleEvent(
+ module: *Module,
+ source: []const u8,
+ path: []const u8,
+ line: usize,
+ column: usize,
+ text: []const u8,
+ ) !void {
+ defer module.destroy();
+ const build_event = await (async module.events.get() catch unreachable);
+
+ switch (build_event) {
+ Module.Event.Ok => {
+ @panic("build incorrectly succeeded");
+ },
+ Module.Event.Error => |err| {
+ @panic("build incorrectly failed");
+ },
+ Module.Event.Fail => |msgs| {
+ assertOrPanic(msgs.len != 0);
+ for (msgs) |msg| {
+ if (mem.endsWith(u8, msg.path, path) and mem.eql(u8, msg.text, text)) {
+ const first_token = msg.tree.tokens.at(msg.span.first);
+ const last_token = msg.tree.tokens.at(msg.span.first);
+ const start_loc = msg.tree.tokenLocationPtr(0, first_token);
+ if (start_loc.line + 1 == line and start_loc.column + 1 == column) {
+ return;
+ }
+ }
+ }
+ std.debug.warn(
+ "\n=====source:=======\n{}\n====expected:========\n{}:{}:{}: error: {}\n",
+ source,
+ path,
+ line,
+ column,
+ text,
+ );
+ std.debug.warn("\n====found:========\n");
+ var stderr = try std.io.getStdErr();
+ for (msgs) |msg| {
+ try errmsg.printToFile(&stderr, msg, errmsg.Color.Auto);
+ }
+ std.debug.warn("============\n");
+ return error.TestFailed;
+ },
+ }
+ }
+};
diff --git a/src/main.cpp b/src/main.cpp
index a409778a78..5f96953f21 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -891,15 +891,19 @@ int main(int argc, char **argv) {
add_package(g, cur_pkg, g->root_package);
- if (cmd == CmdBuild || cmd == CmdRun) {
- codegen_set_emit_file_type(g, emit_file_type);
-
+ if (cmd == CmdBuild || cmd == CmdRun || cmd == CmdTest) {
for (size_t i = 0; i < objects.length; i += 1) {
codegen_add_object(g, buf_create_from_str(objects.at(i)));
}
for (size_t i = 0; i < asm_files.length; i += 1) {
codegen_add_assembly(g, buf_create_from_str(asm_files.at(i)));
}
+ }
+
+
+ if (cmd == CmdBuild || cmd == CmdRun) {
+ codegen_set_emit_file_type(g, emit_file_type);
+
codegen_build(g);
codegen_link(g, out_file);
if (timing_info)
diff --git a/std/atomic/index.zig b/std/atomic/index.zig
index c0ea5be183..cf344a8231 100644
--- a/std/atomic/index.zig
+++ b/std/atomic/index.zig
@@ -1,9 +1,11 @@
pub const Stack = @import("stack.zig").Stack;
pub const QueueMpsc = @import("queue_mpsc.zig").QueueMpsc;
pub const QueueMpmc = @import("queue_mpmc.zig").QueueMpmc;
+pub const Int = @import("int.zig").Int;
test "std.atomic" {
_ = @import("stack.zig");
_ = @import("queue_mpsc.zig");
_ = @import("queue_mpmc.zig");
+ _ = @import("int.zig");
}
diff --git a/std/atomic/int.zig b/std/atomic/int.zig
new file mode 100644
index 0000000000..7042bca78d
--- /dev/null
+++ b/std/atomic/int.zig
@@ -0,0 +1,19 @@
+const builtin = @import("builtin");
+const AtomicOrder = builtin.AtomicOrder;
+
+/// Thread-safe, lock-free integer
+pub fn Int(comptime T: type) type {
+ return struct {
+ value: T,
+
+ pub const Self = this;
+
+ pub fn init(init_val: T) Self {
+ return Self{ .value = init_val };
+ }
+
+ pub fn next(self: *Self) T {
+ return @atomicRmw(T, &self.value, builtin.AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+ }
+ };
+}
diff --git a/std/build.zig b/std/build.zig
index 24fa85383a..cea760e8a2 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -1596,6 +1596,8 @@ pub const TestStep = struct {
target: Target,
exec_cmd_args: ?[]const ?[]const u8,
include_dirs: ArrayList([]const u8),
+ lib_paths: ArrayList([]const u8),
+ object_files: ArrayList([]const u8),
pub fn init(builder: *Builder, root_src: []const u8) TestStep {
const step_name = builder.fmt("test {}", root_src);
@@ -1611,9 +1613,15 @@ pub const TestStep = struct {
.target = Target{ .Native = {} },
.exec_cmd_args = null,
.include_dirs = ArrayList([]const u8).init(builder.allocator),
+ .lib_paths = ArrayList([]const u8).init(builder.allocator),
+ .object_files = ArrayList([]const u8).init(builder.allocator),
};
}
+ pub fn addLibPath(self: *TestStep, path: []const u8) void {
+ self.lib_paths.append(path) catch unreachable;
+ }
+
pub fn setVerbose(self: *TestStep, value: bool) void {
self.verbose = value;
}
@@ -1638,6 +1646,10 @@ pub const TestStep = struct {
self.filter = text;
}
+ pub fn addObjectFile(self: *TestStep, path: []const u8) void {
+ self.object_files.append(path) catch unreachable;
+ }
+
pub fn setTarget(self: *TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
self.target = Target{
.Cross = CrossTarget{
@@ -1699,6 +1711,11 @@ pub const TestStep = struct {
try zig_args.append(self.name_prefix);
}
+ for (self.object_files.toSliceConst()) |object_file| {
+ try zig_args.append("--object");
+ try zig_args.append(builder.pathFromRoot(object_file));
+ }
+
{
var it = self.link_libs.iterator();
while (true) {
@@ -1734,6 +1751,11 @@ pub const TestStep = struct {
try zig_args.append(rpath);
}
+ for (self.lib_paths.toSliceConst()) |lib_path| {
+ try zig_args.append("--library-path");
+ try zig_args.append(lib_path);
+ }
+
for (builder.lib_paths.toSliceConst()) |lib_path| {
try zig_args.append("--library-path");
try zig_args.append(lib_path);
--
cgit v1.2.3
From 9751a0ae045110fb615c866b94ad47680b9c48c7 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Wed, 11 Jul 2018 19:38:01 -0400
Subject: std.atomic: use spinlocks
the lock-free data structures all had ABA problems and
std.atomic.Stack had a possibility to load an unmapped memory address.
---
CMakeLists.txt | 3 +-
build.zig | 8 +-
std/atomic/index.zig | 6 +-
std/atomic/queue.zig | 226 ++++++++++++++++++++++++++++++++++++++++++++++
std/atomic/queue_mpmc.zig | 214 -------------------------------------------
std/atomic/queue_mpsc.zig | 185 -------------------------------------
std/atomic/stack.zig | 32 ++++---
std/event/channel.zig | 12 +--
std/event/future.zig | 21 +++--
std/event/lock.zig | 2 +-
std/event/loop.zig | 6 +-
test/tests.zig | 26 +++---
12 files changed, 286 insertions(+), 455 deletions(-)
create mode 100644 std/atomic/queue.zig
delete mode 100644 std/atomic/queue_mpmc.zig
delete mode 100644 std/atomic/queue_mpsc.zig
(limited to 'std/atomic')
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 51d348f042..e606855555 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -432,8 +432,7 @@ set(ZIG_STD_FILES
"array_list.zig"
"atomic/index.zig"
"atomic/int.zig"
- "atomic/queue_mpmc.zig"
- "atomic/queue_mpsc.zig"
+ "atomic/queue.zig"
"atomic/stack.zig"
"base64.zig"
"buf_map.zig"
diff --git a/build.zig b/build.zig
index fd37138f33..c9e70887e3 100644
--- a/build.zig
+++ b/build.zig
@@ -91,11 +91,11 @@ pub fn build(b: *Builder) !void {
test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests", modes));
- test_step.dependOn(tests.addCompareOutputTests(b, test_filter));
+ test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
test_step.dependOn(tests.addBuildExampleTests(b, test_filter));
- test_step.dependOn(tests.addCompileErrorTests(b, test_filter));
- test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter));
- test_step.dependOn(tests.addRuntimeSafetyTests(b, test_filter));
+ test_step.dependOn(tests.addCompileErrorTests(b, test_filter, modes));
+ test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes));
+ test_step.dependOn(tests.addRuntimeSafetyTests(b, test_filter, modes));
test_step.dependOn(tests.addTranslateCTests(b, test_filter));
test_step.dependOn(tests.addGenHTests(b, test_filter));
test_step.dependOn(docs_step);
diff --git a/std/atomic/index.zig b/std/atomic/index.zig
index cf344a8231..a94cff1973 100644
--- a/std/atomic/index.zig
+++ b/std/atomic/index.zig
@@ -1,11 +1,9 @@
pub const Stack = @import("stack.zig").Stack;
-pub const QueueMpsc = @import("queue_mpsc.zig").QueueMpsc;
-pub const QueueMpmc = @import("queue_mpmc.zig").QueueMpmc;
+pub const Queue = @import("queue.zig").Queue;
pub const Int = @import("int.zig").Int;
test "std.atomic" {
_ = @import("stack.zig");
- _ = @import("queue_mpsc.zig");
- _ = @import("queue_mpmc.zig");
+ _ = @import("queue.zig");
_ = @import("int.zig");
}
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
new file mode 100644
index 0000000000..1fd07714e8
--- /dev/null
+++ b/std/atomic/queue.zig
@@ -0,0 +1,226 @@
+const builtin = @import("builtin");
+const AtomicOrder = builtin.AtomicOrder;
+const AtomicRmwOp = builtin.AtomicRmwOp;
+
+/// Many producer, many consumer, non-allocating, thread-safe.
+/// Uses a spinlock to protect get() and put().
+pub fn Queue(comptime T: type) type {
+ return struct {
+ head: ?*Node,
+ tail: ?*Node,
+ lock: u8,
+
+ pub const Self = this;
+
+ pub const Node = struct {
+ next: ?*Node,
+ data: T,
+ };
+
+ pub fn init() Self {
+ return Self{
+ .head = null,
+ .tail = null,
+ .lock = 0,
+ };
+ }
+
+ pub fn put(self: *Self, node: *Node) void {
+ node.next = null;
+
+ while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
+ defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
+
+ const opt_tail = self.tail;
+ self.tail = node;
+ if (opt_tail) |tail| {
+ tail.next = node;
+ } else {
+ assert(self.head == null);
+ self.head = node;
+ }
+ }
+
+ pub fn get(self: *Self) ?*Node {
+ while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
+ defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
+
+ const head = self.head orelse return null;
+ self.head = head.next;
+ if (head.next == null) self.tail = null;
+ return head;
+ }
+
+ pub fn isEmpty(self: *Self) bool {
+ return @atomicLoad(?*Node, &self.head, builtin.AtomicOrder.SeqCst) != null;
+ }
+
+ pub fn dump(self: *Self) void {
+ while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
+ defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
+
+ std.debug.warn("head: ");
+ dumpRecursive(self.head, 0);
+ std.debug.warn("tail: ");
+ dumpRecursive(self.tail, 0);
+ }
+
+ fn dumpRecursive(optional_node: ?*Node, indent: usize) void {
+ var stderr_file = std.io.getStdErr() catch return;
+ const stderr = &std.io.FileOutStream.init(&stderr_file).stream;
+ stderr.writeByteNTimes(' ', indent) catch return;
+ if (optional_node) |node| {
+ std.debug.warn("0x{x}={}\n", @ptrToInt(node), node.data);
+ dumpRecursive(node.next, indent + 1);
+ } else {
+ std.debug.warn("(null)\n");
+ }
+ }
+ };
+}
+
+const std = @import("../index.zig");
+const assert = std.debug.assert;
+
+const Context = struct {
+ allocator: *std.mem.Allocator,
+ queue: *Queue(i32),
+ put_sum: isize,
+ get_sum: isize,
+ get_count: usize,
+ puts_done: u8, // TODO make this a bool
+};
+
+// TODO add lazy evaluated build options and then put puts_per_thread behind
+// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
+// CI we would use a less aggressive setting since at 1 core, while we still
+// want this test to pass, we need a smaller value since there is so much thrashing
+// we would also use a less aggressive setting when running in valgrind
+const puts_per_thread = 500;
+const put_thread_count = 3;
+
+test "std.atomic.Queue" {
+ var direct_allocator = std.heap.DirectAllocator.init();
+ defer direct_allocator.deinit();
+
+ var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
+ defer direct_allocator.allocator.free(plenty_of_memory);
+
+ var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
+ var a = &fixed_buffer_allocator.allocator;
+
+ var queue = Queue(i32).init();
+ var context = Context{
+ .allocator = a,
+ .queue = &queue,
+ .put_sum = 0,
+ .get_sum = 0,
+ .puts_done = 0,
+ .get_count = 0,
+ };
+
+ var putters: [put_thread_count]*std.os.Thread = undefined;
+ for (putters) |*t| {
+ t.* = try std.os.spawnThread(&context, startPuts);
+ }
+ var getters: [put_thread_count]*std.os.Thread = undefined;
+ for (getters) |*t| {
+ t.* = try std.os.spawnThread(&context, startGets);
+ }
+
+ for (putters) |t|
+ t.wait();
+ _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
+ for (getters) |t|
+ t.wait();
+
+ if (context.put_sum != context.get_sum) {
+ std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
+ }
+
+ if (context.get_count != puts_per_thread * put_thread_count) {
+ std.debug.panic(
+ "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
+ context.get_count,
+ u32(puts_per_thread),
+ u32(put_thread_count),
+ );
+ }
+}
+
+fn startPuts(ctx: *Context) u8 {
+ var put_count: usize = puts_per_thread;
+ var r = std.rand.DefaultPrng.init(0xdeadbeef);
+ while (put_count != 0) : (put_count -= 1) {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ const x = @bitCast(i32, r.random.scalar(u32));
+ const node = ctx.allocator.create(Queue(i32).Node{
+ .next = undefined,
+ .data = x,
+ }) catch unreachable;
+ ctx.queue.put(node);
+ _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
+ }
+ return 0;
+}
+
+fn startGets(ctx: *Context) u8 {
+ while (true) {
+ const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
+
+ while (ctx.queue.get()) |node| {
+ std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
+ _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
+ _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
+ }
+
+ if (last) return 0;
+ }
+}
+
+test "std.atomic.Queue single-threaded" {
+ var queue = Queue(i32).init();
+
+ var node_0 = Queue(i32).Node{
+ .data = 0,
+ .next = undefined,
+ };
+ queue.put(&node_0);
+
+ var node_1 = Queue(i32).Node{
+ .data = 1,
+ .next = undefined,
+ };
+ queue.put(&node_1);
+
+ assert(queue.get().?.data == 0);
+
+ var node_2 = Queue(i32).Node{
+ .data = 2,
+ .next = undefined,
+ };
+ queue.put(&node_2);
+
+ var node_3 = Queue(i32).Node{
+ .data = 3,
+ .next = undefined,
+ };
+ queue.put(&node_3);
+
+ assert(queue.get().?.data == 1);
+
+ assert(queue.get().?.data == 2);
+
+ var node_4 = Queue(i32).Node{
+ .data = 4,
+ .next = undefined,
+ };
+ queue.put(&node_4);
+
+ assert(queue.get().?.data == 3);
+ node_3.next = null;
+
+ assert(queue.get().?.data == 4);
+
+ assert(queue.get() == null);
+}
diff --git a/std/atomic/queue_mpmc.zig b/std/atomic/queue_mpmc.zig
deleted file mode 100644
index 7ffc9f9ccb..0000000000
--- a/std/atomic/queue_mpmc.zig
+++ /dev/null
@@ -1,214 +0,0 @@
-const builtin = @import("builtin");
-const AtomicOrder = builtin.AtomicOrder;
-const AtomicRmwOp = builtin.AtomicRmwOp;
-
-/// Many producer, many consumer, non-allocating, thread-safe, lock-free
-/// This implementation has a crippling limitation - it hangs onto node
-/// memory for 1 extra get() and 1 extra put() operation - when get() returns a node, that
-/// node must not be freed until both the next get() and the next put() completes.
-pub fn QueueMpmc(comptime T: type) type {
- return struct {
- head: *Node,
- tail: *Node,
- root: Node,
-
- pub const Self = this;
-
- pub const Node = struct {
- next: ?*Node,
- data: T,
- };
-
- /// TODO: well defined copy elision: https://github.com/ziglang/zig/issues/287
- pub fn init(self: *Self) void {
- self.root.next = null;
- self.head = &self.root;
- self.tail = &self.root;
- }
-
- pub fn put(self: *Self, node: *Node) void {
- node.next = null;
-
- const tail = @atomicRmw(*Node, &self.tail, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
- _ = @atomicRmw(?*Node, &tail.next, AtomicRmwOp.Xchg, node, AtomicOrder.SeqCst);
- }
-
- /// node must not be freed until both the next get() and the next put() complete
- pub fn get(self: *Self) ?*Node {
- var head = @atomicLoad(*Node, &self.head, AtomicOrder.SeqCst);
- while (true) {
- const node = head.next orelse return null;
- head = @cmpxchgWeak(*Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return node;
- }
- }
-
- ///// This is a debug function that is not thread-safe.
- pub fn dump(self: *Self) void {
- std.debug.warn("head: ");
- dumpRecursive(self.head, 0);
- std.debug.warn("tail: ");
- dumpRecursive(self.tail, 0);
- }
-
- fn dumpRecursive(optional_node: ?*Node, indent: usize) void {
- var stderr_file = std.io.getStdErr() catch return;
- const stderr = &std.io.FileOutStream.init(&stderr_file).stream;
- stderr.writeByteNTimes(' ', indent) catch return;
- if (optional_node) |node| {
- std.debug.warn("0x{x}={}\n", @ptrToInt(node), node.data);
- dumpRecursive(node.next, indent + 1);
- } else {
- std.debug.warn("(null)\n");
- }
- }
- };
-}
-
-const std = @import("std");
-const assert = std.debug.assert;
-
-const Context = struct {
- allocator: *std.mem.Allocator,
- queue: *QueueMpmc(i32),
- put_sum: isize,
- get_sum: isize,
- get_count: usize,
- puts_done: u8, // TODO make this a bool
-};
-
-// TODO add lazy evaluated build options and then put puts_per_thread behind
-// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
-// CI we would use a less aggressive setting since at 1 core, while we still
-// want this test to pass, we need a smaller value since there is so much thrashing
-// we would also use a less aggressive setting when running in valgrind
-const puts_per_thread = 500;
-const put_thread_count = 3;
-
-test "std.atomic.queue_mpmc" {
- var direct_allocator = std.heap.DirectAllocator.init();
- defer direct_allocator.deinit();
-
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
- defer direct_allocator.allocator.free(plenty_of_memory);
-
- var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
- var a = &fixed_buffer_allocator.allocator;
-
- var queue: QueueMpmc(i32) = undefined;
- queue.init();
- var context = Context{
- .allocator = a,
- .queue = &queue,
- .put_sum = 0,
- .get_sum = 0,
- .puts_done = 0,
- .get_count = 0,
- };
-
- var putters: [put_thread_count]*std.os.Thread = undefined;
- for (putters) |*t| {
- t.* = try std.os.spawnThread(&context, startPuts);
- }
- var getters: [put_thread_count]*std.os.Thread = undefined;
- for (getters) |*t| {
- t.* = try std.os.spawnThread(&context, startGets);
- }
-
- for (putters) |t|
- t.wait();
- _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
- for (getters) |t|
- t.wait();
-
- if (context.put_sum != context.get_sum) {
- std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
- }
-
- if (context.get_count != puts_per_thread * put_thread_count) {
- std.debug.panic(
- "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
- context.get_count,
- u32(puts_per_thread),
- u32(put_thread_count),
- );
- }
-}
-
-fn startPuts(ctx: *Context) u8 {
- var put_count: usize = puts_per_thread;
- var r = std.rand.DefaultPrng.init(0xdeadbeef);
- while (put_count != 0) : (put_count -= 1) {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
- const x = @bitCast(i32, r.random.scalar(u32));
- const node = ctx.allocator.create(QueueMpmc(i32).Node{
- .next = undefined,
- .data = x,
- }) catch unreachable;
- ctx.queue.put(node);
- _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
- }
- return 0;
-}
-
-fn startGets(ctx: *Context) u8 {
- while (true) {
- const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
-
- while (ctx.queue.get()) |node| {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
- _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
- _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
- }
-
- if (last) return 0;
- }
-}
-
-test "std.atomic.queue_mpmc single-threaded" {
- var queue: QueueMpmc(i32) = undefined;
- queue.init();
-
- var node_0 = QueueMpmc(i32).Node{
- .data = 0,
- .next = undefined,
- };
- queue.put(&node_0);
-
- var node_1 = QueueMpmc(i32).Node{
- .data = 1,
- .next = undefined,
- };
- queue.put(&node_1);
-
- assert(queue.get().?.data == 0);
-
- var node_2 = QueueMpmc(i32).Node{
- .data = 2,
- .next = undefined,
- };
- queue.put(&node_2);
-
- var node_3 = QueueMpmc(i32).Node{
- .data = 3,
- .next = undefined,
- };
- queue.put(&node_3);
-
- assert(queue.get().?.data == 1);
-
- assert(queue.get().?.data == 2);
-
- var node_4 = QueueMpmc(i32).Node{
- .data = 4,
- .next = undefined,
- };
- queue.put(&node_4);
-
- assert(queue.get().?.data == 3);
- // if we were to set node_3.next to null here, it would cause this test
- // to fail. this demonstrates the limitation of hanging on to extra memory.
-
- assert(queue.get().?.data == 4);
-
- assert(queue.get() == null);
-}
diff --git a/std/atomic/queue_mpsc.zig b/std/atomic/queue_mpsc.zig
deleted file mode 100644
index 978e189453..0000000000
--- a/std/atomic/queue_mpsc.zig
+++ /dev/null
@@ -1,185 +0,0 @@
-const std = @import("../index.zig");
-const assert = std.debug.assert;
-const builtin = @import("builtin");
-const AtomicOrder = builtin.AtomicOrder;
-const AtomicRmwOp = builtin.AtomicRmwOp;
-
-/// Many producer, single consumer, non-allocating, thread-safe, lock-free
-pub fn QueueMpsc(comptime T: type) type {
- return struct {
- inboxes: [2]std.atomic.Stack(T),
- outbox: std.atomic.Stack(T),
- inbox_index: usize,
-
- pub const Self = this;
-
- pub const Node = std.atomic.Stack(T).Node;
-
- /// Not thread-safe. The call to init() must complete before any other functions are called.
- /// No deinitialization required.
- pub fn init() Self {
- return Self{
- .inboxes = []std.atomic.Stack(T){
- std.atomic.Stack(T).init(),
- std.atomic.Stack(T).init(),
- },
- .outbox = std.atomic.Stack(T).init(),
- .inbox_index = 0,
- };
- }
-
- /// Fully thread-safe. put() may be called from any thread at any time.
- pub fn put(self: *Self, node: *Node) void {
- const inbox_index = @atomicLoad(usize, &self.inbox_index, AtomicOrder.SeqCst);
- const inbox = &self.inboxes[inbox_index];
- inbox.push(node);
- }
-
- /// Must be called by only 1 consumer at a time. Every call to get() and isEmpty() must complete before
- /// the next call to get().
- pub fn get(self: *Self) ?*Node {
- if (self.outbox.pop()) |node| {
- return node;
- }
- const prev_inbox_index = @atomicRmw(usize, &self.inbox_index, AtomicRmwOp.Xor, 0x1, AtomicOrder.SeqCst);
- const prev_inbox = &self.inboxes[prev_inbox_index];
- while (prev_inbox.pop()) |node| {
- self.outbox.push(node);
- }
- return self.outbox.pop();
- }
-
- /// Must be called by only 1 consumer at a time. Every call to get() and isEmpty() must complete before
- /// the next call to isEmpty().
- pub fn isEmpty(self: *Self) bool {
- if (!self.outbox.isEmpty()) return false;
- const prev_inbox_index = @atomicRmw(usize, &self.inbox_index, AtomicRmwOp.Xor, 0x1, AtomicOrder.SeqCst);
- const prev_inbox = &self.inboxes[prev_inbox_index];
- while (prev_inbox.pop()) |node| {
- self.outbox.push(node);
- }
- return self.outbox.isEmpty();
- }
-
- /// For debugging only. No API guarantees about what this does.
- pub fn dump(self: *Self) void {
- {
- var it = self.outbox.root;
- while (it) |node| {
- std.debug.warn("0x{x} -> ", @ptrToInt(node));
- it = node.next;
- }
- }
- const inbox_index = self.inbox_index;
- const inboxes = []*std.atomic.Stack(T){
- &self.inboxes[self.inbox_index],
- &self.inboxes[1 - self.inbox_index],
- };
- for (inboxes) |inbox| {
- var it = inbox.root;
- while (it) |node| {
- std.debug.warn("0x{x} -> ", @ptrToInt(node));
- it = node.next;
- }
- }
-
- std.debug.warn("null\n");
- }
- };
-}
-
-const Context = struct {
- allocator: *std.mem.Allocator,
- queue: *QueueMpsc(i32),
- put_sum: isize,
- get_sum: isize,
- get_count: usize,
- puts_done: u8, // TODO make this a bool
-};
-
-// TODO add lazy evaluated build options and then put puts_per_thread behind
-// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
-// CI we would use a less aggressive setting since at 1 core, while we still
-// want this test to pass, we need a smaller value since there is so much thrashing
-// we would also use a less aggressive setting when running in valgrind
-const puts_per_thread = 500;
-const put_thread_count = 3;
-
-test "std.atomic.queue_mpsc" {
- var direct_allocator = std.heap.DirectAllocator.init();
- defer direct_allocator.deinit();
-
- var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
- defer direct_allocator.allocator.free(plenty_of_memory);
-
- var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
- var a = &fixed_buffer_allocator.allocator;
-
- var queue = QueueMpsc(i32).init();
- var context = Context{
- .allocator = a,
- .queue = &queue,
- .put_sum = 0,
- .get_sum = 0,
- .puts_done = 0,
- .get_count = 0,
- };
-
- var putters: [put_thread_count]*std.os.Thread = undefined;
- for (putters) |*t| {
- t.* = try std.os.spawnThread(&context, startPuts);
- }
- var getters: [1]*std.os.Thread = undefined;
- for (getters) |*t| {
- t.* = try std.os.spawnThread(&context, startGets);
- }
-
- for (putters) |t|
- t.wait();
- _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
- for (getters) |t|
- t.wait();
-
- if (context.put_sum != context.get_sum) {
- std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum);
- }
-
- if (context.get_count != puts_per_thread * put_thread_count) {
- std.debug.panic(
- "failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}",
- context.get_count,
- u32(puts_per_thread),
- u32(put_thread_count),
- );
- }
-}
-
-fn startPuts(ctx: *Context) u8 {
- var put_count: usize = puts_per_thread;
- var r = std.rand.DefaultPrng.init(0xdeadbeef);
- while (put_count != 0) : (put_count -= 1) {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
- const x = @bitCast(i32, r.random.scalar(u32));
- const node = ctx.allocator.create(QueueMpsc(i32).Node{
- .next = undefined,
- .data = x,
- }) catch unreachable;
- ctx.queue.put(node);
- _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
- }
- return 0;
-}
-
-fn startGets(ctx: *Context) u8 {
- while (true) {
- const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
-
- while (ctx.queue.get()) |node| {
- std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
- _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
- _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
- }
-
- if (last) return 0;
- }
-}
diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig
index d74bee8e8b..16d5c9503b 100644
--- a/std/atomic/stack.zig
+++ b/std/atomic/stack.zig
@@ -1,10 +1,13 @@
+const assert = std.debug.assert;
const builtin = @import("builtin");
const AtomicOrder = builtin.AtomicOrder;
-/// Many reader, many writer, non-allocating, thread-safe, lock-free
+/// Many reader, many writer, non-allocating, thread-safe
+/// Uses a spinlock to protect push() and pop()
pub fn Stack(comptime T: type) type {
return struct {
root: ?*Node,
+ lock: u8,
pub const Self = this;
@@ -14,7 +17,10 @@ pub fn Stack(comptime T: type) type {
};
pub fn init() Self {
- return Self{ .root = null };
+ return Self{
+ .root = null,
+ .lock = 0,
+ };
}
/// push operation, but only if you are the first item in the stack. if you did not succeed in
@@ -25,18 +31,20 @@ pub fn Stack(comptime T: type) type {
}
pub fn push(self: *Self, node: *Node) void {
- var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
- while (true) {
- node.next = root;
- root = @cmpxchgWeak(?*Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse break;
- }
+ while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
+ defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
+
+ node.next = self.root;
+ self.root = node;
}
pub fn pop(self: *Self) ?*Node {
- var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
- while (true) {
- root = @cmpxchgWeak(?*Node, &self.root, root, (root orelse return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return root;
- }
+ while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
+ defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
+
+ const root = self.root orelse return null;
+ self.root = root.next;
+ return root;
}
pub fn isEmpty(self: *Self) bool {
@@ -45,7 +53,7 @@ pub fn Stack(comptime T: type) type {
};
}
-const std = @import("std");
+const std = @import("../index.zig");
const Context = struct {
allocator: *std.mem.Allocator,
stack: *Stack(i32),
diff --git a/std/event/channel.zig b/std/event/channel.zig
index 4b3a7177a2..d4d713bdee 100644
--- a/std/event/channel.zig
+++ b/std/event/channel.zig
@@ -12,8 +12,8 @@ pub fn Channel(comptime T: type) type {
return struct {
loop: *Loop,
- getters: std.atomic.QueueMpsc(GetNode),
- putters: std.atomic.QueueMpsc(PutNode),
+ getters: std.atomic.Queue(GetNode),
+ putters: std.atomic.Queue(PutNode),
get_count: usize,
put_count: usize,
dispatch_lock: u8, // TODO make this a bool
@@ -46,8 +46,8 @@ pub fn Channel(comptime T: type) type {
.buffer_index = 0,
.dispatch_lock = 0,
.need_dispatch = 0,
- .getters = std.atomic.QueueMpsc(GetNode).init(),
- .putters = std.atomic.QueueMpsc(PutNode).init(),
+ .getters = std.atomic.Queue(GetNode).init(),
+ .putters = std.atomic.Queue(PutNode).init(),
.get_count = 0,
.put_count = 0,
});
@@ -81,7 +81,7 @@ pub fn Channel(comptime T: type) type {
.next = undefined,
.data = handle,
};
- var queue_node = std.atomic.QueueMpsc(PutNode).Node{
+ var queue_node = std.atomic.Queue(PutNode).Node{
.data = PutNode{
.tick_node = &my_tick_node,
.data = data,
@@ -111,7 +111,7 @@ pub fn Channel(comptime T: type) type {
.next = undefined,
.data = handle,
};
- var queue_node = std.atomic.QueueMpsc(GetNode).Node{
+ var queue_node = std.atomic.Queue(GetNode).Node{
.data = GetNode{
.ptr = &result,
.tick_node = &my_tick_node,
diff --git a/std/event/future.zig b/std/event/future.zig
index 8001f675a2..6c03641828 100644
--- a/std/event/future.zig
+++ b/std/event/future.zig
@@ -17,7 +17,7 @@ pub fn Future(comptime T: type) type {
available: u8, // TODO make this a bool
const Self = this;
- const Queue = std.atomic.QueueMpsc(promise);
+ const Queue = std.atomic.Queue(promise);
pub fn init(loop: *Loop) Self {
return Self{
@@ -30,19 +30,19 @@ pub fn Future(comptime T: type) type {
/// Obtain the value. If it's not available, wait until it becomes
/// available.
/// Thread-safe.
- pub async fn get(self: *Self) T {
+ pub async fn get(self: *Self) *T {
if (@atomicLoad(u8, &self.available, AtomicOrder.SeqCst) == 1) {
- return self.data;
+ return &self.data;
}
const held = await (async self.lock.acquire() catch unreachable);
- defer held.release();
+ held.release();
- return self.data;
+ return &self.data;
}
/// Make the data become available. May be called only once.
- pub fn put(self: *Self, value: T) void {
- self.data = value;
+ /// Before calling this, modify the `data` property.
+ pub fn resolve(self: *Self) void {
const prev = @atomicRmw(u8, &self.available, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
assert(prev == 0); // put() called twice
Lock.Held.release(Lock.Held{ .lock = &self.lock });
@@ -57,7 +57,7 @@ test "std.event.Future" {
const allocator = &da.allocator;
var loop: Loop = undefined;
- try loop.initMultiThreaded(allocator);
+ try loop.initSingleThreaded(allocator);
defer loop.deinit();
const handle = try async testFuture(&loop);
@@ -79,9 +79,10 @@ async fn testFuture(loop: *Loop) void {
}
async fn waitOnFuture(future: *Future(i32)) i32 {
- return await (async future.get() catch @panic("memory"));
+ return (await (async future.get() catch @panic("memory"))).*;
}
async fn resolveFuture(future: *Future(i32)) void {
- future.put(6);
+ future.data = 6;
+ future.resolve();
}
diff --git a/std/event/lock.zig b/std/event/lock.zig
index cba3594b50..2013b5595f 100644
--- a/std/event/lock.zig
+++ b/std/event/lock.zig
@@ -15,7 +15,7 @@ pub const Lock = struct {
queue: Queue,
queue_empty_bit: u8, // TODO make this a bool
- const Queue = std.atomic.QueueMpsc(promise);
+ const Queue = std.atomic.Queue(promise);
pub const Held = struct {
lock: *Lock,
diff --git a/std/event/loop.zig b/std/event/loop.zig
index 646f15875f..07575cf2e8 100644
--- a/std/event/loop.zig
+++ b/std/event/loop.zig
@@ -9,7 +9,7 @@ const AtomicOrder = builtin.AtomicOrder;
pub const Loop = struct {
allocator: *mem.Allocator,
- next_tick_queue: std.atomic.QueueMpsc(promise),
+ next_tick_queue: std.atomic.Queue(promise),
os_data: OsData,
final_resume_node: ResumeNode,
dispatch_lock: u8, // TODO make this a bool
@@ -21,7 +21,7 @@ pub const Loop = struct {
available_eventfd_resume_nodes: std.atomic.Stack(ResumeNode.EventFd),
eventfd_resume_nodes: []std.atomic.Stack(ResumeNode.EventFd).Node,
- pub const NextTickNode = std.atomic.QueueMpsc(promise).Node;
+ pub const NextTickNode = std.atomic.Queue(promise).Node;
pub const ResumeNode = struct {
id: Id,
@@ -77,7 +77,7 @@ pub const Loop = struct {
.pending_event_count = 0,
.allocator = allocator,
.os_data = undefined,
- .next_tick_queue = std.atomic.QueueMpsc(promise).init(),
+ .next_tick_queue = std.atomic.Queue(promise).init(),
.dispatch_lock = 1, // start locked so threads go directly into epoll wait
.extra_threads = undefined,
.available_eventfd_resume_nodes = std.atomic.Stack(ResumeNode.EventFd).init(),
diff --git a/test/tests.zig b/test/tests.zig
index b1453776a8..3a72f58753 100644
--- a/test/tests.zig
+++ b/test/tests.zig
@@ -47,12 +47,13 @@ const test_targets = []TestTarget{
const max_stdout_size = 1 * 1024 * 1024; // 1 MB
-pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
+pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
const cases = b.allocator.create(CompareOutputContext{
.b = b,
.step = b.step("test-compare-output", "Run the compare output tests"),
.test_index = 0,
.test_filter = test_filter,
+ .modes = modes,
}) catch unreachable;
compare_output.addCases(cases);
@@ -60,12 +61,13 @@ pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8) *build
return cases.step;
}
-pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
+pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
const cases = b.allocator.create(CompareOutputContext{
.b = b,
.step = b.step("test-runtime-safety", "Run the runtime safety tests"),
.test_index = 0,
.test_filter = test_filter,
+ .modes = modes,
}) catch unreachable;
runtime_safety.addCases(cases);
@@ -73,12 +75,13 @@ pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8) *build
return cases.step;
}
-pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
+pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
const cases = b.allocator.create(CompileErrorContext{
.b = b,
.step = b.step("test-compile-errors", "Run the compile error tests"),
.test_index = 0,
.test_filter = test_filter,
+ .modes = modes,
}) catch unreachable;
compile_errors.addCases(cases);
@@ -99,12 +102,13 @@ pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8) *build.
return cases.step;
}
-pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
+pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
const cases = b.allocator.create(CompareOutputContext{
.b = b,
.step = b.step("test-asm-link", "Run the assemble and link tests"),
.test_index = 0,
.test_filter = test_filter,
+ .modes = modes,
}) catch unreachable;
assemble_and_link.addCases(cases);
@@ -173,6 +177,7 @@ pub const CompareOutputContext = struct {
step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
+ modes: []const Mode,
const Special = enum {
None,
@@ -423,12 +428,7 @@ pub const CompareOutputContext = struct {
self.step.dependOn(&run_and_cmp_output.step);
},
Special.None => {
- for ([]Mode{
- Mode.Debug,
- Mode.ReleaseSafe,
- Mode.ReleaseFast,
- Mode.ReleaseSmall,
- }) |mode| {
+ for (self.modes) |mode| {
const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {} ({})", "compare-output", case.name, @tagName(mode)) catch unreachable;
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
@@ -483,6 +483,7 @@ pub const CompileErrorContext = struct {
step: *build.Step,
test_index: usize,
test_filter: ?[]const u8,
+ modes: []const Mode,
const TestCase = struct {
name: []const u8,
@@ -673,10 +674,7 @@ pub const CompileErrorContext = struct {
pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
const b = self.b;
- for ([]Mode{
- Mode.Debug,
- Mode.ReleaseFast,
- }) |mode| {
+ for (self.modes) |mode| {
const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {} ({})", case.name, @tagName(mode)) catch unreachable;
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
--
cgit v1.2.3
From 278829fc2cc23e55b09915ce07ce1ec2dbf7e68b Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Sat, 14 Jul 2018 15:45:15 -0400
Subject: self-hosted: adding a fn to an llvm module
---
src-self-hosted/codegen.zig | 61 ++++++++++++++++++
src-self-hosted/ir.zig | 9 +--
src-self-hosted/llvm.zig | 23 ++++++-
src-self-hosted/main.zig | 7 ++-
src-self-hosted/module.zig | 112 ++++++++++++++++++++++-----------
src-self-hosted/test.zig | 11 +++-
src-self-hosted/type.zig | 147 +++++++++++++++++++++++++++++++++++++++++++-
src-self-hosted/value.zig | 20 ++++--
std/atomic/int.zig | 18 ++++--
std/event/loop.zig | 1 -
10 files changed, 346 insertions(+), 63 deletions(-)
create mode 100644 src-self-hosted/codegen.zig
(limited to 'std/atomic')
diff --git a/src-self-hosted/codegen.zig b/src-self-hosted/codegen.zig
new file mode 100644
index 0000000000..df8f451856
--- /dev/null
+++ b/src-self-hosted/codegen.zig
@@ -0,0 +1,61 @@
+const std = @import("std");
+// TODO codegen pretends that Module is renamed to Build because I plan to
+// do that refactor at some point
+const Build = @import("module.zig").Module;
+// we go through llvm instead of c for 2 reasons:
+// 1. to avoid accidentally calling the non-thread-safe functions
+// 2. patch up some of the types to remove nullability
+const llvm = @import("llvm.zig");
+const ir = @import("ir.zig");
+const Value = @import("value.zig").Value;
+const Type = @import("type.zig").Type;
+const event = std.event;
+
+pub async fn renderToLlvm(build: *Build, fn_val: *Value.Fn, code: *ir.Code) !void {
+ fn_val.base.ref();
+ defer fn_val.base.deref(build);
+ defer code.destroy(build.a());
+
+ const llvm_handle = try build.event_loop_local.getAnyLlvmContext();
+ defer llvm_handle.release(build.event_loop_local);
+
+ const context = llvm_handle.node.data;
+
+ const module = llvm.ModuleCreateWithNameInContext(build.name.ptr(), context) orelse return error.OutOfMemory;
+ defer llvm.DisposeModule(module);
+
+ const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
+ defer llvm.DisposeBuilder(builder);
+
+ var cunit = CompilationUnit{
+ .build = build,
+ .module = module,
+ .builder = builder,
+ .context = context,
+ .lock = event.Lock.init(build.loop),
+ };
+
+ try renderToLlvmModule(&cunit, fn_val, code);
+
+ if (build.verbose_llvm_ir) {
+ llvm.DumpModule(cunit.module);
+ }
+}
+
+pub const CompilationUnit = struct {
+ build: *Build,
+ module: llvm.ModuleRef,
+ builder: llvm.BuilderRef,
+ context: llvm.ContextRef,
+ lock: event.Lock,
+
+ fn a(self: *CompilationUnit) *std.mem.Allocator {
+ return self.build.a();
+ }
+};
+
+pub fn renderToLlvmModule(cunit: *CompilationUnit, fn_val: *Value.Fn, code: *ir.Code) !void {
+ // TODO audit more of codegen.cpp:fn_llvm_value and port more logic
+ const llvm_fn_type = try fn_val.base.typeof.getLlvmType(cunit);
+ const llvm_fn = llvm.AddFunction(cunit.module, fn_val.symbol_name.ptr(), llvm_fn_type);
+}
diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig
index 22161a0c27..f1c395a790 100644
--- a/src-self-hosted/ir.zig
+++ b/src-self-hosted/ir.zig
@@ -375,15 +375,8 @@ pub const Instruction = struct {
pub fn analyze(self: *const AddImplicitReturnType, ira: *Analyze) !*Instruction {
const target = try self.params.target.getAsParam();
-
try ira.src_implicit_return_type_list.append(target);
-
- return ira.irb.build(
- AddImplicitReturnType,
- self.base.scope,
- self.base.span,
- Params{ .target = target },
- );
+ return ira.irb.buildConstVoid(self.base.scope, self.base.span, true);
}
};
};
diff --git a/src-self-hosted/llvm.zig b/src-self-hosted/llvm.zig
index 391a92cd63..b815f75b05 100644
--- a/src-self-hosted/llvm.zig
+++ b/src-self-hosted/llvm.zig
@@ -2,10 +2,27 @@ const builtin = @import("builtin");
const c = @import("c.zig");
const assert = @import("std").debug.assert;
-pub const ValueRef = removeNullability(c.LLVMValueRef);
-pub const ModuleRef = removeNullability(c.LLVMModuleRef);
-pub const ContextRef = removeNullability(c.LLVMContextRef);
pub const BuilderRef = removeNullability(c.LLVMBuilderRef);
+pub const ContextRef = removeNullability(c.LLVMContextRef);
+pub const ModuleRef = removeNullability(c.LLVMModuleRef);
+pub const ValueRef = removeNullability(c.LLVMValueRef);
+pub const TypeRef = removeNullability(c.LLVMTypeRef);
+
+pub const AddFunction = c.LLVMAddFunction;
+pub const CreateBuilderInContext = c.LLVMCreateBuilderInContext;
+pub const DisposeBuilder = c.LLVMDisposeBuilder;
+pub const DisposeModule = c.LLVMDisposeModule;
+pub const DumpModule = c.LLVMDumpModule;
+pub const ModuleCreateWithNameInContext = c.LLVMModuleCreateWithNameInContext;
+pub const VoidTypeInContext = c.LLVMVoidTypeInContext;
+
+pub const FunctionType = LLVMFunctionType;
+extern fn LLVMFunctionType(
+ ReturnType: TypeRef,
+ ParamTypes: [*]TypeRef,
+ ParamCount: c_uint,
+ IsVarArg: c_int,
+) ?TypeRef;
fn removeNullability(comptime T: type) type {
comptime assert(@typeId(T) == builtin.TypeId.Optional);
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index 972aaae9ac..77ec7f6d32 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -14,6 +14,7 @@ const c = @import("c.zig");
const introspect = @import("introspect.zig");
const Args = arg.Args;
const Flag = arg.Flag;
+const EventLoopLocal = @import("module.zig").EventLoopLocal;
const Module = @import("module.zig").Module;
const Target = @import("target.zig").Target;
const errmsg = @import("errmsg.zig");
@@ -386,9 +387,13 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Mo
var loop: event.Loop = undefined;
try loop.initMultiThreaded(allocator);
+ defer loop.deinit();
+
+ var event_loop_local = EventLoopLocal.init(&loop);
+ defer event_loop_local.deinit();
var module = try Module.create(
- &loop,
+ &event_loop_local,
root_name,
root_source_file,
Target.Native,
diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig
index 3fbe6d54ad..617bd0d44a 100644
--- a/src-self-hosted/module.zig
+++ b/src-self-hosted/module.zig
@@ -25,14 +25,58 @@ const ParsedFile = @import("parsed_file.zig").ParsedFile;
const Value = @import("value.zig").Value;
const Type = Value.Type;
const Span = errmsg.Span;
+const codegen = @import("codegen.zig");
+
+/// Data that is local to the event loop.
+pub const EventLoopLocal = struct {
+ loop: *event.Loop,
+ llvm_handle_pool: std.atomic.Stack(llvm.ContextRef),
+
+ fn init(loop: *event.Loop) EventLoopLocal {
+ return EventLoopLocal{
+ .loop = loop,
+ .llvm_handle_pool = std.atomic.Stack(llvm.ContextRef).init(),
+ };
+ }
+
+ fn deinit(self: *EventLoopLocal) void {
+ while (self.llvm_handle_pool.pop()) |node| {
+ c.LLVMContextDispose(node.data);
+ self.loop.allocator.destroy(node);
+ }
+ }
+
+ /// Gets an exclusive handle on any LlvmContext.
+ /// Caller must release the handle when done.
+ pub fn getAnyLlvmContext(self: *EventLoopLocal) !LlvmHandle {
+ if (self.llvm_handle_pool.pop()) |node| return LlvmHandle{ .node = node };
+
+ const context_ref = c.LLVMContextCreate() orelse return error.OutOfMemory;
+ errdefer c.LLVMContextDispose(context_ref);
+
+ const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node{
+ .next = undefined,
+ .data = context_ref,
+ });
+ errdefer self.loop.allocator.destroy(node);
+
+ return LlvmHandle{ .node = node };
+ }
+};
+
+pub const LlvmHandle = struct {
+ node: *std.atomic.Stack(llvm.ContextRef).Node,
+
+ pub fn release(self: LlvmHandle, event_loop_local: *EventLoopLocal) void {
+ event_loop_local.llvm_handle_pool.push(self.node);
+ }
+};
pub const Module = struct {
+ event_loop_local: *EventLoopLocal,
loop: *event.Loop,
name: Buffer,
root_src_path: ?[]const u8,
- llvm_module: llvm.ModuleRef,
- context: llvm.ContextRef,
- builder: llvm.BuilderRef,
target: Target,
build_mode: builtin.Mode,
zig_lib_dir: []const u8,
@@ -187,7 +231,7 @@ pub const Module = struct {
};
pub fn create(
- loop: *event.Loop,
+ event_loop_local: *EventLoopLocal,
name: []const u8,
root_src_path: ?[]const u8,
target: *const Target,
@@ -196,29 +240,20 @@ pub const Module = struct {
zig_lib_dir: []const u8,
cache_dir: []const u8,
) !*Module {
+ const loop = event_loop_local.loop;
+
var name_buffer = try Buffer.init(loop.allocator, name);
errdefer name_buffer.deinit();
- const context = c.LLVMContextCreate() orelse return error.OutOfMemory;
- errdefer c.LLVMContextDispose(context);
-
- const llvm_module = c.LLVMModuleCreateWithNameInContext(name_buffer.ptr(), context) orelse return error.OutOfMemory;
- errdefer c.LLVMDisposeModule(llvm_module);
-
- const builder = c.LLVMCreateBuilderInContext(context) orelse return error.OutOfMemory;
- errdefer c.LLVMDisposeBuilder(builder);
-
const events = try event.Channel(Event).create(loop, 0);
errdefer events.destroy();
const module = try loop.allocator.create(Module{
.loop = loop,
+ .event_loop_local = event_loop_local,
.events = events,
.name = name_buffer,
.root_src_path = root_src_path,
- .llvm_module = llvm_module,
- .context = context,
- .builder = builder,
.target = target.*,
.kind = kind,
.build_mode = build_mode,
@@ -290,7 +325,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Type,
.typeof = undefined,
- .ref_count = 3, // 3 because it references itself twice
+ .ref_count = std.atomic.Int(usize).init(3), // 3 because it references itself twice
},
.id = builtin.TypeId.Type,
},
@@ -305,7 +340,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Type,
.typeof = &Type.MetaType.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.id = builtin.TypeId.Void,
},
@@ -317,7 +352,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Type,
.typeof = &Type.MetaType.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.id = builtin.TypeId.NoReturn,
},
@@ -329,7 +364,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Type,
.typeof = &Type.MetaType.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.id = builtin.TypeId.Bool,
},
@@ -340,7 +375,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Void,
.typeof = &Type.Void.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
});
errdefer module.a().destroy(module.void_value);
@@ -349,7 +384,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Bool,
.typeof = &Type.Bool.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.x = true,
});
@@ -359,7 +394,7 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.Bool,
.typeof = &Type.Bool.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.x = false,
});
@@ -369,16 +404,12 @@ pub const Module = struct {
.base = Value{
.id = Value.Id.NoReturn,
.typeof = &Type.NoReturn.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
});
errdefer module.a().destroy(module.noreturn_value);
}
- fn dump(self: *Module) void {
- c.LLVMDumpModule(self.module);
- }
-
pub fn destroy(self: *Module) void {
self.noreturn_value.base.deref(self);
self.void_value.base.deref(self);
@@ -389,9 +420,6 @@ pub const Module = struct {
self.meta_type.base.base.deref(self);
self.events.destroy();
- c.LLVMDisposeBuilder(self.builder);
- c.LLVMDisposeModule(self.llvm_module);
- c.LLVMContextDispose(self.context);
self.name.deinit();
self.a().destroy(self);
@@ -657,10 +685,19 @@ async fn generateDeclFn(module: *Module, fn_decl: *Decl.Fn) !void {
const fndef_scope = try Scope.FnDef.create(module, fn_decl.base.parent_scope);
defer fndef_scope.base.deref(module);
- const fn_type = try Type.Fn.create(module);
+ // TODO actually look at the return type of the AST
+ const return_type = &Type.Void.get(module).base;
+ defer return_type.base.deref(module);
+
+ const is_var_args = false;
+ const params = ([*]Type.Fn.Param)(undefined)[0..0];
+ const fn_type = try Type.Fn.create(module, return_type, params, is_var_args);
defer fn_type.base.base.deref(module);
- const fn_val = try Value.Fn.create(module, fn_type, fndef_scope);
+ var symbol_name = try std.Buffer.init(module.a(), fn_decl.base.name);
+ errdefer symbol_name.deinit();
+
+ const fn_val = try Value.Fn.create(module, fn_type, fndef_scope, symbol_name);
defer fn_val.base.deref(module);
fn_decl.value = Decl.Fn.Val{ .Ok = fn_val };
@@ -674,6 +711,7 @@ async fn generateDeclFn(module: *Module, fn_decl: *Decl.Fn) !void {
) catch unreachable)) catch |err| switch (err) {
// This poison value should not cause the errdefers to run. It simply means
// that self.compile_errors is populated.
+ // TODO https://github.com/ziglang/zig/issues/769
error.SemanticAnalysisFailed => return {},
else => return err,
};
@@ -692,14 +730,18 @@ async fn generateDeclFn(module: *Module, fn_decl: *Decl.Fn) !void {
) catch unreachable)) catch |err| switch (err) {
// This poison value should not cause the errdefers to run. It simply means
// that self.compile_errors is populated.
+ // TODO https://github.com/ziglang/zig/issues/769
error.SemanticAnalysisFailed => return {},
else => return err,
};
- defer analyzed_code.destroy(module.a());
+ errdefer analyzed_code.destroy(module.a());
if (module.verbose_ir) {
std.debug.warn("analyzed:\n");
analyzed_code.dump();
}
- // TODO now render to LLVM module
+
+ // Kick off rendering to LLVM module, but it doesn't block the fn decl
+ // analysis from being complete.
+ try module.build_group.call(codegen.renderToLlvm, module, fn_val, analyzed_code);
}
diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig
index 4455352f95..e609eb2791 100644
--- a/src-self-hosted/test.zig
+++ b/src-self-hosted/test.zig
@@ -6,6 +6,7 @@ const Module = @import("module.zig").Module;
const introspect = @import("introspect.zig");
const assertOrPanic = std.debug.assertOrPanic;
const errmsg = @import("errmsg.zig");
+const EventLoopLocal = @import("module.zig").EventLoopLocal;
test "compile errors" {
var ctx: TestContext = undefined;
@@ -22,6 +23,7 @@ const allocator = std.heap.c_allocator;
pub const TestContext = struct {
loop: std.event.Loop,
+ event_loop_local: EventLoopLocal,
zig_lib_dir: []u8,
zig_cache_dir: []u8,
file_index: std.atomic.Int(usize),
@@ -34,6 +36,7 @@ pub const TestContext = struct {
self.* = TestContext{
.any_err = {},
.loop = undefined,
+ .event_loop_local = undefined,
.zig_lib_dir = undefined,
.zig_cache_dir = undefined,
.group = undefined,
@@ -43,6 +46,9 @@ pub const TestContext = struct {
try self.loop.initMultiThreaded(allocator);
errdefer self.loop.deinit();
+ self.event_loop_local = EventLoopLocal.init(&self.loop);
+ errdefer self.event_loop_local.deinit();
+
self.group = std.event.Group(error!void).init(&self.loop);
errdefer self.group.cancelAll();
@@ -60,6 +66,7 @@ pub const TestContext = struct {
std.os.deleteTree(allocator, tmp_dir_name) catch {};
allocator.free(self.zig_cache_dir);
allocator.free(self.zig_lib_dir);
+ self.event_loop_local.deinit();
self.loop.deinit();
}
@@ -83,7 +90,7 @@ pub const TestContext = struct {
msg: []const u8,
) !void {
var file_index_buf: [20]u8 = undefined;
- const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.next());
+ const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
const file1_path = try std.os.path.join(allocator, tmp_dir_name, file_index, file1);
if (std.os.path.dirname(file1_path)) |dirname| {
@@ -94,7 +101,7 @@ pub const TestContext = struct {
try std.io.writeFile(allocator, file1_path, source);
var module = try Module.create(
- &self.loop,
+ &self.event_loop_local,
"test",
file1_path,
Target.Native,
diff --git a/src-self-hosted/type.zig b/src-self-hosted/type.zig
index 66e1470cc0..e4c31018a3 100644
--- a/src-self-hosted/type.zig
+++ b/src-self-hosted/type.zig
@@ -1,7 +1,10 @@
+const std = @import("std");
const builtin = @import("builtin");
const Scope = @import("scope.zig").Scope;
const Module = @import("module.zig").Module;
const Value = @import("value.zig").Value;
+const llvm = @import("llvm.zig");
+const CompilationUnit = @import("codegen.zig").CompilationUnit;
pub const Type = struct {
base: Value,
@@ -39,6 +42,36 @@ pub const Type = struct {
}
}
+ pub fn getLlvmType(base: *Type, cunit: *CompilationUnit) (error{OutOfMemory}!llvm.TypeRef) {
+ switch (base.id) {
+ Id.Struct => return @fieldParentPtr(Struct, "base", base).getLlvmType(cunit),
+ Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmType(cunit),
+ Id.Type => unreachable,
+ Id.Void => unreachable,
+ Id.Bool => return @fieldParentPtr(Bool, "base", base).getLlvmType(cunit),
+ Id.NoReturn => unreachable,
+ Id.Int => return @fieldParentPtr(Int, "base", base).getLlvmType(cunit),
+ Id.Float => return @fieldParentPtr(Float, "base", base).getLlvmType(cunit),
+ Id.Pointer => return @fieldParentPtr(Pointer, "base", base).getLlvmType(cunit),
+ Id.Array => return @fieldParentPtr(Array, "base", base).getLlvmType(cunit),
+ Id.ComptimeFloat => unreachable,
+ Id.ComptimeInt => unreachable,
+ Id.Undefined => unreachable,
+ Id.Null => unreachable,
+ Id.Optional => return @fieldParentPtr(Optional, "base", base).getLlvmType(cunit),
+ Id.ErrorUnion => return @fieldParentPtr(ErrorUnion, "base", base).getLlvmType(cunit),
+ Id.ErrorSet => return @fieldParentPtr(ErrorSet, "base", base).getLlvmType(cunit),
+ Id.Enum => return @fieldParentPtr(Enum, "base", base).getLlvmType(cunit),
+ Id.Union => return @fieldParentPtr(Union, "base", base).getLlvmType(cunit),
+ Id.Namespace => unreachable,
+ Id.Block => unreachable,
+ Id.BoundFn => return @fieldParentPtr(BoundFn, "base", base).getLlvmType(cunit),
+ Id.ArgTuple => unreachable,
+ Id.Opaque => return @fieldParentPtr(Opaque, "base", base).getLlvmType(cunit),
+ Id.Promise => return @fieldParentPtr(Promise, "base", base).getLlvmType(cunit),
+ }
+ }
+
pub fn dump(base: *const Type) void {
std.debug.warn("{}", @tagName(base.id));
}
@@ -54,27 +87,72 @@ pub const Type = struct {
pub fn destroy(self: *Struct, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Struct, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
pub const Fn = struct {
base: Type,
+ return_type: *Type,
+ params: []Param,
+ is_var_args: bool,
- pub fn create(module: *Module) !*Fn {
- return module.a().create(Fn{
+ pub const Param = struct {
+ is_noalias: bool,
+ typeof: *Type,
+ };
+
+ pub fn create(module: *Module, return_type: *Type, params: []Param, is_var_args: bool) !*Fn {
+ const result = try module.a().create(Fn{
.base = Type{
.base = Value{
.id = Value.Id.Type,
.typeof = &MetaType.get(module).base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.id = builtin.TypeId.Fn,
},
+ .return_type = return_type,
+ .params = params,
+ .is_var_args = is_var_args,
});
+ errdefer module.a().destroy(result);
+
+ result.return_type.base.ref();
+ for (result.params) |param| {
+ param.typeof.base.ref();
+ }
+ return result;
}
pub fn destroy(self: *Fn, module: *Module) void {
+ self.return_type.base.deref(module);
+ for (self.params) |param| {
+ param.typeof.base.deref(module);
+ }
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Fn, cunit: *CompilationUnit) !llvm.TypeRef {
+ const llvm_return_type = switch (self.return_type.id) {
+ Type.Id.Void => llvm.VoidTypeInContext(cunit.context) orelse return error.OutOfMemory,
+ else => try self.return_type.getLlvmType(cunit),
+ };
+ const llvm_param_types = try cunit.a().alloc(llvm.TypeRef, self.params.len);
+ defer cunit.a().free(llvm_param_types);
+ for (llvm_param_types) |*llvm_param_type, i| {
+ llvm_param_type.* = try self.params[i].typeof.getLlvmType(cunit);
+ }
+
+ return llvm.FunctionType(
+ llvm_return_type,
+ llvm_param_types.ptr,
+ @intCast(c_uint, llvm_param_types.len),
+ @boolToInt(self.is_var_args),
+ ) orelse error.OutOfMemory;
+ }
};
pub const MetaType = struct {
@@ -118,6 +196,10 @@ pub const Type = struct {
pub fn destroy(self: *Bool, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Bool, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
pub const NoReturn = struct {
@@ -140,6 +222,10 @@ pub const Type = struct {
pub fn destroy(self: *Int, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Int, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
pub const Float = struct {
@@ -148,6 +234,10 @@ pub const Type = struct {
pub fn destroy(self: *Float, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Float, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
pub const Pointer = struct {
base: Type,
@@ -180,14 +270,24 @@ pub const Type = struct {
) *Pointer {
@panic("TODO get pointer");
}
+
+ pub fn getLlvmType(self: *Pointer, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const Array = struct {
base: Type,
pub fn destroy(self: *Array, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Array, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const ComptimeFloat = struct {
base: Type,
@@ -195,6 +295,7 @@ pub const Type = struct {
module.a().destroy(self);
}
};
+
pub const ComptimeInt = struct {
base: Type,
@@ -202,6 +303,7 @@ pub const Type = struct {
module.a().destroy(self);
}
};
+
pub const Undefined = struct {
base: Type,
@@ -209,6 +311,7 @@ pub const Type = struct {
module.a().destroy(self);
}
};
+
pub const Null = struct {
base: Type,
@@ -216,41 +319,67 @@ pub const Type = struct {
module.a().destroy(self);
}
};
+
pub const Optional = struct {
base: Type,
pub fn destroy(self: *Optional, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Optional, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const ErrorUnion = struct {
base: Type,
pub fn destroy(self: *ErrorUnion, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *ErrorUnion, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const ErrorSet = struct {
base: Type,
pub fn destroy(self: *ErrorSet, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *ErrorSet, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const Enum = struct {
base: Type,
pub fn destroy(self: *Enum, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Enum, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const Union = struct {
base: Type,
pub fn destroy(self: *Union, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Union, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
+
pub const Namespace = struct {
base: Type,
@@ -273,6 +402,10 @@ pub const Type = struct {
pub fn destroy(self: *BoundFn, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *BoundFn, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
pub const ArgTuple = struct {
@@ -289,6 +422,10 @@ pub const Type = struct {
pub fn destroy(self: *Opaque, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Opaque, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
pub const Promise = struct {
@@ -297,5 +434,9 @@ pub const Type = struct {
pub fn destroy(self: *Promise, module: *Module) void {
module.a().destroy(self);
}
+
+ pub fn getLlvmType(self: *Promise, cunit: *CompilationUnit) llvm.TypeRef {
+ @panic("TODO");
+ }
};
};
diff --git a/src-self-hosted/value.zig b/src-self-hosted/value.zig
index 7ee594b41c..779e5c2e45 100644
--- a/src-self-hosted/value.zig
+++ b/src-self-hosted/value.zig
@@ -8,15 +8,16 @@ const Module = @import("module.zig").Module;
pub const Value = struct {
id: Id,
typeof: *Type,
- ref_count: usize,
+ ref_count: std.atomic.Int(usize),
+ /// Thread-safe
pub fn ref(base: *Value) void {
- base.ref_count += 1;
+ _ = base.ref_count.incr();
}
+ /// Thread-safe
pub fn deref(base: *Value, module: *Module) void {
- base.ref_count -= 1;
- if (base.ref_count == 0) {
+ if (base.ref_count.decr() == 1) {
base.typeof.base.deref(module);
switch (base.id) {
Id.Type => @fieldParentPtr(Type, "base", base).destroy(module),
@@ -52,6 +53,10 @@ pub const Value = struct {
pub const Fn = struct {
base: Value,
+ /// The main external name that is used in the .o file.
+ /// TODO https://github.com/ziglang/zig/issues/265
+ symbol_name: std.Buffer,
+
/// parent should be the top level decls or container decls
fndef_scope: *Scope.FnDef,
@@ -62,16 +67,18 @@ pub const Value = struct {
block_scope: *Scope.Block,
/// Creates a Fn value with 1 ref
- pub fn create(module: *Module, fn_type: *Type.Fn, fndef_scope: *Scope.FnDef) !*Fn {
+ /// Takes ownership of symbol_name
+ pub fn create(module: *Module, fn_type: *Type.Fn, fndef_scope: *Scope.FnDef, symbol_name: std.Buffer) !*Fn {
const self = try module.a().create(Fn{
.base = Value{
.id = Value.Id.Fn,
.typeof = &fn_type.base,
- .ref_count = 1,
+ .ref_count = std.atomic.Int(usize).init(1),
},
.fndef_scope = fndef_scope,
.child_scope = &fndef_scope.base,
.block_scope = undefined,
+ .symbol_name = symbol_name,
});
fn_type.base.base.ref();
fndef_scope.fn_val = self;
@@ -81,6 +88,7 @@ pub const Value = struct {
pub fn destroy(self: *Fn, module: *Module) void {
self.fndef_scope.base.deref(module);
+ self.symbol_name.deinit();
module.a().destroy(self);
}
};
diff --git a/std/atomic/int.zig b/std/atomic/int.zig
index 7042bca78d..d51454c673 100644
--- a/std/atomic/int.zig
+++ b/std/atomic/int.zig
@@ -4,16 +4,26 @@ const AtomicOrder = builtin.AtomicOrder;
/// Thread-safe, lock-free integer
pub fn Int(comptime T: type) type {
return struct {
- value: T,
+ unprotected_value: T,
pub const Self = this;
pub fn init(init_val: T) Self {
- return Self{ .value = init_val };
+ return Self{ .unprotected_value = init_val };
}
- pub fn next(self: *Self) T {
- return @atomicRmw(T, &self.value, builtin.AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+ /// Returns previous value
+ pub fn incr(self: *Self) T {
+ return @atomicRmw(T, &self.unprotected_value, builtin.AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
+ }
+
+ /// Returns previous value
+ pub fn decr(self: *Self) T {
+ return @atomicRmw(T, &self.unprotected_value, builtin.AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ }
+
+ pub fn get(self: *Self) T {
+ return @atomicLoad(T, &self.unprotected_value, AtomicOrder.SeqCst);
}
};
}
diff --git a/std/event/loop.zig b/std/event/loop.zig
index ba75109a72..fc927592b9 100644
--- a/std/event/loop.zig
+++ b/std/event/loop.zig
@@ -101,7 +101,6 @@ pub const Loop = struct {
errdefer self.deinitOsData();
}
- /// must call stop before deinit
pub fn deinit(self: *Loop) void {
self.deinitOsData();
self.allocator.free(self.extra_threads);
--
cgit v1.2.3
From 97bfeac13f89e1b5a22fcd7d4705341b4c3e1950 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Mon, 16 Jul 2018 20:52:50 -0400
Subject: self-hosted: create tmp dir for .o files and emit .o file for fn
---
CMakeLists.txt | 1 +
src-self-hosted/codegen.zig | 86 ++++++++++-
src-self-hosted/compilation.zig | 335 ++++++++++++++++++++++++++++++++--------
src-self-hosted/ir.zig | 10 +-
src-self-hosted/llvm.zig | 75 ++++++++-
src-self-hosted/main.zig | 8 +-
src-self-hosted/package.zig | 29 ++++
src-self-hosted/scope.zig | 32 ++--
src-self-hosted/target.zig | 116 ++++++++++----
src-self-hosted/test.zig | 3 +-
src-self-hosted/type.zig | 58 +++----
src-self-hosted/value.zig | 41 ++++-
src/zig_llvm.cpp | 5 +
src/zig_llvm.h | 3 +-
std/atomic/int.zig | 4 +
std/buffer.zig | 13 ++
std/dwarf.zig | 37 +++++
std/event/future.zig | 39 ++++-
std/index.zig | 3 +
std/lazy_init.zig | 85 ++++++++++
20 files changed, 808 insertions(+), 175 deletions(-)
create mode 100644 src-self-hosted/package.zig
create mode 100644 std/lazy_init.zig
(limited to 'std/atomic')
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e606855555..0e7c1df350 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -479,6 +479,7 @@ set(ZIG_STD_FILES
"index.zig"
"io.zig"
"json.zig"
+ "lazy_init.zig"
"linked_list.zig"
"macho.zig"
"math/acos.zig"
diff --git a/src-self-hosted/codegen.zig b/src-self-hosted/codegen.zig
index 698f1e5b45..28ba2a1564 100644
--- a/src-self-hosted/codegen.zig
+++ b/src-self-hosted/codegen.zig
@@ -1,19 +1,22 @@
const std = @import("std");
+const builtin = @import("builtin");
const Compilation = @import("compilation.zig").Compilation;
-// we go through llvm instead of c for 2 reasons:
-// 1. to avoid accidentally calling the non-thread-safe functions
-// 2. patch up some of the types to remove nullability
const llvm = @import("llvm.zig");
+const c = @import("c.zig");
const ir = @import("ir.zig");
const Value = @import("value.zig").Value;
const Type = @import("type.zig").Type;
const event = std.event;
const assert = std.debug.assert;
+const DW = std.dwarf;
pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) !void {
fn_val.base.ref();
defer fn_val.base.deref(comp);
- defer code.destroy(comp.a());
+ defer code.destroy(comp.gpa());
+
+ var output_path = try await (async comp.createRandomOutputPath(comp.target.oFileExt()) catch unreachable);
+ errdefer output_path.deinit();
const llvm_handle = try comp.event_loop_local.getAnyLlvmContext();
defer llvm_handle.release(comp.event_loop_local);
@@ -23,13 +26,56 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
const module = llvm.ModuleCreateWithNameInContext(comp.name.ptr(), context) orelse return error.OutOfMemory;
defer llvm.DisposeModule(module);
+ llvm.SetTarget(module, comp.llvm_triple.ptr());
+ llvm.SetDataLayout(module, comp.target_layout_str);
+
+ if (comp.target.getObjectFormat() == builtin.ObjectFormat.coff) {
+ llvm.AddModuleCodeViewFlag(module);
+ } else {
+ llvm.AddModuleDebugInfoFlag(module);
+ }
+
const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
defer llvm.DisposeBuilder(builder);
+ const dibuilder = llvm.CreateDIBuilder(module, true) orelse return error.OutOfMemory;
+ defer llvm.DisposeDIBuilder(dibuilder);
+
+ // Don't use ZIG_VERSION_STRING here. LLVM misparses it when it includes
+ // the git revision.
+ const producer = try std.Buffer.allocPrint(
+ &code.arena.allocator,
+ "zig {}.{}.{}",
+ u32(c.ZIG_VERSION_MAJOR),
+ u32(c.ZIG_VERSION_MINOR),
+ u32(c.ZIG_VERSION_PATCH),
+ );
+ const flags = c"";
+ const runtime_version = 0;
+ const compile_unit_file = llvm.CreateFile(
+ dibuilder,
+ comp.name.ptr(),
+ comp.root_package.root_src_dir.ptr(),
+ ) orelse return error.OutOfMemory;
+ const is_optimized = comp.build_mode != builtin.Mode.Debug;
+ const compile_unit = llvm.CreateCompileUnit(
+ dibuilder,
+ DW.LANG_C99,
+ compile_unit_file,
+ producer.ptr(),
+ is_optimized,
+ flags,
+ runtime_version,
+ c"",
+ 0,
+ !comp.strip,
+ ) orelse return error.OutOfMemory;
+
var ofile = ObjectFile{
.comp = comp,
.module = module,
.builder = builder,
+ .dibuilder = dibuilder,
.context = context,
.lock = event.Lock.init(comp.loop),
};
@@ -41,8 +87,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
// LLVMSetModuleInlineAsm(g->module, buf_ptr(&g->global_asm));
//}
- // TODO
- //ZigLLVMDIBuilderFinalize(g->dbuilder);
+ llvm.DIBuilderFinalize(dibuilder);
if (comp.verbose_llvm_ir) {
llvm.DumpModule(ofile.module);
@@ -53,17 +98,42 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
var error_ptr: ?[*]u8 = null;
_ = llvm.VerifyModule(ofile.module, llvm.AbortProcessAction, &error_ptr);
}
+
+ assert(comp.emit_file_type == Compilation.Emit.Binary); // TODO support other types
+
+ const is_small = comp.build_mode == builtin.Mode.ReleaseSmall;
+ const is_debug = comp.build_mode == builtin.Mode.Debug;
+
+ var err_msg: [*]u8 = undefined;
+ // TODO integrate this with evented I/O
+ if (llvm.TargetMachineEmitToFile(
+ comp.target_machine,
+ module,
+ output_path.ptr(),
+ llvm.EmitBinary,
+ &err_msg,
+ is_debug,
+ is_small,
+ )) {
+ if (std.debug.runtime_safety) {
+ std.debug.panic("unable to write object file {}: {s}\n", output_path.toSliceConst(), err_msg);
+ }
+ return error.WritingObjectFileFailed;
+ }
+ //validate_inline_fns(g); TODO
+ fn_val.containing_object = output_path;
}
pub const ObjectFile = struct {
comp: *Compilation,
module: llvm.ModuleRef,
builder: llvm.BuilderRef,
+ dibuilder: *llvm.DIBuilder,
context: llvm.ContextRef,
lock: event.Lock,
- fn a(self: *ObjectFile) *std.mem.Allocator {
- return self.comp.a();
+ fn gpa(self: *ObjectFile) *std.mem.Allocator {
+ return self.comp.gpa();
}
};
diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig
index 1dbbf21206..d5380a0644 100644
--- a/src-self-hosted/compilation.zig
+++ b/src-self-hosted/compilation.zig
@@ -26,16 +26,31 @@ const Value = @import("value.zig").Value;
const Type = Value.Type;
const Span = errmsg.Span;
const codegen = @import("codegen.zig");
+const Package = @import("package.zig").Package;
/// Data that is local to the event loop.
pub const EventLoopLocal = struct {
loop: *event.Loop,
llvm_handle_pool: std.atomic.Stack(llvm.ContextRef),
- fn init(loop: *event.Loop) EventLoopLocal {
+ /// TODO pool these so that it doesn't have to lock
+ prng: event.Locked(std.rand.DefaultPrng),
+
+ var lazy_init_targets = std.lazyInit(void);
+
+ fn init(loop: *event.Loop) !EventLoopLocal {
+ lazy_init_targets.get() orelse {
+ Target.initializeAll();
+ lazy_init_targets.resolve();
+ };
+
+ var seed_bytes: [@sizeOf(u64)]u8 = undefined;
+ try std.os.getRandomBytes(seed_bytes[0..]);
+ const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
return EventLoopLocal{
.loop = loop,
.llvm_handle_pool = std.atomic.Stack(llvm.ContextRef).init(),
+ .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),
};
}
@@ -76,10 +91,16 @@ pub const Compilation = struct {
event_loop_local: *EventLoopLocal,
loop: *event.Loop,
name: Buffer,
+ llvm_triple: Buffer,
root_src_path: ?[]const u8,
target: Target,
+ llvm_target: llvm.TargetRef,
build_mode: builtin.Mode,
zig_lib_dir: []const u8,
+ zig_std_dir: []const u8,
+
+ /// lazily created when we need it
+ tmp_dir: event.Future(BuildError![]u8),
version_major: u32,
version_minor: u32,
@@ -106,8 +127,16 @@ pub const Compilation = struct {
lib_dirs: []const []const u8,
rpath_list: []const []const u8,
assembly_files: []const []const u8,
+
+ /// paths that are explicitly provided by the user to link against
link_objects: []const []const u8,
+ /// functions that have their own objects that we need to link
+ /// it uses an optional pointer so that tombstone removals are possible
+ fn_link_set: event.Locked(FnLinkSet),
+
+ pub const FnLinkSet = std.LinkedList(?*Value.Fn);
+
windows_subsystem_windows: bool,
windows_subsystem_console: bool,
@@ -141,7 +170,7 @@ pub const Compilation = struct {
/// Before code generation starts, must wait on this group to make sure
/// the build is complete.
- build_group: event.Group(BuildError!void),
+ prelink_group: event.Group(BuildError!void),
compile_errors: event.Locked(CompileErrList),
@@ -155,6 +184,16 @@ pub const Compilation = struct {
false_value: *Value.Bool,
noreturn_value: *Value.NoReturn,
+ target_machine: llvm.TargetMachineRef,
+ target_data_ref: llvm.TargetDataRef,
+ target_layout_str: [*]u8,
+
+ /// for allocating things which have the same lifetime as this Compilation
+ arena_allocator: std.heap.ArenaAllocator,
+
+ root_package: *Package,
+ std_package: *Package,
+
const CompileErrList = std.ArrayList(*errmsg.Msg);
// TODO handle some of these earlier and report them in a way other than error codes
@@ -195,6 +234,9 @@ pub const Compilation = struct {
BufferTooSmall,
Unimplemented, // TODO remove this one
SemanticAnalysisFailed, // TODO remove this one
+ ReadOnlyFileSystem,
+ LinkQuotaExceeded,
+ EnvironmentVariableNotFound,
};
pub const Event = union(enum) {
@@ -234,31 +276,31 @@ pub const Compilation = struct {
event_loop_local: *EventLoopLocal,
name: []const u8,
root_src_path: ?[]const u8,
- target: *const Target,
+ target: Target,
kind: Kind,
build_mode: builtin.Mode,
+ is_static: bool,
zig_lib_dir: []const u8,
cache_dir: []const u8,
) !*Compilation {
const loop = event_loop_local.loop;
-
- var name_buffer = try Buffer.init(loop.allocator, name);
- errdefer name_buffer.deinit();
-
- const events = try event.Channel(Event).create(loop, 0);
- errdefer events.destroy();
-
- const comp = try loop.allocator.create(Compilation{
+ const comp = try event_loop_local.loop.allocator.create(Compilation{
.loop = loop,
+ .arena_allocator = std.heap.ArenaAllocator.init(loop.allocator),
.event_loop_local = event_loop_local,
- .events = events,
- .name = name_buffer,
+ .events = undefined,
.root_src_path = root_src_path,
- .target = target.*,
+ .target = target,
+ .llvm_target = undefined,
.kind = kind,
.build_mode = build_mode,
.zig_lib_dir = zig_lib_dir,
+ .zig_std_dir = undefined,
.cache_dir = cache_dir,
+ .tmp_dir = event.Future(BuildError![]u8).init(loop),
+
+ .name = undefined,
+ .llvm_triple = undefined,
.version_major = 0,
.version_minor = 0,
@@ -283,7 +325,7 @@ pub const Compilation = struct {
.is_test = false,
.each_lib_rpath = false,
.strip = false,
- .is_static = false,
+ .is_static = is_static,
.linker_rdynamic = false,
.clang_argv = [][]const u8{},
.llvm_argv = [][]const u8{},
@@ -291,9 +333,10 @@ pub const Compilation = struct {
.rpath_list = [][]const u8{},
.assembly_files = [][]const u8{},
.link_objects = [][]const u8{},
+ .fn_link_set = event.Locked(FnLinkSet).init(loop, FnLinkSet.init()),
.windows_subsystem_windows = false,
.windows_subsystem_console = false,
- .link_libs_list = ArrayList(*LinkLib).init(loop.allocator),
+ .link_libs_list = undefined,
.libc_link_lib = null,
.err_color = errmsg.Color.Auto,
.darwin_frameworks = [][]const u8{},
@@ -303,7 +346,7 @@ pub const Compilation = struct {
.emit_file_type = Emit.Binary,
.link_out_file = null,
.exported_symbol_names = event.Locked(Decl.Table).init(loop, Decl.Table.init(loop.allocator)),
- .build_group = event.Group(BuildError!void).init(loop),
+ .prelink_group = event.Group(BuildError!void).init(loop),
.compile_errors = event.Locked(CompileErrList).init(loop, CompileErrList.init(loop.allocator)),
.meta_type = undefined,
@@ -314,13 +357,82 @@ pub const Compilation = struct {
.false_value = undefined,
.noreturn_type = undefined,
.noreturn_value = undefined,
+
+ .target_machine = undefined,
+ .target_data_ref = undefined,
+ .target_layout_str = undefined,
+
+ .root_package = undefined,
+ .std_package = undefined,
});
+ errdefer {
+ comp.arena_allocator.deinit();
+ comp.loop.allocator.destroy(comp);
+ }
+
+ comp.name = try Buffer.init(comp.arena(), name);
+ comp.llvm_triple = try target.getTriple(comp.arena());
+ comp.llvm_target = try Target.llvmTargetFromTriple(comp.llvm_triple);
+ comp.link_libs_list = ArrayList(*LinkLib).init(comp.arena());
+ comp.zig_std_dir = try std.os.path.join(comp.arena(), zig_lib_dir, "std");
+
+ const opt_level = switch (build_mode) {
+ builtin.Mode.Debug => llvm.CodeGenLevelNone,
+ else => llvm.CodeGenLevelAggressive,
+ };
+
+ const reloc_mode = if (is_static) llvm.RelocStatic else llvm.RelocPIC;
+
+ // LLVM creates invalid binaries on Windows sometimes.
+ // See https://github.com/ziglang/zig/issues/508
+ // As a workaround we do not use target native features on Windows.
+ var target_specific_cpu_args: ?[*]u8 = null;
+ var target_specific_cpu_features: ?[*]u8 = null;
+ errdefer llvm.DisposeMessage(target_specific_cpu_args);
+ errdefer llvm.DisposeMessage(target_specific_cpu_features);
+ if (target == Target.Native and !target.isWindows()) {
+ target_specific_cpu_args = llvm.GetHostCPUName() orelse return error.OutOfMemory;
+ target_specific_cpu_features = llvm.GetNativeFeatures() orelse return error.OutOfMemory;
+ }
+
+ comp.target_machine = llvm.CreateTargetMachine(
+ comp.llvm_target,
+ comp.llvm_triple.ptr(),
+ target_specific_cpu_args orelse c"",
+ target_specific_cpu_features orelse c"",
+ opt_level,
+ reloc_mode,
+ llvm.CodeModelDefault,
+ ) orelse return error.OutOfMemory;
+ errdefer llvm.DisposeTargetMachine(comp.target_machine);
+
+ comp.target_data_ref = llvm.CreateTargetDataLayout(comp.target_machine) orelse return error.OutOfMemory;
+ errdefer llvm.DisposeTargetData(comp.target_data_ref);
+
+ comp.target_layout_str = llvm.CopyStringRepOfTargetData(comp.target_data_ref) orelse return error.OutOfMemory;
+ errdefer llvm.DisposeMessage(comp.target_layout_str);
+
+ comp.events = try event.Channel(Event).create(comp.loop, 0);
+ errdefer comp.events.destroy();
+
+ if (root_src_path) |root_src| {
+ const dirname = std.os.path.dirname(root_src) orelse ".";
+ const basename = std.os.path.basename(root_src);
+
+ comp.root_package = try Package.create(comp.arena(), dirname, basename);
+ comp.std_package = try Package.create(comp.arena(), comp.zig_std_dir, "index.zig");
+ try comp.root_package.add("std", comp.std_package);
+ } else {
+ comp.root_package = try Package.create(comp.arena(), ".", "");
+ }
+
try comp.initTypes();
+
return comp;
}
fn initTypes(comp: *Compilation) !void {
- comp.meta_type = try comp.a().create(Type.MetaType{
+ comp.meta_type = try comp.gpa().create(Type.MetaType{
.base = Type{
.base = Value{
.id = Value.Id.Type,
@@ -333,9 +445,9 @@ pub const Compilation = struct {
});
comp.meta_type.value = &comp.meta_type.base;
comp.meta_type.base.base.typeof = &comp.meta_type.base;
- errdefer comp.a().destroy(comp.meta_type);
+ errdefer comp.gpa().destroy(comp.meta_type);
- comp.void_type = try comp.a().create(Type.Void{
+ comp.void_type = try comp.gpa().create(Type.Void{
.base = Type{
.base = Value{
.id = Value.Id.Type,
@@ -345,9 +457,9 @@ pub const Compilation = struct {
.id = builtin.TypeId.Void,
},
});
- errdefer comp.a().destroy(comp.void_type);
+ errdefer comp.gpa().destroy(comp.void_type);
- comp.noreturn_type = try comp.a().create(Type.NoReturn{
+ comp.noreturn_type = try comp.gpa().create(Type.NoReturn{
.base = Type{
.base = Value{
.id = Value.Id.Type,
@@ -357,9 +469,9 @@ pub const Compilation = struct {
.id = builtin.TypeId.NoReturn,
},
});
- errdefer comp.a().destroy(comp.noreturn_type);
+ errdefer comp.gpa().destroy(comp.noreturn_type);
- comp.bool_type = try comp.a().create(Type.Bool{
+ comp.bool_type = try comp.gpa().create(Type.Bool{
.base = Type{
.base = Value{
.id = Value.Id.Type,
@@ -369,18 +481,18 @@ pub const Compilation = struct {
.id = builtin.TypeId.Bool,
},
});
- errdefer comp.a().destroy(comp.bool_type);
+ errdefer comp.gpa().destroy(comp.bool_type);
- comp.void_value = try comp.a().create(Value.Void{
+ comp.void_value = try comp.gpa().create(Value.Void{
.base = Value{
.id = Value.Id.Void,
.typeof = &Type.Void.get(comp).base,
.ref_count = std.atomic.Int(usize).init(1),
},
});
- errdefer comp.a().destroy(comp.void_value);
+ errdefer comp.gpa().destroy(comp.void_value);
- comp.true_value = try comp.a().create(Value.Bool{
+ comp.true_value = try comp.gpa().create(Value.Bool{
.base = Value{
.id = Value.Id.Bool,
.typeof = &Type.Bool.get(comp).base,
@@ -388,9 +500,9 @@ pub const Compilation = struct {
},
.x = true,
});
- errdefer comp.a().destroy(comp.true_value);
+ errdefer comp.gpa().destroy(comp.true_value);
- comp.false_value = try comp.a().create(Value.Bool{
+ comp.false_value = try comp.gpa().create(Value.Bool{
.base = Value{
.id = Value.Id.Bool,
.typeof = &Type.Bool.get(comp).base,
@@ -398,19 +510,23 @@ pub const Compilation = struct {
},
.x = false,
});
- errdefer comp.a().destroy(comp.false_value);
+ errdefer comp.gpa().destroy(comp.false_value);
- comp.noreturn_value = try comp.a().create(Value.NoReturn{
+ comp.noreturn_value = try comp.gpa().create(Value.NoReturn{
.base = Value{
.id = Value.Id.NoReturn,
.typeof = &Type.NoReturn.get(comp).base,
.ref_count = std.atomic.Int(usize).init(1),
},
});
- errdefer comp.a().destroy(comp.noreturn_value);
+ errdefer comp.gpa().destroy(comp.noreturn_value);
}
pub fn destroy(self: *Compilation) void {
+ if (self.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| {
+ os.deleteTree(self.arena(), tmp_dir) catch {};
+ } else |_| {};
+
self.noreturn_value.base.deref(self);
self.void_value.base.deref(self);
self.false_value.base.deref(self);
@@ -420,14 +536,18 @@ pub const Compilation = struct {
self.meta_type.base.base.deref(self);
self.events.destroy();
- self.name.deinit();
- self.a().destroy(self);
+ llvm.DisposeMessage(self.target_layout_str);
+ llvm.DisposeTargetData(self.target_data_ref);
+ llvm.DisposeTargetMachine(self.target_machine);
+
+ self.arena_allocator.deinit();
+ self.gpa().destroy(self);
}
pub fn build(self: *Compilation) !void {
if (self.llvm_argv.len != 0) {
- var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.a(), [][]const []const u8{
+ var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.arena(), [][]const []const u8{
[][]const u8{"zig (LLVM option parsing)"},
self.llvm_argv,
});
@@ -436,7 +556,7 @@ pub const Compilation = struct {
c.ZigLLVMParseCommandLineOptions(self.llvm_argv.len + 1, c_compatible_args.ptr);
}
- _ = try async self.buildAsync();
+ _ = try async self.buildAsync();
}
async fn buildAsync(self: *Compilation) void {
@@ -464,7 +584,7 @@ pub const Compilation = struct {
}
} else |err| {
// if there's an error then the compile errors have dangling references
- self.a().free(compile_errors);
+ self.gpa().free(compile_errors);
await (async self.events.put(Event{ .Error = err }) catch unreachable);
}
@@ -477,26 +597,26 @@ pub const Compilation = struct {
async fn addRootSrc(self: *Compilation) !void {
const root_src_path = self.root_src_path orelse @panic("TODO handle null root src path");
// TODO async/await os.path.real
- const root_src_real_path = os.path.real(self.a(), root_src_path) catch |err| {
+ const root_src_real_path = os.path.real(self.gpa(), root_src_path) catch |err| {
try printError("unable to get real path '{}': {}", root_src_path, err);
return err;
};
- errdefer self.a().free(root_src_real_path);
+ errdefer self.gpa().free(root_src_real_path);
// TODO async/await readFileAlloc()
- const source_code = io.readFileAlloc(self.a(), root_src_real_path) catch |err| {
+ const source_code = io.readFileAlloc(self.gpa(), root_src_real_path) catch |err| {
try printError("unable to open '{}': {}", root_src_real_path, err);
return err;
};
- errdefer self.a().free(source_code);
+ errdefer self.gpa().free(source_code);
- const parsed_file = try self.a().create(ParsedFile{
+ const parsed_file = try self.gpa().create(ParsedFile{
.tree = undefined,
.realpath = root_src_real_path,
});
- errdefer self.a().destroy(parsed_file);
+ errdefer self.gpa().destroy(parsed_file);
- parsed_file.tree = try std.zig.parse(self.a(), source_code);
+ parsed_file.tree = try std.zig.parse(self.gpa(), source_code);
errdefer parsed_file.tree.deinit();
const tree = &parsed_file.tree;
@@ -525,7 +645,7 @@ pub const Compilation = struct {
continue;
};
- const fn_decl = try self.a().create(Decl.Fn{
+ const fn_decl = try self.gpa().create(Decl.Fn{
.base = Decl{
.id = Decl.Id.Fn,
.name = name,
@@ -538,7 +658,7 @@ pub const Compilation = struct {
.value = Decl.Fn.Val{ .Unresolved = {} },
.fn_proto = fn_proto,
});
- errdefer self.a().destroy(fn_decl);
+ errdefer self.gpa().destroy(fn_decl);
try decl_group.call(addTopLevelDecl, self, &fn_decl.base);
},
@@ -547,15 +667,15 @@ pub const Compilation = struct {
}
}
try await (async decl_group.wait() catch unreachable);
- try await (async self.build_group.wait() catch unreachable);
+ try await (async self.prelink_group.wait() catch unreachable);
}
async fn addTopLevelDecl(self: *Compilation, decl: *Decl) !void {
const is_export = decl.isExported(&decl.parsed_file.tree);
if (is_export) {
- try self.build_group.call(verifyUniqueSymbol, self, decl);
- try self.build_group.call(resolveDecl, self, decl);
+ try self.prelink_group.call(verifyUniqueSymbol, self, decl);
+ try self.prelink_group.call(resolveDecl, self, decl);
}
}
@@ -563,7 +683,7 @@ pub const Compilation = struct {
const text = try std.fmt.allocPrint(self.loop.allocator, fmt, args);
errdefer self.loop.allocator.free(text);
- try self.build_group.call(addCompileErrorAsync, self, parsed_file, span, text);
+ try self.prelink_group.call(addCompileErrorAsync, self, parsed_file, span, text);
}
async fn addCompileErrorAsync(
@@ -625,11 +745,11 @@ pub const Compilation = struct {
}
}
- const link_lib = try self.a().create(LinkLib{
+ const link_lib = try self.gpa().create(LinkLib{
.name = name,
.path = null,
.provided_explicitly = provided_explicitly,
- .symbols = ArrayList([]u8).init(self.a()),
+ .symbols = ArrayList([]u8).init(self.gpa()),
});
try self.link_libs_list.append(link_lib);
if (is_libc) {
@@ -638,9 +758,71 @@ pub const Compilation = struct {
return link_lib;
}
- fn a(self: Compilation) *mem.Allocator {
+ /// General Purpose Allocator. Must free when done.
+ fn gpa(self: Compilation) *mem.Allocator {
return self.loop.allocator;
}
+
+ /// Arena Allocator. Automatically freed when the Compilation is destroyed.
+ fn arena(self: *Compilation) *mem.Allocator {
+ return &self.arena_allocator.allocator;
+ }
+
+ /// If the temporary directory for this compilation has not been created, it creates it.
+ /// Then it creates a random file name in that dir and returns it.
+ pub async fn createRandomOutputPath(self: *Compilation, suffix: []const u8) !Buffer {
+ const tmp_dir = try await (async self.getTmpDir() catch unreachable);
+ const file_prefix = await (async self.getRandomFileName() catch unreachable);
+
+ const file_name = try std.fmt.allocPrint(self.gpa(), "{}{}", file_prefix[0..], suffix);
+ defer self.gpa().free(file_name);
+
+ const full_path = try os.path.join(self.gpa(), tmp_dir, file_name[0..]);
+ errdefer self.gpa().free(full_path);
+
+ return Buffer.fromOwnedSlice(self.gpa(), full_path);
+ }
+
+ /// If the temporary directory for this Compilation has not been created, creates it.
+ /// Then returns it. The directory is unique to this Compilation and cleaned up when
+ /// the Compilation deinitializes.
+ async fn getTmpDir(self: *Compilation) ![]const u8 {
+ if (await (async self.tmp_dir.start() catch unreachable)) |ptr| return ptr.*;
+ self.tmp_dir.data = await (async self.getTmpDirImpl() catch unreachable);
+ self.tmp_dir.resolve();
+ return self.tmp_dir.data;
+ }
+
+ async fn getTmpDirImpl(self: *Compilation) ![]u8 {
+ const comp_dir_name = await (async self.getRandomFileName() catch unreachable);
+ const zig_dir_path = try getZigDir(self.gpa());
+ defer self.gpa().free(zig_dir_path);
+
+ const tmp_dir = try os.path.join(self.arena(), zig_dir_path, comp_dir_name[0..]);
+ try os.makePath(self.gpa(), tmp_dir);
+ return tmp_dir;
+ }
+
+ async fn getRandomFileName(self: *Compilation) [12]u8 {
+ // here we replace the standard +/ with -_ so that it can be used in a file name
+ const b64_fs_encoder = std.base64.Base64Encoder.init(
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
+ std.base64.standard_pad_char,
+ );
+
+ var rand_bytes: [9]u8 = undefined;
+
+ {
+ const held = await (async self.event_loop_local.prng.acquire() catch unreachable);
+ defer held.release();
+
+ held.value.random.bytes(rand_bytes[0..]);
+ }
+
+ var result: [12]u8 = undefined;
+ b64_fs_encoder.encode(result[0..], rand_bytes);
+ return result;
+ }
};
fn printError(comptime format: []const u8, args: ...) !void {
@@ -662,13 +844,11 @@ fn parseVisibToken(tree: *ast.Tree, optional_token_index: ?ast.TokenIndex) Visib
/// This declaration has been blessed as going into the final code generation.
pub async fn resolveDecl(comp: *Compilation, decl: *Decl) !void {
- if (@atomicRmw(u8, &decl.resolution_in_progress, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) == 0) {
- decl.resolution.data = await (async generateDecl(comp, decl) catch unreachable);
- decl.resolution.resolve();
- return decl.resolution.data;
- } else {
- return (await (async decl.resolution.get() catch unreachable)).*;
- }
+ if (await (async decl.resolution.start() catch unreachable)) |ptr| return ptr.*;
+
+ decl.resolution.data = await (async generateDecl(comp, decl) catch unreachable);
+ decl.resolution.resolve();
+ return decl.resolution.data;
}
/// The function that actually does the generation.
@@ -698,7 +878,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
const fn_type = try Type.Fn.create(comp, return_type, params, is_var_args);
defer fn_type.base.base.deref(comp);
- var symbol_name = try std.Buffer.init(comp.a(), fn_decl.base.name);
+ var symbol_name = try std.Buffer.init(comp.gpa(), fn_decl.base.name);
errdefer symbol_name.deinit();
const fn_val = try Value.Fn.create(comp, fn_type, fndef_scope, symbol_name);
@@ -719,7 +899,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
error.SemanticAnalysisFailed => return {},
else => return err,
};
- defer unanalyzed_code.destroy(comp.a());
+ defer unanalyzed_code.destroy(comp.gpa());
if (comp.verbose_ir) {
std.debug.warn("unanalyzed:\n");
@@ -738,7 +918,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
error.SemanticAnalysisFailed => return {},
else => return err,
};
- errdefer analyzed_code.destroy(comp.a());
+ errdefer analyzed_code.destroy(comp.gpa());
if (comp.verbose_ir) {
std.debug.warn("analyzed:\n");
@@ -747,5 +927,30 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
// Kick off rendering to LLVM module, but it doesn't block the fn decl
// analysis from being complete.
- try comp.build_group.call(codegen.renderToLlvm, comp, fn_val, analyzed_code);
+ try comp.prelink_group.call(codegen.renderToLlvm, comp, fn_val, analyzed_code);
+ try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);
+}
+
+async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void {
+ fn_val.base.ref();
+ defer fn_val.base.deref(comp);
+
+ fn_val.link_set_node.data = fn_val;
+
+ const held = await (async comp.fn_link_set.acquire() catch unreachable);
+ defer held.release();
+
+ held.value.append(fn_val.link_set_node);
+}
+
+fn getZigDir(allocator: *mem.Allocator) ![]u8 {
+ const home_dir = try getHomeDir(allocator);
+ defer allocator.free(home_dir);
+
+ return os.path.join(allocator, home_dir, ".zig");
+}
+
+/// TODO move to zig std lib, and make it work for other OSes
+fn getHomeDir(allocator: *mem.Allocator) ![]u8 {
+ return os.getEnvVarOwned(allocator, "HOME");
}
diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig
index 0e0a4f9bf3..c1f9c97001 100644
--- a/src-self-hosted/ir.zig
+++ b/src-self-hosted/ir.zig
@@ -453,7 +453,7 @@ pub const Code = struct {
arena: std.heap.ArenaAllocator,
return_type: ?*Type,
- /// allocator is comp.a()
+ /// allocator is comp.gpa()
pub fn destroy(self: *Code, allocator: *Allocator) void {
self.arena.deinit();
allocator.destroy(self);
@@ -483,13 +483,13 @@ pub const Builder = struct {
pub const Error = Analyze.Error;
pub fn init(comp: *Compilation, parsed_file: *ParsedFile) !Builder {
- const code = try comp.a().create(Code{
+ const code = try comp.gpa().create(Code{
.basic_block_list = undefined,
- .arena = std.heap.ArenaAllocator.init(comp.a()),
+ .arena = std.heap.ArenaAllocator.init(comp.gpa()),
.return_type = null,
});
code.basic_block_list = std.ArrayList(*BasicBlock).init(&code.arena.allocator);
- errdefer code.destroy(comp.a());
+ errdefer code.destroy(comp.gpa());
return Builder{
.comp = comp,
@@ -502,7 +502,7 @@ pub const Builder = struct {
}
pub fn abort(self: *Builder) void {
- self.code.destroy(self.comp.a());
+ self.code.destroy(self.comp.gpa());
}
/// Call code.destroy() when done
diff --git a/src-self-hosted/llvm.zig b/src-self-hosted/llvm.zig
index 13480dc2c6..b196656367 100644
--- a/src-self-hosted/llvm.zig
+++ b/src-self-hosted/llvm.zig
@@ -2,6 +2,12 @@ const builtin = @import("builtin");
const c = @import("c.zig");
const assert = @import("std").debug.assert;
+// we wrap the c module for 3 reasons:
+// 1. to avoid accidentally calling the non-thread-safe functions
+// 2. patch up some of the types to remove nullability
+// 3. some functions have been augmented by zig_llvm.cpp to be more powerful,
+// such as ZigLLVMTargetMachineEmitToFile
+
pub const AttributeIndex = c_uint;
pub const Bool = c_int;
@@ -12,25 +18,51 @@ pub const ValueRef = removeNullability(c.LLVMValueRef);
pub const TypeRef = removeNullability(c.LLVMTypeRef);
pub const BasicBlockRef = removeNullability(c.LLVMBasicBlockRef);
pub const AttributeRef = removeNullability(c.LLVMAttributeRef);
+pub const TargetRef = removeNullability(c.LLVMTargetRef);
+pub const TargetMachineRef = removeNullability(c.LLVMTargetMachineRef);
+pub const TargetDataRef = removeNullability(c.LLVMTargetDataRef);
+pub const DIBuilder = c.ZigLLVMDIBuilder;
pub const AddAttributeAtIndex = c.LLVMAddAttributeAtIndex;
pub const AddFunction = c.LLVMAddFunction;
+pub const AddModuleCodeViewFlag = c.ZigLLVMAddModuleCodeViewFlag;
+pub const AddModuleDebugInfoFlag = c.ZigLLVMAddModuleDebugInfoFlag;
pub const ClearCurrentDebugLocation = c.ZigLLVMClearCurrentDebugLocation;
+pub const ConstAllOnes = c.LLVMConstAllOnes;
pub const ConstInt = c.LLVMConstInt;
+pub const ConstNull = c.LLVMConstNull;
pub const ConstStringInContext = c.LLVMConstStringInContext;
pub const ConstStructInContext = c.LLVMConstStructInContext;
+pub const CopyStringRepOfTargetData = c.LLVMCopyStringRepOfTargetData;
pub const CreateBuilderInContext = c.LLVMCreateBuilderInContext;
+pub const CreateCompileUnit = c.ZigLLVMCreateCompileUnit;
+pub const CreateDIBuilder = c.ZigLLVMCreateDIBuilder;
pub const CreateEnumAttribute = c.LLVMCreateEnumAttribute;
+pub const CreateFile = c.ZigLLVMCreateFile;
pub const CreateStringAttribute = c.LLVMCreateStringAttribute;
+pub const CreateTargetDataLayout = c.LLVMCreateTargetDataLayout;
+pub const CreateTargetMachine = c.LLVMCreateTargetMachine;
+pub const DIBuilderFinalize = c.ZigLLVMDIBuilderFinalize;
pub const DisposeBuilder = c.LLVMDisposeBuilder;
+pub const DisposeDIBuilder = c.ZigLLVMDisposeDIBuilder;
+pub const DisposeMessage = c.LLVMDisposeMessage;
pub const DisposeModule = c.LLVMDisposeModule;
+pub const DisposeTargetData = c.LLVMDisposeTargetData;
+pub const DisposeTargetMachine = c.LLVMDisposeTargetMachine;
pub const DoubleTypeInContext = c.LLVMDoubleTypeInContext;
pub const DumpModule = c.LLVMDumpModule;
pub const FP128TypeInContext = c.LLVMFP128TypeInContext;
pub const FloatTypeInContext = c.LLVMFloatTypeInContext;
pub const GetEnumAttributeKindForName = c.LLVMGetEnumAttributeKindForName;
+pub const GetHostCPUName = c.ZigLLVMGetHostCPUName;
pub const GetMDKindIDInContext = c.LLVMGetMDKindIDInContext;
+pub const GetNativeFeatures = c.ZigLLVMGetNativeFeatures;
pub const HalfTypeInContext = c.LLVMHalfTypeInContext;
+pub const InitializeAllAsmParsers = c.LLVMInitializeAllAsmParsers;
+pub const InitializeAllAsmPrinters = c.LLVMInitializeAllAsmPrinters;
+pub const InitializeAllTargetInfos = c.LLVMInitializeAllTargetInfos;
+pub const InitializeAllTargetMCs = c.LLVMInitializeAllTargetMCs;
+pub const InitializeAllTargets = c.LLVMInitializeAllTargets;
pub const InsertBasicBlockInContext = c.LLVMInsertBasicBlockInContext;
pub const Int128TypeInContext = c.LLVMInt128TypeInContext;
pub const Int16TypeInContext = c.LLVMInt16TypeInContext;
@@ -47,13 +79,16 @@ pub const MDStringInContext = c.LLVMMDStringInContext;
pub const MetadataTypeInContext = c.LLVMMetadataTypeInContext;
pub const ModuleCreateWithNameInContext = c.LLVMModuleCreateWithNameInContext;
pub const PPCFP128TypeInContext = c.LLVMPPCFP128TypeInContext;
+pub const SetDataLayout = c.LLVMSetDataLayout;
+pub const SetTarget = c.LLVMSetTarget;
pub const StructTypeInContext = c.LLVMStructTypeInContext;
pub const TokenTypeInContext = c.LLVMTokenTypeInContext;
pub const VoidTypeInContext = c.LLVMVoidTypeInContext;
pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext;
pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext;
-pub const ConstAllOnes = c.LLVMConstAllOnes;
-pub const ConstNull = c.LLVMConstNull;
+
+pub const GetTargetFromTriple = LLVMGetTargetFromTriple;
+extern fn LLVMGetTargetFromTriple(Triple: [*]const u8, T: *TargetRef, ErrorMessage: ?*[*]u8) Bool;
pub const VerifyModule = LLVMVerifyModule;
extern fn LLVMVerifyModule(M: ModuleRef, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool;
@@ -83,6 +118,31 @@ pub const PrintMessageAction = VerifierFailureAction.LLVMPrintMessageAction;
pub const ReturnStatusAction = VerifierFailureAction.LLVMReturnStatusAction;
pub const VerifierFailureAction = c.LLVMVerifierFailureAction;
+pub const CodeGenLevelNone = c.LLVMCodeGenOptLevel.LLVMCodeGenLevelNone;
+pub const CodeGenLevelLess = c.LLVMCodeGenOptLevel.LLVMCodeGenLevelLess;
+pub const CodeGenLevelDefault = c.LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault;
+pub const CodeGenLevelAggressive = c.LLVMCodeGenOptLevel.LLVMCodeGenLevelAggressive;
+pub const CodeGenOptLevel = c.LLVMCodeGenOptLevel;
+
+pub const RelocDefault = c.LLVMRelocMode.LLVMRelocDefault;
+pub const RelocStatic = c.LLVMRelocMode.LLVMRelocStatic;
+pub const RelocPIC = c.LLVMRelocMode.LLVMRelocPIC;
+pub const RelocDynamicNoPic = c.LLVMRelocMode.LLVMRelocDynamicNoPic;
+pub const RelocMode = c.LLVMRelocMode;
+
+pub const CodeModelDefault = c.LLVMCodeModel.LLVMCodeModelDefault;
+pub const CodeModelJITDefault = c.LLVMCodeModel.LLVMCodeModelJITDefault;
+pub const CodeModelSmall = c.LLVMCodeModel.LLVMCodeModelSmall;
+pub const CodeModelKernel = c.LLVMCodeModel.LLVMCodeModelKernel;
+pub const CodeModelMedium = c.LLVMCodeModel.LLVMCodeModelMedium;
+pub const CodeModelLarge = c.LLVMCodeModel.LLVMCodeModelLarge;
+pub const CodeModel = c.LLVMCodeModel;
+
+pub const EmitAssembly = EmitOutputType.ZigLLVM_EmitAssembly;
+pub const EmitBinary = EmitOutputType.ZigLLVM_EmitBinary;
+pub const EmitLLVMIr = EmitOutputType.ZigLLVM_EmitLLVMIr;
+pub const EmitOutputType = c.ZigLLVM_EmitOutputType;
+
fn removeNullability(comptime T: type) type {
comptime assert(@typeId(T) == builtin.TypeId.Optional);
return T.Child;
@@ -90,3 +150,14 @@ fn removeNullability(comptime T: type) type {
pub const BuildRet = LLVMBuildRet;
extern fn LLVMBuildRet(arg0: BuilderRef, V: ?ValueRef) ValueRef;
+
+pub const TargetMachineEmitToFile = ZigLLVMTargetMachineEmitToFile;
+extern fn ZigLLVMTargetMachineEmitToFile(
+ targ_machine_ref: TargetMachineRef,
+ module_ref: ModuleRef,
+ filename: [*]const u8,
+ output_type: EmitOutputType,
+ error_message: *[*]u8,
+ is_debug: bool,
+ is_small: bool,
+) bool;
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index c9478954c5..8b668e35bd 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -363,6 +363,8 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
}
};
+ const is_static = flags.present("static");
+
const assembly_files = flags.many("assembly");
const link_objects = flags.many("object");
if (root_source_file == null and link_objects.len == 0 and assembly_files.len == 0) {
@@ -389,7 +391,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
try loop.initMultiThreaded(allocator);
defer loop.deinit();
- var event_loop_local = EventLoopLocal.init(&loop);
+ var event_loop_local = try EventLoopLocal.init(&loop);
defer event_loop_local.deinit();
var comp = try Compilation.create(
@@ -399,6 +401,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
Target.Native,
out_type,
build_mode,
+ is_static,
zig_lib_dir,
full_cache_dir,
);
@@ -426,7 +429,6 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
comp.clang_argv = clang_argv_buf.toSliceConst();
comp.strip = flags.present("strip");
- comp.is_static = flags.present("static");
if (flags.single("libc-lib-dir")) |libc_lib_dir| {
comp.libc_lib_dir = libc_lib_dir;
@@ -481,9 +483,9 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
}
comp.emit_file_type = emit_type;
- comp.link_objects = link_objects;
comp.assembly_files = assembly_files;
comp.link_out_file = flags.single("out-file");
+ comp.link_objects = link_objects;
try comp.build();
const process_build_events_handle = try async processBuildEvents(comp, color);
diff --git a/src-self-hosted/package.zig b/src-self-hosted/package.zig
new file mode 100644
index 0000000000..720b279651
--- /dev/null
+++ b/src-self-hosted/package.zig
@@ -0,0 +1,29 @@
+const std = @import("std");
+const mem = std.mem;
+const assert = std.debug.assert;
+const Buffer = std.Buffer;
+
+pub const Package = struct {
+ root_src_dir: Buffer,
+ root_src_path: Buffer,
+
+ /// relative to root_src_dir
+ table: Table,
+
+ pub const Table = std.HashMap([]const u8, *Package, mem.hash_slice_u8, mem.eql_slice_u8);
+
+ /// makes internal copies of root_src_dir and root_src_path
+ /// allocator should be an arena allocator because Package never frees anything
+ pub fn create(allocator: *mem.Allocator, root_src_dir: []const u8, root_src_path: []const u8) !*Package {
+ return allocator.create(Package{
+ .root_src_dir = try Buffer.init(allocator, root_src_dir),
+ .root_src_path = try Buffer.init(allocator, root_src_path),
+ .table = Table.init(allocator),
+ });
+ }
+
+ pub fn add(self: *Package, name: []const u8, package: *Package) !void {
+ const entry = try self.table.put(try mem.dupe(self.table.allocator, u8, name), package);
+ assert(entry == null);
+ }
+};
diff --git a/src-self-hosted/scope.zig b/src-self-hosted/scope.zig
index 4326617fa0..1c519d6c08 100644
--- a/src-self-hosted/scope.zig
+++ b/src-self-hosted/scope.zig
@@ -64,7 +64,7 @@ pub const Scope = struct {
/// Creates a Decls scope with 1 reference
pub fn create(comp: *Compilation, parent: ?*Scope) !*Decls {
- const self = try comp.a().create(Decls{
+ const self = try comp.gpa().create(Decls{
.base = Scope{
.id = Id.Decls,
.parent = parent,
@@ -72,9 +72,9 @@ pub const Scope = struct {
},
.table = undefined,
});
- errdefer comp.a().destroy(self);
+ errdefer comp.gpa().destroy(self);
- self.table = Decl.Table.init(comp.a());
+ self.table = Decl.Table.init(comp.gpa());
errdefer self.table.deinit();
if (parent) |p| p.ref();
@@ -126,7 +126,7 @@ pub const Scope = struct {
/// Creates a Block scope with 1 reference
pub fn create(comp: *Compilation, parent: ?*Scope) !*Block {
- const self = try comp.a().create(Block{
+ const self = try comp.gpa().create(Block{
.base = Scope{
.id = Id.Block,
.parent = parent,
@@ -138,14 +138,14 @@ pub const Scope = struct {
.is_comptime = undefined,
.safety = Safety.Auto,
});
- errdefer comp.a().destroy(self);
+ errdefer comp.gpa().destroy(self);
if (parent) |p| p.ref();
return self;
}
pub fn destroy(self: *Block, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -158,7 +158,7 @@ pub const Scope = struct {
/// Creates a FnDef scope with 1 reference
/// Must set the fn_val later
pub fn create(comp: *Compilation, parent: ?*Scope) !*FnDef {
- const self = try comp.a().create(FnDef{
+ const self = try comp.gpa().create(FnDef{
.base = Scope{
.id = Id.FnDef,
.parent = parent,
@@ -173,7 +173,7 @@ pub const Scope = struct {
}
pub fn destroy(self: *FnDef, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -182,7 +182,7 @@ pub const Scope = struct {
/// Creates a CompTime scope with 1 reference
pub fn create(comp: *Compilation, parent: ?*Scope) !*CompTime {
- const self = try comp.a().create(CompTime{
+ const self = try comp.gpa().create(CompTime{
.base = Scope{
.id = Id.CompTime,
.parent = parent,
@@ -195,7 +195,7 @@ pub const Scope = struct {
}
pub fn destroy(self: *CompTime, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -216,7 +216,7 @@ pub const Scope = struct {
kind: Kind,
defer_expr_scope: *DeferExpr,
) !*Defer {
- const self = try comp.a().create(Defer{
+ const self = try comp.gpa().create(Defer{
.base = Scope{
.id = Id.Defer,
.parent = parent,
@@ -225,7 +225,7 @@ pub const Scope = struct {
.defer_expr_scope = defer_expr_scope,
.kind = kind,
});
- errdefer comp.a().destroy(self);
+ errdefer comp.gpa().destroy(self);
defer_expr_scope.base.ref();
@@ -235,7 +235,7 @@ pub const Scope = struct {
pub fn destroy(self: *Defer, comp: *Compilation) void {
self.defer_expr_scope.base.deref(comp);
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -245,7 +245,7 @@ pub const Scope = struct {
/// Creates a DeferExpr scope with 1 reference
pub fn create(comp: *Compilation, parent: ?*Scope, expr_node: *ast.Node) !*DeferExpr {
- const self = try comp.a().create(DeferExpr{
+ const self = try comp.gpa().create(DeferExpr{
.base = Scope{
.id = Id.DeferExpr,
.parent = parent,
@@ -253,14 +253,14 @@ pub const Scope = struct {
},
.expr_node = expr_node,
});
- errdefer comp.a().destroy(self);
+ errdefer comp.gpa().destroy(self);
if (parent) |p| p.ref();
return self;
}
pub fn destroy(self: *DeferExpr, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
};
diff --git a/src-self-hosted/target.zig b/src-self-hosted/target.zig
index 724d99ea23..db673e421a 100644
--- a/src-self-hosted/target.zig
+++ b/src-self-hosted/target.zig
@@ -1,60 +1,118 @@
+const std = @import("std");
const builtin = @import("builtin");
-const c = @import("c.zig");
-
-pub const CrossTarget = struct {
- arch: builtin.Arch,
- os: builtin.Os,
- environ: builtin.Environ,
-};
+const llvm = @import("llvm.zig");
pub const Target = union(enum) {
Native,
- Cross: CrossTarget,
+ Cross: Cross,
- pub fn oFileExt(self: *const Target) []const u8 {
- const environ = switch (self.*) {
- Target.Native => builtin.environ,
- Target.Cross => |t| t.environ,
- };
- return switch (environ) {
- builtin.Environ.msvc => ".obj",
+ pub const Cross = struct {
+ arch: builtin.Arch,
+ os: builtin.Os,
+ environ: builtin.Environ,
+ object_format: builtin.ObjectFormat,
+ };
+
+ pub fn oFileExt(self: Target) []const u8 {
+ return switch (self.getObjectFormat()) {
+ builtin.ObjectFormat.coff => ".obj",
else => ".o",
};
}
- pub fn exeFileExt(self: *const Target) []const u8 {
+ pub fn exeFileExt(self: Target) []const u8 {
return switch (self.getOs()) {
builtin.Os.windows => ".exe",
else => "",
};
}
- pub fn getOs(self: *const Target) builtin.Os {
- return switch (self.*) {
+ pub fn getOs(self: Target) builtin.Os {
+ return switch (self) {
Target.Native => builtin.os,
- Target.Cross => |t| t.os,
+ @TagType(Target).Cross => |t| t.os,
+ };
+ }
+
+ pub fn getArch(self: Target) builtin.Arch {
+ return switch (self) {
+ Target.Native => builtin.arch,
+ @TagType(Target).Cross => |t| t.arch,
+ };
+ }
+
+ pub fn getEnviron(self: Target) builtin.Environ {
+ return switch (self) {
+ Target.Native => builtin.environ,
+ @TagType(Target).Cross => |t| t.environ,
+ };
+ }
+
+ pub fn getObjectFormat(self: Target) builtin.ObjectFormat {
+ return switch (self) {
+ Target.Native => builtin.object_format,
+ @TagType(Target).Cross => |t| t.object_format,
};
}
- pub fn isDarwin(self: *const Target) bool {
+ pub fn isWasm(self: Target) bool {
+ return switch (self.getArch()) {
+ builtin.Arch.wasm32, builtin.Arch.wasm64 => true,
+ else => false,
+ };
+ }
+
+ pub fn isDarwin(self: Target) bool {
return switch (self.getOs()) {
builtin.Os.ios, builtin.Os.macosx => true,
else => false,
};
}
- pub fn isWindows(self: *const Target) bool {
+ pub fn isWindows(self: Target) bool {
return switch (self.getOs()) {
builtin.Os.windows => true,
else => false,
};
}
-};
-pub fn initializeAll() void {
- c.LLVMInitializeAllTargets();
- c.LLVMInitializeAllTargetInfos();
- c.LLVMInitializeAllTargetMCs();
- c.LLVMInitializeAllAsmPrinters();
- c.LLVMInitializeAllAsmParsers();
-}
+ pub fn initializeAll() void {
+ llvm.InitializeAllTargets();
+ llvm.InitializeAllTargetInfos();
+ llvm.InitializeAllTargetMCs();
+ llvm.InitializeAllAsmPrinters();
+ llvm.InitializeAllAsmParsers();
+ }
+
+ pub fn getTriple(self: Target, allocator: *std.mem.Allocator) !std.Buffer {
+ var result = try std.Buffer.initSize(allocator, 0);
+ errdefer result.deinit();
+
+ // LLVM WebAssembly output support requires the target to be activated at
+ // build type with -DCMAKE_LLVM_EXPIERMENTAL_TARGETS_TO_BUILD=WebAssembly.
+ //
+ // LLVM determines the output format based on the environment suffix,
+ // defaulting to an object based on the architecture. The default format in
+ // LLVM 6 sets the wasm arch output incorrectly to ELF. We need to
+ // explicitly set this ourself in order for it to work.
+ //
+ // This is fixed in LLVM 7 and you will be able to get wasm output by
+ // using the target triple `wasm32-unknown-unknown-unknown`.
+ const env_name = if (self.isWasm()) "wasm" else @tagName(self.getEnviron());
+
+ var out = &std.io.BufferOutStream.init(&result).stream;
+ try out.print("{}-unknown-{}-{}", @tagName(self.getArch()), @tagName(self.getOs()), env_name);
+
+ return result;
+ }
+
+ pub fn llvmTargetFromTriple(triple: std.Buffer) !llvm.TargetRef {
+ var result: llvm.TargetRef = undefined;
+ var err_msg: [*]u8 = undefined;
+ if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) {
+ std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg);
+ return error.UnsupportedTarget;
+ }
+ return result;
+ }
+};
diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig
index 3edb267ca9..45e5362124 100644
--- a/src-self-hosted/test.zig
+++ b/src-self-hosted/test.zig
@@ -46,7 +46,7 @@ pub const TestContext = struct {
try self.loop.initMultiThreaded(allocator);
errdefer self.loop.deinit();
- self.event_loop_local = EventLoopLocal.init(&self.loop);
+ self.event_loop_local = try EventLoopLocal.init(&self.loop);
errdefer self.event_loop_local.deinit();
self.group = std.event.Group(error!void).init(&self.loop);
@@ -107,6 +107,7 @@ pub const TestContext = struct {
Target.Native,
Compilation.Kind.Obj,
builtin.Mode.Debug,
+ true, // is_static
self.zig_lib_dir,
self.zig_cache_dir,
);
diff --git a/src-self-hosted/type.zig b/src-self-hosted/type.zig
index 670547cce2..bb1fb9bb01 100644
--- a/src-self-hosted/type.zig
+++ b/src-self-hosted/type.zig
@@ -160,7 +160,7 @@ pub const Type = struct {
decls: *Scope.Decls,
pub fn destroy(self: *Struct, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Struct, ofile: *ObjectFile) llvm.TypeRef {
@@ -180,7 +180,7 @@ pub const Type = struct {
};
pub fn create(comp: *Compilation, return_type: *Type, params: []Param, is_var_args: bool) !*Fn {
- const result = try comp.a().create(Fn{
+ const result = try comp.gpa().create(Fn{
.base = Type{
.base = Value{
.id = Value.Id.Type,
@@ -193,7 +193,7 @@ pub const Type = struct {
.params = params,
.is_var_args = is_var_args,
});
- errdefer comp.a().destroy(result);
+ errdefer comp.gpa().destroy(result);
result.return_type.base.ref();
for (result.params) |param| {
@@ -207,7 +207,7 @@ pub const Type = struct {
for (self.params) |param| {
param.typeof.base.deref(comp);
}
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Fn, ofile: *ObjectFile) !llvm.TypeRef {
@@ -215,8 +215,8 @@ pub const Type = struct {
Type.Id.Void => llvm.VoidTypeInContext(ofile.context) orelse return error.OutOfMemory,
else => try self.return_type.getLlvmType(ofile),
};
- const llvm_param_types = try ofile.a().alloc(llvm.TypeRef, self.params.len);
- defer ofile.a().free(llvm_param_types);
+ const llvm_param_types = try ofile.gpa().alloc(llvm.TypeRef, self.params.len);
+ defer ofile.gpa().free(llvm_param_types);
for (llvm_param_types) |*llvm_param_type, i| {
llvm_param_type.* = try self.params[i].typeof.getLlvmType(ofile);
}
@@ -241,7 +241,7 @@ pub const Type = struct {
}
pub fn destroy(self: *MetaType, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -255,7 +255,7 @@ pub const Type = struct {
}
pub fn destroy(self: *Void, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -269,7 +269,7 @@ pub const Type = struct {
}
pub fn destroy(self: *Bool, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Bool, ofile: *ObjectFile) llvm.TypeRef {
@@ -287,7 +287,7 @@ pub const Type = struct {
}
pub fn destroy(self: *NoReturn, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -295,7 +295,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Int, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Int, ofile: *ObjectFile) llvm.TypeRef {
@@ -307,7 +307,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Float, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Float, ofile: *ObjectFile) llvm.TypeRef {
@@ -332,7 +332,7 @@ pub const Type = struct {
pub const Size = builtin.TypeInfo.Pointer.Size;
pub fn destroy(self: *Pointer, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn get(
@@ -355,7 +355,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Array, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Array, ofile: *ObjectFile) llvm.TypeRef {
@@ -367,7 +367,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *ComptimeFloat, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -375,7 +375,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *ComptimeInt, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -383,7 +383,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Undefined, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -391,7 +391,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Null, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -399,7 +399,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Optional, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Optional, ofile: *ObjectFile) llvm.TypeRef {
@@ -411,7 +411,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *ErrorUnion, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *ErrorUnion, ofile: *ObjectFile) llvm.TypeRef {
@@ -423,7 +423,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *ErrorSet, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *ErrorSet, ofile: *ObjectFile) llvm.TypeRef {
@@ -435,7 +435,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Enum, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Enum, ofile: *ObjectFile) llvm.TypeRef {
@@ -447,7 +447,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Union, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Union, ofile: *ObjectFile) llvm.TypeRef {
@@ -459,7 +459,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Namespace, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -467,7 +467,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Block, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -475,7 +475,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *BoundFn, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *BoundFn, ofile: *ObjectFile) llvm.TypeRef {
@@ -487,7 +487,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *ArgTuple, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -495,7 +495,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Opaque, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Opaque, ofile: *ObjectFile) llvm.TypeRef {
@@ -507,7 +507,7 @@ pub const Type = struct {
base: Type,
pub fn destroy(self: *Promise, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmType(self: *Promise, ofile: *ObjectFile) llvm.TypeRef {
diff --git a/src-self-hosted/value.zig b/src-self-hosted/value.zig
index e3b91d2807..be19c6bccf 100644
--- a/src-self-hosted/value.zig
+++ b/src-self-hosted/value.zig
@@ -4,6 +4,7 @@ const Scope = @import("scope.zig").Scope;
const Compilation = @import("compilation.zig").Compilation;
const ObjectFile = @import("codegen.zig").ObjectFile;
const llvm = @import("llvm.zig");
+const Buffer = std.Buffer;
/// Values are ref-counted, heap-allocated, and copy-on-write
/// If there is only 1 ref then write need not copy
@@ -68,7 +69,7 @@ pub const Value = struct {
/// The main external name that is used in the .o file.
/// TODO https://github.com/ziglang/zig/issues/265
- symbol_name: std.Buffer,
+ symbol_name: Buffer,
/// parent should be the top level decls or container decls
fndef_scope: *Scope.FnDef,
@@ -79,10 +80,22 @@ pub const Value = struct {
/// parent is child_scope
block_scope: *Scope.Block,
+ /// Path to the object file that contains this function
+ containing_object: Buffer,
+
+ link_set_node: *std.LinkedList(?*Value.Fn).Node,
+
/// Creates a Fn value with 1 ref
/// Takes ownership of symbol_name
- pub fn create(comp: *Compilation, fn_type: *Type.Fn, fndef_scope: *Scope.FnDef, symbol_name: std.Buffer) !*Fn {
- const self = try comp.a().create(Fn{
+ pub fn create(comp: *Compilation, fn_type: *Type.Fn, fndef_scope: *Scope.FnDef, symbol_name: Buffer) !*Fn {
+ const link_set_node = try comp.gpa().create(Compilation.FnLinkSet.Node{
+ .data = null,
+ .next = undefined,
+ .prev = undefined,
+ });
+ errdefer comp.gpa().destroy(link_set_node);
+
+ const self = try comp.gpa().create(Fn{
.base = Value{
.id = Value.Id.Fn,
.typeof = &fn_type.base,
@@ -92,6 +105,8 @@ pub const Value = struct {
.child_scope = &fndef_scope.base,
.block_scope = undefined,
.symbol_name = symbol_name,
+ .containing_object = Buffer.initNull(comp.gpa()),
+ .link_set_node = link_set_node,
});
fn_type.base.base.ref();
fndef_scope.fn_val = self;
@@ -100,9 +115,19 @@ pub const Value = struct {
}
pub fn destroy(self: *Fn, comp: *Compilation) void {
+ // remove with a tombstone so that we do not have to grab a lock
+ if (self.link_set_node.data != null) {
+ // it's now the job of the link step to find this tombstone and
+ // deallocate it.
+ self.link_set_node.data = null;
+ } else {
+ comp.gpa().destroy(self.link_set_node);
+ }
+
+ self.containing_object.deinit();
self.fndef_scope.base.deref(comp);
self.symbol_name.deinit();
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -115,7 +140,7 @@ pub const Value = struct {
}
pub fn destroy(self: *Void, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -134,7 +159,7 @@ pub const Value = struct {
}
pub fn destroy(self: *Bool, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
pub fn getLlvmConst(self: *Bool, ofile: *ObjectFile) ?llvm.ValueRef {
@@ -156,7 +181,7 @@ pub const Value = struct {
}
pub fn destroy(self: *NoReturn, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
@@ -170,7 +195,7 @@ pub const Value = struct {
};
pub fn destroy(self: *Ptr, comp: *Compilation) void {
- comp.a().destroy(self);
+ comp.gpa().destroy(self);
}
};
};
diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp
index 24f2a8a343..a43d2d182c 100644
--- a/src/zig_llvm.cpp
+++ b/src/zig_llvm.cpp
@@ -440,6 +440,11 @@ ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unreso
return reinterpret_cast(di_builder);
}
+void ZigLLVMDisposeDIBuilder(ZigLLVMDIBuilder *dbuilder) {
+ DIBuilder *di_builder = reinterpret_cast(dbuilder);
+ delete di_builder;
+}
+
void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, ZigLLVMDIScope *scope) {
unwrap(builder)->SetCurrentDebugLocation(DebugLoc::get(
line, column, reinterpret_cast(scope)));
diff --git a/src/zig_llvm.h b/src/zig_llvm.h
index d34300b8ae..6f25df8674 100644
--- a/src/zig_llvm.h
+++ b/src/zig_llvm.h
@@ -39,7 +39,7 @@ struct ZigLLVMInsertionPoint;
ZIG_EXTERN_C void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R);
ZIG_EXTERN_C void ZigLLVMInitializeLowerIntrinsicsPass(LLVMPassRegistryRef R);
-/// Caller must free memory.
+/// Caller must free memory with LLVMDisposeMessage
ZIG_EXTERN_C char *ZigLLVMGetHostCPUName(void);
ZIG_EXTERN_C char *ZigLLVMGetNativeFeatures(void);
@@ -139,6 +139,7 @@ ZIG_EXTERN_C unsigned ZigLLVMTag_DW_enumeration_type(void);
ZIG_EXTERN_C unsigned ZigLLVMTag_DW_union_type(void);
ZIG_EXTERN_C struct ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved);
+ZIG_EXTERN_C void ZigLLVMDisposeDIBuilder(struct ZigLLVMDIBuilder *dbuilder);
ZIG_EXTERN_C void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module);
ZIG_EXTERN_C void ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module);
diff --git a/std/atomic/int.zig b/std/atomic/int.zig
index d51454c673..4103d52719 100644
--- a/std/atomic/int.zig
+++ b/std/atomic/int.zig
@@ -25,5 +25,9 @@ pub fn Int(comptime T: type) type {
pub fn get(self: *Self) T {
return @atomicLoad(T, &self.unprotected_value, AtomicOrder.SeqCst);
}
+
+ pub fn xchg(self: *Self, new_value: T) T {
+ return @atomicRmw(T, &self.unprotected_value, builtin.AtomicRmwOp.Xchg, new_value, AtomicOrder.SeqCst);
+ }
};
}
diff --git a/std/buffer.zig b/std/buffer.zig
index aff7fa86ef..3b58002aba 100644
--- a/std/buffer.zig
+++ b/std/buffer.zig
@@ -54,6 +54,19 @@ pub const Buffer = struct {
return result;
}
+ pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: ...) !Buffer {
+ const countSize = struct {
+ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
+ size.* += bytes.len;
+ }
+ }.countSize;
+ var size: usize = 0;
+ std.fmt.format(&size, error{}, countSize, format, args) catch |err| switch (err) {};
+ var self = try Buffer.initSize(allocator, size);
+ assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size);
+ return self;
+ }
+
pub fn deinit(self: *Buffer) void {
self.list.deinit();
}
diff --git a/std/dwarf.zig b/std/dwarf.zig
index 76ed122447..2cf8ed953e 100644
--- a/std/dwarf.zig
+++ b/std/dwarf.zig
@@ -639,3 +639,40 @@ pub const LNE_define_file = 0x03;
pub const LNE_set_discriminator = 0x04;
pub const LNE_lo_user = 0x80;
pub const LNE_hi_user = 0xff;
+
+pub const LANG_C89 = 0x0001;
+pub const LANG_C = 0x0002;
+pub const LANG_Ada83 = 0x0003;
+pub const LANG_C_plus_plus = 0x0004;
+pub const LANG_Cobol74 = 0x0005;
+pub const LANG_Cobol85 = 0x0006;
+pub const LANG_Fortran77 = 0x0007;
+pub const LANG_Fortran90 = 0x0008;
+pub const LANG_Pascal83 = 0x0009;
+pub const LANG_Modula2 = 0x000a;
+pub const LANG_Java = 0x000b;
+pub const LANG_C99 = 0x000c;
+pub const LANG_Ada95 = 0x000d;
+pub const LANG_Fortran95 = 0x000e;
+pub const LANG_PLI = 0x000f;
+pub const LANG_ObjC = 0x0010;
+pub const LANG_ObjC_plus_plus = 0x0011;
+pub const LANG_UPC = 0x0012;
+pub const LANG_D = 0x0013;
+pub const LANG_Python = 0x0014;
+pub const LANG_Go = 0x0016;
+pub const LANG_C_plus_plus_11 = 0x001a;
+pub const LANG_Rust = 0x001c;
+pub const LANG_C11 = 0x001d;
+pub const LANG_C_plus_plus_14 = 0x0021;
+pub const LANG_Fortran03 = 0x0022;
+pub const LANG_Fortran08 = 0x0023;
+pub const LANG_lo_user = 0x8000;
+pub const LANG_hi_user = 0xffff;
+pub const LANG_Mips_Assembler = 0x8001;
+pub const LANG_Upc = 0x8765;
+pub const LANG_HP_Bliss = 0x8003;
+pub const LANG_HP_Basic91 = 0x8004;
+pub const LANG_HP_Pascal91 = 0x8005;
+pub const LANG_HP_IMacro = 0x8006;
+pub const LANG_HP_Assembler = 0x8007;
diff --git a/std/event/future.zig b/std/event/future.zig
index 0f27b4131b..f5d14d1ca6 100644
--- a/std/event/future.zig
+++ b/std/event/future.zig
@@ -6,15 +6,20 @@ const AtomicOrder = builtin.AtomicOrder;
const Lock = std.event.Lock;
const Loop = std.event.Loop;
-/// This is a value that starts out unavailable, until a value is put().
+/// This is a value that starts out unavailable, until resolve() is called
/// While it is unavailable, coroutines suspend when they try to get() it,
-/// and then are resumed when the value is put().
-/// At this point the value remains forever available, and another put() is not allowed.
+/// and then are resumed when resolve() is called.
+/// At this point the value remains forever available, and another resolve() is not allowed.
pub fn Future(comptime T: type) type {
return struct {
lock: Lock,
data: T,
- available: u8, // TODO make this a bool
+
+ /// TODO make this an enum
+ /// 0 - not started
+ /// 1 - started
+ /// 2 - finished
+ available: u8,
const Self = this;
const Queue = std.atomic.Queue(promise);
@@ -31,7 +36,7 @@ pub fn Future(comptime T: type) type {
/// available.
/// Thread-safe.
pub async fn get(self: *Self) *T {
- if (@atomicLoad(u8, &self.available, AtomicOrder.SeqCst) == 1) {
+ if (@atomicLoad(u8, &self.available, AtomicOrder.SeqCst) == 2) {
return &self.data;
}
const held = await (async self.lock.acquire() catch unreachable);
@@ -43,18 +48,36 @@ pub fn Future(comptime T: type) type {
/// Gets the data without waiting for it. If it's available, a pointer is
/// returned. Otherwise, null is returned.
pub fn getOrNull(self: *Self) ?*T {
- if (@atomicLoad(u8, &self.available, AtomicOrder.SeqCst) == 1) {
+ if (@atomicLoad(u8, &self.available, AtomicOrder.SeqCst) == 2) {
return &self.data;
} else {
return null;
}
}
+ /// If someone else has started working on the data, wait for them to complete
+ /// and return a pointer to the data. Otherwise, return null, and the caller
+ /// should start working on the data.
+ /// It's not required to call start() before resolve() but it can be useful since
+ /// this method is thread-safe.
+ pub async fn start(self: *Self) ?*T {
+ const state = @cmpxchgStrong(u8, &self.available, 0, 1, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return null;
+ switch (state) {
+ 1 => {
+ const held = await (async self.lock.acquire() catch unreachable);
+ held.release();
+ return &self.data;
+ },
+ 2 => return &self.data,
+ else => unreachable,
+ }
+ }
+
/// Make the data become available. May be called only once.
/// Before calling this, modify the `data` property.
pub fn resolve(self: *Self) void {
- const prev = @atomicRmw(u8, &self.available, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
- assert(prev == 0); // put() called twice
+ const prev = @atomicRmw(u8, &self.available, AtomicRmwOp.Xchg, 2, AtomicOrder.SeqCst);
+ assert(prev == 0 or prev == 1); // resolve() called twice
Lock.Held.release(Lock.Held{ .lock = &self.lock });
}
};
diff --git a/std/index.zig b/std/index.zig
index 3b523f519f..2f4cfb7553 100644
--- a/std/index.zig
+++ b/std/index.zig
@@ -36,6 +36,8 @@ pub const sort = @import("sort.zig");
pub const unicode = @import("unicode.zig");
pub const zig = @import("zig/index.zig");
+pub const lazyInit = @import("lazy_init.zig").lazyInit;
+
test "std" {
// run tests from these
_ = @import("atomic/index.zig");
@@ -71,4 +73,5 @@ test "std" {
_ = @import("sort.zig");
_ = @import("unicode.zig");
_ = @import("zig/index.zig");
+ _ = @import("lazy_init.zig");
}
diff --git a/std/lazy_init.zig b/std/lazy_init.zig
new file mode 100644
index 0000000000..c46c067810
--- /dev/null
+++ b/std/lazy_init.zig
@@ -0,0 +1,85 @@
+const std = @import("index.zig");
+const builtin = @import("builtin");
+const assert = std.debug.assert;
+const AtomicRmwOp = builtin.AtomicRmwOp;
+const AtomicOrder = builtin.AtomicOrder;
+
+/// Thread-safe initialization of global data.
+/// TODO use a mutex instead of a spinlock
+pub fn lazyInit(comptime T: type) LazyInit(T) {
+ return LazyInit(T){
+ .data = undefined,
+ .state = 0,
+ };
+}
+
+fn LazyInit(comptime T: type) type {
+ return struct {
+ state: u8, // TODO make this an enum
+ data: Data,
+
+ const Self = this;
+
+ // TODO this isn't working for void, investigate and then remove this special case
+ const Data = if (@sizeOf(T) == 0) u8 else T;
+ const Ptr = if (T == void) void else *T;
+
+ /// Returns a usable pointer to the initialized data,
+ /// or returns null, indicating that the caller should
+ /// perform the initialization and then call resolve().
+ pub fn get(self: *Self) ?Ptr {
+ while (true) {
+ var state = @cmpxchgWeak(u8, &self.state, 0, 1, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return null;
+ switch (state) {
+ 0 => continue,
+ 1 => {
+ // TODO mutex instead of a spinlock
+ continue;
+ },
+ 2 => {
+ if (@sizeOf(T) == 0) {
+ return T(undefined);
+ } else {
+ return &self.data;
+ }
+ },
+ else => unreachable,
+ }
+ }
+ }
+
+ pub fn resolve(self: *Self) void {
+ const prev = @atomicRmw(u8, &self.state, AtomicRmwOp.Xchg, 2, AtomicOrder.SeqCst);
+ assert(prev == 1); // resolve() called twice
+ }
+ };
+}
+
+var global_number = lazyInit(i32);
+
+test "std.lazyInit" {
+ if (global_number.get()) |_| @panic("bad") else {
+ global_number.data = 1234;
+ global_number.resolve();
+ }
+ if (global_number.get()) |x| {
+ assert(x.* == 1234);
+ } else {
+ @panic("bad");
+ }
+ if (global_number.get()) |x| {
+ assert(x.* == 1234);
+ } else {
+ @panic("bad");
+ }
+}
+
+var global_void = lazyInit(void);
+
+test "std.lazyInit(void)" {
+ if (global_void.get()) |_| @panic("bad") else {
+ global_void.resolve();
+ }
+ assert(global_void.get() != null);
+ assert(global_void.get() != null);
+}
--
cgit v1.2.3
From a9ab528e348a3f07dfaffcdcb1031062b1bfe8bb Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Date: Tue, 17 Jul 2018 15:17:06 -0400
Subject: std.event.Loop.onNextTick dispatches work to waiting threads
---
std/atomic/queue.zig | 14 ++++
std/event/loop.zig | 205 +++++++++++++++++++++++++--------------------------
2 files changed, 113 insertions(+), 106 deletions(-)
(limited to 'std/atomic')
diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig
index 1fd07714e8..df31c88d2a 100644
--- a/std/atomic/queue.zig
+++ b/std/atomic/queue.zig
@@ -51,6 +51,20 @@ pub fn Queue(comptime T: type) type {
return head;
}
+ pub fn unget(self: *Self, node: *Node) void {
+ while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
+ defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
+
+ const opt_head = self.head;
+ self.head = node;
+ if (opt_head) |head| {
+ head.next = node;
+ } else {
+ assert(self.tail == null);
+ self.tail = node;
+ }
+ }
+
pub fn isEmpty(self: *Self) bool {
return @atomicLoad(?*Node, &self.head, builtin.AtomicOrder.SeqCst) != null;
}
diff --git a/std/event/loop.zig b/std/event/loop.zig
index fc927592b9..e2bc9e7eb9 100644
--- a/std/event/loop.zig
+++ b/std/event/loop.zig
@@ -12,7 +12,6 @@ pub const Loop = struct {
next_tick_queue: std.atomic.Queue(promise),
os_data: OsData,
final_resume_node: ResumeNode,
- dispatch_lock: u8, // TODO make this a bool
pending_event_count: usize,
extra_threads: []*std.os.Thread,
@@ -74,11 +73,10 @@ pub const Loop = struct {
/// max(thread_count - 1, 0)
fn initInternal(self: *Loop, allocator: *mem.Allocator, thread_count: usize) !void {
self.* = Loop{
- .pending_event_count = 0,
+ .pending_event_count = 1,
.allocator = allocator,
.os_data = undefined,
.next_tick_queue = std.atomic.Queue(promise).init(),
- .dispatch_lock = 1, // start locked so threads go directly into epoll wait
.extra_threads = undefined,
.available_eventfd_resume_nodes = std.atomic.Stack(ResumeNode.EventFd).init(),
.eventfd_resume_nodes = undefined,
@@ -306,7 +304,7 @@ pub const Loop = struct {
pub fn addFd(self: *Loop, fd: i32, resume_node: *ResumeNode) !void {
_ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
errdefer {
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ self.finishOneEvent();
}
try self.modFd(
fd,
@@ -326,7 +324,7 @@ pub const Loop = struct {
pub fn removeFd(self: *Loop, fd: i32) void {
self.removeFdNoCounter(fd);
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ self.finishOneEvent();
}
fn removeFdNoCounter(self: *Loop, fd: i32) void {
@@ -345,14 +343,70 @@ pub const Loop = struct {
}
}
+ fn dispatch(self: *Loop) void {
+ while (self.available_eventfd_resume_nodes.pop()) |resume_stack_node| {
+ const next_tick_node = self.next_tick_queue.get() orelse {
+ self.available_eventfd_resume_nodes.push(resume_stack_node);
+ return;
+ };
+ const eventfd_node = &resume_stack_node.data;
+ eventfd_node.base.handle = next_tick_node.data;
+ switch (builtin.os) {
+ builtin.Os.macosx => {
+ const kevent_array = (*[1]posix.Kevent)(&eventfd_node.kevent);
+ const eventlist = ([*]posix.Kevent)(undefined)[0..0];
+ _ = std.os.bsdKEvent(self.os_data.kqfd, kevent_array, eventlist, null) catch {
+ self.next_tick_queue.unget(next_tick_node);
+ self.available_eventfd_resume_nodes.push(resume_stack_node);
+ return;
+ };
+ },
+ builtin.Os.linux => {
+ // the pending count is already accounted for
+ const epoll_events = posix.EPOLLONESHOT | std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT |
+ std.os.linux.EPOLLET;
+ self.modFd(
+ eventfd_node.eventfd,
+ eventfd_node.epoll_op,
+ epoll_events,
+ &eventfd_node.base,
+ ) catch {
+ self.next_tick_queue.unget(next_tick_node);
+ self.available_eventfd_resume_nodes.push(resume_stack_node);
+ return;
+ };
+ },
+ builtin.Os.windows => {
+ // this value is never dereferenced but we need it to be non-null so that
+ // the consumer code can decide whether to read the completion key.
+ // it has to do this for normal I/O, so we match that behavior here.
+ const overlapped = @intToPtr(?*windows.OVERLAPPED, 0x1);
+ std.os.windowsPostQueuedCompletionStatus(
+ self.os_data.io_port,
+ undefined,
+ eventfd_node.completion_key,
+ overlapped,
+ ) catch {
+ self.next_tick_queue.unget(next_tick_node);
+ self.available_eventfd_resume_nodes.push(resume_stack_node);
+ return;
+ };
+ },
+ else => @compileError("unsupported OS"),
+ }
+ }
+ }
+
/// Bring your own linked list node. This means it can't fail.
pub fn onNextTick(self: *Loop, node: *NextTickNode) void {
_ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
self.next_tick_queue.put(node);
+ self.dispatch();
}
pub fn run(self: *Loop) void {
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ self.finishOneEvent(); // the reference we start with
+
self.workerRun();
for (self.extra_threads) |extra_thread| {
extra_thread.wait();
@@ -396,106 +450,45 @@ pub const Loop = struct {
}
}
- fn workerRun(self: *Loop) void {
- start_over: while (true) {
- if (@atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) == 0) {
- while (self.next_tick_queue.get()) |next_tick_node| {
- const handle = next_tick_node.data;
- if (self.next_tick_queue.isEmpty()) {
- // last node, just resume it
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
- resume handle;
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
- continue :start_over;
- }
-
- // non-last node, stick it in the epoll/kqueue set so that
- // other threads can get to it
- if (self.available_eventfd_resume_nodes.pop()) |resume_stack_node| {
- const eventfd_node = &resume_stack_node.data;
- eventfd_node.base.handle = handle;
- switch (builtin.os) {
- builtin.Os.macosx => {
- const kevent_array = (*[1]posix.Kevent)(&eventfd_node.kevent);
- const eventlist = ([*]posix.Kevent)(undefined)[0..0];
- _ = std.os.bsdKEvent(self.os_data.kqfd, kevent_array, eventlist, null) catch {
- // fine, we didn't need it anyway
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
- self.available_eventfd_resume_nodes.push(resume_stack_node);
- resume handle;
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
- continue :start_over;
- };
- },
- builtin.Os.linux => {
- // the pending count is already accounted for
- const epoll_events = posix.EPOLLONESHOT | std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT | std.os.linux.EPOLLET;
- self.modFd(eventfd_node.eventfd, eventfd_node.epoll_op, epoll_events, &eventfd_node.base) catch {
- // fine, we didn't need it anyway
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
- self.available_eventfd_resume_nodes.push(resume_stack_node);
- resume handle;
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
- continue :start_over;
- };
- },
- builtin.Os.windows => {
- // this value is never dereferenced but we need it to be non-null so that
- // the consumer code can decide whether to read the completion key.
- // it has to do this for normal I/O, so we match that behavior here.
- const overlapped = @intToPtr(?*windows.OVERLAPPED, 0x1);
- std.os.windowsPostQueuedCompletionStatus(self.os_data.io_port, undefined, eventfd_node.completion_key, overlapped) catch {
- // fine, we didn't need it anyway
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
- self.available_eventfd_resume_nodes.push(resume_stack_node);
- resume handle;
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
- continue :start_over;
- };
- },
- else => @compileError("unsupported OS"),
+ fn finishOneEvent(self: *Loop) void {
+ if (@atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst) == 1) {
+ // cause all the threads to stop
+ switch (builtin.os) {
+ builtin.Os.linux => {
+ // writing 8 bytes to an eventfd cannot fail
+ std.os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable;
+ return;
+ },
+ builtin.Os.macosx => {
+ const final_kevent = (*[1]posix.Kevent)(&self.os_data.final_kevent);
+ const eventlist = ([*]posix.Kevent)(undefined)[0..0];
+ // cannot fail because we already added it and this just enables it
+ _ = std.os.bsdKEvent(self.os_data.kqfd, final_kevent, eventlist, null) catch unreachable;
+ return;
+ },
+ builtin.Os.windows => {
+ var i: usize = 0;
+ while (i < self.os_data.extra_thread_count) : (i += 1) {
+ while (true) {
+ const overlapped = @intToPtr(?*windows.OVERLAPPED, 0x1);
+ std.os.windowsPostQueuedCompletionStatus(self.os_data.io_port, undefined, @ptrToInt(&self.final_resume_node), overlapped) catch continue;
+ break;
}
- } else {
- // threads are too busy, can't add another eventfd to wake one up
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
- resume handle;
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
- continue :start_over;
}
- }
-
- const pending_event_count = @atomicLoad(usize, &self.pending_event_count, AtomicOrder.SeqCst);
- if (pending_event_count == 0) {
- // cause all the threads to stop
- switch (builtin.os) {
- builtin.Os.linux => {
- // writing 8 bytes to an eventfd cannot fail
- std.os.posixWrite(self.os_data.final_eventfd, wakeup_bytes) catch unreachable;
- return;
- },
- builtin.Os.macosx => {
- const final_kevent = (*[1]posix.Kevent)(&self.os_data.final_kevent);
- const eventlist = ([*]posix.Kevent)(undefined)[0..0];
- // cannot fail because we already added it and this just enables it
- _ = std.os.bsdKEvent(self.os_data.kqfd, final_kevent, eventlist, null) catch unreachable;
- return;
- },
- builtin.Os.windows => {
- var i: usize = 0;
- while (i < self.os_data.extra_thread_count) : (i += 1) {
- while (true) {
- const overlapped = @intToPtr(?*windows.OVERLAPPED, 0x1);
- std.os.windowsPostQueuedCompletionStatus(self.os_data.io_port, undefined, @ptrToInt(&self.final_resume_node), overlapped) catch continue;
- break;
- }
- }
- return;
- },
- else => @compileError("unsupported OS"),
- }
- }
+ return;
+ },
+ else => @compileError("unsupported OS"),
+ }
+ }
+ }
- _ = @atomicRmw(u8, &self.dispatch_lock, AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst);
+ fn workerRun(self: *Loop) void {
+ while (true) {
+ while (true) {
+ const next_tick_node = self.next_tick_queue.get() orelse break;
+ self.dispatch();
+ resume next_tick_node.data;
+ self.finishOneEvent();
}
switch (builtin.os) {
@@ -519,7 +512,7 @@ pub const Loop = struct {
}
resume handle;
if (resume_node_id == ResumeNode.Id.EventFd) {
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ self.finishOneEvent();
}
}
},
@@ -541,7 +534,7 @@ pub const Loop = struct {
}
resume handle;
if (resume_node_id == ResumeNode.Id.EventFd) {
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ self.finishOneEvent();
}
}
},
@@ -570,7 +563,7 @@ pub const Loop = struct {
}
resume handle;
if (resume_node_id == ResumeNode.Id.EventFd) {
- _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
+ self.finishOneEvent();
}
},
else => @compileError("unsupported OS"),
--
cgit v1.2.3
|