authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-01-31 18:41:10+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:10-07:00
log29771440b23f5a05ee522d444704131f1f6ab173
tree7af030c4b802ae1c417b97e416d0415753ec702b
parent0135d2271600db92d1ae727b2a5935d0fc711c13

autodocs: add support for int values


2 files changed, 93 insertions(+), 57 deletions(-)

lib/docs/main.js+27-15
......@@ -169,23 +169,23 @@
169169
170170 renderNav();
171171
172 var lastDeclType = curNav.declObjs[curNav.declObjs.length - 1];
173 if (lastDeclType.pubDecls != null) {
174 renderContainer(lastDeclType);
175 }
176 if (lastDeclType.kind == null) {
177 return renderUnknownDecl(lastDeclType);
178 } else if (lastDeclType.kind === 'var') {
179 return renderVar(lastDeclType);
180 } else if (lastDeclType.kind === 'const' && lastDeclType.type != null) {
181 var typeObj = zigAnalysis.types[lastDeclType.type];
172 var lastDeclOrType = curNav.declObjs[curNav.declObjs.length - 1];
173 if (lastDeclOrType.pubDecls != null) {
174 renderContainer(lastDeclOrType);
175 }
176 if (lastDeclOrType.kind == null) {
177 return renderUnknownDecl(lastDeclOrType);
178 } else if (lastDeclOrType.kind === 'var') {
179 return renderVar(lastDeclOrType);
180 } else if (lastDeclOrType.kind === 'const' && lastDeclOrType.type != null) {
181 var typeObj = zigAnalysis.types[lastDeclOrType.type];
182182 if (typeObj.kind === typeKinds.Fn) {
183 return renderFn(lastDeclType);
183 return renderFn(lastDeclOrType);
184184 } else {
185 return renderValue(lastDeclType);
185 return renderValue(lastDeclOrType);
186186 }
187187 } else {
188 renderType(lastDeclType);
188 renderType(lastDeclOrType);
189189 }
190190 }
191191
......@@ -959,8 +959,20 @@
959959 }
960960
961961 function renderValue(decl) {
962
963 var declValueText = "";
964 switch(Object.keys(decl.value)[0]) {
965 case "int":
966 declValueText += decl.value.int.value;
967 break;
968 default:
969 console.log("TODO: renderValue for ", Object.keys(decl.value)[0]);
970 declValueText += "#TODO#";
971 }
972
962973 domFnProtoCode.innerHTML = '<span class="tok-kw">const</span> ' +
963 escapeHtml(decl.name) + ': ' + typeIndexName(decl.type, true, true);
974 escapeHtml(decl.name) + ': ' + typeIndexName(decl.type, true, true) +
975 " = " + declValueText;
964976
965977 var docs = zigAnalysis.astNodes[decl.src].docs;
966978 if (docs != null) {
......@@ -1120,7 +1132,7 @@
11201132 if (valType.kind === typeKinds.Struct) {
11211133 valTypeName = "struct";
11221134 }
1123
1135
11241136 html += '<a href="'+navLinkDecl(decl.name)+'">';
11251137 html += '<span class="tok-kw" style="color:lightblue;">' + decl.name + '</span>';
11261138 html += '</a>';
src/Autodoc.zig+66-42
......@@ -3,6 +3,7 @@ const Autodoc = @This();
33const Compilation = @import("Compilation.zig");
44const Module = @import("Module.zig");
55const Zir = @import("Zir.zig");
6const Ref = Zir.Inst.Ref;
67
78module: *Module,
89doc_location: ?Compilation.EmitLoc,
......@@ -53,12 +54,12 @@ pub fn generateZirData(self: *Autodoc) !void {
5354
5455 // TODO: we don't want to add .none, but the index math has to check out
5556 var i: u32 = 0;
56 while (i <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type)) : (i += 1) {
57 while (i <= @enumToInt(Ref.anyerror_void_error_union_type)) : (i += 1) {
5758 var tmpbuf = std.ArrayList(u8).init(self.arena);
58 try Zir.Inst.Ref.typed_value_map[i].val.format("", .{}, tmpbuf.writer());
59 try Ref.typed_value_map[i].val.format("", .{}, tmpbuf.writer());
5960 try self.types.append(self.arena, .{
6061 .name = tmpbuf.toOwnedSlice(),
61 .kind = switch (@intToEnum(Zir.Inst.Ref, i)) {
62 .kind = switch (@intToEnum(Ref, i)) {
6263 else => |t| blk: {
6364 std.debug.print("TODO: categorize `{s}` in typeKinds\n", .{
6465 @tagName(t),
......@@ -87,6 +88,7 @@ pub fn generateZirData(self: *Autodoc) !void {
8788 .c_longlong_type,
8889 .c_ulonglong_type,
8990 .c_longdouble_type,
91 .comptime_int_type,
9092 => @enumToInt(std.builtin.TypeId.Int),
9193 .f16_type,
9294 .f32_type,
......@@ -234,7 +236,11 @@ const DocData = struct {
234236 failure: bool,
235237 type: usize, // index in `types`
236238 decl_ref: usize, // index in `decls`
237
239 int: struct {
240 type: usize, // index in `types`
241 value: usize, // direct value
242 negated: bool = false,
243 },
238244 pub fn jsonStringify(
239245 self: WalkResult,
240246 _: std.json.StringifyOptions,
......@@ -251,6 +257,12 @@ const DocData = struct {
251257 \\{{ "{s}":{} }}
252258 , .{ @tagName(self), v });
253259 },
260 .int => |v| {
261 const neg = if (v.negated) "-" else "";
262 try w.print(
263 \\{{ "int": {{ "type": {}, "value": {s}{} }} }}
264 , .{ v.type, neg, v.value });
265 },
254266
255267 // .decl_ref => |v| {
256268 // try w.print(
......@@ -282,6 +294,31 @@ fn walkInstruction(
282294 );
283295 return DocData.WalkResult{ .failure = true };
284296 },
297 .int => {
298 const int = data[inst_index].int;
299 return DocData.WalkResult{
300 .int = .{
301 .type = @enumToInt(Ref.comptime_int_type),
302 .value = int,
303 },
304 };
305 },
306 .negate => {
307 const un_node = data[inst_index].un_node;
308 var operand = try self.walkRef(zir, parent_scope, un_node.operand);
309 operand.int.negated = true; // only support ints for now
310 return operand;
311 },
312 .as_node => {
313 const pl_node = data[inst_index].pl_node;
314 const extra = zir.extraData(Zir.Inst.As, pl_node.payload_index);
315 const dest_type_walk = try self.walkRef(zir, parent_scope, extra.data.dest_type);
316 const dest_type = dest_type_walk.type; // asserts that we got a type
317
318 var operand = try self.walkRef(zir, parent_scope, extra.data.operand);
319 operand.int.type = dest_type; // only support ints for now
320 return operand;
321 },
285322 .decl_val => {
286323 const str_tok = data[inst_index].str_tok;
287324 const decls_slot_index = parent_scope.resolveDeclName(str_tok.start);
......@@ -313,7 +350,7 @@ fn walkInstruction(
313350 const break_operand = data[break_index].@"break".operand;
314351 return if (Zir.refToIndex(break_operand)) |bi|
315352 self.walkInstruction(zir, parent_scope, bi)
316 else if (@enumToInt(break_operand) <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type))
353 else if (@enumToInt(break_operand) <= @enumToInt(Ref.anyerror_void_error_union_type))
317354 // we append all the types in ref first, so we can just do this if we encounter a ref that is a type
318355 return DocData.WalkResult{ .type = @enumToInt(break_operand) }
319356 else
......@@ -525,10 +562,15 @@ fn walkDecls(
525562 try priv_decl_indexes.append(self.arena, decls_slot_index);
526563 }
527564
565 const decl_type = switch (walk_result) {
566 .int => |i| i.type,
567 else => @enumToInt(Ref.type_type),
568 };
569
528570 self.decls.items[decls_slot_index] = .{
529571 .name = name,
530572 .src = ast_node_index,
531 .type = @enumToInt(Zir.Inst.Ref.type_type),
573 .type = decl_type,
532574 .value = walk_result,
533575 .kind = "const", // find where this information can be found
534576 };
......@@ -581,42 +623,8 @@ fn collectFieldInfo(
581623
582624 // type
583625 {
584 switch (field_type) {
585 .void_type => {
586 try field_type_indexes.append(self.arena, .{
587 .type = self.types.items.len,
588 });
589 try self.types.append(self.arena, .{
590 .kind = @enumToInt(std.builtin.TypeId.Void),
591 .name = "void",
592 });
593 },
594 .usize_type => {
595 try field_type_indexes.append(self.arena, .{ .type = self.types.items.len });
596 try self.types.append(self.arena, .{
597 .kind = @enumToInt(std.builtin.TypeId.Int),
598 .name = "usize",
599 });
600 },
601
602 else => {
603 const enum_value = @enumToInt(field_type);
604 if (enum_value < Zir.Inst.Ref.typed_value_map.len) {
605 try field_type_indexes.append(
606 self.arena,
607 DocData.WalkResult{ .type = enum_value },
608 );
609 } else {
610 const zir_index = enum_value - Zir.Inst.Ref.typed_value_map.len;
611 const walk_result = try self.walkInstruction(
612 zir,
613 scope,
614 zir_index,
615 );
616 try field_type_indexes.append(self.arena, walk_result);
617 }
618 },
619 }
626 const walk_result = try self.walkRef(zir, scope, field_type);
627 try field_type_indexes.append(self.arena, walk_result);
620628 }
621629
622630 // ast node
......@@ -633,3 +641,19 @@ fn collectFieldInfo(
633641 }
634642 }
635643}
644
645fn walkRef(
646 self: *Autodoc,
647 zir: Zir,
648 parent_scope: *Scope,
649 ref: Ref,
650) !DocData.WalkResult {
651 const enum_value = @enumToInt(ref);
652 if (enum_value < Zir.Inst.Ref.typed_value_map.len) {
653 // TODO: well... Refs are not all types
654 return DocData.WalkResult{ .type = enum_value };
655 } else {
656 const zir_index = enum_value - Ref.typed_value_map.len;
657 return self.walkInstruction(zir, parent_scope, zir_index);
658 }
659}