[project @ 2004-07-27 10:35:54 by ross]
[ghc-base.git] / Foreign / C / Types.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Foreign.C.Types
5 -- Copyright   :  (c) The FFI task force 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  ffi@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- Mapping of C types to corresponding Haskell types.
13 --
14 -----------------------------------------------------------------------------
15
16 module Foreign.C.Types
17         ( -- * Representations of C types
18 #ifndef __NHC__
19           -- $ctypes
20
21           -- ** Integral types
22           -- | These types are are represented as @newtype@s of types in
23           -- "Data.Int" and "Data.Word", and are instances of
24           -- 'Eq', 'Ord', 'Num', 'Read', 'Show', 'Enum', 'Typeable',
25           -- 'Storable', 'Bounded', 'Real', 'Integral' and 'Bits'.
26           CChar,  CSChar,  CUChar
27         , CShort, CUShort, CInt,   CUInt
28         , CLong,  CULong
29         , CPtrdiff, CSize, CWchar, CSigAtomic
30         , CLLong, CULLong
31
32           -- ** Numeric types
33           -- | These types are are represented as @newtype@s of basic
34           -- foreign types, and are instances of
35           -- 'Eq', 'Ord', 'Num', 'Read', 'Show', 'Enum', 'Typeable' and
36           -- 'Storable'.
37         , CClock,   CTime
38
39           -- ** Floating types
40           -- | These types are are represented as @newtype@s of 'Float'
41           -- and 'Double', and are instances of
42           -- 'Eq', 'Ord', 'Num', 'Read', 'Show', 'Enum', 'Typeable',
43           -- 'Storable', 'Real', 'Fractional', 'Floating', 'RealFrac'
44           -- and 'RealFloat'.
45         , CFloat,  CDouble, CLDouble
46 #else
47           -- Exported non-abstractly in nhc98 to fix an interface file problem.
48           CChar(..),    CSChar(..),  CUChar(..)
49         , CShort(..),   CUShort(..), CInt(..),   CUInt(..)
50         , CLong(..),    CULong(..)
51         , CPtrdiff(..), CSize(..),   CWchar(..), CSigAtomic(..)
52         , CLLong(..),   CULLong(..)
53         , CClock(..),   CTime(..)
54         , CFloat(..),   CDouble(..), CLDouble(..)
55 #endif
56           -- ** Other types
57
58           -- Instances of: Eq and Storable
59         , CFile,        CFpos,     CJmpBuf
60         ) where
61
62 #ifndef __NHC__
63
64 import Foreign.Storable
65 import Data.Bits        ( Bits(..) )
66 import Data.Int         ( Int8,  Int16,  Int32,  Int64  )
67 import Data.Word        ( Word8, Word16, Word32, Word64 )
68 import Data.Typeable
69
70 #ifdef __GLASGOW_HASKELL__
71 import GHC.Base
72 import GHC.Float
73 import GHC.Enum
74 import GHC.Real
75 import GHC.Show
76 import GHC.Read
77 import GHC.Num
78 #else
79 import Control.Monad
80 import Foreign.Ptr
81 #endif
82
83 #include "Typeable.h"
84 #include "CTypes.h"
85
86 -- | Haskell type representing the C @char@ type.
87 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
88 -- | Haskell type representing the C @signed char@ type.
89 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
90 -- | Haskell type representing the C @unsigned char@ type.
91 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
92
93 -- | Haskell type representing the C @short@ type.
94 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
95 -- | Haskell type representing the C @unsigned short@ type.
96 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
97
98 -- | Haskell type representing the C @int@ type.
99 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
100 -- | Haskell type representing the C @unsigned int@ type.
101 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
102
103 -- | Haskell type representing the C @long@ type.
104 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
105 -- | Haskell type representing the C @unsigned long@ type.
106 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
107
108 -- | Haskell type representing the C @long long@ type.
109 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
110 -- | Haskell type representing the C @unsigned long long@ type.
111 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
112
113 {-# RULES
114 "fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
115 "fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
116 "fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
117 "fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
118 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
119 "fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
120 "fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
121 "fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
122 "fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
123 "fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
124 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
125
126 "fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
127 "fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
128 "fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
129 "fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
130 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
131 "fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
132 "fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
133 "fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
134 "fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
135 "fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
136 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
137  #-}
138
139 -- | Haskell type representing the C @float@ type.
140 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
141 -- | Haskell type representing the C @double@ type.
142 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
143 -- HACK: Currently no long double in the FFI, so we simply re-use double
144 -- | Haskell type representing the C @long double@ type.
145 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
146
147 {-# RULES
148 "realToFrac/a->CFloat"    realToFrac = \x -> CFloat   (realToFrac x)
149 "realToFrac/a->CDouble"   realToFrac = \x -> CDouble  (realToFrac x)
150 "realToFrac/a->CLDouble"  realToFrac = \x -> CLDouble (realToFrac x)
151
152 "realToFrac/CFloat->a"    realToFrac = \(CFloat   x) -> realToFrac x
153 "realToFrac/CDouble->a"   realToFrac = \(CDouble  x) -> realToFrac x
154 "realToFrac/CLDouble->a"  realToFrac = \(CLDouble x) -> realToFrac x
155  #-}
156
157 -- | Haskell type representing the C @ptrdiff_t@ type.
158 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
159 -- | Haskell type representing the C @size_t@ type.
160 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
161 -- | Haskell type representing the C @wchar_t@ type.
162 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
163 -- | Haskell type representing the C @sig_atomic_t@ type.
164 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
165
166 {-# RULES
167 "fromIntegral/a->CPtrdiff"   fromIntegral = \x -> CPtrdiff   (fromIntegral x)
168 "fromIntegral/a->CSize"      fromIntegral = \x -> CSize      (fromIntegral x)
169 "fromIntegral/a->CWchar"     fromIntegral = \x -> CWchar     (fromIntegral x)
170 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
171
172 "fromIntegral/CPtrdiff->a"   fromIntegral = \(CPtrdiff   x) -> fromIntegral x
173 "fromIntegral/CSize->a"      fromIntegral = \(CSize      x) -> fromIntegral x
174 "fromIntegral/CWchar->a"     fromIntegral = \(CWchar     x) -> fromIntegral x
175 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
176  #-}
177
178 -- | Haskell type representing the C @clock_t@ type.
179 ARITHMETIC_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
180 -- | Haskell type representing the C @time_t@ type.
181 ARITHMETIC_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
182
183 -- FIXME: Implement and provide instances for Eq and Storable
184 -- | Haskell type representing the C @FILE@ type.
185 data CFile = CFile
186 -- | Haskell type representing the C @fpos_t@ type.
187 data CFpos = CFpos
188 -- | Haskell type representing the C @jmp_buf@ type.
189 data CJmpBuf = CJmpBuf
190
191 -- C99 types which are still missing include:
192 -- intptr_t, uintptr_t, intmax_t, uintmax_t, wint_t, wctrans_t, wctype_t
193
194 {- $ctypes
195
196 These types are needed to accurately represent C function prototypes,
197 in order to access C library interfaces in Haskell.  The Haskell system
198 is not required to represent those types exactly as C does, but the
199 following guarantees are provided concerning a Haskell type @CT@
200 representing a C type @t@:
201
202 * If a C function prototype has @t@ as an argument or result type,
203   the use of @CT@ in the corresponding position in a foreign declaration
204   permits the Haskell program to access the full range of values encoded by
205   the C type; and conversely, any Haskell value for @CT@ has a valid
206   representation in C.
207
208 * @'sizeOf' ('undefined' :: CT)@ will yield the same value as
209   @sizeof (t)@ in C.
210
211 * @'alignment' ('undefined' :: CT)@ matches the alignment constraint
212   enforced by the C implementation for @t@.
213
214 * The members 'peek' and 'poke' of the 'Storable' class map all values of
215   @CT@ to the corresponding value of @t@ and vice versa.
216
217 * When an instance of 'Bounded' is defined for @CT@, the values of
218   'minBound' and 'maxBound' coincide with @t_MIN@ and @t_MAX@ in C.
219
220 * When an instance of 'Eq' or 'Ord' is defined for @CT@, the predicates
221   defined by the type class implement the same relation as the
222   corresponding predicate in C on @t@.
223
224 * When an instance of 'Num', 'Read', 'Integral', 'Fractional', 'Floating',
225   'RealFrac', or 'RealFloat' is defined for @CT@, the arithmetic
226   operations defined by the type class implement the same function as
227   the corresponding arithmetic operations (if available) in C on @t@.
228
229 * When an instance of 'Bits' is defined for @CT@, the bitwise operation
230   defined by the type class implement the same function as the
231   corresponding bitwise operation in C on @t@.
232
233 -}
234
235 #else   /* __NHC__ */
236
237 import NHC.FFI
238   ( CChar(..),    CSChar(..),  CUChar(..)
239   , CShort(..),   CUShort(..), CInt(..),   CUInt(..)
240   , CLong(..),    CULong(..),  CLLong(..), CULLong(..)
241   , CPtrdiff(..), CSize(..),   CWchar(..), CSigAtomic(..)
242   , CClock(..),   CTime(..)
243   , CFloat(..),   CDouble(..), CLDouble(..)
244   , CFile,        CFpos,       CJmpBuf
245   , Storable(..)
246   )
247 import Data.Bits
248 import NHC.SizedTypes
249
250 #define INSTANCE_BITS(T) \
251 instance Bits T where { \
252   (T x) .&.     (T y)   = T (x .&.   y) ; \
253   (T x) .|.     (T y)   = T (x .|.   y) ; \
254   (T x) `xor`   (T y)   = T (x `xor` y) ; \
255   complement    (T x)   = T (complement x) ; \
256   shift         (T x) n = T (shift x n) ; \
257   rotate        (T x) n = T (rotate x n) ; \
258   bit                 n = T (bit n) ; \
259   setBit        (T x) n = T (setBit x n) ; \
260   clearBit      (T x) n = T (clearBit x n) ; \
261   complementBit (T x) n = T (complementBit x n) ; \
262   testBit       (T x) n = testBit x n ; \
263   bitSize       (T x)   = bitSize x ; \
264   isSigned      (T x)   = isSigned x }
265
266 INSTANCE_BITS(CChar)
267 INSTANCE_BITS(CSChar)
268 INSTANCE_BITS(CUChar)
269 INSTANCE_BITS(CShort)
270 INSTANCE_BITS(CUShort)
271 INSTANCE_BITS(CInt)
272 INSTANCE_BITS(CUInt)
273 INSTANCE_BITS(CLong)
274 INSTANCE_BITS(CULong)
275 INSTANCE_BITS(CLLong)
276 INSTANCE_BITS(CULLong)
277 INSTANCE_BITS(CPtrdiff)
278 INSTANCE_BITS(CWchar)
279 INSTANCE_BITS(CSigAtomic)
280 INSTANCE_BITS(CSize)
281
282 #endif