import GHC ( Session, verbosity, dopt, DynFlag(..),
mkModule, pprModule, Type, Module, SuccessFlag(..),
TyThing(..), Name, LoadHowMuch(..),
- GhcException(..), showGhcException )
+ GhcException(..), showGhcException,
+ CheckedModule(..) )
import Outputable
-- following all needed for :info... ToDo: remove
pprIfaceDeclHead, pprParendIfaceType,
pprIfaceForAllPart, pprIfaceType )
import FunDeps ( pprFundeps )
-import SrcLoc ( SrcLoc, isGoodSrcLoc )
+import SrcLoc ( SrcLoc, pprDefnLoc )
import OccName ( OccName, parenSymOcc, occNameUserString )
-import BasicTypes ( StrictnessMark(..), defaultFixity )
+import BasicTypes ( StrictnessMark(..), defaultFixity, failed, successIf )
-- Other random utilities
import Panic ( panic, installSignalHandlers )
import Linker ( showLinkerState )
import Util ( removeSpaces, handle, global, toArgs,
looksLikeModuleName, prefixMatch )
+import ErrUtils ( printErrorsAndWarnings )
#ifndef mingw32_HOST_OS
import Util ( handle )
#if __GLASGOW_HASKELL__ > 504
hiding (getEnv)
#endif
+#else
+import GHC.ConsoleHandler ( flushConsole )
#endif
#ifdef USE_READLINE
import Numeric
import Data.List
import Data.Int ( Int64 )
+import Data.Maybe ( isJust )
import System.Cmd
import System.CPUTime
import System.Environment
("help", keepGoing help),
("?", keepGoing help),
("info", keepGoing info),
- ("load", keepGoingPaths loadModule),
+ ("load", keepGoingPaths loadModule_),
("module", keepGoing setContext),
("reload", keepGoing reloadModule),
+ ("check", keepGoing checkModule),
("set", keepGoing setCmd),
("show", keepGoing showCmd),
("type", keepGoing typeOfExpr),
Readline.initialize
#endif
+#if defined(mingw32_HOST_OS)
+ -- The win32 Console API mutates the first character of
+ -- type-ahead when reading from it in a non-buffered manner. Work
+ -- around this by flushing the input buffer of type-ahead characters.
+ --
+ GHC.ConsoleHandler.flushConsole stdin
+#endif
startGHCi (runGHCi srcs maybe_expr)
GHCiState{ progname = "<interactive>",
args = [],
Right hdl -> fileLoop hdl False
-- Perform a :load for files given on the GHCi command line
- when (not (null paths)) $
- ghciHandle showException $
- loadModule paths
+ -- When in -e mode, if the load fails then we want to stop
+ -- immediately rather than going on to evaluate the expression.
+ when (not (null paths)) $ do
+ ok <- ghciHandle (\e -> do showException e; return Failed) $
+ loadModule paths
+ when (isJust maybe_expr && failed ok) $
+ io (exitWith (ExitFailure 1))
-- if verbosity is greater than 0, or we are connected to a
-- terminal, display the prompt in the interactive loop.
showWithLoc :: SrcLoc -> SDoc -> SDoc
showWithLoc loc doc
- = hang doc 2 (char '\t' <> show_loc loc)
+ = hang doc 2 (char '\t' <> comment <+> pprDefnLoc loc)
-- The tab tries to make them line up a bit
where
- show_loc loc -- The ppr function for SrcLocs is a bit wonky
- | isGoodSrcLoc loc = comment <+> ptext SLIT("Defined at") <+> ppr loc
- | otherwise = comment <+> ppr loc
comment = ptext SLIT("--")
io (writeIORef commands (filter ((/= macro_name) . fst) cmds))
-loadModule :: [FilePath] -> GHCi ()
+loadModule :: [FilePath] -> GHCi SuccessFlag
loadModule fs = timeIt (loadModule' fs)
-loadModule' :: [FilePath] -> GHCi ()
+loadModule_ :: [FilePath] -> GHCi ()
+loadModule_ fs = do loadModule fs; return ()
+
+loadModule' :: [FilePath] -> GHCi SuccessFlag
loadModule' files = do
session <- getSession
io (GHC.setTargets session targets)
ok <- io (GHC.load session LoadAllTargets)
afterLoad ok session
+ return ok
+checkModule :: String -> GHCi ()
+checkModule m = do
+ let modl = mkModule m
+ session <- getSession
+ result <- io (GHC.checkModule session modl printErrorsAndWarnings)
+ case result of
+ Nothing -> io $ putStrLn "Nothing"
+ Just r -> io $ putStrLn (showSDoc (
+ case checkedModuleInfo r of
+ Just cm | Just scope <- GHC.modInfoTopLevelScope cm ->
+ let
+ (local,global) = partition ((== modl) . GHC.nameModule) scope
+ in
+ (text "global names: " <+> ppr global) $$
+ (text "local names: " <+> ppr local)
+ _ -> empty))
+ afterLoad (successIf (isJust result)) session
reloadModule :: String -> GHCi ()
reloadModule "" = do