ocr*_*amz 4 c haskell ffi c2hs
AC枚举类型由c2hs呈现到Haskell中,完成Storable正确编译的实例(TypesC2Hs.chs).我将这个不合格的导入到我为inline-ccontext(Internal.hs)指定的模块中..hs由c2hs生成的模块和Internal.hs导入的模块,InlineC.hs另一个inline-c模块包含C调用的quasiquotes.
TypesC2Hs.hs -------------
| |
V V
Internal.hs -------> InlineC.hs
Run Code Online (Sandbox Code Playgroud)
InlineC.hs 抱怨这种类型无法编组:"外国声明中不可接受的参数类型:'DMBoundaryType'无法在外部调用中编组时检查声明:"
到底是怎么回事?这是第一次inline-c给我这个错误的类型.
我应该注意,其他类型不需要直接解除引用,例如newtype DM = DM (Ptr DM) deriving Storable,使用上述方法可以正常工作.
提前致谢
TypesC2Hs.chs
{# enum DMBoundaryType as DMBoundaryType {underscoreToCase} deriving (Eq, Show) #}
instance Storable DMBoundaryType where
sizeOf _ = {# sizeof DMBoundaryType #}
alignment _ = {# alignof DMBoundaryType #}
peek = peek
poke = poke
Run Code Online (Sandbox Code Playgroud)
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 |]) ]
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)