[project @ 2003-07-31 10:48:50 by panne]
[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.Storable
62 import Data.Bits        ( Bits(..) )
63 import Data.Int         ( Int8,  Int16,  Int32,  Int64  )
64 import Data.Word        ( Word8, Word16, Word32, Word64 )
65 import Data.Typeable
66
67 #ifdef __GLASGOW_HASKELL__
68 import GHC.Base
69 import GHC.Float
70 import GHC.Enum
71 import GHC.Real
72 import GHC.Show
73 import GHC.Read
74 import GHC.Num
75 #else
76 import Control.Monad
77 import Foreign.Ptr
78 #endif
79
80 #include "Typeable.h"
81 #include "CTypes.h"
82
83 INTEGRAL_TYPE(CChar,tyConCChar,"CChar",HTYPE_CHAR)
84 INTEGRAL_TYPE(CSChar,tyConCSChar,"CSChar",HTYPE_SIGNED_CHAR)
85 INTEGRAL_TYPE(CUChar,tyConCUChar,"CUChar",HTYPE_UNSIGNED_CHAR)
86
87 INTEGRAL_TYPE(CShort,tyConCShort,"CShort",HTYPE_SHORT)
88 INTEGRAL_TYPE(CUShort,tyConCUShort,"CUShort",HTYPE_UNSIGNED_SHORT)
89
90 INTEGRAL_TYPE(CInt,tyConCInt,"CInt",HTYPE_INT)
91 INTEGRAL_TYPE(CUInt,tyConCUInt,"CUInt",HTYPE_UNSIGNED_INT)
92
93 INTEGRAL_TYPE(CLong,tyConCLong,"CLong",HTYPE_LONG)
94 INTEGRAL_TYPE(CULong,tyConCULong,"CULong",HTYPE_UNSIGNED_LONG)
95
96 INTEGRAL_TYPE(CLLong,tyConCLLong,"CLLong",HTYPE_LONG_LONG)
97 INTEGRAL_TYPE(CULLong,tyConCULLong,"CULLong",HTYPE_UNSIGNED_LONG_LONG)
98
99 {-# RULES
100 "fromIntegral/a->CChar"   fromIntegral = \x -> CChar   (fromIntegral x)
101 "fromIntegral/a->CSChar"  fromIntegral = \x -> CSChar  (fromIntegral x)
102 "fromIntegral/a->CUChar"  fromIntegral = \x -> CUChar  (fromIntegral x)
103 "fromIntegral/a->CShort"  fromIntegral = \x -> CShort  (fromIntegral x)
104 "fromIntegral/a->CUShort" fromIntegral = \x -> CUShort (fromIntegral x)
105 "fromIntegral/a->CInt"    fromIntegral = \x -> CInt    (fromIntegral x)
106 "fromIntegral/a->CUInt"   fromIntegral = \x -> CUInt   (fromIntegral x)
107 "fromIntegral/a->CLong"   fromIntegral = \x -> CLong   (fromIntegral x)
108 "fromIntegral/a->CULong"  fromIntegral = \x -> CULong  (fromIntegral x)
109 "fromIntegral/a->CLLong"  fromIntegral = \x -> CLLong  (fromIntegral x)
110 "fromIntegral/a->CULLong" fromIntegral = \x -> CULLong (fromIntegral x)
111
112 "fromIntegral/CChar->a"   fromIntegral = \(CChar   x) -> fromIntegral x
113 "fromIntegral/CSChar->a"  fromIntegral = \(CSChar  x) -> fromIntegral x
114 "fromIntegral/CUChar->a"  fromIntegral = \(CUChar  x) -> fromIntegral x
115 "fromIntegral/CShort->a"  fromIntegral = \(CShort  x) -> fromIntegral x
116 "fromIntegral/CUShort->a" fromIntegral = \(CUShort x) -> fromIntegral x
117 "fromIntegral/CInt->a"    fromIntegral = \(CInt    x) -> fromIntegral x
118 "fromIntegral/CUInt->a"   fromIntegral = \(CUInt   x) -> fromIntegral x
119 "fromIntegral/CLong->a"   fromIntegral = \(CLong   x) -> fromIntegral x
120 "fromIntegral/CULong->a"  fromIntegral = \(CULong  x) -> fromIntegral x
121 "fromIntegral/CLLong->a"  fromIntegral = \(CLLong  x) -> fromIntegral x
122 "fromIntegral/CULLong->a" fromIntegral = \(CULLong x) -> fromIntegral x
123  #-}
124
125 FLOATING_TYPE(CFloat,tyConCFloat,"CFloat",HTYPE_FLOAT)
126 FLOATING_TYPE(CDouble,tyConCDouble,"CDouble",HTYPE_DOUBLE)
127 -- HACK: Currently no long double in the FFI, so we simply re-use double
128 FLOATING_TYPE(CLDouble,tyConCLDouble,"CLDouble",HTYPE_DOUBLE)
129
130 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
131 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
132 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
133 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
134
135 {-# RULES
136 "fromIntegral/a->CPtrdiff"   fromIntegral = \x -> CPtrdiff   (fromIntegral x)
137 "fromIntegral/a->CSize"      fromIntegral = \x -> CSize      (fromIntegral x)
138 "fromIntegral/a->CWchar"     fromIntegral = \x -> CWchar     (fromIntegral x)
139 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
140
141 "fromIntegral/CPtrdiff->a"   fromIntegral = \(CPtrdiff   x) -> fromIntegral x
142 "fromIntegral/CSize->a"      fromIntegral = \(CSize      x) -> fromIntegral x
143 "fromIntegral/CWchar->a"     fromIntegral = \(CWchar     x) -> fromIntegral x
144 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
145  #-}
146
147 INTEGRAL_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
148 INTEGRAL_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
149
150 -- FIXME: Implement and provide instances for Eq and Storable
151 data CFile = CFile
152 data CFpos = CFpos
153 data CJmpBuf = CJmpBuf
154
155 -- C99 types which are still missing include:
156 -- intptr_t, uintptr_t, intmax_t, uintmax_t, wint_t, wctrans_t, wctype_t
157
158 #endif