8 #include "HsVersions.h"
10 import TcRnTypes -- Re-export all
11 import IOEnv -- Re-export all
13 import HsSyn ( MonoBinds(..) )
14 import HscTypes ( HscEnv(..), ModGuts(..), ModIface(..),
15 TyThing, Dependencies(..), TypeEnv, emptyTypeEnv,
16 ExternalPackageState(..), HomePackageTable,
17 ModDetails(..), HomeModInfo(..),
18 Deprecs(..), FixityEnv, FixItem,
19 GhciMode, lookupType, unQualInScope )
20 import Module ( Module, ModuleName, unitModuleEnv, foldModuleEnv, emptyModuleEnv )
21 import RdrName ( GlobalRdrEnv, emptyGlobalRdrEnv,
22 LocalRdrEnv, emptyLocalRdrEnv )
23 import Name ( Name, isInternalName )
25 import NameEnv ( extendNameEnvList )
26 import InstEnv ( InstEnv, emptyInstEnv, extendInstEnv )
28 import VarSet ( emptyVarSet )
29 import VarEnv ( TidyEnv, emptyTidyEnv )
30 import ErrUtils ( Message, Messages, emptyMessages, errorsFound,
31 addShortErrLocLine, addShortWarnLocLine, printErrorsAndWarnings )
32 import SrcLoc ( SrcLoc, mkGeneralSrcLoc )
33 import NameEnv ( emptyNameEnv )
34 import NameSet ( emptyDUs, emptyNameSet )
35 import OccName ( emptyOccEnv )
36 import Module ( moduleName )
37 import Bag ( emptyBag )
39 import UniqSupply ( UniqSupply, mkSplitUniqSupply, uniqFromSupply, splitUniqSupply )
40 import Unique ( Unique )
41 import CmdLineOpts ( DynFlags, DynFlag(..), dopt, opt_PprStyle_Debug )
42 import Bag ( snocBag, unionBags )
43 import Panic ( showException )
45 import Maybe ( isJust )
47 import DATA_IOREF ( newIORef, readIORef )
48 import EXCEPTION ( Exception )
53 %************************************************************************
57 %************************************************************************
60 ioToTcRn :: IO r -> TcRn r
69 -- Nothing => error thrown by the thing inside
70 -- (error messages should have been printed already)
72 initTc hsc_env mod do_this
73 = do { errs_var <- newIORef (emptyBag, emptyBag) ;
74 tvs_var <- newIORef emptyVarSet ;
75 type_env_var <- newIORef emptyNameEnv ;
76 dfuns_var <- newIORef emptyNameSet ;
81 tcg_rdr_env = emptyGlobalRdrEnv,
82 tcg_fix_env = emptyNameEnv,
83 tcg_default = Nothing,
84 tcg_type_env = emptyNameEnv,
85 tcg_type_env_var = type_env_var,
86 tcg_inst_env = mkImpInstEnv hsc_env,
87 tcg_inst_uses = dfuns_var,
89 tcg_imports = init_imports,
91 tcg_binds = EmptyMonoBinds,
92 tcg_deprecs = NoDeprecs,
99 tcl_loc = mkGeneralSrcLoc FSLIT("Top level of module"),
101 tcl_rdr = emptyLocalRdrEnv,
102 tcl_th_ctxt = topStage,
103 tcl_arrow_ctxt = topArrowCtxt,
104 tcl_env = emptyNameEnv,
105 tcl_tyvars = tvs_var,
106 tcl_lie = panic "initTc:LIE" -- LIE only valid inside a getLIE
110 -- OK, here's the business end!
111 maybe_res <- initTcRnIf 'a' hsc_env gbl_env lcl_env $
112 do { r <- tryM do_this
114 Right res -> return (Just res)
115 Left _ -> return Nothing } ;
117 -- Print any error messages
118 msgs <- readIORef errs_var ;
119 printErrorsAndWarnings msgs ;
121 let { dflags = hsc_dflags hsc_env
122 ; final_res | errorsFound dflags msgs = Nothing
123 | otherwise = maybe_res } ;
128 init_imports = emptyImportAvails { imp_qual = unitModuleEnv mod emptyAvailEnv }
129 -- Initialise tcg_imports with an empty set of bindings for
130 -- this module, so that if we see 'module M' in the export
131 -- list, and there are no bindings in M, we don't bleat
132 -- "unknown module M".
134 mkImpInstEnv :: HscEnv -> InstEnv
135 -- At the moment we (wrongly) build an instance environment from all the
136 -- home-package modules we have already compiled.
137 -- We should really only get instances from modules below us in the
138 -- module import tree.
139 mkImpInstEnv (HscEnv {hsc_dflags = dflags, hsc_HPT = hpt})
140 = foldModuleEnv (add . md_insts . hm_details) emptyInstEnv hpt
142 add dfuns inst_env = foldl extendInstEnv inst_env dfuns
144 -- mkImpTypeEnv makes the imported symbol table
145 mkImpTypeEnv :: ExternalPackageState -> HomePackageTable
146 -> Name -> Maybe TyThing
147 mkImpTypeEnv pcs hpt = lookup
150 lookup name | isInternalName name = Nothing
151 | otherwise = lookupType hpt pte name
155 %************************************************************************
159 %************************************************************************
163 initTcRnIf :: Char -- Tag for unique supply
168 initTcRnIf uniq_tag hsc_env gbl_env lcl_env thing_inside
169 = do { us <- mkSplitUniqSupply uniq_tag ;
170 ; us_var <- newIORef us ;
172 ; let { env = Env { env_top = hsc_env,
175 env_lcl = lcl_env } }
177 ; runIOEnv env thing_inside
181 %************************************************************************
185 %************************************************************************
188 getTopEnv :: TcRnIf gbl lcl HscEnv
189 getTopEnv = do { env <- getEnv; return (env_top env) }
191 getGblEnv :: TcRnIf gbl lcl gbl
192 getGblEnv = do { env <- getEnv; return (env_gbl env) }
194 updGblEnv :: (gbl -> gbl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
195 updGblEnv upd = updEnv (\ env@(Env { env_gbl = gbl }) ->
196 env { env_gbl = upd gbl })
198 setGblEnv :: gbl -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
199 setGblEnv gbl_env = updEnv (\ env -> env { env_gbl = gbl_env })
201 getLclEnv :: TcRnIf gbl lcl lcl
202 getLclEnv = do { env <- getEnv; return (env_lcl env) }
204 updLclEnv :: (lcl -> lcl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
205 updLclEnv upd = updEnv (\ env@(Env { env_lcl = lcl }) ->
206 env { env_lcl = upd lcl })
208 setLclEnv :: lcl' -> TcRnIf gbl lcl' a -> TcRnIf gbl lcl a
209 setLclEnv lcl_env = updEnv (\ env -> env { env_lcl = lcl_env })
211 getEnvs :: TcRnIf gbl lcl (gbl, lcl)
212 getEnvs = do { env <- getEnv; return (env_gbl env, env_lcl env) }
214 setEnvs :: (gbl', lcl') -> TcRnIf gbl' lcl' a -> TcRnIf gbl lcl a
215 setEnvs (gbl_env, lcl_env) = updEnv (\ env -> env { env_gbl = gbl_env, env_lcl = lcl_env })
222 getDOpts :: TcRnIf gbl lcl DynFlags
223 getDOpts = do { env <- getTopEnv; return (hsc_dflags env) }
225 doptM :: DynFlag -> TcRnIf gbl lcl Bool
226 doptM flag = do { dflags <- getDOpts; return (dopt flag dflags) }
228 ifOptM :: DynFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl () -- Do it flag is true
229 ifOptM flag thing_inside = do { b <- doptM flag;
230 if b then thing_inside else return () }
232 getGhciMode :: TcRnIf gbl lcl GhciMode
233 getGhciMode = do { env <- getTopEnv; return (hsc_mode env) }
237 getEpsVar :: TcRnIf gbl lcl (TcRef ExternalPackageState)
238 getEpsVar = do { env <- getTopEnv; return (hsc_EPS env) }
240 getEps :: TcRnIf gbl lcl ExternalPackageState
241 getEps = do { env <- getTopEnv; readMutVar (hsc_EPS env) }
243 setEps :: ExternalPackageState -> TcRnIf gbl lcl ()
244 setEps eps = do { env <- getTopEnv; writeMutVar (hsc_EPS env) eps }
246 updateEps :: (ExternalPackageState -> (ExternalPackageState, a))
248 updateEps upd_fn = do { eps_var <- getEpsVar
249 ; eps <- readMutVar eps_var
250 ; let { (eps', val) = upd_fn eps }
251 ; writeMutVar eps_var eps'
254 updateEps_ :: (ExternalPackageState -> ExternalPackageState)
256 updateEps_ upd_fn = do { eps_var <- getEpsVar
257 ; updMutVar eps_var upd_fn }
259 getHpt :: TcRnIf gbl lcl HomePackageTable
260 getHpt = do { env <- getTopEnv; return (hsc_HPT env) }
263 %************************************************************************
267 %************************************************************************
270 newUnique :: TcRnIf gbl lcl Unique
271 newUnique = do { us <- newUniqueSupply ;
272 return (uniqFromSupply us) }
274 newUniqueSupply :: TcRnIf gbl lcl UniqSupply
276 = do { env <- getEnv ;
277 let { u_var = env_us env } ;
278 us <- readMutVar u_var ;
279 let { (us1, us2) = splitUniqSupply us } ;
280 writeMutVar u_var us1 ;
285 %************************************************************************
289 %************************************************************************
292 traceTc, traceRn :: SDoc -> TcRn ()
293 traceRn = dumpOptTcRn Opt_D_dump_rn_trace
294 traceTc = dumpOptTcRn Opt_D_dump_tc_trace
295 traceSplice = dumpOptTcRn Opt_D_dump_splices
298 traceIf :: SDoc -> TcRnIf m n ()
299 traceIf = dumpOptIf Opt_D_dump_if_trace
300 traceHiDiffs = dumpOptIf Opt_D_dump_hi_diffs
303 dumpOptIf :: DynFlag -> SDoc -> TcRnIf m n () -- No RdrEnv available, so qualify everything
304 dumpOptIf flag doc = ifOptM flag $
305 ioToIOEnv (printForUser stderr alwaysQualify doc)
307 dumpOptTcRn :: DynFlag -> SDoc -> TcRn ()
308 dumpOptTcRn flag doc = ifOptM flag (dumpTcRn doc)
310 dumpTcRn :: SDoc -> TcRn ()
311 dumpTcRn doc = do { rdr_env <- getGlobalRdrEnv ;
312 ioToTcRn (printForUser stderr (unQualInScope rdr_env) doc) }
316 %************************************************************************
318 Typechecker global environment
320 %************************************************************************
323 getModule :: TcRn Module
324 getModule = do { env <- getGblEnv; return (tcg_mod env) }
326 getGlobalRdrEnv :: TcRn GlobalRdrEnv
327 getGlobalRdrEnv = do { env <- getGblEnv; return (tcg_rdr_env env) }
329 getImports :: TcRn ImportAvails
330 getImports = do { env <- getGblEnv; return (tcg_imports env) }
332 getFixityEnv :: TcRn FixityEnv
333 getFixityEnv = do { env <- getGblEnv; return (tcg_fix_env env) }
335 extendFixityEnv :: [(Name,FixItem)] -> RnM a -> RnM a
336 extendFixityEnv new_bit
337 = updGblEnv (\env@(TcGblEnv { tcg_fix_env = old_fix_env }) ->
338 env {tcg_fix_env = extendNameEnvList old_fix_env new_bit})
340 getDefaultTys :: TcRn (Maybe [Type])
341 getDefaultTys = do { env <- getGblEnv; return (tcg_default env) }
344 %************************************************************************
348 %************************************************************************
351 getSrcLocM :: TcRn SrcLoc
352 -- Avoid clash with Name.getSrcLoc
353 getSrcLocM = do { env <- getLclEnv; return (tcl_loc env) }
355 addSrcLoc :: SrcLoc -> TcRn a -> TcRn a
356 addSrcLoc loc = updLclEnv (\env -> env { tcl_loc = loc })
361 getErrsVar :: TcRn (TcRef Messages)
362 getErrsVar = do { env <- getLclEnv; return (tcl_errs env) }
364 setErrsVar :: TcRef Messages -> TcRn a -> TcRn a
365 setErrsVar v = updLclEnv (\ env -> env { tcl_errs = v })
367 addErr :: Message -> TcRn ()
368 addErr msg = do { loc <- getSrcLocM ; addErrAt loc msg }
370 addErrAt :: SrcLoc -> Message -> TcRn ()
372 = do { errs_var <- getErrsVar ;
373 rdr_env <- getGlobalRdrEnv ;
374 let { err = addShortErrLocLine loc (unQualInScope rdr_env) msg } ;
375 (warns, errs) <- readMutVar errs_var ;
376 writeMutVar errs_var (warns, errs `snocBag` err) }
378 addErrs :: [(SrcLoc,Message)] -> TcRn ()
379 addErrs msgs = mappM_ add msgs
381 add (loc,msg) = addErrAt loc msg
383 addWarn :: Message -> TcRn ()
385 = do { errs_var <- getErrsVar ;
387 rdr_env <- getGlobalRdrEnv ;
388 let { warn = addShortWarnLocLine loc (unQualInScope rdr_env) msg } ;
389 (warns, errs) <- readMutVar errs_var ;
390 writeMutVar errs_var (warns `snocBag` warn, errs) }
392 checkErr :: Bool -> Message -> TcRn ()
393 -- Add the error if the bool is False
394 checkErr ok msg = checkM ok (addErr msg)
396 warnIf :: Bool -> Message -> TcRn ()
397 warnIf True msg = addWarn msg
398 warnIf False msg = return ()
400 addMessages :: Messages -> TcRn ()
401 addMessages (m_warns, m_errs)
402 = do { errs_var <- getErrsVar ;
403 (warns, errs) <- readMutVar errs_var ;
404 writeMutVar errs_var (warns `unionBags` m_warns,
405 errs `unionBags` m_errs) }
410 recoverM :: TcRn r -- Recovery action; do this if the main one fails
411 -> TcRn r -- Main action: do this first
413 recoverM recover thing
414 = do { mb_res <- try_m thing ;
417 Right res -> returnM res }
419 tryTc :: TcRn a -> TcRn (Messages, Maybe a)
420 -- (tryTc m) executes m, and returns
421 -- Just r, if m succeeds (returning r) and caused no errors
422 -- Nothing, if m fails, or caused errors
423 -- It also returns all the errors accumulated by m
424 -- (even in the Just case, there might be warnings)
426 -- It always succeeds (never raises an exception)
428 = do { errs_var <- newMutVar emptyMessages ;
430 mb_r <- try_m (setErrsVar errs_var m) ;
432 new_errs <- readMutVar errs_var ;
439 Right r | errorsFound dflags new_errs -> Nothing
440 | otherwise -> Just r)
443 try_m :: TcRn r -> TcRn (Either Exception r)
444 -- Does try_m, with a debug-trace on failure
446 = do { mb_r <- tryM thing ;
448 Left exn -> do { traceTc (exn_msg exn); return mb_r }
449 Right r -> return mb_r }
451 exn_msg exn = text "tryTc/recoverM recovering from" <+> text (showException exn)
453 tryTcLIE :: TcM a -> TcM (Messages, Maybe a)
454 -- Just like tryTc, except that it ensures that the LIE
455 -- for the thing is propagated only if there are no errors
456 -- Hence it's restricted to the type-check monad
457 tryTcLIE thing_inside
458 = do { ((errs, mb_r), lie) <- getLIE (tryTc thing_inside) ;
459 ifM (isJust mb_r) (extendLIEs lie) ;
460 return (errs, mb_r) }
462 tryTcLIE_ :: TcM r -> TcM r -> TcM r
463 -- (tryTcLIE_ r m) tries m; if it succeeds it returns it,
464 -- otherwise it returns r. Any error messages added by m are discarded,
465 -- whether or not m succeeds.
466 tryTcLIE_ recover main
467 = do { (_msgs, mb_res) <- tryTcLIE main ;
469 Just res -> return res
472 checkNoErrs :: TcM r -> TcM r
473 -- (checkNoErrs m) succeeds iff m succeeds and generates no errors
474 -- If m fails then (checkNoErrsTc m) fails.
475 -- If m succeeds, it checks whether m generated any errors messages
476 -- (it might have recovered internally)
477 -- If so, it fails too.
478 -- Regardless, any errors generated by m are propagated to the enclosing context.
480 = do { (msgs, mb_res) <- tryTcLIE main ;
487 ifErrsM :: TcRn r -> TcRn r -> TcRn r
488 -- ifErrsM bale_out main
489 -- does 'bale_out' if there are errors in errors collection
490 -- otherwise does 'main'
491 ifErrsM bale_out normal
492 = do { errs_var <- getErrsVar ;
493 msgs <- readMutVar errs_var ;
495 if errorsFound dflags msgs then
500 failIfErrsM :: TcRn ()
501 -- Useful to avoid error cascades
502 failIfErrsM = ifErrsM failM (return ())
506 %************************************************************************
508 Context management and error message generation
511 %************************************************************************
514 setErrCtxtM, addErrCtxtM :: (TidyEnv -> TcM (TidyEnv, Message)) -> TcM a -> TcM a
515 setErrCtxtM msg = updCtxt (\ msgs -> [msg])
516 addErrCtxtM msg = updCtxt (\ msgs -> msg : msgs)
518 setErrCtxt, addErrCtxt :: Message -> TcM a -> TcM a
519 setErrCtxt msg = setErrCtxtM (\env -> returnM (env, msg))
520 addErrCtxt msg = addErrCtxtM (\env -> returnM (env, msg))
522 popErrCtxt :: TcM a -> TcM a
523 popErrCtxt = updCtxt (\ msgs -> case msgs of { [] -> []; (m:ms) -> ms })
525 getErrCtxt :: TcM ErrCtxt
526 getErrCtxt = do { env <- getLclEnv ; return (tcl_ctxt env) }
528 -- Helper function for the above
529 updCtxt :: (ErrCtxt -> ErrCtxt) -> TcM a -> TcM a
530 updCtxt upd = updLclEnv (\ env@(TcLclEnv { tcl_ctxt = ctxt }) ->
531 env { tcl_ctxt = upd ctxt })
533 getInstLoc :: InstOrigin -> TcM InstLoc
535 = do { loc <- getSrcLocM ; env <- getLclEnv ;
536 return (InstLoc origin loc (tcl_ctxt env)) }
538 addInstCtxt :: InstLoc -> TcM a -> TcM a
539 -- Add the SrcLoc and context from the first Inst in the list
540 -- (they all have similar locations)
541 addInstCtxt (InstLoc _ src_loc ctxt) thing_inside
542 = addSrcLoc src_loc (updCtxt (\ old_ctxt -> ctxt) thing_inside)
545 The addErrTc functions add an error message, but do not cause failure.
546 The 'M' variants pass a TidyEnv that has already been used to
547 tidy up the message; we then use it to tidy the context messages
550 addErrTc :: Message -> TcM ()
551 addErrTc err_msg = addErrTcM (emptyTidyEnv, err_msg)
553 addErrsTc :: [Message] -> TcM ()
554 addErrsTc err_msgs = mappM_ addErrTc err_msgs
556 addErrTcM :: (TidyEnv, Message) -> TcM ()
557 addErrTcM (tidy_env, err_msg)
558 = do { ctxt <- getErrCtxt ;
560 add_err_tcm tidy_env err_msg loc ctxt }
563 The failWith functions add an error message and cause failure
566 failWithTc :: Message -> TcM a -- Add an error message and fail
568 = addErrTc err_msg >> failM
570 failWithTcM :: (TidyEnv, Message) -> TcM a -- Add an error message and fail
571 failWithTcM local_and_msg
572 = addErrTcM local_and_msg >> failM
574 checkTc :: Bool -> Message -> TcM () -- Check that the boolean is true
575 checkTc True err = returnM ()
576 checkTc False err = failWithTc err
579 Warnings have no 'M' variant, nor failure
582 addWarnTc :: Message -> TcM ()
584 = do { ctxt <- getErrCtxt ;
585 ctxt_msgs <- do_ctxt emptyTidyEnv ctxt ;
586 addWarn (vcat (msg : ctxt_to_use ctxt_msgs)) }
588 warnTc :: Bool -> Message -> TcM ()
589 warnTc warn_if_true warn_msg
590 | warn_if_true = addWarnTc warn_msg
591 | otherwise = return ()
597 add_err_tcm tidy_env err_msg loc ctxt
598 = do { ctxt_msgs <- do_ctxt tidy_env ctxt ;
599 addErrAt loc (vcat (err_msg : ctxt_to_use ctxt_msgs)) }
603 do_ctxt tidy_env (c:cs)
604 = do { (tidy_env', m) <- c tidy_env ;
605 ms <- do_ctxt tidy_env' cs ;
608 ctxt_to_use ctxt | opt_PprStyle_Debug = ctxt
609 | otherwise = take 3 ctxt
612 %************************************************************************
614 Type constraints (the so-called LIE)
616 %************************************************************************
619 getLIEVar :: TcM (TcRef LIE)
620 getLIEVar = do { env <- getLclEnv; return (tcl_lie env) }
622 setLIEVar :: TcRef LIE -> TcM a -> TcM a
623 setLIEVar lie_var = updLclEnv (\ env -> env { tcl_lie = lie_var })
625 getLIE :: TcM a -> TcM (a, [Inst])
626 -- (getLIE m) runs m, and returns the type constraints it generates
628 = do { lie_var <- newMutVar emptyLIE ;
629 res <- updLclEnv (\ env -> env { tcl_lie = lie_var })
631 lie <- readMutVar lie_var ;
632 return (res, lieToList lie) }
634 extendLIE :: Inst -> TcM ()
636 = do { lie_var <- getLIEVar ;
637 lie <- readMutVar lie_var ;
638 writeMutVar lie_var (inst `consLIE` lie) }
640 extendLIEs :: [Inst] -> TcM ()
644 = do { lie_var <- getLIEVar ;
645 lie <- readMutVar lie_var ;
646 writeMutVar lie_var (mkLIE insts `plusLIE` lie) }
650 setLclTypeEnv :: TcLclEnv -> TcM a -> TcM a
651 -- Set the local type envt, but do *not* disturb other fields,
652 -- notably the lie_var
653 setLclTypeEnv lcl_env thing_inside
654 = updLclEnv upd thing_inside
656 upd env = env { tcl_env = tcl_env lcl_env,
657 tcl_tyvars = tcl_tyvars lcl_env }
661 %************************************************************************
663 Template Haskell context
665 %************************************************************************
668 getStage :: TcM ThStage
669 getStage = do { env <- getLclEnv; return (tcl_th_ctxt env) }
671 setStage :: ThStage -> TcM a -> TcM a
672 setStage s = updLclEnv (\ env -> env { tcl_th_ctxt = s })
676 %************************************************************************
680 %************************************************************************
683 popArrowBinders :: TcM a -> TcM a -- Move to the left of a (-<); see comments in TcRnTypes
685 = updLclEnv (\ env -> env { tcl_arrow_ctxt = pop (tcl_arrow_ctxt env) })
687 pop (ArrCtxt {proc_level = curr_lvl, proc_banned = banned})
688 = ASSERT( not (curr_lvl `elem` banned) )
689 ArrCtxt {proc_level = curr_lvl, proc_banned = curr_lvl : banned}
691 getBannedProcLevels :: TcM [ProcLevel]
692 = do { env <- getLclEnv; return (proc_banned (tcl_arrow_ctxt env)) }
694 incProcLevel :: TcM a -> TcM a
696 = updLclEnv (\ env -> env { tcl_arrow_ctxt = inc (tcl_arrow_ctxt env) })
698 inc ctxt = ctxt { proc_level = proc_level ctxt + 1 }
702 %************************************************************************
704 Stuff for the renamer's local env
706 %************************************************************************
709 getLocalRdrEnv :: RnM LocalRdrEnv
710 getLocalRdrEnv = do { env <- getLclEnv; return (tcl_rdr env) }
712 setLocalRdrEnv :: LocalRdrEnv -> RnM a -> RnM a
713 setLocalRdrEnv rdr_env thing_inside
714 = updLclEnv (\env -> env {tcl_rdr = rdr_env}) thing_inside
718 %************************************************************************
720 Stuff for interface decls
722 %************************************************************************
725 initIfaceTcRn :: IfG a -> TcRn a
726 initIfaceTcRn thing_inside
727 = do { tcg_env <- getGblEnv
728 ; let { if_env = IfGblEnv {
729 if_rec_types = Just (tcg_mod tcg_env, get_type_env),
730 if_is_boot = imp_dep_mods (tcg_imports tcg_env) }
731 ; get_type_env = readMutVar (tcg_type_env_var tcg_env) }
732 ; setEnvs (if_env, ()) thing_inside }
734 initIfaceExtCore :: IfL a -> TcRn a
735 initIfaceExtCore thing_inside
736 = do { tcg_env <- getGblEnv
737 ; let { mod = tcg_mod tcg_env
738 ; if_env = IfGblEnv {
739 if_rec_types = Just (mod, return (tcg_type_env tcg_env)),
740 if_is_boot = imp_dep_mods (tcg_imports tcg_env) }
741 ; if_lenv = IfLclEnv { if_mod = moduleName mod,
742 if_tv_env = emptyOccEnv,
743 if_id_env = emptyOccEnv }
745 ; setEnvs (if_env, if_lenv) thing_inside }
747 initIfaceCheck :: HscEnv -> IfG a -> IO a
748 -- Used when checking the up-to-date-ness of the old Iface
749 -- Initialise the environment with no useful info at all
750 initIfaceCheck hsc_env do_this
751 = do { let { gbl_env = IfGblEnv { if_is_boot = emptyModuleEnv,
752 if_rec_types = Nothing } ;
754 ; initTcRnIf 'i' hsc_env gbl_env () do_this
757 initIfaceTc :: HscEnv -> ModIface
758 -> (TcRef TypeEnv -> IfL a) -> IO a
759 -- Used when type-checking checking an up-to-date interface file
760 -- No type envt from the current module, but we do know the module dependencies
761 initIfaceTc hsc_env iface do_this
762 = do { tc_env_var <- newIORef emptyTypeEnv
763 ; let { gbl_env = IfGblEnv { if_is_boot = mkModDeps (dep_mods (mi_deps iface)),
764 if_rec_types = Just (mod, readMutVar tc_env_var) } ;
765 ; if_lenv = IfLclEnv { if_mod = moduleName mod,
766 if_tv_env = emptyOccEnv,
767 if_id_env = emptyOccEnv }
769 ; initTcRnIf 'i' hsc_env gbl_env if_lenv (do_this tc_env_var)
772 mod = mi_module iface
774 initIfaceRules :: HscEnv -> ModGuts -> IfG a -> IO a
775 -- Used when sucking in new Rules in SimplCore
776 -- We have available the type envt of the module being compiled, and we must use it
777 initIfaceRules hsc_env guts do_this
779 is_boot = mkModDeps (dep_mods (mg_deps guts))
780 -- Urgh! But we do somehow need to get the info
781 -- on whether (for this particular compilation) we should
782 -- import a hi-boot file or not.
783 ; type_info = (mg_module guts, return (mg_types guts))
784 ; gbl_env = IfGblEnv { if_is_boot = is_boot,
785 if_rec_types = Just type_info } ;
788 -- Run the thing; any exceptions just bubble out from here
789 ; initTcRnIf 'i' hsc_env gbl_env () do_this
792 initIfaceLcl :: ModuleName -> IfL a -> IfM lcl a
793 initIfaceLcl mod thing_inside
794 = setLclEnv (IfLclEnv { if_mod = mod,
795 if_tv_env = emptyOccEnv,
796 if_id_env = emptyOccEnv })
801 forkM_maybe :: SDoc -> IfL a -> IfL (Maybe a)
802 -- Run thing_inside in an interleaved thread.
803 -- It shares everything with the parent thread, so this is DANGEROUS.
805 -- It returns Nothing if the computation fails
807 -- It's used for lazily type-checking interface
808 -- signatures, which is pretty benign
810 forkM_maybe doc thing_inside
811 = do { unsafeInterleaveM $
812 do { traceIf (text "Starting fork {" <+> doc)
813 ; mb_res <- tryM thing_inside ;
815 Right r -> do { traceIf (text "} ending fork" <+> doc)
819 -- Bleat about errors in the forked thread, if -ddump-if-trace is on
820 -- Otherwise we silently discard errors. Errors can legitimately
821 -- happen when compiling interface signatures (see tcInterfaceSigs)
822 ifOptM Opt_D_dump_if_trace
823 (print_errs (hang (text "forkM failed:" <+> doc)
824 4 (text (show exn))))
826 ; traceIf (text "} ending fork (badly)" <+> doc)
830 print_errs sdoc = ioToIOEnv (printErrs (sdoc defaultErrStyle))
832 forkM :: SDoc -> IfL a -> IfL a
833 forkM doc thing_inside
834 = do { mb_res <- forkM_maybe doc thing_inside
835 ; return (case mb_res of
836 Nothing -> pprPanic "forkM" doc