[project @ 2000-10-23 16:39:11 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnMonad.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnMonad]{The monad used by the renamer}
5
6 \begin{code}
7 module RnMonad(
8         module RnMonad,
9
10         module RdrName,         -- Re-exports
11         module Name,            -- from these two
12
13         Module,
14         FiniteMap,
15         Bag,
16         RdrNameHsDecl,
17         RdrNameInstDecl,
18         Version,
19         NameSet,
20         OccName,
21         Fixity
22     ) where
23
24 #include "HsVersions.h"
25
26 #if   defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 405
27 import IOExts           ( fixIO )
28 #elif defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 302
29 import PrelIOBase       ( fixIO )       -- Should be in GlaExts
30 #else
31 import IOBase           ( fixIO )
32 #endif
33 import IOExts           ( IORef, newIORef, readIORef, writeIORef, unsafePerformIO )
34         
35 import HsSyn            
36 import RdrHsSyn
37 import RnHsSyn          ( RenamedFixitySig )
38 import HscTypes         ( Finder,
39                           AvailEnv, lookupTypeEnv,
40                           OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
41                           WhetherHasOrphans, ImportVersion, 
42                           PersistentRenamerState(..), IsBootInterface, Avails,
43                           DeclsMap, IfaceInsts, IfaceRules, 
44                           HomeSymbolTable, PackageSymbolTable,
45                           PersistentCompilerState(..), GlobalRdrEnv,
46                           HomeIfaceTable, PackageIfaceTable,
47                           RdrAvailInfo, ModIface )
48 import BasicTypes       ( Version, defaultFixity )
49 import ErrUtils         ( addShortErrLocLine, addShortWarnLocLine,
50                           pprBagOfErrors, ErrMsg, WarnMsg, Message
51                         )
52 import RdrName          ( RdrName, dummyRdrVarName, rdrNameModule, rdrNameOcc,
53                           RdrNameEnv, emptyRdrEnv, extendRdrEnv, 
54                           lookupRdrEnv, addListToRdrEnv, rdrEnvToList, rdrEnvElts
55                         )
56 import Name             ( Name, OccName, NamedThing(..), getSrcLoc,
57                           isLocallyDefinedName, nameModule, nameOccName,
58                           decode, mkLocalName, mkKnownKeyGlobal,
59                           NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, 
60                           extendNameEnvList
61                         )
62 import Module           ( Module, ModuleName, lookupModuleEnvByName )
63 import NameSet          
64 import CmdLineOpts      ( DynFlags, DynFlag(..), dopt )
65 import SrcLoc           ( SrcLoc, generatedSrcLoc )
66 import Unique           ( Unique )
67 import FiniteMap        ( FiniteMap, emptyFM )
68 import Bag              ( Bag, emptyBag, isEmptyBag, snocBag )
69 import UniqSupply
70 import Outputable
71 import PrelNames        ( mkUnboundName )
72 import Maybes           ( maybeToBool, seqMaybe, orElse )
73
74 infixr 9 `thenRn`, `thenRn_`
75 \end{code}
76
77
78 %************************************************************************
79 %*                                                                      *
80 \subsection{Somewhat magical interface to other monads}
81 %*                                                                      *
82 %************************************************************************
83
84 \begin{code}
85 ioToRnM :: IO r -> RnM d (Either IOError r)
86 ioToRnM io rn_down g_down = (io >>= \ ok -> return (Right ok)) 
87                             `catch` 
88                             (\ err -> return (Left err))
89             
90 traceRn :: SDoc -> RnM d ()
91 traceRn msg
92    = doptRn Opt_D_dump_rn_trace `thenRn` \b ->
93      if b then putDocRn msg else returnRn ()
94
95 putDocRn :: SDoc -> RnM d ()
96 putDocRn msg = ioToRnM (printErrs msg)  `thenRn_`
97                returnRn ()
98 \end{code}
99
100
101 %************************************************************************
102 %*                                                                      *
103 \subsection{Data types}
104 %*                                                                      *
105 %************************************************************************
106
107 %===================================================
108 \subsubsection{         MONAD TYPES}
109 %===================================================
110
111 \begin{code}
112 type RnM d r = RnDown -> d -> IO r
113 type RnMS r  = RnM SDown r              -- Renaming source
114 type RnMG r  = RnM ()    r              -- Getting global names etc
115
116         -- Common part
117 data RnDown
118   = RnDown {
119         rn_mod     :: Module,           -- This module
120         rn_loc     :: SrcLoc,           -- Current locn
121
122         rn_finder  :: Finder,
123         rn_dflags  :: DynFlags,
124
125         rn_hit     :: HomeIfaceTable,
126         rn_done    :: Name -> Bool,     -- Tells what things (both in the
127                                         -- home package and other packages)
128                                         -- were already available (i.e. in
129                                         -- the relevant SymbolTable) before 
130                                         -- compiling this module
131
132         rn_errs    :: IORef (Bag WarnMsg, Bag ErrMsg),
133
134         -- The second and third components are a flattened-out OrigNameEnv
135         rn_ns      :: IORef (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv),
136         rn_ifaces  :: IORef Ifaces
137     }
138
139         -- For renaming source code
140 data SDown = SDown {
141                   rn_mode :: RnMode,
142
143                   rn_genv :: GlobalRdrEnv,      -- Global envt
144
145                   rn_lenv :: LocalRdrEnv,       -- Local name envt
146                         --   Does *not* include global name envt; may shadow it
147                         --   Includes both ordinary variables and type variables;
148                         --   they are kept distinct because tyvar have a different
149                         --   occurrence contructor (Name.TvOcc)
150                         -- We still need the unsullied global name env so that
151                         --   we can look up record field names
152
153                   rn_fixenv :: LocalFixityEnv   -- Local fixities
154                         -- The global fixities are held in the
155                         -- rn_ifaces field.  Why?  See the comments
156                         -- with RnIfaces.lookupLocalFixity
157                 }
158
159 data RnMode     = SourceMode                    -- Renaming source code
160                 | InterfaceMode                 -- Renaming interface declarations.  
161 \end{code}
162
163 %===================================================
164 \subsubsection{         ENVIRONMENTS}
165 %===================================================
166
167 \begin{code}
168 --------------------------------
169 type LocalRdrEnv    = RdrNameEnv Name
170 type LocalFixityEnv = NameEnv RenamedFixitySig
171         -- We keep the whole fixity sig so that we
172         -- can report line-number info when there is a duplicate
173         -- fixity declaration
174
175 lookupLocalFixity :: LocalFixityEnv -> Name -> Fixity
176 lookupLocalFixity env name
177   = case lookupNameEnv env name of 
178         Just (FixitySig _ fix _) -> fix
179         Nothing                  -> defaultFixity
180 \end{code}
181
182 \begin{code}
183 type ExportAvails = (FiniteMap ModuleName Avails,
184         -- Used to figure out "module M" export specifiers
185         -- Includes avails only from *unqualified* imports
186         -- (see 1.4 Report Section 5.1.1)
187
188                      AvailEnv)  -- Used to figure out all other export specifiers.
189 \end{code}
190
191 %===================================================
192 \subsubsection{         INTERFACE FILE STUFF}
193 %===================================================
194
195 \begin{code}
196 type ExportItem = (ModuleName, [RdrAvailInfo])
197
198 data ParsedIface
199   = ParsedIface {
200       pi_mod       :: Module,                           -- Complete with package info
201       pi_vers      :: Version,                          -- Module version number
202       pi_orphan    :: WhetherHasOrphans,                -- Whether this module has orphans
203       pi_usages    :: [ImportVersion OccName],          -- Usages
204       pi_exports   :: (Version, [ExportItem]),          -- Exports
205       pi_insts     :: [RdrNameInstDecl],                -- Local instance declarations
206       pi_decls     :: [(Version, RdrNameHsDecl)],       -- Local definitions
207       pi_fixity    :: [RdrNameFixitySig],               -- Local fixity declarations,
208       pi_rules     :: (Version, [RdrNameRuleDecl]),     -- Rules, with their version
209       pi_deprecs   :: [RdrNameDeprecation]              -- Deprecations
210     }
211 \end{code}
212
213 %************************************************************************
214 %*                                                                      *
215 \subsection{The renamer state}
216 %*                                                                      *
217 %************************************************************************
218
219 \begin{code}
220 data Ifaces = Ifaces {
221     -- PERSISTENT FIELDS
222         iPIT :: PackageIfaceTable,
223                 -- The ModuleIFaces for modules in other packages
224                 -- whose interfaces we have opened
225                 -- The declarations in these interface files are held in
226                 -- iDecls, iInsts, iRules (below), not in the mi_decls fields
227                 -- of the iPIT.  What _is_ in the iPIT is:
228                 --      * The Module 
229                 --      * Version info
230                 --      * Its exports
231                 --      * Fixities
232                 --      * Deprecations
233                 -- The iPIT field is initialised from the compiler's persistent
234                 -- package symbol table, and the renamer incrementally adds
235                 -- to it.
236
237         iDecls :: DeclsMap,     
238                 -- A single, global map of Names to unslurped decls
239
240         iInsts :: IfaceInsts,
241                 -- The as-yet un-slurped instance decls; this bag is depleted when we
242                 -- slurp an instance decl so that we don't slurp the same one twice.
243                 -- Each is 'gated' by the names that must be available before
244                 -- this instance decl is needed.
245
246         iRules :: IfaceRules,
247                 -- Similar to instance decls, only for rules
248
249     -- EPHEMERAL FIELDS
250     -- These fields persist during the compilation of a single module only
251         iImpModInfo :: ImportedModuleInfo,
252                         -- Modules this one depends on: that is, the union 
253                         -- of the modules its *direct* imports depend on.
254                         -- NB: The direct imports have .hi files that enumerate *all* the
255                         -- dependencies (direct or not) of the imported module.
256
257         iSlurp :: NameSet,
258                 -- All the names (whether "big" or "small", whether wired-in or not,
259                 -- whether locally defined or not) that have been slurped in so far.
260
261         iVSlurp :: [Name]
262                 -- All the (a) non-wired-in (b) "big" (c) non-locally-defined 
263                 -- names that have been slurped in so far, with their versions.
264                 -- This is used to generate the "usage" information for this module.
265                 -- Subset of the previous field.
266                 -- It's worth keeping separately, because there's no very easy 
267                 -- way to distinguish the "big" names from the "non-big" ones.
268                 -- But this is a decision we might want to revisit.
269     }
270
271 type ImportedModuleInfo = FiniteMap ModuleName 
272                                     (WhetherHasOrphans, IsBootInterface, IsLoaded)
273 type IsLoaded = Bool
274 \end{code}
275
276
277 %************************************************************************
278 %*                                                                      *
279 \subsection{Main monad code}
280 %*                                                                      *
281 %************************************************************************
282
283 \begin{code}
284 initRn :: DynFlags 
285        -> Finder 
286        -> HomeIfaceTable
287        -> HomeSymbolTable
288        -> PersistentCompilerState
289        -> Module 
290        -> SrcLoc
291        -> RnMG t
292        -> IO (t, (Bag WarnMsg, Bag ErrMsg), PersistentCompilerState)
293
294 initRn dflags finder hit hst pcs mod loc do_rn
295   = do 
296         let prs = pcs_PRS pcs
297         let pst = pcs_PST pcs
298
299         uniqs     <- mkSplitUniqSupply 'r'
300         names_var <- newIORef (uniqs, origNames (prsOrig prs), 
301                                       origIParam (prsOrig prs))
302         errs_var  <- newIORef (emptyBag,emptyBag)
303         iface_var <- newIORef (initIfaces pcs)
304         let rn_down = RnDown { rn_mod = mod,
305                                rn_loc = loc, 
306         
307                                rn_finder = finder,
308                                rn_dflags = dflags,
309                                rn_hit    = hit,
310                                rn_done   = is_done hst pst,
311                                              
312                                rn_ns     = names_var, 
313                                rn_errs   = errs_var, 
314                                rn_ifaces = iface_var,
315                              }
316         
317         -- do the business
318         res <- do_rn rn_down ()
319         
320         -- Grab state and record it
321         (warns, errs)              <- readIORef errs_var
322         new_ifaces                 <- readIORef iface_var
323         (_, new_origN, new_origIP) <- readIORef names_var
324         let new_orig = Orig { origNames = new_origN, origIParam = new_origIP }
325         let new_prs = prs { prsOrig = new_orig,
326                             prsDecls = iDecls new_ifaces,
327                             prsInsts = iInsts new_ifaces,
328                             prsRules = iRules new_ifaces }
329         let new_pcs = pcs { pcs_PIT = iPIT new_ifaces, 
330                             pcs_PRS = new_prs }
331         
332         return (res, (warns, errs), new_pcs)
333
334 is_done :: HomeSymbolTable -> PackageSymbolTable -> Name -> Bool
335 -- Returns True iff the name is in either symbol table
336 is_done hst pst n = maybeToBool (lookupTypeEnv pst n `seqMaybe` lookupTypeEnv hst n)
337
338 lookupIface :: HomeIfaceTable -> PackageIfaceTable -> ModuleName -> ModIface
339 lookupIface hit pit mod = lookupModuleEnvByName hit mod `orElse` 
340                           lookupModuleEnvByName pit mod `orElse`
341                           pprPanic "lookupIface" (ppr mod)
342
343 initIfaces :: PersistentCompilerState -> Ifaces
344 initIfaces (PCS { pcs_PIT = pit, pcs_PRS = prs })
345   = Ifaces { iPIT   = pit,
346              iDecls = prsDecls prs,
347              iInsts = prsInsts prs,
348              iRules = prsRules prs,
349
350              iImpModInfo = emptyFM,
351              iSlurp      = unitNameSet (mkUnboundName dummyRdrVarName),
352                         -- Pretend that the dummy unbound name has already been
353                         -- slurped.  This is what's returned for an out-of-scope name,
354                         -- and we don't want thereby to try to suck it in!
355              iVSlurp = []
356       }
357
358
359 initRnMS :: GlobalRdrEnv -> LocalFixityEnv -> RnMode -> RnMS r -> RnM d r
360 initRnMS rn_env fixity_env mode thing_inside rn_down g_down
361   = let
362         s_down = SDown { rn_genv = rn_env, rn_lenv = emptyRdrEnv, 
363                          rn_fixenv = fixity_env, rn_mode = mode }
364     in
365     thing_inside rn_down s_down
366
367 initIfaceRnMS :: Module -> RnMS r -> RnM d r
368 initIfaceRnMS mod thing_inside 
369   = initRnMS emptyRdrEnv emptyNameEnv InterfaceMode $
370     setModuleRn mod thing_inside
371
372 \end{code}
373
374 @renameSourceCode@ is used to rename stuff ``out-of-line'';
375 that is, not as part of the main renamer.
376 Sole examples: derived definitions,
377 which are only generated in the type checker.
378
379 The @NameSupply@ includes a @UniqueSupply@, so if you call it more than
380 once you must either split it, or install a fresh unique supply.
381
382 \begin{code}
383 renameSourceCode :: DynFlags 
384                  -> Module
385                  -> PersistentRenamerState
386                  -> RnMS r
387                  -> r
388
389 renameSourceCode dflags mod prs m
390   = unsafePerformIO (
391         -- It's not really unsafe!  When renaming source code we
392         -- only do any I/O if we need to read in a fixity declaration;
393         -- and that doesn't happen in pragmas etc
394
395         mkSplitUniqSupply 'r'                           >>= \ new_us ->
396         newIORef (new_us, origNames (prsOrig prs), 
397                           origIParam (prsOrig prs))     >>= \ names_var ->
398         newIORef (emptyBag,emptyBag)                    >>= \ errs_var ->
399         let
400             rn_down = RnDown { rn_dflags = dflags,
401                                rn_loc = generatedSrcLoc, rn_ns = names_var,
402                                rn_errs = errs_var, 
403                                rn_mod = mod, 
404                                rn_done   = bogus "rn_done",     rn_hit    = bogus "rn_hit",
405                                rn_ifaces = bogus "rn_ifaces",   rn_finder = bogus "rn_finder"
406                              }
407             s_down = SDown { rn_mode = InterfaceMode,
408                                -- So that we can refer to PrelBase.True etc
409                              rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
410                              rn_fixenv = emptyNameEnv }
411         in
412         m rn_down s_down                        >>= \ result ->
413         
414         readIORef errs_var                      >>= \ (warns,errs) ->
415
416         (if not (isEmptyBag errs) then
417                 pprTrace "Urk! renameSourceCode found errors" (display errs) 
418 #ifdef DEBUG
419          else if not (isEmptyBag warns) then
420                 pprTrace "Note: renameSourceCode found warnings" (display warns)
421 #endif
422          else
423                 id) $
424
425         return result
426     )
427   where
428     display errs = pprBagOfErrors errs
429
430 bogus s = panic ("rnameSourceCode: " ++ s)  -- Used for unused record fields
431
432 {-# INLINE thenRn #-}
433 {-# INLINE thenRn_ #-}
434 {-# INLINE returnRn #-}
435 {-# INLINE andRn #-}
436
437 returnRn :: a -> RnM d a
438 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
439 thenRn_  :: RnM d a -> RnM d b -> RnM d b
440 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
441 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
442 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
443 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
444 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
445 sequenceRn :: [RnM d a] -> RnM d [a]
446 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
447 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
448 fixRn    :: (a -> RnM d a) -> RnM d a
449
450 returnRn v gdown ldown  = return v
451 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
452 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
453 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
454 andRn combiner m1 m2 gdown ldown
455   = m1 gdown ldown >>= \ res1 ->
456     m2 gdown ldown >>= \ res2 ->
457     return (combiner res1 res2)
458
459 sequenceRn []     = returnRn []
460 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
461                      sequenceRn ms      `thenRn` \ rs ->
462                      returnRn (r:rs)
463
464 mapRn f []     = returnRn []
465 mapRn f (x:xs)
466   = f x         `thenRn` \ r ->
467     mapRn f xs  `thenRn` \ rs ->
468     returnRn (r:rs)
469
470 mapRn_ f []     = returnRn ()
471 mapRn_ f (x:xs) = 
472     f x         `thenRn_`
473     mapRn_ f xs
474
475 foldlRn k z [] = returnRn z
476 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
477                      foldlRn k z' xs
478
479 mapAndUnzipRn f [] = returnRn ([],[])
480 mapAndUnzipRn f (x:xs)
481   = f x                 `thenRn` \ (r1,  r2)  ->
482     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
483     returnRn (r1:rs1, r2:rs2)
484
485 mapAndUnzip3Rn f [] = returnRn ([],[],[])
486 mapAndUnzip3Rn f (x:xs)
487   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
488     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
489     returnRn (r1:rs1, r2:rs2, r3:rs3)
490
491 mapMaybeRn f []     = returnRn []
492 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
493                       mapMaybeRn f xs   `thenRn` \ rs ->
494                       case maybe_r of
495                         Nothing -> returnRn rs
496                         Just r  -> returnRn (r:rs)
497
498 flatMapRn f []     = returnRn []
499 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
500                      flatMapRn f xs     `thenRn` \ rs ->
501                      returnRn (r ++ rs)
502 \end{code}
503
504
505
506 %************************************************************************
507 %*                                                                      *
508 \subsection{Boring plumbing for common part}
509 %*                                                                      *
510 %************************************************************************
511
512
513 %================
514 \subsubsection{  Errors and warnings}
515 %=====================
516
517 \begin{code}
518 failWithRn :: a -> Message -> RnM d a
519 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
520   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
521     writeIORef errs_var (warns, errs `snocBag` err)             >> 
522     return res
523   where
524     err = addShortErrLocLine loc msg
525
526 warnWithRn :: a -> Message -> RnM d a
527 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
528   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
529     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
530     return res
531   where
532     warn = addShortWarnLocLine loc msg
533
534 addErrRn :: Message -> RnM d ()
535 addErrRn err = failWithRn () err
536
537 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
538 checkRn False err = addErrRn err
539 checkRn True  err = returnRn ()
540
541 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
542 warnCheckRn False err = addWarnRn err
543 warnCheckRn True  err = returnRn ()
544
545 addWarnRn :: Message -> RnM d ()
546 addWarnRn warn = warnWithRn () warn
547
548 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
549 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
550   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
551     return (isEmptyBag errs)
552
553 doptRn :: DynFlag -> RnM d Bool
554 doptRn dflag (RnDown { rn_dflags = dflags}) l_down
555    = return (dopt dflag dflags)
556
557 getDOptsRn :: RnM d DynFlags
558 getDOptsRn (RnDown { rn_dflags = dflags}) l_down
559    = return dflags
560 \end{code}
561
562
563 %================
564 \subsubsection{Source location}
565 %=====================
566
567 \begin{code}
568 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
569 pushSrcLocRn loc' m down l_down
570   = m (down {rn_loc = loc'}) l_down
571
572 getSrcLocRn :: RnM d SrcLoc
573 getSrcLocRn down l_down
574   = return (rn_loc down)
575 \end{code}
576
577 %================
578 \subsubsection{The finder and home symbol table}
579 %=====================
580
581 \begin{code}
582 getFinderRn :: RnM d Finder
583 getFinderRn down l_down = return (rn_finder down)
584
585 getHomeIfaceTableRn :: RnM d HomeIfaceTable
586 getHomeIfaceTableRn down l_down = return (rn_hit down)
587
588 checkAlreadyAvailable :: Name -> RnM d Bool
589 checkAlreadyAvailable name down l_down = return (rn_done down name)
590 \end{code}
591
592 %================
593 \subsubsection{Name supply}
594 %=====================
595
596 \begin{code}
597 getNameSupplyRn :: RnM d (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv)
598 getNameSupplyRn rn_down l_down
599   = readIORef (rn_ns rn_down)
600
601 setNameSupplyRn :: (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv) -> RnM d ()
602 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
603   = writeIORef names_var names'
604
605 getUniqRn :: RnM d Unique
606 getUniqRn (RnDown {rn_ns = names_var}) l_down
607  = readIORef names_var >>= \ (us, cache, ipcache) ->
608    let
609      (us1,us') = splitUniqSupply us
610    in
611    writeIORef names_var (us', cache, ipcache)  >>
612    return (uniqFromSupply us1)
613 \end{code}
614
615 %================
616 \subsubsection{  Module}
617 %=====================
618
619 \begin{code}
620 getModuleRn :: RnM d Module
621 getModuleRn (RnDown {rn_mod = mod}) l_down
622   = return mod
623
624 setModuleRn :: Module -> RnM d a -> RnM d a
625 setModuleRn new_mod enclosed_thing rn_down l_down
626   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
627 \end{code}
628
629
630 %************************************************************************
631 %*                                                                      *
632 \subsection{Plumbing for rename-source part}
633 %*                                                                      *
634 %************************************************************************
635
636 %================
637 \subsubsection{  RnEnv}
638 %=====================
639
640 \begin{code}
641 getLocalNameEnv :: RnMS LocalRdrEnv
642 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
643   = return local_env
644
645 getGlobalNameEnv :: RnMS GlobalRdrEnv
646 getGlobalNameEnv rn_down (SDown {rn_genv = global_env})
647   = return global_env
648
649 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
650 setLocalNameEnv local_env' m rn_down l_down
651   = m rn_down (l_down {rn_lenv = local_env'})
652
653 getFixityEnv :: RnMS LocalFixityEnv
654 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
655   = return fixity_env
656
657 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
658 extendFixityEnv fixes enclosed_scope
659                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
660   = let
661         new_fixity_env = extendNameEnvList fixity_env fixes
662     in
663     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
664 \end{code}
665
666 %================
667 \subsubsection{  Mode}
668 %=====================
669
670 \begin{code}
671 getModeRn :: RnMS RnMode
672 getModeRn rn_down (SDown {rn_mode = mode})
673   = return mode
674
675 setModeRn :: RnMode -> RnMS a -> RnMS a
676 setModeRn new_mode thing_inside rn_down l_down
677   = thing_inside rn_down (l_down {rn_mode = new_mode})
678 \end{code}
679
680
681 %************************************************************************
682 %*                                                                      *
683 \subsection{Plumbing for rename-globals part}
684 %*                                                                      *
685 %************************************************************************
686
687 \begin{code}
688 getIfacesRn :: RnM d Ifaces
689 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
690   = readIORef iface_var
691
692 setIfacesRn :: Ifaces -> RnM d ()
693 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
694   = writeIORef iface_var ifaces
695 \end{code}