authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-10 10:17:32-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-10 11:20:08-04:00
log8f292431b03055e789f75aa98888c0f49520e268
tree711a6305c0269de5de1786791749854efeefac82
parent3d2dfbe8289c2ecb45e1ba1fe79c4d7e21dd26c3

InternPool: fix undefined decl fully qualified name

This is now possible after moving `File.Index` to `*File` mapping into intern pool.

5 files changed, 35 insertions(+), 33 deletions(-)

src/InternPool.zig+2
...@@ -8030,6 +8030,8 @@ fn finishFuncInstance(...@@ -8030,6 +8030,8 @@ fn finishFuncInstance(
8030 decl.name = try ip.getOrPutStringFmt(gpa, tid, "{}__anon_{d}", .{8030 decl.name = try ip.getOrPutStringFmt(gpa, tid, "{}__anon_{d}", .{
8031 fn_owner_decl.name.fmt(ip), @intFromEnum(decl_index),8031 fn_owner_decl.name.fmt(ip), @intFromEnum(decl_index),
8032 }, .no_embedded_nulls);8032 }, .no_embedded_nulls);
8033 decl.fqn = try ip.namespacePtr(fn_owner_decl.src_namespace)
8034 .internFullyQualifiedName(ip, gpa, tid, decl.name);
8033}8035}
80348036
8035pub const EnumTypeInit = struct {8037pub const EnumTypeInit = struct {
src/Sema.zig-3
...@@ -9737,9 +9737,6 @@ fn funcCommon(...@@ -9737,9 +9737,6 @@ fn funcCommon(
9737 .generic_owner = sema.generic_owner,9737 .generic_owner = sema.generic_owner,
9738 .comptime_args = sema.comptime_args,9738 .comptime_args = sema.comptime_args,
9739 });9739 });
9740 const func_decl = mod.declPtr(ip.indexToKey(func_index).func.owner_decl);
9741 func_decl.fqn =
9742 try ip.namespacePtr(func_decl.src_namespace).internFullyQualifiedName(pt, func_decl.name);
9743 return finishFunc(9740 return finishFunc(
9744 sema,9741 sema,
9745 block,9742 block,
src/Type.zig+1-1
...@@ -337,7 +337,7 @@ pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error...@@ -337,7 +337,7 @@ pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error
337 try writer.print("{}", .{decl.fqn.fmt(ip)});337 try writer.print("{}", .{decl.fqn.fmt(ip)});
338 } else if (ip.loadStructType(ty.toIntern()).namespace.unwrap()) |namespace_index| {338 } else if (ip.loadStructType(ty.toIntern()).namespace.unwrap()) |namespace_index| {
339 const namespace = mod.namespacePtr(namespace_index);339 const namespace = mod.namespacePtr(namespace_index);
340 try namespace.renderFullyQualifiedName(mod, .empty, writer);340 try namespace.renderFullyQualifiedName(ip, .empty, writer);
341 } else {341 } else {
342 try writer.writeAll("@TypeOf(.{})");342 try writer.writeAll("@TypeOf(.{})");
343 }343 }
src/Zcu.zig+28-25
...@@ -628,23 +628,27 @@ pub const Namespace = struct {...@@ -628,23 +628,27 @@ pub const Namespace = struct {
628 return zcu.fileByIndex(ns.file_scope);628 return zcu.fileByIndex(ns.file_scope);
629 }629 }
630630
631 pub fn fileScopeIp(ns: Namespace, ip: *InternPool) *File {
632 return ip.filePtr(ns.file_scope);
633 }
634
631 // This renders e.g. "std.fs.Dir.OpenOptions"635 // This renders e.g. "std.fs.Dir.OpenOptions"
632 pub fn renderFullyQualifiedName(636 pub fn renderFullyQualifiedName(
633 ns: Namespace,637 ns: Namespace,
634 zcu: *Zcu,638 ip: *InternPool,
635 name: InternPool.NullTerminatedString,639 name: InternPool.NullTerminatedString,
636 writer: anytype,640 writer: anytype,
637 ) @TypeOf(writer).Error!void {641 ) @TypeOf(writer).Error!void {
638 if (ns.parent.unwrap()) |parent| {642 if (ns.parent.unwrap()) |parent| {
639 try zcu.namespacePtr(parent).renderFullyQualifiedName(643 try ip.namespacePtr(parent).renderFullyQualifiedName(
640 zcu,644 ip,
641 zcu.declPtr(ns.decl_index).name,645 ip.declPtr(ns.decl_index).name,
642 writer,646 writer,
643 );647 );
644 } else {648 } else {
645 try ns.fileScope(zcu).renderFullyQualifiedName(writer);649 try ns.fileScopeIp(ip).renderFullyQualifiedName(writer);
646 }650 }
647 if (name != .empty) try writer.print(".{}", .{name.fmt(&zcu.intern_pool)});651 if (name != .empty) try writer.print(".{}", .{name.fmt(ip)});
648 }652 }
649653
650 /// This renders e.g. "std/fs.zig:Dir.OpenOptions"654 /// This renders e.g. "std/fs.zig:Dir.OpenOptions"
...@@ -670,44 +674,43 @@ pub const Namespace = struct {...@@ -670,44 +674,43 @@ pub const Namespace = struct {
670674
671 pub fn internFullyQualifiedName(675 pub fn internFullyQualifiedName(
672 ns: Namespace,676 ns: Namespace,
673 pt: Zcu.PerThread,677 ip: *InternPool,
678 gpa: Allocator,
679 tid: Zcu.PerThread.Id,
674 name: InternPool.NullTerminatedString,680 name: InternPool.NullTerminatedString,
675 ) !InternPool.NullTerminatedString {681 ) !InternPool.NullTerminatedString {
676 const zcu = pt.zcu;682 const strings = ip.getLocal(tid).getMutableStrings(gpa);
677 const ip = &zcu.intern_pool;
678
679 const gpa = zcu.gpa;
680 const strings = ip.getLocal(pt.tid).getMutableStrings(gpa);
681 // Protects reads of interned strings from being reallocated during the call to683 // Protects reads of interned strings from being reallocated during the call to
682 // renderFullyQualifiedName.684 // renderFullyQualifiedName.
683 const slice = try strings.addManyAsSlice(count: {685 const slice = try strings.addManyAsSlice(count: {
684 var count: usize = name.length(ip) + 1;686 var count: usize = name.length(ip) + 1;
685 var cur_ns = &ns;687 var cur_ns = &ns;
686 while (true) {688 while (true) {
687 const decl = zcu.declPtr(cur_ns.decl_index);689 const decl = ip.declPtr(cur_ns.decl_index);
688 cur_ns = zcu.namespacePtr(cur_ns.parent.unwrap() orelse {690 cur_ns = ip.namespacePtr(cur_ns.parent.unwrap() orelse {
689 count += ns.fileScope(zcu).fullyQualifiedNameLen();691 count += ns.fileScopeIp(ip).fullyQualifiedNameLen();
690 break :count count;692 break :count count;
691 });693 });
692 count += decl.name.length(ip) + 1;694 count += decl.name.length(ip) + 1;
693 }695 }
694 });696 });
695 var fbs = std.io.fixedBufferStream(slice[0]);697 var fbs = std.io.fixedBufferStream(slice[0]);
696 ns.renderFullyQualifiedName(zcu, name, fbs.writer()) catch unreachable;698 ns.renderFullyQualifiedName(ip, name, fbs.writer()) catch unreachable;
697 assert(fbs.pos == slice[0].len);699 assert(fbs.pos == slice[0].len);
698700
699 // Sanitize the name for nvptx which is more restrictive.701 // Sanitize the name for nvptx which is more restrictive.
700 // TODO This should be handled by the backend, not the frontend. Have a702 // TODO This should be handled by the backend, not the frontend. Have a
701 // look at how the C backend does it for inspiration.703 // look at how the C backend does it for inspiration.
702 const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;704 // FIXME This has bitrotted and is no longer able to be implemented here.
703 if (cpu_arch.isNvptx()) {705 //const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;
704 for (slice[0]) |*byte| switch (byte.*) {706 //if (cpu_arch.isNvptx()) {
705 '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',707 // for (slice[0]) |*byte| switch (byte.*) {
706 else => {},708 // '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',
707 };709 // else => {},
708 }710 // };
709711 //}
710 return ip.getOrPutTrailingString(gpa, pt.tid, @intCast(slice[0].len), .no_embedded_nulls);712
713 return ip.getOrPutTrailingString(gpa, tid, @intCast(slice[0].len), .no_embedded_nulls);
711 }714 }
712715
713 pub fn getType(ns: Namespace, zcu: *Zcu) Type {716 pub fn getType(ns: Namespace, zcu: *Zcu) Type {
src/Zcu/PerThread.zig+4-4
...@@ -1896,7 +1896,7 @@ const ScanDeclIter = struct {...@@ -1896,7 +1896,7 @@ const ScanDeclIter = struct {
1896 const was_exported = decl.is_exported;1896 const was_exported = decl.is_exported;
1897 assert(decl.kind == kind); // ZIR tracking should preserve this1897 assert(decl.kind == kind); // ZIR tracking should preserve this
1898 decl.name = decl_name;1898 decl.name = decl_name;
1899 decl.fqn = try namespace.internFullyQualifiedName(pt, decl_name);1899 decl.fqn = try namespace.internFullyQualifiedName(ip, gpa, pt.tid, decl_name);
1900 decl.is_pub = declaration.flags.is_pub;1900 decl.is_pub = declaration.flags.is_pub;
1901 decl.is_exported = declaration.flags.is_export;1901 decl.is_exported = declaration.flags.is_export;
1902 break :decl_index .{ was_exported, decl_index };1902 break :decl_index .{ was_exported, decl_index };
...@@ -1906,7 +1906,7 @@ const ScanDeclIter = struct {...@@ -1906,7 +1906,7 @@ const ScanDeclIter = struct {
1906 const new_decl = zcu.declPtr(new_decl_index);1906 const new_decl = zcu.declPtr(new_decl_index);
1907 new_decl.kind = kind;1907 new_decl.kind = kind;
1908 new_decl.name = decl_name;1908 new_decl.name = decl_name;
1909 new_decl.fqn = try namespace.internFullyQualifiedName(pt, decl_name);1909 new_decl.fqn = try namespace.internFullyQualifiedName(ip, gpa, pt.tid, decl_name);
1910 new_decl.is_pub = declaration.flags.is_pub;1910 new_decl.is_pub = declaration.flags.is_pub;
1911 new_decl.is_exported = declaration.flags.is_export;1911 new_decl.is_exported = declaration.flags.is_export;
1912 new_decl.zir_decl_index = tracked_inst.toOptional();1912 new_decl.zir_decl_index = tracked_inst.toOptional();
...@@ -2279,8 +2279,8 @@ pub fn initNewAnonDecl(...@@ -2279,8 +2279,8 @@ pub fn initNewAnonDecl(
2279 const new_decl = pt.zcu.declPtr(new_decl_index);2279 const new_decl = pt.zcu.declPtr(new_decl_index);
22802280
2281 new_decl.name = name;2281 new_decl.name = name;
2282 new_decl.fqn = fqn.unwrap() orelse2282 new_decl.fqn = fqn.unwrap() orelse try pt.zcu.namespacePtr(new_decl.src_namespace)
2283 try pt.zcu.namespacePtr(new_decl.src_namespace).internFullyQualifiedName(pt, name);2283 .internFullyQualifiedName(&pt.zcu.intern_pool, pt.zcu.gpa, pt.tid, name);
2284 new_decl.val = val;2284 new_decl.val = val;
2285 new_decl.alignment = .none;2285 new_decl.alignment = .none;
2286 new_decl.@"linksection" = .none;2286 new_decl.@"linksection" = .none;