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