2 % (c) The GRASP Project, Glasgow University, 1992-2000
4 \section{Panic error messages}
6 Defines basic funtions for printing error messages.
8 It's hard to put these functions anywhere else without causing
9 some unnecessary loops in the module dependency graph.
14 GhcException(..), ghcError, progName,
15 panic, panic#, assertPanic, trace,
16 showException, showGhcException, tryMost,
17 installSignalHandlers,
19 catchJust, tryJust, ioErrors, throwTo,
22 #include "HsVersions.h"
28 #ifndef mingw32_HOST_OS
29 # if __GLASGOW_HASKELL__ > 504
30 import System.Posix.Signals
32 import Posix ( Handler(Catch), installHandler, sigINT, sigQUIT )
33 # endif /* GHC > 504 */
35 import CONCURRENT ( myThreadId )
37 # if __GLASGOW_HASKELL__ < 500
38 import EXCEPTION ( raiseInThread )
40 import EXCEPTION ( throwTo )
41 # endif /* GHC < 500 */
42 #endif /* mingw32_HOST_OS */
44 #if __GLASGOW_HASKELL__ > 408
45 import EXCEPTION ( catchJust, tryJust, ioErrors )
49 import qualified EXCEPTION as Exception
50 import TRACE ( trace )
51 import UNSAFE_IO ( unsafePerformIO )
56 GHC's own exception type.
59 ghcError :: GhcException -> a
60 ghcError e = Exception.throwDyn e
62 -- error messages all take the form
64 -- <location>: <error>
66 -- If the location is on the command line, or in GHC itself, then
67 -- <location>="ghc". All of the error types below correspond to
68 -- a <location> of "ghc", except for ProgramError (where the string is
69 -- assumed to contain a location already, so we don't print one).
72 = PhaseFailed String -- name of phase
73 ExitCode -- an external phase (eg. cpp) failed
74 | Interrupted -- someone pressed ^C
75 | UsageError String -- prints the short usage msg after the error
76 | CmdLineError String -- cmdline prob, but doesn't print usage
77 | Panic String -- the `impossible' happened
78 | InstallationError String -- an installation problem
79 | ProgramError String -- error in the user's code, probably
82 progName = unsafePerformIO (getProgName)
83 {-# NOINLINE progName #-}
85 short_usage = "Usage: For basic information, try the `--help' option."
87 showException :: Exception.Exception -> String
88 -- Show expected dynamic exceptions specially
89 showException (Exception.DynException d) | Just e <- fromDynamic d
90 = show (e::GhcException)
91 showException other_exn = show other_exn
93 instance Show GhcException where
94 showsPrec _ e@(ProgramError _) = showGhcException e
95 showsPrec _ e = showString progName . showString ": " . showGhcException e
97 showGhcException (UsageError str)
98 = showString str . showChar '\n' . showString short_usage
99 showGhcException (PhaseFailed phase code)
100 = showString "phase `" . showString phase .
101 showString "' failed (exitcode = " . shows int_code .
106 ExitSuccess -> (0::Int)
108 showGhcException (CmdLineError str)
110 showGhcException (ProgramError str)
112 showGhcException (InstallationError str)
114 showGhcException (Interrupted)
115 = showString "interrupted"
116 showGhcException (Panic s)
117 = showString ("panic! (the `impossible' happened, GHC version "
118 ++ cProjectVersion ++ "):\n\t"
120 ++ "Please report it as a compiler bug "
121 ++ "to glasgow-haskell-bugs@haskell.org,\n"
122 ++ "or http://sourceforge.net/projects/ghc/.\n\n")
124 ghcExceptionTc = mkTyCon "GhcException"
125 {-# NOINLINE ghcExceptionTc #-}
126 instance Typeable GhcException where
127 typeOf _ = mkAppTy ghcExceptionTc []
134 panic x = Exception.throwDyn (Panic x)
136 -- #-versions because panic can't return an unboxed int, and that's
137 -- what TAG_ is with GHC at the moment. Ugh. (Simon)
138 -- No, man -- Too Beautiful! (Will)
140 panic# :: String -> FastInt
141 panic# s = case (panic s) of () -> _ILIT 0
143 assertPanic :: String -> Int -> a
144 assertPanic file line =
145 Exception.throw (Exception.AssertionFailed
146 ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
150 -- | tryMost is like try, but passes through Interrupted and Panic
151 -- exceptions. Used when we want soft failures when reading interface
152 -- files, for example.
154 tryMost :: IO a -> IO (Either Exception.Exception a)
155 tryMost action = do r <- myTry action; filter r
157 filter (Left e@(Exception.DynException d))
158 | Just ghc_ex <- fromDynamic d
160 Interrupted -> Exception.throw e
161 Panic _ -> Exception.throw e
162 _other -> return (Left e)
166 #if __GLASGOW_HASKELL__ <= 408
167 myTry = Exception.tryAllIO
169 myTry = Exception.try
176 #if __GLASGOW_HASKELL__ <= 408
177 catchJust = Exception.catchIO
178 tryJust = Exception.tryIO
179 ioErrors = Exception.justIoErrors
180 throwTo = Exception.raiseInThread
184 Standard signal handlers for catching ^C, which just throw an
185 exception in the main thread. NOTE: must be called from the main
189 installSignalHandlers :: IO ()
190 installSignalHandlers = do
191 #ifndef mingw32_HOST_OS
192 main_thread <- myThreadId
193 let sig_handler = Catch (throwTo main_thread
194 (Exception.DynException (toDyn Interrupted)))
195 installHandler sigQUIT sig_handler Nothing
196 installHandler sigINT sig_handler Nothing