-data HscResult
- -- Compilation failed
- = HscFail
-
- -- Concluded that it wasn't necessary
- | HscNoRecomp ModDetails -- new details (HomeSymbolTable additions)
- ModIface -- new iface (if any compilation was done)
-
- -- Did recompilation
- | HscRecomp ModDetails -- new details (HomeSymbolTable additions)
- (Maybe GlobalRdrEnv)
- ModIface -- new iface (if any compilation was done)
- Bool -- stub_h exists
- Bool -- stub_c exists
- (Maybe CompiledByteCode)
-
- -- no errors or warnings; the individual passes
- -- (parse/rename/typecheck) print messages themselves
-
-hscMain
- :: HscEnv
- -> Module
- -> ModLocation -- location info
- -> Bool -- True <=> source unchanged
- -> Bool -- True <=> have an object file (for msgs only)
- -> Maybe ModIface -- old interface, if available
- -> IO HscResult
-
-hscMain hsc_env mod location
- source_unchanged have_object maybe_old_iface
- = do {
- (recomp_reqd, maybe_checked_iface) <-
- _scc_ "checkOldIface"
- checkOldIface hsc_env mod
- (ml_hi_file location)
- source_unchanged maybe_old_iface;
-
- let no_old_iface = not (isJust maybe_checked_iface)
- what_next | recomp_reqd || no_old_iface = hscRecomp
- | otherwise = hscNoRecomp
-
- ; what_next hsc_env have_object
- mod location maybe_checked_iface
- }
-
-
--- hscNoRecomp definitely expects to have the old interface available
-hscNoRecomp hsc_env have_object
- mod location (Just old_iface)
- | hsc_mode hsc_env == OneShot
- = do {
- when (verbosity (hsc_dflags hsc_env) > 0) $
- hPutStrLn stderr "compilation IS NOT required";
- dumpIfaceStats hsc_env ;
-
- let { bomb = panic "hscNoRecomp:OneShot" };
- return (HscNoRecomp bomb bomb)
- }
- | otherwise
- = do {
- when (verbosity (hsc_dflags hsc_env) >= 1) $
- hPutStrLn stderr ("Skipping " ++
- showModMsg have_object mod location);
-
- new_details <- _scc_ "tcRnIface"
- typecheckIface hsc_env old_iface ;
- dumpIfaceStats hsc_env ;
-
- return (HscNoRecomp new_details old_iface)
- }
-
-hscRecomp hsc_env have_object
- mod location maybe_checked_iface
- = do {
- -- what target are we shooting for?
- ; let one_shot = hsc_mode hsc_env == OneShot
- ; let dflags = hsc_dflags hsc_env
- ; let toInterp = dopt_HscLang dflags == HscInterpreted
- ; let toCore = isJust (ml_hs_file location) &&
- isExtCore_file (fromJust (ml_hs_file location))
-
- ; when (not one_shot && verbosity dflags >= 1) $
- hPutStrLn stderr ("Compiling " ++
- showModMsg (not toInterp) mod location);
+
+data HscChecked
+ = HscChecked
+ -- parsed
+ (Located (HsModule RdrName))
+ -- renamed
+ (Maybe (HsGroup Name,[LImportDecl Name],Maybe [LIE Name]))
+ -- typechecked
+ (Maybe (LHsBinds Id, GlobalRdrEnv, ModDetails))
+
+
+-- Status of a compilation to hard-code or nothing.
+data HscStatus
+ = HscNoRecomp
+ | HscRecomp Bool -- Has stub files.
+ -- This is a hack. We can't compile C files here
+ -- since it's done in DriverPipeline. For now we
+ -- just return True if we want the caller to compile
+ -- it for us.
+
+-- Status of a compilation to byte-code.
+data InteractiveStatus
+ = InteractiveNoRecomp
+ | InteractiveRecomp Bool -- Same as HscStatus
+ CompiledByteCode
+
+type NoRecomp result = HscEnv -> ModSummary -> ModIface -> Maybe (Int,Int) -> IO result
+type FrontEnd core = HscEnv -> ModSummary -> Maybe (Int,Int) -> IO (Maybe core)
+type BackEnd core prepCore = HscEnv -> ModSummary -> Maybe ModIface -> core -> IO prepCore
+type CodeGen prepCore result = HscEnv -> ModSummary -> prepCore -> IO result
+
+-- FIXME: The old interface and module index are only using in 'make' and
+-- 'interactive' mode. They should be removed from 'oneshot' mode.
+type Compiler result = HscEnv
+ -> ModSummary
+ -> Bool -- True <=> source unchanged
+ -> Maybe ModIface -- Old interface, if available
+ -> Maybe (Int,Int) -- Just (i,n) <=> module i of n (for msgs)
+ -> IO (Maybe result)
+
+
+-- This functions checks if recompilation is necessary and
+-- then combines the FrontEnd, BackEnd and CodeGen to a
+-- working compiler.
+hscMkCompiler :: NoRecomp result -- What to do when recompilation isn't required.
+ -> FrontEnd core
+ -> BackEnd core prepCore
+ -> CodeGen prepCore result
+ -> Compiler result
+hscMkCompiler norecomp frontend backend codegen
+ hsc_env mod_summary source_unchanged
+ mbOldIface mbModIndex
+ = do (recomp_reqd, mbCheckedIface)
+ <- {-# SCC "checkOldIface" #-}
+ checkOldIface hsc_env mod_summary
+ source_unchanged mbOldIface
+ case mbCheckedIface of
+ Just iface | not recomp_reqd
+ -> do result <- norecomp hsc_env mod_summary iface mbModIndex
+ return (Just result)
+ _otherwise
+ -> do mbCore <- frontend hsc_env mod_summary mbModIndex
+ case mbCore of
+ Nothing
+ -> return Nothing
+ Just core
+ -> do prepCore <- backend hsc_env mod_summary
+ mbCheckedIface core
+ result <- codegen hsc_env mod_summary prepCore
+ return (Just result)
+
+--------------------------------------------------------------
+-- Compilers
+--------------------------------------------------------------
+
+-- Compile Haskell, boot and extCore in OneShot mode.
+hscCompileOneShot :: Compiler HscStatus
+hscCompileOneShot hsc_env mod_summary =
+ compiler hsc_env mod_summary
+ where mkComp = hscMkCompiler (norecompOneShot HscNoRecomp)
+ compiler
+ = case ms_hsc_src mod_summary of
+ ExtCoreFile
+ -> mkComp hscCoreFrontEnd hscNewBackEnd hscCodeGenOneShot
+-- 1 2 3 4 5 6 7 8 9
+ HsSrcFile
+ -> mkComp hscFileFrontEnd hscNewBackEnd hscCodeGenOneShot
+ HsBootFile
+ -> mkComp hscFileFrontEnd hscNewBootBackEnd
+ (hscCodeGenConst (HscRecomp False))
+
+-- Compile Haskell, boot and extCore in --make mode.
+hscCompileMake :: Compiler (HscStatus, ModIface, ModDetails)
+hscCompileMake hsc_env mod_summary
+ = compiler hsc_env mod_summary
+ where mkComp = hscMkCompiler norecompMake
+ backend = case hscTarget (hsc_dflags hsc_env) of
+ HscNothing -> hscCodeGenNothing
+ _other -> hscCodeGenMake
+ compiler
+ = case ms_hsc_src mod_summary of
+ ExtCoreFile
+ -> mkComp hscCoreFrontEnd hscNewBackEnd backend
+ HsSrcFile
+ -> mkComp hscFileFrontEnd hscNewBackEnd backend
+ HsBootFile
+ -> mkComp hscFileFrontEnd hscNewBootBackEnd hscCodeGenIdentity
+
+
+-- Compile Haskell, extCore to bytecode.
+hscCompileInteractive :: Compiler (InteractiveStatus, ModIface, ModDetails)
+hscCompileInteractive hsc_env mod_summary =
+ hscMkCompiler norecompInteractive frontend hscNewBackEnd hscCodeGenInteractive
+ hsc_env mod_summary
+ where frontend = case ms_hsc_src mod_summary of
+ ExtCoreFile -> hscCoreFrontEnd
+ HsSrcFile -> hscFileFrontEnd
+ HsBootFile -> panic bootErrorMsg
+ bootErrorMsg = "Compiling a HsBootFile to bytecode doesn't make sense. " ++
+ "Use 'hscCompileMake' instead."
+
+--------------------------------------------------------------
+-- NoRecomp handlers
+--------------------------------------------------------------
+
+norecompOneShot :: a -> NoRecomp a
+norecompOneShot a hsc_env mod_summary
+ old_iface
+ mb_mod_index
+ = do compilationProgressMsg (hsc_dflags hsc_env) $
+ "compilation IS NOT required"
+ dumpIfaceStats hsc_env
+ return a
+
+norecompMake :: NoRecomp (HscStatus, ModIface, ModDetails)
+norecompMake = norecompWorker HscNoRecomp False
+
+norecompInteractive :: NoRecomp (InteractiveStatus, ModIface, ModDetails)
+norecompInteractive = norecompWorker InteractiveNoRecomp True
+
+norecompWorker :: a -> Bool -> NoRecomp (a, ModIface, ModDetails)
+norecompWorker a isInterp hsc_env mod_summary
+ old_iface mb_mod_index
+ = do compilationProgressMsg (hsc_dflags hsc_env) $
+ (showModuleIndex mb_mod_index ++
+ "Skipping " ++ showModMsg isInterp mod_summary)
+ new_details <- {-# SCC "tcRnIface" #-}
+ initIfaceCheck hsc_env $
+ typecheckIface old_iface
+ dumpIfaceStats hsc_env
+ return (a, old_iface, new_details)
+
+--------------------------------------------------------------
+-- FrontEnds
+--------------------------------------------------------------
+
+hscCoreFrontEnd :: FrontEnd ModGuts
+hscCoreFrontEnd hsc_env mod_summary mb_mod_index = do {
+ -------------------
+ -- PARSE
+ -------------------
+ ; inp <- readFile (expectJust "hscCoreFrontEnd" (ms_hspp_file mod_summary))
+ ; case parseCore inp 1 of
+ FailP s -> do errorMsg (hsc_dflags hsc_env) (text s{-ToDo: wrong-})
+ return Nothing
+ OkP rdr_module -> do {
+
+ -------------------
+ -- RENAME and TYPECHECK
+ -------------------
+ ; (tc_msgs, maybe_tc_result) <- {-# SCC "TypeCheck" #-}
+ tcRnExtCore hsc_env rdr_module
+ ; printErrorsAndWarnings (hsc_dflags hsc_env) tc_msgs
+ ; case maybe_tc_result of
+ Nothing -> return Nothing
+ Just mod_guts -> return (Just mod_guts) -- No desugaring to do!
+ }}
+
+
+hscFileFrontEnd :: FrontEnd ModGuts
+hscFileFrontEnd hsc_env mod_summary mb_mod_index = do {
+-- FIXME: Move 'DISPLAY PROGRESS MESSAGE' out of the frontend.
+ -------------------
+ -- DISPLAY PROGRESS MESSAGE
+ -------------------
+ ; let dflags = hsc_dflags hsc_env
+ one_shot = isOneShot (ghcMode dflags)
+ toInterp = hscTarget dflags == HscInterpreted
+ ; when (not one_shot) $
+ compilationProgressMsg dflags $
+ (showModuleIndex mb_mod_index ++
+ "Compiling " ++ showModMsg (not toInterp) mod_summary)