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