authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-18 17:10:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
logfe87bae7e39657c96015aa7f3c7a35a0b01da1ad
tree1f5bfe4101613eebe88713d0ba7d8cba3c0f8b79
parent90cc408c1479e5e7ccd82369253d19f79d2812a5

frontend: fix handling of special builtin module

it's allocated differently and imported differently

4 files changed, 51 insertions(+), 42 deletions(-)

src/Builtin.zig+5-1
......@@ -266,11 +266,14 @@ pub fn populateFile(comp: *Compilation, mod: *Module, file: *File) !void {
266266 else => |e| return e,
267267 }
268268
269 log.debug("parsing and generating '{s}'", .{mod.root_src_path});
270
269271 file.tree = try std.zig.Ast.parse(comp.gpa, file.source, .zig);
270 file.tree_loaded = true;
271272 assert(file.tree.errors.len == 0); // builtin.zig must parse
273 file.tree_loaded = true;
272274
273275 file.zir = try AstGen.generate(comp.gpa, file.tree);
276 assert(!file.zir.hasCompileErrors()); // builtin.zig must not have astgen errors
274277 file.zir_loaded = true;
275278 file.status = .success_zir;
276279}
......@@ -296,3 +299,4 @@ const assert = std.debug.assert;
296299const AstGen = @import("AstGen.zig");
297300const File = @import("Module.zig").File;
298301const Compilation = @import("Compilation.zig");
302const log = std.log.scoped(.builtin);
src/Module.zig+44-20
......@@ -14,7 +14,9 @@ const BigIntMutable = std.math.big.int.Mutable;
1414const Target = std.Target;
1515const Ast = std.zig.Ast;
1616
17const Module = @This();
17/// Deprecated, use `Zcu`.
18const Module = Zcu;
19const Zcu = @This();
1820const Compilation = @import("Compilation.zig");
1921const Cache = std.Build.Cache;
2022const Value = @import("value.zig").Value;
......@@ -947,15 +949,21 @@ pub const File = struct {
947949
948950 pub fn deinit(file: *File, mod: *Module) void {
949951 const gpa = mod.gpa;
952 const is_builtin = file.mod.isBuiltin();
950953 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 }
951961 file.deleted_decls.deinit(gpa);
952962 file.outdated_decls.deinit(gpa);
953963 file.references.deinit(gpa);
954964 if (file.root_decl.unwrap()) |root_decl| {
955965 mod.destroyDecl(root_decl);
956966 }
957 gpa.free(file.sub_file_path);
958 file.unload(gpa);
959967 if (file.prev_zir) |prev_zir| {
960968 prev_zir.deinit(gpa);
961969 gpa.destroy(prev_zir);
......@@ -1017,8 +1025,9 @@ pub const File = struct {
10171025
10181026 pub fn destroy(file: *File, mod: *Module) void {
10191027 const gpa = mod.gpa;
1028 const is_builtin = file.mod.isBuiltin();
10201029 file.deinit(mod);
1021 gpa.destroy(file);
1030 if (!is_builtin) gpa.destroy(file);
10221031 }
10231032
10241033 pub fn renderFullyQualifiedName(file: File, writer: anytype) !void {
......@@ -3408,6 +3417,9 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
34083417 if (file.root_decl != .none) return;
34093418
34103419 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 });
34113423
34123424 // Because these three things each reference each other, `undefined`
34133425 // 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 {
35233535 defer tracy.end();
35243536
35253537 const decl = mod.declPtr(decl_index);
3538 const ip = &mod.intern_pool;
35263539
35273540 if (decl.getFileScope(mod).status != .success_zir) {
35283541 return error.AnalysisFail;
......@@ -3532,7 +3545,6 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
35323545 const zir = decl.getFileScope(mod).zir;
35333546 const zir_datas = zir.instructions.items(.data);
35343547
3535 // TODO: figure out how this works under incremental changes to builtin.zig!
35363548 const builtin_type_target_index: InternPool.Index = blk: {
35373549 const std_mod = mod.std_mod;
35383550 if (decl.getFileScope(mod).mod != std_mod) break :blk .none;
......@@ -3540,12 +3552,11 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
35403552 const std_file = (try mod.importPkg(std_mod)).file;
35413553 const std_decl = mod.declPtr(std_file.root_decl.unwrap().?);
35423554 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");
35443556 const builtin_decl = mod.declPtr(std_namespace.decls.getKeyAdapted(builtin_str, DeclAdapter{ .mod = mod }) orelse break :blk .none);
35453557 const builtin_namespace = builtin_decl.getInnerNamespaceIndex(mod).unwrap() orelse break :blk .none;
35463558 if (decl.src_namespace != builtin_namespace) break :blk .none;
35473559 // 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);
35493560 for ([_]struct { []const u8, InternPool.Index }{
35503561 .{ "AtomicOrder", .atomic_order_type },
35513562 .{ "AtomicRmwOp", .atomic_rmw_op_type },
......@@ -3559,6 +3570,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
35593570 .{ "ExternOptions", .extern_options_type },
35603571 .{ "Type", .type_info_type },
35613572 }) |pair| {
3573 const decl_name = ip.stringToSlice(decl.name);
35623574 if (std.mem.eql(u8, decl_name, pair[0])) {
35633575 break :blk pair[1];
35643576 }
......@@ -3654,7 +3666,6 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
36543666 return true;
36553667 }
36563668
3657 const ip = &mod.intern_pool;
36583669 switch (ip.indexToKey(decl_tv.val.toIntern())) {
36593670 .func => |func| {
36603671 const owns_tv = func.owner_decl == decl_index;
......@@ -3798,25 +3809,24 @@ pub const ImportFileResult = struct {
37983809 is_pkg: bool,
37993810};
38003811
3801/// https://github.com/ziglang/zig/issues/14307
3802pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult {
3803 const gpa = mod.gpa;
3812pub fn importPkg(zcu: *Zcu, mod: *Package.Module) !ImportFileResult {
3813 const gpa = zcu.gpa;
38043814
38053815 // The resolved path is used as the key in the import table, to detect if
38063816 // an import refers to the same as another, despite different relative paths
38073817 // or differently mapped package names.
38083818 const resolved_path = try std.fs.path.resolve(gpa, &.{
3809 pkg.root.root_dir.path orelse ".",
3810 pkg.root.sub_path,
3811 pkg.root_src_path,
3819 mod.root.root_dir.path orelse ".",
3820 mod.root.sub_path,
3821 mod.root_src_path,
38123822 });
38133823 var keep_resolved_path = false;
38143824 defer if (!keep_resolved_path) gpa.free(resolved_path);
38153825
3816 const gop = try mod.import_table.getOrPut(gpa, resolved_path);
3817 errdefer _ = mod.import_table.pop();
3826 const gop = try zcu.import_table.getOrPut(gpa, resolved_path);
3827 errdefer _ = zcu.import_table.pop();
38183828 if (gop.found_existing) {
3819 try gop.value_ptr.*.addReference(mod.*, .{ .root = pkg });
3829 try gop.value_ptr.*.addReference(zcu.*, .{ .root = mod });
38203830 return ImportFileResult{
38213831 .file = gop.value_ptr.*,
38223832 .is_new = false,
......@@ -3824,7 +3834,18 @@ pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult {
38243834 };
38253835 }
38263836
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);
38283849 errdefer gpa.free(sub_file_path);
38293850
38303851 const new_file = try gpa.create(File);
......@@ -3842,10 +3863,10 @@ pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult {
38423863 .tree = undefined,
38433864 .zir = undefined,
38443865 .status = .never_loaded,
3845 .mod = pkg,
3866 .mod = mod,
38463867 .root_decl = .none,
38473868 };
3848 try new_file.addReference(mod.*, .{ .root = pkg });
3869 try new_file.addReference(zcu.*, .{ .root = mod });
38493870 return ImportFileResult{
38503871 .file = new_file,
38513872 .is_new = true,
......@@ -4267,6 +4288,9 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
42674288 },
42684289 };
42694290 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 });
42704294 comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl_index });
42714295 }
42724296 new_decl.is_pub = is_pub;
src/codegen.zig+1-4
......@@ -119,10 +119,7 @@ pub fn generateLazySymbol(
119119
120120 const comp = bin_file.comp;
121121 const zcu = comp.module.?;
122 const decl_index = lazy_sym.ty.getOwnerDecl(zcu);
123 const decl = zcu.declPtr(decl_index);
124 const namespace = zcu.namespacePtr(decl.src_namespace);
125 const target = namespace.file_scope.mod.resolved_target.result;
122 const target = comp.root_mod.resolved_target.result;
126123 const endian = target.cpu.arch.endian();
127124 const gpa = comp.gpa;
128125
src/main.zig+1-17
......@@ -2662,23 +2662,7 @@ fn buildOutputType(
26622662 const std_mod = m: {
26632663 if (main_mod_is_std) break :m main_mod;
26642664 if (create_module.modules.get("std")) |cli_mod| break :m cli_mod.resolved.?;
2665
2666 break :m try Package.Module.create(arena, .{
2667 .global_cache_directory = global_cache_directory,
2668 .paths = .{
2669 .root = .{
2670 .root_dir = zig_lib_directory,
2671 .sub_path = "std",
2672 },
2673 .root_src_path = "std.zig",
2674 },
2675 .fully_qualified_name = "std",
2676 .cc_argv = &.{},
2677 .inherited = .{},
2678 .global = create_module.resolved_options,
2679 .parent = main_mod,
2680 .builtin_mod = main_mod.getBuiltinDependency(),
2681 });
2665 break :m null;
26822666 };
26832667
26842668 const root_mod = if (arg_mode == .zig_test) root_mod: {