1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.C.Types
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Mapping of C types to corresponding Haskell types.
14 -----------------------------------------------------------------------------
16 module Foreign.C.Types
17 ( -- * Representations of C 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
29 , CShort, CUShort, CInt, CUInt
31 , CPtrdiff, CSize, CWchar, CSigAtomic
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'.
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
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(..)
63 -- Instances of: Eq and Storable
64 , CFile, CFpos, CJmpBuf
69 import Foreign.Storable
70 import Data.Bits ( Bits(..) )
71 import Data.Int ( Int8, Int16, Int32, Int64 )
72 import Data.Word ( Word8, Word16, Word32, Word64 )
75 #ifdef __GLASGOW_HASKELL__
88 #include "HsBaseConfig.h"
91 -- | Haskell type representing the C @char@ type.
92 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
93 -- | Haskell type representing the C @signed char@ type.
94 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
95 -- | Haskell type representing the C @unsigned char@ type.
96 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
98 -- | Haskell type representing the C @short@ type.
99 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
100 -- | Haskell type representing the C @unsigned short@ type.
101 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
103 -- | Haskell type representing the C @int@ type.
104 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
105 -- | Haskell type representing the C @unsigned int@ type.
106 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
108 -- | Haskell type representing the C @long@ type.
109 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
110 -- | Haskell type representing the C @unsigned long@ type.
111 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
113 -- | Haskell type representing the C @long long@ type.
114 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
115 -- | Haskell type representing the C @unsigned long long@ type.
116 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
119 "fromIntegral/a->CChar" fromIntegral = \x -> CChar (fromIntegral x)
120 "fromIntegral/a->CSChar" fromIntegral = \x -> CSChar (fromIntegral x)
121 "fromIntegral/a->CUChar" fromIntegral = \x -> CUChar (fromIntegral x)
122 "fromIntegral/a->CShort" fromIntegral = \x -> CShort (fromIntegral x)
123 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
124 "fromIntegral/a->CInt" fromIntegral = \x -> CInt (fromIntegral x)
125 "fromIntegral/a->CUInt" fromIntegral = \x -> CUInt (fromIntegral x)
126 "fromIntegral/a->CLong" fromIntegral = \x -> CLong (fromIntegral x)
127 "fromIntegral/a->CULong" fromIntegral = \x -> CULong (fromIntegral x)
128 "fromIntegral/a->CLLong" fromIntegral = \x -> CLLong (fromIntegral x)
129 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
131 "fromIntegral/CChar->a" fromIntegral = \(CChar x) -> fromIntegral x
132 "fromIntegral/CSChar->a" fromIntegral = \(CSChar x) -> fromIntegral x
133 "fromIntegral/CUChar->a" fromIntegral = \(CUChar x) -> fromIntegral x
134 "fromIntegral/CShort->a" fromIntegral = \(CShort x) -> fromIntegral x
135 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
136 "fromIntegral/CInt->a" fromIntegral = \(CInt x) -> fromIntegral x
137 "fromIntegral/CUInt->a" fromIntegral = \(CUInt x) -> fromIntegral x
138 "fromIntegral/CLong->a" fromIntegral = \(CLong x) -> fromIntegral x
139 "fromIntegral/CULong->a" fromIntegral = \(CULong x) -> fromIntegral x
140 "fromIntegral/CLLong->a" fromIntegral = \(CLLong x) -> fromIntegral x
141 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
144 -- | Haskell type representing the C @float@ type.
145 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
146 -- | Haskell type representing the C @double@ type.
147 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
148 -- HACK: Currently no long double in the FFI, so we simply re-use double
149 -- | Haskell type representing the C @long double@ type.
150 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
153 "realToFrac/a->CFloat" realToFrac = \x -> CFloat (realToFrac x)
154 "realToFrac/a->CDouble" realToFrac = \x -> CDouble (realToFrac x)
155 "realToFrac/a->CLDouble" realToFrac = \x -> CLDouble (realToFrac x)
157 "realToFrac/CFloat->a" realToFrac = \(CFloat x) -> realToFrac x
158 "realToFrac/CDouble->a" realToFrac = \(CDouble x) -> realToFrac x
159 "realToFrac/CLDouble->a" realToFrac = \(CLDouble x) -> realToFrac x
162 -- | Haskell type representing the C @ptrdiff_t@ type.
163 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
164 -- | Haskell type representing the C @size_t@ type.
165 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
166 -- | Haskell type representing the C @wchar_t@ type.
167 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
168 -- | Haskell type representing the C @sig_atomic_t@ type.
169 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
172 "fromIntegral/a->CPtrdiff" fromIntegral = \x -> CPtrdiff (fromIntegral x)
173 "fromIntegral/a->CSize" fromIntegral = \x -> CSize (fromIntegral x)
174 "fromIntegral/a->CWchar" fromIntegral = \x -> CWchar (fromIntegral x)
175 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
177 "fromIntegral/CPtrdiff->a" fromIntegral = \(CPtrdiff x) -> fromIntegral x
178 "fromIntegral/CSize->a" fromIntegral = \(CSize x) -> fromIntegral x
179 "fromIntegral/CWchar->a" fromIntegral = \(CWchar x) -> fromIntegral x
180 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
183 -- | Haskell type representing the C @clock_t@ type.
184 ARITHMETIC_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
185 -- | Haskell type representing the C @time_t@ type.
186 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
188 -- FIXME: Implement and provide instances for Eq and Storable
189 -- | Haskell type representing the C @FILE@ type.
191 -- | Haskell type representing the C @fpos_t@ type.
193 -- | Haskell type representing the C @jmp_buf@ type.
194 data CJmpBuf = CJmpBuf
196 INTEGRAL_TYPE(CIntPtr,tyConCIntPtr,"CIntPtr",HTYPE_INTPTR_T)
197 INTEGRAL_TYPE(CUIntPtr,tyConCUIntPtr,"CUIntPtr",HTYPE_UINTPTR_T)
198 INTEGRAL_TYPE(CIntMax,tyConCIntMax,"CIntMax",HTYPE_INTMAX_T)
199 INTEGRAL_TYPE(CUIntMax,tyConCUIntMax,"CUIntMax",HTYPE_UINTMAX_T)
202 "fromIntegral/a->CIntPtr" fromIntegral = \x -> CIntPtr (fromIntegral x)
203 "fromIntegral/a->CUIntPtr" fromIntegral = \x -> CUIntPtr (fromIntegral x)
204 "fromIntegral/a->CIntMax" fromIntegral = \x -> CIntMax (fromIntegral x)
205 "fromIntegral/a->CUIntMax" fromIntegral = \x -> CUIntMax (fromIntegral x)
208 -- C99 types which are still missing include:
209 -- wint_t, wctrans_t, wctype_t
213 These types are needed to accurately represent C function prototypes,
214 in order to access C library interfaces in Haskell. The Haskell system
215 is not required to represent those types exactly as C does, but the
216 following guarantees are provided concerning a Haskell type @CT@
217 representing a C type @t@:
219 * If a C function prototype has @t@ as an argument or result type, the
220 use of @CT@ in the corresponding position in a foreign declaration
221 permits the Haskell program to access the full range of values encoded
222 by the C type; and conversely, any Haskell value for @CT@ has a valid
225 * @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
228 * @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
229 constraint enforced by the C implementation for @t@.
231 * The members 'peek' and 'poke' of the 'Storable' class map all values
232 of @CT@ to the corresponding value of @t@ and vice versa.
234 * When an instance of 'Prelude.Bounded' is defined for @CT@, the values
235 of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
238 * When an instance of 'Prelude.Eq' or 'Prelude.Ord' is defined for @CT@,
239 the predicates defined by the type class implement the same relation
240 as the corresponding predicate in C on @t@.
242 * When an instance of 'Prelude.Num', 'Prelude.Read', 'Prelude.Integral',
243 'Prelude.Fractional', 'Prelude.Floating', 'Prelude.RealFrac', or
244 'Prelude.RealFloat' is defined for @CT@, the arithmetic operations
245 defined by the type class implement the same function as the
246 corresponding arithmetic operations (if available) in C on @t@.
248 * When an instance of 'Bits' is defined for @CT@, the bitwise operation
249 defined by the type class implement the same function as the
250 corresponding bitwise operation in C on @t@.
257 ( CChar(..), CSChar(..), CUChar(..)
258 , CShort(..), CUShort(..), CInt(..), CUInt(..)
259 , CLong(..), CULong(..), CLLong(..), CULLong(..)
260 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
261 , CClock(..), CTime(..)
262 , CFloat(..), CDouble(..), CLDouble(..)
263 , CFile, CFpos, CJmpBuf
267 import NHC.SizedTypes
269 #define INSTANCE_BITS(T) \
270 instance Bits T where { \
271 (T x) .&. (T y) = T (x .&. y) ; \
272 (T x) .|. (T y) = T (x .|. y) ; \
273 (T x) `xor` (T y) = T (x `xor` y) ; \
274 complement (T x) = T (complement x) ; \
275 shift (T x) n = T (shift x n) ; \
276 rotate (T x) n = T (rotate x n) ; \
277 bit n = T (bit n) ; \
278 setBit (T x) n = T (setBit x n) ; \
279 clearBit (T x) n = T (clearBit x n) ; \
280 complementBit (T x) n = T (complementBit x n) ; \
281 testBit (T x) n = testBit x n ; \
282 bitSize (T x) = bitSize x ; \
283 isSigned (T x) = isSigned x }
286 INSTANCE_BITS(CSChar)
287 INSTANCE_BITS(CUChar)
288 INSTANCE_BITS(CShort)
289 INSTANCE_BITS(CUShort)
293 INSTANCE_BITS(CULong)
294 INSTANCE_BITS(CLLong)
295 INSTANCE_BITS(CULLong)
296 INSTANCE_BITS(CPtrdiff)
297 INSTANCE_BITS(CWchar)
298 INSTANCE_BITS(CSigAtomic)