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