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