d273fe86af2e20bddd1396075237cc2f1fe03e0e
[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 ExitCode -- an external phase (eg. cpp) failed
46   | Interrupted                 -- someone pressed ^C
47   | UsageError String           -- prints the short usage msg after the error
48   | CmdLineError String         -- cmdline prob, but doesn't print usage
49   | Panic String                -- the `impossible' happened
50   | InstallationError String    -- an installation problem
51   | ProgramError String         -- error in the user's code, probably
52   deriving Eq
53
54 progName = unsafePerformIO (getProgName)
55 {-# NOINLINE progName #-}
56
57 short_usage = "Usage: For basic information, try the `--help' option."
58    
59 instance Show GhcException where
60   showsPrec _ e@(ProgramError _) = showGhcException e
61   showsPrec _ e = showString progName . showString ": " . showGhcException e
62
63 showGhcException (UsageError str)
64    = showString str . showChar '\n' . showString short_usage
65 showGhcException (PhaseFailed phase code)
66    = showString phase . showString " failed, code = " . shows code
67 showGhcException (CmdLineError str)
68    = showString str
69 showGhcException (ProgramError str)
70    = showString str
71 showGhcException (InstallationError str)
72    = showString str
73 showGhcException (Interrupted)
74    = showString "interrupted"
75 showGhcException (Panic s)
76    = showString ("panic! (the `impossible' happened, GHC version "
77                  ++ cProjectVersion ++ "):\n\t"
78                  ++ s ++ "\n\n"
79                  ++ "Please report it as a compiler bug "
80                  ++ "to glasgow-haskell-bugs@haskell.org,\n"
81                  ++ "or http://sourceforge.net/projects/ghc/.\n\n")
82
83 ghcExceptionTc = mkTyCon "GhcException"
84 {-# NOINLINE ghcExceptionTc #-}
85 instance Typeable GhcException where
86   typeOf _ = mkAppTy ghcExceptionTc []
87 \end{code}
88
89 Panics and asserts.
90
91 \begin{code}
92 panic :: String -> a
93 panic x = throwDyn (Panic x)
94
95 -- #-versions because panic can't return an unboxed int, and that's
96 -- what TAG_ is with GHC at the moment.  Ugh. (Simon)
97 -- No, man -- Too Beautiful! (Will)
98
99 panic# :: String -> FastInt
100 panic# s = case (panic s) of () -> _ILIT 0
101
102 assertPanic :: String -> Int -> a
103 assertPanic file line = 
104   throw (AssertionFailed 
105            ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
106 \end{code}