{-# OPTIONS_GHC -optc-DUSE_RINTERNALS #-}
{-# LINE 1 "src/Foreign/R/Internal.hsc" #-}
-- |
-- Copyright: (C) 2013 Amgen, Inc.
--
-- Low-level bindings to core R datatypes and functions which depend on
-- computing offsets of C struct field. We use hsc2hs for this purpose.

{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}

{-# LINE 14 "src/Foreign/R/Internal.hsc" #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

{-# LINE 23 "src/Foreign/R/Internal.hsc" #-}
-- We don't use ticks in this module, because they confuse hsc2hs.
{-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-}

{-# LINE 26 "src/Foreign/R/Internal.hsc" #-}
module Foreign.R.Internal where

import Control.Memory.Region
import {-# SOURCE #-} Language.R.HExp (HExp)
import Foreign.R.Type
import Foreign.R.Type as R
import Foreign.R.Context (SEXP0)

import Control.Applicative
import Control.DeepSeq (NFData(..))
import Control.Monad.Primitive ( unsafeInlineIO )
import Data.Singletons (fromSing)

{-# LINE 41 "src/Foreign/R/Internal.hsc" #-}
import Foreign (Ptr, castPtr, plusPtr, Storable(..))
import Foreign.C
import Prelude hiding (asTypeOf, length)







--------------------------------------------------------------------------------
-- R data structures                                                          --
--------------------------------------------------------------------------------

-- | The basic type of all R expressions, classified by the form of the
-- expression, and the memory region in which it has been allocated.
newtype SEXP s (a :: SEXPTYPE) = SEXP { SEXP s a -> Ptr (HExp s a)
unSEXP :: Ptr (HExp s a) }
  deriving ( SEXP s a -> SEXP s a -> Bool
(SEXP s a -> SEXP s a -> Bool)
-> (SEXP s a -> SEXP s a -> Bool) -> Eq (SEXP s a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
/= :: SEXP s a -> SEXP s a -> Bool
$c/= :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
== :: SEXP s a -> SEXP s a -> Bool
$c== :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
Eq
           , Eq (SEXP s a)
Eq (SEXP s a) =>
(SEXP s a -> SEXP s a -> Ordering)
-> (SEXP s a -> SEXP s a -> Bool)
-> (SEXP s a -> SEXP s a -> Bool)
-> (SEXP s a -> SEXP s a -> Bool)
-> (SEXP s a -> SEXP s a -> Bool)
-> (SEXP s a -> SEXP s a -> SEXP s a)
-> (SEXP s a -> SEXP s a -> SEXP s a)
-> Ord (SEXP s a)
SEXP s a -> SEXP s a -> Bool
SEXP s a -> SEXP s a -> Ordering
SEXP s a -> SEXP s a -> SEXP s a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall s (a :: SEXPTYPE). Eq (SEXP s a)
forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Ordering
forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> SEXP s a
min :: SEXP s a -> SEXP s a -> SEXP s a
$cmin :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> SEXP s a
max :: SEXP s a -> SEXP s a -> SEXP s a
$cmax :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> SEXP s a
>= :: SEXP s a -> SEXP s a -> Bool
$c>= :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
> :: SEXP s a -> SEXP s a -> Bool
$c> :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
<= :: SEXP s a -> SEXP s a -> Bool
$c<= :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
< :: SEXP s a -> SEXP s a -> Bool
$c< :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Bool
compare :: SEXP s a -> SEXP s a -> Ordering
$ccompare :: forall s (a :: SEXPTYPE). SEXP s a -> SEXP s a -> Ordering
$cp1Ord :: forall s (a :: SEXPTYPE). Eq (SEXP s a)
Ord
           , Ptr b -> Int -> IO (SEXP s a)
Ptr b -> Int -> SEXP s a -> IO ()
Ptr (SEXP s a) -> IO (SEXP s a)
Ptr (SEXP s a) -> Int -> IO (SEXP s a)
Ptr (SEXP s a) -> Int -> SEXP s a -> IO ()
Ptr (SEXP s a) -> SEXP s a -> IO ()
SEXP s a -> Int
(SEXP s a -> Int)
-> (SEXP s a -> Int)
-> (Ptr (SEXP s a) -> Int -> IO (SEXP s a))
-> (Ptr (SEXP s a) -> Int -> SEXP s a -> IO ())
-> (forall b. Ptr b -> Int -> IO (SEXP s a))
-> (forall b. Ptr b -> Int -> SEXP s a -> IO ())
-> (Ptr (SEXP s a) -> IO (SEXP s a))
-> (Ptr (SEXP s a) -> SEXP s a -> IO ())
-> Storable (SEXP s a)
forall b. Ptr b -> Int -> IO (SEXP s a)
forall b. Ptr b -> Int -> SEXP s a -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
forall s (a :: SEXPTYPE). Ptr (SEXP s a) -> IO (SEXP s a)
forall s (a :: SEXPTYPE). Ptr (SEXP s a) -> Int -> IO (SEXP s a)
forall s (a :: SEXPTYPE).
Ptr (SEXP s a) -> Int -> SEXP s a -> IO ()
forall s (a :: SEXPTYPE). Ptr (SEXP s a) -> SEXP s a -> IO ()
forall s (a :: SEXPTYPE). SEXP s a -> Int
forall s (a :: SEXPTYPE) b. Ptr b -> Int -> IO (SEXP s a)
forall s (a :: SEXPTYPE) b. Ptr b -> Int -> SEXP s a -> IO ()
poke :: Ptr (SEXP s a) -> SEXP s a -> IO ()
$cpoke :: forall s (a :: SEXPTYPE). Ptr (SEXP s a) -> SEXP s a -> IO ()
peek :: Ptr (SEXP s a) -> IO (SEXP s a)
$cpeek :: forall s (a :: SEXPTYPE). Ptr (SEXP s a) -> IO (SEXP s a)
pokeByteOff :: Ptr b -> Int -> SEXP s a -> IO ()
$cpokeByteOff :: forall s (a :: SEXPTYPE) b. Ptr b -> Int -> SEXP s a -> IO ()
peekByteOff :: Ptr b -> Int -> IO (SEXP s a)
$cpeekByteOff :: forall s (a :: SEXPTYPE) b. Ptr b -> Int -> IO (SEXP s a)
pokeElemOff :: Ptr (SEXP s a) -> Int -> SEXP s a -> IO ()
$cpokeElemOff :: forall s (a :: SEXPTYPE).
Ptr (SEXP s a) -> Int -> SEXP s a -> IO ()
peekElemOff :: Ptr (SEXP s a) -> Int -> IO (SEXP s a)
$cpeekElemOff :: forall s (a :: SEXPTYPE). Ptr (SEXP s a) -> Int -> IO (SEXP s a)
alignment :: SEXP s a -> Int
$calignment :: forall s (a :: SEXPTYPE). SEXP s a -> Int
sizeOf :: SEXP s a -> Int
$csizeOf :: forall s (a :: SEXPTYPE). SEXP s a -> Int
Storable

{-# LINE 64 "src/Foreign/R/Internal.hsc" #-}
           )

instance Show (SEXP s a) where
  show :: SEXP s a -> String
show (SEXP ptr :: Ptr (HExp s a)
ptr) = Ptr (HExp s a) -> String
forall a. Show a => a -> String
show Ptr (HExp s a)
ptr

instance NFData (SEXP s a) where
  rnf :: SEXP s a -> ()
rnf = (SEXP s a -> () -> ()
forall a b. a -> b -> b
`seq` ())

-- | Add a type index to the pointer.
sexp :: SEXP0 -> SEXP s a
sexp :: SEXP0 -> SEXP s a
sexp = Ptr (HExp s a) -> SEXP s a
forall s (a :: SEXPTYPE). Ptr (HExp s a) -> SEXP s a
SEXP (Ptr (HExp s a) -> SEXP s a)
-> (SEXP0 -> Ptr (HExp s a)) -> SEXP0 -> SEXP s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP0 -> Ptr (HExp s a)
forall a b. Ptr a -> Ptr b
castPtr

-- | Remove the type index from the pointer.
unsexp :: SEXP s a -> SEXP0
unsexp :: SEXP s a -> SEXP0
unsexp = Ptr (HExp s a) -> SEXP0
forall a b. Ptr a -> Ptr b
castPtr (Ptr (HExp s a) -> SEXP0)
-> (SEXP s a -> Ptr (HExp s a)) -> SEXP s a -> SEXP0
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP s a -> Ptr (HExp s a)
forall s (a :: SEXPTYPE). SEXP s a -> Ptr (HExp s a)
unSEXP

-- | Like 'sexp' but for 'SomeSEXP'.
somesexp :: SEXP0 -> SomeSEXP s
somesexp :: SEXP0 -> SomeSEXP s
somesexp = SEXP s Any -> SomeSEXP s
forall s (a :: SEXPTYPE). SEXP s a -> SomeSEXP s
SomeSEXP (SEXP s Any -> SomeSEXP s)
-> (SEXP0 -> SEXP s Any) -> SEXP0 -> SomeSEXP s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP0 -> SEXP s Any
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
sexp

-- | Release object into another region. Releasing is safe so long as the target
-- region is "smaller" than the source region, in the sense of
-- '(Control.Memory.Region.<=)'.
release :: (t <= s) => SEXP s a -> SEXP t a
release :: SEXP s a -> SEXP t a
release = SEXP s a -> SEXP t a
forall s (a :: SEXPTYPE) r. SEXP s a -> SEXP r a
unsafeRelease

unsafeRelease :: SEXP s a -> SEXP r a
unsafeRelease :: SEXP s a -> SEXP r a
unsafeRelease = SEXP0 -> SEXP r a
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
sexp (SEXP0 -> SEXP r a) -> (SEXP s a -> SEXP0) -> SEXP s a -> SEXP r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp

unsafeReleaseSome :: SomeSEXP s -> SomeSEXP g
unsafeReleaseSome :: SomeSEXP s -> SomeSEXP g
unsafeReleaseSome (SomeSEXP x :: SEXP s a
x) = SEXP g a -> SomeSEXP g
forall s (a :: SEXPTYPE). SEXP s a -> SomeSEXP s
SomeSEXP (SEXP s a -> SEXP g a
forall s (a :: SEXPTYPE) r. SEXP s a -> SEXP r a
unsafeRelease SEXP s a
x)

-- | A 'SEXP' of unknown form.
data SomeSEXP s = forall a. SomeSEXP {-# UNPACK #-} !(SEXP s a)

instance Show (SomeSEXP s) where
  show :: SomeSEXP s -> String
show s :: SomeSEXP s
s = SomeSEXP s
-> (forall (a :: SEXPTYPE). SEXP s a -> String) -> String
forall s r.
SomeSEXP s -> (forall (a :: SEXPTYPE). SEXP s a -> r) -> r
unSomeSEXP SomeSEXP s
s forall a. Show a => a -> String
forall (a :: SEXPTYPE). SEXP s a -> String
show

instance Storable (SomeSEXP s) where
  sizeOf :: SomeSEXP s -> Int
sizeOf _ = SEXP Any Any -> Int
forall a. Storable a => a -> Int
sizeOf (forall s (a :: SEXPTYPE). SEXP s a
forall a. HasCallStack => a
undefined :: SEXP s a)
  alignment :: SomeSEXP s -> Int
alignment _ = SEXP Any Any -> Int
forall a. Storable a => a -> Int
alignment (forall s (a :: SEXPTYPE). SEXP s a
forall a. HasCallStack => a
undefined :: SEXP s a)
  peek :: Ptr (SomeSEXP s) -> IO (SomeSEXP s)
peek ptr :: Ptr (SomeSEXP s)
ptr = SEXP s Any -> SomeSEXP s
forall s (a :: SEXPTYPE). SEXP s a -> SomeSEXP s
SomeSEXP (SEXP s Any -> SomeSEXP s) -> IO (SEXP s Any) -> IO (SomeSEXP s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr (SEXP s Any) -> IO (SEXP s Any)
forall a. Storable a => Ptr a -> IO a
peek (Ptr (SomeSEXP s) -> Ptr (SEXP s Any)
forall a b. Ptr a -> Ptr b
castPtr Ptr (SomeSEXP s)
ptr)
  poke :: Ptr (SomeSEXP s) -> SomeSEXP s -> IO ()
poke ptr :: Ptr (SomeSEXP s)
ptr (SomeSEXP s :: SEXP s a
s) = Ptr (SEXP s a) -> SEXP s a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr (SomeSEXP s) -> Ptr (SEXP s a)
forall a b. Ptr a -> Ptr b
castPtr Ptr (SomeSEXP s)
ptr) SEXP s a
s

instance NFData (SomeSEXP s) where
  rnf :: SomeSEXP s -> ()
rnf = (SomeSEXP s -> () -> ()
forall a b. a -> b -> b
`seq` ())

-- | Deconstruct a 'SomeSEXP'. Takes a continuation since otherwise the
-- existentially quantified variable hidden inside 'SomeSEXP' would escape.
unSomeSEXP :: SomeSEXP s -> (forall a. SEXP s a -> r) -> r
unSomeSEXP :: SomeSEXP s -> (forall (a :: SEXPTYPE). SEXP s a -> r) -> r
unSomeSEXP (SomeSEXP s :: SEXP s a
s) k :: forall (a :: SEXPTYPE). SEXP s a -> r
k = SEXP s a -> r
forall (a :: SEXPTYPE). SEXP s a -> r
k SEXP s a
s

cIntConv :: (Integral a, Integral b) => a -> b
cIntConv :: a -> b
cIntConv = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral

cIntToEnum :: Enum a => CInt -> a
cIntToEnum :: CInt -> a
cIntToEnum = Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> (CInt -> Int) -> CInt -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Int
forall a b. (Integral a, Integral b) => a -> b
cIntConv

cUIntFromSingEnum :: SSEXPTYPE a -> CUInt
cUIntFromSingEnum :: SSEXPTYPE a -> CUInt
cUIntFromSingEnum = Int -> CUInt
forall a b. (Integral a, Integral b) => a -> b
cIntConv (Int -> CUInt) -> (SSEXPTYPE a -> Int) -> SSEXPTYPE a -> CUInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXPTYPE -> Int
forall a. Enum a => a -> Int
fromEnum (SEXPTYPE -> Int)
-> (SSEXPTYPE a -> SEXPTYPE) -> SSEXPTYPE a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SSEXPTYPE a -> SEXPTYPE
forall k (a :: k). SingKind k => Sing a -> Demote k
fromSing

cIntFromEnum :: Enum a => a -> CInt
cIntFromEnum :: a -> CInt
cIntFromEnum = Int -> CInt
forall a b. (Integral a, Integral b) => a -> b
cIntConv (Int -> CInt) -> (a -> Int) -> a -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Enum a => a -> Int
fromEnum

--------------------------------------------------------------------------------
-- Generic accessor functions                                                 --
--------------------------------------------------------------------------------

-- | Return the \"type\" tag (aka the form tag) of the given 'SEXP'. This
-- function is pure because the type of an object does not normally change over
-- the lifetime of the object.
typeOf :: SEXP s a -> SEXPTYPE
typeOf :: SEXP s a -> SEXPTYPE
typeOf s :: SEXP s a
s = IO SEXPTYPE -> SEXPTYPE
forall a. IO a -> a
unsafeInlineIO (IO SEXPTYPE -> SEXPTYPE) -> IO SEXPTYPE -> SEXPTYPE
forall a b. (a -> b) -> a -> b
$ CInt -> SEXPTYPE
forall a. Enum a => CInt -> a
cIntToEnum (CInt -> SEXPTYPE) -> IO CInt -> IO SEXPTYPE
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cTYPEOF (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s)

foreign import capi unsafe "TYPEOF" cTYPEOF :: SEXP0 -> IO CInt

-- | Set CAR field of object, when object is viewed as a cons cell.
setCar :: SEXP s a -> SEXP s b -> IO ()
setCar :: SEXP s a -> SEXP s b -> IO ()
setCar s :: SEXP s a
s s' :: SEXP s b
s' = (\hsc_ptr :: SEXP0
hsc_ptr -> SEXP0 -> Int -> Ptr Any -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff SEXP0
hsc_ptr 32) (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s) (SEXP0 -> Ptr Any
forall a b. Ptr a -> Ptr b
castPtr (SEXP0 -> Ptr Any) -> SEXP0 -> Ptr Any
forall a b. (a -> b) -> a -> b
$ SEXP s b -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s b
s')
{-# LINE 143 "src/Foreign/R/Internal.hsc" #-}

-- | Set CDR field of object, when object is viewed as a cons cell.
setCdr :: SEXP s a -> SEXP s b -> IO ()
setCdr :: SEXP s a -> SEXP s b -> IO ()
setCdr s :: SEXP s a
s s' :: SEXP s b
s' = (\hsc_ptr :: SEXP0
hsc_ptr -> SEXP0 -> Int -> Ptr Any -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff SEXP0
hsc_ptr 40) (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s) (SEXP0 -> Ptr Any
forall a b. Ptr a -> Ptr b
castPtr (SEXP0 -> Ptr Any) -> SEXP0 -> Ptr Any
forall a b. (a -> b) -> a -> b
$ SEXP s b -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s b
s')
{-# LINE 147 "src/Foreign/R/Internal.hsc" #-}

-- | Set TAG field of object, when object is viewed as a cons cell.
setTag :: SEXP s a -> SEXP s b -> IO ()
setTag :: SEXP s a -> SEXP s b -> IO ()
setTag s :: SEXP s a
s s' :: SEXP s b
s' = (\hsc_ptr :: SEXP0
hsc_ptr -> SEXP0 -> Int -> Ptr Any -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff SEXP0
hsc_ptr 48) (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s) (SEXP0 -> Ptr Any
forall a b. Ptr a -> Ptr b
castPtr (SEXP0 -> Ptr Any) -> SEXP0 -> Ptr Any
forall a b. (a -> b) -> a -> b
$ SEXP s b -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s b
s')
{-# LINE 151 "src/Foreign/R/Internal.hsc" #-}

--------------------------------------------------------------------------------
-- Coercion functions                                                         --
--------------------------------------------------------------------------------

-- $cast-coerce
--
-- /Coercions/ have no runtime cost, but are completely unsafe. Use with
-- caution, only when you know that a 'SEXP' is of the target type. /Casts/ are
-- safer, but introduce a runtime type check. The difference between the two is
-- akin to the difference between a C-style typecasts and C++-style
-- @dynamic_cast@'s.

unsafeCast :: SEXPTYPE -> SomeSEXP s -> SEXP s b
unsafeCast :: SEXPTYPE -> SomeSEXP s -> SEXP s b
unsafeCast ty :: SEXPTYPE
ty (SomeSEXP s :: SEXP s a
s)
  | SEXPTYPE
ty SEXPTYPE -> SEXPTYPE -> Bool
forall a. Eq a => a -> a -> Bool
== SEXP s a -> SEXPTYPE
forall s (a :: SEXPTYPE). SEXP s a -> SEXPTYPE
typeOf SEXP s a
s = SEXP s a -> SEXP s b
forall s (a :: SEXPTYPE) (b :: SEXPTYPE). SEXP s a -> SEXP s b
unsafeCoerce SEXP s a
s
  | Bool
otherwise =
    String -> SEXP s b
forall a. HasCallStack => String -> a
error (String -> SEXP s b) -> String -> SEXP s b
forall a b. (a -> b) -> a -> b
$ "cast: Dynamic type cast failed. Expected: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SEXPTYPE -> String
forall a. Show a => a -> String
show SEXPTYPE
ty String -> ShowS
forall a. [a] -> [a] -> [a]
++
            ". Actual: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SEXPTYPE -> String
forall a. Show a => a -> String
show (SEXP s a -> SEXPTYPE
forall s (a :: SEXPTYPE). SEXP s a -> SEXPTYPE
typeOf SEXP s a
s) String -> ShowS
forall a. [a] -> [a] -> [a]
++ "."

-- | Cast the type of a 'SEXP' into another type. This function is partial: at
-- runtime, an error is raised if the source form tag does not match the target
-- form tag.
cast :: SSEXPTYPE a -> SomeSEXP s -> SEXP s a
cast :: SSEXPTYPE a -> SomeSEXP s -> SEXP s a
cast ty :: SSEXPTYPE a
ty s :: SomeSEXP s
s = SEXPTYPE -> SomeSEXP s -> SEXP s a
forall s (b :: SEXPTYPE). SEXPTYPE -> SomeSEXP s -> SEXP s b
unsafeCast (Sing a -> Demote SEXPTYPE
forall k (a :: k). SingKind k => Sing a -> Demote k
fromSing Sing a
SSEXPTYPE a
ty) SomeSEXP s
s

-- | Cast form of first argument to that of the second argument.
asTypeOf :: SomeSEXP s -> SEXP s a -> SEXP s a
asTypeOf :: SomeSEXP s -> SEXP s a -> SEXP s a
asTypeOf s :: SomeSEXP s
s s' :: SEXP s a
s' = SEXP s a -> SEXPTYPE
forall s (a :: SEXPTYPE). SEXP s a -> SEXPTYPE
typeOf SEXP s a
s' SEXPTYPE -> SomeSEXP s -> SEXP s a
forall s (b :: SEXPTYPE). SEXPTYPE -> SomeSEXP s -> SEXP s b
`unsafeCast` SomeSEXP s
s

-- | Unsafe coercion from one form to another. This is unsafe, in the sense that
-- using this function improperly could cause code to crash in unpredictable
-- ways. Contrary to 'cast', it has no runtime cost since it does not introduce
-- any dynamic check at runtime.
unsafeCoerce :: SEXP s a -> SEXP s b
unsafeCoerce :: SEXP s a -> SEXP s b
unsafeCoerce = SEXP0 -> SEXP s b
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
sexp (SEXP0 -> SEXP s b) -> (SEXP s a -> SEXP0) -> SEXP s a -> SEXP s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP0 -> SEXP0
forall a b. Ptr a -> Ptr b
castPtr (SEXP0 -> SEXP0) -> (SEXP s a -> SEXP0) -> SEXP s a -> SEXP0
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp

--------------------------------------------------------------------------------
-- Vector accessor functions                                                  --
--------------------------------------------------------------------------------

-- | Length of the vector.
length :: R.IsVector a => SEXP s a -> IO Int
length :: SEXP s a -> IO Int
length s :: SEXP s a
s = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
             ((\hsc_ptr :: SEXP0
hsc_ptr -> SEXP0 -> Int -> IO CInt
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff SEXP0
hsc_ptr 32) (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s) :: IO CInt)
{-# LINE 196 "src/Foreign/R/Internal.hsc" #-}

-- | Extract the data pointer from a vector.
unsafeSEXPToVectorPtr :: SEXP s a -> Ptr ()
unsafeSEXPToVectorPtr :: SEXP s a -> Ptr ()
unsafeSEXPToVectorPtr s :: SEXP s a
s = (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s) SEXP0 -> Int -> Ptr ()
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (48)
{-# LINE 200 "src/Foreign/R/Internal.hsc" #-}

-- | Inverse of 'vectorPtr'.
unsafeVectorPtrToSEXP :: Ptr a -> SomeSEXP s
unsafeVectorPtrToSEXP :: Ptr a -> SomeSEXP s
unsafeVectorPtrToSEXP s :: Ptr a
s = SEXP s Any -> SomeSEXP s
forall s (a :: SEXPTYPE). SEXP s a -> SomeSEXP s
SomeSEXP (SEXP s Any -> SomeSEXP s) -> SEXP s Any -> SomeSEXP s
forall a b. (a -> b) -> a -> b
$ SEXP0 -> SEXP s Any
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
sexp (SEXP0 -> SEXP s Any) -> SEXP0 -> SEXP s Any
forall a b. (a -> b) -> a -> b
$ Ptr a
s Ptr a -> Int -> SEXP0
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (- (48))
{-# LINE 204 "src/Foreign/R/Internal.hsc" #-}

--------------------------------------------------------------------------------
-- Global variables                                                           --
--------------------------------------------------------------------------------

foreign import ccall "&R_Interactive" isRInteractive :: Ptr CInt

-- | Global nil value. Constant throughout the lifetime of the R instance.
foreign import ccall "&R_NilValue" nilValue  :: Ptr (SEXP G R.Nil)

-- | Unbound marker. Constant throughout the lifetime of the R instance.
foreign import ccall "&R_UnboundValue" unboundValue :: Ptr (SEXP G R.Symbol)

-- | Missing argument marker. Constant throughout the lifetime of the R instance.
foreign import ccall "&R_MissingArg" missingArg :: Ptr (SEXP G R.Symbol)

-- | The base environment.
foreign import ccall "&R_BaseEnv" baseEnv :: Ptr (SEXP G R.Env)

-- | The empty environment.
foreign import ccall "&R_EmptyEnv" emptyEnv :: Ptr (SEXP G R.Env)

-- | Global environment.
foreign import ccall "&R_GlobalEnv" globalEnv :: Ptr (SEXP G R.Env)

-- | Signal handler switch
foreign import ccall "&R_SignalHandlers" signalHandlers :: Ptr CInt

-- | Flag that shows if computation should be interrupted.
foreign import ccall "&R_interrupts_pending" interruptsPending :: Ptr CInt

----------------------------------------------------------------------------------
-- Structure header                                                             --
----------------------------------------------------------------------------------

-- | Info header for the SEXP data structure.
data SEXPInfo = SEXPInfo
      { SEXPInfo -> SEXPTYPE
infoType  :: SEXPTYPE    -- ^ Type of the SEXP.
      , SEXPInfo -> Bool
infoObj   :: Bool        -- ^ Is this an object with a class attribute.
      , SEXPInfo -> Int
infoNamed :: Int         -- ^ Control copying information.
      , SEXPInfo -> Int
infoGp    :: Int         -- ^ General purpose data.
      , SEXPInfo -> Bool
infoMark  :: Bool        -- ^ Mark object as 'in use' in GC.
      , SEXPInfo -> Bool
infoDebug :: Bool        -- ^ Debug marker.
      , SEXPInfo -> Bool
infoTrace :: Bool        -- ^ Trace marker.
      , SEXPInfo -> Bool
infoSpare :: Bool        -- ^ Alignment (not in use).
      , SEXPInfo -> Int
infoGcGen :: Int         -- ^ GC Generation.
      , SEXPInfo -> Int
infoGcCls :: Int         -- ^ GC Class of node.
      } deriving ( Int -> SEXPInfo -> ShowS
[SEXPInfo] -> ShowS
SEXPInfo -> String
(Int -> SEXPInfo -> ShowS)
-> (SEXPInfo -> String) -> ([SEXPInfo] -> ShowS) -> Show SEXPInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SEXPInfo] -> ShowS
$cshowList :: [SEXPInfo] -> ShowS
show :: SEXPInfo -> String
$cshow :: SEXPInfo -> String
showsPrec :: Int -> SEXPInfo -> ShowS
$cshowsPrec :: Int -> SEXPInfo -> ShowS
Show )

-- | Extract the header from the given 'SEXP'.
peekInfo :: SEXP s a -> IO SEXPInfo
peekInfo :: SEXP s a -> IO SEXPInfo
peekInfo ts :: SEXP s a
ts =
    SEXPTYPE
-> Bool
-> Int
-> Int
-> Bool
-> Bool
-> Bool
-> Bool
-> Int
-> Int
-> SEXPInfo
SEXPInfo
      (SEXPTYPE
 -> Bool
 -> Int
 -> Int
 -> Bool
 -> Bool
 -> Bool
 -> Bool
 -> Int
 -> Int
 -> SEXPInfo)
-> IO SEXPTYPE
-> IO
     (Bool
      -> Int
      -> Int
      -> Bool
      -> Bool
      -> Bool
      -> Bool
      -> Int
      -> Int
      -> SEXPInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> SEXPTYPE
forall a. Enum a => Int -> a
toEnum(Int -> SEXPTYPE) -> (CInt -> Int) -> CInt -> SEXPTYPE
forall b c a. (b -> c) -> (a -> b) -> a -> c
.CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> SEXPTYPE) -> IO CInt -> IO SEXPTYPE
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cTYPEOF SEXP0
s)
      IO
  (Bool
   -> Int
   -> Int
   -> Bool
   -> Bool
   -> Bool
   -> Bool
   -> Int
   -> Int
   -> SEXPInfo)
-> IO Bool
-> IO
     (Int
      -> Int -> Bool -> Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/=0)              (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cOBJECT SEXP0
s)
      IO
  (Int
   -> Int -> Bool -> Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
-> IO Int
-> IO
     (Int -> Bool -> Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral       (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cNAMED SEXP0
s)
      IO (Int -> Bool -> Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
-> IO Int
-> IO (Bool -> Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral       (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cLEVELS SEXP0
s)
      IO (Bool -> Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
-> IO Bool -> IO (Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/=0)              (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cMARK SEXP0
s)
      IO (Bool -> Bool -> Bool -> Int -> Int -> SEXPInfo)
-> IO Bool -> IO (Bool -> Bool -> Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/=0)              (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cRDEBUG SEXP0
s)
      IO (Bool -> Bool -> Int -> Int -> SEXPInfo)
-> IO Bool -> IO (Bool -> Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/=0)              (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cRTRACE SEXP0
s)
      IO (Bool -> Int -> Int -> SEXPInfo)
-> IO Bool -> IO (Int -> Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/=0)              (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cRSTEP SEXP0
s)
      IO (Int -> Int -> SEXPInfo) -> IO Int -> IO (Int -> SEXPInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral       (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cGCGEN SEXP0
s)
      IO (Int -> SEXPInfo) -> IO Int -> IO SEXPInfo
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral       (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO CInt
cGCCLS SEXP0
s)
  where
    s :: SEXP0
s = SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
ts

-- These accessors are necessary because hsc2hs cannot determine the offset of
-- C struct bit-fields. https://ghc.haskell.org/trac/ghc/ticket/12149
foreign import capi unsafe "OBJECT" cOBJECT :: SEXP0 -> IO CInt
foreign import capi unsafe "NAMED" cNAMED :: SEXP0 -> IO CInt
foreign import capi unsafe "LEVELS" cLEVELS :: SEXP0 -> IO CInt
foreign import capi unsafe "MARK" cMARK :: SEXP0 -> IO CInt
foreign import capi unsafe "RDEBUG" cRDEBUG :: SEXP0 -> IO CInt
foreign import capi unsafe "RTRACE" cRTRACE :: SEXP0 -> IO CInt
foreign import capi unsafe "RSTEP" cRSTEP :: SEXP0 -> IO CInt
foreign import capi unsafe "missing_r.h GCGEN" cGCGEN :: SEXP0 -> IO CInt
foreign import capi unsafe "missing_r.h GCCLS" cGCCLS :: SEXP0 -> IO CInt

-- | Write a new header.
pokeInfo :: SEXP s a -> SEXPInfo -> IO ()
pokeInfo :: SEXP s a -> SEXPInfo -> IO ()
pokeInfo (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp -> SEXP0
s) i :: SEXPInfo
i = do
    SEXP0 -> CInt -> IO ()
cSET_TYPEOF SEXP0
s (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral(Int -> CInt) -> (SEXPTYPE -> Int) -> SEXPTYPE -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
.SEXPTYPE -> Int
forall a. Enum a => a -> Int
fromEnum (SEXPTYPE -> CInt) -> SEXPTYPE -> CInt
forall a b. (a -> b) -> a -> b
$ SEXPInfo -> SEXPTYPE
infoType SEXPInfo
i)
    SEXP0 -> CInt -> IO ()
cSET_OBJECT SEXP0
s (if SEXPInfo -> Bool
infoObj  SEXPInfo
i then 1 else 0)
    SEXP0 -> CInt -> IO ()
cSET_NAMED SEXP0
s (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> Int -> CInt
forall a b. (a -> b) -> a -> b
$ SEXPInfo -> Int
infoNamed SEXPInfo
i)
    SEXP0 -> CInt -> IO ()
cSETLEVELS SEXP0
s (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> Int -> CInt
forall a b. (a -> b) -> a -> b
$ SEXPInfo -> Int
infoGp SEXPInfo
i)
    SEXP0 -> CInt -> IO ()
cSET_MARK SEXP0
s (if SEXPInfo -> Bool
infoMark SEXPInfo
i  then 1 else 0)
    SEXP0 -> CInt -> IO ()
cSET_RDEBUG SEXP0
s (if SEXPInfo -> Bool
infoDebug SEXPInfo
i then 1 else 0)
    SEXP0 -> CInt -> IO ()
cSET_RTRACE SEXP0
s (if SEXPInfo -> Bool
infoTrace SEXPInfo
i then 1 else 0)
    SEXP0 -> CInt -> IO ()
cSET_RSTEP SEXP0
s (if SEXPInfo -> Bool
infoSpare SEXPInfo
i then 1 else 0)
    SEXP0 -> CInt -> IO ()
cSET_GCGEN SEXP0
s (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> Int -> CInt
forall a b. (a -> b) -> a -> b
$ SEXPInfo -> Int
infoGcGen SEXPInfo
i)
    SEXP0 -> CInt -> IO ()
cSET_GCCLS SEXP0
s (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> Int -> CInt
forall a b. (a -> b) -> a -> b
$ SEXPInfo -> Int
infoGcCls SEXPInfo
i)

foreign import capi unsafe "SET_TYPEOF" cSET_TYPEOF :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SET_OBJECT" cSET_OBJECT :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SET_NAMED" cSET_NAMED :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SETLEVELS" cSETLEVELS :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SET_MARK" cSET_MARK :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SET_RDEBUG" cSET_RDEBUG :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SET_RTRACE" cSET_RTRACE :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "SET_RSTEP" cSET_RSTEP :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "missing_r.h SET_GCGEN" cSET_GCGEN :: SEXP0 -> CInt -> IO ()
foreign import capi unsafe "missing_r.h SET_GCCLS" cSET_GCCLS :: SEXP0 -> CInt -> IO ()

-- | Set the GC mark.
mark :: Bool -> SEXP s a -> IO ()
mark :: Bool -> SEXP s a -> IO ()
mark b :: Bool
b ts :: SEXP s a
ts = SEXP0 -> CInt -> IO ()
cSET_MARK (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
ts) (if Bool
b then 1 else 0)

named :: Int -> SEXP s a -> IO ()
named :: Int -> SEXP s a -> IO ()
named v :: Int
v ts :: SEXP s a
ts = SEXP0 -> CInt -> IO ()
cSET_NAMED (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
ts) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
v)

-------------------------------------------------------------------------------
-- Attribute header                                                          --
-------------------------------------------------------------------------------

-- | Check if object is an S4 object.
--
-- This is a function call so it will be more precise than using 'typeOf'.
isS4 :: SEXP s ty -> Bool
isS4 :: SEXP s ty -> Bool
isS4 s :: SEXP s ty
s = (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>0) (Int -> Bool) -> Int -> Bool
forall a b. (a -> b) -> a -> b
$ SEXP0 -> Int
cisS4 (SEXP s ty -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s ty
s)

-- | Get the attribute list from the given object.
getAttributes :: SEXP s a -> IO (SEXP s b)
getAttributes :: SEXP s a -> IO (SEXP s b)
getAttributes s :: SEXP s a
s = SEXP0 -> SEXP s b
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
sexp (SEXP0 -> SEXP s b) -> IO SEXP0 -> IO (SEXP s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SEXP0 -> IO SEXP0
cAttrib (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s)

-- | Get attribute with the given name.
getAttribute :: SEXP s  a -- ^ Value
             -> SEXP s2 b -- ^ Attribute name
             -> SEXP s  c
getAttribute :: SEXP s a -> SEXP s2 b -> SEXP s c
getAttribute a :: SEXP s a
a b :: SEXP s2 b
b = SEXP0 -> SEXP s c
forall s (a :: SEXPTYPE). SEXP0 -> SEXP s a
sexp (SEXP0 -> SEXP s c) -> SEXP0 -> SEXP s c
forall a b. (a -> b) -> a -> b
$ SEXP0 -> SEXP0 -> SEXP0
cgetAttrib (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
a) (SEXP s2 b -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s2 b
b)


-- | Set the attribute list.
setAttributes :: SEXP s a -> SEXP s b -> IO ()
setAttributes :: SEXP s a -> SEXP s b -> IO ()
setAttributes s :: SEXP s a
s v :: SEXP s b
v = SEXP0 -> SEXP0 -> IO ()
csetAttrib (SEXP s a -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s a
s) (SEXP0 -> SEXP0
forall a b. Ptr a -> Ptr b
castPtr (SEXP0 -> SEXP0) -> SEXP0 -> SEXP0
forall a b. (a -> b) -> a -> b
$ SEXP s b -> SEXP0
forall s (a :: SEXPTYPE). SEXP s a -> SEXP0
unsexp SEXP s b
v)

foreign import capi unsafe "Rinternals.h ATTRIB" cAttrib :: SEXP0 -> IO SEXP0
foreign import capi unsafe "Rinternals.h SET_ATTRIB" csetAttrib :: SEXP0 -> SEXP0 -> IO ()
foreign import capi unsafe "Rinternals.h Rf_getAttrib" cgetAttrib :: SEXP0 -> SEXP0 -> SEXP0
foreign import capi unsafe "Rinternals.h Rf_isS4" cisS4 :: SEXP0 -> Int