Avoid using deprecated flags
[ghc-base.git] / Foreign / C / Types.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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 --
190 -- To convert to a @Data.Time.UTCTime@, use the following formula:
191 --
192 -- >  posixSecondsToUTCTime (realToFrac :: POSIXTime)
193 --
194 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
195
196 -- FIXME: Implement and provide instances for Eq and Storable
197 -- | Haskell type representing the C @FILE@ type.
198 data CFile = CFile
199 -- | Haskell type representing the C @fpos_t@ type.
200 data CFpos = CFpos
201 -- | Haskell type representing the C @jmp_buf@ type.
202 data CJmpBuf = CJmpBuf
203
204 INTEGRAL_TYPE(CIntPtr,tyConCIntPtr,"CIntPtr",HTYPE_INTPTR_T)
205 INTEGRAL_TYPE(CUIntPtr,tyConCUIntPtr,"CUIntPtr",HTYPE_UINTPTR_T)
206 INTEGRAL_TYPE(CIntMax,tyConCIntMax,"CIntMax",HTYPE_INTMAX_T)
207 INTEGRAL_TYPE(CUIntMax,tyConCUIntMax,"CUIntMax",HTYPE_UINTMAX_T)
208
209 {-# RULES
210 "fromIntegral/a->CIntPtr"  fromIntegral = \x -> CIntPtr  (fromIntegral x)
211 "fromIntegral/a->CUIntPtr" fromIntegral = \x -> CUIntPtr (fromIntegral x)
212 "fromIntegral/a->CIntMax"  fromIntegral = \x -> CIntMax  (fromIntegral x)
213 "fromIntegral/a->CUIntMax" fromIntegral = \x -> CUIntMax (fromIntegral x)
214  #-}
215
216 -- C99 types which are still missing include:
217 -- wint_t, wctrans_t, wctype_t
218
219 {- $ctypes
220
221 These types are needed to accurately represent C function prototypes,
222 in order to access C library interfaces in Haskell.  The Haskell system
223 is not required to represent those types exactly as C does, but the
224 following guarantees are provided concerning a Haskell type @CT@
225 representing a C type @t@:
226
227 * If a C function prototype has @t@ as an argument or result type, the
228   use of @CT@ in the corresponding position in a foreign declaration
229   permits the Haskell program to access the full range of values encoded
230   by the C type; and conversely, any Haskell value for @CT@ has a valid
231   representation in C.
232
233 * @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
234   @sizeof (t)@ in C.
235
236 * @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
237   constraint enforced by the C implementation for @t@.
238
239 * The members 'peek' and 'poke' of the 'Storable' class map all values
240   of @CT@ to the corresponding value of @t@ and vice versa.
241
242 * When an instance of 'Prelude.Bounded' is defined for @CT@, the values
243   of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
244   and @t_MAX@ in C.
245
246 * When an instance of 'Prelude.Eq' or 'Prelude.Ord' is defined for @CT@,
247   the predicates defined by the type class implement the same relation
248   as the corresponding predicate in C on @t@.
249
250 * When an instance of 'Prelude.Num', 'Prelude.Read', 'Prelude.Integral',
251   'Prelude.Fractional', 'Prelude.Floating', 'Prelude.RealFrac', or
252   'Prelude.RealFloat' is defined for @CT@, the arithmetic operations
253   defined by the type class implement the same function as the
254   corresponding arithmetic operations (if available) in C on @t@.
255
256 * When an instance of 'Bits' is defined for @CT@, the bitwise operation
257   defined by the type class implement the same function as the
258   corresponding bitwise operation in C on @t@.
259
260 -}
261
262 #else   /* __NHC__ */
263
264 import NHC.FFI
265   ( CChar(..),    CSChar(..),  CUChar(..)
266   , CShort(..),   CUShort(..), CInt(..),   CUInt(..)
267   , CLong(..),    CULong(..),  CLLong(..), CULLong(..)
268   , CPtrdiff(..), CSize(..),   CWchar(..), CSigAtomic(..)
269   , CClock(..),   CTime(..)
270   , CFloat(..),   CDouble(..), CLDouble(..)
271   , CFile,        CFpos,       CJmpBuf
272   , Storable(..)
273   )
274 import Data.Bits
275 import NHC.SizedTypes
276
277 #define INSTANCE_BITS(T) \
278 instance Bits T where { \
279   (T x) .&.     (T y)   = T (x .&.   y) ; \
280   (T x) .|.     (T y)   = T (x .|.   y) ; \
281   (T x) `xor`   (T y)   = T (x `xor` y) ; \
282   complement    (T x)   = T (complement x) ; \
283   shift         (T x) n = T (shift x n) ; \
284   rotate        (T x) n = T (rotate x n) ; \
285   bit                 n = T (bit n) ; \
286   setBit        (T x) n = T (setBit x n) ; \
287   clearBit      (T x) n = T (clearBit x n) ; \
288   complementBit (T x) n = T (complementBit x n) ; \
289   testBit       (T x) n = testBit x n ; \
290   bitSize       (T x)   = bitSize x ; \
291   isSigned      (T x)   = isSigned x }
292
293 INSTANCE_BITS(CChar)
294 INSTANCE_BITS(CSChar)
295 INSTANCE_BITS(CUChar)
296 INSTANCE_BITS(CShort)
297 INSTANCE_BITS(CUShort)
298 INSTANCE_BITS(CInt)
299 INSTANCE_BITS(CUInt)
300 INSTANCE_BITS(CLong)
301 INSTANCE_BITS(CULong)
302 INSTANCE_BITS(CLLong)
303 INSTANCE_BITS(CULLong)
304 INSTANCE_BITS(CPtrdiff)
305 INSTANCE_BITS(CWchar)
306 INSTANCE_BITS(CSigAtomic)
307 INSTANCE_BITS(CSize)
308
309 #endif