| ... | @@ -12691,7 +12691,103 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I | ... | @@ -12691,7 +12691,103 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12691 | try new_decl.finalizeNewArena(&new_decl_arena); | 12691 | try new_decl.finalizeNewArena(&new_decl_arena); |
| 12692 | return sema.analyzeDeclVal(block, src, new_decl); | 12692 | return sema.analyzeDeclVal(block, src, new_decl); |
| 12693 | }, | 12693 | }, |
| 12694 | .Union => return sema.fail(block, src, "TODO: Sema.zirReify for Union", .{}), | 12694 | .Union => { |
| | 12695 | // TODO use reflection instead of magic numbers here |
| | 12696 | const struct_val = union_val.val.castTag(.aggregate).?.data; |
| | 12697 | // layout: containerlayout, |
| | 12698 | const layout_val = struct_val[0]; |
| | 12699 | // tag_type: ?type, |
| | 12700 | const tag_type_val = struct_val[1]; |
| | 12701 | // fields: []const enumfield, |
| | 12702 | const fields_val = struct_val[2]; |
| | 12703 | // decls: []const declaration, |
| | 12704 | const decls_val = struct_val[3]; |
| | 12705 | |
| | 12706 | // Decls |
| | 12707 | if (decls_val.sliceLen() > 0) { |
| | 12708 | return sema.fail(block, src, "reified unions must have no decls", .{}); |
| | 12709 | } |
| | 12710 | |
| | 12711 | var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa); |
| | 12712 | errdefer new_decl_arena.deinit(); |
| | 12713 | const new_decl_arena_allocator = new_decl_arena.allocator(); |
| | 12714 | |
| | 12715 | const union_obj = try new_decl_arena_allocator.create(Module.Union); |
| | 12716 | const type_tag: Type.Tag = if (!tag_type_val.isNull()) .union_tagged else .@"union"; |
| | 12717 | const union_payload = try new_decl_arena_allocator.create(Type.Payload.Union); |
| | 12718 | union_payload.* = .{ |
| | 12719 | .base = .{ .tag = type_tag }, |
| | 12720 | .data = union_obj, |
| | 12721 | }; |
| | 12722 | const union_ty = Type.initPayload(&union_payload.base); |
| | 12723 | const new_union_val = try Value.Tag.ty.create(new_decl_arena_allocator, union_ty); |
| | 12724 | const type_name = try sema.createTypeName(block, .anon); |
| | 12725 | const new_decl = try sema.mod.createAnonymousDeclNamed(block, .{ |
| | 12726 | .ty = Type.type, |
| | 12727 | .val = new_union_val, |
| | 12728 | }, type_name); |
| | 12729 | new_decl.owns_tv = true; |
| | 12730 | errdefer sema.mod.abortAnonDecl(new_decl); |
| | 12731 | union_obj.* = .{ |
| | 12732 | .owner_decl = new_decl, |
| | 12733 | .tag_ty = Type.initTag(.@"null"), |
| | 12734 | .fields = .{}, |
| | 12735 | .node_offset = src.node_offset, |
| | 12736 | .zir_index = inst, |
| | 12737 | .layout = layout_val.toEnum(std.builtin.Type.ContainerLayout), |
| | 12738 | .status = .have_field_types, |
| | 12739 | .namespace = .{ |
| | 12740 | .parent = block.namespace, |
| | 12741 | .ty = union_ty, |
| | 12742 | .file_scope = block.getFileScope(), |
| | 12743 | }, |
| | 12744 | }; |
| | 12745 | |
| | 12746 | // Tag type |
| | 12747 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen()); |
| | 12748 | union_obj.tag_ty = if (tag_type_val.optionalValue()) |payload_val| blk: { |
| | 12749 | var buffer: Value.ToTypeBuffer = undefined; |
| | 12750 | break :blk try payload_val.toType(&buffer).copy(new_decl_arena_allocator); |
| | 12751 | } else try sema.generateUnionTagTypeSimple(block, fields_len); |
| | 12752 | |
| | 12753 | // Fields |
| | 12754 | if (fields_len > 0) { |
| | 12755 | try union_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len); |
| | 12756 | |
| | 12757 | var i: usize = 0; |
| | 12758 | while (i < fields_len) : (i += 1) { |
| | 12759 | const elem_val = try fields_val.elemValue(sema.arena, i); |
| | 12760 | const field_struct_val = elem_val.castTag(.aggregate).?.data; |
| | 12761 | // TODO use reflection instead of magic numbers here |
| | 12762 | // name: []const u8 |
| | 12763 | const name_val = field_struct_val[0]; |
| | 12764 | // field_type: type, |
| | 12765 | const field_type_val = field_struct_val[1]; |
| | 12766 | // alignment: comptime_int, |
| | 12767 | const alignment_val = field_struct_val[2]; |
| | 12768 | |
| | 12769 | const field_name = try name_val.toAllocatedBytes( |
| | 12770 | Type.initTag(.const_slice_u8), |
| | 12771 | new_decl_arena_allocator, |
| | 12772 | ); |
| | 12773 | |
| | 12774 | const gop = union_obj.fields.getOrPutAssumeCapacity(field_name); |
| | 12775 | if (gop.found_existing) { |
| | 12776 | // TODO: better source location |
| | 12777 | return sema.fail(block, src, "duplicate union field {s}", .{field_name}); |
| | 12778 | } |
| | 12779 | |
| | 12780 | var buffer: Value.ToTypeBuffer = undefined; |
| | 12781 | gop.value_ptr.* = .{ |
| | 12782 | .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator), |
| | 12783 | .abi_align = try alignment_val.copy(new_decl_arena_allocator), |
| | 12784 | }; |
| | 12785 | } |
| | 12786 | } |
| | 12787 | |
| | 12788 | try new_decl.finalizeNewArena(&new_decl_arena); |
| | 12789 | return sema.analyzeDeclVal(block, src, new_decl); |
| | 12790 | }, |
| 12695 | .Fn => return sema.fail(block, src, "TODO: Sema.zirReify for Fn", .{}), | 12791 | .Fn => return sema.fail(block, src, "TODO: Sema.zirReify for Fn", .{}), |
| 12696 | .BoundFn => @panic("TODO delete BoundFn from the language"), | 12792 | .BoundFn => @panic("TODO delete BoundFn from the language"), |
| 12697 | .Frame => @panic("TODO implement https://github.com/ziglang/zig/issues/10710"), | 12793 | .Frame => @panic("TODO implement https://github.com/ziglang/zig/issues/10710"), |
| ... | @@ -20417,7 +20513,7 @@ fn generateUnionTagTypeNumbered( | ... | @@ -20417,7 +20513,7 @@ fn generateUnionTagTypeNumbered( |
| 20417 | return enum_ty; | 20513 | return enum_ty; |
| 20418 | } | 20514 | } |
| 20419 | | 20515 | |
| 20420 | fn generateUnionTagTypeSimple(sema: *Sema, block: *Block, fields_len: u32) !Type { | 20516 | fn generateUnionTagTypeSimple(sema: *Sema, block: *Block, fields_len: usize) !Type { |
| 20421 | const mod = sema.mod; | 20517 | const mod = sema.mod; |
| 20422 | | 20518 | |
| 20423 | var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa); | 20519 | var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa); |