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