authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-06-12 19:01:08+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:12-07:00
log8ec34eb0463d489f109a228d4528ebfbec2dc733
tree3fb0f5d658277489d9e2f6f79a66ee4271030529
parent947eff6e395c7f4ffd6ef6cc10f1312f73f78d3b

autodoc: handle result location instructions


2 files changed, 66 insertions(+), 15 deletions(-)

lib/docs/main.js+18-3
......@@ -529,6 +529,16 @@ var zigAnalysis;
529529 if (typeIsGenericFn(value.expr.type)) {
530530 // does the generic_ret contain a container?
531531 var resolvedGenericRet = resolveValue({expr: typeObj.generic_ret});
532
533 if ("call" in resolvedGenericRet.expr){
534 let call = zigAnalysis.calls[resolvedGenericRet.expr.call];
535 let resolvedFunc = resolveValue({expr: call.func});
536 if (!("type" in resolvedFunc.expr)) return;
537 let callee = zigAnalysis.types[resolvedFunc.expr.type];
538 if (!callee.generic_ret) return;
539 resolvedGenericRet = resolveValue({expr: callee.generic_ret});
540 }
541
532542 // TODO: see if unwrapping the `as` here is a good idea or not.
533543 if ("as" in resolvedGenericRet.expr) {
534544 resolvedGenericRet = {
......@@ -753,7 +763,6 @@ var zigAnalysis;
753763
754764
755765 function navLinkPkg(pkgIndex) {
756 console.log(canonPkgPaths);
757766 return navLink(canonPkgPaths[pkgIndex], []);
758767 }
759768
......@@ -825,7 +834,7 @@ var zigAnalysis;
825834 }
826835 case "enumLiteral": {
827836 let literal = expr.enumLiteral;
828 return literal;
837 return "." + literal;
829838 }
830839 case "void": {
831840 return "void";
......@@ -923,7 +932,13 @@ var zigAnalysis;
923932 const declRef = expr.refPath[0].declRef;
924933 let name = zigAnalysis.decls[declRef].name;
925934 for (let i = 1; i < expr.refPath.length; i++) {
926 name += "." + exprName(expr.refPath[i]);
935 let component = undefined;
936 if ("string" in expr.refPath[i]) {
937 component = expr.refPath[i].string;
938 } else {
939 component = exprName(expr.refPath[i]);
940 }
941 name += "." + component;
927942 }
928943 return name;
929944 }
src/Autodoc.zig+48-12
......@@ -738,18 +738,6 @@ fn walkInstruction(
738738 );
739739 return self.cteTodo(@tagName(tags[inst_index]));
740740 },
741 .ret_node => {
742 const un_node = data[inst_index].un_node;
743 return self.walkRef(file, parent_scope, un_node.operand, false);
744 },
745 .closure_get => {
746 const inst_node = data[inst_index].inst_node;
747 return try self.walkInstruction(file, parent_scope, inst_node.inst, need_type);
748 },
749 .closure_capture => {
750 const un_tok = data[inst_index].un_tok;
751 return try self.walkRef(file, parent_scope, un_tok.operand, need_type);
752 },
753741 .import => {
754742 const str_tok = data[inst_index].str_tok;
755743 var path = str_tok.get(file.zir);
......@@ -848,6 +836,54 @@ fn walkInstruction(
848836 need_type,
849837 );
850838 },
839 .ret_node => {
840 const un_node = data[inst_index].un_node;
841 return self.walkRef(file, parent_scope, un_node.operand, false);
842 },
843 .ret_load => {
844 const un_node = data[inst_index].un_node;
845 const res_ptr_ref = un_node.operand;
846 const res_ptr_inst = @enumToInt(res_ptr_ref) - Ref.typed_value_map.len;
847 // TODO: this instruction doesn't let us know trivially if there's
848 // branching involved or not. For now here's the strat:
849 // We search backwarts until `ret_ptr` for `store_node`,
850 // if we find only one, then that's our value, if we find more
851 // than one, then it means that there's branching involved.
852 // Maybe.
853
854 var i = inst_index - 1;
855 var result_ref: ?Ref = null;
856 while (i > res_ptr_inst) : (i -= 1) {
857 if (tags[i] == .store_node) {
858 const pl_node = data[i].pl_node;
859 const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
860 if (extra.data.lhs == res_ptr_ref) {
861 // this store_load instruction is indeed pointing at
862 // the result location that we care about!
863 if (result_ref != null) return DocData.WalkResult{
864 .expr = .{ .comptimeExpr = 0 },
865 };
866 result_ref = extra.data.rhs;
867 }
868 }
869 }
870
871 if (result_ref) |rr| {
872 return self.walkRef(file, parent_scope, rr, need_type);
873 }
874
875 return DocData.WalkResult{
876 .expr = .{ .comptimeExpr = 0 },
877 };
878 },
879 .closure_get => {
880 const inst_node = data[inst_index].inst_node;
881 return try self.walkInstruction(file, parent_scope, inst_node.inst, need_type);
882 },
883 .closure_capture => {
884 const un_tok = data[inst_index].un_tok;
885 return try self.walkRef(file, parent_scope, un_tok.operand, need_type);
886 },
851887 .cmpxchg_strong, .cmpxchg_weak => {
852888 const pl_node = data[inst_index].pl_node;
853889 const extra = file.zir.extraData(Zir.Inst.Cmpxchg, pl_node.payload_index);