{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Language.C.Inline
(
Context
, baseCtx
, fptrCtx
, funCtx
, vecCtx
, bsCtx
, context
, substitute
, getHaskellType
, exp
, pure
, block
, include
, verbatim
, withPtr
, withPtr_
, WithPtrs(..)
, funPtr
, mkFunPtr
, mkFunPtrFromName
, peekFunPtr
, module Foreign.C.Types
) where
#if __GLASGOW_HASKELL__ < 710
import Prelude hiding (exp)
#else
import Prelude hiding (exp, pure)
#endif
import Control.Monad (void)
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (Ptr)
import Foreign.Storable (peek, Storable)
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Quote as TH
import Language.C.Inline.Context
import Language.C.Inline.Internal
import Language.C.Inline.FunPtr
exp :: TH.QuasiQuoter
exp :: QuasiQuoter
exp = Purity
-> (Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter
genericQuote Purity
IO ((Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter)
-> (Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter
forall a b. (a -> b) -> a -> b
$ Safety
-> Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ
inlineExp Safety
TH.Safe
pure :: TH.QuasiQuoter
pure :: QuasiQuoter
pure = Purity
-> (Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter
genericQuote Purity
Pure ((Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter)
-> (Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter
forall a b. (a -> b) -> a -> b
$ Safety
-> Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ
inlineExp Safety
TH.Safe
block :: TH.QuasiQuoter
block :: QuasiQuoter
block = Purity
-> (Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter
genericQuote Purity
IO ((Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter)
-> (Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ)
-> QuasiQuoter
forall a b. (a -> b) -> a -> b
$ Safety
-> Bool
-> Maybe String
-> Loc
-> TypeQ
-> Type CIdentifier
-> [(CIdentifier, Type CIdentifier)]
-> String
-> ExpQ
inlineItems Safety
TH.Safe Bool
False Maybe String
forall a. Maybe a
Nothing
funPtr :: TH.QuasiQuoter
funPtr :: QuasiQuoter
funPtr = Safety -> QuasiQuoter
funPtrQuote Safety
TH.Unsafe
include :: String -> TH.DecsQ
include :: String -> DecsQ
include String
s
| String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s = String -> DecsQ
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"inline-c: empty string (include)"
| String -> Char
forall a. [a] -> a
head String
s Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'<' = String -> DecsQ
verbatim (String -> DecsQ) -> String -> DecsQ
forall a b. (a -> b) -> a -> b
$ String
"#include " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
| Bool
otherwise = String -> DecsQ
verbatim (String -> DecsQ) -> String -> DecsQ
forall a b. (a -> b) -> a -> b
$ String
"#include \"" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\""
verbatim :: String -> TH.DecsQ
verbatim :: String -> DecsQ
verbatim String
s = do
DecsQ -> Q ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (DecsQ -> Q ()) -> DecsQ -> Q ()
forall a b. (a -> b) -> a -> b
$ String -> DecsQ
emitVerbatim String
s
[Dec] -> DecsQ
forall (m :: * -> *) a. Monad m => a -> m a
return []
withPtr :: (Storable a) => (Ptr a -> IO b) -> IO (a, b)
withPtr :: (Ptr a -> IO b) -> IO (a, b)
withPtr Ptr a -> IO b
f = do
(Ptr a -> IO (a, b)) -> IO (a, b)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr a -> IO (a, b)) -> IO (a, b))
-> (Ptr a -> IO (a, b)) -> IO (a, b)
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
b
x <- Ptr a -> IO b
f Ptr a
ptr
a
y <- Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
ptr
(a, b) -> IO (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
y, b
x)
withPtr_ :: (Storable a) => (Ptr a -> IO ()) -> IO a
withPtr_ :: (Ptr a -> IO ()) -> IO a
withPtr_ Ptr a -> IO ()
f = do
(a
x, ()) <- (Ptr a -> IO ()) -> IO (a, ())
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr Ptr a -> IO ()
f
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
class WithPtrs a where
type WithPtrsPtrs a :: *
withPtrs :: (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs_ :: (WithPtrsPtrs a -> IO ()) -> IO a
withPtrs_ WithPtrsPtrs a -> IO ()
f = do
(a
x, ()
_) <- (WithPtrsPtrs a -> IO ()) -> IO (a, ())
forall a b. WithPtrs a => (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs WithPtrsPtrs a -> IO ()
f
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
instance (Storable a, Storable b) => WithPtrs (a, b) where
type WithPtrsPtrs (a, b) = (Ptr a, Ptr b)
withPtrs :: (WithPtrsPtrs (a, b) -> IO b) -> IO ((a, b), b)
withPtrs WithPtrsPtrs (a, b) -> IO b
f = do
(a
a, (b
b, b
x)) <- (Ptr a -> IO (b, b)) -> IO (a, (b, b))
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr a -> IO (b, b)) -> IO (a, (b, b)))
-> (Ptr a -> IO (b, b)) -> IO (a, (b, b))
forall a b. (a -> b) -> a -> b
$ \Ptr a
a -> (Ptr b -> IO b) -> IO (b, b)
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr b -> IO b) -> IO (b, b)) -> (Ptr b -> IO b) -> IO (b, b)
forall a b. (a -> b) -> a -> b
$ \Ptr b
b -> WithPtrsPtrs (a, b) -> IO b
f (Ptr a
a, Ptr b
b)
((a, b), b) -> IO ((a, b), b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, b
b), b
x)
instance (Storable a, Storable b, Storable c) => WithPtrs (a, b, c) where
type WithPtrsPtrs (a, b, c) = (Ptr a, Ptr b, Ptr c)
withPtrs :: (WithPtrsPtrs (a, b, c) -> IO b) -> IO ((a, b, c), b)
withPtrs WithPtrsPtrs (a, b, c) -> IO b
f = do
(a
a, ((b
b, c
c), b
x)) <- (Ptr a -> IO ((b, c), b)) -> IO (a, ((b, c), b))
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr a -> IO ((b, c), b)) -> IO (a, ((b, c), b)))
-> (Ptr a -> IO ((b, c), b)) -> IO (a, ((b, c), b))
forall a b. (a -> b) -> a -> b
$ \Ptr a
a -> (WithPtrsPtrs (b, c) -> IO b) -> IO ((b, c), b)
forall a b. WithPtrs a => (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs ((WithPtrsPtrs (b, c) -> IO b) -> IO ((b, c), b))
-> (WithPtrsPtrs (b, c) -> IO b) -> IO ((b, c), b)
forall a b. (a -> b) -> a -> b
$ \(b, c) -> WithPtrsPtrs (a, b, c) -> IO b
f (Ptr a
a, Ptr b
b, Ptr c
c)
((a, b, c), b) -> IO ((a, b, c), b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, b
b, c
c), b
x)
instance (Storable a, Storable b, Storable c, Storable d) => WithPtrs (a, b, c, d) where
type WithPtrsPtrs (a, b, c, d) = (Ptr a, Ptr b, Ptr c, Ptr d)
withPtrs :: (WithPtrsPtrs (a, b, c, d) -> IO b) -> IO ((a, b, c, d), b)
withPtrs WithPtrsPtrs (a, b, c, d) -> IO b
f = do
(a
a, ((b
b, c
c, d
d), b
x)) <- (Ptr a -> IO ((b, c, d), b)) -> IO (a, ((b, c, d), b))
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr a -> IO ((b, c, d), b)) -> IO (a, ((b, c, d), b)))
-> (Ptr a -> IO ((b, c, d), b)) -> IO (a, ((b, c, d), b))
forall a b. (a -> b) -> a -> b
$ \Ptr a
a -> (WithPtrsPtrs (b, c, d) -> IO b) -> IO ((b, c, d), b)
forall a b. WithPtrs a => (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs ((WithPtrsPtrs (b, c, d) -> IO b) -> IO ((b, c, d), b))
-> (WithPtrsPtrs (b, c, d) -> IO b) -> IO ((b, c, d), b)
forall a b. (a -> b) -> a -> b
$ \(b, c, d) -> WithPtrsPtrs (a, b, c, d) -> IO b
f (Ptr a
a, Ptr b
b, Ptr c
c, Ptr d
d)
((a, b, c, d), b) -> IO ((a, b, c, d), b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, b
b, c
c, d
d), b
x)
instance (Storable a, Storable b, Storable c, Storable d, Storable e) => WithPtrs (a, b, c, d, e) where
type WithPtrsPtrs (a, b, c, d, e) = (Ptr a, Ptr b, Ptr c, Ptr d, Ptr e)
withPtrs :: (WithPtrsPtrs (a, b, c, d, e) -> IO b) -> IO ((a, b, c, d, e), b)
withPtrs WithPtrsPtrs (a, b, c, d, e) -> IO b
f = do
(a
a, ((b
b, c
c, d
d, e
e), b
x)) <- (Ptr a -> IO ((b, c, d, e), b)) -> IO (a, ((b, c, d, e), b))
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr a -> IO ((b, c, d, e), b)) -> IO (a, ((b, c, d, e), b)))
-> (Ptr a -> IO ((b, c, d, e), b)) -> IO (a, ((b, c, d, e), b))
forall a b. (a -> b) -> a -> b
$ \Ptr a
a -> (WithPtrsPtrs (b, c, d, e) -> IO b) -> IO ((b, c, d, e), b)
forall a b. WithPtrs a => (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs ((WithPtrsPtrs (b, c, d, e) -> IO b) -> IO ((b, c, d, e), b))
-> (WithPtrsPtrs (b, c, d, e) -> IO b) -> IO ((b, c, d, e), b)
forall a b. (a -> b) -> a -> b
$ \(b, c, d, e) -> WithPtrsPtrs (a, b, c, d, e) -> IO b
f (Ptr a
a, Ptr b
b, Ptr c
c, Ptr d
d, Ptr e
e)
((a, b, c, d, e), b) -> IO ((a, b, c, d, e), b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, b
b, c
c, d
d, e
e), b
x)
instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f) => WithPtrs (a, b, c, d, e, f) where
type WithPtrsPtrs (a, b, c, d, e, f) = (Ptr a, Ptr b, Ptr c, Ptr d, Ptr e, Ptr f)
withPtrs :: (WithPtrsPtrs (a, b, c, d, e, f) -> IO b)
-> IO ((a, b, c, d, e, f), b)
withPtrs WithPtrsPtrs (a, b, c, d, e, f) -> IO b
fun = do
(a
a, ((b
b, c
c, d
d, e
e, f
f), b
x)) <- (Ptr a -> IO ((b, c, d, e, f), b)) -> IO (a, ((b, c, d, e, f), b))
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr a -> IO ((b, c, d, e, f), b))
-> IO (a, ((b, c, d, e, f), b)))
-> (Ptr a -> IO ((b, c, d, e, f), b))
-> IO (a, ((b, c, d, e, f), b))
forall a b. (a -> b) -> a -> b
$ \Ptr a
a -> (WithPtrsPtrs (b, c, d, e, f) -> IO b) -> IO ((b, c, d, e, f), b)
forall a b. WithPtrs a => (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs ((WithPtrsPtrs (b, c, d, e, f) -> IO b) -> IO ((b, c, d, e, f), b))
-> (WithPtrsPtrs (b, c, d, e, f) -> IO b)
-> IO ((b, c, d, e, f), b)
forall a b. (a -> b) -> a -> b
$ \(b, c, d, e, f) -> WithPtrsPtrs (a, b, c, d, e, f) -> IO b
fun (Ptr a
a, Ptr b
b, Ptr c
c, Ptr d
d, Ptr e
e, Ptr f
f)
((a, b, c, d, e, f), b) -> IO ((a, b, c, d, e, f), b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, b
b, c
c, d
d, e
e, f
f), b
x)
instance (Storable a, Storable b, Storable c, Storable d, Storable e, Storable f, Storable g) => WithPtrs (a, b, c, d, e, f, g) where
type WithPtrsPtrs (a, b, c, d, e, f, g) = (Ptr a, Ptr b, Ptr c, Ptr d, Ptr e, Ptr f, Ptr g)
withPtrs :: (WithPtrsPtrs (a, b, c, d, e, f, g) -> IO b)
-> IO ((a, b, c, d, e, f, g), b)
withPtrs WithPtrsPtrs (a, b, c, d, e, f, g) -> IO b
fun = do
(a
a, ((b
b, c
c, d
d, e
e, f
f, g
g), b
x)) <- (Ptr a -> IO ((b, c, d, e, f, g), b))
-> IO (a, ((b, c, d, e, f, g), b))
forall a b. Storable a => (Ptr a -> IO b) -> IO (a, b)
withPtr ((Ptr a -> IO ((b, c, d, e, f, g), b))
-> IO (a, ((b, c, d, e, f, g), b)))
-> (Ptr a -> IO ((b, c, d, e, f, g), b))
-> IO (a, ((b, c, d, e, f, g), b))
forall a b. (a -> b) -> a -> b
$ \Ptr a
a -> (WithPtrsPtrs (b, c, d, e, f, g) -> IO b)
-> IO ((b, c, d, e, f, g), b)
forall a b. WithPtrs a => (WithPtrsPtrs a -> IO b) -> IO (a, b)
withPtrs ((WithPtrsPtrs (b, c, d, e, f, g) -> IO b)
-> IO ((b, c, d, e, f, g), b))
-> (WithPtrsPtrs (b, c, d, e, f, g) -> IO b)
-> IO ((b, c, d, e, f, g), b)
forall a b. (a -> b) -> a -> b
$ \(b, c, d, e, f, g) -> WithPtrsPtrs (a, b, c, d, e, f, g) -> IO b
fun (Ptr a
a, Ptr b
b, Ptr c
c, Ptr d
d, Ptr e
e, Ptr f
f, Ptr g
g)
((a, b, c, d, e, f, g), b) -> IO ((a, b, c, d, e, f, g), b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, b
b, c
c, d
d, e
e, f
f, g
g), b
x)
context :: Context -> TH.DecsQ
context :: Context -> DecsQ
context Context
ctx = do
Context -> Q ()
setContext Context
ctx
[Dec] -> DecsQ
forall (m :: * -> *) a. Monad m => a -> m a
return []