authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-20 13:53:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-20 13:53:53-04:00
log8c10178a1e209af53d28cc70343d0ce908bccc1b
tree71e5cc488764897d62acee7e8a52ef58eb17976f
parent4617c5907a4b7933947a3223d19d637d143fd705

golly jeepers it's taking a long time to update translate-c


4 files changed, 341 insertions(+), 269 deletions(-)

lib/std/linked_list.zig+30
......@@ -49,6 +49,26 @@ pub fn SinglyLinkedList(comptime T: type) type {
4949 node.next = next_node.next;
5050 return next_node;
5151 }
52
53 /// Iterate over the singly-linked list from this node, until the final node is found.
54 /// This operation is O(N).
55 pub fn findLast(node: *Node) *Node {
56 var it = node;
57 while (true) {
58 it = it.next orelse return it;
59 }
60 }
61
62 /// Iterate over each next node, returning the count of all nodes except the starting one.
63 /// This operation is O(N).
64 pub fn countChildren(node: *const Node) usize {
65 var count: usize = 0;
66 var it: ?*const Node = node;
67 while (it) |n| : (it = n.next) {
68 count += 1;
69 }
70 return count;
71 }
5272 };
5373
5474 first: ?*Node = null,
......@@ -87,6 +107,16 @@ pub fn SinglyLinkedList(comptime T: type) type {
87107 list.first = first.next;
88108 return first;
89109 }
110
111 /// Iterate over all nodes, returning the count.
112 /// This operation is O(N).
113 pub fn len(list: Self) usize {
114 if (list.first) |n| {
115 return 1 + n.countChildren();
116 } else {
117 return 0;
118 }
119 }
90120 };
91121}
92122
lib/std/zig/render.zig+3-20
......@@ -915,8 +915,7 @@ fn renderExpression(
915915
916916 if (maybe_row_size) |row_size| {
917917 // A place to store the width of each expression and its column's maximum
918 const exprs_len = countLen(exprs.first);
919 var widths = try allocator.alloc(usize, exprs_len + row_size);
918 var widths = try allocator.alloc(usize, exprs.len() + row_size);
920919 defer allocator.free(widths);
921920 mem.set(usize, widths, 0);
922921
......@@ -1391,7 +1390,7 @@ fn renderExpression(
13911390 {
13921391 break :blk false;
13931392 }
1394 const last_node = findLast(builtin_call.params.first.?).data;
1393 const last_node = builtin_call.params.first.?.findLast().data;
13951394 const maybe_comma = tree.nextToken(last_node.lastToken());
13961395 break :blk tree.tokens[maybe_comma].id == .Comma;
13971396 };
......@@ -1615,7 +1614,7 @@ fn renderExpression(
16151614
16161615 assert(switch_case.items.first != null);
16171616 const src_has_trailing_comma = blk: {
1618 const last_node = findLast(switch_case.items.first.?).data;
1617 const last_node = switch_case.items.first.?.findLast().data;
16191618 const maybe_comma = tree.nextToken(last_node.lastToken());
16201619 break :blk tree.tokens[maybe_comma].id == .Comma;
16211620 };
......@@ -2510,19 +2509,3 @@ fn copyFixingWhitespace(stream: var, slice: []const u8) @TypeOf(stream).Error!vo
25102509 else => try stream.writeByte(byte),
25112510 };
25122511}
2513
2514fn countLen(node: ?*std.SinglyLinkedList(*ast.Node).Node) usize {
2515 var count: usize = 0;
2516 var it = node;
2517 while (it) |n| : (it = n.next) {
2518 count += 1;
2519 }
2520 return count;
2521}
2522
2523fn findLast(node: *std.SinglyLinkedList(*ast.Node).Node) *std.SinglyLinkedList(*ast.Node).Node {
2524 var it = node;
2525 while (true) {
2526 it = it.next orelse return it;
2527 }
2528}
src-self-hosted/main.zig+6-8
......@@ -600,8 +600,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
600600 };
601601 defer tree.deinit();
602602
603 var error_it = tree.errors.iterator(0);
604 while (error_it.next()) |parse_error| {
603 for (tree.errors) |parse_error| {
605604 try printErrMsgToFile(gpa, parse_error, tree, "<stdin>", stderr_file, color);
606605 }
607606 if (tree.errors.len != 0) {
......@@ -701,8 +700,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
701700 };
702701 defer tree.deinit();
703702
704 var error_it = tree.errors.iterator(0);
705 while (error_it.next()) |parse_error| {
703 for (tree.errors) |parse_error| {
706704 try printErrMsgToFile(fmt.gpa, parse_error, tree, file_path, std.io.getStdErr(), fmt.color);
707705 }
708706 if (tree.errors.len != 0) {
......@@ -730,7 +728,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
730728
731729fn printErrMsgToFile(
732730 gpa: *mem.Allocator,
733 parse_error: *const ast.Error,
731 parse_error: ast.Error,
734732 tree: *ast.Tree,
735733 path: []const u8,
736734 file: fs.File,
......@@ -745,15 +743,15 @@ fn printErrMsgToFile(
745743 const span_first = lok_token;
746744 const span_last = lok_token;
747745
748 const first_token = tree.tokens.at(span_first);
749 const last_token = tree.tokens.at(span_last);
746 const first_token = tree.tokens[span_first];
747 const last_token = tree.tokens[span_last];
750748 const start_loc = tree.tokenLocationPtr(0, first_token);
751749 const end_loc = tree.tokenLocationPtr(first_token.end, last_token);
752750
753751 var text_buf = std.ArrayList(u8).init(gpa);
754752 defer text_buf.deinit();
755753 const out_stream = text_buf.outStream();
756 try parse_error.render(&tree.tokens, out_stream);
754 try parse_error.render(tree.tokens, out_stream);
757755 const text = text_buf.span();
758756
759757 const stream = file.outStream();
src-self-hosted/translate_c.zig+302-241
......@@ -37,10 +37,10 @@ fn addrEql(a: usize, b: usize) bool {
3737}
3838
3939const SymbolTable = std.StringHashMap(*ast.Node);
40const AliasList = std.SegmentedList(struct {
40const AliasList = std.ArrayList(struct {
4141 alias: []const u8,
4242 name: []const u8,
43}, 4);
43});
4444
4545const Scope = struct {
4646 id: Id,
......@@ -64,40 +64,50 @@ const Scope = struct {
6464 const Block = struct {
6565 base: Scope,
6666 block_node: *ast.Node.Block,
67 statements_it: *?*std.SinglyLinkedList(*ast.Node),
6768 variables: AliasList,
6869 label: ?[]const u8,
6970 mangle_count: u32 = 0,
7071
7172 /// Don't forget to set rbrace token and block_node later
7273 fn init(c: *Context, parent: *Scope, label: ?[]const u8) !*Block {
73 const block = try c.a().create(Block);
74 const block = try c.arena.create(Block);
7475 block.* = .{
7576 .base = .{
7677 .id = .Block,
7778 .parent = parent,
7879 },
7980 .block_node = undefined,
80 .variables = AliasList.init(c.a()),
81 .statements_it = undefined,
82 .variables = AliasList.init(c.arena),
8183 .label = label,
8284 };
8385 return block;
8486 }
8587
88 fn pushStatement(self: *Block, c: *Context, stmt: *ast.Node) !void {
89 self.statements_it = c.llpush(*ast.Node, self.statements_it, stmt);
90 }
91
92 fn setBlockNode(self: *Block, block: *ast.Node.Block) void {
93 self.block_node = block;
94 self.statements_it = &block.statements.first;
95 }
96
8697 /// Given the desired name, return a name that does not shadow anything from outer scopes.
8798 /// Inserts the returned name into the scope.
8899 fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 {
89100 var proposed_name = name;
90101 while (scope.contains(proposed_name)) {
91102 scope.mangle_count += 1;
92 proposed_name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ name, scope.mangle_count });
103 proposed_name = try std.fmt.allocPrint(c.arena, "{}_{}", .{ name, scope.mangle_count });
93104 }
94 try scope.variables.push(.{ .name = name, .alias = proposed_name });
105 try scope.variables.append(.{ .name = name, .alias = proposed_name });
95106 return proposed_name;
96107 }
97108
98109 fn getAlias(scope: *Block, name: []const u8) []const u8 {
99 var it = scope.variables.iterator(0);
100 while (it.next()) |p| {
110 for (scope.variables.items) |p| {
101111 if (mem.eql(u8, p.name, name))
102112 return p.alias;
103113 }
......@@ -105,8 +115,7 @@ const Scope = struct {
105115 }
106116
107117 fn localContains(scope: *Block, name: []const u8) bool {
108 var it = scope.variables.iterator(0);
109 while (it.next()) |p| {
118 for (scope.variables.items) |p| {
110119 if (mem.eql(u8, p.name, name))
111120 return true;
112121 }
......@@ -132,8 +141,8 @@ const Scope = struct {
132141 .id = .Root,
133142 .parent = null,
134143 },
135 .sym_table = SymbolTable.init(c.a()),
136 .macro_table = SymbolTable.init(c.a()),
144 .sym_table = SymbolTable.init(c.arena),
145 .macro_table = SymbolTable.init(c.arena),
137146 .context = c,
138147 };
139148 }
......@@ -208,7 +217,10 @@ const Scope = struct {
208217};
209218
210219pub const Context = struct {
211 tree: *ast.Tree,
220 gpa: *mem.Allocator,
221 arena: *mem.Allocator,
222 tokens: std.ArrayListUnmanaged(Token),
223 errors: std.ArrayListUnmanaged(ast.Error),
212224 source_buffer: *std.ArrayList(u8),
213225 err: Error,
214226 source_manager: *ZigClangSourceManager,
......@@ -217,6 +229,8 @@ pub const Context = struct {
217229 global_scope: *Scope.Root,
218230 clang_context: *ZigClangASTContext,
219231 mangle_count: u32 = 0,
232 root_node: *ast.Node.Root,
233 root_decls_it: *?*std.SinglyLinkedList(*ast.Node).Node,
220234
221235 /// This one is different than the root scope's name table. This contains
222236 /// a list of names that we found by visiting all the top level decls without
......@@ -224,18 +238,45 @@ pub const Context = struct {
224238 /// up front in a pre-processing step.
225239 global_names: std.StringHashMap(void),
226240
241 /// Helper type to append elements to a singly linked list.
242 const LinkedListPusher = struct {
243 c: *Context,
244 it: *?*std.SinglyLinkedList(*ast.Node).Node,
245
246 fn push(self: *LinkedListPusher, element: *ast.Node) !void {
247 self.it = try self.c.llpush(*ast.Node, self.it, element);
248 }
249 };
250
251 /// Helper function to append items to a singly linked list.
252 fn llpusher(c: *Context, list: *std.SinglyLinkedList(*ast.Node)) LinkedListPusher {
253 assert(list.first == null);
254 return .{
255 .c = c,
256 .it = &list.first,
257 };
258 }
259
260 fn llpush(
261 c: *Context,
262 comptime T: type,
263 it: *?*std.SinglyLinkedList(T).Node,
264 data: T,
265 ) !*?*std.SinglyLinkedList(T).Node {
266 const llnode = try c.arena.create(std.SinglyLinkedList(T).Node);
267 llnode.* = .{ .data = data };
268 it.* = llnode;
269 return &llnode.next;
270 }
271
227272 fn getMangle(c: *Context) u32 {
228273 c.mangle_count += 1;
229274 return c.mangle_count;
230275 }
231276
232 fn a(c: *Context) *mem.Allocator {
233 return &c.tree.arena_allocator.allocator;
234 }
235
236277 /// Convert a null-terminated C string to a slice allocated in the arena
237278 fn str(c: *Context, s: [*:0]const u8) ![]u8 {
238 return mem.dupe(c.a(), u8, mem.spanZ(s));
279 return mem.dupe(c.arena, u8, mem.spanZ(s));
239280 }
240281
241282 /// Convert a clang source location to a file:line:column string
......@@ -246,12 +287,12 @@ pub const Context = struct {
246287
247288 const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc);
248289 const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc);
249 return std.fmt.allocPrint(c.a(), "{}:{}:{}", .{ filename, line, column });
290 return std.fmt.allocPrint(c.arena, "{}:{}:{}", .{ filename, line, column });
250291 }
251292};
252293
253294pub fn translate(
254 backing_allocator: *mem.Allocator,
295 gpa: *mem.Allocator,
255296 args_begin: [*]?[*]const u8,
256297 args_end: [*]?[*]const u8,
257298 errors: *[]ClangErrMsg,
......@@ -269,47 +310,43 @@ pub fn translate(
269310 };
270311 defer ZigClangASTUnit_delete(ast_unit);
271312
272 const tree = blk: {
273 var tree_arena = std.heap.ArenaAllocator.init(backing_allocator);
274 errdefer tree_arena.deinit();
275
276 const tree = try tree_arena.allocator.create(ast.Tree);
277 tree.* = .{
278 .source = undefined, // need to use toOwnedSlice later
279 .root_node = undefined,
280 .arena_allocator = tree_arena,
281 .tokens = undefined, // can't reference the allocator yet
282 .errors = undefined, // can't reference the allocator yet
283 .generated = true,
284 };
285 break :blk tree;
286 };
287 const arena = &tree.arena_allocator.allocator; // now we can reference the allocator
288 errdefer tree.arena_allocator.deinit();
289 tree.tokens = ast.Tree.TokenList.init(arena);
290 tree.errors = ast.Tree.ErrorList.init(arena);
291
292 tree.root_node = try arena.create(ast.Node.Root);
293 tree.root_node.* = .{
294 .decls = ast.Node.Root.DeclList.init(arena),
313 var source_buffer = std.ArrayList(u8).init(gpa);
314 defer source_buffer.deinit();
315
316 // For memory that has the same lifetime as the Tree that we return
317 // from this function.
318 var arena = std.heap.ArenaAllocator.init(gpa);
319 errdefer arena.deinit();
320
321 const root_node = try arena.allocator.create(ast.Node.Root);
322 root_node.* = .{
323 .decls = ast.Node.Root.DeclList{},
295324 // initialized with the eof token at the end
296325 .eof_token = undefined,
297326 };
298327
299 var source_buffer = std.ArrayList(u8).init(arena);
300
301328 var context = Context{
302 .tree = tree,
329 .gpa = gpa,
330 .arena = &arena.allocator,
303331 .source_buffer = &source_buffer,
304332 .source_manager = ZigClangASTUnit_getSourceManager(ast_unit),
305333 .err = undefined,
306 .decl_table = DeclTable.init(arena),
307 .alias_list = AliasList.init(arena),
308 .global_scope = try arena.create(Scope.Root),
334 .decl_table = DeclTable.init(gpa),
335 .alias_list = AliasList.init(gpa),
336 .global_scope = try arena.allocator.create(Scope.Root),
309337 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,
310 .global_names = std.StringHashMap(void).init(arena),
338 .global_names = std.StringHashMap(void).init(gpa),
339 .tokens = .{},
340 .errors = .{},
341 .root_node = root_node,
342 .root_decls_it = &root_node.decls.first,
311343 };
312344 context.global_scope.* = Scope.Root.init(&context);
345 defer context.decl_table.deinit();
346 defer context.alias_list.deinit();
347 defer context.tokens.deinit(gpa);
348 defer context.errors.deinit(gpa);
349 defer context.global_names.deinit();
313350
314351 try prepopulateGlobalNameTable(ast_unit, &context);
315352
......@@ -320,23 +357,30 @@ pub fn translate(
320357 try transPreprocessorEntities(&context, ast_unit);
321358
322359 try addMacros(&context);
323 var it = context.alias_list.iterator(0);
324 while (it.next()) |alias| {
360 for (context.alias_list.items) |alias| {
325361 if (!context.global_scope.sym_table.contains(alias.alias)) {
326362 try createAlias(&context, alias);
327363 }
328364 }
329365
330 tree.root_node.eof_token = try appendToken(&context, .Eof, "");
331 tree.source = source_buffer.toOwnedSlice();
366 root_node.eof_token = try appendToken(&context, .Eof, "");
332367 if (false) {
333 std.debug.warn("debug source:\n{}\n==EOF==\ntokens:\n", .{tree.source});
334 var i: usize = 0;
335 while (i < tree.tokens.len) : (i += 1) {
336 const token = tree.tokens.at(i);
368 std.debug.warn("debug source:\n{}\n==EOF==\ntokens:\n", .{source_buffer.items});
369 for (context.tokens.items) |token| {
337370 std.debug.warn("{}\n", .{token});
338371 }
339372 }
373
374 const tree = try arena.allocator.create(ast.Tree);
375 tree.* = .{
376 .gpa = gpa,
377 .source = try arena.allocator.dupe(u8, source_buffer.items),
378 .tokens = context.tokens.toOwnedSlice(gpa),
379 .errors = context.errors.toOwnedSlice(gpa),
380 .root_node = root_node,
381 .arena = arena.state,
382 .generated = true,
383 };
340384 return tree;
341385}
342386
......@@ -493,17 +537,19 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
493537 const block_scope = try Scope.Block.init(rp.c, &c.global_scope.base, null);
494538 var scope = &block_scope.base;
495539 const block_node = try transCreateNodeBlock(rp.c, null);
496 block_scope.block_node = block_node;
540 block_scope.setBlockNode(block_node);
497541
498 var it = proto_node.params.iterator(0);
542 var it = proto_node.params.first;
499543 var param_id: c_uint = 0;
500 while (it.next()) |p| {
501 const param = @fieldParentPtr(ast.Node.ParamDecl, "base", p.*);
544 var prev_node_link = &proto_node.params_first;
545 while (it) |p_node| : ({prev_node_link = &p_node.next; it = p_node.next;}) {
546 const p = p_node.data;
547 const param = @fieldParentPtr(ast.Node.ParamDecl, "base", p);
502548 const param_name = if (param.name_token) |name_tok|
503549 tokenSlice(c, name_tok)
504550 else if (param.param_type == .var_args) {
505 assert(it.next() == null);
506 _ = proto_node.params.pop();
551 assert(p_node.next == null);
552 prev_node_link.* = null;
507553 break;
508554 } else
509555 return failDecl(c, fn_decl_loc, fn_name, "function {} parameter has no name", .{fn_name});
......@@ -516,7 +562,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
516562
517563 const arg_name = blk: {
518564 const param_prefix = if (is_const) "" else "arg_";
519 const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });
565 const bare_arg_name = try std.fmt.allocPrint(c.arena, "{}{}", .{ param_prefix, mangled_param_name });
520566 break :blk try block_scope.makeMangledName(c, bare_arg_name);
521567 };
522568
......@@ -525,7 +571,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
525571 node.eq_token = try appendToken(c, .Equal, "=");
526572 node.init_node = try transCreateNodeIdentifier(c, arg_name);
527573 node.semicolon_token = try appendToken(c, .Semicolon, ";");
528 try block_node.statements.push(&node.base);
574 block_scope.block_statements_it = try c.llpush(*ast.Node, block_scope.block_statements_it, &node.base);
529575 param.name_token = try appendIdentifier(c, arg_name);
530576 _ = try appendToken(c, .Colon, ":");
531577 }
......@@ -560,7 +606,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
560606
561607 // TODO https://github.com/ziglang/zig/issues/3756
562608 // TODO https://github.com/ziglang/zig/issues/1802
563 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{ var_name, c.getMangle() }) else var_name;
609 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.arena, "{}_{}", .{ var_name, c.getMangle() }) else var_name;
564610 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);
565611
566612 const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);
......@@ -620,7 +666,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
620666 _ = try appendToken(rp.c, .LParen, "(");
621667 const expr = try transCreateNodeStringLiteral(
622668 rp.c,
623 try std.fmt.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
669 try std.fmt.allocPrint(rp.c.arena, "\"{}\"", .{str_ptr[0..str_len]}),
624670 );
625671 _ = try appendToken(rp.c, .RParen, ")");
626672
......@@ -643,7 +689,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
643689 break :blk null;
644690 };
645691
646 const node = try c.a().create(ast.Node.VarDecl);
692 const node = try c.arena.create(ast.Node.VarDecl);
647693 node.* = .{
648694 .doc_comments = null,
649695 .visib_token = visib_tok,
......@@ -702,7 +748,7 @@ fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, top_l
702748
703749 // TODO https://github.com/ziglang/zig/issues/3756
704750 // TODO https://github.com/ziglang/zig/issues/1802
705 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{ typedef_name, c.getMangle() }) else typedef_name;
751 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.arena, "{}_{}", .{ typedef_name, c.getMangle() }) else typedef_name;
706752 if (checkForBuiltinTypedef(checked_name)) |builtin| {
707753 return transTypeDefAsBuiltin(c, typedef_decl, builtin);
708754 }
......@@ -745,7 +791,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
745791 // Record declarations such as `struct {...} x` have no name but they're not
746792 // anonymous hence here isAnonymousStructOrUnion is not needed
747793 if (bare_name.len == 0) {
748 bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
794 bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{}", .{c.getMangle()});
749795 is_unnamed = true;
750796 }
751797
......@@ -762,7 +808,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
762808 return null;
763809 }
764810
765 const name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name });
811 const name = try std.fmt.allocPrint(c.arena, "{}_{}", .{ container_kind_name, bare_name });
766812 _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name);
767813
768814 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);
......@@ -785,15 +831,16 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
785831 const container_tok = try appendToken(c, container_kind, container_kind_name);
786832 const lbrace_token = try appendToken(c, .LBrace, "{");
787833
788 const container_node = try c.a().create(ast.Node.ContainerDecl);
834 const container_node = try c.arena.create(ast.Node.ContainerDecl);
789835 container_node.* = .{
790836 .layout_token = layout_tok,
791837 .kind_token = container_tok,
792838 .init_arg_expr = .None,
793 .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()),
839 .fields_and_decls = ast.Node.ContainerDecl.DeclList{},
794840 .lbrace_token = lbrace_token,
795841 .rbrace_token = undefined,
796842 };
843 var container_fields_and_decls = c.llpusher(&container_node.fields_and_decls);
797844
798845 var unnamed_field_count: u32 = 0;
799846 var it = ZigClangRecordDecl_field_begin(record_def);
......@@ -821,7 +868,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
821868 var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
822869 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl) or raw_name.len == 0) {
823870 // Context.getMangle() is not used here because doing so causes unpredictable field names for anonymous fields.
824 raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{unnamed_field_count});
871 raw_name = try std.fmt.allocPrint(c.arena, "unnamed_{}", .{unnamed_field_count});
825872 unnamed_field_count += 1;
826873 is_anon = true;
827874 }
......@@ -851,7 +898,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
851898 break :blk null;
852899 };
853900
854 const field_node = try c.a().create(ast.Node.ContainerField);
901 const field_node = try c.arena.create(ast.Node.ContainerField);
855902 field_node.* = .{
856903 .doc_comments = null,
857904 .comptime_token = null,
......@@ -868,7 +915,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
868915 );
869916 }
870917
871 try container_node.fields_and_decls.push(&field_node.base);
918 try container_fields_and_decls.push(&field_node.base);
872919 _ = try appendToken(c, .Comma, ",");
873920 }
874921 container_node.rbrace_token = try appendToken(c, .RBrace, "}");
......@@ -892,11 +939,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
892939 var bare_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, enum_decl)));
893940 var is_unnamed = false;
894941 if (bare_name.len == 0) {
895 bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
942 bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{}", .{c.getMangle()});
896943 is_unnamed = true;
897944 }
898945
899 const name = try std.fmt.allocPrint(c.a(), "enum_{}", .{bare_name});
946 const name = try std.fmt.allocPrint(c.arena, "enum_{}", .{bare_name});
900947 _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name);
901948 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);
902949 node.eq_token = try appendToken(c, .Equal, "=");
......@@ -916,15 +963,16 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
916963 const extern_tok = try appendToken(c, .Keyword_extern, "extern");
917964 const container_tok = try appendToken(c, .Keyword_enum, "enum");
918965
919 const container_node = try c.a().create(ast.Node.ContainerDecl);
966 const container_node = try c.arena.create(ast.Node.ContainerDecl);
920967 container_node.* = .{
921968 .layout_token = extern_tok,
922969 .kind_token = container_tok,
923970 .init_arg_expr = .None,
924 .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()),
971 .fields_and_decls = ast.Node.ContainerDecl.DeclList{},
925972 .lbrace_token = undefined,
926973 .rbrace_token = undefined,
927974 };
975 var container_node_fields_and_decls = c.llpusher(&container_node.fields_and_decls);
928976
929977 const int_type = ZigClangEnumDecl_getIntegerType(enum_decl);
930978 // The underlying type may be null in case of forward-declared enum
......@@ -971,7 +1019,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
9711019 } else
9721020 null;
9731021
974 const field_node = try c.a().create(ast.Node.ContainerField);
1022 const field_node = try c.arena.create(ast.Node.ContainerField);
9751023 field_node.* = .{
9761024 .doc_comments = null,
9771025 .comptime_token = null,
......@@ -981,7 +1029,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
9811029 .align_expr = null,
9821030 };
9831031
984 try container_node.fields_and_decls.push(&field_node.base);
1032 try container_node_fields_and_decls.push(&field_node.base);
9851033 _ = try appendToken(c, .Comma, ",");
9861034
9871035 // In C each enum value is in the global namespace. So we put them there too.
......@@ -992,7 +1040,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
9921040 const enum_ident = try transCreateNodeIdentifier(c, name);
9931041 const period_tok = try appendToken(c, .Period, ".");
9941042 const field_ident = try transCreateNodeIdentifier(c, field_name);
995 const field_access_node = try c.a().create(ast.Node.InfixOp);
1043 const field_access_node = try c.arena.create(ast.Node.InfixOp);
9961044 field_access_node.* = .{
9971045 .op_token = period_tok,
9981046 .lhs = enum_ident,
......@@ -1006,7 +1054,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
10061054 try addTopLevelDecl(c, field_name, &tld_node.base);
10071055 }
10081056 // make non exhaustive
1009 const field_node = try c.a().create(ast.Node.ContainerField);
1057 const field_node = try c.arena.create(ast.Node.ContainerField);
10101058 field_node.* = .{
10111059 .doc_comments = null,
10121060 .comptime_token = null,
......@@ -1016,7 +1064,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
10161064 .align_expr = null,
10171065 };
10181066
1019 try container_node.fields_and_decls.push(&field_node.base);
1067 try container_node_fields_and_decls.push(&field_node.base);
10201068 _ = try appendToken(c, .Comma, ",");
10211069 container_node.rbrace_token = try appendToken(c, .RBrace, "}");
10221070
......@@ -1071,7 +1119,7 @@ fn transStmt(
10711119 .ParenExprClass => {
10721120 const expr = try transExpr(rp, scope, ZigClangParenExpr_getSubExpr(@ptrCast(*const ZigClangParenExpr, stmt)), .used, lrvalue);
10731121 if (expr.id == .GroupedExpression) return maybeSuppressResult(rp, scope, result_used, expr);
1074 const node = try rp.c.a().create(ast.Node.GroupedExpression);
1122 const node = try rp.c.arena.create(ast.Node.GroupedExpression);
10751123 node.* = .{
10761124 .lparen = try appendToken(rp.c, .LParen, "("),
10771125 .expr = expr,
......@@ -1116,7 +1164,7 @@ fn transStmt(
11161164 const source_expr = ZigClangOpaqueValueExpr_getSourceExpr(@ptrCast(*const ZigClangOpaqueValueExpr, stmt)).?;
11171165 const expr = try transExpr(rp, scope, source_expr, .used, lrvalue);
11181166 if (expr.id == .GroupedExpression) return maybeSuppressResult(rp, scope, result_used, expr);
1119 const node = try rp.c.a().create(ast.Node.GroupedExpression);
1167 const node = try rp.c.arena.create(ast.Node.GroupedExpression);
11201168 node.* = .{
11211169 .lparen = try appendToken(rp.c, .LParen, "("),
11221170 .expr = expr,
......@@ -1147,18 +1195,18 @@ fn transBinaryOperator(
11471195 var op_token: ast.TokenIndex = undefined;
11481196 var op_id: ast.Node.InfixOp.Op = undefined;
11491197 switch (op) {
1150 .Assign => return transCreateNodeAssign(rp, scope, result_used, ZigClangBinaryOperator_getLHS(stmt), ZigClangBinaryOperator_getRHS(stmt)),
1198 .Assign => return try transCreateNodeAssign(rp, scope, result_used, ZigClangBinaryOperator_getLHS(stmt), ZigClangBinaryOperator_getRHS(stmt)),
11511199 .Comma => {
11521200 const block_scope = try scope.findBlockScope(rp.c);
11531201 const expr = block_scope.base.parent == scope;
11541202 const lparen = if (expr) blk: {
11551203 const l = try appendToken(rp.c, .LParen, "(");
1156 block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label);
1204 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, block_scope.label));
11571205 break :blk l;
11581206 } else undefined;
11591207
11601208 const lhs = try transExpr(rp, &block_scope.base, ZigClangBinaryOperator_getLHS(stmt), .unused, .r_value);
1161 try block_scope.block_node.statements.push(lhs);
1209 try block_scope.statements.push(lhs);
11621210
11631211 const rhs = try transExpr(rp, &block_scope.base, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value);
11641212 if (expr) {
......@@ -1168,7 +1216,7 @@ fn transBinaryOperator(
11681216 try block_scope.block_node.statements.push(&break_node.base);
11691217 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
11701218 const rparen = try appendToken(rp.c, .RParen, ")");
1171 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
1219 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
11721220 grouped_expr.* = .{
11731221 .lparen = lparen,
11741222 .expr = &block_scope.block_node.base,
......@@ -1335,7 +1383,7 @@ fn transCompoundStmtInline(
13351383
13361384fn transCompoundStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundStmt) TransError!*ast.Node {
13371385 const block_scope = try Scope.Block.init(rp.c, scope, null);
1338 block_scope.block_node = try transCreateNodeBlock(rp.c, null);
1386 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, null));
13391387 try transCompoundStmtInline(rp, &block_scope.base, stmt, block_scope.block_node);
13401388 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
13411389 return &block_scope.block_node.base;
......@@ -1458,7 +1506,7 @@ fn transImplicitCastExpr(
14581506 switch (ZigClangImplicitCastExpr_getCastKind(expr)) {
14591507 .BitCast, .FloatingCast, .FloatingToIntegral, .IntegralToFloating, .IntegralCast, .PointerToIntegral, .IntegralToPointer => {
14601508 const sub_expr_node = try transExpr(rp, scope, sub_expr, .used, .r_value);
1461 return transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, sub_expr_node);
1509 return try transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, sub_expr_node);
14621510 },
14631511 .LValueToRValue, .NoOp, .FunctionToPointerDecay => {
14641512 const sub_expr_node = try transExpr(rp, scope, sub_expr, .used, .r_value);
......@@ -1539,7 +1587,7 @@ fn transBoolExpr(
15391587
15401588 if (grouped) {
15411589 const rparen = try appendToken(rp.c, .RParen, ")");
1542 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
1590 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
15431591 grouped_expr.* = .{
15441592 .lparen = lparen,
15451593 .expr = node,
......@@ -1747,13 +1795,13 @@ fn transStringLiteral(
17471795 len = 0;
17481796 for (str) |c| len += escapeChar(c, &char_buf).len;
17491797
1750 const buf = try rp.c.a().alloc(u8, len + "\"\"".len);
1798 const buf = try rp.c.arena.alloc(u8, len + "\"\"".len);
17511799 buf[0] = '"';
17521800 writeEscapedString(buf[1..], str);
17531801 buf[buf.len - 1] = '"';
17541802
17551803 const token = try appendToken(rp.c, .StringLiteral, buf);
1756 const node = try rp.c.a().create(ast.Node.StringLiteral);
1804 const node = try rp.c.arena.create(ast.Node.StringLiteral);
17571805 node.* = .{
17581806 .token = token,
17591807 };
......@@ -2038,13 +2086,13 @@ fn transInitListExprRecord(
20382086 var raw_name = try rp.c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
20392087 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {
20402088 const name = rp.c.decl_table.get(@ptrToInt(ZigClangFieldDecl_getCanonicalDecl(field_decl))).?;
2041 raw_name = try mem.dupe(rp.c.a(), u8, name.value);
2089 raw_name = try mem.dupe(rp.c.arena, u8, name.value);
20422090 }
20432091 const field_name_tok = try appendIdentifier(rp.c, raw_name);
20442092
20452093 _ = try appendToken(rp.c, .Equal, "=");
20462094
2047 const field_init_node = try rp.c.a().create(ast.Node.FieldInitializer);
2095 const field_init_node = try rp.c.arena.create(ast.Node.FieldInitializer);
20482096 field_init_node.* = .{
20492097 .period_token = period_tok,
20502098 .name_token = field_name_tok,
......@@ -2133,7 +2181,7 @@ fn transInitListExprArray(
21332181 &filler_init_node.base
21342182 else blk: {
21352183 const mul_tok = try appendToken(rp.c, .AsteriskAsterisk, "**");
2136 const mul_node = try rp.c.a().create(ast.Node.InfixOp);
2184 const mul_node = try rp.c.arena.create(ast.Node.InfixOp);
21372185 mul_node.* = .{
21382186 .op_token = mul_tok,
21392187 .lhs = &filler_init_node.base,
......@@ -2147,7 +2195,7 @@ fn transInitListExprArray(
21472195 return rhs_node;
21482196 }
21492197
2150 const cat_node = try rp.c.a().create(ast.Node.InfixOp);
2198 const cat_node = try rp.c.arena.create(ast.Node.InfixOp);
21512199 cat_node.* = .{
21522200 .op_token = cat_tok,
21532201 .lhs = &init_node.base,
......@@ -2382,7 +2430,7 @@ fn transForLoop(
23822430 if (ZigClangForStmt_getInit(stmt)) |init| {
23832431 block_scope = try Scope.Block.init(rp.c, scope, null);
23842432 const block = try transCreateNodeBlock(rp.c, null);
2385 block_scope.?.block_node = block;
2433 block_scope.?.setBlockNode(block);
23862434 loop_scope.parent = &block_scope.?.base;
23872435 const result = try transStmt(rp, &block_scope.?.base, init, .unused, .r_value);
23882436 if (result != &block.base)
......@@ -2445,7 +2493,7 @@ fn transSwitch(
24452493 const block_scope = try Scope.Block.init(rp.c, &switch_scope.base, null);
24462494 // tmp block that all statements will go before being picked up by a case or default
24472495 const block = try transCreateNodeBlock(rp.c, null);
2448 block_scope.block_node = block;
2496 block_scope.setBlockNode(block);
24492497
24502498 const switch_block = try transCreateNodeBlock(rp.c, null);
24512499 try switch_block.statements.push(&switch_node.base);
......@@ -2479,7 +2527,7 @@ fn transCase(
24792527) TransError!*ast.Node {
24802528 const block_scope = scope.findBlockScope(rp.c) catch unreachable;
24812529 const switch_scope = scope.getSwitch();
2482 const label = try std.fmt.allocPrint(rp.c.a(), "__case_{}", .{switch_scope.cases.len - @boolToInt(switch_scope.has_default)});
2530 const label = try std.fmt.allocPrint(rp.c.arena, "__case_{}", .{switch_scope.cases.len() - @boolToInt(switch_scope.has_default)});
24832531 _ = try appendToken(rp.c, .Semicolon, ";");
24842532
24852533 const expr = if (ZigClangCaseStmt_getRHS(stmt)) |rhs| blk: {
......@@ -2487,7 +2535,7 @@ fn transCase(
24872535 const ellips = try appendToken(rp.c, .Ellipsis3, "...");
24882536 const rhs_node = try transExpr(rp, scope, rhs, .used, .r_value);
24892537
2490 const node = try rp.c.a().create(ast.Node.InfixOp);
2538 const node = try rp.c.arena.create(ast.Node.InfixOp);
24912539 node.* = .{
24922540 .op_token = ellips,
24932541 .lhs = lhs_node,
......@@ -2606,7 +2654,7 @@ fn transCharLiteral(
26062654 }
26072655 var char_buf: [4]u8 = undefined;
26082656 const token = try appendTokenFmt(rp.c, .CharLiteral, "'{}'", .{escapeChar(@intCast(u8, val), &char_buf)});
2609 const node = try rp.c.a().create(ast.Node.CharLiteral);
2657 const node = try rp.c.arena.create(ast.Node.CharLiteral);
26102658 node.* = .{
26112659 .token = token,
26122660 };
......@@ -2646,7 +2694,7 @@ fn transStmtExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmtExpr,
26462694 const lparen = try appendToken(rp.c, .LParen, "(");
26472695 const block_scope = try Scope.Block.init(rp.c, scope, "blk");
26482696 const block = try transCreateNodeBlock(rp.c, "blk");
2649 block_scope.block_node = block;
2697 block_scope.setBlockNode(block);
26502698
26512699 var it = ZigClangCompoundStmt_body_begin(comp);
26522700 const end_it = ZigClangCompoundStmt_body_end(comp);
......@@ -2661,7 +2709,7 @@ fn transStmtExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmtExpr,
26612709 try block.statements.push(&break_node.base);
26622710 block.rbrace = try appendToken(rp.c, .RBrace, "}");
26632711 const rparen = try appendToken(rp.c, .RParen, ")");
2664 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
2712 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
26652713 grouped_expr.* = .{
26662714 .lparen = lparen,
26672715 .expr = &block.base,
......@@ -2686,7 +2734,7 @@ fn transMemberExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangMemberE
26862734 const field_decl = @ptrCast(*const struct_ZigClangFieldDecl, member_decl);
26872735 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {
26882736 const name = rp.c.decl_table.get(@ptrToInt(ZigClangFieldDecl_getCanonicalDecl(field_decl))).?;
2689 break :blk try mem.dupe(rp.c.a(), u8, name.value);
2737 break :blk try mem.dupe(rp.c.arena, u8, name.value);
26902738 }
26912739 }
26922740 const decl = @ptrCast(*const ZigClangNamedDecl, member_decl);
......@@ -2941,7 +2989,7 @@ fn transCreatePreCrement(
29412989 // zig: break :blk _ref.*
29422990 // zig: })
29432991 const block_scope = try Scope.Block.init(rp.c, scope, "blk");
2944 block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label);
2992 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, block_scope.label));
29452993 const ref = try block_scope.makeMangledName(rp.c, "ref");
29462994
29472995 const node = try transCreateNodeVarDecl(rp.c, false, true, ref);
......@@ -2967,7 +3015,7 @@ fn transCreatePreCrement(
29673015 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
29683016 // semicolon must immediately follow rbrace because it is the last token in a block
29693017 _ = try appendToken(rp.c, .Semicolon, ";");
2970 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
3018 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
29713019 grouped_expr.* = .{
29723020 .lparen = try appendToken(rp.c, .LParen, "("),
29733021 .expr = &block_scope.block_node.base,
......@@ -3007,7 +3055,7 @@ fn transCreatePostCrement(
30073055 // zig: break :blk _tmp
30083056 // zig: })
30093057 const block_scope = try Scope.Block.init(rp.c, scope, "blk");
3010 block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label);
3058 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, block_scope.label));
30113059 const ref = try block_scope.makeMangledName(rp.c, "ref");
30123060
30133061 const node = try transCreateNodeVarDecl(rp.c, false, true, ref);
......@@ -3040,7 +3088,7 @@ fn transCreatePostCrement(
30403088 try block_scope.block_node.statements.push(&break_node.base);
30413089 _ = try appendToken(rp.c, .Semicolon, ";");
30423090 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
3043 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
3091 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
30443092 grouped_expr.* = .{
30453093 .lparen = try appendToken(rp.c, .LParen, "("),
30463094 .expr = &block_scope.block_node.base,
......@@ -3106,7 +3154,7 @@ fn transCreateCompoundAssign(
31063154
31073155 if ((is_mod or is_div) and is_signed) {
31083156 const op_token = try appendToken(rp.c, .Equal, "=");
3109 const op_node = try rp.c.a().create(ast.Node.InfixOp);
3157 const op_node = try rp.c.arena.create(ast.Node.InfixOp);
31103158 const builtin = if (is_mod) "@rem" else "@divTrunc";
31113159 const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, builtin);
31123160 const lhs_node = try transExpr(rp, scope, lhs, .used, .l_value);
......@@ -3152,7 +3200,7 @@ fn transCreateCompoundAssign(
31523200 // zig: break :blk _ref.*
31533201 // zig: })
31543202 const block_scope = try Scope.Block.init(rp.c, scope, "blk");
3155 block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label);
3203 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, block_scope.label));
31563204 const ref = try block_scope.makeMangledName(rp.c, "ref");
31573205
31583206 const node = try transCreateNodeVarDecl(rp.c, false, true, ref);
......@@ -3169,7 +3217,7 @@ fn transCreateCompoundAssign(
31693217
31703218 if ((is_mod or is_div) and is_signed) {
31713219 const op_token = try appendToken(rp.c, .Equal, "=");
3172 const op_node = try rp.c.a().create(ast.Node.InfixOp);
3220 const op_node = try rp.c.arena.create(ast.Node.InfixOp);
31733221 const builtin = if (is_mod) "@rem" else "@divTrunc";
31743222 const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, builtin);
31753223 try builtin_node.params.push(try transCreateNodePtrDeref(rp.c, lhs_node));
......@@ -3211,7 +3259,7 @@ fn transCreateCompoundAssign(
32113259 break_node.rhs = ref_node;
32123260 try block_scope.block_node.statements.push(&break_node.base);
32133261 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
3214 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
3262 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
32153263 grouped_expr.* = .{
32163264 .lparen = try appendToken(rp.c, .LParen, "("),
32173265 .expr = &block_scope.block_node.base,
......@@ -3295,7 +3343,7 @@ fn transBreak(rp: RestorePoint, scope: *Scope) TransError!*ast.Node {
32953343fn transFloatingLiteral(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangFloatingLiteral, used: ResultUsed) TransError!*ast.Node {
32963344 // TODO use something more accurate
32973345 const dbl = ZigClangAPFloat_getValueAsApproximateDouble(stmt);
3298 const node = try rp.c.a().create(ast.Node.FloatLiteral);
3346 const node = try rp.c.arena.create(ast.Node.FloatLiteral);
32993347 node.* = .{
33003348 .token = try appendTokenFmt(rp.c, .FloatLiteral, "{d}", .{dbl}),
33013349 };
......@@ -3318,7 +3366,7 @@ fn transBinaryConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const
33183366 const lparen = try appendToken(rp.c, .LParen, "(");
33193367
33203368 const block_scope = try Scope.Block.init(rp.c, scope, "blk");
3321 block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label);
3369 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, block_scope.label));
33223370
33233371 const mangled_name = try block_scope.makeMangledName(rp.c, "cond_temp");
33243372 const tmp_var = try transCreateNodeVarDecl(rp.c, false, true, mangled_name);
......@@ -3351,7 +3399,7 @@ fn transBinaryConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const
33513399 try block_scope.block_node.statements.push(&break_node.base);
33523400 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
33533401
3354 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
3402 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
33553403 grouped_expr.* = .{
33563404 .lparen = lparen,
33573405 .expr = &block_scope.block_node.base,
......@@ -3384,7 +3432,7 @@ fn transConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigCla
33843432
33853433 if (grouped) {
33863434 const rparen = try appendToken(rp.c, .RParen, ")");
3387 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
3435 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
33883436 grouped_expr.* = .{
33893437 .lparen = lparen,
33903438 .expr = &if_node.base,
......@@ -3415,7 +3463,7 @@ fn maybeSuppressResult(
34153463 }
34163464 const lhs = try transCreateNodeIdentifier(rp.c, "_");
34173465 const op_token = try appendToken(rp.c, .Equal, "=");
3418 const op_node = try rp.c.a().create(ast.Node.InfixOp);
3466 const op_node = try rp.c.arena.create(ast.Node.InfixOp);
34193467 op_node.* = .{
34203468 .op_token = op_token,
34213469 .lhs = lhs,
......@@ -3426,7 +3474,7 @@ fn maybeSuppressResult(
34263474}
34273475
34283476fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: *ast.Node) !void {
3429 try c.tree.root_node.decls.push(decl_node);
3477 c.root_decls_it = try c.llpush(*ast.Node, c.root_decls_it, decl_node);
34303478 _ = try c.global_scope.sym_table.put(name, decl_node);
34313479}
34323480
......@@ -3524,7 +3572,7 @@ fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigC
35243572 if (int_bit_width != 0) {
35253573 // we can perform the log2 now.
35263574 const cast_bit_width = math.log2_int(u64, int_bit_width);
3527 const node = try rp.c.a().create(ast.Node.IntegerLiteral);
3575 const node = try rp.c.arena.create(ast.Node.IntegerLiteral);
35283576 node.* = .{
35293577 .token = try appendTokenFmt(rp.c, .Identifier, "u{}", .{cast_bit_width}),
35303578 };
......@@ -3547,7 +3595,7 @@ fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigC
35473595
35483596 const import_fn_call = try transCreateNodeBuiltinFnCall(rp.c, "@import");
35493597 const std_token = try appendToken(rp.c, .StringLiteral, "\"std\"");
3550 const std_node = try rp.c.a().create(ast.Node.StringLiteral);
3598 const std_node = try rp.c.arena.create(ast.Node.StringLiteral);
35513599 std_node.* = .{
35523600 .token = std_token,
35533601 };
......@@ -3749,7 +3797,7 @@ fn transCreateNodeAssign(
37493797 // zig: break :blk _tmp
37503798 // zig: })
37513799 const block_scope = try Scope.Block.init(rp.c, scope, "blk");
3752 block_scope.block_node = try transCreateNodeBlock(rp.c, block_scope.label);
3800 block_scope.setBlockNode(try transCreateNodeBlock(rp.c, block_scope.label));
37533801 const tmp = try block_scope.makeMangledName(rp.c, "tmp");
37543802
37553803 const node = try transCreateNodeVarDecl(rp.c, false, true, tmp);
......@@ -3786,10 +3834,10 @@ fn transCreateNodeAssign(
37863834fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.BuiltinCall {
37873835 const builtin_token = try appendToken(c, .Builtin, name);
37883836 _ = try appendToken(c, .LParen, "(");
3789 const node = try c.a().create(ast.Node.BuiltinCall);
3837 const node = try c.arena.create(ast.Node.BuiltinCall);
37903838 node.* = .{
37913839 .builtin_token = builtin_token,
3792 .params = ast.Node.BuiltinCall.ParamList.init(c.a()),
3840 .params = ast.Node.BuiltinCall.ParamList{},
37933841 .rparen_token = undefined, // set after appending args
37943842 };
37953843 return node;
......@@ -3797,12 +3845,12 @@ fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.Builti
37973845
37983846fn transCreateNodeFnCall(c: *Context, fn_expr: *ast.Node) !*ast.Node.SuffixOp {
37993847 _ = try appendToken(c, .LParen, "(");
3800 const node = try c.a().create(ast.Node.SuffixOp);
3848 const node = try c.arena.create(ast.Node.SuffixOp);
38013849 node.* = .{
38023850 .lhs = .{ .node = fn_expr },
38033851 .op = .{
38043852 .Call = .{
3805 .params = ast.Node.SuffixOp.Op.Call.ParamList.init(c.a()),
3853 .params = ast.Node.SuffixOp.Op.Call.ParamList{},
38063854 .async_token = null,
38073855 },
38083856 },
......@@ -3812,7 +3860,7 @@ fn transCreateNodeFnCall(c: *Context, fn_expr: *ast.Node) !*ast.Node.SuffixOp {
38123860}
38133861
38143862fn transCreateNodeFieldAccess(c: *Context, container: *ast.Node, field_name: []const u8) !*ast.Node {
3815 const field_access_node = try c.a().create(ast.Node.InfixOp);
3863 const field_access_node = try c.arena.create(ast.Node.InfixOp);
38163864 field_access_node.* = .{
38173865 .op_token = try appendToken(c, .Period, "."),
38183866 .lhs = container,
......@@ -3828,7 +3876,7 @@ fn transCreateNodePrefixOp(
38283876 op_tok_id: std.zig.Token.Id,
38293877 bytes: []const u8,
38303878) !*ast.Node.PrefixOp {
3831 const node = try c.a().create(ast.Node.PrefixOp);
3879 const node = try c.arena.create(ast.Node.PrefixOp);
38323880 node.* = .{
38333881 .op_token = try appendToken(c, op_tok_id, bytes),
38343882 .op = op,
......@@ -3851,7 +3899,7 @@ fn transCreateNodeInfixOp(
38513899 try appendToken(rp.c, .LParen, "(")
38523900 else
38533901 null;
3854 const node = try rp.c.a().create(ast.Node.InfixOp);
3902 const node = try rp.c.arena.create(ast.Node.InfixOp);
38553903 node.* = .{
38563904 .op_token = op_token,
38573905 .lhs = lhs_node,
......@@ -3860,7 +3908,7 @@ fn transCreateNodeInfixOp(
38603908 };
38613909 if (!grouped) return maybeSuppressResult(rp, scope, used, &node.base);
38623910 const rparen = try appendToken(rp.c, .RParen, ")");
3863 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
3911 const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression);
38643912 grouped_expr.* = .{
38653913 .lparen = lparen.?,
38663914 .expr = &node.base,
......@@ -3904,7 +3952,7 @@ fn transCreateNodePtrType(
39043952 is_volatile: bool,
39053953 op_tok_id: std.zig.Token.Id,
39063954) !*ast.Node.PrefixOp {
3907 const node = try c.a().create(ast.Node.PrefixOp);
3955 const node = try c.arena.create(ast.Node.PrefixOp);
39083956 const op_token = switch (op_tok_id) {
39093957 .LBracket => blk: {
39103958 const lbracket = try appendToken(c, .LBracket, "[");
......@@ -3946,8 +3994,8 @@ fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node {
39463994 ZigClangAPSInt_free(aps_int);
39473995 };
39483996
3949 const limbs = try c.a().alloc(math.big.Limb, num_limbs);
3950 defer c.a().free(limbs);
3997 const limbs = try c.arena.alloc(math.big.Limb, num_limbs);
3998 defer c.arena.free(limbs);
39513999
39524000 const data = ZigClangAPSInt_getRawData(aps_int);
39534001 switch (@sizeOf(math.big.Limb)) {
......@@ -3972,12 +4020,12 @@ fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node {
39724020 }
39734021
39744022 const big: math.big.int.Const = .{ .limbs = limbs, .positive = !is_negative };
3975 const str = big.toStringAlloc(c.a(), 10, false) catch |err| switch (err) {
4023 const str = big.toStringAlloc(c.arena, 10, false) catch |err| switch (err) {
39764024 error.OutOfMemory => return error.OutOfMemory,
39774025 };
3978 defer c.a().free(str);
4026 defer c.arena.free(str);
39794027 const token = try appendToken(c, .IntegerLiteral, str);
3980 const node = try c.a().create(ast.Node.IntegerLiteral);
4028 const node = try c.arena.create(ast.Node.IntegerLiteral);
39814029 node.* = .{
39824030 .token = token,
39834031 };
......@@ -3986,7 +4034,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node {
39864034
39874035fn transCreateNodeReturnExpr(c: *Context) !*ast.Node.ControlFlowExpression {
39884036 const ltoken = try appendToken(c, .Keyword_return, "return");
3989 const node = try c.a().create(ast.Node.ControlFlowExpression);
4037 const node = try c.arena.create(ast.Node.ControlFlowExpression);
39904038 node.* = .{
39914039 .ltoken = ltoken,
39924040 .kind = .Return,
......@@ -3997,7 +4045,7 @@ fn transCreateNodeReturnExpr(c: *Context) !*ast.Node.ControlFlowExpression {
39974045
39984046fn transCreateNodeUndefinedLiteral(c: *Context) !*ast.Node {
39994047 const token = try appendToken(c, .Keyword_undefined, "undefined");
4000 const node = try c.a().create(ast.Node.UndefinedLiteral);
4048 const node = try c.arena.create(ast.Node.UndefinedLiteral);
40014049 node.* = .{
40024050 .token = token,
40034051 };
......@@ -4006,7 +4054,7 @@ fn transCreateNodeUndefinedLiteral(c: *Context) !*ast.Node {
40064054
40074055fn transCreateNodeNullLiteral(c: *Context) !*ast.Node {
40084056 const token = try appendToken(c, .Keyword_null, "null");
4009 const node = try c.a().create(ast.Node.NullLiteral);
4057 const node = try c.arena.create(ast.Node.NullLiteral);
40104058 node.* = .{
40114059 .token = token,
40124060 };
......@@ -4018,7 +4066,7 @@ fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node {
40184066 try appendToken(c, .Keyword_true, "true")
40194067 else
40204068 try appendToken(c, .Keyword_false, "false");
4021 const node = try c.a().create(ast.Node.BoolLiteral);
4069 const node = try c.arena.create(ast.Node.BoolLiteral);
40224070 node.* = .{
40234071 .token = token,
40244072 };
......@@ -4027,11 +4075,11 @@ fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node {
40274075
40284076fn transCreateNodeArrayInitializer(c: *Context, ty: *ast.Node) !*ast.Node.SuffixOp {
40294077 _ = try appendToken(c, .LBrace, "{");
4030 const node = try c.a().create(ast.Node.SuffixOp);
4078 const node = try c.arena.create(ast.Node.SuffixOp);
40314079 node.* = .{
40324080 .lhs = .{ .node = ty },
40334081 .op = .{
4034 .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()),
4082 .ArrayInitializer = ast.Node.SuffixOp.Op.InitList{},
40354083 },
40364084 .rtoken = undefined, // set after appending values
40374085 };
......@@ -4040,11 +4088,11 @@ fn transCreateNodeArrayInitializer(c: *Context, ty: *ast.Node) !*ast.Node.Suffix
40404088
40414089fn transCreateNodeStructInitializer(c: *Context, ty: *ast.Node) !*ast.Node.SuffixOp {
40424090 _ = try appendToken(c, .LBrace, "{");
4043 const node = try c.a().create(ast.Node.SuffixOp);
4091 const node = try c.arena.create(ast.Node.SuffixOp);
40444092 node.* = .{
40454093 .lhs = .{ .node = ty },
40464094 .op = .{
4047 .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()),
4095 .StructInitializer = ast.Node.SuffixOp.Op.InitList{},
40484096 },
40494097 .rtoken = undefined, // set after appending values
40504098 };
......@@ -4053,7 +4101,7 @@ fn transCreateNodeStructInitializer(c: *Context, ty: *ast.Node) !*ast.Node.Suffi
40534101
40544102fn transCreateNodeInt(c: *Context, int: var) !*ast.Node {
40554103 const token = try appendTokenFmt(c, .IntegerLiteral, "{}", .{int});
4056 const node = try c.a().create(ast.Node.IntegerLiteral);
4104 const node = try c.arena.create(ast.Node.IntegerLiteral);
40574105 node.* = .{
40584106 .token = token,
40594107 };
......@@ -4062,7 +4110,7 @@ fn transCreateNodeInt(c: *Context, int: var) !*ast.Node {
40624110
40634111fn transCreateNodeFloat(c: *Context, int: var) !*ast.Node {
40644112 const token = try appendTokenFmt(c, .FloatLiteral, "{}", .{int});
4065 const node = try c.a().create(ast.Node.FloatLiteral);
4113 const node = try c.arena.create(ast.Node.FloatLiteral);
40664114 node.* = .{
40674115 .token = token,
40684116 };
......@@ -4085,20 +4133,22 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a
40854133 const name_tok = try appendIdentifier(c, name);
40864134 _ = try appendToken(c, .LParen, "(");
40874135
4088 var fn_params = ast.Node.FnProto.ParamList.init(c.a());
4089 var it = proto_alias.params.iterator(0);
4090 while (it.next()) |pn| {
4091 if (it.index != 0) {
4136 var fn_params = ast.Node.FnProto.ParamList{};
4137 var fn_params_list = &fn_params.first;
4138 var it = proto_alias.params.first;
4139 while (it) |pn_node| : (it = pn_node.next) {
4140 const pn = pn_node.data;
4141 if (pn_node != proto_alias.params.first.?) {
40924142 _ = try appendToken(c, .Comma, ",");
40934143 }
4094 const param = pn.*.cast(ast.Node.ParamDecl).?;
4144 const param = pn.cast(ast.Node.ParamDecl).?;
40954145
40964146 const param_name_tok = param.name_token orelse
40974147 try appendTokenFmt(c, .Identifier, "arg_{}", .{c.getMangle()});
40984148
40994149 _ = try appendToken(c, .Colon, ":");
41004150
4101 const param_node = try c.a().create(ast.Node.ParamDecl);
4151 const param_node = try c.arena.create(ast.Node.ParamDecl);
41024152 param_node.* = .{
41034153 .doc_comments = null,
41044154 .comptime_token = null,
......@@ -4106,12 +4156,12 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a
41064156 .name_token = param_name_tok,
41074157 .param_type = param.param_type,
41084158 };
4109 try fn_params.push(&param_node.base);
4159 fn_params_list = try c.llpush(*ast.Node, fn_params_list, &param_node.base);
41104160 }
41114161
41124162 _ = try appendToken(c, .RParen, ")");
41134163
4114 const fn_proto = try c.a().create(ast.Node.FnProto);
4164 const fn_proto = try c.arena.create(ast.Node.FnProto);
41154165 fn_proto.* = .{
41164166 .doc_comments = null,
41174167 .visib_token = pub_tok,
......@@ -4129,24 +4179,28 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a
41294179 };
41304180
41314181 const block = try transCreateNodeBlock(c, null);
4182 var block_statements_it = &block.statements.first;
41324183
41334184 const return_expr = try transCreateNodeReturnExpr(c);
41344185 const unwrap_expr = try transCreateNodeUnwrapNull(c, ref.cast(ast.Node.VarDecl).?.init_node.?);
41354186 const call_expr = try transCreateNodeFnCall(c, unwrap_expr);
4136 it = fn_params.iterator(0);
4137 while (it.next()) |pn| {
4138 if (it.index != 0) {
4187 var call_params_it = &call_expr.op.Call.params.first;
4188
4189 it = fn_params.first;
4190 while (it) |pn_node| : (it = pn_node.next) {
4191 const pn = pn_node.data;
4192 if (fn_params.first.? != pn_node) {
41394193 _ = try appendToken(c, .Comma, ",");
41404194 }
4141 const param = pn.*.cast(ast.Node.ParamDecl).?;
4142 try call_expr.op.Call.params.push(try transCreateNodeIdentifier(c, tokenSlice(c, param.name_token.?)));
4195 const param = pn.cast(ast.Node.ParamDecl).?;
4196 call_params_it = try c.llpush(*ast.Node, call_params_it, try transCreateNodeIdentifier(c, tokenSlice(c, param.name_token.?)),);
41434197 }
41444198 call_expr.rtoken = try appendToken(c, .RParen, ")");
41454199 return_expr.rhs = &call_expr.base;
41464200 _ = try appendToken(c, .Semicolon, ";");
41474201
41484202 block.rbrace = try appendToken(c, .RBrace, "}");
4149 try block.statements.push(&return_expr.base);
4203 block_statements_it = try c.llpush(*ast.Node, block_statements_it, &return_expr.base);
41504204 fn_proto.body_node = &block.base;
41514205 return &fn_proto.base;
41524206}
......@@ -4154,7 +4208,7 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a
41544208fn transCreateNodeUnwrapNull(c: *Context, wrapped: *ast.Node) !*ast.Node {
41554209 _ = try appendToken(c, .Period, ".");
41564210 const qm = try appendToken(c, .QuestionMark, "?");
4157 const node = try c.a().create(ast.Node.SuffixOp);
4211 const node = try c.arena.create(ast.Node.SuffixOp);
41584212 node.* = .{
41594213 .op = .UnwrapOptional,
41604214 .lhs = .{ .node = wrapped },
......@@ -4164,7 +4218,7 @@ fn transCreateNodeUnwrapNull(c: *Context, wrapped: *ast.Node) !*ast.Node {
41644218}
41654219
41664220fn transCreateNodeEnumLiteral(c: *Context, name: []const u8) !*ast.Node {
4167 const node = try c.a().create(ast.Node.EnumLiteral);
4221 const node = try c.arena.create(ast.Node.EnumLiteral);
41684222 node.* = .{
41694223 .dot = try appendToken(c, .Period, "."),
41704224 .name = try appendIdentifier(c, name),
......@@ -4173,7 +4227,7 @@ fn transCreateNodeEnumLiteral(c: *Context, name: []const u8) !*ast.Node {
41734227}
41744228
41754229fn transCreateNodeStringLiteral(c: *Context, str: []const u8) !*ast.Node {
4176 const node = try c.a().create(ast.Node.StringLiteral);
4230 const node = try c.arena.create(ast.Node.StringLiteral);
41774231 node.* = .{
41784232 .token = try appendToken(c, .StringLiteral, str),
41794233 };
......@@ -4183,7 +4237,7 @@ fn transCreateNodeStringLiteral(c: *Context, str: []const u8) !*ast.Node {
41834237fn transCreateNodeIf(c: *Context) !*ast.Node.If {
41844238 const if_tok = try appendToken(c, .Keyword_if, "if");
41854239 _ = try appendToken(c, .LParen, "(");
4186 const node = try c.a().create(ast.Node.If);
4240 const node = try c.arena.create(ast.Node.If);
41874241 node.* = .{
41884242 .if_token = if_tok,
41894243 .condition = undefined,
......@@ -4195,7 +4249,7 @@ fn transCreateNodeIf(c: *Context) !*ast.Node.If {
41954249}
41964250
41974251fn transCreateNodeElse(c: *Context) !*ast.Node.Else {
4198 const node = try c.a().create(ast.Node.Else);
4252 const node = try c.arena.create(ast.Node.Else);
41994253 node.* = .{
42004254 .else_token = try appendToken(c, .Keyword_else, "else"),
42014255 .payload = null,
......@@ -4210,11 +4264,11 @@ fn transCreateNodeBlock(c: *Context, label: ?[]const u8) !*ast.Node.Block {
42104264 _ = try appendToken(c, .Colon, ":");
42114265 break :blk ll;
42124266 } else null;
4213 const block_node = try c.a().create(ast.Node.Block);
4267 const block_node = try c.arena.create(ast.Node.Block);
42144268 block_node.* = .{
42154269 .label = label_node,
42164270 .lbrace = try appendToken(c, .LBrace, "{"),
4217 .statements = ast.Node.Block.StatementList.init(c.a()),
4271 .statements = ast.Node.Block.StatementList{},
42184272 .rbrace = undefined,
42194273 };
42204274 return block_node;
......@@ -4226,7 +4280,7 @@ fn transCreateNodeBreak(c: *Context, label: ?[]const u8) !*ast.Node.ControlFlowE
42264280 _ = try appendToken(c, .Colon, ":");
42274281 break :blk try transCreateNodeIdentifier(c, l);
42284282 } else null;
4229 const node = try c.a().create(ast.Node.ControlFlowExpression);
4283 const node = try c.arena.create(ast.Node.ControlFlowExpression);
42304284 node.* = .{
42314285 .ltoken = ltoken,
42324286 .kind = .{ .Break = label_node },
......@@ -4240,7 +4294,7 @@ fn transCreateNodeVarDecl(c: *Context, is_pub: bool, is_const: bool, name: []con
42404294 const mut_tok = if (is_const) try appendToken(c, .Keyword_const, "const") else try appendToken(c, .Keyword_var, "var");
42414295 const name_tok = try appendIdentifier(c, name);
42424296
4243 const node = try c.a().create(ast.Node.VarDecl);
4297 const node = try c.arena.create(ast.Node.VarDecl);
42444298 node.* = .{
42454299 .doc_comments = null,
42464300 .visib_token = visib_tok,
......@@ -4264,7 +4318,7 @@ fn transCreateNodeWhile(c: *Context) !*ast.Node.While {
42644318 const while_tok = try appendToken(c, .Keyword_while, "while");
42654319 _ = try appendToken(c, .LParen, "(");
42664320
4267 const node = try c.a().create(ast.Node.While);
4321 const node = try c.arena.create(ast.Node.While);
42684322 node.* = .{
42694323 .label = null,
42704324 .inline_token = null,
......@@ -4280,7 +4334,7 @@ fn transCreateNodeWhile(c: *Context) !*ast.Node.While {
42804334
42814335fn transCreateNodeContinue(c: *Context) !*ast.Node {
42824336 const ltoken = try appendToken(c, .Keyword_continue, "continue");
4283 const node = try c.a().create(ast.Node.ControlFlowExpression);
4337 const node = try c.arena.create(ast.Node.ControlFlowExpression);
42844338 node.* = .{
42854339 .ltoken = ltoken,
42864340 .kind = .{ .Continue = null },
......@@ -4294,11 +4348,11 @@ fn transCreateNodeSwitch(c: *Context) !*ast.Node.Switch {
42944348 const switch_tok = try appendToken(c, .Keyword_switch, "switch");
42954349 _ = try appendToken(c, .LParen, "(");
42964350
4297 const node = try c.a().create(ast.Node.Switch);
4351 const node = try c.arena.create(ast.Node.Switch);
42984352 node.* = .{
42994353 .switch_token = switch_tok,
43004354 .expr = undefined,
4301 .cases = ast.Node.Switch.CaseList.init(c.a()),
4355 .cases = ast.Node.Switch.CaseList{},
43024356 .rbrace = undefined,
43034357 };
43044358 return node;
......@@ -4307,9 +4361,9 @@ fn transCreateNodeSwitch(c: *Context) !*ast.Node.Switch {
43074361fn transCreateNodeSwitchCase(c: *Context, lhs: *ast.Node) !*ast.Node.SwitchCase {
43084362 const arrow_tok = try appendToken(c, .EqualAngleBracketRight, "=>");
43094363
4310 const node = try c.a().create(ast.Node.SwitchCase);
4364 const node = try c.arena.create(ast.Node.SwitchCase);
43114365 node.* = .{
4312 .items = ast.Node.SwitchCase.ItemList.init(c.a()),
4366 .items = ast.Node.SwitchCase.ItemList{},
43134367 .arrow_token = arrow_tok,
43144368 .payload = null,
43154369 .expr = undefined,
......@@ -4319,7 +4373,7 @@ fn transCreateNodeSwitchCase(c: *Context, lhs: *ast.Node) !*ast.Node.SwitchCase
43194373}
43204374
43214375fn transCreateNodeSwitchElse(c: *Context) !*ast.Node {
4322 const node = try c.a().create(ast.Node.SwitchElse);
4376 const node = try c.arena.create(ast.Node.SwitchElse);
43234377 node.* = .{
43244378 .token = try appendToken(c, .Keyword_else, "else"),
43254379 };
......@@ -4352,7 +4406,7 @@ fn transCreateNodeShiftOp(
43524406 try cast_node.params.push(rhs);
43534407 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
43544408
4355 const node = try rp.c.a().create(ast.Node.InfixOp);
4409 const node = try rp.c.arena.create(ast.Node.InfixOp);
43564410 node.* = .{
43574411 .op_token = op_token,
43584412 .lhs = lhs,
......@@ -4364,7 +4418,7 @@ fn transCreateNodeShiftOp(
43644418}
43654419
43664420fn transCreateNodePtrDeref(c: *Context, lhs: *ast.Node) !*ast.Node {
4367 const node = try c.a().create(ast.Node.SuffixOp);
4421 const node = try c.arena.create(ast.Node.SuffixOp);
43684422 node.* = .{
43694423 .lhs = .{ .node = lhs },
43704424 .op = .Deref,
......@@ -4375,7 +4429,7 @@ fn transCreateNodePtrDeref(c: *Context, lhs: *ast.Node) !*ast.Node {
43754429
43764430fn transCreateNodeArrayAccess(c: *Context, lhs: *ast.Node) !*ast.Node.SuffixOp {
43774431 _ = try appendToken(c, .LBrace, "[");
4378 const node = try c.a().create(ast.Node.SuffixOp);
4432 const node = try c.arena.create(ast.Node.SuffixOp);
43794433 node.* = .{
43804434 .lhs = .{ .node = lhs },
43814435 .op = .{
......@@ -4392,7 +4446,7 @@ const RestorePoint = struct {
43924446 src_buf_index: usize,
43934447
43944448 fn activate(self: RestorePoint) void {
4395 self.c.tree.tokens.shrink(self.token_index);
4449 self.c.tokens.shrink(self.c.gpa, self.token_index);
43964450 self.c.source_buffer.shrink(self.src_buf_index);
43974451 }
43984452};
......@@ -4400,7 +4454,7 @@ const RestorePoint = struct {
44004454fn makeRestorePoint(c: *Context) RestorePoint {
44014455 return RestorePoint{
44024456 .c = c,
4403 .token_index = c.tree.tokens.len,
4457 .token_index = c.tokens.items.len,
44044458 .src_buf_index = c.source_buffer.items.len,
44054459 };
44064460}
......@@ -4647,7 +4701,8 @@ fn finishTransFnProto(
46474701 const name_tok = if (fn_decl_context) |ctx| try appendIdentifier(rp.c, ctx.fn_name) else null;
46484702 const lparen_tok = try appendToken(rp.c, .LParen, "(");
46494703
4650 var fn_params = ast.Node.FnProto.ParamList.init(rp.c.a());
4704 var fn_params = ast.Node.FnProto.ParamList{};
4705 var fn_params_list = rp.c.llpusher(&fn_params);
46514706 const param_count: usize = if (fn_proto_ty != null) ZigClangFunctionProtoType_getNumParams(fn_proto_ty.?) else 0;
46524707
46534708 var i: usize = 0;
......@@ -4672,7 +4727,7 @@ fn finishTransFnProto(
46724727
46734728 const type_node = try transQualType(rp, param_qt, source_loc);
46744729
4675 const param_node = try rp.c.a().create(ast.Node.ParamDecl);
4730 const param_node = try rp.c.arena.create(ast.Node.ParamDecl);
46764731 param_node.* = .{
46774732 .doc_comments = null,
46784733 .comptime_token = null,
......@@ -4680,7 +4735,7 @@ fn finishTransFnProto(
46804735 .name_token = param_name_tok,
46814736 .param_type = .{ .type_expr = type_node },
46824737 };
4683 try fn_params.push(&param_node.base);
4738 try fn_params_list.push(&param_node.base);
46844739
46854740 if (i + 1 < param_count) {
46864741 _ = try appendToken(rp.c, .Comma, ",");
......@@ -4692,7 +4747,7 @@ fn finishTransFnProto(
46924747 _ = try appendToken(rp.c, .Comma, ",");
46934748 }
46944749
4695 const var_arg_node = try rp.c.a().create(ast.Node.ParamDecl);
4750 const var_arg_node = try rp.c.arena.create(ast.Node.ParamDecl);
46964751 var_arg_node.* = .{
46974752 .doc_comments = null,
46984753 .comptime_token = null,
......@@ -4700,7 +4755,7 @@ fn finishTransFnProto(
47004755 .name_token = null,
47014756 .param_type = .{ .var_args = try appendToken(rp.c, .Ellipsis3, "...") }
47024757 };
4703 try fn_params.push(&var_arg_node.base);
4758 try fn_params_list.push(&var_arg_node.base);
47044759 }
47054760
47064761 const rparen_tok = try appendToken(rp.c, .RParen, ")");
......@@ -4713,7 +4768,7 @@ fn finishTransFnProto(
47134768 _ = try appendToken(rp.c, .LParen, "(");
47144769 const expr = try transCreateNodeStringLiteral(
47154770 rp.c,
4716 try std.fmt.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
4771 try std.fmt.allocPrint(rp.c.arena, "\"{}\"", .{str_ptr[0..str_len]}),
47174772 );
47184773 _ = try appendToken(rp.c, .RParen, ")");
47194774
......@@ -4767,7 +4822,7 @@ fn finishTransFnProto(
47674822 }
47684823 };
47694824
4770 const fn_proto = try rp.c.a().create(ast.Node.FnProto);
4825 const fn_proto = try rp.c.arena.create(ast.Node.FnProto);
47714826 fn_proto.* = .{
47724827 .doc_comments = null,
47734828 .visib_token = pub_tok,
......@@ -4816,20 +4871,21 @@ pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comp
48164871 const semi_tok = try appendToken(c, .Semicolon, ";");
48174872 _ = try appendTokenFmt(c, .LineComment, "// {}", .{c.locStr(loc)});
48184873
4819 const msg_node = try c.a().create(ast.Node.StringLiteral);
4874 const msg_node = try c.arena.create(ast.Node.StringLiteral);
48204875 msg_node.* = .{
48214876 .token = msg_tok,
48224877 };
48234878
4824 const call_node = try c.a().create(ast.Node.BuiltinCall);
4879 const call_node = try c.arena.create(ast.Node.BuiltinCall);
48254880 call_node.* = .{
48264881 .builtin_token = builtin_tok,
4827 .params = ast.Node.BuiltinCall.ParamList.init(c.a()),
4882 .params = ast.Node.BuiltinCall.ParamList{},
48284883 .rparen_token = rparen_tok,
48294884 };
4830 try call_node.params.push(&msg_node.base);
4885 var call_params_it = &call_node.params.first;
4886 call_params_it = try c.llpush(*ast.Node, call_params_it, &msg_node.base);
48314887
4832 const var_decl_node = try c.a().create(ast.Node.VarDecl);
4888 const var_decl_node = try c.arena.create(ast.Node.VarDecl);
48334889 var_decl_node.* = .{
48344890 .doc_comments = null,
48354891 .visib_token = pub_tok,
......@@ -4861,9 +4917,9 @@ fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8,
48614917
48624918 try c.source_buffer.outStream().print(format, args);
48634919 const end_index = c.source_buffer.items.len;
4864 const token_index = c.tree.tokens.len;
4865 const new_token = try c.tree.tokens.addOne();
4866 errdefer c.tree.tokens.shrink(token_index);
4920 const token_index = c.tokens.items.len;
4921 const new_token = try c.tokens.addOne(c.gpa);
4922 errdefer c.tokens.shrink(c.gpa, token_index);
48674923
48684924 new_token.* = .{
48694925 .id = token_id,
......@@ -4931,7 +4987,7 @@ fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex {
49314987
49324988fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {
49334989 const token_index = try appendIdentifier(c, name);
4934 const identifier = try c.a().create(ast.Node.Identifier);
4990 const identifier = try c.arena.create(ast.Node.Identifier);
49354991 identifier.* = .{
49364992 .token = token_index,
49374993 };
......@@ -4940,7 +4996,7 @@ fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {
49404996
49414997fn transCreateNodeIdentifierUnchecked(c: *Context, name: []const u8) !*ast.Node {
49424998 const token_index = try appendTokenFmt(c, .Identifier, "{}", .{name});
4943 const identifier = try c.a().create(ast.Node.Identifier);
4999 const identifier = try c.arena.create(ast.Node.Identifier);
49445000 identifier.* = .{
49455001 .token = token_index,
49465002 };
......@@ -4955,7 +5011,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
49555011 // TODO if we see #undef, delete it from the table
49565012 var it = ZigClangASTUnit_getLocalPreprocessingEntities_begin(unit);
49575013 const it_end = ZigClangASTUnit_getLocalPreprocessingEntities_end(unit);
4958 var tok_list = CTokenList.init(c.a());
5014 var tok_list = CTokenList.init(c.arena);
49595015 const scope = c.global_scope;
49605016
49615017 while (it.I != it_end.I) : (it.I += 1) {
......@@ -4970,7 +5026,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
49705026 const name = try c.str(raw_name);
49715027 // TODO https://github.com/ziglang/zig/issues/3756
49725028 // TODO https://github.com/ziglang/zig/issues/1802
4973 const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{ name, c.getMangle() }) else name;
5029 const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.arena, "{}_{}", .{ name, c.getMangle() }) else name;
49745030 if (scope.containsNow(mangled_name)) {
49755031 continue;
49765032 }
......@@ -5078,7 +5134,8 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
50785134 .{},
50795135 );
50805136 }
5081 var fn_params = ast.Node.FnProto.ParamList.init(c.a());
5137 var fn_params = ast.Node.FnProto.ParamList{};
5138 var fn_params_it = &fn_params.first;
50825139 while (true) {
50835140 const param_tok = it.next().?;
50845141 if (param_tok.id != .Identifier) {
......@@ -5096,12 +5153,12 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
50965153 _ = try appendToken(c, .Colon, ":");
50975154
50985155 const token_index = try appendToken(c, .Keyword_var, "var");
5099 const identifier = try c.a().create(ast.Node.Identifier);
5156 const identifier = try c.arena.create(ast.Node.Identifier);
51005157 identifier.* = .{
51015158 .token = token_index,
51025159 };
51035160
5104 const param_node = try c.a().create(ast.Node.ParamDecl);
5161 const param_node = try c.arena.create(ast.Node.ParamDecl);
51055162 param_node.* = .{
51065163 .doc_comments = null,
51075164 .comptime_token = null,
......@@ -5109,7 +5166,7 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
51095166 .name_token = param_name_tok,
51105167 .param_type = .{ .type_expr = &identifier.base },
51115168 };
5112 try fn_params.push(&param_node.base);
5169 fn_params_it = try c.llpush(*ast.Node, fn_params_it, &param_node.base);
51135170
51145171 if (it.peek().?.id != .Comma)
51155172 break;
......@@ -5131,8 +5188,9 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
51315188
51325189 const type_of = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
51335190 type_of.rparen_token = try appendToken(c, .RParen, ")");
5191 var type_of_params = c.llpusher(&type_of.params);
51345192
5135 const fn_proto = try c.a().create(ast.Node.FnProto);
5193 const fn_proto = try c.arena.create(ast.Node.FnProto);
51365194 fn_proto.* = .{
51375195 .visib_token = pub_tok,
51385196 .extern_export_inline_token = inline_tok,
......@@ -5150,6 +5208,7 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
51505208 };
51515209
51525210 const block = try transCreateNodeBlock(c, null);
5211 var block_statements = c.llpusher(&block.statements);
51535212
51545213 const return_expr = try transCreateNodeReturnExpr(c);
51555214 const expr = try parseCExpr(c, it, source, source_loc, scope);
......@@ -5165,16 +5224,16 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
51655224 _ = try appendToken(c, .Semicolon, ";");
51665225 const type_of_arg = if (expr.id != .Block) expr else blk: {
51675226 const blk = @fieldParentPtr(ast.Node.Block, "base", expr);
5168 const blk_last = blk.statements.at(blk.statements.len - 1).*;
5227 const blk_last = blk.statements.first.?.findLast().data;
51695228 std.debug.assert(blk_last.id == .ControlFlowExpression);
51705229 const br = @fieldParentPtr(ast.Node.ControlFlowExpression, "base", blk_last);
51715230 break :blk br.rhs.?;
51725231 };
5173 try type_of.params.push(type_of_arg);
5232 try type_of_params.push(type_of_arg);
51745233 return_expr.rhs = expr;
51755234
51765235 block.rbrace = try appendToken(c, .RBrace, "}");
5177 try block.statements.push(&return_expr.base);
5236 try block_statements.push(&return_expr.base);
51785237 fn_proto.body_node = &block.base;
51795238 _ = try c.global_scope.macro_table.put(name, &fn_proto.base);
51805239}
......@@ -5208,14 +5267,14 @@ fn parseCExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, source_
52085267 .Comma => {
52095268 _ = try appendToken(c, .Semicolon, ";");
52105269 const block_scope = try Scope.Block.init(c, scope, "blk");
5211 block_scope.block_node = try transCreateNodeBlock(c, block_scope.label);
5270 block_scope.setBlockNode(try transCreateNodeBlock(c, block_scope.label));
52125271
52135272 var last = node;
52145273 while (true) {
52155274 // suppress result
52165275 const lhs = try transCreateNodeIdentifier(c, "_");
52175276 const op_token = try appendToken(c, .Equal, "=");
5218 const op_node = try c.a().create(ast.Node.InfixOp);
5277 const op_node = try c.arena.create(ast.Node.InfixOp);
52195278 op_node.* = .{
52205279 .op_token = op_token,
52215280 .lhs = lhs,
......@@ -5253,11 +5312,11 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
52535312 switch (lit_bytes[1]) {
52545313 '0'...'7' => {
52555314 // Octal
5256 lit_bytes = try std.fmt.allocPrint(c.a(), "0o{}", .{lit_bytes});
5315 lit_bytes = try std.fmt.allocPrint(c.arena, "0o{}", .{lit_bytes});
52575316 },
52585317 'X' => {
52595318 // Hexadecimal with capital X, valid in C but not in Zig
5260 lit_bytes = try std.fmt.allocPrint(c.a(), "0x{}", .{lit_bytes[2..]});
5319 lit_bytes = try std.fmt.allocPrint(c.arena, "0x{}", .{lit_bytes[2..]});
52615320 },
52625321 else => {},
52635322 }
......@@ -5288,7 +5347,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
52885347 return &cast_node.base;
52895348 } else if (tok.id == .FloatLiteral) {
52905349 if (lit_bytes[0] == '.')
5291 lit_bytes = try std.fmt.allocPrint(c.a(), "0{}", .{lit_bytes});
5350 lit_bytes = try std.fmt.allocPrint(c.arena, "0{}", .{lit_bytes});
52925351 if (tok.id.FloatLiteral == .None) {
52935352 return transCreateNodeFloat(c, lit_bytes);
52945353 }
......@@ -5318,7 +5377,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
53185377 break;
53195378 }
53205379 } else return source;
5321 var bytes = try ctx.a().alloc(u8, source.len * 2);
5380 var bytes = try ctx.arena.alloc(u8, source.len * 2);
53225381 var state: enum {
53235382 Start,
53245383 Escape,
......@@ -5472,14 +5531,14 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54725531 const first_tok = it.list.at(0);
54735532 if (source[tok.start] != '\'' or source[tok.start + 1] == '\\' or tok.end - tok.start == 3) {
54745533 const token = try appendToken(c, .CharLiteral, try zigifyEscapeSequences(c, source[tok.start..tok.end], source[first_tok.start..first_tok.end], source_loc));
5475 const node = try c.a().create(ast.Node.CharLiteral);
5534 const node = try c.arena.create(ast.Node.CharLiteral);
54765535 node.* = .{
54775536 .token = token,
54785537 };
54795538 return &node.base;
54805539 } else {
54815540 const token = try appendTokenFmt(c, .IntegerLiteral, "0x{x}", .{source[tok.start+1..tok.end-1]});
5482 const node = try c.a().create(ast.Node.IntegerLiteral);
5541 const node = try c.arena.create(ast.Node.IntegerLiteral);
54835542 node.* = .{
54845543 .token = token,
54855544 };
......@@ -5489,7 +5548,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54895548 .StringLiteral => {
54905549 const first_tok = it.list.at(0);
54915550 const token = try appendToken(c, .StringLiteral, try zigifyEscapeSequences(c, source[tok.start..tok.end], source[first_tok.start..first_tok.end], source_loc));
5492 const node = try c.a().create(ast.Node.StringLiteral);
5551 const node = try c.arena.create(ast.Node.StringLiteral);
54935552 node.* = .{
54945553 .token = token,
54955554 };
......@@ -5572,7 +5631,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
55725631 const type_info_node = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
55735632 try type_info_node.params.push(inner_node);
55745633 type_info_node.rparen_token = try appendToken(c, .LParen, ")");
5575 const cmp_node = try c.a().create(ast.Node.InfixOp);
5634 const cmp_node = try c.arena.create(ast.Node.InfixOp);
55765635 cmp_node.* = .{
55775636 .op_token = try appendToken(c, .EqualEqual, "=="),
55785637 .lhs = &type_info_node.base,
......@@ -5597,7 +5656,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
55975656 as_node.rparen_token = try appendToken(c, .RParen, ")");
55985657 else_node.body = &as_node.base;
55995658
5600 const group_node = try c.a().create(ast.Node.GroupedExpression);
5659 const group_node = try c.arena.create(ast.Node.GroupedExpression);
56015660 group_node.* = .{
56025661 .lparen = lparen,
56035662 .expr = &if_node.base,
......@@ -5621,7 +5680,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56215680 type_of_1.rparen_token = try appendToken(c, .RParen, ")");
56225681 type_info_1.rparen_token = try appendToken(c, .RParen, ")");
56235682
5624 const cmp_1 = try c.a().create(ast.Node.InfixOp);
5683 const cmp_1 = try c.arena.create(ast.Node.InfixOp);
56255684 cmp_1.* = .{
56265685 .op_token = try appendToken(c, .EqualEqual, "=="),
56275686 .lhs = &type_info_1.base,
......@@ -5633,7 +5692,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56335692
56345693 const period_tok = try appendToken(c, .Period, ".");
56355694 const child_ident = try transCreateNodeIdentifier(c, "Child");
5636 const inner_node_child = try c.a().create(ast.Node.InfixOp);
5695 const inner_node_child = try c.arena.create(ast.Node.InfixOp);
56375696 inner_node_child.* = .{
56385697 .op_token = period_tok,
56395698 .lhs = inner_node,
......@@ -5669,7 +5728,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56695728 type_of_2.rparen_token = try appendToken(c, .RParen, ")");
56705729 type_info_2.rparen_token = try appendToken(c, .RParen, ")");
56715730
5672 const cmp_2 = try c.a().create(ast.Node.InfixOp);
5731 const cmp_2 = try c.arena.create(ast.Node.InfixOp);
56735732 cmp_2.* = .{
56745733 .op_token = try appendToken(c, .EqualEqual, "=="),
56755734 .lhs = &type_info_2.base,
......@@ -5677,7 +5736,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56775736 .rhs = try transCreateNodeEnumLiteral(c, "Int"),
56785737 };
56795738 if_2.condition = &cmp_2.base;
5680 const cmp_4 = try c.a().create(ast.Node.InfixOp);
5739 const cmp_4 = try c.arena.create(ast.Node.InfixOp);
56815740 cmp_4.* = .{
56825741 .op_token = try appendToken(c, .Keyword_and, "and"),
56835742 .lhs = &cmp_2.base,
......@@ -5687,7 +5746,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56875746 const type_info_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
56885747 try type_info_3.params.push(inner_node);
56895748 type_info_3.rparen_token = try appendToken(c, .LParen, ")");
5690 const cmp_3 = try c.a().create(ast.Node.InfixOp);
5749 const cmp_3 = try c.arena.create(ast.Node.InfixOp);
56915750 cmp_3.* = .{
56925751 .op_token = try appendToken(c, .EqualEqual, "=="),
56935752 .lhs = &type_info_3.base,
......@@ -5714,7 +5773,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
57145773 as.rparen_token = try appendToken(c, .RParen, ")");
57155774 else_2.body = &as.base;
57165775
5717 const group_node = try c.a().create(ast.Node.GroupedExpression);
5776 const group_node = try c.arena.create(ast.Node.GroupedExpression);
57185777 group_node.* = .{
57195778 .lparen = lparen,
57205779 .expr = &if_1.base,
......@@ -5740,7 +5799,7 @@ fn macroBoolToInt(c: *Context, node: *ast.Node) !*ast.Node {
57405799 if (!isBoolRes(node)) {
57415800 if (node.id != .InfixOp) return node;
57425801
5743 const group_node = try c.a().create(ast.Node.GroupedExpression);
5802 const group_node = try c.arena.create(ast.Node.GroupedExpression);
57445803 group_node.* = .{
57455804 .lparen = try appendToken(c, .LParen, "("),
57465805 .expr = node,
......@@ -5759,7 +5818,7 @@ fn macroIntToBool(c: *Context, node: *ast.Node) !*ast.Node {
57595818 if (isBoolRes(node)) {
57605819 if (node.id != .InfixOp) return node;
57615820
5762 const group_node = try c.a().create(ast.Node.GroupedExpression);
5821 const group_node = try c.arena.create(ast.Node.GroupedExpression);
57635822 group_node.* = .{
57645823 .lparen = try appendToken(c, .LParen, "("),
57655824 .expr = node,
......@@ -5770,14 +5829,14 @@ fn macroIntToBool(c: *Context, node: *ast.Node) !*ast.Node {
57705829
57715830 const op_token = try appendToken(c, .BangEqual, "!=");
57725831 const zero = try transCreateNodeInt(c, 0);
5773 const res = try c.a().create(ast.Node.InfixOp);
5832 const res = try c.arena.create(ast.Node.InfixOp);
57745833 res.* = .{
57755834 .op_token = op_token,
57765835 .lhs = node,
57775836 .op = .BangEqual,
57785837 .rhs = zero,
57795838 };
5780 const group_node = try c.a().create(ast.Node.GroupedExpression);
5839 const group_node = try c.arena.create(ast.Node.GroupedExpression);
57815840 group_node.* = .{
57825841 .lparen = try appendToken(c, .LParen, "("),
57835842 .expr = &res.base,
......@@ -5989,7 +6048,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
59896048 const cast_fn = if (bool_op) macroIntToBool else macroBoolToInt;
59906049 const lhs_node = try cast_fn(c, node);
59916050 const rhs_node = try parseCPrefixOpExpr(c, it, source, source_loc, scope);
5992 const op_node = try c.a().create(ast.Node.InfixOp);
6051 const op_node = try c.arena.create(ast.Node.InfixOp);
59936052 op_node.* = .{
59946053 .op_token = op_token,
59956054 .lhs = lhs_node,
......@@ -6037,7 +6096,7 @@ fn parseCPrefixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
60376096}
60386097
60396098fn tokenSlice(c: *Context, token: ast.TokenIndex) []u8 {
6040 const tok = c.tree.tokens.at(token);
6099 const tok = c.tokens.items[token];
60416100 const slice = c.source_buffer.span()[tok.start..tok.end];
60426101 return if (mem.startsWith(u8, slice, "@\""))
60436102 slice[2 .. slice.len - 1]
......@@ -6060,9 +6119,10 @@ fn getContainer(c: *Context, node: *ast.Node) ?*ast.Node {
60606119 return null;
60616120 if (getContainerTypeOf(c, infix.lhs)) |ty_node| {
60626121 if (ty_node.cast(ast.Node.ContainerDecl)) |container| {
6063 var it = container.fields_and_decls.iterator(0);
6064 while (it.next()) |field_ref| {
6065 const field = field_ref.*.cast(ast.Node.ContainerField).?;
6122 var it = container.fields_and_decls.first;
6123 while (it) |field_ref_node| : (it = field_ref_node.next) {
6124 const field_ref = field_ref_node.data;
6125 const field = field_ref.cast(ast.Node.ContainerField).?;
60666126 const ident = infix.rhs.cast(ast.Node.Identifier).?;
60676127 if (mem.eql(u8, tokenSlice(c, field.name_token), tokenSlice(c, ident.token))) {
60686128 return getContainer(c, field.type_expr.?);
......@@ -6087,9 +6147,10 @@ fn getContainerTypeOf(c: *Context, ref: *ast.Node) ?*ast.Node {
60876147 return null;
60886148 if (getContainerTypeOf(c, infix.lhs)) |ty_node| {
60896149 if (ty_node.cast(ast.Node.ContainerDecl)) |container| {
6090 var it = container.fields_and_decls.iterator(0);
6091 while (it.next()) |field_ref| {
6092 const field = field_ref.*.cast(ast.Node.ContainerField).?;
6150 var it = container.fields_and_decls.first;
6151 while (it) |field_ref_node| : (it = field_ref_node.next) {
6152 const field_ref = field_ref_node.data;
6153 const field = field_ref.cast(ast.Node.ContainerField).?;
60936154 const ident = infix.rhs.cast(ast.Node.Identifier).?;
60946155 if (mem.eql(u8, tokenSlice(c, field.name_token), tokenSlice(c, ident.token))) {
60956156 return getContainer(c, field.type_expr.?);