{-
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

\section[Foreign]{Foreign calls}
-}

{-# LANGUAGE DeriveDataTypeable #-}

module GHC.Types.ForeignCall (
        ForeignCall(..), isSafeForeignCall,
        Safety(..), playSafe, playInterruptible,

        CExportSpec(..), CLabelString, isCLabelString, pprCLabelString,
        CCallSpec(..),
        CCallTarget(..), isDynamicTarget,
        CCallConv(..), defaultCCallConv, ccallConvToInt, ccallConvAttribute,

        Header(..), CType(..),
    ) where

import GHC.Prelude

import GHC.Data.FastString
import GHC.Utils.Binary
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Unit.Module
import GHC.Types.SourceText ( SourceText, pprWithSourceText )

import Data.Char
import Data.Data

{-
************************************************************************
*                                                                      *
\subsubsection{Data types}
*                                                                      *
************************************************************************
-}

newtype ForeignCall = CCall CCallSpec
  deriving ForeignCall -> ForeignCall -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ForeignCall -> ForeignCall -> Bool
$c/= :: ForeignCall -> ForeignCall -> Bool
== :: ForeignCall -> ForeignCall -> Bool
$c== :: ForeignCall -> ForeignCall -> Bool
Eq

isSafeForeignCall :: ForeignCall -> Bool
isSafeForeignCall :: ForeignCall -> Bool
isSafeForeignCall (CCall (CCallSpec CCallTarget
_ CCallConv
_ Safety
safe)) = Safety -> Bool
playSafe Safety
safe

-- We may need more clues to distinguish foreign calls
-- but this simple printer will do for now
instance Outputable ForeignCall where
  ppr :: ForeignCall -> SDoc
ppr (CCall CCallSpec
cc)  = forall a. Outputable a => a -> SDoc
ppr CCallSpec
cc

data Safety
  = PlaySafe          -- ^ Might invoke Haskell GC, or do a call back, or
                      --   switch threads, etc.  So make sure things are
                      --   tidy before the call. Additionally, in the threaded
                      --   RTS we arrange for the external call to be executed
                      --   by a separate OS thread, i.e., _concurrently_ to the
                      --   execution of other Haskell threads.

  | PlayInterruptible -- ^ Like PlaySafe, but additionally
                      --   the worker thread running this foreign call may
                      --   be unceremoniously killed, so it must be scheduled
                      --   on an unbound thread.

  | PlayRisky         -- ^ None of the above can happen; the call will return
                      --   without interacting with the runtime system at all.
                      --   Specifically:
                      --
                      --     * No GC
                      --     * No call backs
                      --     * No blocking
                      --     * No precise exceptions
                      --
  deriving ( Safety -> Safety -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Safety -> Safety -> Bool
$c/= :: Safety -> Safety -> Bool
== :: Safety -> Safety -> Bool
$c== :: Safety -> Safety -> Bool
Eq, Int -> Safety -> ShowS
[Safety] -> ShowS
Safety -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Safety] -> ShowS
$cshowList :: [Safety] -> ShowS
show :: Safety -> String
$cshow :: Safety -> String
showsPrec :: Int -> Safety -> ShowS
$cshowsPrec :: Int -> Safety -> ShowS
Show, Typeable Safety
Safety -> DataType
Safety -> Constr
(forall b. Data b => b -> b) -> Safety -> Safety
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Safety -> u
forall u. (forall d. Data d => d -> u) -> Safety -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Safety -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Safety -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Safety
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Safety -> c Safety
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Safety)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Safety)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Safety -> m Safety
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Safety -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Safety -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Safety -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Safety -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Safety -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Safety -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Safety -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Safety -> r
gmapT :: (forall b. Data b => b -> b) -> Safety -> Safety
$cgmapT :: (forall b. Data b => b -> b) -> Safety -> Safety
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Safety)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Safety)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Safety)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Safety)
dataTypeOf :: Safety -> DataType
$cdataTypeOf :: Safety -> DataType
toConstr :: Safety -> Constr
$ctoConstr :: Safety -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Safety
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Safety
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Safety -> c Safety
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Safety -> c Safety
Data, Int -> Safety
Safety -> Int
Safety -> [Safety]
Safety -> Safety
Safety -> Safety -> [Safety]
Safety -> Safety -> Safety -> [Safety]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Safety -> Safety -> Safety -> [Safety]
$cenumFromThenTo :: Safety -> Safety -> Safety -> [Safety]
enumFromTo :: Safety -> Safety -> [Safety]
$cenumFromTo :: Safety -> Safety -> [Safety]
enumFromThen :: Safety -> Safety -> [Safety]
$cenumFromThen :: Safety -> Safety -> [Safety]
enumFrom :: Safety -> [Safety]
$cenumFrom :: Safety -> [Safety]
fromEnum :: Safety -> Int
$cfromEnum :: Safety -> Int
toEnum :: Int -> Safety
$ctoEnum :: Int -> Safety
pred :: Safety -> Safety
$cpred :: Safety -> Safety
succ :: Safety -> Safety
$csucc :: Safety -> Safety
Enum )
        -- Show used just for Show Lex.Token, I think

