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 -----------------------------------------------------------------------------
7 -- Module : Foreign.C.Types
8 -- Copyright : (c) The FFI task force 2001
9 -- License : BSD-style (see the file libraries/base/LICENSE)
11 -- Maintainer : ffi@haskell.org
12 -- Stability : provisional
13 -- Portability : portable
15 -- Mapping of C types to corresponding Haskell types.
17 -----------------------------------------------------------------------------
19 module Foreign.C.Types
20 ( -- * Representations of C 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
32 , CShort, CUShort, CInt, CUInt
34 , CPtrdiff, CSize, CWchar, CSigAtomic
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'.
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, CLDouble
55 -- Exported non-abstractly in nhc98 to fix an interface file problem.
56 CChar(..), CSChar(..), CUChar(..)
57 , CShort(..), CUShort(..), CInt(..), CUInt(..)
58 , CLong(..), CULong(..)
59 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
60 , CLLong(..), CULLong(..)
61 , CClock(..), CTime(..)
62 , CFloat(..), CDouble(..), CLDouble(..)
66 -- Instances of: Eq and Storable
67 , CFile, CFpos, CJmpBuf
72 import Foreign.Storable
73 import Data.Bits ( Bits(..) )
74 import Data.Int ( Int8, Int16, Int32, Int64 )
75 import Data.Word ( Word8, Word16, Word32, Word64 )
76 import {-# SOURCE #-} Data.Typeable (Typeable(typeOf), TyCon, mkTyCon, mkTyConApp)
78 #ifdef __GLASGOW_HASKELL__
87 import Control.Monad ( liftM )
91 import Hugs.Ptr ( castPtr )
94 #include "HsBaseConfig.h"
97 -- | Haskell type representing the C @char@ type.
98 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
99 -- | Haskell type representing the C @signed char@ type.
100 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
101 -- | Haskell type representing the C @unsigned char@ type.
102 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
104 -- | Haskell type representing the C @short@ type.
105 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
106 -- | Haskell type representing the C @unsigned short@ type.
107 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
109 -- | Haskell type representing the C @int@ type.
110 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
111 -- | Haskell type representing the C @unsigned int@ type.
112 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
114 -- | Haskell type representing the C @long@ type.
115 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
116 -- | Haskell type representing the C @unsigned long@ type.
117 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
119 -- | Haskell type representing the C @long long@ type.
120 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
121 -- | Haskell type representing the C @unsigned long long@ type.
122 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
125 "fromIntegral/a->CChar" fromIntegral = \x -> CChar (fromIntegral x)
126 "fromIntegral/a->CSChar" fromIntegral = \x -> CSChar (fromIntegral x)
127 "fromIntegral/a->CUChar" fromIntegral = \x -> CUChar (fromIntegral x)
128 "fromIntegral/a->CShort" fromIntegral = \x -> CShort (fromIntegral x)
129 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
130 "fromIntegral/a->CInt" fromIntegral = \x -> CInt (fromIntegral x)
131 "fromIntegral/a->CUInt" fromIntegral = \x -> CUInt (fromIntegral x)
132 "fromIntegral/a->CLong" fromIntegral = \x -> CLong (fromIntegral x)
133 "fromIntegral/a->CULong" fromIntegral = \x -> CULong (fromIntegral x)
134 "fromIntegral/a->CLLong" fromIntegral = \x -> CLLong (fromIntegral x)
135 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
137 "fromIntegral/CChar->a" fromIntegral = \(CChar x) -> fromIntegral x
138 "fromIntegral/CSChar->a" fromIntegral = \(CSChar x) -> fromIntegral x
139 "fromIntegral/CUChar->a" fromIntegral = \(CUChar x) -> fromIntegral x
140 "fromIntegral/CShort->a" fromIntegral = \(CShort x) -> fromIntegral x
141 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
142 "fromIntegral/CInt->a" fromIntegral = \(CInt x) -> fromIntegral x
143 "fromIntegral/CUInt->a" fromIntegral = \(CUInt x) -> fromIntegral x
144 "fromIntegral/CLong->a" fromIntegral = \(CLong x) -> fromIntegral x
145 "fromIntegral/CULong->a" fromIntegral = \(CULong x) -> fromIntegral x
146 "fromIntegral/CLLong->a" fromIntegral = \(CLLong x) -> fromIntegral x
147 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
150 -- | Haskell type representing the C @float@ type.
151 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
152 -- | Haskell type representing the C @double@ type.
153 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
154 -- HACK: Currently no long double in the FFI, so we simply re-use double
155 -- | Haskell type representing the C @long double@ type.
156 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
159 "realToFrac/a->CFloat" realToFrac = \x -> CFloat (realToFrac x)
160 "realToFrac/a->CDouble" realToFrac = \x -> CDouble (realToFrac x)
161 "realToFrac/a->CLDouble" realToFrac = \x -> CLDouble (realToFrac x)
163 "realToFrac/CFloat->a" realToFrac = \(CFloat x) -> realToFrac x
164 "realToFrac/CDouble->a" realToFrac = \(CDouble x) -> realToFrac x
165 "realToFrac/CLDouble->a" realToFrac = \(CLDouble x) -> realToFrac x
168 -- | Haskell type representing the C @ptrdiff_t@ type.
169 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
170 -- | Haskell type representing the C @size_t@ type.
171 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
172 -- | Haskell type representing the C @wchar_t@ type.
173 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
174 -- | Haskell type representing the C @sig_atomic_t@ type.
175 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
178 "fromIntegral/a->CPtrdiff" fromIntegral = \x -> CPtrdiff (fromIntegral x)
179 "fromIntegral/a->CSize" fromIntegral = \x -> CSize (fromIntegral x)
180 "fromIntegral/a->CWchar" fromIntegral = \x -> CWchar (fromIntegral x)
181 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
183 "fromIntegral/CPtrdiff->a" fromIntegral = \(CPtrdiff x) -> fromIntegral x
184 "fromIntegral/CSize->a" fromIntegral = \(CSize x) -> fromIntegral x
185 "fromIntegral/CWchar->a" fromIntegral = \(CWchar x) -> fromIntegral x
186 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
189 -- | Haskell type representing the C @clock_t@ type.
190 ARITHMETIC_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
191 -- | Haskell type representing the C @time_t@ type.
193 -- To convert to a @Data.Time.UTCTime@, use the following formula:
195 -- > posixSecondsToUTCTime (realToFrac :: POSIXTime)
197 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
199 -- FIXME: Implement and provide instances for Eq and Storable
200 -- | Haskell type representing the C @FILE@ type.
202 -- | Haskell type representing the C @fpos_t@ type.
204 -- | Haskell type representing the C @jmp_buf@ type.
205 data CJmpBuf = CJmpBuf
207 INTEGRAL_TYPE(CIntPtr,tyConCIntPtr,"CIntPtr",HTYPE_INTPTR_T)
208 INTEGRAL_TYPE(CUIntPtr,tyConCUIntPtr,"CUIntPtr",HTYPE_UINTPTR_T)
209 INTEGRAL_TYPE(CIntMax,tyConCIntMax,"CIntMax",HTYPE_INTMAX_T)
210 INTEGRAL_TYPE(CUIntMax,tyConCUIntMax,"CUIntMax",HTYPE_UINTMAX_T)
213 "fromIntegral/a->CIntPtr" fromIntegral = \x -> CIntPtr (fromIntegral x)
214 "fromIntegral/a->CUIntPtr" fromIntegral = \x -> CUIntPtr (fromIntegral x)
215 "fromIntegral/a->CIntMax" fromIntegral = \x -> CIntMax (fromIntegral x)
216 "fromIntegral/a->CUIntMax" fromIntegral = \x -> CUIntMax (fromIntegral x)
219 -- C99 types which are still missing include:
220 -- wint_t, wctrans_t, wctype_t
224 These types are needed to accurately represent C function prototypes,
225 in order to access C library interfaces in Haskell. The Haskell system
226 is not required to represent those types exactly as C does, but the
227 following guarantees are provided concerning a Haskell type @CT@
228 representing a C type @t@:
230 * If a C function prototype has @t@ as an argument or result type, the
231 use of @CT@ in the corresponding position in a foreign declaration
232 permits the Haskell program to access the full range of values encoded
233 by the C type; and conversely, any Haskell value for @CT@ has a valid
236 * @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
239 * @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
240 constraint enforced by the C implementation for @t@.
242 * The members 'peek' and 'poke' of the 'Storable' class map all values
243 of @CT@ to the corresponding value of @t@ and vice versa.
245 * When an instance of 'Prelude.Bounded' is defined for @CT@, the values
246 of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
249 * When an instance of 'Prelude.Eq' or 'Prelude.Ord' is defined for @CT@,
250 the predicates defined by the type class implement the same relation
251 as the corresponding predicate in C on @t@.
253 * When an instance of 'Prelude.Num', 'Prelude.Read', 'Prelude.Integral',
254 'Prelude.Fractional', 'Prelude.Floating', 'Prelude.RealFrac', or
255 'Prelude.RealFloat' is defined for @CT@, the arithmetic operations
256 defined by the type class implement the same function as the
257 corresponding arithmetic operations (if available) in C on @t@.
259 * When an instance of 'Bits' is defined for @CT@, the bitwise operation
260 defined by the type class implement the same function as the
261 corresponding bitwise operation in C on @t@.
268 ( CChar(..), CSChar(..), CUChar(..)
269 , CShort(..), CUShort(..), CInt(..), CUInt(..)
270 , CLong(..), CULong(..), CLLong(..), CULLong(..)
271 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
272 , CClock(..), CTime(..)
273 , CFloat(..), CDouble(..), CLDouble(..)
274 , CFile, CFpos, CJmpBuf
278 import NHC.SizedTypes
280 #define INSTANCE_BITS(T) \
281 instance Bits T where { \
282 (T x) .&. (T y) = T (x .&. y) ; \
283 (T x) .|. (T y) = T (x .|. y) ; \
284 (T x) `xor` (T y) = T (x `xor` y) ; \
285 complement (T x) = T (complement x) ; \
286 shift (T x) n = T (shift x n) ; \
287 rotate (T x) n = T (rotate x n) ; \
288 bit n = T (bit n) ; \
289 setBit (T x) n = T (setBit x n) ; \
290 clearBit (T x) n = T (clearBit x n) ; \
291 complementBit (T x) n = T (complementBit x n) ; \
292 testBit (T x) n = testBit x n ; \
293 bitSize (T x) = bitSize x ; \
294 isSigned (T x) = isSigned x }
297 INSTANCE_BITS(CSChar)
298 INSTANCE_BITS(CUChar)
299 INSTANCE_BITS(CShort)
300 INSTANCE_BITS(CUShort)
304 INSTANCE_BITS(CULong)
305 INSTANCE_BITS(CLLong)
306 INSTANCE_BITS(CULLong)
307 INSTANCE_BITS(CPtrdiff)
308 INSTANCE_BITS(CWchar)
309 INSTANCE_BITS(CSigAtomic)