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
35 -- | These types are are represented as @newtype@s of basic
36 -- foreign types, and are instances of
37 -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
38 -- 'Prelude.Show', 'Prelude.Enum', 'Typeable' and 'Storable'.
42 -- | These types are are represented as @newtype@s of
43 -- 'Prelude.Float' and 'Prelude.Double', and are instances of
44 -- 'Prelude.Eq', 'Prelude.Ord', 'Prelude.Num', 'Prelude.Read',
45 -- 'Prelude.Show', 'Prelude.Enum', 'Typeable', 'Storable',
46 -- 'Prelude.Real', 'Prelude.Fractional', 'Prelude.Floating',
47 -- 'Prelude.RealFrac' and 'Prelude.RealFloat'.
48 , CFloat, CDouble, CLDouble
50 -- Exported non-abstractly in nhc98 to fix an interface file problem.
51 CChar(..), CSChar(..), CUChar(..)
52 , CShort(..), CUShort(..), CInt(..), CUInt(..)
53 , CLong(..), CULong(..)
54 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
55 , CLLong(..), CULLong(..)
56 , CClock(..), CTime(..)
57 , CFloat(..), CDouble(..), CLDouble(..)
61 -- Instances of: Eq and Storable
62 , CFile, CFpos, CJmpBuf
67 import Foreign.Storable
68 import Data.Bits ( Bits(..) )
69 import Data.Int ( Int8, Int16, Int32, Int64 )
70 import Data.Word ( Word8, Word16, Word32, Word64 )
73 #ifdef __GLASGOW_HASKELL__
86 #include "HsBaseConfig.h"
89 -- | Haskell type representing the C @char@ type.
90 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
91 -- | Haskell type representing the C @signed char@ type.
92 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
93 -- | Haskell type representing the C @unsigned char@ type.
94 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
96 -- | Haskell type representing the C @short@ type.
97 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
98 -- | Haskell type representing the C @unsigned short@ type.
99 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
101 -- | Haskell type representing the C @int@ type.
102 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
103 -- | Haskell type representing the C @unsigned int@ type.
104 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
106 -- | Haskell type representing the C @long@ type.
107 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
108 -- | Haskell type representing the C @unsigned long@ type.
109 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
111 -- | Haskell type representing the C @long long@ type.
112 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
113 -- | Haskell type representing the C @unsigned long long@ type.
114 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
117 "fromIntegral/a->CChar" fromIntegral = \x -> CChar (fromIntegral x)
118 "fromIntegral/a->CSChar" fromIntegral = \x -> CSChar (fromIntegral x)
119 "fromIntegral/a->CUChar" fromIntegral = \x -> CUChar (fromIntegral x)
120 "fromIntegral/a->CShort" fromIntegral = \x -> CShort (fromIntegral x)
121 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
122 "fromIntegral/a->CInt" fromIntegral = \x -> CInt (fromIntegral x)
123 "fromIntegral/a->CUInt" fromIntegral = \x -> CUInt (fromIntegral x)
124 "fromIntegral/a->CLong" fromIntegral = \x -> CLong (fromIntegral x)
125 "fromIntegral/a->CULong" fromIntegral = \x -> CULong (fromIntegral x)
126 "fromIntegral/a->CLLong" fromIntegral = \x -> CLLong (fromIntegral x)
127 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
129 "fromIntegral/CChar->a" fromIntegral = \(CChar x) -> fromIntegral x
130 "fromIntegral/CSChar->a" fromIntegral = \(CSChar x) -> fromIntegral x
131 "fromIntegral/CUChar->a" fromIntegral = \(CUChar x) -> fromIntegral x
132 "fromIntegral/CShort->a" fromIntegral = \(CShort x) -> fromIntegral x
133 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
134 "fromIntegral/CInt->a" fromIntegral = \(CInt x) -> fromIntegral x
135 "fromIntegral/CUInt->a" fromIntegral = \(CUInt x) -> fromIntegral x
136 "fromIntegral/CLong->a" fromIntegral = \(CLong x) -> fromIntegral x
137 "fromIntegral/CULong->a" fromIntegral = \(CULong x) -> fromIntegral x
138 "fromIntegral/CLLong->a" fromIntegral = \(CLLong x) -> fromIntegral x
139 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
142 -- | Haskell type representing the C @float@ type.
143 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
144 -- | Haskell type representing the C @double@ type.
145 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
146 -- HACK: Currently no long double in the FFI, so we simply re-use double
147 -- | Haskell type representing the C @long double@ type.
148 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
151 "realToFrac/a->CFloat" realToFrac = \x -> CFloat (realToFrac x)
152 "realToFrac/a->CDouble" realToFrac = \x -> CDouble (realToFrac x)
153 "realToFrac/a->CLDouble" realToFrac = \x -> CLDouble (realToFrac x)
155 "realToFrac/CFloat->a" realToFrac = \(CFloat x) -> realToFrac x
156 "realToFrac/CDouble->a" realToFrac = \(CDouble x) -> realToFrac x
157 "realToFrac/CLDouble->a" realToFrac = \(CLDouble x) -> realToFrac x
160 -- | Haskell type representing the C @ptrdiff_t@ type.
161 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
162 -- | Haskell type representing the C @size_t@ type.
163 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
164 -- | Haskell type representing the C @wchar_t@ type.
165 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
166 -- | Haskell type representing the C @sig_atomic_t@ type.
167 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
170 "fromIntegral/a->CPtrdiff" fromIntegral = \x -> CPtrdiff (fromIntegral x)
171 "fromIntegral/a->CSize" fromIntegral = \x -> CSize (fromIntegral x)
172 "fromIntegral/a->CWchar" fromIntegral = \x -> CWchar (fromIntegral x)
173 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
175 "fromIntegral/CPtrdiff->a" fromIntegral = \(CPtrdiff x) -> fromIntegral x
176 "fromIntegral/CSize->a" fromIntegral = \(CSize x) -> fromIntegral x
177 "fromIntegral/CWchar->a" fromIntegral = \(CWchar x) -> fromIntegral x
178 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
181 -- | Haskell type representing the C @clock_t@ type.
182 ARITHMETIC_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
183 -- | Haskell type representing the C @time_t@ type.
184 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
186 -- FIXME: Implement and provide instances for Eq and Storable
187 -- | Haskell type representing the C @FILE@ type.
189 -- | Haskell type representing the C @fpos_t@ type.
191 -- | Haskell type representing the C @jmp_buf@ type.
192 data CJmpBuf = CJmpBuf
194 -- C99 types which are still missing include:
195 -- intptr_t, uintptr_t, intmax_t, uintmax_t, wint_t, wctrans_t, wctype_t
199 These types are needed to accurately represent C function prototypes,
200 in order to access C library interfaces in Haskell. The Haskell system
201 is not required to represent those types exactly as C does, but the
202 following guarantees are provided concerning a Haskell type @CT@
203 representing a C type @t@:
205 * If a C function prototype has @t@ as an argument or result type, the
206 use of @CT@ in the corresponding position in a foreign declaration
207 permits the Haskell program to access the full range of values encoded
208 by the C type; and conversely, any Haskell value for @CT@ has a valid
211 * @'sizeOf' ('Prelude.undefined' :: CT)@ will yield the same value as
214 * @'alignment' ('Prelude.undefined' :: CT)@ matches the alignment
215 constraint enforced by the C implementation for @t@.
217 * The members 'peek' and 'poke' of the 'Storable' class map all values
218 of @CT@ to the corresponding value of @t@ and vice versa.
220 * When an instance of 'Prelude.Bounded' is defined for @CT@, the values
221 of 'Prelude.minBound' and 'Prelude.maxBound' coincide with @t_MIN@
224 * When an instance of 'Prelude.Eq' or 'Prelude.Ord' is defined for @CT@,
225 the predicates defined by the type class implement the same relation
226 as the corresponding predicate in C on @t@.
228 * When an instance of 'Prelude.Num', 'Prelude.Read', 'Prelude.Integral',
229 'Prelude.Fractional', 'Prelude.Floating', 'Prelude.RealFrac', or
230 'Prelude.RealFloat' is defined for @CT@, the arithmetic operations
231 defined by the type class implement the same function as the
232 corresponding arithmetic operations (if available) in C on @t@.
234 * When an instance of 'Bits' is defined for @CT@, the bitwise operation
235 defined by the type class implement the same function as the
236 corresponding bitwise operation in C on @t@.
243 ( CChar(..), CSChar(..), CUChar(..)
244 , CShort(..), CUShort(..), CInt(..), CUInt(..)
245 , CLong(..), CULong(..), CLLong(..), CULLong(..)
246 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
247 , CClock(..), CTime(..)
248 , CFloat(..), CDouble(..), CLDouble(..)
249 , CFile, CFpos, CJmpBuf
253 import NHC.SizedTypes
255 #define INSTANCE_BITS(T) \
256 instance Bits T where { \
257 (T x) .&. (T y) = T (x .&. y) ; \
258 (T x) .|. (T y) = T (x .|. y) ; \
259 (T x) `xor` (T y) = T (x `xor` y) ; \
260 complement (T x) = T (complement x) ; \
261 shift (T x) n = T (shift x n) ; \
262 rotate (T x) n = T (rotate x n) ; \
263 bit n = T (bit n) ; \
264 setBit (T x) n = T (setBit x n) ; \
265 clearBit (T x) n = T (clearBit x n) ; \
266 complementBit (T x) n = T (complementBit x n) ; \
267 testBit (T x) n = testBit x n ; \
268 bitSize (T x) = bitSize x ; \
269 isSigned (T x) = isSigned x }
272 INSTANCE_BITS(CSChar)
273 INSTANCE_BITS(CUChar)
274 INSTANCE_BITS(CShort)
275 INSTANCE_BITS(CUShort)
279 INSTANCE_BITS(CULong)
280 INSTANCE_BITS(CLLong)
281 INSTANCE_BITS(CULLong)
282 INSTANCE_BITS(CPtrdiff)
283 INSTANCE_BITS(CWchar)
284 INSTANCE_BITS(CSigAtomic)