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