[C2hs] Pointers to structs without typedef

Manuel M T Chakravarty chak at cse.unsw.edu.au
Thu Nov 24 01:07:33 EST 2005


Sorry for the late answer.

Jelmer Vernooij:
> On Tue, Nov 15, 2005 at 01:20:13AM +1100, Manuel M T Chakravarty wrote
> about 'Re: [C2hs] Pointers to structs without typedef':
> > Ok, I see what you mean.  This does look like a bug.  Is this a show
> > stopper for you?
> Pretty much, as it means I can't just use the original headers. Is there 
> anything I can do to help fix it? I'd be happy to spend some hours on 
> fixing it, if you can point me in the right direction.

When running c2hs on basically your example (see attachement) using the
dump facilities as follows:

  c2hs -d genbind test.chs

I get the appended output.  Interesting are the two sections for the
"call hook"s for bar1 and bar2 and especially the messages of function
`extractCompType' for the "(not for a result)" case of each.  You can
find the function extractCompType in

  http://www.cse.unsw.edu.au/~chak/repos/c2hs/c2hs/gen/GenBind.hs

It computes a Haskell type from a C type specification.

For both bar1 and bar2, we get the message

  extractCompType: explicit pointer type

which means that the test `isPtrDeclr declr' in the first case of the
function succeeded and we enter the local function `ptrType'.  In the
case of bar1, we now also get a

  extractCompType: checking for alias

whereas that doesn't happen for bar2.  This suggests that the local
variable `oHsRepr' has the value `Nothing' for bar1, which is surely the
reason for the unwanted `Ptr ()' type.  This value of `Nothing' maybe
due to the previous call to `checkForOneAliasName' also returning
`Nothing' or because of the result returned by the call to `queryPtr'.

Does that give you a reasonable starting point?

Cheers,
Manuel

-=- Output of run with -d genbind -=-

** Pointer hook:
found C tag definition for `foo'
associated with Haskell entity `Foo'
having type ()
** Pointer hook:
found C declaration for `foo_def'
extractCompType: checking for alias
associated with Haskell entity `FooDef'
having type ()
** Call hook for `bar1':
Entering `extractSimpleType' (for a result)...
extractCompType: checking for alias
Entering `extractSimpleType' (not for a result)...
extractCompType: explicit pointer type
extractCompType: checking for alias
Imported function type: ((Ptr ()) -> (IO ()))
** Call hook for `bar2':
Entering `extractSimpleType' (for a result)...
extractCompType: checking for alias
Entering `extractSimpleType' (not for a result)...
extractCompType: explicit pointer type
Imported function type: ((FooDef) -> (IO ()))

-------------- next part --------------
module Foo
where

#c

struct foo { char x; };
typedef struct foo foo_def;

void bar1(struct foo *x);
void bar2(foo_def *x);

#endc

{#pointer *foo as Foo newtype#}
{#pointer *foo_def as FooDef newtype#}

test1 = {#call bar1#}
test2 = {#call bar2#}

-- test1 generates:
--   foreign import ccall safe "test.h bar1"
--     bar1 :: ((Ptr ()) -> (IO ()))
-- where the `Ptr ()' argument should better be `Foo'


More information about the C2hs mailing list