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