{-# LANGUAGE Unsafe #-}
{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, RoleAnnotations #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.Ptr (
Ptr(..), FunPtr(..),
nullPtr, castPtr, plusPtr, alignPtr, minusPtr,
nullFunPtr, castFunPtr,
castFunPtrToPtr, castPtrToFunPtr,
) where
import GHC.Base
import GHC.Show
import GHC.Num
import GHC.List ( length, replicate )
import Numeric ( showHex )
#include "MachDeps.h"
type role Ptr phantom
data Ptr a = Ptr Addr#
deriving ( Ptr a -> Ptr a -> Bool
(Ptr a -> Ptr a -> Bool) -> (Ptr a -> Ptr a -> Bool) -> Eq (Ptr a)
forall a. Ptr a -> Ptr a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Ptr a -> Ptr a -> Bool
== :: Ptr a -> Ptr a -> Bool
$c/= :: forall a. Ptr a -> Ptr a -> Bool
/= :: Ptr a -> Ptr a -> Bool
Eq
, Eq (Ptr a)
Eq (Ptr a) =>
(Ptr a -> Ptr a -> Ordering)
-> (Ptr a -> Ptr a -> Bool)
-> (Ptr a -> Ptr a -> Bool)
-> (Ptr a -> Ptr a -> Bool)
-> (Ptr a -> Ptr a -> Bool)
-> (Ptr a -> Ptr a -> Ptr a)
-> (Ptr a -> Ptr a -> Ptr a)
-> Ord (Ptr a)
Ptr a -> Ptr a -> Bool
Ptr a -> Ptr a -> Ordering
Ptr a -> Ptr a -> Ptr a
forall a. Eq (Ptr a)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ptr a -> Ptr a -> Bool
forall a. Ptr a -> Ptr a -> Ordering
forall a. Ptr a -> Ptr a -> Ptr a
$ccompare :: forall a. Ptr a -> Ptr a -> Ordering
compare :: Ptr a -> Ptr a -> Ordering
$c< :: forall a. Ptr a -> Ptr a -> Bool
< :: Ptr a -> Ptr a -> Bool
$c<= :: forall a. Ptr a -> Ptr a -> Bool
<= :: Ptr a -> Ptr a -> Bool
$c> :: forall a. Ptr a -> Ptr a -> Bool
> :: Ptr a -> Ptr a -> Bool
$c>= :: forall a. Ptr a -> Ptr a -> Bool
>= :: Ptr a -> Ptr a -> Bool
$cmax :: forall a. Ptr a -> Ptr a -> Ptr a
max :: Ptr a -> Ptr a -> Ptr a
$cmin :: forall a. Ptr a -> Ptr a -> Ptr a
min :: Ptr a -> Ptr a -> Ptr a
Ord
)
nullPtr :: Ptr a
nullPtr :: forall a. Ptr a
nullPtr = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
nullAddr#
castPtr :: Ptr a -> Ptr b
castPtr :: forall a b. Ptr a -> Ptr b
castPtr = Ptr a -> Ptr b
forall a b. Coercible a b => a -> b
coerce
plusPtr :: Ptr a -> Int -> Ptr b
plusPtr :: forall a b. Ptr a -> Int -> Ptr b
plusPtr (Ptr Addr#
addr) (I# Int#
d) = Addr# -> Ptr b
forall a. Addr# -> Ptr a
Ptr (Addr# -> Int# -> Addr#
plusAddr# Addr#
addr Int#
d)
alignPtr :: Ptr a -> Int -> Ptr a
alignPtr :: forall a. Ptr a -> Int -> Ptr a
alignPtr addr :: Ptr a
addr@(Ptr Addr#
a) (I# Int#
i)
= case Addr# -> Int# -> Int#
remAddr# Addr#
a Int#
i of {
Int#
0# -> Ptr a
addr;
Int#
n -> Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr (Addr# -> Int# -> Addr#
plusAddr# Addr#
a (Int#
i Int# -> Int# -> Int#
-# Int#
n)) }
minusPtr :: Ptr a -> Ptr b -> Int
minusPtr :: forall a b. Ptr a -> Ptr b -> Int
minusPtr (Ptr Addr#
a1) (Ptr Addr#
a2) = Int# -> Int
I# (Addr# -> Addr# -> Int#
minusAddr# Addr#
a1 Addr#
a2)
type role FunPtr phantom
data FunPtr a = FunPtr Addr# deriving (FunPtr a -> FunPtr a -> Bool
(FunPtr a -> FunPtr a -> Bool)
-> (FunPtr a -> FunPtr a -> Bool) -> Eq (FunPtr a)
forall a. FunPtr a -> FunPtr a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. FunPtr a -> FunPtr a -> Bool
== :: FunPtr a -> FunPtr a -> Bool
$c/= :: forall a. FunPtr a -> FunPtr a -> Bool
/= :: FunPtr a -> FunPtr a -> Bool
Eq, Eq (FunPtr a)
Eq (FunPtr a) =>
(FunPtr a -> FunPtr a -> Ordering)
-> (FunPtr a -> FunPtr a -> Bool)
-> (FunPtr a -> FunPtr a -> Bool)
-> (FunPtr a -> FunPtr a -> Bool)
-> (FunPtr a -> FunPtr a -> Bool)
-> (FunPtr a -> FunPtr a -> FunPtr a)
-> (FunPtr a -> FunPtr a -> FunPtr a)
-> Ord (FunPtr a)
FunPtr a -> FunPtr a -> Bool
FunPtr a -> FunPtr a -> Ordering
FunPtr a -> FunPtr a -> FunPtr a
forall a. Eq (FunPtr a)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. FunPtr a -> FunPtr a -> Bool
forall a. FunPtr a -> FunPtr a -> Ordering
forall a. FunPtr a -> FunPtr a -> FunPtr a
$ccompare :: forall a. FunPtr a -> FunPtr a -> Ordering
compare :: FunPtr a -> FunPtr a -> Ordering
$c< :: forall a. FunPtr a -> FunPtr a -> Bool
< :: FunPtr a -> FunPtr a -> Bool
$c<= :: forall a. FunPtr a -> FunPtr a -> Bool
<= :: FunPtr a -> FunPtr a -> Bool
$c> :: forall a. FunPtr a -> FunPtr a -> Bool
> :: FunPtr a -> FunPtr a -> Bool
$c>= :: forall a. FunPtr a -> FunPtr a -> Bool
>= :: FunPtr a -> FunPtr a -> Bool
$cmax :: forall a. FunPtr a -> FunPtr a -> FunPtr a
max :: FunPtr a -> FunPtr a -> FunPtr a
$cmin :: forall a. FunPtr a -> FunPtr a -> FunPtr a
min :: FunPtr a -> FunPtr a -> FunPtr a
Ord)
nullFunPtr :: FunPtr a
nullFunPtr :: forall a. FunPtr a
nullFunPtr = Addr# -> FunPtr a
forall a. Addr# -> FunPtr a
FunPtr Addr#
nullAddr#
castFunPtr :: FunPtr a -> FunPtr b
castFunPtr :: forall a b. FunPtr a -> FunPtr b
castFunPtr = FunPtr a -> FunPtr b
forall a b. Coercible a b => a -> b
coerce
castFunPtrToPtr :: FunPtr a -> Ptr b
castFunPtrToPtr :: forall a b. FunPtr a -> Ptr b
castFunPtrToPtr (FunPtr Addr#
addr) = Addr# -> Ptr b
forall a. Addr# -> Ptr a
Ptr Addr#
addr
castPtrToFunPtr :: Ptr a -> FunPtr b
castPtrToFunPtr :: forall a b. Ptr a -> FunPtr b
castPtrToFunPtr (Ptr Addr#
addr) = Addr# -> FunPtr b
forall a. Addr# -> FunPtr a
FunPtr Addr#
addr
instance Show (Ptr a) where
showsPrec :: Int -> Ptr a -> ShowS
showsPrec Int
_ (Ptr Addr#
a) String
rs = ShowS
pad_out (Integer -> ShowS
forall a. Integral a => a -> ShowS
showHex (Word# -> Integer
integerFromWord#(Int# -> Word#
int2Word#(Addr# -> Int#
addr2Int# Addr#
a))) String
"")
where
pad_out :: ShowS
pad_out String
ls =
Char
'0'Char -> ShowS
forall a. a -> [a] -> [a]
:Char
'x'Char -> ShowS
forall a. a -> [a] -> [a]
:(Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*SIZEOF_HSPTR - length ls) '0') ++ ls ++ rs
instance Show (FunPtr a) where
showsPrec :: Int -> FunPtr a -> ShowS
showsPrec Int
p = Int -> Ptr Any -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p (Ptr Any -> ShowS) -> (FunPtr a -> Ptr Any) -> FunPtr a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FunPtr a -> Ptr Any
forall a b. FunPtr a -> Ptr b
castFunPtrToPtr