-- GENERATED by C->Haskell Compiler, version 0.28.7 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "lib/CPython/Types/Function.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

module CPython.Types.Function
  ( Function
  , functionType
  , new
  , getCode
  , getGlobals
  , getModule
  , getDefaults
  , setDefaults
  , getClosure
  , setClosure
  , getAnnotations
  , setAnnotations
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp





import qualified CPython.Constants as Const
import           CPython.Internal hiding (new)
import           CPython.Types.Code (Code)

newtype Function = Function (ForeignPtr Function)

instance Object Function where
  toObject :: Function -> SomeObject
toObject (Function x :: ForeignPtr Function
x) = ForeignPtr Function -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr Function
x
  fromForeignPtr :: ForeignPtr Function -> Function
fromForeignPtr = ForeignPtr Function -> Function
Function

instance Concrete Function where
  concreteType :: Function -> Type
concreteType _ = Type
functionType

functionType :: (Type)
functionType :: Type
functionType =
  IO Type -> Type
forall a. IO a -> a
C2HSImp.unsafePerformIO (IO Type -> Type) -> IO Type -> Type
forall a b. (a -> b) -> a -> b
$
  IO (Ptr ())
functionType'_ IO (Ptr ()) -> (Ptr () -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO Type
forall obj a. Object obj => Ptr a -> IO obj
peekStaticObject Ptr ()
res IO Type -> (Type -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Type
res' ->
  Type -> IO Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
res')

{-# LINE 49 "lib/CPython/Types/Function.chs" #-}


-- | Return a new function associated with the given code object. The second
-- parameter will be used as the globals accessible to the function.
--
-- The function's docstring, name, and @__module__@ are retrieved from the
-- code object. The parameter defaults and closure are set to 'Nothing'.
new :: (Code) -> (Dictionary) -> IO ((Function))
new a1 a2 =
  withObject a1 $ \a1' -> 
  withObject a2 $ \a2' -> 
  new'_ a1' a2' >>= \res ->
  stealObject res >>= \res' ->
  return (res')

{-# LINE 59 "lib/CPython/Types/Function.chs" #-}


-- | Return the code object associated with a function.
getCode :: (Function) -> IO ((Code))
getCode a1 =
  withObject a1 $ \a1' -> 
  getCode'_ a1' >>= \res ->
  peekObject res >>= \res' ->
  return (res')

{-# LINE 64 "lib/CPython/Types/Function.chs" #-}


-- | Return the globals dictionary associated with a function.
getGlobals :: (Function) -> IO ((Dictionary))
getGlobals a1 =
  withObject a1 $ \a1' -> 
  getGlobals'_ a1' >>= \res ->
  peekObject res >>= \res' ->
  return (res')

{-# LINE 69 "lib/CPython/Types/Function.chs" #-}


-- | Return the @__module__@ attribute of a function. This is normally
-- a 'Unicode' containing the module name, but can be set to any other
-- object by Python code.
getModule :: (Function) -> IO ((SomeObject))
getModule :: Function -> IO SomeObject
getModule a1 :: Function
a1 =
  Function -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Function
a1 ((Ptr () -> IO SomeObject) -> IO SomeObject)
-> (Ptr () -> IO SomeObject) -> IO SomeObject
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
getModule'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO SomeObject
forall obj a. Object obj => Ptr a -> IO obj
peekObject Ptr ()
res IO SomeObject -> (SomeObject -> IO SomeObject) -> IO SomeObject
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: SomeObject
res' ->
  SomeObject -> IO SomeObject
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeObject
res')

{-# LINE 76 "lib/CPython/Types/Function.chs" #-}


withNullableObject :: Object obj => Maybe obj -> (Ptr a -> IO b) -> IO b
withNullableObject Nothing io = do
  none <- Const.none
  withObject none io
withNullableObject (Just obj) io = withObject obj io

peekNullableObject :: Object obj => Ptr a -> IO (Maybe obj)
peekNullableObject :: Ptr a -> IO (Maybe obj)
peekNullableObject = (Ptr a -> IO obj) -> Ptr a -> IO (Maybe obj)
forall a b. (Ptr a -> IO b) -> Ptr a -> IO (Maybe b)
maybePeek Ptr a -> IO obj
forall obj a. Object obj => Ptr a -> IO obj
peekObject

-- | Return the default parameter values for a function. This can be a tuple
-- or 'Nothing'.
getDefaults :: (Function) -> IO ((Maybe Tuple))
getDefaults :: Function -> IO (Maybe Tuple)
getDefaults a1 :: Function
a1 =
  Function -> (Ptr () -> IO (Maybe Tuple)) -> IO (Maybe Tuple)
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Function
a1 ((Ptr () -> IO (Maybe Tuple)) -> IO (Maybe Tuple))
-> (Ptr () -> IO (Maybe Tuple)) -> IO (Maybe Tuple)
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
getDefaults'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO (Maybe Tuple)) -> IO (Maybe Tuple)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO (Maybe Tuple)
forall obj a. Object obj => Ptr a -> IO (Maybe obj)
peekNullableObject Ptr ()
res IO (Maybe Tuple)
-> (Maybe Tuple -> IO (Maybe Tuple)) -> IO (Maybe Tuple)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Maybe Tuple
res' ->
  Maybe Tuple -> IO (Maybe Tuple)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Tuple
res')

{-# LINE 91 "lib/CPython/Types/Function.chs" #-}


-- | Set the default values for a function.
setDefaults :: (Function) -> (Maybe Tuple) -> IO ((()))
setDefaults a1 a2 =
  withObject a1 $ \a1' -> 
  withNullableObject a2 $ \a2' -> 
  setDefaults'_ a1' a2' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 97 "lib/CPython/Types/Function.chs" #-}


-- | Return the closure associated with a function. This can be 'Nothing',
-- or a tuple of 'Cell's.
getClosure :: (Function) -> IO ((Maybe Tuple))
getClosure a1 =
  withObject a1 $ \a1' -> 
  getClosure'_ a1' >>= \res ->
  peekNullableObject res >>= \res' ->
  return (res')

{-# LINE 103 "lib/CPython/Types/Function.chs" #-}


-- | Set the closure associated with a function. The tuple should contain
-- 'Cell's.
setClosure :: (Function) -> (Maybe Tuple) -> IO ((()))
setClosure a1 a2 =
  withObject a1 $ \a1' -> 
  withNullableObject a2 $ \a2' -> 
  setClosure'_ a1' a2' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 110 "lib/CPython/Types/Function.chs" #-}


-- | Return the annotations for a function. This can be a mutable dictionary,
-- or 'Nothing'.
getAnnotations :: (Function) -> IO ((Maybe Dictionary))
getAnnotations :: Function -> IO (Maybe Dictionary)
getAnnotations a1 :: Function
a1 =
  Function
-> (Ptr () -> IO (Maybe Dictionary)) -> IO (Maybe Dictionary)
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Function
a1 ((Ptr () -> IO (Maybe Dictionary)) -> IO (Maybe Dictionary))
-> (Ptr () -> IO (Maybe Dictionary)) -> IO (Maybe Dictionary)
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
getAnnotations'_ Ptr ()
a1' IO (Ptr ())
-> (Ptr () -> IO (Maybe Dictionary)) -> IO (Maybe Dictionary)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO (Maybe Dictionary)
forall obj a. Object obj => Ptr a -> IO (Maybe obj)
peekNullableObject Ptr ()
res IO (Maybe Dictionary)
-> (Maybe Dictionary -> IO (Maybe Dictionary))
-> IO (Maybe Dictionary)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Maybe Dictionary
res' ->
  Maybe Dictionary -> IO (Maybe Dictionary)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Dictionary
res')

{-# LINE 116 "lib/CPython/Types/Function.chs" #-}


-- | Set the annotations for a function object.
setAnnotations :: (Function) -> (Maybe Dictionary) -> IO ((()))
setAnnotations a1 a2 =
  withObject a1 $ \a1' -> 
  withNullableObject a2 $ \a2' -> 
  setAnnotations'_ a1' a2' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 122 "lib/CPython/Types/Function.chs" #-}


foreign import ccall unsafe "CPython/Types/Function.chs.h hscpython_PyFunction_Type"
  functionType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_New"
  new'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_GetCode"
  getCode'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_GetGlobals"
  getGlobals'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_GetModule"
  getModule'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_GetDefaults"
  getDefaults'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_SetDefaults"
  setDefaults'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_GetClosure"
  getClosure'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_SetClosure"
  setClosure'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_GetAnnotations"
  getAnnotations'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Function.chs.h PyFunction_SetAnnotations"
  setAnnotations'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))