[project @ 2002-10-11 11:05:20 by malcolm]
[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         , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
23 #ifndef __HUGS__
24         , CLLong(..), CULLong(..)
25 #endif
26           -- Numeric types, instances of: Eq, Ord, Num, Read, Show, Enum,
27           -- Typeable, Storable
28         , CClock(..),   CTime(..)
29
30           -- Floating types, instances of: Eq, Ord, Num, Read, Show, Enum,
31           -- Typeable, Storable, Real, Fractional, Floating, RealFrac,
32           -- RealFloat 
33         , CFloat(..),  CDouble(..), CLDouble(..)
34
35           -- Instances of: Eq and Storable
36         , CFile,        CFpos,     CJmpBuf
37         ) where
38
39 #ifdef __NHC__
40 import NHC.FFI
41   ( CChar(..),  CSChar(..),  CUChar(..)
42   , CShort(..), CUShort(..), CInt(..),   CUInt(..)
43   , CLong(..),  CULong(..), CLLong(..), CULLong(..)
44   , CPtrdiff(..), CSize(..), CWchar(..), CSigAtomic(..)
45   , CClock(..),   CTime(..)
46   , CFloat(..),  CDouble(..), CLDouble(..)
47   , CFile,        CFpos,     CJmpBuf
48   , Storable(..)
49   )
50 #else
51
52 import Foreign.C.TypesISO
53 import Foreign.Storable
54 import Data.Bits        ( Bits(..) )
55 import Data.Int         ( Int8,  Int16,  Int32,  Int64  )
56 import Data.Word        ( Word8, Word16, Word32, Word64 )
57 #ifndef __NHC__
58 import Data.Dynamic
59 #endif
60
61 #ifdef __GLASGOW_HASKELL__
62 import GHC.Base
63 import GHC.Float
64 import GHC.Enum
65 import GHC.Real
66 import GHC.Show
67 import GHC.Read
68 import GHC.Num
69 #else
70 import Control.Monad
71 import Foreign.Ptr
72 #endif
73
74 #include "Dynamic.h"
75 #include "CTypes.h"
76
77 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
78 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
79 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
80
81 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
82 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
83
84 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
85 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
86
87 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
88 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
89
90 #ifndef __HUGS__
91 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
92 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
93 #endif
94
95 {-# RULES
96 "fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
97 "fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
98 "fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
99 "fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
100 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
101 "fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
102 "fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
103 "fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
104 "fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
105 "fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
106 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
107
108 "fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
109 "fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
110 "fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
111 "fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
112 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
113 "fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
114 "fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
115 "fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
116 "fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
117 "fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
118 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
119  #-}
120
121 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
122 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
123 -- HACK: Currently no long double in the FFI, so we simply re-use double
124 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
125
126 #endif