[project @ 2002-08-29 15:44:11 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      showGhcException
17    ) where
18
19 #include "HsVersions.h"
20
21 import Config
22 import FastTypes
23
24 import DYNAMIC
25 import EXCEPTION
26 import TRACE            ( trace )
27 import UNSAFE_IO        ( unsafePerformIO )
28
29 import System
30 \end{code}
31
32 GHC's own exception type.
33
34 \begin{code}
35 ghcError :: GhcException -> a
36 ghcError e = throwDyn e
37
38 -- error messages all take the form
39 --
40 --      <location>: <error>
41 --
42 -- If the location is on the command line, or in GHC itself, then 
43 -- <location>="ghc".  All of the error types below correspond to 
44 -- a <location> of "ghc", except for ProgramError (where the string is
45 -- assumed to contain a location already, so we don't print one).
46
47 data GhcException
48   = PhaseFailed String          -- name of phase 
49                 ExitCode        -- an external phase (eg. cpp) failed
50   | Interrupted                 -- someone pressed ^C
51   | UsageError String           -- prints the short usage msg after the error
52   | CmdLineError String         -- cmdline prob, but doesn't print usage
53   | Panic String                -- the `impossible' happened
54   | InstallationError String    -- an installation problem
55   | ProgramError String         -- error in the user's code, probably
56   deriving Eq
57
58 progName = unsafePerformIO (getProgName)
59 {-# NOINLINE progName #-}
60
61 short_usage = "Usage: For basic information, try the `--help' option."
62    
63 instance Show GhcException where
64   showsPrec _ e@(ProgramError _) = showGhcException e
65   showsPrec _ e = showString progName . showString ": " . showGhcException e
66
67 showGhcException (UsageError str)
68    = showString str . showChar '\n' . showString short_usage
69 showGhcException (PhaseFailed phase code)
70    = showString "phase `" . showString phase . 
71      showString "' failed (exitcode = " . shows int_code . 
72      showString ")"
73   where
74     int_code = 
75       case code of
76         ExitSuccess   -> (0::Int)
77         ExitFailure x -> x
78 showGhcException (CmdLineError str)
79    = showString str
80 showGhcException (ProgramError str)
81    = showString str
82 showGhcException (InstallationError str)
83    = showString str
84 showGhcException (Interrupted)
85    = showString "interrupted"
86 showGhcException (Panic s)
87    = showString ("panic! (the `impossible' happened, GHC version "
88                  ++ cProjectVersion ++ "):\n\t"
89                  ++ s ++ "\n\n"
90                  ++ "Please report it as a compiler bug "
91                  ++ "to glasgow-haskell-bugs@haskell.org,\n"
92                  ++ "or http://sourceforge.net/projects/ghc/.\n\n")
93
94 ghcExceptionTc = mkTyCon "GhcException"
95 {-# NOINLINE ghcExceptionTc #-}
96 instance Typeable GhcException where
97   typeOf _ = mkAppTy ghcExceptionTc []
98 \end{code}
99
100 Panics and asserts.
101
102 \begin{code}
103 panic :: String -> a
104 panic x = throwDyn (Panic x)
105
106 -- #-versions because panic can't return an unboxed int, and that's
107 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
108 -- No, man -- Too Beautiful! (Will)
109
110 panic# :: String -> FastInt
111 panic# s = case (panic s) of () -> _ILIT 0
112
113 assertPanic :: String -> Int -> a
114 assertPanic file line = 
115   throw (AssertionFailed 
116            ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
117 \end{code}