aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-10-03 11:20:08 -0700
committerGitHub <noreply@github.com>2023-10-03 11:20:08 -0700
commit87d09edf2d328b21e02abb233899ebaa257a5204 (patch)
treec43c0688ff29c2f488d78dc39cf304d2c2c50a2e /src/type.zig
parentc933a7c58c6685f65da26e7c4cc826a32c3d1e43 (diff)
parent1b8a50ea5e9a179bf85486230d6b058d1379a4aa (diff)
downloadzig-87d09edf2d328b21e02abb233899ebaa257a5204.tar.gz
zig-87d09edf2d328b21e02abb233899ebaa257a5204.zip
Merge pull request #17352 from kcbanner/extern_union_comptime_memory
sema: Support reinterpreting extern/packed unions at comptime via field access
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/type.zig b/src/type.zig
index 78514fe3d5..87b29731f6 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -1954,6 +1954,16 @@ pub const Type = struct {
return true;
}
+ /// Returns the type used for backing storage of this union during comptime operations.
+ /// Asserts the type is either an extern or packed union.
+ pub fn unionBackingType(ty: Type, mod: *Module) !Type {
+ return switch (ty.containerLayout(mod)) {
+ .Extern => try mod.arrayType(.{ .len = ty.abiSize(mod), .child = .u8_type }),
+ .Packed => try mod.intType(.unsigned, @intCast(ty.bitSize(mod))),
+ .Auto => unreachable,
+ };
+ }
+
pub fn unionGetLayout(ty: Type, mod: *Module) Module.UnionLayout {
const ip = &mod.intern_pool;
const union_type = ip.indexToKey(ty.toIntern()).union_type;