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 addReport :: 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 addWarn :: Message -> TcRn ()
398 addWarn msg = addReport (ptext SLIT("Warning:") <+> msg)
400 checkErr :: Bool -> Message -> TcRn ()
401 -- Add the error if the bool is False
402 checkErr ok msg = checkM ok (addErr msg)
404 warnIf :: Bool -> Message -> TcRn ()
405 warnIf True msg = addWarn msg
406 warnIf False msg = return ()
408 addMessages :: Messages -> TcRn ()
409 addMessages (m_warns, m_errs)
410 = do { errs_var <- getErrsVar ;
411 (warns, errs) <- readMutVar errs_var ;
412 writeMutVar errs_var (warns `unionBags` m_warns,
413 errs `unionBags` m_errs) }
415 discardWarnings :: TcRn a -> TcRn a
416 -- Ignore warnings inside the thing inside;
417 -- used to ignore-unused-variable warnings inside derived code
418 -- With -dppr-debug, the effects is switched off, so you can still see
419 -- what warnings derived code would give
420 discardWarnings thing_inside
421 | opt_PprStyle_Debug = thing_inside
423 = do { errs_var <- newMutVar emptyMessages
424 ; result <- setErrsVar errs_var thing_inside
425 ; (_warns, errs) <- readMutVar errs_var
426 ; addMessages (emptyBag, errs)
432 recoverM :: TcRn r -- Recovery action; do this if the main one fails
433 -> TcRn r -- Main action: do this first
435 recoverM recover thing
436 = do { mb_res <- try_m thing ;
439 Right res -> returnM res }
441 tryTc :: TcRn a -> TcRn (Messages, Maybe a)
442 -- (tryTc m) executes m, and returns
443 -- Just r, if m succeeds (returning r) and caused no errors
444 -- Nothing, if m fails, or caused errors
445 -- It also returns all the errors accumulated by m
446 -- (even in the Just case, there might be warnings)
448 -- It always succeeds (never raises an exception)
450 = do { errs_var <- newMutVar emptyMessages ;
452 mb_r <- try_m (setErrsVar errs_var m) ;
454 new_errs <- readMutVar errs_var ;
461 Right r | errorsFound dflags new_errs -> Nothing
462 | otherwise -> Just r)
465 try_m :: TcRn r -> TcRn (Either Exception r)
466 -- Does try_m, with a debug-trace on failure
468 = do { mb_r <- tryM thing ;
470 Left exn -> do { traceTc (exn_msg exn); return mb_r }
471 Right r -> return mb_r }
473 exn_msg exn = text "tryTc/recoverM recovering from" <+> text (showException exn)
475 tryTcLIE :: TcM a -> TcM (Messages, Maybe a)
476 -- Just like tryTc, except that it ensures that the LIE
477 -- for the thing is propagated only if there are no errors
478 -- Hence it's restricted to the type-check monad
479 tryTcLIE thing_inside
480 = do { ((errs, mb_r), lie) <- getLIE (tryTc thing_inside) ;
481 ifM (isJust mb_r) (extendLIEs lie) ;
482 return (errs, mb_r) }
484 tryTcLIE_ :: TcM r -> TcM r -> TcM r
485 -- (tryTcLIE_ r m) tries m; if it succeeds it returns it,
486 -- otherwise it returns r. Any error messages added by m are discarded,
487 -- whether or not m succeeds.
488 tryTcLIE_ recover main
489 = do { (_msgs, mb_res) <- tryTcLIE main ;
491 Just res -> return res
494 checkNoErrs :: TcM r -> TcM r
495 -- (checkNoErrs m) succeeds iff m succeeds and generates no errors
496 -- If m fails then (checkNoErrsTc m) fails.
497 -- If m succeeds, it checks whether m generated any errors messages
498 -- (it might have recovered internally)
499 -- If so, it fails too.
500 -- Regardless, any errors generated by m are propagated to the enclosing context.
502 = do { (msgs, mb_res) <- tryTcLIE main ;
509 ifErrsM :: TcRn r -> TcRn r -> TcRn r
510 -- ifErrsM bale_out main
511 -- does 'bale_out' if there are errors in errors collection
512 -- otherwise does 'main'
513 ifErrsM bale_out normal
514 = do { errs_var <- getErrsVar ;
515 msgs <- readMutVar errs_var ;
517 if errorsFound dflags msgs then
522 failIfErrsM :: TcRn ()
523 -- Useful to avoid error cascades
524 failIfErrsM = ifErrsM failM (return ())
528 %************************************************************************
530 Context management and error message generation
533 %************************************************************************
536 setErrCtxtM, addErrCtxtM :: (TidyEnv -> TcM (TidyEnv, Message)) -> TcM a -> TcM a
537 setErrCtxtM msg = updCtxt (\ msgs -> [msg])
538 addErrCtxtM msg = updCtxt (\ msgs -> msg : msgs)
540 setErrCtxt, addErrCtxt :: Message -> TcM a -> TcM a
541 setErrCtxt msg = setErrCtxtM (\env -> returnM (env, msg))
542 addErrCtxt msg = addErrCtxtM (\env -> returnM (env, msg))
544 popErrCtxt :: TcM a -> TcM a
545 popErrCtxt = updCtxt (\ msgs -> case msgs of { [] -> []; (m:ms) -> ms })
547 getErrCtxt :: TcM ErrCtxt
548 getErrCtxt = do { env <- getLclEnv ; return (tcl_ctxt env) }
550 -- Helper function for the above
551 updCtxt :: (ErrCtxt -> ErrCtxt) -> TcM a -> TcM a
552 updCtxt upd = updLclEnv (\ env@(TcLclEnv { tcl_ctxt = ctxt }) ->
553 env { tcl_ctxt = upd ctxt })
555 getInstLoc :: InstOrigin -> TcM InstLoc
557 = do { loc <- getSrcLocM ; env <- getLclEnv ;
558 return (InstLoc origin loc (tcl_ctxt env)) }
560 addInstCtxt :: InstLoc -> TcM a -> TcM a
561 -- Add the SrcLoc and context from the first Inst in the list
562 -- (they all have similar locations)
563 addInstCtxt (InstLoc _ src_loc ctxt) thing_inside
564 = addSrcLoc src_loc (updCtxt (\ old_ctxt -> ctxt) thing_inside)
567 The addErrTc functions add an error message, but do not cause failure.
568 The 'M' variants pass a TidyEnv that has already been used to
569 tidy up the message; we then use it to tidy the context messages
572 addErrTc :: Message -> TcM ()
573 addErrTc err_msg = addErrTcM (emptyTidyEnv, err_msg)
575 addErrsTc :: [Message] -> TcM ()
576 addErrsTc err_msgs = mappM_ addErrTc err_msgs
578 addErrTcM :: (TidyEnv, Message) -> TcM ()
579 addErrTcM (tidy_env, err_msg)
580 = do { ctxt <- getErrCtxt ;
582 add_err_tcm tidy_env err_msg loc ctxt }
585 The failWith functions add an error message and cause failure
588 failWithTc :: Message -> TcM a -- Add an error message and fail
590 = addErrTc err_msg >> failM
592 failWithTcM :: (TidyEnv, Message) -> TcM a -- Add an error message and fail
593 failWithTcM local_and_msg
594 = addErrTcM local_and_msg >> failM
596 checkTc :: Bool -> Message -> TcM () -- Check that the boolean is true
597 checkTc True err = returnM ()
598 checkTc False err = failWithTc err
601 Warnings have no 'M' variant, nor failure
604 addWarnTc :: Message -> TcM ()
606 = do { ctxt <- getErrCtxt ;
607 ctxt_msgs <- do_ctxt emptyTidyEnv ctxt ;
608 addWarn (vcat (msg : ctxt_to_use ctxt_msgs)) }
610 warnTc :: Bool -> Message -> TcM ()
611 warnTc warn_if_true warn_msg
612 | warn_if_true = addWarnTc warn_msg
613 | otherwise = return ()
619 add_err_tcm tidy_env err_msg loc ctxt
620 = do { ctxt_msgs <- do_ctxt tidy_env ctxt ;
621 addErrAt loc (vcat (err_msg : ctxt_to_use ctxt_msgs)) }
625 do_ctxt tidy_env (c:cs)
626 = do { (tidy_env', m) <- c tidy_env ;
627 ms <- do_ctxt tidy_env' cs ;
630 ctxt_to_use ctxt | opt_PprStyle_Debug = ctxt
631 | otherwise = take 3 ctxt
634 %************************************************************************
636 Type constraints (the so-called LIE)
638 %************************************************************************
641 getLIEVar :: TcM (TcRef LIE)
642 getLIEVar = do { env <- getLclEnv; return (tcl_lie env) }
644 setLIEVar :: TcRef LIE -> TcM a -> TcM a
645 setLIEVar lie_var = updLclEnv (\ env -> env { tcl_lie = lie_var })
647 getLIE :: TcM a -> TcM (a, [Inst])
648 -- (getLIE m) runs m, and returns the type constraints it generates
650 = do { lie_var <- newMutVar emptyLIE ;
651 res <- updLclEnv (\ env -> env { tcl_lie = lie_var })
653 lie <- readMutVar lie_var ;
654 return (res, lieToList lie) }
656 extendLIE :: Inst -> TcM ()
658 = do { lie_var <- getLIEVar ;
659 lie <- readMutVar lie_var ;
660 writeMutVar lie_var (inst `consLIE` lie) }
662 extendLIEs :: [Inst] -> TcM ()
666 = do { lie_var <- getLIEVar ;
667 lie <- readMutVar lie_var ;
668 writeMutVar lie_var (mkLIE insts `plusLIE` lie) }
672 setLclTypeEnv :: TcLclEnv -> TcM a -> TcM a
673 -- Set the local type envt, but do *not* disturb other fields,
674 -- notably the lie_var
675 setLclTypeEnv lcl_env thing_inside
676 = updLclEnv upd thing_inside
678 upd env = env { tcl_env = tcl_env lcl_env,
679 tcl_tyvars = tcl_tyvars lcl_env }
683 %************************************************************************
685 Template Haskell context
687 %************************************************************************
690 getStage :: TcM ThStage
691 getStage = do { env <- getLclEnv; return (tcl_th_ctxt env) }
693 setStage :: ThStage -> TcM a -> TcM a
694 setStage s = updLclEnv (\ env -> env { tcl_th_ctxt = s })
698 %************************************************************************
702 %************************************************************************
705 popArrowBinders :: TcM a -> TcM a -- Move to the left of a (-<); see comments in TcRnTypes
707 = updLclEnv (\ env -> env { tcl_arrow_ctxt = pop (tcl_arrow_ctxt env) })
709 pop (ArrCtxt {proc_level = curr_lvl, proc_banned = banned})
710 = ASSERT( not (curr_lvl `elem` banned) )
711 ArrCtxt {proc_level = curr_lvl, proc_banned = curr_lvl : banned}
713 getBannedProcLevels :: TcM [ProcLevel]
714 = do { env <- getLclEnv; return (proc_banned (tcl_arrow_ctxt env)) }
716 incProcLevel :: TcM a -> TcM a
718 = updLclEnv (\ env -> env { tcl_arrow_ctxt = inc (tcl_arrow_ctxt env) })
720 inc ctxt = ctxt { proc_level = proc_level ctxt + 1 }
724 %************************************************************************
726 Stuff for the renamer's local env
728 %************************************************************************
731 getLocalRdrEnv :: RnM LocalRdrEnv
732 getLocalRdrEnv = do { env <- getLclEnv; return (tcl_rdr env) }
734 setLocalRdrEnv :: LocalRdrEnv -> RnM a -> RnM a
735 setLocalRdrEnv rdr_env thing_inside
736 = updLclEnv (\env -> env {tcl_rdr = rdr_env}) thing_inside
740 %************************************************************************
742 Stuff for interface decls
744 %************************************************************************
747 initIfaceTcRn :: IfG a -> TcRn a
748 initIfaceTcRn thing_inside
749 = do { tcg_env <- getGblEnv
750 ; let { if_env = IfGblEnv {
751 if_rec_types = Just (tcg_mod tcg_env, get_type_env),
752 if_is_boot = imp_dep_mods (tcg_imports tcg_env) }
753 ; get_type_env = readMutVar (tcg_type_env_var tcg_env) }
754 ; setEnvs (if_env, ()) thing_inside }
756 initIfaceExtCore :: IfL a -> TcRn a
757 initIfaceExtCore thing_inside
758 = do { tcg_env <- getGblEnv
759 ; let { mod = tcg_mod tcg_env
760 ; if_env = IfGblEnv {
761 if_rec_types = Just (mod, return (tcg_type_env tcg_env)),
762 if_is_boot = imp_dep_mods (tcg_imports tcg_env) }
763 ; if_lenv = IfLclEnv { if_mod = moduleName mod,
764 if_tv_env = emptyOccEnv,
765 if_id_env = emptyOccEnv }
767 ; setEnvs (if_env, if_lenv) thing_inside }
769 initIfaceCheck :: HscEnv -> IfG a -> IO a
770 -- Used when checking the up-to-date-ness of the old Iface
771 -- Initialise the environment with no useful info at all
772 initIfaceCheck hsc_env do_this
773 = do { let { gbl_env = IfGblEnv { if_is_boot = emptyModuleEnv,
774 if_rec_types = Nothing } ;
776 ; initTcRnIf 'i' hsc_env gbl_env () do_this
779 initIfaceTc :: HscEnv -> ModIface
780 -> (TcRef TypeEnv -> IfL a) -> IO a
781 -- Used when type-checking checking an up-to-date interface file
782 -- No type envt from the current module, but we do know the module dependencies
783 initIfaceTc hsc_env iface do_this
784 = do { tc_env_var <- newIORef emptyTypeEnv
785 ; let { gbl_env = IfGblEnv { if_is_boot = mkModDeps (dep_mods (mi_deps iface)),
786 if_rec_types = Just (mod, readMutVar tc_env_var) } ;
787 ; if_lenv = IfLclEnv { if_mod = moduleName mod,
788 if_tv_env = emptyOccEnv,
789 if_id_env = emptyOccEnv }
791 ; initTcRnIf 'i' hsc_env gbl_env if_lenv (do_this tc_env_var)
794 mod = mi_module iface
796 initIfaceRules :: HscEnv -> ModGuts -> IfG a -> IO a
797 -- Used when sucking in new Rules in SimplCore
798 -- We have available the type envt of the module being compiled, and we must use it
799 initIfaceRules hsc_env guts do_this
801 is_boot = mkModDeps (dep_mods (mg_deps guts))
802 -- Urgh! But we do somehow need to get the info
803 -- on whether (for this particular compilation) we should
804 -- import a hi-boot file or not.
805 ; type_info = (mg_module guts, return (mg_types guts))
806 ; gbl_env = IfGblEnv { if_is_boot = is_boot,
807 if_rec_types = Just type_info } ;
810 -- Run the thing; any exceptions just bubble out from here
811 ; initTcRnIf 'i' hsc_env gbl_env () do_this
814 initIfaceLcl :: ModuleName -> IfL a -> IfM lcl a
815 initIfaceLcl mod thing_inside
816 = setLclEnv (IfLclEnv { if_mod = mod,
817 if_tv_env = emptyOccEnv,
818 if_id_env = emptyOccEnv })
823 forkM_maybe :: SDoc -> IfL a -> IfL (Maybe a)
824 -- Run thing_inside in an interleaved thread.
825 -- It shares everything with the parent thread, so this is DANGEROUS.
827 -- It returns Nothing if the computation fails
829 -- It's used for lazily type-checking interface
830 -- signatures, which is pretty benign
832 forkM_maybe doc thing_inside
833 = do { unsafeInterleaveM $
834 do { traceIf (text "Starting fork {" <+> doc)
835 ; mb_res <- tryM thing_inside ;
837 Right r -> do { traceIf (text "} ending fork" <+> doc)
841 -- Bleat about errors in the forked thread, if -ddump-if-trace is on
842 -- Otherwise we silently discard errors. Errors can legitimately
843 -- happen when compiling interface signatures (see tcInterfaceSigs)
844 ifOptM Opt_D_dump_if_trace
845 (print_errs (hang (text "forkM failed:" <+> doc)
846 4 (text (show exn))))
848 ; traceIf (text "} ending fork (badly)" <+> doc)
852 print_errs sdoc = ioToIOEnv (printErrs (sdoc defaultErrStyle))
854 forkM :: SDoc -> IfL a -> IfL a
855 forkM doc thing_inside
856 = do { mb_res <- forkM_maybe doc thing_inside
857 ; return (case mb_res of
858 Nothing -> pprPanic "forkM" doc