authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-09-30 13:34:12-05:00
committergravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-10-02 15:21:48-05:00
log269e54877051791790f5dffc4d4f1476834e4e43
treef1a050defea8bff3d8f39934fdca4111afb1af0f
parent4916e26be434309209585a7c8a7918ed58c79466

Fix namespace references for deeply nested structs


2 files changed, 21 insertions(+), 39 deletions(-)

src/Module.zig+7-12
...@@ -1107,7 +1107,7 @@ pub const Scope = struct {...@@ -1107,7 +1107,7 @@ pub const Scope = struct {
1107 /// Asserts the scope has a parent which is a Namespace and returns it.1107 /// Asserts the scope has a parent which is a Namespace and returns it.
1108 pub fn namespace(scope: *Scope) *Namespace {1108 pub fn namespace(scope: *Scope) *Namespace {
1109 switch (scope.tag) {1109 switch (scope.tag) {
1110 .block => return scope.cast(Block).?.sema.owner_decl.namespace,1110 .block => return scope.cast(Block).?.src_decl.namespace,
1111 .file => return scope.cast(File).?.root_decl.?.namespace,1111 .file => return scope.cast(File).?.root_decl.?.namespace,
1112 .namespace => return scope.cast(Namespace).?,1112 .namespace => return scope.cast(Namespace).?,
1113 }1113 }
...@@ -3244,11 +3244,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {...@@ -3244,11 +3244,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
3244 .file_scope = file,3244 .file_scope = file,
3245 },3245 },
3246 };3246 };
3247 const new_decl = try mod.allocateNewDecl(&struct_obj.namespace, 0, null);3247 const decl_name = try file.fullyQualifiedNameZ(gpa);
3248 const new_decl = try mod.allocateNewDecl(decl_name, &struct_obj.namespace, 0, null);
3248 file.root_decl = new_decl;3249 file.root_decl = new_decl;
3249 struct_obj.owner_decl = new_decl;3250 struct_obj.owner_decl = new_decl;
3250 new_decl.src_line = 0;3251 new_decl.src_line = 0;
3251 new_decl.name = try file.fullyQualifiedNameZ(gpa);
3252 new_decl.is_pub = true;3252 new_decl.is_pub = true;
3253 new_decl.is_exported = false;3253 new_decl.is_exported = false;
3254 new_decl.has_align = false;3254 new_decl.has_align = false;
...@@ -3276,7 +3276,6 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {...@@ -3276,7 +3276,6 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
3276 .perm_arena = &new_decl_arena.allocator,3276 .perm_arena = &new_decl_arena.allocator,
3277 .code = file.zir,3277 .code = file.zir,
3278 .owner_decl = new_decl,3278 .owner_decl = new_decl,
3279 .namespace = &struct_obj.namespace,
3280 .func = null,3279 .func = null,
3281 .fn_ret_ty = Type.initTag(.void),3280 .fn_ret_ty = Type.initTag(.void),
3282 .owner_func = null,3281 .owner_func = null,
...@@ -3342,7 +3341,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3342,7 +3341,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3342 .perm_arena = &decl_arena.allocator,3341 .perm_arena = &decl_arena.allocator,
3343 .code = zir,3342 .code = zir,
3344 .owner_decl = decl,3343 .owner_decl = decl,
3345 .namespace = decl.namespace,
3346 .func = null,3344 .func = null,
3347 .fn_ret_ty = Type.initTag(.void),3345 .fn_ret_ty = Type.initTag(.void),
3348 .owner_func = null,3346 .owner_func = null,
...@@ -3818,13 +3816,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi...@@ -3818,13 +3816,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
3818 // We create a Decl for it regardless of analysis status.3816 // We create a Decl for it regardless of analysis status.
3819 const gop = try namespace.decls.getOrPut(gpa, decl_name);3817 const gop = try namespace.decls.getOrPut(gpa, decl_name);
3820 if (!gop.found_existing) {3818 if (!gop.found_existing) {
3821 const new_decl = try mod.allocateNewDecl(namespace, decl_node, iter.parent_decl.src_scope);3819 const new_decl = try mod.allocateNewDecl(decl_name, namespace, decl_node, iter.parent_decl.src_scope);
3822 if (is_usingnamespace) {3820 if (is_usingnamespace) {
3823 namespace.usingnamespace_set.putAssumeCapacity(new_decl, is_pub);3821 namespace.usingnamespace_set.putAssumeCapacity(new_decl, is_pub);
3824 }3822 }
3825 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });3823 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
3826 new_decl.src_line = line;3824 new_decl.src_line = line;
3827 new_decl.name = decl_name;
3828 gop.value_ptr.* = new_decl;3825 gop.value_ptr.* = new_decl;
3829 // Exported decls, comptime decls, usingnamespace decls, and3826 // Exported decls, comptime decls, usingnamespace decls, and
3830 // test decls if in test mode, get analyzed.3827 // test decls if in test mode, get analyzed.
...@@ -4089,7 +4086,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se...@@ -4089,7 +4086,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
4089 .perm_arena = &decl_arena.allocator,4086 .perm_arena = &decl_arena.allocator,
4090 .code = decl.namespace.file_scope.zir,4087 .code = decl.namespace.file_scope.zir,
4091 .owner_decl = decl,4088 .owner_decl = decl,
4092 .namespace = decl.namespace,
4093 .func = func,4089 .func = func,
4094 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),4090 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),
4095 .owner_func = func,4091 .owner_func = func,
...@@ -4226,7 +4222,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {...@@ -4226,7 +4222,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {
4226 decl.analysis = .outdated;4222 decl.analysis = .outdated;
4227}4223}
42284224
4229pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.Node.Index, src_scope: ?*CaptureScope) !*Decl {4225pub fn allocateNewDecl(mod: *Module, name: [:0]const u8, namespace: *Scope.Namespace, src_node: Ast.Node.Index, src_scope: ?*CaptureScope) !*Decl {
4230 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.4226 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.
4231 const new_decl: *Decl = if (mod.emit_h != null) blk: {4227 const new_decl: *Decl = if (mod.emit_h != null) blk: {
4232 const parent_struct = try mod.gpa.create(DeclPlusEmitH);4228 const parent_struct = try mod.gpa.create(DeclPlusEmitH);
...@@ -4238,7 +4234,7 @@ pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast....@@ -4238,7 +4234,7 @@ pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.
4238 } else try mod.gpa.create(Decl);4234 } else try mod.gpa.create(Decl);
42394235
4240 new_decl.* = .{4236 new_decl.* = .{
4241 .name = "",4237 .name = name,
4242 .namespace = namespace,4238 .namespace = namespace,
4243 .src_node = src_node,4239 .src_node = src_node,
4244 .src_line = undefined,4240 .src_line = undefined,
...@@ -4414,9 +4410,8 @@ pub fn createAnonymousDeclFromDeclNamed(...@@ -4414,9 +4410,8 @@ pub fn createAnonymousDeclFromDeclNamed(
4414 const namespace = src_decl.namespace;4410 const namespace = src_decl.namespace;
4415 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);4411 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);
44164412
4417 const new_decl = try mod.allocateNewDecl(namespace, src_decl.src_node, src_scope);4413 const new_decl = try mod.allocateNewDecl(name, namespace, src_decl.src_node, src_scope);
44184414
4419 new_decl.name = name;
4420 new_decl.src_line = src_decl.src_line;4415 new_decl.src_line = src_decl.src_line;
4421 new_decl.ty = typed_value.ty;4416 new_decl.ty = typed_value.ty;
4422 new_decl.val = typed_value.val;4417 new_decl.val = typed_value.val;
src/Sema.zig+14-27
...@@ -24,8 +24,6 @@ inst_map: InstMap = .{},...@@ -24,8 +24,6 @@ inst_map: InstMap = .{},
24/// and `src_decl` of `Scope.Block` is the `Decl` of the callee.24/// and `src_decl` of `Scope.Block` is the `Decl` of the callee.
25/// This `Decl` owns the arena memory of this `Sema`.25/// This `Decl` owns the arena memory of this `Sema`.
26owner_decl: *Decl,26owner_decl: *Decl,
27/// How to look up decl names.
28namespace: *Scope.Namespace,
29/// For an inline or comptime function call, this will be the root parent function27/// For an inline or comptime function call, this will be the root parent function
30/// which contains the callsite. Corresponds to `owner_decl`.28/// which contains the callsite. Corresponds to `owner_decl`.
31owner_func: ?*Module.Fn,29owner_func: ?*Module.Fn,
...@@ -1048,6 +1046,8 @@ pub fn analyzeStructDecl(...@@ -1048,6 +1046,8 @@ pub fn analyzeStructDecl(
1048 } else 0;1046 } else 0;
10491047
1050 _ = try sema.mod.scanNamespace(&struct_obj.namespace, extra_index, decls_len, new_decl);1048 _ = try sema.mod.scanNamespace(&struct_obj.namespace, extra_index, decls_len, new_decl);
1049
1050 new_decl.namespace = &struct_obj.namespace;
1051}1051}
10521052
1053fn zirStructDecl(1053fn zirStructDecl(
...@@ -1084,7 +1084,7 @@ fn zirStructDecl(...@@ -1084,7 +1084,7 @@ fn zirStructDecl(
1084 .status = .none,1084 .status = .none,
1085 .known_has_bits = undefined,1085 .known_has_bits = undefined,
1086 .namespace = .{1086 .namespace = .{
1087 .parent = sema.owner_decl.namespace,1087 .parent = block.src_decl.namespace,
1088 .ty = struct_ty,1088 .ty = struct_ty,
1089 .file_scope = block.getFileScope(),1089 .file_scope = block.getFileScope(),
1090 },1090 },
...@@ -1194,7 +1194,7 @@ fn zirEnumDecl(...@@ -1194,7 +1194,7 @@ fn zirEnumDecl(
1194 .values = .{},1194 .values = .{},
1195 .node_offset = src.node_offset,1195 .node_offset = src.node_offset,
1196 .namespace = .{1196 .namespace = .{
1197 .parent = sema.owner_decl.namespace,1197 .parent = block.src_decl.namespace,
1198 .ty = enum_ty,1198 .ty = enum_ty,
1199 .file_scope = block.getFileScope(),1199 .file_scope = block.getFileScope(),
1200 },1200 },
...@@ -1205,6 +1205,8 @@ fn zirEnumDecl(...@@ -1205,6 +1205,8 @@ fn zirEnumDecl(
12051205
1206 extra_index = try mod.scanNamespace(&enum_obj.namespace, extra_index, decls_len, new_decl);1206 extra_index = try mod.scanNamespace(&enum_obj.namespace, extra_index, decls_len, new_decl);
12071207
1208 new_decl.namespace = &enum_obj.namespace;
1209
1208 const body = sema.code.extra[extra_index..][0..body_len];1210 const body = sema.code.extra[extra_index..][0..body_len];
1209 if (fields_len == 0) {1211 if (fields_len == 0) {
1210 assert(body.len == 0);1212 assert(body.len == 0);
...@@ -1227,10 +1229,6 @@ fn zirEnumDecl(...@@ -1227,10 +1229,6 @@ fn zirEnumDecl(
1227 sema.owner_decl = new_decl;1229 sema.owner_decl = new_decl;
1228 defer sema.owner_decl = prev_owner_decl;1230 defer sema.owner_decl = prev_owner_decl;
12291231
1230 const prev_namespace = sema.namespace;
1231 sema.namespace = &enum_obj.namespace;
1232 defer sema.namespace = prev_namespace;
1233
1234 const prev_owner_func = sema.owner_func;1232 const prev_owner_func = sema.owner_func;
1235 sema.owner_func = null;1233 sema.owner_func = null;
1236 defer sema.owner_func = prev_owner_func;1234 defer sema.owner_func = prev_owner_func;
...@@ -1385,7 +1383,7 @@ fn zirUnionDecl(...@@ -1385,7 +1383,7 @@ fn zirUnionDecl(
1385 .layout = small.layout,1383 .layout = small.layout,
1386 .status = .none,1384 .status = .none,
1387 .namespace = .{1385 .namespace = .{
1388 .parent = sema.owner_decl.namespace,1386 .parent = block.src_decl.namespace,
1389 .ty = union_ty,1387 .ty = union_ty,
1390 .file_scope = block.getFileScope(),1388 .file_scope = block.getFileScope(),
1391 },1389 },
...@@ -1396,6 +1394,8 @@ fn zirUnionDecl(...@@ -1396,6 +1394,8 @@ fn zirUnionDecl(
13961394
1397 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);1395 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);
13981396
1397 new_decl.namespace = &union_obj.namespace;
1398
1399 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);1399 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
1400 try new_decl.finalizeNewArena(&new_decl_arena);1400 try new_decl.finalizeNewArena(&new_decl_arena);
1401 sema.types_pending_resolution.appendAssumeCapacity(union_ty);1401 sema.types_pending_resolution.appendAssumeCapacity(union_ty);
...@@ -2692,7 +2692,7 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -2692,7 +2692,7 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
2692}2692}
26932693
2694fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {2694fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {
2695 var namespace = sema.namespace;2695 var namespace = block.src_decl.namespace;
2696 while (true) {2696 while (true) {
2697 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {2697 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {
2698 return decl;2698 return decl;
...@@ -3010,10 +3010,6 @@ fn analyzeCall(...@@ -3010,10 +3010,6 @@ fn analyzeCall(
3010 sema.inst_map = parent_inst_map;3010 sema.inst_map = parent_inst_map;
3011 }3011 }
30123012
3013 const parent_namespace = sema.namespace;
3014 sema.namespace = module_fn.owner_decl.namespace;
3015 defer sema.namespace = parent_namespace;
3016
3017 const parent_func = sema.func;3013 const parent_func = sema.func;
3018 sema.func = module_fn;3014 sema.func = module_fn;
3019 defer sema.func = parent_func;3015 defer sema.func = parent_func;
...@@ -3276,12 +3272,12 @@ fn analyzeCall(...@@ -3276,12 +3272,12 @@ fn analyzeCall(
32763272
3277 // Create a Decl for the new function.3273 // Create a Decl for the new function.
3278 const src_decl = namespace.getDecl();3274 const src_decl = namespace.getDecl();
3279 const new_decl = try mod.allocateNewDecl(namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
3280 // TODO better names for generic function instantiations3275 // TODO better names for generic function instantiations
3281 const name_index = mod.getNextAnonNameIndex();3276 const name_index = mod.getNextAnonNameIndex();
3282 new_decl.name = try std.fmt.allocPrintZ(gpa, "{s}__anon_{d}", .{3277 const decl_name = try std.fmt.allocPrintZ(gpa, "{s}__anon_{d}", .{
3283 module_fn.owner_decl.name, name_index,3278 module_fn.owner_decl.name, name_index,
3284 });3279 });
3280 const new_decl = try mod.allocateNewDecl(decl_name, namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
3285 new_decl.src_line = module_fn.owner_decl.src_line;3281 new_decl.src_line = module_fn.owner_decl.src_line;
3286 new_decl.is_pub = module_fn.owner_decl.is_pub;3282 new_decl.is_pub = module_fn.owner_decl.is_pub;
3287 new_decl.is_exported = module_fn.owner_decl.is_exported;3283 new_decl.is_exported = module_fn.owner_decl.is_exported;
...@@ -3311,7 +3307,6 @@ fn analyzeCall(...@@ -3311,7 +3307,6 @@ fn analyzeCall(
3311 .perm_arena = &new_decl_arena.allocator,3307 .perm_arena = &new_decl_arena.allocator,
3312 .code = fn_zir,3308 .code = fn_zir,
3313 .owner_decl = new_decl,3309 .owner_decl = new_decl,
3314 .namespace = namespace,
3315 .func = null,3310 .func = null,
3316 .fn_ret_ty = Type.initTag(.void),3311 .fn_ret_ty = Type.initTag(.void),
3317 .owner_func = null,3312 .owner_func = null,
...@@ -11809,7 +11804,7 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:...@@ -11809,7 +11804,7 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
11809 switch (ty.tag()) {11804 switch (ty.tag()) {
11810 .@"struct" => {11805 .@"struct" => {
11811 const struct_obj = ty.castTag(.@"struct").?.data;11806 const struct_obj = ty.castTag(.@"struct").?.data;
11812 if (struct_obj.owner_decl.namespace != sema.owner_decl.namespace) return;11807 if (struct_obj.owner_decl.namespace.parent != sema.owner_decl.namespace) return;
11813 switch (struct_obj.status) {11808 switch (struct_obj.status) {
11814 .none => {},11809 .none => {},
11815 .field_types_wip => {11810 .field_types_wip => {
...@@ -11817,10 +11812,6 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:...@@ -11817,10 +11812,6 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
11817 },11812 },
11818 .have_field_types, .have_layout, .layout_wip => return,11813 .have_field_types, .have_layout, .layout_wip => return,
11819 }11814 }
11820 const prev_namespace = sema.namespace;
11821 sema.namespace = &struct_obj.namespace;
11822 defer sema.namespace = prev_namespace;
11823
11824 const old_src = block.src_decl;11815 const old_src = block.src_decl;
11825 defer block.src_decl = old_src;11816 defer block.src_decl = old_src;
11826 block.src_decl = struct_obj.owner_decl;11817 block.src_decl = struct_obj.owner_decl;
...@@ -11831,7 +11822,7 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:...@@ -11831,7 +11822,7 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
11831 },11822 },
11832 .@"union", .union_tagged => {11823 .@"union", .union_tagged => {
11833 const union_obj = ty.cast(Type.Payload.Union).?.data;11824 const union_obj = ty.cast(Type.Payload.Union).?.data;
11834 if (union_obj.owner_decl.namespace != sema.owner_decl.namespace) return;11825 if (union_obj.owner_decl.namespace.parent != sema.owner_decl.namespace) return;
11835 switch (union_obj.status) {11826 switch (union_obj.status) {
11836 .none => {},11827 .none => {},
11837 .field_types_wip => {11828 .field_types_wip => {
...@@ -11839,10 +11830,6 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:...@@ -11839,10 +11830,6 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
11839 },11830 },
11840 .have_field_types, .have_layout, .layout_wip => return,11831 .have_field_types, .have_layout, .layout_wip => return,
11841 }11832 }
11842 const prev_namespace = sema.namespace;
11843 sema.namespace = &union_obj.namespace;
11844 defer sema.namespace = prev_namespace;
11845
11846 const old_src = block.src_decl;11833 const old_src = block.src_decl;
11847 defer block.src_decl = old_src;11834 defer block.src_decl = old_src;
11848 block.src_decl = union_obj.owner_decl;11835 block.src_decl = union_obj.owner_decl;