haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
198 stars 50 forks source link

Support circular imports #170

Open kseo opened 8 years ago

kseo commented 8 years ago

I would like to create a separate c2hs file for each C header file for the ease of maintenance. But what if there are looping struct declarations?

struct _bar;

typedef struct _foo {
  struct _bar* b;
} foo;
struct _foo;

typedef struct _bar {
  struct _foo* f;
} bar;

I made Foo and Bar mutually recursive as in the following:

{-# LANGUAGE ForeignFunctionInterface #-}

#include "foo.h"

module Foo where

-- Imports here
{#import Bar #}

data Foo = Foo
  { bar :: BarPtr
  }
{#pointer *foo as FooPtr -> Foo #}
{#pointer *_foo as FooPtr -> Foo nocode #}
{-# LANGUAGE ForeignFunctionInterface #-}

#include "bar.h"

module Bar where

-- Imports here
{#import Foo #}

data Bar = Bar
  { foo :: FooPtr
  }
{#pointer *bar as BarPtr -> Bar #}
{#pointer *_bar as BarPtr -> Bar nocode #}

However, it seems there is no way to process two mutually recursive c2hs files. c2hs complains when I pass these two c2hs files as inputs:

$ c2hs Foo.chs Bar.chs
There must be exactly one binding file (suffix .chs),
and optionally one or more header files (suffix .h).
Try the option `--help' on its own for more information.