authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-10-01 00:42:50-05:00
committergravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-10-02 15:21:48-05:00
logf7c11acb7f487806b14ada9d13d6afe8d3fe54d5
tree834666b13ed58fc9fbf25b2a9aec82ce4737fe0b
parent806eee8e99fbbd86f01b62b4306bd48f1cd3c872

Resolve struct fields in a separate sema context


2 files changed, 109 insertions(+), 99 deletions(-)

src/Module.zig-6
......@@ -3584,12 +3584,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
35843584 try mod.comp.work_queue.writeItem(.{ .emit_h_decl = decl });
35853585 }
35863586 }
3587 // In case this Decl is a struct or union, we need to resolve the fields
3588 // while we still have the `Sema` in scope, so that the field type expressions
3589 // can use the resolved AIR instructions that they possibly reference.
3590 // We do this after the decl is populated and set to `complete` so that a `Decl`
3591 // may reference itself.
3592 try sema.resolvePendingTypes(&block_scope);
35933587
35943588 if (decl.is_exported) {
35953589 const export_src = src; // TODO point to the export token
src/Sema.zig+109-93
......@@ -60,9 +60,6 @@ comptime_args_fn_inst: Zir.Inst.Index = 0,
6060/// extra hash table lookup in the `monomorphed_funcs` set.
6161/// Sema will set this to null when it takes ownership.
6262preallocated_new_func: ?*Module.Fn = null,
63/// Collects struct, union, enum, and opaque decls which need to have their
64/// fields resolved before this Sema is deinitialized.
65types_pending_resolution: std.ArrayListUnmanaged(Type) = .{},
6663
6764const std = @import("std");
6865const mem = std.mem;
......@@ -99,7 +96,6 @@ pub fn deinit(sema: *Sema) void {
9996 sema.air_values.deinit(gpa);
10097 sema.inst_map.deinit(gpa);
10198 sema.decl_val_table.deinit(gpa);
102 sema.types_pending_resolution.deinit(gpa);
10399 sema.* = undefined;
104100}
105101
......@@ -1093,9 +1089,7 @@ fn zirStructDecl(
10931089 &struct_obj.namespace, new_decl, new_decl.name,
10941090 });
10951091 try sema.analyzeStructDecl(new_decl, inst, struct_obj);
1096 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
10971092 try new_decl.finalizeNewArena(&new_decl_arena);
1098 sema.types_pending_resolution.appendAssumeCapacity(struct_ty);
10991093 return sema.analyzeDeclVal(block, src, new_decl);
11001094}
11011095
......@@ -1396,9 +1390,7 @@ fn zirUnionDecl(
13961390
13971391 new_decl.namespace = &union_obj.namespace;
13981392
1399 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
14001393 try new_decl.finalizeNewArena(&new_decl_arena);
1401 sema.types_pending_resolution.appendAssumeCapacity(union_ty);
14021394 return sema.analyzeDeclVal(block, src, new_decl);
14031395}
14041396
......@@ -3176,12 +3168,6 @@ fn analyzeCall(
31763168 });
31773169 delete_memoized_call_key = false;
31783170 }
3179
3180 // Much like in `Module.semaDecl`, if the result is a struct or union type,
3181 // we need to resolve the field type expressions right here, right now, while
3182 // the child `Sema` is still available, with the AIR instruction map intact,
3183 // because the field type expressions may reference into it.
3184 try sema.resolvePendingTypes(&child_block);
31853171 }
31863172
31873173 break :res2 result;
......@@ -11792,70 +11778,23 @@ pub fn resolveTypeLayout(
1179211778 }
1179311779}
1179411780
11795pub fn resolvePendingTypes(sema: *Sema, block: *Scope.Block) !void {
11796 for (sema.types_pending_resolution.items) |ty| {
11797 // If an error happens resolving the fields of a struct, it will be marked
11798 // invalid and a proper compile error set up. But we should still look at the
11799 // other types pending resolution.
11800 const src: LazySrcLoc = .{ .node_offset = 0 };
11801 sema.resolveDeclFields(block, src, ty) catch continue;
11802 }
11803}
11804
11805/// `sema` and `block` are expected to be the same ones used for the `Decl`.
11806pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void {
11781fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {
1180711782 switch (ty.tag()) {
1180811783 .@"struct" => {
1180911784 const struct_obj = ty.castTag(.@"struct").?.data;
11810 if (struct_obj.owner_decl.namespace.parent != block.src_decl.namespace) return;
1181111785 switch (struct_obj.status) {
1181211786 .none => {},
1181311787 .field_types_wip => {
1181411788 return sema.mod.fail(&block.base, src, "struct {} depends on itself", .{ty});
1181511789 },
11816 .have_field_types, .have_layout, .layout_wip => return,
11790 .have_field_types, .have_layout, .layout_wip => return ty,
1181711791 }
11818 const old_src = block.src_decl;
11819 defer block.src_decl = old_src;
11820 block.src_decl = struct_obj.owner_decl;
1182111792
1182211793 struct_obj.status = .field_types_wip;
11823 try sema.analyzeStructFields(block, struct_obj);
11794 try semaStructFields(sema.mod, struct_obj);
1182411795 struct_obj.status = .have_field_types;
11825 },
11826 .@"union", .union_tagged => {
11827 const union_obj = ty.cast(Type.Payload.Union).?.data;
11828 if (union_obj.owner_decl.namespace.parent != block.src_decl.namespace) return;
11829 switch (union_obj.status) {
11830 .none => {},
11831 .field_types_wip => {
11832 return sema.mod.fail(&block.base, src, "union {} depends on itself", .{ty});
11833 },
11834 .have_field_types, .have_layout, .layout_wip => return,
11835 }
11836 const old_src = block.src_decl;
11837 defer block.src_decl = old_src;
11838 block.src_decl = union_obj.owner_decl;
11839
11840 union_obj.status = .field_types_wip;
11841 try sema.analyzeUnionFields(block, union_obj);
11842 union_obj.status = .have_field_types;
11843 },
11844 else => return,
11845 }
11846}
1184711796
11848fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {
11849 switch (ty.tag()) {
11850 .@"struct" => {
11851 const struct_obj = ty.castTag(.@"struct").?.data;
11852 switch (struct_obj.status) {
11853 .none => unreachable,
11854 .field_types_wip => {
11855 return sema.mod.fail(&block.base, src, "struct {} depends on itself", .{ty});
11856 },
11857 .have_field_types, .have_layout, .layout_wip => return ty,
11858 }
11797 return ty;
1185911798 },
1186011799 .type_info => return sema.resolveBuiltinTypeFields(block, src, "TypeInfo"),
1186111800 .extern_options => return sema.resolveBuiltinTypeFields(block, src, "ExternOptions"),
......@@ -11871,12 +11810,18 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type
1187111810 .@"union", .union_tagged => {
1187211811 const union_obj = ty.cast(Type.Payload.Union).?.data;
1187311812 switch (union_obj.status) {
11874 .none => unreachable,
11813 .none => {},
1187511814 .field_types_wip => {
1187611815 return sema.mod.fail(&block.base, src, "union {} depends on itself", .{ty});
1187711816 },
1187811817 .have_field_types, .have_layout, .layout_wip => return ty,
1187911818 }
11819
11820 union_obj.status = .field_types_wip;
11821 try semaUnionFields(sema.mod, union_obj);
11822 union_obj.status = .have_field_types;
11823
11824 return ty;
1188011825 },
1188111826 else => return ty,
1188211827 }
......@@ -11892,16 +11837,16 @@ fn resolveBuiltinTypeFields(
1189211837 return sema.resolveTypeFields(block, src, resolved_ty);
1189311838}
1189411839
11895fn analyzeStructFields(
11896 sema: *Sema,
11897 block: *Scope.Block,
11840fn semaStructFields(
11841 mod: *Module,
1189811842 struct_obj: *Module.Struct,
1189911843) CompileError!void {
1190011844 const tracy = trace(@src());
1190111845 defer tracy.end();
1190211846
11903 const gpa = sema.gpa;
11904 const zir = sema.code;
11847 const gpa = mod.gpa;
11848 const decl = struct_obj.owner_decl;
11849 const zir = struct_obj.namespace.file_scope.zir;
1190511850 const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended;
1190611851 assert(extended.opcode == .struct_decl);
1190711852 const small = @bitCast(Zir.Inst.StructDecl.Small, extended.small);
......@@ -11940,15 +11885,50 @@ fn analyzeStructFields(
1194011885 }
1194111886 extra_index += body.len;
1194211887
11943 var decl_arena = struct_obj.owner_decl.value_arena.?.promote(gpa);
11944 defer struct_obj.owner_decl.value_arena.?.* = decl_arena.state;
11888 var decl_arena = decl.value_arena.?.promote(gpa);
11889 defer decl.value_arena.?.* = decl_arena.state;
11890
11891 var analysis_arena = std.heap.ArenaAllocator.init(gpa);
11892 defer analysis_arena.deinit();
11893
11894 var sema: Sema = .{
11895 .mod = mod,
11896 .gpa = gpa,
11897 .arena = &analysis_arena.allocator,
11898 .perm_arena = &decl_arena.allocator,
11899 .code = zir,
11900 .owner_decl = decl,
11901 .func = null,
11902 .fn_ret_ty = Type.initTag(.void),
11903 .owner_func = null,
11904 };
11905 defer sema.deinit();
1194511906
11946 try struct_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
11907 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
11908 defer wip_captures.deinit();
11909
11910 var block_scope: Scope.Block = .{
11911 .parent = null,
11912 .sema = &sema,
11913 .src_decl = decl,
11914 .wip_capture_scope = wip_captures.scope,
11915 .instructions = .{},
11916 .inlining = null,
11917 .is_comptime = true,
11918 };
11919 defer {
11920 assert(block_scope.instructions.items.len == 0);
11921 block_scope.params.deinit(gpa);
11922 }
1194711923
1194811924 if (body.len != 0) {
11949 _ = try sema.analyzeBody(block, body);
11925 _ = try sema.analyzeBody(&block_scope, body);
1195011926 }
1195111927
11928 try wip_captures.finalize();
11929
11930 try struct_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
11931
1195211932 const bits_per_field = 4;
1195311933 const fields_per_u32 = 32 / bits_per_field;
1195411934 const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable;
......@@ -11985,7 +11965,7 @@ fn analyzeStructFields(
1198511965 // TODO: if we need to report an error here, use a source location
1198611966 // that points to this type expression rather than the struct.
1198711967 // But only resolve the source location if we need to emit a compile error.
11988 try sema.resolveType(block, src, field_type_ref);
11968 try sema.resolveType(&block_scope, src, field_type_ref);
1198911969
1199011970 const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name);
1199111971 assert(!gop.found_existing);
......@@ -12003,7 +11983,7 @@ fn analyzeStructFields(
1200311983 // TODO: if we need to report an error here, use a source location
1200411984 // that points to this alignment expression rather than the struct.
1200511985 // But only resolve the source location if we need to emit a compile error.
12006 const abi_align_val = (try sema.resolveInstConst(block, src, align_ref)).val;
11986 const abi_align_val = (try sema.resolveInstConst(&block_scope, src, align_ref)).val;
1200711987 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);
1200811988 }
1200911989 if (has_default) {
......@@ -12013,23 +11993,23 @@ fn analyzeStructFields(
1201311993 // TODO: if we need to report an error here, use a source location
1201411994 // that points to this default value expression rather than the struct.
1201511995 // But only resolve the source location if we need to emit a compile error.
12016 const default_val = (try sema.resolveMaybeUndefVal(block, src, default_inst)) orelse
12017 return sema.failWithNeededComptime(block, src);
11996 const default_val = (try sema.resolveMaybeUndefVal(&block_scope, src, default_inst)) orelse
11997 return sema.failWithNeededComptime(&block_scope, src);
1201811998 gop.value_ptr.default_val = try default_val.copy(&decl_arena.allocator);
1201911999 }
1202012000 }
1202112001}
1202212002
12023fn analyzeUnionFields(
12024 sema: *Sema,
12025 block: *Scope.Block,
12003fn semaUnionFields(
12004 mod: *Module,
1202612005 union_obj: *Module.Union,
1202712006) CompileError!void {
1202812007 const tracy = trace(@src());
1202912008 defer tracy.end();
1203012009
12031 const gpa = sema.gpa;
12032 const zir = sema.code;
12010 const gpa = mod.gpa;
12011 const decl = union_obj.owner_decl;
12012 const zir = union_obj.namespace.file_scope.zir;
1203312013 const extended = zir.instructions.items(.data)[union_obj.zir_index].extended;
1203412014 assert(extended.opcode == .union_decl);
1203512015 const small = @bitCast(Zir.Inst.UnionDecl.Small, extended.small);
......@@ -12077,20 +12057,56 @@ fn analyzeUnionFields(
1207712057 var decl_arena = union_obj.owner_decl.value_arena.?.promote(gpa);
1207812058 defer union_obj.owner_decl.value_arena.?.* = decl_arena.state;
1207912059
12080 try union_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12060 var analysis_arena = std.heap.ArenaAllocator.init(gpa);
12061 defer analysis_arena.deinit();
12062
12063 var sema: Sema = .{
12064 .mod = mod,
12065 .gpa = gpa,
12066 .arena = &analysis_arena.allocator,
12067 .perm_arena = &decl_arena.allocator,
12068 .code = zir,
12069 .owner_decl = decl,
12070 .func = null,
12071 .fn_ret_ty = Type.initTag(.void),
12072 .owner_func = null,
12073 };
12074 defer sema.deinit();
12075
12076 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
12077 defer wip_captures.deinit();
12078
12079 var block_scope: Scope.Block = .{
12080 .parent = null,
12081 .sema = &sema,
12082 .src_decl = decl,
12083 .wip_capture_scope = wip_captures.scope,
12084 .instructions = .{},
12085 .inlining = null,
12086 .is_comptime = true,
12087 };
12088 defer {
12089 assert(block_scope.instructions.items.len == 0);
12090 block_scope.params.deinit(gpa);
12091 }
1208112092
1208212093 if (body.len != 0) {
12083 _ = try sema.analyzeBody(block, body);
12094 _ = try sema.analyzeBody(&block_scope, body);
1208412095 }
12096
12097 try wip_captures.finalize();
12098
12099 try union_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12100
1208512101 var int_tag_ty: Type = undefined;
1208612102 var enum_field_names: ?*Module.EnumNumbered.NameMap = null;
1208712103 var enum_value_map: ?*Module.EnumNumbered.ValueMap = null;
1208812104 if (tag_type_ref != .none) {
12089 const provided_ty = try sema.resolveType(block, src, tag_type_ref);
12105 const provided_ty = try sema.resolveType(&block_scope, src, tag_type_ref);
1209012106 if (small.auto_enum_tag) {
1209112107 // The provided type is an integer type and we must construct the enum tag type here.
1209212108 int_tag_ty = provided_ty;
12093 union_obj.tag_ty = try sema.generateUnionTagTypeNumbered(block, fields_len, provided_ty);
12109 union_obj.tag_ty = try sema.generateUnionTagTypeNumbered(&block_scope, fields_len, provided_ty);
1209412110 enum_field_names = &union_obj.tag_ty.castTag(.enum_numbered).?.data.fields;
1209512111 enum_value_map = &union_obj.tag_ty.castTag(.enum_numbered).?.data.values;
1209612112 } else {
......@@ -12101,7 +12117,7 @@ fn analyzeUnionFields(
1210112117 // If auto_enum_tag is false, this is an untagged union. However, for semantic analysis
1210212118 // purposes, we still auto-generate an enum tag type the same way. That the union is
1210312119 // untagged is represented by the Type tag (union vs union_tagged).
12104 union_obj.tag_ty = try sema.generateUnionTagTypeSimple(block, fields_len);
12120 union_obj.tag_ty = try sema.generateUnionTagTypeSimple(&block_scope, fields_len);
1210512121 enum_field_names = &union_obj.tag_ty.castTag(.enum_simple).?.data.fields;
1210612122 }
1210712123
......@@ -12150,8 +12166,8 @@ fn analyzeUnionFields(
1215012166
1215112167 if (enum_value_map) |map| {
1215212168 const tag_src = src; // TODO better source location
12153 const coerced = try sema.coerce(block, int_tag_ty, tag_ref, tag_src);
12154 const val = try sema.resolveConstValue(block, tag_src, coerced);
12169 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, tag_src);
12170 const val = try sema.resolveConstValue(&block_scope, tag_src, coerced);
1215512171 map.putAssumeCapacityContext(val, {}, .{ .ty = int_tag_ty });
1215612172 }
1215712173
......@@ -12167,7 +12183,7 @@ fn analyzeUnionFields(
1216712183 // TODO: if we need to report an error here, use a source location
1216812184 // that points to this type expression rather than the union.
1216912185 // But only resolve the source location if we need to emit a compile error.
12170 try sema.resolveType(block, src, field_type_ref);
12186 try sema.resolveType(&block_scope, src, field_type_ref);
1217112187
1217212188 const gop = union_obj.fields.getOrPutAssumeCapacity(field_name);
1217312189 assert(!gop.found_existing);
......@@ -12180,7 +12196,7 @@ fn analyzeUnionFields(
1218012196 // TODO: if we need to report an error here, use a source location
1218112197 // that points to this alignment expression rather than the struct.
1218212198 // But only resolve the source location if we need to emit a compile error.
12183 const abi_align_val = (try sema.resolveInstConst(block, src, align_ref)).val;
12199 const abi_align_val = (try sema.resolveInstConst(&block_scope, src, align_ref)).val;
1218412200 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);
1218512201 } else {
1218612202 gop.value_ptr.abi_align = Value.initTag(.abi_align_default);