authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-03 16:13:28-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-03 16:13:28-05:00
logdfbc063f79dc1358208216b466c1bf8c44baa430
tree219c5f46a2e688fc1799117a1680102528691ce3
parentc90c256868a80cd35e9ba679ba082330592620c9
signaturelock-open Commit is signed but in an unrecognized format.

`std.mem.Allocator.create` replaced with better API

`std.mem.Allocator.createOne` is renamed to `std.mem.Allocator.create`. The problem with the previous API is that even after copy elision, the initalization value passed as a parameter would always be a copy. With the new API, once copy elision is done, initialization functions can directly initialize allocated memory in place. Related: * #1872 * #1873

23 files changed, 451 insertions(+), 312 deletions(-)

src-self-hosted/compilation.zig+43-29
......@@ -83,10 +83,11 @@ pub const ZigCompiler = struct {
8383 const context_ref = c.LLVMContextCreate() orelse return error.OutOfMemory;
8484 errdefer c.LLVMContextDispose(context_ref);
8585
86 const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node{
86 const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node);
87 node.* = std.atomic.Stack(llvm.ContextRef).Node{
8788 .next = undefined,
8889 .data = context_ref,
89 });
90 };
9091 errdefer self.loop.allocator.destroy(node);
9192
9293 return LlvmHandle{ .node = node };
......@@ -596,7 +597,8 @@ pub const Compilation = struct {
596597 }
597598
598599 fn initTypes(comp: *Compilation) !void {
599 comp.meta_type = try comp.arena().create(Type.MetaType{
600 comp.meta_type = try comp.arena().create(Type.MetaType);
601 comp.meta_type.* = Type.MetaType{
600602 .base = Type{
601603 .name = "type",
602604 .base = Value{
......@@ -608,12 +610,13 @@ pub const Compilation = struct {
608610 .abi_alignment = Type.AbiAlignment.init(comp.loop),
609611 },
610612 .value = undefined,
611 });
613 };
612614 comp.meta_type.value = &comp.meta_type.base;
613615 comp.meta_type.base.base.typ = &comp.meta_type.base;
614616 assert((try comp.primitive_type_table.put(comp.meta_type.base.name, &comp.meta_type.base)) == null);
615617
616 comp.void_type = try comp.arena().create(Type.Void{
618 comp.void_type = try comp.arena().create(Type.Void);
619 comp.void_type.* = Type.Void{
617620 .base = Type{
618621 .name = "void",
619622 .base = Value{
......@@ -624,10 +627,11 @@ pub const Compilation = struct {
624627 .id = builtin.TypeId.Void,
625628 .abi_alignment = Type.AbiAlignment.init(comp.loop),
626629 },
627 });
630 };
628631 assert((try comp.primitive_type_table.put(comp.void_type.base.name, &comp.void_type.base)) == null);
629632
630 comp.noreturn_type = try comp.arena().create(Type.NoReturn{
633 comp.noreturn_type = try comp.arena().create(Type.NoReturn);
634 comp.noreturn_type.* = Type.NoReturn{
631635 .base = Type{
632636 .name = "noreturn",
633637 .base = Value{
......@@ -638,10 +642,11 @@ pub const Compilation = struct {
638642 .id = builtin.TypeId.NoReturn,
639643 .abi_alignment = Type.AbiAlignment.init(comp.loop),
640644 },
641 });
645 };
642646 assert((try comp.primitive_type_table.put(comp.noreturn_type.base.name, &comp.noreturn_type.base)) == null);
643647
644 comp.comptime_int_type = try comp.arena().create(Type.ComptimeInt{
648 comp.comptime_int_type = try comp.arena().create(Type.ComptimeInt);
649 comp.comptime_int_type.* = Type.ComptimeInt{
645650 .base = Type{
646651 .name = "comptime_int",
647652 .base = Value{
......@@ -652,10 +657,11 @@ pub const Compilation = struct {
652657 .id = builtin.TypeId.ComptimeInt,
653658 .abi_alignment = Type.AbiAlignment.init(comp.loop),
654659 },
655 });
660 };
656661 assert((try comp.primitive_type_table.put(comp.comptime_int_type.base.name, &comp.comptime_int_type.base)) == null);
657662
658 comp.bool_type = try comp.arena().create(Type.Bool{
663 comp.bool_type = try comp.arena().create(Type.Bool);
664 comp.bool_type.* = Type.Bool{
659665 .base = Type{
660666 .name = "bool",
661667 .base = Value{
......@@ -666,45 +672,50 @@ pub const Compilation = struct {
666672 .id = builtin.TypeId.Bool,
667673 .abi_alignment = Type.AbiAlignment.init(comp.loop),
668674 },
669 });
675 };
670676 assert((try comp.primitive_type_table.put(comp.bool_type.base.name, &comp.bool_type.base)) == null);
671677
672 comp.void_value = try comp.arena().create(Value.Void{
678 comp.void_value = try comp.arena().create(Value.Void);
679 comp.void_value.* = Value.Void{
673680 .base = Value{
674681 .id = Value.Id.Void,
675682 .typ = &Type.Void.get(comp).base,
676683 .ref_count = std.atomic.Int(usize).init(1),
677684 },
678 });
685 };
679686
680 comp.true_value = try comp.arena().create(Value.Bool{
687 comp.true_value = try comp.arena().create(Value.Bool);
688 comp.true_value.* = Value.Bool{
681689 .base = Value{
682690 .id = Value.Id.Bool,
683691 .typ = &Type.Bool.get(comp).base,
684692 .ref_count = std.atomic.Int(usize).init(1),
685693 },
686694 .x = true,
687 });
695 };
688696
689 comp.false_value = try comp.arena().create(Value.Bool{
697 comp.false_value = try comp.arena().create(Value.Bool);
698 comp.false_value.* = Value.Bool{
690699 .base = Value{
691700 .id = Value.Id.Bool,
692701 .typ = &Type.Bool.get(comp).base,
693702 .ref_count = std.atomic.Int(usize).init(1),
694703 },
695704 .x = false,
696 });
705 };
697706
698 comp.noreturn_value = try comp.arena().create(Value.NoReturn{
707 comp.noreturn_value = try comp.arena().create(Value.NoReturn);
708 comp.noreturn_value.* = Value.NoReturn{
699709 .base = Value{
700710 .id = Value.Id.NoReturn,
701711 .typ = &Type.NoReturn.get(comp).base,
702712 .ref_count = std.atomic.Int(usize).init(1),
703713 },
704 });
714 };
705715
706716 for (CInt.list) |cint, i| {
707 const c_int_type = try comp.arena().create(Type.Int{
717 const c_int_type = try comp.arena().create(Type.Int);
718 c_int_type.* = Type.Int{
708719 .base = Type{
709720 .name = cint.zig_name,
710721 .base = Value{
......@@ -720,11 +731,12 @@ pub const Compilation = struct {
720731 .bit_count = comp.target.cIntTypeSizeInBits(cint.id),
721732 },
722733 .garbage_node = undefined,
723 });
734 };
724735 comp.c_int_types[i] = c_int_type;
725736 assert((try comp.primitive_type_table.put(cint.zig_name, &c_int_type.base)) == null);
726737 }
727 comp.u8_type = try comp.arena().create(Type.Int{
738 comp.u8_type = try comp.arena().create(Type.Int);
739 comp.u8_type.* = Type.Int{
728740 .base = Type{
729741 .name = "u8",
730742 .base = Value{
......@@ -740,7 +752,7 @@ pub const Compilation = struct {
740752 .bit_count = 8,
741753 },
742754 .garbage_node = undefined,
743 });
755 };
744756 assert((try comp.primitive_type_table.put(comp.u8_type.base.name, &comp.u8_type.base)) == null);
745757 }
746758
......@@ -829,7 +841,7 @@ pub const Compilation = struct {
829841 };
830842 errdefer self.gpa().free(source_code);
831843
832 const tree = try self.gpa().createOne(ast.Tree);
844 const tree = try self.gpa().create(ast.Tree);
833845 tree.* = try std.zig.parse(self.gpa(), source_code);
834846 errdefer {
835847 tree.deinit();
......@@ -925,7 +937,8 @@ pub const Compilation = struct {
925937 }
926938 } else {
927939 // add new decl
928 const fn_decl = try self.gpa().create(Decl.Fn{
940 const fn_decl = try self.gpa().create(Decl.Fn);
941 fn_decl.* = Decl.Fn{
929942 .base = Decl{
930943 .id = Decl.Id.Fn,
931944 .name = name,
......@@ -936,7 +949,7 @@ pub const Compilation = struct {
936949 },
937950 .value = Decl.Fn.Val{ .Unresolved = {} },
938951 .fn_proto = fn_proto,
939 });
952 };
940953 tree_scope.base.ref();
941954 errdefer self.gpa().destroy(fn_decl);
942955
......@@ -1140,12 +1153,13 @@ pub const Compilation = struct {
11401153 }
11411154 }
11421155
1143 const link_lib = try self.gpa().create(LinkLib{
1156 const link_lib = try self.gpa().create(LinkLib);
1157 link_lib.* = LinkLib{
11441158 .name = name,
11451159 .path = null,
11461160 .provided_explicitly = provided_explicitly,
11471161 .symbols = ArrayList([]u8).init(self.gpa()),
1148 });
1162 };
11491163 try self.link_libs_list.append(link_lib);
11501164 if (is_libc) {
11511165 self.libc_link_lib = link_lib;
src-self-hosted/errmsg.zig+12-8
......@@ -118,7 +118,8 @@ pub const Msg = struct {
118118 const realpath = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
119119 errdefer comp.gpa().free(realpath);
120120
121 const msg = try comp.gpa().create(Msg{
121 const msg = try comp.gpa().create(Msg);
122 msg.* = Msg{
122123 .text = text,
123124 .realpath = realpath,
124125 .data = Data{
......@@ -128,7 +129,7 @@ pub const Msg = struct {
128129 .span = span,
129130 },
130131 },
131 });
132 };
132133 tree_scope.base.ref();
133134 return msg;
134135 }
......@@ -139,13 +140,14 @@ pub const Msg = struct {
139140 const realpath_copy = try mem.dupe(comp.gpa(), u8, realpath);
140141 errdefer comp.gpa().free(realpath_copy);
141142
142 const msg = try comp.gpa().create(Msg{
143 const msg = try comp.gpa().create(Msg);
144 msg.* = Msg{
143145 .text = text,
144146 .realpath = realpath_copy,
145147 .data = Data{
146148 .Cli = Cli{ .allocator = comp.gpa() },
147149 },
148 });
150 };
149151 return msg;
150152 }
151153
......@@ -164,7 +166,8 @@ pub const Msg = struct {
164166 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
165167 try parse_error.render(&tree_scope.tree.tokens, out_stream);
166168
167 const msg = try comp.gpa().create(Msg{
169 const msg = try comp.gpa().create(Msg);
170 msg.* = Msg{
168171 .text = undefined,
169172 .realpath = realpath_copy,
170173 .data = Data{
......@@ -177,7 +180,7 @@ pub const Msg = struct {
177180 },
178181 },
179182 },
180 });
183 };
181184 tree_scope.base.ref();
182185 msg.text = text_buf.toOwnedSlice();
183186 return msg;
......@@ -203,7 +206,8 @@ pub const Msg = struct {
203206 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
204207 try parse_error.render(&tree.tokens, out_stream);
205208
206 const msg = try allocator.create(Msg{
209 const msg = try allocator.create(Msg);
210 msg.* = Msg{
207211 .text = undefined,
208212 .realpath = realpath_copy,
209213 .data = Data{
......@@ -216,7 +220,7 @@ pub const Msg = struct {
216220 },
217221 },
218222 },
219 });
223 };
220224 msg.text = text_buf.toOwnedSlice();
221225 errdefer allocator.destroy(msg);
222226
src-self-hosted/ir.zig+9-6
......@@ -1021,12 +1021,13 @@ pub const Builder = struct {
10211021 pub const Error = Analyze.Error;
10221022
10231023 pub fn init(comp: *Compilation, tree_scope: *Scope.AstTree, begin_scope: ?*Scope) !Builder {
1024 const code = try comp.gpa().create(Code{
1024 const code = try comp.gpa().create(Code);
1025 code.* = Code{
10251026 .basic_block_list = undefined,
10261027 .arena = std.heap.ArenaAllocator.init(comp.gpa()),
10271028 .return_type = null,
10281029 .tree_scope = tree_scope,
1029 });
1030 };
10301031 code.basic_block_list = std.ArrayList(*BasicBlock).init(&code.arena.allocator);
10311032 errdefer code.destroy(comp.gpa());
10321033
......@@ -1052,7 +1053,8 @@ pub const Builder = struct {
10521053
10531054 /// No need to clean up resources thanks to the arena allocator.
10541055 pub fn createBasicBlock(self: *Builder, scope: *Scope, name_hint: [*]const u8) !*BasicBlock {
1055 const basic_block = try self.arena().create(BasicBlock{
1056 const basic_block = try self.arena().create(BasicBlock);
1057 basic_block.* = BasicBlock{
10561058 .ref_count = 0,
10571059 .name_hint = name_hint,
10581060 .debug_id = self.next_debug_id,
......@@ -1063,7 +1065,7 @@ pub const Builder = struct {
10631065 .ref_instruction = null,
10641066 .llvm_block = undefined,
10651067 .llvm_exit_block = undefined,
1066 });
1068 };
10671069 self.next_debug_id += 1;
10681070 return basic_block;
10691071 }
......@@ -1774,7 +1776,8 @@ pub const Builder = struct {
17741776 params: I.Params,
17751777 is_generated: bool,
17761778 ) !*Inst {
1777 const inst = try self.arena().create(I{
1779 const inst = try self.arena().create(I);
1780 inst.* = I{
17781781 .base = Inst{
17791782 .id = Inst.typeToId(I),
17801783 .is_generated = is_generated,
......@@ -1793,7 +1796,7 @@ pub const Builder = struct {
17931796 .owner_bb = self.current_basic_block,
17941797 },
17951798 .params = params,
1796 });
1799 };
17971800
17981801 // Look at the params and ref() other instructions
17991802 comptime var i = 0;
src-self-hosted/main.zig+3-2
......@@ -944,12 +944,13 @@ const CliPkg = struct {
944944 parent: ?*CliPkg,
945945
946946 pub fn init(allocator: *mem.Allocator, name: []const u8, path: []const u8, parent: ?*CliPkg) !*CliPkg {
947 var pkg = try allocator.create(CliPkg{
947 var pkg = try allocator.create(CliPkg);
948 pkg.* = CliPkg{
948949 .name = name,
949950 .path = path,
950951 .children = ArrayList(*CliPkg).init(allocator),
951952 .parent = parent,
952 });
953 };
953954 return pkg;
954955 }
955956
src-self-hosted/package.zig+4-2
......@@ -15,11 +15,13 @@ pub const Package = struct {
1515 /// makes internal copies of root_src_dir and root_src_path
1616 /// allocator should be an arena allocator because Package never frees anything
1717 pub fn create(allocator: *mem.Allocator, root_src_dir: []const u8, root_src_path: []const u8) !*Package {
18 return allocator.create(Package{
18 const ptr = try allocator.create(Package);
19 ptr.* = Package{
1920 .root_src_dir = try Buffer.init(allocator, root_src_dir),
2021 .root_src_path = try Buffer.init(allocator, root_src_path),
2122 .table = Table.init(allocator),
22 });
23 };
24 return ptr;
2325 }
2426
2527 pub fn add(self: *Package, name: []const u8, package: *Package) !void {
src-self-hosted/scope.zig+9-9
......@@ -120,7 +120,7 @@ pub const Scope = struct {
120120 /// Creates a Root scope with 1 reference
121121 /// Takes ownership of realpath
122122 pub fn create(comp: *Compilation, realpath: []u8) !*Root {
123 const self = try comp.gpa().createOne(Root);
123 const self = try comp.gpa().create(Root);
124124 self.* = Root{
125125 .base = Scope{
126126 .id = Id.Root,
......@@ -150,7 +150,7 @@ pub const Scope = struct {
150150 /// Creates a scope with 1 reference
151151 /// Takes ownership of tree, will deinit and destroy when done.
152152 pub fn create(comp: *Compilation, tree: *ast.Tree, root_scope: *Root) !*AstTree {
153 const self = try comp.gpa().createOne(AstTree);
153 const self = try comp.gpa().create(AstTree);
154154 self.* = AstTree{
155155 .base = undefined,
156156 .tree = tree,
......@@ -182,7 +182,7 @@ pub const Scope = struct {
182182
183183 /// Creates a Decls scope with 1 reference
184184 pub fn create(comp: *Compilation, parent: *Scope) !*Decls {
185 const self = try comp.gpa().createOne(Decls);
185 const self = try comp.gpa().create(Decls);
186186 self.* = Decls{
187187 .base = undefined,
188188 .table = event.RwLocked(Decl.Table).init(comp.loop, Decl.Table.init(comp.gpa())),
......@@ -235,7 +235,7 @@ pub const Scope = struct {
235235
236236 /// Creates a Block scope with 1 reference
237237 pub fn create(comp: *Compilation, parent: *Scope) !*Block {
238 const self = try comp.gpa().createOne(Block);
238 const self = try comp.gpa().create(Block);
239239 self.* = Block{
240240 .base = undefined,
241241 .incoming_values = undefined,
......@@ -262,7 +262,7 @@ pub const Scope = struct {
262262 /// Creates a FnDef scope with 1 reference
263263 /// Must set the fn_val later
264264 pub fn create(comp: *Compilation, parent: *Scope) !*FnDef {
265 const self = try comp.gpa().createOne(FnDef);
265 const self = try comp.gpa().create(FnDef);
266266 self.* = FnDef{
267267 .base = undefined,
268268 .fn_val = null,
......@@ -281,7 +281,7 @@ pub const Scope = struct {
281281
282282 /// Creates a CompTime scope with 1 reference
283283 pub fn create(comp: *Compilation, parent: *Scope) !*CompTime {
284 const self = try comp.gpa().createOne(CompTime);
284 const self = try comp.gpa().create(CompTime);
285285 self.* = CompTime{ .base = undefined };
286286 self.base.init(Id.CompTime, parent);
287287 return self;
......@@ -309,7 +309,7 @@ pub const Scope = struct {
309309 kind: Kind,
310310 defer_expr_scope: *DeferExpr,
311311 ) !*Defer {
312 const self = try comp.gpa().createOne(Defer);
312 const self = try comp.gpa().create(Defer);
313313 self.* = Defer{
314314 .base = undefined,
315315 .defer_expr_scope = defer_expr_scope,
......@@ -333,7 +333,7 @@ pub const Scope = struct {
333333
334334 /// Creates a DeferExpr scope with 1 reference
335335 pub fn create(comp: *Compilation, parent: *Scope, expr_node: *ast.Node) !*DeferExpr {
336 const self = try comp.gpa().createOne(DeferExpr);
336 const self = try comp.gpa().create(DeferExpr);
337337 self.* = DeferExpr{
338338 .base = undefined,
339339 .expr_node = expr_node,
......@@ -398,7 +398,7 @@ pub const Scope = struct {
398398 }
399399
400400 fn create(comp: *Compilation, parent: *Scope, name: []const u8, src_node: *ast.Node) !*Var {
401 const self = try comp.gpa().createOne(Var);
401 const self = try comp.gpa().create(Var);
402402 self.* = Var{
403403 .base = undefined,
404404 .name = name,
src-self-hosted/type.zig+10-7
......@@ -413,7 +413,7 @@ pub const Type = struct {
413413 key.ref();
414414 errdefer key.deref(comp);
415415
416 const self = try comp.gpa().createOne(Fn);
416 const self = try comp.gpa().create(Fn);
417417 self.* = Fn{
418418 .base = undefined,
419419 .key = key,
......@@ -615,11 +615,12 @@ pub const Type = struct {
615615 }
616616 }
617617
618 const self = try comp.gpa().create(Int{
618 const self = try comp.gpa().create(Int);
619 self.* = Int{
619620 .base = undefined,
620621 .key = key,
621622 .garbage_node = undefined,
622 });
623 };
623624 errdefer comp.gpa().destroy(self);
624625
625626 const u_or_i = "ui"[@boolToInt(key.is_signed)];
......@@ -781,11 +782,12 @@ pub const Type = struct {
781782 }
782783 }
783784
784 const self = try comp.gpa().create(Pointer{
785 const self = try comp.gpa().create(Pointer);
786 self.* = Pointer{
785787 .base = undefined,
786788 .key = normal_key,
787789 .garbage_node = undefined,
788 });
790 };
789791 errdefer comp.gpa().destroy(self);
790792
791793 const size_str = switch (self.key.size) {
......@@ -879,11 +881,12 @@ pub const Type = struct {
879881 }
880882 }
881883
882 const self = try comp.gpa().create(Array{
884 const self = try comp.gpa().create(Array);
885 self.* = Array{
883886 .base = undefined,
884887 .key = key,
885888 .garbage_node = undefined,
886 });
889 };
887890 errdefer comp.gpa().destroy(self);
888891
889892 const name = try std.fmt.allocPrint(comp.gpa(), "[{}]{}", key.len, key.elem_type.name);
src-self-hosted/value.zig+21-14
......@@ -135,14 +135,15 @@ pub const Value = struct {
135135 symbol_name: Buffer,
136136
137137 pub fn create(comp: *Compilation, fn_type: *Type.Fn, symbol_name: Buffer) !*FnProto {
138 const self = try comp.gpa().create(FnProto{
138 const self = try comp.gpa().create(FnProto);
139 self.* = FnProto{
139140 .base = Value{
140141 .id = Value.Id.FnProto,
141142 .typ = &fn_type.base,
142143 .ref_count = std.atomic.Int(usize).init(1),
143144 },
144145 .symbol_name = symbol_name,
145 });
146 };
146147 fn_type.base.base.ref();
147148 return self;
148149 }
......@@ -190,14 +191,16 @@ pub const Value = struct {
190191 /// Creates a Fn value with 1 ref
191192 /// Takes ownership of symbol_name
192193 pub fn create(comp: *Compilation, fn_type: *Type.Fn, fndef_scope: *Scope.FnDef, symbol_name: Buffer) !*Fn {
193 const link_set_node = try comp.gpa().create(Compilation.FnLinkSet.Node{
194 const link_set_node = try comp.gpa().create(Compilation.FnLinkSet.Node);
195 link_set_node.* = Compilation.FnLinkSet.Node{
194196 .data = null,
195197 .next = undefined,
196198 .prev = undefined,
197 });
199 };
198200 errdefer comp.gpa().destroy(link_set_node);
199201
200 const self = try comp.gpa().create(Fn{
202 const self = try comp.gpa().create(Fn);
203 self.* = Fn{
201204 .base = Value{
202205 .id = Value.Id.Fn,
203206 .typ = &fn_type.base,
......@@ -209,7 +212,7 @@ pub const Value = struct {
209212 .symbol_name = symbol_name,
210213 .containing_object = Buffer.initNull(comp.gpa()),
211214 .link_set_node = link_set_node,
212 });
215 };
213216 fn_type.base.base.ref();
214217 fndef_scope.fn_val = self;
215218 fndef_scope.base.ref();
......@@ -353,7 +356,8 @@ pub const Value = struct {
353356 var ptr_type_consumed = false;
354357 errdefer if (!ptr_type_consumed) ptr_type.base.base.deref(comp);
355358
356 const self = try comp.gpa().create(Value.Ptr{
359 const self = try comp.gpa().create(Value.Ptr);
360 self.* = Value.Ptr{
357361 .base = Value{
358362 .id = Value.Id.Ptr,
359363 .typ = &ptr_type.base,
......@@ -366,7 +370,7 @@ pub const Value = struct {
366370 },
367371 },
368372 .mut = Mut.CompTimeConst,
369 });
373 };
370374 ptr_type_consumed = true;
371375 errdefer comp.gpa().destroy(self);
372376
......@@ -430,14 +434,15 @@ pub const Value = struct {
430434 }) catch unreachable);
431435 errdefer array_type.base.base.deref(comp);
432436
433 const self = try comp.gpa().create(Value.Array{
437 const self = try comp.gpa().create(Value.Array);
438 self.* = Value.Array{
434439 .base = Value{
435440 .id = Value.Id.Array,
436441 .typ = &array_type.base,
437442 .ref_count = std.atomic.Int(usize).init(1),
438443 },
439444 .special = Special{ .OwnedBuffer = buffer },
440 });
445 };
441446 errdefer comp.gpa().destroy(self);
442447
443448 return self;
......@@ -509,14 +514,15 @@ pub const Value = struct {
509514 big_int: std.math.big.Int,
510515
511516 pub fn createFromString(comp: *Compilation, typ: *Type, base: u8, value: []const u8) !*Int {
512 const self = try comp.gpa().create(Value.Int{
517 const self = try comp.gpa().create(Value.Int);
518 self.* = Value.Int{
513519 .base = Value{
514520 .id = Value.Id.Int,
515521 .typ = typ,
516522 .ref_count = std.atomic.Int(usize).init(1),
517523 },
518524 .big_int = undefined,
519 });
525 };
520526 typ.base.ref();
521527 errdefer comp.gpa().destroy(self);
522528
......@@ -557,14 +563,15 @@ pub const Value = struct {
557563 old.base.typ.base.ref();
558564 errdefer old.base.typ.base.deref(comp);
559565
560 const new = try comp.gpa().create(Value.Int{
566 const new = try comp.gpa().create(Value.Int);
567 new.* = Value.Int{
561568 .base = Value{
562569 .id = Value.Id.Int,
563570 .typ = old.base.typ,
564571 .ref_count = std.atomic.Int(usize).init(1),
565572 },
566573 .big_int = undefined,
567 });
574 };
568575 errdefer comp.gpa().destroy(new);
569576
570577 new.big_int = try old.big_int.clone();
std/atomic/queue.zig+3-2
......@@ -221,11 +221,12 @@ fn startPuts(ctx: *Context) u8 {
221221 while (put_count != 0) : (put_count -= 1) {
222222 std.os.time.sleep(1); // let the os scheduler be our fuzz
223223 const x = @bitCast(i32, r.random.scalar(u32));
224 const node = ctx.allocator.create(Queue(i32).Node{
224 const node = ctx.allocator.create(Queue(i32).Node) catch unreachable;
225 node.* = Queue(i32).Node{
225226 .prev = undefined,
226227 .next = undefined,
227228 .data = x,
228 }) catch unreachable;
229 };
229230 ctx.queue.put(node);
230231 _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
231232 }
std/atomic/stack.zig+3-2
......@@ -155,10 +155,11 @@ fn startPuts(ctx: *Context) u8 {
155155 while (put_count != 0) : (put_count -= 1) {
156156 std.os.time.sleep(1); // let the os scheduler be our fuzz
157157 const x = @bitCast(i32, r.random.scalar(u32));
158 const node = ctx.allocator.create(Stack(i32).Node{
158 const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
159 node.* = Stack(i32).Node{
159160 .next = undefined,
160161 .data = x,
161 }) catch unreachable;
162 };
162163 ctx.stack.push(node);
163164 _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
164165 }
std/build.zig+36-20
......@@ -89,7 +89,7 @@ pub const Builder = struct {
8989 };
9090
9191 pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
92 const env_map = allocator.createOne(BufMap) catch unreachable;
92 const env_map = allocator.create(BufMap) catch unreachable;
9393 env_map.* = os.getEnvMap(allocator) catch unreachable;
9494 var self = Builder{
9595 .zig_exe = zig_exe,
......@@ -170,7 +170,8 @@ pub const Builder = struct {
170170 }
171171
172172 pub fn addTest(self: *Builder, root_src: []const u8) *TestStep {
173 const test_step = self.allocator.create(TestStep.init(self, root_src)) catch unreachable;
173 const test_step = self.allocator.create(TestStep) catch unreachable;
174 test_step.* = TestStep.init(self, root_src);
174175 return test_step;
175176 }
176177
......@@ -202,18 +203,21 @@ pub const Builder = struct {
202203 }
203204
204205 pub fn addWriteFile(self: *Builder, file_path: []const u8, data: []const u8) *WriteFileStep {
205 const write_file_step = self.allocator.create(WriteFileStep.init(self, file_path, data)) catch unreachable;
206 const write_file_step = self.allocator.create(WriteFileStep) catch unreachable;
207 write_file_step.* = WriteFileStep.init(self, file_path, data);
206208 return write_file_step;
207209 }
208210
209211 pub fn addLog(self: *Builder, comptime format: []const u8, args: ...) *LogStep {
210212 const data = self.fmt(format, args);
211 const log_step = self.allocator.create(LogStep.init(self, data)) catch unreachable;
213 const log_step = self.allocator.create(LogStep) catch unreachable;
214 log_step.* = LogStep.init(self, data);
212215 return log_step;
213216 }
214217
215218 pub fn addRemoveDirTree(self: *Builder, dir_path: []const u8) *RemoveDirStep {
216 const remove_dir_step = self.allocator.create(RemoveDirStep.init(self, dir_path)) catch unreachable;
219 const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable;
220 remove_dir_step.* = RemoveDirStep.init(self, dir_path);
217221 return remove_dir_step;
218222 }
219223
......@@ -414,10 +418,11 @@ pub const Builder = struct {
414418 }
415419
416420 pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {
417 const step_info = self.allocator.create(TopLevelStep{
421 const step_info = self.allocator.create(TopLevelStep) catch unreachable;
422 step_info.* = TopLevelStep{
418423 .step = Step.initNoOp(name, self.allocator),
419424 .description = description,
420 }) catch unreachable;
425 };
421426 self.top_level_steps.append(step_info) catch unreachable;
422427 return &step_info.step;
423428 }
......@@ -616,7 +621,8 @@ pub const Builder = struct {
616621 const full_dest_path = os.path.resolve(self.allocator, self.prefix, dest_rel_path) catch unreachable;
617622 self.pushInstalledFile(full_dest_path);
618623
619 const install_step = self.allocator.create(InstallFileStep.init(self, src_path, full_dest_path)) catch unreachable;
624 const install_step = self.allocator.create(InstallFileStep) catch unreachable;
625 install_step.* = InstallFileStep.init(self, src_path, full_dest_path);
620626 return install_step;
621627 }
622628
......@@ -865,43 +871,51 @@ pub const LibExeObjStep = struct {
865871 };
866872
867873 pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep {
868 const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, false, ver)) catch unreachable;
874 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
875 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
869876 return self;
870877 }
871878
872879 pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: Version) *LibExeObjStep {
873 const self = builder.allocator.create(initC(builder, name, Kind.Lib, version, false)) catch unreachable;
880 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
881 self.* = initC(builder, name, Kind.Lib, version, false);
874882 return self;
875883 }
876884
877885 pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
878 const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0))) catch unreachable;
886 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
887 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
879888 return self;
880889 }
881890
882891 pub fn createCStaticLibrary(builder: *Builder, name: []const u8) *LibExeObjStep {
883 const self = builder.allocator.create(initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true)) catch unreachable;
892 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
893 self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
884894 return self;
885895 }
886896
887897 pub fn createObject(builder: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep {
888 const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0))) catch unreachable;
898 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
899 self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
889900 return self;
890901 }
891902
892903 pub fn createCObject(builder: *Builder, name: []const u8, src: []const u8) *LibExeObjStep {
893 const self = builder.allocator.create(initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false)) catch unreachable;
904 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
905 self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
894906 self.object_src = src;
895907 return self;
896908 }
897909
898910 pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8, static: bool) *LibExeObjStep {
899 const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Exe, static, builder.version(0, 0, 0))) catch unreachable;
911 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
912 self.* = initExtraArgs(builder, name, root_src, Kind.Exe, static, builder.version(0, 0, 0));
900913 return self;
901914 }
902915
903916 pub fn createCExecutable(builder: *Builder, name: []const u8) *LibExeObjStep {
904 const self = builder.allocator.create(initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false)) catch unreachable;
917 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
918 self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
905919 return self;
906920 }
907921
......@@ -1914,13 +1928,14 @@ pub const CommandStep = struct {
19141928
19151929 /// ::argv is copied.
19161930 pub fn create(builder: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) *CommandStep {
1917 const self = builder.allocator.create(CommandStep{
1931 const self = builder.allocator.create(CommandStep) catch unreachable;
1932 self.* = CommandStep{
19181933 .builder = builder,
19191934 .step = Step.init(argv[0], builder.allocator, make),
19201935 .argv = builder.allocator.alloc([]u8, argv.len) catch unreachable,
19211936 .cwd = cwd,
19221937 .env_map = env_map,
1923 }) catch unreachable;
1938 };
19241939
19251940 mem.copy([]const u8, self.argv, argv);
19261941 self.step.name = self.argv[0];
......@@ -1949,12 +1964,13 @@ const InstallArtifactStep = struct {
19491964 LibExeObjStep.Kind.Exe => builder.exe_dir,
19501965 LibExeObjStep.Kind.Lib => builder.lib_dir,
19511966 };
1952 const self = builder.allocator.create(Self{
1967 const self = builder.allocator.create(Self) catch unreachable;
1968 self.* = Self{
19531969 .builder = builder,
19541970 .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
19551971 .artifact = artifact,
19561972 .dest_file = os.path.join(builder.allocator, dest_dir, artifact.out_filename) catch unreachable,
1957 }) catch unreachable;
1973 };
19581974 self.step.dependOn(&artifact.step);
19591975 builder.pushInstalledFile(self.dest_file);
19601976 if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
std/debug/index.zig+4-3
......@@ -751,7 +751,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo {
751751 const self_file = try os.openSelfExe();
752752 defer self_file.close();
753753
754 const coff_obj = try allocator.createOne(coff.Coff);
754 const coff_obj = try allocator.create(coff.Coff);
755755 coff_obj.* = coff.Coff{
756756 .in_file = self_file,
757757 .allocator = allocator,
......@@ -1036,7 +1036,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
10361036 }
10371037 }
10381038 }
1039 const sentinel = try allocator.createOne(macho.nlist_64);
1039 const sentinel = try allocator.create(macho.nlist_64);
10401040 sentinel.* = macho.nlist_64{
10411041 .n_strx = 0,
10421042 .n_type = 36,
......@@ -1949,7 +1949,8 @@ fn scanAllCompileUnits(di: *DwarfInfo) !void {
19491949
19501950 try di.dwarf_seekable_stream.seekTo(compile_unit_pos);
19511951
1952 const compile_unit_die = try di.allocator().create(try parseDie(di, abbrev_table, is_64));
1952 const compile_unit_die = try di.allocator().create(Die);
1953 compile_unit_die.* = try parseDie(di, abbrev_table, is_64);
19531954
19541955 if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo;
19551956
std/event/channel.zig+3-2
......@@ -54,7 +54,8 @@ pub fn Channel(comptime T: type) type {
5454 const buffer_nodes = try loop.allocator.alloc(T, capacity);
5555 errdefer loop.allocator.free(buffer_nodes);
5656
57 const self = try loop.allocator.create(SelfChannel{
57 const self = try loop.allocator.create(SelfChannel);
58 self.* = SelfChannel{
5859 .loop = loop,
5960 .buffer_len = 0,
6061 .buffer_nodes = buffer_nodes,
......@@ -66,7 +67,7 @@ pub fn Channel(comptime T: type) type {
6667 .or_null_queue = std.atomic.Queue(*std.atomic.Queue(GetNode).Node).init(),
6768 .get_count = 0,
6869 .put_count = 0,
69 });
70 };
7071 errdefer loop.allocator.destroy(self);
7172
7273 return self;
std/event/fs.zig+4-4
......@@ -495,7 +495,7 @@ pub const CloseOperation = struct {
495495 };
496496
497497 pub fn start(loop: *Loop) (error{OutOfMemory}!*CloseOperation) {
498 const self = try loop.allocator.createOne(CloseOperation);
498 const self = try loop.allocator.create(CloseOperation);
499499 self.* = CloseOperation{
500500 .loop = loop,
501501 .os_data = switch (builtin.os) {
......@@ -787,7 +787,7 @@ pub fn Watch(comptime V: type) type {
787787 },
788788
789789 builtin.Os.windows => {
790 const self = try loop.allocator.createOne(Self);
790 const self = try loop.allocator.create(Self);
791791 errdefer loop.allocator.destroy(self);
792792 self.* = Self{
793793 .channel = channel,
......@@ -802,7 +802,7 @@ pub fn Watch(comptime V: type) type {
802802 },
803803
804804 builtin.Os.macosx, builtin.Os.freebsd => {
805 const self = try loop.allocator.createOne(Self);
805 const self = try loop.allocator.create(Self);
806806 errdefer loop.allocator.destroy(self);
807807
808808 self.* = Self{
......@@ -1068,7 +1068,7 @@ pub fn Watch(comptime V: type) type {
10681068 }
10691069 } else {
10701070 errdefer _ = self.os_data.dir_table.remove(dirname);
1071 const dir = try self.channel.loop.allocator.createOne(OsData.Dir);
1071 const dir = try self.channel.loop.allocator.create(OsData.Dir);
10721072 errdefer self.channel.loop.allocator.destroy(dir);
10731073
10741074 dir.* = OsData.Dir{
std/event/group.zig+3-2
......@@ -42,10 +42,11 @@ pub fn Group(comptime ReturnType: type) type {
4242
4343 /// Add a promise to the group. Thread-safe.
4444 pub fn add(self: *Self, handle: promise->ReturnType) (error{OutOfMemory}!void) {
45 const node = try self.lock.loop.allocator.create(Stack.Node{
45 const node = try self.lock.loop.allocator.create(Stack.Node);
46 node.* = Stack.Node{
4647 .next = undefined,
4748 .data = handle,
48 });
49 };
4950 self.alloc_stack.push(node);
5051 }
5152
std/heap.zig+2-1
......@@ -518,7 +518,8 @@ fn testAllocator(allocator: *mem.Allocator) !void {
518518 var slice = try allocator.alloc(*i32, 100);
519519 assert(slice.len == 100);
520520 for (slice) |*item, i| {
521 item.* = try allocator.create(@intCast(i32, i));
521 item.* = try allocator.create(i32);
522 item.*.* = @intCast(i32, i);
522523 }
523524
524525 slice = try allocator.realloc(*i32, slice, 20000);
std/io.zig+3-2
......@@ -944,12 +944,13 @@ pub const BufferedAtomicFile = struct {
944944
945945 pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile {
946946 // TODO with well defined copy elision we don't need this allocation
947 var self = try allocator.create(BufferedAtomicFile{
947 var self = try allocator.create(BufferedAtomicFile);
948 self.* = BufferedAtomicFile{
948949 .atomic_file = undefined,
949950 .file_stream = undefined,
950951 .buffered_stream = undefined,
951952 .allocator = allocator,
952 });
953 };
953954 errdefer allocator.destroy(self);
954955
955956 self.atomic_file = try os.AtomicFile.init(dest_path, os.File.default_mode);
std/linked_list.zig+1-1
......@@ -190,7 +190,7 @@ pub fn LinkedList(comptime T: type) type {
190190 /// Returns:
191191 /// A pointer to the new node.
192192 pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node {
193 return allocator.create(Node(undefined));
193 return allocator.create(Node);
194194 }
195195
196196 /// Deallocate a node.
std/mem.zig+1-12
......@@ -36,20 +36,9 @@ pub const Allocator = struct {
3636 /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
3737 freeFn: fn (self: *Allocator, old_mem: []u8) void,
3838
39 /// Call `destroy` with the result
40 /// TODO this is deprecated. use createOne instead
41 pub fn create(self: *Allocator, init: var) Error!*@typeOf(init) {
42 const T = @typeOf(init);
43 if (@sizeOf(T) == 0) return &(T{});
44 const slice = try self.alloc(T, 1);
45 const ptr = &slice[0];
46 ptr.* = init;
47 return ptr;
48 }
49
5039 /// Call `destroy` with the result.
5140 /// Returns undefined memory.
52 pub fn createOne(self: *Allocator, comptime T: type) Error!*T {
41 pub fn create(self: *Allocator, comptime T: type) Error!*T {
5342 if (@sizeOf(T) == 0) return &(T{});
5443 const slice = try self.alloc(T, 1);
5544 return &slice[0];
std/os/child_process.zig+3-2
......@@ -88,7 +88,8 @@ pub const ChildProcess = struct {
8888 /// First argument in argv is the executable.
8989 /// On success must call deinit.
9090 pub fn init(argv: []const []const u8, allocator: *mem.Allocator) !*ChildProcess {
91 const child = try allocator.create(ChildProcess{
91 const child = try allocator.create(ChildProcess);
92 child.* = ChildProcess{
9293 .allocator = allocator,
9394 .argv = argv,
9495 .pid = undefined,
......@@ -109,7 +110,7 @@ pub const ChildProcess = struct {
109110 .stdin_behavior = StdIo.Inherit,
110111 .stdout_behavior = StdIo.Inherit,
111112 .stderr_behavior = StdIo.Inherit,
112 });
113 };
113114 errdefer allocator.destroy(child);
114115 return child;
115116 }
std/os/index.zig+3-2
......@@ -3047,7 +3047,8 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
30473047 const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) orelse return SpawnThreadError.OutOfMemory;
30483048 errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0);
30493049 const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count];
3050 const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext{
3050 const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable;
3051 outer_context.* = WinThread.OuterContext{
30513052 .thread = Thread{
30523053 .data = Thread.Data{
30533054 .heap_handle = heap_handle,
......@@ -3056,7 +3057,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
30563057 },
30573058 },
30583059 .inner = context,
3059 }) catch unreachable;
3060 };
30603061
30613062 const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(*c_void, &outer_context.inner);
30623063 outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) orelse {
std/zig/parse.zig+226-150
......@@ -17,14 +17,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
1717 defer stack.deinit();
1818
1919 const arena = &tree_arena.allocator;
20 const root_node = try arena.create(ast.Node.Root{
20 const root_node = try arena.create(ast.Node.Root);
21 root_node.* = ast.Node.Root{
2122 .base = ast.Node{ .id = ast.Node.Id.Root },
2223 .decls = ast.Node.Root.DeclList.init(arena),
2324 .doc_comments = null,
2425 .shebang = null,
2526 // initialized when we get the eof token
2627 .eof_token = undefined,
27 });
28 };
2829
2930 var tree = ast.Tree{
3031 .source = source,
......@@ -75,20 +76,22 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
7576 Token.Id.Keyword_test => {
7677 stack.append(State.TopLevel) catch unreachable;
7778
78 const block = try arena.create(ast.Node.Block{
79 const block = try arena.create(ast.Node.Block);
80 block.* = ast.Node.Block{
7981 .base = ast.Node{ .id = ast.Node.Id.Block },
8082 .label = null,
8183 .lbrace = undefined,
8284 .statements = ast.Node.Block.StatementList.init(arena),
8385 .rbrace = undefined,
84 });
85 const test_node = try arena.create(ast.Node.TestDecl{
86 };
87 const test_node = try arena.create(ast.Node.TestDecl);
88 test_node.* = ast.Node.TestDecl{
8689 .base = ast.Node{ .id = ast.Node.Id.TestDecl },
8790 .doc_comments = comments,
8891 .test_token = token_index,
8992 .name = undefined,
9093 .body_node = &block.base,
91 });
94 };
9295 try root_node.decls.push(&test_node.base);
9396 try stack.append(State{ .Block = block });
9497 try stack.append(State{
......@@ -119,19 +122,21 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
119122 continue;
120123 },
121124 Token.Id.Keyword_comptime => {
122 const block = try arena.create(ast.Node.Block{
125 const block = try arena.create(ast.Node.Block);
126 block.* = ast.Node.Block{
123127 .base = ast.Node{ .id = ast.Node.Id.Block },
124128 .label = null,
125129 .lbrace = undefined,
126130 .statements = ast.Node.Block.StatementList.init(arena),
127131 .rbrace = undefined,
128 });
129 const node = try arena.create(ast.Node.Comptime{
132 };
133 const node = try arena.create(ast.Node.Comptime);
134 node.* = ast.Node.Comptime{
130135 .base = ast.Node{ .id = ast.Node.Id.Comptime },
131136 .comptime_token = token_index,
132137 .expr = &block.base,
133138 .doc_comments = comments,
134 });
139 };
135140 try root_node.decls.push(&node.base);
136141
137142 stack.append(State.TopLevel) catch unreachable;
......@@ -235,14 +240,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
235240 return tree;
236241 }
237242
238 const node = try arena.create(ast.Node.Use{
243 const node = try arena.create(ast.Node.Use);
244 node.* = ast.Node.Use{
239245 .base = ast.Node{ .id = ast.Node.Id.Use },
240246 .use_token = token_index,
241247 .visib_token = ctx.visib_token,
242248 .expr = undefined,
243249 .semicolon_token = undefined,
244250 .doc_comments = ctx.comments,
245 });
251 };
246252 try ctx.decls.push(&node.base);
247253
248254 stack.append(State{
......@@ -276,7 +282,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
276282 continue;
277283 },
278284 Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc, Token.Id.Keyword_async => {
279 const fn_proto = try arena.create(ast.Node.FnProto{
285 const fn_proto = try arena.create(ast.Node.FnProto);
286 fn_proto.* = ast.Node.FnProto{
280287 .base = ast.Node{ .id = ast.Node.Id.FnProto },
281288 .doc_comments = ctx.comments,
282289 .visib_token = ctx.visib_token,
......@@ -292,7 +299,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
292299 .lib_name = ctx.lib_name,
293300 .align_expr = null,
294301 .section_expr = null,
295 });
302 };
296303 try ctx.decls.push(&fn_proto.base);
297304 stack.append(State{ .FnDef = fn_proto }) catch unreachable;
298305 try stack.append(State{ .FnProto = fn_proto });
......@@ -309,12 +316,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
309316 continue;
310317 },
311318 Token.Id.Keyword_async => {
312 const async_node = try arena.create(ast.Node.AsyncAttribute{
319 const async_node = try arena.create(ast.Node.AsyncAttribute);
320 async_node.* = ast.Node.AsyncAttribute{
313321 .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
314322 .async_token = token_index,
315323 .allocator_type = null,
316324 .rangle_bracket = null,
317 });
325 };
318326 fn_proto.async_attr = async_node;
319327
320328 try stack.append(State{
......@@ -341,13 +349,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
341349 },
342350 State.TopLevelExternOrField => |ctx| {
343351 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |identifier| {
344 const node = try arena.create(ast.Node.StructField{
352 const node = try arena.create(ast.Node.StructField);
353 node.* = ast.Node.StructField{
345354 .base = ast.Node{ .id = ast.Node.Id.StructField },
346355 .doc_comments = ctx.comments,
347356 .visib_token = ctx.visib_token,
348357 .name_token = identifier,
349358 .type_expr = undefined,
350 });
359 };
351360 const node_ptr = try ctx.container_decl.fields_and_decls.addOne();
352361 node_ptr.* = &node.base;
353362
......@@ -391,7 +400,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
391400 const token = nextToken(&tok_it, &tree);
392401 const token_index = token.index;
393402 const token_ptr = token.ptr;
394 const node = try arena.create(ast.Node.ContainerDecl{
403 const node = try arena.create(ast.Node.ContainerDecl);
404 node.* = ast.Node.ContainerDecl{
395405 .base = ast.Node{ .id = ast.Node.Id.ContainerDecl },
396406 .layout_token = ctx.layout_token,
397407 .kind_token = switch (token_ptr.id) {
......@@ -405,7 +415,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
405415 .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(arena),
406416 .lbrace_token = undefined,
407417 .rbrace_token = undefined,
408 });
418 };
409419 ctx.opt_ctx.store(&node.base);
410420
411421 stack.append(State{ .ContainerDecl = node }) catch unreachable;
......@@ -464,13 +474,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
464474 Token.Id.Identifier => {
465475 switch (tree.tokens.at(container_decl.kind_token).id) {
466476 Token.Id.Keyword_struct => {
467 const node = try arena.create(ast.Node.StructField{
477 const node = try arena.create(ast.Node.StructField);
478 node.* = ast.Node.StructField{
468479 .base = ast.Node{ .id = ast.Node.Id.StructField },
469480 .doc_comments = comments,
470481 .visib_token = null,
471482 .name_token = token_index,
472483 .type_expr = undefined,
473 });
484 };
474485 const node_ptr = try container_decl.fields_and_decls.addOne();
475486 node_ptr.* = &node.base;
476487
......@@ -485,13 +496,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
485496 continue;
486497 },
487498 Token.Id.Keyword_union => {
488 const node = try arena.create(ast.Node.UnionTag{
499 const node = try arena.create(ast.Node.UnionTag);
500 node.* = ast.Node.UnionTag{
489501 .base = ast.Node{ .id = ast.Node.Id.UnionTag },
490502 .name_token = token_index,
491503 .type_expr = null,
492504 .value_expr = null,
493505 .doc_comments = comments,
494 });
506 };
495507 try container_decl.fields_and_decls.push(&node.base);
496508
497509 try stack.append(State{
......@@ -506,12 +518,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
506518 continue;
507519 },
508520 Token.Id.Keyword_enum => {
509 const node = try arena.create(ast.Node.EnumTag{
521 const node = try arena.create(ast.Node.EnumTag);
522 node.* = ast.Node.EnumTag{
510523 .base = ast.Node{ .id = ast.Node.Id.EnumTag },
511524 .name_token = token_index,
512525 .value = null,
513526 .doc_comments = comments,
514 });
527 };
515528 try container_decl.fields_and_decls.push(&node.base);
516529
517530 try stack.append(State{
......@@ -593,7 +606,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
593606 },
594607
595608 State.VarDecl => |ctx| {
596 const var_decl = try arena.create(ast.Node.VarDecl{
609 const var_decl = try arena.create(ast.Node.VarDecl);
610 var_decl.* = ast.Node.VarDecl{
597611 .base = ast.Node{ .id = ast.Node.Id.VarDecl },
598612 .doc_comments = ctx.comments,
599613 .visib_token = ctx.visib_token,
......@@ -609,7 +623,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
609623 .name_token = undefined,
610624 .eq_token = undefined,
611625 .semicolon_token = undefined,
612 });
626 };
613627 try ctx.list.push(&var_decl.base);
614628
615629 try stack.append(State{ .VarDeclAlign = var_decl });
......@@ -708,13 +722,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
708722 const token_ptr = token.ptr;
709723 switch (token_ptr.id) {
710724 Token.Id.LBrace => {
711 const block = try arena.create(ast.Node.Block{
725 const block = try arena.create(ast.Node.Block);
726 block.* = ast.Node.Block{
712727 .base = ast.Node{ .id = ast.Node.Id.Block },
713728 .label = null,
714729 .lbrace = token_index,
715730 .statements = ast.Node.Block.StatementList.init(arena),
716731 .rbrace = undefined,
717 });
732 };
718733 fn_proto.body_node = &block.base;
719734 stack.append(State{ .Block = block }) catch unreachable;
720735 continue;
......@@ -770,10 +785,11 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
770785 // TODO: this is a special case. Remove this when #760 is fixed
771786 if (token_ptr.id == Token.Id.Keyword_anyerror) {
772787 if (tok_it.peek().?.id == Token.Id.LBrace) {
773 const error_type_node = try arena.create(ast.Node.ErrorType{
788 const error_type_node = try arena.create(ast.Node.ErrorType);
789 error_type_node.* = ast.Node.ErrorType{
774790 .base = ast.Node{ .id = ast.Node.Id.ErrorType },
775791 .token = token_index,
776 });
792 };
777793 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .Explicit = &error_type_node.base };
778794 continue;
779795 }
......@@ -791,14 +807,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
791807 if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| {
792808 continue;
793809 }
794 const param_decl = try arena.create(ast.Node.ParamDecl{
810 const param_decl = try arena.create(ast.Node.ParamDecl);
811 param_decl.* = ast.Node.ParamDecl{
795812 .base = ast.Node{ .id = ast.Node.Id.ParamDecl },
796813 .comptime_token = null,
797814 .noalias_token = null,
798815 .name_token = null,
799816 .type_node = undefined,
800817 .var_args_token = null,
801 });
818 };
802819 try fn_proto.params.push(&param_decl.base);
803820
804821 stack.append(State{
......@@ -877,13 +894,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
877894 const token_ptr = token.ptr;
878895 switch (token_ptr.id) {
879896 Token.Id.LBrace => {
880 const block = try arena.create(ast.Node.Block{
897 const block = try arena.create(ast.Node.Block);
898 block.* = ast.Node.Block{
881899 .base = ast.Node{ .id = ast.Node.Id.Block },
882900 .label = ctx.label,
883901 .lbrace = token_index,
884902 .statements = ast.Node.Block.StatementList.init(arena),
885903 .rbrace = undefined,
886 });
904 };
887905 ctx.opt_ctx.store(&block.base);
888906 stack.append(State{ .Block = block }) catch unreachable;
889907 continue;
......@@ -970,7 +988,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
970988 }
971989 },
972990 State.While => |ctx| {
973 const node = try arena.create(ast.Node.While{
991 const node = try arena.create(ast.Node.While);
992 node.* = ast.Node.While{
974993 .base = ast.Node{ .id = ast.Node.Id.While },
975994 .label = ctx.label,
976995 .inline_token = ctx.inline_token,
......@@ -980,7 +999,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
980999 .continue_expr = null,
9811000 .body = undefined,
9821001 .@"else" = null,
983 });
1002 };
9841003 ctx.opt_ctx.store(&node.base);
9851004 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
9861005 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
......@@ -999,7 +1018,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
9991018 continue;
10001019 },
10011020 State.For => |ctx| {
1002 const node = try arena.create(ast.Node.For{
1021 const node = try arena.create(ast.Node.For);
1022 node.* = ast.Node.For{
10031023 .base = ast.Node{ .id = ast.Node.Id.For },
10041024 .label = ctx.label,
10051025 .inline_token = ctx.inline_token,
......@@ -1008,7 +1028,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
10081028 .payload = null,
10091029 .body = undefined,
10101030 .@"else" = null,
1011 });
1031 };
10121032 ctx.opt_ctx.store(&node.base);
10131033 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
10141034 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
......@@ -1020,12 +1040,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
10201040 },
10211041 State.Else => |dest| {
10221042 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
1023 const node = try arena.create(ast.Node.Else{
1043 const node = try arena.create(ast.Node.Else);
1044 node.* = ast.Node.Else{
10241045 .base = ast.Node{ .id = ast.Node.Id.Else },
10251046 .else_token = else_token,
10261047 .payload = null,
10271048 .body = undefined,
1028 });
1049 };
10291050 dest.* = node;
10301051
10311052 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } }) catch unreachable;
......@@ -1083,11 +1104,12 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
10831104 continue;
10841105 },
10851106 Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => {
1086 const node = try arena.create(ast.Node.Defer{
1107 const node = try arena.create(ast.Node.Defer);
1108 node.* = ast.Node.Defer{
10871109 .base = ast.Node{ .id = ast.Node.Id.Defer },
10881110 .defer_token = token_index,
10891111 .expr = undefined,
1090 });
1112 };
10911113 const node_ptr = try block.statements.addOne();
10921114 node_ptr.* = &node.base;
10931115
......@@ -1096,13 +1118,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
10961118 continue;
10971119 },
10981120 Token.Id.LBrace => {
1099 const inner_block = try arena.create(ast.Node.Block{
1121 const inner_block = try arena.create(ast.Node.Block);
1122 inner_block.* = ast.Node.Block{
11001123 .base = ast.Node{ .id = ast.Node.Id.Block },
11011124 .label = null,
11021125 .lbrace = token_index,
11031126 .statements = ast.Node.Block.StatementList.init(arena),
11041127 .rbrace = undefined,
1105 });
1128 };
11061129 try block.statements.push(&inner_block.base);
11071130
11081131 stack.append(State{ .Block = inner_block }) catch unreachable;
......@@ -1164,14 +1187,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
11641187 continue;
11651188 }
11661189
1167 const node = try arena.create(ast.Node.AsmOutput{
1190 const node = try arena.create(ast.Node.AsmOutput);
1191 node.* = ast.Node.AsmOutput{
11681192 .base = ast.Node{ .id = ast.Node.Id.AsmOutput },
11691193 .lbracket = lbracket_index,
11701194 .symbolic_name = undefined,
11711195 .constraint = undefined,
11721196 .kind = undefined,
11731197 .rparen = undefined,
1174 });
1198 };
11751199 try items.push(node);
11761200
11771201 stack.append(State{ .AsmOutputItems = items }) catch unreachable;
......@@ -1218,14 +1242,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
12181242 continue;
12191243 }
12201244
1221 const node = try arena.create(ast.Node.AsmInput{
1245 const node = try arena.create(ast.Node.AsmInput);
1246 node.* = ast.Node.AsmInput{
12221247 .base = ast.Node{ .id = ast.Node.Id.AsmInput },
12231248 .lbracket = lbracket_index,
12241249 .symbolic_name = undefined,
12251250 .constraint = undefined,
12261251 .expr = undefined,
12271252 .rparen = undefined,
1228 });
1253 };
12291254 try items.push(node);
12301255
12311256 stack.append(State{ .AsmInputItems = items }) catch unreachable;
......@@ -1283,12 +1308,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
12831308 continue;
12841309 }
12851310
1286 const node = try arena.create(ast.Node.FieldInitializer{
1311 const node = try arena.create(ast.Node.FieldInitializer);
1312 node.* = ast.Node.FieldInitializer{
12871313 .base = ast.Node{ .id = ast.Node.Id.FieldInitializer },
12881314 .period_token = undefined,
12891315 .name_token = undefined,
12901316 .expr = undefined,
1291 });
1317 };
12921318 try list_state.list.push(&node.base);
12931319
12941320 stack.append(State{ .FieldInitListCommaOrEnd = list_state }) catch unreachable;
......@@ -1390,13 +1416,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
13901416 }
13911417
13921418 const comments = try eatDocComments(arena, &tok_it, &tree);
1393 const node = try arena.create(ast.Node.SwitchCase{
1419 const node = try arena.create(ast.Node.SwitchCase);
1420 node.* = ast.Node.SwitchCase{
13941421 .base = ast.Node{ .id = ast.Node.Id.SwitchCase },
13951422 .items = ast.Node.SwitchCase.ItemList.init(arena),
13961423 .payload = null,
13971424 .expr = undefined,
13981425 .arrow_token = undefined,
1399 });
1426 };
14001427 try list_state.list.push(&node.base);
14011428 try stack.append(State{ .SwitchCaseCommaOrEnd = list_state });
14021429 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });
......@@ -1427,10 +1454,11 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
14271454 const token_index = token.index;
14281455 const token_ptr = token.ptr;
14291456 if (token_ptr.id == Token.Id.Keyword_else) {
1430 const else_node = try arena.create(ast.Node.SwitchElse{
1457 const else_node = try arena.create(ast.Node.SwitchElse);
1458 else_node.* = ast.Node.SwitchElse{
14311459 .base = ast.Node{ .id = ast.Node.Id.SwitchElse },
14321460 .token = token_index,
1433 });
1461 };
14341462 try switch_case.items.push(&else_node.base);
14351463
14361464 try stack.append(State{
......@@ -1537,7 +1565,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
15371565
15381566 State.ExternType => |ctx| {
15391567 if (eatToken(&tok_it, &tree, Token.Id.Keyword_fn)) |fn_token| {
1540 const fn_proto = try arena.create(ast.Node.FnProto{
1568 const fn_proto = try arena.create(ast.Node.FnProto);
1569 fn_proto.* = ast.Node.FnProto{
15411570 .base = ast.Node{ .id = ast.Node.Id.FnProto },
15421571 .doc_comments = ctx.comments,
15431572 .visib_token = null,
......@@ -1553,7 +1582,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
15531582 .lib_name = null,
15541583 .align_expr = null,
15551584 .section_expr = null,
1556 });
1585 };
15571586 ctx.opt_ctx.store(&fn_proto.base);
15581587 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
15591588 continue;
......@@ -1711,12 +1740,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
17111740 continue;
17121741 }
17131742
1714 const node = try arena.create(ast.Node.Payload{
1743 const node = try arena.create(ast.Node.Payload);
1744 node.* = ast.Node.Payload{
17151745 .base = ast.Node{ .id = ast.Node.Id.Payload },
17161746 .lpipe = token_index,
17171747 .error_symbol = undefined,
17181748 .rpipe = undefined,
1719 });
1749 };
17201750 opt_ctx.store(&node.base);
17211751
17221752 stack.append(State{
......@@ -1747,13 +1777,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
17471777 continue;
17481778 }
17491779
1750 const node = try arena.create(ast.Node.PointerPayload{
1780 const node = try arena.create(ast.Node.PointerPayload);
1781 node.* = ast.Node.PointerPayload{
17511782 .base = ast.Node{ .id = ast.Node.Id.PointerPayload },
17521783 .lpipe = token_index,
17531784 .ptr_token = null,
17541785 .value_symbol = undefined,
17551786 .rpipe = undefined,
1756 });
1787 };
17571788 opt_ctx.store(&node.base);
17581789
17591790 try stack.append(State{
......@@ -1790,14 +1821,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
17901821 continue;
17911822 }
17921823
1793 const node = try arena.create(ast.Node.PointerIndexPayload{
1824 const node = try arena.create(ast.Node.PointerIndexPayload);
1825 node.* = ast.Node.PointerIndexPayload{
17941826 .base = ast.Node{ .id = ast.Node.Id.PointerIndexPayload },
17951827 .lpipe = token_index,
17961828 .ptr_token = null,
17971829 .value_symbol = undefined,
17981830 .index_symbol = null,
17991831 .rpipe = undefined,
1800 });
1832 };
18011833 opt_ctx.store(&node.base);
18021834
18031835 stack.append(State{
......@@ -1824,12 +1856,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
18241856 const token_ptr = token.ptr;
18251857 switch (token_ptr.id) {
18261858 Token.Id.Keyword_return, Token.Id.Keyword_break, Token.Id.Keyword_continue => {
1827 const node = try arena.create(ast.Node.ControlFlowExpression{
1859 const node = try arena.create(ast.Node.ControlFlowExpression);
1860 node.* = ast.Node.ControlFlowExpression{
18281861 .base = ast.Node{ .id = ast.Node.Id.ControlFlowExpression },
18291862 .ltoken = token_index,
18301863 .kind = undefined,
18311864 .rhs = null,
1832 });
1865 };
18331866 opt_ctx.store(&node.base);
18341867
18351868 stack.append(State{ .Expression = OptionalCtx{ .Optional = &node.rhs } }) catch unreachable;
......@@ -1853,7 +1886,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
18531886 continue;
18541887 },
18551888 Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => {
1856 const node = try arena.create(ast.Node.PrefixOp{
1889 const node = try arena.create(ast.Node.PrefixOp);
1890 node.* = ast.Node.PrefixOp{
18571891 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
18581892 .op_token = token_index,
18591893 .op = switch (token_ptr.id) {
......@@ -1863,7 +1897,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
18631897 else => unreachable,
18641898 },
18651899 .rhs = undefined,
1866 });
1900 };
18671901 opt_ctx.store(&node.base);
18681902
18691903 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
......@@ -1887,13 +1921,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
18871921 const lhs = opt_ctx.get() orelse continue;
18881922
18891923 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
1890 const node = try arena.create(ast.Node.InfixOp{
1924 const node = try arena.create(ast.Node.InfixOp);
1925 node.* = ast.Node.InfixOp{
18911926 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
18921927 .lhs = lhs,
18931928 .op_token = ellipsis3,
18941929 .op = ast.Node.InfixOp.Op.Range,
18951930 .rhs = undefined,
1896 });
1931 };
18971932 opt_ctx.store(&node.base);
18981933 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
18991934 continue;
......@@ -1912,13 +1947,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
19121947 const token_index = token.index;
19131948 const token_ptr = token.ptr;
19141949 if (tokenIdToAssignment(token_ptr.id)) |ass_id| {
1915 const node = try arena.create(ast.Node.InfixOp{
1950 const node = try arena.create(ast.Node.InfixOp);
1951 node.* = ast.Node.InfixOp{
19161952 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19171953 .lhs = lhs,
19181954 .op_token = token_index,
19191955 .op = ass_id,
19201956 .rhs = undefined,
1921 });
1957 };
19221958 opt_ctx.store(&node.base);
19231959 stack.append(State{ .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
19241960 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } });
......@@ -1942,13 +1978,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
19421978 const token_index = token.index;
19431979 const token_ptr = token.ptr;
19441980 if (tokenIdToUnwrapExpr(token_ptr.id)) |unwrap_id| {
1945 const node = try arena.create(ast.Node.InfixOp{
1981 const node = try arena.create(ast.Node.InfixOp);
1982 node.* = ast.Node.InfixOp{
19461983 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19471984 .lhs = lhs,
19481985 .op_token = token_index,
19491986 .op = unwrap_id,
19501987 .rhs = undefined,
1951 });
1988 };
19521989 opt_ctx.store(&node.base);
19531990
19541991 stack.append(State{ .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
......@@ -1974,13 +2011,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
19742011 const lhs = opt_ctx.get() orelse continue;
19752012
19762013 if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| {
1977 const node = try arena.create(ast.Node.InfixOp{
2014 const node = try arena.create(ast.Node.InfixOp);
2015 node.* = ast.Node.InfixOp{
19782016 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19792017 .lhs = lhs,
19802018 .op_token = or_token,
19812019 .op = ast.Node.InfixOp.Op.BoolOr,
19822020 .rhs = undefined,
1983 });
2021 };
19842022 opt_ctx.store(&node.base);
19852023 stack.append(State{ .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
19862024 try stack.append(State{ .BoolAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -1998,13 +2036,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
19982036 const lhs = opt_ctx.get() orelse continue;
19992037
20002038 if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| {
2001 const node = try arena.create(ast.Node.InfixOp{
2039 const node = try arena.create(ast.Node.InfixOp);
2040 node.* = ast.Node.InfixOp{
20022041 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20032042 .lhs = lhs,
20042043 .op_token = and_token,
20052044 .op = ast.Node.InfixOp.Op.BoolAnd,
20062045 .rhs = undefined,
2007 });
2046 };
20082047 opt_ctx.store(&node.base);
20092048 stack.append(State{ .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
20102049 try stack.append(State{ .ComparisonExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2025,13 +2064,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
20252064 const token_index = token.index;
20262065 const token_ptr = token.ptr;
20272066 if (tokenIdToComparison(token_ptr.id)) |comp_id| {
2028 const node = try arena.create(ast.Node.InfixOp{
2067 const node = try arena.create(ast.Node.InfixOp);
2068 node.* = ast.Node.InfixOp{
20292069 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20302070 .lhs = lhs,
20312071 .op_token = token_index,
20322072 .op = comp_id,
20332073 .rhs = undefined,
2034 });
2074 };
20352075 opt_ctx.store(&node.base);
20362076 stack.append(State{ .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
20372077 try stack.append(State{ .BinaryOrExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2052,13 +2092,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
20522092 const lhs = opt_ctx.get() orelse continue;
20532093
20542094 if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| {
2055 const node = try arena.create(ast.Node.InfixOp{
2095 const node = try arena.create(ast.Node.InfixOp);
2096 node.* = ast.Node.InfixOp{
20562097 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20572098 .lhs = lhs,
20582099 .op_token = pipe,
20592100 .op = ast.Node.InfixOp.Op.BitOr,
20602101 .rhs = undefined,
2061 });
2102 };
20622103 opt_ctx.store(&node.base);
20632104 stack.append(State{ .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
20642105 try stack.append(State{ .BinaryXorExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2076,13 +2117,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
20762117 const lhs = opt_ctx.get() orelse continue;
20772118
20782119 if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| {
2079 const node = try arena.create(ast.Node.InfixOp{
2120 const node = try arena.create(ast.Node.InfixOp);
2121 node.* = ast.Node.InfixOp{
20802122 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20812123 .lhs = lhs,
20822124 .op_token = caret,
20832125 .op = ast.Node.InfixOp.Op.BitXor,
20842126 .rhs = undefined,
2085 });
2127 };
20862128 opt_ctx.store(&node.base);
20872129 stack.append(State{ .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
20882130 try stack.append(State{ .BinaryAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2100,13 +2142,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
21002142 const lhs = opt_ctx.get() orelse continue;
21012143
21022144 if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| {
2103 const node = try arena.create(ast.Node.InfixOp{
2145 const node = try arena.create(ast.Node.InfixOp);
2146 node.* = ast.Node.InfixOp{
21042147 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21052148 .lhs = lhs,
21062149 .op_token = ampersand,
21072150 .op = ast.Node.InfixOp.Op.BitAnd,
21082151 .rhs = undefined,
2109 });
2152 };
21102153 opt_ctx.store(&node.base);
21112154 stack.append(State{ .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
21122155 try stack.append(State{ .BitShiftExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2127,13 +2170,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
21272170 const token_index = token.index;
21282171 const token_ptr = token.ptr;
21292172 if (tokenIdToBitShift(token_ptr.id)) |bitshift_id| {
2130 const node = try arena.create(ast.Node.InfixOp{
2173 const node = try arena.create(ast.Node.InfixOp);
2174 node.* = ast.Node.InfixOp{
21312175 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21322176 .lhs = lhs,
21332177 .op_token = token_index,
21342178 .op = bitshift_id,
21352179 .rhs = undefined,
2136 });
2180 };
21372181 opt_ctx.store(&node.base);
21382182 stack.append(State{ .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
21392183 try stack.append(State{ .AdditionExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2157,13 +2201,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
21572201 const token_index = token.index;
21582202 const token_ptr = token.ptr;
21592203 if (tokenIdToAddition(token_ptr.id)) |add_id| {
2160 const node = try arena.create(ast.Node.InfixOp{
2204 const node = try arena.create(ast.Node.InfixOp);
2205 node.* = ast.Node.InfixOp{
21612206 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21622207 .lhs = lhs,
21632208 .op_token = token_index,
21642209 .op = add_id,
21652210 .rhs = undefined,
2166 });
2211 };
21672212 opt_ctx.store(&node.base);
21682213 stack.append(State{ .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
21692214 try stack.append(State{ .MultiplyExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2187,13 +2232,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
21872232 const token_index = token.index;
21882233 const token_ptr = token.ptr;
21892234 if (tokenIdToMultiply(token_ptr.id)) |mult_id| {
2190 const node = try arena.create(ast.Node.InfixOp{
2235 const node = try arena.create(ast.Node.InfixOp);
2236 node.* = ast.Node.InfixOp{
21912237 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21922238 .lhs = lhs,
21932239 .op_token = token_index,
21942240 .op = mult_id,
21952241 .rhs = undefined,
2196 });
2242 };
21972243 opt_ctx.store(&node.base);
21982244 stack.append(State{ .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
21992245 try stack.append(State{ .CurlySuffixExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
......@@ -2215,12 +2261,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22152261 const lhs = opt_ctx.get() orelse continue;
22162262
22172263 if (tok_it.peek().?.id == Token.Id.Period) {
2218 const node = try arena.create(ast.Node.SuffixOp{
2264 const node = try arena.create(ast.Node.SuffixOp);
2265 node.* = ast.Node.SuffixOp{
22192266 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
22202267 .lhs = lhs,
22212268 .op = ast.Node.SuffixOp.Op{ .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
22222269 .rtoken = undefined,
2223 });
2270 };
22242271 opt_ctx.store(&node.base);
22252272
22262273 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
......@@ -2234,12 +2281,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22342281 continue;
22352282 }
22362283
2237 const node = try arena.create(ast.Node.SuffixOp{
2284 const node = try arena.create(ast.Node.SuffixOp);
2285 node.* = ast.Node.SuffixOp{
22382286 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
22392287 .lhs = lhs,
22402288 .op = ast.Node.SuffixOp.Op{ .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
22412289 .rtoken = undefined,
2242 });
2290 };
22432291 opt_ctx.store(&node.base);
22442292 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
22452293 try stack.append(State{ .IfToken = Token.Id.LBrace });
......@@ -2263,13 +2311,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22632311 const lhs = opt_ctx.get() orelse continue;
22642312
22652313 if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| {
2266 const node = try arena.create(ast.Node.InfixOp{
2314 const node = try arena.create(ast.Node.InfixOp);
2315 node.* = ast.Node.InfixOp{
22672316 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
22682317 .lhs = lhs,
22692318 .op_token = bang,
22702319 .op = ast.Node.InfixOp.Op.ErrorUnion,
22712320 .rhs = undefined,
2272 });
2321 };
22732322 opt_ctx.store(&node.base);
22742323 stack.append(State{ .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable;
22752324 try stack.append(State{ .PrefixOpExpression = OptionalCtx{ .Required = &node.rhs } });
......@@ -2282,22 +2331,24 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22822331 const token_index = token.index;
22832332 const token_ptr = token.ptr;
22842333 if (tokenIdToPrefixOp(token_ptr.id)) |prefix_id| {
2285 var node = try arena.create(ast.Node.PrefixOp{
2334 var node = try arena.create(ast.Node.PrefixOp);
2335 node.* = ast.Node.PrefixOp{
22862336 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
22872337 .op_token = token_index,
22882338 .op = prefix_id,
22892339 .rhs = undefined,
2290 });
2340 };
22912341 opt_ctx.store(&node.base);
22922342
22932343 // Treat '**' token as two pointer types
22942344 if (token_ptr.id == Token.Id.AsteriskAsterisk) {
2295 const child = try arena.create(ast.Node.PrefixOp{
2345 const child = try arena.create(ast.Node.PrefixOp);
2346 child.* = ast.Node.PrefixOp{
22962347 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
22972348 .op_token = token_index,
22982349 .op = prefix_id,
22992350 .rhs = undefined,
2300 });
2351 };
23012352 node.rhs = &child.base;
23022353 node = child;
23032354 }
......@@ -2316,12 +2367,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
23162367
23172368 State.SuffixOpExpressionBegin => |opt_ctx| {
23182369 if (eatToken(&tok_it, &tree, Token.Id.Keyword_async)) |async_token| {
2319 const async_node = try arena.create(ast.Node.AsyncAttribute{
2370 const async_node = try arena.create(ast.Node.AsyncAttribute);
2371 async_node.* = ast.Node.AsyncAttribute{
23202372 .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
23212373 .async_token = async_token,
23222374 .allocator_type = null,
23232375 .rangle_bracket = null,
2324 });
2376 };
23252377 stack.append(State{
23262378 .AsyncEnd = AsyncEndCtx{
23272379 .ctx = opt_ctx,
......@@ -2347,7 +2399,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
23472399 const token_ptr = token.ptr;
23482400 switch (token_ptr.id) {
23492401 Token.Id.LParen => {
2350 const node = try arena.create(ast.Node.SuffixOp{
2402 const node = try arena.create(ast.Node.SuffixOp);
2403 node.* = ast.Node.SuffixOp{
23512404 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
23522405 .lhs = lhs,
23532406 .op = ast.Node.SuffixOp.Op{
......@@ -2357,7 +2410,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
23572410 },
23582411 },
23592412 .rtoken = undefined,
2360 });
2413 };
23612414 opt_ctx.store(&node.base);
23622415
23632416 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
......@@ -2371,12 +2424,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
23712424 continue;
23722425 },
23732426 Token.Id.LBracket => {
2374 const node = try arena.create(ast.Node.SuffixOp{
2427 const node = try arena.create(ast.Node.SuffixOp);
2428 node.* = ast.Node.SuffixOp{
23752429 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
23762430 .lhs = lhs,
23772431 .op = ast.Node.SuffixOp.Op{ .ArrayAccess = undefined },
23782432 .rtoken = undefined,
2379 });
2433 };
23802434 opt_ctx.store(&node.base);
23812435
23822436 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
......@@ -2386,34 +2440,37 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
23862440 },
23872441 Token.Id.Period => {
23882442 if (eatToken(&tok_it, &tree, Token.Id.Asterisk)) |asterisk_token| {
2389 const node = try arena.create(ast.Node.SuffixOp{
2443 const node = try arena.create(ast.Node.SuffixOp);
2444 node.* = ast.Node.SuffixOp{
23902445 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
23912446 .lhs = lhs,
23922447 .op = ast.Node.SuffixOp.Op.Deref,
23932448 .rtoken = asterisk_token,
2394 });
2449 };
23952450 opt_ctx.store(&node.base);
23962451 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
23972452 continue;
23982453 }
23992454 if (eatToken(&tok_it, &tree, Token.Id.QuestionMark)) |question_token| {
2400 const node = try arena.create(ast.Node.SuffixOp{
2455 const node = try arena.create(ast.Node.SuffixOp);
2456 node.* = ast.Node.SuffixOp{
24012457 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
24022458 .lhs = lhs,
24032459 .op = ast.Node.SuffixOp.Op.UnwrapOptional,
24042460 .rtoken = question_token,
2405 });
2461 };
24062462 opt_ctx.store(&node.base);
24072463 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
24082464 continue;
24092465 }
2410 const node = try arena.create(ast.Node.InfixOp{
2466 const node = try arena.create(ast.Node.InfixOp);
2467 node.* = ast.Node.InfixOp{
24112468 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
24122469 .lhs = lhs,
24132470 .op_token = token_index,
24142471 .op = ast.Node.InfixOp.Op.Period,
24152472 .rhs = undefined,
2416 });
2473 };
24172474 opt_ctx.store(&node.base);
24182475
24192476 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
......@@ -2467,11 +2524,12 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
24672524 continue;
24682525 },
24692526 Token.Id.Keyword_promise => {
2470 const node = try arena.create(ast.Node.PromiseType{
2527 const node = try arena.create(ast.Node.PromiseType);
2528 node.* = ast.Node.PromiseType{
24712529 .base = ast.Node{ .id = ast.Node.Id.PromiseType },
24722530 .promise_token = token.index,
24732531 .result = null,
2474 });
2532 };
24752533 opt_ctx.store(&node.base);
24762534 const next_token = nextToken(&tok_it, &tree);
24772535 const next_token_index = next_token.index;
......@@ -2493,12 +2551,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
24932551 continue;
24942552 },
24952553 Token.Id.LParen => {
2496 const node = try arena.create(ast.Node.GroupedExpression{
2554 const node = try arena.create(ast.Node.GroupedExpression);
2555 node.* = ast.Node.GroupedExpression{
24972556 .base = ast.Node{ .id = ast.Node.Id.GroupedExpression },
24982557 .lparen = token.index,
24992558 .expr = undefined,
25002559 .rparen = undefined,
2501 });
2560 };
25022561 opt_ctx.store(&node.base);
25032562
25042563 stack.append(State{
......@@ -2511,12 +2570,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
25112570 continue;
25122571 },
25132572 Token.Id.Builtin => {
2514 const node = try arena.create(ast.Node.BuiltinCall{
2573 const node = try arena.create(ast.Node.BuiltinCall);
2574 node.* = ast.Node.BuiltinCall{
25152575 .base = ast.Node{ .id = ast.Node.Id.BuiltinCall },
25162576 .builtin_token = token.index,
25172577 .params = ast.Node.BuiltinCall.ParamList.init(arena),
25182578 .rparen_token = undefined,
2519 });
2579 };
25202580 opt_ctx.store(&node.base);
25212581
25222582 stack.append(State{
......@@ -2530,12 +2590,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
25302590 continue;
25312591 },
25322592 Token.Id.LBracket => {
2533 const node = try arena.create(ast.Node.PrefixOp{
2593 const node = try arena.create(ast.Node.PrefixOp);
2594 node.* = ast.Node.PrefixOp{
25342595 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
25352596 .op_token = token.index,
25362597 .op = undefined,
25372598 .rhs = undefined,
2538 });
2599 };
25392600 opt_ctx.store(&node.base);
25402601
25412602 stack.append(State{ .SliceOrArrayType = node }) catch unreachable;
......@@ -2593,7 +2654,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
25932654 continue;
25942655 },
25952656 Token.Id.Keyword_fn => {
2596 const fn_proto = try arena.create(ast.Node.FnProto{
2657 const fn_proto = try arena.create(ast.Node.FnProto);
2658 fn_proto.* = ast.Node.FnProto{
25972659 .base = ast.Node{ .id = ast.Node.Id.FnProto },
25982660 .doc_comments = null,
25992661 .visib_token = null,
......@@ -2609,13 +2671,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
26092671 .lib_name = null,
26102672 .align_expr = null,
26112673 .section_expr = null,
2612 });
2674 };
26132675 opt_ctx.store(&fn_proto.base);
26142676 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
26152677 continue;
26162678 },
26172679 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {
2618 const fn_proto = try arena.create(ast.Node.FnProto{
2680 const fn_proto = try arena.create(ast.Node.FnProto);
2681 fn_proto.* = ast.Node.FnProto{
26192682 .base = ast.Node{ .id = ast.Node.Id.FnProto },
26202683 .doc_comments = null,
26212684 .visib_token = null,
......@@ -2631,7 +2694,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
26312694 .lib_name = null,
26322695 .align_expr = null,
26332696 .section_expr = null,
2634 });
2697 };
26352698 opt_ctx.store(&fn_proto.base);
26362699 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
26372700 try stack.append(State{
......@@ -2643,7 +2706,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
26432706 continue;
26442707 },
26452708 Token.Id.Keyword_asm => {
2646 const node = try arena.create(ast.Node.Asm{
2709 const node = try arena.create(ast.Node.Asm);
2710 node.* = ast.Node.Asm{
26472711 .base = ast.Node{ .id = ast.Node.Id.Asm },
26482712 .asm_token = token.index,
26492713 .volatile_token = null,
......@@ -2652,7 +2716,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
26522716 .inputs = ast.Node.Asm.InputList.init(arena),
26532717 .clobbers = ast.Node.Asm.ClobberList.init(arena),
26542718 .rparen = undefined,
2655 });
2719 };
26562720 opt_ctx.store(&node.base);
26572721
26582722 stack.append(State{
......@@ -2701,13 +2765,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
27012765
27022766 State.ErrorTypeOrSetDecl => |ctx| {
27032767 if (eatToken(&tok_it, &tree, Token.Id.LBrace) == null) {
2704 const node = try arena.create(ast.Node.InfixOp{
2768 const node = try arena.create(ast.Node.InfixOp);
2769 node.* = ast.Node.InfixOp{
27052770 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
27062771 .lhs = &(try createLiteral(arena, ast.Node.ErrorType, ctx.error_token)).base,
27072772 .op_token = undefined,
27082773 .op = ast.Node.InfixOp.Op.Period,
27092774 .rhs = undefined,
2710 });
2775 };
27112776 ctx.opt_ctx.store(&node.base);
27122777 stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
27132778 try stack.append(State{
......@@ -2719,12 +2784,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
27192784 continue;
27202785 }
27212786
2722 const node = try arena.create(ast.Node.ErrorSetDecl{
2787 const node = try arena.create(ast.Node.ErrorSetDecl);
2788 node.* = ast.Node.ErrorSetDecl{
27232789 .base = ast.Node{ .id = ast.Node.Id.ErrorSetDecl },
27242790 .error_token = ctx.error_token,
27252791 .decls = ast.Node.ErrorSetDecl.DeclList.init(arena),
27262792 .rbrace_token = undefined,
2727 });
2793 };
27282794 ctx.opt_ctx.store(&node.base);
27292795
27302796 stack.append(State{
......@@ -2785,11 +2851,12 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
27852851 return tree;
27862852 }
27872853
2788 const node = try arena.create(ast.Node.ErrorTag{
2854 const node = try arena.create(ast.Node.ErrorTag);
2855 node.* = ast.Node.ErrorTag{
27892856 .base = ast.Node{ .id = ast.Node.Id.ErrorTag },
27902857 .doc_comments = comments,
27912858 .name_token = ident_token_index,
2792 });
2859 };
27932860 node_ptr.* = &node.base;
27942861 continue;
27952862 },
......@@ -3129,10 +3196,11 @@ fn pushDocComment(arena: *mem.Allocator, line_comment: TokenIndex, result: *?*as
31293196 if (result.*) |comment_node| {
31303197 break :blk comment_node;
31313198 } else {
3132 const comment_node = try arena.create(ast.Node.DocComment{
3199 const comment_node = try arena.create(ast.Node.DocComment);
3200 comment_node.* = ast.Node.DocComment{
31333201 .base = ast.Node{ .id = ast.Node.Id.DocComment },
31343202 .lines = ast.Node.DocComment.LineList.init(arena),
3135 });
3203 };
31363204 result.* = comment_node;
31373205 break :blk comment_node;
31383206 }
......@@ -3158,10 +3226,11 @@ fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterato
31583226 return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base;
31593227 },
31603228 Token.Id.MultilineStringLiteralLine => {
3161 const node = try arena.create(ast.Node.MultilineStringLiteral{
3229 const node = try arena.create(ast.Node.MultilineStringLiteral);
3230 node.* = ast.Node.MultilineStringLiteral{
31623231 .base = ast.Node{ .id = ast.Node.Id.MultilineStringLiteral },
31633232 .lines = ast.Node.MultilineStringLiteral.LineList.init(arena),
3164 });
3233 };
31653234 try node.lines.push(token_index);
31663235 while (true) {
31673236 const multiline_str = nextToken(tok_it, tree);
......@@ -3186,25 +3255,27 @@ fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterato
31863255fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: OptionalCtx, token_ptr: Token, token_index: TokenIndex) !bool {
31873256 switch (token_ptr.id) {
31883257 Token.Id.Keyword_suspend => {
3189 const node = try arena.create(ast.Node.Suspend{
3258 const node = try arena.create(ast.Node.Suspend);
3259 node.* = ast.Node.Suspend{
31903260 .base = ast.Node{ .id = ast.Node.Id.Suspend },
31913261 .suspend_token = token_index,
31923262 .body = null,
3193 });
3263 };
31943264 ctx.store(&node.base);
31953265
31963266 stack.append(State{ .SuspendBody = node }) catch unreachable;
31973267 return true;
31983268 },
31993269 Token.Id.Keyword_if => {
3200 const node = try arena.create(ast.Node.If{
3270 const node = try arena.create(ast.Node.If);
3271 node.* = ast.Node.If{
32013272 .base = ast.Node{ .id = ast.Node.Id.If },
32023273 .if_token = token_index,
32033274 .condition = undefined,
32043275 .payload = null,
32053276 .body = undefined,
32063277 .@"else" = null,
3207 });
3278 };
32083279 ctx.store(&node.base);
32093280
32103281 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
......@@ -3238,13 +3309,14 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: Opti
32383309 return true;
32393310 },
32403311 Token.Id.Keyword_switch => {
3241 const node = try arena.create(ast.Node.Switch{
3312 const node = try arena.create(ast.Node.Switch);
3313 node.* = ast.Node.Switch{
32423314 .base = ast.Node{ .id = ast.Node.Id.Switch },
32433315 .switch_token = token_index,
32443316 .expr = undefined,
32453317 .cases = ast.Node.Switch.CaseList.init(arena),
32463318 .rbrace = undefined,
3247 });
3319 };
32483320 ctx.store(&node.base);
32493321
32503322 stack.append(State{
......@@ -3260,25 +3332,27 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: Opti
32603332 return true;
32613333 },
32623334 Token.Id.Keyword_comptime => {
3263 const node = try arena.create(ast.Node.Comptime{
3335 const node = try arena.create(ast.Node.Comptime);
3336 node.* = ast.Node.Comptime{
32643337 .base = ast.Node{ .id = ast.Node.Id.Comptime },
32653338 .comptime_token = token_index,
32663339 .expr = undefined,
32673340 .doc_comments = null,
3268 });
3341 };
32693342 ctx.store(&node.base);
32703343
32713344 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
32723345 return true;
32733346 },
32743347 Token.Id.LBrace => {
3275 const block = try arena.create(ast.Node.Block{
3348 const block = try arena.create(ast.Node.Block);
3349 block.* = ast.Node.Block{
32763350 .base = ast.Node{ .id = ast.Node.Id.Block },
32773351 .label = null,
32783352 .lbrace = token_index,
32793353 .statements = ast.Node.Block.StatementList.init(arena),
32803354 .rbrace = undefined,
3281 });
3355 };
32823356 ctx.store(&block.base);
32833357 stack.append(State{ .Block = block }) catch unreachable;
32843358 return true;
......@@ -3412,10 +3486,12 @@ fn tokenIdToPrefixOp(id: Token.Id) ?ast.Node.PrefixOp.Op {
34123486}
34133487
34143488fn createLiteral(arena: *mem.Allocator, comptime T: type, token_index: TokenIndex) !*T {
3415 return arena.create(T{
3489 const result = try arena.create(T);
3490 result.* = T{
34163491 .base = ast.Node{ .id = ast.Node.typeToId(T) },
34173492 .token = token_index,
3418 });
3493 };
3494 return result;
34193495}
34203496
34213497fn createToCtxLiteral(arena: *mem.Allocator, opt_ctx: OptionalCtx, comptime T: type, token_index: TokenIndex) !*T {
test/tests.zig+45-30
......@@ -48,13 +48,14 @@ const test_targets = []TestTarget{
4848const max_stdout_size = 1 * 1024 * 1024; // 1 MB
4949
5050pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
51 const cases = b.allocator.create(CompareOutputContext{
51 const cases = b.allocator.create(CompareOutputContext) catch unreachable;
52 cases.* = CompareOutputContext{
5253 .b = b,
5354 .step = b.step("test-compare-output", "Run the compare output tests"),
5455 .test_index = 0,
5556 .test_filter = test_filter,
5657 .modes = modes,
57 }) catch unreachable;
58 };
5859
5960 compare_output.addCases(cases);
6061
......@@ -62,13 +63,14 @@ pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes:
6263}
6364
6465pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
65 const cases = b.allocator.create(CompareOutputContext{
66 const cases = b.allocator.create(CompareOutputContext) catch unreachable;
67 cases.* = CompareOutputContext{
6668 .b = b,
6769 .step = b.step("test-runtime-safety", "Run the runtime safety tests"),
6870 .test_index = 0,
6971 .test_filter = test_filter,
7072 .modes = modes,
71 }) catch unreachable;
73 };
7274
7375 runtime_safety.addCases(cases);
7476
......@@ -76,13 +78,14 @@ pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes:
7678}
7779
7880pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
79 const cases = b.allocator.create(CompileErrorContext{
81 const cases = b.allocator.create(CompileErrorContext) catch unreachable;
82 cases.* = CompileErrorContext{
8083 .b = b,
8184 .step = b.step("test-compile-errors", "Run the compile error tests"),
8285 .test_index = 0,
8386 .test_filter = test_filter,
8487 .modes = modes,
85 }) catch unreachable;
88 };
8689
8790 compile_errors.addCases(cases);
8891
......@@ -90,13 +93,14 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes:
9093}
9194
9295pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
93 const cases = b.allocator.create(BuildExamplesContext{
96 const cases = b.allocator.create(BuildExamplesContext) catch unreachable;
97 cases.* = BuildExamplesContext{
9498 .b = b,
9599 .step = b.step("test-build-examples", "Build the examples"),
96100 .test_index = 0,
97101 .test_filter = test_filter,
98102 .modes = modes,
99 }) catch unreachable;
103 };
100104
101105 build_examples.addCases(cases);
102106
......@@ -119,13 +123,14 @@ pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const M
119123}
120124
121125pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
122 const cases = b.allocator.create(CompareOutputContext{
126 const cases = b.allocator.create(CompareOutputContext) catch unreachable;
127 cases.* = CompareOutputContext{
123128 .b = b,
124129 .step = b.step("test-asm-link", "Run the assemble and link tests"),
125130 .test_index = 0,
126131 .test_filter = test_filter,
127132 .modes = modes,
128 }) catch unreachable;
133 };
129134
130135 assemble_and_link.addCases(cases);
131136
......@@ -133,12 +138,13 @@ pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, mode
133138}
134139
135140pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
136 const cases = b.allocator.create(TranslateCContext{
141 const cases = b.allocator.create(TranslateCContext) catch unreachable;
142 cases.* = TranslateCContext{
137143 .b = b,
138144 .step = b.step("test-translate-c", "Run the C transation tests"),
139145 .test_index = 0,
140146 .test_filter = test_filter,
141 }) catch unreachable;
147 };
142148
143149 translate_c.addCases(cases);
144150
......@@ -146,12 +152,13 @@ pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.St
146152}
147153
148154pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
149 const cases = b.allocator.create(GenHContext{
155 const cases = b.allocator.create(GenHContext) catch unreachable;
156 cases.* = GenHContext{
150157 .b = b,
151158 .step = b.step("test-gen-h", "Run the C header file generation tests"),
152159 .test_index = 0,
153160 .test_filter = test_filter,
154 }) catch unreachable;
161 };
155162
156163 gen_h.addCases(cases);
157164
......@@ -244,7 +251,8 @@ pub const CompareOutputContext = struct {
244251
245252 pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) *RunCompareOutputStep {
246253 const allocator = context.b.allocator;
247 const ptr = allocator.create(RunCompareOutputStep{
254 const ptr = allocator.create(RunCompareOutputStep) catch unreachable;
255 ptr.* = RunCompareOutputStep{
248256 .context = context,
249257 .exe_path = exe_path,
250258 .name = name,
......@@ -252,7 +260,7 @@ pub const CompareOutputContext = struct {
252260 .test_index = context.test_index,
253261 .step = build.Step.init("RunCompareOutput", allocator, make),
254262 .cli_args = cli_args,
255 }) catch unreachable;
263 };
256264 context.test_index += 1;
257265 return ptr;
258266 }
......@@ -331,13 +339,14 @@ pub const CompareOutputContext = struct {
331339
332340 pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8) *RuntimeSafetyRunStep {
333341 const allocator = context.b.allocator;
334 const ptr = allocator.create(RuntimeSafetyRunStep{
342 const ptr = allocator.create(RuntimeSafetyRunStep) catch unreachable;
343 ptr.* = RuntimeSafetyRunStep{
335344 .context = context,
336345 .exe_path = exe_path,
337346 .name = name,
338347 .test_index = context.test_index,
339348 .step = build.Step.init("RuntimeSafetyRun", allocator, make),
340 }) catch unreachable;
349 };
341350
342351 context.test_index += 1;
343352 return ptr;
......@@ -542,14 +551,15 @@ pub const CompileErrorContext = struct {
542551
543552 pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep {
544553 const allocator = context.b.allocator;
545 const ptr = allocator.create(CompileCmpOutputStep{
554 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
555 ptr.* = CompileCmpOutputStep{
546556 .step = build.Step.init("CompileCmpOutput", allocator, make),
547557 .context = context,
548558 .name = name,
549559 .test_index = context.test_index,
550560 .case = case,
551561 .build_mode = build_mode,
552 }) catch unreachable;
562 };
553563
554564 context.test_index += 1;
555565 return ptr;
......@@ -661,13 +671,14 @@ pub const CompileErrorContext = struct {
661671 }
662672
663673 pub fn create(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
664 const tc = self.b.allocator.create(TestCase{
674 const tc = self.b.allocator.create(TestCase) catch unreachable;
675 tc.* = TestCase{
665676 .name = name,
666677 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
667678 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
668679 .link_libc = false,
669680 .is_exe = false,
670 }) catch unreachable;
681 };
671682
672683 tc.addSourceFile(".tmp_source.zig", source);
673684 comptime var arg_i = 0;
......@@ -821,13 +832,14 @@ pub const TranslateCContext = struct {
821832
822833 pub fn create(context: *TranslateCContext, name: []const u8, case: *const TestCase) *TranslateCCmpOutputStep {
823834 const allocator = context.b.allocator;
824 const ptr = allocator.create(TranslateCCmpOutputStep{
835 const ptr = allocator.create(TranslateCCmpOutputStep) catch unreachable;
836 ptr.* = TranslateCCmpOutputStep{
825837 .step = build.Step.init("ParseCCmpOutput", allocator, make),
826838 .context = context,
827839 .name = name,
828840 .test_index = context.test_index,
829841 .case = case,
830 }) catch unreachable;
842 };
831843
832844 context.test_index += 1;
833845 return ptr;
......@@ -928,12 +940,13 @@ pub const TranslateCContext = struct {
928940 }
929941
930942 pub fn create(self: *TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
931 const tc = self.b.allocator.create(TestCase{
943 const tc = self.b.allocator.create(TestCase) catch unreachable;
944 tc.* = TestCase{
932945 .name = name,
933946 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
934947 .expected_lines = ArrayList([]const u8).init(self.b.allocator),
935948 .allow_warnings = allow_warnings,
936 }) catch unreachable;
949 };
937950
938951 tc.addSourceFile(filename, source);
939952 comptime var arg_i = 0;
......@@ -1015,14 +1028,15 @@ pub const GenHContext = struct {
10151028
10161029 pub fn create(context: *GenHContext, h_path: []const u8, name: []const u8, case: *const TestCase) *GenHCmpOutputStep {
10171030 const allocator = context.b.allocator;
1018 const ptr = allocator.create(GenHCmpOutputStep{
1031 const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
1032 ptr.* = GenHCmpOutputStep{
10191033 .step = build.Step.init("ParseCCmpOutput", allocator, make),
10201034 .context = context,
10211035 .h_path = h_path,
10221036 .name = name,
10231037 .test_index = context.test_index,
10241038 .case = case,
1025 }) catch unreachable;
1039 };
10261040
10271041 context.test_index += 1;
10281042 return ptr;
......@@ -1062,11 +1076,12 @@ pub const GenHContext = struct {
10621076 }
10631077
10641078 pub fn create(self: *GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase {
1065 const tc = self.b.allocator.create(TestCase{
1079 const tc = self.b.allocator.create(TestCase) catch unreachable;
1080 tc.* = TestCase{
10661081 .name = name,
10671082 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
10681083 .expected_lines = ArrayList([]const u8).init(self.b.allocator),
1069 }) catch unreachable;
1084 };
10701085
10711086 tc.addSourceFile(filename, source);
10721087 comptime var arg_i = 0;