instance Outputable Safety where
  ppr :: Safety -> SDoc
ppr Safety
PlaySafe = forall doc. IsLine doc => String -> doc
text String
"safe"
  ppr Safety
PlayInterruptible = forall doc. IsLine doc => String -> doc
text String
"interruptible"
  ppr Safety
PlayRisky = forall doc. IsLine doc => String -> doc
text String
"unsafe"

playSafe :: Safety -> Bool
playSafe :: Safety -> Bool
playSafe Safety
PlaySafe = Bool
True
playSafe Safety
PlayInterruptible = Bool
True
playSafe Safety
PlayRisky = Bool
False

playInterruptible :: Safety -> Bool
playInterruptible :: Safety -> Bool
playInterruptible Safety
PlayInterruptible = Bool
True
playInterruptible Safety
_ = Bool
False

{-
************************************************************************
*                                                                      *
\subsubsection{Calling C}
*                                                                      *
************************************************************************
-}

data CExportSpec
  = CExportStatic               -- foreign export ccall foo :: ty
        SourceText              -- of the CLabelString.
                                -- See Note [Pragma source text] in GHC.Types.SourceText
        CLabelString            -- C Name of exported function
        CCallConv
  deriving Typeable CExportSpec
CExportSpec -> DataType
CExportSpec -> Constr
(forall b. Data b => b -> b) -> CExportSpec -> CExportSpec
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> CExportSpec -> u
forall u. (forall d. Data d => d -> u) -> CExportSpec -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CExportSpec -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CExportSpec -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CExportSpec
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CExportSpec -> c CExportSpec
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CExportSpec)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CExportSpec)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CExportSpec -> m CExportSpec
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CExportSpec -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CExportSpec -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CExportSpec -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CExportSpec -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CExportSpec -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CExportSpec -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CExportSpec -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CExportSpec -> r
gmapT :: (forall b. Data b => b -> b) -> CExportSpec -> CExportSpec
$cgmapT :: (forall b. Data b => b -> b) -> CExportSpec -> CExportSpec
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CExportSpec)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CExportSpec)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CExportSpec)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CExportSpec)
dataTypeOf :: CExportSpec -> DataType
$cdataTypeOf :: CExportSpec -> DataType
toConstr :: CExportSpec -> Constr
$ctoConstr :: CExportSpec -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CExportSpec
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CExportSpec
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CExportSpec -> c CExportSpec
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CExportSpec -> c CExportSpec
Data

data CCallSpec
  =  CCallSpec  CCallTarget     -- What to call
                CCallConv       -- Calling convention to use.
                Safety
  deriving( CCallSpec -> CCallSpec -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CCallSpec -> CCallSpec -> Bool
$c/= :: CCallSpec -> CCallSpec -> Bool
== :: CCallSpec -> CCallSpec -> Bool
$c== :: CCallSpec -> CCallSpec -> Bool
Eq )

-- The call target:

