[project @ 2001-03-01 15:59:51 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 Config
19 import FastTypes
20
21 import Dynamic
22 import IOExts
23 import Exception
24
25 import System
26 #include "HsVersions.h"
27 \end{code}
28
29 GHC's own exception type.
30
31 \begin{code}
32 ghcError :: GhcException -> a
33 ghcError e = throwDyn e
34
35 data GhcException
36   = PhaseFailed String ExitCode
37   | Interrupted
38   | UsageError String           -- prints the short usage msg after the error
39   | Panic String                -- the `impossible' happened
40   | OtherError String           -- just prints the error message
41   deriving Eq
42
43 progName = unsafePerformIO (getProgName)
44 {-# NOINLINE progName #-}
45
46 short_usage = "Usage: For basic information, try the `--help' option."
47    
48 instance Show GhcException where
49   showsPrec _ e = showString progName . showString ": " . showBarf e
50
51 showBarf (UsageError str)
52    = showString str . showChar '\n' . showString short_usage
53 showBarf (OtherError str)
54    = showString str
55 showBarf (PhaseFailed phase code)
56    = showString phase . showString " failed, code = " . shows code
57 showBarf (Interrupted)
58    = showString "interrupted"
59 showBarf (Panic s)
60    = showString ("panic! (the `impossible' happened, GHC version "
61                  ++ cProjectVersion ++ "):\n\t"
62                  ++ s ++ "\n\n"
63                  ++ "Please report it as a compiler bug "
64                  ++ "to glasgow-haskell-bugs@haskell.org.\n\n")
65
66 ghcExceptionTc = mkTyCon "GhcException"
67 {-# NOINLINE ghcExceptionTc #-}
68 instance Typeable GhcException where
69   typeOf _ = mkAppTy ghcExceptionTc []
70 \end{code}
71
72 Panics and asserts.
73
74 \begin{code}
75 panic :: String -> a
76 panic x = throwDyn (Panic x)
77
78 -- #-versions because panic can't return an unboxed int, and that's
79 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
80 -- No, man -- Too Beautiful! (Will)
81
82 panic# :: String -> FastInt
83 panic# s = case (panic s) of () -> _ILIT 0
84
85 assertPanic :: String -> Int -> a
86 assertPanic file line = 
87   throw (AssertionFailed 
88            ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
89 \end{code}