Panic.lhs 3.03 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
15
16
module Panic  
   ( 
     GhcException(..), ghcError, progName, 
     panic, panic#, assertPanic, trace 
   ) where
17

18
import Config
19
import FastTypes
20

21
22
23
24
25
import Dynamic
import IOExts
import Exception

import System
26
27
28
#include "HsVersions.h"
\end{code}

29
30
31
32
33
34
GHC's own exception type.

\begin{code}
ghcError :: GhcException -> a
ghcError e = throwDyn e

35
36
37
38
39
40
41
42
43
-- 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).

44
data GhcException
45
46
  = PhaseFailed String ExitCode	-- an external phase (eg. cpp) failed
  | 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
51
  | InstallationError String	-- an installation problem
  | ProgramError String		-- error in the user's code, probably
52
53
54
55
56
57
58
59
  deriving Eq

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

short_usage = "Usage: For basic information, try the `--help' option."
   
instance Show GhcException where
60
61
  showsPrec _ e@(ProgramError _) = showGhcException e
  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
68
69
70
71
72
73
showGhcException (CmdLineError str)
   = showString str
showGhcException (ProgramError str)
   = showString str
showGhcException (InstallationError str)
   = showString str
showGhcException (Interrupted)
74
   = showString "interrupted"
75
showGhcException (Panic s)
76
77
   = showString ("panic! (the `impossible' happened, GHC version "
		 ++ cProjectVersion ++ "):\n\t"
78
79
	         ++ s ++ "\n\n"
	         ++ "Please report it as a compiler bug "
80
81
	         ++ "to glasgow-haskell-bugs@haskell.org,\n"
		 ++ "or http://sourceforge.net/projects/ghc/.\n\n")
82
83
84
85
86
87
88
89
90

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

Panics and asserts.

91
92
\begin{code}
panic :: String -> a
93
panic x = throwDyn (Panic x)
94
95
96
97
98

-- #-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)

99
100
panic# :: String -> FastInt
panic# s = case (panic s) of () -> _ILIT 0
101
102

assertPanic :: String -> Int -> a
103
104
105
assertPanic file line = 
  throw (AssertionFailed 
           ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
106
\end{code}