K&R-style variadic functions

Regan Heath regan at netmail.co.nz
Tue Jul 17 05:32:08 PDT 2012


On Tue, 17 Jul 2012 12:43:46 +0100, Jacob Carlborg <doob at me.com> wrote:

> On 2012-07-17 09:06, Mehrdad wrote:
>
>> My eyes!! They're bleeding!!
>
> First I wanted to know if my interpretation is correct and then I was  
> trying to figure out how my tool should behave if it encounters a  
> function like this.

After a bit of googling and a test with my local MSVC9 I think old-style  
variadics look like this:

#include <varargs.h>
#include <stdio.h>

void foo(va_alist)
	va_dcl
{
	va_list p;
	va_start(p);
	vprintf("%d %d %d\n", p);
}

void main()
{
	foo(1, 2, 3);
}

(the above runs and outputs "1 2 3" on the console)

The same syntax is/was supported by GNU C, see:
http://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_chapter/libc_34.html#SEC676


I believe, if you see an "old-style" function declaration in a header file  
like:

	int foo ();

that you can't actually assume anything about it's parameters, it may have  
/any/ number of parameters, and may or may not be variadic.

e.g. see: http://msdn.microsoft.com/en-us/library/efx873ys.aspx

The "declaration" syntax shown there for a function with 2 arguments is  
simply:

	double old_style();

In short, I don't think you can produce a valid .di/d file for old-style  
declarations automatically, it will require a manual step where the user  
enters the function type-arguments.


However, if you see an old-style:

	int foo(void);

you can be sure it has 0 parameters and can happily produce:

extern (C) int foo();


So, I think you should assume no old-style function declarations but have  
a command line option/mode which assumes all functions declarations are  
old-style.  In this mode you would output something like..

extern (C) <return-type> <function-name>(/*TODO: add args here*/);

and require the user manually find and define these.

I can't think of any other way you could do it.

R

p.s. I found this copy of the C spec online:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
section 6.7.5.3 appears to deal with function declarations.

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list