static int check_body_send(ast_t* ast, bool in_final) { if(ast_checkflag(ast, AST_FLAG_RECURSE_1)) return FINAL_RECURSE; if(ast_cansend(ast)) return FINAL_CAN_SEND; if(!ast_mightsend(ast)) return FINAL_NO_SEND; ast_setflag(ast, AST_FLAG_RECURSE_1); int r = check_expr_send(ast, in_final); if(r == FINAL_NO_SEND) { // Mark the body as no send. ast_clearmightsend(ast); } else if((r & FINAL_CAN_SEND) != 0) { // Mark the body as can send. ast_setsend(ast); } ast_clearflag(ast, AST_FLAG_RECURSE_1); return r; }
static void show_send(pass_opt_t* opt, ast_t* ast) { ast_t* child = ast_child(ast); while(child != NULL) { if(ast_cansend(child) || ast_mightsend(child)) show_send(opt, child); child = ast_sibling(child); } if(ast_id(ast) == TK_CALL) { if(ast_cansend(ast)) ast_error(opt->check.errors, ast, "a message can be sent here"); else if(ast_mightsend(ast)) ast_error(opt->check.errors, ast, "a message might be sent here"); } }
static int check_expr_send(ast_t* ast, bool in_final) { int send = FINAL_NO_SEND; if(ast_id(ast) == TK_CALL) send |= check_call_send(ast, in_final); ast_t* child = ast_child(ast); while(child != NULL) { if(ast_mightsend(child)) send |= check_expr_send(child, in_final); child = ast_sibling(child); } return send; }
bool expr_try(pass_opt_t* opt, ast_t* ast) { AST_GET_CHILDREN(ast, body, else_clause, then_clause); // It has to be possible for the left side to result in an error. if((ast_id(ast) == TK_TRY) && !ast_canerror(body)) { ast_error(body, "try expression never results in an error"); return false; } ast_t* body_type = ast_type(body); ast_t* else_type = ast_type(else_clause); ast_t* then_type = ast_type(then_clause); if(is_typecheck_error(body_type) || is_typecheck_error(else_type) || is_typecheck_error(then_type)) return false; ast_t* type = NULL; if(!is_control_type(body_type)) type = control_type_add_branch(type, body); if(!is_control_type(else_type)) type = control_type_add_branch(type, else_clause); if(type == NULL) { if(ast_sibling(ast) != NULL) { ast_error(ast_sibling(ast), "unreachable code"); return false; } type = ast_from(ast, TK_TRY); } // The then clause does not affect the type of the expression. if(is_control_type(then_type)) { ast_error(then_clause, "then clause always terminates the function"); return false; } if(is_type_literal(then_type)) { ast_error(then_clause, "Cannot infer type of unused literal"); return false; } ast_settype(ast, type); // Doesn't inherit error from the body. if(ast_canerror(else_clause) || ast_canerror(then_clause)) ast_seterror(ast); if(ast_cansend(body) || ast_cansend(else_clause) || ast_cansend(then_clause)) ast_setsend(ast); if(ast_mightsend(body) || ast_mightsend(else_clause) || ast_mightsend(then_clause)) ast_setmightsend(ast); literal_unify_control(ast, opt); // Push the symbol status from the then clause to our parent scope. ast_inheritstatus(ast_parent(ast), then_clause); return true; }