1 {-# OPTIONS -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
18 ( -- Integral types, instances of: Eq, Ord, Num, Read, Show, Enum,
19 -- Typeable, Storable, Bounded, Real, Integral, Bits
21 , CShort, CUShort, CInt, CUInt
23 , CPtrdiff, CSize, CWchar, CSigAtomic
25 -- Numeric types, instances of: Eq, Ord, Num, Read, Show, Enum,
29 -- Floating types, instances of: Eq, Ord, Num, Read, Show, Enum,
30 -- Typeable, Storable, Real, Fractional, Floating, RealFrac,
32 , CFloat, CDouble, CLDouble
34 ( -- Exported non-abstractly in nhc98 to fix an interface file problem.
35 CChar(..), CSChar(..), CUChar(..)
36 , CShort(..), CUShort(..), CInt(..), CUInt(..)
37 , CLong(..), CULong(..)
38 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
39 , CLLong(..), CULLong(..)
40 , CClock(..), CTime(..)
41 , CFloat(..), CDouble(..), CLDouble(..)
44 -- Instances of: Eq and Storable
45 , CFile, CFpos, CJmpBuf
50 ( CChar(..), CSChar(..), CUChar(..)
51 , CShort(..), CUShort(..), CInt(..), CUInt(..)
52 , CLong(..), CULong(..), CLLong(..), CULLong(..)
53 , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
54 , CClock(..), CTime(..)
55 , CFloat(..), CDouble(..), CLDouble(..)
56 , CFile, CFpos, CJmpBuf
61 import Foreign.Storable
62 import Data.Bits ( Bits(..) )
63 import Data.Int ( Int8, Int16, Int32, Int64 )
64 import Data.Word ( Word8, Word16, Word32, Word64 )
67 #ifdef __GLASGOW_HASKELL__
83 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
84 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
85 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
87 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
88 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
90 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
91 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
93 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
94 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
96 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
97 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
100 "fromIntegral/a->CChar" fromIntegral = \x -> CChar (fromIntegral x)
101 "fromIntegral/a->CSChar" fromIntegral = \x -> CSChar (fromIntegral x)
102 "fromIntegral/a->CUChar" fromIntegral = \x -> CUChar (fromIntegral x)
103 "fromIntegral/a->CShort" fromIntegral = \x -> CShort (fromIntegral x)
104 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
105 "fromIntegral/a->CInt" fromIntegral = \x -> CInt (fromIntegral x)
106 "fromIntegral/a->CUInt" fromIntegral = \x -> CUInt (fromIntegral x)
107 "fromIntegral/a->CLong" fromIntegral = \x -> CLong (fromIntegral x)
108 "fromIntegral/a->CULong" fromIntegral = \x -> CULong (fromIntegral x)
109 "fromIntegral/a->CLLong" fromIntegral = \x -> CLLong (fromIntegral x)
110 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
112 "fromIntegral/CChar->a" fromIntegral = \(CChar x) -> fromIntegral x
113 "fromIntegral/CSChar->a" fromIntegral = \(CSChar x) -> fromIntegral x
114 "fromIntegral/CUChar->a" fromIntegral = \(CUChar x) -> fromIntegral x
115 "fromIntegral/CShort->a" fromIntegral = \(CShort x) -> fromIntegral x
116 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
117 "fromIntegral/CInt->a" fromIntegral = \(CInt x) -> fromIntegral x
118 "fromIntegral/CUInt->a" fromIntegral = \(CUInt x) -> fromIntegral x
119 "fromIntegral/CLong->a" fromIntegral = \(CLong x) -> fromIntegral x
120 "fromIntegral/CULong->a" fromIntegral = \(CULong x) -> fromIntegral x
121 "fromIntegral/CLLong->a" fromIntegral = \(CLLong x) -> fromIntegral x
122 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
125 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
126 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
127 -- HACK: Currently no long double in the FFI, so we simply re-use double
128 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
131 "realToFrac/a->CFloat" realToFrac = \x -> CFloat (realToFrac x)
132 "realToFrac/a->CDouble" realToFrac = \x -> CDouble (realToFrac x)
133 "realToFrac/a->CLDouble" realToFrac = \x -> CLDouble (realToFrac x)
135 "realToFrac/CFloat->a" realToFrac = \(CFloat x) -> realToFrac x
136 "realToFrac/CDouble->a" realToFrac = \(CDouble x) -> realToFrac x
137 "realToFrac/CLDouble->a" realToFrac = \(CLDouble x) -> realToFrac x
140 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
141 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
142 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
143 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
146 "fromIntegral/a->CPtrdiff" fromIntegral = \x -> CPtrdiff (fromIntegral x)
147 "fromIntegral/a->CSize" fromIntegral = \x -> CSize (fromIntegral x)
148 "fromIntegral/a->CWchar" fromIntegral = \x -> CWchar (fromIntegral x)
149 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
151 "fromIntegral/CPtrdiff->a" fromIntegral = \(CPtrdiff x) -> fromIntegral x
152 "fromIntegral/CSize->a" fromIntegral = \(CSize x) -> fromIntegral x
153 "fromIntegral/CWchar->a" fromIntegral = \(CWchar x) -> fromIntegral x
154 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
157 INTEGRAL_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
158 INTEGRAL_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
160 -- FIXME: Implement and provide instances for Eq and Storable
163 data CJmpBuf = CJmpBuf
165 -- C99 types which are still missing include:
166 -- intptr_t, uintptr_t, intmax_t, uintmax_t, wint_t, wctrans_t, wctype_t