Skip to content

Commit 2e40a1d

Browse files
committed
simplify AstGen handling of slicing syntax
1 parent 01081cc commit 2e40a1d

File tree

1 file changed

+38
-70
lines changed

1 file changed

+38
-70
lines changed

lib/std/zig/AstGen.zig

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -876,100 +876,68 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
876876

877877
.for_simple, .@"for" => return forExpr(gz, scope, ri.br(), node, tree.fullFor(node).?, false),
878878

879-
.slice_open => {
880-
const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
881-
882-
const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
883-
const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
884-
try emitDbgStmt(gz, cursor);
885-
const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{
886-
.lhs = lhs,
887-
.start = start,
888-
});
889-
return rvalue(gz, ri, result, node);
890-
},
891-
.slice => {
892-
const extra = tree.extraData(node_datas[node].rhs, Ast.Node.Slice);
893-
const lhs_node = node_datas[node].lhs;
894-
const lhs_tag = node_tags[lhs_node];
879+
.slice_open,
880+
.slice,
881+
.slice_sentinel,
882+
=> {
883+
const full = tree.fullSlice(node).?;
884+
const lhs_tag = node_tags[full.ast.sliced];
895885
const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;
896886
const lhs_is_open_slice = lhs_tag == .slice_open or
897-
(lhs_is_slice_sentinel and tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel).end == 0);
898-
if (lhs_is_open_slice and nodeIsTriviallyZero(tree, extra.start)) {
899-
const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[lhs_node].lhs);
887+
(lhs_is_slice_sentinel and tree.fullSlice(full.ast.sliced).?.ast.end == 0);
888+
if (node_tags[node] != .slice_open and
889+
lhs_is_open_slice and
890+
nodeIsTriviallyZero(tree, full.ast.start))
891+
{
892+
const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[full.ast.sliced].lhs);
900893

901894
const start = if (lhs_is_slice_sentinel) start: {
902-
const lhs_extra = tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel);
895+
const lhs_extra = tree.extraData(node_datas[full.ast.sliced].rhs, Ast.Node.SliceSentinel);
903896
break :start try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);
904-
} else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[lhs_node].rhs);
897+
} else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[full.ast.sliced].rhs);
905898

906899
const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
907-
const len = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
900+
const len = if (full.ast.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end) else .none;
901+
const sentinel = if (full.ast.sentinel != 0) try expr(gz, scope, .{ .rl = .none }, full.ast.sentinel) else .none;
908902
try emitDbgStmt(gz, cursor);
909903
const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
910904
.lhs = lhs,
911905
.start = start,
912906
.len = len,
913-
.start_src_node_offset = gz.nodeIndexToRelative(lhs_node),
914-
.sentinel = .none,
907+
.start_src_node_offset = gz.nodeIndexToRelative(full.ast.sliced),
908+
.sentinel = sentinel,
915909
});
916910
return rvalue(gz, ri, result, node);
917911
}
918-
const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
912+
const lhs = try expr(gz, scope, .{ .rl = .ref }, full.ast.sliced);
919913

920914
const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
921-
const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
922-
const end = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end);
915+
const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.start);
916+
const end = if (full.ast.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end) else .none;
917+
const sentinel = if (full.ast.sentinel != 0) try expr(gz, scope, .{ .rl = .none }, full.ast.sentinel) else .none;
923918
try emitDbgStmt(gz, cursor);
924-
const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{
925-
.lhs = lhs,
926-
.start = start,
927-
.end = end,
928-
});
929-
return rvalue(gz, ri, result, node);
930-
},
931-
.slice_sentinel => {
932-
const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SliceSentinel);
933-
const lhs_node = node_datas[node].lhs;
934-
const lhs_tag = node_tags[lhs_node];
935-
const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;
936-
const lhs_is_open_slice = lhs_tag == .slice_open or
937-
(lhs_is_slice_sentinel and tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel).end == 0);
938-
if (lhs_is_open_slice and nodeIsTriviallyZero(tree, extra.start)) {
939-
const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[lhs_node].lhs);
940-
941-
const start = if (lhs_is_slice_sentinel) start: {
942-
const lhs_extra = tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel);
943-
break :start try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);
944-
} else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[lhs_node].rhs);
945-
946-
const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
947-
const len = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
948-
const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);
949-
try emitDbgStmt(gz, cursor);
950-
const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
919+
if (sentinel != .none) {
920+
const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
951921
.lhs = lhs,
952922
.start = start,
953-
.len = len,
954-
.start_src_node_offset = gz.nodeIndexToRelative(lhs_node),
923+
.end = end,
955924
.sentinel = sentinel,
956925
});
957926
return rvalue(gz, ri, result, node);
927+
} else if (end != .none) {
928+
const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{
929+
.lhs = lhs,
930+
.start = start,
931+
.end = end,
932+
});
933+
return rvalue(gz, ri, result, node);
934+
} else {
935+
const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{
936+
.lhs = lhs,
937+
.start = start,
938+
});
939+
return rvalue(gz, ri, result, node);
958940
}
959-
const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
960-
961-
const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
962-
const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
963-
const end = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
964-
const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);
965-
try emitDbgStmt(gz, cursor);
966-
const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
967-
.lhs = lhs,
968-
.start = start,
969-
.end = end,
970-
.sentinel = sentinel,
971-
});
972-
return rvalue(gz, ri, result, node);
973941
},
974942

975943
.deref => {

0 commit comments

Comments
 (0)