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