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