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