[project @ 2001-03-14 15:26:00 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 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 NameEnv          ( NameEnv, lookupNameEnv, emptyNameEnv, 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                 -- It's used for two things:
281                 --      a) To record what we've already slurped, so
282                 --         we can no-op if we try to slurp it again
283                 --      b) As the 'gates' for importing rules.  We import a rule
284                 --         if all its LHS free vars have been slurped
285
286         iVSlurp :: (ModuleSet, NameSet)
287                 -- The Names are all the (a) non-wired-in
288                 --                       (b) "big"
289                 --                       (c) non-locally-defined
290                 --                       (d) home-package
291                 -- names that have been slurped in so far, with their versions.
292                 -- This is used to generate the "usage" information for this module.
293                 -- Subset of the previous field.
294                 --
295                 -- The module set is the non-home-package modules from which we have
296                 -- slurped at least one name.
297                 -- It's worth keeping separately, because there's no very easy 
298                 -- way to distinguish the "big" names from the "non-big" ones.
299                 -- But this is a decision we might want to revisit.
300     }
301 \end{code}
302
303
304 %************************************************************************
305 %*                                                                      *
306 \subsection{Main monad code}
307 %*                                                                      *
308 %************************************************************************
309
310 \begin{code}
311 runRn dflags hit hst pcs mod do_rn
312   = do { (pcs, msgs, r) <- initRn dflags hit hst pcs mod do_rn ;
313          printErrorsAndWarnings alwaysQualify msgs ;
314          return (pcs, errorsFound msgs, r)
315     }
316
317 initRn :: DynFlags
318        -> HomeIfaceTable -> HomeSymbolTable
319        -> PersistentCompilerState
320        -> Module
321        -> RnMG t
322        -> IO (PersistentCompilerState, Messages, t)     
323
324 initRn dflags hit hst pcs mod do_rn
325   = do 
326         let prs = pcs_PRS pcs
327         let pte = pcs_PTE pcs
328         let ifaces = Ifaces { iPIT   = pcs_PIT pcs,
329                               iDecls = prsDecls prs,
330                               iInsts = prsInsts prs,
331                               iRules = prsRules prs,
332
333                               iImpModInfo = prsImpMods prs,
334                               iSlurp      = unitNameSet (mkUnboundName dummyRdrVarName),
335                                 -- Pretend that the dummy unbound name has already been
336                                 -- slurped.  This is what's returned for an out-of-scope name,
337                                 -- and we don't want thereby to try to suck it in!
338                               iVSlurp = (emptyModuleSet, emptyNameSet)
339                       }
340         names_var <- newIORef (prsOrig prs)
341         errs_var  <- newIORef (emptyBag,emptyBag)
342         iface_var <- newIORef ifaces
343         let rn_down = RnDown { rn_mod = mod,
344                                rn_loc = noSrcLoc, 
345         
346                                rn_dflags = dflags,
347                                rn_hit    = hit,
348                                rn_done   = lookupType hst pte,
349                                              
350                                rn_ns     = names_var, 
351                                rn_errs   = errs_var, 
352                                rn_ifaces = iface_var,
353                              }
354         
355         -- do the business
356         res <- do_rn rn_down ()
357         
358         -- Grab state and record it
359         (warns, errs)   <- readIORef errs_var
360         new_ifaces      <- readIORef iface_var
361         new_orig        <- readIORef names_var
362         let new_prs = prs { prsOrig    = new_orig,
363                             prsImpMods = iImpModInfo new_ifaces,
364                             prsDecls   = iDecls new_ifaces,
365                             prsInsts   = iInsts new_ifaces,
366                             prsRules   = iRules new_ifaces }
367         let new_pcs = pcs { pcs_PIT = iPIT new_ifaces, 
368                             pcs_PRS = new_prs }
369         
370         return (new_pcs, (warns, errs), res)
371
372 initRnMS :: GlobalRdrEnv -> LocalRdrEnv -> LocalFixityEnv -> RnMode
373          -> RnMS a -> RnM d a
374
375 initRnMS rn_env local_env fixity_env mode thing_inside rn_down g_down
376         -- The fixity_env appears in both the rn_fixenv field
377         -- and in the HIT.  See comments with RnHiFiles.lookupFixityRn
378   = let
379         s_down = SDown { rn_genv = rn_env, rn_lenv = local_env, 
380                          rn_fixenv = fixity_env, rn_mode = mode }
381     in
382     thing_inside rn_down s_down
383
384 initIfaceRnMS :: Module -> RnMS r -> RnM d r
385 initIfaceRnMS mod thing_inside 
386   = initRnMS emptyRdrEnv emptyRdrEnv emptyLocalFixityEnv InterfaceMode $
387     setModuleRn mod thing_inside
388 \end{code}
389
390 @renameDerivedCode@ is used to rename stuff ``out-of-line'';
391 that is, not as part of the main renamer.
392 Sole examples: derived definitions,
393 which are only generated in the type checker.
394
395 The @NameSupply@ includes a @UniqueSupply@, so if you call it more than
396 once you must either split it, or install a fresh unique supply.
397
398 \begin{code}
399 renameDerivedCode :: DynFlags 
400                   -> Module
401                   -> PersistentRenamerState
402                   -> RnMS r
403                   -> r
404
405 renameDerivedCode dflags mod prs thing_inside
406   = unsafePerformIO $
407         -- It's not really unsafe!  When renaming source code we
408         -- only do any I/O if we need to read in a fixity declaration;
409         -- and that doesn't happen in pragmas etc
410
411     do  { us <- mkSplitUniqSupply 'r'
412         ; names_var <- newIORef ((prsOrig prs) { nsUniqs = us })
413         ; errs_var <- newIORef (emptyBag,emptyBag)
414
415         ; let rn_down = RnDown { rn_dflags = dflags,
416                                  rn_loc    = generatedSrcLoc, rn_ns = names_var,
417                                  rn_errs   = errs_var, 
418                                  rn_mod    = mod, 
419                                  rn_done   = bogus "rn_done",   
420                                  rn_hit    = bogus "rn_hit",
421                                  rn_ifaces = bogus "rn_ifaces"
422                                }
423         ; let s_down = SDown { rn_mode = InterfaceMode,
424                                -- So that we can refer to PrelBase.True etc
425                                rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
426                                rn_fixenv = emptyLocalFixityEnv }
427
428         ; result <- thing_inside rn_down s_down
429         ; messages <- readIORef errs_var
430
431         ; if bad messages then
432                 do { hPutStr stderr "Urk!  renameDerivedCode found errors or warnings"
433                    ; printErrorsAndWarnings alwaysQualify messages
434                    }
435            else
436                 return()
437
438         ; return result
439         }
440   where
441 #ifdef DEBUG
442     bad messages = errorsFound messages || warningsFound messages
443 #else
444     bad messages = errorsFound messages
445 #endif
446
447 bogus s = panic ("rnameSourceCode: " ++ s)  -- Used for unused record fields
448
449 {-# INLINE thenRn #-}
450 {-# INLINE thenRn_ #-}
451 {-# INLINE returnRn #-}
452 {-# INLINE andRn #-}
453
454 returnRn :: a -> RnM d a
455 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
456 thenRn_  :: RnM d a -> RnM d b -> RnM d b
457 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
458 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
459 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
460 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
461 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
462 sequenceRn  :: [RnM d a] -> RnM d [a]
463 sequenceRn_ :: [RnM d a] -> RnM d ()
464 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
465 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
466 fixRn    :: (a -> RnM d a) -> RnM d a
467
468 returnRn v gdown ldown  = return v
469 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
470 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
471 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
472 andRn combiner m1 m2 gdown ldown
473   = m1 gdown ldown >>= \ res1 ->
474     m2 gdown ldown >>= \ res2 ->
475     return (combiner res1 res2)
476
477 sequenceRn []     = returnRn []
478 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
479                      sequenceRn ms      `thenRn` \ rs ->
480                      returnRn (r:rs)
481
482 sequenceRn_ []     = returnRn ()
483 sequenceRn_ (m:ms) = m `thenRn_` sequenceRn_ ms
484
485 mapRn f []     = returnRn []
486 mapRn f (x:xs)
487   = f x         `thenRn` \ r ->
488     mapRn f xs  `thenRn` \ rs ->
489     returnRn (r:rs)
490
491 mapRn_ f []     = returnRn ()
492 mapRn_ f (x:xs) = 
493     f x         `thenRn_`
494     mapRn_ f xs
495
496 foldlRn k z [] = returnRn z
497 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
498                      foldlRn k z' xs
499
500 mapAndUnzipRn f [] = returnRn ([],[])
501 mapAndUnzipRn f (x:xs)
502   = f x                 `thenRn` \ (r1,  r2)  ->
503     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
504     returnRn (r1:rs1, r2:rs2)
505
506 mapAndUnzip3Rn f [] = returnRn ([],[],[])
507 mapAndUnzip3Rn f (x:xs)
508   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
509     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
510     returnRn (r1:rs1, r2:rs2, r3:rs3)
511
512 mapMaybeRn f []     = returnRn []
513 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
514                       mapMaybeRn f xs   `thenRn` \ rs ->
515                       case maybe_r of
516                         Nothing -> returnRn rs
517                         Just r  -> returnRn (r:rs)
518
519 flatMapRn f []     = returnRn []
520 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
521                      flatMapRn f xs     `thenRn` \ rs ->
522                      returnRn (r ++ rs)
523 \end{code}
524
525
526
527 %************************************************************************
528 %*                                                                      *
529 \subsection{Boring plumbing for common part}
530 %*                                                                      *
531 %************************************************************************
532
533
534 %================
535 \subsubsection{  Errors and warnings}
536 %=====================
537
538 \begin{code}
539 failWithRn :: a -> Message -> RnM d a
540 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
541   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
542     writeIORef errs_var (warns, errs `snocBag` err)             >> 
543     return res
544   where
545     err = addShortErrLocLine loc msg
546
547 warnWithRn :: a -> Message -> RnM d a
548 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
549   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
550     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
551     return res
552   where
553     warn = addShortWarnLocLine loc msg
554
555 addErrRn :: Message -> RnM d ()
556 addErrRn err = failWithRn () err
557
558 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
559 checkRn False err = addErrRn err
560 checkRn True  err = returnRn ()
561
562 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
563 warnCheckRn False err = addWarnRn err
564 warnCheckRn True  err = returnRn ()
565
566 addWarnRn :: Message -> RnM d ()
567 addWarnRn warn = warnWithRn () warn
568
569 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
570 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
571   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
572     return (isEmptyBag errs)
573
574 doptRn :: DynFlag -> RnM d Bool
575 doptRn dflag (RnDown { rn_dflags = dflags}) l_down
576    = return (dopt dflag dflags)
577
578 getDOptsRn :: RnM d DynFlags
579 getDOptsRn (RnDown { rn_dflags = dflags}) l_down
580    = return dflags
581 \end{code}
582
583
584 %================
585 \subsubsection{Source location}
586 %=====================
587
588 \begin{code}
589 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
590 pushSrcLocRn loc' m down l_down
591   = m (down {rn_loc = loc'}) l_down
592
593 getSrcLocRn :: RnM d SrcLoc
594 getSrcLocRn down l_down
595   = return (rn_loc down)
596 \end{code}
597
598 %================
599 \subsubsection{The finder and home symbol table}
600 %=====================
601
602 \begin{code}
603 getHomeIfaceTableRn :: RnM d HomeIfaceTable
604 getHomeIfaceTableRn down l_down = return (rn_hit down)
605
606 getTypeEnvRn :: RnM d (Name -> Maybe TyThing)
607 getTypeEnvRn down l_down = return (rn_done down)
608 \end{code}
609
610 %================
611 \subsubsection{Name supply}
612 %=====================
613
614 \begin{code}
615 getNameSupplyRn :: RnM d NameSupply
616 getNameSupplyRn rn_down l_down
617   = readIORef (rn_ns rn_down)
618
619 setNameSupplyRn :: NameSupply -> RnM d ()
620 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
621   = writeIORef names_var names'
622
623 getUniqRn :: RnM d Unique
624 getUniqRn (RnDown {rn_ns = names_var}) l_down
625  = readIORef names_var >>= \ ns ->
626    let
627      (us1,us') = splitUniqSupply (nsUniqs ns)
628    in
629    writeIORef names_var (ns {nsUniqs = us'})    >>
630    return (uniqFromSupply us1)
631 \end{code}
632
633 %================
634 \subsubsection{  Module}
635 %=====================
636
637 \begin{code}
638 getModuleRn :: RnM d Module
639 getModuleRn (RnDown {rn_mod = mod}) l_down
640   = return mod
641
642 setModuleRn :: Module -> RnM d a -> RnM d a
643 setModuleRn new_mod enclosed_thing rn_down l_down
644   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
645 \end{code}
646
647
648 %************************************************************************
649 %*                                                                      *
650 \subsection{Plumbing for rename-source part}
651 %*                                                                      *
652 %************************************************************************
653
654 %================
655 \subsubsection{  RnEnv}
656 %=====================
657
658 \begin{code}
659 getLocalNameEnv :: RnMS LocalRdrEnv
660 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
661   = return local_env
662
663 getGlobalNameEnv :: RnMS GlobalRdrEnv
664 getGlobalNameEnv rn_down (SDown {rn_genv = global_env})
665   = return global_env
666
667 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
668 setLocalNameEnv local_env' m rn_down l_down
669   = m rn_down (l_down {rn_lenv = local_env'})
670
671 getFixityEnv :: RnMS LocalFixityEnv
672 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
673   = return fixity_env
674
675 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
676 extendFixityEnv fixes enclosed_scope
677                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
678   = let
679         new_fixity_env = extendNameEnvList fixity_env fixes
680     in
681     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
682 \end{code}
683
684 %================
685 \subsubsection{  Mode}
686 %=====================
687
688 \begin{code}
689 getModeRn :: RnMS RnMode
690 getModeRn rn_down (SDown {rn_mode = mode})
691   = return mode
692
693 setModeRn :: RnMode -> RnMS a -> RnMS a
694 setModeRn new_mode thing_inside rn_down l_down
695   = thing_inside rn_down (l_down {rn_mode = new_mode})
696 \end{code}
697
698
699 %************************************************************************
700 %*                                                                      *
701 \subsection{Plumbing for rename-globals part}
702 %*                                                                      *
703 %************************************************************************
704
705 \begin{code}
706 getIfacesRn :: RnM d Ifaces
707 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
708   = readIORef iface_var
709
710 setIfacesRn :: Ifaces -> RnM d ()
711 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
712   = writeIORef iface_var ifaces
713 \end{code}