authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-23 20:14:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-23 20:14:38-04:00
logf771545a7ecfa77ad18745b11d80d0f5238ecb65
tree921c2313fa60e27e6bc2fdc4a98cad82714d0aa2
parentc78a2e2e8dc50a6e57ad4d390184e564dbe7be67

revert std.zig.ast iterator changes back to master branch API


1 files changed, 105 insertions(+), 381 deletions(-)

lib/std/zig/ast.zig+105-381
...@@ -399,22 +399,6 @@ pub const Error = union(enum) {...@@ -399,22 +399,6 @@ pub const Error = union(enum) {
399pub const Node = struct {399pub const Node = struct {
400 id: Id,400 id: Id,
401401
402 /// All the child Node types use this same Iterator state for their iteration.
403 pub const Iterator = struct {
404 parent_node: *const Node,
405 index: usize,
406
407 pub fn next(it: *Iterator) ?*Node {
408 inline for (@typeInfo(Id).Enum.fields) |f| {
409 if (it.parent_node.id == @field(Id, f.name)) {
410 const T = @field(Node, f.name);
411 return @fieldParentPtr(T, "base", it.parent_node).iterateNext(it);
412 }
413 }
414 unreachable;
415 }
416 };
417
418 pub const Id = enum {402 pub const Id = enum {
419 // Top level403 // Top level
420 Root,404 Root,
...@@ -497,11 +481,11 @@ pub const Node = struct {...@@ -497,11 +481,11 @@ pub const Node = struct {
497 return null;481 return null;
498 }482 }
499483
500 pub fn iterate(base: *Node) Iterator {484 pub fn iterate(base: *Node, index: usize) ?*Node {
501 inline for (@typeInfo(Id).Enum.fields) |f| {485 inline for (@typeInfo(Id).Enum.fields) |f| {
502 if (base.id == @field(Id, f.name)) {486 if (base.id == @field(Id, f.name)) {
503 const T = @field(Node, f.name);487 const T = @field(Node, f.name);
504 return @fieldParentPtr(T, "base", base).iterate();488 return @fieldParentPtr(T, "base", base).iterate(index);
505 }489 }
506 }490 }
507 unreachable;491 unreachable;
...@@ -647,13 +631,8 @@ pub const Node = struct {...@@ -647,13 +631,8 @@ pub const Node = struct {
647 allocator.free(bytes);631 allocator.free(bytes);
648 }632 }
649633
650 pub fn iterate(self: *const Root) Node.Iterator {634 pub fn iterate(self: *const Root, index: usize) ?*Node {
651 return .{ .parent_node = &self.base, .index = 0 };635 var i = index;
652 }
653
654 pub fn iterateNext(self: *const Root, it: *Node.Iterator) ?*Node {
655 var i = it.index;
656 it.index += 1;
657636
658 if (i < self.decls_len) return self.declsConst()[i];637 if (i < self.decls_len) return self.declsConst()[i];
659 return null;638 return null;
...@@ -701,13 +680,8 @@ pub const Node = struct {...@@ -701,13 +680,8 @@ pub const Node = struct {
701 init_node: ?*Node,680 init_node: ?*Node,
702 semicolon_token: TokenIndex,681 semicolon_token: TokenIndex,
703682
704 pub fn iterate(self: *const VarDecl) Node.Iterator {683 pub fn iterate(self: *const VarDecl, index: usize) ?*Node {
705 return .{ .parent_node = &self.base, .index = 0 };684 var i = index;
706 }
707
708 pub fn iterateNext(self: *const VarDecl, it: *Node.Iterator) ?*Node {
709 var i = it.index;
710 it.index += 1;
711685
712 if (self.type_node) |type_node| {686 if (self.type_node) |type_node| {
713 if (i < 1) return type_node;687 if (i < 1) return type_node;
...@@ -755,13 +729,8 @@ pub const Node = struct {...@@ -755,13 +729,8 @@ pub const Node = struct {
755 expr: *Node,729 expr: *Node,
756 semicolon_token: TokenIndex,730 semicolon_token: TokenIndex,
757731
758 pub fn iterate(self: *const Use) Node.Iterator {732 pub fn iterate(self: *const Use, index: usize) ?*Node {
759 return .{ .parent_node = &self.base, .index = 0 };733 var i = index;
760 }
761
762 pub fn iterateNext(self: *const Use, it: *Node.Iterator) ?*Node {
763 var i = it.index;
764 it.index += 1;
765734
766 if (i < 1) return self.expr;735 if (i < 1) return self.expr;
767 i -= 1;736 i -= 1;
...@@ -796,13 +765,8 @@ pub const Node = struct {...@@ -796,13 +765,8 @@ pub const Node = struct {
796 allocator.free(bytes);765 allocator.free(bytes);
797 }766 }
798767
799 pub fn iterate(self: *const ErrorSetDecl) Node.Iterator {768 pub fn iterate(self: *const ErrorSetDecl, index: usize) ?*Node {
800 return .{ .parent_node = &self.base, .index = 0 };769 var i = index;
801 }
802
803 pub fn iterateNext(self: *const ErrorSetDecl, it: *Node.Iterator) ?*Node {
804 var i = it.index;
805 it.index += 1;
806770
807 if (i < self.decls_len) return self.declsConst()[i];771 if (i < self.decls_len) return self.declsConst()[i];
808 i -= self.decls_len;772 i -= self.decls_len;
...@@ -860,13 +824,8 @@ pub const Node = struct {...@@ -860,13 +824,8 @@ pub const Node = struct {
860 allocator.free(bytes);824 allocator.free(bytes);
861 }825 }
862826
863 pub fn iterate(self: *const ContainerDecl) Node.Iterator {827 pub fn iterate(self: *const ContainerDecl, index: usize) ?*Node {
864 return .{ .parent_node = &self.base, .index = 0 };828 var i = index;
865 }
866
867 pub fn iterateNext(self: *const ContainerDecl, it: *Node.Iterator) ?*Node {
868 var i = it.index;
869 it.index += 1;
870829
871 switch (self.init_arg_expr) {830 switch (self.init_arg_expr) {
872 .Type => |t| {831 .Type => |t| {
...@@ -917,13 +876,8 @@ pub const Node = struct {...@@ -917,13 +876,8 @@ pub const Node = struct {
917 value_expr: ?*Node,876 value_expr: ?*Node,
918 align_expr: ?*Node,877 align_expr: ?*Node,
919878
920 pub fn iterate(self: *const ContainerField) Node.Iterator {879 pub fn iterate(self: *const ContainerField, index: usize) ?*Node {
921 return .{ .parent_node = &self.base, .index = 0 };880 var i = index;
922 }
923
924 pub fn iterateNext(self: *const ContainerField, it: *Node.Iterator) ?*Node {
925 var i = it.index;
926 it.index += 1;
927881
928 if (self.type_expr) |type_expr| {882 if (self.type_expr) |type_expr| {
929 if (i < 1) return type_expr;883 if (i < 1) return type_expr;
...@@ -969,13 +923,8 @@ pub const Node = struct {...@@ -969,13 +923,8 @@ pub const Node = struct {
969 doc_comments: ?*DocComment,923 doc_comments: ?*DocComment,
970 name_token: TokenIndex,924 name_token: TokenIndex,
971925
972 pub fn iterate(self: *const ErrorTag) Node.Iterator {926 pub fn iterate(self: *const ErrorTag, index: usize) ?*Node {
973 return .{ .parent_node = &self.base, .index = 0 };927 var i = index;
974 }
975
976 pub fn iterateNext(self: *const ErrorTag, it: *Node.Iterator) ?*Node {
977 var i = it.index;
978 it.index += 1;
979928
980 if (self.doc_comments) |comments| {929 if (self.doc_comments) |comments| {
981 if (i < 1) return &comments.base;930 if (i < 1) return &comments.base;
...@@ -998,11 +947,7 @@ pub const Node = struct {...@@ -998,11 +947,7 @@ pub const Node = struct {
998 base: Node = Node{ .id = .Identifier },947 base: Node = Node{ .id = .Identifier },
999 token: TokenIndex,948 token: TokenIndex,
1000949
1001 pub fn iterate(self: *const Identifier) Node.Iterator {950 pub fn iterate(self: *const Identifier, index: usize) ?*Node {
1002 return .{ .parent_node = &self.base, .index = 0 };
1003 }
1004
1005 pub fn iterateNext(self: *const Identifier, it: *Node.Iterator) ?*Node {
1006 return null;951 return null;
1007 }952 }
1008953
...@@ -1053,13 +998,8 @@ pub const Node = struct {...@@ -1053,13 +998,8 @@ pub const Node = struct {
1053 type_expr: *Node,998 type_expr: *Node,
1054 };999 };
10551000
1056 pub fn iterate(self: *const ParamDecl) Node.Iterator {1001 pub fn iterate(self: *const ParamDecl, index: usize) ?*Node {
1057 return .{ .parent_node = &self.base, .index = 0 };1002 var i = index;
1058 }
1059
1060 pub fn iterateNext(self: *const ParamDecl, it: *Node.Iterator) ?*Node {
1061 var i = it.index;
1062 it.index += 1;
10631003
1064 if (i < 1) {1004 if (i < 1) {
1065 switch (self.param_type) {1005 switch (self.param_type) {
...@@ -1101,30 +1041,26 @@ pub const Node = struct {...@@ -1101,30 +1041,26 @@ pub const Node = struct {
1101 allocator.free(bytes);1041 allocator.free(bytes);
1102 }1042 }
11031043
1104 pub fn iterate(self: *const FnProto) Node.Iterator {1044 pub fn iterate(self: *const FnProto, index: usize) ?*Node {
1105 return .{ .parent_node = &self.base, .index = 0 };1045 var i = index;
1106 }
1107
1108 pub fn iterateNext(self: *const FnProto, it: *Node.Iterator) ?*Node {
1109 var i = it.index;
1110 it.index += 1;
11111046
1112 if (self.lib_name) |lib_name| {1047 if (self.lib_name) |lib_name| {
1113 if (i < 1) return lib_name;1048 if (i < 1) return lib_name;
1114 i -= 1;1049 i -= 1;
1115 }1050 }
11161051
1117 if (i < self.params_len) {1052 const params_len = switch (self.paramsConst()[self.params_len - 1].param_type) {
1053 .var_type, .type_expr => self.params_len,
1054 .var_args => self.params_len - 1,
1055 };
1056 if (i < params_len) {
1118 switch (self.paramsConst()[i].param_type) {1057 switch (self.paramsConst()[i].param_type) {
1119 .var_type => |n| return n,1058 .var_type => |n| return n,
1120 .var_args => {1059 .var_args => unreachable,
1121 i += 1;
1122 it.index += 1;
1123 },
1124 .type_expr => |n| return n,1060 .type_expr => |n| return n,
1125 }1061 }
1126 }1062 }
1127 i -= self.params_len;1063 i -= params_len;
11281064
1129 if (self.align_expr) |align_expr| {1065 if (self.align_expr) |align_expr| {
1130 if (i < 1) return align_expr;1066 if (i < 1) return align_expr;
...@@ -1192,13 +1128,8 @@ pub const Node = struct {...@@ -1192,13 +1128,8 @@ pub const Node = struct {
1192 return_type: *Node,1128 return_type: *Node,
1193 };1129 };
11941130
1195 pub fn iterate(self: *const AnyFrameType) Node.Iterator {1131 pub fn iterate(self: *const AnyFrameType, index: usize) ?*Node {
1196 return .{ .parent_node = &self.base, .index = 0 };1132 var i = index;
1197 }
1198
1199 pub fn iterateNext(self: *const AnyFrameType, it: *Node.Iterator) ?*Node {
1200 var i = it.index;
1201 it.index += 1;
12021133
1203 if (self.result) |result| {1134 if (self.result) |result| {
1204 if (i < 1) return result.return_type;1135 if (i < 1) return result.return_type;
...@@ -1237,13 +1168,8 @@ pub const Node = struct {...@@ -1237,13 +1168,8 @@ pub const Node = struct {
1237 allocator.free(bytes);1168 allocator.free(bytes);
1238 }1169 }
12391170
1240 pub fn iterate(self: *const Block) Node.Iterator {1171 pub fn iterate(self: *const Block, index: usize) ?*Node {
1241 return .{ .parent_node = &self.base, .index = 0 };1172 var i = index;
1242 }
1243
1244 pub fn iterateNext(self: *const Block, it: *Node.Iterator) ?*Node {
1245 var i = it.index;
1246 it.index += 1;
12471173
1248 if (i < self.statements_len) return self.statementsConst()[i];1174 if (i < self.statements_len) return self.statementsConst()[i];
1249 i -= self.statements_len;1175 i -= self.statements_len;
...@@ -1284,13 +1210,8 @@ pub const Node = struct {...@@ -1284,13 +1210,8 @@ pub const Node = struct {
1284 payload: ?*Node,1210 payload: ?*Node,
1285 expr: *Node,1211 expr: *Node,
12861212
1287 pub fn iterate(self: *const Defer) Node.Iterator {1213 pub fn iterate(self: *const Defer, index: usize) ?*Node {
1288 return .{ .parent_node = &self.base, .index = 0 };1214 var i = index;
1289 }
1290
1291 pub fn iterateNext(self: *const Defer, it: *Node.Iterator) ?*Node {
1292 var i = it.index;
1293 it.index += 1;
12941215
1295 if (i < 1) return self.expr;1216 if (i < 1) return self.expr;
1296 i -= 1;1217 i -= 1;
...@@ -1313,13 +1234,8 @@ pub const Node = struct {...@@ -1313,13 +1234,8 @@ pub const Node = struct {
1313 comptime_token: TokenIndex,1234 comptime_token: TokenIndex,
1314 expr: *Node,1235 expr: *Node,
13151236
1316 pub fn iterate(self: *const Comptime) Node.Iterator {1237 pub fn iterate(self: *const Comptime, index: usize) ?*Node {
1317 return .{ .parent_node = &self.base, .index = 0 };1238 var i = index;
1318 }
1319
1320 pub fn iterateNext(self: *const Comptime, it: *Node.Iterator) ?*Node {
1321 var i = it.index;
1322 it.index += 1;
13231239
1324 if (i < 1) return self.expr;1240 if (i < 1) return self.expr;
1325 i -= 1;1241 i -= 1;
...@@ -1341,13 +1257,8 @@ pub const Node = struct {...@@ -1341,13 +1257,8 @@ pub const Node = struct {
1341 nosuspend_token: TokenIndex,1257 nosuspend_token: TokenIndex,
1342 expr: *Node,1258 expr: *Node,
13431259
1344 pub fn iterate(self: *const Nosuspend) Node.Iterator {1260 pub fn iterate(self: *const Nosuspend, index: usize) ?*Node {
1345 return .{ .parent_node = &self.base, .index = 0 };1261 var i = index;
1346 }
1347
1348 pub fn iterateNext(self: *const Nosuspend, it: *Node.Iterator) ?*Node {
1349 var i = it.index;
1350 it.index += 1;
13511262
1352 if (i < 1) return self.expr;1263 if (i < 1) return self.expr;
1353 i -= 1;1264 i -= 1;
...@@ -1370,13 +1281,8 @@ pub const Node = struct {...@@ -1370,13 +1281,8 @@ pub const Node = struct {
1370 error_symbol: *Node,1281 error_symbol: *Node,
1371 rpipe: TokenIndex,1282 rpipe: TokenIndex,
13721283
1373 pub fn iterate(self: *const Payload) Node.Iterator {1284 pub fn iterate(self: *const Payload, index: usize) ?*Node {
1374 return .{ .parent_node = &self.base, .index = 0 };1285 var i = index;
1375 }
1376
1377 pub fn iterateNext(self: *const Payload, it: *Node.Iterator) ?*Node {
1378 var i = it.index;
1379 it.index += 1;
13801286
1381 if (i < 1) return self.error_symbol;1287 if (i < 1) return self.error_symbol;
1382 i -= 1;1288 i -= 1;
...@@ -1400,13 +1306,8 @@ pub const Node = struct {...@@ -1400,13 +1306,8 @@ pub const Node = struct {
1400 value_symbol: *Node,1306 value_symbol: *Node,
1401 rpipe: TokenIndex,1307 rpipe: TokenIndex,
14021308
1403 pub fn iterate(self: *const PointerPayload) Node.Iterator {1309 pub fn iterate(self: *const PointerPayload, index: usize) ?*Node {
1404 return .{ .parent_node = &self.base, .index = 0 };1310 var i = index;
1405 }
1406
1407 pub fn iterateNext(self: *const PointerPayload, it: *Node.Iterator) ?*Node {
1408 var i = it.index;
1409 it.index += 1;
14101311
1411 if (i < 1) return self.value_symbol;1312 if (i < 1) return self.value_symbol;
1412 i -= 1;1313 i -= 1;
...@@ -1431,13 +1332,8 @@ pub const Node = struct {...@@ -1431,13 +1332,8 @@ pub const Node = struct {
1431 index_symbol: ?*Node,1332 index_symbol: ?*Node,
1432 rpipe: TokenIndex,1333 rpipe: TokenIndex,
14331334
1434 pub fn iterate(self: *const PointerIndexPayload) Node.Iterator {1335 pub fn iterate(self: *const PointerIndexPayload, index: usize) ?*Node {
1435 return .{ .parent_node = &self.base, .index = 0 };1336 var i = index;
1436 }
1437
1438 pub fn iterateNext(self: *const PointerIndexPayload, it: *Node.Iterator) ?*Node {
1439 var i = it.index;
1440 it.index += 1;
14411337
1442 if (i < 1) return self.value_symbol;1338 if (i < 1) return self.value_symbol;
1443 i -= 1;1339 i -= 1;
...@@ -1465,13 +1361,8 @@ pub const Node = struct {...@@ -1465,13 +1361,8 @@ pub const Node = struct {
1465 payload: ?*Node,1361 payload: ?*Node,
1466 body: *Node,1362 body: *Node,
14671363
1468 pub fn iterate(self: *const Else) Node.Iterator {1364 pub fn iterate(self: *const Else, index: usize) ?*Node {
1469 return .{ .parent_node = &self.base, .index = 0 };1365 var i = index;
1470 }
1471
1472 pub fn iterateNext(self: *const Else, it: *Node.Iterator) ?*Node {
1473 var i = it.index;
1474 it.index += 1;
14751366
1476 if (self.payload) |payload| {1367 if (self.payload) |payload| {
1477 if (i < 1) return payload;1368 if (i < 1) return payload;
...@@ -1513,13 +1404,8 @@ pub const Node = struct {...@@ -1513,13 +1404,8 @@ pub const Node = struct {
1513 allocator.free(bytes);1404 allocator.free(bytes);
1514 }1405 }
15151406
1516 pub fn iterate(self: *const Switch) Node.Iterator {1407 pub fn iterate(self: *const Switch, index: usize) ?*Node {
1517 return .{ .parent_node = &self.base, .index = 0 };1408 var i = index;
1518 }
1519
1520 pub fn iterateNext(self: *const Switch, it: *Node.Iterator) ?*Node {
1521 var i = it.index;
1522 it.index += 1;
15231409
1524 if (i < 1) return self.expr;1410 if (i < 1) return self.expr;
1525 i -= 1;1411 i -= 1;
...@@ -1572,13 +1458,8 @@ pub const Node = struct {...@@ -1572,13 +1458,8 @@ pub const Node = struct {
1572 allocator.free(bytes);1458 allocator.free(bytes);
1573 }1459 }
15741460
1575 pub fn iterate(self: *const SwitchCase) Node.Iterator {1461 pub fn iterate(self: *const SwitchCase, index: usize) ?*Node {
1576 return .{ .parent_node = &self.base, .index = 0 };1462 var i = index;
1577 }
1578
1579 pub fn iterateNext(self: *const SwitchCase, it: *Node.Iterator) ?*Node {
1580 var i = it.index;
1581 it.index += 1;
15821463
1583 if (i < self.items_len) return self.itemsConst()[i];1464 if (i < self.items_len) return self.itemsConst()[i];
1584 i -= self.items_len;1465 i -= self.items_len;
...@@ -1621,11 +1502,7 @@ pub const Node = struct {...@@ -1621,11 +1502,7 @@ pub const Node = struct {
1621 base: Node = Node{ .id = .SwitchElse },1502 base: Node = Node{ .id = .SwitchElse },
1622 token: TokenIndex,1503 token: TokenIndex,
16231504
1624 pub fn iterate(self: *const SwitchElse) Node.Iterator {1505 pub fn iterate(self: *const SwitchElse, index: usize) ?*Node {
1625 return .{ .parent_node = &self.base, .index = 0 };
1626 }
1627
1628 pub fn iterateNext(self: *const SwitchElse, it: *Node.Iterator) ?*Node {
1629 return null;1506 return null;
1630 }1507 }
16311508
...@@ -1649,13 +1526,8 @@ pub const Node = struct {...@@ -1649,13 +1526,8 @@ pub const Node = struct {
1649 body: *Node,1526 body: *Node,
1650 @"else": ?*Else,1527 @"else": ?*Else,
16511528
1652 pub fn iterate(self: *const While) Node.Iterator {1529 pub fn iterate(self: *const While, index: usize) ?*Node {
1653 return .{ .parent_node = &self.base, .index = 0 };1530 var i = index;
1654 }
1655
1656 pub fn iterateNext(self: *const While, it: *Node.Iterator) ?*Node {
1657 var i = it.index;
1658 it.index += 1;
16591531
1660 if (i < 1) return self.condition;1532 if (i < 1) return self.condition;
1661 i -= 1;1533 i -= 1;
...@@ -1712,13 +1584,8 @@ pub const Node = struct {...@@ -1712,13 +1584,8 @@ pub const Node = struct {
1712 body: *Node,1584 body: *Node,
1713 @"else": ?*Else,1585 @"else": ?*Else,
17141586
1715 pub fn iterate(self: *const For) Node.Iterator {1587 pub fn iterate(self: *const For, index: usize) ?*Node {
1716 return .{ .parent_node = &self.base, .index = 0 };1588 var i = index;
1717 }
1718
1719 pub fn iterateNext(self: *const For, it: *Node.Iterator) ?*Node {
1720 var i = it.index;
1721 it.index += 1;
17221589
1723 if (i < 1) return self.array_expr;1590 if (i < 1) return self.array_expr;
1724 i -= 1;1591 i -= 1;
...@@ -1766,13 +1633,8 @@ pub const Node = struct {...@@ -1766,13 +1633,8 @@ pub const Node = struct {
1766 body: *Node,1633 body: *Node,
1767 @"else": ?*Else,1634 @"else": ?*Else,
17681635
1769 pub fn iterate(self: *const If) Node.Iterator {1636 pub fn iterate(self: *const If, index: usize) ?*Node {
1770 return .{ .parent_node = &self.base, .index = 0 };1637 var i = index;
1771 }
1772
1773 pub fn iterateNext(self: *const If, it: *Node.Iterator) ?*Node {
1774 var i = it.index;
1775 it.index += 1;
17761638
1777 if (i < 1) return self.condition;1639 if (i < 1) return self.condition;
1778 i -= 1;1640 i -= 1;
...@@ -1859,13 +1721,8 @@ pub const Node = struct {...@@ -1859,13 +1721,8 @@ pub const Node = struct {
1859 UnwrapOptional,1721 UnwrapOptional,
1860 };1722 };
18611723
1862 pub fn iterate(self: *const InfixOp) Node.Iterator {1724 pub fn iterate(self: *const InfixOp, index: usize) ?*Node {
1863 return .{ .parent_node = &self.base, .index = 0 };1725 var i = index;
1864 }
1865
1866 pub fn iterateNext(self: *const InfixOp, it: *Node.Iterator) ?*Node {
1867 var i = it.index;
1868 it.index += 1;
18691726
1870 if (i < 1) return self.lhs;1727 if (i < 1) return self.lhs;
1871 i -= 1;1728 i -= 1;
...@@ -1982,13 +1839,8 @@ pub const Node = struct {...@@ -1982,13 +1839,8 @@ pub const Node = struct {
1982 };1839 };
1983 };1840 };
19841841
1985 pub fn iterate(self: *const PrefixOp) Node.Iterator {1842 pub fn iterate(self: *const PrefixOp, index: usize) ?*Node {
1986 return .{ .parent_node = &self.base, .index = 0 };1843 var i = index;
1987 }
1988
1989 pub fn iterateNext(self: *const PrefixOp, it: *Node.Iterator) ?*Node {
1990 var i = it.index;
1991 it.index += 1;
19921844
1993 switch (self.op) {1845 switch (self.op) {
1994 .PtrType, .SliceType => |addr_of_info| {1846 .PtrType, .SliceType => |addr_of_info| {
...@@ -2045,13 +1897,8 @@ pub const Node = struct {...@@ -2045,13 +1897,8 @@ pub const Node = struct {
2045 name_token: TokenIndex,1897 name_token: TokenIndex,
2046 expr: *Node,1898 expr: *Node,
20471899
2048 pub fn iterate(self: *const FieldInitializer) Node.Iterator {1900 pub fn iterate(self: *const FieldInitializer, index: usize) ?*Node {
2049 return .{ .parent_node = &self.base, .index = 0 };1901 var i = index;
2050 }
2051
2052 pub fn iterateNext(self: *const FieldInitializer, it: *Node.Iterator) ?*Node {
2053 var i = it.index;
2054 it.index += 1;
20551902
2056 if (i < 1) return self.expr;1903 if (i < 1) return self.expr;
2057 i -= 1;1904 i -= 1;
...@@ -2086,13 +1933,8 @@ pub const Node = struct {...@@ -2086,13 +1933,8 @@ pub const Node = struct {
2086 allocator.free(bytes);1933 allocator.free(bytes);
2087 }1934 }
20881935
2089 pub fn iterate(self: *const ArrayInitializer) Node.Iterator {1936 pub fn iterate(self: *const ArrayInitializer, index: usize) ?*Node {
2090 return .{ .parent_node = &self.base, .index = 0 };1937 var i = index;
2091 }
2092
2093 pub fn iterateNext(self: *const ArrayInitializer, it: *Node.Iterator) ?*Node {
2094 var i = it.index;
2095 it.index += 1;
20961938
2097 if (i < 1) return self.lhs;1939 if (i < 1) return self.lhs;
2098 i -= 1;1940 i -= 1;
...@@ -2144,13 +1986,8 @@ pub const Node = struct {...@@ -2144,13 +1986,8 @@ pub const Node = struct {
2144 allocator.free(bytes);1986 allocator.free(bytes);
2145 }1987 }
21461988
2147 pub fn iterate(self: *const ArrayInitializerDot) Node.Iterator {1989 pub fn iterate(self: *const ArrayInitializerDot, index: usize) ?*Node {
2148 return .{ .parent_node = &self.base, .index = 0 };1990 var i = index;
2149 }
2150
2151 pub fn iterateNext(self: *const ArrayInitializerDot, it: *Node.Iterator) ?*Node {
2152 var i = it.index;
2153 it.index += 1;
21541991
2155 if (i < self.list_len) return self.listConst()[i];1992 if (i < self.list_len) return self.listConst()[i];
2156 i -= self.list_len;1993 i -= self.list_len;
...@@ -2199,13 +2036,8 @@ pub const Node = struct {...@@ -2199,13 +2036,8 @@ pub const Node = struct {
2199 allocator.free(bytes);2036 allocator.free(bytes);
2200 }2037 }
22012038
2202 pub fn iterate(self: *const StructInitializer) Node.Iterator {2039 pub fn iterate(self: *const StructInitializer, index: usize) ?*Node {
2203 return .{ .parent_node = &self.base, .index = 0 };2040 var i = index;
2204 }
2205
2206 pub fn iterateNext(self: *const StructInitializer, it: *Node.Iterator) ?*Node {
2207 var i = it.index;
2208 it.index += 1;
22092041
2210 if (i < 1) return self.lhs;2042 if (i < 1) return self.lhs;
2211 i -= 1;2043 i -= 1;
...@@ -2257,13 +2089,8 @@ pub const Node = struct {...@@ -2257,13 +2089,8 @@ pub const Node = struct {
2257 allocator.free(bytes);2089 allocator.free(bytes);
2258 }2090 }
22592091
2260 pub fn iterate(self: *const StructInitializerDot) Node.Iterator {2092 pub fn iterate(self: *const StructInitializerDot, index: usize) ?*Node {
2261 return .{ .parent_node = &self.base, .index = 0 };2093 var i = index;
2262 }
2263
2264 pub fn iterateNext(self: *const StructInitializerDot, it: *Node.Iterator) ?*Node {
2265 var i = it.index;
2266 it.index += 1;
22672094
2268 if (i < self.list_len) return self.listConst()[i];2095 if (i < self.list_len) return self.listConst()[i];
2269 i -= self.list_len;2096 i -= self.list_len;
...@@ -2313,13 +2140,8 @@ pub const Node = struct {...@@ -2313,13 +2140,8 @@ pub const Node = struct {
2313 allocator.free(bytes);2140 allocator.free(bytes);
2314 }2141 }
23152142
2316 pub fn iterate(self: *const Call) Node.Iterator {2143 pub fn iterate(self: *const Call, index: usize) ?*Node {
2317 return .{ .parent_node = &self.base, .index = 0};2144 var i = index;
2318 }
2319
2320 pub fn iterateNext(self: *const Call, it: *Node.Iterator) ?*Node {
2321 var i = it.index;
2322 it.index += 1;
23232145
2324 if (i < 1) return self.lhs;2146 if (i < 1) return self.lhs;
2325 i -= 1;2147 i -= 1;
...@@ -2373,13 +2195,8 @@ pub const Node = struct {...@@ -2373,13 +2195,8 @@ pub const Node = struct {
2373 };2195 };
2374 };2196 };
23752197
2376 pub fn iterate(self: *const SuffixOp) Node.Iterator {2198 pub fn iterate(self: *const SuffixOp, index: usize) ?*Node {
2377 return .{ .parent_node = &self.base, .index = 0};2199 var i = index;
2378 }
2379
2380 pub fn iterateNext(self: *const SuffixOp, it: *Node.Iterator) ?*Node {
2381 var i = it.index;
2382 it.index += 1;
23832200
2384 if (i == 0) return self.lhs;2201 if (i == 0) return self.lhs;
2385 i -= 1;2202 i -= 1;
...@@ -2425,13 +2242,8 @@ pub const Node = struct {...@@ -2425,13 +2242,8 @@ pub const Node = struct {
2425 expr: *Node,2242 expr: *Node,
2426 rparen: TokenIndex,2243 rparen: TokenIndex,
24272244
2428 pub fn iterate(self: *const GroupedExpression) Node.Iterator {2245 pub fn iterate(self: *const GroupedExpression, index: usize) ?*Node {
2429 return .{ .parent_node = &self.base, .index = 0 };2246 var i = index;
2430 }
2431
2432 pub fn iterateNext(self: *const GroupedExpression, it: *Node.Iterator) ?*Node {
2433 var i = it.index;
2434 it.index += 1;
24352247
2436 if (i < 1) return self.expr;2248 if (i < 1) return self.expr;
2437 i -= 1;2249 i -= 1;
...@@ -2460,13 +2272,8 @@ pub const Node = struct {...@@ -2460,13 +2272,8 @@ pub const Node = struct {
2460 Return,2272 Return,
2461 };2273 };
24622274
2463 pub fn iterate(self: *const ControlFlowExpression) Node.Iterator {2275 pub fn iterate(self: *const ControlFlowExpression, index: usize) ?*Node {
2464 return .{ .parent_node = &self.base, .index = 0 };2276 var i = index;
2465 }
2466
2467 pub fn iterateNext(self: *const ControlFlowExpression, it: *Node.Iterator) ?*Node {
2468 var i = it.index;
2469 it.index += 1;
24702277
2471 switch (self.kind) {2278 switch (self.kind) {
2472 .Break, .Continue => |maybe_label| {2279 .Break, .Continue => |maybe_label| {
...@@ -2513,13 +2320,8 @@ pub const Node = struct {...@@ -2513,13 +2320,8 @@ pub const Node = struct {
2513 suspend_token: TokenIndex,2320 suspend_token: TokenIndex,
2514 body: ?*Node,2321 body: ?*Node,
25152322
2516 pub fn iterate(self: *const Suspend) Node.Iterator {2323 pub fn iterate(self: *const Suspend, index: usize) ?*Node {
2517 return .{ .parent_node = &self.base, .index = 0 };2324 var i = index;
2518 }
2519
2520 pub fn iterateNext(self: *const Suspend, it: *Node.Iterator) ?*Node {
2521 var i = it.index;
2522 it.index += 1;
25232325
2524 if (self.body) |body| {2326 if (self.body) |body| {
2525 if (i < 1) return body;2327 if (i < 1) return body;
...@@ -2546,11 +2348,7 @@ pub const Node = struct {...@@ -2546,11 +2348,7 @@ pub const Node = struct {
2546 base: Node = Node{ .id = .IntegerLiteral },2348 base: Node = Node{ .id = .IntegerLiteral },
2547 token: TokenIndex,2349 token: TokenIndex,
25482350
2549 pub fn iterate(self: *const IntegerLiteral) Node.Iterator {2351 pub fn iterate(self: *const IntegerLiteral, index: usize) ?*Node {
2550 return .{ .parent_node = &self.base, .index = 0 };
2551 }
2552
2553 pub fn iterateNext(self: *const IntegerLiteral, it: *Node.Iterator) ?*Node {
2554 return null;2352 return null;
2555 }2353 }
25562354
...@@ -2568,11 +2366,7 @@ pub const Node = struct {...@@ -2568,11 +2366,7 @@ pub const Node = struct {
2568 dot: TokenIndex,2366 dot: TokenIndex,
2569 name: TokenIndex,2367 name: TokenIndex,
25702368
2571 pub fn iterate(self: *const EnumLiteral) Node.Iterator {2369 pub fn iterate(self: *const EnumLiteral, index: usize) ?*Node {
2572 return .{ .parent_node = &self.base, .index = 0 };
2573 }
2574
2575 pub fn iterateNext(self: *const EnumLiteral, it: *Node.Iterator) ?*Node {
2576 return null;2370 return null;
2577 }2371 }
25782372
...@@ -2589,11 +2383,7 @@ pub const Node = struct {...@@ -2589,11 +2383,7 @@ pub const Node = struct {
2589 base: Node = Node{ .id = .FloatLiteral },2383 base: Node = Node{ .id = .FloatLiteral },
2590 token: TokenIndex,2384 token: TokenIndex,
25912385
2592 pub fn iterate(self: *const FloatLiteral) Node.Iterator {2386 pub fn iterate(self: *const FloatLiteral, index: usize) ?*Node {
2593 return .{ .parent_node = &self.base, .index = 0 };
2594 }
2595
2596 pub fn iterateNext(self: *const FloatLiteral, it: *Node.Iterator) ?*Node {
2597 return null;2387 return null;
2598 }2388 }
25992389
...@@ -2624,13 +2414,8 @@ pub const Node = struct {...@@ -2624,13 +2414,8 @@ pub const Node = struct {
2624 allocator.free(bytes);2414 allocator.free(bytes);
2625 }2415 }
26262416
2627 pub fn iterate(self: *const BuiltinCall) Node.Iterator {2417 pub fn iterate(self: *const BuiltinCall, index: usize) ?*Node {
2628 return .{ .parent_node = &self.base, .index = 0 };2418 var i = index;
2629 }
2630
2631 pub fn iterateNext(self: *const BuiltinCall, it: *Node.Iterator) ?*Node {
2632 var i = it.index;
2633 it.index += 1;
26342419
2635 if (i < self.params_len) return self.paramsConst()[i];2420 if (i < self.params_len) return self.paramsConst()[i];
2636 i -= self.params_len;2421 i -= self.params_len;
...@@ -2665,11 +2450,7 @@ pub const Node = struct {...@@ -2665,11 +2450,7 @@ pub const Node = struct {
2665 base: Node = Node{ .id = .StringLiteral },2450 base: Node = Node{ .id = .StringLiteral },
2666 token: TokenIndex,2451 token: TokenIndex,
26672452
2668 pub fn iterate(self: *const StringLiteral) Node.Iterator {2453 pub fn iterate(self: *const StringLiteral, index: usize) ?*Node {
2669 return .{ .parent_node = &self.base, .index = 0 };
2670 }
2671
2672 pub fn iterateNext(self: *const StringLiteral, it: *Node.Iterator) ?*Node {
2673 return null;2454 return null;
2674 }2455 }
26752456
...@@ -2698,11 +2479,7 @@ pub const Node = struct {...@@ -2698,11 +2479,7 @@ pub const Node = struct {
2698 allocator.free(bytes);2479 allocator.free(bytes);
2699 }2480 }
27002481
2701 pub fn iterate(self: *const MultilineStringLiteral) Node.Iterator {2482 pub fn iterate(self: *const MultilineStringLiteral, index: usize) ?*Node {
2702 return .{ .parent_node = &self.base, .index = 0 };
2703 }
2704
2705 pub fn iterateNext(self: *const MultilineStringLiteral, it: *Node.Iterator) ?*Node {
2706 return null;2483 return null;
2707 }2484 }
27082485
...@@ -2733,11 +2510,7 @@ pub const Node = struct {...@@ -2733,11 +2510,7 @@ pub const Node = struct {
2733 base: Node = Node{ .id = .CharLiteral },2510 base: Node = Node{ .id = .CharLiteral },
2734 token: TokenIndex,2511 token: TokenIndex,
27352512
2736 pub fn iterate(self: *const CharLiteral) Node.Iterator {2513 pub fn iterate(self: *const CharLiteral, index: usize) ?*Node {
2737 return .{ .parent_node = &self.base, .index = 0 };
2738 }
2739
2740 pub fn iterateNext(self: *const CharLiteral, it: *Node.Iterator) ?*Node {
2741 return null;2514 return null;
2742 }2515 }
27432516
...@@ -2754,11 +2527,7 @@ pub const Node = struct {...@@ -2754,11 +2527,7 @@ pub const Node = struct {
2754 base: Node = Node{ .id = .BoolLiteral },2527 base: Node = Node{ .id = .BoolLiteral },
2755 token: TokenIndex,2528 token: TokenIndex,
27562529
2757 pub fn iterate(self: *const BoolLiteral) Node.Iterator {2530 pub fn iterate(self: *const BoolLiteral, index: usize) ?*Node {
2758 return .{ .parent_node = &self.base, .index = 0 };
2759 }
2760
2761 pub fn iterateNext(self: *const BoolLiteral, it: *Node.Iterator) ?*Node {
2762 return null;2531 return null;
2763 }2532 }
27642533
...@@ -2775,11 +2544,7 @@ pub const Node = struct {...@@ -2775,11 +2544,7 @@ pub const Node = struct {
2775 base: Node = Node{ .id = .NullLiteral },2544 base: Node = Node{ .id = .NullLiteral },
2776 token: TokenIndex,2545 token: TokenIndex,
27772546
2778 pub fn iterate(self: *const NullLiteral) Node.Iterator {2547 pub fn iterate(self: *const NullLiteral, index: usize) ?*Node {
2779 return .{ .parent_node = &self.base, .index = 0 };
2780 }
2781
2782 pub fn iterateNext(self: *const NullLiteral, it: *Node.Iterator) ?*Node {
2783 return null;2548 return null;
2784 }2549 }
27852550
...@@ -2796,11 +2561,7 @@ pub const Node = struct {...@@ -2796,11 +2561,7 @@ pub const Node = struct {
2796 base: Node = Node{ .id = .UndefinedLiteral },2561 base: Node = Node{ .id = .UndefinedLiteral },
2797 token: TokenIndex,2562 token: TokenIndex,
27982563
2799 pub fn iterate(self: *const UndefinedLiteral) Node.Iterator {2564 pub fn iterate(self: *const UndefinedLiteral, index: usize) ?*Node {
2800 return .{ .parent_node = &self.base, .index = 0 };
2801 }
2802
2803 pub fn iterateNext(self: *const UndefinedLiteral, it: *Node.Iterator) ?*Node {
2804 return null;2565 return null;
2805 }2566 }
28062567
...@@ -2836,13 +2597,8 @@ pub const Node = struct {...@@ -2836,13 +2597,8 @@ pub const Node = struct {
2836 Return: *Node,2597 Return: *Node,
2837 };2598 };
28382599
2839 pub fn iterate(self: *const Output) Node.Iterator {2600 pub fn iterate(self: *const Output, index: usize) ?*Node {
2840 return .{ .parent_node = &self.base, .index = 0 };2601 var i = index;
2841 }
2842
2843 pub fn iterateNext(self: *const Output, it: *Node.Iterator) ?*Node {
2844 var i = it.index;
2845 it.index += 1;
28462602
2847 if (i < 1) return self.symbolic_name;2603 if (i < 1) return self.symbolic_name;
2848 i -= 1;2604 i -= 1;
...@@ -2880,13 +2636,8 @@ pub const Node = struct {...@@ -2880,13 +2636,8 @@ pub const Node = struct {
2880 expr: *Node,2636 expr: *Node,
2881 rparen: TokenIndex,2637 rparen: TokenIndex,
28822638
2883 pub fn iterate(self: *const Input) Node.Iterator {2639 pub fn iterate(self: *const Input, index: usize) ?*Node {
2884 return .{ .parent_node = &self.base, .index = 0 };2640 var i = index;
2885 }
2886
2887 pub fn iterateNext(self: *const Input, it: *Node.Iterator) ?*Node {
2888 var i = it.index;
2889 it.index += 1;
28902641
2891 if (i < 1) return self.symbolic_name;2642 if (i < 1) return self.symbolic_name;
2892 i -= 1;2643 i -= 1;
...@@ -2910,13 +2661,8 @@ pub const Node = struct {...@@ -2910,13 +2661,8 @@ pub const Node = struct {
2910 };2661 };
29112662
29122663
2913 pub fn iterate(self: *const Asm) Node.Iterator {2664 pub fn iterate(self: *const Asm, index: usize) ?*Node {
2914 return .{ .parent_node = &self.base, .index = 0};2665 var i = index;
2915 }
2916
2917 pub fn iterateNext(self: *const Asm, it: *Node.Iterator) ?*Node {
2918 var i = it.index;
2919 it.index += 1;
29202666
2921 if (i < self.outputs.len * 3) switch (i % 3) {2667 if (i < self.outputs.len * 3) switch (i % 3) {
2922 0 => return self.outputs[i / 3].symbolic_name,2668 0 => return self.outputs[i / 3].symbolic_name,
...@@ -2953,11 +2699,7 @@ pub const Node = struct {...@@ -2953,11 +2699,7 @@ pub const Node = struct {
2953 base: Node = Node{ .id = .Unreachable },2699 base: Node = Node{ .id = .Unreachable },
2954 token: TokenIndex,2700 token: TokenIndex,
29552701
2956 pub fn iterate(self: *const Unreachable) Node.Iterator {2702 pub fn iterate(self: *const Unreachable, index: usize) ?*Node {
2957 return .{ .parent_node = &self.base, .index = 0 };
2958 }
2959
2960 pub fn iterateNext(self: *const Unreachable, it: *Node.Iterator) ?*Node {
2961 return null;2703 return null;
2962 }2704 }
29632705
...@@ -2974,11 +2716,7 @@ pub const Node = struct {...@@ -2974,11 +2716,7 @@ pub const Node = struct {
2974 base: Node = Node{ .id = .ErrorType },2716 base: Node = Node{ .id = .ErrorType },
2975 token: TokenIndex,2717 token: TokenIndex,
29762718
2977 pub fn iterate(self: *const ErrorType) Node.Iterator {2719 pub fn iterate(self: *const ErrorType, index: usize) ?*Node {
2978 return .{ .parent_node = &self.base, .index = 0 };
2979 }
2980
2981 pub fn iterateNext(self: *const ErrorType, it: *Node.Iterator) ?*Node {
2982 return null;2720 return null;
2983 }2721 }
29842722
...@@ -2995,11 +2733,7 @@ pub const Node = struct {...@@ -2995,11 +2733,7 @@ pub const Node = struct {
2995 base: Node = Node{ .id = .VarType },2733 base: Node = Node{ .id = .VarType },
2996 token: TokenIndex,2734 token: TokenIndex,
29972735
2998 pub fn iterate(self: *const VarType) Node.Iterator {2736 pub fn iterate(self: *const VarType, index: usize) ?*Node {
2999 return .{ .parent_node = &self.base, .index = 0 };
3000 }
3001
3002 pub fn iterateNext(self: *const VarType, it: *Node.Iterator) ?*Node {
3003 return null;2737 return null;
3004 }2738 }
30052739
...@@ -3019,11 +2753,7 @@ pub const Node = struct {...@@ -3019,11 +2753,7 @@ pub const Node = struct {
3019 /// at the first other token.2753 /// at the first other token.
3020 first_line: TokenIndex,2754 first_line: TokenIndex,
30212755
3022 pub fn iterate(self: *const DocComment) Node.Iterator {2756 pub fn iterate(self: *const DocComment, index: usize) ?*Node {
3023 return .{ .parent_node = &self.base, .index = 0 };
3024 }
3025
3026 pub fn iterateNext(self: *const DocComment, it: *Node.Iterator) ?*Node {
3027 return null;2757 return null;
3028 }2758 }
30292759
...@@ -3045,13 +2775,8 @@ pub const Node = struct {...@@ -3045,13 +2775,8 @@ pub const Node = struct {
3045 name: *Node,2775 name: *Node,
3046 body_node: *Node,2776 body_node: *Node,
30472777
3048 pub fn iterate(self: *const TestDecl) Node.Iterator {2778 pub fn iterate(self: *const TestDecl, index: usize) ?*Node {
3049 return .{ .parent_node = &self.base, .index = 0 };2779 var i = index;
3050 }
3051
3052 pub fn iterateNext(self: *const TestDecl, it: *Node.Iterator) ?*Node {
3053 var i = it.index;
3054 it.index += 1;
30552780
3056 if (i < 1) return self.body_node;2781 if (i < 1) return self.body_node;
3057 i -= 1;2782 i -= 1;
...@@ -3076,6 +2801,5 @@ test "iterate" {...@@ -3076,6 +2801,5 @@ test "iterate" {
3076 .eof_token = 0,2801 .eof_token = 0,
3077 };2802 };
3078 var base = &root.base;2803 var base = &root.base;
3079 var it = base.iterate();2804 testing.expect(base.iterate(0) == null);
3080 testing.expect(it.next() == null);
3081}2805}