Decay an array to a pointer in the func param context

This commit is contained in:
Rui Ueyama 2019-08-14 11:00:05 +09:00
parent 29ed294906
commit 79632219d0
2 changed files with 17 additions and 3 deletions

16
parse.c
View File

@ -380,9 +380,19 @@ static Type *func_params(Token **rest, Token *tok, Type *ty) {
while (!equal(tok, ")")) {
if (cur != &head)
tok = skip(tok, ",");
Type *basety = declspec(&tok, tok, NULL);
Type *ty = declarator(&tok, tok, basety);
cur = cur->next = copy_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.
if (ty2->kind == TY_ARRAY) {
Token *name = ty2->name;
ty2 = pointer_to(ty2->base);
ty2->name = name;
}
cur = cur->next = copy_type(ty2);
}
ty = func_type(ty);

View File

@ -53,6 +53,8 @@ _Bool bool_fn_sub(_Bool x) { return x - 1; }
static int static_fn() { return 3; }
int param_decay(int x[]) { return x[0]; }
int main() {
ASSERT(3, ret3());
ASSERT(8, add2(3, 5));
@ -86,6 +88,8 @@ int main() {
ASSERT(3, static_fn());
ASSERT(3, ({ int x[2]; x[0]=3; param_decay(x); }));
printf("OK\n");
return 0;
}