authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-05-30 21:48:46+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:12-07:00
log413cfd4066c547ec1cf3c7b81b0ca34e9eddb82a
treef3b07b2f7b29f34731a32e72d4faf70454c434f7
parent51647c305eea1c38da47e3c20b39418ce3a7689f

autodoc: init generic function support


2 files changed, 73 insertions(+), 11 deletions(-)

lib/docs/main.js+27-8
......@@ -724,7 +724,7 @@ var zigAnalysis;
724724 if (typeObj.kind !== typeKinds.Fn) {
725725 return false;
726726 }
727 return /** @type {Fn} */(typeObj).generic;
727 return /** @type {Fn} */(typeObj).generic_ret != null;
728728 }
729729
730730 /** @param {Decl} fnDecl */
......@@ -772,7 +772,26 @@ var zigAnalysis;
772772
773773 let protoSrcIndex = fnDecl.src;
774774 if (typeIsGenericFn(value.expr.type)) {
775 throw "TODO";
775 // does the generic_ret contain a container?
776 var resolvedGenericRet = resolveValue({expr: typeObj.generic_ret});
777 // TODO: see if unwrapping the `as` here is a good idea or not.
778 if ("as" in resolvedGenericRet.expr) {
779 resolvedGenericRet = {
780 expr: zigAnalysis.exprs[resolvedGenericRet.expr.as.exprArg]
781 };
782 }
783
784 if (!("type" in resolvedGenericRet.expr)) return;
785 const genericType = zigAnalysis.types[resolvedGenericRet.expr.type];
786 if (isContainerType(genericType)) {
787 renderContainer(genericType)
788 }
789
790
791
792
793
794 // old code
776795 // let instantiations = nodesToFnsMap[protoSrcIndex];
777796 // let calls = nodesToCallsMap[protoSrcIndex];
778797 // if (instantiations == null && calls == null) {
......@@ -1530,7 +1549,7 @@ var zigAnalysis;
15301549 return html;
15311550 }
15321551 }
1533
1552
15341553 case typeKinds.ErrorUnion:
15351554 {
15361555 let errUnionObj = /** @type {ErrUnionType} */(typeObj);
......@@ -1614,7 +1633,7 @@ var zigAnalysis;
16141633
16151634 if (isVarArgs && i === fnObj.params.length - 1) {
16161635 payloadHtml += '...';
1617 }
1636 }
16181637 else if ("typeOf" in value) {
16191638 if (opts.wantHtml) {
16201639 payloadHtml += '<a href="">';
......@@ -1626,7 +1645,7 @@ var zigAnalysis;
16261645 payloadHtml += exprName(value, opts);
16271646 }
16281647
1629 }
1648 }
16301649 else if ("typeOf_peer" in value) {
16311650 if (opts.wantHtml) {
16321651 payloadHtml += '<a href="">';
......@@ -1638,7 +1657,7 @@ var zigAnalysis;
16381657 payloadHtml += exprName(value, opts);
16391658 }
16401659
1641 }
1660 }
16421661 else if ("declRef" in value) {
16431662 if (opts.wantHtml) {
16441663 payloadHtml += '<a href="">';
......@@ -1650,7 +1669,7 @@ var zigAnalysis;
16501669 payloadHtml += exprName(value, opts);
16511670 }
16521671
1653 }
1672 }
16541673 else if ("call" in value) {
16551674 if (opts.wantHtml) {
16561675 payloadHtml += '<a href="">';
......@@ -1661,7 +1680,7 @@ var zigAnalysis;
16611680 } else {
16621681 payloadHtml += exprName(value, opts);
16631682 }
1664 }
1683 }
16651684 else if ("refPath" in value) {
16661685 if (opts.wantHtml) {
16671686 payloadHtml += '<a href="">';
src/Autodoc.zig+46-3
......@@ -476,8 +476,9 @@ const DocData = struct {
476476 },
477477 Fn: struct {
478478 name: []const u8,
479 src: ?usize = null, // index into astNodes
479 src: ?usize = null, // index into `astNodes`
480480 ret: Expr,
481 generic_ret: ?Expr = null,
481482 params: ?[]Expr = null, // (use src->fields to find names)
482483 lib_name: []const u8 = "",
483484 is_var_args: bool = false,
......@@ -522,7 +523,6 @@ const DocData = struct {
522523 .ComptimeFloat => |v| try printTypeBody(v, options, w),
523524 .Null => |v| try printTypeBody(v, options, w),
524525 .Optional => |v| try printTypeBody(v, options, w),
525
526526 .Struct => |v| try printTypeBody(v, options, w),
527527 .Fn => |v| try printTypeBody(v, options, w),
528528 .Union => |v| try printTypeBody(v, options, w),
......@@ -886,6 +886,10 @@ fn walkInstruction(
886886 );
887887 return self.cteTodo(@tagName(tags[inst_index]));
888888 },
889 .ret_node => {
890 const un_node = data[inst_index].un_node;
891 return self.walkRef(file, parent_scope, un_node.operand, false);
892 },
889893 .closure_get => {
890894 const inst_node = data[inst_index].inst_node;
891895 return try self.walkInstruction(file, parent_scope, inst_node.inst, need_type);
......@@ -3882,7 +3886,20 @@ fn analyzeFunctionExtended(
38823886 }
38833887
38843888 self.types.items[type_slot_index] = .{
3885 .Fn = .{ .name = "todo_name func", .src = self_ast_node_index, .params = param_type_refs.items, .ret = ret_type_ref.expr, .is_extern = extra.data.bits.is_extern, .has_cc = extra.data.bits.has_cc, .has_align = extra.data.bits.has_align, .has_lib_name = extra.data.bits.has_lib_name, .lib_name = lib_name, .is_inferred_error = extra.data.bits.is_inferred_error, .cc = cc_index, .@"align" = align_index },
3889 .Fn = .{
3890 .name = "todo_name func",
3891 .src = self_ast_node_index,
3892 .params = param_type_refs.items,
3893 .ret = ret_type_ref.expr,
3894 .is_extern = extra.data.bits.is_extern,
3895 .has_cc = extra.data.bits.has_cc,
3896 .has_align = extra.data.bits.has_align,
3897 .has_lib_name = extra.data.bits.has_lib_name,
3898 .lib_name = lib_name,
3899 .is_inferred_error = extra.data.bits.is_inferred_error,
3900 .cc = cc_index,
3901 .@"align" = align_index,
3902 },
38863903 };
38873904
38883905 return DocData.WalkResult{
......@@ -3974,6 +3991,21 @@ fn analyzeFunction(
39743991 break :blk wr;
39753992 };
39763993
3994 // TODO: a complete version of this will probably need a scope
3995 // in order to evaluate correctly closures around funcion
3996 // parameters etc.
3997 const generic_ret: ?DocData.Expr = switch (ret_type_ref.expr) {
3998 .type => |t| if (t == @enumToInt(Ref.type_type))
3999 try self.getGenericReturnType(
4000 file,
4001 scope,
4002 fn_info.body[fn_info.body.len - 1],
4003 )
4004 else
4005 null,
4006 else => null,
4007 };
4008
39774009 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;
39784010 self.types.items[type_slot_index] = .{
39794011 .Fn = .{
......@@ -3981,6 +4013,7 @@ fn analyzeFunction(
39814013 .src = self_ast_node_index,
39824014 .params = param_type_refs.items,
39834015 .ret = ret_type_ref.expr,
4016 .generic_ret = generic_ret,
39844017 },
39854018 };
39864019
......@@ -3990,6 +4023,16 @@ fn analyzeFunction(
39904023 };
39914024}
39924025
4026fn getGenericReturnType(
4027 self: *Autodoc,
4028 file: *File,
4029 scope: *Scope,
4030 body_end: usize,
4031) !DocData.Expr {
4032 const wr = try self.walkInstruction(file, scope, body_end, false);
4033 return wr.expr;
4034}
4035
39934036fn collectUnionFieldInfo(
39944037 self: *Autodoc,
39954038 file: *File,