[project @ 2000-10-24 07:35:00 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                           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 type IfaceDeprecs = Maybe (Either DeprecTxt [(RdrName,DeprecTxt)])
198         -- Nothing        => NoDeprecs
199         -- Just (Left t)  => DeprecAll
200         -- Just (Right p) => DeprecSome
201
202 data ParsedIface
203   = ParsedIface {
204       pi_mod       :: Module,                           -- Complete with package info
205       pi_vers      :: Version,                          -- Module version number
206       pi_orphan    :: WhetherHasOrphans,                -- Whether this module has orphans
207       pi_usages    :: [ImportVersion OccName],          -- Usages
208       pi_exports   :: (Version, [ExportItem]),          -- Exports
209       pi_decls     :: [(Version, RdrNameHsDecl)],       -- Local definitions
210       pi_fixity    :: [RdrNameFixitySig],               -- Local fixity declarations,
211       pi_insts     :: [RdrNameInstDecl],                -- Local instance declarations
212       pi_rules     :: (Version, [RdrNameRuleDecl]),     -- Rules, with their version
213       pi_deprecs   :: IfaceDeprecs                      -- Deprecations
214     }
215 \end{code}
216
217 %************************************************************************
218 %*                                                                      *
219 \subsection{The renamer state}
220 %*                                                                      *
221 %************************************************************************
222
223 \begin{code}
224 data Ifaces = Ifaces {
225     -- PERSISTENT FIELDS
226         iPIT :: PackageIfaceTable,
227                 -- The ModuleIFaces for modules in other packages
228                 -- whose interfaces we have opened
229                 -- The declarations in these interface files are held in
230                 -- iDecls, iInsts, iRules (below), not in the mi_decls fields
231                 -- of the iPIT.  What _is_ in the iPIT is:
232                 --      * The Module 
233                 --      * Version info
234                 --      * Its exports
235                 --      * Fixities
236                 --      * Deprecations
237                 -- The iPIT field is initialised from the compiler's persistent
238                 -- package symbol table, and the renamer incrementally adds
239                 -- to it.
240
241         iDecls :: DeclsMap,     
242                 -- A single, global map of Names to unslurped decls
243
244         iInsts :: IfaceInsts,
245                 -- The as-yet un-slurped instance decls; this bag is depleted when we
246                 -- slurp an instance decl so that we don't slurp the same one twice.
247                 -- Each is 'gated' by the names that must be available before
248                 -- this instance decl is needed.
249
250         iRules :: IfaceRules,
251                 -- Similar to instance decls, only for rules
252
253     -- EPHEMERAL FIELDS
254     -- These fields persist during the compilation of a single module only
255         iImpModInfo :: ImportedModuleInfo,
256                         -- Modules this one depends on: that is, the union 
257                         -- of the modules its *direct* imports depend on.
258                         -- NB: The direct imports have .hi files that enumerate *all* the
259                         -- dependencies (direct or not) of the imported module.
260
261         iSlurp :: NameSet,
262                 -- All the names (whether "big" or "small", whether wired-in or not,
263                 -- whether locally defined or not) that have been slurped in so far.
264
265         iVSlurp :: [Name]
266                 -- All the (a) non-wired-in (b) "big" (c) non-locally-defined 
267                 -- names that have been slurped in so far, with their versions.
268                 -- This is used to generate the "usage" information for this module.
269                 -- Subset of the previous field.
270                 -- It's worth keeping separately, because there's no very easy 
271                 -- way to distinguish the "big" names from the "non-big" ones.
272                 -- But this is a decision we might want to revisit.
273     }
274
275 type ImportedModuleInfo = FiniteMap ModuleName 
276                                     (WhetherHasOrphans, IsBootInterface, IsLoaded)
277 type IsLoaded = Bool
278 \end{code}
279
280
281 %************************************************************************
282 %*                                                                      *
283 \subsection{Main monad code}
284 %*                                                                      *
285 %************************************************************************
286
287 \begin{code}
288 initRn :: DynFlags 
289        -> Finder 
290        -> HomeIfaceTable
291        -> HomeSymbolTable
292        -> PersistentCompilerState
293        -> Module 
294        -> SrcLoc
295        -> RnMG t
296        -> IO (t, (Bag WarnMsg, Bag ErrMsg), PersistentCompilerState)
297
298 initRn dflags finder hit hst pcs mod loc do_rn
299   = do 
300         let prs = pcs_PRS pcs
301         let pst = pcs_PST pcs
302
303         uniqs     <- mkSplitUniqSupply 'r'
304         names_var <- newIORef (uniqs, origNames (prsOrig prs), 
305                                       origIParam (prsOrig prs))
306         errs_var  <- newIORef (emptyBag,emptyBag)
307         iface_var <- newIORef (initIfaces pcs)
308         let rn_down = RnDown { rn_mod = mod,
309                                rn_loc = loc, 
310         
311                                rn_finder = finder,
312                                rn_dflags = dflags,
313                                rn_hit    = hit,
314                                rn_done   = is_done hst pst,
315                                              
316                                rn_ns     = names_var, 
317                                rn_errs   = errs_var, 
318                                rn_ifaces = iface_var,
319                              }
320         
321         -- do the business
322         res <- do_rn rn_down ()
323         
324         -- Grab state and record it
325         (warns, errs)              <- readIORef errs_var
326         new_ifaces                 <- readIORef iface_var
327         (_, new_origN, new_origIP) <- readIORef names_var
328         let new_orig = Orig { origNames = new_origN, origIParam = new_origIP }
329         let new_prs = prs { prsOrig = new_orig,
330                             prsDecls = iDecls new_ifaces,
331                             prsInsts = iInsts new_ifaces,
332                             prsRules = iRules new_ifaces }
333         let new_pcs = pcs { pcs_PIT = iPIT new_ifaces, 
334                             pcs_PRS = new_prs }
335         
336         return (res, (warns, errs), new_pcs)
337
338 is_done :: HomeSymbolTable -> PackageSymbolTable -> Name -> Bool
339 -- Returns True iff the name is in either symbol table
340 is_done hst pst n = maybeToBool (lookupTypeEnv pst n `seqMaybe` lookupTypeEnv hst n)
341
342 lookupIface :: HomeIfaceTable -> PackageIfaceTable -> ModuleName -> ModIface
343 lookupIface hit pit mod = lookupModuleEnvByName hit mod `orElse` 
344                           lookupModuleEnvByName pit mod `orElse`
345                           pprPanic "lookupIface" (ppr mod)
346
347 initIfaces :: PersistentCompilerState -> Ifaces
348 initIfaces (PCS { pcs_PIT = pit, pcs_PRS = prs })
349   = Ifaces { iPIT   = pit,
350              iDecls = prsDecls prs,
351              iInsts = prsInsts prs,
352              iRules = prsRules prs,
353
354              iImpModInfo = emptyFM,
355              iSlurp      = unitNameSet (mkUnboundName dummyRdrVarName),
356                         -- Pretend that the dummy unbound name has already been
357                         -- slurped.  This is what's returned for an out-of-scope name,
358                         -- and we don't want thereby to try to suck it in!
359              iVSlurp = []
360       }
361
362
363 initRnMS :: GlobalRdrEnv -> LocalFixityEnv -> RnMode -> RnMS r -> RnM d r
364 initRnMS rn_env fixity_env mode thing_inside rn_down g_down
365   = let
366         s_down = SDown { rn_genv = rn_env, rn_lenv = emptyRdrEnv, 
367                          rn_fixenv = fixity_env, rn_mode = mode }
368     in
369     thing_inside rn_down s_down
370
371 initIfaceRnMS :: Module -> RnMS r -> RnM d r
372 initIfaceRnMS mod thing_inside 
373   = initRnMS emptyRdrEnv emptyNameEnv InterfaceMode $
374     setModuleRn mod thing_inside
375
376 \end{code}
377
378 @renameSourceCode@ is used to rename stuff ``out-of-line'';
379 that is, not as part of the main renamer.
380 Sole examples: derived definitions,
381 which are only generated in the type checker.
382
383 The @NameSupply@ includes a @UniqueSupply@, so if you call it more than
384 once you must either split it, or install a fresh unique supply.
385
386 \begin{code}
387 renameSourceCode :: DynFlags 
388                  -> Module
389                  -> PersistentRenamerState
390                  -> RnMS r
391                  -> r
392
393 renameSourceCode dflags mod prs m
394   = unsafePerformIO (
395         -- It's not really unsafe!  When renaming source code we
396         -- only do any I/O if we need to read in a fixity declaration;
397         -- and that doesn't happen in pragmas etc
398
399         mkSplitUniqSupply 'r'                           >>= \ new_us ->
400         newIORef (new_us, origNames (prsOrig prs), 
401                           origIParam (prsOrig prs))     >>= \ names_var ->
402         newIORef (emptyBag,emptyBag)                    >>= \ errs_var ->
403         let
404             rn_down = RnDown { rn_dflags = dflags,
405                                rn_loc = generatedSrcLoc, rn_ns = names_var,
406                                rn_errs = errs_var, 
407                                rn_mod = mod, 
408                                rn_done   = bogus "rn_done",     rn_hit    = bogus "rn_hit",
409                                rn_ifaces = bogus "rn_ifaces",   rn_finder = bogus "rn_finder"
410                              }
411             s_down = SDown { rn_mode = InterfaceMode,
412                                -- So that we can refer to PrelBase.True etc
413                              rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
414                              rn_fixenv = emptyNameEnv }
415         in
416         m rn_down s_down                        >>= \ result ->
417         
418         readIORef errs_var                      >>= \ (warns,errs) ->
419
420         (if not (isEmptyBag errs) then
421                 pprTrace "Urk! renameSourceCode found errors" (display errs) 
422 #ifdef DEBUG
423          else if not (isEmptyBag warns) then
424                 pprTrace "Note: renameSourceCode found warnings" (display warns)
425 #endif
426          else
427                 id) $
428
429         return result
430     )
431   where
432     display errs = pprBagOfErrors errs
433
434 bogus s = panic ("rnameSourceCode: " ++ s)  -- Used for unused record fields
435
436 {-# INLINE thenRn #-}
437 {-# INLINE thenRn_ #-}
438 {-# INLINE returnRn #-}
439 {-# INLINE andRn #-}
440
441 returnRn :: a -> RnM d a
442 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
443 thenRn_  :: RnM d a -> RnM d b -> RnM d b
444 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
445 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
446 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
447 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
448 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
449 sequenceRn :: [RnM d a] -> RnM d [a]
450 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
451 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
452 fixRn    :: (a -> RnM d a) -> RnM d a
453
454 returnRn v gdown ldown  = return v
455 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
456 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
457 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
458 andRn combiner m1 m2 gdown ldown
459   = m1 gdown ldown >>= \ res1 ->
460     m2 gdown ldown >>= \ res2 ->
461     return (combiner res1 res2)
462
463 sequenceRn []     = returnRn []
464 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
465                      sequenceRn ms      `thenRn` \ rs ->
466                      returnRn (r:rs)
467
468 mapRn f []     = returnRn []
469 mapRn f (x:xs)
470   = f x         `thenRn` \ r ->
471     mapRn f xs  `thenRn` \ rs ->
472     returnRn (r:rs)
473
474 mapRn_ f []     = returnRn ()
475 mapRn_ f (x:xs) = 
476     f x         `thenRn_`
477     mapRn_ f xs
478
479 foldlRn k z [] = returnRn z
480 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
481                      foldlRn k z' xs
482
483 mapAndUnzipRn f [] = returnRn ([],[])
484 mapAndUnzipRn f (x:xs)
485   = f x                 `thenRn` \ (r1,  r2)  ->
486     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
487     returnRn (r1:rs1, r2:rs2)
488
489 mapAndUnzip3Rn f [] = returnRn ([],[],[])
490 mapAndUnzip3Rn f (x:xs)
491   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
492     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
493     returnRn (r1:rs1, r2:rs2, r3:rs3)
494
495 mapMaybeRn f []     = returnRn []
496 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
497                       mapMaybeRn f xs   `thenRn` \ rs ->
498                       case maybe_r of
499                         Nothing -> returnRn rs
500                         Just r  -> returnRn (r:rs)
501
502 flatMapRn f []     = returnRn []
503 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
504                      flatMapRn f xs     `thenRn` \ rs ->
505                      returnRn (r ++ rs)
506 \end{code}
507
508
509
510 %************************************************************************
511 %*                                                                      *
512 \subsection{Boring plumbing for common part}
513 %*                                                                      *
514 %************************************************************************
515
516
517 %================
518 \subsubsection{  Errors and warnings}
519 %=====================
520
521 \begin{code}
522 failWithRn :: a -> Message -> RnM d a
523 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
524   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
525     writeIORef errs_var (warns, errs `snocBag` err)             >> 
526     return res
527   where
528     err = addShortErrLocLine loc msg
529
530 warnWithRn :: a -> Message -> RnM d a
531 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
532   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
533     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
534     return res
535   where
536     warn = addShortWarnLocLine loc msg
537
538 addErrRn :: Message -> RnM d ()
539 addErrRn err = failWithRn () err
540
541 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
542 checkRn False err = addErrRn err
543 checkRn True  err = returnRn ()
544
545 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
546 warnCheckRn False err = addWarnRn err
547 warnCheckRn True  err = returnRn ()
548
549 addWarnRn :: Message -> RnM d ()
550 addWarnRn warn = warnWithRn () warn
551
552 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
553 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
554   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
555     return (isEmptyBag errs)
556
557 doptRn :: DynFlag -> RnM d Bool
558 doptRn dflag (RnDown { rn_dflags = dflags}) l_down
559    = return (dopt dflag dflags)
560
561 getDOptsRn :: RnM d DynFlags
562 getDOptsRn (RnDown { rn_dflags = dflags}) l_down
563    = return dflags
564 \end{code}
565
566
567 %================
568 \subsubsection{Source location}
569 %=====================
570
571 \begin{code}
572 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
573 pushSrcLocRn loc' m down l_down
574   = m (down {rn_loc = loc'}) l_down
575
576 getSrcLocRn :: RnM d SrcLoc
577 getSrcLocRn down l_down
578   = return (rn_loc down)
579 \end{code}
580
581 %================
582 \subsubsection{The finder and home symbol table}
583 %=====================
584
585 \begin{code}
586 getFinderRn :: RnM d Finder
587 getFinderRn down l_down = return (rn_finder down)
588
589 getHomeIfaceTableRn :: RnM d HomeIfaceTable
590 getHomeIfaceTableRn down l_down = return (rn_hit down)
591
592 checkAlreadyAvailable :: Name -> RnM d Bool
593 checkAlreadyAvailable name down l_down = return (rn_done down name)
594 \end{code}
595
596 %================
597 \subsubsection{Name supply}
598 %=====================
599
600 \begin{code}
601 getNameSupplyRn :: RnM d (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv)
602 getNameSupplyRn rn_down l_down
603   = readIORef (rn_ns rn_down)
604
605 setNameSupplyRn :: (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv) -> RnM d ()
606 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
607   = writeIORef names_var names'
608
609 getUniqRn :: RnM d Unique
610 getUniqRn (RnDown {rn_ns = names_var}) l_down
611  = readIORef names_var >>= \ (us, cache, ipcache) ->
612    let
613      (us1,us') = splitUniqSupply us
614    in
615    writeIORef names_var (us', cache, ipcache)  >>
616    return (uniqFromSupply us1)
617 \end{code}
618
619 %================
620 \subsubsection{  Module}
621 %=====================
622
623 \begin{code}
624 getModuleRn :: RnM d Module
625 getModuleRn (RnDown {rn_mod = mod}) l_down
626   = return mod
627
628 setModuleRn :: Module -> RnM d a -> RnM d a
629 setModuleRn new_mod enclosed_thing rn_down l_down
630   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
631 \end{code}
632
633
634 %************************************************************************
635 %*                                                                      *
636 \subsection{Plumbing for rename-source part}
637 %*                                                                      *
638 %************************************************************************
639
640 %================
641 \subsubsection{  RnEnv}
642 %=====================
643
644 \begin{code}
645 getLocalNameEnv :: RnMS LocalRdrEnv
646 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
647   = return local_env
648
649 getGlobalNameEnv :: RnMS GlobalRdrEnv
650 getGlobalNameEnv rn_down (SDown {rn_genv = global_env})
651   = return global_env
652
653 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
654 setLocalNameEnv local_env' m rn_down l_down
655   = m rn_down (l_down {rn_lenv = local_env'})
656
657 getFixityEnv :: RnMS LocalFixityEnv
658 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
659   = return fixity_env
660
661 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
662 extendFixityEnv fixes enclosed_scope
663                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
664   = let
665         new_fixity_env = extendNameEnvList fixity_env fixes
666     in
667     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
668 \end{code}
669
670 %================
671 \subsubsection{  Mode}
672 %=====================
673
674 \begin{code}
675 getModeRn :: RnMS RnMode
676 getModeRn rn_down (SDown {rn_mode = mode})
677   = return mode
678
679 setModeRn :: RnMode -> RnMS a -> RnMS a
680 setModeRn new_mode thing_inside rn_down l_down
681   = thing_inside rn_down (l_down {rn_mode = new_mode})
682 \end{code}
683
684
685 %************************************************************************
686 %*                                                                      *
687 \subsection{Plumbing for rename-globals part}
688 %*                                                                      *
689 %************************************************************************
690
691 \begin{code}
692 getIfacesRn :: RnM d Ifaces
693 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
694   = readIORef iface_var
695
696 setIfacesRn :: Ifaces -> RnM d ()
697 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
698   = writeIORef iface_var ifaces
699 \end{code}