What you are trying to do isn't quite possible, I don't think, when you define a macro in a hsc
file it will get inlined into your generated .hs
file, and if that macro calls .hsc
macros.. well those won't be available anymore. The best you can do:
module Test where
import Foreign.Ptr
import Foreign.C.Types
data T
data Tn
data Ct
#opaque_t vector_T
#ccall create_std_vectorTn , IO (Ptr <vector_T>)
#ccall carray_to_std_vectorTn , Ptr Ct -> CSize -> IO (Ptr <vector_T>)
#ccall std_vectorTn_to_carray , Ptr <vector_T> -> IO (Ptr Ct)
#ccall std_vectorTn_length , Ptr <vector_T> -> IO CSize
If your C compiler knows where to find bindings.dsl.h
then you should include it at the top; mine doesn't so I compile with hsc2hs --include=<path-to-bindings.dsl.h> test.hsc
. This produces the following file:
{-# LINE 1 "test.hsc" #-}
module Test where
{-# LINE 2 "test.hsc" #-}
import Foreign.Ptr
import Foreign.C.Types
data T
data Tn
data Ct
data C'vector_T = C'vector_T
{-# LINE 11 "test.hsc" #-}
foreign import ccall "create_std_vectorTn" c'create_std_vectorTn
:: IO (Ptr C'vector_T)
foreign import ccall "&create_std_vectorTn" p'create_std_vectorTn
:: FunPtr (IO (Ptr C'vector_T))
{-# LINE 12 "test.hsc" #-}
foreign import ccall "carray_to_std_vectorTn" c'carray_to_std_vectorTn
:: Ptr Ct -> CSize -> IO (Ptr C'vector_T)
foreign import ccall "&carray_to_std_vectorTn" p'carray_to_std_vectorTn
:: FunPtr (Ptr Ct -> CSize -> IO (Ptr C'vector_T))
{-# LINE 13 "test.hsc" #-}
foreign import ccall "std_vectorTn_to_carray" c'std_vectorTn_to_carray
:: Ptr C'vector_T -> IO (Ptr Ct)
foreign import ccall "&std_vectorTn_to_carray" p'std_vectorTn_to_carray
:: FunPtr (Ptr C'vector_T -> IO (Ptr Ct))
{-# LINE 14 "test.hsc" #-}
foreign import ccall "std_vectorTn_length" c'std_vectorTn_length
:: Ptr C'vector_T -> IO CSize
foreign import ccall "&std_vectorTn_length" p'std_vectorTn_length
:: FunPtr (Ptr C'vector_T -> IO CSize)
{-# LINE 15 "test.hsc" #-}
So looks like defining your own macro is the way to go:
mymacro.h
#define hsc_declare_vector_funcs(t,tn,ct)\
hsc_opaque_t(vector_##t)\
hsc_ccall(create_std_vector##tn ,IO (Ptr <vector_##t>))\
hsc_ccall(carray_to_std_vector##tn , Ptr ct -> CSize -> IO (Ptr <vector_##t>)) \
hsc_ccall(std_vector##tn##_to_carray , Ptr <vector_##t> -> IO (Ptr ct)) \
hsc_ccall(std_vector##tn##_length , Ptr <vector_##t> -> IO CSize)
test.hsc
module Test where
#include "mymacro.h"
import Foreign.Ptr
import Foreign.C.Types
#declare_vector_funcs int, i, CInt
test.hs
module Test where
import Foreign.Ptr
import Foreign.C.Types
data C'vector_int = C'vector_int
foreign import ccall "create_std_vectori" c'create_std_vectori
:: IO (Ptr C'vector_int)
foreign import ccall "&create_std_vectori" p'create_std_vectori
:: FunPtr (IO (Ptr C'vector_int))
foreign import ccall "carray_to_std_vectori" c'carray_to_std_vectori
:: Ptr CInt -> CSize -> IO (Ptr C'vector_int)
foreign import ccall "&carray_to_std_vectori" p'carray_to_std_vectori
:: FunPtr (Ptr CInt -> CSize -> IO (Ptr C'vector_int))
foreign import ccall "std_vectori_to_carray" c'std_vectori_to_carray
:: Ptr C'vector_int -> IO (Ptr CInt)
foreign import ccall "&std_vectori_to_carray" p'std_vectori_to_carray
:: FunPtr (Ptr C'vector_int -> IO (Ptr CInt))
foreign import ccall "std_vectori_length" c'std_vectori_length
:: Ptr C'vector_int -> IO CSize
foreign import ccall "&std_vectori_length" p'std_vectori_length
:: FunPtr (Ptr C'vector_int -> IO CSize)