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