[project @ 2000-07-06 16:31:45 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnMonad.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnMonad]{The monad used by the renamer}
5
6 \begin{code}
7 module RnMonad(
8         module RnMonad,
9
10         module RdrName,         -- Re-exports
11         module Name,            -- from these two
12
13         Module,
14         FiniteMap,
15         Bag,
16         RdrNameHsDecl,
17         RdrNameInstDecl,
18         Version,
19         NameSet,
20         OccName,
21         Fixity
22     ) where
23
24 #include "HsVersions.h"
25
26 #if   defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 405
27 import IOExts           ( fixIO )
28 #elif defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 302
29 import PrelIOBase       ( fixIO )       -- Should be in GlaExts
30 #else
31 import IOBase           ( fixIO )
32 #endif
33 import IOExts           ( IORef, newIORef, readIORef, writeIORef, unsafePerformIO )
34         
35 import HsSyn            
36 import RdrHsSyn
37 import RnHsSyn          ( RenamedFixitySig, RenamedDeprecation )
38 import BasicTypes       ( Version, defaultFixity )
39 import SrcLoc           ( noSrcLoc )
40 import ErrUtils         ( addShortErrLocLine, addShortWarnLocLine,
41                           pprBagOfErrors, ErrMsg, WarnMsg, Message
42                         )
43 import RdrName          ( RdrName, dummyRdrVarName, rdrNameOcc,
44                           RdrNameEnv, emptyRdrEnv, extendRdrEnv, 
45                           lookupRdrEnv, addListToRdrEnv, rdrEnvToList, rdrEnvElts
46                         )
47 import Name             ( Name, OccName, NamedThing(..), getSrcLoc,
48                           isLocallyDefinedName, nameModule, nameOccName,
49                           decode, mkLocalName, mkUnboundName,
50                           NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, extendNameEnv,
51                           addToNameEnv_C, plusNameEnv_C, nameEnvElts, 
52                           elemNameEnv, addToNameEnv, addListToNameEnv
53                         )
54 import Module           ( Module, ModuleName, ModuleHiMap, SearchPath, WhereFrom,
55                           mkModuleHiMaps, moduleName, mkSearchPath
56                         )
57 import NameSet          
58 import CmdLineOpts      ( opt_D_dump_rn_trace, opt_HiMap )
59 import PrelInfo         ( builtinNames )
60 import TysWiredIn       ( boolTyCon )
61 import SrcLoc           ( SrcLoc, mkGeneratedSrcLoc )
62 import Unique           ( Unique, getUnique, unboundKey )
63 import FiniteMap        ( FiniteMap, emptyFM, bagToFM, lookupFM, addToFM, addListToFM, 
64                           addListToFM_C, addToFM_C, eltsFM, fmToList
65                         )
66 import Bag              ( Bag, mapBag, emptyBag, isEmptyBag, snocBag )
67 import Maybes           ( mapMaybe )
68 import UniqSet
69 import UniqSupply
70 import Util
71 import Outputable
72
73 infixr 9 `thenRn`, `thenRn_`
74 \end{code}
75
76
77 %************************************************************************
78 %*                                                                      *
79 \subsection{Somewhat magical interface to other monads}
80 %*                                                                      *
81 %************************************************************************
82
83 \begin{code}
84 ioToRnM :: IO r -> RnM d (Either IOError r)
85 ioToRnM io rn_down g_down = (io >>= \ ok -> return (Right ok)) 
86                             `catch` 
87                             (\ err -> return (Left err))
88             
89 traceRn :: SDoc -> RnM d ()
90 traceRn msg | opt_D_dump_rn_trace = putDocRn msg
91             | otherwise           = returnRn ()
92
93 putDocRn :: SDoc -> RnM d ()
94 putDocRn msg = ioToRnM (printErrs msg)  `thenRn_`
95                returnRn ()
96 \end{code}
97
98
99 %************************************************************************
100 %*                                                                      *
101 \subsection{Data types}
102 %*                                                                      *
103 %************************************************************************
104
105 %===================================================
106 \subsubsection{         MONAD TYPES}
107 %===================================================
108
109 \begin{code}
110 type RnM d r = RnDown -> d -> IO r
111 type RnMS r  = RnM SDown r              -- Renaming source
112 type RnMG r  = RnM ()    r              -- Getting global names etc
113
114         -- Common part
115 data RnDown = RnDown {
116                   rn_mod     :: ModuleName,
117                   rn_loc     :: SrcLoc,
118                   rn_ns      :: IORef RnNameSupply,
119                   rn_errs    :: IORef (Bag WarnMsg, Bag ErrMsg),
120                   rn_ifaces  :: IORef Ifaces,
121                   rn_hi_maps :: (SearchPath,    -- For error messages
122                                  ModuleHiMap,   -- for .hi files
123                                  ModuleHiMap)   -- for .hi-boot files
124                 }
125
126         -- For renaming source code
127 data SDown = SDown {
128                   rn_mode :: RnMode,
129
130                   rn_genv :: GlobalRdrEnv,
131                         --   Global envt; the fixity component gets extended
132                         --   with local fixity decls
133
134                   rn_lenv :: LocalRdrEnv,       -- Local name envt
135                         --   Does *not* include global name envt; may shadow it
136                         --   Includes both ordinary variables and type variables;
137                         --   they are kept distinct because tyvar have a different
138                         --   occurrence contructor (Name.TvOcc)
139                         -- We still need the unsullied global name env so that
140                         --   we can look up record field names
141
142                   rn_fixenv :: FixityEnv        -- Local fixities
143                         -- The global fixities are held in the
144                         -- rn_ifaces field.  Why?  See the comments
145                         -- with RnIfaces.lookupFixity
146                 }
147
148 data RnMode     = SourceMode                    -- Renaming source code
149                 | InterfaceMode                 -- Renaming interface declarations.  
150 \end{code}
151
152 %===================================================
153 \subsubsection{         ENVIRONMENTS}
154 %===================================================
155
156 \begin{code}
157 --------------------------------
158 type GlobalRdrEnv = RdrNameEnv [Name]   -- The list is because there may be name clashes
159                                         -- These only get reported on lookup,
160                                         -- not on construction
161 type LocalRdrEnv  = RdrNameEnv Name
162
163 --------------------------------
164 type FixityEnv = NameEnv RenamedFixitySig
165         -- We keep the whole fixity sig so that we
166         -- can report line-number info when there is a duplicate
167         -- fixity declaration
168
169 lookupFixity :: FixityEnv -> Name -> Fixity
170 lookupFixity env name
171   = case lookupNameEnv env name of 
172         Just (FixitySig _ fix _) -> fix
173         Nothing                  -> defaultFixity
174
175 --------------------------------
176 type DeprecationEnv = NameEnv DeprecTxt
177 \end{code}
178
179 \begin{code}
180 --------------------------------
181 type RnNameSupply
182  = ( UniqSupply
183
184    , FiniteMap String Int
185         -- This is used as a name supply for dictionary functions
186         -- From the inst decl we derive a string, usually by glomming together
187         -- the class and tycon name -- but it doesn't matter exactly how;
188         -- this map then gives a unique int for each inst decl with that
189         -- string.  (In Haskell 98 there can only be one,
190         -- but not so in more extended versions; also class CC type T
191         -- and class C type TT might both give the string CCT
192         --      
193         -- We could just use one Int for all the instance decls, but this
194         -- way the uniques change less when you add an instance decl,   
195         -- hence less recompilation
196
197    , FiniteMap (ModuleName, OccName) Name
198         -- Ensures that one (module,occname) pair gets one unique
199    , FiniteMap OccName Name
200         -- Ensures that one implicit parameter name gets one unique
201    )
202
203
204 --------------------------------
205 type Avails       = [AvailInfo]
206
207 type ExportAvails = (FiniteMap ModuleName Avails,
208         -- Used to figure out "module M" export specifiers
209         -- Includes avails only from *unqualified* imports
210         -- (see 1.4 Report Section 5.1.1)
211
212                      AvailEnv)  -- Used to figure out all other export specifiers.
213                         
214
215 data GenAvailInfo name  = Avail name     -- An ordinary identifier
216                         | AvailTC name   -- The name of the type or class
217                                   [name] -- The available pieces of type/class.
218                                          -- NB: If the type or class is itself
219                                          -- to be in scope, it must be in this list.
220                                          -- Thus, typically: AvailTC Eq [Eq, ==, /=]
221                         deriving( Eq )
222                         -- Equality used when deciding if the interface has changed
223
224 type AvailEnv     = NameEnv AvailInfo   -- Maps a Name to the AvailInfo that contains it
225 type AvailInfo    = GenAvailInfo Name
226 type RdrAvailInfo = GenAvailInfo OccName
227 \end{code}
228
229 %===================================================
230 \subsubsection{         INTERFACE FILE STUFF}
231 %===================================================
232
233 \begin{code}
234 type ExportItem          = (ModuleName, [RdrAvailInfo])
235
236 type ImportVersion name  = (ModuleName, WhetherHasOrphans, IsBootInterface, WhatsImported name)
237
238 type ModVersionInfo     = (Version,             -- Version of the whole module
239                            Version,             -- Version number for all fixity decls together
240                            Version)             -- ...ditto all rules together
241
242 type WhetherHasOrphans   = Bool
243         -- An "orphan" is 
244         --      * an instance decl in a module other than the defn module for 
245         --              one of the tycons or classes in the instance head
246         --      * a transformation rule in a module other than the one defining
247         --              the function in the head of the rule.
248
249 type IsBootInterface     = Bool
250
251 data WhatsImported name  = NothingAtAll                         -- The module is below us in the
252                                                                 -- hierarchy, but we import nothing
253
254                          | Everything Version                   -- The module version
255
256                          | Specifically Version                 -- Module version
257                                         Version                 -- Fixity version
258                                         Version                 -- Rules version
259                                         [(name,Version)]        -- List guaranteed non-empty
260                          deriving( Eq )
261         -- 'Specifically' doesn't let you say "I imported f but none of the fixities in
262         -- the module. If you use anything in the module you get its fixity and rule version
263         -- So if the fixities or rules change, you'll recompile, even if you don't use either.
264         -- This is easy to implement, and it's safer: you might not have used the rules last
265         -- time round, but if someone has added a new rule you might need it this time
266
267         -- 'Everything' means there was a "module M" in 
268         -- this module's export list, so we just have to go by M's version,
269         -- not the list of (name,version) pairs
270
271 data ParsedIface
272   = ParsedIface {
273       pi_mod       :: Module,                           -- Complete with package info
274       pi_vers      :: Version,                          -- Module version number
275       pi_orphan    :: WhetherHasOrphans,                -- Whether this module has orphans
276       pi_usages    :: [ImportVersion OccName],          -- Usages
277       pi_exports   :: [ExportItem],                     -- Exports
278       pi_insts     :: [RdrNameInstDecl],                -- Local instance declarations
279       pi_decls     :: [(Version, RdrNameHsDecl)],       -- Local definitions
280       pi_fixity    :: (Version, [RdrNameFixitySig]),    -- Local fixity declarations, with their version
281       pi_rules     :: (Version, [RdrNameRuleDecl]),     -- Rules, with their version
282       pi_deprecs   :: [RdrNameDeprecation]              -- Deprecations
283     }
284
285
286 type RdrNamePragma = ()                         -- Fudge for now
287 -------------------
288
289 data Ifaces = Ifaces {
290                 iImpModInfo :: ImportedModuleInfo,
291                                 -- Modules this one depends on: that is, the union 
292                                 -- of the modules its *direct* imports depend on.
293                                 -- NB: The direct imports have .hi files that enumerate *all* the
294                                 -- dependencies (direct or not) of the imported module.
295
296                 iDecls :: DeclsMap,     -- A single, global map of Names to decls
297
298                 iDeferred :: NameSet,   -- data (not newtype) TyCons that have been slurped, 
299                                         -- but none of their constructors have.
300                                         -- If this is still the case right at the end
301                                         -- we can get away with importing them abstractly
302
303                 iFixes :: FixityEnv,    
304                                 -- A single, global map of Names to fixities
305                                 -- See comments with RnIfaces.lookupFixity
306
307                 iSlurp :: NameSet,
308                 -- All the names (whether "big" or "small", whether wired-in or not,
309                 -- whether locally defined or not) that have been slurped in so far.
310
311                 iVSlurp :: [(Name,Version)],
312                 -- All the (a) non-wired-in (b) "big" (c) non-locally-defined 
313                 -- names that have been slurped in so far, with their versions.
314                 -- This is used to generate the "usage" information for this module.
315                 -- Subset of the previous field.
316
317                 iInsts :: IfaceInsts,
318                 -- The as-yet un-slurped instance decls; this bag is depleted when we
319                 -- slurp an instance decl so that we don't slurp the same one twice.
320                 -- Each is 'gated' by the names that must be available before
321                 -- this instance decl is needed.
322
323                 iRules :: IfaceRules,
324                 -- Similar to instance decls, only for rules
325
326                 iDeprecs :: DeprecationEnv
327         }
328
329 type IfaceInsts = Bag GatedDecl
330 type IfaceRules = Bag GatedDecl
331
332 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
333
334 type ImportedModuleInfo 
335      = FiniteMap ModuleName (WhetherHasOrphans, IsBootInterface, 
336                              Maybe (Module, Version, Version, Version, WhereFrom, Avails))
337                                 -- The three Versions are module version, fixity version, rules version
338
339                 -- Suppose the domain element is module 'A'
340                 --
341                 -- The first Bool is True if A contains 
342                 -- 'orphan' rules or instance decls
343
344                 -- The second Bool is true if the interface file actually
345                 -- read was an .hi-boot file
346
347                 -- Nothing => A's interface not yet read, but this module has
348                 --            imported a module, B, that itself depends on A
349                 --
350                 -- Just xx => A's interface has been read.  The Module in 
351                 --              the Just has the correct Dll flag
352
353                 -- This set is used to decide whether to look for
354                 -- A.hi or A.hi-boot when importing A.f.
355                 -- Basically, we look for A.hi if A is in the map, and A.hi-boot
356                 -- otherwise
357
358 type DeclsMap = NameEnv (Version, AvailInfo, Bool, (Module, RdrNameHsDecl))
359                 -- A DeclsMap contains a binding for each Name in the declaration
360                 -- including the constructors of a type decl etc.
361                 -- The Bool is True just for the 'main' Name.
362 \end{code}
363
364
365 %************************************************************************
366 %*                                                                      *
367 \subsection{Main monad code}
368 %*                                                                      *
369 %************************************************************************
370
371 \begin{code}
372 initRn :: ModuleName -> UniqSupply -> SearchPath -> SrcLoc
373        -> RnMG r
374        -> IO (r, Bag ErrMsg, Bag WarnMsg)
375
376 initRn mod us dirs loc do_rn = do
377   himaps    <- mkModuleHiMaps dirs
378   names_var <- newIORef (us, emptyFM, builtins, emptyFM)
379   errs_var  <- newIORef (emptyBag,emptyBag)
380   iface_var <- newIORef emptyIfaces 
381   let
382         rn_down = RnDown { rn_loc = loc, rn_ns = names_var, 
383                            rn_errs = errs_var, 
384                            rn_hi_maps = himaps, 
385                            rn_ifaces = iface_var,
386                            rn_mod = mod }
387
388         -- do the business
389   res <- do_rn rn_down ()
390
391         -- grab errors and return
392   (warns, errs) <- readIORef errs_var
393
394   return (res, errs, warns)
395
396
397 initRnMS :: GlobalRdrEnv -> FixityEnv -> RnMode -> RnMS r -> RnM d r
398 initRnMS rn_env fixity_env mode thing_inside rn_down g_down
399   = let
400         s_down = SDown { rn_genv = rn_env, rn_lenv = emptyRdrEnv, 
401                          rn_fixenv = fixity_env, rn_mode = mode }
402     in
403     thing_inside rn_down s_down
404
405 initIfaceRnMS :: Module -> RnMS r -> RnM d r
406 initIfaceRnMS mod thing_inside 
407   = initRnMS emptyRdrEnv emptyNameEnv InterfaceMode $
408     setModuleRn (moduleName mod) thing_inside
409
410 emptyIfaces :: Ifaces
411 emptyIfaces = Ifaces { iImpModInfo = emptyFM,
412                        iDecls = emptyNameEnv,
413                        iDeferred = emptyNameSet,
414                        iFixes = emptyNameEnv,
415                        iSlurp = unitNameSet (mkUnboundName dummyRdrVarName),
416                         -- Pretend that the dummy unbound name has already been
417                         -- slurped.  This is what's returned for an out-of-scope name,
418                         -- and we don't want thereby to try to suck it in!
419                        iVSlurp = [],
420                        iInsts = emptyBag,
421                        iRules = emptyBag,
422                        iDeprecs = emptyNameEnv
423               }
424
425 builtins :: FiniteMap (ModuleName,OccName) Name
426 builtins = 
427    bagToFM (
428    mapBag (\ name ->  ((moduleName (nameModule name), nameOccName name), name))
429           builtinNames)
430 \end{code}
431
432 @renameSourceCode@ is used to rename stuff ``out-of-line'';
433 that is, not as part of the main renamer.
434 Sole examples: derived definitions,
435 which are only generated in the type checker.
436
437 The @RnNameSupply@ includes a @UniqueSupply@, so if you call it more than
438 once you must either split it, or install a fresh unique supply.
439
440 \begin{code}
441 renameSourceCode :: ModuleName
442                  -> RnNameSupply
443                  -> RnMS r
444                  -> r
445
446 renameSourceCode mod_name name_supply m
447   = unsafePerformIO (
448         -- It's not really unsafe!  When renaming source code we
449         -- only do any I/O if we need to read in a fixity declaration;
450         -- and that doesn't happen in pragmas etc
451
452         mkModuleHiMaps (mkSearchPath opt_HiMap) >>= \ himaps ->
453         newIORef name_supply            >>= \ names_var ->
454         newIORef (emptyBag,emptyBag)    >>= \ errs_var ->
455         let
456             rn_down = RnDown { rn_loc = mkGeneratedSrcLoc, rn_ns = names_var,
457                                rn_errs = errs_var, rn_hi_maps = himaps,
458                                rn_mod = mod_name, 
459                                rn_ifaces = panic "rnameSourceCode: rn_ifaces"  -- Not required
460                              }
461             s_down = SDown { rn_mode = InterfaceMode,
462                                -- So that we can refer to PrelBase.True etc
463                              rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
464                              rn_fixenv = emptyNameEnv }
465         in
466         m rn_down s_down                        >>= \ result ->
467         
468         readIORef errs_var                      >>= \ (warns,errs) ->
469
470         (if not (isEmptyBag errs) then
471                 pprTrace "Urk! renameSourceCode found errors" (display errs) 
472 #ifdef DEBUG
473          else if not (isEmptyBag warns) then
474                 pprTrace "Note: renameSourceCode found warnings" (display warns)
475 #endif
476          else
477                 id) $
478
479         return result
480     )
481   where
482     display errs = pprBagOfErrors errs
483
484 {-# INLINE thenRn #-}
485 {-# INLINE thenRn_ #-}
486 {-# INLINE returnRn #-}
487 {-# INLINE andRn #-}
488
489 returnRn :: a -> RnM d a
490 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
491 thenRn_  :: RnM d a -> RnM d b -> RnM d b
492 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
493 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
494 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
495 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
496 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
497 sequenceRn :: [RnM d a] -> RnM d [a]
498 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
499 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
500 fixRn    :: (a -> RnM d a) -> RnM d a
501
502 returnRn v gdown ldown  = return v
503 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
504 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
505 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
506 andRn combiner m1 m2 gdown ldown
507   = m1 gdown ldown >>= \ res1 ->
508     m2 gdown ldown >>= \ res2 ->
509     return (combiner res1 res2)
510
511 sequenceRn []     = returnRn []
512 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
513                      sequenceRn ms      `thenRn` \ rs ->
514                      returnRn (r:rs)
515
516 mapRn f []     = returnRn []
517 mapRn f (x:xs)
518   = f x         `thenRn` \ r ->
519     mapRn f xs  `thenRn` \ rs ->
520     returnRn (r:rs)
521
522 mapRn_ f []     = returnRn ()
523 mapRn_ f (x:xs) = 
524     f x         `thenRn_`
525     mapRn_ f xs
526
527 foldlRn k z [] = returnRn z
528 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
529                      foldlRn k z' xs
530
531 mapAndUnzipRn f [] = returnRn ([],[])
532 mapAndUnzipRn f (x:xs)
533   = f x                 `thenRn` \ (r1,  r2)  ->
534     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
535     returnRn (r1:rs1, r2:rs2)
536
537 mapAndUnzip3Rn f [] = returnRn ([],[],[])
538 mapAndUnzip3Rn f (x:xs)
539   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
540     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
541     returnRn (r1:rs1, r2:rs2, r3:rs3)
542
543 mapMaybeRn f []     = returnRn []
544 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
545                       mapMaybeRn f xs   `thenRn` \ rs ->
546                       case maybe_r of
547                         Nothing -> returnRn rs
548                         Just r  -> returnRn (r:rs)
549
550 flatMapRn f []     = returnRn []
551 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
552                      flatMapRn f xs     `thenRn` \ rs ->
553                      returnRn (r ++ rs)
554 \end{code}
555
556
557
558 %************************************************************************
559 %*                                                                      *
560 \subsection{Boring plumbing for common part}
561 %*                                                                      *
562 %************************************************************************
563
564
565 %================
566 \subsubsection{  Errors and warnings}
567 %=====================
568
569 \begin{code}
570 failWithRn :: a -> Message -> RnM d a
571 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
572   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
573     writeIORef errs_var (warns, errs `snocBag` err)             >> 
574     return res
575   where
576     err = addShortErrLocLine loc msg
577
578 warnWithRn :: a -> Message -> RnM d a
579 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
580   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
581     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
582     return res
583   where
584     warn = addShortWarnLocLine loc msg
585
586 addErrRn :: Message -> RnM d ()
587 addErrRn err = failWithRn () err
588
589 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
590 checkRn False err = addErrRn err
591 checkRn True  err = returnRn ()
592
593 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
594 warnCheckRn False err = addWarnRn err
595 warnCheckRn True  err = returnRn ()
596
597 addWarnRn :: Message -> RnM d ()
598 addWarnRn warn = warnWithRn () warn
599
600 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
601 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
602   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
603     return (isEmptyBag errs)
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{  Name supply}
623 %=====================
624
625 \begin{code}
626 getNameSupplyRn :: RnM d RnNameSupply
627 getNameSupplyRn rn_down l_down
628   = readIORef (rn_ns rn_down)
629
630 setNameSupplyRn :: RnNameSupply -> RnM d ()
631 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
632   = writeIORef names_var names'
633
634 -- See comments with RnNameSupply above.
635 newInstUniq :: String -> RnM d Int
636 newInstUniq key (RnDown {rn_ns = names_var}) l_down
637   = readIORef names_var                         >>= \ (us, mapInst, cache, ipcache) ->
638     let
639         uniq = case lookupFM mapInst key of
640                    Just x  -> x+1
641                    Nothing -> 0
642         mapInst' = addToFM mapInst key uniq
643     in
644     writeIORef names_var (us, mapInst', cache, ipcache) >>
645     return uniq
646
647 getUniqRn :: RnM d Unique
648 getUniqRn (RnDown {rn_ns = names_var}) l_down
649  = readIORef names_var >>= \ (us, mapInst, cache, ipcache) ->
650    let
651      (us1,us') = splitUniqSupply us
652    in
653    writeIORef names_var (us', mapInst, cache, ipcache)  >>
654    return (uniqFromSupply us1)
655 \end{code}
656
657 %================
658 \subsubsection{  Module}
659 %=====================
660
661 \begin{code}
662 getModuleRn :: RnM d ModuleName
663 getModuleRn (RnDown {rn_mod = mod_name}) l_down
664   = return mod_name
665
666 setModuleRn :: ModuleName -> RnM d a -> RnM d a
667 setModuleRn new_mod enclosed_thing rn_down l_down
668   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
669 \end{code}
670
671
672 %************************************************************************
673 %*                                                                      *
674 \subsection{Plumbing for rename-source part}
675 %*                                                                      *
676 %************************************************************************
677
678 %================
679 \subsubsection{  RnEnv}
680 %=====================
681
682 \begin{code}
683 getNameEnvs :: RnMS (GlobalRdrEnv, LocalRdrEnv)
684 getNameEnvs rn_down (SDown {rn_genv = global_env, rn_lenv = local_env})
685   = return (global_env, local_env)
686
687 getLocalNameEnv :: RnMS LocalRdrEnv
688 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
689   = return local_env
690
691 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
692 setLocalNameEnv local_env' m rn_down l_down
693   = m rn_down (l_down {rn_lenv = local_env'})
694
695 getFixityEnv :: RnMS FixityEnv
696 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
697   = return fixity_env
698
699 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
700 extendFixityEnv fixes enclosed_scope
701                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
702   = let
703         new_fixity_env = extendNameEnv fixity_env fixes
704     in
705     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
706 \end{code}
707
708 %================
709 \subsubsection{  Mode}
710 %=====================
711
712 \begin{code}
713 getModeRn :: RnMS RnMode
714 getModeRn rn_down (SDown {rn_mode = mode})
715   = return mode
716
717 setModeRn :: RnMode -> RnMS a -> RnMS a
718 setModeRn new_mode thing_inside rn_down l_down
719   = thing_inside rn_down (l_down {rn_mode = new_mode})
720 \end{code}
721
722
723 %************************************************************************
724 %*                                                                      *
725 \subsection{Plumbing for rename-globals part}
726 %*                                                                      *
727 %************************************************************************
728
729 \begin{code}
730 getIfacesRn :: RnM d Ifaces
731 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
732   = readIORef iface_var
733
734 setIfacesRn :: Ifaces -> RnM d ()
735 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
736   = writeIORef iface_var ifaces
737
738 getHiMaps :: RnM d (SearchPath, ModuleHiMap, ModuleHiMap)
739 getHiMaps (RnDown {rn_hi_maps = himaps}) _ 
740   = return himaps
741 \end{code}
742 \end{code}