Remove Control.Parallel*, now in package parallel
[packages/random.git] / Foreign / C / Types.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Foreign.C.Types
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 -- Mapping of C types to corresponding Haskell types.
13 --
14 -----------------------------------------------------------------------------
15
16 module Foreign.C.Types
17 ( -- * Representations of C types
18 #ifndef __NHC__
19 -- $ctypes
20
21 -- ** Integral types
22 -- | These types are are represented as @newtype@s of
23 -- types in "Data.Int" and "Data.Word", and are instances of
24 -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
25 -- 'Prelude.Show', 'Prelude.Enum', 'Typeable', 'Storable',
26 -- 'Prelude.Bounded', 'Prelude.Real', 'Prelude.Integral' and
27 -- 'Bits'.
28 CChar, CSChar, CUChar
29 , CShort, CUShort, CInt, CUInt
30 , CLong, CULong
31 , CPtrdiff, CSize, CWchar, CSigAtomic
32 , CLLong, CULLong
33 , CIntPtr, CUIntPtr
34 , CIntMax, CUIntMax
35
36 -- ** Numeric types
37 -- | These types are are represented as @newtype@s of basic
38 -- foreign types, and are instances of
39 -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
40 -- 'Prelude.Show', 'Prelude.Enum', 'Typeable' and 'Storable'.
41 , CClock, CTime
42
43 -- ** Floating types
44 -- | These types are are represented as @newtype@s of
45 -- 'Prelude.Float' and 'Prelude.Double', and are instances of
46 -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
47 -- 'Prelude.Show', 'Prelude.Enum', 'Typeable', 'Storable',
48 -- 'Prelude.Real', 'Prelude.Fractional', 'Prelude.Floating',
49 -- 'Prelude.RealFrac' and 'Prelude.RealFloat'.
50 , CFloat, CDouble, CLDouble
51 #else
52 -- Exported non-abstractly in nhc98 to fix an interface file problem.
53 CChar(..), CSChar(..), CUChar(..)
54 , CShort(..), CUShort(..), CInt(..), CUInt(..)
55 , CLong(..), CULong(..)
56 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
57 , CLLong(..), CULLong(..)
58 , CClock(..), CTime(..)
59 , CFloat(..), CDouble(..), CLDouble(..)
60 #endif
61 -- ** Other types
62
63 -- Instances of: Eq and Storable
64 , CFile, CFpos, CJmpBuf
65 ) where
66
67 #ifndef __NHC__
68
69 import {-# SOURCE #-} Foreign.Storable
70 import Data.Bits ( Bits(..) )
71 import Data.Int ( Int8, Int16, Int32, Int64 )
72 import Data.Word ( Word8, Word16, Word32, Word64 )
73 import {-# SOURCE #-} Data.Typeable
74
75 #ifdef __GLASGOW_HASKELL__
76 import GHC.Base
77 import GHC.Float
78 import GHC.Enum
79 import GHC.Real
80 import GHC.Show
81 import GHC.Read
82 import GHC.Num
83 #else
84 import Control.Monad ( liftM )
85 #endif
86
87 #ifdef __HUGS__
88 import Hugs.Ptr ( castPtr )
89 #endif
90
91 #include "HsBaseConfig.h"
92 #include "CTypes.h"
93
94 -- | Haskell type representing the C @char@ type.
95 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
96 -- | Haskell type representing the C @signed char@ type.
97 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
98 -- | Haskell type representing the C @unsigned char@ type.
99 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
100
101 -- | Haskell type representing the C @short@ type.
102 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
103 -- | Haskell type representing the C @unsigned short@ type.
104 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
105
106 -- | Haskell type representing the C @int@ type.
107 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
108 -- | Haskell type representing the C @unsigned int@ type.
109 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
110
111 -- | Haskell type representing the C @long@ type.
112 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
113 -- | Haskell type representing the C @unsigned long@ type.
114 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
115
116 -- | Haskell type representing the C @long long@ type.
117 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
118 -- | Haskell type representing the C @unsigned long long@ type.
119 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
120
121 {-# RULES
122 "fromIntegral/a->CChar" fromIntegral = \x -> CChar (fromIntegral x)
123 "fromIntegral/a->CSChar" fromIntegral = \x -> CSChar (fromIntegral x)
124 "fromIntegral/a->CUChar" fromIntegral = \x -> CUChar (fromIntegral x)
125 "fromIntegral/a->CShort" fromIntegral = \x -> CShort (fromIntegral x)
126 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
127 "fromIntegral/a->CInt" fromIntegral = \x -> CInt (fromIntegral x)
128 "fromIntegral/a->CUInt" fromIntegral = \x -> CUInt (fromIntegral x)
129 "fromIntegral/a->CLong" fromIntegral = \x -> CLong (fromIntegral x)
130 "fromIntegral/a->CULong" fromIntegral = \x -> CULong (fromIntegral x)
131 "fromIntegral/a->CLLong" fromIntegral = \x -> CLLong (fromIntegral x)
132 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
133
134 "fromIntegral/CChar->a" fromIntegral = \(CChar x) -> fromIntegral x
135 "fromIntegral/CSChar->a" fromIntegral = \(CSChar x) -> fromIntegral x
136 "fromIntegral/CUChar->a" fromIntegral = \(CUChar x) -> fromIntegral x
137 "fromIntegral/CShort->a" fromIntegral = \(CShort x) -> fromIntegral x
138 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
139 "fromIntegral/CInt->a" fromIntegral = \(CInt x) -> fromIntegral x
140 "fromIntegral/CUInt->a" fromIntegral = \(CUInt x) -> fromIntegral x
141 "fromIntegral/CLong->a" fromIntegral = \(CLong x) -> fromIntegral x
142 "fromIntegral/CULong->a" fromIntegral = \(CULong x) -> fromIntegral x
143 "fromIntegral/CLLong->a" fromIntegral = \(CLLong x) -> fromIntegral x
144 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
145 #-}
146
147 -- | Haskell type representing the C @float@ type.
148 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
149 -- | Haskell type representing the C @double@ type.
150 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
151 -- HACK: Currently no long double in the FFI, so we simply re-use double
152 -- | Haskell type representing the C @long double@ type.
153 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
154
155 {-# RULES
156 "realToFrac/a->CFloat" realToFrac = \x -> CFloat (realToFrac x)
157 "realToFrac/a->CDouble" realToFrac = \x -> CDouble (realToFrac x)
158 "realToFrac/a->CLDouble" realToFrac = \x -> CLDouble (realToFrac x)
159
160 "realToFrac/CFloat->a" realToFrac = \(CFloat x) -> realToFrac x
161 "realToFrac/CDouble->a" realToFrac = \(CDouble x) -> realToFrac x
162 "realToFrac/CLDouble->a" realToFrac = \(CLDouble x) -> realToFrac x
163 #-}
164
165 -- | Haskell type representing the C @ptrdiff_t@ type.
166 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
167 -- | Haskell type representing the C @size_t@ type.
168 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
169 -- | Haskell type representing the C @wchar_t@ type.
170 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
171 -- | Haskell type representing the C @sig_atomic_t@ type.
172 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
173
174 {-# RULES
175 "fromIntegral/a->CPtrdiff" fromIntegral = \x -> CPtrdiff (fromIntegral x)
176 "fromIntegral/a->CSize" fromIntegral = \x -> CSize (fromIntegral x)
177 "fromIntegral/a->CWchar" fromIntegral = \x -> CWchar (fromIntegral x)
178 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
179
180 "fromIntegral/CPtrdiff->a" fromIntegral = \(CPtrdiff x) -> fromIntegral x
181 "fromIntegral/CSize->a" fromIntegral = \(CSize x) -> fromIntegral x
182 "fromIntegral/CWchar->a" fromIntegral = \(CWchar x) -> fromIntegral x
183 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
184 #-}
185
186 -- | Haskell type representing the C @clock_t@ type.
187 ARITHMETIC_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
188 -- | Haskell type representing the C @time_t@ type.
189 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
190
191 -- FIXME: Implement and provide instances for Eq and Storable
192 -- | Haskell type representing the C @FILE@ type.
193 data CFile = CFile
194 -- | Haskell type representing the C @fpos_t@ type.
195 data CFpos = CFpos
196 -- | Haskell type representing the C @jmp_buf@ type.
197 data CJmpBuf = CJmpBuf
198
199 INTEGRAL_TYPE(CIntPtr,tyConCIntPtr,"CIntPtr",HTYPE_INTPTR_T)
200 INTEGRAL_TYPE(CUIntPtr,tyConCUIntPtr,"CUIntPtr",HTYPE_UINTPTR_T)
201 INTEGRAL_TYPE(CIntMax,tyConCIntMax,"CIntMax",HTYPE_INTMAX_T)
202 INTEGRAL_TYPE(CUIntMax,tyConCUIntMax,"CUIntMax",HTYPE_UINTMAX_T)
203
204 {-# RULES
205 "fromIntegral/a->CIntPtr" fromIntegral = \x -> CIntPtr (fromIntegral x)
206 "fromIntegral/a->CUIntPtr" fromIntegral = \x -> CUIntPtr (fromIntegral x)
207 "fromIntegral/a->CIntMax" fromIntegral = \x -> CIntMax (fromIntegral x)
208 "fromIntegral/a->CUIntMax" fromIntegral = \x -> CUIntMax (fromIntegral x)
209 #-}
210
211 -- C99 types which are still missing include:
212 -- wint_t, wctrans_t, wctype_t
213
214 {- $ctypes
215
216 These types are needed to accurately represent C function prototypes,
217 in order to access C library interfaces in Haskell. The Haskell system
218 is not required to represent those types exactly as C does, but the
219 following guarantees are provided concerning a Haskell type @CT@
220 representing a C type @t@:
221
222 * If a C function prototype has @t@ as an argument or result type, the
223 use of @CT@ in the corresponding position in a foreign declaration
224 permits the Haskell program to access the full range of values encoded
225 by the C type; and conversely, any Haskell value for @CT@ has a valid
226 representation in C.
227
228 * @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
229 @sizeof (t)@ in C.
230
231 * @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
232 constraint enforced by the C implementation for @t@.
233
234 * The members 'peek' and 'poke' of the 'Storable' class map all values
235 of @CT@ to the corresponding value of @t@ and vice versa.
236
237 * When an instance of 'Prelude.Bounded' is defined for @CT@, the values
238 of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
239 and @t_MAX@ in C.
240
241 * When an instance of 'Prelude.Eq' or 'Prelude.Ord' is defined for @CT@,
242 the predicates defined by the type class implement the same relation
243 as the corresponding predicate in C on @t@.
244
245 * When an instance of 'Prelude.Num', 'Prelude.Read', 'Prelude.Integral',
246 'Prelude.Fractional', 'Prelude.Floating', 'Prelude.RealFrac', or
247 'Prelude.RealFloat' is defined for @CT@, the arithmetic operations
248 defined by the type class implement the same function as the
249 corresponding arithmetic operations (if available) in C on @t@.
250
251 * When an instance of 'Bits' is defined for @CT@, the bitwise operation
252 defined by the type class implement the same function as the
253 corresponding bitwise operation in C on @t@.
254
255 -}
256
257 #else /* __NHC__ */
258
259 import NHC.FFI
260 ( CChar(..), CSChar(..), CUChar(..)
261 , CShort(..), CUShort(..), CInt(..), CUInt(..)
262 , CLong(..), CULong(..), CLLong(..), CULLong(..)
263 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
264 , CClock(..), CTime(..)
265 , CFloat(..), CDouble(..), CLDouble(..)
266 , CFile, CFpos, CJmpBuf
267 , Storable(..)
268 )
269 import Data.Bits
270 import NHC.SizedTypes
271
272 #define INSTANCE_BITS(T) \
273 instance Bits T where { \
274 (T x) .&. (T y) = T (x .&. y) ; \
275 (T x) .|. (T y) = T (x .|. y) ; \
276 (T x) `xor` (T y) = T (x `xor` y) ; \
277 complement (T x) = T (complement x) ; \
278 shift (T x) n = T (shift x n) ; \
279 rotate (T x) n = T (rotate x n) ; \
280 bit n = T (bit n) ; \
281 setBit (T x) n = T (setBit x n) ; \
282 clearBit (T x) n = T (clearBit x n) ; \
283 complementBit (T x) n = T (complementBit x n) ; \
284 testBit (T x) n = testBit x n ; \
285 bitSize (T x) = bitSize x ; \
286 isSigned (T x) = isSigned x }
287
288 INSTANCE_BITS(CChar)
289 INSTANCE_BITS(CSChar)
290 INSTANCE_BITS(CUChar)
291 INSTANCE_BITS(CShort)
292 INSTANCE_BITS(CUShort)
293 INSTANCE_BITS(CInt)
294 INSTANCE_BITS(CUInt)
295 INSTANCE_BITS(CLong)
296 INSTANCE_BITS(CULong)
297 INSTANCE_BITS(CLLong)
298 INSTANCE_BITS(CULLong)
299 INSTANCE_BITS(CPtrdiff)
300 INSTANCE_BITS(CWchar)
301 INSTANCE_BITS(CSigAtomic)
302 INSTANCE_BITS(CSize)
303
304 #endif