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