[fpc-pascal] H2Pas sugestion: new -V param

silvioprog silvioprog at gmail.com
Mon Sep 21 16:31:56 CEST 2015


Hello,

Some days ago, I did a conversion from a C header to Pascal, and I noticed
that H2Pas tool could implement a little bit improvement for Variadic[1]
(or varargs) functions conversion.

Consider the following C function with `varargs`:

int function foo(int bar, ...) {};

I can call this function in two ways:

1. foo("Bar");
2. foo("Bar", "some", "separed", "arguments", "without", "squared",
"braces").

Usually, H2Pas convert it overloaded:

function foo(bar: cint): cint; cdecl ...
function foo(bar: cint; args: array of const): cint; cdecl ...

And it doesn't work when a code needs to work both in FPC and Delphi. For
Delphi compiler, to compile the converted header, you needs to specify the
overload directive:

function foo(bar: cint): cint; cdecl; overload ...
function foo(bar: cint; args: array of const): cint; cdecl;  overload ...

But, even specifying the overload, the converted function doesn't works in
Delphi. But someone can say: why Delphi, Mr? No special reason, currently I
just write code that needs to compile both in FPC and Delphi.

OK, but, how to declare a variadic function in FPC/Delphi? It is very easy:

function foo(bar: cint): cint; verargs; cdecl; ...

Let's go to read its documentation:

This modifier can only be used together with the cdecl modifier, for
> external C procedures. It indicates that the procedure accepts a variable
> number of arguments after the last declared variable. These arguments are
> passed on without any type checking. It is equivalent to using the array of
> const construction for cdecl procedures, without having to declare the
> array of const. The square brackets around the variable arguments do not
> need to be used when this form of declaration is used. [2]


Great! So, using the varargs directive, I can declare and use my function
more C-like. =)

We already use something like this in WriteLn/ReadLn, eg:

WriteLn('some', 'separed', 'arguments', 'without', 'squared', 'braces').

So, after this little explanation, what do you think about add a new param
in H2Pas tool? Something like:

-V Declare C variadic functions with varargs directive.

Thank you!

[1] https://en.wikipedia.org/wiki/Variadic_function
[2] http://www.freepascal.org/docs-html/ref/refsu83.html

-- 
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150921/90728ade/attachment.html>


More information about the fpc-pascal mailing list