Skip to content

Commit

Permalink
assign function results as tuples if the result type is known to be a…
Browse files Browse the repository at this point in the history
… tuple
  • Loading branch information
sylvanc committed Mar 15, 2016
1 parent 192e3ec commit 071db10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/libponyc/codegen/genexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ LLVMValueRef gen_assign_cast(compile_t* c, LLVMTypeRef l_type,
case LLVMHalfTypeKind:
case LLVMFloatTypeKind:
case LLVMDoubleTypeKind:
assert(LLVMGetTypeKind(r_type) == LLVMPointerTypeKind);
return gen_unbox(c, type, r_value);

case LLVMPointerTypeKind:
Expand All @@ -255,7 +256,10 @@ LLVMValueRef gen_assign_cast(compile_t* c, LLVMTypeRef l_type,

case LLVMStructTypeKind:
if(LLVMGetTypeKind(r_type) == LLVMPointerTypeKind)
{
r_value = gen_unbox(c, type, r_value);
assert(LLVMGetTypeKind(LLVMTypeOf(r_value)) == LLVMStructTypeKind);
}

return assign_to_tuple(c, l_type, r_value, type);

Expand Down
18 changes: 16 additions & 2 deletions src/libponyc/codegen/genfun.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,22 @@ static LLVMValueRef genfun_fun(compile_t* c, gentype_t* g, const char *name,
LLVMTypeRef f_type = LLVMGetElementType(LLVMTypeOf(func));
LLVMTypeRef r_type = LLVMGetReturnType(f_type);

LLVMValueRef ret = gen_assign_cast(c, r_type, value,
ast_childidx(fun, 4));
// If the result type is known to be a tuple, do the correct assignment
// cast even if the body type is not a tuple.
ast_t* body_type = ast_type(body);
ast_t* result_type = ast_childidx(fun, 4);

if(ast_id(result_type) == TK_TUPLETYPE)
body_type = result_type;

LLVMValueRef ret = gen_assign_cast(c, r_type, value, body_type);

if(ret == NULL)
{
ast_free_unattached(fun);
return NULL;
}

LLVMBuildRet(c->builder, ret);
}

Expand Down

0 comments on commit 071db10

Please sign in to comment.