-- | How to call a particular function in C-land.
data CCallTarget
  -- An "unboxed" ccall# to named function in a particular package.
  = StaticTarget
        SourceText                -- of the CLabelString.
                                  -- See Note [Pragma source text] in GHC.Types.SourceText
        CLabelString                    -- C-land name of label.

        (Maybe Unit)                    -- What package the function is in.
                                        -- If Nothing, then it's taken to be in the current package.
                                        -- Note: This information is only used for PrimCalls on Windows.
                                        --       See CLabel.labelDynamic and CoreToStg.coreToStgApp
                                        --       for the difference in representation between PrimCalls
                                        --       and ForeignCalls. If the CCallTarget is representing
                                        --       a regular ForeignCall then it's safe to set this to Nothing.

  -- The first argument of the import is the name of a function pointer (an Addr#).
  --    Used when importing a label as "foreign import ccall "dynamic" ..."
        Bool                            -- True => really a function
                                        -- False => a value; only
                                        -- allowed in CAPI imports
  | DynamicTarget

  deriving( CCallTarget -> CCallTarget -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CCallTarget -> CCallTarget -> Bool
$c/= :: CCallTarget -> CCallTarget -> Bool
== :: CCallTarget -> CCallTarget -> Bool
$c== :: CCallTarget -> CCallTarget -> Bool
Eq, Typeable CCallTarget
CCallTarget -> DataType
CCallTarget -> Constr
(forall b. Data b => b -> b) -> CCallTarget -> CCallTarget
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> CCallTarget -> u
forall u. (forall d. Data d => d -> u) -> CCallTarget -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CCallTarget -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CCallTarget -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CCallTarget
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CCallTarget -> c CCallTarget
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CCallTarget)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CCallTarget)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CCallTarget -> m CCallTarget
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CCallTarget -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CCallTarget -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CCallTarget -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CCallTarget -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CCallTarget -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CCallTarget -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CCallTarget -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CCallTarget -> r
gmapT :: (forall b. Data b => b -> b) -> CCallTarget -> CCallTarget
$cgmapT :: (forall b. Data b => b -> b) -> CCallTarget -> CCallTarget
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CCallTarget)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CCallTarget)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CCallTarget)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CCallTarget)
dataTypeOf :: CCallTarget -> DataType
$cdataTypeOf :: CCallTarget -> DataType
toConstr :: CCallTarget -> Constr
$ctoConstr :: CCallTarget -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CCallTarget
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CCallTarget
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CCallTarget -> c CCallTarget
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CCallTarget -> c CCallTarget
Data )

isDynamicTarget :: CCallTarget -> Bool
isDynamicTarget :: CCallTarget -> Bool
isDynamicTarget CCallTarget
DynamicTarget = Bool
True
isDynamicTarget CCallTarget
_             = Bool
False

{-
Stuff to do with calling convention:

ccall:          Caller allocates parameters, *and* deallocates them.

stdcall:        Caller allocates parameters, callee deallocates.
                Function name has @N after it, where N is number of arg bytes
                e.g.  _Foo@8. This convention is x86 (win32) specific.

See: http://www.programmersheaven.com/2/Calling-conventions
-}

-- any changes here should be replicated in the Callconv type in template haskell
data CCallConv
  = CCallConv
  | CApiConv
  | StdCallConv
  | PrimCallConv
  | JavaScriptCallConv
  deriving (CCallConv -> CCallConv -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CCallConv -> CCallConv -> Bool
$c/= :: CCallConv -> CCallConv -> Bool
== :: CCallConv -> CCallConv -> Bool
$c== :: CCallConv -> CCallConv -> Bool
Eq, Typeable CCallConv
CCallConv -> DataType
CCallConv -> Constr
(forall b. Data b => b -> b) -> CCallConv -> CCallConv
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> CCallConv -> u
forall u. (forall d. Data d => d -> u) -> CCallConv -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CCallConv -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CCallConv -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CCallConv
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CCallConv -> c CCallConv
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CCallConv)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c CCallConv)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CCallConv -> m CCallConv
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CCallConv -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CCallConv -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CCallConv -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CCallConv -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CCallConv -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CCallConv -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CCallConv -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CCallConv -> r
gmapT :: (forall b. Data b => b -> b) -> CCallConv -> CCallConv
$cgmapT :: (forall b. Data b => b -> b) -> CCallConv -> CCallConv
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c CCallConv)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c CCallConv)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CCallConv)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CCallConv)
dataTypeOf :: CCallConv -> DataType
$cdataTypeOf :: CCallConv -> DataType
toConstr :: CCallConv -> Constr
$ctoConstr :: CCallConv -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CCallConv
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CCallConv
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CCallConv -> c CCallConv
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CCallConv -> c CCallConv
Data, Int -> CCallConv
CCallConv -> Int
CCallConv -> [CCallConv]
CCallConv -> CCallConv
CCallConv -> CCallConv -> [CCallConv]
CCallConv -> CCallConv -> CCallConv -> [CCallConv]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CCallConv -> CCallConv -> CCallConv -> [CCallConv]
$cenumFromThenTo :: CCallConv -> CCallConv -> CCallConv -> [CCallConv]
enumFromTo :: CCallConv -> CCallConv -> [CCallConv]
$cenumFromTo :: CCallConv -> CCallConv -> [CCallConv]
enumFromThen :: CCallConv -> CCallConv -> [CCallConv]
$cenumFromThen :: CCallConv -> CCallConv -> [CCallConv]
enumFrom :: CCallConv -> [CCallConv]
$cenumFrom :: CCallConv -> [CCallConv]
fromEnum :: CCallConv -> Int
$cfromEnum :: CCallConv -> Int
toEnum :: Int -> CCallConv
$ctoEnum :: Int -> CCallConv
pred :: CCallConv -> CCallConv
$cpred :: CCallConv -> CCallConv
succ :: CCallConv -> CCallConv
$csucc :: CCallConv -> CCallConv
Enum)

