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