[project @ 2002-08-03 19:32:16 by reid]
[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. A cool hack...
13 --
14 -----------------------------------------------------------------------------
15
16 module Foreign.C.Types
17         ( -- Integral types, instances of: Eq, Ord, Num, Read, Show, Enum,
18           -- Typeable, Storable, Bounded, Real, Integral, Bits
19           CChar(..),  CSChar(..),  CUChar(..)
20         , CShort(..), CUShort(..), CInt(..),   CUInt(..)
21         , CLong(..),  CULong(..)
22 #ifndef __HUGS__
23         , CLLong(..), CULLong(..)
24 #endif
25
26           -- Floating types, instances of: Eq, Ord, Num, Read, Show, Enum,
27           -- Typeable, Storable, Real, Fractional, Floating, RealFrac,
28           -- RealFloat 
29         , CFloat(..),  CDouble(..), CLDouble(..)
30         ) where
31
32 import Data.Bits        ( Bits(..) )
33 import Data.Int         ( Int8,  Int16,  Int32,  Int64  )
34 import Data.Word        ( Word8, Word16, Word32, Word64 )
35 import Data.Dynamic
36
37 #ifdef __GLASGOW_HASKELL__
38 import GHC.Base
39 import GHC.Float
40 import GHC.Enum
41 import GHC.Real
42 import GHC.Show
43 import GHC.Read
44 import GHC.Num
45 #endif
46
47 #include "Dynamic.h"
48 #include "CTypes.h"
49
50 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
51 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
52 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
53
54 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
55 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
56
57 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
58 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
59
60 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
61 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
62
63 #ifndef __HUGS__
64 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
65 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
66 #endif
67
68 {-# RULES
69 "fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
70 "fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
71 "fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
72 "fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
73 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
74 "fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
75 "fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
76 "fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
77 "fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
78 "fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
79 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
80
81 "fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
82 "fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
83 "fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
84 "fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
85 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
86 "fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
87 "fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
88 "fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
89 "fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
90 "fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
91 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
92  #-}
93
94 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
95 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
96 -- HACK: Currently no long double in the FFI, so we simply re-use double
97 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
98