jwiegley / c2hsc

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

support top-level array typedefs #27

Open ghorn opened 9 years ago

ghorn commented 9 years ago

This code:

#ifndef MY_TYPES_H
#define MY_TYPES_H

typedef int MyArray[20];
typedef struct {
  MyArray x;
} MyStruct;

#endif // MY_TYPES_H

produces this code:

{-# OPTIONS_GHC -fno-warn-unused-imports #-}

module MyModule.MyTypes where
import Foreign.Ptr
import Foreign.Ptr (Ptr,FunPtr,plusPtr)
import Foreign.Ptr (wordPtrToPtr,castPtrToFunPtr)
import Foreign.Storable
import Foreign.C.Types
import Foreign.C.String (CString,CStringLen,CWString,CWStringLen)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (peekArray,pokeArray)
import Data.Int
import Data.Word

foreign import ccall "array_MyArray" c'MyArray
  :: Ptr (CInt)

{- typedef struct {
            MyArray x;
        } MyStruct; -}

data C'MyStruct = C'MyStruct{
  c'MyStruct'x :: C'MyArray
} deriving (Eq,Show)
p'MyStruct'x p = plusPtr p 0
p'MyStruct'x :: Ptr (C'MyStruct) -> Ptr (C'MyArray)
instance Storable C'MyStruct where
  sizeOf _ = 80
  alignment _ = 4
  peek p = do
    v0 <- peekByteOff p 0
    return $ C'MyStruct v0
  poke p (C'MyStruct v0) = do
    pokeByteOff p 0 v0
    return ()

which doesn't compile because:

Not in scope: type constructor or class ‘C'MyArray’