authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-08 20:37:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-08 20:37:19-07:00
log61b868f9a5345ab1dba3395107c7cdee3fd2989e
treebddaf5710f76aaf49edbee3251d1dd6d13995ccb
parent482b995a4963e045860c7fb1a4e11c48cf4de880

stage2: simplify Decl src_node field

Also fix "previous definition here" error notes to be correct.

6 files changed, 68 insertions(+), 103 deletions(-)

src/Module.zig+44-86
...@@ -150,9 +150,15 @@ pub const Decl = struct {...@@ -150,9 +150,15 @@ pub const Decl = struct {
150 /// The direct parent container of the Decl.150 /// The direct parent container of the Decl.
151 /// Reference to externally owned memory.151 /// Reference to externally owned memory.
152 container: *Scope.Container,152 container: *Scope.Container,
153 /// The AST Node decl index or ZIR Inst index that contains this declaration.153
154 /// An integer that can be checked against the corresponding incrementing
155 /// generation field of Module. This is used to determine whether `complete` status
156 /// represents pre- or post- re-analysis.
157 generation: u32,
158 /// The AST Node index or ZIR Inst index that contains this declaration.
154 /// Must be recomputed when the corresponding source file is modified.159 /// Must be recomputed when the corresponding source file is modified.
155 src_index: usize,160 src_node: ast.Node.Index,
161
156 /// The most recent value of the Decl after a successful semantic analysis.162 /// The most recent value of the Decl after a successful semantic analysis.
157 typed_value: union(enum) {163 typed_value: union(enum) {
158 never_succeeded: void,164 never_succeeded: void,
...@@ -198,11 +204,6 @@ pub const Decl = struct {...@@ -198,11 +204,6 @@ pub const Decl = struct {
198 /// Whether the corresponding AST decl has a `pub` keyword.204 /// Whether the corresponding AST decl has a `pub` keyword.
199 is_pub: bool,205 is_pub: bool,
200206
201 /// An integer that can be checked against the corresponding incrementing
202 /// generation field of Module. This is used to determine whether `complete` status
203 /// represents pre- or post- re-analysis.
204 generation: u32,
205
206 /// Represents the position of the code in the output file.207 /// Represents the position of the code in the output file.
207 /// This is populated regardless of semantic analysis and code generation.208 /// This is populated regardless of semantic analysis and code generation.
208 link: link.File.LinkBlock,209 link: link.File.LinkBlock,
...@@ -249,11 +250,11 @@ pub const Decl = struct {...@@ -249,11 +250,11 @@ pub const Decl = struct {
249 }250 }
250251
251 pub fn relativeToNodeIndex(decl: Decl, offset: i32) ast.Node.Index {252 pub fn relativeToNodeIndex(decl: Decl, offset: i32) ast.Node.Index {
252 return @bitCast(ast.Node.Index, offset + @bitCast(i32, decl.srcNode()));253 return @bitCast(ast.Node.Index, offset + @bitCast(i32, decl.src_node));
253 }254 }
254255
255 pub fn nodeIndexToRelative(decl: Decl, node_index: ast.Node.Index) i32 {256 pub fn nodeIndexToRelative(decl: Decl, node_index: ast.Node.Index) i32 {
256 return @bitCast(i32, node_index) - @bitCast(i32, decl.srcNode());257 return @bitCast(i32, node_index) - @bitCast(i32, decl.src_node);
257 }258 }
258259
259 pub fn tokSrcLoc(decl: Decl, token_index: ast.TokenIndex) LazySrcLoc {260 pub fn tokSrcLoc(decl: Decl, token_index: ast.TokenIndex) LazySrcLoc {
...@@ -271,14 +272,9 @@ pub const Decl = struct {...@@ -271,14 +272,9 @@ pub const Decl = struct {
271 };272 };
272 }273 }
273274
274 pub fn srcNode(decl: Decl) u32 {
275 const tree = &decl.container.file_scope.tree;
276 return tree.rootDecls()[decl.src_index];
277 }
278
279 pub fn srcToken(decl: Decl) u32 {275 pub fn srcToken(decl: Decl) u32 {
280 const tree = &decl.container.file_scope.tree;276 const tree = &decl.container.file_scope.tree;
281 return tree.firstToken(decl.srcNode());277 return tree.firstToken(decl.src_node);
282 }278 }
283279
284 pub fn srcByteOffset(decl: Decl) u32 {280 pub fn srcByteOffset(decl: Decl) u32 {
...@@ -2458,7 +2454,7 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {...@@ -2458,7 +2454,7 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
2458 const tree = try mod.getAstTree(decl.container.file_scope);2454 const tree = try mod.getAstTree(decl.container.file_scope);
2459 const node_tags = tree.nodes.items(.tag);2455 const node_tags = tree.nodes.items(.tag);
2460 const node_datas = tree.nodes.items(.data);2456 const node_datas = tree.nodes.items(.data);
2461 const decl_node = tree.rootDecls()[decl.src_index];2457 const decl_node = decl.src_node;
2462 switch (node_tags[decl_node]) {2458 switch (node_tags[decl_node]) {
2463 .fn_decl => {2459 .fn_decl => {
2464 const fn_proto = node_datas[decl_node].lhs;2460 const fn_proto = node_datas[decl_node].lhs;
...@@ -3292,7 +3288,7 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3292,7 +3288,7 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3292 var outdated_decls = std.AutoArrayHashMap(*Decl, void).init(mod.gpa);3288 var outdated_decls = std.AutoArrayHashMap(*Decl, void).init(mod.gpa);
3293 defer outdated_decls.deinit();3289 defer outdated_decls.deinit();
32943290
3295 for (decls) |decl_node, decl_i| switch (node_tags[decl_node]) {3291 for (decls) |decl_node| switch (node_tags[decl_node]) {
3296 .fn_decl => {3292 .fn_decl => {
3297 const fn_proto = node_datas[decl_node].lhs;3293 const fn_proto = node_datas[decl_node].lhs;
3298 const body = node_datas[decl_node].rhs;3294 const body = node_datas[decl_node].rhs;
...@@ -3304,7 +3300,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3304,7 +3300,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3304 &deleted_decls,3300 &deleted_decls,
3305 &outdated_decls,3301 &outdated_decls,
3306 decl_node,3302 decl_node,
3307 decl_i,
3308 tree.*,3303 tree.*,
3309 body,3304 body,
3310 tree.fnProtoSimple(&params, fn_proto),3305 tree.fnProtoSimple(&params, fn_proto),
...@@ -3315,7 +3310,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3315,7 +3310,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3315 &deleted_decls,3310 &deleted_decls,
3316 &outdated_decls,3311 &outdated_decls,
3317 decl_node,3312 decl_node,
3318 decl_i,
3319 tree.*,3313 tree.*,
3320 body,3314 body,
3321 tree.fnProtoMulti(fn_proto),3315 tree.fnProtoMulti(fn_proto),
...@@ -3327,7 +3321,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3327,7 +3321,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3327 &deleted_decls,3321 &deleted_decls,
3328 &outdated_decls,3322 &outdated_decls,
3329 decl_node,3323 decl_node,
3330 decl_i,
3331 tree.*,3324 tree.*,
3332 body,3325 body,
3333 tree.fnProtoOne(&params, fn_proto),3326 tree.fnProtoOne(&params, fn_proto),
...@@ -3338,7 +3331,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3338,7 +3331,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3338 &deleted_decls,3331 &deleted_decls,
3339 &outdated_decls,3332 &outdated_decls,
3340 decl_node,3333 decl_node,
3341 decl_i,
3342 tree.*,3334 tree.*,
3343 body,3335 body,
3344 tree.fnProto(fn_proto),3336 tree.fnProto(fn_proto),
...@@ -3353,7 +3345,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3353,7 +3345,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3353 &deleted_decls,3345 &deleted_decls,
3354 &outdated_decls,3346 &outdated_decls,
3355 decl_node,3347 decl_node,
3356 decl_i,
3357 tree.*,3348 tree.*,
3358 0,3349 0,
3359 tree.fnProtoSimple(&params, decl_node),3350 tree.fnProtoSimple(&params, decl_node),
...@@ -3364,7 +3355,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3364,7 +3355,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3364 &deleted_decls,3355 &deleted_decls,
3365 &outdated_decls,3356 &outdated_decls,
3366 decl_node,3357 decl_node,
3367 decl_i,
3368 tree.*,3358 tree.*,
3369 0,3359 0,
3370 tree.fnProtoMulti(decl_node),3360 tree.fnProtoMulti(decl_node),
...@@ -3376,7 +3366,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3376,7 +3366,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3376 &deleted_decls,3366 &deleted_decls,
3377 &outdated_decls,3367 &outdated_decls,
3378 decl_node,3368 decl_node,
3379 decl_i,
3380 tree.*,3369 tree.*,
3381 0,3370 0,
3382 tree.fnProtoOne(&params, decl_node),3371 tree.fnProtoOne(&params, decl_node),
...@@ -3387,7 +3376,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3387,7 +3376,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3387 &deleted_decls,3376 &deleted_decls,
3388 &outdated_decls,3377 &outdated_decls,
3389 decl_node,3378 decl_node,
3390 decl_i,
3391 tree.*,3379 tree.*,
3392 0,3380 0,
3393 tree.fnProto(decl_node),3381 tree.fnProto(decl_node),
...@@ -3398,7 +3386,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3398,7 +3386,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3398 &deleted_decls,3386 &deleted_decls,
3399 &outdated_decls,3387 &outdated_decls,
3400 decl_node,3388 decl_node,
3401 decl_i,
3402 tree.*,3389 tree.*,
3403 tree.globalVarDecl(decl_node),3390 tree.globalVarDecl(decl_node),
3404 ),3391 ),
...@@ -3407,7 +3394,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3407,7 +3394,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3407 &deleted_decls,3394 &deleted_decls,
3408 &outdated_decls,3395 &outdated_decls,
3409 decl_node,3396 decl_node,
3410 decl_i,
3411 tree.*,3397 tree.*,
3412 tree.localVarDecl(decl_node),3398 tree.localVarDecl(decl_node),
3413 ),3399 ),
...@@ -3416,7 +3402,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3416,7 +3402,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3416 &deleted_decls,3402 &deleted_decls,
3417 &outdated_decls,3403 &outdated_decls,
3418 decl_node,3404 decl_node,
3419 decl_i,
3420 tree.*,3405 tree.*,
3421 tree.simpleVarDecl(decl_node),3406 tree.simpleVarDecl(decl_node),
3422 ),3407 ),
...@@ -3425,7 +3410,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3425,7 +3410,6 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3425 &deleted_decls,3410 &deleted_decls,
3426 &outdated_decls,3411 &outdated_decls,
3427 decl_node,3412 decl_node,
3428 decl_i,
3429 tree.*,3413 tree.*,
3430 tree.alignedVarDecl(decl_node),3414 tree.alignedVarDecl(decl_node),
3431 ),3415 ),
...@@ -3438,35 +3422,16 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {...@@ -3438,35 +3422,16 @@ pub fn analyzeContainer(mod: *Module, container_scope: *Scope.Container) !void {
3438 const name_hash = container_scope.fullyQualifiedNameHash(name);3422 const name_hash = container_scope.fullyQualifiedNameHash(name);
3439 const contents_hash = std.zig.hashSrc(tree.getNodeSource(decl_node));3423 const contents_hash = std.zig.hashSrc(tree.getNodeSource(decl_node));
34403424
3441 const new_decl = try mod.createNewDecl(&container_scope.base, name, decl_i, name_hash, contents_hash);3425 const new_decl = try mod.createNewDecl(&container_scope.base, name, decl_node, name_hash, contents_hash);
3442 container_scope.decls.putAssumeCapacity(new_decl, {});3426 container_scope.decls.putAssumeCapacity(new_decl, {});
3443 mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });3427 mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });
3444 },3428 },
34453429
3446 .container_field_init => try mod.semaContainerField(3430 // Container fields are handled in AstGen.
3447 container_scope,3431 .container_field_init,
3448 &deleted_decls,3432 .container_field_align,
3449 decl_node,3433 .container_field,
3450 decl_i,3434 => continue,
3451 tree.*,
3452 tree.containerFieldInit(decl_node),
3453 ),
3454 .container_field_align => try mod.semaContainerField(
3455 container_scope,
3456 &deleted_decls,
3457 decl_node,
3458 decl_i,
3459 tree.*,
3460 tree.containerFieldAlign(decl_node),
3461 ),
3462 .container_field => try mod.semaContainerField(
3463 container_scope,
3464 &deleted_decls,
3465 decl_node,
3466 decl_i,
3467 tree.*,
3468 tree.containerField(decl_node),
3469 ),
34703435
3471 .test_decl => {3436 .test_decl => {
3472 if (mod.comp.bin_file.options.is_test) {3437 if (mod.comp.bin_file.options.is_test) {
...@@ -3508,7 +3473,6 @@ fn semaContainerFn(...@@ -3508,7 +3473,6 @@ fn semaContainerFn(
3508 deleted_decls: *std.AutoArrayHashMap(*Decl, void),3473 deleted_decls: *std.AutoArrayHashMap(*Decl, void),
3509 outdated_decls: *std.AutoArrayHashMap(*Decl, void),3474 outdated_decls: *std.AutoArrayHashMap(*Decl, void),
3510 decl_node: ast.Node.Index,3475 decl_node: ast.Node.Index,
3511 decl_i: usize,
3512 tree: ast.Tree,3476 tree: ast.Tree,
3513 body_node: ast.Node.Index,3477 body_node: ast.Node.Index,
3514 fn_proto: ast.full.FnProto,3478 fn_proto: ast.full.FnProto,
...@@ -3517,25 +3481,30 @@ fn semaContainerFn(...@@ -3517,25 +3481,30 @@ fn semaContainerFn(
3517 defer tracy.end();3481 defer tracy.end();
35183482
3519 // We will create a Decl for it regardless of analysis status.3483 // We will create a Decl for it regardless of analysis status.
3520 const name_tok = fn_proto.name_token orelse {3484 const name_token = fn_proto.name_token orelse {
3521 // This problem will go away with #1717.3485 // This problem will go away with #1717.
3522 @panic("TODO missing function name");3486 @panic("TODO missing function name");
3523 };3487 };
3524 const name = tree.tokenSlice(name_tok); // TODO use identifierTokenString3488 const name = tree.tokenSlice(name_token); // TODO use identifierTokenString
3525 const name_hash = container_scope.fullyQualifiedNameHash(name);3489 const name_hash = container_scope.fullyQualifiedNameHash(name);
3526 const contents_hash = std.zig.hashSrc(tree.getNodeSource(decl_node));3490 const contents_hash = std.zig.hashSrc(tree.getNodeSource(decl_node));
3527 if (mod.decl_table.get(name_hash)) |decl| {3491 if (mod.decl_table.get(name_hash)) |decl| {
3528 // Update the AST Node index of the decl, even if its contents are unchanged, it may3492 // Update the AST Node index of the decl, even if its contents are unchanged, it may
3529 // have been re-ordered.3493 // have been re-ordered.
3530 decl.src_index = decl_i;3494 const prev_src_node = decl.src_node;
3495 decl.src_node = decl_node;
3531 if (deleted_decls.swapRemove(decl) == null) {3496 if (deleted_decls.swapRemove(decl) == null) {
3532 decl.analysis = .sema_failure;3497 decl.analysis = .sema_failure;
3533 const msg = try ErrorMsg.create(mod.gpa, .{3498 const msg = try ErrorMsg.create(mod.gpa, .{
3534 .container = .{ .file_scope = container_scope.file_scope },3499 .container = .{ .file_scope = container_scope.file_scope },
3535 .lazy = .{ .token_abs = name_tok },3500 .lazy = .{ .token_abs = name_token },
3536 }, "redefinition of '{s}'", .{decl.name});3501 }, "redefinition of '{s}'", .{decl.name});
3537 errdefer msg.destroy(mod.gpa);3502 errdefer msg.destroy(mod.gpa);
3538 try mod.errNoteNonLazy(decl.srcLoc(), msg, "previous definition here", .{});3503 const other_src_loc: SrcLoc = .{
3504 .container = .{ .file_scope = decl.container.file_scope },
3505 .lazy = .{ .node_abs = prev_src_node },
3506 };
3507 try mod.errNoteNonLazy(other_src_loc, msg, "previous definition here", .{});
3539 try mod.failed_decls.putNoClobber(mod.gpa, decl, msg);3508 try mod.failed_decls.putNoClobber(mod.gpa, decl, msg);
3540 } else {3509 } else {
3541 if (!srcHashEql(decl.contents_hash, contents_hash)) {3510 if (!srcHashEql(decl.contents_hash, contents_hash)) {
...@@ -3559,7 +3528,7 @@ fn semaContainerFn(...@@ -3559,7 +3528,7 @@ fn semaContainerFn(
3559 }3528 }
3560 }3529 }
3561 } else {3530 } else {
3562 const new_decl = try mod.createNewDecl(&container_scope.base, name, decl_i, name_hash, contents_hash);3531 const new_decl = try mod.createNewDecl(&container_scope.base, name, decl_node, name_hash, contents_hash);
3563 container_scope.decls.putAssumeCapacity(new_decl, {});3532 container_scope.decls.putAssumeCapacity(new_decl, {});
3564 if (fn_proto.extern_export_token) |maybe_export_token| {3533 if (fn_proto.extern_export_token) |maybe_export_token| {
3565 const token_tags = tree.tokens.items(.tag);3534 const token_tags = tree.tokens.items(.tag);
...@@ -3576,7 +3545,6 @@ fn semaContainerVar(...@@ -3576,7 +3545,6 @@ fn semaContainerVar(
3576 deleted_decls: *std.AutoArrayHashMap(*Decl, void),3545 deleted_decls: *std.AutoArrayHashMap(*Decl, void),
3577 outdated_decls: *std.AutoArrayHashMap(*Decl, void),3546 outdated_decls: *std.AutoArrayHashMap(*Decl, void),
3578 decl_node: ast.Node.Index,3547 decl_node: ast.Node.Index,
3579 decl_i: usize,
3580 tree: ast.Tree,3548 tree: ast.Tree,
3581 var_decl: ast.full.VarDecl,3549 var_decl: ast.full.VarDecl,
3582) !void {3550) !void {
...@@ -3590,7 +3558,8 @@ fn semaContainerVar(...@@ -3590,7 +3558,8 @@ fn semaContainerVar(
3590 if (mod.decl_table.get(name_hash)) |decl| {3558 if (mod.decl_table.get(name_hash)) |decl| {
3591 // Update the AST Node index of the decl, even if its contents are unchanged, it may3559 // Update the AST Node index of the decl, even if its contents are unchanged, it may
3592 // have been re-ordered.3560 // have been re-ordered.
3593 decl.src_index = decl_i;3561 const prev_src_node = decl.src_node;
3562 decl.src_node = decl_node;
3594 if (deleted_decls.swapRemove(decl) == null) {3563 if (deleted_decls.swapRemove(decl) == null) {
3595 decl.analysis = .sema_failure;3564 decl.analysis = .sema_failure;
3596 const msg = try ErrorMsg.create(mod.gpa, .{3565 const msg = try ErrorMsg.create(mod.gpa, .{
...@@ -3598,14 +3567,18 @@ fn semaContainerVar(...@@ -3598,14 +3567,18 @@ fn semaContainerVar(
3598 .lazy = .{ .token_abs = name_token },3567 .lazy = .{ .token_abs = name_token },
3599 }, "redefinition of '{s}'", .{decl.name});3568 }, "redefinition of '{s}'", .{decl.name});
3600 errdefer msg.destroy(mod.gpa);3569 errdefer msg.destroy(mod.gpa);
3601 try mod.errNoteNonLazy(decl.srcLoc(), msg, "previous definition here", .{});3570 const other_src_loc: SrcLoc = .{
3571 .container = .{ .file_scope = decl.container.file_scope },
3572 .lazy = .{ .node_abs = prev_src_node },
3573 };
3574 try mod.errNoteNonLazy(other_src_loc, msg, "previous definition here", .{});
3602 try mod.failed_decls.putNoClobber(mod.gpa, decl, msg);3575 try mod.failed_decls.putNoClobber(mod.gpa, decl, msg);
3603 } else if (!srcHashEql(decl.contents_hash, contents_hash)) {3576 } else if (!srcHashEql(decl.contents_hash, contents_hash)) {
3604 try outdated_decls.put(decl, {});3577 try outdated_decls.put(decl, {});
3605 decl.contents_hash = contents_hash;3578 decl.contents_hash = contents_hash;
3606 }3579 }
3607 } else {3580 } else {
3608 const new_decl = try mod.createNewDecl(&container_scope.base, name, decl_i, name_hash, contents_hash);3581 const new_decl = try mod.createNewDecl(&container_scope.base, name, decl_node, name_hash, contents_hash);
3609 container_scope.decls.putAssumeCapacity(new_decl, {});3582 container_scope.decls.putAssumeCapacity(new_decl, {});
3610 if (var_decl.extern_export_token) |maybe_export_token| {3583 if (var_decl.extern_export_token) |maybe_export_token| {
3611 const token_tags = tree.tokens.items(.tag);3584 const token_tags = tree.tokens.items(.tag);
...@@ -3616,21 +3589,6 @@ fn semaContainerVar(...@@ -3616,21 +3589,6 @@ fn semaContainerVar(
3616 }3589 }
3617}3590}
36183591
3619fn semaContainerField(
3620 mod: *Module,
3621 container_scope: *Scope.Container,
3622 deleted_decls: *std.AutoArrayHashMap(*Decl, void),
3623 decl_node: ast.Node.Index,
3624 decl_i: usize,
3625 tree: ast.Tree,
3626 field: ast.full.ContainerField,
3627) !void {
3628 const tracy = trace(@src());
3629 defer tracy.end();
3630
3631 log.err("TODO: analyze container field", .{});
3632}
3633
3634pub fn deleteDecl(3592pub fn deleteDecl(
3635 mod: *Module,3593 mod: *Module,
3636 decl: *Decl,3594 decl: *Decl,
...@@ -3813,7 +3771,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {...@@ -3813,7 +3771,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {
3813fn allocateNewDecl(3771fn allocateNewDecl(
3814 mod: *Module,3772 mod: *Module,
3815 scope: *Scope,3773 scope: *Scope,
3816 src_index: usize,3774 src_node: ast.Node.Index,
3817 contents_hash: std.zig.SrcHash,3775 contents_hash: std.zig.SrcHash,
3818) !*Decl {3776) !*Decl {
3819 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.3777 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.
...@@ -3829,7 +3787,7 @@ fn allocateNewDecl(...@@ -3829,7 +3787,7 @@ fn allocateNewDecl(
3829 new_decl.* = .{3787 new_decl.* = .{
3830 .name = "",3788 .name = "",
3831 .container = scope.namespace(),3789 .container = scope.namespace(),
3832 .src_index = src_index,3790 .src_node = src_node,
3833 .typed_value = .{ .never_succeeded = {} },3791 .typed_value = .{ .never_succeeded = {} },
3834 .analysis = .unreferenced,3792 .analysis = .unreferenced,
3835 .deletion_flag = false,3793 .deletion_flag = false,
...@@ -3860,12 +3818,12 @@ fn createNewDecl(...@@ -3860,12 +3818,12 @@ fn createNewDecl(
3860 mod: *Module,3818 mod: *Module,
3861 scope: *Scope,3819 scope: *Scope,
3862 decl_name: []const u8,3820 decl_name: []const u8,
3863 src_index: usize,3821 src_node: ast.Node.Index,
3864 name_hash: Scope.NameHash,3822 name_hash: Scope.NameHash,
3865 contents_hash: std.zig.SrcHash,3823 contents_hash: std.zig.SrcHash,
3866) !*Decl {3824) !*Decl {
3867 try mod.decl_table.ensureCapacity(mod.gpa, mod.decl_table.items().len + 1);3825 try mod.decl_table.ensureCapacity(mod.gpa, mod.decl_table.items().len + 1);
3868 const new_decl = try mod.allocateNewDecl(scope, src_index, contents_hash);3826 const new_decl = try mod.allocateNewDecl(scope, src_node, contents_hash);
3869 errdefer mod.gpa.destroy(new_decl);3827 errdefer mod.gpa.destroy(new_decl);
3870 new_decl.name = try mem.dupeZ(mod.gpa, u8, decl_name);3828 new_decl.name = try mem.dupeZ(mod.gpa, u8, decl_name);
3871 mod.decl_table.putAssumeCapacityNoClobber(name_hash, new_decl);3829 mod.decl_table.putAssumeCapacityNoClobber(name_hash, new_decl);
...@@ -4078,7 +4036,7 @@ pub fn createAnonymousDecl(...@@ -4078,7 +4036,7 @@ pub fn createAnonymousDecl(
4078 defer mod.gpa.free(name);4036 defer mod.gpa.free(name);
4079 const name_hash = scope.namespace().fullyQualifiedNameHash(name);4037 const name_hash = scope.namespace().fullyQualifiedNameHash(name);
4080 const src_hash: std.zig.SrcHash = undefined;4038 const src_hash: std.zig.SrcHash = undefined;
4081 const new_decl = try mod.createNewDecl(scope, name, scope_decl.src_index, name_hash, src_hash);4039 const new_decl = try mod.createNewDecl(scope, name, scope_decl.src_node, name_hash, src_hash);
4082 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);4040 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
40834041
4084 decl_arena_state.* = decl_arena.state;4042 decl_arena_state.* = decl_arena.state;
...@@ -4114,7 +4072,7 @@ pub fn createContainerDecl(...@@ -4114,7 +4072,7 @@ pub fn createContainerDecl(
4114 defer mod.gpa.free(name);4072 defer mod.gpa.free(name);
4115 const name_hash = scope.namespace().fullyQualifiedNameHash(name);4073 const name_hash = scope.namespace().fullyQualifiedNameHash(name);
4116 const src_hash: std.zig.SrcHash = undefined;4074 const src_hash: std.zig.SrcHash = undefined;
4117 const new_decl = try mod.createNewDecl(scope, name, scope_decl.src_index, name_hash, src_hash);4075 const new_decl = try mod.createNewDecl(scope, name, scope_decl.src_node, name_hash, src_hash);
4118 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);4076 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
41194077
4120 decl_arena_state.* = decl_arena.state;4078 decl_arena_state.* = decl_arena.state;
src/codegen.zig+1-1
...@@ -417,7 +417,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -417,7 +417,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
417 const node_datas = tree.nodes.items(.data);417 const node_datas = tree.nodes.items(.data);
418 const token_starts = tree.tokens.items(.start);418 const token_starts = tree.tokens.items(.start);
419419
420 const fn_decl = tree.rootDecls()[module_fn.owner_decl.src_index];420 const fn_decl = module_fn.owner_decl.src_node;
421 assert(node_tags[fn_decl] == .fn_decl);421 assert(node_tags[fn_decl] == .fn_decl);
422 const block = node_datas[fn_decl].rhs;422 const block = node_datas[fn_decl].rhs;
423 const lbrace_src = token_starts[tree.firstToken(block)];423 const lbrace_src = token_starts[tree.firstToken(block)];
src/link/Elf.zig+2-4
...@@ -2228,10 +2228,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {...@@ -2228,10 +2228,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
2228 const node_datas = tree.nodes.items(.data);2228 const node_datas = tree.nodes.items(.data);
2229 const token_starts = tree.tokens.items(.start);2229 const token_starts = tree.tokens.items(.start);
22302230
2231 const file_ast_decls = tree.rootDecls();
2232 // TODO Look into improving the performance here by adding a token-index-to-line2231 // TODO Look into improving the performance here by adding a token-index-to-line
2233 // lookup table. Currently this involves scanning over the source code for newlines.2232 // lookup table. Currently this involves scanning over the source code for newlines.
2234 const fn_decl = file_ast_decls[decl.src_index];2233 const fn_decl = decl.src_node;
2235 assert(node_tags[fn_decl] == .fn_decl);2234 assert(node_tags[fn_decl] == .fn_decl);
2236 const block = node_datas[fn_decl].rhs;2235 const block = node_datas[fn_decl].rhs;
2237 const lbrace = tree.firstToken(block);2236 const lbrace = tree.firstToken(block);
...@@ -2755,10 +2754,9 @@ pub fn updateDeclLineNumber(self: *Elf, module: *Module, decl: *const Module.Dec...@@ -2755,10 +2754,9 @@ pub fn updateDeclLineNumber(self: *Elf, module: *Module, decl: *const Module.Dec
2755 const node_datas = tree.nodes.items(.data);2754 const node_datas = tree.nodes.items(.data);
2756 const token_starts = tree.tokens.items(.start);2755 const token_starts = tree.tokens.items(.start);
27572756
2758 const file_ast_decls = tree.rootDecls();
2759 // TODO Look into improving the performance here by adding a token-index-to-line2757 // TODO Look into improving the performance here by adding a token-index-to-line
2760 // lookup table. Currently this involves scanning over the source code for newlines.2758 // lookup table. Currently this involves scanning over the source code for newlines.
2761 const fn_decl = file_ast_decls[decl.src_index];2759 const fn_decl = decl.src_node;
2762 assert(node_tags[fn_decl] == .fn_decl);2760 assert(node_tags[fn_decl] == .fn_decl);
2763 const block = node_datas[fn_decl].rhs;2761 const block = node_datas[fn_decl].rhs;
2764 const lbrace = tree.firstToken(block);2762 const lbrace = tree.firstToken(block);
src/link/MachO/DebugSymbols.zig+2-4
...@@ -909,10 +909,9 @@ pub fn updateDeclLineNumber(self: *DebugSymbols, module: *Module, decl: *const M...@@ -909,10 +909,9 @@ pub fn updateDeclLineNumber(self: *DebugSymbols, module: *Module, decl: *const M
909 const node_datas = tree.nodes.items(.data);909 const node_datas = tree.nodes.items(.data);
910 const token_starts = tree.tokens.items(.start);910 const token_starts = tree.tokens.items(.start);
911911
912 const file_ast_decls = tree.rootDecls();
913 // TODO Look into improving the performance here by adding a token-index-to-line912 // TODO Look into improving the performance here by adding a token-index-to-line
914 // lookup table. Currently this involves scanning over the source code for newlines.913 // lookup table. Currently this involves scanning over the source code for newlines.
915 const fn_decl = file_ast_decls[decl.src_index];914 const fn_decl = decl.src_node;
916 assert(node_tags[fn_decl] == .fn_decl);915 assert(node_tags[fn_decl] == .fn_decl);
917 const block = node_datas[fn_decl].rhs;916 const block = node_datas[fn_decl].rhs;
918 const lbrace = tree.firstToken(block);917 const lbrace = tree.firstToken(block);
...@@ -959,10 +958,9 @@ pub fn initDeclDebugBuffers(...@@ -959,10 +958,9 @@ pub fn initDeclDebugBuffers(
959 const node_datas = tree.nodes.items(.data);958 const node_datas = tree.nodes.items(.data);
960 const token_starts = tree.tokens.items(.start);959 const token_starts = tree.tokens.items(.start);
961960
962 const file_ast_decls = tree.rootDecls();
963 // TODO Look into improving the performance here by adding a token-index-to-line961 // TODO Look into improving the performance here by adding a token-index-to-line
964 // lookup table. Currently this involves scanning over the source code for newlines.962 // lookup table. Currently this involves scanning over the source code for newlines.
965 const fn_decl = file_ast_decls[decl.src_index];963 const fn_decl = decl.src_node;
966 assert(node_tags[fn_decl] == .fn_decl);964 assert(node_tags[fn_decl] == .fn_decl);
967 const block = node_datas[fn_decl].rhs;965 const block = node_datas[fn_decl].rhs;
968 const lbrace = tree.firstToken(block);966 const lbrace = tree.firstToken(block);
src/test.zig+5-7
...@@ -622,6 +622,7 @@ pub const TestContext = struct {...@@ -622,6 +622,7 @@ pub const TestContext = struct {
622 var root_pkg: Package = .{622 var root_pkg: Package = .{
623 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },623 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
624 .root_src_path = tmp_src_path,624 .root_src_path = tmp_src_path,
625 .namespace_hash = Package.root_namespace_hash,
625 };626 };
626627
627 const bin_name = try std.zig.binNameAlloc(arena, .{628 const bin_name = try std.zig.binNameAlloc(arena, .{
...@@ -639,13 +640,10 @@ pub const TestContext = struct {...@@ -639,13 +640,10 @@ pub const TestContext = struct {
639 .directory = emit_directory,640 .directory = emit_directory,
640 .basename = bin_name,641 .basename = bin_name,
641 };642 };
642 const emit_h: ?Compilation.EmitLoc = if (case.emit_h)643 const emit_h: ?Compilation.EmitLoc = if (case.emit_h) .{
643 .{644 .directory = emit_directory,
644 .directory = emit_directory,645 .basename = "test_case.h",
645 .basename = "test_case.h",646 } else null;
646 }
647 else
648 null;
649 const comp = try Compilation.create(allocator, .{647 const comp = try Compilation.create(allocator, .{
650 .local_cache_directory = zig_cache_directory,648 .local_cache_directory = zig_cache_directory,
651 .global_cache_directory = global_cache_directory,649 .global_cache_directory = global_cache_directory,
test/stage2/test.zig+14-1
...@@ -1040,9 +1040,22 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1040,9 +1040,22 @@ pub fn addCases(ctx: *TestContext) !void {
1040 }1040 }
10411041
1042 ctx.compileError("function redefinition", linux_x64,1042 ctx.compileError("function redefinition", linux_x64,
1043 \\// dummy comment
1043 \\fn entry() void {}1044 \\fn entry() void {}
1044 \\fn entry() void {}1045 \\fn entry() void {}
1045 , &[_][]const u8{":2:4: error: redefinition of 'entry'"});1046 , &[_][]const u8{
1047 ":3:4: error: redefinition of 'entry'",
1048 ":2:1: note: previous definition here",
1049 });
1050
1051 ctx.compileError("global variable redefinition", linux_x64,
1052 \\// dummy comment
1053 \\var foo = false;
1054 \\var foo = true;
1055 , &[_][]const u8{
1056 ":3:5: error: redefinition of 'foo'",
1057 ":2:1: note: previous definition here",
1058 });
10461059
1047 ctx.compileError("compileError", linux_x64,1060 ctx.compileError("compileError", linux_x64,
1048 \\export fn _start() noreturn {1061 \\export fn _start() noreturn {