51319d123a47a40ea9034610bc5f70edc67cc368
[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                           OrigNameEnv(..), OrigNameNameEnv, OrigNameIParamEnv,
41                           WhetherHasOrphans, ImportVersion, 
42                           PersistentRenamerState(..), IsBootInterface, Avails,
43                           DeclsMap, IfaceInsts, IfaceRules, 
44                           HomeSymbolTable, TyThing,
45                           PersistentCompilerState(..), GlobalRdrEnv,
46                           HomeIfaceTable, PackageIfaceTable,
47                           RdrAvailInfo )
48 import BasicTypes       ( Version, defaultFixity )
49 import ErrUtils         ( addShortErrLocLine, addShortWarnLocLine,
50                           Message, Messages, errorsFound, warningsFound,
51                           printErrorsAndWarnings
52                         )
53 import RdrName          ( RdrName, dummyRdrVarName, rdrNameModule, rdrNameOcc,
54                           RdrNameEnv, emptyRdrEnv, extendRdrEnv, 
55                           addListToRdrEnv, rdrEnvToList, rdrEnvElts
56                         )
57 import Name             ( Name, OccName, NamedThing(..), 
58                           nameOccName,
59                           decode, mkLocalName, mkKnownKeyGlobal
60                         )
61 import Name             ( NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, extendNameEnvList )
62 import Module           ( Module, ModuleName, ModuleSet, emptyModuleSet )
63 import NameSet          
64 import CmdLineOpts      ( DynFlags, DynFlag(..), dopt )
65 import SrcLoc           ( SrcLoc, generatedSrcLoc, noSrcLoc )
66 import Unique           ( Unique )
67 import FiniteMap        ( FiniteMap, emptyFM )
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
145         -- The second and third components are a flattened-out OrigNameEnv
146         rn_ns      :: IORef (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv),
147         rn_ifaces  :: IORef Ifaces
148     }
149
150         -- For renaming source code
151 data SDown = SDown {
152                   rn_mode :: RnMode,
153
154                   rn_genv :: GlobalRdrEnv,      -- Top level environment
155
156                   rn_lenv :: LocalRdrEnv,       -- Local name envt
157                         --   Does *not* include global name envt; may shadow it
158                         --   Includes both ordinary variables and type variables;
159                         --   they are kept distinct because tyvar have a different
160                         --   occurrence contructor (Name.TvOcc)
161                         -- We still need the unsullied global name env so that
162                         --   we can look up record field names
163
164                   rn_fixenv :: LocalFixityEnv   -- Local fixities (for non-top-level
165                                                 -- declarations)
166                         -- The global fixities are held in the
167                         -- HIT or PIT.  Why?  See the comments
168                         -- with RnIfaces.lookupLocalFixity
169                 }
170
171 data RnMode     = SourceMode                    -- Renaming source code
172                 | InterfaceMode                 -- Renaming interface declarations.  
173 \end{code}
174
175 %===================================================
176 \subsubsection{         ENVIRONMENTS}
177 %===================================================
178
179 \begin{code}
180 --------------------------------
181 type LocalRdrEnv    = RdrNameEnv Name
182 type LocalFixityEnv = NameEnv RenamedFixitySig
183         -- We keep the whole fixity sig so that we
184         -- can report line-number info when there is a duplicate
185         -- fixity declaration
186
187 emptyLocalFixityEnv :: LocalFixityEnv
188 emptyLocalFixityEnv = emptyNameEnv
189
190 lookupLocalFixity :: LocalFixityEnv -> Name -> Fixity
191 lookupLocalFixity env name
192   = case lookupNameEnv env name of 
193         Just (FixitySig _ fix _) -> fix
194         Nothing                  -> defaultFixity
195 \end{code}
196
197 \begin{code}
198 type ExportAvails = (FiniteMap ModuleName Avails,
199         -- Used to figure out "module M" export specifiers
200         -- Includes avails only from *unqualified* imports
201         -- (see 1.4 Report Section 5.1.1)
202
203                      AvailEnv)  -- Used to figure out all other export specifiers.
204 \end{code}
205
206 %===================================================
207 \subsubsection{         INTERFACE FILE STUFF}
208 %===================================================
209
210 \begin{code}
211 type ExportItem   = (ModuleName, [RdrAvailInfo])
212 type IfaceDeprecs = Maybe (Either DeprecTxt [(RdrName,DeprecTxt)])
213         -- Nothing        => NoDeprecs
214         -- Just (Left t)  => DeprecAll
215         -- Just (Right p) => DeprecSome
216
217 data ParsedIface
218   = ParsedIface {
219       pi_mod       :: Module,                           -- Complete with package info
220       pi_vers      :: Version,                          -- Module version number
221       pi_orphan    :: WhetherHasOrphans,                -- Whether this module has orphans
222       pi_usages    :: [ImportVersion OccName],          -- Usages
223       pi_exports   :: (Version, [ExportItem]),          -- Exports
224       pi_decls     :: [(Version, RdrNameTyClDecl)],     -- Local definitions
225       pi_fixity    :: [RdrNameFixitySig],               -- Local fixity declarations,
226       pi_insts     :: [RdrNameInstDecl],                -- Local instance declarations
227       pi_rules     :: (Version, [RdrNameRuleDecl]),     -- Rules, with their version
228       pi_deprecs   :: IfaceDeprecs                      -- Deprecations
229     }
230 \end{code}
231
232 %************************************************************************
233 %*                                                                      *
234 \subsection{The renamer state}
235 %*                                                                      *
236 %************************************************************************
237
238 \begin{code}
239 data Ifaces = Ifaces {
240     -- PERSISTENT FIELDS
241         iPIT :: PackageIfaceTable,
242                 -- The ModuleIFaces for modules in other packages
243                 -- whose interfaces we have opened
244                 -- The declarations in these interface files are held in
245                 -- iDecls, iInsts, iRules (below), not in the mi_decls fields
246                 -- of the iPIT.  What _is_ in the iPIT is:
247                 --      * The Module 
248                 --      * Version info
249                 --      * Its exports
250                 --      * Fixities
251                 --      * Deprecations
252                 -- The iPIT field is initialised from the compiler's persistent
253                 -- package symbol table, and the renamer incrementally adds
254                 -- to it.
255
256         iDecls :: DeclsMap,     
257                 -- A single, global map of Names to unslurped decls
258
259         iInsts :: IfaceInsts,
260                 -- The as-yet un-slurped instance decls; this bag is depleted when we
261                 -- slurp an instance decl so that we don't slurp the same one twice.
262                 -- Each is 'gated' by the names that must be available before
263                 -- this instance decl is needed.
264
265         iRules :: IfaceRules,
266                 -- Similar to instance decls, only for rules
267
268     -- EPHEMERAL FIELDS
269     -- These fields persist during the compilation of a single module only
270         iImpModInfo :: ImportedModuleInfo,
271                         -- Modules that we know something about, because they are mentioned
272                         -- in interface files, BUT which we have not loaded yet.  
273                         -- No module is both in here and in the PIT
274
275         iSlurp :: NameSet,
276                 -- All the names (whether "big" or "small", whether wired-in or not,
277                 -- whether locally defined or not) that have been slurped in so far.
278
279         iVSlurp :: (ModuleSet, NameSet)
280                 -- The Names are all the (a) non-wired-in
281                 --                       (b) "big"
282                 --                       (c) non-locally-defined
283                 --                       (d) home-package
284                 -- names that have been slurped in so far, with their versions.
285                 -- This is used to generate the "usage" information for this module.
286                 -- Subset of the previous field.
287                 -- The module set is the non-home-package modules from which we have
288                 -- slurped at least one name.
289                 -- It's worth keeping separately, because there's no very easy 
290                 -- way to distinguish the "big" names from the "non-big" ones.
291                 -- But this is a decision we might want to revisit.
292     }
293
294 type ImportedModuleInfo = FiniteMap ModuleName (WhetherHasOrphans, IsBootInterface)
295         -- Contains info ONLY about modules that have not yet
296         --- been loaded into the iPIT
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 = emptyFM,
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         let uniqs = prsNS prs
337
338         names_var <- newIORef (uniqs, origNames (prsOrig prs), 
339                                       origIParam (prsOrig prs))
340         errs_var  <- newIORef (emptyBag,emptyBag)
341         iface_var <- newIORef ifaces
342         let rn_down = RnDown { rn_mod = mod,
343                                rn_loc = noSrcLoc, 
344         
345                                rn_dflags = dflags,
346                                rn_hit    = hit,
347                                rn_done   = lookupType hst pte,
348                                              
349                                rn_ns     = names_var, 
350                                rn_errs   = errs_var, 
351                                rn_ifaces = iface_var,
352                              }
353         
354         -- do the business
355         res <- do_rn rn_down ()
356         
357         -- Grab state and record it
358         (warns, errs)                   <- readIORef errs_var
359         new_ifaces                      <- readIORef iface_var
360         (new_NS, new_origN, new_origIP) <- readIORef names_var
361         let new_orig = Orig { origNames = new_origN, origIParam = new_origIP }
362         let new_prs = prs { prsOrig = new_orig,
363                             prsDecls = iDecls new_ifaces,
364                             prsInsts = iInsts new_ifaces,
365                             prsRules = iRules new_ifaces,
366                             prsNS    = new_NS }
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 -> LocalFixityEnv -> RnMode
373          -> RnMS a -> RnM d a
374
375 initRnMS rn_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 = emptyRdrEnv, 
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 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 (us, origNames (prsOrig prs), 
413                                  origIParam (prsOrig prs))
414         ; errs_var <- newIORef (emptyBag,emptyBag)
415
416         ; let rn_down = RnDown { rn_dflags = dflags,
417                                  rn_loc    = generatedSrcLoc, rn_ns = names_var,
418                                  rn_errs   = errs_var, 
419                                  rn_mod    = mod, 
420                                  rn_done   = bogus "rn_done",   
421                                  rn_hit    = bogus "rn_hit",
422                                  rn_ifaces = bogus "rn_ifaces"
423                                }
424         ; let s_down = SDown { rn_mode = InterfaceMode,
425                                -- So that we can refer to PrelBase.True etc
426                                rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
427                                rn_fixenv = emptyLocalFixityEnv }
428
429         ; result <- thing_inside rn_down s_down
430         ; messages <- readIORef errs_var
431
432         ; if bad messages then
433                 do { hPutStr stderr "Urk!  renameDerivedCode found errors or warnings"
434                    ; printErrorsAndWarnings alwaysQualify messages
435                    }
436            else
437                 return()
438
439         ; return result
440         }
441   where
442 #ifdef DEBUG
443     bad messages = errorsFound messages || warningsFound messages
444 #else
445     bad messages = errorsFound messages
446 #endif
447
448 bogus s = panic ("rnameSourceCode: " ++ s)  -- Used for unused record fields
449
450 {-# INLINE thenRn #-}
451 {-# INLINE thenRn_ #-}
452 {-# INLINE returnRn #-}
453 {-# INLINE andRn #-}
454
455 returnRn :: a -> RnM d a
456 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
457 thenRn_  :: RnM d a -> RnM d b -> RnM d b
458 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
459 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
460 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
461 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
462 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
463 sequenceRn  :: [RnM d a] -> RnM d [a]
464 sequenceRn_ :: [RnM d a] -> RnM d ()
465 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
466 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
467 fixRn    :: (a -> RnM d a) -> RnM d a
468
469 returnRn v gdown ldown  = return v
470 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
471 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
472 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
473 andRn combiner m1 m2 gdown ldown
474   = m1 gdown ldown >>= \ res1 ->
475     m2 gdown ldown >>= \ res2 ->
476     return (combiner res1 res2)
477
478 sequenceRn []     = returnRn []
479 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
480                      sequenceRn ms      `thenRn` \ rs ->
481                      returnRn (r:rs)
482
483 sequenceRn_ []     = returnRn ()
484 sequenceRn_ (m:ms) = m `thenRn_` sequenceRn_ ms
485
486 mapRn f []     = returnRn []
487 mapRn f (x:xs)
488   = f x         `thenRn` \ r ->
489     mapRn f xs  `thenRn` \ rs ->
490     returnRn (r:rs)
491
492 mapRn_ f []     = returnRn ()
493 mapRn_ f (x:xs) = 
494     f x         `thenRn_`
495     mapRn_ f xs
496
497 foldlRn k z [] = returnRn z
498 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
499                      foldlRn k z' xs
500
501 mapAndUnzipRn f [] = returnRn ([],[])
502 mapAndUnzipRn f (x:xs)
503   = f x                 `thenRn` \ (r1,  r2)  ->
504     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
505     returnRn (r1:rs1, r2:rs2)
506
507 mapAndUnzip3Rn f [] = returnRn ([],[],[])
508 mapAndUnzip3Rn f (x:xs)
509   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
510     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
511     returnRn (r1:rs1, r2:rs2, r3:rs3)
512
513 mapMaybeRn f []     = returnRn []
514 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
515                       mapMaybeRn f xs   `thenRn` \ rs ->
516                       case maybe_r of
517                         Nothing -> returnRn rs
518                         Just r  -> returnRn (r:rs)
519
520 flatMapRn f []     = returnRn []
521 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
522                      flatMapRn f xs     `thenRn` \ rs ->
523                      returnRn (r ++ rs)
524 \end{code}
525
526
527
528 %************************************************************************
529 %*                                                                      *
530 \subsection{Boring plumbing for common part}
531 %*                                                                      *
532 %************************************************************************
533
534
535 %================
536 \subsubsection{  Errors and warnings}
537 %=====================
538
539 \begin{code}
540 failWithRn :: a -> Message -> RnM d a
541 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
542   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
543     writeIORef errs_var (warns, errs `snocBag` err)             >> 
544     return res
545   where
546     err = addShortErrLocLine loc msg
547
548 warnWithRn :: a -> Message -> RnM d a
549 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
550   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
551     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
552     return res
553   where
554     warn = addShortWarnLocLine loc msg
555
556 addErrRn :: Message -> RnM d ()
557 addErrRn err = failWithRn () err
558
559 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
560 checkRn False err = addErrRn err
561 checkRn True  err = returnRn ()
562
563 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
564 warnCheckRn False err = addWarnRn err
565 warnCheckRn True  err = returnRn ()
566
567 addWarnRn :: Message -> RnM d ()
568 addWarnRn warn = warnWithRn () warn
569
570 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
571 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
572   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
573     return (isEmptyBag errs)
574
575 doptRn :: DynFlag -> RnM d Bool
576 doptRn dflag (RnDown { rn_dflags = dflags}) l_down
577    = return (dopt dflag dflags)
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 (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv)
617 getNameSupplyRn rn_down l_down
618   = readIORef (rn_ns rn_down)
619
620 setNameSupplyRn :: (UniqSupply, OrigNameNameEnv, OrigNameIParamEnv) -> 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 >>= \ (us, cache, ipcache) ->
627    let
628      (us1,us') = splitUniqSupply us
629    in
630    writeIORef names_var (us', cache, ipcache)  >>
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}