authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-03-04 18:20:41+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
log8bb529b39598f53939f7429257b862b513fb3091
tree35cd338b521cf96e1d63ad73e930c79213e88ebe
parent195231b212492cda5e75bcdb347765e7ca035f1c

autodoc: add support for generic functions & generic function calls


2 files changed, 48 insertions(+), 32 deletions(-)

lib/docs/main.js+5-4
......@@ -1218,10 +1218,11 @@
12181218 var kind = value.kind;
12191219 if (kind === typeKinds.Fn) {
12201220 //if (allCompTimeFnCallsHaveTypeResult(decl.type, declTypeId)) {
1221 // typesList.push(decl);
1222 //} else {
1223 fnsList.push(decl);
1224 // }
1221 if (resolveTypeRefToTypeId(value.ret) == typeTypeId) {
1222 typesList.push(decl);
1223 } else {
1224 fnsList.push(decl);
1225 }
12251226
12261227 } else if (typeIsErrSet(declValue.type)) {
12271228 errSetsList.push(decl);
src/Autodoc.zig+43-28
......@@ -328,6 +328,7 @@ const DocData = struct {
328328 name: ?[]const u8 = null,
329329 docs: ?[]const u8 = null,
330330 fields: ?[]usize = null, // index into astNodes
331 @"comptime": bool = false,
331332 };
332333
333334 const Type = union(DocTypeKinds) {
......@@ -709,7 +710,7 @@ fn walkInstruction(
709710 path[0] = decls_slot_index;
710711 return DocData.WalkResult{ .declPath = path };
711712 },
712 .field_val => {
713 .field_val, .field_call_bind, .field_ptr => {
713714 const pl_node = data[inst_index].pl_node;
714715 const extra = file.zir.extraData(Zir.Inst.Field, pl_node.payload_index);
715716
......@@ -719,7 +720,10 @@ fn walkInstruction(
719720 try path.append(self.arena, extra.data.field_name_start);
720721 // Put inside path the starting index of each decl name
721722 // that we encounter as we navigate through all the field_vals
722 while (tags[lhs] == .field_val) {
723 while (tags[lhs] == .field_val or
724 tags[lhs] == .field_call_bind or
725 tags[lhs] == .field_ptr)
726 {
723727 const lhs_extra = file.zir.extraData(
724728 Zir.Inst.Field,
725729 data[lhs].pl_node.payload_index,
......@@ -729,8 +733,11 @@ fn walkInstruction(
729733 lhs = @enumToInt(lhs_extra.data.lhs) - Ref.typed_value_map.len; // underflow = need to handle Refs
730734 }
731735
732 if (tags[lhs] != .decl_val) {
733 @panic("TODO: handle non-decl_val endings in walkInstruction.field_val");
736 if (tags[lhs] != .decl_val and tags[lhs] != .decl_ref) {
737 std.debug.panic(
738 "TODO: handle `{s}` endings in walkInstruction.field_val",
739 .{@tagName(tags[lhs])},
740 );
734741 }
735742 const str_tok = data[lhs].str_tok;
736743 const decls_slot_index = parent_scope.resolveDeclName(str_tok.start);
......@@ -770,7 +777,6 @@ fn walkInstruction(
770777 const pl_node = data[inst_index].pl_node;
771778 const extra = file.zir.extraData(Zir.Inst.Call, pl_node.payload_index);
772779
773 // TODO: handle the way scoping works with fn args
774780 const callee = DocData.TypeRef.fromWalkResult(
775781 try self.walkRef(file, parent_scope, extra.data.callee),
776782 );
......@@ -794,9 +800,7 @@ fn walkInstruction(
794800 .func => {
795801 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));
796802
797 // TODO: change this to a resize and change the appends accordingly
798803 try self.ast_nodes.ensureUnusedCapacity(self.arena, fn_info.total_params_len);
799 try self.types.ensureUnusedCapacity(self.arena, fn_info.total_params_len);
800804 var param_type_refs = try std.ArrayListUnmanaged(DocData.TypeRef).initCapacity(
801805 self.arena,
802806 fn_info.total_params_len,
......@@ -805,28 +809,39 @@ fn walkInstruction(
805809 self.arena,
806810 fn_info.total_params_len,
807811 );
812 // TODO: handle scope rules for fn parameters
808813 for (fn_info.param_body[0..fn_info.total_params_len]) |param_index| {
809 if (tags[param_index] != .param) unreachable; // TODO: handle more param types
810 const pl_tok = data[param_index].pl_tok;
811 const extra = file.zir.extraData(Zir.Inst.Param, pl_tok.payload_index);
812 const doc_comment = if (extra.data.doc_comment != 0)
813 file.zir.nullTerminatedString(extra.data.doc_comment)
814 else
815 "";
816
817 param_ast_indexes.appendAssumeCapacity(self.ast_nodes.items.len);
818 try self.ast_nodes.append(self.arena, .{
819 .name = file.zir.nullTerminatedString(extra.data.name),
820 .docs = doc_comment,
821 });
822
823 const break_index = file.zir.extra[extra.end..][extra.data.body_len - 1];
824 const break_operand = data[break_index].@"break".operand;
825 const param_type_ref = try self.walkRef(file, parent_scope, break_operand);
826
827 param_type_refs.appendAssumeCapacity(
828 DocData.TypeRef.fromWalkResult(param_type_ref),
829 );
814 switch (tags[param_index]) {
815 else => {
816 std.debug.panic(
817 "TODO: handle `{s}` in walkInstruction.func\n",
818 .{@tagName(tags[param_index])},
819 );
820 },
821 .param, .param_comptime => {
822 const pl_tok = data[param_index].pl_tok;
823 const extra = file.zir.extraData(Zir.Inst.Param, pl_tok.payload_index);
824 const doc_comment = if (extra.data.doc_comment != 0)
825 file.zir.nullTerminatedString(extra.data.doc_comment)
826 else
827 "";
828
829 param_ast_indexes.appendAssumeCapacity(self.ast_nodes.items.len);
830 self.ast_nodes.appendAssumeCapacity(.{
831 .name = file.zir.nullTerminatedString(extra.data.name),
832 .docs = doc_comment,
833 .@"comptime" = tags[param_index] == .param_comptime,
834 });
835
836 const break_index = file.zir.extra[extra.end..][extra.data.body_len - 1];
837 const break_operand = data[break_index].@"break".operand;
838 const param_type_ref = try self.walkRef(file, parent_scope, break_operand);
839
840 param_type_refs.appendAssumeCapacity(
841 DocData.TypeRef.fromWalkResult(param_type_ref),
842 );
843 },
844 }
830845 }
831846
832847 // ret