[project @ 2003-05-23 10:21:27 by simonmar]
[haskell-directory.git] / Foreign / C / TypesISO.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Foreign.C.TypesISO
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 -- A mapping of C types defined by the ISO C standard to corresponding Haskell
13 -- types. Like CTypes, this is a cool hack...
14 --
15 -----------------------------------------------------------------------------
16
17 -- #hide
18 module Foreign.C.TypesISO
19         ( -- Integral types, instances of: Eq, Ord, Num, Read, Show, Enum,
20           -- Typeable, Storable, Bounded, Real, Integral, Bits
21           CPtrdiff, CSize, CWchar, CSigAtomic
22
23           -- Numeric types, instances of: Eq, Ord, Num, Read, Show, Enum,
24           -- Typeable, Storable
25         , CClock,   CTime
26
27           -- Instances of: Eq and Storable
28         , CFile,        CFpos,     CJmpBuf
29         ) where
30
31 #ifdef __NHC__
32 import NHC.FFI
33   ( CPtrdiff
34   , CSize
35   , CWchar
36   , CSigAtomic
37   , CClock
38   , CTime
39   , CFile
40   , CFpos
41   , CJmpBuf
42   )
43 #else
44
45 import Data.Bits        ( Bits(..) )
46 import Data.Int
47 import Data.Word
48 import Data.Dynamic
49 import Foreign.Storable
50
51 #ifdef __GLASGOW_HASKELL__
52 import GHC.Base
53 import GHC.Enum
54 import GHC.Real
55 import GHC.Show
56 import GHC.Read
57 import GHC.Num
58 #else
59 import Control.Monad
60 import Foreign.Ptr
61 #endif
62
63 #include "Dynamic.h"
64 #include "CTypes.h"
65
66 INTEGRAL_TYPE(CPtrdiff,tyConCPtrdiff,"CPtrdiff",HTYPE_PTRDIFF_T)
67 INTEGRAL_TYPE(CSize,tyConCSize,"CSize",HTYPE_SIZE_T)
68 INTEGRAL_TYPE(CWchar,tyConCWchar,"CWchar",HTYPE_WCHAR_T)
69 INTEGRAL_TYPE(CSigAtomic,tyConCSigAtomic,"CSigAtomic",HTYPE_SIG_ATOMIC_T)
70
71 {-# RULES
72 "fromIntegral/a->CPtrdiff"   fromIntegral = \x -> CPtrdiff   (fromIntegral x)
73 "fromIntegral/a->CSize"      fromIntegral = \x -> CSize      (fromIntegral x)
74 "fromIntegral/a->CWchar"     fromIntegral = \x -> CWchar     (fromIntegral x)
75 "fromIntegral/a->CSigAtomic" fromIntegral = \x -> CSigAtomic (fromIntegral x)
76
77 "fromIntegral/CPtrdiff->a"   fromIntegral = \(CPtrdiff   x) -> fromIntegral x
78 "fromIntegral/CSize->a"      fromIntegral = \(CSize      x) -> fromIntegral x
79 "fromIntegral/CWchar->a"     fromIntegral = \(CWchar     x) -> fromIntegral x
80 "fromIntegral/CSigAtomic->a" fromIntegral = \(CSigAtomic x) -> fromIntegral x
81  #-}
82
83 INTEGRAL_TYPE(CClock,tyConCClock,"CClock",HTYPE_CLOCK_T)
84 INTEGRAL_TYPE(CTime,tyConCTime,"CTime",HTYPE_TIME_T)
85
86 -- FIXME: Implement and provide instances for Eq and Storable
87 data CFile = CFile
88 data CFpos = CFpos
89 data CJmpBuf = CJmpBuf
90
91 -- C99 types which are still missing include:
92 -- intptr_t, uintptr_t, intmax_t, uintmax_t, wint_t, wctrans_t, wctype_t
93
94 #endif