authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-10-01 12:05:12-05:00
committergravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-10-02 15:21:48-05:00
log53a36eacfa145244902e9230243c76892d141b03
tree6d750e53c9b86537f9ddfdeda7a24ee54356a7e9
parentd1a4bdb1f375b6aa737f1c02c346899336465e00

Remove my dumb "namespace decl" hack


9 files changed, 96 insertions(+), 87 deletions(-)

src/Compilation.zig+7-7
......@@ -1770,7 +1770,7 @@ pub fn update(self: *Compilation) !void {
17701770 assert(decl.deletion_flag);
17711771 assert(decl.dependants.count() == 0);
17721772 const is_anon = if (decl.zir_decl_index == 0) blk: {
1773 break :blk decl.namespace.anon_decls.swapRemove(decl);
1773 break :blk decl.src_namespace.anon_decls.swapRemove(decl);
17741774 } else false;
17751775
17761776 try module.clearDecl(decl, null);
......@@ -1889,13 +1889,13 @@ pub fn totalErrorCount(self: *Compilation) usize {
18891889 // the previous parse success, including compile errors, but we cannot
18901890 // emit them until the file succeeds parsing.
18911891 for (module.failed_decls.keys()) |key| {
1892 if (key.namespace.file_scope.okToReportErrors()) {
1892 if (key.getFileScope().okToReportErrors()) {
18931893 total += 1;
18941894 }
18951895 }
18961896 if (module.emit_h) |emit_h| {
18971897 for (emit_h.failed_decls.keys()) |key| {
1898 if (key.namespace.file_scope.okToReportErrors()) {
1898 if (key.getFileScope().okToReportErrors()) {
18991899 total += 1;
19001900 }
19011901 }
......@@ -1968,7 +1968,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
19681968 while (it.next()) |entry| {
19691969 // Skip errors for Decls within files that had a parse failure.
19701970 // We'll try again once parsing succeeds.
1971 if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {
1971 if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
19721972 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
19731973 }
19741974 }
......@@ -1978,7 +1978,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
19781978 while (it.next()) |entry| {
19791979 // Skip errors for Decls within files that had a parse failure.
19801980 // We'll try again once parsing succeeds.
1981 if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {
1981 if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
19821982 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
19831983 }
19841984 }
......@@ -2169,12 +2169,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
21692169 defer air.deinit(gpa);
21702170
21712171 log.debug("analyze liveness of {s}", .{decl.name});
2172 var liveness = try Liveness.analyze(gpa, air, decl.namespace.file_scope.zir);
2172 var liveness = try Liveness.analyze(gpa, air, decl.getFileScope().zir);
21732173 defer liveness.deinit(gpa);
21742174
21752175 if (builtin.mode == .Debug and self.verbose_air) {
21762176 std.debug.print("# Begin Function AIR: {s}:\n", .{decl.name});
2177 @import("print_air.zig").dump(gpa, air, decl.namespace.file_scope.zir, liveness);
2177 @import("print_air.zig").dump(gpa, air, decl.getFileScope().zir, liveness);
21782178 std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});
21792179 }
21802180
src/Module.zig+55-49
......@@ -264,7 +264,7 @@ pub const Export = struct {
264264
265265 pub fn getSrcLoc(exp: Export) SrcLoc {
266266 return .{
267 .file_scope = exp.src_decl.namespace.file_scope,
267 .file_scope = exp.src_decl.getFileScope(),
268268 .parent_decl_node = exp.src_decl.src_node,
269269 .lazy = exp.src,
270270 };
......@@ -350,7 +350,7 @@ pub const Decl = struct {
350350 /// Reference to externally owned memory.
351351 /// In the case of the Decl corresponding to a file, this is
352352 /// the namespace of the struct, since there is no parent.
353 namespace: *Scope.Namespace,
353 src_namespace: *Scope.Namespace,
354354
355355 /// The scope which lexically contains this decl. A decl must depend
356356 /// on its lexical parent, in order to ensure that this pointer is valid.
......@@ -516,7 +516,7 @@ pub const Decl = struct {
516516 /// This name is relative to the containing namespace of the decl.
517517 /// The memory is owned by the containing File ZIR.
518518 pub fn getName(decl: Decl) ?[:0]const u8 {
519 const zir = decl.namespace.file_scope.zir;
519 const zir = decl.getFileScope().zir;
520520 return decl.getNameZir(zir);
521521 }
522522
......@@ -528,7 +528,7 @@ pub const Decl = struct {
528528 }
529529
530530 pub fn contentsHash(decl: Decl) std.zig.SrcHash {
531 const zir = decl.namespace.file_scope.zir;
531 const zir = decl.getFileScope().zir;
532532 return decl.contentsHashZir(zir);
533533 }
534534
......@@ -541,21 +541,21 @@ pub const Decl = struct {
541541
542542 pub fn zirBlockIndex(decl: *const Decl) Zir.Inst.Index {
543543 assert(decl.zir_decl_index != 0);
544 const zir = decl.namespace.file_scope.zir;
544 const zir = decl.getFileScope().zir;
545545 return zir.extra[decl.zir_decl_index + 6];
546546 }
547547
548548 pub fn zirAlignRef(decl: Decl) Zir.Inst.Ref {
549549 if (!decl.has_align) return .none;
550550 assert(decl.zir_decl_index != 0);
551 const zir = decl.namespace.file_scope.zir;
551 const zir = decl.getFileScope().zir;
552552 return @intToEnum(Zir.Inst.Ref, zir.extra[decl.zir_decl_index + 7]);
553553 }
554554
555555 pub fn zirLinksectionRef(decl: Decl) Zir.Inst.Ref {
556556 if (!decl.has_linksection_or_addrspace) return .none;
557557 assert(decl.zir_decl_index != 0);
558 const zir = decl.namespace.file_scope.zir;
558 const zir = decl.getFileScope().zir;
559559 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align);
560560 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
561561 }
......@@ -563,16 +563,16 @@ pub const Decl = struct {
563563 pub fn zirAddrspaceRef(decl: Decl) Zir.Inst.Ref {
564564 if (!decl.has_linksection_or_addrspace) return .none;
565565 assert(decl.zir_decl_index != 0);
566 const zir = decl.namespace.file_scope.zir;
566 const zir = decl.getFileScope().zir;
567567 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align) + 1;
568568 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
569569 }
570570
571571 /// Returns true if and only if the Decl is the top level struct associated with a File.
572572 pub fn isRoot(decl: *const Decl) bool {
573 if (decl.namespace.parent != null)
573 if (decl.src_namespace.parent != null)
574574 return false;
575 return decl == decl.namespace.ty.getOwnerDecl();
575 return decl == decl.src_namespace.getDecl();
576576 }
577577
578578 pub fn relativeToLine(decl: Decl, offset: u32) u32 {
......@@ -608,29 +608,21 @@ pub const Decl = struct {
608608 }
609609
610610 pub fn srcToken(decl: Decl) Ast.TokenIndex {
611 const tree = &decl.namespace.file_scope.tree;
611 const tree = &decl.getFileScope().tree;
612612 return tree.firstToken(decl.src_node);
613613 }
614614
615615 pub fn srcByteOffset(decl: Decl) u32 {
616 const tree = &decl.namespace.file_scope.tree;
616 const tree = &decl.getFileScope().tree;
617617 return tree.tokens.items(.start)[decl.srcToken()];
618618 }
619619
620620 pub fn renderFullyQualifiedName(decl: *const Decl, writer: anytype) @TypeOf(writer).Error!void {
621 // Namespace decls (struct/enum/union/opaque) use their own namespace,
622 // which means the decl name and the namespace name are the same.
623 // In that case we want to omit the decl name, unless this is the root decl.
624 const unqualified_name = if (decl.namespace.getDecl() != decl or decl.namespace.parent == null) mem.spanZ(decl.name) else "";
625 return try decl.namespace.renderFullyQualifiedName(unqualified_name, writer);
621 return try decl.src_namespace.renderFullyQualifiedName(mem.spanZ(decl.name), writer);
626622 }
627623
628624 pub fn renderFullyQualifiedDebugName(decl: *const Decl, writer: anytype) @TypeOf(writer).Error!void {
629 // Namespace decls (struct/enum/union/opaque) use their own namespace,
630 // which means the decl name and the namespace name are the same.
631 // In that case we want to omit the decl name, unless this is the root decl.
632 const unqualified_name = if (decl.namespace.getDecl() != decl or decl.namespace.parent == null) mem.spanZ(decl.name) else "";
633 return try decl.namespace.renderFullyQualifiedDebugName(unqualified_name, writer);
625 return try decl.src_namespace.renderFullyQualifiedDebugName(mem.spanZ(decl.name), writer);
634626 }
635627
636628 pub fn getFullyQualifiedName(decl: *const Decl, gpa: *Allocator) ![:0]u8 {
......@@ -742,7 +734,7 @@ pub const Decl = struct {
742734 }
743735
744736 pub fn getFileScope(decl: Decl) *Scope.File {
745 return decl.namespace.file_scope;
737 return decl.src_namespace.file_scope;
746738 }
747739
748740 pub fn getEmitH(decl: *Decl, module: *Module) *EmitH {
......@@ -1118,8 +1110,8 @@ pub const Scope = struct {
11181110 /// Asserts the scope has a parent which is a Namespace and returns it.
11191111 pub fn namespace(scope: *Scope) *Namespace {
11201112 switch (scope.tag) {
1121 .block => return scope.cast(Block).?.src_decl.namespace,
1122 .file => return scope.cast(File).?.root_decl.?.namespace,
1113 .block => return scope.cast(Block).?.namespace,
1114 .file => return scope.cast(File).?.root_decl.?.src_namespace,
11231115 .namespace => return scope.cast(Namespace).?,
11241116 }
11251117 }
......@@ -1134,14 +1126,14 @@ pub const Scope = struct {
11341126 }
11351127 }
11361128
1137 /// When called from inside a Block Scope, chases the src_decl, not the owner_decl.
1129 /// When called from inside a Block Scope, chases the namespace, not the owner_decl.
11381130 pub fn getFileScope(base: *Scope) *Scope.File {
11391131 var cur = base;
11401132 while (true) {
11411133 cur = switch (cur.tag) {
11421134 .namespace => return @fieldParentPtr(Namespace, "base", cur).file_scope,
11431135 .file => return @fieldParentPtr(File, "base", cur),
1144 .block => return @fieldParentPtr(Block, "base", cur).src_decl.namespace.file_scope,
1136 .block => return @fieldParentPtr(Block, "base", cur).namespace.file_scope,
11451137 };
11461138 }
11471139 }
......@@ -1475,6 +1467,10 @@ pub const Scope = struct {
14751467 /// This can vary during inline or comptime function calls. See `Sema.owner_decl`
14761468 /// for the one that will be the same for all Block instances.
14771469 src_decl: *Decl,
1470 /// The namespace to use for lookups from this source block
1471 /// When analyzing fields, this is different from src_decl.src_namepsace.
1472 namespace: *Namespace,
1473 /// The AIR instructions generated for this block.
14781474 instructions: ArrayListUnmanaged(Air.Inst.Index),
14791475 // `param` instructions are collected here to be used by the `func` instruction.
14801476 params: std.ArrayListUnmanaged(Param) = .{},
......@@ -1541,6 +1537,7 @@ pub const Scope = struct {
15411537 .parent = parent,
15421538 .sema = parent.sema,
15431539 .src_decl = parent.src_decl,
1540 .namespace = parent.namespace,
15441541 .instructions = .{},
15451542 .wip_capture_scope = parent.wip_capture_scope,
15461543 .label = null,
......@@ -1564,7 +1561,7 @@ pub const Scope = struct {
15641561 }
15651562
15661563 pub fn getFileScope(block: *Block) *Scope.File {
1567 return block.src_decl.namespace.file_scope;
1564 return block.namespace.file_scope;
15681565 }
15691566
15701567 pub fn addTy(
......@@ -3327,6 +3324,7 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
33273324 .parent = null,
33283325 .sema = &sema,
33293326 .src_decl = new_decl,
3327 .namespace = &struct_obj.namespace,
33303328 .wip_capture_scope = wip_captures.scope,
33313329 .instructions = .{},
33323330 .inlining = null,
......@@ -3355,12 +3353,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
33553353 const tracy = trace(@src());
33563354 defer tracy.end();
33573355
3358 if (decl.namespace.file_scope.status != .success_zir) {
3356 if (decl.getFileScope().status != .success_zir) {
33593357 return error.AnalysisFail;
33603358 }
33613359
33623360 const gpa = mod.gpa;
3363 const zir = decl.namespace.file_scope.zir;
3361 const zir = decl.getFileScope().zir;
33643362 const zir_datas = zir.instructions.items(.data);
33653363
33663364 decl.analysis = .in_progress;
......@@ -3406,6 +3404,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
34063404 .parent = null,
34073405 .sema = &sema,
34083406 .src_decl = decl,
3407 .namespace = decl.src_namespace,
34093408 .wip_capture_scope = wip_captures.scope,
34103409 .instructions = .{},
34113410 .inlining = null,
......@@ -4042,8 +4041,8 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
40424041 },
40434042 }
40444043
4045 const owner_namespace = if (decl.namespace.getDecl() == decl and decl.namespace.parent != null) decl.namespace.parent.? else decl.namespace;
4046 assert(owner_namespace.anon_decls.swapRemove(decl));
4044 assert(!decl.isRoot());
4045 assert(decl.src_namespace.anon_decls.swapRemove(decl));
40474046
40484047 const dependants = decl.dependants.keys();
40494048 for (dependants) |dep| {
......@@ -4059,8 +4058,8 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
40594058pub fn abortAnonDecl(mod: *Module, decl: *Decl) void {
40604059 log.debug("abortAnonDecl {*} ({s})", .{ decl, decl.name });
40614060
4062 const owner_namespace = if (decl.namespace.getDecl() == decl and decl.namespace.parent != null) decl.namespace.parent.? else decl.namespace;
4063 assert(owner_namespace.anon_decls.swapRemove(decl));
4061 assert(!decl.isRoot());
4062 assert(decl.src_namespace.anon_decls.swapRemove(decl));
40644063
40654064 const dependants = decl.dependants.keys();
40664065 for (dependants) |dep| {
......@@ -4128,7 +4127,7 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
41284127 .gpa = gpa,
41294128 .arena = arena,
41304129 .perm_arena = &decl_arena.allocator,
4131 .code = decl.namespace.file_scope.zir,
4130 .code = decl.getFileScope().zir,
41324131 .owner_decl = decl,
41334132 .func = func,
41344133 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),
......@@ -4148,6 +4147,7 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
41484147 .parent = null,
41494148 .sema = &sema,
41504149 .src_decl = decl,
4150 .namespace = decl.src_namespace,
41514151 .wip_capture_scope = wip_captures.scope,
41524152 .instructions = .{},
41534153 .inlining = null,
......@@ -4279,7 +4279,7 @@ pub fn allocateNewDecl(mod: *Module, name: [:0]const u8, namespace: *Scope.Names
42794279
42804280 new_decl.* = .{
42814281 .name = name,
4282 .namespace = namespace,
4282 .src_namespace = namespace,
42834283 .src_node = src_node,
42844284 .src_line = undefined,
42854285 .has_tv = false,
......@@ -4426,32 +4426,38 @@ pub fn createAnonymousDeclNamed(
44264426 typed_value: TypedValue,
44274427 name: [:0]u8,
44284428) !*Decl {
4429 return mod.createAnonymousDeclFromDeclNamed(scope.srcDecl().?, scope.srcScope(), typed_value, name);
4429 return mod.createAnonymousDeclFromDeclNamed(scope.srcDecl().?, scope.namespace(), scope.srcScope(), typed_value, name);
44304430}
44314431
44324432pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl {
4433 return mod.createAnonymousDeclFromDecl(scope.srcDecl().?, scope.srcScope(), typed_value);
4433 return mod.createAnonymousDeclFromDecl(scope.srcDecl().?, scope.namespace(), scope.srcScope(), typed_value);
44344434}
44354435
4436pub fn createAnonymousDeclFromDecl(mod: *Module, src_decl: *Decl, src_scope: ?*CaptureScope, tv: TypedValue) !*Decl {
4436pub fn createAnonymousDeclFromDecl(
4437 mod: *Module,
4438 src_decl: *Decl,
4439 namespace: *Scope.Namespace,
4440 src_scope: ?*CaptureScope,
4441 tv: TypedValue,
4442) !*Decl {
44374443 const name_index = mod.getNextAnonNameIndex();
44384444 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{
44394445 src_decl.name, name_index,
44404446 });
4441 return mod.createAnonymousDeclFromDeclNamed(src_decl, src_scope, tv, name);
4447 return mod.createAnonymousDeclFromDeclNamed(src_decl, namespace, src_scope, tv, name);
44424448}
44434449
44444450/// Takes ownership of `name` even if it returns an error.
44454451pub fn createAnonymousDeclFromDeclNamed(
44464452 mod: *Module,
44474453 src_decl: *Decl,
4454 namespace: *Scope.Namespace,
44484455 src_scope: ?*CaptureScope,
44494456 typed_value: TypedValue,
44504457 name: [:0]u8,
44514458) !*Decl {
44524459 errdefer mod.gpa.free(name);
44534460
4454 const namespace = src_decl.namespace;
44554461 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);
44564462
44574463 const new_decl = try mod.allocateNewDecl(name, namespace, src_decl.src_node, src_scope);
......@@ -4705,10 +4711,10 @@ pub const SwitchProngSrc = union(enum) {
47054711 range_expand: RangeExpand,
47064712 ) LazySrcLoc {
47074713 @setCold(true);
4708 const tree = decl.namespace.file_scope.getTree(gpa) catch |err| {
4714 const tree = decl.getFileScope().getTree(gpa) catch |err| {
47094715 // In this case we emit a warning + a less precise source location.
47104716 log.warn("unable to load {s}: {s}", .{
4711 decl.namespace.file_scope.sub_file_path, @errorName(err),
4717 decl.getFileScope().sub_file_path, @errorName(err),
47124718 });
47134719 return LazySrcLoc{ .node_offset = 0 };
47144720 };
......@@ -4817,10 +4823,10 @@ pub const PeerTypeCandidateSrc = union(enum) {
48174823 else => {},
48184824 }
48194825
4820 const tree = decl.namespace.file_scope.getTree(gpa) catch |err| {
4826 const tree = decl.getFileScope().getTree(gpa) catch |err| {
48214827 // In this case we emit a warning + a less precise source location.
48224828 log.warn("unable to load {s}: {s}", .{
4823 decl.namespace.file_scope.sub_file_path, @errorName(err),
4829 decl.getFileScope().sub_file_path, @errorName(err),
48244830 });
48254831 return LazySrcLoc{ .node_offset = 0 };
48264832 };
......@@ -4863,7 +4869,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
48634869
48644870 // Remove from the namespace it resides in, preserving declaration order.
48654871 assert(decl.zir_decl_index != 0);
4866 _ = decl.namespace.decls.orderedRemove(mem.spanZ(decl.name));
4872 _ = decl.src_namespace.decls.orderedRemove(mem.spanZ(decl.name));
48674873
48684874 try mod.clearDecl(decl, &outdated_decls);
48694875 decl.destroy(mod);
......@@ -4929,7 +4935,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
49294935 const gpa = mod.gpa;
49304936 const builtin_pkg = mod.main_pkg.table.get("builtin").?;
49314937 const builtin_file = (mod.importPkg(builtin_pkg) catch unreachable).file;
4932 const builtin_namespace = builtin_file.root_decl.?.namespace;
4938 const builtin_namespace = builtin_file.root_decl.?.src_namespace;
49334939 const decl = builtin_namespace.decls.get("test_functions").?;
49344940 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
49354941 const tmp_test_fn_ty = decl.ty.slicePtrFieldType(&buf).elemType();
......@@ -4942,7 +4948,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
49424948 const arena = &new_decl_arena.allocator;
49434949
49444950 const test_fn_vals = try arena.alloc(Value, mod.test_functions.count());
4945 const array_decl = try mod.createAnonymousDeclFromDecl(decl, null, .{
4951 const array_decl = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, null, .{
49464952 .ty = try Type.Tag.array.create(arena, .{
49474953 .len = test_fn_vals.len,
49484954 .elem_type = try tmp_test_fn_ty.copy(arena),
......@@ -4955,7 +4961,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
49554961 var name_decl_arena = std.heap.ArenaAllocator.init(gpa);
49564962 errdefer name_decl_arena.deinit();
49574963 const bytes = try name_decl_arena.allocator.dupe(u8, test_name_slice);
4958 const test_name_decl = try mod.createAnonymousDeclFromDecl(array_decl, null, .{
4964 const test_name_decl = try mod.createAnonymousDeclFromDecl(array_decl, array_decl.src_namespace, null, .{
49594965 .ty = try Type.Tag.array_u8.create(&name_decl_arena.allocator, bytes.len),
49604966 .val = try Value.Tag.bytes.create(&name_decl_arena.allocator, bytes),
49614967 });
src/Sema.zig+23-20
......@@ -1042,8 +1042,6 @@ pub fn analyzeStructDecl(
10421042 } else 0;
10431043
10441044 _ = try sema.mod.scanNamespace(&struct_obj.namespace, extra_index, decls_len, new_decl);
1045
1046 new_decl.namespace = &struct_obj.namespace;
10471045}
10481046
10491047fn zirStructDecl(
......@@ -1080,7 +1078,7 @@ fn zirStructDecl(
10801078 .status = .none,
10811079 .known_has_bits = undefined,
10821080 .namespace = .{
1083 .parent = block.src_decl.namespace,
1081 .parent = block.namespace,
10841082 .ty = struct_ty,
10851083 .file_scope = block.getFileScope(),
10861084 },
......@@ -1188,7 +1186,7 @@ fn zirEnumDecl(
11881186 .values = .{},
11891187 .node_offset = src.node_offset,
11901188 .namespace = .{
1191 .parent = block.src_decl.namespace,
1189 .parent = block.namespace,
11921190 .ty = enum_ty,
11931191 .file_scope = block.getFileScope(),
11941192 },
......@@ -1199,8 +1197,6 @@ fn zirEnumDecl(
11991197
12001198 extra_index = try mod.scanNamespace(&enum_obj.namespace, extra_index, decls_len, new_decl);
12011199
1202 new_decl.namespace = &enum_obj.namespace;
1203
12041200 const body = sema.code.extra[extra_index..][0..body_len];
12051201 if (fields_len == 0) {
12061202 assert(body.len == 0);
......@@ -1238,6 +1234,7 @@ fn zirEnumDecl(
12381234 .parent = null,
12391235 .sema = sema,
12401236 .src_decl = new_decl,
1237 .namespace = &enum_obj.namespace,
12411238 .wip_capture_scope = wip_captures.scope,
12421239 .instructions = .{},
12431240 .inlining = null,
......@@ -1377,7 +1374,7 @@ fn zirUnionDecl(
13771374 .layout = small.layout,
13781375 .status = .none,
13791376 .namespace = .{
1380 .parent = block.src_decl.namespace,
1377 .parent = block.namespace,
13811378 .ty = union_ty,
13821379 .file_scope = block.getFileScope(),
13831380 },
......@@ -1388,8 +1385,6 @@ fn zirUnionDecl(
13881385
13891386 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);
13901387
1391 new_decl.namespace = &union_obj.namespace;
1392
13931388 try new_decl.finalizeNewArena(&new_decl_arena);
13941389 return sema.analyzeDeclVal(block, src, new_decl);
13951390}
......@@ -2283,6 +2278,7 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
22832278 .parent = parent_block,
22842279 .sema = sema,
22852280 .src_decl = parent_block.src_decl,
2281 .namespace = parent_block.namespace,
22862282 .wip_capture_scope = parent_block.wip_capture_scope,
22872283 .instructions = .{},
22882284 .inlining = parent_block.inlining,
......@@ -2383,6 +2379,7 @@ fn zirBlock(
23832379 .parent = parent_block,
23842380 .sema = sema,
23852381 .src_decl = parent_block.src_decl,
2382 .namespace = parent_block.namespace,
23862383 .wip_capture_scope = parent_block.wip_capture_scope,
23872384 .instructions = .{},
23882385 .label = &label,
......@@ -2684,7 +2681,7 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
26842681}
26852682
26862683fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {
2687 var namespace = block.src_decl.namespace;
2684 var namespace = block.namespace;
26882685 while (true) {
26892686 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {
26902687 return decl;
......@@ -2713,7 +2710,7 @@ fn lookupInNamespace(
27132710 }
27142711
27152712 if (observe_usingnamespace and namespace.usingnamespace_set.count() != 0) {
2716 const src_file = block.src_decl.namespace.file_scope;
2713 const src_file = block.namespace.file_scope;
27172714
27182715 const gpa = sema.gpa;
27192716 var checked_namespaces: std.AutoArrayHashMapUnmanaged(*Scope.Namespace, void) = .{};
......@@ -2731,7 +2728,7 @@ fn lookupInNamespace(
27312728 if (check_ns.decls.get(ident_name)) |decl| {
27322729 // Skip decls which are not marked pub, which are in a different
27332730 // file than the `a.b`/`@hasDecl` syntax.
2734 if (decl.is_pub or src_file == decl.namespace.file_scope) {
2731 if (decl.is_pub or src_file == decl.getFileScope()) {
27352732 try candidates.append(gpa, decl);
27362733 }
27372734 }
......@@ -2739,7 +2736,7 @@ fn lookupInNamespace(
27392736 while (it.next()) |entry| {
27402737 const sub_usingnamespace_decl = entry.key_ptr.*;
27412738 const sub_is_pub = entry.value_ptr.*;
2742 if (!sub_is_pub and src_file != sub_usingnamespace_decl.namespace.file_scope) {
2739 if (!sub_is_pub and src_file != sub_usingnamespace_decl.getFileScope()) {
27432740 // Skip usingnamespace decls which are not marked pub, which are in
27442741 // a different file than the `a.b`/`@hasDecl` syntax.
27452742 continue;
......@@ -2992,7 +2989,7 @@ fn analyzeCall(
29922989 // In order to save a bit of stack space, directly modify Sema rather
29932990 // than create a child one.
29942991 const parent_zir = sema.code;
2995 sema.code = module_fn.owner_decl.namespace.file_scope.zir;
2992 sema.code = module_fn.owner_decl.getFileScope().zir;
29962993 defer sema.code = parent_zir;
29972994
29982995 const parent_inst_map = sema.inst_map;
......@@ -3013,6 +3010,7 @@ fn analyzeCall(
30133010 .parent = null,
30143011 .sema = sema,
30153012 .src_decl = module_fn.owner_decl,
3013 .namespace = module_fn.owner_decl.src_namespace,
30163014 .wip_capture_scope = wip_captures.scope,
30173015 .instructions = .{},
30183016 .label = null,
......@@ -3182,7 +3180,7 @@ fn analyzeCall(
31823180 // Check the Module's generic function map with an adapted context, so that we
31833181 // can match against `uncasted_args` rather than doing the work below to create a
31843182 // generic Scope only to junk it if it matches an existing instantiation.
3185 const namespace = module_fn.owner_decl.namespace;
3183 const namespace = module_fn.owner_decl.src_namespace;
31863184 const fn_zir = namespace.file_scope.zir;
31873185 const fn_info = fn_zir.getFnInfo(module_fn.zir_body_inst);
31883186 const zir_tags = fn_zir.instructions.items(.tag);
......@@ -3314,6 +3312,7 @@ fn analyzeCall(
33143312 .parent = null,
33153313 .sema = &child_sema,
33163314 .src_decl = new_decl,
3315 .namespace = namespace,
33173316 .wip_capture_scope = wip_captures.scope,
33183317 .instructions = .{},
33193318 .inlining = null,
......@@ -5338,6 +5337,7 @@ fn analyzeSwitch(
53385337 .parent = block,
53395338 .sema = sema,
53405339 .src_decl = block.src_decl,
5340 .namespace = block.namespace,
53415341 .wip_capture_scope = block.wip_capture_scope,
53425342 .instructions = .{},
53435343 .label = &label,
......@@ -5861,7 +5861,7 @@ fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
58615861 .{container_type},
58625862 );
58635863 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {
5864 if (decl.is_pub or decl.namespace.file_scope == block.base.namespace().file_scope) {
5864 if (decl.is_pub or decl.getFileScope() == block.base.namespace().file_scope) {
58655865 return Air.Inst.Ref.bool_true;
58665866 }
58675867 }
......@@ -9603,8 +9603,9 @@ fn addSafetyCheck(
96039603 var fail_block: Scope.Block = .{
96049604 .parent = parent_block,
96059605 .sema = sema,
9606 .wip_capture_scope = parent_block.wip_capture_scope,
96079606 .src_decl = parent_block.src_decl,
9607 .namespace = parent_block.namespace,
9608 .wip_capture_scope = parent_block.wip_capture_scope,
96089609 .instructions = .{},
96099610 .inlining = parent_block.inlining,
96109611 .is_comptime = parent_block.is_comptime,
......@@ -10259,7 +10260,7 @@ fn namespaceLookup(
1025910260 const mod = sema.mod;
1026010261 const gpa = sema.gpa;
1026110262 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {
10262 if (!decl.is_pub and decl.namespace.file_scope != block.getFileScope()) {
10263 if (!decl.is_pub and decl.getFileScope() != block.getFileScope()) {
1026310264 const msg = msg: {
1026410265 const msg = try mod.errMsg(&block.base, src, "'{s}' is not marked 'pub'", .{
1026510266 decl_name,
......@@ -11911,6 +11912,7 @@ fn semaStructFields(
1191111912 .parent = null,
1191211913 .sema = &sema,
1191311914 .src_decl = decl,
11915 .namespace = &struct_obj.namespace,
1191411916 .wip_capture_scope = wip_captures.scope,
1191511917 .instructions = .{},
1191611918 .inlining = null,
......@@ -12080,6 +12082,7 @@ fn semaUnionFields(
1208012082 .parent = null,
1208112083 .sema = &sema,
1208212084 .src_decl = decl,
12085 .namespace = &union_obj.namespace,
1208312086 .wip_capture_scope = wip_captures.scope,
1208412087 .instructions = .{},
1208512088 .inlining = null,
......@@ -12290,7 +12293,7 @@ fn getBuiltin(
1229012293 const opt_builtin_inst = try sema.namespaceLookupRef(
1229112294 block,
1229212295 src,
12293 std_file.root_decl.?.namespace,
12296 std_file.root_decl.?.src_namespace,
1229412297 "builtin",
1229512298 );
1229612299 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst.?, src);
......@@ -12484,7 +12487,7 @@ fn typeHasOnePossibleValue(
1248412487}
1248512488
1248612489fn getAstTree(sema: *Sema, block: *Scope.Block) CompileError!*const std.zig.Ast {
12487 return block.src_decl.namespace.file_scope.getTree(sema.gpa) catch |err| {
12490 return block.namespace.file_scope.getTree(sema.gpa) catch |err| {
1248812491 log.err("unable to load AST to report compile error: {s}", .{@errorName(err)});
1248912492 return error.AnalysisFail;
1249012493 };
src/codegen.zig+2-2
......@@ -2584,7 +2584,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
25842584
25852585 fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue) !void {
25862586 const ty_str = self.air.instructions.items(.data)[inst].ty_str;
2587 const zir = &self.mod_fn.owner_decl.namespace.file_scope.zir;
2587 const zir = &self.mod_fn.owner_decl.getFileScope().zir;
25882588 const name = zir.nullTerminatedString(ty_str.str);
25892589 const name_with_null = name.ptr[0 .. name.len + 1];
25902590 const ty = self.air.getRefType(ty_str.ty);
......@@ -3834,7 +3834,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
38343834 fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
38353835 const air_datas = self.air.instructions.items(.data);
38363836 const air_extra = self.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);
3837 const zir = self.mod_fn.owner_decl.namespace.file_scope.zir;
3837 const zir = self.mod_fn.owner_decl.getFileScope().zir;
38383838 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
38393839 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
38403840 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/c.zig+1-1
......@@ -1696,7 +1696,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
16961696fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
16971697 const air_datas = f.air.instructions.items(.data);
16981698 const air_extra = f.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);
1699 const zir = f.object.dg.decl.namespace.file_scope.zir;
1699 const zir = f.object.dg.decl.getFileScope().zir;
17001700 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
17011701 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
17021702 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/llvm.zig+1-1
......@@ -1819,7 +1819,7 @@ pub const FuncGen = struct {
18191819
18201820 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
18211821 const air_asm = self.air.extraData(Air.Asm, ty_pl.payload);
1822 const zir = self.dg.decl.namespace.file_scope.zir;
1822 const zir = self.dg.decl.getFileScope().zir;
18231823 const extended = zir.instructions.items(.data)[air_asm.data.zir_index].extended;
18241824 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
18251825 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/spirv.zig+1-1
......@@ -139,7 +139,7 @@ pub const SPIRVModule = struct {
139139 }
140140
141141 fn resolveSourceFileName(self: *SPIRVModule, decl: *Decl) !ResultId {
142 const path = decl.namespace.file_scope.sub_file_path;
142 const path = decl.getFileScope().sub_file_path;
143143 const result = try self.file_names.getOrPut(path);
144144 if (!result.found_existing) {
145145 result.value_ptr.* = self.allocResultId();
src/crash_report.zig+3-3
......@@ -97,7 +97,7 @@ fn dumpStatusReport() !void {
9797 allocator,
9898 anal.body,
9999 anal.body_index,
100 block.src_decl.getFileScope(),
100 block.namespace.file_scope,
101101 block.src_decl.src_node,
102102 6, // indent
103103 stderr,
......@@ -106,7 +106,7 @@ fn dumpStatusReport() !void {
106106 else => |e| return e,
107107 };
108108 try stderr.writeAll(" For full context, use the command\n zig ast-check -t ");
109 try writeFilePath(block.src_decl.getFileScope(), stderr);
109 try writeFilePath(block.namespace.file_scope, stderr);
110110 try stderr.writeAll("\n\n");
111111
112112 var parent = anal.parent;
......@@ -118,7 +118,7 @@ fn dumpStatusReport() !void {
118118 print_zir.renderSingleInstruction(
119119 allocator,
120120 curr.body[curr.body_index],
121 curr.block.src_decl.getFileScope(),
121 curr.block.namespace.file_scope,
122122 curr.block.src_decl.src_node,
123123 6, // indent
124124 stderr,
src/link/Plan9.zig+3-3
......@@ -163,11 +163,11 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Plan9 {
163163
164164fn putFn(self: *Plan9, decl: *Module.Decl, out: FnDeclOutput) !void {
165165 const gpa = self.base.allocator;
166 const fn_map_res = try self.fn_decl_table.getOrPut(gpa, decl.namespace.file_scope);
166 const fn_map_res = try self.fn_decl_table.getOrPut(gpa, decl.getFileScope());
167167 if (fn_map_res.found_existing) {
168168 try fn_map_res.value_ptr.functions.put(gpa, decl, out);
169169 } else {
170 const file = decl.namespace.file_scope;
170 const file = decl.getFileScope();
171171 const arena = &self.path_arena.allocator;
172172 // each file gets a symbol
173173 fn_map_res.value_ptr.* = .{
......@@ -548,7 +548,7 @@ pub fn freeDecl(self: *Plan9, decl: *Module.Decl) void {
548548 const is_fn = (decl.val.tag() == .function);
549549 if (is_fn) {
550550 var symidx_and_submap =
551 self.fn_decl_table.get(decl.namespace.file_scope).?;
551 self.fn_decl_table.get(decl.getFileScope()).?;
552552 var submap = symidx_and_submap.functions;
553553 _ = submap.swapRemove(decl);
554554 if (submap.count() == 0) {