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;
11801180 if (typeof typeObj === 'number') typeObj = zigAnalysis.types[typeObj];
11811181 switch (typeObj.kind) {
11821182 default: throw "TODO";
1183 case typeKinds.Unanalyzed:
1184 {
1185 return "Unanalyzed";
1186 }
11871183 case typeKinds.ComptimeExpr:
11881184 {
11891185 return "anyopaque";
......@@ -1528,8 +1524,8 @@ var zigAnalysis;
15281524
15291525 payloadHtml += ') ';
15301526 if (fnObj.has_cc) {
1531 let cc = zigAnalysis.types[fnObj.cc]
1532 payloadHtml += "callconv(." + cc.name + ") ";
1527 let cc = zigAnalysis.exprs[fnObj.cc]
1528 payloadHtml += "callconv(." + cc.enumLiteral + ") ";
15331529 }
15341530
15351531 if (fnObj.is_inferred_error) {
src/Autodoc.zig+146-58
......@@ -162,9 +162,9 @@ pub fn generateZirData(self: *Autodoc) !void {
162162 .anyerror_type => .{
163163 .ErrorSet = .{ .name = tmpbuf.toOwnedSlice() },
164164 },
165 // .calling_convention_inline, .calling_convention_c, .calling_convention_type => .{
166 // .EnumLiteral = .{ .name = tmpbuf.toOwnedSlice() },
167 // },
165 .calling_convention_inline, .calling_convention_c, .calling_convention_type => .{
166 .EnumLiteral = .{ .name = tmpbuf.toOwnedSlice() },
167 },
168168 },
169169 );
170170 }
......@@ -520,13 +520,6 @@ const DocData = struct {
520520 .Type => |v| try printTypeBody(v, options, w),
521521 .NoReturn => |v| try printTypeBody(v, options, w),
522522 .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 },
530523 .Pointer => |v| {
531524 if (options.whitespace) |ws| try ws.outputIndent(w);
532525 try w.print(
......@@ -885,7 +878,7 @@ fn walkInstruction(
885878 const literal = file.zir.nullTerminatedString(str_tok.start);
886879 const type_index = self.types.items.len;
887880 try self.types.append(self.arena, .{
888 .EnumLiteral = .{ .name = literal },
881 .EnumLiteral = .{ .name = "todo enum literal" },
889882 });
890883
891884 return DocData.WalkResult{
......@@ -1643,17 +1636,33 @@ fn walkInstruction(
16431636 .expr = .{ .call = call_slot_index },
16441637 };
16451638 },
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 => {
16471654 const type_slot_index = self.types.items.len;
16481655 try self.types.append(self.arena, .{ .Unanalyzed = {} });
16491656
1650 return self.analyzeFunction(
1657 const result = self.analyzeFunctionExtended(
16511658 file,
16521659 parent_scope,
16531660 inst_index,
16541661 self_ast_node_index,
16551662 type_slot_index,
16561663 );
1664
1665 return result;
16571666 },
16581667 .extended => {
16591668 const extended = data[inst_index].extended;
......@@ -2677,7 +2686,119 @@ fn tryResolveRefPath(
26772686 // that said, we might want to store it elsewhere and reclaim memory asap
26782687 }
26792688}
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}
26812802fn analyzeFunction(
26822803 self: *Autodoc,
26832804 file: *File,
......@@ -2763,46 +2884,13 @@ fn analyzeFunction(
27632884 };
27642885
27652886 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;
2766 self.types.items[type_slot_index] = switch (tags[inst_index]) {
2767 .func, .func_inferred => blk: {
2768 break :blk .{
2769 .Fn = .{
2770 .name = "todo_name func",
2771 .src = self_ast_node_index,
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 };
2887 self.types.items[type_slot_index] = .{
2888 .Fn = .{
2889 .name = "todo_name func",
2890 .src = self_ast_node_index,
2891 .params = param_type_refs.items,
2892 .ret = ret_type_ref.expr,
28042893 },
2805 else => unreachable,
28062894 };
28072895
28082896 return DocData.WalkResult{
......@@ -3032,22 +3120,22 @@ fn walkRef(
30323120 // TODO: dunno what to do with those
30333121 .calling_convention_type => {
30343122 return DocData.WalkResult{
3035 // .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },
3036 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
3123 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },
3124 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
30373125 .expr = .{ .int = .{ .value = 1 } },
30383126 };
30393127 },
30403128 .calling_convention_c => {
30413129 return DocData.WalkResult{
3042 // .typeRef = .{ .type = @enumToInt(Ref.calling_convention_c) },
3043 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
3130 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_c) },
3131 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
30443132 .expr = .{ .int = .{ .value = 1 } },
30453133 };
30463134 },
30473135 .calling_convention_inline => {
30483136 return DocData.WalkResult{
3049 // .typeRef = .{ .type = @enumToInt(Ref.calling_convention_inline) },
3050 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
3137 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_inline) },
3138 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
30513139 .expr = .{ .int = .{ .value = 1 } },
30523140 };
30533141 },