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