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