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 {...@@ -338,19 +338,6 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
338 .@"usingnamespace" => {338 .@"usingnamespace" => {
339 decl.analysis = .in_progress;339 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
354 var code: Zir = blk: {341 var code: Zir = blk: {
355 var astgen = try AstGen.init(mod, decl, &analysis_arena.allocator);342 var astgen = try AstGen.init(mod, decl, &analysis_arena.allocator);
356 defer astgen.deinit();343 defer astgen.deinit();
...@@ -363,39 +350,8 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {...@@ -363,39 +350,8 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
363 defer gen_scope.instructions.deinit(mod.gpa);350 defer gen_scope.instructions.deinit(mod.gpa);
364351
365 const ns_type = try AstGen.typeExpr(&gen_scope, &gen_scope.base, type_expr);352 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;
373 };354 };
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);
399 try decl.namespace.usingnamespace_set.put(mod.gpa, ty.getNamespace().?, is_pub);355 try decl.namespace.usingnamespace_set.put(mod.gpa, ty.getNamespace().?, is_pub);
400356
401 decl.analysis = .complete;357 decl.analysis = .complete;
src/AstGen.zig+50-6
...@@ -2160,19 +2160,59 @@ fn globalVarDecl(...@@ -2160,19 +2160,59 @@ fn globalVarDecl(
2160fn comptimeDecl(2160fn comptimeDecl(
2161 astgen: *AstGen,2161 astgen: *AstGen,
2162 gz: *GenZir,2162 gz: *GenZir,
2163 wip_decls: *WipDecls,2163 scope: *Scope,
2164 node: ast.Node.Index,2164 node: ast.Node.Index,
2165) InnerError!void {2165) 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);
2167}2171}
21682172
2169fn usingnamespaceDecl(2173fn usingnamespaceDecl(
2170 astgen: *AstGen,2174 astgen: *AstGen,
2171 gz: *GenZir,2175 gz: *GenZir,
2172 wip_decls: *WipDecls,2176 scope: *Scope,
2173 node: ast.Node.Index,2177 node: ast.Node.Index,
2174) InnerError!void {2178) 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);
2176}2216}
21772217
2178fn structDeclInner(2218fn structDeclInner(
...@@ -2289,11 +2329,15 @@ fn structDeclInner(...@@ -2289,11 +2329,15 @@ fn structDeclInner(
2289 },2329 },
22902330
2291 .@"comptime" => {2331 .@"comptime" => {
2292 try astgen.comptimeDecl(gz, &wip_decls, member_node);2332 try astgen.comptimeDecl(gz, scope, member_node);
2293 continue;2333 continue;
2294 },2334 },
2295 .@"usingnamespace" => {2335 .@"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);
2297 continue;2341 continue;
2298 },2342 },
2299 else => unreachable,2343 else => unreachable,