Skip to content

Commit 031ee48

Browse files
committed
add std.zig.Ast.blockStatements and std.zig.Ast.builtinCallParams
1 parent 41b3183 commit 031ee48

File tree

7 files changed

+142
-259
lines changed

7 files changed

+142
-259
lines changed

lib/compiler/reduce/Walk.zig

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,11 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {
230230

231231
.block_two,
232232
.block_two_semicolon,
233-
=> {
234-
const statements = [2]Ast.Node.Index{ datas[node].lhs, datas[node].rhs };
235-
if (datas[node].lhs == 0) {
236-
return walkBlock(w, node, statements[0..0]);
237-
} else if (datas[node].rhs == 0) {
238-
return walkBlock(w, node, statements[0..1]);
239-
} else {
240-
return walkBlock(w, node, statements[0..2]);
241-
}
242-
},
243233
.block,
244234
.block_semicolon,
245235
=> {
246-
const statements = ast.extra_data[datas[node].lhs..datas[node].rhs];
236+
var buf: [2]Ast.Node.Index = undefined;
237+
const statements = ast.blockStatements(&buf, node).?;
247238
return walkBlock(w, node, statements);
248239
},
249240

@@ -506,17 +497,13 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {
506497
}
507498
},
508499

509-
.builtin_call_two, .builtin_call_two_comma => {
510-
if (datas[node].lhs == 0) {
511-
return walkBuiltinCall(w, node, &.{});
512-
} else if (datas[node].rhs == 0) {
513-
return walkBuiltinCall(w, node, &.{datas[node].lhs});
514-
} else {
515-
return walkBuiltinCall(w, node, &.{ datas[node].lhs, datas[node].rhs });
516-
}
517-
},
518-
.builtin_call, .builtin_call_comma => {
519-
const params = ast.extra_data[datas[node].lhs..datas[node].rhs];
500+
.builtin_call_two,
501+
.builtin_call_two_comma,
502+
.builtin_call,
503+
.builtin_call_comma,
504+
=> {
505+
var buf: [2]Ast.Node.Index = undefined;
506+
const params = ast.builtinCallParams(&buf, node).?;
520507
return walkBuiltinCall(w, node, params);
521508
},
522509

@@ -972,24 +959,13 @@ fn walkParamList(w: *Walk, params: []const Ast.Node.Index) Error!void {
972959
fn isFnBodyGutted(ast: *const Ast, body_node: Ast.Node.Index) bool {
973960
// skip over discards
974961
const node_tags = ast.nodes.items(.tag);
975-
const datas = ast.nodes.items(.data);
976962
var statements_buf: [2]Ast.Node.Index = undefined;
977963
const statements = switch (node_tags[body_node]) {
978964
.block_two,
979965
.block_two_semicolon,
980-
=> blk: {
981-
statements_buf[0..2].* = .{ datas[body_node].lhs, datas[body_node].rhs };
982-
break :blk if (datas[body_node].lhs == 0)
983-
statements_buf[0..0]
984-
else if (datas[body_node].rhs == 0)
985-
statements_buf[0..1]
986-
else
987-
statements_buf[0..2];
988-
},
989-
990966
.block,
991967
.block_semicolon,
992-
=> ast.extra_data[datas[body_node].lhs..datas[body_node].rhs],
968+
=> ast.blockStatements(&statements_buf, body_node).?,
993969

994970
else => return false,
995971
};
@@ -1016,17 +992,13 @@ fn categorizeStmt(ast: *const Ast, stmt: Ast.Node.Index) StmtCategory {
1016992
const datas = ast.nodes.items(.data);
1017993
const main_tokens = ast.nodes.items(.main_token);
1018994
switch (node_tags[stmt]) {
1019-
.builtin_call_two, .builtin_call_two_comma => {
1020-
if (datas[stmt].lhs == 0) {
1021-
return categorizeBuiltinCall(ast, main_tokens[stmt], &.{});
1022-
} else if (datas[stmt].rhs == 0) {
1023-
return categorizeBuiltinCall(ast, main_tokens[stmt], &.{datas[stmt].lhs});
1024-
} else {
1025-
return categorizeBuiltinCall(ast, main_tokens[stmt], &.{ datas[stmt].lhs, datas[stmt].rhs });
1026-
}
1027-
},
1028-
.builtin_call, .builtin_call_comma => {
1029-
const params = ast.extra_data[datas[stmt].lhs..datas[stmt].rhs];
995+
.builtin_call_two,
996+
.builtin_call_two_comma,
997+
.builtin_call,
998+
.builtin_call_comma,
999+
=> {
1000+
var buf: [2]Ast.Node.Index = undefined;
1001+
const params = ast.builtinCallParams(&buf, stmt).?;
10301002
return categorizeBuiltinCall(ast, main_tokens[stmt], params);
10311003
},
10321004
.assign => {

lib/docs/wasm/Walk.zig

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,13 @@ pub const File = struct {
232232
return .{ .global_const = node };
233233
},
234234

235-
.builtin_call_two, .builtin_call_two_comma => {
236-
if (node_datas[node].lhs == 0) {
237-
const params = [_]Ast.Node.Index{};
238-
return categorize_builtin_call(file_index, node, &params);
239-
} else if (node_datas[node].rhs == 0) {
240-
const params = [_]Ast.Node.Index{node_datas[node].lhs};
241-
return categorize_builtin_call(file_index, node, &params);
242-
} else {
243-
const params = [_]Ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
244-
return categorize_builtin_call(file_index, node, &params);
245-
}
246-
},
247-
.builtin_call, .builtin_call_comma => {
248-
const params = ast.extra_data[node_datas[node].lhs..node_datas[node].rhs];
235+
.builtin_call_two,
236+
.builtin_call_two_comma,
237+
.builtin_call,
238+
.builtin_call_comma,
239+
=> {
240+
var buf: [2]Ast.Node.Index = undefined;
241+
const params = ast.builtinCallParams(&buf, node).?;
249242
return categorize_builtin_call(file_index, node, params);
250243
},
251244

@@ -801,20 +794,13 @@ fn expr(w: *Walk, scope: *Scope, parent_decl: Decl.Index, node: Ast.Node.Index)
801794
try expr(w, scope, parent_decl, full.ast.template);
802795
},
803796

804-
.builtin_call_two, .builtin_call_two_comma => {
805-
if (node_datas[node].lhs == 0) {
806-
const params = [_]Ast.Node.Index{};
807-
return builtin_call(w, scope, parent_decl, node, &params);
808-
} else if (node_datas[node].rhs == 0) {
809-
const params = [_]Ast.Node.Index{node_datas[node].lhs};
810-
return builtin_call(w, scope, parent_decl, node, &params);
811-
} else {
812-
const params = [_]Ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
813-
return builtin_call(w, scope, parent_decl, node, &params);
814-
}
815-
},
816-
.builtin_call, .builtin_call_comma => {
817-
const params = ast.extra_data[node_datas[node].lhs..node_datas[node].rhs];
797+
.builtin_call_two,
798+
.builtin_call_two_comma,
799+
.builtin_call,
800+
.builtin_call_comma,
801+
=> {
802+
var buf: [2]Ast.Node.Index = undefined;
803+
const params = ast.builtinCallParams(&buf, node).?;
818804
return builtin_call(w, scope, parent_decl, node, params);
819805
},
820806

@@ -869,18 +855,13 @@ fn expr(w: *Walk, scope: *Scope, parent_decl: Decl.Index, node: Ast.Node.Index)
869855
.slice_open => return slice(w, scope, parent_decl, ast.sliceOpen(node)),
870856
.slice_sentinel => return slice(w, scope, parent_decl, ast.sliceSentinel(node)),
871857

872-
.block_two, .block_two_semicolon => {
873-
const statements = [2]Ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
874-
if (node_datas[node].lhs == 0) {
875-
return block(w, scope, parent_decl, statements[0..0]);
876-
} else if (node_datas[node].rhs == 0) {
877-
return block(w, scope, parent_decl, statements[0..1]);
878-
} else {
879-
return block(w, scope, parent_decl, statements[0..2]);
880-
}
881-
},
882-
.block, .block_semicolon => {
883-
const statements = ast.extra_data[node_datas[node].lhs..node_datas[node].rhs];
858+
.block_two,
859+
.block_two_semicolon,
860+
.block,
861+
.block_semicolon,
862+
=> {
863+
var buf: [2]Ast.Node.Index = undefined;
864+
const statements = ast.blockStatements(&buf, node).?;
884865
return block(w, scope, parent_decl, statements);
885866
},
886867

lib/std/zig/Ast.zig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,42 @@ pub fn fullCall(tree: Ast, buffer: *[1]Ast.Node.Index, node: Node.Index) ?full.C
24202420
};
24212421
}
24222422

2423+
pub fn builtinCallParams(tree: Ast, buffer: *[2]Ast.Node.Index, node: Ast.Node.Index) ?[]const Node.Index {
2424+
const data = tree.nodes.items(.data)[node];
2425+
return switch (tree.nodes.items(.tag)[node]) {
2426+
.builtin_call_two, .builtin_call_two_comma => {
2427+
buffer.* = .{ data.lhs, data.rhs };
2428+
if (data.rhs != 0) {
2429+
return buffer[0..2];
2430+
} else if (data.lhs != 0) {
2431+
return buffer[0..1];
2432+
} else {
2433+
return buffer[0..0];
2434+
}
2435+
},
2436+
.builtin_call, .builtin_call_comma => tree.extra_data[data.lhs..data.rhs],
2437+
else => null,
2438+
};
2439+
}
2440+
2441+
pub fn blockStatements(tree: Ast, buffer: *[2]Ast.Node.Index, node: Ast.Node.Index) ?[]const Node.Index {
2442+
const data = tree.nodes.items(.data)[node];
2443+
return switch (tree.nodes.items(.tag)[node]) {
2444+
.block_two, .block_two_semicolon => {
2445+
buffer.* = .{ data.lhs, data.rhs };
2446+
if (data.rhs != 0) {
2447+
return buffer[0..2];
2448+
} else if (data.lhs != 0) {
2449+
return buffer[0..1];
2450+
} else {
2451+
return buffer[0..0];
2452+
}
2453+
},
2454+
.block, .block_semicolon => tree.extra_data[data.lhs..data.rhs],
2455+
else => null,
2456+
};
2457+
}
2458+
24232459
/// Fully assembled AST node information.
24242460
pub const full = struct {
24252461
pub const VarDecl = struct {

0 commit comments

Comments
 (0)