| ... | ... | @@ -60,9 +60,6 @@ comptime_args_fn_inst: Zir.Inst.Index = 0, |
| 60 | 60 | /// extra hash table lookup in the `monomorphed_funcs` set. |
| 61 | 61 | /// Sema will set this to null when it takes ownership. |
| 62 | 62 | preallocated_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. |
| 65 | | types_pending_resolution: std.ArrayListUnmanaged(Type) = .{}, |
| 66 | 63 | |
| 67 | 64 | const std = @import("std"); |
| 68 | 65 | const mem = std.mem; |
| ... | ... | @@ -99,7 +96,6 @@ pub fn deinit(sema: *Sema) void { |
| 99 | 96 | sema.air_values.deinit(gpa); |
| 100 | 97 | sema.inst_map.deinit(gpa); |
| 101 | 98 | sema.decl_val_table.deinit(gpa); |
| 102 | | sema.types_pending_resolution.deinit(gpa); |
| 103 | 99 | sema.* = undefined; |
| 104 | 100 | } |
| 105 | 101 | |
| ... | ... | @@ -1093,9 +1089,7 @@ fn zirStructDecl( |
| 1093 | 1089 | &struct_obj.namespace, new_decl, new_decl.name, |
| 1094 | 1090 | }); |
| 1095 | 1091 | try sema.analyzeStructDecl(new_decl, inst, struct_obj); |
| 1096 | | try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1); |
| 1097 | 1092 | try new_decl.finalizeNewArena(&new_decl_arena); |
| 1098 | | sema.types_pending_resolution.appendAssumeCapacity(struct_ty); |
| 1099 | 1093 | return sema.analyzeDeclVal(block, src, new_decl); |
| 1100 | 1094 | } |
| 1101 | 1095 | |
| ... | ... | @@ -1396,9 +1390,7 @@ fn zirUnionDecl( |
| 1396 | 1390 | |
| 1397 | 1391 | new_decl.namespace = &union_obj.namespace; |
| 1398 | 1392 | |
| 1399 | | try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1); |
| 1400 | 1393 | try new_decl.finalizeNewArena(&new_decl_arena); |
| 1401 | | sema.types_pending_resolution.appendAssumeCapacity(union_ty); |
| 1402 | 1394 | return sema.analyzeDeclVal(block, src, new_decl); |
| 1403 | 1395 | } |
| 1404 | 1396 | |
| ... | ... | @@ -3176,12 +3168,6 @@ fn analyzeCall( |
| 3176 | 3168 | }); |
| 3177 | 3169 | delete_memoized_call_key = false; |
| 3178 | 3170 | } |
| 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); |
| 3185 | 3171 | } |
| 3186 | 3172 | |
| 3187 | 3173 | break :res2 result; |
| ... | ... | @@ -11792,70 +11778,23 @@ pub fn resolveTypeLayout( |
| 11792 | 11778 | } |
| 11793 | 11779 | } |
| 11794 | 11780 | |
| 11795 | | pub 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`. |
| 11806 | | pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void { |
| 11781 | fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type { |
| 11807 | 11782 | switch (ty.tag()) { |
| 11808 | 11783 | .@"struct" => { |
| 11809 | 11784 | const struct_obj = ty.castTag(.@"struct").?.data; |
| 11810 | | if (struct_obj.owner_decl.namespace.parent != block.src_decl.namespace) return; |
| 11811 | 11785 | switch (struct_obj.status) { |
| 11812 | 11786 | .none => {}, |
| 11813 | 11787 | .field_types_wip => { |
| 11814 | 11788 | return sema.mod.fail(&block.base, src, "struct {} depends on itself", .{ty}); |
| 11815 | 11789 | }, |
| 11816 | | .have_field_types, .have_layout, .layout_wip => return, |
| 11790 | .have_field_types, .have_layout, .layout_wip => return ty, |
| 11817 | 11791 | } |
| 11818 | | const old_src = block.src_decl; |
| 11819 | | defer block.src_decl = old_src; |
| 11820 | | block.src_decl = struct_obj.owner_decl; |
| 11821 | 11792 | |
| 11822 | 11793 | struct_obj.status = .field_types_wip; |
| 11823 | | try sema.analyzeStructFields(block, struct_obj); |
| 11794 | try semaStructFields(sema.mod, struct_obj); |
| 11824 | 11795 | 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 | | } |
| 11847 | 11796 | |
| 11848 | | fn 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; |
| 11859 | 11798 | }, |
| 11860 | 11799 | .type_info => return sema.resolveBuiltinTypeFields(block, src, "TypeInfo"), |
| 11861 | 11800 | .extern_options => return sema.resolveBuiltinTypeFields(block, src, "ExternOptions"), |
| ... | ... | @@ -11871,12 +11810,18 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type |
| 11871 | 11810 | .@"union", .union_tagged => { |
| 11872 | 11811 | const union_obj = ty.cast(Type.Payload.Union).?.data; |
| 11873 | 11812 | switch (union_obj.status) { |
| 11874 | | .none => unreachable, |
| 11813 | .none => {}, |
| 11875 | 11814 | .field_types_wip => { |
| 11876 | 11815 | return sema.mod.fail(&block.base, src, "union {} depends on itself", .{ty}); |
| 11877 | 11816 | }, |
| 11878 | 11817 | .have_field_types, .have_layout, .layout_wip => return ty, |
| 11879 | 11818 | } |
| 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; |
| 11880 | 11825 | }, |
| 11881 | 11826 | else => return ty, |
| 11882 | 11827 | } |
| ... | ... | @@ -11892,16 +11837,16 @@ fn resolveBuiltinTypeFields( |
| 11892 | 11837 | return sema.resolveTypeFields(block, src, resolved_ty); |
| 11893 | 11838 | } |
| 11894 | 11839 | |
| 11895 | | fn analyzeStructFields( |
| 11896 | | sema: *Sema, |
| 11897 | | block: *Scope.Block, |
| 11840 | fn semaStructFields( |
| 11841 | mod: *Module, |
| 11898 | 11842 | struct_obj: *Module.Struct, |
| 11899 | 11843 | ) CompileError!void { |
| 11900 | 11844 | const tracy = trace(@src()); |
| 11901 | 11845 | defer tracy.end(); |
| 11902 | 11846 | |
| 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; |
| 11905 | 11850 | const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended; |
| 11906 | 11851 | assert(extended.opcode == .struct_decl); |
| 11907 | 11852 | const small = @bitCast(Zir.Inst.StructDecl.Small, extended.small); |
| ... | ... | @@ -11940,15 +11885,50 @@ fn analyzeStructFields( |
| 11940 | 11885 | } |
| 11941 | 11886 | extra_index += body.len; |
| 11942 | 11887 | |
| 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(); |
| 11945 | 11906 | |
| 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 | } |
| 11947 | 11923 | |
| 11948 | 11924 | if (body.len != 0) { |
| 11949 | | _ = try sema.analyzeBody(block, body); |
| 11925 | _ = try sema.analyzeBody(&block_scope, body); |
| 11950 | 11926 | } |
| 11951 | 11927 | |
| 11928 | try wip_captures.finalize(); |
| 11929 | |
| 11930 | try struct_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len); |
| 11931 | |
| 11952 | 11932 | const bits_per_field = 4; |
| 11953 | 11933 | const fields_per_u32 = 32 / bits_per_field; |
| 11954 | 11934 | const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable; |
| ... | ... | @@ -11985,7 +11965,7 @@ fn analyzeStructFields( |
| 11985 | 11965 | // TODO: if we need to report an error here, use a source location |
| 11986 | 11966 | // that points to this type expression rather than the struct. |
| 11987 | 11967 | // 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); |
| 11989 | 11969 | |
| 11990 | 11970 | const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name); |
| 11991 | 11971 | assert(!gop.found_existing); |
| ... | ... | @@ -12003,7 +11983,7 @@ fn analyzeStructFields( |
| 12003 | 11983 | // TODO: if we need to report an error here, use a source location |
| 12004 | 11984 | // that points to this alignment expression rather than the struct. |
| 12005 | 11985 | // 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; |
| 12007 | 11987 | gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator); |
| 12008 | 11988 | } |
| 12009 | 11989 | if (has_default) { |
| ... | ... | @@ -12013,23 +11993,23 @@ fn analyzeStructFields( |
| 12013 | 11993 | // TODO: if we need to report an error here, use a source location |
| 12014 | 11994 | // that points to this default value expression rather than the struct. |
| 12015 | 11995 | // 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); |
| 12018 | 11998 | gop.value_ptr.default_val = try default_val.copy(&decl_arena.allocator); |
| 12019 | 11999 | } |
| 12020 | 12000 | } |
| 12021 | 12001 | } |
| 12022 | 12002 | |
| 12023 | | fn analyzeUnionFields( |
| 12024 | | sema: *Sema, |
| 12025 | | block: *Scope.Block, |
| 12003 | fn semaUnionFields( |
| 12004 | mod: *Module, |
| 12026 | 12005 | union_obj: *Module.Union, |
| 12027 | 12006 | ) CompileError!void { |
| 12028 | 12007 | const tracy = trace(@src()); |
| 12029 | 12008 | defer tracy.end(); |
| 12030 | 12009 | |
| 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; |
| 12033 | 12013 | const extended = zir.instructions.items(.data)[union_obj.zir_index].extended; |
| 12034 | 12014 | assert(extended.opcode == .union_decl); |
| 12035 | 12015 | const small = @bitCast(Zir.Inst.UnionDecl.Small, extended.small); |
| ... | ... | @@ -12077,20 +12057,56 @@ fn analyzeUnionFields( |
| 12077 | 12057 | var decl_arena = union_obj.owner_decl.value_arena.?.promote(gpa); |
| 12078 | 12058 | defer union_obj.owner_decl.value_arena.?.* = decl_arena.state; |
| 12079 | 12059 | |
| 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 | } |
| 12081 | 12092 | |
| 12082 | 12093 | if (body.len != 0) { |
| 12083 | | _ = try sema.analyzeBody(block, body); |
| 12094 | _ = try sema.analyzeBody(&block_scope, body); |
| 12084 | 12095 | } |
| 12096 | |
| 12097 | try wip_captures.finalize(); |
| 12098 | |
| 12099 | try union_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len); |
| 12100 | |
| 12085 | 12101 | var int_tag_ty: Type = undefined; |
| 12086 | 12102 | var enum_field_names: ?*Module.EnumNumbered.NameMap = null; |
| 12087 | 12103 | var enum_value_map: ?*Module.EnumNumbered.ValueMap = null; |
| 12088 | 12104 | 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); |
| 12090 | 12106 | if (small.auto_enum_tag) { |
| 12091 | 12107 | // The provided type is an integer type and we must construct the enum tag type here. |
| 12092 | 12108 | 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); |
| 12094 | 12110 | enum_field_names = &union_obj.tag_ty.castTag(.enum_numbered).?.data.fields; |
| 12095 | 12111 | enum_value_map = &union_obj.tag_ty.castTag(.enum_numbered).?.data.values; |
| 12096 | 12112 | } else { |
| ... | ... | @@ -12101,7 +12117,7 @@ fn analyzeUnionFields( |
| 12101 | 12117 | // If auto_enum_tag is false, this is an untagged union. However, for semantic analysis |
| 12102 | 12118 | // purposes, we still auto-generate an enum tag type the same way. That the union is |
| 12103 | 12119 | // 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); |
| 12105 | 12121 | enum_field_names = &union_obj.tag_ty.castTag(.enum_simple).?.data.fields; |
| 12106 | 12122 | } |
| 12107 | 12123 | |
| ... | ... | @@ -12150,8 +12166,8 @@ fn analyzeUnionFields( |
| 12150 | 12166 | |
| 12151 | 12167 | if (enum_value_map) |map| { |
| 12152 | 12168 | 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); |
| 12155 | 12171 | map.putAssumeCapacityContext(val, {}, .{ .ty = int_tag_ty }); |
| 12156 | 12172 | } |
| 12157 | 12173 | |
| ... | ... | @@ -12167,7 +12183,7 @@ fn analyzeUnionFields( |
| 12167 | 12183 | // TODO: if we need to report an error here, use a source location |
| 12168 | 12184 | // that points to this type expression rather than the union. |
| 12169 | 12185 | // 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); |
| 12171 | 12187 | |
| 12172 | 12188 | const gop = union_obj.fields.getOrPutAssumeCapacity(field_name); |
| 12173 | 12189 | assert(!gop.found_existing); |
| ... | ... | @@ -12180,7 +12196,7 @@ fn analyzeUnionFields( |
| 12180 | 12196 | // TODO: if we need to report an error here, use a source location |
| 12181 | 12197 | // that points to this alignment expression rather than the struct. |
| 12182 | 12198 | // 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; |
| 12184 | 12200 | gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator); |
| 12185 | 12201 | } else { |
| 12186 | 12202 | gop.value_ptr.abi_align = Value.initTag(.abi_align_default); |