authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2022-01-07 18:58:40+01:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2022-01-07 22:10:36+01:00
log9d6bef49a5fc2a5387734fa8d8cc698f0da8d5fa
tree86304781ddc0b37b2b7881b54f72fb789660c7eb
parentd5093b6c1395d4a60a923e1911321784ce368bda

Add two more resolution status' to Struct and Union

resolveTypeForCodegen is called when we needed to resolve a type fully, even through pointer. This commit fully implements this, even through pointer fields on structs and unions. The function has now also been renamed to resolveTypeFully

3 files changed, 116 insertions(+), 19 deletions(-)

src/Module.zig+41-1
...@@ -831,6 +831,10 @@ pub const Struct = struct {...@@ -831,6 +831,10 @@ pub const Struct = struct {
831 have_field_types,831 have_field_types,
832 layout_wip,832 layout_wip,
833 have_layout,833 have_layout,
834 fully_resolved_wip,
835 // The types and all its fields have had their layout resolved. Even through pointer,
836 // which `have_layout` does not ensure.
837 fully_resolved,
834 },838 },
835 /// If true, definitely nonzero size at runtime. If false, resolving the fields839 /// If true, definitely nonzero size at runtime. If false, resolving the fields
836 /// is necessary to determine whether it has bits at runtime.840 /// is necessary to determine whether it has bits at runtime.
...@@ -889,6 +893,22 @@ pub const Struct = struct {...@@ -889,6 +893,22 @@ pub const Struct = struct {
889 .have_field_types,893 .have_field_types,
890 .layout_wip,894 .layout_wip,
891 .have_layout,895 .have_layout,
896 .fully_resolved_wip,
897 .fully_resolved,
898 => true,
899 };
900 }
901
902 pub fn haveLayout(s: Struct) bool {
903 return switch (s.status) {
904 .none,
905 .field_types_wip,
906 .have_field_types,
907 .layout_wip,
908 => false,
909 .have_layout,
910 .fully_resolved_wip,
911 .fully_resolved,
892 => true,912 => true,
893 };913 };
894 }914 }
...@@ -1003,6 +1023,10 @@ pub const Union = struct {...@@ -1003,6 +1023,10 @@ pub const Union = struct {
1003 have_field_types,1023 have_field_types,
1004 layout_wip,1024 layout_wip,
1005 have_layout,1025 have_layout,
1026 fully_resolved_wip,
1027 // The types and all its fields have had their layout resolved. Even through pointer,
1028 // which `have_layout` does not ensure.
1029 fully_resolved,
1006 },1030 },
10071031
1008 pub const Field = struct {1032 pub const Field = struct {
...@@ -1033,6 +1057,8 @@ pub const Union = struct {...@@ -1033,6 +1057,8 @@ pub const Union = struct {
1033 .have_field_types,1057 .have_field_types,
1034 .layout_wip,1058 .layout_wip,
1035 .have_layout,1059 .have_layout,
1060 .fully_resolved_wip,
1061 .fully_resolved,
1036 => true,1062 => true,
1037 };1063 };
1038 }1064 }
...@@ -1102,8 +1128,22 @@ pub const Union = struct {...@@ -1102,8 +1128,22 @@ pub const Union = struct {
1102 tag_size: u64,1128 tag_size: u64,
1103 };1129 };
11041130
1131 pub fn haveLayout(u: Union) bool {
1132 return switch (u.status) {
1133 .none,
1134 .field_types_wip,
1135 .have_field_types,
1136 .layout_wip,
1137 => false,
1138 .have_layout,
1139 .fully_resolved_wip,
1140 .fully_resolved,
1141 => true,
1142 };
1143 }
1144
1105 pub fn getLayout(u: Union, target: Target, have_tag: bool) Layout {1145 pub fn getLayout(u: Union, target: Target, have_tag: bool) Layout {
1106 assert(u.status == .have_layout);1146 assert(u.haveLayout());
1107 var most_aligned_field: u32 = undefined;1147 var most_aligned_field: u32 = undefined;
1108 var most_aligned_field_size: u64 = undefined;1148 var most_aligned_field_size: u64 = undefined;
1109 var biggest_field: u32 = undefined;1149 var biggest_field: u32 = undefined;
src/Sema.zig+72-15
...@@ -4503,14 +4503,14 @@ fn analyzeCall(...@@ -4503,14 +4503,14 @@ fn analyzeCall(
4503 const arg_src = call_src; // TODO: better source location4503 const arg_src = call_src; // TODO: better source location
4504 if (i < fn_params_len) {4504 if (i < fn_params_len) {
4505 const param_ty = func_ty.fnParamType(i);4505 const param_ty = func_ty.fnParamType(i);
4506 try sema.resolveTypeForCodegen(block, arg_src, param_ty);4506 try sema.resolveTypeFully(block, arg_src, param_ty);
4507 args[i] = try sema.coerce(block, param_ty, uncasted_arg, arg_src);4507 args[i] = try sema.coerce(block, param_ty, uncasted_arg, arg_src);
4508 } else {4508 } else {
4509 args[i] = uncasted_arg;4509 args[i] = uncasted_arg;
4510 }4510 }
4511 }4511 }
45124512
4513 try sema.resolveTypeForCodegen(block, call_src, func_ty_info.return_type);4513 try sema.resolveTypeFully(block, call_src, func_ty_info.return_type);
45144514
4515 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Call).Struct.fields.len +4515 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Call).Struct.fields.len +
4516 args.len);4516 args.len);
...@@ -4580,7 +4580,7 @@ fn finishGenericCall(...@@ -4580,7 +4580,7 @@ fn finishGenericCall(
4580 const param_ty = new_fn_ty.fnParamType(runtime_i);4580 const param_ty = new_fn_ty.fnParamType(runtime_i);
4581 const arg_src = call_src; // TODO: better source location4581 const arg_src = call_src; // TODO: better source location
4582 const uncasted_arg = uncasted_args[total_i];4582 const uncasted_arg = uncasted_args[total_i];
4583 try sema.resolveTypeForCodegen(block, arg_src, param_ty);4583 try sema.resolveTypeFully(block, arg_src, param_ty);
4584 const casted_arg = try sema.coerce(block, param_ty, uncasted_arg, arg_src);4584 const casted_arg = try sema.coerce(block, param_ty, uncasted_arg, arg_src);
4585 runtime_args[runtime_i] = casted_arg;4585 runtime_args[runtime_i] = casted_arg;
4586 runtime_i += 1;4586 runtime_i += 1;
...@@ -4588,7 +4588,7 @@ fn finishGenericCall(...@@ -4588,7 +4588,7 @@ fn finishGenericCall(
4588 total_i += 1;4588 total_i += 1;
4589 }4589 }
45904590
4591 try sema.resolveTypeForCodegen(block, call_src, new_fn_ty.fnReturnType());4591 try sema.resolveTypeFully(block, call_src, new_fn_ty.fnReturnType());
4592 }4592 }
4593 try sema.air_extra.ensureUnusedCapacity(sema.gpa, @typeInfo(Air.Call).Struct.fields.len +4593 try sema.air_extra.ensureUnusedCapacity(sema.gpa, @typeInfo(Air.Call).Struct.fields.len +
4594 runtime_args_len);4594 runtime_args_len);
...@@ -15228,7 +15228,7 @@ fn resolveStructLayout(...@@ -15228,7 +15228,7 @@ fn resolveStructLayout(
15228 .field_types_wip, .layout_wip => {15228 .field_types_wip, .layout_wip => {
15229 return sema.fail(block, src, "struct {} depends on itself", .{ty});15229 return sema.fail(block, src, "struct {} depends on itself", .{ty});
15230 },15230 },
15231 .have_layout => return,15231 .have_layout, .fully_resolved_wip, .fully_resolved => return,
15232 }15232 }
15233 struct_obj.status = .layout_wip;15233 struct_obj.status = .layout_wip;
15234 for (struct_obj.fields.values()) |field| {15234 for (struct_obj.fields.values()) |field| {
...@@ -15250,7 +15250,7 @@ fn resolveUnionLayout(...@@ -15250,7 +15250,7 @@ fn resolveUnionLayout(
15250 .field_types_wip, .layout_wip => {15250 .field_types_wip, .layout_wip => {
15251 return sema.fail(block, src, "union {} depends on itself", .{ty});15251 return sema.fail(block, src, "union {} depends on itself", .{ty});
15252 },15252 },
15253 .have_layout => return,15253 .have_layout, .fully_resolved_wip, .fully_resolved => return,
15254 }15254 }
15255 union_obj.status = .layout_wip;15255 union_obj.status = .layout_wip;
15256 for (union_obj.fields.values()) |field| {15256 for (union_obj.fields.values()) |field| {
...@@ -15259,7 +15259,7 @@ fn resolveUnionLayout(...@@ -15259,7 +15259,7 @@ fn resolveUnionLayout(
15259 union_obj.status = .have_layout;15259 union_obj.status = .have_layout;
15260}15260}
1526115261
15262fn resolveTypeForCodegen(15262fn resolveTypeFully(
15263 sema: *Sema,15263 sema: *Sema,
15264 block: *Block,15264 block: *Block,
15265 src: LazySrcLoc,15265 src: LazySrcLoc,
...@@ -15268,20 +15268,67 @@ fn resolveTypeForCodegen(...@@ -15268,20 +15268,67 @@ fn resolveTypeForCodegen(
15268 switch (ty.zigTypeTag()) {15268 switch (ty.zigTypeTag()) {
15269 .Pointer => {15269 .Pointer => {
15270 const child_ty = try sema.resolveTypeFields(block, src, ty.childType());15270 const child_ty = try sema.resolveTypeFields(block, src, ty.childType());
15271 return resolveTypeForCodegen(sema, block, src, child_ty);15271 return resolveTypeFully(sema, block, src, child_ty);
15272 },15272 },
15273 .Struct => return resolveStructLayout(sema, block, src, ty),15273 .Struct => return resolveStructFully(sema, block, src, ty),
15274 .Union => return resolveUnionLayout(sema, block, src, ty),15274 .Union => return resolveUnionFully(sema, block, src, ty),
15275 .Array => return resolveTypeForCodegen(sema, block, src, ty.childType()),15275 .Array => return resolveTypeFully(sema, block, src, ty.childType()),
15276 .Optional => {15276 .Optional => {
15277 var buf: Type.Payload.ElemType = undefined;15277 var buf: Type.Payload.ElemType = undefined;
15278 return resolveTypeForCodegen(sema, block, src, ty.optionalChild(&buf));15278 return resolveTypeFully(sema, block, src, ty.optionalChild(&buf));
15279 },15279 },
15280 .ErrorUnion => return resolveTypeForCodegen(sema, block, src, ty.errorUnionPayload()),15280 .ErrorUnion => return resolveTypeFully(sema, block, src, ty.errorUnionPayload()),
15281 else => {},15281 else => {},
15282 }15282 }
15283}15283}
1528415284
15285fn resolveStructFully(
15286 sema: *Sema,
15287 block: *Block,
15288 src: LazySrcLoc,
15289 ty: Type,
15290) CompileError!void {
15291 try resolveStructLayout(sema, block, src, ty);
15292
15293 const resolved_ty = try sema.resolveTypeFields(block, src, ty);
15294 const struct_obj = resolved_ty.castTag(.@"struct").?.data;
15295 switch (struct_obj.status) {
15296 .none, .have_field_types, .field_types_wip, .layout_wip, .have_layout => {},
15297 .fully_resolved_wip, .fully_resolved => return,
15298 }
15299
15300 // After we have resolve struct layout we have to go over the fields again to
15301 // make sure pointer fields get their child types resolved as well
15302 struct_obj.status = .fully_resolved_wip;
15303 for (struct_obj.fields.values()) |field| {
15304 try sema.resolveTypeFully(block, src, field.ty);
15305 }
15306 struct_obj.status = .fully_resolved;
15307}
15308
15309fn resolveUnionFully(
15310 sema: *Sema,
15311 block: *Block,
15312 src: LazySrcLoc,
15313 ty: Type,
15314) CompileError!void {
15315 try resolveUnionLayout(sema, block, src, ty);
15316
15317 const resolved_ty = try sema.resolveTypeFields(block, src, ty);
15318 const union_obj = resolved_ty.cast(Type.Payload.Union).?.data;
15319 switch (union_obj.status) {
15320 .none, .have_field_types, .field_types_wip, .layout_wip, .have_layout => {},
15321 .fully_resolved_wip, .fully_resolved => return,
15322 }
15323
15324 // Same goes for unions (see comment about structs)
15325 union_obj.status = .fully_resolved_wip;
15326 for (union_obj.fields.values()) |field| {
15327 try sema.resolveTypeFully(block, src, field.ty);
15328 }
15329 union_obj.status = .fully_resolved;
15330}
15331
15285fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!Type {15332fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!Type {
15286 switch (ty.tag()) {15333 switch (ty.tag()) {
15287 .@"struct" => {15334 .@"struct" => {
...@@ -15291,7 +15338,12 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp...@@ -15291,7 +15338,12 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp
15291 .field_types_wip => {15338 .field_types_wip => {
15292 return sema.fail(block, src, "struct {} depends on itself", .{ty});15339 return sema.fail(block, src, "struct {} depends on itself", .{ty});
15293 },15340 },
15294 .have_field_types, .have_layout, .layout_wip => return ty,15341 .have_field_types,
15342 .have_layout,
15343 .layout_wip,
15344 .fully_resolved_wip,
15345 .fully_resolved,
15346 => return ty,
15295 }15347 }
1529615348
15297 struct_obj.status = .field_types_wip;15349 struct_obj.status = .field_types_wip;
...@@ -15324,7 +15376,12 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp...@@ -15324,7 +15376,12 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp
15324 .field_types_wip => {15376 .field_types_wip => {
15325 return sema.fail(block, src, "union {} depends on itself", .{ty});15377 return sema.fail(block, src, "union {} depends on itself", .{ty});
15326 },15378 },
15327 .have_field_types, .have_layout, .layout_wip => return ty,15379 .have_field_types,
15380 .have_layout,
15381 .layout_wip,
15382 .fully_resolved_wip,
15383 .fully_resolved,
15384 => return ty,
15328 }15385 }
1532915386
15330 union_obj.status = .field_types_wip;15387 union_obj.status = .field_types_wip;
src/type.zig+3-3
...@@ -1916,7 +1916,7 @@ pub const Type = extern union {...@@ -1916,7 +1916,7 @@ pub const Type = extern union {
1916 const fields = self.structFields();1916 const fields = self.structFields();
1917 const is_packed = if (self.castTag(.@"struct")) |payload| p: {1917 const is_packed = if (self.castTag(.@"struct")) |payload| p: {
1918 const struct_obj = payload.data;1918 const struct_obj = payload.data;
1919 assert(struct_obj.status == .have_layout);1919 assert(struct_obj.haveLayout());
1920 break :p struct_obj.layout == .Packed;1920 break :p struct_obj.layout == .Packed;
1921 } else false;1921 } else false;
19221922
...@@ -2220,7 +2220,7 @@ pub const Type = extern union {...@@ -2220,7 +2220,7 @@ pub const Type = extern union {
2220 if (field_count == 0) return 0;2220 if (field_count == 0) return 0;
22212221
2222 const struct_obj = ty.castTag(.@"struct").?.data;2222 const struct_obj = ty.castTag(.@"struct").?.data;
2223 assert(struct_obj.status == .have_layout);2223 assert(struct_obj.haveLayout());
22242224
2225 var total: u64 = 0;2225 var total: u64 = 0;
2226 for (struct_obj.fields.values()) |field| {2226 for (struct_obj.fields.values()) |field| {
...@@ -3771,7 +3771,7 @@ pub const Type = extern union {...@@ -3771,7 +3771,7 @@ pub const Type = extern union {
3771 switch (ty.tag()) {3771 switch (ty.tag()) {
3772 .@"struct" => {3772 .@"struct" => {
3773 const struct_obj = ty.castTag(.@"struct").?.data;3773 const struct_obj = ty.castTag(.@"struct").?.data;
3774 assert(struct_obj.status == .have_layout);3774 assert(struct_obj.haveLayout());
3775 const is_packed = struct_obj.layout == .Packed;3775 const is_packed = struct_obj.layout == .Packed;
3776 if (!is_packed) {3776 if (!is_packed) {
3777 var offset: u64 = 0;3777 var offset: u64 = 0;