[project @ 2000-12-12 14:35:08 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / Panic.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-2000
3 %
4 \section{Panic error messages}
5
6 Defines basic funtions for printing error messages.
7
8 It's hard to put these functions anywhere else without causing
9 some unnecessary loops in the module dependency graph.
10
11 \begin{code}
12 module Panic  
13    ( 
14      GhcException(..), ghcError, progName, 
15      panic, panic#, assertPanic, trace 
16    ) where
17
18 import FastTypes
19
20 import Dynamic
21 import IOExts
22 import Exception
23
24 import System
25 #include "HsVersions.h"
26 \end{code}
27
28 GHC's own exception type.
29
30 \begin{code}
31 ghcError :: GhcException -> a
32 ghcError e = throwDyn e
33
34 data GhcException
35   = PhaseFailed String ExitCode
36   | Interrupted
37   | UsageError String           -- prints the short usage msg after the error
38   | Panic String                -- the `impossible' happened
39   | OtherError String           -- just prints the error message
40   deriving Eq
41
42 progName = unsafePerformIO (getProgName)
43 {-# NOINLINE progName #-}
44
45 short_usage = "Usage: For basic information, try the `--help' option."
46    
47 instance Show GhcException where
48   showsPrec _ e = showString progName . showString ": " . showBarf e
49
50 showBarf (UsageError str)
51    = showString str . showChar '\n' . showString short_usage
52 showBarf (OtherError str)
53    = showString str
54 showBarf (PhaseFailed phase code)
55    = showString phase . showString " failed, code = " . shows code
56 showBarf (Interrupted)
57    = showString "interrupted"
58 showBarf (Panic s)
59    = showString ("panic! (the `impossible' happened):\n\t"
60                  ++ s ++ "\n\n"
61                  ++ "Please report it as a compiler bug "
62                  ++ "to glasgow-haskell-bugs@haskell.org.\n\n")
63
64 ghcExceptionTc = mkTyCon "GhcException"
65 {-# NOINLINE ghcExceptionTc #-}
66 instance Typeable GhcException where
67   typeOf _ = mkAppTy ghcExceptionTc []
68 \end{code}
69
70 Panics and asserts.
71
72 \begin{code}
73 panic :: String -> a
74 panic x = throwDyn (Panic x)
75
76 -- #-versions because panic can't return an unboxed int, and that's
77 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
78 -- No, man -- Too Beautiful! (Will)
79
80 panic# :: String -> FastInt
81 panic# s = case (panic s) of () -> _ILIT 0
82
83 assertPanic :: String -> Int -> a
84 assertPanic file line = 
85   throw (AssertionFailed 
86            ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
87 \end{code}