[project @ 2002-05-09 13:16:29 by simonmar]
[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(..),  CLLong(..), CULLong(..)
22
23           -- Floating types, instances of: Eq, Ord, Num, Read, Show, Enum,
24           -- Typeable, Storable, Real, Fractional, Floating, RealFrac,
25           -- RealFloat 
26         , CFloat(..),  CDouble(..), CLDouble(..)
27         ) where
28
29 import Data.Bits        ( Bits(..) )
30 import Data.Int         ( Int8,  Int16,  Int32,  Int64  )
31 import Data.Word        ( Word8, Word16, Word32, Word64 )
32 import Data.Dynamic
33
34 #ifdef __GLASGOW_HASKELL__
35 import GHC.Base
36 import GHC.Float
37 import GHC.Enum
38 import GHC.Real
39 import GHC.Show
40 import GHC.Read
41 import GHC.Num
42 #endif
43
44 #include "CTypes.h"
45
46 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
47 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
48 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
49
50 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
51 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
52
53 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
54 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
55
56 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
57 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
58
59 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
60 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
61
62 {-# RULES
63 "fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
64 "fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
65 "fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
66 "fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
67 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
68 "fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
69 "fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
70 "fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
71 "fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
72 "fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
73 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
74
75 "fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
76 "fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
77 "fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
78 "fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
79 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
80 "fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
81 "fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
82 "fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
83 "fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
84 "fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
85 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
86  #-}
87
88 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
89 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
90 -- HACK: Currently no long double in the FFI, so we simply re-use double
91 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
92
93
94 #include "Dynamic.h"
95 INSTANCE_TYPEABLE0(CChar,cCharTc,"CChar")
96 INSTANCE_TYPEABLE0(CSChar,cSCharTc,"CSChar")
97 INSTANCE_TYPEABLE0(CUChar,cUCharTc,"CUChar")
98
99 INSTANCE_TYPEABLE0(CShort,cShortTc,"CShort")
100 INSTANCE_TYPEABLE0(CUShort,cUShortTc,"CUShort")
101
102 INSTANCE_TYPEABLE0(CInt,cIntTc,"CInt")
103 INSTANCE_TYPEABLE0(CUInt,cUIntTc,"CUInt")
104
105 INSTANCE_TYPEABLE0(CLong,cLongTc,"CLong")
106 INSTANCE_TYPEABLE0(CULong,cULongTc,"CULong")
107
108 INSTANCE_TYPEABLE0(CLLong,cLLongTc,"CLLong")
109 INSTANCE_TYPEABLE0(CULLong,cULLongTc,"CULLong")
110
111 INSTANCE_TYPEABLE0(CFloat,cFloatTc,"CFloat")
112 INSTANCE_TYPEABLE0(CDouble,cDoubleTc,"CDouble")
113 INSTANCE_TYPEABLE0(CLDouble,cLDoubleTc,"CLDouble")