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, RdrNameImportDecl
15 import RnHsSyn ( RenamedHsDecl, RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl,
17 instDeclFVs, tyClDeclFVs, ruleDeclFVs
20 import CmdLineOpts ( DynFlags, DynFlag(..), dopt )
22 import RnNames ( getGlobalNames )
23 import RnSource ( rnSourceDecls, rnTyClDecl, rnIfaceRuleDecl, rnInstDecl )
24 import RnIfaces ( slurpImpDecls, mkImportInfo,
25 getInterfaceExports, closeDecls,
26 RecompileRequired, outOfDate, recompileRequired
28 import RnHiFiles ( readIface, removeContext,
29 loadExports, loadFixDecls, loadDeprecs )
30 import RnEnv ( availName,
31 emptyAvailEnv, unitAvailEnv, availEnvElts, plusAvailEnv, groupAvails,
32 warnUnusedImports, warnUnusedLocalBinds, warnUnusedModules,
33 lookupOrigNames, lookupGlobalRn, newGlobalName
35 import Module ( Module, ModuleName, WhereFrom(..),
36 moduleNameUserString, moduleName,
37 mkModuleInThisPackage, mkModuleName, moduleEnvElts
39 import Name ( Name, isLocallyDefined, NamedThing(..), getSrcLoc,
40 nameOccName, nameModule,
41 mkNameEnv, nameEnvElts, extendNameEnv
43 import RdrName ( elemRdrEnv )
44 import OccName ( occNameFlavour )
46 import TysWiredIn ( unitTyCon, intTyCon, boolTyCon )
47 import PrelNames ( mAIN_Name, pREL_MAIN_Name, pRELUDE_Name,
48 ioTyCon_RDR, main_RDR,
49 unpackCString_RDR, unpackCStringFoldr_RDR, unpackCStringUtf8_RDR,
52 import PrelInfo ( derivingOccurrences )
53 import Type ( funTyCon )
54 import ErrUtils ( dumpIfSet )
55 import Bag ( bagToList )
56 import FiniteMap ( FiniteMap, fmToList, emptyFM, lookupFM,
57 addToFM_C, elemFM, addToFM
59 import UniqFM ( lookupUFM )
60 import Maybes ( maybeToBool, catMaybes )
62 import IO ( openFile, IOMode(..) )
63 import HscTypes ( PersistentCompilerState, HomeIfaceTable, HomeSymbolTable,
64 ModIface(..), WhatsImported(..),
65 VersionInfo(..), ImportVersion, IfaceDecls(..),
66 GlobalRdrEnv, AvailEnv, GenAvailInfo(..), AvailInfo,
67 Provenance(..), ImportReason(..), initialVersionInfo,
68 Deprecations(..), lookupDeprec, lookupTable
70 import List ( partition, nub )
75 %*********************************************************
77 \subsection{The main function: rename}
79 %*********************************************************
82 renameModule :: DynFlags
83 -> HomeIfaceTable -> HomeSymbolTable
84 -> PersistentCompilerState
85 -> Module -> RdrNameHsModule
86 -> IO (PersistentCompilerState, Maybe (ModIface, [RenamedHsDecl]))
87 -- Nothing => some error occurred in the renamer
89 renameModule dflags hit hst old_pcs this_module rdr_module
90 = -- Initialise the renamer monad
92 (new_pcs, errors_found, maybe_rn_stuff)
93 <- initRn dflags hit hst old_pcs this_module (rename this_module rdr_module) ;
95 -- Return results. No harm in updating the PCS
97 return (new_pcs, Nothing)
99 return (new_pcs, maybe_rn_stuff)
104 rename :: Module -> RdrNameHsModule -> RnMG (Maybe (ModIface, [RenamedHsDecl]))
105 rename this_module contents@(HsModule _ _ _ imports local_decls mod_deprec loc)
108 -- FIND THE GLOBAL NAME ENVIRONMENT
109 getGlobalNames this_module contents `thenRn` \ (gbl_env, local_gbl_env,
110 export_avails, global_avail_env) ->
112 -- Exit if we've found any errors
113 checkErrsRn `thenRn` \ no_errs_so_far ->
114 if not no_errs_so_far then
115 -- Found errors already, so exit now
116 rnDump [] [] `thenRn_`
120 -- DEAL WITH DEPRECATIONS
121 rnDeprecs local_gbl_env mod_deprec
122 [d | DeprecD d <- local_decls] `thenRn` \ my_deprecs ->
124 -- DEAL WITH LOCAL FIXITIES
125 fixitiesFromLocalDecls local_gbl_env local_decls `thenRn` \ local_fixity_env ->
128 initRnMS gbl_env local_fixity_env SourceMode (
129 rnSourceDecls local_decls
130 ) `thenRn` \ (rn_local_decls, source_fvs) ->
132 -- CHECK THAT main IS DEFINED, IF REQUIRED
133 checkMain this_module local_gbl_env `thenRn_`
135 -- SLURP IN ALL THE NEEDED DECLARATIONS
136 implicitFVs mod_name rn_local_decls `thenRn` \ implicit_fvs ->
138 -- The export_fvs make the exported names look just as if they
139 -- occurred in the source program. For the reasoning, see the
140 -- comments with RnIfaces.getImportVersions.
141 -- We only need the 'parent name' of the avail;
142 -- that's enough to suck in the declaration.
143 export_fvs = mkNameSet (map availName export_avails)
144 real_source_fvs = source_fvs `plusFV` export_fvs
146 slurp_fvs = implicit_fvs `plusFV` real_source_fvs
147 -- It's important to do the "plus" this way round, so that
148 -- when compiling the prelude, locally-defined (), Bool, etc
149 -- override the implicit ones.
151 slurpImpDecls slurp_fvs `thenRn` \ rn_imp_decls ->
153 -- EXIT IF ERRORS FOUND
154 rnDump rn_imp_decls rn_local_decls `thenRn_`
155 checkErrsRn `thenRn` \ no_errs_so_far ->
156 if not no_errs_so_far then
157 -- Found errors already, so exit now
161 -- GENERATE THE VERSION/USAGE INFO
162 mkImportInfo mod_name imports `thenRn` \ my_usages ->
164 -- RETURN THE RENAMED MODULE
165 getNameSupplyRn `thenRn` \ name_supply ->
166 getIfacesRn `thenRn` \ ifaces ->
168 -- We record fixities even for things that aren't exported,
169 -- so that we can change into the context of this moodule easily
170 fixities = mkNameEnv [ (name, fixity)
171 | FixitySig name fixity loc <- nameEnvElts local_fixity_env
175 -- Sort the exports to make them easier to compare for versions
176 my_exports = groupAvails this_module export_avails
178 mod_iface = ModIface { mi_module = this_module,
179 mi_version = initialVersionInfo,
181 mi_orphan = any isOrphanDecl rn_local_decls,
182 mi_exports = my_exports,
183 mi_globals = gbl_env,
184 mi_usages = my_usages,
185 mi_fixities = fixities,
186 mi_deprecs = my_deprecs,
187 mi_decls = panic "mi_decls"
190 final_decls = rn_local_decls ++ rn_imp_decls
193 -- REPORT UNUSED NAMES, AND DEBUG DUMP
194 reportUnusedNames mod_iface imports global_avail_env
195 real_source_fvs rn_imp_decls `thenRn_`
197 returnRn (Just (mod_iface, final_decls))
199 mod_name = moduleName this_module
202 Checking that main is defined
205 checkMain :: Module -> GlobalRdrEnv -> RnMG ()
206 checkMain this_mod local_env
207 | moduleName this_mod == mAIN_Name
208 = checkRn (main_RDR `elemRdrEnv` local_env) noMainErr
213 @implicitFVs@ forces the renamer to slurp in some things which aren't
214 mentioned explicitly, but which might be needed by the type checker.
217 implicitFVs mod_name decls
218 = lookupOrigNames implicit_occs `thenRn` \ implicit_names ->
219 returnRn (mkNameSet (map getName default_tycons) `plusFV`
222 -- Add occurrences for Int, and (), because they
223 -- are the types to which ambigious type variables may be defaulted by
224 -- the type checker; so they won't always appear explicitly.
225 -- [The () one is a GHC extension for defaulting CCall results.]
226 -- ALSO: funTyCon, since it occurs implicitly everywhere!
227 -- (we don't want to be bothered with making funTyCon a
228 -- free var at every function application!)
229 -- Double is dealt with separately in getGates
230 default_tycons = [unitTyCon, funTyCon, boolTyCon, intTyCon]
232 -- Add occurrences for IO or PrimIO
233 implicit_main | mod_name == mAIN_Name
234 || mod_name == pREL_MAIN_Name = [ioTyCon_RDR]
237 -- Now add extra "occurrences" for things that
238 -- the deriving mechanism, or defaulting, will later need in order to
240 implicit_occs = string_occs ++ foldr ((++) . get) implicit_main decls
242 -- Virtually every program has error messages in it somewhere
243 string_occs = [unpackCString_RDR, unpackCStringFoldr_RDR, unpackCStringUtf8_RDR,
246 get (TyClD (TyData _ _ _ _ _ _ (Just deriv_classes) _ _ _))
247 = concat (map get_deriv deriv_classes)
250 get_deriv cls = case lookupUFM derivingOccurrences cls of
256 isOrphanDecl (InstD (InstDecl inst_ty _ _ _ _))
257 = not (foldNameSet ((||) . isLocallyDefined) False (extractHsTyNames (removeContext inst_ty)))
258 -- The 'removeContext' is because of
259 -- instance Foo a => Baz T where ...
260 -- The decl is an orphan if Baz and T are both not locally defined,
261 -- even if Foo *is* locally defined
263 isOrphanDecl (RuleD (HsRule _ _ _ lhs _ _))
266 -- At the moment we just check for common LHS forms
267 -- Expand as necessary. Getting it wrong just means
268 -- more orphans than necessary
269 check (HsVar v) = not (isLocallyDefined v)
270 check (HsApp f a) = check f && check a
271 check (HsLit _) = False
272 check (HsOverLit _) = False
273 check (OpApp l o _ r) = check l && check o && check r
274 check (NegApp e _) = check e
275 check (HsPar e) = check e
276 check (SectionL e o) = check e && check o
277 check (SectionR o e) = check e && check o
279 check other = True -- Safe fall through
281 isOrphanDecl other = False
285 %*********************************************************
287 \subsection{Fixities}
289 %*********************************************************
292 fixitiesFromLocalDecls :: GlobalRdrEnv -> [RdrNameHsDecl] -> RnMG LocalFixityEnv
293 fixitiesFromLocalDecls gbl_env decls
294 = doptRn Opt_WarnUnusedBinds `thenRn` \ warn_unused ->
295 foldlRn (getFixities warn_unused) emptyNameEnv decls `thenRn` \ env ->
296 traceRn (text "fixity env" <+> vcat (map ppr (nameEnvElts env)))
300 getFixities :: Bool -> LocalFixityEnv -> RdrNameHsDecl -> RnMG LocalFixityEnv
301 getFixities warn_uu acc (FixD fix)
302 = fix_decl warn_uu acc fix
304 getFixities warn_uu acc (TyClD (ClassDecl _ _ _ _ sigs _ _ _ ))
305 = foldlRn (fix_decl warn_uu) acc [sig | FixSig sig <- sigs]
306 -- Get fixities from class decl sigs too.
307 getFixities warn_uu acc other_decl
310 fix_decl warn_uu acc sig@(FixitySig rdr_name fixity loc)
311 = -- Check for fixity decl for something not declared
313 lookupGlobalRn gbl_env rdr_name `thenRn` \ maybe_name ->
315 Nothing -> checkRn (not warn_uu) (unusedFixityDecl rdr_name fixity) `thenRn_`
320 -- Check for duplicate fixity decl
321 case lookupNameEnv acc name of {
322 Just (FixitySig _ _ loc') -> addErrRn (dupFixityDecl rdr_name loc loc')
323 `thenRn_` returnRn acc ;
325 Nothing -> returnRn (extendNameEnv acc name (FixitySig name fixity loc))
330 %*********************************************************
332 \subsection{Deprecations}
334 %*********************************************************
336 For deprecations, all we do is check that the names are in scope.
337 It's only imported deprecations, dealt with in RnIfaces, that we
338 gather them together.
341 rnDeprecs :: GlobalRdrEnv -> Maybe DeprecTxt
342 -> [RdrNameDeprecation] -> RnMG Deprecations
343 rnDeprecs gbl_env Nothing []
346 rnDeprecs gbl_env (Just txt) decls
347 = mapRn (addErrRn . badDeprec) decls `thenRn_`
348 returnRn (DeprecAll txt)
350 rnDeprecs gbl_env Nothing decls
351 = mapRn rn_deprec decls `thenRn` \ pairs ->
352 returnRn (DeprecSome (mkNameEnv (catMaybes pairs)))
354 rn_deprec (Deprecation rdr_name txt loc)
356 lookupGlobalRn gbl_env rdr_name `thenRn` \ maybe_name ->
358 Just n -> returnRn (Just (n,(n,txt)))
359 Nothing -> returnRn Nothing
363 %************************************************************************
365 \subsection{Grabbing the old interface file and checking versions}
367 %************************************************************************
370 checkOldIface :: DynFlags
371 -> HomeIfaceTable -> HomeSymbolTable
372 -> PersistentCompilerState
374 -> Bool -- Source unchanged
375 -> Maybe ModIface -- Old interface from compilation manager, if any
376 -> IO (PersistentCompilerState, Bool, (RecompileRequired, Maybe ModIface))
377 -- True <=> errors happened
379 checkOldIface dflags hit hst pcs iface_path source_unchanged maybe_iface
380 = case maybe_iface of
381 Just old_iface -> -- Use the one we already have
382 startRn (mi_module old_iface) $
383 check_versions old_iface
384 Nothing -- try and read it from a file
385 -> do read_result <- readIface do_traceRn iface_path
387 Left err -> -- Old interface file not found, or garbled; give up
388 return (pcs, False, (outOfDate, Nothing))
390 -> startRn (pi_mod parsed_iface) $
391 loadOldIface parsed_iface `thenRn` \ m_iface ->
392 check_versions m_iface
394 check_versions :: ModIface -> RnMG (RecompileRequired, Maybe ModIface)
397 recompileRequired iface_path source_unchanged iface
398 `thenRn` \ recompile ->
399 returnRn (recompile, Just iface)
401 do_traceRn = dopt Opt_D_dump_rn_trace dflags
402 ioTraceRn sdoc = if do_traceRn then printErrs sdoc else return ()
403 startRn mod = initRn dflags hit hst pcs mod
406 I think the following function should now have a more representative name,
410 loadOldIface :: ParsedIface -> RnMG ModIface
412 loadOldIface parsed_iface
413 = let iface = parsed_iface
415 let mod = pi_mod iface
416 doc_str = ptext SLIT("need usage info from") <+> ppr mod
419 loadHomeDecls (pi_decls iface) `thenRn` \ decls ->
420 loadHomeRules (pi_rules iface) `thenRn` \ rules ->
421 loadHomeInsts (pi_insts iface) `thenRn` \ insts ->
422 returnRn (decls, rules, insts)
424 `thenRn` \ ((decls_vers, new_decls), (rule_vers, new_rules), new_insts) ->
426 mapRn loadHomeUsage (pi_usages iface) `thenRn` \ usages ->
427 loadExports (pi_exports iface) `thenRn` \ (export_vers, avails) ->
428 loadFixDecls mod (pi_fixity iface) `thenRn` \ fix_env ->
429 loadDeprecs mod (pi_deprecs iface) `thenRn` \ deprec_env ->
431 version = VersionInfo { vers_module = pi_vers iface,
432 vers_exports = export_vers,
433 vers_rules = rule_vers,
434 vers_decls = decls_vers }
436 decls = IfaceDecls { dcl_tycl = new_decls,
437 dcl_rules = new_rules,
438 dcl_insts = new_insts }
440 mod_iface = ModIface { mi_module = mod, mi_version = version,
441 mi_exports = avails, mi_usages = usages,
442 mi_boot = False, mi_orphan = pi_orphan iface,
443 mi_fixities = fix_env, mi_deprecs = deprec_env,
445 mi_globals = panic "No mi_globals in old interface"
452 loadHomeDecls :: [(Version, RdrNameTyClDecl)]
453 -> RnMS (NameEnv Version, [RenamedTyClDecl])
454 loadHomeDecls decls = foldlRn loadHomeDecl (emptyNameEnv, []) decls
456 loadHomeDecl :: (NameEnv Version, [RenamedTyClDecl])
457 -> (Version, RdrNameTyClDecl)
458 -> RnMS (NameEnv Version, [RenamedTyClDecl])
459 loadHomeDecl (version_map, decls) (version, decl)
460 = rnTyClDecl decl `thenRn` \ decl' ->
461 returnRn (extendNameEnv version_map (tyClDeclName decl') version, decl':decls)
464 loadHomeRules :: (Version, [RdrNameRuleDecl])
465 -> RnMS (Version, [RenamedRuleDecl])
466 loadHomeRules (version, rules)
467 = mapRn rnIfaceRuleDecl rules `thenRn` \ rules' ->
468 returnRn (version, rules')
471 loadHomeInsts :: [RdrNameInstDecl]
472 -> RnMS [RenamedInstDecl]
473 loadHomeInsts insts = mapRn rnInstDecl insts
476 loadHomeUsage :: ImportVersion OccName
477 -> RnMG (ImportVersion Name)
478 loadHomeUsage (mod_name, orphans, is_boot, whats_imported)
479 = rn_imps whats_imported `thenRn` \ whats_imported' ->
480 returnRn (mod_name, orphans, is_boot, whats_imported')
482 rn_imps NothingAtAll = returnRn NothingAtAll
483 rn_imps (Everything v) = returnRn (Everything v)
484 rn_imps (Specifically mv ev items rv) = mapRn rn_imp items `thenRn` \ items' ->
485 returnRn (Specifically mv ev items' rv)
486 rn_imp (occ,vers) = newGlobalName mod_name occ `thenRn` \ name ->
492 %*********************************************************
494 \subsection{Closing up the interface decls}
496 %*********************************************************
498 Suppose we discover we don't need to recompile. Then we start from the
499 IfaceDecls in the ModIface, and fluff them up by sucking in all the decls they need.
502 closeIfaceDecls :: DynFlags
503 -> HomeIfaceTable -> HomeSymbolTable
504 -> PersistentCompilerState
505 -> ModIface -- Get the decls from here
506 -> IO (PersistentCompilerState, Bool, [RenamedHsDecl])
507 -- True <=> errors happened
508 closeIfaceDecls dflags hit hst pcs
509 mod_iface@(ModIface { mi_module = mod, mi_decls = iface_decls })
510 = initRn dflags hit hst pcs mod $
513 rule_decls = dcl_rules iface_decls
514 inst_decls = dcl_insts iface_decls
515 tycl_decls = dcl_tycl iface_decls
516 decls = map RuleD rule_decls ++
517 map InstD inst_decls ++
519 needed = unionManyNameSets (map ruleDeclFVs rule_decls) `unionNameSets`
520 unionManyNameSets (map instDeclFVs inst_decls) `unionNameSets`
521 unionManyNameSets (map tyClDeclFVs tycl_decls)
523 closeDecls decls needed
526 %*********************************************************
528 \subsection{Unused names}
530 %*********************************************************
533 reportUnusedNames :: ModIface -> [RdrNameImportDecl]
538 reportUnusedNames my_mod_iface imports avail_env
539 used_names imported_decls
540 = warnUnusedModules unused_imp_mods `thenRn_`
541 warnUnusedLocalBinds bad_locals `thenRn_`
542 warnUnusedImports bad_imp_names `thenRn_`
543 printMinimalImports my_mod_iface minimal_imports `thenRn_`
544 warnDeprecations my_mod_iface really_used_names `thenRn_`
548 gbl_env = mi_globals my_mod_iface
550 -- Now, a use of C implies a use of T,
551 -- if C was brought into scope by T(..) or T(C)
552 really_used_names = used_names `unionNameSets`
553 mkNameSet [ parent_name
554 | sub_name <- nameSetToList used_names
556 -- Usually, every used name will appear in avail_env, but there
557 -- is one time when it doesn't: tuples and other built in syntax. When you
558 -- write (a,b) that gives rise to a *use* of "(,)", so that the
559 -- instances will get pulled in, but the tycon "(,)" isn't actually
560 -- in scope. Also, (-x) gives rise to an implicit use of 'negate';
561 -- similarly, 3.5 gives rise to an implcit use of :%
562 -- Hence the silent 'False' in all other cases
564 , Just parent_name <- [case lookupNameEnv avail_env sub_name of
565 Just (AvailTC n _) -> Just n
569 defined_names, defined_and_used, defined_but_not_used :: [(Name,Provenance)]
570 defined_names = concat (rdrEnvElts gbl_env)
571 (defined_and_used, defined_but_not_used) = partition used defined_names
572 used (name,_) = not (name `elemNameSet` really_used_names)
574 -- Filter out the ones only defined implicitly
576 bad_locals = [n | (n,LocalDef) <- defined_but_not_used]
578 bad_imp_names :: [(Name,Provenance)]
579 bad_imp_names = [(n,p) | (n,p@(NonLocalDef (UserImport mod _ True) _)) <- defined_but_not_used,
580 not (module_unused mod)]
582 -- inst_mods are directly-imported modules that
583 -- contain instance decl(s) that the renamer decided to suck in
584 -- It's not necessarily redundant to import such modules.
590 -- The import M() is not *necessarily* redundant, even if
591 -- we suck in no instance decls from M (e.g. it contains
592 -- no instance decls, or This contains no code). It may be
593 -- that we import M solely to ensure that M's orphan instance
594 -- decls (or those in its imports) are visible to people who
595 -- import This. Sigh.
596 -- There's really no good way to detect this, so the error message
597 -- in RnEnv.warnUnusedModules is weakened instead
598 inst_mods :: [ModuleName]
599 inst_mods = [m | InstD (InstDecl _ _ _ (Just dfun) _) <- imported_decls,
600 let m = moduleName (nameModule dfun),
601 m `elem` direct_import_mods
604 -- To figure out the minimal set of imports, start with the things
605 -- that are in scope (i.e. in gbl_env). Then just combine them
606 -- into a bunch of avails, so they are properly grouped
607 minimal_imports :: FiniteMap ModuleName AvailEnv
608 minimal_imports0 = emptyFM
609 minimal_imports1 = foldr add_name minimal_imports0 defined_and_used
610 minimal_imports = foldr add_inst_mod minimal_imports1 inst_mods
612 add_name (n,NonLocalDef (UserImport m _ _) _) acc = addToFM_C plusAvailEnv acc (moduleName (nameModule n))
613 (unitAvailEnv (mk_avail n))
614 add_name (n,other_prov) acc = acc
616 mk_avail n = case lookupNameEnv avail_env n of
617 Just (AvailTC m _) | n==m -> AvailTC n [n]
618 | otherwise -> AvailTC m [n,m]
619 Just avail -> Avail n
620 Nothing -> pprPanic "mk_avail" (ppr n)
623 | m `elemFM` acc = acc -- We import something already
624 | otherwise = addToFM acc m emptyAvailEnv
625 -- Add an empty collection of imports for a module
626 -- from which we have sucked only instance decls
628 direct_import_mods :: [ModuleName]
629 direct_import_mods = nub [m | ImportDecl m _ _ _ _ _ <- imports]
631 -- unused_imp_mods are the directly-imported modules
632 -- that are not mentioned in minimal_imports
633 unused_imp_mods = [m | m <- direct_import_mods,
634 not (maybeToBool (lookupFM minimal_imports m)),
637 module_unused :: Module -> Bool
638 module_unused mod = moduleName mod `elem` unused_imp_mods
641 warnDeprecations my_mod_iface used_names
642 = doptRn Opt_WarnDeprecations `thenRn` \ warn_drs ->
643 if not warn_drs then returnRn () else
645 getIfacesRn `thenRn` \ ifaces ->
646 getHomeIfaceTableRn `thenRn` \ hit ->
650 | n <- nameSetToList used_names,
651 Just txt <- [lookup_deprec hit pit n] ]
653 mapRn_ warnDeprec deprecs
656 my_deprecs = mi_deprecs my_mod_iface
657 lookup_deprec hit pit n
658 | isLocallyDefined n = lookupDeprec my_deprecs n
659 | otherwise = case lookupTable hit pit n of
660 Just iface -> lookupDeprec (mi_deprecs iface) n
661 Nothing -> pprPanic "warnDeprecations:" (ppr n)
663 -- ToDo: deal with original imports with 'qualified' and 'as M' clauses
664 printMinimalImports my_mod_iface imps
665 = doptRn Opt_D_dump_minimal_imports `thenRn` \ dump_minimal ->
666 if not dump_minimal then returnRn () else
668 mapRn to_ies (fmToList imps) `thenRn` \ mod_ies ->
669 ioToRnM (do { h <- openFile filename WriteMode ;
670 printForUser h (vcat (map ppr_mod_ie mod_ies))
674 filename = moduleNameUserString (moduleName (mi_module my_mod_iface))
676 ppr_mod_ie (mod_name, ies)
677 | mod_name == pRELUDE_Name
680 = ptext SLIT("import") <+> ppr mod_name <>
681 parens (fsep (punctuate comma (map ppr ies)))
683 to_ies (mod, avail_env) = mapRn to_ie (availEnvElts avail_env) `thenRn` \ ies ->
686 to_ie :: AvailInfo -> RnMG (IE Name)
687 to_ie (Avail n) = returnRn (IEVar n)
688 to_ie (AvailTC n [m]) = ASSERT( n==m )
689 returnRn (IEThingAbs n)
691 = getInterfaceExports n_mod ImportBySystem `thenRn` \ (_, avails_by_module) ->
692 case [xs | (m,as) <- avails_by_module,
696 [xs] | all (`elem` ns) xs -> returnRn (IEThingAll n)
697 | otherwise -> returnRn (IEThingWith n (filter (/= n) ns))
698 other -> pprTrace "to_ie" (ppr n <+> ppr (nameModule n) <+> ppr other) $
701 n_mod = moduleName (nameModule n)
703 rnDump :: [RenamedHsDecl] -- Renamed imported decls
704 -> [RenamedHsDecl] -- Renamed local decls
706 rnDump imp_decls local_decls
707 = doptRn Opt_D_dump_rn_trace `thenRn` \ dump_rn_trace ->
708 doptRn Opt_D_dump_rn_stats `thenRn` \ dump_rn_stats ->
709 doptRn Opt_D_dump_rn `thenRn` \ dump_rn ->
710 getIfacesRn `thenRn` \ ifaces ->
712 ioToRnM (do { dumpIfSet (dump_rn_trace || dump_rn_stats || dump_rn)
714 (getRnStats imp_decls ifaces) ;
716 dumpIfSet dump_rn "Renamer:"
717 (vcat (map ppr (local_decls ++ imp_decls)))
724 %*********************************************************
726 \subsection{Statistics}
728 %*********************************************************
731 getRnStats :: [RenamedHsDecl] -> Ifaces -> SDoc
732 getRnStats imported_decls ifaces
733 = hcat [text "Renamer stats: ", stats]
735 n_mods = length [() | _ <- moduleEnvElts (iPIT ifaces)]
736 -- This is really only right for a one-shot compile
738 decls_read = [decl | (avail, True, (_,decl)) <- nameEnvElts (iDecls ifaces),
739 -- Data, newtype, and class decls are in the decls_fm
740 -- under multiple names; the tycon/class, and each
741 -- constructor/class op too.
742 -- The 'True' selects just the 'main' decl
743 not (isLocallyDefined (availName avail))
746 (cd_rd, dd_rd, nd_rd, sd_rd, vd_rd) = countTyClDecls decls_read
747 (cd_sp, dd_sp, nd_sp, sd_sp, vd_sp, id_sp) = count_decls imported_decls
749 unslurped_insts = iInsts ifaces
750 inst_decls_unslurped = length (bagToList unslurped_insts)
751 inst_decls_read = id_sp + inst_decls_unslurped
754 [int n_mods <+> text "interfaces read",
755 hsep [ int cd_sp, text "class decls imported, out of",
756 int cd_rd, text "read"],
757 hsep [ int dd_sp, text "data decls imported, out of",
758 int dd_rd, text "read"],
759 hsep [ int nd_sp, text "newtype decls imported, out of",
760 int nd_rd, text "read"],
761 hsep [int sd_sp, text "type synonym decls imported, out of",
762 int sd_rd, text "read"],
763 hsep [int vd_sp, text "value signatures imported, out of",
764 int vd_rd, text "read"],
765 hsep [int id_sp, text "instance decls imported, out of",
766 int inst_decls_read, text "read"],
767 text "cls dcls slurp" <+> fsep (map (ppr . tyClDeclName)
768 [d | TyClD d <- imported_decls, isClassDecl d]),
769 text "cls dcls read" <+> fsep (map (ppr . tyClDeclName)
770 [d | d <- decls_read, isClassDecl d])]
780 tycl_decls = [d | TyClD d <- decls]
781 (class_decls, data_decls, newtype_decls, syn_decls, val_decls) = countTyClDecls tycl_decls
783 inst_decls = length [() | InstD _ <- decls]
787 %************************************************************************
789 \subsection{Errors and warnings}
791 %************************************************************************
794 warnDeprec :: (Name, DeprecTxt) -> RnM d ()
795 warnDeprec (name, txt)
796 = pushSrcLocRn (getSrcLoc name) $
798 sep [ text (occNameFlavour (nameOccName name)) <+> ppr name <+>
799 text "is deprecated:", nest 4 (ppr txt) ]
802 unusedFixityDecl rdr_name fixity
803 = hsep [ptext SLIT("Unused fixity declaration for"), quotes (ppr rdr_name)]
805 dupFixityDecl rdr_name loc1 loc2
806 = vcat [ptext SLIT("Multiple fixity declarations for") <+> quotes (ppr rdr_name),
807 ptext SLIT("at ") <+> ppr loc1,
808 ptext SLIT("and") <+> ppr loc2]
811 = sep [ptext SLIT("Illegal deprecation when whole module is deprecated"),
815 = hsep [ptext SLIT("Module"), quotes (ppr mAIN_Name),
816 ptext SLIT("must include a definition for"), quotes (ptext SLIT("main"))]