authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-09 12:31:36+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-09 12:33:24+02:00
log3618256c97a9988f7d623eeabb667010ca30656f
tree0e6e0d8f3fc958809ffe9ea007416738d136c032
parent6f8d732599461aa816f545b658a068eceb6ac9bc
signaturelock-open Commit is signed but in an unrecognized format.

implement noasync scopes


6 files changed, 77 insertions(+), 2 deletions(-)

src/all_types.hpp+6
......@@ -2330,6 +2330,7 @@ enum ScopeId {
23302330 ScopeIdRuntime,
23312331 ScopeIdTypeOf,
23322332 ScopeIdExpr,
2333 ScopeIdNoAsync,
23332334};
23342335
23352336struct Scope {
......@@ -2462,6 +2463,11 @@ struct ScopeCompTime {
24622463 Scope base;
24632464};
24642465
2466// This scope is created for a noasync expression.
2467// NodeTypeNoAsync
2468struct ScopeNoAsync {
2469 Scope base;
2470};
24652471
24662472// This scope is created for a function definition.
24672473// NodeTypeFnDef
src/analyze.cpp+9
......@@ -106,6 +106,7 @@ static ScopeExpr *find_expr_scope(Scope *scope) {
106106 case ScopeIdDecls:
107107 case ScopeIdFnDef:
108108 case ScopeIdCompTime:
109 case ScopeIdNoAsync:
109110 case ScopeIdVarDecl:
110111 case ScopeIdCImport:
111112 case ScopeIdSuspend:
......@@ -226,6 +227,12 @@ Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {
226227 return &scope->base;
227228}
228229
230Scope *create_noasync_scope(CodeGen *g, AstNode *node, Scope *parent) {
231 ScopeNoAsync *scope = heap::c_allocator.create<ScopeNoAsync>();
232 init_scope(g, &scope->base, ScopeIdNoAsync, node, parent);
233 return &scope->base;
234}
235
229236Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent) {
230237 ScopeTypeOf *scope = heap::c_allocator.create<ScopeTypeOf>();
231238 init_scope(g, &scope->base, ScopeIdTypeOf, node, parent);
......@@ -3755,6 +3762,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
37553762 case NodeTypeCompTime:
37563763 preview_comptime_decl(g, node, decls_scope);
37573764 break;
3765 case NodeTypeNoAsync:
37583766 case NodeTypeParamDecl:
37593767 case NodeTypeReturnExpr:
37603768 case NodeTypeDefer:
......@@ -6176,6 +6184,7 @@ static void mark_suspension_point(Scope *scope) {
61766184 case ScopeIdDecls:
61776185 case ScopeIdFnDef:
61786186 case ScopeIdCompTime:
6187 case ScopeIdNoAsync:
61796188 case ScopeIdCImport:
61806189 case ScopeIdSuspend:
61816190 case ScopeIdTypeOf:
src/analyze.hpp+1
......@@ -125,6 +125,7 @@ ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent);
125125ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent);
126126ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry);
127127Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
128Scope *create_noasync_scope(CodeGen *g, AstNode *node, Scope *parent);
128129Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstSrc *is_comptime);
129130Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent);
130131ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);
src/codegen.cpp+2
......@@ -687,6 +687,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
687687 case ScopeIdLoop:
688688 case ScopeIdSuspend:
689689 case ScopeIdCompTime:
690 case ScopeIdNoAsync:
690691 case ScopeIdRuntime:
691692 case ScopeIdTypeOf:
692693 case ScopeIdExpr:
......@@ -3934,6 +3935,7 @@ static void render_async_var_decls(CodeGen *g, Scope *scope) {
39343935 case ScopeIdLoop:
39353936 case ScopeIdSuspend:
39363937 case ScopeIdCompTime:
3938 case ScopeIdNoAsync:
39373939 case ScopeIdRuntime:
39383940 case ScopeIdTypeOf:
39393941 case ScopeIdExpr:
src/ir.cpp+50-2
......@@ -4978,6 +4978,7 @@ static void ir_count_defers(IrBuilderSrc *irb, Scope *inner_scope, Scope *outer_
49784978 case ScopeIdLoop:
49794979 case ScopeIdSuspend:
49804980 case ScopeIdCompTime:
4981 case ScopeIdNoAsync:
49814982 case ScopeIdRuntime:
49824983 case ScopeIdTypeOf:
49834984 case ScopeIdExpr:
......@@ -5033,6 +5034,7 @@ static bool ir_gen_defers_for_block(IrBuilderSrc *irb, Scope *inner_scope, Scope
50335034 case ScopeIdLoop:
50345035 case ScopeIdSuspend:
50355036 case ScopeIdCompTime:
5037 case ScopeIdNoAsync:
50365038 case ScopeIdRuntime:
50375039 case ScopeIdTypeOf:
50385040 case ScopeIdExpr:
......@@ -7307,6 +7309,31 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
73077309 zig_unreachable();
73087310}
73097311
7312static bool is_noasync_scope(Scope *scope) {
7313 for (;;) {
7314 switch (scope->id) {
7315 case ScopeIdNoAsync:
7316 return true;
7317 case ScopeIdDefer:
7318 case ScopeIdDeferExpr:
7319 case ScopeIdDecls:
7320 case ScopeIdFnDef:
7321 case ScopeIdCompTime:
7322 case ScopeIdVarDecl:
7323 case ScopeIdCImport:
7324 case ScopeIdSuspend:
7325 return false;
7326 case ScopeIdExpr:
7327 case ScopeIdTypeOf:
7328 case ScopeIdBlock:
7329 case ScopeIdLoop:
7330 case ScopeIdRuntime:
7331 scope = scope->parent;
7332 continue;
7333 }
7334 }
7335}
7336
73107337static IrInstSrc *ir_gen_fn_call(IrBuilderSrc *irb, Scope *scope, AstNode *node, LVal lval,
73117338 ResultLoc *result_loc)
73127339{
......@@ -7315,8 +7342,19 @@ static IrInstSrc *ir_gen_fn_call(IrBuilderSrc *irb, Scope *scope, AstNode *node,
73157342 if (node->data.fn_call_expr.modifier == CallModifierBuiltin)
73167343 return ir_gen_builtin_fn_call(irb, scope, node, lval, result_loc);
73177344
7345 bool is_noasync = is_noasync_scope(scope);
7346 CallModifier modifier = node->data.fn_call_expr.modifier;
7347 if (is_noasync) {
7348 if (modifier == CallModifierAsync) {
7349 add_node_error(irb->codegen, node,
7350 buf_sprintf("async call in noasync scope"));
7351 return irb->codegen->invalid_inst_src;
7352 }
7353 modifier = CallModifierNoAsync;
7354 }
7355
73187356 AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
7319 return ir_gen_fn_call_with_args(irb, scope, node, fn_ref_node, node->data.fn_call_expr.modifier,
7357 return ir_gen_fn_call_with_args(irb, scope, node, fn_ref_node, modifier,
73207358 nullptr, node->data.fn_call_expr.params.items, node->data.fn_call_expr.params.length, lval, result_loc);
73217359}
73227360
......@@ -9170,6 +9208,14 @@ static IrInstSrc *ir_gen_comptime(IrBuilderSrc *irb, Scope *parent_scope, AstNod
91709208 return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval, nullptr);
91719209}
91729210
9211static IrInstSrc *ir_gen_noasync(IrBuilderSrc *irb, Scope *parent_scope, AstNode *node, LVal lval) {
9212 assert(node->type == NodeTypeNoAsync);
9213
9214 Scope *child_scope = create_noasync_scope(irb->codegen, node, parent_scope);
9215 // purposefully pass null for result_loc and let EndExpr handle it
9216 return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval, nullptr);
9217}
9218
91739219static IrInstSrc *ir_gen_return_from_block(IrBuilderSrc *irb, Scope *break_scope, AstNode *node, ScopeBlock *block_scope) {
91749220 IrInstSrc *is_comptime;
91759221 if (ir_should_inline(irb->exec, break_scope)) {
......@@ -9763,7 +9809,7 @@ static IrInstSrc *ir_gen_await_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
97639809{
97649810 assert(node->type == NodeTypeAwaitExpr);
97659811
9766 bool is_noasync = node->data.await_expr.noasync_token != nullptr;
9812 bool is_noasync = is_noasync_scope(scope);
97679813
97689814 AstNode *expr_node = node->data.await_expr.expr;
97699815 if (expr_node->type == NodeTypeFnCallExpr && expr_node->data.fn_call_expr.modifier == CallModifierBuiltin) {
......@@ -9938,6 +9984,8 @@ static IrInstSrc *ir_gen_node_raw(IrBuilderSrc *irb, AstNode *node, Scope *scope
99389984 return ir_gen_switch_expr(irb, scope, node, lval, result_loc);
99399985 case NodeTypeCompTime:
99409986 return ir_expr_wrap(irb, scope, ir_gen_comptime(irb, scope, node, lval), result_loc);
9987 case NodeTypeNoAsync:
9988 return ir_expr_wrap(irb, scope, ir_gen_noasync(irb, scope, node, lval), result_loc);
99419989 case NodeTypeErrorType:
99429990 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval, result_loc);
99439991 case NodeTypeBreak:
test/compile_errors.zig+9
......@@ -2,6 +2,15 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.addTest("combination of noasync and async",
6 \\export fn entry() void {
7 \\ noasync async foo();
8 \\}
9 \\fn foo() void {}
10 , &[_][]const u8{
11 "tmp.zig:2:13: error: async call in noasync scope",
12 });
13
514 cases.addTest("@TypeOf with no arguments",
615 \\export fn entry() void {
716 \\ _ = @TypeOf();