instance Outputable CCallConv where
  ppr :: CCallConv -> SDoc
ppr CCallConv
StdCallConv = forall doc. IsLine doc => String -> doc
text String
"stdcall"
  ppr CCallConv
CCallConv   = forall doc. IsLine doc => String -> doc
text String
"ccall"
  ppr CCallConv
CApiConv    = forall doc. IsLine doc => String -> doc
text String
"capi"
  ppr CCallConv
PrimCallConv = forall doc. IsLine doc => String -> doc
text String
"prim"
  ppr CCallConv
JavaScriptCallConv = forall doc. IsLine doc => String -> doc
text String
"javascript"

defaultCCallConv :: CCallConv
defaultCCallConv :: CCallConv
defaultCCallConv = CCallConv
CCallConv

ccallConvToInt :: CCallConv -> Int
ccallConvToInt :: CCallConv -> Int
ccallConvToInt CCallConv
StdCallConv = Int
0
ccallConvToInt CCallConv
CCallConv   = Int
1
ccallConvToInt CCallConv
CApiConv    = forall a. HasCallStack => String -> a
panic String
"ccallConvToInt CApiConv"
ccallConvToInt (PrimCallConv {}) = forall a. HasCallStack => String -> a
panic String
"ccallConvToInt PrimCallConv"
ccallConvToInt CCallConv
JavaScriptCallConv = forall a. HasCallStack => String -> a
panic String
"ccallConvToInt JavaScriptCallConv"

{-
Generate the gcc attribute corresponding to the given
calling convention (used by PprAbsC):
-}

ccallConvAttribute :: CCallConv -> SDoc
ccallConvAttribute :: CCallConv -> SDoc
ccallConvAttribute CCallConv
StdCallConv       = forall doc. IsLine doc => String -> doc
text String
"__attribute__((__stdcall__))"
ccallConvAttribute CCallConv
CCallConv         = forall doc. IsOutput doc => doc
empty
ccallConvAttribute CCallConv
CApiConv          = forall doc. IsOutput doc => doc
empty
ccallConvAttribute (PrimCallConv {}) = forall a. HasCallStack => String -> a
panic String
"ccallConvAttribute PrimCallConv"
ccallConvAttribute CCallConv
JavaScriptCallConv = forall a. HasCallStack => String -> a
panic String
"ccallConvAttribute JavaScriptCallConv"

type CLabelString = FastString          -- A C label, completely unencoded

pprCLabelString :: CLabelString -> SDoc
pprCLabelString :: CLabelString -> SDoc
pprCLabelString CLabelString
lbl = forall doc. IsLine doc => CLabelString -> doc
ftext CLabelString
lbl

isCLabelString :: CLabelString -> Bool  -- Checks to see if this is a valid C label
isCLabelString :: CLabelString -> Bool
isCLabelString CLabelString
lbl
  = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
