Skip to content

Commit

Permalink
compiler: move unions into InternPool
Browse files Browse the repository at this point in the history
There are a couple concepts here worth understanding:

Key.UnionType - This type is available *before* resolving the union's
fields. The enum tag type, number of fields, and field names, field
types, and field alignments are not available with this.

InternPool.UnionType - This one can be obtained from the above type with
`InternPool.loadUnionType` which asserts that the union's enum tag type
has been resolved. This one has all the information available.

Additionally:

* ZIR: Turn an unused bit into `any_aligned_fields` flag to help
  semantic analysis know whether a union has explicit alignment on any
  fields (usually not).
* Sema: delete `resolveTypeRequiresComptime` which had the same type
  signature and near-duplicate logic to `typeRequiresComptime`.
  - Make opaque types not report comptime-only (this was inconsistent
    between the two implementations of this function).
* Implement accepted proposal ziglang#12556 which is a breaking change.
  • Loading branch information
andrewrk committed Aug 22, 2023
1 parent 6a54639 commit ada0010
Show file tree
Hide file tree
Showing 27 changed files with 1,396 additions and 1,358 deletions.
14 changes: 7 additions & 7 deletions lib/std/dwarf/call_frame.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,9 @@ pub const Instruction = union(Opcode) {
register: u8,
offset: u64,
},
offset_extended: struct {
register: u8,
offset: u64,
},
restore: struct {
register: u8,
},
restore_extended: struct {
register: u8,
},
nop: void,
set_loc: struct {
address: u64,
Expand All @@ -92,6 +85,13 @@ pub const Instruction = union(Opcode) {
advance_loc4: struct {
delta: u32,
},
offset_extended: struct {
register: u8,
offset: u64,
},
restore_extended: struct {
register: u8,
},
undefined: struct {
register: u8,
},
Expand Down
4 changes: 2 additions & 2 deletions lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,9 @@ test "std.meta.FieldEnum" {
const Tagged = union(enum) { a: u8, b: void, c: f32 };
try testing.expectEqual(Tag(Tagged), FieldEnum(Tagged));

const Tag2 = enum { b, c, a };
const Tag2 = enum { a, b, c };
const Tagged2 = union(Tag2) { a: u8, b: void, c: f32 };
try testing.expect(Tag(Tagged2) != FieldEnum(Tagged2));
try testing.expect(Tag(Tagged2) == FieldEnum(Tagged2));

const Tag3 = enum(u8) { a, b, c = 7 };
const Tagged3 = union(Tag3) { a: u8, b: void, c: f32 };
Expand Down
5 changes: 5 additions & 0 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4696,6 +4696,7 @@ fn unionDeclInner(

const bits_per_field = 4;
const max_field_size = 5;
var any_aligned_fields = false;
var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, field_count, bits_per_field, max_field_size);
defer wip_members.deinit();

Expand Down Expand Up @@ -4733,6 +4734,7 @@ fn unionDeclInner(
if (have_align) {
const align_inst = try expr(&block_scope, &block_scope.base, .{ .rl = .{ .ty = .u32_type } }, member.ast.align_expr);
wip_members.appendToField(@intFromEnum(align_inst));
any_aligned_fields = true;
}
if (have_value) {
if (arg_inst == .none) {
Expand Down Expand Up @@ -4783,6 +4785,7 @@ fn unionDeclInner(
.fields_len = field_count,
.decls_len = decl_count,
.auto_enum_tag = auto_enum_tok != null,
.any_aligned_fields = any_aligned_fields,
});

wip_members.finishBits(bits_per_field);
Expand Down Expand Up @@ -11754,6 +11757,7 @@ const GenZir = struct {
decls_len: u32,
layout: std.builtin.Type.ContainerLayout,
auto_enum_tag: bool,
any_aligned_fields: bool,
}) !void {
const astgen = gz.astgen;
const gpa = astgen.gpa;
Expand Down Expand Up @@ -11790,6 +11794,7 @@ const GenZir = struct {
.name_strategy = gz.anon_name_strategy,
.layout = args.layout,
.auto_enum_tag = args.auto_enum_tag,
.any_aligned_fields = args.any_aligned_fields,
}),
.operand = payload_index,
} },
Expand Down
Loading

0 comments on commit ada0010

Please sign in to comment.