authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-26 21:34:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-28 16:57:01-07:00
logfa6bb4b662155e4d6a61cc551b5d02a2a7d5d144
tree23fb0d451b9aefa61c21cb7f1ffb40ec83dd57ef
parent2354cbafdb01d2a763b82cf03a72adef0794f187

Sema: do not analyze test decls when not in test mode

We do this by reserving string table indexes 0 and 1 in ZIR to be special. Decls now have 0 to mean comptime or usingnamespace, and 1 to mean an unnamed test decl.

4 files changed, 34 insertions(+), 74 deletions(-)

BRANCH_TODO-59
......@@ -39,34 +39,6 @@
3939 * AstGen: add result location pointers to function calls
4040 * nested function decl: how to refer to params?
4141
42pub fn createContainerDecl(
43 mod: *Module,
44 scope: *Scope,
45 base_token: std.zig.ast.TokenIndex,
46 decl_arena: *std.heap.ArenaAllocator,
47 typed_value: TypedValue,
48) !*Decl {
49 const scope_decl = scope.ownerDecl().?;
50 const name = try mod.getAnonTypeName(scope, base_token);
51 defer mod.gpa.free(name);
52 const name_hash = scope.namespace().fullyQualifiedNameHash(name);
53 const src_hash: std.zig.SrcHash = undefined;
54 const new_decl = try mod.createNewDecl(scope, name, scope_decl.src_node, name_hash, src_hash);
55 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
56
57 decl_arena_state.* = decl_arena.state;
58 new_decl.typed_value = .{
59 .most_recent = .{
60 .typed_value = typed_value,
61 .arena = decl_arena_state,
62 },
63 };
64 new_decl.analysis = .complete;
65 new_decl.generation = mod.generation;
66
67 return new_decl;
68}
69
7042fn getAnonTypeName(mod: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 {
7143 // TODO add namespaces, generic function signatrues
7244 const tree = scope.tree();
......@@ -83,14 +55,6 @@ fn getAnonTypeName(mod: *Module, scope: *Scope, base_token: std.zig.ast.TokenInd
8355}
8456
8557
86pub fn analyzeFile(mod: *Module, file: *Scope.File) !void {
87 // We call `getAstTree` here so that `analyzeFile` has the error set that includes
88 // file system operations, but `analyzeNamespace` does not.
89 const tree = try mod.getAstTree(file.namespace.file_scope);
90 const decls = tree.rootDecls();
91 return mod.analyzeNamespace(file.namespace, decls);
92}
93
9458/// Returns `true` if the Decl type changed.
9559/// Returns `true` if this is the first time analyzing the Decl.
9660/// Returns `false` otherwise.
......@@ -143,29 +107,6 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
143107 var analysis_arena = std.heap.ArenaAllocator.init(mod.gpa);
144108 defer analysis_arena.deinit();
145109
146 var code: Zir = blk: {
147 var astgen = try AstGen.init(mod, decl, &analysis_arena.allocator);
148 defer astgen.deinit();
149
150 var gen_scope: Scope.GenZir = .{
151 .force_comptime = true,
152 .parent = &decl.namespace.base,
153 .astgen = &astgen,
154 };
155 defer gen_scope.instructions.deinit(mod.gpa);
156
157 const block_expr = node_datas[decl_node].lhs;
158 _ = try AstGen.comptimeExpr(&gen_scope, &gen_scope.base, .none, block_expr);
159 _ = try gen_scope.addBreak(.break_inline, 0, .void_value);
160
161 const code = try gen_scope.finish();
162 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
163 code.dump(mod.gpa, "comptime_block", &gen_scope.base, 0) catch {};
164 }
165 break :blk code;
166 };
167 defer code.deinit(mod.gpa);
168
169110 var sema: Sema = .{
170111 .mod = mod,
171112 .gpa = mod.gpa,
src/AstGen.zig+5-1
......@@ -78,6 +78,9 @@ pub fn generate(gpa: *Allocator, file: *Scope.File) InnerError!Zir {
7878 };
7979 defer astgen.deinit(gpa);
8080
81 // String table indexes 0 and 1 are reserved for special meaning.
82 try astgen.string_bytes.appendSlice(gpa, &[_]u8{ 0, 0 });
83
8184 // We expect at least as many ZIR instructions and extra data items
8285 // as AST nodes.
8386 try astgen.instructions.ensureTotalCapacity(gpa, file.tree.nodes.len);
......@@ -3124,7 +3127,8 @@ fn testDecl(
31243127 if (token_tags[str_lit_token] == .string_literal) {
31253128 break :blk (try decl_block.strLitAsString(str_lit_token)).index;
31263129 }
3127 break :blk 0;
3130 // String table index 1 has a special meaning here of test decl with no name.
3131 break :blk 1;
31283132 };
31293133
31303134 var fn_block: GenZir = .{
src/Module.zig+16-6
......@@ -3415,7 +3415,7 @@ pub fn scanNamespace(
34153415
34163416 const hash_u32s = zir.extra[extra_index..][0..4];
34173417 extra_index += 4;
3418 const name_idx = zir.extra[extra_index];
3418 const decl_name_index = zir.extra[extra_index];
34193419 extra_index += 1;
34203420 const decl_index = zir.extra[extra_index];
34213421 extra_index += 1;
......@@ -3429,7 +3429,6 @@ pub fn scanNamespace(
34293429 extra_index += 1;
34303430 break :inst inst;
34313431 };
3432 const decl_name: ?[]const u8 = if (name_idx == 0) null else zir.nullTerminatedString(name_idx);
34333432 const contents_hash = @bitCast(std.zig.SrcHash, hash_u32s.*);
34343433
34353434 try mod.scanDecl(
......@@ -3437,7 +3436,7 @@ pub fn scanNamespace(
34373436 &deleted_decls,
34383437 &outdated_decls,
34393438 contents_hash,
3440 decl_name,
3439 decl_name_index,
34413440 decl_index,
34423441 is_pub,
34433442 is_exported,
......@@ -3477,7 +3476,7 @@ fn scanDecl(
34773476 deleted_decls: *std.AutoArrayHashMap(*Decl, void),
34783477 outdated_decls: *std.AutoArrayHashMap(*Decl, void),
34793478 contents_hash: std.zig.SrcHash,
3480 decl_name: ?[]const u8,
3479 decl_name_index: u32,
34813480 decl_index: Zir.Inst.Index,
34823481 is_pub: bool,
34833482 is_exported: bool,
......@@ -3493,6 +3492,11 @@ fn scanDecl(
34933492 const decl_block_inst_data = zir.instructions.items(.data)[decl_index].pl_node;
34943493 const decl_node = parent_decl.relativeToNodeIndex(decl_block_inst_data.src_node);
34953494
3495 const decl_name: ?[]const u8 = if (decl_name_index > 1)
3496 zir.nullTerminatedString(decl_name_index)
3497 else
3498 null;
3499
34963500 // We create a Decl for it regardless of analysis status.
34973501 // Decls that have names are keyed in the namespace by the name. Decls without
34983502 // names are keyed by their contents hash. This way we can detect if, for example,
......@@ -3510,8 +3514,14 @@ fn scanDecl(
35103514 // Update the key reference to the longer-lived memory.
35113515 gop.entry.key = &new_decl.contents_hash;
35123516 gop.entry.value = new_decl;
3513 // exported decls, comptime, test, and usingnamespace decls get analyzed.
3514 if (decl_name == null or is_exported) {
3517 // Exported decls, comptime decls, usingnamespace decls, and
3518 // test decls if in test mode, get analyzed.
3519 const want_analysis = is_exported or switch (decl_name_index) {
3520 0 => true, // comptime decl
3521 1 => mod.comp.bin_file.options.is_test, // test decl
3522 else => false,
3523 };
3524 if (want_analysis) {
35153525 mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl });
35163526 }
35173527 new_decl.is_pub = is_pub;
src/Zir.zig+13-8
......@@ -32,6 +32,7 @@ instructions: std.MultiArrayList(Inst).Slice,
3232/// is referencing the data here whether they want to store both index and length,
3333/// thus allowing null bytes, or store only index, and use null-termination. The
3434/// `string_bytes` array is agnostic to either usage.
35/// Indexes 0 and 1 are reserved for special cases.
3536string_bytes: []u8,
3637/// The meaning of this data is determined by `Inst.Tag` value.
3738/// The first few indexes are reserved. See `ExtraIndex` for the values.
......@@ -2378,8 +2379,9 @@ pub const Inst = struct {
23782379 /// 1. decl: { // for every decls_len
23792380 /// src_hash: [4]u32, // hash of source bytes
23802381 /// name: u32, // null terminated string index
2381 /// - can be 0 for test decls. always 0 for comptime and usingnamespace decls.
2382 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2382 /// - 0 means comptime or usingnamespace decl.
2383 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2384 /// - 1 means test decl with no name.
23832385 /// value: Index,
23842386 /// align: Ref, // if corresponding bit is set
23852387 /// link_section: Ref, // if corresponding bit is set
......@@ -2411,8 +2413,9 @@ pub const Inst = struct {
24112413 /// 1. decl: { // for every decls_len
24122414 /// src_hash: [4]u32, // hash of source bytes
24132415 /// name: u32, // null terminated string index
2414 /// - can be 0 for test decls. always 0 for comptime and usingnamespace decls.
2415 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2416 /// - 0 means comptime or usingnamespace decl.
2417 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2418 /// - 1 means test decl with no name.
24162419 /// value: Index,
24172420 /// align: Ref, // if corresponding bit is set
24182421 /// link_section: Ref, // if corresponding bit is set
......@@ -2442,8 +2445,9 @@ pub const Inst = struct {
24422445 /// 1. decl: { // for every decls_len
24432446 /// src_hash: [4]u32, // hash of source bytes
24442447 /// name: u32, // null terminated string index
2445 /// - can be 0 for test decls. always 0 for comptime and usingnamespace decls.
2446 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2448 /// - 0 means comptime or usingnamespace decl.
2449 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2450 /// - 1 means test decl with no name.
24472451 /// value: Index,
24482452 /// align: Ref, // if corresponding bit is set
24492453 /// link_section: Ref, // if corresponding bit is set
......@@ -2483,8 +2487,9 @@ pub const Inst = struct {
24832487 /// 1. decl: { // for every decls_len
24842488 /// src_hash: [4]u32, // hash of source bytes
24852489 /// name: u32, // null terminated string index
2486 /// - can be 0 for test decls. always 0 for comptime and usingnamespace decls.
2487 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2490 /// - 0 means comptime or usingnamespace decl.
2491 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
2492 /// - 1 means test decl with no name.
24882493 /// value: Index,
24892494 /// - one of: block_inline, block_inline_var
24902495 /// align: Ref, // if corresponding bit is set