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(
80308030 decl.name = try ip.getOrPutStringFmt(gpa, tid, "{}__anon_{d}", .{
80318031 fn_owner_decl.name.fmt(ip), @intFromEnum(decl_index),
80328032 }, .no_embedded_nulls);
8033 decl.fqn = try ip.namespacePtr(fn_owner_decl.src_namespace)
8034 .internFullyQualifiedName(ip, gpa, tid, decl.name);
80338035}
80348036
80358037pub const EnumTypeInit = struct {
src/Sema.zig-3
......@@ -9737,9 +9737,6 @@ fn funcCommon(
97379737 .generic_owner = sema.generic_owner,
97389738 .comptime_args = sema.comptime_args,
97399739 });
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);
97439740 return finishFunc(
97449741 sema,
97459742 block,
src/Type.zig+1-1
......@@ -337,7 +337,7 @@ pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error
337337 try writer.print("{}", .{decl.fqn.fmt(ip)});
338338 } else if (ip.loadStructType(ty.toIntern()).namespace.unwrap()) |namespace_index| {
339339 const namespace = mod.namespacePtr(namespace_index);
340 try namespace.renderFullyQualifiedName(mod, .empty, writer);
340 try namespace.renderFullyQualifiedName(ip, .empty, writer);
341341 } else {
342342 try writer.writeAll("@TypeOf(.{})");
343343 }
src/Zcu.zig+28-25
......@@ -628,23 +628,27 @@ pub const Namespace = struct {
628628 return zcu.fileByIndex(ns.file_scope);
629629 }
630630
631 pub fn fileScopeIp(ns: Namespace, ip: *InternPool) *File {
632 return ip.filePtr(ns.file_scope);
633 }
634
631635 // This renders e.g. "std.fs.Dir.OpenOptions"
632636 pub fn renderFullyQualifiedName(
633637 ns: Namespace,
634 zcu: *Zcu,
638 ip: *InternPool,
635639 name: InternPool.NullTerminatedString,
636640 writer: anytype,
637641 ) @TypeOf(writer).Error!void {
638642 if (ns.parent.unwrap()) |parent| {
639 try zcu.namespacePtr(parent).renderFullyQualifiedName(
640 zcu,
641 zcu.declPtr(ns.decl_index).name,
643 try ip.namespacePtr(parent).renderFullyQualifiedName(
644 ip,
645 ip.declPtr(ns.decl_index).name,
642646 writer,
643647 );
644648 } else {
645 try ns.fileScope(zcu).renderFullyQualifiedName(writer);
649 try ns.fileScopeIp(ip).renderFullyQualifiedName(writer);
646650 }
647 if (name != .empty) try writer.print(".{}", .{name.fmt(&zcu.intern_pool)});
651 if (name != .empty) try writer.print(".{}", .{name.fmt(ip)});
648652 }
649653
650654 /// This renders e.g. "std/fs.zig:Dir.OpenOptions"
......@@ -670,44 +674,43 @@ pub const Namespace = struct {
670674
671675 pub fn internFullyQualifiedName(
672676 ns: Namespace,
673 pt: Zcu.PerThread,
677 ip: *InternPool,
678 gpa: Allocator,
679 tid: Zcu.PerThread.Id,
674680 name: InternPool.NullTerminatedString,
675681 ) !InternPool.NullTerminatedString {
676 const zcu = pt.zcu;
677 const ip = &zcu.intern_pool;
678
679 const gpa = zcu.gpa;
680 const strings = ip.getLocal(pt.tid).getMutableStrings(gpa);
682 const strings = ip.getLocal(tid).getMutableStrings(gpa);
681683 // Protects reads of interned strings from being reallocated during the call to
682684 // renderFullyQualifiedName.
683685 const slice = try strings.addManyAsSlice(count: {
684686 var count: usize = name.length(ip) + 1;
685687 var cur_ns = &ns;
686688 while (true) {
687 const decl = zcu.declPtr(cur_ns.decl_index);
688 cur_ns = zcu.namespacePtr(cur_ns.parent.unwrap() orelse {
689 count += ns.fileScope(zcu).fullyQualifiedNameLen();
689 const decl = ip.declPtr(cur_ns.decl_index);
690 cur_ns = ip.namespacePtr(cur_ns.parent.unwrap() orelse {
691 count += ns.fileScopeIp(ip).fullyQualifiedNameLen();
690692 break :count count;
691693 });
692694 count += decl.name.length(ip) + 1;
693695 }
694696 });
695697 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;
697699 assert(fbs.pos == slice[0].len);
698700
699701 // Sanitize the name for nvptx which is more restrictive.
700702 // TODO This should be handled by the backend, not the frontend. Have a
701703 // look at how the C backend does it for inspiration.
702 const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;
703 if (cpu_arch.isNvptx()) {
704 for (slice[0]) |*byte| switch (byte.*) {
705 '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',
706 else => {},
707 };
708 }
709
710 return ip.getOrPutTrailingString(gpa, pt.tid, @intCast(slice[0].len), .no_embedded_nulls);
704 // FIXME This has bitrotted and is no longer able to be implemented here.
705 //const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;
706 //if (cpu_arch.isNvptx()) {
707 // for (slice[0]) |*byte| switch (byte.*) {
708 // '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',
709 // else => {},
710 // };
711 //}
712
713 return ip.getOrPutTrailingString(gpa, tid, @intCast(slice[0].len), .no_embedded_nulls);
711714 }
712715
713716 pub fn getType(ns: Namespace, zcu: *Zcu) Type {
src/Zcu/PerThread.zig+4-4
......@@ -1896,7 +1896,7 @@ const ScanDeclIter = struct {
18961896 const was_exported = decl.is_exported;
18971897 assert(decl.kind == kind); // ZIR tracking should preserve this
18981898 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);
19001900 decl.is_pub = declaration.flags.is_pub;
19011901 decl.is_exported = declaration.flags.is_export;
19021902 break :decl_index .{ was_exported, decl_index };
......@@ -1906,7 +1906,7 @@ const ScanDeclIter = struct {
19061906 const new_decl = zcu.declPtr(new_decl_index);
19071907 new_decl.kind = kind;
19081908 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);
19101910 new_decl.is_pub = declaration.flags.is_pub;
19111911 new_decl.is_exported = declaration.flags.is_export;
19121912 new_decl.zir_decl_index = tracked_inst.toOptional();
......@@ -2279,8 +2279,8 @@ pub fn initNewAnonDecl(
22792279 const new_decl = pt.zcu.declPtr(new_decl_index);
22802280
22812281 new_decl.name = name;
2282 new_decl.fqn = fqn.unwrap() orelse
2283 try pt.zcu.namespacePtr(new_decl.src_namespace).internFullyQualifiedName(pt, name);
2282 new_decl.fqn = fqn.unwrap() orelse try pt.zcu.namespacePtr(new_decl.src_namespace)
2283 .internFullyQualifiedName(&pt.zcu.intern_pool, pt.zcu.gpa, pt.tid, name);
22842284 new_decl.val = val;
22852285 new_decl.alignment = .none;
22862286 new_decl.@"linksection" = .none;