authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-09-04 22:51:03+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-04 22:51:03+02:00
log3deb33fffa7e4c83c2ecda8ede0b7cd685ee8456
tree5454c07c5c43b07f4c5494fd4b6608806306f191
parentdbd60e3d296f1c195c4b4d56ca55ecb30444d407
parentb8001335c48096113520ab07592f96086b799cdc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12738 from der-teufel-programming/autodoc-opaque-types

autodoc: Opaque now handled like other container types

2 files changed, 105 insertions(+), 19 deletions(-)

lib/docs/main.js+3-3
......@@ -243,7 +243,8 @@ var zigAnalysis;
243243 return (
244244 typeKind === typeKinds.Struct ||
245245 typeKind === typeKinds.Union ||
246 typeKind === typeKinds.Enum
246 typeKind === typeKinds.Enum ||
247 typeKind === typeKinds.Opaque
247248 );
248249 }
249250
......@@ -1624,8 +1625,7 @@ var zigAnalysis;
16241625 }
16251626 case typeKinds.Opaque: {
16261627 let opaqueObj = typeObj;
1627
1628 return opaqueObj.name;
1628 return opaqueObj;
16291629 }
16301630 case typeKinds.ComptimeExpr: {
16311631 return "anyopaque";
src/Autodoc.zig+102-16
......@@ -577,7 +577,13 @@ const DocData = struct {
577577 is_extern: bool = false,
578578 },
579579 BoundFn: struct { name: []const u8 },
580 Opaque: struct { name: []const u8 },
580 Opaque: struct {
581 name: []const u8,
582 src: usize, // index into astNodes
583 privDecls: []usize = &.{}, // index into decls
584 pubDecls: []usize = &.{}, // index into decls
585 ast: usize,
586 },
581587 Frame: struct { name: []const u8 },
582588 AnyFrame: struct { name: []const u8 },
583589 Vector: struct { name: []const u8 },
......@@ -2433,6 +2439,14 @@ fn walkInstruction(
24332439 return result;
24342440 },
24352441 .opaque_decl => {
2442 const type_slot_index = self.types.items.len;
2443 try self.types.append(self.arena, .{ .Unanalyzed = .{} });
2444
2445 var scope: Scope = .{
2446 .parent = parent_scope,
2447 .enclosing_type = type_slot_index,
2448 };
2449
24362450 const small = @bitCast(Zir.Inst.OpaqueDecl.Small, extended.small);
24372451 var extra_index: usize = extended.operand;
24382452
......@@ -2453,22 +2467,63 @@ fn walkInstruction(
24532467 extra_index += 1;
24542468 break :blk decls_len;
24552469 } else 0;
2456 _ = decls_len;
2457
2458 const decls_bits = file.zir.extra[extra_index];
2459 _ = decls_bits;
2460
2461 // const sep = "=" ** 200;
2462 // log.debug("{s}", .{sep});
2463 // log.debug("small = {any}", .{small});
2464 // log.debug("src_node = {}", .{src_node});
2465 // log.debug("decls_len = {}", .{decls_len});
2466 // log.debug("decls_bit = {}", .{decls_bits});
2467 // log.debug("{s}", .{sep});
2468 const type_slot_index = self.types.items.len - 1;
2469 try self.types.append(self.arena, .{ .Opaque = .{ .name = "TODO" } });
2470
2471 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};
2472 var priv_decl_indexes: std.ArrayListUnmanaged(usize) = .{};
2473
2474 const decls_first_index = self.decls.items.len;
2475 // Decl name lookahead for reserving slots in `scope` (and `decls`).
2476 // Done to make sure that all decl refs can be resolved correctly,
2477 // even if we haven't fully analyzed the decl yet.
2478 {
2479 var it = file.zir.declIterator(@intCast(u32, inst_index));
2480 try self.decls.resize(self.arena, decls_first_index + it.decls_len);
2481 for (self.decls.items[decls_first_index..]) |*slot| {
2482 slot._analyzed = false;
2483 }
2484 var decls_slot_index = decls_first_index;
2485 while (it.next()) |d| : (decls_slot_index += 1) {
2486 const decl_name_index = file.zir.extra[d.sub_index + 5];
2487 try scope.insertDeclRef(self.arena, decl_name_index, decls_slot_index);
2488 }
2489 }
2490
2491 extra_index = try self.walkDecls(
2492 file,
2493 &scope,
2494 src_info,
2495 decls_first_index,
2496 decls_len,
2497 &decl_indexes,
2498 &priv_decl_indexes,
2499 extra_index,
2500 );
2501
2502 self.types.items[type_slot_index] = .{
2503 .Opaque = .{
2504 .name = "todo_name",
2505 .src = self_ast_node_index,
2506 .privDecls = priv_decl_indexes.items,
2507 .pubDecls = decl_indexes.items,
2508 .ast = self_ast_node_index,
2509 },
2510 };
2511 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {
2512 for (paths.items) |resume_info| {
2513 try self.tryResolveRefPath(
2514 resume_info.file,
2515 inst_index,
2516 resume_info.ref_path,
2517 );
2518 }
2519
2520 _ = self.ref_paths_pending_on_types.remove(type_slot_index);
2521 // TODO: we should deallocate the arraylist that holds all the
2522 // decl paths. not doing it now since it's arena-allocated
2523 // anyway, but maybe we should put it elsewhere.
2524 }
24702525 return DocData.WalkResult{
2471 .typeRef = .{ .type = @enumToInt(Ref.anyopaque_type) },
2526 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
24722527 .expr = .{ .type = type_slot_index },
24732528 };
24742529 },
......@@ -3428,6 +3483,37 @@ fn tryResolveRefPath(
34283483 path[i + 1] = (try self.cteTodo(child_string)).expr;
34293484 continue :outer;
34303485 },
3486 .Opaque => |t_opaque| {
3487 for (t_opaque.pubDecls) |d| {
3488 // TODO: this could be improved a lot
3489 // by having our own string table!
3490 const decl = self.decls.items[d];
3491 if (std.mem.eql(u8, decl.name, child_string)) {
3492 path[i + 1] = .{ .declRef = d };
3493 continue :outer;
3494 }
3495 }
3496 for (t_opaque.privDecls) |d| {
3497 // TODO: this could be improved a lot
3498 // by having our own string table!
3499 const decl = self.decls.items[d];
3500 if (std.mem.eql(u8, decl.name, child_string)) {
3501 path[i + 1] = .{ .declRef = d };
3502 continue :outer;
3503 }
3504 }
3505
3506 // if we got here, our search failed
3507 printWithContext(
3508 file,
3509 inst_index,
3510 "failed to match `{s}` in opaque",
3511 .{child_string},
3512 );
3513
3514 path[i + 1] = (try self.cteTodo("match failure")).expr;
3515 continue :outer;
3516 },
34313517 },
34323518 }
34333519 }