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