Pointers to structs without typedef

Hi, c2hs appears to have trouble with pointers to structs when not using typedefs. A simple that shows this behaviour: module Foo where #c struct foo { char x; }; void bar(struct foo *x); #endc {#pointer *'struct foo' as Foo newtype#} Am I doing something incorrectly or is this simply not supported? Cheers, Jelmer

Jelmer Vernooij:
Hi,
c2hs appears to have trouble with pointers to structs when not using typedefs. A simple that shows this behaviour:
module Foo where
#c
struct foo { char x; };
void bar(struct foo *x);
#endc
{#pointer *'struct foo' as Foo newtype#}
I think you meant {#pointer *foo as Foo newtype#} See also http://www.cse.unsw.edu.au/~chak/haskell/c2hs/docu/c2hs-3.html#ss3.10 Manuel

On Thu, Nov 10, 2005 at 12:29:02AM +1100, Manuel M T Chakravarty wrote about 'Re: [C2hs] Pointers to structs without typedef':
Jelmer Vernooij:
c2hs appears to have trouble with pointers to structs when not using typedefs. A simple that shows this behaviour:
module Foo where
#c
struct foo { char x; };
void bar(struct foo *x);
#endc
{#pointer *'struct foo' as Foo newtype#}
I think you meant
{#pointer *foo as Foo newtype#}
That's what I tried initially, but that doesn't appear to work; c2hs will generate "Ptr ()" as the first argument of bar. It does work correctly if I make foo a typdef. If I try this: module Foo where #c typedef struct foo { char x; } foo_typedef; void bar1(struct foo *x); void bar2(foo_typedef *x); #endc {#pointer *foo as Foo newtype#} {#pointer *foo_typedef as FooTypedef newtype#} test1 = {#call bar1#} test2 = {#call bar2#} c2hs generates the following stub for `bar1': foreign import ccall safe "c2hstest.h bar1" bar1 :: ((Ptr ()) -> (IO ())) whereas I would expect the `Ptr ()' to be `Foo' instead. the stub for `bar2' is correct: foreign import ccall safe "c2hstest.h bar2" bar2 :: ((FooTypedef) -> (IO ())) Cheers, Jelmer

Ok, I see what you mean. This does look like a bug. Is this a show stopper for you? Manuel Jelmer Vernooij:
On Thu, Nov 10, 2005 at 12:29:02AM +1100, Manuel M T Chakravarty wrote about 'Re: [C2hs] Pointers to structs without typedef':
Jelmer Vernooij:
c2hs appears to have trouble with pointers to structs when not using typedefs. A simple that shows this behaviour:
module Foo where
#c
struct foo { char x; };
void bar(struct foo *x);
#endc
{#pointer *'struct foo' as Foo newtype#}
I think you meant
{#pointer *foo as Foo newtype#}
That's what I tried initially, but that doesn't appear to work; c2hs will generate "Ptr ()" as the first argument of bar. It does work correctly if I make foo a typdef.
If I try this:
module Foo where
#c
typedef struct foo { char x; } foo_typedef;
void bar1(struct foo *x); void bar2(foo_typedef *x);
#endc
{#pointer *foo as Foo newtype#} {#pointer *foo_typedef as FooTypedef newtype#}
test1 = {#call bar1#} test2 = {#call bar2#}
c2hs generates the following stub for `bar1':
foreign import ccall safe "c2hstest.h bar1" bar1 :: ((Ptr ()) -> (IO ()))
whereas I would expect the `Ptr ()' to be `Foo' instead.
the stub for `bar2' is correct:
foreign import ccall safe "c2hstest.h bar2" bar2 :: ((FooTypedef) -> (IO ()))
Cheers,
Jelmer

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.
Cheers, Jelmer
Jelmer Vernooij:
On Thu, Nov 10, 2005 at 12:29:02AM +1100, Manuel M T Chakravarty wrote about 'Re: [C2hs] Pointers to structs without typedef':
Jelmer Vernooij:
c2hs appears to have trouble with pointers to structs when not using typedefs. A simple that shows this behaviour:
module Foo where
#c
struct foo { char x; };
void bar(struct foo *x);
#endc
{#pointer *'struct foo' as Foo newtype#}
I think you meant
{#pointer *foo as Foo newtype#}
That's what I tried initially, but that doesn't appear to work; c2hs will generate "Ptr ()" as the first argument of bar. It does work correctly if I make foo a typdef.
If I try this:
module Foo where
#c
typedef struct foo { char x; } foo_typedef;
void bar1(struct foo *x); void bar2(foo_typedef *x);
#endc
{#pointer *foo as Foo newtype#} {#pointer *foo_typedef as FooTypedef newtype#}
test1 = {#call bar1#} test2 = {#call bar2#}
c2hs generates the following stub for `bar1':
foreign import ccall safe "c2hstest.h bar1" bar1 :: ((Ptr ()) -> (IO ()))
whereas I would expect the `Ptr ()' to be `Foo' instead.
the stub for `bar2' is correct:
foreign import ccall safe "c2hstest.h bar2" bar2 :: ((FooTypedef) -> (IO ()))
Cheers,
Jelmer
--
Jelmer Vernooij

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 ()))

On Thu, Nov 24, 2005 at 05:07:33PM +1100, Manuel M T Chakravarty wrote about 'Re: [C2hs] Pointers to structs without typedef':
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': 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
[comments]
Does that give you a reasonable starting point? Yes, that helps a lot. Thanks.
Cheers, Jelmer --

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Jelmer Vernooij wrote:
On Thu, Nov 24, 2005 at 05:07:33PM +1100, Manuel M T Chakravarty wrote about 'Re: [C2hs] Pointers to structs without typedef':
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': 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 [comments] Does that give you a reasonable starting point?
Yes, that helps a lot. Thanks. I've just sent you three patches to c2hs, one of which fixes this issue. Please comment and/or apply.
These patches are also available from the darcs repository at http://samba.org/~jelmer/c2hs/, for those interested. Cheers, Jelmer -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) iD8DBQFDi36VPa9Uoh7vUnYRAmv4AJ9KjZs5U7lUAi2L5zfPyG/OwfHbIgCgk3QK B88ZlB+3w6ofd2JJKGuoMus= =zKHX -----END PGP SIGNATURE-----

Am Montag, den 28.11.2005, 23:03 +0100 schrieb Jelmer Vernooij:
I've just sent you three patches to c2hs, one of which fixes this issue. Please comment and/or apply.
Thanks! ... but I only received two patches: 1. darcs patch: add prettify functions for structs, enum... (and 1 more) 2. darcs patch: Updates for GHC from CVS Did anything get lost? Moreover, am I right to assume that the second patch will break building with released versions of GHC? Cheers, Manuel

On Tue, Nov 29, 2005 at 01:36:15PM +1100, Manuel M T Chakravarty wrote about 'Re: [C2hs] Pointers to structs without typedef':
Am Montag, den 28.11.2005, 23:03 +0100 schrieb Jelmer Vernooij:
I've just sent you three patches to c2hs, one of which fixes this issue. Please comment and/or apply. Thanks! ... but I only received two patches:
1. darcs patch: add prettify functions for structs, enum... (and 1 more)
This one should contain patch with the fix as well (in the same email). If it doesn't, please let me know.
2. darcs patch: Updates for GHC from CVS
Did anything get lost? Moreover, am I right to assume that the second patch will break building with released versions of GHC? Yes, that is correct. Also the reason why I sent it seperately. It just might help you in the future if the next release of GHC breaks.
Cheers,
Jelmer
--
Jelmer Vernooij
participants (2)
-
Jelmer Vernooij
-
Manuel M T Chakravarty