7 #include "HsVersions.h"
9 import HsSyn ( MonoBinds(..) )
10 import HscTypes ( HscEnv(..), PersistentCompilerState(..),
11 emptyFixityEnv, emptyGlobalRdrEnv, TyThing,
12 ExternalPackageState(..), HomePackageTable,
13 ModDetails(..), HomeModInfo(..), Deprecations(..),
14 GlobalRdrEnv, LocalRdrEnv, NameCache, FixityEnv,
15 GhciMode, lookupType, unQualInScope )
17 import Module ( Module, unitModuleEnv, foldModuleEnv )
18 import Name ( Name, isInternalName )
20 import NameEnv ( extendNameEnvList )
21 import InstEnv ( InstEnv, extendInstEnv )
22 import TysWiredIn ( integerTy, doubleTy )
24 import VarSet ( emptyVarSet )
25 import VarEnv ( TidyEnv, emptyTidyEnv )
26 import RdrName ( emptyRdrEnv )
27 import ErrUtils ( Message, Messages, emptyMessages, errorsFound,
28 addShortErrLocLine, addShortWarnLocLine, printErrorsAndWarnings )
29 import SrcLoc ( SrcLoc, noSrcLoc )
30 import NameEnv ( emptyNameEnv )
31 import Bag ( emptyBag )
33 import UniqSupply ( UniqSupply, mkSplitUniqSupply, uniqFromSupply, splitUniqSupply )
34 import Unique ( Unique )
35 import CmdLineOpts ( DynFlags, DynFlag(..), dopt, opt_PprStyle_Debug )
36 import BasicTypes ( FixitySig )
37 import Bag ( snocBag, unionBags )
38 import Panic ( showException )
40 import Maybe ( isJust )
42 import DATA_IOREF ( newIORef, readIORef )
43 import EXCEPTION ( Exception )
46 %************************************************************************
48 Standard combinators, but specialised for this monad
51 6%************************************************************************
54 mappM :: (a -> TcRn m b) -> [a] -> TcRn m [b]
55 mappM_ :: (a -> TcRn m b) -> [a] -> TcRn m ()
56 -- Funny names to avoid clash with Prelude
57 sequenceM :: [TcRn m a] -> TcRn m [a]
58 foldlM :: (a -> b -> TcRn m a) -> a -> [b] -> TcRn m a
59 mapAndUnzipM :: (a -> TcRn m (b,c)) -> [a] -> TcRn m ([b],[c])
60 mapAndUnzip3M :: (a -> TcRn m (b,c,d)) -> [a] -> TcRn m ([b],[c],[d])
61 checkM :: Bool -> TcRn m () -> TcRn m () -- Perform arg if bool is False
62 ifM :: Bool -> TcRn m () -> TcRn m () -- Perform arg if bool is True
64 mappM f [] = return []
65 mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
67 mappM_ f [] = return ()
68 mappM_ f (x:xs) = f x >> mappM_ f xs
70 sequenceM [] = return []
71 sequenceM (x:xs) = do { r <- x; rs <- sequenceM xs; return (r:rs) }
73 foldlM k z [] = return z
74 foldlM k z (x:xs) = do { r <- k z x; foldlM k r xs }
76 mapAndUnzipM f [] = return ([],[])
77 mapAndUnzipM f (x:xs) = do { (r,s) <- f x;
78 (rs,ss) <- mapAndUnzipM f xs;
81 mapAndUnzip3M f [] = return ([],[], [])
82 mapAndUnzip3M f (x:xs) = do { (r,s,t) <- f x;
83 (rs,ss,ts) <- mapAndUnzip3M f xs;
84 return (r:rs, s:ss, t:ts) }
86 checkM True err = return ()
87 checkM False err = err
89 ifM True do_it = do_it
90 ifM False do_it = return ()
94 %************************************************************************
98 %************************************************************************
101 initTc :: HscEnv -> PersistentCompilerState
104 -> IO (PersistentCompilerState, Maybe r)
105 -- Nothing => error thrown by the thing inside
106 -- (error messages should have been printed already)
108 initTc (HscEnv { hsc_mode = ghci_mode,
110 hsc_dflags = dflags })
112 = do { us <- mkSplitUniqSupply 'a' ;
113 us_var <- newIORef us ;
114 errs_var <- newIORef (emptyBag, emptyBag) ;
115 tvs_var <- newIORef emptyVarSet ;
116 usg_var <- newIORef emptyUsages ;
117 nc_var <- newIORef (pcs_nc pcs) ;
118 eps_var <- newIORef eps ;
119 ie_var <- newIORef (mkImpInstEnv dflags eps hpt) ;
122 env = Env { env_top = top_env,
125 env_loc = noSrcLoc } ;
128 top_mode = ghci_mode,
134 top_errs = errs_var } ;
138 tcg_usages = usg_var,
139 tcg_rdr_env = emptyGlobalRdrEnv,
140 tcg_fix_env = emptyFixityEnv,
141 tcg_default = defaultDefaultTys,
142 tcg_type_env = emptyNameEnv,
143 tcg_inst_env = ie_var,
145 tcg_imports = init_imports,
146 tcg_binds = EmptyMonoBinds,
147 tcg_deprecs = NoDeprecs,
154 tcl_level = topStage,
155 tcl_env = emptyNameEnv,
156 tcl_tyvars = tvs_var,
157 tcl_lie = panic "initTc:LIE" } ;
158 -- LIE only valid inside a getLIE
161 -- OK, here's the business end!
162 maybe_res <- catch (do { res <- runTcRn env do_this ;
164 (\_ -> return Nothing) ;
166 -- Print any error messages
167 msgs <- readIORef errs_var ;
168 printErrorsAndWarnings msgs ;
170 -- Get final PCS and return
171 eps' <- readIORef eps_var ;
172 nc' <- readIORef nc_var ;
173 let { pcs' = PCS { pcs_EPS = eps', pcs_nc = nc' } ;
174 final_res | errorsFound dflags msgs = Nothing
175 | otherwise = maybe_res } ;
177 return (pcs', final_res)
182 init_imports = emptyImportAvails { imp_qual = unitModuleEnv mod emptyAvailEnv }
183 -- Initialise tcg_imports with an empty set of bindings for
184 -- this module, so that if we see 'module M' in the export
185 -- list, and there are no bindings in M, we don't bleat
186 -- "unknown module M".
188 defaultDefaultTys :: [Type]
189 defaultDefaultTys = [integerTy, doubleTy]
191 mkImpInstEnv :: DynFlags -> ExternalPackageState -> HomePackageTable -> InstEnv
192 -- At the moment we (wrongly) build an instance environment from all the
193 -- modules we have already compiled:
194 -- (a) eps_inst_env from the external package state
195 -- (b) all the md_insts in the home package table
196 -- We should really only get instances from modules below us in the
197 -- module import tree.
198 mkImpInstEnv dflags eps hpt
199 = foldModuleEnv (add . md_insts . hm_details)
203 -- We shouldn't get instance conflict errors from
204 -- the package and home type envs
205 add dfuns inst_env = WARN( not (null errs), vcat (map snd errs) ) inst_env'
207 (inst_env', errs) = extendInstEnv dflags inst_env dfuns
209 -- mkImpTypeEnv makes the imported symbol table
210 mkImpTypeEnv :: ExternalPackageState -> HomePackageTable
211 -> Name -> Maybe TyThing
212 mkImpTypeEnv pcs hpt = lookup
215 lookup name | isInternalName name = Nothing
216 | otherwise = lookupType hpt pte name
220 %************************************************************************
224 %************************************************************************
227 getTopEnv :: TcRn m TopEnv
228 getTopEnv = do { env <- getEnv; return (env_top env) }
230 getGblEnv :: TcRn m TcGblEnv
231 getGblEnv = do { env <- getEnv; return (env_gbl env) }
233 updGblEnv :: (TcGblEnv -> TcGblEnv) -> TcRn m a -> TcRn m a
234 updGblEnv upd = updEnv (\ env@(Env { env_gbl = gbl }) ->
235 env { env_gbl = upd gbl })
237 setGblEnv :: TcGblEnv -> TcRn m a -> TcRn m a
238 setGblEnv gbl_env = updEnv (\ env -> env { env_gbl = gbl_env })
240 getLclEnv :: TcRn m m
241 getLclEnv = do { env <- getEnv; return (env_lcl env) }
243 updLclEnv :: (m -> m) -> TcRn m a -> TcRn m a
244 updLclEnv upd = updEnv (\ env@(Env { env_lcl = lcl }) ->
245 env { env_lcl = upd lcl })
247 setLclEnv :: m -> TcRn m a -> TcRn n a
248 setLclEnv lcl_env = updEnv (\ env -> env { env_lcl = lcl_env })
254 getDOpts :: TcRn m DynFlags
255 getDOpts = do { env <- getTopEnv; return (top_dflags env) }
257 doptM :: DynFlag -> TcRn m Bool
258 doptM flag = do { dflags <- getDOpts; return (dopt flag dflags) }
260 ifOptM :: DynFlag -> TcRn m () -> TcRn m () -- Do it flag is true
261 ifOptM flag thing_inside = do { b <- doptM flag;
262 if b then thing_inside else return () }
264 getGhciMode :: TcRn m GhciMode
265 getGhciMode = do { env <- getTopEnv; return (top_mode env) }
269 getSrcLocM :: TcRn m SrcLoc
270 -- Avoid clash with Name.getSrcLoc
271 getSrcLocM = do { env <- getEnv; return (env_loc env) }
273 addSrcLoc :: SrcLoc -> TcRn m a -> TcRn m a
274 addSrcLoc loc = updEnv (\env -> env { env_loc = loc })
278 getEps :: TcRn m ExternalPackageState
279 getEps = do { env <- getTopEnv; readMutVar (top_eps env) }
281 setEps :: ExternalPackageState -> TcRn m ()
282 setEps eps = do { env <- getTopEnv; writeMutVar (top_eps env) eps }
284 getHpt :: TcRn m HomePackageTable
285 getHpt = do { env <- getTopEnv; return (top_hpt env) }
287 getModule :: TcRn m Module
288 getModule = do { env <- getGblEnv; return (tcg_mod env) }
290 getGlobalRdrEnv :: TcRn m GlobalRdrEnv
291 getGlobalRdrEnv = do { env <- getGblEnv; return (tcg_rdr_env env) }
293 getImports :: TcRn m ImportAvails
294 getImports = do { env <- getGblEnv; return (tcg_imports env) }
296 getFixityEnv :: TcRn m FixityEnv
297 getFixityEnv = do { env <- getGblEnv; return (tcg_fix_env env) }
299 extendFixityEnv :: [(Name,FixitySig Name)] -> RnM a -> RnM a
300 extendFixityEnv new_bit
301 = updGblEnv (\env@(TcGblEnv { tcg_fix_env = old_fix_env }) ->
302 env {tcg_fix_env = extendNameEnvList old_fix_env new_bit})
304 getDefaultTys :: TcRn m [Type]
305 getDefaultTys = do { env <- getGblEnv; return (tcg_default env) }
309 getUsageVar :: TcRn m (TcRef EntityUsage)
310 getUsageVar = do { env <- getGblEnv; return (tcg_usages env) }
312 getUsages :: TcRn m EntityUsage
313 getUsages = do { usg_var <- getUsageVar; readMutVar usg_var }
315 updUsages :: (EntityUsage -> EntityUsage) -> TcRn m ()
316 updUsages upd = do { usg_var <- getUsageVar ;
317 usg <- readMutVar usg_var ;
318 writeMutVar usg_var (upd usg) }
322 %************************************************************************
326 %************************************************************************
329 getErrsVar :: TcRn m (TcRef Messages)
330 getErrsVar = do { env <- getTopEnv; return (top_errs env) }
332 setErrsVar :: TcRef Messages -> TcRn m a -> TcRn m a
333 setErrsVar v = updEnv (\ env@(Env { env_top = top_env }) ->
334 env { env_top = top_env { top_errs = v }})
336 addErr :: Message -> TcRn m ()
337 addErr msg = do { loc <- getSrcLocM ; addErrAt loc msg }
339 addErrAt :: SrcLoc -> Message -> TcRn m ()
341 = do { errs_var <- getErrsVar ;
342 rdr_env <- getGlobalRdrEnv ;
343 let { err = addShortErrLocLine loc (unQualInScope rdr_env) msg } ;
344 (warns, errs) <- readMutVar errs_var ;
345 writeMutVar errs_var (warns, errs `snocBag` err) }
347 addErrs :: [(SrcLoc,Message)] -> TcRn m ()
348 addErrs msgs = mappM_ add msgs
350 add (loc,msg) = addErrAt loc msg
352 addWarn :: Message -> TcRn m ()
354 = do { errs_var <- getErrsVar ;
356 rdr_env <- getGlobalRdrEnv ;
357 let { warn = addShortWarnLocLine loc (unQualInScope rdr_env) msg } ;
358 (warns, errs) <- readMutVar errs_var ;
359 writeMutVar errs_var (warns `snocBag` warn, errs) }
361 checkErr :: Bool -> Message -> TcRn m ()
362 -- Add the error if the bool is False
363 checkErr ok msg = checkM ok (addErr msg)
365 warnIf :: Bool -> Message -> TcRn m ()
366 warnIf True msg = addWarn msg
367 warnIf False msg = return ()
369 addMessages :: Messages -> TcRn m ()
370 addMessages (m_warns, m_errs)
371 = do { errs_var <- getErrsVar ;
372 (warns, errs) <- readMutVar errs_var ;
373 writeMutVar errs_var (warns `unionBags` m_warns,
374 errs `unionBags` m_errs) }
379 recoverM :: TcRn m r -- Recovery action; do this if the main one fails
380 -> TcRn m r -- Main action: do this first
382 recoverM recover thing
383 = do { mb_res <- try_m thing ;
386 Right res -> returnM res }
388 tryTc :: TcRn m a -> TcRn m (Messages, Maybe a)
389 -- (tryTc m) executes m, and returns
390 -- Just r, if m succeeds (returning r) and caused no errors
391 -- Nothing, if m fails, or caused errors
392 -- It also returns all the errors accumulated by m
393 -- (even in the Just case, there might be warnings)
395 -- It always succeeds (never raises an exception)
397 = do { errs_var <- newMutVar emptyMessages ;
399 mb_r <- try_m (setErrsVar errs_var m) ;
401 new_errs <- readMutVar errs_var ;
408 Right r | errorsFound dflags new_errs -> Nothing
409 | otherwise -> Just r)
412 try_m :: TcRn m r -> TcRn m (Either Exception r)
413 -- Does try_m, with a debug-trace on failure
415 = do { mb_r <- tryM thing ;
417 Left exn -> do { traceTc (exn_msg exn); return mb_r }
418 Right r -> return mb_r }
420 exn_msg exn = text "recoverM recovering from" <+> text (showException exn)
422 tryTcLIE :: TcM a -> TcM (Messages, Maybe a)
423 -- Just like tryTc, except that it ensures that the LIE
424 -- for the thing is propagated only if there are no errors
425 -- Hence it's restricted to the type-check monad
426 tryTcLIE thing_inside
427 = do { ((errs, mb_r), lie) <- getLIE (tryTc thing_inside) ;
428 ifM (isJust mb_r) (extendLIEs lie) ;
429 return (errs, mb_r) }
431 tryTcLIE_ :: TcM r -> TcM r -> TcM r
432 -- (tryTcLIE_ r m) tries m; if it succeeds it returns it,
433 -- otherwise it returns r. Any error messages added by m are discarded,
434 -- whether or not m succeeds.
435 tryTcLIE_ recover main
436 = do { (_msgs, mb_res) <- tryTcLIE main ;
438 Just res -> return res
441 checkNoErrs :: TcM r -> TcM r
442 -- (checkNoErrs m) succeeds iff m succeeds and generates no errors
443 -- If m fails then (checkNoErrsTc m) fails.
444 -- If m succeeds, it checks whether m generated any errors messages
445 -- (it might have recovered internally)
446 -- If so, it fails too.
447 -- Regardless, any errors generated by m are propagated to the enclosing context.
449 = do { (msgs, mb_res) <- tryTcLIE main ;
456 ifErrsM :: TcRn m r -> TcRn m r -> TcRn m r
457 -- ifErrsM bale_out main
458 -- does 'bale_out' if there are errors in errors collection
459 -- otherwise does 'main'
460 ifErrsM bale_out normal
461 = do { errs_var <- getErrsVar ;
462 msgs <- readMutVar errs_var ;
464 if errorsFound dflags msgs then
469 failIfErrsM :: TcRn m ()
470 -- Useful to avoid error cascades
471 failIfErrsM = ifErrsM failM (return ())
475 forkM :: SDoc -> TcM a -> TcM (Maybe a)
476 -- Run thing_inside in an interleaved thread. It gets a separate
479 -- * LIE var is set to bottom (should never be used)
480 -- but everything else is shared, so this is DANGEROUS.
482 -- It returns Nothing if the computation fails
484 -- It's used for lazily type-checking interface
485 -- signatures, which is pretty benign
487 forkM doc thing_inside
488 = do { us <- newUniqueSupply ;
490 do { us_var <- newMutVar us ;
491 (msgs, mb_res) <- tryTc (setLIEVar (panic "forkM: LIE used") $
492 setUsVar us_var thing_inside) ;
494 Just r -> return (Just r)
497 -- Bleat about errors in the forked thread, if -ddump-tc-trace is on
498 -- Otherwise we silently discard errors. Errors can legitimately
499 -- happen when compiling interface signatures (see tcInterfaceSigs)
500 ifOptM Opt_D_dump_tc_trace
501 (ioToTcRn (do { printErrs (hdr_doc defaultErrStyle) ;
502 printErrorsAndWarnings msgs })) ;
507 hdr_doc = text "forkM failed:" <+> doc
511 %************************************************************************
515 %************************************************************************
518 getUsVar :: TcRn m (TcRef UniqSupply)
519 getUsVar = do { env <- getTopEnv; return (top_us env) }
521 setUsVar :: TcRef UniqSupply -> TcRn m a -> TcRn m a
522 setUsVar v = updEnv (\ env@(Env { env_top = top_env }) ->
523 env { env_top = top_env { top_us = v }})
525 newUnique :: TcRn m Unique
526 newUnique = do { us <- newUniqueSupply ;
527 return (uniqFromSupply us) }
529 newUniqueSupply :: TcRn m UniqSupply
531 = do { u_var <- getUsVar ;
532 us <- readMutVar u_var ;
533 let { (us1, us2) = splitUniqSupply us } ;
534 writeMutVar u_var us1 ;
540 getNameCache :: TcRn m NameCache
541 getNameCache = do { TopEnv { top_nc = nc_var } <- getTopEnv;
544 setNameCache :: NameCache -> TcRn m ()
545 setNameCache nc = do { TopEnv { top_nc = nc_var } <- getTopEnv;
546 writeMutVar nc_var nc }
550 %************************************************************************
554 %************************************************************************
557 traceTc, traceRn :: SDoc -> TcRn a ()
558 traceRn = dumpOptTcRn Opt_D_dump_rn_trace
559 traceTc = dumpOptTcRn Opt_D_dump_tc_trace
560 traceSplice = dumpOptTcRn Opt_D_dump_splices
561 traceHiDiffs = dumpOptTcRn Opt_D_dump_hi_diffs
563 dumpOptTcRn :: DynFlag -> SDoc -> TcRn a ()
564 dumpOptTcRn flag doc = ifOptM flag (dumpTcRn doc)
566 dumpTcRn :: SDoc -> TcRn a ()
567 dumpTcRn doc = do { rdr_env <- getGlobalRdrEnv ;
568 ioToTcRn (printForUser stderr (unQualInScope rdr_env) doc) }
572 %************************************************************************
574 Context management and error message generation
577 %************************************************************************
580 setErrCtxtM, addErrCtxtM :: (TidyEnv -> TcM (TidyEnv, Message)) -> TcM a -> TcM a
581 setErrCtxtM msg = updCtxt (\ msgs -> [msg])
582 addErrCtxtM msg = updCtxt (\ msgs -> msg : msgs)
584 setErrCtxt, addErrCtxt :: Message -> TcM a -> TcM a
585 setErrCtxt msg = setErrCtxtM (\env -> returnM (env, msg))
586 addErrCtxt msg = addErrCtxtM (\env -> returnM (env, msg))
588 popErrCtxt :: TcM a -> TcM a
589 popErrCtxt = updCtxt (\ msgs -> case msgs of { [] -> []; (m:ms) -> ms })
591 getErrCtxt :: TcM ErrCtxt
592 getErrCtxt = do { env <- getLclEnv ; return (tcl_ctxt env) }
594 -- Helper function for the above
595 updCtxt :: (ErrCtxt -> ErrCtxt) -> TcM a -> TcM a
596 updCtxt upd = updLclEnv (\ env@(TcLclEnv { tcl_ctxt = ctxt }) ->
597 env { tcl_ctxt = upd ctxt })
599 getInstLoc :: InstOrigin -> TcM InstLoc
601 = do { loc <- getSrcLocM ; env <- getLclEnv ;
602 return (InstLoc origin loc (tcl_ctxt env)) }
604 addInstCtxt :: InstLoc -> TcM a -> TcM a
605 -- Add the SrcLoc and context from the first Inst in the list
606 -- (they all have similar locations)
607 addInstCtxt (InstLoc _ src_loc ctxt) thing_inside
608 = addSrcLoc src_loc (updCtxt (\ old_ctxt -> ctxt) thing_inside)
611 The addErrTc functions add an error message, but do not cause failure.
612 The 'M' variants pass a TidyEnv that has already been used to
613 tidy up the message; we then use it to tidy the context messages
616 addErrTc :: Message -> TcM ()
617 addErrTc err_msg = addErrTcM (emptyTidyEnv, err_msg)
619 addErrsTc :: [Message] -> TcM ()
620 addErrsTc err_msgs = mappM_ addErrTc err_msgs
622 addErrTcM :: (TidyEnv, Message) -> TcM ()
623 addErrTcM (tidy_env, err_msg)
624 = do { ctxt <- getErrCtxt ;
626 add_err_tcm tidy_env err_msg loc ctxt }
629 The failWith functions add an error message and cause failure
632 failWithTc :: Message -> TcM a -- Add an error message and fail
634 = addErrTc err_msg >> failM
636 failWithTcM :: (TidyEnv, Message) -> TcM a -- Add an error message and fail
637 failWithTcM local_and_msg
638 = addErrTcM local_and_msg >> failM
640 checkTc :: Bool -> Message -> TcM () -- Check that the boolean is true
641 checkTc True err = returnM ()
642 checkTc False err = failWithTc err
645 Warnings have no 'M' variant, nor failure
648 addWarnTc :: Message -> TcM ()
650 = do { ctxt <- getErrCtxt ;
651 ctxt_msgs <- do_ctxt emptyTidyEnv ctxt ;
652 addWarn (vcat (msg : ctxt_to_use ctxt_msgs)) }
654 warnTc :: Bool -> Message -> TcM ()
655 warnTc warn_if_true warn_msg
656 | warn_if_true = addWarnTc warn_msg
657 | otherwise = return ()
663 add_err_tcm tidy_env err_msg loc ctxt
664 = do { ctxt_msgs <- do_ctxt tidy_env ctxt ;
665 addErrAt loc (vcat (err_msg : ctxt_to_use ctxt_msgs)) }
669 do_ctxt tidy_env (c:cs)
670 = do { (tidy_env', m) <- c tidy_env ;
671 ms <- do_ctxt tidy_env' cs ;
674 ctxt_to_use ctxt | opt_PprStyle_Debug = ctxt
675 | otherwise = take 3 ctxt
678 %************************************************************************
680 Other stuff specific to type checker
682 %************************************************************************
685 getLIEVar :: TcM (TcRef LIE)
686 getLIEVar = do { env <- getLclEnv; return (tcl_lie env) }
688 setLIEVar :: TcRef LIE -> TcM a -> TcM a
689 setLIEVar lie_var = updLclEnv (\ env -> env { tcl_lie = lie_var })
691 getLIE :: TcM a -> TcM (a, [Inst])
692 -- (getLIE m) runs m, and returns the type constraints it generates
694 = do { lie_var <- newMutVar emptyLIE ;
695 res <- updLclEnv (\ env -> env { tcl_lie = lie_var })
697 lie <- readMutVar lie_var ;
698 return (res, lieToList lie) }
700 extendLIE :: Inst -> TcM ()
702 = do { lie_var <- getLIEVar ;
703 lie <- readMutVar lie_var ;
704 writeMutVar lie_var (inst `consLIE` lie) }
706 extendLIEs :: [Inst] -> TcM ()
710 = do { lie_var <- getLIEVar ;
711 lie <- readMutVar lie_var ;
712 writeMutVar lie_var (mkLIE insts `plusLIE` lie) }
717 getStage :: TcM Stage
718 getStage = do { env <- getLclEnv; return (tcl_level env) }
720 setStage :: Stage -> TcM a -> TcM a
721 setStage s = updLclEnv (\ env -> env { tcl_level = s })
723 setLclTypeEnv :: TcLclEnv -> TcM a -> TcM a
724 -- Set the local type envt, but do *not* disturb other fields,
725 -- notably the lie_var
726 setLclTypeEnv lcl_env thing_inside
727 = updLclEnv upd thing_inside
729 upd env = env { tcl_env = tcl_env lcl_env,
730 tcl_tyvars = tcl_tyvars lcl_env }
734 %************************************************************************
736 Stuff for the renamer's local env
738 %************************************************************************
741 initRn :: RnMode -> RnM a -> TcRn m a
742 initRn mode thing_inside
743 = do { env <- getGblEnv ;
744 let { lcl_env = RnLclEnv {
746 rn_lenv = emptyRdrEnv }} ;
747 setLclEnv lcl_env thing_inside }
751 getLocalRdrEnv :: RnM LocalRdrEnv
752 getLocalRdrEnv = do { env <- getLclEnv; return (rn_lenv env) }
754 setLocalRdrEnv :: LocalRdrEnv -> RnM a -> RnM a
755 setLocalRdrEnv rdr_env thing_inside
756 = updLclEnv (\env -> env {rn_lenv = rdr_env}) thing_inside
758 getModeRn :: RnM RnMode
759 getModeRn = do { env <- getLclEnv; return (rn_mode env) }