Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-09

H2Pas sugestion: new -V param

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

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

Show quoted text 6 lines
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
leledumbo
to silvioprog
We already use something like this in WriteLn/ReadLn, eg: WriteLn('some', 'separed', 'arguments', 'without', 'squared', 'braces').

Different semantics, this one is properly type checked (and each converted to different actual procedure based on its type).

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.

Delphi compatibility that doesn't break existing implementation should be good.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/H2Pas-sugestion-new-V-param-tp5722636p5722637.html Sent from the Free Pascal - General mailing list .