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