ok (CLabelString -> String
unpackFS CLabelString
lbl)
  where
    ok :: Char -> Bool
ok Char
c = Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'_' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'.'
        -- The '.' appears in e.g. "foo.so" in the
        -- module part of a ExtName.  Maybe it should be separate

-- Printing into C files:

instance Outputable CExportSpec where
  ppr :: CExportSpec -> SDoc
ppr (CExportStatic SourceText
_ CLabelString
str CCallConv
_) = CLabelString -> SDoc
pprCLabelString CLabelString
str

instance Outputable CCallSpec where
  ppr :: CCallSpec -> SDoc
ppr (CCallSpec CCallTarget
fun CCallConv
cconv Safety
safety)
    = forall doc. IsLine doc => [doc] -> doc
hcat [ forall doc. IsOutput doc => doc -> doc
whenPprDebug SDoc
callconv, CCallTarget -> SDoc
ppr_fun CCallTarget
fun, forall doc. IsLine doc => String -> doc
text String
" ::" ]
    where
      callconv :: SDoc
callconv = forall doc. IsLine doc => String -> doc
text String
"{-" forall doc. IsLine doc => doc -> doc -> doc
<> forall a. Outputable a => a -> SDoc
ppr CCallConv
cconv forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => String -> doc
text String
"-}"

      gc_suf :: SDoc
gc_suf | Safety -> Bool
playSafe Safety
safety = forall doc. IsLine doc => String -> doc
text String
"_safe"
             | Bool
otherwise       = forall doc. IsLine doc => String -> doc
text String
"_unsafe"

      ppr_fun :: CCallTarget -> SDoc
ppr_fun (StaticTarget SourceText
st CLabelString
lbl Maybe Unit
mPkgId Bool
isFun)
        = (if Bool
isFun then forall doc. IsLine doc => String -> doc
text String
"__ffi_static_ccall"
                    else forall doc. IsLine doc => String -> doc
text String
"__ffi_static_ccall_value")
       forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
gc_suf
       forall doc. IsLine doc => doc -> doc -> doc
<+> (case Maybe Unit
mPkgId of
            Maybe Unit
Nothing -> forall doc. IsOutput doc => doc
empty
            Just Unit
pkgId -> forall a. Outputable a => a -> SDoc
ppr Unit
pkgId)
       forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => String -> doc
text String
":"
       forall doc. IsLine doc => doc -> doc -> doc
<> forall a. Outputable a => a -> SDoc
ppr CLabelString
lbl
       forall doc. IsLine doc => doc -> doc -> doc
<+> (SourceText -> SDoc -> SDoc
pprWithSourceText SourceText
st forall doc. IsOutput doc => doc
empty)

      ppr_fun CCallTarget
DynamicTarget
        = forall doc. IsLine doc => String -> doc
text String
"__ffi_dyn_ccall" forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
gc_suf forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => String -> doc
text String
"\"\""

-- The filename for a C header file
-- Note [Pragma source text] in GHC.Types.SourceText
data Header = Header SourceText FastString
    deriving (Header -> Header -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Header -> Header -> Bool
$c/= :: Header -> Header -> Bool
== :: Header -> Header -> Bool
$c== :: Header -> Header -> Bool
Eq, Typeable Header
Header -> DataType
Header -> Constr
(forall b. Data b => b -> b) -> Header -> Header
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Header -> u
forall u. (forall d. Data d => d -> u) -> Header -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Header -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Header -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Header -> m Header
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Header -> m Header
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Header
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Header -> c Header
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Header)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Header)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Header -> m Header
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Header -> m Header
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Header -> m Header
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Header -> m Header
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Header -> m Header
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Header -> m Header
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Header -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Header -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Header -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Header -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Header -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Header -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Header -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Header -> r
gmapT :: (forall b. Data b => b -> b) -> Header -> Header
$cgmapT :: (forall b. Data b => b -> b) -> Header -> Header
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Header)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Header)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Header)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Header)
dataTypeOf :: Header -> DataType
$cdataTypeOf :: Header -> DataType
toConstr :: Header -> Constr
$ctoConstr :: Header -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Header
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Header
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Header -> c Header
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Header -> c Header
Data)

