Cleaned up generate_function_call. It's quite understandable now!

This commit is contained in:
Nathan Braswell
2017-08-18 10:05:12 -04:00
parent 61feff50a8
commit 4921371afe

View File

@@ -547,12 +547,10 @@ obj c_generator (Object) {
var func_name = string()
var call_string = code_triple()
var func_return_type = get_ast_type(node)
if (func_return_type->is_ref) {
error("still ref in function calling")
}
// handle the obj.method() style of method call
// Note that this function may not be 100% reliable
// There may be a problem around detecting properly
// inside of lambdas. Gotta move it out so the interpreter
// can use it too, though.
var dot_style_method_call = is_dot_style_method_call(node)
if (dot_style_method_call) {
@@ -560,22 +558,20 @@ obj c_generator (Object) {
// don't add & if it was ->
if (node->function_call.func->function_call.func->function.name == ".")
call_string += "&"
// having a null defer stack should be ok, as the only things we should get through here are identifiers and function calls
// XXX should it? wouldn't function calls be a problem?
// should this be true if ref?
call_string += generate(node->function_call.func->function_call.parameters[0], enclosing_object, enclosing_func, true)
} else if (is_identifier(node->function_call.func) || is_function(node->function_call.func)) {
} else if (is_function(node->function_call.func)) {
// we handle the case when it's not this later, i.e. it's a lambda returned from another function or something
func_name = generate_function(node->function_call.func, enclosing_object, enclosing_func, false, false).one_string()
} else {
// not function, so we must be an identifier or function call return or something
func_name = generate(node->function_call.func, enclosing_object, enclosing_func, false).one_string()
}
// handle method call from inside method of same object
if (!dot_style_method_call && enclosing_object) {
if (method_in_object(node->function_call.func, enclosing_object)) {
/*if (enclosing_func && enclosing_func->function.closed_variables.size())*/
/*call_string += "(*(closure_data->this))";*/
/*else*/
call_string += "this";
call_string += "this";
}
}
@@ -608,7 +604,6 @@ obj c_generator (Object) {
// for checking if we pass in a ref
var func_type = get_ast_type(node->function_call.func)
// regular parameter generation
// parameters.for_each(fun(param: *ast_node) {
for (var i = 0; i < parameters.size; i++;) {
var param = parameters[i]
var in_function_param_type = null<type>()
@@ -621,78 +616,26 @@ obj c_generator (Object) {
call_string += ", "
if (in_function_param_type->is_ref) {
/*for (var n = node->function_call.func; n; get_from_scope(n, "~enclosing_scope"))) {*/
/*}*/
error(string("problem :") + (node->function_call.func) cast int + " " + get_fully_scoped_name(node->function_call.func) + ": still ref in function calling, func_type: " + func_type->to_string())
}
/*call_string += "&"*/
var param_type = get_ast_type(param)
call_string += generate(param, enclosing_object, enclosing_func, in_function_param_type->is_ref)
}
var pre_call = string()
// temporary returns if we're asked for them or we need them for destruct
if (!func_return_type->is_ref && !func_return_type->is_void() && need_variable) {
var pre_call = string()
// kind of ugly combo here of
var temp_ident = ast_identifier_ptr(string("temporary_return") + get_id(), func_return_type, null<ast_node>())
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
call_string.pre += generate_declaration_statement(declaration, enclosing_object, enclosing_func, false).one_string() + ";\n"
pre_call = generate_identifier(temp_ident, enclosing_object, enclosing_func).one_string()
}
var ref_pre = string()
var ref_post = string()
if (func_return_type->is_ref) {
error("still ref in function calling")
ref_pre += "(*"
ref_post += ")"
}
if (!is_function(node->function_call.func)) {
/*if (!is_function(node->function_call.func) || node->function_call.func->function.closed_variables.size()) {*/
// not function, so we must be an identifier or function call return or something
if (!dot_style_method_call) {
// and also is closure style, so need special call
/*if (!func_type->is_raw) {*/
/*// lambda*/
/*if (pre_call == "" && (!func_return_type->is_void() || func_return_type->indirection)) {*/
/*var temp_ident = ast_identifier_ptr(string("temporary_return") + get_id(), func_return_type, null<ast_node>())*/
/*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*/
/*call_string.pre += generate_declaration_statement(declaration, enclosing_object, enclosing_func, false).one_string() + ";\n"*/
/*pre_call = generate_identifier(temp_ident, enclosing_object, enclosing_func).one_string()*/
/*}*/
/*var name_temp = generate(node->function_call.func, enclosing_object, enclosing_func, false)*/
/*call_string.pre += name_temp.pre*/
/*call_string.post += name_temp.post*/
/*func_name = name_temp.value*/
/*// should not have return var because is void*/
/*var pre_call_plus = pre_call*/
/*if (pre_call_plus != "")*/
/*pre_call_plus += " = "*/
/*var func_type = get_ast_type(node->function_call.func)*/
/*call_string.pre += string("if (")+func_name+".data) " + pre_call_plus + " ((" + type_to_c(func_type) + "_with_data) "+func_name+".func)("+func_name +".data"*/
/*if (call_string.value != "")*/
/*call_string.pre += string(",") + call_string.value*/
/*call_string.pre += ");\n"*/
/*call_string.pre += string("else ") + pre_call_plus + " ((" + type_to_c(func_type) + "_without_data) " + func_name+".func)(" + call_string.value + ");\n"*/
/*call_string.value = pre_call*/
/*call_string.value = ref_pre + call_string.value + ref_post*/
/*return call_string*/
/*} else {*/
// this is a raw function value
func_name = generate(node->function_call.func, enclosing_object, enclosing_func, false).one_string()
/*}*/
}
}
if (pre_call != "") {
call_string.pre += pre_call + " = " + func_name + "(" + call_string.value + ");"
call_string.value = pre_call
} else {
call_string.value = func_name + "(" + call_string.value + ")"
}
call_string.value = ref_pre + call_string.value + ref_post
return call_string
}
fun generate_compiler_intrinsic(node: *ast_node): code_triple {