cc65: Invalid parameter handling for K&R functions
kugelfuhr opened this issue · comments
The following code should compile but doesn't:
int foo(a, b) { return a + b; }
int main()
{
foo(1, 2, 3);
return 0;
}
Gives:
test.c:5: Error: Too many arguments in function call
This is related to the problem described in #2014: The compiler checks the parameter list when a K&R function is called, but it shouldn't. The information given by the function definition
int foo(a, b) { return a + b; }
is equivalent to
int foo();
If the expression that denotes the called function has a type that does not include a prototype, the
integer promotions are performed on each argument, and arguments that have type float are
promoted to double. These are called the default argument promotions. If the number of arguments
does not equal the number of parameters, the behavior is undefined.
So I think the compiler can just reject the code according to the standard.
OK. So it is actually easier than I thought to implement the "K&R function definition as empty-parameter-list function declaration", and it makes some other things easier.
Still there are the questions:
When there is indeed a K&R function definition (but no prototype) visible in the scope, should we give diagnosis on the call to the function if something is wrong with the arguments eg. incompatible type, wrong count etc.? If we would like to accept such code, should we at least give a warning on it? If the type of the argument is wrong eg. int
when the function expects a long
, should we try to fix it? And if the pushed size of the argument could be wrong, should we try to "fix" the stack, or just compile and let it blow up?