aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-03-08 20:52:38 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-03-08 20:52:38 -0700
commit6f560c99094eec2210f2d76a2449b5f60191e11b (patch)
tree7576694e5db2ef0ae63190fbb9fe20710920703f /src
parenta91753219df5d0b51f55dcad3e2ef96044efdd62 (diff)
downloadzig-6f560c99094eec2210f2d76a2449b5f60191e11b.tar.gz
zig-6f560c99094eec2210f2d76a2449b5f60191e11b.zip
Sema: implement comptime struct fields
Diffstat (limited to 'src')
-rw-r--r--src/Module.zig1
-rw-r--r--src/Sema.zig17
2 files changed, 18 insertions, 0 deletions
diff --git a/src/Module.zig b/src/Module.zig
index 68e3a56752..b7299ebdab 100644
--- a/src/Module.zig
+++ b/src/Module.zig
@@ -884,6 +884,7 @@ pub const Struct = struct {
default_val: Value,
/// undefined until `status` is `have_layout`.
offset: u32,
+ /// If true then `default_val` is the comptime field value.
is_comptime: bool,
/// Returns the field alignment, assuming the struct is not packed.
diff --git a/src/Sema.zig b/src/Sema.zig
index 5bddead0ba..2602cdc21e 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -15282,6 +15282,19 @@ fn structFieldPtrByIndex(
const target = sema.mod.getTarget();
const ptr_field_ty = try Type.ptr(sema.arena, target, ptr_ty_data);
+ if (field.is_comptime) {
+ var anon_decl = try block.startAnonDecl(field_src);
+ defer anon_decl.deinit();
+ const decl = try anon_decl.finish(
+ try field.ty.copy(anon_decl.arena()),
+ try field.default_val.copy(anon_decl.arena()),
+ );
+ if (ptr_ty_data.@"align" != 0) {
+ decl.align_val = field.abi_align;
+ }
+ return sema.analyzeDeclRef(decl);
+ }
+
if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| {
return sema.addConstant(
ptr_field_ty,
@@ -15330,6 +15343,10 @@ fn structFieldVal(
const field_index = @intCast(u32, field_index_usize);
const field = struct_obj.fields.values()[field_index];
+ if (field.is_comptime) {
+ return sema.addConstant(field.ty, field.default_val);
+ }
+
if (try sema.resolveMaybeUndefVal(block, src, struct_byval)) |struct_val| {
if (struct_val.isUndef()) return sema.addConstUndef(field.ty);
if ((try sema.typeHasOnePossibleValue(block, src, field.ty))) |opv| {