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