4

环境

AC 枚举类型由 c2hs 渲染到 Haskell 中,并带有Storable正确编译的实例 ( TypesC2Hs.chs)。我将这个不合格的导入到我为inline-c上下文 ( Internal.hs) 分配的模块中。.hs由 c2hs 生成的模块和Internal.hs由 导入的模块,InlineC.hs另一个inline-c模块包含包装 C 调用的准引号。

TypesC2Hs.hs ------------- 
    |                    |
    V                    V
Internal.hs -------> InlineC.hs

问题

InlineC.hs抱怨这种类型无法编组:“外部声明中不可接受的参数类型:'DMBoundaryType' 不能在外部调用中编组检查声明时:”

到底是怎么回事?这是第一次inline-c给我这种错误的类型。

我应该注意,不需要直接取消引用的其他类型,例如newtype DM = DM (Ptr DM) deriving Storable,可以使用上述方法正常工作。

提前致谢


类型C2Hs.chs

{# enum DMBoundaryType as DMBoundaryType {underscoreToCase} deriving (Eq, Show) #}

instance Storable DMBoundaryType where
  sizeOf _ = {# sizeof DMBoundaryType #}
  alignment _ = {# alignof DMBoundaryType #}
  peek = peek
  poke = poke

内部.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 |])  ]

内联C.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
4

1 回答 1

6

Cenum不是可编组的外部类型,这是编译器试图告诉你的。要解决它,请将其作为CIntusing传递fromEnum(看起来c2hs现在通过hooks支持它,但我从未尝试过。)

于 2016-05-26T18:14:35.550 回答