Panic.lhs 4.48 KB
Newer Older
1
%
2
% (c) The GRASP Project, Glasgow University, 1992-2000
3
4
5
6
7
8
9
10
11
%
\section{Panic error messages}

Defines basic funtions for printing error messages.

It's hard to put these functions anywhere else without causing
some unnecessary loops in the module dependency graph.

\begin{code}
12
13
14
module Panic  
   ( 
     GhcException(..), ghcError, progName, 
sof's avatar
sof committed
15
     panic, panic#, assertPanic, trace,
16
     showException, showGhcException, tryMost,
17
18
19
20

#if __GLASGOW_HASKELL__ <= 408
     catchJust, ioErrors, throwTo,
#endif
21
   ) where
22

23
24
#include "HsVersions.h"

25
import Config
26
import FastTypes
27

28
import DYNAMIC
29
import qualified EXCEPTION as Exception
30
31
import TRACE		( trace )
import UNSAFE_IO	( unsafePerformIO )
32
33

import System
34
35
\end{code}

36
37
38
39
GHC's own exception type.

\begin{code}
ghcError :: GhcException -> a
40
ghcError e = Exception.throwDyn e
41

42
43
44
45
46
47
48
49
50
-- error messages all take the form
--
--	<location>: <error>
--
-- If the location is on the command line, or in GHC itself, then 
-- <location>="ghc".  All of the error types below correspond to 
-- a <location> of "ghc", except for ProgramError (where the string is
-- assumed to contain a location already, so we don't print one).

51
data GhcException
sof's avatar
sof committed
52
53
  = PhaseFailed String		-- name of phase 
  		ExitCode	-- an external phase (eg. cpp) failed
54
  | Interrupted			-- someone pressed ^C
55
  | UsageError String		-- prints the short usage msg after the error
56
  | CmdLineError String		-- cmdline prob, but doesn't print usage
57
  | Panic String		-- the `impossible' happened
58
59
  | InstallationError String	-- an installation problem
  | ProgramError String		-- error in the user's code, probably
60
61
62
63
64
65
66
  deriving Eq

progName = unsafePerformIO (getProgName)
{-# NOINLINE progName #-}

short_usage = "Usage: For basic information, try the `--help' option."
   
67
showException :: Exception.Exception -> String
68
-- Show expected dynamic exceptions specially
69
70
71
showException (Exception.DynException d) | Just e <- fromDynamic d 
					 = show (e::GhcException)
showException other_exn	       	 	 = show other_exn
72

73
instance Show GhcException where
74
75
  showsPrec _ e@(ProgramError _) = showGhcException e
  showsPrec _ e = showString progName . showString ": " . showGhcException e
76

77
showGhcException (UsageError str)
78
   = showString str . showChar '\n' . showString short_usage
79
showGhcException (PhaseFailed phase code)
sof's avatar
sof committed
80
81
82
83
84
85
86
87
   = showString "phase `" . showString phase . 
     showString "' failed (exitcode = " . shows int_code . 
     showString ")"
  where
    int_code = 
      case code of
        ExitSuccess   -> (0::Int)
	ExitFailure x -> x
88
89
90
91
92
93
94
showGhcException (CmdLineError str)
   = showString str
showGhcException (ProgramError str)
   = showString str
showGhcException (InstallationError str)
   = showString str
showGhcException (Interrupted)
95
   = showString "interrupted"
96
showGhcException (Panic s)
97
98
   = showString ("panic! (the `impossible' happened, GHC version "
		 ++ cProjectVersion ++ "):\n\t"
99
100
	         ++ s ++ "\n\n"
	         ++ "Please report it as a compiler bug "
101
102
	         ++ "to glasgow-haskell-bugs@haskell.org,\n"
		 ++ "or http://sourceforge.net/projects/ghc/.\n\n")
103
104
105
106
107
108
109
110
111

ghcExceptionTc = mkTyCon "GhcException"
{-# NOINLINE ghcExceptionTc #-}
instance Typeable GhcException where
  typeOf _ = mkAppTy ghcExceptionTc []
\end{code}

Panics and asserts.

112
113
\begin{code}
panic :: String -> a
114
panic x = Exception.throwDyn (Panic x)
115
116
117
118
119

-- #-versions because panic can't return an unboxed int, and that's
-- what TAG_ is with GHC at the moment.  Ugh. (Simon)
-- No, man -- Too Beautiful! (Will)

120
121
panic# :: String -> FastInt
panic# s = case (panic s) of () -> _ILIT 0
122
123

assertPanic :: String -> Int -> a
124
assertPanic file line = 
125
  Exception.throw (Exception.AssertionFailed 
126
           ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
127
\end{code}
128
129
130
131
132
133

\begin{code}
-- | tryMost is like try, but passes through Interrupted and Panic
-- exceptions.  Used when we want soft failures when reading interface
-- files, for example.

134
tryMost :: IO a -> IO (Either Exception.Exception a)
135
tryMost action = do r <- myTry action; filter r
136
  where
137
   filter (Left e@(Exception.DynException d))
138
139
	    | Just ghc_ex <- fromDynamic d
		= case ghc_ex of
140
141
		    Interrupted -> Exception.throw e
		    Panic _     -> Exception.throw e
142
143
144
145
146
		    _other      -> return (Left e)
   filter other 
     = return other

#if __GLASGOW_HASKELL__ <= 408
147
myTry = Exception.tryAllIO
148
#else
149
myTry = Exception.try
150
151
152
153
154
155
156
#endif
\end{code}	

Compatibility stuff:

\begin{code}
#if __GLASGOW_HASKELL__ <= 408
sof's avatar
sof committed
157
158
159
catchJust = Exception.catchIO
ioErrors  = Exception.justIoErrors
throwTo   = Exception.raiseInThread
160
161
#endif
\end{code}