{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
module GHC.Internal.Stack (
errorWithStackTrace,
currentCallStack,
whoCreated,
CallStack, HasCallStack, callStack, emptyCallStack, freezeCallStack,
fromCallSiteList, getCallStack, popCallStack,
pushCallStack, withFrozenCallStack,
prettyCallStackLines, prettyCallStack,
SrcLoc(..), prettySrcLoc,
CostCentreStack,
CostCentre,
getCurrentCCS,
getCCSOf,
clearCCS,
ccsCC,
ccsParent,
ccLabel,
ccModule,
ccSrcSpan,
ccsToStrings,
renderStack
) where
import GHC.Internal.Show
import GHC.Internal.Stack.CCS
import GHC.Internal.Stack.Types
import GHC.Internal.IO
import GHC.Internal.Base
import GHC.Internal.List
import GHC.Internal.Data.OldList (intercalate)
import GHC.Internal.Exception
{-# DEPRECATED errorWithStackTrace "'error' appends the call stack now" #-}
errorWithStackTrace :: String -> a
errorWithStackTrace :: forall a. String -> a
errorWithStackTrace String
x = IO a -> a
forall a. IO a -> a
unsafeDupablePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
stack <- Ptr CostCentreStack -> IO [String]
ccsToStrings (Ptr CostCentreStack -> IO [String])
-> IO (Ptr CostCentreStack) -> IO [String]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> IO (Ptr CostCentreStack)
forall dummy. dummy -> IO (Ptr CostCentreStack)
getCurrentCCS String
x
if null stack
then throwIO (ErrorCall x)
else throwIO (ErrorCallWithLocation x (renderStack stack))
popCallStack :: CallStack -> CallStack
popCallStack :: CallStack -> CallStack
popCallStack CallStack
stk = case CallStack
stk of
CallStack
EmptyCallStack -> String -> CallStack
forall a. String -> a
errorWithoutStackTrace String
"popCallStack: empty stack"
PushCallStack String
_ SrcLoc
_ CallStack
stk' -> CallStack
stk'
FreezeCallStack CallStack
_ -> CallStack
stk
{-# INLINE popCallStack #-}
callStack :: HasCallStack => CallStack
callStack :: HasCallStack => CallStack
callStack =
case HasCallStack
CallStack
?callStack of
CallStack
EmptyCallStack -> CallStack
EmptyCallStack
CallStack
_ -> CallStack -> CallStack
popCallStack HasCallStack
CallStack
?callStack
{-# INLINE callStack #-}
withFrozenCallStack :: HasCallStack
=> ( HasCallStack => a )
-> a
withFrozenCallStack :: forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack HasCallStack => a
do_this =
let ?callStack = CallStack -> CallStack
freezeCallStack (CallStack -> CallStack
popCallStack CallStack
HasCallStack => CallStack
callStack)
in a
HasCallStack => a
do_this
prettySrcLoc :: SrcLoc -> String
prettySrcLoc :: SrcLoc -> String
prettySrcLoc SrcLoc {Int
String
srcLocPackage :: String
srcLocModule :: String
srcLocFile :: String
srcLocStartLine :: Int
srcLocStartCol :: Int
srcLocEndLine :: Int
srcLocEndCol :: Int
srcLocEndCol :: SrcLoc -> Int
srcLocEndLine :: SrcLoc -> Int
srcLocStartCol :: SrcLoc -> Int
srcLocStartLine :: SrcLoc -> Int
srcLocFile :: SrcLoc -> String
srcLocModule :: SrcLoc -> String
srcLocPackage :: SrcLoc -> String
..}
= (String -> String -> String) -> String -> [String] -> String
forall a b. (a -> b -> b) -> b -> [a] -> b
foldr String -> String -> String
forall a. [a] -> [a] -> [a]
(++) String
""
[ String
srcLocFile, String
":"
, Int -> String
forall a. Show a => a -> String
show Int
srcLocStartLine, String
":"
, Int -> String
forall a. Show a => a -> String
show Int
srcLocStartCol, String
" in "
, String
srcLocPackage, String
":", String
srcLocModule
]
prettyCallStack :: CallStack -> String
prettyCallStack :: CallStack -> String
prettyCallStack = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String)
-> (CallStack -> [String]) -> CallStack -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CallStack -> [String]
prettyCallStackLines
prettyCallStackLines :: CallStack -> [String]
prettyCallStackLines :: CallStack -> [String]
prettyCallStackLines CallStack
cs = case CallStack -> [(String, SrcLoc)]
getCallStack CallStack
cs of
[] -> []
[(String, SrcLoc)]
stk -> String
"CallStack (from HasCallStack):"
String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ((String, SrcLoc) -> String) -> [(String, SrcLoc)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++) (String -> String)
-> ((String, SrcLoc) -> String) -> (String, SrcLoc) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, SrcLoc) -> String
prettyCallSite) [(String, SrcLoc)]
stk
where
prettyCallSite :: (String, SrcLoc) -> String
prettyCallSite (String
f, SrcLoc
loc) = String
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", called at " String -> String -> String
forall a. [a] -> [a] -> [a]
++ SrcLoc -> String
prettySrcLoc SrcLoc
loc