authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-02 18:50:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-02 18:50:01-07:00
log807a8b6f7549732d73239a649886cc64f0303f34
treed947dae63f799b66432744ae3b000f141d71e4cf
parent5f4c52209eab66666cc4a8fd24bf27c372cfd54f

stage2: make struct field analysis lazy

This commit breaks struct field analysis; will be fixed in a future commit.

3 files changed, 30 insertions(+), 14 deletions(-)

lib/std/start.zig+2-2
......@@ -29,10 +29,10 @@ comptime {
2929 if (builtin.zig_is_stage2) {
3030 if (builtin.output_mode == .Exe) {
3131 if (builtin.link_libc or builtin.object_format == .c) {
32 @export(main2, "main");
32 @export(main2, .{ .name = "main" });
3333 } else {
3434 if (!@hasDecl(root, "_start")) {
35 @export(_start2, "_start");
35 @export(_start2, .{ .name = "_start" });
3636 }
3737 }
3838 }
src/Module.zig+13-1
......@@ -497,12 +497,22 @@ pub const Struct = struct {
497497 /// Offset from `owner_decl`, points to the struct AST node.
498498 node_offset: i32,
499499
500 layout: std.builtin.TypeInfo.ContainerLayout,
501 status: enum {
502 none,
503 have_field_types,
504 have_layout,
505 },
506
500507 pub const Field = struct {
501508 /// Uses `noreturn` to indicate `anytype`.
509 /// undefined until `status` is `have_field_types` or `have_layout`.
502510 ty: Type,
503511 abi_align: Value,
504512 /// Uses `unreachable_value` to indicate no default.
505513 default_val: Value,
514 /// undefined until `status` is `have_layout`.
515 offset: u32,
506516 is_comptime: bool,
507517 };
508518
......@@ -2408,6 +2418,8 @@ pub fn semaFile(mod: *Module, file: *Scope.File) InnerError!void {
24082418 .owner_decl = undefined, // set below
24092419 .fields = .{},
24102420 .node_offset = 0, // it's the struct for the root file
2421 .layout = .Auto,
2422 .status = .none,
24112423 .namespace = .{
24122424 .parent = null,
24132425 .ty = struct_ty,
......@@ -2458,7 +2470,7 @@ pub fn semaFile(mod: *Module, file: *Scope.File) InnerError!void {
24582470
24592471 const main_struct_inst = file.zir.extra[@enumToInt(Zir.ExtraIndex.main_struct)] -
24602472 @intCast(u32, Zir.Inst.Ref.typed_value_map.len);
2461 try sema.analyzeStructDecl(&block_scope, &new_decl_arena, new_decl, main_struct_inst, .Auto, struct_obj);
2473 try sema.analyzeStructDecl(new_decl, main_struct_inst, struct_obj);
24622474 try new_decl.finalizeNewArena(&new_decl_arena);
24632475
24642476 file.status = .success_air;
src/Sema.zig+15-11
......@@ -664,12 +664,21 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) In
664664
665665pub fn analyzeStructDecl(
666666 sema: *Sema,
667 block: *Scope.Block,
668 new_decl_arena: *std.heap.ArenaAllocator,
669667 new_decl: *Decl,
670668 inst: Zir.Inst.Index,
671 layout: std.builtin.TypeInfo.ContainerLayout,
672669 struct_obj: *Module.Struct,
670) InnerError!void {
671 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
672 const extra = sema.code.extraData(Zir.Inst.StructDecl, inst_data.payload_index);
673 const decls_len = extra.data.decls_len;
674
675 _ = try sema.mod.scanNamespace(&struct_obj.namespace, extra.end, decls_len, new_decl);
676}
677
678pub fn analyzeStructFields(
679 sema: *Sema,
680 block: *Scope.Block,
681 new_decl_arena: *std.heap.ArenaAllocator,
673682) InnerError!void {
674683 const tracy = trace(@src());
675684 defer tracy.end();
......@@ -682,13 +691,6 @@ pub fn analyzeStructDecl(
682691 const fields_len = extra.data.fields_len;
683692 const decls_len = extra.data.decls_len;
684693
685 var extra_index: usize = try mod.scanNamespace(
686 &struct_obj.namespace,
687 extra.end,
688 decls_len,
689 new_decl,
690 );
691
692694 const body = sema.code.extra[extra_index..][0..extra.data.body_len];
693695 if (fields_len == 0) {
694696 assert(body.len == 0);
......@@ -824,13 +826,15 @@ fn zirStructDecl(
824826 .owner_decl = sema.owner_decl,
825827 .fields = .{},
826828 .node_offset = inst_data.src_node,
829 .layout = layout,
830 .status = .none,
827831 .namespace = .{
828832 .parent = sema.owner_decl.namespace,
829833 .ty = struct_ty,
830834 .file_scope = block.getFileScope(),
831835 },
832836 };
833 try sema.analyzeStructDecl(block, &new_decl_arena, new_decl, inst, layout, struct_obj);
837 try sema.analyzeStructDecl(new_decl, inst, struct_obj);
834838 try new_decl.finalizeNewArena(&new_decl_arena);
835839 return sema.analyzeDeclVal(block, src, new_decl);
836840}