c2hs / Language.C feature to discover cdecl / stdcall

It was pointed out to me yesterday that it would be rather useful if c2hs could automatically work out if the ffi import for a {# call/fun #} hook should be ccall or stdcall calling convention. It would be useful because some Windows libs use stdcall. Some, otherwise portable, libs use ccall on most platforms and stdcall on Windows. OpenGL is an example of the latter. In principle this is possible because, in GNU C at least, the calling convention in indicated in a function __attribute__((__stdcall__)). So the first job would be for Language.C to keep the __attribute__'s in the abstract syntax tree. They already get parsed, but at least last time I looked they were not entered into the AST. The attribute syntax is pretty uniform so a generic representation in the AST would be fine. Any code that interpreted the attributes would just have to match on known string values. That's the most work. With that done it should be relatively straightforward for c2hs to inspect the set of attributes to see if it's got a stdcall attribute and if so generate an appropriate ffi import. Duncan

On 02.04.2009, at 11:34, Duncan Coutts wrote:
It was pointed out to me yesterday that it would be rather useful if c2hs could automatically work out if the ffi import for a {# call/ fun #} hook should be ccall or stdcall calling convention ... So the first job would be for Language.C to keep the __attribute__'s in the abstract syntax tree. They already get parsed, but at least last time I looked they were not entered into the AST. Hi Duncan,
language.c records attributes already :) (implemented during SoC last year). See: http://hackage.haskell.org/packages/archive/language-c/0.3.1.1/doc/html/Lang... examples $ cat test.i __attribute__((__stdcall__)) int f(); examples $ ./ParseAndPrint test.i __attribute__((__stdcall__)) int f(); In the AST, this kind of attribute is recorded like this: * CDecl * declspecs-list + CTypeQual + CAttrQual * CAttr "__stdcall__" [] nodeinfo * declr-list * nodeinfo
The attribute syntax is pretty uniform so a generic representation in the AST would be fine. Any code that interpreted the attributes would just have to match on known string values. Right, it is easy to check whether the "stdcall" attribute is present. Is the current language.c support sufficient for implementing this request ?
benedikt

On Thu, 2009-04-02 at 12:09 +0200, Benedikt Huber wrote:
On 02.04.2009, at 11:34, Duncan Coutts wrote:
So the first job would be for Language.C to keep the __attribute__'s in > the abstract syntax tree. They already get parsed, but at least last time I looked they were not entered into the AST.
language.c records attributes already :) (implemented during SoC last year).
Excellent. :-)
Right, it is easy to check whether the "stdcall" attribute is present. Is the current language.c support sufficient for implementing this request ?
Sounds like it is, great! :-) Duncan
participants (2)
-
Benedikt Huber
-
Duncan Coutts