import symbol:* import tree:* import vector:* import map:* import util:* import string:* import mem:* import io:* import ast_nodes:* import ast_transformation:* import pass_common:* import hash_set:* // Here's how we lower objects // 1 first, we make sure that all functions, if statments, while loops and for loops have code blocks // as children, not just statements. // 1 during the same pass, we "uglify" for loops and while loops so that functions that need pre and post statements always // have a code block to insert them into that makes sure that they get run. // 2 in another pass (more complicated because different children have different parent scopes) // we transform the short circuit operators // 3 then on the pass up the chain, at function calls we add in they copy_construct in and defer destruct out // temporaries. // 3 this is also when we add in defer destructs for function parameters (inside the function) and declaration statements fun obj_lower(name_ast_map: *map,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree>) { var visited1 = hash_set<*ast_node>() var visited2 = hash_set<*ast_node>() var visited3 = hash_set<*ast_node>() var functions_visited_for_construct_in_destruct_out = hash_set<*ast_node>() name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree,*ast_node>) { // Pass 1 var ensure_block_and_munge = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::function(backing) if (backing.body_statement && !is_code_block(backing.body_statement)) backing.body_statement = ast_code_block_ptr(backing.body_statement) ast_node::if_statement(backing) { if (!is_code_block(backing.then_part)) backing.then_part = ast_code_block_ptr(backing.then_part) if (backing.else_part && !is_code_block(backing.else_part)) backing.else_part = ast_code_block_ptr(backing.else_part) } // no need for case because it's already been lowered ast_node::while_loop(backing) { if (!is_code_block(backing.statement)) backing.statement = ast_code_block_ptr(backing.statement) var condition = backing.condition backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean())) // objects do not coerce to booleans, so it should be ok for this not to be a ref var condition_ident = ast_identifier_ptr("condition_temp", get_ast_type(condition), backing.statement) backing.statement->code_block.children.add(0, ast_declaration_statement_ptr(condition_ident, condition)) var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident))) condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt()) backing.statement->code_block.children.add(1, condition_if) } ast_node::for_loop(backing) { if (!is_code_block(backing.body)) backing.body = ast_code_block_ptr(backing.body) add_before_in(backing.init, node, parent_chain->top()) backing.init = null() // the do_update goes in the block above the for var update_ident = ast_identifier_ptr("do_update", type_ptr(base_type::boolean()), parent_chain->top()) add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(string("false"), type_ptr(base_type::boolean()))), node, parent_chain->top()) var update_if = ast_if_statement_ptr(update_ident) update_if->if_statement.then_part = ast_code_block_ptr(backing.update) backing.update = null() backing.body->code_block.children.add(0, update_if) backing.body->code_block.children.add(1, ast_assignment_statement_ptr(update_ident, ast_value_ptr(string("true"), type_ptr(base_type::boolean())))) var condition = backing.condition backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean())) // objects do not coerce to booleans, so it should be ok for this not to be a ref var condition_ident = ast_identifier_ptr("condition_temp", get_ast_type(condition), backing.body) backing.body->code_block.children.add(2, ast_declaration_statement_ptr(condition_ident, condition)) var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident))) condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt()) backing.body->code_block.children.add(3, condition_if) } } } run_on_tree(ensure_block_and_munge, empty_pass_second_half(), syntax_ast_pair.second, &visited1) // Pass 2 var short_circut_op: fun(*ast_node,*stack<*ast_node>,*hash_set<*ast_node>): bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>, visited: *hash_set<*ast_node>): bool { match(*node) { ast_node::function_call(backing) { var func_name = string() if (is_function(backing.func)) { func_name = backing.func->function.name if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" || func_name == "<" || func_name == ">" || func_name == "<=" || func_name == ">=" || func_name == "==" || func_name == "!=" || func_name == "%" || func_name == "^" || func_name == "|" || func_name == "&" || func_name == "." || func_name == "->" || func_name == "." || func_name == "->" || func_name == "[]" || func_name == "++p" || func_name == "--p" || func_name == "*" || func_name == "&" ) return true } if (func_name == "||" || func_name == "&&") { var enclosing_block_idx = parent_chain->index_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i);) var short_circuit_result = ast_identifier_ptr("short_circut_result", type_ptr(base_type::boolean()), parent_chain->from_top(enclosing_block_idx)) var short_circuit_declaration = ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0]) var condition = short_circuit_result if (func_name == "||") condition = make_operator_call("!", vector(condition)) var short_circuit_if = ast_if_statement_ptr(condition) // how to get proper parent scoping working for this part short_circuit_if->if_statement.then_part = ast_code_block_ptr(ast_assignment_statement_ptr(short_circuit_result, backing.parameters[1])) add_before_in(short_circuit_declaration, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx)) add_before_in(short_circuit_if, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx)) replace_with_in(node, short_circuit_result, parent_chain) var shorter_tree = stack_from_vector( parent_chain->data.slice(0, parent_chain->size()-enclosing_block_idx)) run_on_tree_helper(short_circut_op, empty_pass_second_half(), short_circuit_declaration, &shorter_tree, visited) run_on_tree_helper(short_circut_op, empty_pass_second_half(), short_circuit_if, &shorter_tree, visited) return false } } } return true } run_on_tree(short_circut_op, empty_pass_second_half(), syntax_ast_pair.second, &visited2) // Pass 3 var construct_in_destruct_out = fun(node: *ast_node, parent_chain: *stack<*ast_node>) { match(*node) { ast_node::function_call(backing) { if (is_function(backing.func)) { var func_name = backing.func->function.name if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" || func_name == "<" || func_name == ">" || func_name == "<=" || func_name == ">=" || func_name == "==" || func_name == "!=" || func_name == "%" || func_name == "^" || func_name == "|" || func_name == "&" || func_name == "." || func_name == "->" || func_name == "." || func_name == "->" || func_name == "[]" || func_name == "++p" || func_name == "--p" || func_name == "*" || func_name == "&" || func_name == "||" || func_name == "&&" || func_name == "!" ) return } var enclosing_block_idx = parent_chain->index_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i);) var replace_before: *ast_node if (enclosing_block_idx > 0) replace_before = parent_chain->from_top(enclosing_block_idx-1) else replace_before = node var replace_in = parent_chain->from_top(enclosing_block_idx) var func_type = get_ast_type(backing.func) for (var i = 0; i < backing.parameters.size; i++;) { var param = backing.parameters[i] var in_function_param_type = null() // grab type from param itself if we're out of param types (because variadic function) if (i < func_type->parameter_types.size) in_function_param_type = func_type->parameter_types[i] else in_function_param_type = get_ast_type(param)->clone_without_ref() var param_type = get_ast_type(param) if (!in_function_param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "copy_construct", vector(param_type->clone_with_indirection(1))))) { var temp_ident = ast_identifier_ptr("temporary_param_boom", param_type->clone_without_ref(), null()) var declaration = ast_declaration_statement_ptr(temp_ident, null()) var copy_in = make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(param)))) add_before_in(declaration, replace_before, replace_in) add_before_in(copy_in, replace_before, replace_in) backing.parameters[i] = temp_ident } } var func_return_type = func_type->return_type /*if (!func_return_type->is_ref && func_return_type->indirection == 0 && (func_return_type->is_object() && has_method(func_return_type->type_def, "destruct", vector<*type>()))) {*/ if (!func_return_type->is_ref && func_return_type->indirection == 0 && func_return_type->is_object()) { var temp_return = ast_identifier_ptr("temporary_return_boomchaka", func_return_type, null()) var declaration = ast_declaration_statement_ptr(temp_return, node) add_before_in(declaration, replace_before, replace_in) if (has_method(func_return_type->type_def, "destruct", vector<*type>())) { add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vector<*ast_node>())), replace_before, replace_in) } replace_with_in(node, temp_return, parent_chain) } } ast_node::function(backing) { // Because of how iteration is done now, we might touch functions multiple times (binding-like iteration in a DFS) // To deal with this, we keep a visited set. if (functions_visited_for_construct_in_destruct_out.contains(node)) return; functions_visited_for_construct_in_destruct_out.add(node) var order = 0; backing.parameters.for_each(fun(param: *ast_node) { var param_type = get_ast_type(param) if (!param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "destruct", vector<*type>()))) { // the first pass ensures a code_block child backing.body_statement->code_block.children.add(order++, ast_defer_statement_ptr(make_method_call(param, "destruct", vector<*ast_node>()))) } }) } ast_node::declaration_statement(backing) { var ident_type = get_ast_type(backing.identifier) if (is_translation_unit(parent_chain->top()) || is_type_def(parent_chain->top())) return; if (!ident_type->is_ref && ident_type->indirection == 0 && ident_type->is_object()) { if (backing.expression && has_method(ident_type->type_def, "copy_construct", vector(get_ast_type(backing.expression)->clone_with_increased_indirection()))) { var temp_cpy_ctst = ast_identifier_ptr("temp_declaration_copy_construct", get_ast_type(backing.expression)->clone_without_ref(), null()) var declaration = ast_declaration_statement_ptr(temp_cpy_ctst, backing.expression) add_after_in(make_method_call(backing.identifier, "copy_construct", vector(make_operator_call("&", vector(temp_cpy_ctst)))), node, parent_chain->top()) // do second so the order's right add_after_in(declaration, node, parent_chain->top()) backing.expression = null() } if (has_method(ident_type->type_def, "destruct", vector<*type>())) { add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vector<*ast_node>())), node, parent_chain->top()) } } } } } run_on_tree(empty_pass_first_half(), construct_in_destruct_out, syntax_ast_pair.second, &visited3) }) }