<div dir="ltr"><div><div>Hello,</div><div><br></div><div>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.</div></div><div><br></div><div>Consider the following C function with `varargs`:</div><div><br></div><div>int function foo(int bar, ...) {};</div><div><br></div><div>I can call this function in two ways:</div><div><br></div><div>1. foo("Bar");</div><div>2. foo("Bar", "some", "separed", "arguments", "without", "squared", "braces").<br></div><div><br></div><div>Usually, H2Pas convert it overloaded:</div><div><br></div><div>function foo(bar: cint): cint; cdecl ...</div><div>function foo(bar: cint; args: array of const): cint; cdecl ...<br></div><div><br></div><div>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:</div><div><br></div><div><div>function foo(bar: cint): cint; cdecl; overload ...</div><div>function foo(bar: cint; args: array of const): cint; cdecl; overload ...</div></div><div><br></div><div>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.</div><div><br></div><div>OK, but, how to declare a variadic function in FPC/Delphi? It is very easy:</div><div><br></div><div><div>function foo(bar: cint): cint; verargs; cdecl; ...</div></div><div><br></div><div>Let's go to read its documentation:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">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]</blockquote><div><br></div><div>Great! So, using the varargs directive, I can declare and use my function more C-like. =)</div><div><br></div><div>We already use something like this in WriteLn/ReadLn, eg:</div><div><br></div><div>WriteLn('some', 'separed', 'arguments', 'without', 'squared', 'braces').</div><div><br></div><div>So, after this little explanation, what do you think about add a new param in H2Pas tool? Something like:</div><div><br></div><div>-V<span class="" style="white-space:pre"> Declare C variadic functions with varargs directive.</span></div><div><br></div><div>Thank you!</div><div><br></div><div>[1] <a href="https://en.wikipedia.org/wiki/Variadic_function">https://en.wikipedia.org/wiki/Variadic_function</a></div><div>[2] <a href="http://www.freepascal.org/docs-html/ref/refsu83.html">http://www.freepascal.org/docs-html/ref/refsu83.html</a></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Silvio Clécio</div></div></div>
</div>