Commit cf57e8223f

Andrew Kelley <andrew@ziglang.org>
2021-04-16 05:55:54
AstGen: implement comptimeDecl, usingnamespaceDecl, testDecl
1 parent 8387307
Changed files (2)
src/AstGen.zig
@@ -2160,19 +2160,59 @@ fn globalVarDecl(
 fn comptimeDecl(
     astgen: *AstGen,
     gz: *GenZir,
-    wip_decls: *WipDecls,
+    scope: *Scope,
     node: ast.Node.Index,
 ) InnerError!void {
-    @panic("TODO astgen comptimeDecl");
+    const tree = &astgen.file.tree;
+    const node_datas = tree.nodes.items(.data);
+    const block_expr = node_datas[node].lhs;
+    // TODO probably we want to put these into a block and store a list of them
+    _ = try expr(gz, scope, .none, block_expr);
 }
 
 fn usingnamespaceDecl(
     astgen: *AstGen,
     gz: *GenZir,
-    wip_decls: *WipDecls,
+    scope: *Scope,
     node: ast.Node.Index,
 ) InnerError!void {
-    @panic("TODO astgen usingnamespaceDecl");
+    const tree = &astgen.file.tree;
+    const node_datas = tree.nodes.items(.data);
+
+    const type_expr = node_datas[node].lhs;
+    const is_pub = blk: {
+        const main_tokens = tree.nodes.items(.main_token);
+        const token_tags = tree.tokens.items(.tag);
+        const main_token = main_tokens[node];
+        break :blk (main_token > 0 and token_tags[main_token - 1] == .keyword_pub);
+    };
+    // TODO probably we want to put these into a block and store a list of them
+    const namespace_inst = try expr(gz, scope, .{ .ty = .type_type }, type_expr);
+}
+
+fn testDecl(
+    astgen: *AstGen,
+    gz: *GenZir,
+    scope: *Scope,
+    node: ast.Node.Index,
+) InnerError!void {
+    const tree = &astgen.file.tree;
+    const node_datas = tree.nodes.items(.data);
+    const test_expr = node_datas[node].rhs;
+
+    const test_name: u32 = blk: {
+        const main_tokens = tree.nodes.items(.main_token);
+        const token_tags = tree.tokens.items(.tag);
+        const test_token = main_tokens[node];
+        const str_lit_token = test_token + 1;
+        if (token_tags[str_lit_token] == .string_literal) {
+            break :blk (try gz.strLitAsString(str_lit_token)).index;
+        }
+        break :blk 0;
+    };
+
+    // TODO probably we want to put these into a block and store a list of them
+    const block_inst = try expr(gz, scope, .none, test_expr);
 }
 
 fn structDeclInner(
@@ -2289,11 +2329,15 @@ fn structDeclInner(
             },
 
             .@"comptime" => {
-                try astgen.comptimeDecl(gz, &wip_decls, member_node);
+                try astgen.comptimeDecl(gz, scope, member_node);
                 continue;
             },
             .@"usingnamespace" => {
-                try astgen.usingnamespaceDecl(gz, &wip_decls, member_node);
+                try astgen.usingnamespaceDecl(gz, scope, member_node);
+                continue;
+            },
+            .test_decl => {
+                try astgen.testDecl(gz, scope, member_node);
                 continue;
             },
             else => unreachable,
BRANCH_TODO
@@ -338,19 +338,6 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
         .@"usingnamespace" => {
             decl.analysis = .in_progress;
 
-            const type_expr = node_datas[decl_node].lhs;
-            const is_pub = blk: {
-                const main_tokens = tree.nodes.items(.main_token);
-                const token_tags = tree.tokens.items(.tag);
-                const main_token = main_tokens[decl_node];
-                break :blk (main_token > 0 and token_tags[main_token - 1] == .keyword_pub);
-            };
-
-            // A usingnamespace decl does not store any value so we can
-            // deinit this arena after analysis is done.
-            var analysis_arena = std.heap.ArenaAllocator.init(mod.gpa);
-            defer analysis_arena.deinit();
-
             var code: Zir = blk: {
                 var astgen = try AstGen.init(mod, decl, &analysis_arena.allocator);
                 defer astgen.deinit();
@@ -363,39 +350,8 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
                 defer gen_scope.instructions.deinit(mod.gpa);
 
                 const ns_type = try AstGen.typeExpr(&gen_scope, &gen_scope.base, type_expr);
-                _ = try gen_scope.addBreak(.break_inline, 0, ns_type);
 
-                const code = try gen_scope.finish();
-                if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
-                    code.dump(mod.gpa, "usingnamespace_type", &gen_scope.base, 0) catch {};
-                }
-                break :blk code;
             };
-            defer code.deinit(mod.gpa);
-
-            var sema: Sema = .{
-                .mod = mod,
-                .gpa = mod.gpa,
-                .arena = &analysis_arena.allocator,
-                .code = code,
-                .inst_map = try analysis_arena.allocator.alloc(*ir.Inst, code.instructions.len),
-                .owner_decl = decl,
-                .namespace = decl.namespace,
-                .func = null,
-                .owner_func = null,
-                .param_inst_list = &.{},
-            };
-            var block_scope: Scope.Block = .{
-                .parent = null,
-                .sema = &sema,
-                .src_decl = decl,
-                .instructions = .{},
-                .inlining = null,
-                .is_comptime = true,
-            };
-            defer block_scope.instructions.deinit(mod.gpa);
-
-            const ty = try sema.rootAsType(&block_scope);
             try decl.namespace.usingnamespace_set.put(mod.gpa, ty.getNamespace().?, is_pub);
 
             decl.analysis = .complete;