[project @ 2001-02-22 13:17:57 by simonpj]
[ghc-hetmet.git] / ghc / lib / std / PrelCTypes.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelCTypes.lhs,v 1.2 2001/02/22 13:17:58 simonpj Exp $
3 %
4 % (c) The FFI task force, 2000
5 %
6
7 A mapping of C types to corresponding Haskell types. A cool hack...
8
9 #include "cbits/CTypes.h"
10
11 \begin{code}
12 module PrelCTypes
13         ( -- Integral types, instances of: Eq, Ord, Num, Read, Show, Enum,
14           -- Typeable, Storable, Bounded, Real, Integral, Bits
15           CChar(..),    CSChar(..),  CUChar(..)
16         , CShort(..),   CUShort(..), CInt(..),    CUInt(..)
17         , CLong(..),    CULong(..),  CLLong(..),  CULLong(..)
18
19           -- Floating types, instances of: Eq, Ord, Num, Read, Show, Enum,
20           -- Typeable, Storable, Real, Fractional, Floating, RealFrac, RealFloat
21         , CFloat(..),   CDouble(..), CLDouble(..)
22         ) where
23 \end{code}
24
25 \begin{code}
26 import PrelBase ( unsafeCoerce# )
27 import PrelReal ( Integral(toInt) )
28 import PrelBits ( Bits(..) )
29 import PrelInt  ( Int8,  Int16,  Int32,  Int64  )
30 import PrelWord ( Word8, Word16, Word32, Word64 )
31 \end{code}
32
33 \begin{code}
34 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
35 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
36 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
37
38 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
39 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
40
41 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
42 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
43
44 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
45 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
46
47 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
48 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
49
50 {-# RULES
51 "fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
52 "fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
53 "fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
54 "fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
55 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
56 "fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
57 "fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
58 "fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
59 "fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
60 "fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
61 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
62
63 "fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
64 "fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
65 "fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
66 "fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
67 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
68 "fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
69 "fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
70 "fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
71 "fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
72 "fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
73 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
74  #-}
75
76 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
77 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
78 -- HACK: Currently no long double in the FFI, so we simply re-use double
79 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
80 \end{code}