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