| ... | @@ -237,12 +237,20 @@ pub const Decl = struct { | ... | @@ -237,12 +237,20 @@ pub const Decl = struct { |
| 237 | } | 237 | } |
| 238 | } | 238 | } |
| 239 | | 239 | |
| 240 | pub fn tokSrcLoc(decl: *Decl, token_index: ast.TokenIndex) LazySrcLoc { | 240 | pub fn relativeToNodeIndex(decl: Decl, offset: i32) ast.Node.Index { |
| | 241 | return @bitCast(ast.Node.Index, offset + @bitCast(i32, decl.srcNode())); |
| | 242 | } |
| | 243 | |
| | 244 | pub fn nodeIndexToRelative(decl: Decl, node_index: ast.Node.Index) i32 { |
| | 245 | return @bitCast(i32, node_index) - @bitCast(i32, decl.srcNode()); |
| | 246 | } |
| | 247 | |
| | 248 | pub fn tokSrcLoc(decl: Decl, token_index: ast.TokenIndex) LazySrcLoc { |
| 241 | return .{ .token_offset = token_index - decl.srcToken() }; | 249 | return .{ .token_offset = token_index - decl.srcToken() }; |
| 242 | } | 250 | } |
| 243 | | 251 | |
| 244 | pub fn nodeSrcLoc(decl: *Decl, node_index: ast.Node.Index) LazySrcLoc { | 252 | pub fn nodeSrcLoc(decl: Decl, node_index: ast.Node.Index) LazySrcLoc { |
| 245 | return .{ .node_offset = node_index - decl.srcNode() }; | 253 | return .{ .node_offset = decl.nodeIndexToRelative(node_index) }; |
| 246 | } | 254 | } |
| 247 | | 255 | |
| 248 | pub fn srcLoc(decl: *Decl) SrcLoc { | 256 | pub fn srcLoc(decl: *Decl) SrcLoc { |
| ... | @@ -1076,6 +1084,40 @@ pub const Scope = struct { | ... | @@ -1076,6 +1084,40 @@ pub const Scope = struct { |
| 1076 | return new_index + gz.zir_code.ref_start_index; | 1084 | return new_index + gz.zir_code.ref_start_index; |
| 1077 | } | 1085 | } |
| 1078 | | 1086 | |
| | 1087 | pub fn addCall( |
| | 1088 | gz: *GenZir, |
| | 1089 | tag: zir.Inst.Tag, |
| | 1090 | callee: zir.Inst.Ref, |
| | 1091 | args: []const zir.Inst.Ref, |
| | 1092 | /// Absolute node index. This function does the conversion to offset from Decl. |
| | 1093 | abs_node_index: ast.Node.Index, |
| | 1094 | ) !zir.Inst.Index { |
| | 1095 | assert(callee != 0); |
| | 1096 | assert(abs_node_index != 0); |
| | 1097 | const gpa = gz.zir_code.gpa; |
| | 1098 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); |
| | 1099 | try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1); |
| | 1100 | try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len + |
| | 1101 | @typeInfo(zir.Inst.Call).Struct.fields.len + args.len); |
| | 1102 | |
| | 1103 | const payload_index = gz.zir_code.addExtra(zir.Inst.Call{ |
| | 1104 | .callee = callee, |
| | 1105 | .args_len = @intCast(u32, args.len), |
| | 1106 | }) catch unreachable; // Capacity is ensured above. |
| | 1107 | gz.zir_code.extra.appendSliceAssumeCapacity(args); |
| | 1108 | |
| | 1109 | const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len); |
| | 1110 | gz.zir_code.instructions.appendAssumeCapacity(.{ |
| | 1111 | .tag = tag, |
| | 1112 | .data = .{ .pl_node = .{ |
| | 1113 | .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index), |
| | 1114 | .payload_index = payload_index, |
| | 1115 | } }, |
| | 1116 | }); |
| | 1117 | gz.instructions.appendAssumeCapacity(new_index); |
| | 1118 | return new_index + gz.zir_code.ref_start_index; |
| | 1119 | } |
| | 1120 | |
| 1079 | pub fn addInt(gz: *GenZir, integer: u64) !zir.Inst.Ref { | 1121 | pub fn addInt(gz: *GenZir, integer: u64) !zir.Inst.Ref { |
| 1080 | return gz.add(.{ | 1122 | return gz.add(.{ |
| 1081 | .tag = .int, | 1123 | .tag = .int, |
| ... | @@ -1095,7 +1137,7 @@ pub const Scope = struct { | ... | @@ -1095,7 +1137,7 @@ pub const Scope = struct { |
| 1095 | .tag = tag, | 1137 | .tag = tag, |
| 1096 | .data = .{ .un_node = .{ | 1138 | .data = .{ .un_node = .{ |
| 1097 | .operand = operand, | 1139 | .operand = operand, |
| 1098 | .src_node = abs_node_index - gz.zir_code.decl.srcNode(), | 1140 | .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index), |
| 1099 | } }, | 1141 | } }, |
| 1100 | }); | 1142 | }); |
| 1101 | } | 1143 | } |
| ... | @@ -1116,7 +1158,7 @@ pub const Scope = struct { | ... | @@ -1116,7 +1158,7 @@ pub const Scope = struct { |
| 1116 | gz.zir_code.instructions.appendAssumeCapacity(.{ | 1158 | gz.zir_code.instructions.appendAssumeCapacity(.{ |
| 1117 | .tag = tag, | 1159 | .tag = tag, |
| 1118 | .data = .{ .pl_node = .{ | 1160 | .data = .{ .pl_node = .{ |
| 1119 | .src_node = gz.zir_code.decl.srcNode() - abs_node_index, | 1161 | .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index), |
| 1120 | .payload_index = payload_index, | 1162 | .payload_index = payload_index, |
| 1121 | } }, | 1163 | } }, |
| 1122 | }); | 1164 | }); |
| ... | @@ -1177,7 +1219,7 @@ pub const Scope = struct { | ... | @@ -1177,7 +1219,7 @@ pub const Scope = struct { |
| 1177 | ) !zir.Inst.Ref { | 1219 | ) !zir.Inst.Ref { |
| 1178 | return gz.add(.{ | 1220 | return gz.add(.{ |
| 1179 | .tag = tag, | 1221 | .tag = tag, |
| 1180 | .data = .{ .node = abs_node_index - gz.zir_code.decl.srcNode() }, | 1222 | .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index) }, |
| 1181 | }); | 1223 | }); |
| 1182 | } | 1224 | } |
| 1183 | | 1225 | |
| ... | @@ -1205,14 +1247,14 @@ pub const Scope = struct { | ... | @@ -1205,14 +1247,14 @@ pub const Scope = struct { |
| 1205 | try gz.zir_code.instructions.append(gz.zir_code.gpa, .{ | 1247 | try gz.zir_code.instructions.append(gz.zir_code.gpa, .{ |
| 1206 | .tag = tag, | 1248 | .tag = tag, |
| 1207 | .data = .{ .pl_node = .{ | 1249 | .data = .{ .pl_node = .{ |
| 1208 | .src_node = node - gz.zir_code.decl.srcNode(), | 1250 | .src_node = gz.zir_code.decl.nodeIndexToRelative(node), |
| 1209 | .payload_index = undefined, | 1251 | .payload_index = undefined, |
| 1210 | } }, | 1252 | } }, |
| 1211 | }); | 1253 | }); |
| 1212 | return new_index; | 1254 | return new_index; |
| 1213 | } | 1255 | } |
| 1214 | | 1256 | |
| 1215 | fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref { | 1257 | pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref { |
| 1216 | const gpa = gz.zir_code.gpa; | 1258 | const gpa = gz.zir_code.gpa; |
| 1217 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 1259 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); |
| 1218 | try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1); | 1260 | try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1); |
| ... | @@ -1593,7 +1635,7 @@ pub const SrcLoc = struct { | ... | @@ -1593,7 +1635,7 @@ pub const SrcLoc = struct { |
| 1593 | }, | 1635 | }, |
| 1594 | .node_offset => |node_off| { | 1636 | .node_offset => |node_off| { |
| 1595 | const decl = src_loc.container.decl; | 1637 | const decl = src_loc.container.decl; |
| 1596 | const node_index = decl.srcNode() + node_off; | 1638 | const node_index = decl.relativeToNodeIndex(node_off); |
| 1597 | const tree = decl.container.file_scope.base.tree(); | 1639 | const tree = decl.container.file_scope.base.tree(); |
| 1598 | const tok_index = tree.firstToken(node_index); | 1640 | const tok_index = tree.firstToken(node_index); |
| 1599 | const token_starts = tree.tokens.items(.start); | 1641 | const token_starts = tree.tokens.items(.start); |
| ... | @@ -1659,84 +1701,84 @@ pub const LazySrcLoc = union(enum) { | ... | @@ -1659,84 +1701,84 @@ pub const LazySrcLoc = union(enum) { |
| 1659 | /// The source location points to an AST node, which is this value offset | 1701 | /// The source location points to an AST node, which is this value offset |
| 1660 | /// from its containing Decl node AST index. | 1702 | /// from its containing Decl node AST index. |
| 1661 | /// The Decl is determined contextually. | 1703 | /// The Decl is determined contextually. |
| 1662 | node_offset: u32, | 1704 | node_offset: i32, |
| 1663 | /// The source location points to a variable declaration type expression, | 1705 | /// The source location points to a variable declaration type expression, |
| 1664 | /// found by taking this AST node index offset from the containing | 1706 | /// found by taking this AST node index offset from the containing |
| 1665 | /// Decl AST node, which points to a variable declaration AST node. Next, navigate | 1707 | /// Decl AST node, which points to a variable declaration AST node. Next, navigate |
| 1666 | /// to the type expression. | 1708 | /// to the type expression. |
| 1667 | /// The Decl is determined contextually. | 1709 | /// The Decl is determined contextually. |
| 1668 | node_offset_var_decl_ty: u32, | 1710 | node_offset_var_decl_ty: i32, |
| 1669 | /// The source location points to a for loop condition expression, | 1711 | /// The source location points to a for loop condition expression, |
| 1670 | /// found by taking this AST node index offset from the containing | 1712 | /// found by taking this AST node index offset from the containing |
| 1671 | /// Decl AST node, which points to a for loop AST node. Next, navigate | 1713 | /// Decl AST node, which points to a for loop AST node. Next, navigate |
| 1672 | /// to the condition expression. | 1714 | /// to the condition expression. |
| 1673 | /// The Decl is determined contextually. | 1715 | /// The Decl is determined contextually. |
| 1674 | node_offset_for_cond: u32, | 1716 | node_offset_for_cond: i32, |
| 1675 | /// The source location points to the first parameter of a builtin | 1717 | /// The source location points to the first parameter of a builtin |
| 1676 | /// function call, found by taking this AST node index offset from the containing | 1718 | /// function call, found by taking this AST node index offset from the containing |
| 1677 | /// Decl AST node, which points to a builtin call AST node. Next, navigate | 1719 | /// Decl AST node, which points to a builtin call AST node. Next, navigate |
| 1678 | /// to the first parameter. | 1720 | /// to the first parameter. |
| 1679 | /// The Decl is determined contextually. | 1721 | /// The Decl is determined contextually. |
| 1680 | node_offset_builtin_call_arg0: u32, | 1722 | node_offset_builtin_call_arg0: i32, |
| 1681 | /// Same as `node_offset_builtin_call_arg0` except arg index 1. | 1723 | /// Same as `node_offset_builtin_call_arg0` except arg index 1. |
| 1682 | node_offset_builtin_call_arg1: u32, | 1724 | node_offset_builtin_call_arg1: i32, |
| 1683 | /// Same as `node_offset_builtin_call_arg0` except the arg index is contextually | 1725 | /// Same as `node_offset_builtin_call_arg0` except the arg index is contextually |
| 1684 | /// determined. | 1726 | /// determined. |
| 1685 | node_offset_builtin_call_argn: u32, | 1727 | node_offset_builtin_call_argn: i32, |
| 1686 | /// The source location points to the index expression of an array access | 1728 | /// The source location points to the index expression of an array access |
| 1687 | /// expression, found by taking this AST node index offset from the containing | 1729 | /// expression, found by taking this AST node index offset from the containing |
| 1688 | /// Decl AST node, which points to an array access AST node. Next, navigate | 1730 | /// Decl AST node, which points to an array access AST node. Next, navigate |
| 1689 | /// to the index expression. | 1731 | /// to the index expression. |
| 1690 | /// The Decl is determined contextually. | 1732 | /// The Decl is determined contextually. |
| 1691 | node_offset_array_access_index: u32, | 1733 | node_offset_array_access_index: i32, |
| 1692 | /// The source location points to the sentinel expression of a slice | 1734 | /// The source location points to the sentinel expression of a slice |
| 1693 | /// expression, found by taking this AST node index offset from the containing | 1735 | /// expression, found by taking this AST node index offset from the containing |
| 1694 | /// Decl AST node, which points to a slice AST node. Next, navigate | 1736 | /// Decl AST node, which points to a slice AST node. Next, navigate |
| 1695 | /// to the sentinel expression. | 1737 | /// to the sentinel expression. |
| 1696 | /// The Decl is determined contextually. | 1738 | /// The Decl is determined contextually. |
| 1697 | node_offset_slice_sentinel: u32, | 1739 | node_offset_slice_sentinel: i32, |
| 1698 | /// The source location points to the callee expression of a function | 1740 | /// The source location points to the callee expression of a function |
| 1699 | /// call expression, found by taking this AST node index offset from the containing | 1741 | /// call expression, found by taking this AST node index offset from the containing |
| 1700 | /// Decl AST node, which points to a function call AST node. Next, navigate | 1742 | /// Decl AST node, which points to a function call AST node. Next, navigate |
| 1701 | /// to the callee expression. | 1743 | /// to the callee expression. |
| 1702 | /// The Decl is determined contextually. | 1744 | /// The Decl is determined contextually. |
| 1703 | node_offset_call_func: u32, | 1745 | node_offset_call_func: i32, |
| 1704 | /// The source location points to the field name of a field access expression, | 1746 | /// The source location points to the field name of a field access expression, |
| 1705 | /// found by taking this AST node index offset from the containing | 1747 | /// found by taking this AST node index offset from the containing |
| 1706 | /// Decl AST node, which points to a field access AST node. Next, navigate | 1748 | /// Decl AST node, which points to a field access AST node. Next, navigate |
| 1707 | /// to the field name token. | 1749 | /// to the field name token. |
| 1708 | /// The Decl is determined contextually. | 1750 | /// The Decl is determined contextually. |
| 1709 | node_offset_field_name: u32, | 1751 | node_offset_field_name: i32, |
| 1710 | /// The source location points to the pointer of a pointer deref expression, | 1752 | /// The source location points to the pointer of a pointer deref expression, |
| 1711 | /// found by taking this AST node index offset from the containing | 1753 | /// found by taking this AST node index offset from the containing |
| 1712 | /// Decl AST node, which points to a pointer deref AST node. Next, navigate | 1754 | /// Decl AST node, which points to a pointer deref AST node. Next, navigate |
| 1713 | /// to the pointer expression. | 1755 | /// to the pointer expression. |
| 1714 | /// The Decl is determined contextually. | 1756 | /// The Decl is determined contextually. |
| 1715 | node_offset_deref_ptr: u32, | 1757 | node_offset_deref_ptr: i32, |
| 1716 | /// The source location points to the assembly source code of an inline assembly | 1758 | /// The source location points to the assembly source code of an inline assembly |
| 1717 | /// expression, found by taking this AST node index offset from the containing | 1759 | /// expression, found by taking this AST node index offset from the containing |
| 1718 | /// Decl AST node, which points to inline assembly AST node. Next, navigate | 1760 | /// Decl AST node, which points to inline assembly AST node. Next, navigate |
| 1719 | /// to the asm template source code. | 1761 | /// to the asm template source code. |
| 1720 | /// The Decl is determined contextually. | 1762 | /// The Decl is determined contextually. |
| 1721 | node_offset_asm_source: u32, | 1763 | node_offset_asm_source: i32, |
| 1722 | /// The source location points to the return type of an inline assembly | 1764 | /// The source location points to the return type of an inline assembly |
| 1723 | /// expression, found by taking this AST node index offset from the containing | 1765 | /// expression, found by taking this AST node index offset from the containing |
| 1724 | /// Decl AST node, which points to inline assembly AST node. Next, navigate | 1766 | /// Decl AST node, which points to inline assembly AST node. Next, navigate |
| 1725 | /// to the return type expression. | 1767 | /// to the return type expression. |
| 1726 | /// The Decl is determined contextually. | 1768 | /// The Decl is determined contextually. |
| 1727 | node_offset_asm_ret_ty: u32, | 1769 | node_offset_asm_ret_ty: i32, |
| 1728 | /// The source location points to the condition expression of an if | 1770 | /// The source location points to the condition expression of an if |
| 1729 | /// expression, found by taking this AST node index offset from the containing | 1771 | /// expression, found by taking this AST node index offset from the containing |
| 1730 | /// Decl AST node, which points to an if expression AST node. Next, navigate | 1772 | /// Decl AST node, which points to an if expression AST node. Next, navigate |
| 1731 | /// to the condition expression. | 1773 | /// to the condition expression. |
| 1732 | /// The Decl is determined contextually. | 1774 | /// The Decl is determined contextually. |
| 1733 | node_offset_if_cond: u32, | 1775 | node_offset_if_cond: i32, |
| 1734 | /// The source location points to the type expression of an `anyframe->T` | 1776 | /// The source location points to the type expression of an `anyframe->T` |
| 1735 | /// expression, found by taking this AST node index offset from the containing | 1777 | /// expression, found by taking this AST node index offset from the containing |
| 1736 | /// Decl AST node, which points to a `anyframe->T` expression AST node. Next, navigate | 1778 | /// Decl AST node, which points to a `anyframe->T` expression AST node. Next, navigate |
| 1737 | /// to the type expression. | 1779 | /// to the type expression. |
| 1738 | /// The Decl is determined contextually. | 1780 | /// The Decl is determined contextually. |
| 1739 | node_offset_anyframe_type: u32, | 1781 | node_offset_anyframe_type: i32, |
| 1740 | | 1782 | |
| 1741 | /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope. | 1783 | /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope. |
| 1742 | pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc { | 1784 | pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc { |
| ... | @@ -3678,8 +3720,7 @@ pub fn failTok( | ... | @@ -3678,8 +3720,7 @@ pub fn failTok( |
| 3678 | comptime format: []const u8, | 3720 | comptime format: []const u8, |
| 3679 | args: anytype, | 3721 | args: anytype, |
| 3680 | ) InnerError { | 3722 | ) InnerError { |
| 3681 | const decl_token = scope.srcDecl().?.srcToken(); | 3723 | const src = scope.srcDecl().?.tokSrcLoc(token_index); |
| 3682 | const src: LazySrcLoc = .{ .token_offset = token_index - decl_token }; | | |
| 3683 | return mod.fail(scope, src, format, args); | 3724 | return mod.fail(scope, src, format, args); |
| 3684 | } | 3725 | } |
| 3685 | | 3726 | |
| ... | @@ -3692,8 +3733,7 @@ pub fn failNode( | ... | @@ -3692,8 +3733,7 @@ pub fn failNode( |
| 3692 | comptime format: []const u8, | 3733 | comptime format: []const u8, |
| 3693 | args: anytype, | 3734 | args: anytype, |
| 3694 | ) InnerError { | 3735 | ) InnerError { |
| 3695 | const decl_node = scope.srcDecl().?.srcNode(); | 3736 | const src = scope.srcDecl().?.nodeSrcLoc(node_index); |
| 3696 | const src: LazySrcLoc = .{ .node_offset = decl_node - node_index }; | | |
| 3697 | return mod.fail(scope, src, format, args); | 3737 | return mod.fail(scope, src, format, args); |
| 3698 | } | 3738 | } |
| 3699 | | 3739 | |