[project @ 2001-12-07 17:33:26 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(..), 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 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       :: 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 -> AvailEnv -> LocalRdrEnv -> LocalFixityEnv -> RnMode
372          -> RnMS a -> RnM d a
373
374 initRnMS rn_env avails 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_avails = avails, 
379                          rn_lenv = local_env, rn_fixenv = fixity_env, 
380                          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 emptyAvailEnv emptyRdrEnv 
387              emptyLocalFixityEnv InterfaceMode
388              (setModuleRn mod thing_inside)
389 \end{code}
390
391 @renameDerivedCode@ is used to rename stuff ``out-of-line'';
392 that is, not as part of the main renamer.
393 Sole examples: derived definitions,
394 which are only generated in the type checker.
395
396 The @NameSupply@ includes a @UniqueSupply@, so if you call it more than
397 once you must either split it, or install a fresh unique supply.
398
399 \begin{code}
400 renameDerivedCode :: DynFlags 
401                   -> Module
402                   -> PersistentRenamerState
403                   -> RnMS r
404                   -> r
405
406 renameDerivedCode dflags mod prs thing_inside
407   = unsafePerformIO $
408         -- It's not really unsafe!  When renaming source code we
409         -- only do any I/O if we need to read in a fixity declaration;
410         -- and that doesn't happen in pragmas etc
411
412     do  { us <- mkSplitUniqSupply 'r'
413         ; names_var <- newIORef ((prsOrig prs) { nsUniqs = us })
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_avails = emptyAvailEnv,
427                                rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
428                                rn_fixenv = emptyLocalFixityEnv }
429
430         ; result <- thing_inside rn_down s_down
431         ; messages <- readIORef errs_var
432
433         ; if bad messages then
434                 do { hPutStr stderr "Urk!  renameDerivedCode found errors or warnings"
435                    ; printErrorsAndWarnings alwaysQualify messages
436                    }
437            else
438                 return()
439
440         ; return result
441         }
442   where
443 #ifdef DEBUG
444     bad messages = errorsFound messages || warningsFound messages
445 #else
446     bad messages = errorsFound messages
447 #endif
448
449 bogus s = panic ("rnameSourceCode: " ++ s)  -- Used for unused record fields
450
451 {-# INLINE thenRn #-}
452 {-# INLINE thenRn_ #-}
453 {-# INLINE returnRn #-}
454 {-# INLINE andRn #-}
455
456 returnRn :: a -> RnM d a
457 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
458 thenRn_  :: RnM d a -> RnM d b -> RnM d b
459 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
460 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
461 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
462 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
463 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
464 sequenceRn  :: [RnM d a] -> RnM d [a]
465 sequenceRn_ :: [RnM d a] -> RnM d ()
466 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
467 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
468 fixRn    :: (a -> RnM d a) -> RnM d a
469
470 returnRn v gdown ldown  = return v
471 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
472 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
473 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
474 andRn combiner m1 m2 gdown ldown
475   = m1 gdown ldown >>= \ res1 ->
476     m2 gdown ldown >>= \ res2 ->
477     return (combiner res1 res2)
478
479 sequenceRn []     = returnRn []
480 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
481                      sequenceRn ms      `thenRn` \ rs ->
482                      returnRn (r:rs)
483
484 sequenceRn_ []     = returnRn ()
485 sequenceRn_ (m:ms) = m `thenRn_` sequenceRn_ ms
486
487 mapRn f []     = returnRn []
488 mapRn f (x:xs)
489   = f x         `thenRn` \ r ->
490     mapRn f xs  `thenRn` \ rs ->
491     returnRn (r:rs)
492
493 mapRn_ f []     = returnRn ()
494 mapRn_ f (x:xs) = 
495     f x         `thenRn_`
496     mapRn_ f xs
497
498 foldlRn k z [] = returnRn z
499 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
500                      foldlRn k z' xs
501
502 mapAndUnzipRn f [] = returnRn ([],[])
503 mapAndUnzipRn f (x:xs)
504   = f x                 `thenRn` \ (r1,  r2)  ->
505     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
506     returnRn (r1:rs1, r2:rs2)
507
508 mapAndUnzip3Rn f [] = returnRn ([],[],[])
509 mapAndUnzip3Rn f (x:xs)
510   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
511     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
512     returnRn (r1:rs1, r2:rs2, r3:rs3)
513
514 mapMaybeRn f []     = returnRn []
515 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
516                       mapMaybeRn f xs   `thenRn` \ rs ->
517                       case maybe_r of
518                         Nothing -> returnRn rs
519                         Just r  -> returnRn (r:rs)
520
521 flatMapRn f []     = returnRn []
522 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
523                      flatMapRn f xs     `thenRn` \ rs ->
524                      returnRn (r ++ rs)
525 \end{code}
526
527
528
529 %************************************************************************
530 %*                                                                      *
531 \subsection{Boring plumbing for common part}
532 %*                                                                      *
533 %************************************************************************
534
535
536 %================
537 \subsubsection{  Errors and warnings}
538 %=====================
539
540 \begin{code}
541 failWithRn :: a -> Message -> RnM d a
542 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
543   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
544     writeIORef errs_var (warns, errs `snocBag` err)             >> 
545     return res
546   where
547     err = addShortErrLocLine loc msg
548
549 warnWithRn :: a -> Message -> RnM d a
550 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
551   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
552     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
553     return res
554   where
555     warn = addShortWarnLocLine loc msg
556
557 tryRn :: RnM d a -> RnM d (Either Messages a)
558 tryRn try_this down@(RnDown {rn_errs = errs_var}) l_down
559   = do current_msgs <- readIORef errs_var
560        writeIORef errs_var (emptyBag,emptyBag)
561        a <- try_this down l_down
562        (warns, errs) <- readIORef errs_var
563        writeIORef errs_var current_msgs
564        if (isEmptyBag errs)
565           then return (Right a)
566           else return (Left (warns,errs))
567
568 setErrsRn :: Messages -> RnM d ()
569 setErrsRn msgs down@(RnDown {rn_errs = errs_var}) l_down
570   = do writeIORef errs_var msgs; return ()
571
572 addErrRn :: Message -> RnM d ()
573 addErrRn err = failWithRn () err
574
575 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
576 checkRn False err = addErrRn err
577 checkRn True  err = returnRn ()
578
579 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
580 warnCheckRn False err = addWarnRn err
581 warnCheckRn True  err = returnRn ()
582
583 addWarnRn :: Message -> RnM d ()
584 addWarnRn warn = warnWithRn () warn
585
586 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
587 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
588   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
589     return (isEmptyBag errs)
590
591 doptRn :: DynFlag -> RnM d Bool
592 doptRn dflag (RnDown { rn_dflags = dflags}) l_down
593    = return (dopt dflag dflags)
594
595 ifOptRn :: DynFlag -> RnM d a -> RnM d ()
596 ifOptRn dflag thing_inside down@(RnDown { rn_dflags = dflags}) l_down
597   | dopt dflag dflags = thing_inside down l_down >> return ()
598   | otherwise         = return ()
599
600 getDOptsRn :: RnM d DynFlags
601 getDOptsRn (RnDown { rn_dflags = dflags}) l_down
602    = return dflags
603 \end{code}
604
605
606 %================
607 \subsubsection{Source location}
608 %=====================
609
610 \begin{code}
611 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
612 pushSrcLocRn loc' m down l_down
613   = m (down {rn_loc = loc'}) l_down
614
615 getSrcLocRn :: RnM d SrcLoc
616 getSrcLocRn down l_down
617   = return (rn_loc down)
618 \end{code}
619
620 %================
621 \subsubsection{The finder and home symbol table}
622 %=====================
623
624 \begin{code}
625 getHomeIfaceTableRn :: RnM d HomeIfaceTable
626 getHomeIfaceTableRn down l_down = return (rn_hit down)
627
628 getTypeEnvRn :: RnM d (Name -> Maybe TyThing)
629 getTypeEnvRn down l_down = return (rn_done down)
630
631 extendTypeEnvRn :: NameEnv TyThing -> RnM d a -> RnM d a
632 extendTypeEnvRn env inside down l_down
633   = inside down{rn_done=new_rn_done} l_down
634   where new_rn_done = \nm -> lookupNameEnv env nm `seqMaybe` rn_done down nm
635 \end{code}
636
637 %================
638 \subsubsection{Name supply}
639 %=====================
640
641 \begin{code}
642 getNameSupplyRn :: RnM d NameSupply
643 getNameSupplyRn rn_down l_down
644   = readIORef (rn_ns rn_down)
645
646 setNameSupplyRn :: NameSupply -> RnM d ()
647 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
648   = writeIORef names_var names'
649
650 getUniqRn :: RnM d Unique
651 getUniqRn (RnDown {rn_ns = names_var}) l_down
652  = readIORef names_var >>= \ ns ->
653    let
654      (us1,us') = splitUniqSupply (nsUniqs ns)
655    in
656    writeIORef names_var (ns {nsUniqs = us'})    >>
657    return (uniqFromSupply us1)
658 \end{code}
659
660 %================
661 \subsubsection{  Module}
662 %=====================
663
664 \begin{code}
665 getModuleRn :: RnM d Module
666 getModuleRn (RnDown {rn_mod = mod}) l_down
667   = return mod
668
669 setModuleRn :: Module -> RnM d a -> RnM d a
670 setModuleRn new_mod enclosed_thing rn_down l_down
671   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
672 \end{code}
673
674
675 %************************************************************************
676 %*                                                                      *
677 \subsection{Plumbing for rename-source part}
678 %*                                                                      *
679 %************************************************************************
680
681 %================
682 \subsubsection{  RnEnv}
683 %=====================
684
685 \begin{code}
686 getLocalNameEnv :: RnMS LocalRdrEnv
687 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
688   = return local_env
689
690 getGlobalNameEnv :: RnMS GlobalRdrEnv
691 getGlobalNameEnv rn_down (SDown {rn_genv = global_env})
692   = return global_env
693
694 getGlobalAvails :: RnMS AvailEnv
695 getGlobalAvails  rn_down (SDown {rn_avails = avails})
696   = return avails
697
698 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
699 setLocalNameEnv local_env' m rn_down l_down
700   = m rn_down (l_down {rn_lenv = local_env'})
701
702 getFixityEnv :: RnMS LocalFixityEnv
703 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
704   = return fixity_env
705
706 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
707 extendFixityEnv fixes enclosed_scope
708                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
709   = let
710         new_fixity_env = extendNameEnvList fixity_env fixes
711     in
712     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
713 \end{code}
714
715 %================
716 \subsubsection{  Mode}
717 %=====================
718
719 \begin{code}
720 getModeRn :: RnMS RnMode
721 getModeRn rn_down (SDown {rn_mode = mode})
722   = return mode
723
724 setModeRn :: RnMode -> RnMS a -> RnMS a
725 setModeRn new_mode thing_inside rn_down l_down
726   = thing_inside rn_down (l_down {rn_mode = new_mode})
727 \end{code}
728
729
730 %************************************************************************
731 %*                                                                      *
732 \subsection{Plumbing for rename-globals part}
733 %*                                                                      *
734 %************************************************************************
735
736 \begin{code}
737 getIfacesRn :: RnM d Ifaces
738 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
739   = readIORef iface_var
740
741 setIfacesRn :: Ifaces -> RnM d ()
742 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
743   = writeIORef iface_var ifaces
744 \end{code}