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, dopt_set )
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,
96 tcg_keep = emptyNameSet
100 tcl_loc = mkGeneralSrcLoc FSLIT("Top level of module"),
102 tcl_rdr = emptyLocalRdrEnv,
103 tcl_th_ctxt = topStage,
104 tcl_arrow_ctxt = topArrowCtxt,
105 tcl_env = emptyNameEnv,
106 tcl_tyvars = tvs_var,
107 tcl_lie = panic "initTc:LIE" -- LIE only valid inside a getLIE
111 -- OK, here's the business end!
112 maybe_res <- initTcRnIf 'a' hsc_env gbl_env lcl_env $
113 do { r <- tryM do_this
115 Right res -> return (Just res)
116 Left _ -> return Nothing } ;
118 -- Print any error messages
119 msgs <- readIORef errs_var ;
120 printErrorsAndWarnings msgs ;
122 let { dflags = hsc_dflags hsc_env
123 ; final_res | errorsFound dflags msgs = Nothing
124 | otherwise = maybe_res } ;
129 init_imports = emptyImportAvails { imp_qual = unitModuleEnv mod emptyAvailEnv }
130 -- Initialise tcg_imports with an empty set of bindings for
131 -- this module, so that if we see 'module M' in the export
132 -- list, and there are no bindings in M, we don't bleat
133 -- "unknown module M".
135 mkImpInstEnv :: HscEnv -> InstEnv
136 -- At the moment we (wrongly) build an instance environment from all the
137 -- home-package modules we have already compiled.
138 -- We should really only get instances from modules below us in the
139 -- module import tree.
140 mkImpInstEnv (HscEnv {hsc_dflags = dflags, hsc_HPT = hpt})
141 = foldModuleEnv (add . md_insts . hm_details) emptyInstEnv hpt
143 add dfuns inst_env = foldl extendInstEnv inst_env dfuns
145 -- mkImpTypeEnv makes the imported symbol table
146 mkImpTypeEnv :: ExternalPackageState -> HomePackageTable
147 -> Name -> Maybe TyThing
148 mkImpTypeEnv pcs hpt = lookup
151 lookup name | isInternalName name = Nothing
152 | otherwise = lookupType hpt pte name
156 %************************************************************************
160 %************************************************************************
164 initTcRnIf :: Char -- Tag for unique supply
169 initTcRnIf uniq_tag hsc_env gbl_env lcl_env thing_inside
170 = do { us <- mkSplitUniqSupply uniq_tag ;
171 ; us_var <- newIORef us ;
173 ; let { env = Env { env_top = hsc_env,
176 env_lcl = lcl_env } }
178 ; runIOEnv env thing_inside
182 %************************************************************************
186 %************************************************************************
189 getTopEnv :: TcRnIf gbl lcl HscEnv
190 getTopEnv = do { env <- getEnv; return (env_top env) }
192 getGblEnv :: TcRnIf gbl lcl gbl
193 getGblEnv = do { env <- getEnv; return (env_gbl env) }
195 updGblEnv :: (gbl -> gbl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
196 updGblEnv upd = updEnv (\ env@(Env { env_gbl = gbl }) ->
197 env { env_gbl = upd gbl })
199 setGblEnv :: gbl -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
200 setGblEnv gbl_env = updEnv (\ env -> env { env_gbl = gbl_env })
202 getLclEnv :: TcRnIf gbl lcl lcl
203 getLclEnv = do { env <- getEnv; return (env_lcl env) }
205 updLclEnv :: (lcl -> lcl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
206 updLclEnv upd = updEnv (\ env@(Env { env_lcl = lcl }) ->
207 env { env_lcl = upd lcl })
209 setLclEnv :: lcl' -> TcRnIf gbl lcl' a -> TcRnIf gbl lcl a
210 setLclEnv lcl_env = updEnv (\ env -> env { env_lcl = lcl_env })
212 getEnvs :: TcRnIf gbl lcl (gbl, lcl)
213 getEnvs = do { env <- getEnv; return (env_gbl env, env_lcl env) }
215 setEnvs :: (gbl', lcl') -> TcRnIf gbl' lcl' a -> TcRnIf gbl lcl a
216 setEnvs (gbl_env, lcl_env) = updEnv (\ env -> env { env_gbl = gbl_env, env_lcl = lcl_env })
223 getDOpts :: TcRnIf gbl lcl DynFlags
224 getDOpts = do { env <- getTopEnv; return (hsc_dflags env) }
226 doptM :: DynFlag -> TcRnIf gbl lcl Bool
227 doptM flag = do { dflags <- getDOpts; return (dopt flag dflags) }
229 setOptM :: DynFlag -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
230 setOptM flag = updEnv (\ env@(Env { env_top = top }) ->
231 env { env_top = top { hsc_dflags = dopt_set (hsc_dflags top) flag}} )
233 ifOptM :: DynFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl () -- Do it flag is true
234 ifOptM flag thing_inside = do { b <- doptM flag;
235 if b then thing_inside else return () }
237 getGhciMode :: TcRnIf gbl lcl GhciMode
238 getGhciMode = do { env <- getTopEnv; return (hsc_mode env) }
242 getEpsVar :: TcRnIf gbl lcl (TcRef ExternalPackageState)
243 getEpsVar = do { env <- getTopEnv; return (hsc_EPS env) }
245 getEps :: TcRnIf gbl lcl ExternalPackageState
246 getEps = do { env <- getTopEnv; readMutVar (hsc_EPS env) }
248 setEps :: ExternalPackageState -> TcRnIf gbl lcl ()
249 setEps eps = do { env <- getTopEnv; writeMutVar (hsc_EPS env) eps }
251 updateEps :: (ExternalPackageState -> (ExternalPackageState, a))
253 updateEps upd_fn = do { eps_var <- getEpsVar
254 ; eps <- readMutVar eps_var
255 ; let { (eps', val) = upd_fn eps }
256 ; writeMutVar eps_var eps'
259 updateEps_ :: (ExternalPackageState -> ExternalPackageState)
261 updateEps_ upd_fn = do { eps_var <- getEpsVar
262 ; updMutVar eps_var upd_fn }
264 getHpt :: TcRnIf gbl lcl HomePackageTable
265 getHpt = do { env <- getTopEnv; return (hsc_HPT env) }
268 %************************************************************************
272 %************************************************************************
275 newUnique :: TcRnIf gbl lcl Unique
276 newUnique = do { us <- newUniqueSupply ;
277 return (uniqFromSupply us) }
279 newUniqueSupply :: TcRnIf gbl lcl UniqSupply
281 = do { env <- getEnv ;
282 let { u_var = env_us env } ;
283 us <- readMutVar u_var ;
284 let { (us1, us2) = splitUniqSupply us } ;
285 writeMutVar u_var us1 ;
290 %************************************************************************
294 %************************************************************************
297 traceTc, traceRn :: SDoc -> TcRn ()
298 traceRn = dumpOptTcRn Opt_D_dump_rn_trace
299 traceTc = dumpOptTcRn Opt_D_dump_tc_trace
300 traceSplice = dumpOptTcRn Opt_D_dump_splices
303 traceIf :: SDoc -> TcRnIf m n ()
304 traceIf = dumpOptIf Opt_D_dump_if_trace
305 traceHiDiffs = dumpOptIf Opt_D_dump_hi_diffs
308 dumpOptIf :: DynFlag -> SDoc -> TcRnIf m n () -- No RdrEnv available, so qualify everything
309 dumpOptIf flag doc = ifOptM flag $
310 ioToIOEnv (printForUser stderr alwaysQualify doc)
312 dumpOptTcRn :: DynFlag -> SDoc -> TcRn ()
313 dumpOptTcRn flag doc = ifOptM flag (dumpTcRn doc)
315 dumpTcRn :: SDoc -> TcRn ()
316 dumpTcRn doc = do { rdr_env <- getGlobalRdrEnv ;
317 ioToTcRn (printForUser stderr (unQualInScope rdr_env) doc) }
321 %************************************************************************
323 Typechecker global environment
325 %************************************************************************
328 getModule :: TcRn Module
329 getModule = do { env <- getGblEnv; return (tcg_mod env) }
331 getGlobalRdrEnv :: TcRn GlobalRdrEnv
332 getGlobalRdrEnv = do { env <- getGblEnv; return (tcg_rdr_env env) }
334 getImports :: TcRn ImportAvails
335 getImports = do { env <- getGblEnv; return (tcg_imports env) }
337 getFixityEnv :: TcRn FixityEnv
338 getFixityEnv = do { env <- getGblEnv; return (tcg_fix_env env) }
340 extendFixityEnv :: [(Name,FixItem)] -> RnM a -> RnM a
341 extendFixityEnv new_bit
342 = updGblEnv (\env@(TcGblEnv { tcg_fix_env = old_fix_env }) ->
343 env {tcg_fix_env = extendNameEnvList old_fix_env new_bit})
345 getDefaultTys :: TcRn (Maybe [Type])
346 getDefaultTys = do { env <- getGblEnv; return (tcg_default env) }
349 %************************************************************************
353 %************************************************************************
356 getSrcLocM :: TcRn SrcLoc
357 -- Avoid clash with Name.getSrcLoc
358 getSrcLocM = do { env <- getLclEnv; return (tcl_loc env) }
360 addSrcLoc :: SrcLoc -> TcRn a -> TcRn a
361 addSrcLoc loc = updLclEnv (\env -> env { tcl_loc = loc })
366 getErrsVar :: TcRn (TcRef Messages)
367 getErrsVar = do { env <- getLclEnv; return (tcl_errs env) }
369 setErrsVar :: TcRef Messages -> TcRn a -> TcRn a
370 setErrsVar v = updLclEnv (\ env -> env { tcl_errs = v })
372 addErr :: Message -> TcRn ()
373 addErr msg = do { loc <- getSrcLocM ; addErrAt loc msg }
375 addErrAt :: SrcLoc -> Message -> TcRn ()
377 = do { errs_var <- getErrsVar ;
378 rdr_env <- getGlobalRdrEnv ;
379 let { err = addShortErrLocLine loc (unQualInScope rdr_env) msg } ;
380 (warns, errs) <- readMutVar errs_var ;
381 writeMutVar errs_var (warns, errs `snocBag` err) }
383 addErrs :: [(SrcLoc,Message)] -> TcRn ()
384 addErrs msgs = mappM_ add msgs
386 add (loc,msg) = addErrAt loc msg
388 addWarn :: Message -> TcRn ()
390 = do { errs_var <- getErrsVar ;
392 rdr_env <- getGlobalRdrEnv ;
393 let { warn = addShortWarnLocLine loc (unQualInScope rdr_env) msg } ;
394 (warns, errs) <- readMutVar errs_var ;
395 writeMutVar errs_var (warns `snocBag` warn, errs) }
397 checkErr :: Bool -> Message -> TcRn ()
398 -- Add the error if the bool is False
399 checkErr ok msg = checkM ok (addErr msg)
401 warnIf :: Bool -> Message -> TcRn ()
402 warnIf True msg = addWarn msg
403 warnIf False msg = return ()
405 addMessages :: Messages -> TcRn ()
406 addMessages (m_warns, m_errs)
407 = do { errs_var <- getErrsVar ;
408 (warns, errs) <- readMutVar errs_var ;
409 writeMutVar errs_var (warns `unionBags` m_warns,
410 errs `unionBags` m_errs) }
412 discardWarnings :: TcRn a -> TcRn a
413 -- Ignore warnings inside the thing inside;
414 -- used to ignore-unused-variable warnings inside derived code
415 -- With -dppr-debug, the effects is switched off, so you can still see
416 -- what warnings derived code would give
417 discardWarnings thing_inside
418 | opt_PprStyle_Debug = thing_inside
420 = do { errs_var <- newMutVar emptyMessages
421 ; result <- setErrsVar errs_var thing_inside
422 ; (_warns, errs) <- readMutVar errs_var
423 ; addMessages (emptyBag, errs)
429 recoverM :: TcRn r -- Recovery action; do this if the main one fails
430 -> TcRn r -- Main action: do this first
432 recoverM recover thing
433 = do { mb_res <- try_m thing ;
436 Right res -> returnM res }
438 tryTc :: TcRn a -> TcRn (Messages, Maybe a)
439 -- (tryTc m) executes m, and returns
440 -- Just r, if m succeeds (returning r) and caused no errors
441 -- Nothing, if m fails, or caused errors
442 -- It also returns all the errors accumulated by m
443 -- (even in the Just case, there might be warnings)
445 -- It always succeeds (never raises an exception)
447 = do { errs_var <- newMutVar emptyMessages ;
449 mb_r <- try_m (setErrsVar errs_var m) ;
451 new_errs <- readMutVar errs_var ;
458 Right r | errorsFound dflags new_errs -> Nothing
459 | otherwise -> Just r)
462 try_m :: TcRn r -> TcRn (Either Exception r)
463 -- Does try_m, with a debug-trace on failure
465 = do { mb_r <- tryM thing ;
467 Left exn -> do { traceTc (exn_msg exn); return mb_r }
468 Right r -> return mb_r }
470 exn_msg exn = text "tryTc/recoverM recovering from" <+> text (showException exn)
472 tryTcLIE :: TcM a -> TcM (Messages, Maybe a)
473 -- Just like tryTc, except that it ensures that the LIE
474 -- for the thing is propagated only if there are no errors
475 -- Hence it's restricted to the type-check monad
476 tryTcLIE thing_inside
477 = do { ((errs, mb_r), lie) <- getLIE (tryTc thing_inside) ;
478 ifM (isJust mb_r) (extendLIEs lie) ;
479 return (errs, mb_r) }
481 tryTcLIE_ :: TcM r -> TcM r -> TcM r
482 -- (tryTcLIE_ r m) tries m; if it succeeds it returns it,
483 -- otherwise it returns r. Any error messages added by m are discarded,
484 -- whether or not m succeeds.
485 tryTcLIE_ recover main
486 = do { (_msgs, mb_res) <- tryTcLIE main ;
488 Just res -> return res
491 checkNoErrs :: TcM r -> TcM r
492 -- (checkNoErrs m) succeeds iff m succeeds and generates no errors
493 -- If m fails then (checkNoErrsTc m) fails.
494 -- If m succeeds, it checks whether m generated any errors messages
495 -- (it might have recovered internally)
496 -- If so, it fails too.
497 -- Regardless, any errors generated by m are propagated to the enclosing context.
499 = do { (msgs, mb_res) <- tryTcLIE main ;
506 ifErrsM :: TcRn r -> TcRn r -> TcRn r
507 -- ifErrsM bale_out main
508 -- does 'bale_out' if there are errors in errors collection
509 -- otherwise does 'main'
510 ifErrsM bale_out normal
511 = do { errs_var <- getErrsVar ;
512 msgs <- readMutVar errs_var ;
514 if errorsFound dflags msgs then
519 failIfErrsM :: TcRn ()
520 -- Useful to avoid error cascades
521 failIfErrsM = ifErrsM failM (return ())
525 %************************************************************************
527 Context management and error message generation
530 %************************************************************************
533 setErrCtxtM, addErrCtxtM :: (TidyEnv -> TcM (TidyEnv, Message)) -> TcM a -> TcM a
534 setErrCtxtM msg = updCtxt (\ msgs -> [msg])
535 addErrCtxtM msg = updCtxt (\ msgs -> msg : msgs)
537 setErrCtxt, addErrCtxt :: Message -> TcM a -> TcM a
538 setErrCtxt msg = setErrCtxtM (\env -> returnM (env, msg))
539 addErrCtxt msg = addErrCtxtM (\env -> returnM (env, msg))
541 popErrCtxt :: TcM a -> TcM a
542 popErrCtxt = updCtxt (\ msgs -> case msgs of { [] -> []; (m:ms) -> ms })
544 getErrCtxt :: TcM ErrCtxt
545 getErrCtxt = do { env <- getLclEnv ; return (tcl_ctxt env) }
547 -- Helper function for the above
548 updCtxt :: (ErrCtxt -> ErrCtxt) -> TcM a -> TcM a
549 updCtxt upd = updLclEnv (\ env@(TcLclEnv { tcl_ctxt = ctxt }) ->
550 env { tcl_ctxt = upd ctxt })
552 getInstLoc :: InstOrigin -> TcM InstLoc
554 = do { loc <- getSrcLocM ; env <- getLclEnv ;
555 return (InstLoc origin loc (tcl_ctxt env)) }
557 addInstCtxt :: InstLoc -> TcM a -> TcM a
558 -- Add the SrcLoc and context from the first Inst in the list
559 -- (they all have similar locations)
560 addInstCtxt (InstLoc _ src_loc ctxt) thing_inside
561 = addSrcLoc src_loc (updCtxt (\ old_ctxt -> ctxt) thing_inside)
564 The addErrTc functions add an error message, but do not cause failure.
565 The 'M' variants pass a TidyEnv that has already been used to
566 tidy up the message; we then use it to tidy the context messages
569 addErrTc :: Message -> TcM ()
570 addErrTc err_msg = addErrTcM (emptyTidyEnv, err_msg)
572 addErrsTc :: [Message] -> TcM ()
573 addErrsTc err_msgs = mappM_ addErrTc err_msgs
575 addErrTcM :: (TidyEnv, Message) -> TcM ()
576 addErrTcM (tidy_env, err_msg)
577 = do { ctxt <- getErrCtxt ;
579 add_err_tcm tidy_env err_msg loc ctxt }
582 The failWith functions add an error message and cause failure
585 failWithTc :: Message -> TcM a -- Add an error message and fail
587 = addErrTc err_msg >> failM
589 failWithTcM :: (TidyEnv, Message) -> TcM a -- Add an error message and fail
590 failWithTcM local_and_msg
591 = addErrTcM local_and_msg >> failM
593 checkTc :: Bool -> Message -> TcM () -- Check that the boolean is true
594 checkTc True err = returnM ()
595 checkTc False err = failWithTc err
598 Warnings have no 'M' variant, nor failure
601 addWarnTc :: Message -> TcM ()
603 = do { ctxt <- getErrCtxt ;
604 ctxt_msgs <- do_ctxt emptyTidyEnv ctxt ;
605 addWarn (vcat (msg : ctxt_to_use ctxt_msgs)) }
607 warnTc :: Bool -> Message -> TcM ()
608 warnTc warn_if_true warn_msg
609 | warn_if_true = addWarnTc warn_msg
610 | otherwise = return ()
616 add_err_tcm tidy_env err_msg loc ctxt
617 = do { ctxt_msgs <- do_ctxt tidy_env ctxt ;
618 addErrAt loc (vcat (err_msg : ctxt_to_use ctxt_msgs)) }
622 do_ctxt tidy_env (c:cs)
623 = do { (tidy_env', m) <- c tidy_env ;
624 ms <- do_ctxt tidy_env' cs ;
627 ctxt_to_use ctxt | opt_PprStyle_Debug = ctxt
628 | otherwise = take 3 ctxt
631 %************************************************************************
633 Type constraints (the so-called LIE)
635 %************************************************************************
638 getLIEVar :: TcM (TcRef LIE)
639 getLIEVar = do { env <- getLclEnv; return (tcl_lie env) }
641 setLIEVar :: TcRef LIE -> TcM a -> TcM a
642 setLIEVar lie_var = updLclEnv (\ env -> env { tcl_lie = lie_var })
644 getLIE :: TcM a -> TcM (a, [Inst])
645 -- (getLIE m) runs m, and returns the type constraints it generates
647 = do { lie_var <- newMutVar emptyLIE ;
648 res <- updLclEnv (\ env -> env { tcl_lie = lie_var })
650 lie <- readMutVar lie_var ;
651 return (res, lieToList lie) }
653 extendLIE :: Inst -> TcM ()
655 = do { lie_var <- getLIEVar ;
656 lie <- readMutVar lie_var ;
657 writeMutVar lie_var (inst `consLIE` lie) }
659 extendLIEs :: [Inst] -> TcM ()
663 = do { lie_var <- getLIEVar ;
664 lie <- readMutVar lie_var ;
665 writeMutVar lie_var (mkLIE insts `plusLIE` lie) }
669 setLclTypeEnv :: TcLclEnv -> TcM a -> TcM a
670 -- Set the local type envt, but do *not* disturb other fields,
671 -- notably the lie_var
672 setLclTypeEnv lcl_env thing_inside
673 = updLclEnv upd thing_inside
675 upd env = env { tcl_env = tcl_env lcl_env,
676 tcl_tyvars = tcl_tyvars lcl_env }
680 %************************************************************************
682 Template Haskell context
684 %************************************************************************
687 getStage :: TcM ThStage
688 getStage = do { env <- getLclEnv; return (tcl_th_ctxt env) }
690 setStage :: ThStage -> TcM a -> TcM a
691 setStage s = updLclEnv (\ env -> env { tcl_th_ctxt = s })
695 %************************************************************************
699 %************************************************************************
702 popArrowBinders :: TcM a -> TcM a -- Move to the left of a (-<); see comments in TcRnTypes
704 = updLclEnv (\ env -> env { tcl_arrow_ctxt = pop (tcl_arrow_ctxt env) })
706 pop (ArrCtxt {proc_level = curr_lvl, proc_banned = banned})
707 = ASSERT( not (curr_lvl `elem` banned) )
708 ArrCtxt {proc_level = curr_lvl, proc_banned = curr_lvl : banned}
710 getBannedProcLevels :: TcM [ProcLevel]
711 = do { env <- getLclEnv; return (proc_banned (tcl_arrow_ctxt env)) }
713 incProcLevel :: TcM a -> TcM a
715 = updLclEnv (\ env -> env { tcl_arrow_ctxt = inc (tcl_arrow_ctxt env) })
717 inc ctxt = ctxt { proc_level = proc_level ctxt + 1 }
721 %************************************************************************
723 Stuff for the renamer's local env
725 %************************************************************************
728 getLocalRdrEnv :: RnM LocalRdrEnv
729 getLocalRdrEnv = do { env <- getLclEnv; return (tcl_rdr env) }
731 setLocalRdrEnv :: LocalRdrEnv -> RnM a -> RnM a
732 setLocalRdrEnv rdr_env thing_inside
733 = updLclEnv (\env -> env {tcl_rdr = rdr_env}) thing_inside
737 %************************************************************************
739 Stuff for interface decls
741 %************************************************************************
744 initIfaceTcRn :: IfG a -> TcRn a
745 initIfaceTcRn thing_inside
746 = do { tcg_env <- getGblEnv
747 ; let { if_env = IfGblEnv {
748 if_rec_types = Just (tcg_mod tcg_env, get_type_env),
749 if_is_boot = imp_dep_mods (tcg_imports tcg_env) }
750 ; get_type_env = readMutVar (tcg_type_env_var tcg_env) }
751 ; setEnvs (if_env, ()) thing_inside }
753 initIfaceExtCore :: IfL a -> TcRn a
754 initIfaceExtCore thing_inside
755 = do { tcg_env <- getGblEnv
756 ; let { mod = tcg_mod tcg_env
757 ; if_env = IfGblEnv {
758 if_rec_types = Just (mod, return (tcg_type_env tcg_env)),
759 if_is_boot = imp_dep_mods (tcg_imports tcg_env) }
760 ; if_lenv = IfLclEnv { if_mod = moduleName mod,
761 if_tv_env = emptyOccEnv,
762 if_id_env = emptyOccEnv }
764 ; setEnvs (if_env, if_lenv) thing_inside }
766 initIfaceCheck :: HscEnv -> IfG a -> IO a
767 -- Used when checking the up-to-date-ness of the old Iface
768 -- Initialise the environment with no useful info at all
769 initIfaceCheck hsc_env do_this
770 = do { let { gbl_env = IfGblEnv { if_is_boot = emptyModuleEnv,
771 if_rec_types = Nothing } ;
773 ; initTcRnIf 'i' hsc_env gbl_env () do_this
776 initIfaceTc :: HscEnv -> ModIface
777 -> (TcRef TypeEnv -> IfL a) -> IO a
778 -- Used when type-checking checking an up-to-date interface file
779 -- No type envt from the current module, but we do know the module dependencies
780 initIfaceTc hsc_env iface do_this
781 = do { tc_env_var <- newIORef emptyTypeEnv
782 ; let { gbl_env = IfGblEnv { if_is_boot = mkModDeps (dep_mods (mi_deps iface)),
783 if_rec_types = Just (mod, readMutVar tc_env_var) } ;
784 ; if_lenv = IfLclEnv { if_mod = moduleName mod,
785 if_tv_env = emptyOccEnv,
786 if_id_env = emptyOccEnv }
788 ; initTcRnIf 'i' hsc_env gbl_env if_lenv (do_this tc_env_var)
791 mod = mi_module iface
793 initIfaceRules :: HscEnv -> ModGuts -> IfG a -> IO a
794 -- Used when sucking in new Rules in SimplCore
795 -- We have available the type envt of the module being compiled, and we must use it
796 initIfaceRules hsc_env guts do_this
798 is_boot = mkModDeps (dep_mods (mg_deps guts))
799 -- Urgh! But we do somehow need to get the info
800 -- on whether (for this particular compilation) we should
801 -- import a hi-boot file or not.
802 ; type_info = (mg_module guts, return (mg_types guts))
803 ; gbl_env = IfGblEnv { if_is_boot = is_boot,
804 if_rec_types = Just type_info } ;
807 -- Run the thing; any exceptions just bubble out from here
808 ; initTcRnIf 'i' hsc_env gbl_env () do_this
811 initIfaceLcl :: ModuleName -> IfL a -> IfM lcl a
812 initIfaceLcl mod thing_inside
813 = setLclEnv (IfLclEnv { if_mod = mod,
814 if_tv_env = emptyOccEnv,
815 if_id_env = emptyOccEnv })
820 forkM_maybe :: SDoc -> IfL a -> IfL (Maybe a)
821 -- Run thing_inside in an interleaved thread.
822 -- It shares everything with the parent thread, so this is DANGEROUS.
824 -- It returns Nothing if the computation fails
826 -- It's used for lazily type-checking interface
827 -- signatures, which is pretty benign
829 forkM_maybe doc thing_inside
830 = do { unsafeInterleaveM $
831 do { traceIf (text "Starting fork {" <+> doc)
832 ; mb_res <- tryM thing_inside ;
834 Right r -> do { traceIf (text "} ending fork" <+> doc)
838 -- Bleat about errors in the forked thread, if -ddump-if-trace is on
839 -- Otherwise we silently discard errors. Errors can legitimately
840 -- happen when compiling interface signatures (see tcInterfaceSigs)
841 ifOptM Opt_D_dump_if_trace
842 (print_errs (hang (text "forkM failed:" <+> doc)
843 4 (text (show exn))))
845 ; traceIf (text "} ending fork (badly)" <+> doc)
849 print_errs sdoc = ioToIOEnv (printErrs (sdoc defaultErrStyle))
851 forkM :: SDoc -> IfL a -> IfL a
852 forkM doc thing_inside
853 = do { mb_res <- forkM_maybe doc thing_inside
854 ; return (case mb_res of
855 Nothing -> pprPanic "forkM" doc