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