No, that’s all understood. For some reason, I just got the idea at some point that @ccall defers to ccall (instead of making an Expr(:foreigncall) directly). Perhaps because of statements like these:
But they’re not the same! @ccall can call varargs functions with arbitrary (mixed) types, whereas varargs arguments must all be of the same type with ccall. These are equivalent:
Edited to add: I just realized there does seem to be one thing that ccall can do and @ccall can’t, which is specifying a calling convention? (Seems to be hard-coded to :ccall, if I understand the code correctly.) Should be straightforward to add, but it’s not in the current implementation.
Maybe that’s illegal and a bug, maybe everything is passed as a *void, I don’t know Could also just be working due to printf taking its type information from the format string and not needing the “”“type safety”“” that a same-typed va_list would provide.
Anyway, like I said, they’re the same. They lower to the same Expr(:foreigncall) machinery. The only difference is in who ends up building that Expr, the parser directly or the @ccall macro on its own.
I did find these while looking for a PR that made this work, if that exists. I am on the side of Jeff here though, so…
I’m moderately sure that won’t work on Windows, and it “accidentally” works on Linux because it uses a different calling convention, but it can’t be relied upon. The @ccallmacro has a much nicer interface, on the other hand there are things that you can’t do with that instead you can with ccall (e.g. specifying the calling convention).
This problem unfortunately extends to my ccaller code above; we’d want a mechanism for specifying where the variadic arguments begin.
My ccaller code also has no provisions for specifying calling convention—although it seems like the place to start is putting it in the @ccall macro. If that is done, afaict @ccall’s functionality would be a strict superset of ccall, without requiring such weird hacks to the language semantics.
Yes, we could have added support for other calling conventions in the original @ccall PR. But we skipped that because it was a lot of discussion already to get to the point of merging the current version. And it seemed better to merge it than to get stuck working out notation for calling conventions or other attributes (Ccallmacro by ninjaaron · Pull Request #32748 · JuliaLang/julia · GitHub)
If someone wanted to figure out the nitty gritty detail of calling convention for FFI calls, I’m sure that would be a great PR. Last time we weren’t sure if we needed a general annotations syntax for defining side effects, but now we have @assume_effects ... @ccall. I’m still not sure whether there’s other annotations which might be needed.
Sorry, it can’t be linked to directly. I meant this part:
System Independent Types
C name
Fortran name
Standard Julia Alias
Julia Base Type
…
…
…
…
va_arg
Not supported
... (variadic function specification)
T... (where T is one of the above types, when using the ccall function)
... (variadic function specification)
; va_arg1::T, va_arg2::S, etc. (only supported with @ccall macro)
As others have pointed out, some calling conventions differentiate between varargs and “normal” args. The difference is in the nreq field in the Expr(:foreigncall):
Well, his suggestion doesn’t work when the number of arguments is unknown and can be arbitrary, which kind of defeats the whole point of varargs functions.
Yes, that makes sense - good to know that my gut feel about this being dangerous was right I don’t think there’s a (cross platform) way of checking preemptively whether what we want to call is variadic, right? Aaah, the perils of C ABIs…
Gotcha - I took that part of the docs as a “this is how we support it” and not as a “other ways don’t work”. Maybe the difference in calling conventions should be added here as a warning, to make it clear that just passing the extra vararg arguments as regular arguments does not work?
Yeah, I was more referring to the “can’t be done portably” part
Ah OK – I actually didn’t understand that part. What would the portability issue be? As far as I can tell, the original issue was just asking about turning a Julia tuple into the arguments to a ccall, i. e. exactly the same as my original question, which is all just a matter of syntax.
For separating args from varargs, I see appeal in using …, but it seems like : would be more ergonomic. Subjective of course. You could use any other operator too, like |, >, etc, or maybe wrap the varargs in their own tuple.
For the method using Pairs like 2=>Cint, it feels a bit backwards; Cint=>2 feels more natural to me. Another idea would be to use Cint: 2, though that’s likely to get pushback as it would entail type piracy, but that could be solved by getting it into Base.
As discussed in this thread, there might be difficulties in getting some programs that use such a generated function to compile statically. That said, ccall already seems to cause trouble for static compilation. I haven’t dug in to understand why.
If we’re willing to diverge from existing semantics a bit more, maybe something like this becomes compelling:
I already have .. as an alias for … (apparently forgot to document it, though). : would be another option, but .. seems clearer to me.
I agree that it can seem strange because of the “arrow” syntax, but I found it important to keep the same order as in other Julia syntax, which is value::Type. I think of it as “2 is converted to Cint”, which is true!
I wanted to keep it simple since the whole @generated construction is already somewhat obscure, so I didn’t think about stuff like hijacking :.
I do like this! Hm, so many options. It seems more confusing than anything else to have 4 different ways of writing the same thing, but on the other hand, this is a bit of an experiment, so I guess it wouldn’t hurt trying it?