diff options
| author | Veikka Tuominen <git@vexu.eu> | 2023-09-26 11:16:03 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-26 11:16:03 +0300 |
| commit | f4c884617f499b52eaecc0ef674609c774052f8f (patch) | |
| tree | c24e4628c314d5e815f4b9796d0ff70335fa58a3 /src/type.zig | |
| parent | 2adb932ad6ee4ff3d3c640cb8fb7bf7db0ff5d74 (diff) | |
| parent | 9f4649b197b720dbc168ced25eee0805d3b678b1 (diff) | |
| download | zig-f4c884617f499b52eaecc0ef674609c774052f8f.tar.gz zig-f4c884617f499b52eaecc0ef674609c774052f8f.zip | |
Merge pull request #17215 from kcbanner/read_from_memory_union
sema: add support for unions in readFromMemory and writeToMemory
Diffstat (limited to 'src/type.zig')
| -rw-r--r-- | src/type.zig | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/type.zig b/src/type.zig index 88a3a7cc43..6345f1ef6a 100644 --- a/src/type.zig +++ b/src/type.zig @@ -1647,8 +1647,12 @@ pub const Type = struct { }, .union_type => |union_type| { - if (opt_sema) |sema| try sema.resolveTypeFields(ty); - if (ty.containerLayout(mod) != .Packed) { + const is_packed = ty.containerLayout(mod) == .Packed; + if (opt_sema) |sema| { + try sema.resolveTypeFields(ty); + if (is_packed) try sema.resolveTypeLayout(ty); + } + if (!is_packed) { return (try ty.abiSizeAdvanced(mod, strat)).scalar * 8; } const union_obj = ip.loadUnionType(union_type); @@ -1659,6 +1663,7 @@ pub const Type = struct { const field_ty = union_obj.field_types.get(ip)[field_index]; size = @max(size, try bitSizeAdvanced(field_ty.toType(), mod, opt_sema)); } + return size; }, .opaque_type => unreachable, @@ -1927,11 +1932,12 @@ pub const Type = struct { return union_obj.enum_tag_ty.toType(); } - pub fn unionFieldType(ty: Type, enum_tag: Value, mod: *Module) Type { + pub fn unionFieldType(ty: Type, enum_tag: Value, mod: *Module) ?Type { const ip = &mod.intern_pool; const union_obj = mod.typeToUnion(ty).?; - const index = mod.unionTagFieldIndex(union_obj, enum_tag).?; - return union_obj.field_types.get(ip)[index].toType(); + const union_fields = union_obj.field_types.get(ip); + const index = mod.unionTagFieldIndex(union_obj, enum_tag) orelse return null; + return union_fields[index].toType(); } pub fn unionTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?u32 { |
