Commit 2e40a1d22e

Techatrix <techatrix@mailbox.org>
2024-12-29 00:47:24
simplify AstGen handling of slicing syntax
1 parent 01081cc
Changed files (1)
lib
std
lib/std/zig/AstGen.zig
@@ -876,100 +876,68 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
 
         .for_simple, .@"for" => return forExpr(gz, scope, ri.br(), node, tree.fullFor(node).?, false),
 
-        .slice_open => {
-            const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
-
-            const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
-            const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
-            try emitDbgStmt(gz, cursor);
-            const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{
-                .lhs = lhs,
-                .start = start,
-            });
-            return rvalue(gz, ri, result, node);
-        },
-        .slice => {
-            const extra = tree.extraData(node_datas[node].rhs, Ast.Node.Slice);
-            const lhs_node = node_datas[node].lhs;
-            const lhs_tag = node_tags[lhs_node];
+        .slice_open,
+        .slice,
+        .slice_sentinel,
+        => {
+            const full = tree.fullSlice(node).?;
+            const lhs_tag = node_tags[full.ast.sliced];
             const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;
             const lhs_is_open_slice = lhs_tag == .slice_open or
-                (lhs_is_slice_sentinel and tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel).end == 0);
-            if (lhs_is_open_slice and nodeIsTriviallyZero(tree, extra.start)) {
-                const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[lhs_node].lhs);
+                (lhs_is_slice_sentinel and tree.fullSlice(full.ast.sliced).?.ast.end == 0);
+            if (node_tags[node] != .slice_open and
+                lhs_is_open_slice and
+                nodeIsTriviallyZero(tree, full.ast.start))
+            {
+                const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[full.ast.sliced].lhs);
 
                 const start = if (lhs_is_slice_sentinel) start: {
-                    const lhs_extra = tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel);
+                    const lhs_extra = tree.extraData(node_datas[full.ast.sliced].rhs, Ast.Node.SliceSentinel);
                     break :start try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);
-                } else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[lhs_node].rhs);
+                } else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[full.ast.sliced].rhs);
 
                 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
-                const len = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
+                const len = if (full.ast.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end) else .none;
+                const sentinel = if (full.ast.sentinel != 0) try expr(gz, scope, .{ .rl = .none }, full.ast.sentinel) else .none;
                 try emitDbgStmt(gz, cursor);
                 const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
                     .lhs = lhs,
                     .start = start,
                     .len = len,
-                    .start_src_node_offset = gz.nodeIndexToRelative(lhs_node),
-                    .sentinel = .none,
+                    .start_src_node_offset = gz.nodeIndexToRelative(full.ast.sliced),
+                    .sentinel = sentinel,
                 });
                 return rvalue(gz, ri, result, node);
             }
-            const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
+            const lhs = try expr(gz, scope, .{ .rl = .ref }, full.ast.sliced);
 
             const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
-            const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
-            const end = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end);
+            const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.start);
+            const end = if (full.ast.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end) else .none;
+            const sentinel = if (full.ast.sentinel != 0) try expr(gz, scope, .{ .rl = .none }, full.ast.sentinel) else .none;
             try emitDbgStmt(gz, cursor);
-            const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{
-                .lhs = lhs,
-                .start = start,
-                .end = end,
-            });
-            return rvalue(gz, ri, result, node);
-        },
-        .slice_sentinel => {
-            const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SliceSentinel);
-            const lhs_node = node_datas[node].lhs;
-            const lhs_tag = node_tags[lhs_node];
-            const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;
-            const lhs_is_open_slice = lhs_tag == .slice_open or
-                (lhs_is_slice_sentinel and tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel).end == 0);
-            if (lhs_is_open_slice and nodeIsTriviallyZero(tree, extra.start)) {
-                const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[lhs_node].lhs);
-
-                const start = if (lhs_is_slice_sentinel) start: {
-                    const lhs_extra = tree.extraData(node_datas[lhs_node].rhs, Ast.Node.SliceSentinel);
-                    break :start try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);
-                } else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[lhs_node].rhs);
-
-                const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
-                const len = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
-                const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);
-                try emitDbgStmt(gz, cursor);
-                const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
+            if (sentinel != .none) {
+                const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
                     .lhs = lhs,
                     .start = start,
-                    .len = len,
-                    .start_src_node_offset = gz.nodeIndexToRelative(lhs_node),
+                    .end = end,
                     .sentinel = sentinel,
                 });
                 return rvalue(gz, ri, result, node);
+            } else if (end != .none) {
+                const result = try gz.addPlNode(.slice_end, node, Zir.Inst.SliceEnd{
+                    .lhs = lhs,
+                    .start = start,
+                    .end = end,
+                });
+                return rvalue(gz, ri, result, node);
+            } else {
+                const result = try gz.addPlNode(.slice_start, node, Zir.Inst.SliceStart{
+                    .lhs = lhs,
+                    .start = start,
+                });
+                return rvalue(gz, ri, result, node);
             }
-            const lhs = try expr(gz, scope, .{ .rl = .ref }, node_datas[node].lhs);
-
-            const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
-            const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.start);
-            const end = if (extra.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, extra.end) else .none;
-            const sentinel = try expr(gz, scope, .{ .rl = .none }, extra.sentinel);
-            try emitDbgStmt(gz, cursor);
-            const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
-                .lhs = lhs,
-                .start = start,
-                .end = end,
-                .sentinel = sentinel,
-            });
-            return rvalue(gz, ri, result, node);
         },
 
         .deref => {