A C enum type rendered into Haskell by c2hs, complete with Storable
instance which compiles correctly (TypesC2Hs.chs
). I import this unqualified into the module I have assigned for the inline-c
context (Internal.hs
). Both the .hs
module generated by c2hs and Internal.hs
are imported by InlineC.hs
, the other inline-c
module that holds the quasiquotes wrapping the C calls.
TypesC2Hs.hs -------------
| |
V V
Internal.hs -------> InlineC.hs
InlineC.hs
complains that this type cannot be marshalled: "Unacceptable argument type in foreign declaration: ‘DMBoundaryType’ cannot be marshalled in a foreign call When checking declaration:"
What is going on? This is the first time inline-c
gives me type of this error.
I should note that other types that do not need to be dereferenced directly, e.g. newtype DM = DM (Ptr DM) deriving Storable
, work fine with the above approach.
Thanks in advance
TypesC2Hs.chs
{# enum DMBoundaryType as DMBoundaryType {underscoreToCase} deriving (Eq, Show) #}
instance Storable DMBoundaryType where
sizeOf _ = {# sizeof DMBoundaryType #}
alignment _ = {# alignof DMBoundaryType #}
peek = peek
poke = poke
Internal.hs
{-# LANGUAGE QuasiQuotes, TemplateHaskell ,GeneralizedNewtypeDeriving, StandaloneDeriving ,DeriveDataTypeable, DataKinds, OverloadedStrings #-}
module Internal where
import TypesC2Hs
import qualified Language.C.Inline as C
import qualified Language.C.Types as CT
import Language.C.Inline.Context
import qualified Language.Haskell.TH as TH
import Data.Monoid ((<>), mempty)
import qualified Data.Map as Map
ctx :: Context
ctx = baseCtx <> funCtx <> vecCtx <> bsCtx <> pctx where
pctx = mempty {ctxTypesTable = typesTable}
typesTable :: Map.Map CT.TypeSpecifier TH.TypeQ
typesTable = Map.fromList
[ (CT.TypeName "DMBoundaryType", [t| DMBoundaryType |]) ]
InlineC.hs
dmdaCreate1d0' cc bx m dof s =
withPtr ( \ dm -> [C.exp|int{DMDACreate1d($(int c),
$(DMBoundaryType bx),
$(PetscInt m),
$(PetscInt dof),
$(PetscInt s),
NULL,
$(DM* dm))}|] )
where c = unComm cc
C enum
is not marshallable foreign type, that is what compiler tries to tell you. To work around it, pass it as a CInt
using fromEnum
(looks like c2hs
now supports it via hooks, but I never tried it.)