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