jwiegley / c2hsc

Utility for creating .hsc files from C API header files
BSD 3-Clause "New" or "Revised" License
26 stars 15 forks source link

Non-typedefed structs, unions, and enums are not correctly translated #12

Closed cap11235 closed 10 years ago

cap11235 commented 10 years ago

When applied to t.hs, we get the T.hsc below, which yield the listed errors. The output should have added union, struct, and enum before the typenames in the hsc file.

t.h:

struct st {
  int i;
};

enum e {
  CONST
};

union u {
  char c;
};

T.hsc:

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
#include <bindings.dsl.h>
#include "t.h"
module T.T where
import Foreign.Ptr
#strict_import

{- struct st {
    int i;
}; -}
#starttype st
#field i , CInt
#stoptype
{- enum e {
    CONST
}; -}
#integral_t e
#num CONST
{- union u {
    char c;
}; -}
#starttype u
#field c , CChar
#stoptype

Errors:

In file included from T.hsc:2:0:
T.hsc: In function 'main':
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:201:13: error: unknown type name 'st'
      struct {char _; name v;} bc_refdata; \
             ^
T.hsc:11:5: note: in expansion of macro 'hsc_starttype'
 #starttype st
     ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:212:31: error: request for member 'i' in something not a structure or union
          ((char*)&bc_refdata.v.name - (char*)&bc_refdata.v); \
                               ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:220:6: note: in expansion of macro 'bc_basicfield'
      bc_basicfield(name,# type,0,0); \
      ^
T.hsc:12:5: note: in expansion of macro 'hsc_field'
 #field i , CInt
     ^
T.hsc:17:21: error: 'e' undeclared (first use in this function)
 #integral_t e
                     ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:147:18: note: in definition of macro 'hsc_integral_t'
      int sign = (name)(-1)<0; \
                  ^
T.hsc:17:21: note: each undeclared identifier is reported only once for each function it appears in
 #integral_t e
                     ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:147:18: note: in definition of macro 'hsc_integral_t'
      int sign = (name)(-1)<0; \
                  ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:201:13: error: unknown type name 'u'
      struct {char _; name v;} bc_refdata; \
             ^
T.hsc:22:5: note: in expansion of macro 'hsc_starttype'
 #starttype u
     ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:212:31: error: request for member 'c' in something not a structure or union
          ((char*)&bc_refdata.v.name - (char*)&bc_refdata.v); \
                               ^
/home/cap11235/.cabal/lib/x86_64-linux-ghc-7.6.3/bindings-DSL-1.0.20/include/bindings.dsl.h:220:6: note: in expansion of macro 'bc_basicfield'
      bc_basicfield(name,# type,0,0); \
      ^
T.hsc:23:5: note: in expansion of macro 'hsc_field'
 #field c , CChar
     ^
compiling dist/build/T_hsc_make.c failed (exit code 1)
jwiegley commented 10 years ago

Thank you for reporting this simple test case. I've noticed issues with typedef'd types as well.

jwiegley commented 10 years ago

Ok, I've created unit tests for c2hsc, so I'm adding these to it now.