Hi Duncan,<br><br>Yes, I forgot to leave out that I'd like to see 'size_t' mapped to CSize.<br><br>As a (dirty) workaround, one can use 'castPtr' as the marshaler when dealing with pointers to 'size_t'. I'm a little more concerned about FunPtr's (though 'castPtr' still makes the issue go away).<br>
<br>Here's my specific case dealing with function pointers:<br><br>In 64bit world, it looks like this:<br><div style="margin-left: 40px;"><span style="font-family: courier new,monospace;">type ReadableCallback = Ptr () -> Ptr CUChar -> <b>Ptr CULong</b> -> IO CUInt</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{#fun gcry_ac_io_init_readable_callback {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> id `ACIO',</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> id `FunPtr ReadableCallback',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> id `Ptr ()'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> } -> `()'#}</span><br></div><br>In 32bit world, it looks like this:<br><div style="margin-left: 40px;"><span style="font-family: courier new,monospace;">
type ReadableCallback = Ptr () -> Ptr CUChar -> <b>Ptr CUInt</b> -> IO CUInt</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
{#fun gcry_ac_io_init_readable_callback {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
id `ACIO',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
id `FunPtr ReadableCallback',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
id `Ptr ()'</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
} -> `()'#}</span><br></div><br>I'd really like it if I could use 'Ptr CSize' (which corresponds to the 'size_t * ptr' in the C code).<br><br>My current workaround (which doesn't use a cast in Haskell) is to redefine the function pointer type in a C
header file to use 'unsigned long' instead of 'size_t', but this gets
cludgy in a hurry. If c2hs could translate to CSize directly, I could pass a pointer to CSize.<br><br>Is it really such a problem to make the conversion? My assumption is that CSize would match 'size_t' for the specific architecture.<br>
<br>Thanks for your comment.<br><br>/jve<br><br><br><div class="gmail_quote">On Thu, Oct 1, 2009 at 12:37 PM, Duncan Coutts <span dir="ltr"><<a href="mailto:duncan.coutts@googlemail.com">duncan.coutts@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On Thu, 2009-10-01 at 10:20 -0400, John Van Enk wrote:<br>
> Hello List,<br>
><br>
> I'm running into a problem with c2hs and how it parses the C typedef<br>
> 'size_t'. On 32bit systems, this ends up being parsed as a CUInt. On<br>
> 64bit systems, this ends up as a CULong. This gets especially sticky<br>
> with function pointers.<br>
<br>
</div>Right. Of course that's because on those different platforms size_t<br>
really is a typedef for unsigned int or unsigned long.<br>
<div class="im"><br>
> I see there is a ticket open for this:<br>
> <a href="http://hackage.haskell.org/trac/c2hs/ticket/20" target="_blank">http://hackage.haskell.org/trac/c2hs/ticket/20</a><br>
><br>
> Has any one else run into this issue? Is there a good workaround that<br>
> doesn't involve writing a C function/typedef for each collision?<br>
<br>
</div>So what you would want, presumably, is to map the "size_t" typedef to<br>
the Haskell type Foreign.C.Types.CSize, rather than what c2hs discovers<br>
as the actual raw type of "size_t". Then you're making the promise that<br>
unsigned long, or unsigned int really really does match<br>
Foreign.C.Types.CSize.<br>
<br>
Currently c2hs has no support for remapping basic types like that.<br>
<br>
As for a workaround, just use fromIntegral to convert to CSize. You know<br>
this is always possible because you know CSize matches CUInt or CULong<br>
on the appropriate platforms.<br>
<br>
$ cat foo.h<br>
#include <stddef.h><br>
size_t foo(void);<br>
<br>
$ cat foo.c<br>
#include "foo.h"<br>
size_t foo(void) { return 42; }<br>
<br>
$ cat foo.chs<br>
<br>
import Foreign.C.Types<br>
foo :: IO CSize<br>
foo = fmap fromIntegral {# call foo as bar #}<br>
<br>
$ gcc -c foo.c<br>
$ c2hs foo.h foo.chs<br>
$ ghci foo.hs -fffi foo.o<br>
Main> foo<br>
42<br>
<font color="#888888"><br>
<br>
Duncan<br>
<br>
</font></blockquote></div><br>