[project @ 2000-09-22 15:56:12 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 BasicTypes       ( Version, defaultFixity )
39 import ErrUtils         ( addShortErrLocLine, addShortWarnLocLine,
40                           pprBagOfErrors, ErrMsg, WarnMsg, Message
41                         )
42 import RdrName          ( RdrName, dummyRdrVarName, rdrNameOcc,
43                           RdrNameEnv, emptyRdrEnv, extendRdrEnv, 
44                           lookupRdrEnv, addListToRdrEnv, rdrEnvToList, rdrEnvElts
45                         )
46 import Name             ( Name, OccName, NamedThing(..), getSrcLoc,
47                           isLocallyDefinedName, nameModule, nameOccName,
48                           decode, mkLocalName, mkUnboundName,
49                           NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, extendNameEnvList
50                         )
51 import Module           ( Module, ModuleName, ModuleHiMap, SearchPath, WhereFrom,
52                           mkModuleHiMaps, moduleName, mkSearchPath
53                         )
54 import NameSet          
55 import CmdLineOpts      ( opt_D_dump_rn_trace, opt_HiMap )
56 import PrelInfo         ( builtinNames )
57 import SrcLoc           ( SrcLoc, mkGeneratedSrcLoc )
58 import Unique           ( Unique )
59 import FiniteMap        ( FiniteMap, emptyFM, bagToFM )
60 import Bag              ( Bag, mapBag, emptyBag, isEmptyBag, snocBag )
61 import UniqSupply
62 import Outputable
63
64 infixr 9 `thenRn`, `thenRn_`
65 \end{code}
66
67
68 %************************************************************************
69 %*                                                                      *
70 \subsection{Somewhat magical interface to other monads}
71 %*                                                                      *
72 %************************************************************************
73
74 \begin{code}
75 ioToRnM :: IO r -> RnM d (Either IOError r)
76 ioToRnM io rn_down g_down = (io >>= \ ok -> return (Right ok)) 
77                             `catch` 
78                             (\ err -> return (Left err))
79             
80 traceRn :: SDoc -> RnM d ()
81 traceRn msg | opt_D_dump_rn_trace = putDocRn msg
82             | otherwise           = returnRn ()
83
84 putDocRn :: SDoc -> RnM d ()
85 putDocRn msg = ioToRnM (printErrs msg)  `thenRn_`
86                returnRn ()
87 \end{code}
88
89
90 %************************************************************************
91 %*                                                                      *
92 \subsection{Data types}
93 %*                                                                      *
94 %************************************************************************
95
96 %===================================================
97 \subsubsection{         MONAD TYPES}
98 %===================================================
99
100 \begin{code}
101 type RnM d r = RnDown -> d -> IO r
102 type RnMS r  = RnM SDown r              -- Renaming source
103 type RnMG r  = RnM ()    r              -- Getting global names etc
104
105         -- Common part
106 data RnDown = RnDown {
107                   rn_mod     :: Module,
108                   rn_loc     :: SrcLoc,
109                   rn_ns      :: IORef RnNameSupply,
110                   rn_errs    :: IORef (Bag WarnMsg, Bag ErrMsg),
111                   rn_ifaces  :: IORef Ifaces,
112                   rn_hi_maps :: (SearchPath,    -- For error messages
113                                  ModuleHiMap,   -- for .hi files
114                                  ModuleHiMap)   -- for .hi-boot files
115                 }
116
117         -- For renaming source code
118 data SDown = SDown {
119                   rn_mode :: RnMode,
120
121                   rn_genv :: GlobalRdrEnv,
122                         --   Global envt; the fixity component gets extended
123                         --   with local fixity decls
124
125                   rn_lenv :: LocalRdrEnv,       -- Local name envt
126                         --   Does *not* include global name envt; may shadow it
127                         --   Includes both ordinary variables and type variables;
128                         --   they are kept distinct because tyvar have a different
129                         --   occurrence contructor (Name.TvOcc)
130                         -- We still need the unsullied global name env so that
131                         --   we can look up record field names
132
133                   rn_fixenv :: FixityEnv        -- Local fixities
134                         -- The global fixities are held in the
135                         -- rn_ifaces field.  Why?  See the comments
136                         -- with RnIfaces.lookupFixity
137                 }
138
139 data RnMode     = SourceMode                    -- Renaming source code
140                 | InterfaceMode                 -- Renaming interface declarations.  
141 \end{code}
142
143 %===================================================
144 \subsubsection{         ENVIRONMENTS}
145 %===================================================
146
147 \begin{code}
148 --------------------------------
149 type GlobalRdrEnv = RdrNameEnv [Name]   -- The list is because there may be name clashes
150                                         -- These only get reported on lookup,
151                                         -- not on construction
152 type LocalRdrEnv  = RdrNameEnv Name
153
154 --------------------------------
155 type FixityEnv = NameEnv RenamedFixitySig
156         -- We keep the whole fixity sig so that we
157         -- can report line-number info when there is a duplicate
158         -- fixity declaration
159
160 lookupFixity :: FixityEnv -> Name -> Fixity
161 lookupFixity env name
162   = case lookupNameEnv env name of 
163         Just (FixitySig _ fix _) -> fix
164         Nothing                  -> defaultFixity
165
166 --------------------------------
167 type DeprecationEnv = NameEnv DeprecTxt
168 \end{code}
169
170 \begin{code}
171 --------------------------------
172 type RnNameSupply
173  = ( UniqSupply
174
175    , FiniteMap (ModuleName, OccName) Name
176         -- Ensures that one (module,occname) pair gets one unique
177    , FiniteMap OccName Name
178         -- Ensures that one implicit parameter name gets one unique
179    )
180
181
182 --------------------------------
183 type Avails       = [AvailInfo]
184
185 type ExportAvails = (FiniteMap ModuleName Avails,
186         -- Used to figure out "module M" export specifiers
187         -- Includes avails only from *unqualified* imports
188         -- (see 1.4 Report Section 5.1.1)
189
190                      AvailEnv)  -- Used to figure out all other export specifiers.
191                         
192
193 data GenAvailInfo name  = Avail name     -- An ordinary identifier
194                         | AvailTC name   -- The name of the type or class
195                                   [name] -- The available pieces of type/class.
196                                          -- NB: If the type or class is itself
197                                          -- to be in scope, it must be in this list.
198                                          -- Thus, typically: AvailTC Eq [Eq, ==, /=]
199                         deriving( Eq )
200                         -- Equality used when deciding if the interface has changed
201
202 type AvailEnv     = NameEnv AvailInfo   -- Maps a Name to the AvailInfo that contains it
203 type AvailInfo    = GenAvailInfo Name
204 type RdrAvailInfo = GenAvailInfo OccName
205 \end{code}
206
207 %===================================================
208 \subsubsection{         INTERFACE FILE STUFF}
209 %===================================================
210
211 \begin{code}
212 type ExportItem          = (ModuleName, [RdrAvailInfo])
213
214 type ImportVersion name  = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
215
216 type ModVersionInfo     = (Version,             -- Version of the whole module
217                            Version,             -- Version number for all fixity decls together
218                            Version)             -- ...ditto all rules together
219
220 type WhetherHasOrphans   = Bool
221         -- An "orphan" is 
222         --      * an instance decl in a module other than the defn module for 
223         --              one of the tycons or classes in the instance head
224         --      * a transformation rule in a module other than the one defining
225         --              the function in the head of the rule.
226
227 type IsBootInterface     = Bool
228
229 data WhatsImported name  = NothingAtAll                         -- The module is below us in the
230                                                                 -- hierarchy, but we import nothing
231
232                          | Everything Version                   -- The module version
233
234                          | Specifically Version                 -- Module version
235                                         Version                 -- Fixity version
236                                         Version                 -- Rules version
237                                         [(name,Version)]        -- List guaranteed non-empty
238                          deriving( Eq )
239         -- 'Specifically' doesn't let you say "I imported f but none of the fixities in
240         -- the module. If you use anything in the module you get its fixity and rule version
241         -- So if the fixities or rules change, you'll recompile, even if you don't use either.
242         -- This is easy to implement, and it's safer: you might not have used the rules last
243         -- time round, but if someone has added a new rule you might need it this time
244
245         -- 'Everything' means there was a "module M" in 
246         -- this module's export list, so we just have to go by M's version,
247         -- not the list of (name,version) pairs
248
249 data ParsedIface
250   = ParsedIface {
251       pi_mod       :: Module,                           -- Complete with package info
252       pi_vers      :: Version,                          -- Module version number
253       pi_orphan    :: WhetherHasOrphans,                -- Whether this module has orphans
254       pi_usages    :: [ImportVersion OccName],          -- Usages
255       pi_exports   :: [ExportItem],                     -- Exports
256       pi_insts     :: [RdrNameInstDecl],                -- Local instance declarations
257       pi_decls     :: [(Version, RdrNameHsDecl)],       -- Local definitions
258       pi_fixity    :: (Version, [RdrNameFixitySig]),    -- Local fixity declarations, with their version
259       pi_rules     :: (Version, [RdrNameRuleDecl]),     -- Rules, with their version
260       pi_deprecs   :: [RdrNameDeprecation]              -- Deprecations
261     }
262
263
264 type RdrNamePragma = ()                         -- Fudge for now
265 -------------------
266
267 data Ifaces = Ifaces {
268                 iImpModInfo :: ImportedModuleInfo,
269                                 -- Modules this one depends on: that is, the union 
270                                 -- of the modules its *direct* imports depend on.
271                                 -- NB: The direct imports have .hi files that enumerate *all* the
272                                 -- dependencies (direct or not) of the imported module.
273
274                 iDecls :: DeclsMap,     -- A single, global map of Names to decls
275
276                 iDeferred :: NameSet,   -- data (not newtype) TyCons that have been slurped, 
277                                         -- but none of their constructors have.
278                                         -- If this is still the case right at the end
279                                         -- we can get away with importing them abstractly
280
281                 iFixes :: FixityEnv,    
282                                 -- A single, global map of Names to fixities
283                                 -- See comments with RnIfaces.lookupFixity
284
285                 iSlurp :: NameSet,
286                 -- All the names (whether "big" or "small", whether wired-in or not,
287                 -- whether locally defined or not) that have been slurped in so far.
288
289                 iVSlurp :: [(Name,Version)],
290                 -- All the (a) non-wired-in (b) "big" (c) non-locally-defined 
291                 -- names that have been slurped in so far, with their versions.
292                 -- This is used to generate the "usage" information for this module.
293                 -- Subset of the previous field.
294
295                 iInsts :: IfaceInsts,
296                 -- The as-yet un-slurped instance decls; this bag is depleted when we
297                 -- slurp an instance decl so that we don't slurp the same one twice.
298                 -- Each is 'gated' by the names that must be available before
299                 -- this instance decl is needed.
300
301                 iRules :: IfaceRules,
302                 -- Similar to instance decls, only for rules
303
304                 iDeprecs :: DeprecationEnv
305         }
306
307 type IfaceInsts = Bag GatedDecl
308 type IfaceRules = Bag GatedDecl
309
310 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
311
312 type ImportedModuleInfo 
313      = FiniteMap ModuleName (WhetherHasOrphans, IsBootInterface, 
314                              Maybe (Module, Version, Version, Version, WhereFrom, Avails))
315                                 -- The three Versions are module version, fixity version, rules version
316
317                 -- Suppose the domain element is module 'A'
318                 --
319                 -- The first Bool is True if A contains 
320                 -- 'orphan' rules or instance decls
321
322                 -- The second Bool is true if the interface file actually
323                 -- read was an .hi-boot file
324
325                 -- Nothing => A's interface not yet read, but this module has
326                 --            imported a module, B, that itself depends on A
327                 --
328                 -- Just xx => A's interface has been read.  The Module in 
329                 --              the Just has the correct Dll flag
330
331                 -- This set is used to decide whether to look for
332                 -- A.hi or A.hi-boot when importing A.f.
333                 -- Basically, we look for A.hi if A is in the map, and A.hi-boot
334                 -- otherwise
335
336 type DeclsMap = NameEnv (Version, AvailInfo, Bool, (Module, RdrNameHsDecl))
337                 -- A DeclsMap contains a binding for each Name in the declaration
338                 -- including the constructors of a type decl etc.
339                 -- The Bool is True just for the 'main' Name.
340 \end{code}
341
342
343 %************************************************************************
344 %*                                                                      *
345 \subsection{Main monad code}
346 %*                                                                      *
347 %************************************************************************
348
349 \begin{code}
350 initRn :: Module -> UniqSupply -> SearchPath -> SrcLoc
351        -> RnMG r
352        -> IO (r, Bag ErrMsg, Bag WarnMsg)
353
354 initRn mod us dirs loc do_rn = do
355   himaps    <- mkModuleHiMaps dirs
356   names_var <- newIORef (us, builtins, emptyFM)
357   errs_var  <- newIORef (emptyBag,emptyBag)
358   iface_var <- newIORef emptyIfaces 
359   let
360         rn_down = RnDown { rn_loc = loc, rn_ns = names_var, 
361                            rn_errs = errs_var, 
362                            rn_hi_maps = himaps, 
363                            rn_ifaces = iface_var,
364                            rn_mod = mod }
365
366         -- do the business
367   res <- do_rn rn_down ()
368
369         -- grab errors and return
370   (warns, errs) <- readIORef errs_var
371
372   return (res, errs, warns)
373
374
375 initRnMS :: GlobalRdrEnv -> FixityEnv -> RnMode -> RnMS r -> RnM d r
376 initRnMS rn_env fixity_env mode thing_inside rn_down g_down
377   = let
378         s_down = SDown { rn_genv = rn_env, rn_lenv = emptyRdrEnv, 
379                          rn_fixenv = fixity_env, rn_mode = mode }
380     in
381     thing_inside rn_down s_down
382
383 initIfaceRnMS :: Module -> RnMS r -> RnM d r
384 initIfaceRnMS mod thing_inside 
385   = initRnMS emptyRdrEnv emptyNameEnv InterfaceMode $
386     setModuleRn mod thing_inside
387
388 emptyIfaces :: Ifaces
389 emptyIfaces = Ifaces { iImpModInfo = emptyFM,
390                        iDecls = emptyNameEnv,
391                        iDeferred = emptyNameSet,
392                        iFixes = emptyNameEnv,
393                        iSlurp = unitNameSet (mkUnboundName dummyRdrVarName),
394                         -- Pretend that the dummy unbound name has already been
395                         -- slurped.  This is what's returned for an out-of-scope name,
396                         -- and we don't want thereby to try to suck it in!
397                        iVSlurp = [],
398                        iInsts = emptyBag,
399                        iRules = emptyBag,
400                        iDeprecs = emptyNameEnv
401               }
402
403 builtins :: FiniteMap (ModuleName,OccName) Name
404 builtins = 
405    bagToFM (
406    mapBag (\ name ->  ((moduleName (nameModule name), nameOccName name), name))
407           builtinNames)
408 \end{code}
409
410 @renameSourceCode@ is used to rename stuff ``out-of-line'';
411 that is, not as part of the main renamer.
412 Sole examples: derived definitions,
413 which are only generated in the type checker.
414
415 The @RnNameSupply@ includes a @UniqueSupply@, so if you call it more than
416 once you must either split it, or install a fresh unique supply.
417
418 \begin{code}
419 renameSourceCode :: Module
420                  -> RnNameSupply
421                  -> RnMS r
422                  -> r
423
424 renameSourceCode mod name_supply m
425   = unsafePerformIO (
426         -- It's not really unsafe!  When renaming source code we
427         -- only do any I/O if we need to read in a fixity declaration;
428         -- and that doesn't happen in pragmas etc
429
430         mkModuleHiMaps (mkSearchPath opt_HiMap) >>= \ himaps ->
431         newIORef name_supply            >>= \ names_var ->
432         newIORef (emptyBag,emptyBag)    >>= \ errs_var ->
433         let
434             rn_down = RnDown { rn_loc = mkGeneratedSrcLoc, rn_ns = names_var,
435                                rn_errs = errs_var, rn_hi_maps = himaps,
436                                rn_mod = mod, 
437                                rn_ifaces = panic "rnameSourceCode: rn_ifaces"  -- Not required
438                              }
439             s_down = SDown { rn_mode = InterfaceMode,
440                                -- So that we can refer to PrelBase.True etc
441                              rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
442                              rn_fixenv = emptyNameEnv }
443         in
444         m rn_down s_down                        >>= \ result ->
445         
446         readIORef errs_var                      >>= \ (warns,errs) ->
447
448         (if not (isEmptyBag errs) then
449                 pprTrace "Urk! renameSourceCode found errors" (display errs) 
450 #ifdef DEBUG
451          else if not (isEmptyBag warns) then
452                 pprTrace "Note: renameSourceCode found warnings" (display warns)
453 #endif
454          else
455                 id) $
456
457         return result
458     )
459   where
460     display errs = pprBagOfErrors errs
461
462 {-# INLINE thenRn #-}
463 {-# INLINE thenRn_ #-}
464 {-# INLINE returnRn #-}
465 {-# INLINE andRn #-}
466
467 returnRn :: a -> RnM d a
468 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
469 thenRn_  :: RnM d a -> RnM d b -> RnM d b
470 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
471 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
472 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
473 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
474 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
475 sequenceRn :: [RnM d a] -> RnM d [a]
476 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
477 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
478 fixRn    :: (a -> RnM d a) -> RnM d a
479
480 returnRn v gdown ldown  = return v
481 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
482 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
483 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
484 andRn combiner m1 m2 gdown ldown
485   = m1 gdown ldown >>= \ res1 ->
486     m2 gdown ldown >>= \ res2 ->
487     return (combiner res1 res2)
488
489 sequenceRn []     = returnRn []
490 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
491                      sequenceRn ms      `thenRn` \ rs ->
492                      returnRn (r:rs)
493
494 mapRn f []     = returnRn []
495 mapRn f (x:xs)
496   = f x         `thenRn` \ r ->
497     mapRn f xs  `thenRn` \ rs ->
498     returnRn (r:rs)
499
500 mapRn_ f []     = returnRn ()
501 mapRn_ f (x:xs) = 
502     f x         `thenRn_`
503     mapRn_ f xs
504
505 foldlRn k z [] = returnRn z
506 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
507                      foldlRn k z' xs
508
509 mapAndUnzipRn f [] = returnRn ([],[])
510 mapAndUnzipRn f (x:xs)
511   = f x                 `thenRn` \ (r1,  r2)  ->
512     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
513     returnRn (r1:rs1, r2:rs2)
514
515 mapAndUnzip3Rn f [] = returnRn ([],[],[])
516 mapAndUnzip3Rn f (x:xs)
517   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
518     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
519     returnRn (r1:rs1, r2:rs2, r3:rs3)
520
521 mapMaybeRn f []     = returnRn []
522 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
523                       mapMaybeRn f xs   `thenRn` \ rs ->
524                       case maybe_r of
525                         Nothing -> returnRn rs
526                         Just r  -> returnRn (r:rs)
527
528 flatMapRn f []     = returnRn []
529 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
530                      flatMapRn f xs     `thenRn` \ rs ->
531                      returnRn (r ++ rs)
532 \end{code}
533
534
535
536 %************************************************************************
537 %*                                                                      *
538 \subsection{Boring plumbing for common part}
539 %*                                                                      *
540 %************************************************************************
541
542
543 %================
544 \subsubsection{  Errors and warnings}
545 %=====================
546
547 \begin{code}
548 failWithRn :: a -> Message -> RnM d a
549 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
550   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
551     writeIORef errs_var (warns, errs `snocBag` err)             >> 
552     return res
553   where
554     err = addShortErrLocLine loc msg
555
556 warnWithRn :: a -> Message -> RnM d a
557 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
558   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
559     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
560     return res
561   where
562     warn = addShortWarnLocLine loc msg
563
564 addErrRn :: Message -> RnM d ()
565 addErrRn err = failWithRn () err
566
567 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
568 checkRn False err = addErrRn err
569 checkRn True  err = returnRn ()
570
571 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
572 warnCheckRn False err = addWarnRn err
573 warnCheckRn True  err = returnRn ()
574
575 addWarnRn :: Message -> RnM d ()
576 addWarnRn warn = warnWithRn () warn
577
578 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
579 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
580   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
581     return (isEmptyBag errs)
582 \end{code}
583
584
585 %================
586 \subsubsection{  Source location}
587 %=====================
588
589 \begin{code}
590 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
591 pushSrcLocRn loc' m down l_down
592   = m (down {rn_loc = loc'}) l_down
593
594 getSrcLocRn :: RnM d SrcLoc
595 getSrcLocRn down l_down
596   = return (rn_loc down)
597 \end{code}
598
599 %================
600 \subsubsection{  Name supply}
601 %=====================
602
603 \begin{code}
604 getNameSupplyRn :: RnM d RnNameSupply
605 getNameSupplyRn rn_down l_down
606   = readIORef (rn_ns rn_down)
607
608 setNameSupplyRn :: RnNameSupply -> RnM d ()
609 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
610   = writeIORef names_var names'
611
612 getUniqRn :: RnM d Unique
613 getUniqRn (RnDown {rn_ns = names_var}) l_down
614  = readIORef names_var >>= \ (us, cache, ipcache) ->
615    let
616      (us1,us') = splitUniqSupply us
617    in
618    writeIORef names_var (us', cache, ipcache)  >>
619    return (uniqFromSupply us1)
620 \end{code}
621
622 %================
623 \subsubsection{  Module}
624 %=====================
625
626 \begin{code}
627 getModuleRn :: RnM d Module
628 getModuleRn (RnDown {rn_mod = mod}) l_down
629   = return mod
630
631 setModuleRn :: Module -> RnM d a -> RnM d a
632 setModuleRn new_mod enclosed_thing rn_down l_down
633   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
634 \end{code}
635
636
637 %************************************************************************
638 %*                                                                      *
639 \subsection{Plumbing for rename-source part}
640 %*                                                                      *
641 %************************************************************************
642
643 %================
644 \subsubsection{  RnEnv}
645 %=====================
646
647 \begin{code}
648 getNameEnvs :: RnMS (GlobalRdrEnv, LocalRdrEnv)
649 getNameEnvs rn_down (SDown {rn_genv = global_env, rn_lenv = local_env})
650   = return (global_env, local_env)
651
652 getLocalNameEnv :: RnMS LocalRdrEnv
653 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
654   = return local_env
655
656 getGlobalNameEnv :: RnMS GlobalRdrEnv
657 getGlobalNameEnv rn_down (SDown {rn_genv = global_env})
658   = return global_env
659
660 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
661 setLocalNameEnv local_env' m rn_down l_down
662   = m rn_down (l_down {rn_lenv = local_env'})
663
664 getFixityEnv :: RnMS FixityEnv
665 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
666   = return fixity_env
667
668 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
669 extendFixityEnv fixes enclosed_scope
670                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
671   = let
672         new_fixity_env = extendNameEnvList fixity_env fixes
673     in
674     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
675 \end{code}
676
677 %================
678 \subsubsection{  Mode}
679 %=====================
680
681 \begin{code}
682 getModeRn :: RnMS RnMode
683 getModeRn rn_down (SDown {rn_mode = mode})
684   = return mode
685
686 setModeRn :: RnMode -> RnMS a -> RnMS a
687 setModeRn new_mode thing_inside rn_down l_down
688   = thing_inside rn_down (l_down {rn_mode = new_mode})
689 \end{code}
690
691
692 %************************************************************************
693 %*                                                                      *
694 \subsection{Plumbing for rename-globals part}
695 %*                                                                      *
696 %************************************************************************
697
698 \begin{code}
699 getIfacesRn :: RnM d Ifaces
700 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
701   = readIORef iface_var
702
703 setIfacesRn :: Ifaces -> RnM d ()
704 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
705   = writeIORef iface_var ifaces
706
707 getHiMaps :: RnM d (SearchPath, ModuleHiMap, ModuleHiMap)
708 getHiMaps (RnDown {rn_hi_maps = himaps}) _ 
709   = return himaps
710 \end{code}
711 \end{code}