authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-15 20:55:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-15 20:55:54-07:00
logcf57e8223f06f6b305e7274445cf935b1ed312d2
tree6611f960e6f37b5bbc9488fa9099a22e9b636d82
parent8387307807434cf151d72a7dfb5b7da4863b2192

AstGen: implement comptimeDecl, usingnamespaceDecl, testDecl


2 files changed, 50 insertions(+), 50 deletions(-)

BRANCH_TODO-44
......@@ -338,19 +338,6 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
338338 .@"usingnamespace" => {
339339 decl.analysis = .in_progress;
340340
341 const type_expr = node_datas[decl_node].lhs;
342 const is_pub = blk: {
343 const main_tokens = tree.nodes.items(.main_token);
344 const token_tags = tree.tokens.items(.tag);
345 const main_token = main_tokens[decl_node];
346 break :blk (main_token > 0 and token_tags[main_token - 1] == .keyword_pub);
347 };
348
349 // A usingnamespace decl does not store any value so we can
350 // deinit this arena after analysis is done.
351 var analysis_arena = std.heap.ArenaAllocator.init(mod.gpa);
352 defer analysis_arena.deinit();
353
354341 var code: Zir = blk: {
355342 var astgen = try AstGen.init(mod, decl, &analysis_arena.allocator);
356343 defer astgen.deinit();
......@@ -363,39 +350,8 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
363350 defer gen_scope.instructions.deinit(mod.gpa);
364351
365352 const ns_type = try AstGen.typeExpr(&gen_scope, &gen_scope.base, type_expr);
366 _ = try gen_scope.addBreak(.break_inline, 0, ns_type);
367353
368 const code = try gen_scope.finish();
369 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
370 code.dump(mod.gpa, "usingnamespace_type", &gen_scope.base, 0) catch {};
371 }
372 break :blk code;
373354 };
374 defer code.deinit(mod.gpa);
375
376 var sema: Sema = .{
377 .mod = mod,
378 .gpa = mod.gpa,
379 .arena = &analysis_arena.allocator,
380 .code = code,
381 .inst_map = try analysis_arena.allocator.alloc(*ir.Inst, code.instructions.len),
382 .owner_decl = decl,
383 .namespace = decl.namespace,
384 .func = null,
385 .owner_func = null,
386 .param_inst_list = &.{},
387 };
388 var block_scope: Scope.Block = .{
389 .parent = null,
390 .sema = &sema,
391 .src_decl = decl,
392 .instructions = .{},
393 .inlining = null,
394 .is_comptime = true,
395 };
396 defer block_scope.instructions.deinit(mod.gpa);
397
398 const ty = try sema.rootAsType(&block_scope);
399355 try decl.namespace.usingnamespace_set.put(mod.gpa, ty.getNamespace().?, is_pub);
400356
401357 decl.analysis = .complete;
src/AstGen.zig+50-6
......@@ -2160,19 +2160,59 @@ fn globalVarDecl(
21602160fn comptimeDecl(
21612161 astgen: *AstGen,
21622162 gz: *GenZir,
2163 wip_decls: *WipDecls,
2163 scope: *Scope,
21642164 node: ast.Node.Index,
21652165) InnerError!void {
2166 @panic("TODO astgen comptimeDecl");
2166 const tree = &astgen.file.tree;
2167 const node_datas = tree.nodes.items(.data);
2168 const block_expr = node_datas[node].lhs;
2169 // TODO probably we want to put these into a block and store a list of them
2170 _ = try expr(gz, scope, .none, block_expr);
21672171}
21682172
21692173fn usingnamespaceDecl(
21702174 astgen: *AstGen,
21712175 gz: *GenZir,
2172 wip_decls: *WipDecls,
2176 scope: *Scope,
21732177 node: ast.Node.Index,
21742178) InnerError!void {
2175 @panic("TODO astgen usingnamespaceDecl");
2179 const tree = &astgen.file.tree;
2180 const node_datas = tree.nodes.items(.data);
2181
2182 const type_expr = node_datas[node].lhs;
2183 const is_pub = blk: {
2184 const main_tokens = tree.nodes.items(.main_token);
2185 const token_tags = tree.tokens.items(.tag);
2186 const main_token = main_tokens[node];
2187 break :blk (main_token > 0 and token_tags[main_token - 1] == .keyword_pub);
2188 };
2189 // TODO probably we want to put these into a block and store a list of them
2190 const namespace_inst = try expr(gz, scope, .{ .ty = .type_type }, type_expr);
2191}
2192
2193fn testDecl(
2194 astgen: *AstGen,
2195 gz: *GenZir,
2196 scope: *Scope,
2197 node: ast.Node.Index,
2198) InnerError!void {
2199 const tree = &astgen.file.tree;
2200 const node_datas = tree.nodes.items(.data);
2201 const test_expr = node_datas[node].rhs;
2202
2203 const test_name: u32 = blk: {
2204 const main_tokens = tree.nodes.items(.main_token);
2205 const token_tags = tree.tokens.items(.tag);
2206 const test_token = main_tokens[node];
2207 const str_lit_token = test_token + 1;
2208 if (token_tags[str_lit_token] == .string_literal) {
2209 break :blk (try gz.strLitAsString(str_lit_token)).index;
2210 }
2211 break :blk 0;
2212 };
2213
2214 // TODO probably we want to put these into a block and store a list of them
2215 const block_inst = try expr(gz, scope, .none, test_expr);
21762216}
21772217
21782218fn structDeclInner(
......@@ -2289,11 +2329,15 @@ fn structDeclInner(
22892329 },
22902330
22912331 .@"comptime" => {
2292 try astgen.comptimeDecl(gz, &wip_decls, member_node);
2332 try astgen.comptimeDecl(gz, scope, member_node);
22932333 continue;
22942334 },
22952335 .@"usingnamespace" => {
2296 try astgen.usingnamespaceDecl(gz, &wip_decls, member_node);
2336 try astgen.usingnamespaceDecl(gz, scope, member_node);
2337 continue;
2338 },
2339 .test_decl => {
2340 try astgen.testDecl(gz, scope, member_node);
22972341 continue;
22982342 },
22992343 else => unreachable,