portable implementation of WordPtr/IntPtr for non-GHC
[haskell-directory.git] / Foreign / C / Types.hs
index f34b445..7e8c5a3 100644 (file)
@@ -1,4 +1,4 @@
-{-# OPTIONS -fno-implicit-prelude #-}
+{-# OPTIONS_GHC -fno-implicit-prelude #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  Foreign.C.Types
@@ -30,6 +30,8 @@ module Foreign.C.Types
        , CLong,  CULong
        , CPtrdiff, CSize, CWchar, CSigAtomic
         , CLLong, CULLong
+       , CIntPtr, CUIntPtr
+       , CIntMax, CUIntMax
 
          -- ** Numeric types
          -- | These types are are represented as @newtype@s of basic
@@ -79,11 +81,14 @@ import GHC.Show
 import GHC.Read
 import GHC.Num
 #else
-import Control.Monad
-import Foreign.Ptr
+import Control.Monad   ( liftM )
 #endif
 
-#include "Typeable.h"
+#ifdef __HUGS__
+import Hugs.Ptr                ( castPtr )
+#endif
+
+#include "HsBaseConfig.h"
 #include "CTypes.h"
 
 -- | Haskell type representing the C @char@ type.
@@ -191,8 +196,20 @@ data CFpos = CFpos
 -- | Haskell type representing the C @jmp_buf@ type.
 data CJmpBuf = CJmpBuf
 
+INTEGRAL_TYPE(CIntPtr,tyConCIntPtr,"CIntPtr",HTYPE_INTPTR_T)
+INTEGRAL_TYPE(CUIntPtr,tyConCUIntPtr,"CUIntPtr",HTYPE_UINTPTR_T)
+INTEGRAL_TYPE(CIntMax,tyConCIntMax,"CIntMax",HTYPE_INTMAX_T)
+INTEGRAL_TYPE(CUIntMax,tyConCUIntMax,"CUIntMax",HTYPE_UINTMAX_T)
+
+{-# RULES
+"fromIntegral/a->CIntPtr"  fromIntegral = \x -> CIntPtr  (fromIntegral x)
+"fromIntegral/a->CUIntPtr" fromIntegral = \x -> CUIntPtr (fromIntegral x)
+"fromIntegral/a->CIntMax"  fromIntegral = \x -> CIntMax  (fromIntegral x)
+"fromIntegral/a->CUIntMax" fromIntegral = \x -> CUIntMax (fromIntegral x)
+ #-}
+
 -- C99 types which are still missing include:
--- intptr_t, uintptr_t, intmax_t, uintmax_t, wint_t, wctrans_t, wctype_t
+-- wint_t, wctrans_t, wctype_t
 
 {- $ctypes