2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnIfaces]{Cacheing and Renaming of Interfaces}
13 slurpImpDecls, closeDecls,
15 RecompileRequired, outOfDate, upToDate, recompileRequired
19 #include "HsVersions.h"
21 import CmdLineOpts ( opt_IgnoreIfacePragmas, opt_NoPruneDecls )
23 import HsSyn ( HsDecl(..), Sig(..), TyClDecl(..), ConDecl(..), ConDetails(..),
24 InstDecl(..), HsType(..), hsTyVarNames, getBangType
26 import HsImpExp ( ImportDecl(..) )
27 import RdrHsSyn ( RdrNameTyClDecl, RdrNameInstDecl, RdrNameRuleDecl )
28 import RnHsSyn ( RenamedHsDecl, RenamedTyClDecl,
29 extractHsTyNames, extractHsCtxtTyNames,
30 tyClDeclFVs, ruleDeclFVs, instDeclFVs
32 import RnHiFiles ( tryLoadInterface, loadHomeInterface, loadInterface,
35 import RnSource ( rnTyClDecl, rnInstDecl, rnIfaceRuleDecl )
39 import Type ( namesOfType )
40 import TyCon ( isSynTyCon, getSynTyConDefn )
41 import Name ( Name {-instance NamedThing-}, nameOccName,
42 nameModule, isLocalName, nameUnique,
45 import Name ( elemNameEnv, delFromNameEnv )
46 import Module ( Module, ModuleEnv,
47 moduleName, isHomeModule,
48 ModuleName, WhereFrom(..),
50 extendModuleEnv_C, foldModuleEnv, lookupModuleEnv,
51 elemModuleSet, extendModuleSet
54 import PrelInfo ( wiredInThingEnv, fractionalClassKeys )
55 import TysWiredIn ( doubleTyCon )
56 import Maybes ( orElse )
60 import Util ( sortLt )
64 %*********************************************************
66 \subsection{Getting what a module exports}
68 %*********************************************************
70 @getInterfaceExports@ is called only for directly-imported modules.
73 getInterfaceExports :: ModuleName -> WhereFrom -> RnMG (Module, [(ModuleName,Avails)])
74 getInterfaceExports mod_name from
75 = loadInterface doc_str mod_name from `thenRn` \ iface ->
76 returnRn (mi_module iface, mi_exports iface)
78 doc_str = sep [ppr mod_name, ptext SLIT("is directly imported")]
82 %*********************************************************
84 \subsection{Keeping track of what we've slurped, and version numbers}
86 %*********************************************************
88 mkImportInof figures out what the ``usage information'' for this
89 moudule is; that is, what it must record in its interface file as the
92 We produce a line for every module B below the module, A, currently being
95 to record the fact that A does import B indireclty. This is used to decide
96 to look to look for B.hi rather than B.hi-boot when compiling a module that
97 imports A. This line says that A imports B, but uses nothing in it.
98 So we'll get an early bale-out when compiling A if B's version changes.
101 mkImportInfo :: ModuleName -- Name of this module
102 -> [ImportDecl n] -- The import decls
103 -> RnMG [ImportVersion Name]
105 mkImportInfo this_mod imports
106 = getIfacesRn `thenRn` \ ifaces ->
107 getHomeIfaceTableRn `thenRn` \ hit ->
109 (imp_pkg_mods, imp_home_names) = iVSlurp ifaces
112 import_all_mods :: [ModuleName]
113 -- Modules where we imported all the names
114 -- (apart from hiding some, perhaps)
115 import_all_mods = [ m | ImportDecl m _ _ _ imp_list _ <- imports,
116 import_all imp_list ]
118 import_all (Just (False, _)) = False -- Imports are specified explicitly
119 import_all other = True -- Everything is imported
121 -- mv_map groups together all the things imported and used
122 -- from a particular module in this package
123 -- We use a finite map because we want the domain
124 mv_map :: ModuleEnv [Name]
125 mv_map = foldNameSet add_mv emptyModuleEnv imp_home_names
126 add_mv name mv_map = extendModuleEnv_C add_item mv_map mod [name]
128 mod = nameModule name
129 add_item names _ = name:names
131 -- In our usage list we record
132 -- a) Specifically: Detailed version info for imports from modules in this package
133 -- Gotten from iVSlurp plus import_all_mods
135 -- b) Everything: Just the module version for imports from modules in other packages
136 -- Gotten from iVSlurp plus import_all_mods
138 -- c) NothingAtAll: The name only of modules, Baz, in this package that are 'below' us,
139 -- but which we didn't need at all (this is needed only to decide whether
140 -- to open Baz.hi or Baz.hi-boot higher up the tree).
141 -- This happens when a module, Foo, that we explicitly imported has
142 -- 'import Baz' in its interface file, recording that Baz is below
143 -- Foo in the module dependency hierarchy. We want to propagate this info.
144 -- These modules are in a combination of HIT/PIT and iImpModInfo
146 -- d) NothingAtAll: The name only of all orphan modules we know of (this is needed
147 -- so that anyone who imports us can find the orphan modules)
148 -- These modules are in a combination of HIT/PIT and iImpModInfo
150 import_info0 = foldModuleEnv mk_imp_info [] pit
151 import_info1 = foldModuleEnv mk_imp_info import_info0 hit
152 import_info = [ (mod_name, orphans, is_boot, NothingAtAll)
153 | (mod_name, (orphans, is_boot)) <- fmToList (iImpModInfo ifaces) ] ++
156 mk_imp_info :: ModIface -> [ImportVersion Name] -> [ImportVersion Name]
157 mk_imp_info iface so_far
159 | Just ns <- lookupModuleEnv mv_map mod -- Case (a)
160 = go_for_it (Specifically mod_vers maybe_export_vers
161 (mk_import_items ns) rules_vers)
163 | mod `elemModuleSet` imp_pkg_mods -- Case (b)
164 = go_for_it (Everything mod_vers)
166 | import_all_mod -- Case (a) and (b); the import-all part
167 = if is_home_pkg_mod then
168 go_for_it (Specifically mod_vers (Just export_vers) [] rules_vers)
170 go_for_it (Everything mod_vers)
172 | is_home_pkg_mod || has_orphans -- Case (c) or (d)
173 = go_for_it NothingAtAll
177 go_for_it exports = (mod_name, has_orphans, mi_boot iface, exports) : so_far
179 mod = mi_module iface
180 mod_name = moduleName mod
181 is_home_pkg_mod = isHomeModule mod
182 version_info = mi_version iface
183 version_env = vers_decls version_info
184 mod_vers = vers_module version_info
185 rules_vers = vers_rules version_info
186 export_vers = vers_exports version_info
187 import_all_mod = mod_name `elem` import_all_mods
188 has_orphans = mi_orphan iface
190 -- The sort is to put them into canonical order
191 mk_import_items ns = [(n,v) | n <- sortLt lt_occ ns,
192 let v = lookupNameEnv version_env n `orElse`
193 pprPanic "mk_whats_imported" (ppr n)
196 lt_occ n1 n2 = nameOccName n1 < nameOccName n2
198 maybe_export_vers | import_all_mod = Just (vers_exports version_info)
199 | otherwise = Nothing
204 %*********************************************************
206 \subsection{Slurping declarations}
208 %*********************************************************
211 -------------------------------------------------------
212 slurpImpDecls source_fvs
213 = traceRn (text "slurpImp" <+> fsep (map ppr (nameSetToList source_fvs))) `thenRn_`
215 -- The current slurped-set records all local things
216 slurpSourceRefs source_fvs `thenRn` \ (decls, needed) ->
218 -- Then get everything else
219 closeDecls decls needed
222 -------------------------------------------------------
223 slurpSourceRefs :: FreeVars -- Variables referenced in source
224 -> RnMG ([RenamedHsDecl],
225 FreeVars) -- Un-satisfied needs
226 -- The declaration (and hence home module) of each gate has
227 -- already been loaded
229 slurpSourceRefs source_fvs
230 = go_outer [] -- Accumulating decls
231 emptyFVs -- Unsatisfied needs
232 emptyFVs -- Accumulating gates
233 (nameSetToList source_fvs) -- Things whose defn hasn't been loaded yet
235 -- The outer loop repeatedly slurps the decls for the current gates
236 -- and the instance decls
238 -- The outer loop is needed because consider
240 go_outer decls fvs all_gates []
241 = returnRn (decls, fvs)
243 go_outer decls fvs all_gates refs -- refs are not necessarily slurped yet
244 = traceRn (text "go_outer" <+> ppr refs) `thenRn_`
245 foldlRn go_inner (decls, fvs, emptyFVs) refs `thenRn` \ (decls1, fvs1, gates1) ->
246 getImportedInstDecls (all_gates `plusFV` gates1) `thenRn` \ inst_decls ->
247 rnIfaceInstDecls decls1 fvs1 gates1 inst_decls `thenRn` \ (decls2, fvs2, gates2) ->
248 go_outer decls2 fvs2 (all_gates `plusFV` gates2)
249 (nameSetToList (gates2 `minusNameSet` all_gates))
250 -- Knock out the all_gates because even if we don't slurp any new
251 -- decls we can get some apparently-new gates from wired-in names
253 go_inner (decls, fvs, gates) wanted_name
254 = importDecl wanted_name `thenRn` \ import_result ->
255 case import_result of
256 AlreadySlurped -> returnRn (decls, fvs, gates)
257 InTypeEnv ty_thing -> returnRn (decls, fvs, gates `plusFV` getWiredInGates ty_thing)
259 HereItIs decl -> rnIfaceTyClDecl decl `thenRn` \ (new_decl, fvs1) ->
260 returnRn (TyClD new_decl : decls,
262 gates `plusFV` getGates source_fvs new_decl)
267 -------------------------------------------------------
268 -- closeDecls keeps going until the free-var set is empty
269 closeDecls decls needed
270 | not (isEmptyFVs needed)
271 = slurpDecls decls needed `thenRn` \ (decls1, needed1) ->
272 closeDecls decls1 needed1
275 = getImportedRules `thenRn` \ rule_decls ->
277 [] -> returnRn decls -- No new rules, so we are done
278 other -> rnIfaceDecls rnIfaceRuleDecl rule_decls `thenRn` \ rule_decls' ->
280 rule_fvs = plusFVs (map ruleDeclFVs rule_decls')
282 traceRn (text "closeRules" <+> ppr rule_decls' $$ fsep (map ppr (nameSetToList rule_fvs))) `thenRn_`
283 closeDecls (map RuleD rule_decls' ++ decls) rule_fvs
287 -------------------------------------------------------
288 -- Augment decls with any decls needed by needed.
289 -- Return also free vars of the new decls (only)
290 slurpDecls decls needed
291 = go decls emptyFVs (nameSetToList needed)
293 go decls fvs [] = returnRn (decls, fvs)
294 go decls fvs (ref:refs) = slurpDecl decls fvs ref `thenRn` \ (decls1, fvs1) ->
297 -------------------------------------------------------
298 slurpDecl decls fvs wanted_name
299 = importDecl wanted_name `thenRn` \ import_result ->
300 case import_result of
301 -- Found a declaration... rename it
302 HereItIs decl -> rnIfaceTyClDecl decl `thenRn` \ (new_decl, fvs1) ->
303 returnRn (TyClD new_decl:decls, fvs1 `plusFV` fvs)
305 -- No declaration... (wired in thing, or deferred, or already slurped)
306 other -> returnRn (decls, fvs)
309 -------------------------------------------------------
310 rnIfaceDecls rn decls = mapRn (rnIfaceDecl rn) decls
311 rnIfaceDecl rn (mod, decl) = initIfaceRnMS mod (rn decl)
313 rnIfaceInstDecls decls fvs gates inst_decls
314 = rnIfaceDecls rnInstDecl inst_decls `thenRn` \ inst_decls' ->
315 returnRn (map InstD inst_decls' ++ decls,
316 fvs `plusFV` plusFVs (map instDeclFVs inst_decls'),
317 gates `plusFV` plusFVs (map getInstDeclGates inst_decls'))
319 rnIfaceTyClDecl (mod, decl) = initIfaceRnMS mod (rnTyClDecl decl) `thenRn` \ decl' ->
320 returnRn (decl', tyClDeclFVs decl')
325 recordSlurp ifaces@(Ifaces { iDecls = (decls_map, n_slurped),
326 iSlurp = slurped_names,
327 iVSlurp = (imp_mods, imp_names) })
329 = ASSERT2( not (isLocalName (availName avail)), ppr avail )
330 ifaces { iDecls = (decls_map', n_slurped+1),
331 iSlurp = new_slurped_names,
332 iVSlurp = new_vslurp }
334 decls_map' = foldl delFromNameEnv decls_map (availNames avail)
335 main_name = availName avail
336 mod = nameModule main_name
337 new_slurped_names = addAvailToNameSet slurped_names avail
338 new_vslurp | isHomeModule mod = (imp_mods, addOneToNameSet imp_names main_name)
339 | otherwise = (extendModuleSet imp_mods mod, imp_names)
341 recordLocalSlurps new_names
342 = getIfacesRn `thenRn` \ ifaces ->
343 setIfacesRn (ifaces { iSlurp = iSlurp ifaces `unionNameSets` new_names })
348 %*********************************************************
350 \subsection{Extracting the `gates'}
352 %*********************************************************
356 We want to avoid sucking in too many instance declarations.
357 An instance decl is only useful if the types and classes mentioned in
358 its 'head' are all available in the program being compiled. E.g.
360 instance (..) => C (T1 a) (T2 b) where ...
362 is only useful if C, T1 and T2 are all "available". So we keep
363 instance decls that have been parsed from .hi files, but not yet
364 slurped in, in a pool called the 'gated instance pool'.
365 Each has its set of 'gates': {C, T1, T2} in the above example.
367 More precisely, the gates of a module are the types and classes
368 that are mentioned in:
371 b) the type of an Id that's mentioned in the source code
372 [includes constructors and selectors]
373 c) the RHS of a type synonym that is a gate
374 d) the superclasses of a class that is a gate
375 e) the context of an instance decl that is slurped in
377 We slurp in an instance decl from the gated instance pool iff
379 all its gates are either in the gates of the module,
380 or are a previously-loaded class.
382 The latter constraint is because there might have been an instance
383 decl slurped in during an earlier compilation, like this:
385 instance Foo a => Baz (Maybe a) where ...
387 In the module being compiled we might need (Baz (Maybe T)), where T
388 is defined in this module, and hence we need (Foo T). So @Foo@ becomes
389 a gate. But there's no way to 'see' that, so we simply treat all
390 previously-loaded classes as gates.
392 Consructors and class operations
393 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
394 When we import a declaration like
396 data T = T1 Wibble | T2 Wobble
398 we don't want to treat @Wibble@ and @Wobble@ as gates {\em unless}
399 @T1@, @T2@ respectively are mentioned by the user program. If only
400 @T@ is mentioned we want only @T@ to be a gate; that way we don't suck
401 in useless instance decls for (say) @Eq Wibble@, when they can't
404 And that's just what (b) says: we only treat T1's type as a gate if
405 T1 is mentioned. getGates, which deals with decls we are slurping in,
406 has to be a bit careful, because a mention of T1 will slurp in T's whole
409 -----------------------------
410 @getGates@ takes a newly imported (and renamed) decl, and the free
411 vars of the source program, and extracts from the decl the gate names.
414 getGates :: FreeVars -- Things mentioned in the source program
418 getGates source_fvs decl
419 = get_gates (\n -> n `elemNameSet` source_fvs) decl
421 get_gates is_used (IfaceSig {tcdType = ty}) = extractHsTyNames ty
423 get_gates is_used (ClassDecl { tcdCtxt = ctxt, tcdName = cls, tcdTyVars = tvs, tcdSigs = sigs})
424 = (delListFromNameSet (foldr (plusFV . get) (extractHsCtxtTyNames ctxt) sigs)
426 `addOneToNameSet` cls)
427 `plusFV` maybe_double
429 get (ClassOpSig n _ ty _)
430 | is_used n = extractHsTyNames ty
431 | otherwise = emptyFVs
433 -- If we load any numeric class that doesn't have
434 -- Int as an instance, add Double to the gates.
435 -- This takes account of the fact that Double might be needed for
436 -- defaulting, but we don't want to load Double (and all its baggage)
437 -- if the more exotic classes aren't used at all.
438 maybe_double | nameUnique cls `elem` fractionalClassKeys
439 = unitFV (getName doubleTyCon)
443 get_gates is_used (TySynonym {tcdTyVars = tvs, tcdSynRhs = ty})
444 = delListFromNameSet (extractHsTyNames ty) (hsTyVarNames tvs)
445 -- A type synonym type constructor isn't a "gate" for instance decls
447 get_gates is_used (TyData {tcdCtxt = ctxt, tcdName = tycon, tcdTyVars = tvs, tcdCons = cons})
448 = delListFromNameSet (foldr (plusFV . get) (extractHsCtxtTyNames ctxt) cons)
450 `addOneToNameSet` tycon
452 get (ConDecl n _ tvs ctxt details _)
454 -- If the constructor is method, get fvs from all its fields
455 = delListFromNameSet (get_details details `plusFV`
456 extractHsCtxtTyNames ctxt)
458 get (ConDecl n _ tvs ctxt (RecCon fields) _)
459 -- Even if the constructor isn't mentioned, the fields
460 -- might be, as selectors. They can't mention existentially
461 -- bound tyvars (typechecker checks for that) so no need for
462 -- the deleteListFromNameSet part
463 = foldr (plusFV . get_field) emptyFVs fields
465 get other_con = emptyFVs
467 get_details (VanillaCon tys) = plusFVs (map get_bang tys)
468 get_details (InfixCon t1 t2) = get_bang t1 `plusFV` get_bang t2
469 get_details (RecCon fields) = plusFVs [get_bang t | (_, t) <- fields]
471 get_field (fs,t) | any is_used fs = get_bang t
472 | otherwise = emptyFVs
474 get_bang bty = extractHsTyNames (getBangType bty)
477 @getWiredInGates@ is just like @getGates@, but it sees a previously-loaded
478 thing rather than a declaration.
481 getWiredInGates :: TyThing -> FreeVars
482 -- The TyThing is one that we already have in our type environment, either
483 -- a) because the TyCon or Id is wired in, or
484 -- b) from a previous compile
485 -- Either way, we might have instance decls in the (persistent) collection
486 -- of parsed-but-not-slurped instance decls that should be slurped in.
487 -- This might be the first module that mentions both the type and the class
488 -- for that instance decl, even though both the type and the class were
489 -- mentioned in other modules, and hence are in the type environment
491 getWiredInGates (AnId the_id) = namesOfType (idType the_id)
492 getWiredInGates (AClass cl) = emptyFVs -- The superclasses must also be previously
493 -- loaded, and hence are automatically gates
494 getWiredInGates (ATyCon tc)
495 | isSynTyCon tc = delListFromNameSet (namesOfType ty) (map getName tyvars)
496 | otherwise = unitFV (getName tc)
498 (tyvars,ty) = getSynTyConDefn tc
500 getInstDeclGates (InstDecl inst_ty _ _ _ _) = extractHsTyNames inst_ty
504 getImportedInstDecls :: NameSet -> RnMG [(Module,RdrNameInstDecl)]
505 getImportedInstDecls gates
506 = -- First, load any orphan-instance modules that aren't aready loaded
507 -- Orphan-instance modules are recorded in the module dependecnies
508 getIfacesRn `thenRn` \ ifaces ->
511 [mod | (mod, (True, _)) <- fmToList (iImpModInfo ifaces)]
513 loadOrphanModules orphan_mods `thenRn_`
515 -- Now we're ready to grab the instance declarations
516 -- Find the un-gated ones and return them,
517 -- removing them from the bag kept in Ifaces
518 getIfacesRn `thenRn` \ ifaces ->
519 getTypeEnvRn `thenRn` \ lookup ->
521 (decls, new_insts) = selectGated gates lookup (iInsts ifaces)
523 setIfacesRn (ifaces { iInsts = new_insts }) `thenRn_`
525 traceRn (sep [text "getImportedInstDecls:",
526 nest 4 (fsep (map ppr gate_list)),
527 text "Slurped" <+> int (length decls) <+> text "instance declarations",
528 nest 4 (vcat (map ppr_brief_inst_decl decls))]) `thenRn_`
531 gate_list = nameSetToList gates
533 ppr_brief_inst_decl (mod, InstDecl inst_ty _ _ _ _)
535 HsForAllTy _ _ tau -> ppr tau
538 getImportedRules :: RnMG [(Module,RdrNameRuleDecl)]
540 | opt_IgnoreIfacePragmas = returnRn []
542 = getIfacesRn `thenRn` \ ifaces ->
543 getTypeEnvRn `thenRn` \ lookup ->
545 gates = iSlurp ifaces -- Anything at all that's been slurped
546 rules = iRules ifaces
547 (decls, new_rules) = selectGated gates lookup rules
552 setIfacesRn (ifaces { iRules = new_rules }) `thenRn_`
553 traceRn (sep [text "getImportedRules:",
554 text "Slurped" <+> int (length decls) <+> text "rules"]) `thenRn_`
557 selectGated gates lookup (decl_bag, n_slurped)
558 -- Select only those decls whose gates are *all* in 'gates'
559 -- or are a class in 'lookup'
561 | opt_NoPruneDecls -- Just to try the effect of not gating at all
563 decls = foldrBag (\ (_,d) ds -> d:ds) [] decl_bag -- Grab them all
565 (decls, (emptyBag, n_slurped + length decls))
569 = case foldrBag select ([], emptyBag) decl_bag of
570 (decls, new_bag) -> (decls, (new_bag, n_slurped + length decls))
572 available n = n `elemNameSet` gates
573 || case lookup n of { Just (AClass c) -> True; other -> False }
575 select (reqd, decl) (yes, no)
576 | all available reqd = (decl:yes, no)
577 | otherwise = (yes, (reqd,decl) `consBag` no)
581 %*********************************************************
583 \subsection{Getting in a declaration}
585 %*********************************************************
588 importDecl :: Name -> RnMG ImportDeclResult
590 data ImportDeclResult
593 | HereItIs (Module, RdrNameTyClDecl)
596 = -- STEP 1: Check if we've slurped it in while compiling this module
597 getIfacesRn `thenRn` \ ifaces ->
598 traceRn (text "Wanting:" <+> ppr name) `thenRn_`
599 if name `elemNameSet` iSlurp ifaces then
600 returnRn AlreadySlurped
603 -- STEP 2: Check if it's already in the type environment
604 getTypeEnvRn `thenRn` \ lookup ->
605 case lookup name of {
606 Just ty_thing | name `elemNameEnv` wiredInThingEnv
607 -> -- When we find a wired-in name we must load its home
608 -- module so that we find any instance decls lurking therein
609 loadHomeInterface wi_doc name `thenRn_`
610 returnRn (InTypeEnv ty_thing)
613 -> returnRn (InTypeEnv ty_thing) ;
617 -- STEP 3: OK, we have to slurp it in from an interface file
618 -- First load the interface file
619 traceRn nd_doc `thenRn_`
620 loadHomeInterface nd_doc name `thenRn_`
621 getIfacesRn `thenRn` \ ifaces ->
623 -- STEP 4: Get the declaration out
625 (decls_map, _) = iDecls ifaces
627 case lookupNameEnv decls_map name of
629 -> traceRn (text "Record slurp" <+> ppr avail) `thenRn_`
630 setIfacesRn (recordSlurp ifaces avail) `thenRn_`
631 returnRn (HereItIs decl)
634 -> addErrRn (getDeclErr name) `thenRn_`
635 returnRn AlreadySlurped
638 wi_doc = ptext SLIT("need home module for wired in thing") <+> ppr name
639 nd_doc = ptext SLIT("need decl for") <+> ppr name
644 %********************************************************
646 \subsection{Checking usage information}
648 %********************************************************
650 @recompileRequired@ is called from the HscMain. It checks whether
651 a recompilation is required. It needs access to the persistent state,
652 finder, etc, because it may have to load lots of interface files to
653 check their versions.
656 type RecompileRequired = Bool
657 upToDate = False -- Recompile not required
658 outOfDate = True -- Recompile required
660 recompileRequired :: FilePath -- Only needed for debug msgs
661 -> Bool -- Source unchanged
662 -> ModIface -- Old interface
663 -> RnMG RecompileRequired
664 recompileRequired iface_path source_unchanged iface
665 = traceHiDiffsRn (text "Considering whether compilation is required for" <+> text iface_path <> colon) `thenRn_`
667 -- CHECK WHETHER THE SOURCE HAS CHANGED
668 if not source_unchanged then
669 traceHiDiffsRn (nest 4 (text "Source file changed or recompilation check turned off")) `thenRn_`
673 -- Source code unchanged and no errors yet... carry on
674 checkList [checkModUsage u | u <- mi_usages iface]
676 checkList :: [RnMG RecompileRequired] -> RnMG RecompileRequired
677 checkList [] = returnRn upToDate
678 checkList (check:checks) = check `thenRn` \ recompile ->
686 checkModUsage :: ImportVersion Name -> RnMG RecompileRequired
687 -- Given the usage information extracted from the old
688 -- M.hi file for the module being compiled, figure out
689 -- whether M needs to be recompiled.
691 checkModUsage (mod_name, _, _, NothingAtAll)
692 -- If CurrentModule.hi contains
694 -- then that simply records that Foo lies below CurrentModule in the
695 -- hierarchy, but CurrentModule doesn't depend in any way on Foo.
696 -- In this case we don't even want to open Foo's interface.
697 = up_to_date (ptext SLIT("Nothing used from:") <+> ppr mod_name)
699 checkModUsage (mod_name, _, is_boot, whats_imported)
700 = -- Load the imported interface is possible
701 -- We use tryLoadInterface, because failure is not an error
702 -- (might just be that the old .hi file for this module is out of date)
703 -- We use ImportByUser/ImportByUserSource as the 'from' flag,
704 -- a) because we need to know whether to load the .hi-boot file
705 -- b) because loadInterface things matters are amiss if we
706 -- ImportBySystem an interface it knows nothing about
708 doc_str = sep [ptext SLIT("need version info for"), ppr mod_name]
709 from | is_boot = ImportByUserSource
710 | otherwise = ImportByUser
712 tryLoadInterface doc_str mod_name from `thenRn` \ (iface, maybe_err) ->
715 Just err -> out_of_date (sep [ptext SLIT("Can't find version number for module"),
717 -- Couldn't find or parse a module mentioned in the
718 -- old interface file. Don't complain -- it might just be that
719 -- the current module doesn't need that import and it's been deleted
723 new_vers = mi_version iface
724 new_decl_vers = vers_decls new_vers
726 case whats_imported of { -- NothingAtAll dealt with earlier
728 Everything old_mod_vers -> checkModuleVersion old_mod_vers new_vers `thenRn` \ recompile ->
730 out_of_date (ptext SLIT("...and I needed the whole module"))
734 Specifically old_mod_vers maybe_old_export_vers old_decl_vers old_rule_vers ->
737 checkModuleVersion old_mod_vers new_vers `thenRn` \ recompile ->
738 if not recompile then
743 if checkExportList maybe_old_export_vers new_vers then
744 out_of_date (ptext SLIT("Export list changed"))
748 if old_rule_vers /= vers_rules new_vers then
749 out_of_date (ptext SLIT("Rules changed"))
752 -- CHECK ITEMS ONE BY ONE
753 checkList [checkEntityUsage new_decl_vers u | u <- old_decl_vers] `thenRn` \ recompile ->
755 returnRn outOfDate -- This one failed, so just bail out now
757 up_to_date (ptext SLIT("...but the bits I use haven't."))
761 ------------------------
762 checkModuleVersion old_mod_vers new_vers
763 | vers_module new_vers == old_mod_vers
764 = up_to_date (ptext SLIT("Module version unchanged"))
767 = out_of_date (ptext SLIT("Module version has changed"))
769 ------------------------
770 checkExportList Nothing new_vers = upToDate
771 checkExportList (Just v) new_vers = v /= vers_exports new_vers
773 ------------------------
774 checkEntityUsage new_vers (name,old_vers)
775 = case lookupNameEnv new_vers name of
777 Nothing -> -- We used it before, but it ain't there now
778 out_of_date (sep [ptext SLIT("No longer exported:"), ppr name])
780 Just new_vers -- It's there, but is it up to date?
781 | new_vers == old_vers -> returnRn upToDate
782 | otherwise -> out_of_date (sep [ptext SLIT("Out of date:"), ppr name])
784 up_to_date msg = traceHiDiffsRn msg `thenRn_` returnRn upToDate
785 out_of_date msg = traceHiDiffsRn msg `thenRn_` returnRn outOfDate
789 %*********************************************************
793 %*********************************************************
797 = vcat [ptext SLIT("Failed to find interface decl for") <+> quotes (ppr name),
798 ptext SLIT("from module") <+> quotes (ppr (nameModule name))