Skip to content

Commit

Permalink
Implement func decl which returns struct
Browse files Browse the repository at this point in the history
This fixes issue rui314#75
  • Loading branch information
shinh committed Dec 11, 2016
1 parent 1121cdb commit ac1c468
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 14 additions & 2 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1937,12 +1937,24 @@ static Type *read_declarator_array(Type *basety) {
return make_array_type(t, len);
}

static Type *read_declarator_func(Type *basety, Vector *param) {
static Vector *param_types(Vector *params);

static Type *read_declarator_func(Type *basety, Vector *params) {
if (basety->kind == KIND_FUNC)
error("function returning a function");
if (basety->kind == KIND_ARRAY)
error("function returning an array");
return read_func_param_list(param, basety);
Type *functype = read_func_param_list(params, basety);
if (is_large_struct(functype->rettype)) {
Vector *new_params = make_vector();
vec_push(new_params,
ast_lvar(make_ptr_type(functype->rettype), make_tempname()));
if (params)
vec_append(new_params, params);
params = new_params;
functype->params = param_types(params);
}
return functype;
}

static Type *read_declarator_tail(Type *basety, Vector *params) {
Expand Down
13 changes: 12 additions & 1 deletion test/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ struct abi_check {
struct abi_check_nest z;
};

typedef struct abi_check_small {
struct abi_check_small {
int x;
};

Expand Down Expand Up @@ -400,6 +400,16 @@ static void struct_call_small(void) {
expect(43, struct_arg_func_small(1, (struct abi_check_small){ 42 }));
}

static struct abi_check return_struct_func_decl(struct abi_check s, int x);

static void struct_return_with_decl(void) {
expect(42, return_struct_func_decl((struct abi_check){}, 42).x);
}

static struct abi_check return_struct_func_decl(struct abi_check s, int x) {
return (struct abi_check){ x };
}

void testmain() {
print("struct");
t1();
Expand Down Expand Up @@ -433,4 +443,5 @@ void testmain() {
struct_global();
struct_return();
struct_call_small();
struct_return_with_decl();
}

0 comments on commit ac1c468

Please sign in to comment.