-- GENERATED by C->Haskell Compiler, version 0.16.4 Crystal Seed, 24 Jan 2009 (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 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 x) = SomeObject x
	fromForeignPtr = Function

instance Concrete Function where
	concreteType _ = functionType

functionType :: Type
functionType =
  unsafePerformIO $
  let {res = functionType'_} in
  peekStaticObject res >>= \res' ->
  return (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 a1 =
  withObject a1 $ \a1' -> 
  getModule'_ a1' >>= \res ->
  peekObject res >>= \res' ->
  return (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 = maybePeek peekObject

-- | Return the default parameter values for a function. This can be a tuple
-- or 'Nothing'.
getDefaults :: Function -> IO (Maybe Tuple)
getDefaults a1 =
  withObject a1 $ \a1' -> 
  getDefaults'_ a1' >>= \res ->
  peekNullableObject res >>= \res' ->
  return (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 a1 =
  withObject a1 $ \a1' -> 
  getAnnotations'_ a1' >>= \res ->
  peekNullableObject res >>= \res' ->
  return (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'_ :: (Ptr ())

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

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

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

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

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

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

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

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

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

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