Decay a function to a pointer in the func param context

This commit is contained in:
Rui Ueyama 2020-08-31 21:28:53 +09:00
parent d06a8ac6e6
commit c5953ba132
2 changed files with 13 additions and 3 deletions

12
parse.c
View File

@ -569,12 +569,18 @@ static Type *func_params(Token **rest, Token *tok, Type *ty) {
Type *ty2 = declspec(&tok, tok, NULL);
ty2 = declarator(&tok, tok, ty2);
// "array of T" is converted to "pointer to T" only in the parameter
// context. For example, *argv[] is converted to **argv by this.
Token *name = ty2->name;
if (ty2->kind == TY_ARRAY) {
Token *name = ty2->name;
// "array of T" is converted to "pointer to T" only in the parameter
// context. For example, *argv[] is converted to **argv by this.
ty2 = pointer_to(ty2->base);
ty2->name = name;
} else if (ty2->kind == TY_FUNC) {
// Likewise, a function is converted to a pointer to a function
// only in the parameter context.
ty2 = pointer_to(ty2);
ty2->name = name;
}
cur = cur->next = copy_type(ty2);

View File

@ -112,6 +112,8 @@ int (*fnptr(int (*fn)(int n, ...)))(int, ...) {
return fn;
}
int param_decay2(int x()) { return x(); }
int main() {
ASSERT(3, ret3());
ASSERT(8, add2(3, 5));
@ -187,6 +189,8 @@ int main() {
ASSERT(7, ({ int (*fn)(int,int) = add2; fn(2,5); }));
ASSERT(6, fnptr(add_all)(3, 1, 2, 3));
ASSERT(3, param_decay2(ret3));
printf("OK\n");
return 0;
}