495b50f9610bdc6e7a938631ff79b70f8cbfffdb
[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 \begin{code}
206 type ExportAvails = (FiniteMap ModuleName Avails,
207         -- Used to figure out "module M" export specifiers
208         -- Includes avails only from *unqualified* imports
209         -- (see 1.4 Report Section 5.1.1)
210
211                      AvailEnv)  -- Used to figure out all other export specifiers.
212 \end{code}
213
214 %===================================================
215 \subsubsection{         INTERFACE FILE STUFF}
216 %===================================================
217
218 \begin{code}
219 type ExportItem   = (ModuleName, [RdrAvailInfo])
220 type IfaceDeprecs = Maybe (Either DeprecTxt [(RdrName,DeprecTxt)])
221         -- Nothing        => NoDeprecs
222         -- Just (Left t)  => DeprecAll
223         -- Just (Right p) => DeprecSome
224
225 data ParsedIface
226   = ParsedIface {
227       pi_mod       :: Module,                           -- Complete with package info
228       pi_vers      :: Version,                          -- Module version number
229       pi_orphan    :: WhetherHasOrphans,                -- Whether this module has orphans
230       pi_usages    :: [ImportVersion OccName],          -- Usages
231       pi_exports   :: (Version, [ExportItem]),          -- Exports
232       pi_decls     :: [(Version, RdrNameTyClDecl)],     -- Local definitions
233       pi_fixity    :: [RdrNameFixitySig],               -- Local fixity declarations,
234       pi_insts     :: [RdrNameInstDecl],                -- Local instance declarations
235       pi_rules     :: (Version, [RdrNameRuleDecl]),     -- Rules, with their version
236       pi_deprecs   :: IfaceDeprecs                      -- Deprecations
237     }
238 \end{code}
239
240 %************************************************************************
241 %*                                                                      *
242 \subsection{The renamer state}
243 %*                                                                      *
244 %************************************************************************
245
246 \begin{code}
247 data Ifaces = Ifaces {
248     -- PERSISTENT FIELDS
249         iPIT :: PackageIfaceTable,
250                 -- The ModuleIFaces for modules in other packages
251                 -- whose interfaces we have opened
252                 -- The declarations in these interface files are held in
253                 -- iDecls, iInsts, iRules (below), not in the mi_decls fields
254                 -- of the iPIT.  What _is_ in the iPIT is:
255                 --      * The Module 
256                 --      * Version info
257                 --      * Its exports
258                 --      * Fixities
259                 --      * Deprecations
260                 -- The iPIT field is initialised from the compiler's persistent
261                 -- package symbol table, and the renamer incrementally adds
262                 -- to it.
263
264         iImpModInfo :: ImportedModuleInfo,
265                         -- Modules that we know something about, because they are mentioned
266                         -- in interface files, BUT which we have not loaded yet.  
267                         -- No module is both in here and in the PIT
268
269         iDecls :: DeclsMap,     
270                 -- A single, global map of Names to unslurped decls
271
272         iInsts :: IfaceInsts,
273                 -- The as-yet un-slurped instance decls; this bag is depleted when we
274                 -- slurp an instance decl so that we don't slurp the same one twice.
275                 -- Each is 'gated' by the names that must be available before
276                 -- this instance decl is needed.
277
278         iRules :: IfaceRules,
279                 -- Similar to instance decls, only for rules
280
281     -- EPHEMERAL FIELDS
282     -- These fields persist during the compilation of a single module only
283         iSlurp :: NameSet,
284                 -- All the names (whether "big" or "small", whether wired-in or not,
285                 -- whether locally defined or not) that have been slurped in so far.
286                 --
287                 -- It's used for two things:
288                 --      a) To record what we've already slurped, so
289                 --         we can no-op if we try to slurp it again
290                 --      b) As the 'gates' for importing rules.  We import a rule
291                 --         if all its LHS free vars have been slurped
292
293         iVSlurp :: (ModuleSet, NameSet)
294                 -- The Names are all the (a) non-wired-in
295                 --                       (b) "big"
296                 --                       (c) non-locally-defined
297                 --                       (d) home-package
298                 -- names that have been slurped in so far, with their versions.
299                 -- This is used to generate the "usage" information for this module.
300                 -- Subset of the previous field.
301                 --
302                 -- The module set is the non-home-package modules from which we have
303                 -- slurped at least one name.
304                 -- It's worth keeping separately, because there's no very easy 
305                 -- way to distinguish the "big" names from the "non-big" ones.
306                 -- But this is a decision we might want to revisit.
307     }
308 \end{code}
309
310
311 %************************************************************************
312 %*                                                                      *
313 \subsection{Main monad code}
314 %*                                                                      *
315 %************************************************************************
316
317 \begin{code}
318 runRn dflags hit hst pcs mod do_rn
319   = do { (pcs, msgs, r) <- initRn dflags hit hst pcs mod do_rn ;
320          printErrorsAndWarnings alwaysQualify msgs ;
321          return (pcs, errorsFound msgs, r)
322     }
323
324 initRn :: DynFlags
325        -> HomeIfaceTable -> HomeSymbolTable
326        -> PersistentCompilerState
327        -> Module
328        -> RnMG t
329        -> IO (PersistentCompilerState, Messages, t)     
330
331 initRn dflags hit hst pcs mod do_rn
332   = do 
333         let prs = pcs_PRS pcs
334         let pte = pcs_PTE pcs
335         let ifaces = Ifaces { iPIT   = pcs_PIT pcs,
336                               iDecls = prsDecls prs,
337                               iInsts = prsInsts prs,
338                               iRules = prsRules prs,
339
340                               iImpModInfo = prsImpMods prs,
341                               iSlurp      = unitNameSet (mkUnboundName dummyRdrVarName),
342                                 -- Pretend that the dummy unbound name has already been
343                                 -- slurped.  This is what's returned for an out-of-scope name,
344                                 -- and we don't want thereby to try to suck it in!
345                               iVSlurp = (emptyModuleSet, emptyNameSet)
346                       }
347         names_var <- newIORef (prsOrig prs)
348         errs_var  <- newIORef (emptyBag,emptyBag)
349         iface_var <- newIORef ifaces
350         let rn_down = RnDown { rn_mod = mod,
351                                rn_loc = noSrcLoc, 
352         
353                                rn_dflags = dflags,
354                                rn_hit    = hit,
355                                rn_done   = lookupType hst pte,
356                                              
357                                rn_ns     = names_var, 
358                                rn_errs   = errs_var, 
359                                rn_ifaces = iface_var,
360                              }
361         
362         -- do the business
363         res <- do_rn rn_down ()
364         
365         -- Grab state and record it
366         (warns, errs)   <- readIORef errs_var
367         new_ifaces      <- readIORef iface_var
368         new_orig        <- readIORef names_var
369         let new_prs = prs { prsOrig    = new_orig,
370                             prsImpMods = iImpModInfo new_ifaces,
371                             prsDecls   = iDecls new_ifaces,
372                             prsInsts   = iInsts new_ifaces,
373                             prsRules   = iRules new_ifaces }
374         let new_pcs = pcs { pcs_PIT = iPIT new_ifaces, 
375                             pcs_PRS = new_prs }
376         
377         return (new_pcs, (warns, errs), res)
378
379 initRnMS :: GlobalRdrEnv -> AvailEnv -> LocalRdrEnv -> LocalFixityEnv -> RnMode
380          -> RnMS a -> RnM d a
381
382 initRnMS rn_env avails local_env fixity_env mode thing_inside rn_down g_down
383         -- The fixity_env appears in both the rn_fixenv field
384         -- and in the HIT.  See comments with RnHiFiles.lookupFixityRn
385   = let
386         s_down = SDown { rn_genv = rn_env, rn_avails = avails, 
387                          rn_lenv = local_env, rn_fixenv = fixity_env, 
388                          rn_mode = mode }
389     in
390     thing_inside rn_down s_down
391
392 initIfaceRnMS :: Module -> RnMS r -> RnM d r
393 initIfaceRnMS mod thing_inside 
394   = initRnMS emptyRdrEnv emptyAvailEnv emptyRdrEnv 
395              emptyLocalFixityEnv InterfaceMode
396              (setModuleRn mod thing_inside)
397 \end{code}
398
399 @renameDerivedCode@ is used to rename stuff ``out-of-line'';
400 that is, not as part of the main renamer.
401 Sole examples: derived definitions,
402 which are only generated in the type checker.
403
404 The @NameSupply@ includes a @UniqueSupply@, so if you call it more than
405 once you must either split it, or install a fresh unique supply.
406
407 \begin{code}
408 renameDerivedCode :: DynFlags 
409                   -> Module
410                   -> PersistentRenamerState
411                   -> RnMS r
412                   -> r
413
414 renameDerivedCode dflags mod prs thing_inside
415   = unsafePerformIO $
416         -- It's not really unsafe!  When renaming source code we
417         -- only do any I/O if we need to read in a fixity declaration;
418         -- and that doesn't happen in pragmas etc
419
420     do  { us <- mkSplitUniqSupply 'r'
421         ; names_var <- newIORef ((prsOrig prs) { nsUniqs = us })
422         ; errs_var <- newIORef (emptyBag,emptyBag)
423
424         ; let rn_down = RnDown { rn_dflags = dflags,
425                                  rn_loc    = generatedSrcLoc, rn_ns = names_var,
426                                  rn_errs   = errs_var, 
427                                  rn_mod    = mod, 
428                                  rn_done   = bogus "rn_done",   
429                                  rn_hit    = bogus "rn_hit",
430                                  rn_ifaces = bogus "rn_ifaces"
431                                }
432         ; let s_down = SDown { rn_mode = InterfaceMode, 
433                                -- So that we can refer to PrelBase.True etc
434                                rn_avails = emptyAvailEnv,
435                                rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
436                                rn_fixenv = emptyLocalFixityEnv }
437
438         ; result <- thing_inside rn_down s_down
439         ; messages <- readIORef errs_var
440
441         ; if bad messages then
442                 do { hPutStr stderr "Urk!  renameDerivedCode found errors or warnings"
443                    ; printErrorsAndWarnings alwaysQualify messages
444                    }
445            else
446                 return()
447
448         ; return result
449         }
450   where
451 #ifdef DEBUG
452     bad messages = errorsFound messages || warningsFound messages
453 #else
454     bad messages = errorsFound messages
455 #endif
456
457 bogus s = panic ("rnameSourceCode: " ++ s)  -- Used for unused record fields
458
459 {-# INLINE thenRn #-}
460 {-# INLINE thenRn_ #-}
461 {-# INLINE returnRn #-}
462 {-# INLINE andRn #-}
463
464 returnRn :: a -> RnM d a
465 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
466 thenRn_  :: RnM d a -> RnM d b -> RnM d b
467 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
468 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
469 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
470 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
471 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
472 sequenceRn  :: [RnM d a] -> RnM d [a]
473 sequenceRn_ :: [RnM d a] -> RnM d ()
474 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
475 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
476 fixRn    :: (a -> RnM d a) -> RnM d a
477
478 returnRn v gdown ldown  = return v
479 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
480 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
481 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
482 andRn combiner m1 m2 gdown ldown
483   = m1 gdown ldown >>= \ res1 ->
484     m2 gdown ldown >>= \ res2 ->
485     return (combiner res1 res2)
486
487 sequenceRn []     = returnRn []
488 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
489                      sequenceRn ms      `thenRn` \ rs ->
490                      returnRn (r:rs)
491
492 sequenceRn_ []     = returnRn ()
493 sequenceRn_ (m:ms) = m `thenRn_` sequenceRn_ ms
494
495 mapRn f []     = returnRn []
496 mapRn f (x:xs)
497   = f x         `thenRn` \ r ->
498     mapRn f xs  `thenRn` \ rs ->
499     returnRn (r:rs)
500
501 mapRn_ f []     = returnRn ()
502 mapRn_ f (x:xs) = 
503     f x         `thenRn_`
504     mapRn_ f xs
505
506 foldlRn k z [] = returnRn z
507 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
508                      foldlRn k z' xs
509
510 mapAndUnzipRn f [] = returnRn ([],[])
511 mapAndUnzipRn f (x:xs)
512   = f x                 `thenRn` \ (r1,  r2)  ->
513     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
514     returnRn (r1:rs1, r2:rs2)
515
516 mapAndUnzip3Rn f [] = returnRn ([],[],[])
517 mapAndUnzip3Rn f (x:xs)
518   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
519     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
520     returnRn (r1:rs1, r2:rs2, r3:rs3)
521
522 mapMaybeRn f []     = returnRn []
523 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
524                       mapMaybeRn f xs   `thenRn` \ rs ->
525                       case maybe_r of
526                         Nothing -> returnRn rs
527                         Just r  -> returnRn (r:rs)
528
529 flatMapRn f []     = returnRn []
530 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
531                      flatMapRn f xs     `thenRn` \ rs ->
532                      returnRn (r ++ rs)
533 \end{code}
534
535
536
537 %************************************************************************
538 %*                                                                      *
539 \subsection{Boring plumbing for common part}
540 %*                                                                      *
541 %************************************************************************
542
543
544 %================
545 \subsubsection{  Errors and warnings}
546 %=====================
547
548 \begin{code}
549 failWithRn :: a -> Message -> RnM d a
550 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
551   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
552     writeIORef errs_var (warns, errs `snocBag` err)             >> 
553     return res
554   where
555     err = addShortErrLocLine loc msg
556
557 warnWithRn :: a -> Message -> RnM d a
558 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
559   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
560     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
561     return res
562   where
563     warn = addShortWarnLocLine loc msg
564
565 tryRn :: RnM d a -> RnM d (Either Messages a)
566 tryRn try_this down@(RnDown {rn_errs = errs_var}) l_down
567   = do current_msgs <- readIORef errs_var
568        writeIORef errs_var (emptyBag,emptyBag)
569        a <- try_this down l_down
570        (warns, errs) <- readIORef errs_var
571        writeIORef errs_var current_msgs
572        if (isEmptyBag errs)
573           then return (Right a)
574           else return (Left (warns,errs))
575
576 setErrsRn :: Messages -> RnM d ()
577 setErrsRn msgs down@(RnDown {rn_errs = errs_var}) l_down
578   = do writeIORef errs_var msgs; return ()
579
580 addErrRn :: Message -> RnM d ()
581 addErrRn err = failWithRn () err
582
583 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
584 checkRn False err = addErrRn err
585 checkRn True  err = returnRn ()
586
587 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
588 warnCheckRn False err = addWarnRn err
589 warnCheckRn True  err = returnRn ()
590
591 addWarnRn :: Message -> RnM d ()
592 addWarnRn warn = warnWithRn () warn
593
594 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
595 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
596   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
597     return (isEmptyBag errs)
598
599 doptRn :: DynFlag -> RnM d Bool
600 doptRn dflag (RnDown { rn_dflags = dflags}) l_down
601    = return (dopt dflag dflags)
602
603 ifOptRn :: DynFlag -> RnM d a -> RnM d ()
604 ifOptRn dflag thing_inside down@(RnDown { rn_dflags = dflags}) l_down
605   | dopt dflag dflags = thing_inside down l_down >> return ()
606   | otherwise         = return ()
607
608 getDOptsRn :: RnM d DynFlags
609 getDOptsRn (RnDown { rn_dflags = dflags}) l_down
610    = return dflags
611 \end{code}
612
613
614 %================
615 \subsubsection{Source location}
616 %=====================
617
618 \begin{code}
619 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
620 pushSrcLocRn loc' m down l_down
621   = m (down {rn_loc = loc'}) l_down
622
623 getSrcLocRn :: RnM d SrcLoc
624 getSrcLocRn down l_down
625   = return (rn_loc down)
626 \end{code}
627
628 %================
629 \subsubsection{The finder and home symbol table}
630 %=====================
631
632 \begin{code}
633 getHomeIfaceTableRn :: RnM d HomeIfaceTable
634 getHomeIfaceTableRn down l_down = return (rn_hit down)
635
636 getTypeEnvRn :: RnM d (Name -> Maybe TyThing)
637 getTypeEnvRn down l_down = return (rn_done down)
638
639 extendTypeEnvRn :: NameEnv TyThing -> RnM d a -> RnM d a
640 extendTypeEnvRn env inside down l_down
641   = inside down{rn_done=new_rn_done} l_down
642   where new_rn_done = \nm -> lookupNameEnv env nm `seqMaybe` rn_done down nm
643 \end{code}
644
645 %================
646 \subsubsection{Name supply}
647 %=====================
648
649 \begin{code}
650 getNameSupplyRn :: RnM d NameSupply
651 getNameSupplyRn rn_down l_down
652   = readIORef (rn_ns rn_down)
653
654 setNameSupplyRn :: NameSupply -> RnM d ()
655 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
656   = writeIORef names_var names'
657
658 getUniqRn :: RnM d Unique
659 getUniqRn (RnDown {rn_ns = names_var}) l_down
660  = readIORef names_var >>= \ ns ->
661    let
662      (us1,us') = splitUniqSupply (nsUniqs ns)
663    in
664    writeIORef names_var (ns {nsUniqs = us'})    >>
665    return (uniqFromSupply us1)
666 \end{code}
667
668 %================
669 \subsubsection{  Module}
670 %=====================
671
672 \begin{code}
673 getModuleRn :: RnM d Module
674 getModuleRn (RnDown {rn_mod = mod}) l_down
675   = return mod
676
677 setModuleRn :: Module -> RnM d a -> RnM d a
678 setModuleRn new_mod enclosed_thing rn_down l_down
679   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
680 \end{code}
681
682
683 %************************************************************************
684 %*                                                                      *
685 \subsection{Plumbing for rename-source part}
686 %*                                                                      *
687 %************************************************************************
688
689 %================
690 \subsubsection{  RnEnv}
691 %=====================
692
693 \begin{code}
694 getLocalNameEnv :: RnMS LocalRdrEnv
695 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
696   = return local_env
697
698 getGlobalNameEnv :: RnMS GlobalRdrEnv
699 getGlobalNameEnv rn_down (SDown {rn_genv = global_env})
700   = return global_env
701
702 getGlobalAvails :: RnMS AvailEnv
703 getGlobalAvails  rn_down (SDown {rn_avails = avails})
704   = return avails
705
706 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
707 setLocalNameEnv local_env' m rn_down l_down
708   = m rn_down (l_down {rn_lenv = local_env'})
709
710 getFixityEnv :: RnMS LocalFixityEnv
711 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
712   = return fixity_env
713
714 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
715 extendFixityEnv fixes enclosed_scope
716                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
717   = let
718         new_fixity_env = extendNameEnvList fixity_env fixes
719     in
720     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
721 \end{code}
722
723 %================
724 \subsubsection{  Mode}
725 %=====================
726
727 \begin{code}
728 getModeRn :: RnMS RnMode
729 getModeRn rn_down (SDown {rn_mode = mode})
730   = return mode
731
732 setModeRn :: RnMode -> RnMS a -> RnMS a
733 setModeRn new_mode thing_inside rn_down l_down
734   = thing_inside rn_down (l_down {rn_mode = new_mode})
735 \end{code}
736
737
738 %************************************************************************
739 %*                                                                      *
740 \subsection{Plumbing for rename-globals part}
741 %*                                                                      *
742 %************************************************************************
743
744 \begin{code}
745 getIfacesRn :: RnM d Ifaces
746 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
747   = readIORef iface_var
748
749 setIfacesRn :: Ifaces -> RnM d ()
750 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
751   = writeIORef iface_var ifaces
752 \end{code}