fpco / inline-c

284 stars 50 forks source link

Can inline-C return a typedef to a function pointer? #82

Closed georgewsinger closed 5 years ago

georgewsinger commented 5 years ago

I'm working with a C code base for which

typedef void(* wl_notify_func_t) (struct wl_listener *listener, void *data) 

//...

struct wl_listener {
  struct wl_list link;
  wl_notify_func_t notify; //<-- I'd like to return this
};

and have used the Haskell code

type NotifyFuncT = FunPtr (Ptr C'WlListener -> Ptr () -> IO ())

initializeMyCtx = C.context $ C.baseCtx <> C.funCtx <> mempty {
  C.ctxTypesTable = Data.Map.fromList [
     (C.Struct "wl_listener", [t|C'WlListener|])
  -- ...
  ,  (C.TypeName "wl_notify_func_t", [t|NotifyFuncT|])
  ]
}

someHaskellFunction :: Ptr C'WlListener -> IO NotifyFuncT
someHaskellFunction ptrToWlListener = do
  funPtr <- [C.block| wl_notify_func_t {return $(struct wl_listener * ptrToWlListener)->notify;}|]
  return funPtr

Unfortunately, I get an error from the inline-C code block which essentially says:

unexpected identifier  wl_notify_func_t

So is what I'm doing even possible with inline-C?

georgewsinger commented 5 years ago

This issue should be closed. The code above works; I just forgot to include the C context. :bowtie: