authorgravatar for vallahor91@gmail.comVallahor <vallahor91@gmail.com> 2022-05-28 18:28:21-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
log927f087a406602dfca4f56b71cec378ffbfa2949
treec6cd1747aab3b664210efb35455ddeb9ee6f899b
parent0e5a2bbd63d8acb8c570a384db875bc0f25a4999

fix: calling convention


2 files changed, 148 insertions(+), 64 deletions(-)

lib/docs/main.js+2-6
...@@ -1180,10 +1180,6 @@ var zigAnalysis;...@@ -1180,10 +1180,6 @@ var zigAnalysis;
1180 if (typeof typeObj === 'number') typeObj = zigAnalysis.types[typeObj];1180 if (typeof typeObj === 'number') typeObj = zigAnalysis.types[typeObj];
1181 switch (typeObj.kind) {1181 switch (typeObj.kind) {
1182 default: throw "TODO";1182 default: throw "TODO";
1183 case typeKinds.Unanalyzed:
1184 {
1185 return "Unanalyzed";
1186 }
1187 case typeKinds.ComptimeExpr:1183 case typeKinds.ComptimeExpr:
1188 {1184 {
1189 return "anyopaque";1185 return "anyopaque";
...@@ -1528,8 +1524,8 @@ var zigAnalysis;...@@ -1528,8 +1524,8 @@ var zigAnalysis;
15281524
1529 payloadHtml += ') ';1525 payloadHtml += ') ';
1530 if (fnObj.has_cc) {1526 if (fnObj.has_cc) {
1531 let cc = zigAnalysis.types[fnObj.cc]1527 let cc = zigAnalysis.exprs[fnObj.cc]
1532 payloadHtml += "callconv(." + cc.name + ") ";1528 payloadHtml += "callconv(." + cc.enumLiteral + ") ";
1533 }1529 }
15341530
1535 if (fnObj.is_inferred_error) {1531 if (fnObj.is_inferred_error) {
src/Autodoc.zig+146-58
...@@ -162,9 +162,9 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -162,9 +162,9 @@ pub fn generateZirData(self: *Autodoc) !void {
162 .anyerror_type => .{162 .anyerror_type => .{
163 .ErrorSet = .{ .name = tmpbuf.toOwnedSlice() },163 .ErrorSet = .{ .name = tmpbuf.toOwnedSlice() },
164 },164 },
165 // .calling_convention_inline, .calling_convention_c, .calling_convention_type => .{165 .calling_convention_inline, .calling_convention_c, .calling_convention_type => .{
166 // .EnumLiteral = .{ .name = tmpbuf.toOwnedSlice() },166 .EnumLiteral = .{ .name = tmpbuf.toOwnedSlice() },
167 // },167 },
168 },168 },
169 );169 );
170 }170 }
...@@ -520,13 +520,6 @@ const DocData = struct {...@@ -520,13 +520,6 @@ const DocData = struct {
520 .Type => |v| try printTypeBody(v, options, w),520 .Type => |v| try printTypeBody(v, options, w),
521 .NoReturn => |v| try printTypeBody(v, options, w),521 .NoReturn => |v| try printTypeBody(v, options, w),
522 .EnumLiteral => |v| try printTypeBody(v, options, w),522 .EnumLiteral => |v| try printTypeBody(v, options, w),
523 .Unanalyzed => |_| {
524 if (options.whitespace) |ws| try ws.outputIndent(w);
525 try w.print(
526 \\"Unanalyzed": "Unanalyzed"
527 \\
528 , .{});
529 },
530 .Pointer => |v| {523 .Pointer => |v| {
531 if (options.whitespace) |ws| try ws.outputIndent(w);524 if (options.whitespace) |ws| try ws.outputIndent(w);
532 try w.print(525 try w.print(
...@@ -885,7 +878,7 @@ fn walkInstruction(...@@ -885,7 +878,7 @@ fn walkInstruction(
885 const literal = file.zir.nullTerminatedString(str_tok.start);878 const literal = file.zir.nullTerminatedString(str_tok.start);
886 const type_index = self.types.items.len;879 const type_index = self.types.items.len;
887 try self.types.append(self.arena, .{880 try self.types.append(self.arena, .{
888 .EnumLiteral = .{ .name = literal },881 .EnumLiteral = .{ .name = "todo enum literal" },
889 });882 });
890883
891 return DocData.WalkResult{884 return DocData.WalkResult{
...@@ -1643,17 +1636,33 @@ fn walkInstruction(...@@ -1643,17 +1636,33 @@ fn walkInstruction(
1643 .expr = .{ .call = call_slot_index },1636 .expr = .{ .call = call_slot_index },
1644 };1637 };
1645 },1638 },
1646 .func, .func_inferred, .func_extended => {1639 .func, .func_inferred => {
1640 const type_slot_index = self.types.items.len;
1641 try self.types.append(self.arena, .{ .Unanalyzed = {} });
1642
1643 const result = self.analyzeFunction(
1644 file,
1645 parent_scope,
1646 inst_index,
1647 self_ast_node_index,
1648 type_slot_index,
1649 );
1650
1651 return result;
1652 },
1653 .func_extended => {
1647 const type_slot_index = self.types.items.len;1654 const type_slot_index = self.types.items.len;
1648 try self.types.append(self.arena, .{ .Unanalyzed = {} });1655 try self.types.append(self.arena, .{ .Unanalyzed = {} });
16491656
1650 return self.analyzeFunction(1657 const result = self.analyzeFunctionExtended(
1651 file,1658 file,
1652 parent_scope,1659 parent_scope,
1653 inst_index,1660 inst_index,
1654 self_ast_node_index,1661 self_ast_node_index,
1655 type_slot_index,1662 type_slot_index,
1656 );1663 );
1664
1665 return result;
1657 },1666 },
1658 .extended => {1667 .extended => {
1659 const extended = data[inst_index].extended;1668 const extended = data[inst_index].extended;
...@@ -2677,7 +2686,119 @@ fn tryResolveRefPath(...@@ -2677,7 +2686,119 @@ fn tryResolveRefPath(
2677 // that said, we might want to store it elsewhere and reclaim memory asap2686 // that said, we might want to store it elsewhere and reclaim memory asap
2678 }2687 }
2679}2688}
2689fn analyzeFunctionExtended(
2690 self: *Autodoc,
2691 file: *File,
2692 scope: *Scope,
2693 inst_index: usize,
2694 self_ast_node_index: usize,
2695 type_slot_index: usize,
2696) error{OutOfMemory}!DocData.WalkResult {
2697 const tags = file.zir.instructions.items(.tag);
2698 const data = file.zir.instructions.items(.data);
2699 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));
2700
2701 try self.ast_nodes.ensureUnusedCapacity(self.arena, fn_info.total_params_len);
2702 var param_type_refs = try std.ArrayListUnmanaged(DocData.Expr).initCapacity(
2703 self.arena,
2704 fn_info.total_params_len,
2705 );
2706 var param_ast_indexes = try std.ArrayListUnmanaged(usize).initCapacity(
2707 self.arena,
2708 fn_info.total_params_len,
2709 );
2710
2711 // TODO: handle scope rules for fn parameters
2712 for (fn_info.param_body[0..fn_info.total_params_len]) |param_index| {
2713 switch (tags[param_index]) {
2714 else => {
2715 panicWithContext(
2716 file,
2717 param_index,
2718 "TODO: handle `{s}` in walkInstruction.func\n",
2719 .{@tagName(tags[param_index])},
2720 );
2721 },
2722 .param_anytype, .param_anytype_comptime => {
2723 // TODO: where are the doc comments?
2724 const str_tok = data[param_index].str_tok;
26802725
2726 const name = str_tok.get(file.zir);
2727
2728 param_ast_indexes.appendAssumeCapacity(self.ast_nodes.items.len);
2729 self.ast_nodes.appendAssumeCapacity(.{
2730 .name = name,
2731 .docs = "",
2732 .@"comptime" = tags[param_index] == .param_anytype_comptime,
2733 });
2734
2735 param_type_refs.appendAssumeCapacity(
2736 DocData.Expr{ .@"anytype" = {} },
2737 );
2738 },
2739 .param, .param_comptime => {
2740 const pl_tok = data[param_index].pl_tok;
2741 const extra = file.zir.extraData(Zir.Inst.Param, pl_tok.payload_index);
2742 const doc_comment = if (extra.data.doc_comment != 0)
2743 file.zir.nullTerminatedString(extra.data.doc_comment)
2744 else
2745 "";
2746 const name = file.zir.nullTerminatedString(extra.data.name);
2747
2748 param_ast_indexes.appendAssumeCapacity(self.ast_nodes.items.len);
2749 try self.ast_nodes.append(self.arena, .{
2750 .name = name,
2751 .docs = doc_comment,
2752 .@"comptime" = tags[param_index] == .param_comptime,
2753 });
2754
2755 const break_index = file.zir.extra[extra.end..][extra.data.body_len - 1];
2756 const break_operand = data[break_index].@"break".operand;
2757 const param_type_ref = try self.walkRef(file, scope, break_operand, false);
2758
2759 param_type_refs.appendAssumeCapacity(param_type_ref.expr);
2760 },
2761 }
2762 }
2763
2764 // ret
2765 const ret_type_ref = blk: {
2766 const last_instr_index = fn_info.ret_ty_body[fn_info.ret_ty_body.len - 1];
2767 const break_operand = data[last_instr_index].@"break".operand;
2768 const wr = try self.walkRef(file, scope, break_operand, false);
2769
2770 break :blk wr;
2771 };
2772
2773 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;
2774 const inst_data = data[inst_index].pl_node;
2775
2776 const extra = file.zir.extraData(Zir.Inst.ExtendedFunc, inst_data.payload_index);
2777 var cc_index: ?usize = null;
2778 if (extra.data.bits.has_cc) {
2779 const cc_ref = @intToEnum(Zir.Inst.Ref, file.zir.extra[extra.end]);
2780 cc_index = self.exprs.items.len;
2781 _ = try self.walkRef(file, scope, cc_ref, false);
2782 }
2783
2784 self.types.items[type_slot_index] = .{
2785 .Fn = .{
2786 .name = "todo_name func",
2787 .src = self_ast_node_index,
2788 .params = param_type_refs.items,
2789 .ret = ret_type_ref.expr,
2790 .is_extern = extra.data.bits.is_extern,
2791 .has_cc = extra.data.bits.has_cc,
2792 .is_inferred_error = extra.data.bits.is_inferred_error,
2793 .cc = cc_index,
2794 },
2795 };
2796
2797 return DocData.WalkResult{
2798 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
2799 .expr = .{ .type = type_slot_index },
2800 };
2801}
2681fn analyzeFunction(2802fn analyzeFunction(
2682 self: *Autodoc,2803 self: *Autodoc,
2683 file: *File,2804 file: *File,
...@@ -2763,46 +2884,13 @@ fn analyzeFunction(...@@ -2763,46 +2884,13 @@ fn analyzeFunction(
2763 };2884 };
27642885
2765 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;2886 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;
2766 self.types.items[type_slot_index] = switch (tags[inst_index]) {2887 self.types.items[type_slot_index] = .{
2767 .func, .func_inferred => blk: {2888 .Fn = .{
2768 break :blk .{2889 .name = "todo_name func",
2769 .Fn = .{2890 .src = self_ast_node_index,
2770 .name = "todo_name func",2891 .params = param_type_refs.items,
2771 .src = self_ast_node_index,2892 .ret = ret_type_ref.expr,
2772 .params = param_type_refs.items,
2773 .ret = ret_type_ref.expr,
2774 },
2775 };
2776 },
2777 .func_extended => blk: {
2778 const inst_data = data[inst_index].pl_node;
2779 const extra = file.zir.extraData(Zir.Inst.ExtendedFunc, inst_data.payload_index);
2780 var cc_index: ?usize = null;
2781 cc_index = self.types.items.len - 1;
2782 // if (extra.data.bits.has_cc) {
2783 // const cc = file.zir.extra[extra.end];
2784 // const cc_ref = @intToEnum(Zir.Inst.Ref, cc);
2785 // _ = try self.walkRef(file, scope, cc_ref, false);
2786 // cc_index = self.types.items.len - 1;
2787 // std.debug.print("DONE\n", .{});
2788 // std.debug.print("cc_ref = {any}\n", .{cc_ref});
2789 // std.debug.print("index = {}\n", .{cc_index});
2790 // }
2791
2792 break :blk .{
2793 .Fn = .{
2794 .name = "todo_name func",
2795 .src = self_ast_node_index,
2796 .params = param_type_refs.items,
2797 .ret = ret_type_ref.expr,
2798 .is_extern = extra.data.bits.is_extern,
2799 .has_cc = extra.data.bits.has_cc,
2800 .is_inferred_error = extra.data.bits.is_inferred_error,
2801 .cc = cc_index,
2802 },
2803 };
2804 },2893 },
2805 else => unreachable,
2806 };2894 };
28072895
2808 return DocData.WalkResult{2896 return DocData.WalkResult{
...@@ -3032,22 +3120,22 @@ fn walkRef(...@@ -3032,22 +3120,22 @@ fn walkRef(
3032 // TODO: dunno what to do with those3120 // TODO: dunno what to do with those
3033 .calling_convention_type => {3121 .calling_convention_type => {
3034 return DocData.WalkResult{3122 return DocData.WalkResult{
3035 // .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },3123 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },
3036 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },3124 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
3037 .expr = .{ .int = .{ .value = 1 } },3125 .expr = .{ .int = .{ .value = 1 } },
3038 };3126 };
3039 },3127 },
3040 .calling_convention_c => {3128 .calling_convention_c => {
3041 return DocData.WalkResult{3129 return DocData.WalkResult{
3042 // .typeRef = .{ .type = @enumToInt(Ref.calling_convention_c) },3130 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_c) },
3043 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },3131 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
3044 .expr = .{ .int = .{ .value = 1 } },3132 .expr = .{ .int = .{ .value = 1 } },
3045 };3133 };
3046 },3134 },
3047 .calling_convention_inline => {3135 .calling_convention_inline => {
3048 return DocData.WalkResult{3136 return DocData.WalkResult{
3049 // .typeRef = .{ .type = @enumToInt(Ref.calling_convention_inline) },3137 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_inline) },
3050 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },3138 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
3051 .expr = .{ .int = .{ .value = 1 } },3139 .expr = .{ .int = .{ .value = 1 } },
3052 };3140 };
3053 },3141 },