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