authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-04-21 11:52:07+02:00
committergravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-04-21 11:52:07+02:00
log7e18bd7f7108dd3b1d018212d2c1aa6222fe99bd
tree99f55b52b88c4dc0f3a0a40230a45e2521ed7d0e
parenta774f9334473822fd7c6c828dd3fb873788b2f72

autodoc: Handle calling conventions better

special case inline cc in exprName

2 files changed, 47 insertions(+), 24 deletions(-)

lib/docs/main.js+21-6
...@@ -2198,13 +2198,26 @@ const NAV_MODES = {...@@ -2198,13 +2198,26 @@ const NAV_MODES = {
2198 if (opts.addParensIfFnSignature && fnObj.src == 0) {2198 if (opts.addParensIfFnSignature && fnObj.src == 0) {
2199 payloadHtml += "(";2199 payloadHtml += "(";
2200 }2200 }
2201 if (opts.wantHtml) {2201 if (fnObj.is_extern) {
2202 if (fnObj.is_extern) {2202 if (opts.wantHtml) {
2203 payloadHtml += "pub extern ";2203 payloadHtml += '<span class="tok-kw">extern </span>';
2204 } else {
2205 payloadHtml += "extern ";
2204 }2206 }
2205 if (fnObj.has_lib_name) {2207 } else if (fnObj.has_cc) {
2206 payloadHtml += '"' + fnObj.lib_name + '" ';2208 let cc_expr = zigAnalysis.exprs[fnObj.cc];
2209 if (cc_expr.enumLiteral === "Inline") {
2210 if(opts.wantHtml) {
2211 payloadHtml += '<span class="tok-kw">inline </span>'
2212 } else {
2213 payloadHtml += "inline "
2214 }
2207 }2215 }
2216 }
2217 if (fnObj.has_lib_name) {
2218 payloadHtml += '"' + fnObj.lib_name + '" ';
2219 }
2220 if (opts.wantHtml) {
2208 payloadHtml += '<span class="tok-kw">fn </span>';2221 payloadHtml += '<span class="tok-kw">fn </span>';
2209 if (fnDecl) {2222 if (fnDecl) {
2210 payloadHtml += '<span class="tok-fn">';2223 payloadHtml += '<span class="tok-fn">';
...@@ -2324,7 +2337,9 @@ const NAV_MODES = {...@@ -2324,7 +2337,9 @@ const NAV_MODES = {
2324 if (fnObj.has_cc) {2337 if (fnObj.has_cc) {
2325 let cc = zigAnalysis.exprs[fnObj.cc];2338 let cc = zigAnalysis.exprs[fnObj.cc];
2326 if (cc) {2339 if (cc) {
2327 payloadHtml += "callconv(." + cc.enumLiteral + ") ";2340 if (cc.enumLiteral !== "Inline") {
2341 payloadHtml += "callconv(" + exprName(cc, opts) + ") ";
2342 }
2328 }2343 }
2329 }2344 }
23302345
src/Autodoc.zig+26-18
...@@ -112,7 +112,7 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -112,7 +112,7 @@ pub fn generateZirData(self: *Autodoc) !void {
112 .ComptimeExpr = .{ .name = "ComptimeExpr" },112 .ComptimeExpr = .{ .name = "ComptimeExpr" },
113 });113 });
114114
115 // this skipts Ref.none but it's ok becuse we replaced it with ComptimeExpr115 // this skips Ref.none but it's ok becuse we replaced it with ComptimeExpr
116 var i: u32 = 1;116 var i: u32 = 1;
117 while (i <= @enumToInt(Ref.anyerror_void_error_union_type)) : (i += 1) {117 while (i <= @enumToInt(Ref.anyerror_void_error_union_type)) : (i += 1) {
118 var tmpbuf = std.ArrayList(u8).init(self.arena);118 var tmpbuf = std.ArrayList(u8).init(self.arena);
...@@ -196,8 +196,10 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -196,8 +196,10 @@ pub fn generateZirData(self: *Autodoc) !void {
196 .anyerror_type => .{196 .anyerror_type => .{
197 .ErrorSet = .{ .name = try tmpbuf.toOwnedSlice() },197 .ErrorSet = .{ .name = try tmpbuf.toOwnedSlice() },
198 },198 },
199 .calling_convention_inline, .calling_convention_c, .calling_convention_type => .{199 // should be an Enum but if we don't analyze std we don't get the ast node
200 .EnumLiteral = .{ .name = try tmpbuf.toOwnedSlice() },200 // since it's std.builtin.CallingConvention
201 .calling_convention_type => .{
202 .Type = .{ .name = try tmpbuf.toOwnedSlice() },
201 },203 },
202 },204 },
203 );205 );
...@@ -4009,17 +4011,27 @@ fn analyzeFancyFunction(...@@ -4009,17 +4011,27 @@ fn analyzeFancyFunction(
4009 }4011 }
40104012
4011 var cc_index: ?usize = null;4013 var cc_index: ?usize = null;
4012 if (extra.data.bits.has_cc_ref) {4014 if (extra.data.bits.has_cc_ref and !extra.data.bits.has_cc_body) {
4013 const cc_ref = @intToEnum(Zir.Inst.Ref, file.zir.extra[extra_index]);4015 const cc_ref = @intToEnum(Zir.Inst.Ref, file.zir.extra[extra_index]);
4016 const cc_expr = try self.walkRef(file, scope, parent_src, cc_ref, false);
4017
4014 cc_index = self.exprs.items.len;4018 cc_index = self.exprs.items.len;
4015 _ = try self.walkRef(file, scope, parent_src, cc_ref, false);4019 try self.exprs.append(self.arena, cc_expr.expr);
4020
4016 extra_index += 1;4021 extra_index += 1;
4017 } else if (extra.data.bits.has_cc_body) {4022 } else if (extra.data.bits.has_cc_body) {
4018 const cc_body_len = file.zir.extra[extra_index];4023 const cc_body_len = file.zir.extra[extra_index];
4019 extra_index += 1;4024 extra_index += 1;
4020 const cc_body = file.zir.extra[extra_index .. extra_index + cc_body_len];4025 const cc_body = file.zir.extra[extra_index..][0..cc_body_len];
4021 _ = cc_body;4026
4022 // TODO: analyze the block (or bail with a comptimeExpr)4027 // We assume the body ends with a break_inline
4028 const break_index = cc_body[cc_body.len - 1];
4029 const break_operand = data[break_index].@"break".operand;
4030 const cc_expr = try self.walkRef(file, scope, parent_src, break_operand, false);
4031
4032 cc_index = self.exprs.items.len;
4033 try self.exprs.append(self.arena, cc_expr.expr);
4034
4023 extra_index += cc_body_len;4035 extra_index += cc_body_len;
4024 } else {4036 } else {
4025 // auto calling convention4037 // auto calling convention
...@@ -4564,26 +4576,22 @@ fn walkRef(...@@ -4564,26 +4576,22 @@ fn walkRef(
4564 .expr = .{ .int = .{ .value = 1 } },4576 .expr = .{ .int = .{ .value = 1 } },
4565 };4577 };
4566 },4578 },
4567 // TODO: dunno what to do with those
4568 .calling_convention_type => {4579 .calling_convention_type => {
4569 return DocData.WalkResult{4580 return DocData.WalkResult{
4570 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },4581 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
4571 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },4582 .expr = .{ .type = @enumToInt(Ref.calling_convention_type) },
4572 .expr = .{ .int = .{ .value = 1 } },
4573 };4583 };
4574 },4584 },
4575 .calling_convention_c => {4585 .calling_convention_c => {
4576 return DocData.WalkResult{4586 return DocData.WalkResult{
4577 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_c) },4587 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },
4578 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },4588 .expr = .{ .enumLiteral = "C" },
4579 .expr = .{ .int = .{ .value = 1 } },
4580 };4589 };
4581 },4590 },
4582 .calling_convention_inline => {4591 .calling_convention_inline => {
4583 return DocData.WalkResult{4592 return DocData.WalkResult{
4584 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_inline) },4593 .typeRef = .{ .type = @enumToInt(Ref.calling_convention_type) },
4585 // .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },4594 .expr = .{ .enumLiteral = "Inline" },
4586 .expr = .{ .int = .{ .value = 1 } },
4587 };4595 };
4588 },4596 },
4589 // .generic_poison => {4597 // .generic_poison => {