2 % (c) The GRASP Project, Glasgow University, 1992-1998
4 \section[Rename]{Renaming and dependency analysis passes}
7 module Rename ( renameModule, closeIfaceDecls, checkOldIface ) where
9 #include "HsVersions.h"
12 import RdrHsSyn ( RdrNameHsModule, RdrNameHsDecl, RdrNameDeprecation,
13 RdrNameTyClDecl, RdrNameRuleDecl, RdrNameInstDecl
15 import RnHsSyn ( RenamedHsDecl, RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl,
17 instDeclFVs, tyClDeclFVs, ruleDeclFVs
20 import CmdLineOpts ( DynFlags, DynFlag(..) )
22 import RnNames ( getGlobalNames )
23 import RnSource ( rnSourceDecls, rnTyClDecl, rnIfaceRuleDecl, rnInstDecl )
24 import RnIfaces ( slurpImpDecls, mkImportInfo,
25 getInterfaceExports, closeDecls,
26 RecompileRequired, recompileRequired
28 import RnHiFiles ( findAndReadIface, removeContext, loadExports, loadFixDecls, loadDeprecs )
29 import RnEnv ( availName, availsToNameSet,
30 emptyAvailEnv, unitAvailEnv, availEnvElts, plusAvailEnv, groupAvails,
31 warnUnusedImports, warnUnusedLocalBinds, warnUnusedModules,
32 lookupOrigNames, lookupGlobalRn, newGlobalName
34 import Module ( Module, ModuleName, WhereFrom(..),
35 moduleNameUserString, moduleName,
38 import Name ( Name, isLocallyDefined, NamedThing(..), getSrcLoc,
39 nameOccName, nameModule,
40 mkNameEnv, nameEnvElts, extendNameEnv
42 import OccName ( occNameFlavour )
44 import TysWiredIn ( unitTyCon, intTyCon, boolTyCon )
45 import PrelNames ( mAIN_Name, pREL_MAIN_Name, pRELUDE_Name,
47 unpackCString_RDR, unpackCStringFoldr_RDR, unpackCStringUtf8_RDR,
50 import PrelInfo ( derivingOccurrences )
51 import Type ( funTyCon )
52 import ErrUtils ( dumpIfSet )
53 import Bag ( bagToList )
54 import FiniteMap ( FiniteMap, eltsFM, fmToList, emptyFM, lookupFM,
55 addToFM_C, elemFM, addToFM
57 import UniqFM ( lookupUFM )
58 import Maybes ( maybeToBool, catMaybes )
60 import IO ( openFile, IOMode(..) )
61 import HscTypes ( Finder, PersistentCompilerState, HomeIfaceTable, HomeSymbolTable,
62 ModIface(..), WhatsImported(..),
63 VersionInfo(..), ImportVersion, IfaceDecls(..),
64 GlobalRdrEnv, AvailEnv, Avails, GenAvailInfo(..), AvailInfo,
65 Provenance(..), ImportReason(..), initialVersionInfo,
66 Deprecations(..), lookupDeprec
68 import List ( partition, nub )
73 %*********************************************************
75 \subsection{The main function: rename}
77 %*********************************************************
80 renameModule :: DynFlags -> Finder
81 -> HomeIfaceTable -> HomeSymbolTable
82 -> PersistentCompilerState
83 -> Module -> RdrNameHsModule
84 -> IO (PersistentCompilerState, Maybe (ModIface, [RenamedHsDecl]))
85 -- Nothing => some error occurred in the renamer
87 renameModule dflags finder hit hst old_pcs this_module rdr_module
88 = -- Initialise the renamer monad
90 (new_pcs, errors_found, maybe_rn_stuff)
91 <- initRn dflags finder hit hst old_pcs this_module (rename this_module rdr_module) ;
93 -- Return results. No harm in updating the PCS
95 return (new_pcs, Nothing)
97 return (new_pcs, maybe_rn_stuff)
102 rename :: Module -> RdrNameHsModule -> RnMG (Maybe (ModIface, [RenamedHsDecl]))
103 rename this_module this_mod@(HsModule mod_name vers exports imports local_decls mod_deprec loc)
104 = -- FIND THE GLOBAL NAME ENVIRONMENT
105 getGlobalNames this_mod `thenRn` \ maybe_stuff ->
107 -- CHECK FOR EARLY EXIT
108 case maybe_stuff of {
109 Nothing -> -- Everything is up to date; no need to recompile further
110 rnDump [] [] `thenRn_`
113 Just (gbl_env, local_gbl_env, export_avails, global_avail_env) ->
115 -- DEAL WITH DEPRECATIONS
116 rnDeprecs local_gbl_env mod_deprec
117 [d | DeprecD d <- local_decls] `thenRn` \ my_deprecs ->
119 -- DEAL WITH LOCAL FIXITIES
120 fixitiesFromLocalDecls local_gbl_env local_decls `thenRn` \ local_fixity_env ->
123 initRnMS gbl_env local_fixity_env SourceMode (
124 rnSourceDecls local_decls
125 ) `thenRn` \ (rn_local_decls, source_fvs) ->
127 -- SLURP IN ALL THE NEEDED DECLARATIONS
128 implicitFVs mod_name rn_local_decls `thenRn` \ implicit_fvs ->
130 -- The export_fvs make the exported names look just as if they
131 -- occurred in the source program. For the reasoning, see the
132 -- comments with RnIfaces.getImportVersions.
133 -- We only need the 'parent name' of the avail;
134 -- that's enough to suck in the declaration.
135 export_fvs = mkNameSet (map availName export_avails)
136 real_source_fvs = source_fvs `plusFV` export_fvs
138 slurp_fvs = implicit_fvs `plusFV` real_source_fvs
139 -- It's important to do the "plus" this way round, so that
140 -- when compiling the prelude, locally-defined (), Bool, etc
141 -- override the implicit ones.
143 slurpImpDecls slurp_fvs `thenRn` \ rn_imp_decls ->
145 -- EXIT IF ERRORS FOUND
146 rnDump rn_imp_decls rn_local_decls `thenRn_`
147 checkErrsRn `thenRn` \ no_errs_so_far ->
148 if not no_errs_so_far then
149 -- Found errors already, so exit now
153 -- GENERATE THE VERSION/USAGE INFO
154 mkImportInfo mod_name imports `thenRn` \ my_usages ->
156 -- RETURN THE RENAMED MODULE
157 getNameSupplyRn `thenRn` \ name_supply ->
158 getIfacesRn `thenRn` \ ifaces ->
160 direct_import_mods :: [ModuleName]
161 direct_import_mods = nub [m | ImportDecl m _ _ _ _ _ <- imports]
163 -- We record fixities even for things that aren't exported,
164 -- so that we can change into the context of this moodule easily
165 fixities = mkNameEnv [ (name, fixity)
166 | FixitySig name fixity loc <- nameEnvElts local_fixity_env
170 -- Sort the exports to make them easier to compare for versions
171 my_exports = groupAvails export_avails
173 mod_iface = ModIface { mi_module = this_module,
174 mi_version = initialVersionInfo,
175 mi_orphan = any isOrphanDecl rn_local_decls,
176 mi_exports = my_exports,
177 mi_globals = gbl_env,
178 mi_usages = my_usages,
179 mi_fixities = fixities,
180 mi_deprecs = my_deprecs,
181 mi_decls = panic "mi_decls"
184 final_decls = rn_local_decls ++ rn_imp_decls
187 -- REPORT UNUSED NAMES, AND DEBUG DUMP
188 reportUnusedNames mod_name direct_import_mods
189 gbl_env global_avail_env
190 export_avails source_fvs
191 rn_imp_decls `thenRn_`
193 returnRn (Just (mod_iface, final_decls))
197 @implicitFVs@ forces the renamer to slurp in some things which aren't
198 mentioned explicitly, but which might be needed by the type checker.
201 implicitFVs mod_name decls
202 = lookupOrigNames implicit_occs `thenRn` \ implicit_names ->
203 returnRn (mkNameSet (map getName default_tycons) `plusFV`
206 -- Add occurrences for Int, and (), because they
207 -- are the types to which ambigious type variables may be defaulted by
208 -- the type checker; so they won't always appear explicitly.
209 -- [The () one is a GHC extension for defaulting CCall results.]
210 -- ALSO: funTyCon, since it occurs implicitly everywhere!
211 -- (we don't want to be bothered with making funTyCon a
212 -- free var at every function application!)
213 -- Double is dealt with separately in getGates
214 default_tycons = [unitTyCon, funTyCon, boolTyCon, intTyCon]
216 -- Add occurrences for IO or PrimIO
217 implicit_main | mod_name == mAIN_Name
218 || mod_name == pREL_MAIN_Name = [ioTyCon_RDR]
221 -- Now add extra "occurrences" for things that
222 -- the deriving mechanism, or defaulting, will later need in order to
224 implicit_occs = string_occs ++ foldr ((++) . get) implicit_main decls
226 -- Virtually every program has error messages in it somewhere
227 string_occs = [unpackCString_RDR, unpackCStringFoldr_RDR, unpackCStringUtf8_RDR,
230 get (TyClD (TyData _ _ _ _ _ _ (Just deriv_classes) _ _ _))
231 = concat (map get_deriv deriv_classes)
234 get_deriv cls = case lookupUFM derivingOccurrences cls of
240 isOrphanDecl (InstD (InstDecl inst_ty _ _ _ _))
241 = not (foldNameSet ((||) . isLocallyDefined) False (extractHsTyNames (removeContext inst_ty)))
242 -- The 'removeContext' is because of
243 -- instance Foo a => Baz T where ...
244 -- The decl is an orphan if Baz and T are both not locally defined,
245 -- even if Foo *is* locally defined
247 isOrphanDecl (RuleD (HsRule _ _ _ lhs _ _))
250 -- At the moment we just check for common LHS forms
251 -- Expand as necessary. Getting it wrong just means
252 -- more orphans than necessary
253 check (HsVar v) = not (isLocallyDefined v)
254 check (HsApp f a) = check f && check a
255 check (HsLit _) = False
256 check (HsOverLit _) = False
257 check (OpApp l o _ r) = check l && check o && check r
258 check (NegApp e _) = check e
259 check (HsPar e) = check e
260 check (SectionL e o) = check e && check o
261 check (SectionR o e) = check e && check o
263 check other = True -- Safe fall through
265 isOrphanDecl other = False
269 %*********************************************************
271 \subsection{Fixities}
273 %*********************************************************
276 fixitiesFromLocalDecls :: GlobalRdrEnv -> [RdrNameHsDecl] -> RnMG LocalFixityEnv
277 fixitiesFromLocalDecls gbl_env decls
278 = doptRn Opt_WarnUnusedBinds `thenRn` \ warn_unused ->
279 foldlRn (getFixities warn_unused) emptyNameEnv decls `thenRn` \ env ->
280 traceRn (text "fixity env" <+> vcat (map ppr (nameEnvElts env)))
284 getFixities :: Bool -> LocalFixityEnv -> RdrNameHsDecl -> RnMG LocalFixityEnv
285 getFixities warn_uu acc (FixD fix)
286 = fix_decl warn_uu acc fix
288 getFixities warn_uu acc (TyClD (ClassDecl _ _ _ _ sigs _ _ _ ))
289 = foldlRn (fix_decl warn_uu) acc [sig | FixSig sig <- sigs]
290 -- Get fixities from class decl sigs too.
291 getFixities warn_uu acc other_decl
294 fix_decl warn_uu acc sig@(FixitySig rdr_name fixity loc)
295 = -- Check for fixity decl for something not declared
297 lookupGlobalRn gbl_env rdr_name `thenRn` \ maybe_name ->
299 Nothing -> checkRn (not warn_uu) (unusedFixityDecl rdr_name fixity) `thenRn_`
304 -- Check for duplicate fixity decl
305 case lookupNameEnv acc name of {
306 Just (FixitySig _ _ loc') -> addErrRn (dupFixityDecl rdr_name loc loc')
307 `thenRn_` returnRn acc ;
309 Nothing -> returnRn (extendNameEnv acc name (FixitySig name fixity loc))
314 %*********************************************************
316 \subsection{Deprecations}
318 %*********************************************************
320 For deprecations, all we do is check that the names are in scope.
321 It's only imported deprecations, dealt with in RnIfaces, that we
322 gather them together.
325 rnDeprecs :: GlobalRdrEnv -> Maybe DeprecTxt
326 -> [RdrNameDeprecation] -> RnMG Deprecations
327 rnDeprecs gbl_env Nothing []
330 rnDeprecs gbl_env (Just txt) decls
331 = mapRn (addErrRn . badDeprec) decls `thenRn_`
332 returnRn (DeprecAll txt)
334 rnDeprecs gbl_env Nothing decls
335 = mapRn rn_deprec decls `thenRn` \ pairs ->
336 returnRn (DeprecSome (mkNameEnv (catMaybes pairs)))
338 rn_deprec (Deprecation rdr_name txt loc)
340 lookupGlobalRn gbl_env rdr_name `thenRn` \ maybe_name ->
342 Just n -> returnRn (Just (n,txt))
343 Nothing -> returnRn Nothing
347 %************************************************************************
349 \subsection{Grabbing the old interface file and checking versions}
351 %************************************************************************
354 checkOldIface :: DynFlags -> Finder
355 -> HomeIfaceTable -> HomeSymbolTable
356 -> PersistentCompilerState
358 -> Bool -- Source unchanged
359 -> Maybe ModIface -- Old interface from compilation manager, if any
360 -> IO (PersistentCompilerState, Bool, (RecompileRequired, Maybe ModIface))
361 -- True <=> errors happened
363 checkOldIface dflags finder hit hst pcs mod source_unchanged maybe_iface
364 = initRn dflags finder hit hst pcs mod $
366 -- Load the old interface file, if we havn't already got it
367 loadOldIface mod maybe_iface `thenRn` \ maybe_iface ->
370 recompileRequired mod source_unchanged maybe_iface `thenRn` \ recompile ->
372 returnRn (recompile, maybe_iface)
377 loadOldIface :: Module -> Maybe ModIface -> RnMG (Maybe ModIface)
378 loadOldIface mod (Just iface)
379 = returnRn (Just iface)
381 loadOldIface mod Nothing
382 = -- LOAD THE OLD INTERFACE FILE
383 findAndReadIface doc_str (moduleName mod) False {- Not hi-boot -} `thenRn` \ read_result ->
384 case read_result of {
385 Left err -> -- Old interface file not found, or garbled, so we'd better bail out
386 traceRn (vcat [ptext SLIT("No old interface file:"), err]) `thenRn_`
393 loadHomeDecls (pi_decls iface) `thenRn` \ decls ->
394 loadHomeRules (pi_rules iface) `thenRn` \ rules ->
395 loadHomeInsts (pi_insts iface) `thenRn` \ insts ->
396 returnRn (decls, rules, insts)
397 ) `thenRn` \ ((decls_vers, new_decls), (rule_vers, new_rules), new_insts) ->
399 mapRn loadHomeUsage (pi_usages iface) `thenRn` \ usages ->
400 loadExports (pi_exports iface) `thenRn` \ (export_vers, avails) ->
401 loadFixDecls mod (pi_fixity iface) `thenRn` \ fix_env ->
402 loadDeprecs mod (pi_deprecs iface) `thenRn` \ deprec_env ->
404 version = VersionInfo { vers_module = pi_vers iface,
405 vers_exports = export_vers,
406 vers_rules = rule_vers,
407 vers_decls = decls_vers }
409 decls = IfaceDecls { dcl_tycl = new_decls,
410 dcl_rules = new_rules,
411 dcl_insts = new_insts }
413 mod_iface = ModIface { mi_module = mod, mi_version = version,
414 mi_exports = avails, mi_orphan = pi_orphan iface,
415 mi_fixities = fix_env, mi_deprecs = deprec_env,
418 mi_globals = panic "No mi_globals in old interface"
421 returnRn (Just mod_iface)
426 doc_str = ptext SLIT("need usage info from") <+> ppr mod
430 loadHomeDecls :: [(Version, RdrNameTyClDecl)]
431 -> RnMS (NameEnv Version, [RenamedTyClDecl])
432 loadHomeDecls decls = foldlRn loadHomeDecl (emptyNameEnv, []) decls
434 loadHomeDecl :: (NameEnv Version, [RenamedTyClDecl])
435 -> (Version, RdrNameTyClDecl)
436 -> RnMS (NameEnv Version, [RenamedTyClDecl])
437 loadHomeDecl (version_map, decls) (version, decl)
438 = rnTyClDecl decl `thenRn` \ decl' ->
439 returnRn (extendNameEnv version_map (tyClDeclName decl') version, decl':decls)
442 loadHomeRules :: (Version, [RdrNameRuleDecl])
443 -> RnMS (Version, [RenamedRuleDecl])
444 loadHomeRules (version, rules)
445 = mapRn rnIfaceRuleDecl rules `thenRn` \ rules' ->
446 returnRn (version, rules')
449 loadHomeInsts :: [RdrNameInstDecl]
450 -> RnMS [RenamedInstDecl]
451 loadHomeInsts insts = mapRn rnInstDecl insts
454 loadHomeUsage :: ImportVersion OccName
455 -> RnMG (ImportVersion Name)
456 loadHomeUsage (mod_name, orphans, is_boot, whats_imported)
457 = rn_imps whats_imported `thenRn` \ whats_imported' ->
458 returnRn (mod_name, orphans, is_boot, whats_imported')
460 rn_imps NothingAtAll = returnRn NothingAtAll
461 rn_imps (Everything v) = returnRn (Everything v)
462 rn_imps (Specifically mv ev items rv) = mapRn rn_imp items `thenRn` \ items' ->
463 returnRn (Specifically mv ev items' rv)
464 rn_imp (occ,vers) = newGlobalName mod_name occ `thenRn` \ name ->
470 %*********************************************************
472 \subsection{Closing up the interface decls}
474 %*********************************************************
476 Suppose we discover we don't need to recompile. Then we start from the
477 IfaceDecls in the ModIface, and fluff them up by sucking in all the decls they need.
480 closeIfaceDecls :: DynFlags -> Finder
481 -> HomeIfaceTable -> HomeSymbolTable
482 -> PersistentCompilerState
483 -> ModIface -- Get the decls from here
484 -> IO (PersistentCompilerState, Bool, [RenamedHsDecl])
485 -- True <=> errors happened
486 closeIfaceDecls dflags finder hit hst pcs
487 mod_iface@(ModIface { mi_module = mod, mi_decls = iface_decls })
488 = initRn dflags finder hit hst pcs mod $
491 rule_decls = dcl_rules iface_decls
492 inst_decls = dcl_insts iface_decls
493 tycl_decls = dcl_tycl iface_decls
494 decls = map RuleD rule_decls ++
495 map InstD inst_decls ++
497 needed = unionManyNameSets (map ruleDeclFVs rule_decls) `unionNameSets`
498 unionManyNameSets (map instDeclFVs inst_decls) `unionNameSets`
499 unionManyNameSets (map tyClDeclFVs tycl_decls)
501 closeDecls decls needed
504 %*********************************************************
506 \subsection{Unused names}
508 %*********************************************************
511 reportUnusedNames :: ModuleName -> [ModuleName]
512 -> GlobalRdrEnv -> AvailEnv
513 -> Avails -> NameSet -> [RenamedHsDecl]
515 reportUnusedNames mod_name direct_import_mods
517 export_avails mentioned_names
519 = warnUnusedModules unused_imp_mods `thenRn_`
520 warnUnusedLocalBinds bad_locals `thenRn_`
521 warnUnusedImports bad_imp_names `thenRn_`
522 printMinimalImports mod_name minimal_imports `thenRn_`
523 warnDeprecations really_used_names `thenRn_`
527 used_names = mentioned_names `unionNameSets` availsToNameSet export_avails
529 -- Now, a use of C implies a use of T,
530 -- if C was brought into scope by T(..) or T(C)
531 really_used_names = used_names `unionNameSets`
532 mkNameSet [ parent_name
533 | sub_name <- nameSetToList used_names
535 -- Usually, every used name will appear in avail_env, but there
536 -- is one time when it doesn't: tuples and other built in syntax. When you
537 -- write (a,b) that gives rise to a *use* of "(,)", so that the
538 -- instances will get pulled in, but the tycon "(,)" isn't actually
539 -- in scope. Also, (-x) gives rise to an implicit use of 'negate';
540 -- similarly, 3.5 gives rise to an implcit use of :%
541 -- Hence the silent 'False' in all other cases
543 , Just parent_name <- [case lookupNameEnv avail_env sub_name of
544 Just (AvailTC n _) -> Just n
548 defined_names, defined_and_used, defined_but_not_used :: [(Name,Provenance)]
549 defined_names = concat (rdrEnvElts gbl_env)
550 (defined_and_used, defined_but_not_used) = partition used defined_names
551 used (name,_) = not (name `elemNameSet` really_used_names)
553 -- Filter out the ones only defined implicitly
555 bad_locals = [n | (n,LocalDef) <- defined_but_not_used]
557 bad_imp_names :: [(Name,Provenance)]
558 bad_imp_names = [(n,p) | (n,p@(NonLocalDef (UserImport mod _ True) _)) <- defined_but_not_used,
559 not (module_unused mod)]
561 -- inst_mods are directly-imported modules that
562 -- contain instance decl(s) that the renamer decided to suck in
563 -- It's not necessarily redundant to import such modules.
569 -- The import M() is not *necessarily* redundant, even if
570 -- we suck in no instance decls from M (e.g. it contains
571 -- no instance decls, or This contains no code). It may be
572 -- that we import M solely to ensure that M's orphan instance
573 -- decls (or those in its imports) are visible to people who
574 -- import This. Sigh.
575 -- There's really no good way to detect this, so the error message
576 -- in RnEnv.warnUnusedModules is weakened instead
577 inst_mods :: [ModuleName]
578 inst_mods = [m | InstD (InstDecl _ _ _ (Just dfun) _) <- imported_decls,
579 let m = moduleName (nameModule dfun),
580 m `elem` direct_import_mods
583 -- To figure out the minimal set of imports, start with the things
584 -- that are in scope (i.e. in gbl_env). Then just combine them
585 -- into a bunch of avails, so they are properly grouped
586 minimal_imports :: FiniteMap ModuleName AvailEnv
587 minimal_imports0 = emptyFM
588 minimal_imports1 = foldr add_name minimal_imports0 defined_and_used
589 minimal_imports = foldr add_inst_mod minimal_imports1 inst_mods
591 add_name (n,NonLocalDef (UserImport m _ _) _) acc = addToFM_C plusAvailEnv acc (moduleName (nameModule n))
592 (unitAvailEnv (mk_avail n))
593 add_name (n,other_prov) acc = acc
595 mk_avail n = case lookupNameEnv avail_env n of
596 Just (AvailTC m _) | n==m -> AvailTC n [n]
597 | otherwise -> AvailTC m [n,m]
598 Just avail -> Avail n
599 Nothing -> pprPanic "mk_avail" (ppr n)
602 | m `elemFM` acc = acc -- We import something already
603 | otherwise = addToFM acc m emptyAvailEnv
604 -- Add an empty collection of imports for a module
605 -- from which we have sucked only instance decls
607 -- unused_imp_mods are the directly-imported modules
608 -- that are not mentioned in minimal_imports
609 unused_imp_mods = [m | m <- direct_import_mods,
610 not (maybeToBool (lookupFM minimal_imports m)),
613 module_unused :: Module -> Bool
614 module_unused mod = moduleName mod `elem` unused_imp_mods
617 warnDeprecations used_names
618 = doptRn Opt_WarnDeprecations `thenRn` \ warn_drs ->
619 if not warn_drs then returnRn () else
621 getIfacesRn `thenRn` \ ifaces ->
622 getHomeIfaceTableRn `thenRn` \ hit ->
626 | n <- nameSetToList used_names,
627 Just txt <- [lookup_deprec hit pit n] ]
629 mapRn_ warnDeprec deprecs
632 lookup_deprec hit pit n
633 = case lookupModuleEnv hit mod of
634 Just iface -> lookupDeprec iface n
635 Nothing -> case lookupModuleEnv pit mod of
636 Just iface -> lookupDeprec iface n
637 Nothing -> pprPanic "warnDeprecations:" (ppr n)
641 -- ToDo: deal with original imports with 'qualified' and 'as M' clauses
642 printMinimalImports mod_name imps
643 = doptRn Opt_D_dump_minimal_imports `thenRn` \ dump_minimal ->
644 if not dump_minimal then returnRn () else
646 mapRn to_ies (fmToList imps) `thenRn` \ mod_ies ->
647 ioToRnM (do { h <- openFile filename WriteMode ;
648 printForUser h (vcat (map ppr_mod_ie mod_ies))
652 filename = moduleNameUserString mod_name ++ ".imports"
653 ppr_mod_ie (mod_name, ies)
654 | mod_name == pRELUDE_Name
657 = ptext SLIT("import") <+> ppr mod_name <>
658 parens (fsep (punctuate comma (map ppr ies)))
660 to_ies (mod, avail_env) = mapRn to_ie (availEnvElts avail_env) `thenRn` \ ies ->
663 to_ie :: AvailInfo -> RnMG (IE Name)
664 to_ie (Avail n) = returnRn (IEVar n)
665 to_ie (AvailTC n [m]) = ASSERT( n==m )
666 returnRn (IEThingAbs n)
668 = getInterfaceExports n_mod ImportBySystem `thenRn` \ (_, avails_by_module) ->
669 case [xs | (m,as) <- avails_by_module,
673 [xs] | all (`elem` ns) xs -> returnRn (IEThingAll n)
674 | otherwise -> returnRn (IEThingWith n (filter (/= n) ns))
675 other -> pprTrace "to_ie" (ppr n <+> ppr (nameModule n) <+> ppr other) $
678 n_mod = moduleName (nameModule n)
680 rnDump :: [RenamedHsDecl] -- Renamed imported decls
681 -> [RenamedHsDecl] -- Renamed local decls
683 rnDump imp_decls local_decls
684 = doptRn Opt_D_dump_rn_trace `thenRn` \ dump_rn_trace ->
685 doptRn Opt_D_dump_rn_stats `thenRn` \ dump_rn_stats ->
686 doptRn Opt_D_dump_rn `thenRn` \ dump_rn ->
687 getIfacesRn `thenRn` \ ifaces ->
689 ioToRnM (do { dumpIfSet (dump_rn_trace || dump_rn_stats || dump_rn)
691 (getRnStats imp_decls ifaces) ;
693 dumpIfSet dump_rn "Renamer:"
694 (vcat (map ppr (local_decls ++ imp_decls)))
701 %*********************************************************
703 \subsection{Statistics}
705 %*********************************************************
708 getRnStats :: [RenamedHsDecl] -> Ifaces -> SDoc
709 getRnStats imported_decls ifaces
710 = hcat [text "Renamer stats: ", stats]
712 n_mods = length [() | (_, _, True) <- eltsFM (iImpModInfo ifaces)]
714 decls_read = [decl | (avail, True, (_,decl)) <- nameEnvElts (iDecls ifaces),
715 -- Data, newtype, and class decls are in the decls_fm
716 -- under multiple names; the tycon/class, and each
717 -- constructor/class op too.
718 -- The 'True' selects just the 'main' decl
719 not (isLocallyDefined (availName avail))
722 (cd_rd, dd_rd, nd_rd, sd_rd, vd_rd) = countTyClDecls decls_read
723 (cd_sp, dd_sp, nd_sp, sd_sp, vd_sp, id_sp) = count_decls imported_decls
725 unslurped_insts = iInsts ifaces
726 inst_decls_unslurped = length (bagToList unslurped_insts)
727 inst_decls_read = id_sp + inst_decls_unslurped
730 [int n_mods <+> text "interfaces read",
731 hsep [ int cd_sp, text "class decls imported, out of",
732 int cd_rd, text "read"],
733 hsep [ int dd_sp, text "data decls imported, out of",
734 int dd_rd, text "read"],
735 hsep [ int nd_sp, text "newtype decls imported, out of",
736 int nd_rd, text "read"],
737 hsep [int sd_sp, text "type synonym decls imported, out of",
738 int sd_rd, text "read"],
739 hsep [int vd_sp, text "value signatures imported, out of",
740 int vd_rd, text "read"],
741 hsep [int id_sp, text "instance decls imported, out of",
742 int inst_decls_read, text "read"],
743 text "cls dcls slurp" <+> fsep (map (ppr . tyClDeclName)
744 [d | TyClD d <- imported_decls, isClassDecl d]),
745 text "cls dcls read" <+> fsep (map (ppr . tyClDeclName)
746 [d | d <- decls_read, isClassDecl d])]
756 tycl_decls = [d | TyClD d <- decls]
757 (class_decls, data_decls, newtype_decls, syn_decls, val_decls) = countTyClDecls tycl_decls
759 inst_decls = length [() | InstD _ <- decls]
763 %************************************************************************
765 \subsection{Errors and warnings}
767 %************************************************************************
770 warnDeprec :: (Name, DeprecTxt) -> RnM d ()
771 warnDeprec (name, txt)
772 = pushSrcLocRn (getSrcLoc name) $
774 sep [ text (occNameFlavour (nameOccName name)) <+> ppr name <+>
775 text "is deprecated:", nest 4 (ppr txt) ]
778 unusedFixityDecl rdr_name fixity
779 = hsep [ptext SLIT("Unused fixity declaration for"), quotes (ppr rdr_name)]
781 dupFixityDecl rdr_name loc1 loc2
782 = vcat [ptext SLIT("Multiple fixity declarations for") <+> quotes (ppr rdr_name),
783 ptext SLIT("at ") <+> ppr loc1,
784 ptext SLIT("and") <+> ppr loc2]
787 = sep [ptext SLIT("Illegal deprecation when whole module is deprecated"),