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