| ... | @@ -14,7 +14,9 @@ const BigIntMutable = std.math.big.int.Mutable; | ... | @@ -14,7 +14,9 @@ const BigIntMutable = std.math.big.int.Mutable; |
| 14 | const Target = std.Target; | 14 | const Target = std.Target; |
| 15 | const Ast = std.zig.Ast; | 15 | const Ast = std.zig.Ast; |
| 16 | | 16 | |
| 17 | const Module = @This(); | 17 | /// Deprecated, use `Zcu`. |
| | 18 | const Module = Zcu; |
| | 19 | const Zcu = @This(); |
| 18 | const Compilation = @import("Compilation.zig"); | 20 | const Compilation = @import("Compilation.zig"); |
| 19 | const Cache = std.Build.Cache; | 21 | const Cache = std.Build.Cache; |
| 20 | const Value = @import("value.zig").Value; | 22 | const Value = @import("value.zig").Value; |
| ... | @@ -947,15 +949,21 @@ pub const File = struct { | ... | @@ -947,15 +949,21 @@ pub const File = struct { |
| 947 | | 949 | |
| 948 | pub fn deinit(file: *File, mod: *Module) void { | 950 | pub fn deinit(file: *File, mod: *Module) void { |
| 949 | const gpa = mod.gpa; | 951 | const gpa = mod.gpa; |
| | 952 | const is_builtin = file.mod.isBuiltin(); |
| 950 | log.debug("deinit File {s}", .{file.sub_file_path}); | 953 | log.debug("deinit File {s}", .{file.sub_file_path}); |
| | 954 | if (is_builtin) { |
| | 955 | file.unloadTree(gpa); |
| | 956 | file.unloadZir(gpa); |
| | 957 | } else { |
| | 958 | gpa.free(file.sub_file_path); |
| | 959 | file.unload(gpa); |
| | 960 | } |
| 951 | file.deleted_decls.deinit(gpa); | 961 | file.deleted_decls.deinit(gpa); |
| 952 | file.outdated_decls.deinit(gpa); | 962 | file.outdated_decls.deinit(gpa); |
| 953 | file.references.deinit(gpa); | 963 | file.references.deinit(gpa); |
| 954 | if (file.root_decl.unwrap()) |root_decl| { | 964 | if (file.root_decl.unwrap()) |root_decl| { |
| 955 | mod.destroyDecl(root_decl); | 965 | mod.destroyDecl(root_decl); |
| 956 | } | 966 | } |
| 957 | gpa.free(file.sub_file_path); | | |
| 958 | file.unload(gpa); | | |
| 959 | if (file.prev_zir) |prev_zir| { | 967 | if (file.prev_zir) |prev_zir| { |
| 960 | prev_zir.deinit(gpa); | 968 | prev_zir.deinit(gpa); |
| 961 | gpa.destroy(prev_zir); | 969 | gpa.destroy(prev_zir); |
| ... | @@ -1017,8 +1025,9 @@ pub const File = struct { | ... | @@ -1017,8 +1025,9 @@ pub const File = struct { |
| 1017 | | 1025 | |
| 1018 | pub fn destroy(file: *File, mod: *Module) void { | 1026 | pub fn destroy(file: *File, mod: *Module) void { |
| 1019 | const gpa = mod.gpa; | 1027 | const gpa = mod.gpa; |
| | 1028 | const is_builtin = file.mod.isBuiltin(); |
| 1020 | file.deinit(mod); | 1029 | file.deinit(mod); |
| 1021 | gpa.destroy(file); | 1030 | if (!is_builtin) gpa.destroy(file); |
| 1022 | } | 1031 | } |
| 1023 | | 1032 | |
| 1024 | pub fn renderFullyQualifiedName(file: File, writer: anytype) !void { | 1033 | pub fn renderFullyQualifiedName(file: File, writer: anytype) !void { |
| ... | @@ -3408,6 +3417,9 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void { | ... | @@ -3408,6 +3417,9 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void { |
| 3408 | if (file.root_decl != .none) return; | 3417 | if (file.root_decl != .none) return; |
| 3409 | | 3418 | |
| 3410 | const gpa = mod.gpa; | 3419 | const gpa = mod.gpa; |
| | 3420 | log.debug("semaFile mod={s} sub_file_path={s}", .{ |
| | 3421 | file.mod.fully_qualified_name, file.sub_file_path, |
| | 3422 | }); |
| 3411 | | 3423 | |
| 3412 | // Because these three things each reference each other, `undefined` | 3424 | // Because these three things each reference each other, `undefined` |
| 3413 | // placeholders are used before being set after the struct type gains an | 3425 | // placeholders are used before being set after the struct type gains an |
| ... | @@ -3523,6 +3535,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { | ... | @@ -3523,6 +3535,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { |
| 3523 | defer tracy.end(); | 3535 | defer tracy.end(); |
| 3524 | | 3536 | |
| 3525 | const decl = mod.declPtr(decl_index); | 3537 | const decl = mod.declPtr(decl_index); |
| | 3538 | const ip = &mod.intern_pool; |
| 3526 | | 3539 | |
| 3527 | if (decl.getFileScope(mod).status != .success_zir) { | 3540 | if (decl.getFileScope(mod).status != .success_zir) { |
| 3528 | return error.AnalysisFail; | 3541 | return error.AnalysisFail; |
| ... | @@ -3532,7 +3545,6 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { | ... | @@ -3532,7 +3545,6 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { |
| 3532 | const zir = decl.getFileScope(mod).zir; | 3545 | const zir = decl.getFileScope(mod).zir; |
| 3533 | const zir_datas = zir.instructions.items(.data); | 3546 | const zir_datas = zir.instructions.items(.data); |
| 3534 | | 3547 | |
| 3535 | // TODO: figure out how this works under incremental changes to builtin.zig! | | |
| 3536 | const builtin_type_target_index: InternPool.Index = blk: { | 3548 | const builtin_type_target_index: InternPool.Index = blk: { |
| 3537 | const std_mod = mod.std_mod; | 3549 | const std_mod = mod.std_mod; |
| 3538 | if (decl.getFileScope(mod).mod != std_mod) break :blk .none; | 3550 | if (decl.getFileScope(mod).mod != std_mod) break :blk .none; |
| ... | @@ -3540,12 +3552,11 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { | ... | @@ -3540,12 +3552,11 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { |
| 3540 | const std_file = (try mod.importPkg(std_mod)).file; | 3552 | const std_file = (try mod.importPkg(std_mod)).file; |
| 3541 | const std_decl = mod.declPtr(std_file.root_decl.unwrap().?); | 3553 | const std_decl = mod.declPtr(std_file.root_decl.unwrap().?); |
| 3542 | const std_namespace = std_decl.getInnerNamespace(mod).?; | 3554 | const std_namespace = std_decl.getInnerNamespace(mod).?; |
| 3543 | const builtin_str = try mod.intern_pool.getOrPutString(gpa, "builtin"); | 3555 | const builtin_str = try ip.getOrPutString(gpa, "builtin"); |
| 3544 | const builtin_decl = mod.declPtr(std_namespace.decls.getKeyAdapted(builtin_str, DeclAdapter{ .mod = mod }) orelse break :blk .none); | 3556 | const builtin_decl = mod.declPtr(std_namespace.decls.getKeyAdapted(builtin_str, DeclAdapter{ .mod = mod }) orelse break :blk .none); |
| 3545 | const builtin_namespace = builtin_decl.getInnerNamespaceIndex(mod).unwrap() orelse break :blk .none; | 3557 | const builtin_namespace = builtin_decl.getInnerNamespaceIndex(mod).unwrap() orelse break :blk .none; |
| 3546 | if (decl.src_namespace != builtin_namespace) break :blk .none; | 3558 | if (decl.src_namespace != builtin_namespace) break :blk .none; |
| 3547 | // We're in builtin.zig. This could be a builtin we need to add to a specific InternPool index. | 3559 | // We're in builtin.zig. This could be a builtin we need to add to a specific InternPool index. |
| 3548 | const decl_name = mod.intern_pool.stringToSlice(decl.name); | | |
| 3549 | for ([_]struct { []const u8, InternPool.Index }{ | 3560 | for ([_]struct { []const u8, InternPool.Index }{ |
| 3550 | .{ "AtomicOrder", .atomic_order_type }, | 3561 | .{ "AtomicOrder", .atomic_order_type }, |
| 3551 | .{ "AtomicRmwOp", .atomic_rmw_op_type }, | 3562 | .{ "AtomicRmwOp", .atomic_rmw_op_type }, |
| ... | @@ -3559,6 +3570,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { | ... | @@ -3559,6 +3570,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { |
| 3559 | .{ "ExternOptions", .extern_options_type }, | 3570 | .{ "ExternOptions", .extern_options_type }, |
| 3560 | .{ "Type", .type_info_type }, | 3571 | .{ "Type", .type_info_type }, |
| 3561 | }) |pair| { | 3572 | }) |pair| { |
| | 3573 | const decl_name = ip.stringToSlice(decl.name); |
| 3562 | if (std.mem.eql(u8, decl_name, pair[0])) { | 3574 | if (std.mem.eql(u8, decl_name, pair[0])) { |
| 3563 | break :blk pair[1]; | 3575 | break :blk pair[1]; |
| 3564 | } | 3576 | } |
| ... | @@ -3654,7 +3666,6 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { | ... | @@ -3654,7 +3666,6 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { |
| 3654 | return true; | 3666 | return true; |
| 3655 | } | 3667 | } |
| 3656 | | 3668 | |
| 3657 | const ip = &mod.intern_pool; | | |
| 3658 | switch (ip.indexToKey(decl_tv.val.toIntern())) { | 3669 | switch (ip.indexToKey(decl_tv.val.toIntern())) { |
| 3659 | .func => |func| { | 3670 | .func => |func| { |
| 3660 | const owns_tv = func.owner_decl == decl_index; | 3671 | const owns_tv = func.owner_decl == decl_index; |
| ... | @@ -3798,25 +3809,24 @@ pub const ImportFileResult = struct { | ... | @@ -3798,25 +3809,24 @@ pub const ImportFileResult = struct { |
| 3798 | is_pkg: bool, | 3809 | is_pkg: bool, |
| 3799 | }; | 3810 | }; |
| 3800 | | 3811 | |
| 3801 | /// https://github.com/ziglang/zig/issues/14307 | 3812 | pub fn importPkg(zcu: *Zcu, mod: *Package.Module) !ImportFileResult { |
| 3802 | pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult { | 3813 | const gpa = zcu.gpa; |
| 3803 | const gpa = mod.gpa; | | |
| 3804 | | 3814 | |
| 3805 | // The resolved path is used as the key in the import table, to detect if | 3815 | // The resolved path is used as the key in the import table, to detect if |
| 3806 | // an import refers to the same as another, despite different relative paths | 3816 | // an import refers to the same as another, despite different relative paths |
| 3807 | // or differently mapped package names. | 3817 | // or differently mapped package names. |
| 3808 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | 3818 | const resolved_path = try std.fs.path.resolve(gpa, &.{ |
| 3809 | pkg.root.root_dir.path orelse ".", | 3819 | mod.root.root_dir.path orelse ".", |
| 3810 | pkg.root.sub_path, | 3820 | mod.root.sub_path, |
| 3811 | pkg.root_src_path, | 3821 | mod.root_src_path, |
| 3812 | }); | 3822 | }); |
| 3813 | var keep_resolved_path = false; | 3823 | var keep_resolved_path = false; |
| 3814 | defer if (!keep_resolved_path) gpa.free(resolved_path); | 3824 | defer if (!keep_resolved_path) gpa.free(resolved_path); |
| 3815 | | 3825 | |
| 3816 | const gop = try mod.import_table.getOrPut(gpa, resolved_path); | 3826 | const gop = try zcu.import_table.getOrPut(gpa, resolved_path); |
| 3817 | errdefer _ = mod.import_table.pop(); | 3827 | errdefer _ = zcu.import_table.pop(); |
| 3818 | if (gop.found_existing) { | 3828 | if (gop.found_existing) { |
| 3819 | try gop.value_ptr.*.addReference(mod.*, .{ .root = pkg }); | 3829 | try gop.value_ptr.*.addReference(zcu.*, .{ .root = mod }); |
| 3820 | return ImportFileResult{ | 3830 | return ImportFileResult{ |
| 3821 | .file = gop.value_ptr.*, | 3831 | .file = gop.value_ptr.*, |
| 3822 | .is_new = false, | 3832 | .is_new = false, |
| ... | @@ -3824,7 +3834,18 @@ pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult { | ... | @@ -3824,7 +3834,18 @@ pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult { |
| 3824 | }; | 3834 | }; |
| 3825 | } | 3835 | } |
| 3826 | | 3836 | |
| 3827 | const sub_file_path = try gpa.dupe(u8, pkg.root_src_path); | 3837 | if (mod.builtin_file) |builtin_file| { |
| | 3838 | keep_resolved_path = true; // It's now owned by import_table. |
| | 3839 | gop.value_ptr.* = builtin_file; |
| | 3840 | try builtin_file.addReference(zcu.*, .{ .root = mod }); |
| | 3841 | return .{ |
| | 3842 | .file = builtin_file, |
| | 3843 | .is_new = false, |
| | 3844 | .is_pkg = true, |
| | 3845 | }; |
| | 3846 | } |
| | 3847 | |
| | 3848 | const sub_file_path = try gpa.dupe(u8, mod.root_src_path); |
| 3828 | errdefer gpa.free(sub_file_path); | 3849 | errdefer gpa.free(sub_file_path); |
| 3829 | | 3850 | |
| 3830 | const new_file = try gpa.create(File); | 3851 | const new_file = try gpa.create(File); |
| ... | @@ -3842,10 +3863,10 @@ pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult { | ... | @@ -3842,10 +3863,10 @@ pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult { |
| 3842 | .tree = undefined, | 3863 | .tree = undefined, |
| 3843 | .zir = undefined, | 3864 | .zir = undefined, |
| 3844 | .status = .never_loaded, | 3865 | .status = .never_loaded, |
| 3845 | .mod = pkg, | 3866 | .mod = mod, |
| 3846 | .root_decl = .none, | 3867 | .root_decl = .none, |
| 3847 | }; | 3868 | }; |
| 3848 | try new_file.addReference(mod.*, .{ .root = pkg }); | 3869 | try new_file.addReference(zcu.*, .{ .root = mod }); |
| 3849 | return ImportFileResult{ | 3870 | return ImportFileResult{ |
| 3850 | .file = new_file, | 3871 | .file = new_file, |
| 3851 | .is_new = true, | 3872 | .is_new = true, |
| ... | @@ -4267,6 +4288,9 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err | ... | @@ -4267,6 +4288,9 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err |
| 4267 | }, | 4288 | }, |
| 4268 | }; | 4289 | }; |
| 4269 | if (want_analysis) { | 4290 | if (want_analysis) { |
| | 4291 | log.debug("scanDecl queue analyze_decl file='{s}' decl_name='{s}' decl_index={d}", .{ |
| | 4292 | namespace.file_scope.sub_file_path, ip.stringToSlice(decl_name), new_decl_index, |
| | 4293 | }); |
| 4270 | comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl_index }); | 4294 | comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl_index }); |
| 4271 | } | 4295 | } |
| 4272 | new_decl.is_pub = is_pub; | 4296 | new_decl.is_pub = is_pub; |