Commit 866be099f8

Andrew Kelley <andrew@ziglang.org>
2021-03-23 20:54:18
stage2: add helper functions to clean up astgen Ref/Index
1 parent 8301439
Changed files (2)
src/astgen.zig
@@ -1306,11 +1306,11 @@ fn varDecl(
             if (var_decl.ast.type_node != 0) {
                 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
                 opt_type_inst = type_inst;
-                init_scope.rl_ptr = (try init_scope.addUnNode(.alloc, type_inst, node)) - init_scope.zir_code.ref_start_index;
+                init_scope.rl_ptr = try init_scope.addUnNodeAsIndex(.alloc, type_inst, node);
             } else {
-                const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);
-                resolve_inferred_alloc = alloc;
-                init_scope.rl_ptr = alloc - init_scope.zir_code.ref_start_index;
+                const alloc = try init_scope.addUnNodeAsIndex(.alloc_inferred, undefined, node);
+                resolve_inferred_alloc = init_scope.zir_code.ref_start_index + alloc;
+                init_scope.rl_ptr = alloc;
             }
             const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
             const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
@@ -3201,7 +3201,7 @@ fn asRlPtr(
     };
     defer as_scope.instructions.deinit(mod.gpa);
 
-    as_scope.rl_ptr = (try as_scope.addBin(.coerce_result_ptr, dest_type, result_ptr)) - as_scope.zir_code.ref_start_index;
+    as_scope.rl_ptr = try as_scope.addBinAsIndex(.coerce_result_ptr, dest_type, result_ptr);
     const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
     const parent_zir = &parent_gz.instructions;
     if (as_scope.rvalue_rl_count == 1) {
src/Module.zig
@@ -1074,10 +1074,10 @@ pub const Scope = struct {
             callee: zir.Inst.Ref,
             args: []const zir.Inst.Ref,
             /// Absolute node index. This function does the conversion to offset from Decl.
-            abs_node_index: ast.Node.Index,
+            src_node: ast.Node.Index,
         ) !zir.Inst.Ref {
             assert(callee != 0);
-            assert(abs_node_index != 0);
+            assert(src_node != 0);
             const gpa = gz.zir_code.gpa;
             try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
             try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
@@ -1094,7 +1094,7 @@ pub const Scope = struct {
             gz.zir_code.instructions.appendAssumeCapacity(.{
                 .tag = tag,
                 .data = .{ .pl_node = .{
-                    .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
+                    .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
                     .payload_index = payload_index,
                 } },
             });
@@ -1138,14 +1138,24 @@ pub const Scope = struct {
             tag: zir.Inst.Tag,
             operand: zir.Inst.Ref,
             /// Absolute node index. This function does the conversion to offset from Decl.
-            abs_node_index: ast.Node.Index,
+            src_node: ast.Node.Index,
         ) !zir.Inst.Ref {
+            return gz.zir_code.ref_start_index + try gz.addUnNodeAsIndex(tag, operand, src_node);
+        }
+
+        pub fn addUnNodeAsIndex(
+            gz: *GenZir,
+            tag: zir.Inst.Tag,
+            operand: zir.Inst.Ref,
+            /// Absolute node index. This function does the conversion to offset from Decl.
+            src_node: ast.Node.Index,
+        ) !zir.Inst.Index {
             assert(operand != 0);
-            return gz.add(.{
+            return gz.addAsIndex(.{
                 .tag = tag,
                 .data = .{ .un_node = .{
                     .operand = operand,
-                    .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
+                    .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
                 } },
             });
         }
@@ -1154,7 +1164,7 @@ pub const Scope = struct {
             gz: *GenZir,
             tag: zir.Inst.Tag,
             /// Absolute node index. This function does the conversion to offset from Decl.
-            abs_node_index: ast.Node.Index,
+            src_node: ast.Node.Index,
             extra: anytype,
         ) !zir.Inst.Ref {
             const gpa = gz.zir_code.gpa;
@@ -1166,7 +1176,7 @@ pub const Scope = struct {
             gz.zir_code.instructions.appendAssumeCapacity(.{
                 .tag = tag,
                 .data = .{ .pl_node = .{
-                    .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
+                    .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
                     .payload_index = payload_index,
                 } },
             });
@@ -1239,9 +1249,18 @@ pub const Scope = struct {
             lhs: zir.Inst.Ref,
             rhs: zir.Inst.Ref,
         ) !zir.Inst.Ref {
+            return gz.zir_code.ref_start_index + try gz.addBinAsIndex(tag, lhs, rhs);
+        }
+
+        pub fn addBinAsIndex(
+            gz: *GenZir,
+            tag: zir.Inst.Tag,
+            lhs: zir.Inst.Ref,
+            rhs: zir.Inst.Ref,
+        ) !zir.Inst.Index {
             assert(lhs != 0);
             assert(rhs != 0);
-            return gz.add(.{
+            return gz.addAsIndex(.{
                 .tag = tag,
                 .data = .{ .bin = .{
                     .lhs = lhs,
@@ -1265,11 +1284,11 @@ pub const Scope = struct {
             gz: *GenZir,
             tag: zir.Inst.Tag,
             /// Absolute node index. This function does the conversion to offset from Decl.
-            abs_node_index: ast.Node.Index,
+            src_node: ast.Node.Index,
         ) !zir.Inst.Ref {
             return gz.add(.{
                 .tag = tag,
-                .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index) },
+                .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(src_node) },
             });
         }
 
@@ -1321,6 +1340,10 @@ pub const Scope = struct {
         }
 
         pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
+            return gz.zir_code.ref_start_index + try gz.addAsIndex(inst);
+        }
+
+        pub fn addAsIndex(gz: *GenZir, inst: zir.Inst) !zir.Inst.Index {
             const gpa = gz.zir_code.gpa;
             try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
             try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
@@ -1328,7 +1351,7 @@ pub const Scope = struct {
             const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
             gz.zir_code.instructions.appendAssumeCapacity(inst);
             gz.instructions.appendAssumeCapacity(new_index);
-            return new_index + gz.zir_code.ref_start_index;
+            return new_index;
         }
     };