4 , GeneralizedNewtypeDeriving
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
10 -----------------------------------------------------------------------------
12 -- Module : Foreign.C.Types
13 -- Copyright : (c) The FFI task force 2001
14 -- License : BSD-style (see the file libraries/base/LICENSE)
16 -- Maintainer : ffi@haskell.org
17 -- Stability : provisional
18 -- Portability : portable
20 -- Mapping of C types to corresponding Haskell types.
22 -----------------------------------------------------------------------------
24 module Foreign.C.Types
25 ( -- * Representations of C 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
37 , CShort, CUShort, CInt, CUInt
39 , CPtrdiff, CSize, CWchar, CSigAtomic
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'.
51 -- extracted from CTime, because we don't want this comment in
52 -- the Haskell 2010 report:
54 -- | To convert 'CTime' to 'Data.Time.UTCTime', use the following formula:
56 -- > posixSecondsToUTCTime (realToFrac :: POSIXTime)
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'.
67 -- GHC doesn't support CLDouble yet
68 #ifndef __GLASGOW_HASKELL__
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(..)
84 -- Instances of: Eq and Storable
85 , CFile, CFpos, CJmpBuf
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)
96 #ifdef __GLASGOW_HASKELL__
105 import Control.Monad ( liftM )
109 import Hugs.Ptr ( castPtr )
112 #include "HsBaseConfig.h"
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)
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)
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)
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)
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)
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)
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
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)
180 "realToFrac/a->CFloat" realToFrac = \x -> CFloat (realToFrac x)
181 "realToFrac/a->CDouble" realToFrac = \x -> CDouble (realToFrac x)
183 "realToFrac/CFloat->a" realToFrac = \(CFloat x) -> realToFrac x
184 "realToFrac/CDouble->a" realToFrac = \(CDouble x) -> realToFrac x
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
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)
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)
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
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.
216 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
218 -- FIXME: Implement and provide instances for Eq and Storable
219 -- | Haskell type representing the C @FILE@ type.
221 -- | Haskell type representing the C @fpos_t@ type.
223 -- | Haskell type representing the C @jmp_buf@ type.
224 data CJmpBuf = CJmpBuf
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)
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)
238 -- C99 types which are still missing include:
239 -- wint_t, wctrans_t, wctype_t
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@:
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
255 * @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
258 * @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
259 constraint enforced by the C implementation for @t@.
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.
264 * When an instance of 'Prelude.Bounded' is defined for @CT@, the values
265 of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
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@.
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@.
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@.
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
298 import NHC.SizedTypes
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 }
317 INSTANCE_BITS(CSChar)
318 INSTANCE_BITS(CUChar)
319 INSTANCE_BITS(CShort)
320 INSTANCE_BITS(CUShort)
324 INSTANCE_BITS(CULong)
325 INSTANCE_BITS(CLLong)
326 INSTANCE_BITS(CULLong)
327 INSTANCE_BITS(CPtrdiff)
328 INSTANCE_BITS(CWchar)
329 INSTANCE_BITS(CSigAtomic)
331 INSTANCE_BITS(CIntPtr)
332 INSTANCE_BITS(CUIntPtr)
333 INSTANCE_BITS(CIntMax)
334 INSTANCE_BITS(CUIntMax)