| ... | ... | @@ -37,10 +37,10 @@ fn addrEql(a: usize, b: usize) bool { |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | const SymbolTable = std.StringHashMap(*ast.Node); |
| 40 | | const AliasList = std.SegmentedList(struct { |
| 40 | const AliasList = std.ArrayList(struct { |
| 41 | 41 | alias: []const u8, |
| 42 | 42 | name: []const u8, |
| 43 | | }, 4); |
| 43 | }); |
| 44 | 44 | |
| 45 | 45 | const Scope = struct { |
| 46 | 46 | id: Id, |
| ... | ... | @@ -64,40 +64,50 @@ const Scope = struct { |
| 64 | 64 | const Block = struct { |
| 65 | 65 | base: Scope, |
| 66 | 66 | block_node: *ast.Node.Block, |
| 67 | statements_it: *?*std.SinglyLinkedList(*ast.Node), |
| 67 | 68 | variables: AliasList, |
| 68 | 69 | label: ?[]const u8, |
| 69 | 70 | mangle_count: u32 = 0, |
| 70 | 71 | |
| 71 | 72 | /// Don't forget to set rbrace token and block_node later |
| 72 | 73 | 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); |
| 74 | 75 | block.* = .{ |
| 75 | 76 | .base = .{ |
| 76 | 77 | .id = .Block, |
| 77 | 78 | .parent = parent, |
| 78 | 79 | }, |
| 79 | 80 | .block_node = undefined, |
| 80 | | .variables = AliasList.init(c.a()), |
| 81 | .statements_it = undefined, |
| 82 | .variables = AliasList.init(c.arena), |
| 81 | 83 | .label = label, |
| 82 | 84 | }; |
| 83 | 85 | return block; |
| 84 | 86 | } |
| 85 | 87 | |
| 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 | |
| 86 | 97 | /// Given the desired name, return a name that does not shadow anything from outer scopes. |
| 87 | 98 | /// Inserts the returned name into the scope. |
| 88 | 99 | fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { |
| 89 | 100 | var proposed_name = name; |
| 90 | 101 | while (scope.contains(proposed_name)) { |
| 91 | 102 | 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 }); |
| 93 | 104 | } |
| 94 | | try scope.variables.push(.{ .name = name, .alias = proposed_name }); |
| 105 | try scope.variables.append(.{ .name = name, .alias = proposed_name }); |
| 95 | 106 | return proposed_name; |
| 96 | 107 | } |
| 97 | 108 | |
| 98 | 109 | 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| { |
| 101 | 111 | if (mem.eql(u8, p.name, name)) |
| 102 | 112 | return p.alias; |
| 103 | 113 | } |
| ... | ... | @@ -105,8 +115,7 @@ const Scope = struct { |
| 105 | 115 | } |
| 106 | 116 | |
| 107 | 117 | 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| { |
| 110 | 119 | if (mem.eql(u8, p.name, name)) |
| 111 | 120 | return true; |
| 112 | 121 | } |
| ... | ... | @@ -132,8 +141,8 @@ const Scope = struct { |
| 132 | 141 | .id = .Root, |
| 133 | 142 | .parent = null, |
| 134 | 143 | }, |
| 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), |
| 137 | 146 | .context = c, |
| 138 | 147 | }; |
| 139 | 148 | } |
| ... | ... | @@ -208,7 +217,10 @@ const Scope = struct { |
| 208 | 217 | }; |
| 209 | 218 | |
| 210 | 219 | pub 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), |
| 212 | 224 | source_buffer: *std.ArrayList(u8), |
| 213 | 225 | err: Error, |
| 214 | 226 | source_manager: *ZigClangSourceManager, |
| ... | ... | @@ -217,6 +229,8 @@ pub const Context = struct { |
| 217 | 229 | global_scope: *Scope.Root, |
| 218 | 230 | clang_context: *ZigClangASTContext, |
| 219 | 231 | mangle_count: u32 = 0, |
| 232 | root_node: *ast.Node.Root, |
| 233 | root_decls_it: *?*std.SinglyLinkedList(*ast.Node).Node, |
| 220 | 234 | |
| 221 | 235 | /// This one is different than the root scope's name table. This contains |
| 222 | 236 | /// a list of names that we found by visiting all the top level decls without |
| ... | ... | @@ -224,18 +238,45 @@ pub const Context = struct { |
| 224 | 238 | /// up front in a pre-processing step. |
| 225 | 239 | global_names: std.StringHashMap(void), |
| 226 | 240 | |
| 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 | |
| 227 | 272 | fn getMangle(c: *Context) u32 { |
| 228 | 273 | c.mangle_count += 1; |
| 229 | 274 | return c.mangle_count; |
| 230 | 275 | } |
| 231 | 276 | |
| 232 | | fn a(c: *Context) *mem.Allocator { |
| 233 | | return &c.tree.arena_allocator.allocator; |
| 234 | | } |
| 235 | | |
| 236 | 277 | /// Convert a null-terminated C string to a slice allocated in the arena |
| 237 | 278 | 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)); |
| 239 | 280 | } |
| 240 | 281 | |
| 241 | 282 | /// Convert a clang source location to a file:line:column string |
| ... | ... | @@ -246,12 +287,12 @@ pub const Context = struct { |
| 246 | 287 | |
| 247 | 288 | const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc); |
| 248 | 289 | 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 }); |
| 250 | 291 | } |
| 251 | 292 | }; |
| 252 | 293 | |
| 253 | 294 | pub fn translate( |
| 254 | | backing_allocator: *mem.Allocator, |
| 295 | gpa: *mem.Allocator, |
| 255 | 296 | args_begin: [*]?[*]const u8, |
| 256 | 297 | args_end: [*]?[*]const u8, |
| 257 | 298 | errors: *[]ClangErrMsg, |
| ... | ... | @@ -269,47 +310,43 @@ pub fn translate( |
| 269 | 310 | }; |
| 270 | 311 | defer ZigClangASTUnit_delete(ast_unit); |
| 271 | 312 | |
| 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{}, |
| 295 | 324 | // initialized with the eof token at the end |
| 296 | 325 | .eof_token = undefined, |
| 297 | 326 | }; |
| 298 | 327 | |
| 299 | | var source_buffer = std.ArrayList(u8).init(arena); |
| 300 | | |
| 301 | 328 | var context = Context{ |
| 302 | | .tree = tree, |
| 329 | .gpa = gpa, |
| 330 | .arena = &arena.allocator, |
| 303 | 331 | .source_buffer = &source_buffer, |
| 304 | 332 | .source_manager = ZigClangASTUnit_getSourceManager(ast_unit), |
| 305 | 333 | .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), |
| 309 | 337 | .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, |
| 311 | 343 | }; |
| 312 | 344 | 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(); |
| 313 | 350 | |
| 314 | 351 | try prepopulateGlobalNameTable(ast_unit, &context); |
| 315 | 352 | |
| ... | ... | @@ -320,23 +357,30 @@ pub fn translate( |
| 320 | 357 | try transPreprocessorEntities(&context, ast_unit); |
| 321 | 358 | |
| 322 | 359 | try addMacros(&context); |
| 323 | | var it = context.alias_list.iterator(0); |
| 324 | | while (it.next()) |alias| { |
| 360 | for (context.alias_list.items) |alias| { |
| 325 | 361 | if (!context.global_scope.sym_table.contains(alias.alias)) { |
| 326 | 362 | try createAlias(&context, alias); |
| 327 | 363 | } |
| 328 | 364 | } |
| 329 | 365 | |
| 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, ""); |
| 332 | 367 | 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| { |
| 337 | 370 | std.debug.warn("{}\n", .{token}); |
| 338 | 371 | } |
| 339 | 372 | } |
| 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 | }; |
| 340 | 384 | return tree; |
| 341 | 385 | } |
| 342 | 386 | |
| ... | ... | @@ -493,17 +537,19 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 493 | 537 | const block_scope = try Scope.Block.init(rp.c, &c.global_scope.base, null); |
| 494 | 538 | var scope = &block_scope.base; |
| 495 | 539 | const block_node = try transCreateNodeBlock(rp.c, null); |
| 496 | | block_scope.block_node = block_node; |
| 540 | block_scope.setBlockNode(block_node); |
| 497 | 541 | |
| 498 | | var it = proto_node.params.iterator(0); |
| 542 | var it = proto_node.params.first; |
| 499 | 543 | 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); |
| 502 | 548 | const param_name = if (param.name_token) |name_tok| |
| 503 | 549 | tokenSlice(c, name_tok) |
| 504 | 550 | 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; |
| 507 | 553 | break; |
| 508 | 554 | } else |
| 509 | 555 | 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 { |
| 516 | 562 | |
| 517 | 563 | const arg_name = blk: { |
| 518 | 564 | 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 }); |
| 520 | 566 | break :blk try block_scope.makeMangledName(c, bare_arg_name); |
| 521 | 567 | }; |
| 522 | 568 | |
| ... | ... | @@ -525,7 +571,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 525 | 571 | node.eq_token = try appendToken(c, .Equal, "="); |
| 526 | 572 | node.init_node = try transCreateNodeIdentifier(c, arg_name); |
| 527 | 573 | 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); |
| 529 | 575 | param.name_token = try appendIdentifier(c, arg_name); |
| 530 | 576 | _ = try appendToken(c, .Colon, ":"); |
| 531 | 577 | } |
| ... | ... | @@ -560,7 +606,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 560 | 606 | |
| 561 | 607 | // TODO https://github.com/ziglang/zig/issues/3756 |
| 562 | 608 | // 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; |
| 564 | 610 | const var_decl_loc = ZigClangVarDecl_getLocation(var_decl); |
| 565 | 611 | |
| 566 | 612 | const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); |
| ... | ... | @@ -620,7 +666,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 620 | 666 | _ = try appendToken(rp.c, .LParen, "("); |
| 621 | 667 | const expr = try transCreateNodeStringLiteral( |
| 622 | 668 | 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]}), |
| 624 | 670 | ); |
| 625 | 671 | _ = try appendToken(rp.c, .RParen, ")"); |
| 626 | 672 | |
| ... | ... | @@ -643,7 +689,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 643 | 689 | break :blk null; |
| 644 | 690 | }; |
| 645 | 691 | |
| 646 | | const node = try c.a().create(ast.Node.VarDecl); |
| 692 | const node = try c.arena.create(ast.Node.VarDecl); |
| 647 | 693 | node.* = .{ |
| 648 | 694 | .doc_comments = null, |
| 649 | 695 | .visib_token = visib_tok, |
| ... | ... | @@ -702,7 +748,7 @@ fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, top_l |
| 702 | 748 | |
| 703 | 749 | // TODO https://github.com/ziglang/zig/issues/3756 |
| 704 | 750 | // 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; |
| 706 | 752 | if (checkForBuiltinTypedef(checked_name)) |builtin| { |
| 707 | 753 | return transTypeDefAsBuiltin(c, typedef_decl, builtin); |
| 708 | 754 | } |
| ... | ... | @@ -745,7 +791,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* |
| 745 | 791 | // Record declarations such as `struct {...} x` have no name but they're not |
| 746 | 792 | // anonymous hence here isAnonymousStructOrUnion is not needed |
| 747 | 793 | 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()}); |
| 749 | 795 | is_unnamed = true; |
| 750 | 796 | } |
| 751 | 797 | |
| ... | ... | @@ -762,7 +808,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* |
| 762 | 808 | return null; |
| 763 | 809 | } |
| 764 | 810 | |
| 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 }); |
| 766 | 812 | _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name); |
| 767 | 813 | |
| 768 | 814 | const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name); |
| ... | ... | @@ -785,15 +831,16 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* |
| 785 | 831 | const container_tok = try appendToken(c, container_kind, container_kind_name); |
| 786 | 832 | const lbrace_token = try appendToken(c, .LBrace, "{"); |
| 787 | 833 | |
| 788 | | const container_node = try c.a().create(ast.Node.ContainerDecl); |
| 834 | const container_node = try c.arena.create(ast.Node.ContainerDecl); |
| 789 | 835 | container_node.* = .{ |
| 790 | 836 | .layout_token = layout_tok, |
| 791 | 837 | .kind_token = container_tok, |
| 792 | 838 | .init_arg_expr = .None, |
| 793 | | .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()), |
| 839 | .fields_and_decls = ast.Node.ContainerDecl.DeclList{}, |
| 794 | 840 | .lbrace_token = lbrace_token, |
| 795 | 841 | .rbrace_token = undefined, |
| 796 | 842 | }; |
| 843 | var container_fields_and_decls = c.llpusher(&container_node.fields_and_decls); |
| 797 | 844 | |
| 798 | 845 | var unnamed_field_count: u32 = 0; |
| 799 | 846 | var it = ZigClangRecordDecl_field_begin(record_def); |
| ... | ... | @@ -821,7 +868,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* |
| 821 | 868 | var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl))); |
| 822 | 869 | if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl) or raw_name.len == 0) { |
| 823 | 870 | // 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}); |
| 825 | 872 | unnamed_field_count += 1; |
| 826 | 873 | is_anon = true; |
| 827 | 874 | } |
| ... | ... | @@ -851,7 +898,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* |
| 851 | 898 | break :blk null; |
| 852 | 899 | }; |
| 853 | 900 | |
| 854 | | const field_node = try c.a().create(ast.Node.ContainerField); |
| 901 | const field_node = try c.arena.create(ast.Node.ContainerField); |
| 855 | 902 | field_node.* = .{ |
| 856 | 903 | .doc_comments = null, |
| 857 | 904 | .comptime_token = null, |
| ... | ... | @@ -868,7 +915,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?* |
| 868 | 915 | ); |
| 869 | 916 | } |
| 870 | 917 | |
| 871 | | try container_node.fields_and_decls.push(&field_node.base); |
| 918 | try container_fields_and_decls.push(&field_node.base); |
| 872 | 919 | _ = try appendToken(c, .Comma, ","); |
| 873 | 920 | } |
| 874 | 921 | container_node.rbrace_token = try appendToken(c, .RBrace, "}"); |
| ... | ... | @@ -892,11 +939,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No |
| 892 | 939 | var bare_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, enum_decl))); |
| 893 | 940 | var is_unnamed = false; |
| 894 | 941 | 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()}); |
| 896 | 943 | is_unnamed = true; |
| 897 | 944 | } |
| 898 | 945 | |
| 899 | | const name = try std.fmt.allocPrint(c.a(), "enum_{}", .{bare_name}); |
| 946 | const name = try std.fmt.allocPrint(c.arena, "enum_{}", .{bare_name}); |
| 900 | 947 | _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name); |
| 901 | 948 | const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name); |
| 902 | 949 | node.eq_token = try appendToken(c, .Equal, "="); |
| ... | ... | @@ -916,15 +963,16 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No |
| 916 | 963 | const extern_tok = try appendToken(c, .Keyword_extern, "extern"); |
| 917 | 964 | const container_tok = try appendToken(c, .Keyword_enum, "enum"); |
| 918 | 965 | |
| 919 | | const container_node = try c.a().create(ast.Node.ContainerDecl); |
| 966 | const container_node = try c.arena.create(ast.Node.ContainerDecl); |
| 920 | 967 | container_node.* = .{ |
| 921 | 968 | .layout_token = extern_tok, |
| 922 | 969 | .kind_token = container_tok, |
| 923 | 970 | .init_arg_expr = .None, |
| 924 | | .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()), |
| 971 | .fields_and_decls = ast.Node.ContainerDecl.DeclList{}, |
| 925 | 972 | .lbrace_token = undefined, |
| 926 | 973 | .rbrace_token = undefined, |
| 927 | 974 | }; |
| 975 | var container_node_fields_and_decls = c.llpusher(&container_node.fields_and_decls); |
| 928 | 976 | |
| 929 | 977 | const int_type = ZigClangEnumDecl_getIntegerType(enum_decl); |
| 930 | 978 | // 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 |
| 971 | 1019 | } else |
| 972 | 1020 | null; |
| 973 | 1021 | |
| 974 | | const field_node = try c.a().create(ast.Node.ContainerField); |
| 1022 | const field_node = try c.arena.create(ast.Node.ContainerField); |
| 975 | 1023 | field_node.* = .{ |
| 976 | 1024 | .doc_comments = null, |
| 977 | 1025 | .comptime_token = null, |
| ... | ... | @@ -981,7 +1029,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No |
| 981 | 1029 | .align_expr = null, |
| 982 | 1030 | }; |
| 983 | 1031 | |
| 984 | | try container_node.fields_and_decls.push(&field_node.base); |
| 1032 | try container_node_fields_and_decls.push(&field_node.base); |
| 985 | 1033 | _ = try appendToken(c, .Comma, ","); |
| 986 | 1034 | |
| 987 | 1035 | // 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 |
| 992 | 1040 | const enum_ident = try transCreateNodeIdentifier(c, name); |
| 993 | 1041 | const period_tok = try appendToken(c, .Period, "."); |
| 994 | 1042 | 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); |
| 996 | 1044 | field_access_node.* = .{ |
| 997 | 1045 | .op_token = period_tok, |
| 998 | 1046 | .lhs = enum_ident, |
| ... | ... | @@ -1006,7 +1054,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No |
| 1006 | 1054 | try addTopLevelDecl(c, field_name, &tld_node.base); |
| 1007 | 1055 | } |
| 1008 | 1056 | // 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); |
| 1010 | 1058 | field_node.* = .{ |
| 1011 | 1059 | .doc_comments = null, |
| 1012 | 1060 | .comptime_token = null, |
| ... | ... | @@ -1016,7 +1064,7 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No |
| 1016 | 1064 | .align_expr = null, |
| 1017 | 1065 | }; |
| 1018 | 1066 | |
| 1019 | | try container_node.fields_and_decls.push(&field_node.base); |
| 1067 | try container_node_fields_and_decls.push(&field_node.base); |
| 1020 | 1068 | _ = try appendToken(c, .Comma, ","); |
| 1021 | 1069 | container_node.rbrace_token = try appendToken(c, .RBrace, "}"); |
| 1022 | 1070 | |
| ... | ... | @@ -1071,7 +1119,7 @@ fn transStmt( |
| 1071 | 1119 | .ParenExprClass => { |
| 1072 | 1120 | const expr = try transExpr(rp, scope, ZigClangParenExpr_getSubExpr(@ptrCast(*const ZigClangParenExpr, stmt)), .used, lrvalue); |
| 1073 | 1121 | 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); |
| 1075 | 1123 | node.* = .{ |
| 1076 | 1124 | .lparen = try appendToken(rp.c, .LParen, "("), |
| 1077 | 1125 | .expr = expr, |
| ... | ... | @@ -1116,7 +1164,7 @@ fn transStmt( |
| 1116 | 1164 | const source_expr = ZigClangOpaqueValueExpr_getSourceExpr(@ptrCast(*const ZigClangOpaqueValueExpr, stmt)).?; |
| 1117 | 1165 | const expr = try transExpr(rp, scope, source_expr, .used, lrvalue); |
| 1118 | 1166 | 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); |
| 1120 | 1168 | node.* = .{ |
| 1121 | 1169 | .lparen = try appendToken(rp.c, .LParen, "("), |
| 1122 | 1170 | .expr = expr, |
| ... | ... | @@ -1147,18 +1195,18 @@ fn transBinaryOperator( |
| 1147 | 1195 | var op_token: ast.TokenIndex = undefined; |
| 1148 | 1196 | var op_id: ast.Node.InfixOp.Op = undefined; |
| 1149 | 1197 | 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)), |
| 1151 | 1199 | .Comma => { |
| 1152 | 1200 | const block_scope = try scope.findBlockScope(rp.c); |
| 1153 | 1201 | const expr = block_scope.base.parent == scope; |
| 1154 | 1202 | const lparen = if (expr) blk: { |
| 1155 | 1203 | 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)); |
| 1157 | 1205 | break :blk l; |
| 1158 | 1206 | } else undefined; |
| 1159 | 1207 | |
| 1160 | 1208 | 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); |
| 1162 | 1210 | |
| 1163 | 1211 | const rhs = try transExpr(rp, &block_scope.base, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); |
| 1164 | 1212 | if (expr) { |
| ... | ... | @@ -1168,7 +1216,7 @@ fn transBinaryOperator( |
| 1168 | 1216 | try block_scope.block_node.statements.push(&break_node.base); |
| 1169 | 1217 | block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); |
| 1170 | 1218 | 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); |
| 1172 | 1220 | grouped_expr.* = .{ |
| 1173 | 1221 | .lparen = lparen, |
| 1174 | 1222 | .expr = &block_scope.block_node.base, |
| ... | ... | @@ -1335,7 +1383,7 @@ fn transCompoundStmtInline( |
| 1335 | 1383 | |
| 1336 | 1384 | fn transCompoundStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundStmt) TransError!*ast.Node { |
| 1337 | 1385 | 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)); |
| 1339 | 1387 | try transCompoundStmtInline(rp, &block_scope.base, stmt, block_scope.block_node); |
| 1340 | 1388 | block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); |
| 1341 | 1389 | return &block_scope.block_node.base; |
| ... | ... | @@ -1458,7 +1506,7 @@ fn transImplicitCastExpr( |
| 1458 | 1506 | switch (ZigClangImplicitCastExpr_getCastKind(expr)) { |
| 1459 | 1507 | .BitCast, .FloatingCast, .FloatingToIntegral, .IntegralToFloating, .IntegralCast, .PointerToIntegral, .IntegralToPointer => { |
| 1460 | 1508 | 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); |
| 1462 | 1510 | }, |
| 1463 | 1511 | .LValueToRValue, .NoOp, .FunctionToPointerDecay => { |
| 1464 | 1512 | const sub_expr_node = try transExpr(rp, scope, sub_expr, .used, .r_value); |
| ... | ... | @@ -1539,7 +1587,7 @@ fn transBoolExpr( |
| 1539 | 1587 | |
| 1540 | 1588 | if (grouped) { |
| 1541 | 1589 | 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); |
| 1543 | 1591 | grouped_expr.* = .{ |
| 1544 | 1592 | .lparen = lparen, |
| 1545 | 1593 | .expr = node, |
| ... | ... | @@ -1747,13 +1795,13 @@ fn transStringLiteral( |
| 1747 | 1795 | len = 0; |
| 1748 | 1796 | for (str) |c| len += escapeChar(c, &char_buf).len; |
| 1749 | 1797 | |
| 1750 | | const buf = try rp.c.a().alloc(u8, len + "\"\"".len); |
| 1798 | const buf = try rp.c.arena.alloc(u8, len + "\"\"".len); |
| 1751 | 1799 | buf[0] = '"'; |
| 1752 | 1800 | writeEscapedString(buf[1..], str); |
| 1753 | 1801 | buf[buf.len - 1] = '"'; |
| 1754 | 1802 | |
| 1755 | 1803 | 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); |
| 1757 | 1805 | node.* = .{ |
| 1758 | 1806 | .token = token, |
| 1759 | 1807 | }; |
| ... | ... | @@ -2038,13 +2086,13 @@ fn transInitListExprRecord( |
| 2038 | 2086 | var raw_name = try rp.c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl))); |
| 2039 | 2087 | if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) { |
| 2040 | 2088 | 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); |
| 2042 | 2090 | } |
| 2043 | 2091 | const field_name_tok = try appendIdentifier(rp.c, raw_name); |
| 2044 | 2092 | |
| 2045 | 2093 | _ = try appendToken(rp.c, .Equal, "="); |
| 2046 | 2094 | |
| 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); |
| 2048 | 2096 | field_init_node.* = .{ |
| 2049 | 2097 | .period_token = period_tok, |
| 2050 | 2098 | .name_token = field_name_tok, |
| ... | ... | @@ -2133,7 +2181,7 @@ fn transInitListExprArray( |
| 2133 | 2181 | &filler_init_node.base |
| 2134 | 2182 | else blk: { |
| 2135 | 2183 | 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); |
| 2137 | 2185 | mul_node.* = .{ |
| 2138 | 2186 | .op_token = mul_tok, |
| 2139 | 2187 | .lhs = &filler_init_node.base, |
| ... | ... | @@ -2147,7 +2195,7 @@ fn transInitListExprArray( |
| 2147 | 2195 | return rhs_node; |
| 2148 | 2196 | } |
| 2149 | 2197 | |
| 2150 | | const cat_node = try rp.c.a().create(ast.Node.InfixOp); |
| 2198 | const cat_node = try rp.c.arena.create(ast.Node.InfixOp); |
| 2151 | 2199 | cat_node.* = .{ |
| 2152 | 2200 | .op_token = cat_tok, |
| 2153 | 2201 | .lhs = &init_node.base, |
| ... | ... | @@ -2382,7 +2430,7 @@ fn transForLoop( |
| 2382 | 2430 | if (ZigClangForStmt_getInit(stmt)) |init| { |
| 2383 | 2431 | block_scope = try Scope.Block.init(rp.c, scope, null); |
| 2384 | 2432 | const block = try transCreateNodeBlock(rp.c, null); |
| 2385 | | block_scope.?.block_node = block; |
| 2433 | block_scope.?.setBlockNode(block); |
| 2386 | 2434 | loop_scope.parent = &block_scope.?.base; |
| 2387 | 2435 | const result = try transStmt(rp, &block_scope.?.base, init, .unused, .r_value); |
| 2388 | 2436 | if (result != &block.base) |
| ... | ... | @@ -2445,7 +2493,7 @@ fn transSwitch( |
| 2445 | 2493 | const block_scope = try Scope.Block.init(rp.c, &switch_scope.base, null); |
| 2446 | 2494 | // tmp block that all statements will go before being picked up by a case or default |
| 2447 | 2495 | const block = try transCreateNodeBlock(rp.c, null); |
| 2448 | | block_scope.block_node = block; |
| 2496 | block_scope.setBlockNode(block); |
| 2449 | 2497 | |
| 2450 | 2498 | const switch_block = try transCreateNodeBlock(rp.c, null); |
| 2451 | 2499 | try switch_block.statements.push(&switch_node.base); |
| ... | ... | @@ -2479,7 +2527,7 @@ fn transCase( |
| 2479 | 2527 | ) TransError!*ast.Node { |
| 2480 | 2528 | const block_scope = scope.findBlockScope(rp.c) catch unreachable; |
| 2481 | 2529 | 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)}); |
| 2483 | 2531 | _ = try appendToken(rp.c, .Semicolon, ";"); |
| 2484 | 2532 | |
| 2485 | 2533 | const expr = if (ZigClangCaseStmt_getRHS(stmt)) |rhs| blk: { |
| ... | ... | @@ -2487,7 +2535,7 @@ fn transCase( |
| 2487 | 2535 | const ellips = try appendToken(rp.c, .Ellipsis3, "..."); |
| 2488 | 2536 | const rhs_node = try transExpr(rp, scope, rhs, .used, .r_value); |
| 2489 | 2537 | |
| 2490 | | const node = try rp.c.a().create(ast.Node.InfixOp); |
| 2538 | const node = try rp.c.arena.create(ast.Node.InfixOp); |
| 2491 | 2539 | node.* = .{ |
| 2492 | 2540 | .op_token = ellips, |
| 2493 | 2541 | .lhs = lhs_node, |
| ... | ... | @@ -2606,7 +2654,7 @@ fn transCharLiteral( |
| 2606 | 2654 | } |
| 2607 | 2655 | var char_buf: [4]u8 = undefined; |
| 2608 | 2656 | 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); |
| 2610 | 2658 | node.* = .{ |
| 2611 | 2659 | .token = token, |
| 2612 | 2660 | }; |
| ... | ... | @@ -2646,7 +2694,7 @@ fn transStmtExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmtExpr, |
| 2646 | 2694 | const lparen = try appendToken(rp.c, .LParen, "("); |
| 2647 | 2695 | const block_scope = try Scope.Block.init(rp.c, scope, "blk"); |
| 2648 | 2696 | const block = try transCreateNodeBlock(rp.c, "blk"); |
| 2649 | | block_scope.block_node = block; |
| 2697 | block_scope.setBlockNode(block); |
| 2650 | 2698 | |
| 2651 | 2699 | var it = ZigClangCompoundStmt_body_begin(comp); |
| 2652 | 2700 | const end_it = ZigClangCompoundStmt_body_end(comp); |
| ... | ... | @@ -2661,7 +2709,7 @@ fn transStmtExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmtExpr, |
| 2661 | 2709 | try block.statements.push(&break_node.base); |
| 2662 | 2710 | block.rbrace = try appendToken(rp.c, .RBrace, "}"); |
| 2663 | 2711 | 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); |
| 2665 | 2713 | grouped_expr.* = .{ |
| 2666 | 2714 | .lparen = lparen, |
| 2667 | 2715 | .expr = &block.base, |
| ... | ... | @@ -2686,7 +2734,7 @@ fn transMemberExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangMemberE |
| 2686 | 2734 | const field_decl = @ptrCast(*const struct_ZigClangFieldDecl, member_decl); |
| 2687 | 2735 | if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) { |
| 2688 | 2736 | 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); |
| 2690 | 2738 | } |
| 2691 | 2739 | } |
| 2692 | 2740 | const decl = @ptrCast(*const ZigClangNamedDecl, member_decl); |
| ... | ... | @@ -2941,7 +2989,7 @@ fn transCreatePreCrement( |
| 2941 | 2989 | // zig: break :blk _ref.* |
| 2942 | 2990 | // zig: }) |
| 2943 | 2991 | 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)); |
| 2945 | 2993 | const ref = try block_scope.makeMangledName(rp.c, "ref"); |
| 2946 | 2994 | |
| 2947 | 2995 | const node = try transCreateNodeVarDecl(rp.c, false, true, ref); |
| ... | ... | @@ -2967,7 +3015,7 @@ fn transCreatePreCrement( |
| 2967 | 3015 | block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); |
| 2968 | 3016 | // semicolon must immediately follow rbrace because it is the last token in a block |
| 2969 | 3017 | _ = 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); |
| 2971 | 3019 | grouped_expr.* = .{ |
| 2972 | 3020 | .lparen = try appendToken(rp.c, .LParen, "("), |
| 2973 | 3021 | .expr = &block_scope.block_node.base, |
| ... | ... | @@ -3007,7 +3055,7 @@ fn transCreatePostCrement( |
| 3007 | 3055 | // zig: break :blk _tmp |
| 3008 | 3056 | // zig: }) |
| 3009 | 3057 | 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)); |
| 3011 | 3059 | const ref = try block_scope.makeMangledName(rp.c, "ref"); |
| 3012 | 3060 | |
| 3013 | 3061 | const node = try transCreateNodeVarDecl(rp.c, false, true, ref); |
| ... | ... | @@ -3040,7 +3088,7 @@ fn transCreatePostCrement( |
| 3040 | 3088 | try block_scope.block_node.statements.push(&break_node.base); |
| 3041 | 3089 | _ = try appendToken(rp.c, .Semicolon, ";"); |
| 3042 | 3090 | 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); |
| 3044 | 3092 | grouped_expr.* = .{ |
| 3045 | 3093 | .lparen = try appendToken(rp.c, .LParen, "("), |
| 3046 | 3094 | .expr = &block_scope.block_node.base, |
| ... | ... | @@ -3106,7 +3154,7 @@ fn transCreateCompoundAssign( |
| 3106 | 3154 | |
| 3107 | 3155 | if ((is_mod or is_div) and is_signed) { |
| 3108 | 3156 | 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); |
| 3110 | 3158 | const builtin = if (is_mod) "@rem" else "@divTrunc"; |
| 3111 | 3159 | const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, builtin); |
| 3112 | 3160 | const lhs_node = try transExpr(rp, scope, lhs, .used, .l_value); |
| ... | ... | @@ -3152,7 +3200,7 @@ fn transCreateCompoundAssign( |
| 3152 | 3200 | // zig: break :blk _ref.* |
| 3153 | 3201 | // zig: }) |
| 3154 | 3202 | 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)); |
| 3156 | 3204 | const ref = try block_scope.makeMangledName(rp.c, "ref"); |
| 3157 | 3205 | |
| 3158 | 3206 | const node = try transCreateNodeVarDecl(rp.c, false, true, ref); |
| ... | ... | @@ -3169,7 +3217,7 @@ fn transCreateCompoundAssign( |
| 3169 | 3217 | |
| 3170 | 3218 | if ((is_mod or is_div) and is_signed) { |
| 3171 | 3219 | 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); |
| 3173 | 3221 | const builtin = if (is_mod) "@rem" else "@divTrunc"; |
| 3174 | 3222 | const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, builtin); |
| 3175 | 3223 | try builtin_node.params.push(try transCreateNodePtrDeref(rp.c, lhs_node)); |
| ... | ... | @@ -3211,7 +3259,7 @@ fn transCreateCompoundAssign( |
| 3211 | 3259 | break_node.rhs = ref_node; |
| 3212 | 3260 | try block_scope.block_node.statements.push(&break_node.base); |
| 3213 | 3261 | 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); |
| 3215 | 3263 | grouped_expr.* = .{ |
| 3216 | 3264 | .lparen = try appendToken(rp.c, .LParen, "("), |
| 3217 | 3265 | .expr = &block_scope.block_node.base, |
| ... | ... | @@ -3295,7 +3343,7 @@ fn transBreak(rp: RestorePoint, scope: *Scope) TransError!*ast.Node { |
| 3295 | 3343 | fn transFloatingLiteral(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangFloatingLiteral, used: ResultUsed) TransError!*ast.Node { |
| 3296 | 3344 | // TODO use something more accurate |
| 3297 | 3345 | 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); |
| 3299 | 3347 | node.* = .{ |
| 3300 | 3348 | .token = try appendTokenFmt(rp.c, .FloatLiteral, "{d}", .{dbl}), |
| 3301 | 3349 | }; |
| ... | ... | @@ -3318,7 +3366,7 @@ fn transBinaryConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const |
| 3318 | 3366 | const lparen = try appendToken(rp.c, .LParen, "("); |
| 3319 | 3367 | |
| 3320 | 3368 | 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)); |
| 3322 | 3370 | |
| 3323 | 3371 | const mangled_name = try block_scope.makeMangledName(rp.c, "cond_temp"); |
| 3324 | 3372 | const tmp_var = try transCreateNodeVarDecl(rp.c, false, true, mangled_name); |
| ... | ... | @@ -3351,7 +3399,7 @@ fn transBinaryConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const |
| 3351 | 3399 | try block_scope.block_node.statements.push(&break_node.base); |
| 3352 | 3400 | block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}"); |
| 3353 | 3401 | |
| 3354 | | const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); |
| 3402 | const grouped_expr = try rp.c.arena.create(ast.Node.GroupedExpression); |
| 3355 | 3403 | grouped_expr.* = .{ |
| 3356 | 3404 | .lparen = lparen, |
| 3357 | 3405 | .expr = &block_scope.block_node.base, |
| ... | ... | @@ -3384,7 +3432,7 @@ fn transConditionalOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigCla |
| 3384 | 3432 | |
| 3385 | 3433 | if (grouped) { |
| 3386 | 3434 | 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); |
| 3388 | 3436 | grouped_expr.* = .{ |
| 3389 | 3437 | .lparen = lparen, |
| 3390 | 3438 | .expr = &if_node.base, |
| ... | ... | @@ -3415,7 +3463,7 @@ fn maybeSuppressResult( |
| 3415 | 3463 | } |
| 3416 | 3464 | const lhs = try transCreateNodeIdentifier(rp.c, "_"); |
| 3417 | 3465 | 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); |
| 3419 | 3467 | op_node.* = .{ |
| 3420 | 3468 | .op_token = op_token, |
| 3421 | 3469 | .lhs = lhs, |
| ... | ... | @@ -3426,7 +3474,7 @@ fn maybeSuppressResult( |
| 3426 | 3474 | } |
| 3427 | 3475 | |
| 3428 | 3476 | fn 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); |
| 3430 | 3478 | _ = try c.global_scope.sym_table.put(name, decl_node); |
| 3431 | 3479 | } |
| 3432 | 3480 | |
| ... | ... | @@ -3524,7 +3572,7 @@ fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigC |
| 3524 | 3572 | if (int_bit_width != 0) { |
| 3525 | 3573 | // we can perform the log2 now. |
| 3526 | 3574 | 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); |
| 3528 | 3576 | node.* = .{ |
| 3529 | 3577 | .token = try appendTokenFmt(rp.c, .Identifier, "u{}", .{cast_bit_width}), |
| 3530 | 3578 | }; |
| ... | ... | @@ -3547,7 +3595,7 @@ fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigC |
| 3547 | 3595 | |
| 3548 | 3596 | const import_fn_call = try transCreateNodeBuiltinFnCall(rp.c, "@import"); |
| 3549 | 3597 | 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); |
| 3551 | 3599 | std_node.* = .{ |
| 3552 | 3600 | .token = std_token, |
| 3553 | 3601 | }; |
| ... | ... | @@ -3749,7 +3797,7 @@ fn transCreateNodeAssign( |
| 3749 | 3797 | // zig: break :blk _tmp |
| 3750 | 3798 | // zig: }) |
| 3751 | 3799 | 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)); |
| 3753 | 3801 | const tmp = try block_scope.makeMangledName(rp.c, "tmp"); |
| 3754 | 3802 | |
| 3755 | 3803 | const node = try transCreateNodeVarDecl(rp.c, false, true, tmp); |
| ... | ... | @@ -3786,10 +3834,10 @@ fn transCreateNodeAssign( |
| 3786 | 3834 | fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.BuiltinCall { |
| 3787 | 3835 | const builtin_token = try appendToken(c, .Builtin, name); |
| 3788 | 3836 | _ = try appendToken(c, .LParen, "("); |
| 3789 | | const node = try c.a().create(ast.Node.BuiltinCall); |
| 3837 | const node = try c.arena.create(ast.Node.BuiltinCall); |
| 3790 | 3838 | node.* = .{ |
| 3791 | 3839 | .builtin_token = builtin_token, |
| 3792 | | .params = ast.Node.BuiltinCall.ParamList.init(c.a()), |
| 3840 | .params = ast.Node.BuiltinCall.ParamList{}, |
| 3793 | 3841 | .rparen_token = undefined, // set after appending args |
| 3794 | 3842 | }; |
| 3795 | 3843 | return node; |
| ... | ... | @@ -3797,12 +3845,12 @@ fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.Builti |
| 3797 | 3845 | |
| 3798 | 3846 | fn transCreateNodeFnCall(c: *Context, fn_expr: *ast.Node) !*ast.Node.SuffixOp { |
| 3799 | 3847 | _ = try appendToken(c, .LParen, "("); |
| 3800 | | const node = try c.a().create(ast.Node.SuffixOp); |
| 3848 | const node = try c.arena.create(ast.Node.SuffixOp); |
| 3801 | 3849 | node.* = .{ |
| 3802 | 3850 | .lhs = .{ .node = fn_expr }, |
| 3803 | 3851 | .op = .{ |
| 3804 | 3852 | .Call = .{ |
| 3805 | | .params = ast.Node.SuffixOp.Op.Call.ParamList.init(c.a()), |
| 3853 | .params = ast.Node.SuffixOp.Op.Call.ParamList{}, |
| 3806 | 3854 | .async_token = null, |
| 3807 | 3855 | }, |
| 3808 | 3856 | }, |
| ... | ... | @@ -3812,7 +3860,7 @@ fn transCreateNodeFnCall(c: *Context, fn_expr: *ast.Node) !*ast.Node.SuffixOp { |
| 3812 | 3860 | } |
| 3813 | 3861 | |
| 3814 | 3862 | fn 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); |
| 3816 | 3864 | field_access_node.* = .{ |
| 3817 | 3865 | .op_token = try appendToken(c, .Period, "."), |
| 3818 | 3866 | .lhs = container, |
| ... | ... | @@ -3828,7 +3876,7 @@ fn transCreateNodePrefixOp( |
| 3828 | 3876 | op_tok_id: std.zig.Token.Id, |
| 3829 | 3877 | bytes: []const u8, |
| 3830 | 3878 | ) !*ast.Node.PrefixOp { |
| 3831 | | const node = try c.a().create(ast.Node.PrefixOp); |
| 3879 | const node = try c.arena.create(ast.Node.PrefixOp); |
| 3832 | 3880 | node.* = .{ |
| 3833 | 3881 | .op_token = try appendToken(c, op_tok_id, bytes), |
| 3834 | 3882 | .op = op, |
| ... | ... | @@ -3851,7 +3899,7 @@ fn transCreateNodeInfixOp( |
| 3851 | 3899 | try appendToken(rp.c, .LParen, "(") |
| 3852 | 3900 | else |
| 3853 | 3901 | null; |
| 3854 | | const node = try rp.c.a().create(ast.Node.InfixOp); |
| 3902 | const node = try rp.c.arena.create(ast.Node.InfixOp); |
| 3855 | 3903 | node.* = .{ |
| 3856 | 3904 | .op_token = op_token, |
| 3857 | 3905 | .lhs = lhs_node, |
| ... | ... | @@ -3860,7 +3908,7 @@ fn transCreateNodeInfixOp( |
| 3860 | 3908 | }; |
| 3861 | 3909 | if (!grouped) return maybeSuppressResult(rp, scope, used, &node.base); |
| 3862 | 3910 | 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); |
| 3864 | 3912 | grouped_expr.* = .{ |
| 3865 | 3913 | .lparen = lparen.?, |
| 3866 | 3914 | .expr = &node.base, |
| ... | ... | @@ -3904,7 +3952,7 @@ fn transCreateNodePtrType( |
| 3904 | 3952 | is_volatile: bool, |
| 3905 | 3953 | op_tok_id: std.zig.Token.Id, |
| 3906 | 3954 | ) !*ast.Node.PrefixOp { |
| 3907 | | const node = try c.a().create(ast.Node.PrefixOp); |
| 3955 | const node = try c.arena.create(ast.Node.PrefixOp); |
| 3908 | 3956 | const op_token = switch (op_tok_id) { |
| 3909 | 3957 | .LBracket => blk: { |
| 3910 | 3958 | const lbracket = try appendToken(c, .LBracket, "["); |
| ... | ... | @@ -3946,8 +3994,8 @@ fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node { |
| 3946 | 3994 | ZigClangAPSInt_free(aps_int); |
| 3947 | 3995 | }; |
| 3948 | 3996 | |
| 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); |
| 3951 | 3999 | |
| 3952 | 4000 | const data = ZigClangAPSInt_getRawData(aps_int); |
| 3953 | 4001 | switch (@sizeOf(math.big.Limb)) { |
| ... | ... | @@ -3972,12 +4020,12 @@ fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node { |
| 3972 | 4020 | } |
| 3973 | 4021 | |
| 3974 | 4022 | 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) { |
| 3976 | 4024 | error.OutOfMemory => return error.OutOfMemory, |
| 3977 | 4025 | }; |
| 3978 | | defer c.a().free(str); |
| 4026 | defer c.arena.free(str); |
| 3979 | 4027 | 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); |
| 3981 | 4029 | node.* = .{ |
| 3982 | 4030 | .token = token, |
| 3983 | 4031 | }; |
| ... | ... | @@ -3986,7 +4034,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const ZigClangAPSInt) !*ast.Node { |
| 3986 | 4034 | |
| 3987 | 4035 | fn transCreateNodeReturnExpr(c: *Context) !*ast.Node.ControlFlowExpression { |
| 3988 | 4036 | 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); |
| 3990 | 4038 | node.* = .{ |
| 3991 | 4039 | .ltoken = ltoken, |
| 3992 | 4040 | .kind = .Return, |
| ... | ... | @@ -3997,7 +4045,7 @@ fn transCreateNodeReturnExpr(c: *Context) !*ast.Node.ControlFlowExpression { |
| 3997 | 4045 | |
| 3998 | 4046 | fn transCreateNodeUndefinedLiteral(c: *Context) !*ast.Node { |
| 3999 | 4047 | 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); |
| 4001 | 4049 | node.* = .{ |
| 4002 | 4050 | .token = token, |
| 4003 | 4051 | }; |
| ... | ... | @@ -4006,7 +4054,7 @@ fn transCreateNodeUndefinedLiteral(c: *Context) !*ast.Node { |
| 4006 | 4054 | |
| 4007 | 4055 | fn transCreateNodeNullLiteral(c: *Context) !*ast.Node { |
| 4008 | 4056 | 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); |
| 4010 | 4058 | node.* = .{ |
| 4011 | 4059 | .token = token, |
| 4012 | 4060 | }; |
| ... | ... | @@ -4018,7 +4066,7 @@ fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node { |
| 4018 | 4066 | try appendToken(c, .Keyword_true, "true") |
| 4019 | 4067 | else |
| 4020 | 4068 | 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); |
| 4022 | 4070 | node.* = .{ |
| 4023 | 4071 | .token = token, |
| 4024 | 4072 | }; |
| ... | ... | @@ -4027,11 +4075,11 @@ fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node { |
| 4027 | 4075 | |
| 4028 | 4076 | fn transCreateNodeArrayInitializer(c: *Context, ty: *ast.Node) !*ast.Node.SuffixOp { |
| 4029 | 4077 | _ = try appendToken(c, .LBrace, "{"); |
| 4030 | | const node = try c.a().create(ast.Node.SuffixOp); |
| 4078 | const node = try c.arena.create(ast.Node.SuffixOp); |
| 4031 | 4079 | node.* = .{ |
| 4032 | 4080 | .lhs = .{ .node = ty }, |
| 4033 | 4081 | .op = .{ |
| 4034 | | .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()), |
| 4082 | .ArrayInitializer = ast.Node.SuffixOp.Op.InitList{}, |
| 4035 | 4083 | }, |
| 4036 | 4084 | .rtoken = undefined, // set after appending values |
| 4037 | 4085 | }; |
| ... | ... | @@ -4040,11 +4088,11 @@ fn transCreateNodeArrayInitializer(c: *Context, ty: *ast.Node) !*ast.Node.Suffix |
| 4040 | 4088 | |
| 4041 | 4089 | fn transCreateNodeStructInitializer(c: *Context, ty: *ast.Node) !*ast.Node.SuffixOp { |
| 4042 | 4090 | _ = try appendToken(c, .LBrace, "{"); |
| 4043 | | const node = try c.a().create(ast.Node.SuffixOp); |
| 4091 | const node = try c.arena.create(ast.Node.SuffixOp); |
| 4044 | 4092 | node.* = .{ |
| 4045 | 4093 | .lhs = .{ .node = ty }, |
| 4046 | 4094 | .op = .{ |
| 4047 | | .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()), |
| 4095 | .StructInitializer = ast.Node.SuffixOp.Op.InitList{}, |
| 4048 | 4096 | }, |
| 4049 | 4097 | .rtoken = undefined, // set after appending values |
| 4050 | 4098 | }; |
| ... | ... | @@ -4053,7 +4101,7 @@ fn transCreateNodeStructInitializer(c: *Context, ty: *ast.Node) !*ast.Node.Suffi |
| 4053 | 4101 | |
| 4054 | 4102 | fn transCreateNodeInt(c: *Context, int: var) !*ast.Node { |
| 4055 | 4103 | 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); |
| 4057 | 4105 | node.* = .{ |
| 4058 | 4106 | .token = token, |
| 4059 | 4107 | }; |
| ... | ... | @@ -4062,7 +4110,7 @@ fn transCreateNodeInt(c: *Context, int: var) !*ast.Node { |
| 4062 | 4110 | |
| 4063 | 4111 | fn transCreateNodeFloat(c: *Context, int: var) !*ast.Node { |
| 4064 | 4112 | 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); |
| 4066 | 4114 | node.* = .{ |
| 4067 | 4115 | .token = token, |
| 4068 | 4116 | }; |
| ... | ... | @@ -4085,20 +4133,22 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a |
| 4085 | 4133 | const name_tok = try appendIdentifier(c, name); |
| 4086 | 4134 | _ = try appendToken(c, .LParen, "("); |
| 4087 | 4135 | |
| 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.?) { |
| 4092 | 4142 | _ = try appendToken(c, .Comma, ","); |
| 4093 | 4143 | } |
| 4094 | | const param = pn.*.cast(ast.Node.ParamDecl).?; |
| 4144 | const param = pn.cast(ast.Node.ParamDecl).?; |
| 4095 | 4145 | |
| 4096 | 4146 | const param_name_tok = param.name_token orelse |
| 4097 | 4147 | try appendTokenFmt(c, .Identifier, "arg_{}", .{c.getMangle()}); |
| 4098 | 4148 | |
| 4099 | 4149 | _ = try appendToken(c, .Colon, ":"); |
| 4100 | 4150 | |
| 4101 | | const param_node = try c.a().create(ast.Node.ParamDecl); |
| 4151 | const param_node = try c.arena.create(ast.Node.ParamDecl); |
| 4102 | 4152 | param_node.* = .{ |
| 4103 | 4153 | .doc_comments = null, |
| 4104 | 4154 | .comptime_token = null, |
| ... | ... | @@ -4106,12 +4156,12 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a |
| 4106 | 4156 | .name_token = param_name_tok, |
| 4107 | 4157 | .param_type = param.param_type, |
| 4108 | 4158 | }; |
| 4109 | | try fn_params.push(&param_node.base); |
| 4159 | fn_params_list = try c.llpush(*ast.Node, fn_params_list, &param_node.base); |
| 4110 | 4160 | } |
| 4111 | 4161 | |
| 4112 | 4162 | _ = try appendToken(c, .RParen, ")"); |
| 4113 | 4163 | |
| 4114 | | const fn_proto = try c.a().create(ast.Node.FnProto); |
| 4164 | const fn_proto = try c.arena.create(ast.Node.FnProto); |
| 4115 | 4165 | fn_proto.* = .{ |
| 4116 | 4166 | .doc_comments = null, |
| 4117 | 4167 | .visib_token = pub_tok, |
| ... | ... | @@ -4129,24 +4179,28 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a |
| 4129 | 4179 | }; |
| 4130 | 4180 | |
| 4131 | 4181 | const block = try transCreateNodeBlock(c, null); |
| 4182 | var block_statements_it = &block.statements.first; |
| 4132 | 4183 | |
| 4133 | 4184 | const return_expr = try transCreateNodeReturnExpr(c); |
| 4134 | 4185 | const unwrap_expr = try transCreateNodeUnwrapNull(c, ref.cast(ast.Node.VarDecl).?.init_node.?); |
| 4135 | 4186 | 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) { |
| 4139 | 4193 | _ = try appendToken(c, .Comma, ","); |
| 4140 | 4194 | } |
| 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.?)),); |
| 4143 | 4197 | } |
| 4144 | 4198 | call_expr.rtoken = try appendToken(c, .RParen, ")"); |
| 4145 | 4199 | return_expr.rhs = &call_expr.base; |
| 4146 | 4200 | _ = try appendToken(c, .Semicolon, ";"); |
| 4147 | 4201 | |
| 4148 | 4202 | 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); |
| 4150 | 4204 | fn_proto.body_node = &block.base; |
| 4151 | 4205 | return &fn_proto.base; |
| 4152 | 4206 | } |
| ... | ... | @@ -4154,7 +4208,7 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a |
| 4154 | 4208 | fn transCreateNodeUnwrapNull(c: *Context, wrapped: *ast.Node) !*ast.Node { |
| 4155 | 4209 | _ = try appendToken(c, .Period, "."); |
| 4156 | 4210 | 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); |
| 4158 | 4212 | node.* = .{ |
| 4159 | 4213 | .op = .UnwrapOptional, |
| 4160 | 4214 | .lhs = .{ .node = wrapped }, |
| ... | ... | @@ -4164,7 +4218,7 @@ fn transCreateNodeUnwrapNull(c: *Context, wrapped: *ast.Node) !*ast.Node { |
| 4164 | 4218 | } |
| 4165 | 4219 | |
| 4166 | 4220 | fn 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); |
| 4168 | 4222 | node.* = .{ |
| 4169 | 4223 | .dot = try appendToken(c, .Period, "."), |
| 4170 | 4224 | .name = try appendIdentifier(c, name), |
| ... | ... | @@ -4173,7 +4227,7 @@ fn transCreateNodeEnumLiteral(c: *Context, name: []const u8) !*ast.Node { |
| 4173 | 4227 | } |
| 4174 | 4228 | |
| 4175 | 4229 | fn 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); |
| 4177 | 4231 | node.* = .{ |
| 4178 | 4232 | .token = try appendToken(c, .StringLiteral, str), |
| 4179 | 4233 | }; |
| ... | ... | @@ -4183,7 +4237,7 @@ fn transCreateNodeStringLiteral(c: *Context, str: []const u8) !*ast.Node { |
| 4183 | 4237 | fn transCreateNodeIf(c: *Context) !*ast.Node.If { |
| 4184 | 4238 | const if_tok = try appendToken(c, .Keyword_if, "if"); |
| 4185 | 4239 | _ = try appendToken(c, .LParen, "("); |
| 4186 | | const node = try c.a().create(ast.Node.If); |
| 4240 | const node = try c.arena.create(ast.Node.If); |
| 4187 | 4241 | node.* = .{ |
| 4188 | 4242 | .if_token = if_tok, |
| 4189 | 4243 | .condition = undefined, |
| ... | ... | @@ -4195,7 +4249,7 @@ fn transCreateNodeIf(c: *Context) !*ast.Node.If { |
| 4195 | 4249 | } |
| 4196 | 4250 | |
| 4197 | 4251 | fn 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); |
| 4199 | 4253 | node.* = .{ |
| 4200 | 4254 | .else_token = try appendToken(c, .Keyword_else, "else"), |
| 4201 | 4255 | .payload = null, |
| ... | ... | @@ -4210,11 +4264,11 @@ fn transCreateNodeBlock(c: *Context, label: ?[]const u8) !*ast.Node.Block { |
| 4210 | 4264 | _ = try appendToken(c, .Colon, ":"); |
| 4211 | 4265 | break :blk ll; |
| 4212 | 4266 | } else null; |
| 4213 | | const block_node = try c.a().create(ast.Node.Block); |
| 4267 | const block_node = try c.arena.create(ast.Node.Block); |
| 4214 | 4268 | block_node.* = .{ |
| 4215 | 4269 | .label = label_node, |
| 4216 | 4270 | .lbrace = try appendToken(c, .LBrace, "{"), |
| 4217 | | .statements = ast.Node.Block.StatementList.init(c.a()), |
| 4271 | .statements = ast.Node.Block.StatementList{}, |
| 4218 | 4272 | .rbrace = undefined, |
| 4219 | 4273 | }; |
| 4220 | 4274 | return block_node; |
| ... | ... | @@ -4226,7 +4280,7 @@ fn transCreateNodeBreak(c: *Context, label: ?[]const u8) !*ast.Node.ControlFlowE |
| 4226 | 4280 | _ = try appendToken(c, .Colon, ":"); |
| 4227 | 4281 | break :blk try transCreateNodeIdentifier(c, l); |
| 4228 | 4282 | } else null; |
| 4229 | | const node = try c.a().create(ast.Node.ControlFlowExpression); |
| 4283 | const node = try c.arena.create(ast.Node.ControlFlowExpression); |
| 4230 | 4284 | node.* = .{ |
| 4231 | 4285 | .ltoken = ltoken, |
| 4232 | 4286 | .kind = .{ .Break = label_node }, |
| ... | ... | @@ -4240,7 +4294,7 @@ fn transCreateNodeVarDecl(c: *Context, is_pub: bool, is_const: bool, name: []con |
| 4240 | 4294 | const mut_tok = if (is_const) try appendToken(c, .Keyword_const, "const") else try appendToken(c, .Keyword_var, "var"); |
| 4241 | 4295 | const name_tok = try appendIdentifier(c, name); |
| 4242 | 4296 | |
| 4243 | | const node = try c.a().create(ast.Node.VarDecl); |
| 4297 | const node = try c.arena.create(ast.Node.VarDecl); |
| 4244 | 4298 | node.* = .{ |
| 4245 | 4299 | .doc_comments = null, |
| 4246 | 4300 | .visib_token = visib_tok, |
| ... | ... | @@ -4264,7 +4318,7 @@ fn transCreateNodeWhile(c: *Context) !*ast.Node.While { |
| 4264 | 4318 | const while_tok = try appendToken(c, .Keyword_while, "while"); |
| 4265 | 4319 | _ = try appendToken(c, .LParen, "("); |
| 4266 | 4320 | |
| 4267 | | const node = try c.a().create(ast.Node.While); |
| 4321 | const node = try c.arena.create(ast.Node.While); |
| 4268 | 4322 | node.* = .{ |
| 4269 | 4323 | .label = null, |
| 4270 | 4324 | .inline_token = null, |
| ... | ... | @@ -4280,7 +4334,7 @@ fn transCreateNodeWhile(c: *Context) !*ast.Node.While { |
| 4280 | 4334 | |
| 4281 | 4335 | fn transCreateNodeContinue(c: *Context) !*ast.Node { |
| 4282 | 4336 | 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); |
| 4284 | 4338 | node.* = .{ |
| 4285 | 4339 | .ltoken = ltoken, |
| 4286 | 4340 | .kind = .{ .Continue = null }, |
| ... | ... | @@ -4294,11 +4348,11 @@ fn transCreateNodeSwitch(c: *Context) !*ast.Node.Switch { |
| 4294 | 4348 | const switch_tok = try appendToken(c, .Keyword_switch, "switch"); |
| 4295 | 4349 | _ = try appendToken(c, .LParen, "("); |
| 4296 | 4350 | |
| 4297 | | const node = try c.a().create(ast.Node.Switch); |
| 4351 | const node = try c.arena.create(ast.Node.Switch); |
| 4298 | 4352 | node.* = .{ |
| 4299 | 4353 | .switch_token = switch_tok, |
| 4300 | 4354 | .expr = undefined, |
| 4301 | | .cases = ast.Node.Switch.CaseList.init(c.a()), |
| 4355 | .cases = ast.Node.Switch.CaseList{}, |
| 4302 | 4356 | .rbrace = undefined, |
| 4303 | 4357 | }; |
| 4304 | 4358 | return node; |
| ... | ... | @@ -4307,9 +4361,9 @@ fn transCreateNodeSwitch(c: *Context) !*ast.Node.Switch { |
| 4307 | 4361 | fn transCreateNodeSwitchCase(c: *Context, lhs: *ast.Node) !*ast.Node.SwitchCase { |
| 4308 | 4362 | const arrow_tok = try appendToken(c, .EqualAngleBracketRight, "=>"); |
| 4309 | 4363 | |
| 4310 | | const node = try c.a().create(ast.Node.SwitchCase); |
| 4364 | const node = try c.arena.create(ast.Node.SwitchCase); |
| 4311 | 4365 | node.* = .{ |
| 4312 | | .items = ast.Node.SwitchCase.ItemList.init(c.a()), |
| 4366 | .items = ast.Node.SwitchCase.ItemList{}, |
| 4313 | 4367 | .arrow_token = arrow_tok, |
| 4314 | 4368 | .payload = null, |
| 4315 | 4369 | .expr = undefined, |
| ... | ... | @@ -4319,7 +4373,7 @@ fn transCreateNodeSwitchCase(c: *Context, lhs: *ast.Node) !*ast.Node.SwitchCase |
| 4319 | 4373 | } |
| 4320 | 4374 | |
| 4321 | 4375 | fn 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); |
| 4323 | 4377 | node.* = .{ |
| 4324 | 4378 | .token = try appendToken(c, .Keyword_else, "else"), |
| 4325 | 4379 | }; |
| ... | ... | @@ -4352,7 +4406,7 @@ fn transCreateNodeShiftOp( |
| 4352 | 4406 | try cast_node.params.push(rhs); |
| 4353 | 4407 | cast_node.rparen_token = try appendToken(rp.c, .RParen, ")"); |
| 4354 | 4408 | |
| 4355 | | const node = try rp.c.a().create(ast.Node.InfixOp); |
| 4409 | const node = try rp.c.arena.create(ast.Node.InfixOp); |
| 4356 | 4410 | node.* = .{ |
| 4357 | 4411 | .op_token = op_token, |
| 4358 | 4412 | .lhs = lhs, |
| ... | ... | @@ -4364,7 +4418,7 @@ fn transCreateNodeShiftOp( |
| 4364 | 4418 | } |
| 4365 | 4419 | |
| 4366 | 4420 | fn 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); |
| 4368 | 4422 | node.* = .{ |
| 4369 | 4423 | .lhs = .{ .node = lhs }, |
| 4370 | 4424 | .op = .Deref, |
| ... | ... | @@ -4375,7 +4429,7 @@ fn transCreateNodePtrDeref(c: *Context, lhs: *ast.Node) !*ast.Node { |
| 4375 | 4429 | |
| 4376 | 4430 | fn transCreateNodeArrayAccess(c: *Context, lhs: *ast.Node) !*ast.Node.SuffixOp { |
| 4377 | 4431 | _ = try appendToken(c, .LBrace, "["); |
| 4378 | | const node = try c.a().create(ast.Node.SuffixOp); |
| 4432 | const node = try c.arena.create(ast.Node.SuffixOp); |
| 4379 | 4433 | node.* = .{ |
| 4380 | 4434 | .lhs = .{ .node = lhs }, |
| 4381 | 4435 | .op = .{ |
| ... | ... | @@ -4392,7 +4446,7 @@ const RestorePoint = struct { |
| 4392 | 4446 | src_buf_index: usize, |
| 4393 | 4447 | |
| 4394 | 4448 | 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); |
| 4396 | 4450 | self.c.source_buffer.shrink(self.src_buf_index); |
| 4397 | 4451 | } |
| 4398 | 4452 | }; |
| ... | ... | @@ -4400,7 +4454,7 @@ const RestorePoint = struct { |
| 4400 | 4454 | fn makeRestorePoint(c: *Context) RestorePoint { |
| 4401 | 4455 | return RestorePoint{ |
| 4402 | 4456 | .c = c, |
| 4403 | | .token_index = c.tree.tokens.len, |
| 4457 | .token_index = c.tokens.items.len, |
| 4404 | 4458 | .src_buf_index = c.source_buffer.items.len, |
| 4405 | 4459 | }; |
| 4406 | 4460 | } |
| ... | ... | @@ -4647,7 +4701,8 @@ fn finishTransFnProto( |
| 4647 | 4701 | const name_tok = if (fn_decl_context) |ctx| try appendIdentifier(rp.c, ctx.fn_name) else null; |
| 4648 | 4702 | const lparen_tok = try appendToken(rp.c, .LParen, "("); |
| 4649 | 4703 | |
| 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); |
| 4651 | 4706 | const param_count: usize = if (fn_proto_ty != null) ZigClangFunctionProtoType_getNumParams(fn_proto_ty.?) else 0; |
| 4652 | 4707 | |
| 4653 | 4708 | var i: usize = 0; |
| ... | ... | @@ -4672,7 +4727,7 @@ fn finishTransFnProto( |
| 4672 | 4727 | |
| 4673 | 4728 | const type_node = try transQualType(rp, param_qt, source_loc); |
| 4674 | 4729 | |
| 4675 | | const param_node = try rp.c.a().create(ast.Node.ParamDecl); |
| 4730 | const param_node = try rp.c.arena.create(ast.Node.ParamDecl); |
| 4676 | 4731 | param_node.* = .{ |
| 4677 | 4732 | .doc_comments = null, |
| 4678 | 4733 | .comptime_token = null, |
| ... | ... | @@ -4680,7 +4735,7 @@ fn finishTransFnProto( |
| 4680 | 4735 | .name_token = param_name_tok, |
| 4681 | 4736 | .param_type = .{ .type_expr = type_node }, |
| 4682 | 4737 | }; |
| 4683 | | try fn_params.push(&param_node.base); |
| 4738 | try fn_params_list.push(&param_node.base); |
| 4684 | 4739 | |
| 4685 | 4740 | if (i + 1 < param_count) { |
| 4686 | 4741 | _ = try appendToken(rp.c, .Comma, ","); |
| ... | ... | @@ -4692,7 +4747,7 @@ fn finishTransFnProto( |
| 4692 | 4747 | _ = try appendToken(rp.c, .Comma, ","); |
| 4693 | 4748 | } |
| 4694 | 4749 | |
| 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); |
| 4696 | 4751 | var_arg_node.* = .{ |
| 4697 | 4752 | .doc_comments = null, |
| 4698 | 4753 | .comptime_token = null, |
| ... | ... | @@ -4700,7 +4755,7 @@ fn finishTransFnProto( |
| 4700 | 4755 | .name_token = null, |
| 4701 | 4756 | .param_type = .{ .var_args = try appendToken(rp.c, .Ellipsis3, "...") } |
| 4702 | 4757 | }; |
| 4703 | | try fn_params.push(&var_arg_node.base); |
| 4758 | try fn_params_list.push(&var_arg_node.base); |
| 4704 | 4759 | } |
| 4705 | 4760 | |
| 4706 | 4761 | const rparen_tok = try appendToken(rp.c, .RParen, ")"); |
| ... | ... | @@ -4713,7 +4768,7 @@ fn finishTransFnProto( |
| 4713 | 4768 | _ = try appendToken(rp.c, .LParen, "("); |
| 4714 | 4769 | const expr = try transCreateNodeStringLiteral( |
| 4715 | 4770 | 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]}), |
| 4717 | 4772 | ); |
| 4718 | 4773 | _ = try appendToken(rp.c, .RParen, ")"); |
| 4719 | 4774 | |
| ... | ... | @@ -4767,7 +4822,7 @@ fn finishTransFnProto( |
| 4767 | 4822 | } |
| 4768 | 4823 | }; |
| 4769 | 4824 | |
| 4770 | | const fn_proto = try rp.c.a().create(ast.Node.FnProto); |
| 4825 | const fn_proto = try rp.c.arena.create(ast.Node.FnProto); |
| 4771 | 4826 | fn_proto.* = .{ |
| 4772 | 4827 | .doc_comments = null, |
| 4773 | 4828 | .visib_token = pub_tok, |
| ... | ... | @@ -4816,20 +4871,21 @@ pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comp |
| 4816 | 4871 | const semi_tok = try appendToken(c, .Semicolon, ";"); |
| 4817 | 4872 | _ = try appendTokenFmt(c, .LineComment, "// {}", .{c.locStr(loc)}); |
| 4818 | 4873 | |
| 4819 | | const msg_node = try c.a().create(ast.Node.StringLiteral); |
| 4874 | const msg_node = try c.arena.create(ast.Node.StringLiteral); |
| 4820 | 4875 | msg_node.* = .{ |
| 4821 | 4876 | .token = msg_tok, |
| 4822 | 4877 | }; |
| 4823 | 4878 | |
| 4824 | | const call_node = try c.a().create(ast.Node.BuiltinCall); |
| 4879 | const call_node = try c.arena.create(ast.Node.BuiltinCall); |
| 4825 | 4880 | call_node.* = .{ |
| 4826 | 4881 | .builtin_token = builtin_tok, |
| 4827 | | .params = ast.Node.BuiltinCall.ParamList.init(c.a()), |
| 4882 | .params = ast.Node.BuiltinCall.ParamList{}, |
| 4828 | 4883 | .rparen_token = rparen_tok, |
| 4829 | 4884 | }; |
| 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); |
| 4831 | 4887 | |
| 4832 | | const var_decl_node = try c.a().create(ast.Node.VarDecl); |
| 4888 | const var_decl_node = try c.arena.create(ast.Node.VarDecl); |
| 4833 | 4889 | var_decl_node.* = .{ |
| 4834 | 4890 | .doc_comments = null, |
| 4835 | 4891 | .visib_token = pub_tok, |
| ... | ... | @@ -4861,9 +4917,9 @@ fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8, |
| 4861 | 4917 | |
| 4862 | 4918 | try c.source_buffer.outStream().print(format, args); |
| 4863 | 4919 | 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); |
| 4867 | 4923 | |
| 4868 | 4924 | new_token.* = .{ |
| 4869 | 4925 | .id = token_id, |
| ... | ... | @@ -4931,7 +4987,7 @@ fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex { |
| 4931 | 4987 | |
| 4932 | 4988 | fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { |
| 4933 | 4989 | 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); |
| 4935 | 4991 | identifier.* = .{ |
| 4936 | 4992 | .token = token_index, |
| 4937 | 4993 | }; |
| ... | ... | @@ -4940,7 +4996,7 @@ fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { |
| 4940 | 4996 | |
| 4941 | 4997 | fn transCreateNodeIdentifierUnchecked(c: *Context, name: []const u8) !*ast.Node { |
| 4942 | 4998 | 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); |
| 4944 | 5000 | identifier.* = .{ |
| 4945 | 5001 | .token = token_index, |
| 4946 | 5002 | }; |
| ... | ... | @@ -4955,7 +5011,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void { |
| 4955 | 5011 | // TODO if we see #undef, delete it from the table |
| 4956 | 5012 | var it = ZigClangASTUnit_getLocalPreprocessingEntities_begin(unit); |
| 4957 | 5013 | const it_end = ZigClangASTUnit_getLocalPreprocessingEntities_end(unit); |
| 4958 | | var tok_list = CTokenList.init(c.a()); |
| 5014 | var tok_list = CTokenList.init(c.arena); |
| 4959 | 5015 | const scope = c.global_scope; |
| 4960 | 5016 | |
| 4961 | 5017 | while (it.I != it_end.I) : (it.I += 1) { |
| ... | ... | @@ -4970,7 +5026,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void { |
| 4970 | 5026 | const name = try c.str(raw_name); |
| 4971 | 5027 | // TODO https://github.com/ziglang/zig/issues/3756 |
| 4972 | 5028 | // 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; |
| 4974 | 5030 | if (scope.containsNow(mangled_name)) { |
| 4975 | 5031 | continue; |
| 4976 | 5032 | } |
| ... | ... | @@ -5078,7 +5134,8 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5078 | 5134 | .{}, |
| 5079 | 5135 | ); |
| 5080 | 5136 | } |
| 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; |
| 5082 | 5139 | while (true) { |
| 5083 | 5140 | const param_tok = it.next().?; |
| 5084 | 5141 | if (param_tok.id != .Identifier) { |
| ... | ... | @@ -5096,12 +5153,12 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5096 | 5153 | _ = try appendToken(c, .Colon, ":"); |
| 5097 | 5154 | |
| 5098 | 5155 | 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); |
| 5100 | 5157 | identifier.* = .{ |
| 5101 | 5158 | .token = token_index, |
| 5102 | 5159 | }; |
| 5103 | 5160 | |
| 5104 | | const param_node = try c.a().create(ast.Node.ParamDecl); |
| 5161 | const param_node = try c.arena.create(ast.Node.ParamDecl); |
| 5105 | 5162 | param_node.* = .{ |
| 5106 | 5163 | .doc_comments = null, |
| 5107 | 5164 | .comptime_token = null, |
| ... | ... | @@ -5109,7 +5166,7 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5109 | 5166 | .name_token = param_name_tok, |
| 5110 | 5167 | .param_type = .{ .type_expr = &identifier.base }, |
| 5111 | 5168 | }; |
| 5112 | | try fn_params.push(&param_node.base); |
| 5169 | fn_params_it = try c.llpush(*ast.Node, fn_params_it, &param_node.base); |
| 5113 | 5170 | |
| 5114 | 5171 | if (it.peek().?.id != .Comma) |
| 5115 | 5172 | break; |
| ... | ... | @@ -5131,8 +5188,9 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5131 | 5188 | |
| 5132 | 5189 | const type_of = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); |
| 5133 | 5190 | type_of.rparen_token = try appendToken(c, .RParen, ")"); |
| 5191 | var type_of_params = c.llpusher(&type_of.params); |
| 5134 | 5192 | |
| 5135 | | const fn_proto = try c.a().create(ast.Node.FnProto); |
| 5193 | const fn_proto = try c.arena.create(ast.Node.FnProto); |
| 5136 | 5194 | fn_proto.* = .{ |
| 5137 | 5195 | .visib_token = pub_tok, |
| 5138 | 5196 | .extern_export_inline_token = inline_tok, |
| ... | ... | @@ -5150,6 +5208,7 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5150 | 5208 | }; |
| 5151 | 5209 | |
| 5152 | 5210 | const block = try transCreateNodeBlock(c, null); |
| 5211 | var block_statements = c.llpusher(&block.statements); |
| 5153 | 5212 | |
| 5154 | 5213 | const return_expr = try transCreateNodeReturnExpr(c); |
| 5155 | 5214 | const expr = try parseCExpr(c, it, source, source_loc, scope); |
| ... | ... | @@ -5165,16 +5224,16 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5165 | 5224 | _ = try appendToken(c, .Semicolon, ";"); |
| 5166 | 5225 | const type_of_arg = if (expr.id != .Block) expr else blk: { |
| 5167 | 5226 | 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; |
| 5169 | 5228 | std.debug.assert(blk_last.id == .ControlFlowExpression); |
| 5170 | 5229 | const br = @fieldParentPtr(ast.Node.ControlFlowExpression, "base", blk_last); |
| 5171 | 5230 | break :blk br.rhs.?; |
| 5172 | 5231 | }; |
| 5173 | | try type_of.params.push(type_of_arg); |
| 5232 | try type_of_params.push(type_of_arg); |
| 5174 | 5233 | return_expr.rhs = expr; |
| 5175 | 5234 | |
| 5176 | 5235 | block.rbrace = try appendToken(c, .RBrace, "}"); |
| 5177 | | try block.statements.push(&return_expr.base); |
| 5236 | try block_statements.push(&return_expr.base); |
| 5178 | 5237 | fn_proto.body_node = &block.base; |
| 5179 | 5238 | _ = try c.global_scope.macro_table.put(name, &fn_proto.base); |
| 5180 | 5239 | } |
| ... | ... | @@ -5208,14 +5267,14 @@ fn parseCExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, source_ |
| 5208 | 5267 | .Comma => { |
| 5209 | 5268 | _ = try appendToken(c, .Semicolon, ";"); |
| 5210 | 5269 | 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)); |
| 5212 | 5271 | |
| 5213 | 5272 | var last = node; |
| 5214 | 5273 | while (true) { |
| 5215 | 5274 | // suppress result |
| 5216 | 5275 | const lhs = try transCreateNodeIdentifier(c, "_"); |
| 5217 | 5276 | 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); |
| 5219 | 5278 | op_node.* = .{ |
| 5220 | 5279 | .op_token = op_token, |
| 5221 | 5280 | .lhs = lhs, |
| ... | ... | @@ -5253,11 +5312,11 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl |
| 5253 | 5312 | switch (lit_bytes[1]) { |
| 5254 | 5313 | '0'...'7' => { |
| 5255 | 5314 | // 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}); |
| 5257 | 5316 | }, |
| 5258 | 5317 | 'X' => { |
| 5259 | 5318 | // 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..]}); |
| 5261 | 5320 | }, |
| 5262 | 5321 | else => {}, |
| 5263 | 5322 | } |
| ... | ... | @@ -5288,7 +5347,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl |
| 5288 | 5347 | return &cast_node.base; |
| 5289 | 5348 | } else if (tok.id == .FloatLiteral) { |
| 5290 | 5349 | 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}); |
| 5292 | 5351 | if (tok.id.FloatLiteral == .None) { |
| 5293 | 5352 | return transCreateNodeFloat(c, lit_bytes); |
| 5294 | 5353 | } |
| ... | ... | @@ -5318,7 +5377,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const |
| 5318 | 5377 | break; |
| 5319 | 5378 | } |
| 5320 | 5379 | } 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); |
| 5322 | 5381 | var state: enum { |
| 5323 | 5382 | Start, |
| 5324 | 5383 | Escape, |
| ... | ... | @@ -5472,14 +5531,14 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5472 | 5531 | const first_tok = it.list.at(0); |
| 5473 | 5532 | if (source[tok.start] != '\'' or source[tok.start + 1] == '\\' or tok.end - tok.start == 3) { |
| 5474 | 5533 | 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); |
| 5476 | 5535 | node.* = .{ |
| 5477 | 5536 | .token = token, |
| 5478 | 5537 | }; |
| 5479 | 5538 | return &node.base; |
| 5480 | 5539 | } else { |
| 5481 | 5540 | 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); |
| 5483 | 5542 | node.* = .{ |
| 5484 | 5543 | .token = token, |
| 5485 | 5544 | }; |
| ... | ... | @@ -5489,7 +5548,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5489 | 5548 | .StringLiteral => { |
| 5490 | 5549 | const first_tok = it.list.at(0); |
| 5491 | 5550 | 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); |
| 5493 | 5552 | node.* = .{ |
| 5494 | 5553 | .token = token, |
| 5495 | 5554 | }; |
| ... | ... | @@ -5572,7 +5631,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5572 | 5631 | const type_info_node = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); |
| 5573 | 5632 | try type_info_node.params.push(inner_node); |
| 5574 | 5633 | 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); |
| 5576 | 5635 | cmp_node.* = .{ |
| 5577 | 5636 | .op_token = try appendToken(c, .EqualEqual, "=="), |
| 5578 | 5637 | .lhs = &type_info_node.base, |
| ... | ... | @@ -5597,7 +5656,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5597 | 5656 | as_node.rparen_token = try appendToken(c, .RParen, ")"); |
| 5598 | 5657 | else_node.body = &as_node.base; |
| 5599 | 5658 | |
| 5600 | | const group_node = try c.a().create(ast.Node.GroupedExpression); |
| 5659 | const group_node = try c.arena.create(ast.Node.GroupedExpression); |
| 5601 | 5660 | group_node.* = .{ |
| 5602 | 5661 | .lparen = lparen, |
| 5603 | 5662 | .expr = &if_node.base, |
| ... | ... | @@ -5621,7 +5680,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5621 | 5680 | type_of_1.rparen_token = try appendToken(c, .RParen, ")"); |
| 5622 | 5681 | type_info_1.rparen_token = try appendToken(c, .RParen, ")"); |
| 5623 | 5682 | |
| 5624 | | const cmp_1 = try c.a().create(ast.Node.InfixOp); |
| 5683 | const cmp_1 = try c.arena.create(ast.Node.InfixOp); |
| 5625 | 5684 | cmp_1.* = .{ |
| 5626 | 5685 | .op_token = try appendToken(c, .EqualEqual, "=="), |
| 5627 | 5686 | .lhs = &type_info_1.base, |
| ... | ... | @@ -5633,7 +5692,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5633 | 5692 | |
| 5634 | 5693 | const period_tok = try appendToken(c, .Period, "."); |
| 5635 | 5694 | 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); |
| 5637 | 5696 | inner_node_child.* = .{ |
| 5638 | 5697 | .op_token = period_tok, |
| 5639 | 5698 | .lhs = inner_node, |
| ... | ... | @@ -5669,7 +5728,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5669 | 5728 | type_of_2.rparen_token = try appendToken(c, .RParen, ")"); |
| 5670 | 5729 | type_info_2.rparen_token = try appendToken(c, .RParen, ")"); |
| 5671 | 5730 | |
| 5672 | | const cmp_2 = try c.a().create(ast.Node.InfixOp); |
| 5731 | const cmp_2 = try c.arena.create(ast.Node.InfixOp); |
| 5673 | 5732 | cmp_2.* = .{ |
| 5674 | 5733 | .op_token = try appendToken(c, .EqualEqual, "=="), |
| 5675 | 5734 | .lhs = &type_info_2.base, |
| ... | ... | @@ -5677,7 +5736,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5677 | 5736 | .rhs = try transCreateNodeEnumLiteral(c, "Int"), |
| 5678 | 5737 | }; |
| 5679 | 5738 | 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); |
| 5681 | 5740 | cmp_4.* = .{ |
| 5682 | 5741 | .op_token = try appendToken(c, .Keyword_and, "and"), |
| 5683 | 5742 | .lhs = &cmp_2.base, |
| ... | ... | @@ -5687,7 +5746,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5687 | 5746 | const type_info_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); |
| 5688 | 5747 | try type_info_3.params.push(inner_node); |
| 5689 | 5748 | 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); |
| 5691 | 5750 | cmp_3.* = .{ |
| 5692 | 5751 | .op_token = try appendToken(c, .EqualEqual, "=="), |
| 5693 | 5752 | .lhs = &type_info_3.base, |
| ... | ... | @@ -5714,7 +5773,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5714 | 5773 | as.rparen_token = try appendToken(c, .RParen, ")"); |
| 5715 | 5774 | else_2.body = &as.base; |
| 5716 | 5775 | |
| 5717 | | const group_node = try c.a().create(ast.Node.GroupedExpression); |
| 5776 | const group_node = try c.arena.create(ast.Node.GroupedExpression); |
| 5718 | 5777 | group_node.* = .{ |
| 5719 | 5778 | .lparen = lparen, |
| 5720 | 5779 | .expr = &if_1.base, |
| ... | ... | @@ -5740,7 +5799,7 @@ fn macroBoolToInt(c: *Context, node: *ast.Node) !*ast.Node { |
| 5740 | 5799 | if (!isBoolRes(node)) { |
| 5741 | 5800 | if (node.id != .InfixOp) return node; |
| 5742 | 5801 | |
| 5743 | | const group_node = try c.a().create(ast.Node.GroupedExpression); |
| 5802 | const group_node = try c.arena.create(ast.Node.GroupedExpression); |
| 5744 | 5803 | group_node.* = .{ |
| 5745 | 5804 | .lparen = try appendToken(c, .LParen, "("), |
| 5746 | 5805 | .expr = node, |
| ... | ... | @@ -5759,7 +5818,7 @@ fn macroIntToBool(c: *Context, node: *ast.Node) !*ast.Node { |
| 5759 | 5818 | if (isBoolRes(node)) { |
| 5760 | 5819 | if (node.id != .InfixOp) return node; |
| 5761 | 5820 | |
| 5762 | | const group_node = try c.a().create(ast.Node.GroupedExpression); |
| 5821 | const group_node = try c.arena.create(ast.Node.GroupedExpression); |
| 5763 | 5822 | group_node.* = .{ |
| 5764 | 5823 | .lparen = try appendToken(c, .LParen, "("), |
| 5765 | 5824 | .expr = node, |
| ... | ... | @@ -5770,14 +5829,14 @@ fn macroIntToBool(c: *Context, node: *ast.Node) !*ast.Node { |
| 5770 | 5829 | |
| 5771 | 5830 | const op_token = try appendToken(c, .BangEqual, "!="); |
| 5772 | 5831 | 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); |
| 5774 | 5833 | res.* = .{ |
| 5775 | 5834 | .op_token = op_token, |
| 5776 | 5835 | .lhs = node, |
| 5777 | 5836 | .op = .BangEqual, |
| 5778 | 5837 | .rhs = zero, |
| 5779 | 5838 | }; |
| 5780 | | const group_node = try c.a().create(ast.Node.GroupedExpression); |
| 5839 | const group_node = try c.arena.create(ast.Node.GroupedExpression); |
| 5781 | 5840 | group_node.* = .{ |
| 5782 | 5841 | .lparen = try appendToken(c, .LParen, "("), |
| 5783 | 5842 | .expr = &res.base, |
| ... | ... | @@ -5989,7 +6048,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5989 | 6048 | const cast_fn = if (bool_op) macroIntToBool else macroBoolToInt; |
| 5990 | 6049 | const lhs_node = try cast_fn(c, node); |
| 5991 | 6050 | 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); |
| 5993 | 6052 | op_node.* = .{ |
| 5994 | 6053 | .op_token = op_token, |
| 5995 | 6054 | .lhs = lhs_node, |
| ... | ... | @@ -6037,7 +6096,7 @@ fn parseCPrefixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 6037 | 6096 | } |
| 6038 | 6097 | |
| 6039 | 6098 | fn tokenSlice(c: *Context, token: ast.TokenIndex) []u8 { |
| 6040 | | const tok = c.tree.tokens.at(token); |
| 6099 | const tok = c.tokens.items[token]; |
| 6041 | 6100 | const slice = c.source_buffer.span()[tok.start..tok.end]; |
| 6042 | 6101 | return if (mem.startsWith(u8, slice, "@\"")) |
| 6043 | 6102 | slice[2 .. slice.len - 1] |
| ... | ... | @@ -6060,9 +6119,10 @@ fn getContainer(c: *Context, node: *ast.Node) ?*ast.Node { |
| 6060 | 6119 | return null; |
| 6061 | 6120 | if (getContainerTypeOf(c, infix.lhs)) |ty_node| { |
| 6062 | 6121 | 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).?; |
| 6066 | 6126 | const ident = infix.rhs.cast(ast.Node.Identifier).?; |
| 6067 | 6127 | if (mem.eql(u8, tokenSlice(c, field.name_token), tokenSlice(c, ident.token))) { |
| 6068 | 6128 | return getContainer(c, field.type_expr.?); |
| ... | ... | @@ -6087,9 +6147,10 @@ fn getContainerTypeOf(c: *Context, ref: *ast.Node) ?*ast.Node { |
| 6087 | 6147 | return null; |
| 6088 | 6148 | if (getContainerTypeOf(c, infix.lhs)) |ty_node| { |
| 6089 | 6149 | 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).?; |
| 6093 | 6154 | const ident = infix.rhs.cast(ast.Node.Identifier).?; |
| 6094 | 6155 | if (mem.eql(u8, tokenSlice(c, field.name_token), tokenSlice(c, ident.token))) { |
| 6095 | 6156 | return getContainer(c, field.type_expr.?); |