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