Add casting as a language feature. Have not removed the function yet as we need an inbetween version for the bootstrap

This commit is contained in:
Nathan Braswell
2016-04-18 22:56:29 -04:00
parent d5b930739f
commit cf46fb13af
7 changed files with 71 additions and 9 deletions

View File

@@ -586,6 +586,11 @@ obj c_generator (Object) {
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
// have to pass false to the declaration generator, so can't do it through generate_statement
to_ret.pre = generate_declaration_statement(declaration, enclosing_object, enclosing_func, defer_stack, false).one_string() + ";\n"
if ((function_return_type->is_object() || temp_ident_type->is_object()) &&
( (!function_return_type->is_ref && !function_return_type->equality(temp_ident_type, false)) ||
(function_return_type->is_ref && !function_return_type->equality(temp_ident_type->clone_with_decreased_indirection(), false)) ) )
// note the clone with decreased indirection because of the clone with increased indirection above
error(string("return type does not match: ") + function_return_type->to_string() + ", " + temp_ident_type->to_string());
if (!function_return_type->is_ref && return_value_type->indirection == 0 && (return_value_type->is_adt() || (return_value_type->is_object() && has_method(return_value_type->type_def, "copy_construct", vector(return_value_type->clone_with_indirection(1)))))) {
to_ret.pre += generate_statement(ast_statement_ptr(make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(return_value))))), enclosing_object, enclosing_func, defer_stack).one_string()
} else {
@@ -658,6 +663,9 @@ obj c_generator (Object) {
})
return to_ret
}
fun generate_cast(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>, need_variable: bool): code_triple {
return code_triple("(") + type_to_c(node->cast.to_type) + ")(" + generate(node->cast.value, enclosing_object, enclosing_func, defer_stack, false) + ")"
}
fun generate_value(node: *ast_node, need_variable: bool): code_triple {
var value = node->value.string_value
var to_ret = string()
@@ -924,6 +932,7 @@ obj c_generator (Object) {
ast_node::branching_statement(backing) return generate_branching_statement(node, enclosing_object, enclosing_func, defer_stack)
ast_node::defer_statement(backing) return generate_defer_statement(node, enclosing_object, enclosing_func, defer_stack)
ast_node::match_statement(backing) return generate_match_statement(node, enclosing_object, enclosing_func, defer_stack)
ast_node::cast(backing) return generate_cast(node, enclosing_object, enclosing_func, defer_stack, need_variable)
ast_node::value(backing) return generate_value(node, need_variable)
ast_node::identifier(backing) return generate_identifier(node, enclosing_object, enclosing_func)
}