For GHC, implement the Typeable.hs macros using standalone deriving
[ghc-base.git] / Foreign / Ptr.hs
1 {-# LANGUAGE CPP
2            , NoImplicitPrelude
3            , ForeignFunctionInterface
4            , MagicHash
5            , GeneralizedNewtypeDeriving
6   #-}
7 #ifdef __GLASGOW_HASKELL__
8 {-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
9 #endif
10
11 -----------------------------------------------------------------------------
12 -- |
13 -- Module      :  Foreign.Ptr
14 -- Copyright   :  (c) The FFI task force 2001
15 -- License     :  BSD-style (see the file libraries/base/LICENSE)
16 -- 
17 -- Maintainer  :  ffi@haskell.org
18 -- Stability   :  provisional
19 -- Portability :  portable
20 --
21 -- This module provides typed pointers to foreign data.  It is part
22 -- of the Foreign Function Interface (FFI) and will normally be
23 -- imported via the "Foreign" module.
24 --
25 -----------------------------------------------------------------------------
26
27 module Foreign.Ptr (
28
29     -- * Data pointers
30
31     Ptr,      -- data Ptr a
32     nullPtr,      -- :: Ptr a
33     castPtr,      -- :: Ptr a -> Ptr b
34     plusPtr,      -- :: Ptr a -> Int -> Ptr b
35     alignPtr,     -- :: Ptr a -> Int -> Ptr a
36     minusPtr,     -- :: Ptr a -> Ptr b -> Int
37
38     -- * Function pointers
39
40     FunPtr,      -- data FunPtr a
41     nullFunPtr,      -- :: FunPtr a
42     castFunPtr,      -- :: FunPtr a -> FunPtr b
43     castFunPtrToPtr, -- :: FunPtr a -> Ptr b
44     castPtrToFunPtr, -- :: Ptr a -> FunPtr b
45
46     freeHaskellFunPtr, -- :: FunPtr a -> IO ()
47     -- Free the function pointer created by foreign export dynamic.
48
49 #ifndef __NHC__
50     -- * Integral types with lossless conversion to and from pointers
51     IntPtr,
52     ptrToIntPtr,
53     intPtrToPtr,
54     WordPtr,
55     ptrToWordPtr,
56     wordPtrToPtr
57 #endif
58  ) where
59
60 #ifdef __GLASGOW_HASKELL__
61 import GHC.Ptr
62 import GHC.Base
63 import GHC.Num
64 import GHC.Read
65 import GHC.Real
66 import GHC.Show
67 import GHC.Enum
68 import GHC.Word         ( Word(..) )
69
70 -- import Data.Int
71 import Data.Word
72 #else
73 import Control.Monad    ( liftM )
74 import Foreign.C.Types
75 #endif
76
77 import Data.Bits
78 import Data.Typeable
79 import Foreign.Storable ( Storable(..) )
80
81 #ifdef __NHC__
82 import NHC.FFI
83   ( Ptr
84   , nullPtr
85   , castPtr
86   , plusPtr
87   , alignPtr
88   , minusPtr
89   , FunPtr
90   , nullFunPtr
91   , castFunPtr
92   , castFunPtrToPtr
93   , castPtrToFunPtr
94   , freeHaskellFunPtr
95   )
96 #endif
97
98 #ifdef __HUGS__
99 import Hugs.Ptr
100 #endif
101
102 #ifdef __GLASGOW_HASKELL__
103 -- | Release the storage associated with the given 'FunPtr', which
104 -- must have been obtained from a wrapper stub.  This should be called
105 -- whenever the return value from a foreign import wrapper function is
106 -- no longer required; otherwise, the storage it uses will leak.
107 foreign import ccall unsafe "freeHaskellFunctionPtr"
108     freeHaskellFunPtr :: FunPtr a -> IO ()
109 #endif
110
111 #ifndef __NHC__
112 # include "HsBaseConfig.h"
113 # include "CTypes.h"
114
115 # ifdef __GLASGOW_HASKELL__
116 -- | An unsigned integral type that can be losslessly converted to and from
117 -- @Ptr@. This type is also compatible with the C99 type @uintptr_t@, and
118 -- can be marshalled to and from that type safely.
119 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
120         -- Word and Int are guaranteed pointer-sized in GHC
121
122 -- | A signed integral type that can be losslessly converted to and from
123 -- @Ptr@.  This type is also compatible with the C99 type @intptr_t@, and
124 -- can be marshalled to and from that type safely.
125 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
126         -- Word and Int are guaranteed pointer-sized in GHC
127
128 -- | casts a @Ptr@ to a @WordPtr@
129 ptrToWordPtr :: Ptr a -> WordPtr
130 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
131
132 -- | casts a @WordPtr@ to a @Ptr@
133 wordPtrToPtr :: WordPtr -> Ptr a
134 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
135
136 -- | casts a @Ptr@ to an @IntPtr@
137 ptrToIntPtr :: Ptr a -> IntPtr
138 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
139
140 -- | casts an @IntPtr@ to a @Ptr@
141 intPtrToPtr :: IntPtr -> Ptr a
142 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
143
144 # else /* !__GLASGOW_HASKELL__ */
145
146 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
147 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
148
149 {-# CFILES cbits/PrelIOUtils.c #-}
150
151 foreign import ccall unsafe "__hscore_to_uintptr"
152     ptrToWordPtr :: Ptr a -> WordPtr
153
154 foreign import ccall unsafe "__hscore_from_uintptr"
155     wordPtrToPtr :: WordPtr -> Ptr a
156
157 foreign import ccall unsafe "__hscore_to_intptr"
158     ptrToIntPtr :: Ptr a -> IntPtr
159
160 foreign import ccall unsafe "__hscore_from_intptr"
161     intPtrToPtr :: IntPtr -> Ptr a
162
163 # endif /* !__GLASGOW_HASKELL__ */
164 #endif /* !__NHC_ */