[project @ 2002-10-15 11:01:29 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / Panic.lhs
index fd6839b..01d1568 100644 (file)
@@ -13,7 +13,11 @@ module Panic
    ( 
      GhcException(..), ghcError, progName, 
      panic, panic#, assertPanic, trace,
-     showGhcException
+     showException, showGhcException, tryMost,
+
+#if __GLASGOW_HASKELL__ <= 408
+     catchJust, ioErrors, throwTo,
+#endif
    ) where
 
 #include "HsVersions.h"
@@ -22,7 +26,7 @@ import Config
 import FastTypes
 
 import DYNAMIC
-import EXCEPTION
+import qualified EXCEPTION as Exception
 import TRACE           ( trace )
 import UNSAFE_IO       ( unsafePerformIO )
 
@@ -33,7 +37,7 @@ GHC's own exception type.
 
 \begin{code}
 ghcError :: GhcException -> a
-ghcError e = throwDyn e
+ghcError e = Exception.throwDyn e
 
 -- error messages all take the form
 --
@@ -60,6 +64,12 @@ progName = unsafePerformIO (getProgName)
 
 short_usage = "Usage: For basic information, try the `--help' option."
    
+showException :: Exception.Exception -> String
+-- Show expected dynamic exceptions specially
+showException (Exception.DynException d) | Just e <- fromDynamic d 
+                                        = show (e::GhcException)
+showException other_exn                         = show other_exn
+
 instance Show GhcException where
   showsPrec _ e@(ProgramError _) = showGhcException e
   showsPrec _ e = showString progName . showString ": " . showGhcException e
@@ -101,7 +111,7 @@ Panics and asserts.
 
 \begin{code}
 panic :: String -> a
-panic x = throwDyn (Panic x)
+panic x = Exception.throwDyn (Panic x)
 
 -- #-versions because panic can't return an unboxed int, and that's
 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
@@ -112,6 +122,40 @@ panic# s = case (panic s) of () -> _ILIT 0
 
 assertPanic :: String -> Int -> a
 assertPanic file line = 
-  throw (AssertionFailed 
+  Exception.throw (Exception.AssertionFailed 
            ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
 \end{code}
+
+\begin{code}
+-- | tryMost is like try, but passes through Interrupted and Panic
+-- exceptions.  Used when we want soft failures when reading interface
+-- files, for example.
+
+tryMost :: IO a -> IO (Either Exception.Exception a)
+tryMost action = do r <- myTry action; filter r
+  where
+   filter (Left e@(Exception.DynException d))
+           | Just ghc_ex <- fromDynamic d
+               = case ghc_ex of
+                   Interrupted -> Exception.throw e
+                   Panic _     -> Exception.throw e
+                   _other      -> return (Left e)
+   filter other 
+     = return other
+
+#if __GLASGOW_HASKELL__ <= 408
+myTry = Exception.tryAllIO
+#else
+myTry = Exception.try
+#endif
+\end{code}     
+
+Compatibility stuff:
+
+\begin{code}
+#if __GLASGOW_HASKELL__ <= 408
+catchJust = Exception.catchIO
+ioErrors  = Exception.justIoErrors
+throwTo   = Exception.raiseInThread
+#endif
+\end{code}