authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-05 21:55:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-05 21:55:32-04:00
log0a3c6dbda92931eba055c2c6447b7a4412408f17
tree86d98cd519e628c28efe2fe9e2ff3ec86f55246d
parentca70ca7e26aaae3425dad3a2b179f544bacf45e3
signaturelock-open Commit is signed but in an unrecognized format.

implement `noasync` function calls

See #3157

13 files changed, 175 insertions(+), 68 deletions(-)

doc/docgen.zig+2-1
......@@ -786,9 +786,10 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok
786786 .Keyword_for,
787787 .Keyword_if,
788788 .Keyword_inline,
789 .Keyword_noinline,
790789 .Keyword_nakedcc,
791790 .Keyword_noalias,
791 .Keyword_noasync,
792 .Keyword_noinline,
792793 .Keyword_or,
793794 .Keyword_orelse,
794795 .Keyword_packed,
src/all_types.hpp+14-5
......@@ -758,11 +758,17 @@ struct AstNodeUnwrapOptional {
758758 AstNode *expr;
759759};
760760
761enum CallModifier {
762 CallModifierNone,
763 CallModifierAsync,
764 CallModifierNoAsync,
765 CallModifierBuiltin,
766};
767
761768struct AstNodeFnCallExpr {
762769 AstNode *fn_ref_expr;
763770 ZigList<AstNode *> params;
764 bool is_builtin;
765 bool is_async;
771 CallModifier modifier;
766772 bool seen; // used by @compileLog
767773};
768774
......@@ -2730,8 +2736,10 @@ struct IrInstructionCallSrc {
27302736 ResultLoc *result_loc;
27312737
27322738 IrInstruction *new_stack;
2739
27332740 FnInline fn_inline;
2734 bool is_async;
2741 CallModifier modifier;
2742
27352743 bool is_async_call_builtin;
27362744 bool is_comptime;
27372745};
......@@ -2745,10 +2753,11 @@ struct IrInstructionCallGen {
27452753 IrInstruction **args;
27462754 IrInstruction *result_loc;
27472755 IrInstruction *frame_result_loc;
2748
27492756 IrInstruction *new_stack;
2757
27502758 FnInline fn_inline;
2751 bool is_async;
2759 CallModifier modifier;
2760
27522761 bool is_async_call_builtin;
27532762};
27542763
src/analyze.cpp+10-4
......@@ -4214,7 +4214,7 @@ void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn) {
42144214 add_error_note(g, msg, fn->inferred_async_node,
42154215 buf_sprintf("await here is a suspend point"));
42164216 } else if (fn->inferred_async_node->type == NodeTypeFnCallExpr &&
4217 fn->inferred_async_node->data.fn_call_expr.is_builtin)
4217 fn->inferred_async_node->data.fn_call_expr.modifier == CallModifierBuiltin)
42184218 {
42194219 add_error_note(g, msg, fn->inferred_async_node,
42204220 buf_sprintf("@frame() causes function to be async"));
......@@ -4228,8 +4228,10 @@ void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn) {
42284228// ErrorIsAsync - yes async
42294229// ErrorSemanticAnalyzeFail - compile error emitted result is invalid
42304230static Error analyze_callee_async(CodeGen *g, ZigFn *fn, ZigFn *callee, AstNode *call_node,
4231 bool must_not_be_async)
4231 bool must_not_be_async, CallModifier modifier)
42324232{
4233 if (modifier == CallModifierNoAsync)
4234 return ErrorNone;
42334235 if (callee->type_entry->data.fn.fn_type_id.cc != CallingConventionUnspecified)
42344236 return ErrorNone;
42354237 if (callee->anal_state == FnAnalStateReady) {
......@@ -4312,7 +4314,9 @@ static void analyze_fn_async(CodeGen *g, ZigFn *fn, bool resolve_frame) {
43124314 // TODO function pointer call here, could be anything
43134315 continue;
43144316 }
4315 switch (analyze_callee_async(g, fn, call->fn_entry, call->base.source_node, must_not_be_async)) {
4317 switch (analyze_callee_async(g, fn, call->fn_entry, call->base.source_node, must_not_be_async,
4318 call->modifier))
4319 {
43164320 case ErrorSemanticAnalyzeFail:
43174321 fn->anal_state = FnAnalStateInvalid;
43184322 return;
......@@ -4329,7 +4333,9 @@ static void analyze_fn_async(CodeGen *g, ZigFn *fn, bool resolve_frame) {
43294333 }
43304334 for (size_t i = 0; i < fn->await_list.length; i += 1) {
43314335 IrInstructionAwaitGen *await = fn->await_list.at(i);
4332 switch (analyze_callee_async(g, fn, await->target_fn, await->base.source_node, must_not_be_async)) {
4336 switch (analyze_callee_async(g, fn, await->target_fn, await->base.source_node, must_not_be_async,
4337 CallModifierNone))
4338 {
43334339 case ErrorSemanticAnalyzeFail:
43344340 fn->anal_state = FnAnalStateInvalid;
43354341 return;
src/ast_render.cpp+12-5
......@@ -698,11 +698,18 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
698698 }
699699 case NodeTypeFnCallExpr:
700700 {
701 if (node->data.fn_call_expr.is_builtin) {
702 fprintf(ar->f, "@");
703 }
704 if (node->data.fn_call_expr.is_async) {
705 fprintf(ar->f, "async ");
701 switch (node->data.fn_call_expr.modifier) {
702 case CallModifierNone:
703 break;
704 case CallModifierBuiltin:
705 fprintf(ar->f, "@");
706 break;
707 case CallModifierAsync:
708 fprintf(ar->f, "async ");
709 break;
710 case CallModifierNoAsync:
711 fprintf(ar->f, "noasync ");
712 break;
706713 }
707714 AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
708715 bool grouped = (fn_ref_node->type != NodeTypePrefixOpExpr && fn_ref_node->type != NodeTypePointerType);
src/codegen.cpp+47-9
......@@ -186,6 +186,9 @@ static void generate_error_name_table(CodeGen *g);
186186static bool value_is_all_undef(CodeGen *g, ConstExprValue *const_val);
187187static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr);
188188static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment);
189static LLVMValueRef gen_await_early_return(CodeGen *g, IrInstruction *source_instr,
190 LLVMValueRef target_frame_ptr, ZigType *result_type, ZigType *ptr_result_type,
191 LLVMValueRef result_loc, bool non_async);
189192
190193static void addLLVMAttr(LLVMValueRef val, LLVMAttributeIndex attr_index, const char *attr_name) {
191194 unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name, strlen(attr_name));
......@@ -3842,7 +3845,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
38423845 LLVMValueRef ret_ptr;
38433846 if (callee_is_async) {
38443847 if (instruction->new_stack == nullptr) {
3845 if (instruction->is_async) {
3848 if (instruction->modifier == CallModifierAsync) {
38463849 frame_result_loc = result_loc;
38473850 } else {
38483851 frame_result_loc = ir_llvm_value(g, instruction->frame_result_loc);
......@@ -3883,7 +3886,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
38833886 }
38843887 }
38853888 }
3886 if (instruction->is_async) {
3889 if (instruction->modifier == CallModifierAsync) {
38873890 if (instruction->new_stack == nullptr) {
38883891 awaiter_init_val = zero;
38893892
......@@ -3908,9 +3911,15 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
39083911 // even if prefix_arg_err_ret_stack is true, let the async function do its own
39093912 // initialization.
39103913 } else {
3911 // async function called as a normal function
3912
3913 awaiter_init_val = LLVMBuildPtrToInt(g->builder, g->cur_frame_ptr, usize_type_ref, ""); // caller's own frame pointer
3914 if (instruction->modifier == CallModifierNoAsync && !fn_is_async(g->cur_fn)) {
3915 // Async function called as a normal function, and calling function is not async.
3916 // This is allowed because it was called with `noasync` which asserts that it will
3917 // never suspend.
3918 awaiter_init_val = zero;
3919 } else {
3920 // async function called as a normal function
3921 awaiter_init_val = LLVMBuildPtrToInt(g->builder, g->cur_frame_ptr, usize_type_ref, ""); // caller's own frame pointer
3922 }
39143923 if (ret_has_bits) {
39153924 if (result_loc == nullptr) {
39163925 // return type is a scalar, but we still need a pointer to it. Use the async fn frame.
......@@ -3951,7 +3960,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
39513960 LLVMValueRef ret_ptr_ptr = LLVMBuildStructGEP(g->builder, frame_result_loc, frame_ret_start, "");
39523961 LLVMBuildStore(g->builder, ret_ptr, ret_ptr_ptr);
39533962 }
3954 } else if (instruction->is_async) {
3963 } else if (instruction->modifier == CallModifierAsync) {
39553964 // Async call of blocking function
39563965 if (instruction->new_stack != nullptr) {
39573966 zig_panic("TODO @asyncCall of non-async function");
......@@ -4048,13 +4057,20 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
40484057 gen_param_values.at(arg_i));
40494058 }
40504059
4051 if (instruction->is_async) {
4060 if (instruction->modifier == CallModifierAsync) {
40524061 gen_resume(g, fn_val, frame_result_loc, ResumeIdCall);
40534062 if (instruction->new_stack != nullptr) {
40544063 return LLVMBuildBitCast(g->builder, frame_result_loc,
40554064 get_llvm_type(g, instruction->base.value.type), "");
40564065 }
40574066 return nullptr;
4067 } else if (instruction->modifier == CallModifierNoAsync && !fn_is_async(g->cur_fn)) {
4068 gen_resume(g, fn_val, frame_result_loc, ResumeIdCall);
4069
4070 ZigType *result_type = instruction->base.value.type;
4071 ZigType *ptr_result_type = get_pointer_to_type(g, result_type, true);
4072 return gen_await_early_return(g, &instruction->base, frame_result_loc,
4073 result_type, ptr_result_type, result_loc, true);
40584074 } else {
40594075 ZigType *ptr_result_type = get_pointer_to_type(g, src_return_type, true);
40604076
......@@ -4082,7 +4098,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
40824098 if (instruction->new_stack == nullptr || instruction->is_async_call_builtin) {
40834099 result = ZigLLVMBuildCall(g->builder, fn_val,
40844100 gen_param_values.items, (unsigned)gen_param_values.length, llvm_cc, fn_inline, "");
4085 } else if (instruction->is_async) {
4101 } else if (instruction->modifier == CallModifierAsync) {
40864102 zig_panic("TODO @asyncCall of non-async function");
40874103 } else {
40884104 LLVMValueRef stacksave_fn_val = get_stacksave_fn_val(g);
......@@ -4107,7 +4123,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
41074123 LLVMValueRef store_instr = LLVMBuildStore(g->builder, result, result_loc);
41084124 LLVMSetAlignment(store_instr, get_ptr_align(g, instruction->result_loc->value.type));
41094125 return result_loc;
4110 } else if (!callee_is_async && instruction->is_async) {
4126 } else if (!callee_is_async && instruction->modifier == CallModifierAsync) {
41114127 LLVMBuildStore(g->builder, result, ret_ptr);
41124128 return result_loc;
41134129 } else {
......@@ -7104,6 +7120,28 @@ static void do_code_gen(CodeGen *g) {
71047120 }
71057121
71067122 if (!is_async) {
7123 // allocate async frames for noasync calls & awaits to async functions
7124 for (size_t i = 0; i < fn_table_entry->call_list.length; i += 1) {
7125 IrInstructionCallGen *call = fn_table_entry->call_list.at(i);
7126 if (call->fn_entry == nullptr)
7127 continue;
7128 if (!fn_is_async(call->fn_entry))
7129 continue;
7130 if (call->modifier != CallModifierNoAsync)
7131 continue;
7132 if (call->frame_result_loc != nullptr)
7133 continue;
7134 ZigType *callee_frame_type = get_fn_frame_type(g, call->fn_entry);
7135 IrInstructionAllocaGen *alloca_gen = allocate<IrInstructionAllocaGen>(1);
7136 alloca_gen->base.id = IrInstructionIdAllocaGen;
7137 alloca_gen->base.source_node = call->base.source_node;
7138 alloca_gen->base.scope = call->base.scope;
7139 alloca_gen->base.value.type = get_pointer_to_type(g, callee_frame_type, false);
7140 alloca_gen->base.ref_count = 1;
7141 alloca_gen->name_hint = "";
7142 fn_table_entry->alloca_gen_list.append(alloca_gen);
7143 call->frame_result_loc = &alloca_gen->base;
7144 }
71077145 // allocate temporary stack data
71087146 for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_gen_list.length; alloca_i += 1) {
71097147 IrInstructionAllocaGen *instruction = fn_table_entry->alloca_gen_list.at(alloca_i);
src/ir.cpp+27-22
......@@ -1389,7 +1389,7 @@ static IrInstruction *ir_build_union_field_ptr(IrBuilder *irb, Scope *scope, Ast
13891389
13901390static IrInstruction *ir_build_call_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
13911391 ZigFn *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
1392 bool is_comptime, FnInline fn_inline, bool is_async, bool is_async_call_builtin,
1392 bool is_comptime, FnInline fn_inline, CallModifier modifier, bool is_async_call_builtin,
13931393 IrInstruction *new_stack, ResultLoc *result_loc)
13941394{
13951395 IrInstructionCallSrc *call_instruction = ir_build_instruction<IrInstructionCallSrc>(irb, scope, source_node);
......@@ -1399,7 +1399,7 @@ static IrInstruction *ir_build_call_src(IrBuilder *irb, Scope *scope, AstNode *s
13991399 call_instruction->fn_inline = fn_inline;
14001400 call_instruction->args = args;
14011401 call_instruction->arg_count = arg_count;
1402 call_instruction->is_async = is_async;
1402 call_instruction->modifier = modifier;
14031403 call_instruction->is_async_call_builtin = is_async_call_builtin;
14041404 call_instruction->new_stack = new_stack;
14051405 call_instruction->result_loc = result_loc;
......@@ -1407,7 +1407,7 @@ static IrInstruction *ir_build_call_src(IrBuilder *irb, Scope *scope, AstNode *s
14071407 if (fn_ref != nullptr) ir_ref_instruction(fn_ref, irb->current_basic_block);
14081408 for (size_t i = 0; i < arg_count; i += 1)
14091409 ir_ref_instruction(args[i], irb->current_basic_block);
1410 if (is_async && new_stack != nullptr) {
1410 if (modifier == CallModifierAsync && new_stack != nullptr) {
14111411 // in this case the arg at the end is the return pointer
14121412 ir_ref_instruction(args[arg_count], irb->current_basic_block);
14131413 }
......@@ -1418,7 +1418,7 @@ static IrInstruction *ir_build_call_src(IrBuilder *irb, Scope *scope, AstNode *s
14181418
14191419static IrInstructionCallGen *ir_build_call_gen(IrAnalyze *ira, IrInstruction *source_instruction,
14201420 ZigFn *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
1421 FnInline fn_inline, bool is_async, IrInstruction *new_stack, bool is_async_call_builtin,
1421 FnInline fn_inline, CallModifier modifier, IrInstruction *new_stack, bool is_async_call_builtin,
14221422 IrInstruction *result_loc, ZigType *return_type)
14231423{
14241424 IrInstructionCallGen *call_instruction = ir_build_instruction<IrInstructionCallGen>(&ira->new_irb,
......@@ -1429,7 +1429,7 @@ static IrInstructionCallGen *ir_build_call_gen(IrAnalyze *ira, IrInstruction *so
14291429 call_instruction->fn_inline = fn_inline;
14301430 call_instruction->args = args;
14311431 call_instruction->arg_count = arg_count;
1432 call_instruction->is_async = is_async;
1432 call_instruction->modifier = modifier;
14331433 call_instruction->is_async_call_builtin = is_async_call_builtin;
14341434 call_instruction->new_stack = new_stack;
14351435 call_instruction->result_loc = result_loc;
......@@ -4412,10 +4412,10 @@ static IrInstruction *ir_gen_async_call(IrBuilder *irb, Scope *scope, AstNode *a
44124412
44134413 args[arg_count] = ret_ptr;
44144414
4415 bool is_async = await_node == nullptr;
4415 CallModifier modifier = (await_node == nullptr) ? CallModifierAsync : CallModifierNone;
44164416 bool is_async_call_builtin = true;
44174417 IrInstruction *call = ir_build_call_src(irb, scope, call_node, nullptr, fn_ref, arg_count, args, false,
4418 FnInlineAuto, is_async, is_async_call_builtin, bytes, result_loc);
4418 FnInlineAuto, modifier, is_async_call_builtin, bytes, result_loc);
44194419 return ir_lval_wrap(irb, scope, call, lval, result_loc);
44204420}
44214421
......@@ -5302,7 +5302,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
53025302 FnInline fn_inline = (builtin_fn->id == BuiltinFnIdInlineCall) ? FnInlineAlways : FnInlineNever;
53035303
53045304 IrInstruction *call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,
5305 fn_inline, false, false, nullptr, result_loc);
5305 fn_inline, CallModifierNone, false, nullptr, result_loc);
53065306 return ir_lval_wrap(irb, scope, call, lval, result_loc);
53075307 }
53085308 case BuiltinFnIdNewStackCall:
......@@ -5335,7 +5335,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
53355335 }
53365336
53375337 IrInstruction *call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,
5338 FnInlineAuto, false, false, new_stack, result_loc);
5338 FnInlineAuto, CallModifierNone, false, new_stack, result_loc);
53395339 return ir_lval_wrap(irb, scope, call, lval, result_loc);
53405340 }
53415341 case BuiltinFnIdAsyncCall:
......@@ -5624,7 +5624,7 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
56245624{
56255625 assert(node->type == NodeTypeFnCallExpr);
56265626
5627 if (node->data.fn_call_expr.is_builtin)
5627 if (node->data.fn_call_expr.modifier == CallModifierBuiltin)
56285628 return ir_gen_builtin_fn_call(irb, scope, node, lval, result_loc);
56295629
56305630 AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
......@@ -5641,9 +5641,8 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
56415641 return args[i];
56425642 }
56435643
5644 bool is_async = node->data.fn_call_expr.is_async;
56455644 IrInstruction *fn_call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,
5646 FnInlineAuto, is_async, false, nullptr, result_loc);
5645 FnInlineAuto, node->data.fn_call_expr.modifier, false, nullptr, result_loc);
56475646 return ir_lval_wrap(irb, scope, fn_call, lval, result_loc);
56485647}
56495648
......@@ -7937,7 +7936,7 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *scope, AstNode *n
79377936 assert(node->type == NodeTypeAwaitExpr);
79387937
79397938 AstNode *expr_node = node->data.await_expr.expr;
7940 if (expr_node->type == NodeTypeFnCallExpr && expr_node->data.fn_call_expr.is_builtin) {
7939 if (expr_node->type == NodeTypeFnCallExpr && expr_node->data.fn_call_expr.modifier == CallModifierBuiltin) {
79417940 AstNode *fn_ref_expr = expr_node->data.fn_call_expr.fn_ref_expr;
79427941 Buf *name = fn_ref_expr->data.symbol_expr.symbol;
79437942 auto entry = irb->codegen->builtin_fn_table.maybe_get(name);
......@@ -15408,7 +15407,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc
1540815407 ZigType *anyframe_type = get_any_frame_type(ira->codegen, fn_ret_type);
1540915408
1541015409 IrInstructionCallGen *call_gen = ir_build_call_gen(ira, &call_instruction->base, fn_entry, fn_ref,
15411 arg_count, casted_args, FnInlineAuto, true, casted_new_stack,
15410 arg_count, casted_args, FnInlineAuto, CallModifierAsync, casted_new_stack,
1541215411 call_instruction->is_async_call_builtin, ret_ptr, anyframe_type);
1541315412 return &call_gen->base;
1541415413 } else {
......@@ -15422,8 +15421,8 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc
1542215421 if (type_is_invalid(result_loc->value.type))
1542315422 return ira->codegen->invalid_instruction;
1542415423 return &ir_build_call_gen(ira, &call_instruction->base, fn_entry, fn_ref, arg_count,
15425 casted_args, FnInlineAuto, true, casted_new_stack, call_instruction->is_async_call_builtin,
15426 result_loc, frame_type)->base;
15424 casted_args, FnInlineAuto, CallModifierAsync, casted_new_stack,
15425 call_instruction->is_async_call_builtin, result_loc, frame_type)->base;
1542715426 }
1542815427}
1542915428static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node,
......@@ -16174,7 +16173,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1617416173 return ira->codegen->invalid_instruction;
1617516174
1617616175 size_t impl_param_count = impl_fn_type_id->param_count;
16177 if (call_instruction->is_async) {
16176 if (call_instruction->modifier == CallModifierAsync) {
1617816177 IrInstruction *result = ir_analyze_async_call(ira, call_instruction, impl_fn, impl_fn->type_entry,
1617916178 nullptr, casted_args, impl_param_count, casted_new_stack);
1618016179 return ir_finish_anal(ira, result);
......@@ -16201,14 +16200,17 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1620116200 result_loc = nullptr;
1620216201 }
1620316202
16204 if (impl_fn_type_id->cc == CallingConventionAsync && parent_fn_entry->inferred_async_node == nullptr) {
16203 if (impl_fn_type_id->cc == CallingConventionAsync &&
16204 parent_fn_entry->inferred_async_node == nullptr &&
16205 call_instruction->modifier != CallModifierNoAsync)
16206 {
1620516207 parent_fn_entry->inferred_async_node = fn_ref->source_node;
1620616208 parent_fn_entry->inferred_async_fn = impl_fn;
1620716209 }
1620816210
1620916211 IrInstructionCallGen *new_call_instruction = ir_build_call_gen(ira, &call_instruction->base,
1621016212 impl_fn, nullptr, impl_param_count, casted_args, fn_inline,
16211 false, casted_new_stack, call_instruction->is_async_call_builtin, result_loc,
16213 call_instruction->modifier, casted_new_stack, call_instruction->is_async_call_builtin, result_loc,
1621216214 impl_fn_type_id->return_type);
1621316215
1621416216 if (get_scope_typeof(call_instruction->base.scope) == nullptr) {
......@@ -16325,13 +16327,16 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1632516327 if (casted_new_stack != nullptr && type_is_invalid(casted_new_stack->value.type))
1632616328 return ira->codegen->invalid_instruction;
1632716329
16328 if (call_instruction->is_async) {
16330 if (call_instruction->modifier == CallModifierAsync) {
1632916331 IrInstruction *result = ir_analyze_async_call(ira, call_instruction, fn_entry, fn_type, fn_ref,
1633016332 casted_args, call_param_count, casted_new_stack);
1633116333 return ir_finish_anal(ira, result);
1633216334 }
1633316335
16334 if (fn_type_id->cc == CallingConventionAsync && parent_fn_entry->inferred_async_node == nullptr) {
16336 if (fn_type_id->cc == CallingConventionAsync &&
16337 parent_fn_entry->inferred_async_node == nullptr &&
16338 call_instruction->modifier != CallModifierNoAsync)
16339 {
1633516340 parent_fn_entry->inferred_async_node = fn_ref->source_node;
1633616341 parent_fn_entry->inferred_async_fn = fn_entry;
1633716342 }
......@@ -16358,7 +16363,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1635816363 }
1635916364
1636016365 IrInstructionCallGen *new_call_instruction = ir_build_call_gen(ira, &call_instruction->base, fn_entry, fn_ref,
16361 call_param_count, casted_args, fn_inline, false, casted_new_stack,
16366 call_param_count, casted_args, fn_inline, call_instruction->modifier, casted_new_stack,
1636216367 call_instruction->is_async_call_builtin, result_loc, return_type);
1636316368 if (get_scope_typeof(call_instruction->base.scope) == nullptr) {
1636416369 parent_fn_entry->call_list.append(new_call_instruction);
src/ir_print.cpp+22-4
......@@ -608,8 +608,17 @@ static void ir_print_result_loc(IrPrint *irp, ResultLoc *result_loc) {
608608}
609609
610610static void ir_print_call_src(IrPrint *irp, IrInstructionCallSrc *call_instruction) {
611 if (call_instruction->is_async) {
612 fprintf(irp->f, "async ");
611 switch (call_instruction->modifier) {
612 case CallModifierNone:
613 break;
614 case CallModifierAsync:
615 fprintf(irp->f, "async ");
616 break;
617 case CallModifierNoAsync:
618 fprintf(irp->f, "noasync ");
619 break;
620 case CallModifierBuiltin:
621 zig_unreachable();
613622 }
614623 if (call_instruction->fn_entry) {
615624 fprintf(irp->f, "%s", buf_ptr(&call_instruction->fn_entry->symbol_name));
......@@ -629,8 +638,17 @@ static void ir_print_call_src(IrPrint *irp, IrInstructionCallSrc *call_instructi
629638}
630639
631640static void ir_print_call_gen(IrPrint *irp, IrInstructionCallGen *call_instruction) {
632 if (call_instruction->is_async) {
633 fprintf(irp->f, "async ");
641 switch (call_instruction->modifier) {
642 case CallModifierNone:
643 break;
644 case CallModifierAsync:
645 fprintf(irp->f, "async ");
646 break;
647 case CallModifierNoAsync:
648 fprintf(irp->f, "noasync ");
649 break;
650 case CallModifierBuiltin:
651 zig_unreachable();
634652 }
635653 if (call_instruction->fn_entry) {
636654 fprintf(irp->f, "%s", buf_ptr(&call_instruction->fn_entry->symbol_name));
src/parser.cpp+15-13
......@@ -113,7 +113,7 @@ static AstNode *ast_parse_multiply_op(ParseContext *pc);
113113static AstNode *ast_parse_prefix_op(ParseContext *pc);
114114static AstNode *ast_parse_prefix_type_op(ParseContext *pc);
115115static AstNode *ast_parse_suffix_op(ParseContext *pc);
116static AstNode *ast_parse_fn_call_argumnets(ParseContext *pc);
116static AstNode *ast_parse_fn_call_arguments(ParseContext *pc);
117117static AstNode *ast_parse_array_type_start(ParseContext *pc);
118118static AstNode *ast_parse_ptr_type_start(ParseContext *pc);
119119static AstNode *ast_parse_container_decl_auto(ParseContext *pc);
......@@ -1403,12 +1403,14 @@ static AstNode *ast_parse_error_union_expr(ParseContext *pc) {
14031403}
14041404
14051405// SuffixExpr
1406// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments
1406// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments
1407// / KEYWORD_noasync PrimaryTypeExpr SuffixOp* FnCallArguments
14071408// / PrimaryTypeExpr (SuffixOp / FnCallArguments)*
14081409static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
1409 Token *async_token = eat_token_if(pc, TokenIdKeywordAsync);
1410 if (async_token != nullptr) {
1411 if (eat_token_if(pc, TokenIdKeywordFn) != nullptr) {
1410 Token *async_token = eat_token(pc);
1411 bool is_async = async_token->id == TokenIdKeywordAsync;
1412 if (is_async || async_token->id == TokenIdKeywordNoAsync) {
1413 if (is_async && eat_token_if(pc, TokenIdKeywordFn) != nullptr) {
14121414 // HACK: If we see the keyword `fn`, then we assume that
14131415 // we are parsing an async fn proto, and not a call.
14141416 // We therefore put back all tokens consumed by the async
......@@ -1447,24 +1449,24 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
14471449 child = suffix;
14481450 }
14491451
1450 // TODO: Both *_async_prefix and *_fn_call_argumnets returns an
1452 // TODO: Both *_async_prefix and *_fn_call_arguments returns an
14511453 // AstNode *. All we really want here is the arguments of
14521454 // the call we parse. We therefor "leak" the node for now.
14531455 // Wait till we get async rework to fix this.
1454 AstNode *args = ast_parse_fn_call_argumnets(pc);
1456 AstNode *args = ast_parse_fn_call_arguments(pc);
14551457 if (args == nullptr)
14561458 ast_invalid_token_error(pc, peek_token(pc));
14571459
14581460 assert(args->type == NodeTypeFnCallExpr);
14591461
14601462 AstNode *res = ast_create_node(pc, NodeTypeFnCallExpr, async_token);
1461 res->data.fn_call_expr.is_async = true;
1463 res->data.fn_call_expr.modifier = is_async ? CallModifierAsync : CallModifierNoAsync;
14621464 res->data.fn_call_expr.seen = false;
14631465 res->data.fn_call_expr.fn_ref_expr = child;
14641466 res->data.fn_call_expr.params = args->data.fn_call_expr.params;
1465 res->data.fn_call_expr.is_builtin = false;
14661467 return res;
14671468 }
1469 put_back_token(pc);
14681470
14691471 AstNode *res = ast_parse_primary_type_expr(pc);
14701472 if (res == nullptr)
......@@ -1496,7 +1498,7 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
14961498 continue;
14971499 }
14981500
1499 AstNode * call = ast_parse_fn_call_argumnets(pc);
1501 AstNode * call = ast_parse_fn_call_arguments(pc);
15001502 if (call != nullptr) {
15011503 assert(call->type == NodeTypeFnCallExpr);
15021504 call->data.fn_call_expr.fn_ref_expr = res;
......@@ -1552,7 +1554,7 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
15521554 name = buf_create_from_str("export");
15531555 }
15541556
1555 AstNode *res = ast_expect(pc, ast_parse_fn_call_argumnets);
1557 AstNode *res = ast_expect(pc, ast_parse_fn_call_arguments);
15561558 AstNode *name_sym = ast_create_node(pc, NodeTypeSymbol, token);
15571559 name_sym->data.symbol_expr.symbol = name;
15581560
......@@ -1560,7 +1562,7 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
15601562 res->line = at_sign->start_line;
15611563 res->column = at_sign->start_column;
15621564 res->data.fn_call_expr.fn_ref_expr = name_sym;
1563 res->data.fn_call_expr.is_builtin = true;
1565 res->data.fn_call_expr.modifier = CallModifierBuiltin;
15641566 return res;
15651567 }
15661568
......@@ -2672,7 +2674,7 @@ static AstNode *ast_parse_suffix_op(ParseContext *pc) {
26722674}
26732675
26742676// FnCallArguments <- LPAREN ExprList RPAREN
2675static AstNode *ast_parse_fn_call_argumnets(ParseContext *pc) {
2677static AstNode *ast_parse_fn_call_arguments(ParseContext *pc) {
26762678 Token *paren = eat_token_if(pc, TokenIdLParen);
26772679 if (paren == nullptr)
26782680 return nullptr;
src/tokenizer.cpp+4-2
......@@ -130,9 +130,10 @@ static const struct ZigKeyword zig_keywords[] = {
130130 {"for", TokenIdKeywordFor},
131131 {"if", TokenIdKeywordIf},
132132 {"inline", TokenIdKeywordInline},
133 {"noinline", TokenIdKeywordNoInline},
134133 {"nakedcc", TokenIdKeywordNakedCC},
135134 {"noalias", TokenIdKeywordNoAlias},
135 {"noasync", TokenIdKeywordNoAsync},
136 {"noinline", TokenIdKeywordNoInline},
136137 {"null", TokenIdKeywordNull},
137138 {"or", TokenIdKeywordOr},
138139 {"orelse", TokenIdKeywordOrElse},
......@@ -1552,9 +1553,10 @@ const char * token_name(TokenId id) {
15521553 case TokenIdKeywordFor: return "for";
15531554 case TokenIdKeywordIf: return "if";
15541555 case TokenIdKeywordInline: return "inline";
1555 case TokenIdKeywordNoInline: return "noinline";
15561556 case TokenIdKeywordNakedCC: return "nakedcc";
15571557 case TokenIdKeywordNoAlias: return "noalias";
1558 case TokenIdKeywordNoAsync: return "noasync";
1559 case TokenIdKeywordNoInline: return "noinline";
15581560 case TokenIdKeywordNull: return "null";
15591561 case TokenIdKeywordOr: return "or";
15601562 case TokenIdKeywordOrElse: return "orelse";
src/tokenizer.hpp+1
......@@ -78,6 +78,7 @@ enum TokenId {
7878 TokenIdKeywordLinkSection,
7979 TokenIdKeywordNakedCC,
8080 TokenIdKeywordNoAlias,
81 TokenIdKeywordNoAsync,
8182 TokenIdKeywordNull,
8283 TokenIdKeywordOr,
8384 TokenIdKeywordOrElse,
src/translate_c.cpp+1-1
......@@ -253,7 +253,7 @@ static AstNode *trans_create_node_symbol_str(Context *c, const char *name) {
253253static AstNode *trans_create_node_builtin_fn_call(Context *c, Buf *name) {
254254 AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
255255 node->data.fn_call_expr.fn_ref_expr = trans_create_node_symbol(c, name);
256 node->data.fn_call_expr.is_builtin = true;
256 node->data.fn_call_expr.modifier = CallModifierBuiltin;
257257 return node;
258258}
259259
std/zig/tokenizer.zig+4-2
......@@ -36,9 +36,10 @@ pub const Token = struct {
3636 Keyword{ .bytes = "for", .id = Id.Keyword_for },
3737 Keyword{ .bytes = "if", .id = Id.Keyword_if },
3838 Keyword{ .bytes = "inline", .id = Id.Keyword_inline },
39 Keyword{ .bytes = "noinline", .id = Id.Keyword_noinline },
4039 Keyword{ .bytes = "nakedcc", .id = Id.Keyword_nakedcc },
4140 Keyword{ .bytes = "noalias", .id = Id.Keyword_noalias },
41 Keyword{ .bytes = "noasync", .id = Id.Keyword_noasync },
42 Keyword{ .bytes = "noinline", .id = Id.Keyword_noinline },
4243 Keyword{ .bytes = "null", .id = Id.Keyword_null },
4344 Keyword{ .bytes = "or", .id = Id.Keyword_or },
4445 Keyword{ .bytes = "orelse", .id = Id.Keyword_orelse },
......@@ -167,9 +168,10 @@ pub const Token = struct {
167168 Keyword_for,
168169 Keyword_if,
169170 Keyword_inline,
170 Keyword_noinline,
171171 Keyword_nakedcc,
172172 Keyword_noalias,
173 Keyword_noasync,
174 Keyword_noinline,
173175 Keyword_null,
174176 Keyword_or,
175177 Keyword_orelse,
test/stage1/behavior/async_fn.zig+16
......@@ -1092,3 +1092,19 @@ test "recursive call of await @asyncCall with struct return type" {
10921092 expect(res.y == 2);
10931093 expect(res.z == 3);
10941094}
1095
1096test "noasync function call" {
1097 const S = struct {
1098 fn doTheTest() void {
1099 const result = noasync add(50, 100);
1100 expect(result == 150);
1101 }
1102 fn add(a: i32, b: i32) i32 {
1103 if (a > 100) {
1104 suspend;
1105 }
1106 return a + b;
1107 }
1108 };
1109 S.doTheTest();
1110}