[project @ 2000-05-25 12:41:14 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 :: Bag GatedDecl,
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, except that we track the version number of the
325                 -- rules we import from each module
326                 -- [We keep just one rule-version number for each module]
327                 -- The Bool is True if we import any rules at all from that module
328
329                 iDeprecs :: DeprecationEnv
330         }
331
332 type IfaceRules = Bag GatedDecl
333
334 type GatedDecl = (NameSet, (Module, RdrNameHsDecl))
335
336 type ImportedModuleInfo 
337      = FiniteMap ModuleName (WhetherHasOrphans, IsBootInterface, 
338                              Maybe (Module, Version, Version, Version, WhereFrom, Avails))
339                                 -- The three Versions are module version, fixity version, rules version
340
341                 -- Suppose the domain element is module 'A'
342                 --
343                 -- The first Bool is True if A contains 
344                 -- 'orphan' rules or instance decls
345
346                 -- The second Bool is true if the interface file actually
347                 -- read was an .hi-boot file
348
349                 -- Nothing => A's interface not yet read, but this module has
350                 --            imported a module, B, that itself depends on A
351                 --
352                 -- Just xx => A's interface has been read.  The Module in 
353                 --              the Just has the correct Dll flag
354
355                 -- This set is used to decide whether to look for
356                 -- A.hi or A.hi-boot when importing A.f.
357                 -- Basically, we look for A.hi if A is in the map, and A.hi-boot
358                 -- otherwise
359
360 type DeclsMap = NameEnv (Version, AvailInfo, Bool, (Module, RdrNameHsDecl))
361                 -- A DeclsMap contains a binding for each Name in the declaration
362                 -- including the constructors of a type decl etc.
363                 -- The Bool is True just for the 'main' Name.
364 \end{code}
365
366
367 %************************************************************************
368 %*                                                                      *
369 \subsection{Main monad code}
370 %*                                                                      *
371 %************************************************************************
372
373 \begin{code}
374 initRn :: ModuleName -> UniqSupply -> SearchPath -> SrcLoc
375        -> RnMG r
376        -> IO (r, Bag ErrMsg, Bag WarnMsg)
377
378 initRn mod us dirs loc do_rn = do
379   himaps    <- mkModuleHiMaps dirs
380   names_var <- newIORef (us, emptyFM, builtins, emptyFM)
381   errs_var  <- newIORef (emptyBag,emptyBag)
382   iface_var <- newIORef emptyIfaces 
383   let
384         rn_down = RnDown { rn_loc = loc, rn_ns = names_var, 
385                            rn_errs = errs_var, 
386                            rn_hi_maps = himaps, 
387                            rn_ifaces = iface_var,
388                            rn_mod = mod }
389
390         -- do the business
391   res <- do_rn rn_down ()
392
393         -- grab errors and return
394   (warns, errs) <- readIORef errs_var
395
396   return (res, errs, warns)
397
398
399 initRnMS :: GlobalRdrEnv -> FixityEnv -> RnMode -> RnMS r -> RnM d r
400 initRnMS rn_env fixity_env mode thing_inside rn_down g_down
401   = let
402         s_down = SDown { rn_genv = rn_env, rn_lenv = emptyRdrEnv, 
403                          rn_fixenv = fixity_env, rn_mode = mode }
404     in
405     thing_inside rn_down s_down
406
407 initIfaceRnMS :: Module -> RnMS r -> RnM d r
408 initIfaceRnMS mod thing_inside 
409   = initRnMS emptyRdrEnv emptyNameEnv InterfaceMode $
410     setModuleRn (moduleName mod) thing_inside
411
412 emptyIfaces :: Ifaces
413 emptyIfaces = Ifaces { iImpModInfo = emptyFM,
414                        iDecls = emptyNameEnv,
415                        iDeferred = emptyNameSet,
416                        iFixes = emptyNameEnv,
417                        iSlurp = unitNameSet (mkUnboundName dummyRdrVarName),
418                         -- Pretend that the dummy unbound name has already been
419                         -- slurped.  This is what's returned for an out-of-scope name,
420                         -- and we don't want thereby to try to suck it in!
421                        iVSlurp = [],
422                        iInsts = emptyBag,
423                        iRules = emptyBag,
424                        iDeprecs = emptyNameEnv
425               }
426
427 builtins :: FiniteMap (ModuleName,OccName) Name
428 builtins = 
429    bagToFM (
430    mapBag (\ name ->  ((moduleName (nameModule name), nameOccName name), name))
431           builtinNames)
432 \end{code}
433
434 @renameSourceCode@ is used to rename stuff ``out-of-line'';
435 that is, not as part of the main renamer.
436 Sole examples: derived definitions,
437 which are only generated in the type checker.
438
439 The @RnNameSupply@ includes a @UniqueSupply@, so if you call it more than
440 once you must either split it, or install a fresh unique supply.
441
442 \begin{code}
443 renameSourceCode :: ModuleName
444                  -> RnNameSupply
445                  -> RnMS r
446                  -> r
447
448 renameSourceCode mod_name name_supply m
449   = unsafePerformIO (
450         -- It's not really unsafe!  When renaming source code we
451         -- only do any I/O if we need to read in a fixity declaration;
452         -- and that doesn't happen in pragmas etc
453
454         mkModuleHiMaps (mkSearchPath opt_HiMap) >>= \ himaps ->
455         newIORef name_supply            >>= \ names_var ->
456         newIORef (emptyBag,emptyBag)    >>= \ errs_var ->
457         let
458             rn_down = RnDown { rn_loc = mkGeneratedSrcLoc, rn_ns = names_var,
459                                rn_errs = errs_var, rn_hi_maps = himaps,
460                                rn_mod = mod_name, 
461                                rn_ifaces = panic "rnameSourceCode: rn_ifaces"  -- Not required
462                              }
463             s_down = SDown { rn_mode = InterfaceMode,
464                                -- So that we can refer to PrelBase.True etc
465                              rn_genv = emptyRdrEnv, rn_lenv = emptyRdrEnv,
466                              rn_fixenv = emptyNameEnv }
467         in
468         m rn_down s_down                        >>= \ result ->
469         
470         readIORef errs_var                      >>= \ (warns,errs) ->
471
472         (if not (isEmptyBag errs) then
473                 pprTrace "Urk! renameSourceCode found errors" (display errs) 
474 #ifdef DEBUG
475          else if not (isEmptyBag warns) then
476                 pprTrace "Note: renameSourceCode found warnings" (display warns)
477 #endif
478          else
479                 id) $
480
481         return result
482     )
483   where
484     display errs = pprBagOfErrors errs
485
486 {-# INLINE thenRn #-}
487 {-# INLINE thenRn_ #-}
488 {-# INLINE returnRn #-}
489 {-# INLINE andRn #-}
490
491 returnRn :: a -> RnM d a
492 thenRn   :: RnM d a -> (a -> RnM d b) -> RnM d b
493 thenRn_  :: RnM d a -> RnM d b -> RnM d b
494 andRn    :: (a -> a -> a) -> RnM d a -> RnM d a -> RnM d a
495 mapRn    :: (a -> RnM d b) -> [a] -> RnM d [b]
496 mapRn_   :: (a -> RnM d b) -> [a] -> RnM d ()
497 mapMaybeRn :: (a -> RnM d (Maybe b)) -> [a] -> RnM d [b]
498 flatMapRn  :: (a -> RnM d [b])       -> [a] -> RnM d [b]
499 sequenceRn :: [RnM d a] -> RnM d [a]
500 foldlRn :: (b  -> a -> RnM d b) -> b -> [a] -> RnM d b
501 mapAndUnzipRn :: (a -> RnM d (b,c)) -> [a] -> RnM d ([b],[c])
502 fixRn    :: (a -> RnM d a) -> RnM d a
503
504 returnRn v gdown ldown  = return v
505 thenRn m k gdown ldown  = m gdown ldown >>= \ r -> k r gdown ldown
506 thenRn_ m k gdown ldown = m gdown ldown >> k gdown ldown
507 fixRn m gdown ldown = fixIO (\r -> m r gdown ldown)
508 andRn combiner m1 m2 gdown ldown
509   = m1 gdown ldown >>= \ res1 ->
510     m2 gdown ldown >>= \ res2 ->
511     return (combiner res1 res2)
512
513 sequenceRn []     = returnRn []
514 sequenceRn (m:ms) =  m                  `thenRn` \ r ->
515                      sequenceRn ms      `thenRn` \ rs ->
516                      returnRn (r:rs)
517
518 mapRn f []     = returnRn []
519 mapRn f (x:xs)
520   = f x         `thenRn` \ r ->
521     mapRn f xs  `thenRn` \ rs ->
522     returnRn (r:rs)
523
524 mapRn_ f []     = returnRn ()
525 mapRn_ f (x:xs) = 
526     f x         `thenRn_`
527     mapRn_ f xs
528
529 foldlRn k z [] = returnRn z
530 foldlRn k z (x:xs) = k z x      `thenRn` \ z' ->
531                      foldlRn k z' xs
532
533 mapAndUnzipRn f [] = returnRn ([],[])
534 mapAndUnzipRn f (x:xs)
535   = f x                 `thenRn` \ (r1,  r2)  ->
536     mapAndUnzipRn f xs  `thenRn` \ (rs1, rs2) ->
537     returnRn (r1:rs1, r2:rs2)
538
539 mapAndUnzip3Rn f [] = returnRn ([],[],[])
540 mapAndUnzip3Rn f (x:xs)
541   = f x                 `thenRn` \ (r1,  r2,  r3)  ->
542     mapAndUnzip3Rn f xs `thenRn` \ (rs1, rs2, rs3) ->
543     returnRn (r1:rs1, r2:rs2, r3:rs3)
544
545 mapMaybeRn f []     = returnRn []
546 mapMaybeRn f (x:xs) = f x               `thenRn` \ maybe_r ->
547                       mapMaybeRn f xs   `thenRn` \ rs ->
548                       case maybe_r of
549                         Nothing -> returnRn rs
550                         Just r  -> returnRn (r:rs)
551
552 flatMapRn f []     = returnRn []
553 flatMapRn f (x:xs) = f x                `thenRn` \ r ->
554                      flatMapRn f xs     `thenRn` \ rs ->
555                      returnRn (r ++ rs)
556 \end{code}
557
558
559
560 %************************************************************************
561 %*                                                                      *
562 \subsection{Boring plumbing for common part}
563 %*                                                                      *
564 %************************************************************************
565
566
567 %================
568 \subsubsection{  Errors and warnings}
569 %=====================
570
571 \begin{code}
572 failWithRn :: a -> Message -> RnM d a
573 failWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
574   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
575     writeIORef errs_var (warns, errs `snocBag` err)             >> 
576     return res
577   where
578     err = addShortErrLocLine loc msg
579
580 warnWithRn :: a -> Message -> RnM d a
581 warnWithRn res msg (RnDown {rn_errs = errs_var, rn_loc = loc}) l_down
582   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
583     writeIORef errs_var (warns `snocBag` warn, errs)    >> 
584     return res
585   where
586     warn = addShortWarnLocLine loc msg
587
588 addErrRn :: Message -> RnM d ()
589 addErrRn err = failWithRn () err
590
591 checkRn :: Bool -> Message -> RnM d ()  -- Check that a condition is true
592 checkRn False err = addErrRn err
593 checkRn True  err = returnRn ()
594
595 warnCheckRn :: Bool -> Message -> RnM d ()      -- Check that a condition is true
596 warnCheckRn False err = addWarnRn err
597 warnCheckRn True  err = returnRn ()
598
599 addWarnRn :: Message -> RnM d ()
600 addWarnRn warn = warnWithRn () warn
601
602 checkErrsRn :: RnM d Bool               -- True <=> no errors so far
603 checkErrsRn (RnDown {rn_errs = errs_var}) l_down
604   = readIORef  errs_var                                         >>=  \ (warns,errs) ->
605     return (isEmptyBag errs)
606 \end{code}
607
608
609 %================
610 \subsubsection{  Source location}
611 %=====================
612
613 \begin{code}
614 pushSrcLocRn :: SrcLoc -> RnM d a -> RnM d a
615 pushSrcLocRn loc' m down l_down
616   = m (down {rn_loc = loc'}) l_down
617
618 getSrcLocRn :: RnM d SrcLoc
619 getSrcLocRn down l_down
620   = return (rn_loc down)
621 \end{code}
622
623 %================
624 \subsubsection{  Name supply}
625 %=====================
626
627 \begin{code}
628 getNameSupplyRn :: RnM d RnNameSupply
629 getNameSupplyRn rn_down l_down
630   = readIORef (rn_ns rn_down)
631
632 setNameSupplyRn :: RnNameSupply -> RnM d ()
633 setNameSupplyRn names' (RnDown {rn_ns = names_var}) l_down
634   = writeIORef names_var names'
635
636 -- See comments with RnNameSupply above.
637 newInstUniq :: String -> RnM d Int
638 newInstUniq key (RnDown {rn_ns = names_var}) l_down
639   = readIORef names_var                         >>= \ (us, mapInst, cache, ipcache) ->
640     let
641         uniq = case lookupFM mapInst key of
642                    Just x  -> x+1
643                    Nothing -> 0
644         mapInst' = addToFM mapInst key uniq
645     in
646     writeIORef names_var (us, mapInst', cache, ipcache) >>
647     return uniq
648
649 getUniqRn :: RnM d Unique
650 getUniqRn (RnDown {rn_ns = names_var}) l_down
651  = readIORef names_var >>= \ (us, mapInst, cache, ipcache) ->
652    let
653      (us1,us') = splitUniqSupply us
654    in
655    writeIORef names_var (us', mapInst, cache, ipcache)  >>
656    return (uniqFromSupply us1)
657 \end{code}
658
659 %================
660 \subsubsection{  Module}
661 %=====================
662
663 \begin{code}
664 getModuleRn :: RnM d ModuleName
665 getModuleRn (RnDown {rn_mod = mod_name}) l_down
666   = return mod_name
667
668 setModuleRn :: ModuleName -> RnM d a -> RnM d a
669 setModuleRn new_mod enclosed_thing rn_down l_down
670   = enclosed_thing (rn_down {rn_mod = new_mod}) l_down
671 \end{code}
672
673
674 %************************************************************************
675 %*                                                                      *
676 \subsection{Plumbing for rename-source part}
677 %*                                                                      *
678 %************************************************************************
679
680 %================
681 \subsubsection{  RnEnv}
682 %=====================
683
684 \begin{code}
685 getNameEnvs :: RnMS (GlobalRdrEnv, LocalRdrEnv)
686 getNameEnvs rn_down (SDown {rn_genv = global_env, rn_lenv = local_env})
687   = return (global_env, local_env)
688
689 getLocalNameEnv :: RnMS LocalRdrEnv
690 getLocalNameEnv rn_down (SDown {rn_lenv = local_env})
691   = return local_env
692
693 setLocalNameEnv :: LocalRdrEnv -> RnMS a -> RnMS a
694 setLocalNameEnv local_env' m rn_down l_down
695   = m rn_down (l_down {rn_lenv = local_env'})
696
697 getFixityEnv :: RnMS FixityEnv
698 getFixityEnv rn_down (SDown {rn_fixenv = fixity_env})
699   = return fixity_env
700
701 extendFixityEnv :: [(Name, RenamedFixitySig)] -> RnMS a -> RnMS a
702 extendFixityEnv fixes enclosed_scope
703                 rn_down l_down@(SDown {rn_fixenv = fixity_env})
704   = let
705         new_fixity_env = extendNameEnv fixity_env fixes
706     in
707     enclosed_scope rn_down (l_down {rn_fixenv = new_fixity_env})
708 \end{code}
709
710 %================
711 \subsubsection{  Mode}
712 %=====================
713
714 \begin{code}
715 getModeRn :: RnMS RnMode
716 getModeRn rn_down (SDown {rn_mode = mode})
717   = return mode
718
719 setModeRn :: RnMode -> RnMS a -> RnMS a
720 setModeRn new_mode thing_inside rn_down l_down
721   = thing_inside rn_down (l_down {rn_mode = new_mode})
722 \end{code}
723
724
725 %************************************************************************
726 %*                                                                      *
727 \subsection{Plumbing for rename-globals part}
728 %*                                                                      *
729 %************************************************************************
730
731 \begin{code}
732 getIfacesRn :: RnM d Ifaces
733 getIfacesRn (RnDown {rn_ifaces = iface_var}) _
734   = readIORef iface_var
735
736 setIfacesRn :: Ifaces -> RnM d ()
737 setIfacesRn ifaces (RnDown {rn_ifaces = iface_var}) _
738   = writeIORef iface_var ifaces
739
740 getHiMaps :: RnM d (SearchPath, ModuleHiMap, ModuleHiMap)
741 getHiMaps (RnDown {rn_hi_maps = himaps}) _ 
742   = return himaps
743 \end{code}
744 \end{code}