authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2024-12-29 00:47:24+01:00
committergravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2024-12-29 00:47:24+01:00
log2e40a1d22e6837fdfbe8d56065a7ca03a5270601
tree0ac712a19b674eeacc1123643ac4dac96bc7f64d
parent01081cc8e8b79104f7992d60dbd1bc8682e8fedf
signaturebadge-check Signed by SSH key SHA256:HYC3SjXQcAt6uwv9pu/6OoVQ2rUH8rb5zKiUHSe9uxk

simplify AstGen handling of slicing syntax


1 files changed, 38 insertions(+), 70 deletions(-)

lib/std/zig/AstGen.zig+38-70
...@@ -876,100 +876,68 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -876,100 +876,68 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
876876
877 .for_simple, .@"for" => return forExpr(gz, scope, ri.br(), node, tree.fullFor(node).?, false),877 .for_simple, .@"for" => return forExpr(gz, scope, ri.br(), node, tree.fullFor(node).?, false),
878878
879 .slice_open => {879 .slice_open,
880 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);880 .slice,
881881 .slice_sentinel,
882 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);882 => {
883 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);883 const full = tree.fullSlice(node).?;
884 try emitDbgStmt(gz, cursor);884 const lhs_tag = node_tags[full.ast.sliced];
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];
895 const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;885 const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;
896 const lhs_is_open_slice = lhs_tag == .slice_open or886 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);887 (lhs_is_slice_sentinel and tree.fullSlice(full.ast.sliced).?.ast.end == 0);
898 if (lhs_is_open_slice and nodeIsTriviallyZero(tree, extra.start)) {888 if (node_tags[node] != .slice_open and
899 const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[lhs_node].lhs);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
901 const start = if (lhs_is_slice_sentinel) start: {894 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);
903 break :start try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);896 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
906 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);899 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;
908 try emitDbgStmt(gz, cursor);902 try emitDbgStmt(gz, cursor);
909 const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{903 const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
910 .lhs = lhs,904 .lhs = lhs,
911 .start = start,905 .start = start,
912 .len = len,906 .len = len,
913 .start_src_node_offset = gz.nodeIndexToRelative(lhs_node),907 .start_src_node_offset = gz.nodeIndexToRelative(full.ast.sliced),
914 .sentinel = .none,908 .sentinel = sentinel,
915 });909 });
916 return rvalue(gz, ri, result, node);910 return rvalue(gz, ri, result, node);
917 }911 }
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
920 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);914 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
921 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);915 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.start);
922 const end = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end);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;
923 try emitDbgStmt(gz, cursor);918 try emitDbgStmt(gz, cursor);
924 const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{919 if (sentinel != .none) {
925 .lhs = lhs,920 const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
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{
951 .lhs = lhs,921 .lhs = lhs,
952 .start = start,922 .start = start,
953 .len = len,923 .end = end,
954 .start_src_node_offset = gz.nodeIndexToRelative(lhs_node),
955 .sentinel = sentinel,924 .sentinel = sentinel,
956 });925 });
957 return rvalue(gz, ri, result, node);926 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);
958 }940 }
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);
973 },941 },
974942
975 .deref => {943 .deref => {