[project @ 2000-10-13 10:26:38 by sewardj]
[ghc-hetmet.git] / ghc / compiler / main / ErrUtils.lhs
index d455ff0..d6a64f3 100644 (file)
 %
-% (c) The AQUA Project, Glasgow University, 1994-1995
+% (c) The AQUA Project, Glasgow University, 1994-1998
 %
 \section[ErrsUtils]{Utilities for error reporting}
 
 \begin{code}
-#include "HsVersions.h"
-
 module ErrUtils (
-
-       Error(..),
-       addErrLoc, addShortErrLocLine,
-       dontAddErrLoc, pprBagOfErrors,
-
-       TcError(..), TcWarning(..), Message(..),
-       mkTcErr, arityErr
-
+       ErrMsg, WarnMsg, Message,
+       addShortErrLocLine, addShortWarnLocLine,
+       addErrLocHdrLine,
+       dontAddErrLoc,
+       printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
+       ghcExit,
+       doIfSet, dumpIfSet
     ) where
 
-import Ubiq{-uitous-}
+#include "HsVersions.h"
 
-import Bag             ( bagToList )
-import PprStyle                ( PprStyle(..) )
-import Pretty
-import SrcLoc          ( mkUnknownSrcLoc, SrcLoc{-instance-} )
-\end{code}
+import Bag             ( Bag, bagToList, isEmptyBag )
+import SrcLoc          ( SrcLoc, noSrcLoc )
+import Util            ( sortLt )
+import Outputable
+import CmdLineOpts     ( DynFlags )
 
-\begin{code}
-type Error   = PprStyle -> Pretty
-
-addErrLoc :: SrcLoc -> String -> Error -> Error
-addErrLoc locn title rest_of_err_msg sty
-  = ppHang (ppBesides [ppr PprForUser locn,
-                      if null title then ppNil else ppStr (": " ++ title),
-                      ppChar ':'])
-        4 (rest_of_err_msg sty)
-
-addShortErrLocLine :: SrcLoc -> Error -> Error
-addShortErrLocLine locn rest_of_err_msg sty
-  = ppHang (ppBeside (ppr PprForUser locn) (ppChar ':'))
-        4 (rest_of_err_msg sty)
-
-dontAddErrLoc :: String -> Error -> Error
-dontAddErrLoc title rest_of_err_msg sty
-  = ppHang (ppBesides [ppStr title, ppChar ':'])
-        4 (rest_of_err_msg sty)
-
-pprBagOfErrors :: PprStyle -> Bag Error -> Pretty
-pprBagOfErrors sty bag_of_errors
-  = let  pretties = map ( \ e -> e sty ) (bagToList bag_of_errors)  in
-    ppAboves (map (\ p -> ppAbove ppSP p) pretties)
+import System          ( ExitCode(..), exitWith )
+import IO              ( hPutStr, stderr )
 \end{code}
 
-TypeChecking Errors
-~~~~~~~~~~~~~~~~~~~
-
 \begin{code}
-type Message   = PprStyle -> Pretty
-type TcError   = Message
-type TcWarning = Message
+type MsgWithLoc = (SrcLoc, SDoc)
+
+type ErrMsg  = MsgWithLoc
+type WarnMsg = MsgWithLoc
+type Message = SDoc
+
+addShortErrLocLine  :: SrcLoc -> Message -> ErrMsg
+addErrLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
+addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
+
+addShortErrLocLine locn rest_of_err_msg
+  = ( locn
+    , hang (ppr locn <> colon) 
+         4 rest_of_err_msg
+    )
+
+addErrLocHdrLine locn hdr rest_of_err_msg
+  = ( locn
+    , hang (ppr locn <> colon<+> hdr) 
+         4 rest_of_err_msg
+    )
+
+addShortWarnLocLine locn rest_of_err_msg
+  = ( locn
+    , hang (ppr locn <> colon)
+        4 (ptext SLIT("Warning:") <+> rest_of_err_msg)
+    )
+
+dontAddErrLoc :: String -> Message -> ErrMsg
+dontAddErrLoc title rest_of_err_msg
+ | null title = (noSrcLoc, rest_of_err_msg)
+ | otherwise  =
+    ( noSrcLoc, hang (text title <> colon) 4 rest_of_err_msg )
+
+printErrorsAndWarnings :: Bag ErrMsg -> Bag WarnMsg -> IO ()
+       -- Don't print any warnings if there are errors
+printErrorsAndWarnings errs warns
+  | no_errs && no_warns  = return ()
+  | no_errs             = printErrs (pprBagOfWarnings warns)
+  | otherwise           = printErrs (pprBagOfErrors   errs)
+  where
+    no_warns = isEmptyBag warns
+    no_errs  = isEmptyBag errs
+
+pprBagOfErrors :: Bag ErrMsg -> SDoc
+pprBagOfErrors bag_of_errors
+  = vcat [text "" $$ p | (_,p) <- sorted_errs ]
+    where
+      bag_ls     = bagToList bag_of_errors
+      sorted_errs = sortLt occ'ed_before bag_ls
 
+      occ'ed_before (a,_) (b,_) = LT == compare a b
 
-mkTcErr :: SrcLoc              -- Where
-       -> [Message]            -- Context
-       -> Message              -- What went wrong
-       -> TcError              -- The complete error report
+pprBagOfWarnings :: Bag WarnMsg -> SDoc
+pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
+\end{code}
 
-mkTcErr locn ctxt msg sty
-  = ppHang (ppBesides [ppr PprForUser locn, ppStr ": ", msg sty])
-        4 (ppAboves [msg sty | msg <- ctxt])
+\begin{code}
+ghcExit :: Int -> IO ()
+ghcExit val
+  | val == 0  = exitWith ExitSuccess
+  | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
+                  exitWith (ExitFailure val)
+\end{code}
 
+\begin{code}
+doIfSet :: Bool -> IO () -> IO ()
+doIfSet flag action | flag      = action
+                   | otherwise = return ()
+\end{code}
 
-arityErr kind name n m sty =
-    ppBesides [ ppStr "`", ppr sty name, ppStr "' should have ",
-               n_arguments, ppStr ", but has been given ", ppInt m, ppChar '.']
-    where
-       errmsg = kind ++ " has too " ++ quantity ++ " arguments"
-       quantity | m < n     = "few"
-                | otherwise = "many"
-       n_arguments | n == 0 = ppStr "no arguments"
-                   | n == 1 = ppStr "1 argument"
-                   | True   = ppCat [ppInt n, ppStr "arguments"]
+\begin{code}
+dumpIfSet :: DynFlags -> (DynFlags -> Bool) -> String -> SDoc -> IO ()
+dumpIfSet dflags flag hdr doc
+  | not (flag dflags)  = return ()
+  | otherwise          = printDump dump
+  where
+    dump = vcat [text "", 
+                line <+> text hdr <+> line,
+                doc,
+                text ""]
+    line = text (take 20 (repeat '='))
 \end{code}