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