instance Outputable Header where
    ppr :: Header -> SDoc
ppr (Header SourceText
st CLabelString
h) = SourceText -> SDoc -> SDoc
pprWithSourceText SourceText
st (forall doc. IsLine doc => doc -> doc
doubleQuotes forall a b. (a -> b) -> a -> b
$ forall a. Outputable a => a -> SDoc
ppr CLabelString
h)

-- | A C type, used in CAPI FFI calls
--
--  - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'{-\# CTYPE'@,
--        'GHC.Parser.Annotation.AnnHeader','GHC.Parser.Annotation.AnnVal',
--        'GHC.Parser.Annotation.AnnClose' @'\#-}'@,

-- For details on above see Note [exact print annotations] in "GHC.Parser.Annotation"
data CType = CType SourceText -- Note [Pragma source text] in GHC.Types.SourceText
                   (Maybe Header) -- header to include for this type
                   (SourceText,FastString) -- the type itself
    deriving (CType -> CType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CType -> CType -> Bool
$c/= :: CType -> CType -> Bool
== :: CType -> CType -> Bool
$c== :: CType -> CType -> Bool
Eq, Typeable CType
CType -> DataType
CType -> Constr
(forall b. Data b => b -> b) -> CType -> CType
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> CType -> u
forall u. (forall d. Data d => d -> u) -> CType -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> CType -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> CType -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CType -> m CType
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CType -> m CType
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CType
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CType -> c CType
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CType)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c CType)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CType -> m CType
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CType -> m CType
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CType -> m CType
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CType -> m CType
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CType -> m CType
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CType -> m CType
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CType -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CType -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CType -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CType -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> CType -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> CType -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> CType -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> CType -> r
gmapT :: (forall b. Data b => b -> b) -> CType -> CType
$cgmapT :: (forall b. Data b => b -> b) -> CType -> CType
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c CType)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c CType)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CType)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CType)
dataTypeOf :: CType -> DataType
$cdataTypeOf :: CType -> DataType
toConstr :: CType -> Constr
$ctoConstr :: CType -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CType
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CType
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CType -> c CType
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CType -> c CType
Data)

instance Outputable CType where
    ppr :: CType -> SDoc
ppr (CType SourceText
stp Maybe Header
mh (SourceText
stct,CLabelString
ct))
      = SourceText -> SDoc -> SDoc
pprWithSourceText SourceText
stp (forall doc. IsLine doc => String -> doc
text String
"{-# CTYPE") forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc
hDoc
        forall doc. IsLine doc => doc -> doc -> doc
<+> SourceText -> SDoc -> SDoc
pprWithSourceText SourceText
stct (forall doc. IsLine doc => doc -> doc
doubleQuotes (forall doc. IsLine doc => CLabelString -> doc
ftext CLabelString
ct)) forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => String -> doc
text String
"#-}"
        where hDoc :: SDoc
hDoc = case Maybe Header
mh of
                     Maybe Header
Nothing -> forall doc. IsOutput doc => doc
empty
                     Just Header
h -> forall a. Outputable a => a -> SDoc
ppr Header
h

{-
************************************************************************
*                                                                      *
\subsubsection{Misc}
*                                                                      *
************************************************************************
-}

instance Binary ForeignCall where
    put_ :: BinHandle -> ForeignCall -> IO ()
put_ BinHandle
bh (CCall CCallSpec
aa) = forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CCallSpec
aa
    get :: BinHandle -> IO ForeignCall
get BinHandle
bh = do CCallSpec
aa <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh; forall (m :: * -> *) a. Monad m => a -> m a
return (CCallSpec -> ForeignCall
CCall CCallSpec
aa)

instance Binary Safety where
    put_ :: BinHandle -> Safety -> IO ()
put_ BinHandle
bh Safety
PlaySafe =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh Safety
PlayInterruptible =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
    put_ BinHandle
bh Safety
PlayRisky =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2
    get :: BinHandle -> IO Safety
get BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return Safety
PlaySafe
              Word8
1 -> forall (m :: * -> *) a. Monad m => a -> m a
return Safety
PlayInterruptible
              Word8
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Safety
PlayRisky

instance Binary CExportSpec where
    put_ :: BinHandle -> CExportSpec -> IO ()
