[project @ 2000-02-17 14:47:21 by panne]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1998
3 %
4 \section[Rename]{Renaming and dependency analysis passes}
5
6 \begin{code}
7 module Rename ( renameModule ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn
12 import RdrHsSyn         ( RdrNameHsModule )
13 import RnHsSyn          ( RenamedHsModule, RenamedHsDecl, 
14                           extractHsTyNames, extractHsCtxtTyNames
15                         )
16
17 import CmdLineOpts      ( opt_HiMap, opt_D_dump_rn_trace,
18                           opt_D_dump_rn, opt_D_dump_rn_stats,
19                           opt_WarnUnusedBinds, opt_WarnUnusedImports
20                         )
21 import RnMonad
22 import RnNames          ( getGlobalNames )
23 import RnSource         ( rnSourceDecls, rnDecl )
24 import RnIfaces         ( getImportedInstDecls, importDecl, getImportVersions,
25                           getImportedRules, loadHomeInterface, getSlurped, removeContext
26                         )
27 import RnEnv            ( availName, availNames, availsToNameSet, 
28                           warnUnusedImports, warnUnusedLocalBinds, mapFvRn, lookupImplicitOccRn,
29                           FreeVars, plusFVs, plusFV, unitFV, emptyFVs, isEmptyFVs
30                         )
31 import Module           ( Module, ModuleName, pprModule, mkSearchPath, mkThisModule )
32 import Name             ( Name, isLocallyDefined,
33                           NamedThing(..), ImportReason(..), Provenance(..),
34                           pprOccName, nameOccName, nameUnique,
35                           getNameProvenance, isUserImportedExplicitlyName,
36                           maybeWiredInTyConName, maybeWiredInIdName, isWiredInName
37                         )
38 import Id               ( idType )
39 import DataCon          ( dataConTyCon, dataConType )
40 import TyCon            ( TyCon, tyConDataCons, isSynTyCon, getSynTyConDefn )
41 import RdrName          ( RdrName )
42 import NameSet
43 import PrelMods         ( mAIN_Name, pREL_MAIN_Name )
44 import TysWiredIn       ( unitTyCon, intTyCon, doubleTyCon, boolTyCon )
45 import PrelInfo         ( ioTyCon_NAME, thinAirIdNames, fractionalClassKeys, derivingOccurrences )
46 import Type             ( namesOfType, funTyCon )
47 import ErrUtils         ( printErrorsAndWarnings, dumpIfSet, ghcExit )
48 import BasicTypes       ( NewOrData(..) )
49 import Bag              ( isEmptyBag, bagToList )
50 import FiniteMap        ( fmToList, delListFromFM, addToFM, sizeFM, eltsFM )
51 import UniqSupply       ( UniqSupply )
52 import UniqFM           ( lookupUFM )
53 import Util             ( equivClasses )
54 import Maybes           ( maybeToBool )
55 import SrcLoc           ( mkBuiltinSrcLoc )
56 import Outputable
57 \end{code}
58
59
60
61 \begin{code}
62 renameModule :: UniqSupply
63              -> RdrNameHsModule
64              -> IO (Maybe 
65                       ( Module
66                       , RenamedHsModule   -- Output, after renaming
67                       , InterfaceDetails  -- Interface; for interface file generation
68                       , RnNameSupply      -- Final env; for renaming derivings
69                       , [ModuleName]      -- Imported modules; for profiling
70                       ))
71
72 renameModule us this_mod@(HsModule mod_name vers exports imports local_decls _ loc)
73   =     -- Initialise the renamer monad
74     initRn mod_name us (mkSearchPath opt_HiMap) loc
75            (rename this_mod)                            >>=
76         \ ((maybe_rn_stuff, dump_action), rn_errs_bag, rn_warns_bag) ->
77
78         -- Check for warnings
79     printErrorsAndWarnings rn_errs_bag rn_warns_bag     >>
80
81         -- Dump any debugging output
82     dump_action                                         >>
83
84         -- Return results
85     if not (isEmptyBag rn_errs_bag) then
86             ghcExit 1 >> return Nothing
87     else
88             return maybe_rn_stuff
89 \end{code}
90
91
92 \begin{code}
93 rename this_mod@(HsModule mod_name vers _ imports local_decls deprec loc)
94   =     -- FIND THE GLOBAL NAME ENVIRONMENT
95     getGlobalNames this_mod                     `thenRn` \ maybe_stuff ->
96
97         -- CHECK FOR EARLY EXIT
98     if not (maybeToBool maybe_stuff) then
99         -- Everything is up to date; no need to recompile further
100         rnDump [] []            `thenRn` \ dump_action ->
101         returnRn (Nothing, dump_action)
102     else
103     let
104         Just (export_env, gbl_env, fixity_env, global_avail_env) = maybe_stuff
105     in
106
107         -- RENAME THE SOURCE
108     initRnMS gbl_env fixity_env SourceMode (
109         rnSourceDecls local_decls
110     )                                   `thenRn` \ (rn_local_decls, source_fvs) ->
111
112         -- SLURP IN ALL THE NEEDED DECLARATIONS
113     implicitFVs mod_name rn_local_decls         `thenRn` \ implicit_fvs -> 
114     let
115         real_source_fvs = implicit_fvs `plusFV` source_fvs
116                 -- It's important to do the "plus" this way round, so that
117                 -- when compiling the prelude, locally-defined (), Bool, etc
118                 -- override the implicit ones. 
119     in
120     slurpImpDecls real_source_fvs       `thenRn` \ rn_imp_decls ->
121     let
122         rn_all_decls       = rn_local_decls ++ rn_imp_decls
123     in
124
125         -- EXIT IF ERRORS FOUND
126     checkErrsRn                                 `thenRn` \ no_errs_so_far ->
127     if not no_errs_so_far then
128         -- Found errors already, so exit now
129         rnDump rn_imp_decls rn_all_decls        `thenRn` \ dump_action ->
130         returnRn (Nothing, dump_action)
131     else
132
133         -- GENERATE THE VERSION/USAGE INFO
134     getImportVersions mod_name export_env       `thenRn` \ my_usages ->
135     getNameSupplyRn                             `thenRn` \ name_supply ->
136
137         -- REPORT UNUSED NAMES
138     reportUnusedNames gbl_env global_avail_env
139                       export_env
140                       source_fvs                        `thenRn_`
141
142         -- RETURN THE RENAMED MODULE
143     let
144         has_orphans        = any isOrphanDecl rn_local_decls
145         direct_import_mods = [mod | ImportDecl mod _ _ _ _ _ <- imports]
146         renamed_module = HsModule mod_name vers 
147                                   trashed_exports trashed_imports
148                                   rn_all_decls
149                                   deprec
150                                   loc
151     in
152     rnDump rn_imp_decls rn_all_decls            `thenRn` \ dump_action ->
153     returnRn (Just (mkThisModule mod_name,
154                     renamed_module, 
155                     (has_orphans, my_usages, export_env),
156                     name_supply,
157                     direct_import_mods), dump_action)
158   where
159     trashed_exports  = {-trace "rnSource:trashed_exports"-} Nothing
160     trashed_imports  = {-trace "rnSource:trashed_imports"-} []
161 \end{code}
162
163 @implicitFVs@ forces the renamer to slurp in some things which aren't
164 mentioned explicitly, but which might be needed by the type checker.
165
166 \begin{code}
167 implicitFVs mod_name decls
168   = mapRn lookupImplicitOccRn implicit_occs     `thenRn` \ implicit_names ->
169     returnRn (implicit_main                             `plusFV` 
170               mkNameSet (map getName default_tycons)    `plusFV`
171               mkNameSet thinAirIdNames                  `plusFV`
172               mkNameSet implicit_names)
173   where
174         -- Add occurrences for Int, and (), because they
175         -- are the types to which ambigious type variables may be defaulted by
176         -- the type checker; so they won't always appear explicitly.
177         -- [The () one is a GHC extension for defaulting CCall results.]
178         -- ALSO: funTyCon, since it occurs implicitly everywhere!
179         --       (we don't want to be bothered with making funTyCon a
180         --        free var at every function application!)
181         -- Double is dealt with separately in getGates
182     default_tycons = [unitTyCon, funTyCon, boolTyCon, intTyCon]
183
184         -- Add occurrences for IO or PrimIO
185     implicit_main |  mod_name == mAIN_Name
186                   || mod_name == pREL_MAIN_Name = unitFV ioTyCon_NAME
187                   |  otherwise                  = emptyFVs
188
189         -- Now add extra "occurrences" for things that
190         -- the deriving mechanism, or defaulting, will later need in order to
191         -- generate code
192     implicit_occs = foldr ((++) . get) [] decls
193
194     get (TyClD (TyData _ _ _ _ _ (Just deriv_classes) _ _))
195        = concat (map get_deriv deriv_classes)
196     get other = []
197
198     get_deriv cls = case lookupUFM derivingOccurrences cls of
199                         Nothing   -> []
200                         Just occs -> occs
201 \end{code}
202
203 \begin{code}
204 isOrphanDecl (InstD (InstDecl inst_ty _ _ _ _))
205   = not (foldNameSet ((||) . isLocallyDefined) False (extractHsTyNames (removeContext inst_ty)))
206         -- The 'removeContext' is because of
207         --      instance Foo a => Baz T where ...
208         -- The decl is an orphan if Baz and T are both not locally defined,
209         --      even if Foo *is* locally defined
210
211 isOrphanDecl (RuleD (RuleDecl _ _ _ lhs _ _))
212   = check lhs
213   where
214         -- At the moment we just check for common LHS forms
215         -- Expand as necessary.  Getting it wrong just means
216         -- more orphans than necessary
217     check (HsVar v)       = not (isLocallyDefined v)
218     check (HsApp f a)     = check f && check a
219     check (HsLit _)       = False
220     check (OpApp l o _ r) = check l && check o && check r
221     check (NegApp e _)    = check e
222     check (HsPar e)       = check e
223     check (SectionL e o)  = check e && check o
224     check (SectionR o e)  = check e && check o
225
226     check other           = True        -- Safe fall through
227
228 isOrphanDecl other = False
229 \end{code}
230
231
232 \begin{code}
233 dupDefaultDeclErrRn (DefaultDecl _ locn1 : dup_things)
234   = pushSrcLocRn locn1  $
235     addErrRn msg
236   where
237     msg = hang (ptext SLIT("Multiple default declarations"))
238                4  (vcat (map pp dup_things))
239     pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
240 \end{code}
241
242
243 %*********************************************************
244 %*                                                       *
245 \subsection{Slurping declarations}
246 %*                                                       *
247 %*********************************************************
248
249 \begin{code}
250 -------------------------------------------------------
251 slurpImpDecls source_fvs
252   = traceRn (text "slurpImp" <+> fsep (map ppr (nameSetToList source_fvs))) `thenRn_`
253
254         -- The current slurped-set records all local things
255     getSlurped                                  `thenRn` \ source_binders ->
256     slurpSourceRefs source_binders source_fvs   `thenRn` \ (decls, needed) ->
257
258         -- And finally get everything else
259     closeDecls decls needed
260
261 -------------------------------------------------------
262 slurpSourceRefs :: NameSet                      -- Variables defined in source
263                 -> FreeVars                     -- Variables referenced in source
264                 -> RnMG ([RenamedHsDecl],
265                          FreeVars)              -- Un-satisfied needs
266 -- The declaration (and hence home module) of each gate has
267 -- already been loaded
268
269 slurpSourceRefs source_binders source_fvs
270   = go_outer []                         -- Accumulating decls
271              emptyFVs                   -- Unsatisfied needs
272              emptyFVs                   -- Accumulating gates
273              (nameSetToList source_fvs) -- Things whose defn hasn't been loaded yet
274   where
275         -- The outer loop repeatedly slurps the decls for the current gates
276         -- and the instance decls 
277
278         -- The outer loop is needed because consider
279         --      instance Foo a => Baz (Maybe a) where ...
280         -- It may be that @Baz@ and @Maybe@ are used in the source module,
281         -- but not @Foo@; so we need to chase @Foo@ too.
282         --
283         -- We also need to follow superclass refs.  In particular, 'chasing @Foo@' must
284         -- include actually getting in Foo's class decl
285         --      class Wib a => Foo a where ..
286         -- so that its superclasses are discovered.  The point is that Wib is a gate too.
287         -- We do this for tycons too, so that we look through type synonyms.
288
289     go_outer decls fvs all_gates []     
290         = returnRn (decls, fvs)
291
292     go_outer decls fvs all_gates refs   -- refs are not necessarily slurped yet
293         = traceRn (text "go_outer" <+> ppr refs)                `thenRn_`
294           go_inner decls fvs emptyFVs refs                      `thenRn` \ (decls1, fvs1, gates1) ->
295           getImportedInstDecls (all_gates `plusFV` gates1)      `thenRn` \ inst_decls ->
296           rnInstDecls decls1 fvs1 gates1 inst_decls             `thenRn` \ (decls2, fvs2, gates2) ->
297           go_outer decls2 fvs2 (all_gates `plusFV` gates2)
298                                (nameSetToList (gates2 `minusNameSet` all_gates))
299                 -- Knock out the all_gates because even if we don't slurp any new
300                 -- decls we can get some apparently-new gates from wired-in names
301
302     go_inner decls fvs gates []
303         = returnRn (decls, fvs, gates)
304
305     go_inner decls fvs gates (wanted_name:refs) 
306         | isWiredInName wanted_name
307         = load_home wanted_name         `thenRn_`
308           go_inner decls fvs (gates `plusFV` getWiredInGates wanted_name) refs
309
310         | otherwise
311         = importDecl wanted_name                `thenRn` \ maybe_decl ->
312           case maybe_decl of
313             Nothing   -> go_inner decls fvs gates refs  -- No declaration... (already slurped, or local)
314             Just decl -> rnIfaceDecl decl               `thenRn` \ (new_decl, fvs1) ->
315                          go_inner (new_decl : decls)
316                                   (fvs1 `plusFV` fvs)
317                                   (gates `plusFV` getGates source_fvs new_decl)
318                                   refs
319
320         -- When we find a wired-in name we must load its
321         -- home module so that we find any instance decls therein
322     load_home name 
323         | name `elemNameSet` source_binders = returnRn ()
324                 -- When compiling the prelude, a wired-in thing may
325                 -- be defined in this module, in which case we don't
326                 -- want to load its home module!
327                 -- Using 'isLocallyDefined' doesn't work because some of
328                 -- the free variables returned are simply 'listTyCon_Name',
329                 -- with a system provenance.  We could look them up every time
330                 -- but that seems a waste.
331         | otherwise                           = loadHomeInterface doc name      `thenRn_`
332                                                 returnRn ()
333         where
334           doc = ptext SLIT("need home module for wired in thing") <+> ppr name
335
336 rnInstDecls decls fvs gates []
337   = returnRn (decls, fvs, gates)
338 rnInstDecls decls fvs gates (d:ds) 
339   = rnIfaceDecl d               `thenRn` \ (new_decl, fvs1) ->
340     rnInstDecls (new_decl:decls) 
341                 (fvs1 `plusFV` fvs)
342                 (gates `plusFV` getInstDeclGates new_decl)
343                 ds
344 \end{code}
345
346
347 \begin{code}
348 -------------------------------------------------------
349 -- closeDecls keeps going until the free-var set is empty
350 closeDecls decls needed
351   | not (isEmptyFVs needed)
352   = slurpDecls decls needed     `thenRn` \ (decls1, needed1) ->
353     closeDecls decls1 needed1
354
355   | otherwise
356   = getImportedRules                    `thenRn` \ rule_decls ->
357     case rule_decls of
358         []    -> returnRn decls -- No new rules, so we are done
359         other -> rnIfaceDecls decls emptyFVs rule_decls         `thenRn` \ (decls1, needed1) ->
360                  closeDecls decls1 needed1
361                  
362
363 -------------------------------------------------------
364 rnIfaceDecls :: [RenamedHsDecl] -> FreeVars
365              -> [(Module, RdrNameHsDecl)]
366              -> RnM d ([RenamedHsDecl], FreeVars)
367 rnIfaceDecls decls fvs []     = returnRn (decls, fvs)
368 rnIfaceDecls decls fvs (d:ds) = rnIfaceDecl d           `thenRn` \ (new_decl, fvs1) ->
369                                 rnIfaceDecls (new_decl:decls) (fvs1 `plusFV` fvs) ds
370
371 rnIfaceDecl (mod, decl) = initIfaceRnMS mod (rnDecl decl)       
372                         
373
374 -------------------------------------------------------
375 -- Augment decls with any decls needed by needed.
376 -- Return also free vars of the new decls (only)
377 slurpDecls decls needed
378   = go decls emptyFVs (nameSetToList needed) 
379   where
380     go decls fvs []         = returnRn (decls, fvs)
381     go decls fvs (ref:refs) = slurpDecl decls fvs ref   `thenRn` \ (decls1, fvs1) ->
382                               go decls1 fvs1 refs
383
384 -------------------------------------------------------
385 slurpDecl decls fvs wanted_name
386   = importDecl wanted_name              `thenRn` \ maybe_decl ->
387     case maybe_decl of
388         -- No declaration... (wired in thing)
389         Nothing -> returnRn (decls, fvs)
390
391         -- Found a declaration... rename it
392         Just decl -> rnIfaceDecl decl           `thenRn` \ (new_decl, fvs1) ->
393                      returnRn (new_decl:decls, fvs1 `plusFV` fvs)
394 \end{code}
395
396
397 %*********************************************************
398 %*                                                       *
399 \subsection{Extracting the `gates'}
400 %*                                                       *
401 %*********************************************************
402
403 When we import a declaration like
404 \begin{verbatim}
405         data T = T1 Wibble | T2 Wobble
406 \end{verbatim}
407 we don't want to treat @Wibble@ and @Wobble@ as gates
408 {\em unless} @T1@, @T2@ respectively are mentioned by the user program.
409 If only @T@ is mentioned
410 we want only @T@ to be a gate;
411 that way we don't suck in useless instance
412 decls for (say) @Eq Wibble@, when they can't possibly be useful.
413
414 @getGates@ takes a newly imported (and renamed) decl, and the free
415 vars of the source program, and extracts from the decl the gate names.
416
417 \begin{code}
418 getGates source_fvs (SigD (IfaceSig _ ty _ _))
419   = extractHsTyNames ty
420
421 getGates source_fvs (TyClD (ClassDecl ctxt cls tvs _ sigs _ _ _ _ _ _))
422   = (delListFromNameSet (foldr (plusFV . get) (extractHsCtxtTyNames ctxt) sigs)
423                        (map getTyVarName tvs)
424      `addOneToNameSet` cls)
425     `plusFV` maybe_double
426   where
427     get (ClassOpSig n _ _ ty _) 
428         | n `elemNameSet` source_fvs = extractHsTyNames ty
429         | otherwise                  = emptyFVs
430
431         -- If we load any numeric class that doesn't have
432         -- Int as an instance, add Double to the gates. 
433         -- This takes account of the fact that Double might be needed for
434         -- defaulting, but we don't want to load Double (and all its baggage)
435         -- if the more exotic classes aren't used at all.
436     maybe_double | nameUnique cls `elem` fractionalClassKeys 
437                  = unitFV (getName doubleTyCon)
438                  | otherwise
439                  = emptyFVs
440
441 getGates source_fvs (TyClD (TySynonym tycon tvs ty _))
442   = delListFromNameSet (extractHsTyNames ty)
443                        (map getTyVarName tvs)
444         -- A type synonym type constructor isn't a "gate" for instance decls
445
446 getGates source_fvs (TyClD (TyData _ ctxt tycon tvs cons _ _ _))
447   = delListFromNameSet (foldr (plusFV . get) (extractHsCtxtTyNames ctxt) cons)
448                        (map getTyVarName tvs)
449     `addOneToNameSet` tycon
450   where
451     get (ConDecl n tvs ctxt details _)
452         | n `elemNameSet` source_fvs
453                 -- If the constructor is method, get fvs from all its fields
454         = delListFromNameSet (get_details details `plusFV` 
455                               extractHsCtxtTyNames ctxt)
456                              (map getTyVarName tvs)
457     get (ConDecl n tvs ctxt (RecCon fields) _)
458                 -- Even if the constructor isn't mentioned, the fields
459                 -- might be, as selectors.  They can't mention existentially
460                 -- bound tyvars (typechecker checks for that) so no need for 
461                 -- the deleteListFromNameSet part
462         = foldr (plusFV . get_field) emptyFVs fields
463         
464     get other_con = emptyFVs
465
466     get_details (VanillaCon tys) = plusFVs (map get_bang tys)
467     get_details (InfixCon t1 t2) = get_bang t1 `plusFV` get_bang t2
468     get_details (RecCon fields)  = plusFVs [get_bang t | (_, t) <- fields]
469     get_details (NewCon t _)     = extractHsTyNames t
470
471     get_field (fs,t) | any (`elemNameSet` source_fvs) fs = get_bang t
472                      | otherwise                         = emptyFVs
473
474     get_bang (Banged   t) = extractHsTyNames t
475     get_bang (Unbanged t) = extractHsTyNames t
476     get_bang (Unpacked t) = extractHsTyNames t
477
478 getGates source_fvs other_decl = emptyFVs
479 \end{code}
480
481 @getWiredInGates@ is just like @getGates@, but it sees a wired-in @Name@
482 rather than a declaration.
483
484 \begin{code}
485 getWiredInGates :: Name -> FreeVars
486 getWiredInGates name    -- No classes are wired in
487   | is_id                = getWiredInGates_s (namesOfType (idType the_id))
488   | isSynTyCon the_tycon = getWiredInGates_s
489          (delListFromNameSet (namesOfType ty) (map getName tyvars))
490   | otherwise            = unitFV name
491   where
492     maybe_wired_in_id    = maybeWiredInIdName name
493     is_id                = maybeToBool maybe_wired_in_id
494     maybe_wired_in_tycon = maybeWiredInTyConName name
495     Just the_id          = maybe_wired_in_id
496     Just the_tycon       = maybe_wired_in_tycon
497     (tyvars,ty)          = getSynTyConDefn the_tycon
498
499 getWiredInGates_s names = foldr (plusFV . getWiredInGates) emptyFVs (nameSetToList names)
500 \end{code}
501
502 \begin{code}
503 getInstDeclGates (InstD (InstDecl inst_ty _ _ _ _)) = extractHsTyNames inst_ty
504 getInstDeclGates other                              = emptyFVs
505 \end{code}
506
507
508 %*********************************************************
509 %*                                                       *
510 \subsection{Unused names}
511 %*                                                       *
512 %*********************************************************
513
514 \begin{code}
515 reportUnusedNames gbl_env avail_env (ExportEnv export_avails _ _) mentioned_names
516   = let
517         used_names = mentioned_names `unionNameSets` availsToNameSet export_avails
518
519         -- Now, a use of C implies a use of T,
520         -- if C was brought into scope by T(..) or T(C)
521         really_used_names = used_names `unionNameSets`
522           mkNameSet [ availName avail   
523                     | sub_name <- nameSetToList used_names,
524                       let avail = case lookupNameEnv avail_env sub_name of
525                             Just avail -> avail
526                             Nothing -> WARN( True, text "reportUnusedName: not in avail_env" <+> ppr sub_name )
527                                        Avail sub_name
528                     ]
529
530         defined_names = mkNameSet (concat (rdrEnvElts gbl_env))
531         defined_but_not_used =
532            nameSetToList (defined_names `minusNameSet` really_used_names)
533
534         -- Filter out the ones only defined implicitly
535         bad_locals = [n | n <- defined_but_not_used, isLocallyDefined             n]
536         bad_imps   = [n | n <- defined_but_not_used, isUserImportedExplicitlyName n]
537     in
538     warnUnusedLocalBinds bad_locals     `thenRn_`
539     warnUnusedImports bad_imps
540
541 rnDump  :: [RenamedHsDecl]      -- Renamed imported decls
542         -> [RenamedHsDecl]      -- Renamed local decls
543         -> RnMG (IO ())
544 rnDump imp_decls decls
545         | opt_D_dump_rn_trace || 
546           opt_D_dump_rn_stats ||
547           opt_D_dump_rn 
548         = getRnStats imp_decls          `thenRn` \ stats_msg ->
549
550           returnRn (printErrs stats_msg >> 
551                     dumpIfSet opt_D_dump_rn "Renamer:" (vcat (map ppr decls)))
552
553         | otherwise = returnRn (return ())
554 \end{code}
555
556
557 %*********************************************************
558 %*                                                      *
559 \subsection{Statistics}
560 %*                                                      *
561 %*********************************************************
562
563 \begin{code}
564 getRnStats :: [RenamedHsDecl] -> RnMG SDoc
565 getRnStats imported_decls
566   = getIfacesRn                 `thenRn` \ ifaces ->
567     let
568         n_mods = length [() | (_, _, Just _) <- eltsFM (iImpModInfo ifaces)]
569
570         decls_read     = [decl | (_, avail, True, (_,decl)) <- nameEnvElts (iDecls ifaces),
571                                 -- Data, newtype, and class decls are in the decls_fm
572                                 -- under multiple names; the tycon/class, and each
573                                 -- constructor/class op too.
574                                 -- The 'True' selects just the 'main' decl
575                                  not (isLocallyDefined (availName avail))
576                              ]
577
578         (cd_rd, dd_rd, nd_rd, sd_rd, vd_rd,     _) = count_decls decls_read
579         (cd_sp, dd_sp, nd_sp, sd_sp, vd_sp, id_sp) = count_decls imported_decls
580
581         unslurped_insts       = iInsts ifaces
582         inst_decls_unslurped  = length (bagToList unslurped_insts)
583         inst_decls_read       = id_sp + inst_decls_unslurped
584
585         stats = vcat 
586                 [int n_mods <+> text "interfaces read",
587                  hsep [ int cd_sp, text "class decls imported, out of", 
588                         int cd_rd, text "read"],
589                  hsep [ int dd_sp, text "data decls imported, out of",  
590                         int dd_rd, text "read"],
591                  hsep [ int nd_sp, text "newtype decls imported, out of",  
592                         int nd_rd, text "read"],
593                  hsep [int sd_sp, text "type synonym decls imported, out of",  
594                         int sd_rd, text "read"],
595                  hsep [int vd_sp, text "value signatures imported, out of",  
596                         int vd_rd, text "read"],
597                  hsep [int id_sp, text "instance decls imported, out of",  
598                         int inst_decls_read, text "read"],
599                  text "cls dcls slurp" <+> fsep (map (ppr . tyClDeclName) 
600                                            [d | TyClD d <- imported_decls, isClassDecl d]),
601                  text "cls dcls read"  <+> fsep (map (ppr . tyClDeclName) 
602                                            [d | TyClD d <- decls_read, isClassDecl d])]
603     in
604     returnRn (hcat [text "Renamer stats: ", stats])
605
606 count_decls decls
607   = (class_decls, 
608      data_decls, 
609      newtype_decls,
610      syn_decls, 
611      val_decls, 
612      inst_decls)
613   where
614     tycl_decls = [d | TyClD d <- decls]
615     (class_decls, data_decls, newtype_decls, syn_decls) = countTyClDecls tycl_decls
616
617     val_decls     = length [() | SigD _   <- decls]
618     inst_decls    = length [() | InstD _  <- decls]
619 \end{code}    
620