put_ BinHandle
bh (CExportStatic SourceText
ss CLabelString
aa CCallConv
ab) = do
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh SourceText
ss
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CLabelString
aa
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CCallConv
ab
    get :: BinHandle -> IO CExportSpec
get BinHandle
bh = do
          SourceText
ss <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          CLabelString
aa <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          CCallConv
ab <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          forall (m :: * -> *) a. Monad m => a -> m a
return (SourceText -> CLabelString -> CCallConv -> CExportSpec
CExportStatic SourceText
ss CLabelString
aa CCallConv
ab)

instance Binary CCallSpec where
    put_ :: BinHandle -> CCallSpec -> IO ()
put_ BinHandle
bh (CCallSpec CCallTarget
aa CCallConv
ab Safety
ac) = do
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CCallTarget
aa
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CCallConv
ab
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Safety
ac
    get :: BinHandle -> IO CCallSpec
get BinHandle
bh = do
          CCallTarget
aa <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          CCallConv
ab <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          Safety
ac <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          forall (m :: * -> *) a. Monad m => a -> m a
return (CCallTarget -> CCallConv -> Safety -> CCallSpec
CCallSpec CCallTarget
aa CCallConv
ab Safety
ac)

instance Binary CCallTarget where
    put_ :: BinHandle -> CCallTarget -> IO ()
put_ BinHandle
bh (StaticTarget SourceText
ss CLabelString
aa Maybe Unit
ab Bool
ac) = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh SourceText
ss
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CLabelString
aa
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Maybe Unit
ab
            forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Bool
ac
    put_ BinHandle
bh CCallTarget
DynamicTarget =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
    get :: BinHandle -> IO CCallTarget
get BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> do SourceText
ss <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      CLabelString
aa <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      Maybe Unit
ab <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      Bool
ac <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      forall (m :: * -> *) a. Monad m => a -> m a
return (SourceText -> CLabelString -> Maybe Unit -> Bool -> CCallTarget
StaticTarget SourceText
ss CLabelString
aa Maybe Unit
ab Bool
ac)
              Word8
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return CCallTarget
DynamicTarget

instance Binary CCallConv where
    put_ :: BinHandle -> CCallConv -> IO ()
put_ BinHandle
bh CCallConv
CCallConv =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh CCallConv
StdCallConv =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
    put_ BinHandle
bh CCallConv
PrimCallConv =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2
    put_ BinHandle
bh CCallConv
CApiConv =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
3
    put_ BinHandle
bh CCallConv
JavaScriptCallConv =
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
4
    get :: BinHandle -> IO CCallConv
get BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return CCallConv
CCallConv
              Word8
1 -> forall (m :: * -> *) a. Monad m => a -> m a
return CCallConv
StdCallConv
              Word8
2 -> forall (m :: * -> *) a. Monad m => a -> m a
return CCallConv
PrimCallConv
              Word8
3 -> forall (m :: * -> *) a. Monad m => a -> m a
return CCallConv
CApiConv
              Word8
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return CCallConv
JavaScriptCallConv

instance Binary CType where
    put_ :: BinHandle -> CType -> IO ()
put_ BinHandle
bh (CType SourceText
s Maybe Header
mh (SourceText, CLabelString)
fs) = do forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh SourceText
s
                                 forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Maybe Header
mh
                                 forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh (SourceText, CLabelString)
fs
    get :: BinHandle -> IO CType
get BinHandle
bh = do SourceText
s  <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                Maybe Header
mh <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                (SourceText, CLabelString)
fs <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                forall (m :: * -> *) a. Monad m => a -> m a
return (SourceText -> Maybe Header -> (SourceText, CLabelString) -> CType
CType SourceText
s Maybe Header
mh (SourceText, CLabelString)
fs)

instance Binary Header where
    put_ :: BinHandle -> Header -> IO ()
put_ BinHandle
bh (Header SourceText
s CLabelString
h) = forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh SourceText
s forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CLabelString
h
    get :: BinHandle -> IO Header
get BinHandle
bh = do SourceText
s <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                CLabelString
h <- forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                forall (m :: * -> *) a. Monad m => a -> m a
return (SourceText -> CLabelString -> Header
Header SourceText
s CLabelString
h)