2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
8 mkUsageInfo, -- Construct the usage info for a module
10 mkIface, -- Build a ModIface from a ModGuts,
11 -- including computing version information
13 writeIfaceFile, -- Write the interface file
15 checkOldIface, -- See if recompilation is required, by
16 -- comparing version information
18 tyThingToIfaceDecl -- Converting things to their Iface equivalents
22 -----------------------------------------------
23 MkIface.lhs deals with versioning
24 -----------------------------------------------
26 Here's the version-related info in an interface file
28 module Foo 8 -- module-version
29 3 -- export-list-version
31 Usages: -- Version info for what this compilation of Foo imported
32 Baz 3 -- Module version
33 [4] -- The export-list version if Foo depended on it
34 (g,2) -- Function and its version
35 (T,1) -- Type and its version
37 <version> f :: Int -> Int {- Unfolding: \x -> Wib.t[2] x -}
38 -- The [2] says that f's unfolding
39 -- mentions verison 2 of Wib.t
41 -----------------------------------------------
43 -----------------------------------------------
46 * In the mi_usages information in an interface, we record the
47 version number of each free variable of the module
49 * In mkIface, we compute the version number of each exported thing A.f
50 by comparing its A.f's info with its new info, and bumping its
51 version number if it differs. If A.f mentions B.g, and B.g's version
52 number has changed, then we count A.f as having changed too.
54 * In checkOldIface we compare the mi_usages for the module with
55 the actual version info for all each thing recorded in mi_usages
60 We count A.f as changing if its fixity changes
64 If a rule changes, we want to recompile any module that might be
65 affected by that rule. For non-orphan rules, this is relatively easy.
66 If module M defines f, and a rule for f, just arrange that the version
67 number for M.f changes if any of the rules for M.f change. Any module
68 that does not depend on M.f can't be affected by the rule-change
71 Orphan rules (ones whose 'head function' is not defined in M) are
72 harder. Here's what we do.
74 * We have a per-module orphan-rule version number which changes if
75 any orphan rule changes. (It's unaffected by non-orphan rules.)
77 * We record usage info for any orphan module 'below' this one,
78 giving the orphan-rule version number. We recompile if this
81 The net effect is that if an orphan rule changes, we recompile every
82 module above it. That's very conservative, but it's devilishly hard
83 to know what it might affect, so we just have to be conservative.
87 In an iface file we have
89 instance Eq a => Eq [a] = dfun29
92 We have a version number for dfun29, covering its unfolding
93 etc. Suppose we are compiling a module M that imports A only
94 indirectly. If typechecking M uses this instance decl, we record the
95 dependency on A.dfun29 as if it were a free variable of the module
96 (via the tcg_inst_usages accumulator). That means that A will appear
97 in M's usage list. If the shape of the instance declaration changes,
98 then so will dfun29's version, triggering a recompilation.
100 Adding an instance declaration, or changing an instance decl that is
101 not currently used, is more tricky. (This really only makes a
102 difference when we have overlapping instance decls, because then the
103 new instance decl might kick in to override the old one.) We handle
104 this in a very similar way that we handle rules above.
106 * For non-orphan instance decls, identify one locally-defined tycon/class
107 mentioned in the decl. Treat the instance decl as part of the defn of that
108 tycon/class, so that if the shape of the instance decl changes, so does the
109 tycon/class; that in turn will force recompilation of anything that uses
112 * For orphan instance decls, act the same way as for orphan rules.
113 Indeed, we use the same global orphan-rule version number.
117 mkUsageInfo figures out what the ``usage information'' for this
118 moudule is; that is, what it must record in its interface file as the
121 We produce a line for every module B below the module, A, currently being
124 to record the fact that A does import B indirectly. This is used to decide
125 to look to look for B.hi rather than B.hi-boot when compiling a module that
126 imports A. This line says that A imports B, but uses nothing in it.
127 So we'll get an early bale-out when compiling A if B's version changes.
129 The usage information records:
132 \item (a) anything reachable from its body code
133 \item (b) any module exported with a @module Foo@
134 \item (c) anything reachable from an exported item
137 Why (b)? Because if @Foo@ changes then this module's export list
138 will change, so we must recompile this module at least as far as
139 making a new interface file --- but in practice that means complete
142 Why (c)? Consider this:
144 module A( f, g ) where | module B( f ) where
145 import B( f ) | f = h 3
149 Here, @B.f@ isn't used in A. Should we nevertheless record @B.f@ in
150 @A@'s usages? Our idea is that we aren't going to touch A.hi if it is
151 *identical* to what it was before. If anything about @B.f@ changes
152 than anyone who imports @A@ should be recompiled in case they use
153 @B.f@ (they'll get an early exit if they don't). So, if anything
154 about @B.f@ changes we'd better make sure that something in A.hi
155 changes, and the convenient way to do that is to record the version
156 number @B.f@ in A.hi in the usage list. If B.f changes that'll force a
157 complete recompiation of A, which is overkill but it's the only way to
158 write a new, slightly different, A.hi.
160 But the example is tricker. Even if @B.f@ doesn't change at all,
161 @B.h@ may do so, and this change may not be reflected in @f@'s version
162 number. But with -O, a module that imports A must be recompiled if
163 @B.h@ changes! So A must record a dependency on @B.h@. So we treat
164 the occurrence of @B.f@ in the export list *just as if* it were in the
165 code of A, and thereby haul in all the stuff reachable from it.
167 *** Conclusion: if A mentions B.f in its export list,
168 behave just as if A mentioned B.f in its source code,
169 and slurp in B.f and all its transitive closure ***
171 [NB: If B was compiled with -O, but A isn't, we should really *still*
172 haul in all the unfoldings for B, in case the module that imports A *is*
173 compiled with -O. I think this is the case.]
177 #include "HsVersions.h"
210 import PackageConfig hiding ( Version )
212 import BasicTypes hiding ( SuccessFlag(..) )
214 import Util hiding ( eqListBy )
225 %************************************************************************
227 \subsection{Completing an interface}
229 %************************************************************************
233 -> Maybe ModIface -- The old interface, if we have it
234 -> ModGuts -- Usages, deprecations, etc
235 -> ModDetails -- The trimmed, tidied interface
236 -> IO (ModIface, -- The new one, complete with decls and versions
237 Bool) -- True <=> there was an old Iface, and the new one
238 -- is identical, so no need to write it
240 mkIface hsc_env maybe_old_iface
241 (ModGuts{ mg_module = this_mod,
245 mg_rdr_env = rdr_env,
246 mg_fix_env = fix_env,
247 mg_deprecs = src_deprecs})
248 (ModDetails{ md_insts = insts,
249 md_fam_insts = fam_insts,
251 md_vect_info = vect_info,
253 md_exports = exports })
255 -- NB: notice that mkIface does not look at the bindings
256 -- only at the TypeEnv. The previous Tidy phase has
257 -- put exactly the info into the TypeEnv that we want
258 -- to expose in the interface
260 = do { eps <- hscEPS hsc_env
261 ; let { entities = typeEnvElts type_env ;
262 decls = [ tyThingToIfaceDecl entity
263 | entity <- entities,
264 let name = getName entity,
265 not (isImplicitTyThing entity),
266 -- No implicit Ids and class tycons in the interface file
267 not (isWiredInName name),
268 -- Nor wired-in things; the compiler knows about them anyhow
269 nameIsLocalOrFrom this_mod name ]
270 -- Sigh: see Note [Root-main Id] in TcRnDriver
272 ; fixities = [(occ,fix) | FixItem occ fix _ <- nameEnvElts fix_env]
273 ; deprecs = mkIfaceDeprec src_deprecs
274 ; iface_rules = map (coreRuleToIfaceRule this_mod) rules
275 ; iface_insts = map instanceToIfaceInst insts
276 ; iface_fam_insts = map famInstToIfaceFamInst fam_insts
277 ; iface_vect_info = flattenVectInfo vect_info
279 ; intermediate_iface = ModIface {
280 mi_module = this_mod,
284 mi_exports = mkIfaceExports exports,
286 -- Sort these lexicographically, so that
287 -- the result is stable across compilations
288 mi_insts = sortLe le_inst iface_insts,
289 mi_fam_insts= sortLe le_fam_inst iface_fam_insts,
290 mi_rules = sortLe le_rule iface_rules,
292 mi_vect_info = iface_vect_info,
294 mi_fixities = fixities,
295 mi_deprecs = deprecs,
296 mi_globals = Just rdr_env,
298 -- Left out deliberately: filled in by addVersionInfo
299 mi_mod_vers = initialVersion,
300 mi_exp_vers = initialVersion,
301 mi_rule_vers = initialVersion,
302 mi_orphan = False, -- Always set by addVersionInfo, but
303 -- it's a strict field, so we can't omit it.
304 mi_finsts = False, -- Ditto
305 mi_decls = deliberatelyOmitted "decls",
306 mi_ver_fn = deliberatelyOmitted "ver_fn",
308 -- And build the cached values
309 mi_dep_fn = mkIfaceDepCache deprecs,
310 mi_fix_fn = mkIfaceFixCache fixities }
312 -- Add version information
313 ; ext_ver_fn = mkParentVerFun hsc_env eps
314 ; (new_iface, no_change_at_all, pp_diffs, pp_orphs)
315 = _scc_ "versioninfo"
316 addVersionInfo ext_ver_fn maybe_old_iface
317 intermediate_iface decls
321 ; when (isJust pp_orphs && dopt Opt_WarnOrphans dflags)
322 (printDump (expectJust "mkIface" pp_orphs))
323 ; when (dopt Opt_D_dump_hi_diffs dflags) (printDump pp_diffs)
324 ; dumpIfSet_dyn dflags Opt_D_dump_hi "FINAL INTERFACE"
325 (pprModIface new_iface)
327 ; return (new_iface, no_change_at_all) }
329 r1 `le_rule` r2 = ifRuleName r1 <= ifRuleName r2
330 i1 `le_inst` i2 = ifDFun i1 `le_occ` ifDFun i2
331 i1 `le_fam_inst` i2 = ifFamInstTcName i1 `le_occ` ifFamInstTcName i2
333 le_occ :: Name -> Name -> Bool
334 -- Compare lexicographically by OccName, *not* by unique, because
335 -- the latter is not stable across compilations
336 le_occ n1 n2 = nameOccName n1 <= nameOccName n2
338 dflags = hsc_dflags hsc_env
339 deliberatelyOmitted x = panic ("Deliberately omitted: " ++ x)
340 ifFamInstTcName = ifaceTyConName . ifFamInstTyCon
342 flattenVectInfo (VectInfo ccVar) =
343 IfaceVectInfo [Var.varName v | (v, _) <- varEnvElts ccVar]
345 -----------------------------
346 writeIfaceFile :: DynFlags -> ModLocation -> ModIface -> IO ()
347 writeIfaceFile dflags location new_iface
348 = do createDirectoryHierarchy (directoryOf hi_file_path)
349 writeBinIface dflags hi_file_path new_iface
350 where hi_file_path = ml_hi_file location
353 -- -----------------------------------------------------------------------------
354 -- Look up parents and versions of Names
356 -- This is like a global version of the mi_ver_fn field in each ModIface.
357 -- Given a Name, it finds the ModIface, and then uses mi_ver_fn to get
358 -- the parent and version info.
361 :: HscEnv -- needed to look up versions
362 -> ExternalPackageState -- ditto
363 -> (Name -> (OccName,Version))
364 mkParentVerFun hsc_env eps
367 mod = nameModule name
368 occ = nameOccName name
369 iface = lookupIfaceByModule (hsc_dflags hsc_env) hpt pit mod `orElse`
370 pprPanic "lookupVers2" (ppr mod <+> ppr occ)
372 mi_ver_fn iface occ `orElse`
373 pprPanic "lookupVers1" (ppr mod <+> ppr occ)
375 hpt = hsc_HPT hsc_env
378 -----------------------------------------------------------------------------
379 -- Compute version numbers for local decls
382 :: (Name -> (OccName,Version)) -- lookup parents and versions of names
383 -> Maybe ModIface -- The old interface, read from M.hi
384 -> ModIface -- The new interface (lacking decls)
385 -> [IfaceDecl] -- The new decls
386 -> (ModIface, -- Updated interface
387 Bool, -- True <=> no changes at all; no need to write Iface
389 Maybe SDoc) -- Warnings about orphans
391 addVersionInfo ver_fn Nothing new_iface new_decls
392 -- No old interface, so definitely write a new one!
393 = (new_iface { mi_orphan = not (null orph_insts && null orph_rules)
394 , mi_finsts = not . null $ mi_fam_insts new_iface
395 , mi_decls = [(initialVersion, decl) | decl <- new_decls]
396 , mi_ver_fn = mkIfaceVerCache (zip (repeat initialVersion)
400 ptext SLIT("No old interface file"),
401 pprOrphans orph_insts orph_rules)
403 orph_insts = filter (isNothing . ifInstOrph) (mi_insts new_iface)
404 orph_rules = filter (isNothing . ifRuleOrph) (mi_rules new_iface)
406 addVersionInfo ver_fn (Just old_iface@(ModIface {
407 mi_mod_vers = old_mod_vers,
408 mi_exp_vers = old_exp_vers,
409 mi_rule_vers = old_rule_vers,
410 mi_decls = old_decls,
411 mi_ver_fn = old_decl_vers,
412 mi_fix_fn = old_fixities }))
413 new_iface@(ModIface { mi_fix_fn = new_fixities })
416 = (old_iface, True, ptext SLIT("Interface file unchanged"), pp_orphs)
418 = (final_iface, False, vcat [ptext SLIT("Interface file has changed"),
419 nest 2 pp_diffs], pp_orphs)
421 final_iface = new_iface {
422 mi_mod_vers = bump_unless no_output_change old_mod_vers,
423 mi_exp_vers = bump_unless no_export_change old_exp_vers,
424 mi_rule_vers = bump_unless no_rule_change old_rule_vers,
425 mi_orphan = not (null new_orph_rules && null new_orph_insts),
426 mi_finsts = not . null $ mi_fam_insts new_iface,
427 mi_decls = decls_w_vers,
428 mi_ver_fn = mkIfaceVerCache decls_w_vers }
430 decls_w_vers = [(add_vers decl, decl) | decl <- new_decls]
433 (old_non_orph_insts, old_orph_insts) =
434 mkOrphMap ifInstOrph (mi_insts old_iface)
435 (new_non_orph_insts, new_orph_insts) =
436 mkOrphMap ifInstOrph (mi_insts new_iface)
437 old_fam_insts = mi_fam_insts old_iface
438 new_fam_insts = mi_fam_insts new_iface
439 same_insts occ = eqMaybeBy (eqListBy eqIfInst)
440 (lookupOccEnv old_non_orph_insts occ)
441 (lookupOccEnv new_non_orph_insts occ)
443 (old_non_orph_rules, old_orph_rules) =
444 mkOrphMap ifRuleOrph (mi_rules old_iface)
445 (new_non_orph_rules, new_orph_rules) =
446 mkOrphMap ifRuleOrph (mi_rules new_iface)
447 same_rules occ = eqMaybeBy (eqListBy eqIfRule)
448 (lookupOccEnv old_non_orph_rules occ)
449 (lookupOccEnv new_non_orph_rules occ)
451 -- Computing what changed
452 no_output_change = no_decl_change && no_rule_change &&
453 no_export_change && no_deprec_change
454 no_export_change = mi_exports new_iface == mi_exports old_iface
456 no_decl_change = isEmptyOccSet changed_occs
457 no_rule_change = not (changedWrtNames changed_occs (eqListBy eqIfRule old_orph_rules new_orph_rules)
458 || changedWrtNames changed_occs (eqListBy eqIfInst old_orph_insts new_orph_insts)
459 || changedWrtNames changed_occs (eqListBy eqIfFamInst old_fam_insts new_fam_insts))
460 no_deprec_change = mi_deprecs new_iface == mi_deprecs old_iface
462 -- If the usages havn't changed either, we don't need to write the interface file
463 no_other_changes = mi_usages new_iface == mi_usages old_iface &&
464 mi_deps new_iface == mi_deps old_iface
465 no_change_at_all = no_output_change && no_other_changes
467 pp_diffs = vcat [pp_change no_export_change "Export list"
468 (ppr old_exp_vers <+> arrow <+> ppr (mi_exp_vers final_iface)),
469 pp_change no_rule_change "Rules"
470 (ppr old_rule_vers <+> arrow <+> ppr (mi_rule_vers final_iface)),
471 pp_change no_deprec_change "Deprecations" empty,
472 pp_change no_other_changes "Usages" empty,
474 pp_change True what info = empty
475 pp_change False what info = text what <+> ptext SLIT("changed") <+> info
478 old_decl_env = mkOccEnv [(ifName decl, decl) | (_,decl) <- old_decls]
479 same_fixity n = bool (old_fixities n == new_fixities n)
482 -- Adding version info
483 new_version = bumpVersion old_mod_vers
484 -- Start from the old module version, not from
485 -- zero so that if you remove f, and then add
486 -- it again, you don't thereby reduce f's
489 add_vers decl | occ `elemOccSet` changed_occs = new_version
490 | otherwise = snd (expectJust "add_vers" (old_decl_vers occ))
491 -- If it's unchanged, there jolly well
492 where -- should be an old version number
496 -- Deciding which declarations have changed
498 -- For each local decl, the IfaceEq gives the list of things that
499 -- must be unchanged for the declaration as a whole to be unchanged.
500 eq_info :: [(OccName, IfaceEq)]
501 eq_info = map check_eq new_decls
503 | Just old_decl <- lookupOccEnv old_decl_env occ
504 = (occ, new_decl `eqIfDecl` old_decl &&& eq_indirects new_decl)
505 | otherwise {- No corresponding old decl -}
508 occ = ifName new_decl
510 eq_indirects :: IfaceDecl -> IfaceEq
511 -- When seeing if two decls are the same, remember to
512 -- check whether any relevant fixity or rules have changed
513 eq_indirects (IfaceId {ifName = occ}) = eq_ind_occ occ
514 eq_indirects (IfaceClass {ifName = cls_occ, ifSigs = sigs})
515 = same_insts cls_occ &&&
516 eq_ind_occs [op | IfaceClassOp op _ _ <- sigs]
517 eq_indirects (IfaceData {ifName = tc_occ, ifCons = cons})
518 = same_insts tc_occ &&& same_fixity tc_occ &&& -- The TyCon can have a fixity too
519 eq_ind_occs (map ifConOcc (visibleIfConDecls cons))
520 eq_indirects other = Equal -- Synonyms and foreign declarations
522 eq_ind_occ :: OccName -> IfaceEq -- For class ops and Ids; check fixity and rules
523 eq_ind_occ occ = same_fixity occ &&& same_rules occ
524 eq_ind_occs = foldr ((&&&) . eq_ind_occ) Equal
526 -- The Occs of declarations that changed.
527 changed_occs :: OccSet
528 changed_occs = computeChangedOccs ver_fn (mi_module new_iface)
529 (mi_usages old_iface) eq_info
533 pp_decl_diffs :: SDoc -- Nothing => no changes
535 | isEmptyOccSet changed_occs = empty
537 = vcat [ptext SLIT("Changed occs:") <+> ppr (occSetElts changed_occs),
538 ptext SLIT("Version change for these decls:"),
539 nest 2 (vcat (map show_change new_decls))]
541 eq_env = mkOccEnv eq_info
543 | not (occ `elemOccSet` changed_occs) = empty
545 = vcat [ppr occ <+> ppr (old_decl_vers occ) <+> arrow <+> ppr new_version,
548 occ = ifName new_decl
549 why = case lookupOccEnv eq_env occ of
550 Just (EqBut names) -> sep [ppr occ <> colon, ptext SLIT("Free vars (only) changed:") <> ppr names,
551 nest 2 (braces (fsep (map ppr (occSetElts
552 (occs `intersectOccSet` changed_occs)))))]
553 where occs = mkOccSet (map nameOccName (nameSetToList names))
555 | Just old_decl <- lookupOccEnv old_decl_env occ
556 -> vcat [ptext SLIT("Old:") <+> ppr old_decl,
557 ptext SLIT("New:") <+> ppr new_decl]
559 -> ppr occ <+> ptext SLIT("only in new interface")
560 other -> pprPanic "MkIface.show_change" (ppr occ)
562 pp_orphs = pprOrphans new_orph_insts new_orph_rules
565 pprOrphans insts rules
566 | null insts && null rules = Nothing
569 if null insts then empty else
570 hang (ptext SLIT("Warning: orphan instances:"))
571 2 (vcat (map ppr insts)),
572 if null rules then empty else
573 hang (ptext SLIT("Warning: orphan rules:"))
574 2 (vcat (map ppr rules))
578 :: (Name -> (OccName,Version)) -- get parents and versions
579 -> Module -- This module
580 -> [Usage] -- Usages from old iface
581 -> [(OccName, IfaceEq)] -- decl names, equality conditions
582 -> OccSet -- set of things that have changed
583 computeChangedOccs ver_fn this_module old_usages eq_info
584 = foldl add_changes emptyOccSet (stronglyConnComp edges)
587 -- return True if an external name has changed
588 name_changed :: Name -> Bool
590 | Just ents <- lookupUFM usg_modmap (moduleName mod)
591 = case lookupUFM ents parent_occ of
592 Nothing -> pprPanic "computeChangedOccs" (ppr nm)
593 Just v -> v < new_version
594 | otherwise = False -- must be in another package
597 (parent_occ, new_version) = ver_fn nm
599 -- Turn the usages from the old ModIface into a mapping
600 usg_modmap = listToUFM [ (usg_mod usg, listToUFM (usg_entities usg))
601 | usg <- old_usages ]
603 get_local_eq_info :: GenIfaceEq NameSet -> GenIfaceEq OccSet
604 get_local_eq_info Equal = Equal
605 get_local_eq_info NotEqual = NotEqual
606 get_local_eq_info (EqBut ns) = foldNameSet f Equal ns
607 where f name eq | nameModule name == this_module =
608 EqBut (unitOccSet (nameOccName name)) `and_occifeq` eq
609 | name_changed name = NotEqual
612 local_eq_infos = mapSnd get_local_eq_info eq_info
614 edges :: [((OccName, OccIfaceEq), Unique, [Unique])]
615 edges = [ (node, getUnique occ, map getUnique occs)
616 | node@(occ, iface_eq) <- local_eq_infos
617 , let occs = case iface_eq of
618 EqBut occ_set -> occSetElts occ_set
621 -- Changes in declarations
622 add_changes :: OccSet -> SCC (OccName, OccIfaceEq) -> OccSet
623 add_changes so_far (AcyclicSCC (occ, iface_eq))
624 | changedWrt so_far iface_eq -- This one has changed
625 = extendOccSet so_far occ
626 add_changes so_far (CyclicSCC pairs)
627 | changedWrt so_far (foldr1 and_occifeq iface_eqs)
628 -- One of this group has changed
629 = extendOccSetList so_far occs
630 where (occs, iface_eqs) = unzip pairs
631 add_changes so_far other = so_far
633 type OccIfaceEq = GenIfaceEq OccSet
635 changedWrt :: OccSet -> OccIfaceEq -> Bool
636 changedWrt so_far Equal = False
637 changedWrt so_far NotEqual = True
638 changedWrt so_far (EqBut kids) = so_far `intersectsOccSet` kids
640 changedWrtNames :: OccSet -> IfaceEq -> Bool
641 changedWrtNames so_far Equal = False
642 changedWrtNames so_far NotEqual = True
643 changedWrtNames so_far (EqBut kids) =
644 so_far `intersectsOccSet` mkOccSet (map nameOccName (nameSetToList kids))
646 and_occifeq :: OccIfaceEq -> OccIfaceEq -> OccIfaceEq
647 Equal `and_occifeq` x = x
648 NotEqual `and_occifeq` x = NotEqual
649 EqBut nms `and_occifeq` Equal = EqBut nms
650 EqBut nms `and_occifeq` NotEqual = NotEqual
651 EqBut nms1 `and_occifeq` EqBut nms2 = EqBut (nms1 `unionOccSets` nms2)
653 ----------------------
654 -- mkOrphMap partitions instance decls or rules into
655 -- (a) an OccEnv for ones that are not orphans,
656 -- mapping the local OccName to a list of its decls
657 -- (b) a list of orphan decls
658 mkOrphMap :: (decl -> Maybe OccName) -- (Just occ) for a non-orphan decl, keyed by occ
659 -- Nothing for an orphan decl
660 -> [decl] -- Sorted into canonical order
661 -> (OccEnv [decl], -- Non-orphan decls associated with their key;
662 -- each sublist in canonical order
663 [decl]) -- Orphan decls; in canonical order
664 mkOrphMap get_key decls
665 = foldl go (emptyOccEnv, []) decls
667 go (non_orphs, orphs) d
668 | Just occ <- get_key d
669 = (extendOccEnv_C (\ ds _ -> d:ds) non_orphs occ [d], orphs)
670 | otherwise = (non_orphs, d:orphs)
672 ----------------------
673 mkIfaceDeprec :: Deprecations -> IfaceDeprecs
674 mkIfaceDeprec NoDeprecs = NoDeprecs
675 mkIfaceDeprec (DeprecAll t) = DeprecAll t
676 mkIfaceDeprec (DeprecSome env) = DeprecSome (sortLe (<=) (nameEnvElts env))
678 ----------------------
679 bump_unless :: Bool -> Version -> Version
680 bump_unless True v = v -- True <=> no change
681 bump_unless False v = bumpVersion v
685 %*********************************************************
687 \subsection{Keeping track of what we've slurped, and version numbers}
689 %*********************************************************
693 mkUsageInfo :: HscEnv
694 -> ModuleEnv (Module, Bool, SrcSpan)
695 -> [(ModuleName, IsBootInterface)]
696 -> NameSet -> IO [Usage]
697 mkUsageInfo hsc_env dir_imp_mods dep_mods used_names
698 = do { eps <- hscEPS hsc_env
699 ; let usages = mk_usage_info (eps_PIT eps) hsc_env
700 dir_imp_mods dep_mods used_names
701 ; usages `seqList` return usages }
702 -- seq the list of Usages returned: occasionally these
703 -- don't get evaluated for a while and we can end up hanging on to
704 -- the entire collection of Ifaces.
706 mk_usage_info pit hsc_env dir_imp_mods dep_mods used_names
707 = mapCatMaybes mkUsage dep_mods
708 -- ToDo: do we need to sort into canonical order?
710 hpt = hsc_HPT hsc_env
711 dflags = hsc_dflags hsc_env
713 -- ent_map groups together all the things imported and used
714 -- from a particular module in this package
715 ent_map :: ModuleEnv [OccName]
716 ent_map = foldNameSet add_mv emptyModuleEnv used_names
718 | isWiredInName name = mv_map -- ignore wired-in names
720 = case nameModule_maybe name of
721 Nothing -> mv_map -- ignore internal names
722 Just mod -> extendModuleEnv_C add_item mv_map mod [occ]
724 occ = nameOccName name
725 add_item occs _ = occ:occs
727 depend_on_exports mod = case lookupModuleEnv dir_imp_mods mod of
728 Just (_,no_imp,_) -> not no_imp
731 -- We want to create a Usage for a home module if
732 -- a) we used something from; has something in used_names
733 -- b) we imported all of it, even if we used nothing from it
734 -- (need to recompile if its export list changes: export_vers)
735 -- c) is a home-package orphan or family-instance module (need to
736 -- recompile if its instance decls change: rules_vers)
737 mkUsage :: (ModuleName, IsBootInterface) -> Maybe Usage
738 mkUsage (mod_name, _)
739 | isNothing maybe_iface -- We can't depend on it if we didn't
740 || (null used_occs -- load its interface.
741 && isNothing export_vers
744 = Nothing -- Record no usage info
747 = Just (Usage { usg_name = mod_name,
749 usg_exports = export_vers,
750 usg_entities = fmToList ent_vers,
751 usg_rules = rules_vers })
753 maybe_iface = lookupIfaceByModule dflags hpt pit mod
754 -- In one-shot mode, the interfaces for home-package
755 -- modules accumulate in the PIT not HPT. Sigh.
757 mod = mkModule (thisPackage dflags) mod_name
759 Just iface = maybe_iface
760 orphan_mod = mi_orphan iface
761 finsts_mod = mi_finsts iface
762 version_env = mi_ver_fn iface
763 mod_vers = mi_mod_vers iface
764 rules_vers = mi_rule_vers iface
765 export_vers | depend_on_exports mod = Just (mi_exp_vers iface)
766 | otherwise = Nothing
768 used_occs = lookupModuleEnv ent_map mod `orElse` []
770 -- Making a FiniteMap here ensures that (a) we remove duplicates
771 -- when we have usages on several subordinates of a single parent,
772 -- and (b) that the usages emerge in a canonical order, which
773 -- is why we use FiniteMap rather than OccEnv: FiniteMap works
774 -- using Ord on the OccNames, which is a lexicographic ordering.
775 ent_vers :: FiniteMap OccName Version
776 ent_vers = listToFM (map lookup_occ used_occs)
779 case version_env occ of
780 Nothing -> pprTrace "hmm, strange" (ppr mod <+> ppr occ) $
781 (occ, initialVersion) -- does this ever happen?
782 Just (parent, version) -> (parent, version)
786 mkIfaceExports :: [AvailInfo]
787 -> [(Module, [GenAvailInfo OccName])]
788 -- Group by module and sort by occurrence
789 -- This keeps the list in canonical order
790 mkIfaceExports exports
791 = [ (mod, eltsFM avails)
792 | (mod, avails) <- fmToList groupFM
795 -- Deliberately use FiniteMap rather than UniqFM so we
796 -- get a canonical ordering
797 groupFM :: ModuleEnv (FiniteMap FastString (GenAvailInfo OccName))
798 groupFM = foldl add emptyModuleEnv exports
801 = extendModuleEnv_C add_avail env mod (unitFM avail_fs avail_occ)
803 avail_occ = availToOccs avail
804 mod = nameModule (availName avail)
805 avail_fs = occNameFS (availName avail_occ)
806 add_avail avail_fm _ = addToFM avail_fm avail_fs avail_occ
808 availToOccs (Avail n) = Avail (nameOccName n)
809 availToOccs (AvailTC tc ns) = AvailTC (nameOccName tc) (map nameOccName ns)
813 %************************************************************************
815 Load the old interface file for this module (unless
816 we have it aleady), and check whether it is up to date
819 %************************************************************************
822 checkOldIface :: HscEnv
824 -> Bool -- Source unchanged
825 -> Maybe ModIface -- Old interface from compilation manager, if any
826 -> IO (RecompileRequired, Maybe ModIface)
828 checkOldIface hsc_env mod_summary source_unchanged maybe_iface
829 = do { showPass (hsc_dflags hsc_env)
830 ("Checking old interface for " ++
831 showSDoc (ppr (ms_mod mod_summary))) ;
833 ; initIfaceCheck hsc_env $
834 check_old_iface hsc_env mod_summary source_unchanged maybe_iface
837 check_old_iface hsc_env mod_summary source_unchanged maybe_iface
838 = do -- CHECK WHETHER THE SOURCE HAS CHANGED
839 { ifM (not source_unchanged)
840 (traceHiDiffs (nest 4 (text "Source file changed or recompilation check turned off")))
842 -- If the source has changed and we're in interactive mode, avoid reading
843 -- an interface; just return the one we might have been supplied with.
844 ; let dflags = hsc_dflags hsc_env
845 ; if not (isObjectTarget (hscTarget dflags)) && not source_unchanged then
846 return (outOfDate, maybe_iface)
848 case maybe_iface of {
849 Just old_iface -> do -- Use the one we already have
850 { traceIf (text "We already have the old interface for" <+> ppr (ms_mod mod_summary))
851 ; recomp <- checkVersions hsc_env source_unchanged old_iface
852 ; return (recomp, Just old_iface) }
856 -- Try and read the old interface for the current module
857 -- from the .hi file left from the last time we compiled it
858 { let iface_path = msHiFilePath mod_summary
859 ; read_result <- readIface (ms_mod mod_summary) iface_path False
860 ; case read_result of {
861 Failed err -> do -- Old interface file not found, or garbled; give up
862 { traceIf (text "FYI: cannot read old interface file:"
864 ; return (outOfDate, Nothing) }
866 ; Succeeded iface -> do
868 -- We have got the old iface; check its versions
869 { traceIf (text "Read the interface file" <+> text iface_path)
870 ; recomp <- checkVersions hsc_env source_unchanged iface
871 ; returnM (recomp, Just iface)
875 @recompileRequired@ is called from the HscMain. It checks whether
876 a recompilation is required. It needs access to the persistent state,
877 finder, etc, because it may have to load lots of interface files to
878 check their versions.
881 type RecompileRequired = Bool
882 upToDate = False -- Recompile not required
883 outOfDate = True -- Recompile required
885 checkVersions :: HscEnv
886 -> Bool -- True <=> source unchanged
887 -> ModIface -- Old interface
888 -> IfG RecompileRequired
889 checkVersions hsc_env source_unchanged iface
890 | not source_unchanged
893 = do { traceHiDiffs (text "Considering whether compilation is required for" <+>
894 ppr (mi_module iface) <> colon)
896 -- Source code unchanged and no errors yet... carry on
898 -- First put the dependent-module info, read from the old interface, into the envt,
899 -- so that when we look for interfaces we look for the right one (.hi or .hi-boot)
901 -- It's just temporary because either the usage check will succeed
902 -- (in which case we are done with this module) or it'll fail (in which
903 -- case we'll compile the module from scratch anyhow).
905 -- We do this regardless of compilation mode, although in --make mode
906 -- all the dependent modules should be in the HPT already, so it's
908 ; updateEps_ $ \eps -> eps { eps_is_boot = mod_deps }
910 ; let this_pkg = thisPackage (hsc_dflags hsc_env)
911 ; checkList [checkModUsage this_pkg u | u <- mi_usages iface]
914 -- This is a bit of a hack really
915 mod_deps :: ModuleNameEnv (ModuleName, IsBootInterface)
916 mod_deps = mkModDeps (dep_mods (mi_deps iface))
918 checkModUsage :: PackageId ->Usage -> IfG RecompileRequired
919 -- Given the usage information extracted from the old
920 -- M.hi file for the module being compiled, figure out
921 -- whether M needs to be recompiled.
923 checkModUsage this_pkg (Usage { usg_name = mod_name, usg_mod = old_mod_vers,
924 usg_rules = old_rule_vers,
925 usg_exports = maybe_old_export_vers,
926 usg_entities = old_decl_vers })
927 = -- Load the imported interface is possible
929 doc_str = sep [ptext SLIT("need version info for"), ppr mod_name]
931 traceHiDiffs (text "Checking usages for module" <+> ppr mod_name) `thenM_`
934 mod = mkModule this_pkg mod_name
936 loadInterface doc_str mod ImportBySystem `thenM` \ mb_iface ->
937 -- Load the interface, but don't complain on failure;
938 -- Instead, get an Either back which we can test
941 Failed exn -> (out_of_date (sep [ptext SLIT("Can't find version number for module"),
943 -- Couldn't find or parse a module mentioned in the
944 -- old interface file. Don't complain -- it might just be that
945 -- the current module doesn't need that import and it's been deleted
949 new_mod_vers = mi_mod_vers iface
950 new_decl_vers = mi_ver_fn iface
951 new_export_vers = mi_exp_vers iface
952 new_rule_vers = mi_rule_vers iface
955 checkModuleVersion old_mod_vers new_mod_vers `thenM` \ recompile ->
956 if not recompile then
961 if checkExportList maybe_old_export_vers new_export_vers then
962 out_of_date_vers (ptext SLIT(" Export list changed"))
963 (expectJust "checkModUsage" maybe_old_export_vers)
968 if old_rule_vers /= new_rule_vers then
969 out_of_date_vers (ptext SLIT(" Rules changed"))
970 old_rule_vers new_rule_vers
973 -- CHECK ITEMS ONE BY ONE
974 checkList [checkEntityUsage new_decl_vers u | u <- old_decl_vers] `thenM` \ recompile ->
976 returnM outOfDate -- This one failed, so just bail out now
978 up_to_date (ptext SLIT(" Great! The bits I use are up to date"))
981 ------------------------
982 checkModuleVersion old_mod_vers new_mod_vers
983 | new_mod_vers == old_mod_vers
984 = up_to_date (ptext SLIT("Module version unchanged"))
987 = out_of_date_vers (ptext SLIT(" Module version has changed"))
988 old_mod_vers new_mod_vers
990 ------------------------
991 checkExportList Nothing new_vers = upToDate
992 checkExportList (Just v) new_vers = v /= new_vers
994 ------------------------
995 checkEntityUsage new_vers (name,old_vers)
996 = case new_vers name of
998 Nothing -> -- We used it before, but it ain't there now
999 out_of_date (sep [ptext SLIT("No longer exported:"), ppr name])
1001 Just (_, new_vers) -- It's there, but is it up to date?
1002 | new_vers == old_vers -> traceHiDiffs (text " Up to date" <+> ppr name <+> parens (ppr new_vers)) `thenM_`
1004 | otherwise -> out_of_date_vers (ptext SLIT(" Out of date:") <+> ppr name)
1007 up_to_date msg = traceHiDiffs msg `thenM_` returnM upToDate
1008 out_of_date msg = traceHiDiffs msg `thenM_` returnM outOfDate
1009 out_of_date_vers msg old_vers new_vers
1010 = out_of_date (hsep [msg, ppr old_vers, ptext SLIT("->"), ppr new_vers])
1012 ----------------------
1013 checkList :: [IfG RecompileRequired] -> IfG RecompileRequired
1014 -- This helper is used in two places
1015 checkList [] = returnM upToDate
1016 checkList (check:checks) = check `thenM` \ recompile ->
1023 %************************************************************************
1025 Converting things to their Iface equivalents
1027 %************************************************************************
1030 tyThingToIfaceDecl :: TyThing -> IfaceDecl
1031 -- Assumption: the thing is already tidied, so that locally-bound names
1032 -- (lambdas, for-alls) already have non-clashing OccNames
1033 -- Reason: Iface stuff uses OccNames, and the conversion here does
1034 -- not do tidying on the way
1035 tyThingToIfaceDecl (AnId id)
1036 = IfaceId { ifName = getOccName id,
1037 ifType = toIfaceType (idType id),
1040 info = case toIfaceIdInfo (idInfo id) of
1042 items -> HasInfo items
1044 tyThingToIfaceDecl (AClass clas)
1045 = IfaceClass { ifCtxt = toIfaceContext sc_theta,
1046 ifName = getOccName clas,
1047 ifTyVars = toIfaceTvBndrs clas_tyvars,
1048 ifFDs = map toIfaceFD clas_fds,
1049 ifATs = map (tyThingToIfaceDecl . ATyCon) clas_ats,
1050 ifSigs = map toIfaceClassOp op_stuff,
1051 ifRec = boolToRecFlag (isRecursiveTyCon tycon) }
1053 (clas_tyvars, clas_fds, sc_theta, _, clas_ats, op_stuff)
1054 = classExtraBigSig clas
1055 tycon = classTyCon clas
1057 toIfaceClassOp (sel_id, def_meth)
1058 = ASSERT(sel_tyvars == clas_tyvars)
1059 IfaceClassOp (getOccName sel_id) def_meth (toIfaceType op_ty)
1061 -- Be careful when splitting the type, because of things
1062 -- like class Foo a where
1063 -- op :: (?x :: String) => a -> a
1064 -- and class Baz a where
1065 -- op :: (Ord a) => a -> a
1066 (sel_tyvars, rho_ty) = splitForAllTys (idType sel_id)
1067 op_ty = funResultTy rho_ty
1069 toIfaceFD (tvs1, tvs2) = (map getFS tvs1, map getFS tvs2)
1071 tyThingToIfaceDecl (ATyCon tycon)
1073 = IfaceSyn { ifName = getOccName tycon,
1074 ifTyVars = toIfaceTvBndrs tyvars,
1075 ifOpenSyn = syn_isOpen,
1076 ifSynRhs = toIfaceType syn_tyki }
1079 = IfaceData { ifName = getOccName tycon,
1080 ifTyVars = toIfaceTvBndrs tyvars,
1081 ifCtxt = toIfaceContext (tyConStupidTheta tycon),
1082 ifCons = ifaceConDecls (algTyConRhs tycon),
1083 ifRec = boolToRecFlag (isRecursiveTyCon tycon),
1084 ifGadtSyntax = isGadtSyntaxTyCon tycon,
1085 ifGeneric = tyConHasGenerics tycon,
1086 ifFamInst = famInstToIface (tyConFamInst_maybe tycon)}
1088 | isForeignTyCon tycon
1089 = IfaceForeign { ifName = getOccName tycon,
1090 ifExtName = tyConExtName tycon }
1092 | otherwise = pprPanic "toIfaceDecl" (ppr tycon)
1094 tyvars = tyConTyVars tycon
1095 (syn_isOpen, syn_tyki) = case synTyConRhs tycon of
1096 OpenSynTyCon ki _ -> (True , ki)
1097 SynonymTyCon ty -> (False, ty)
1099 ifaceConDecls (NewTyCon { data_con = con }) =
1100 IfNewTyCon (ifaceConDecl con)
1101 ifaceConDecls (DataTyCon { data_cons = cons }) =
1102 IfDataTyCon (map ifaceConDecl cons)
1103 ifaceConDecls OpenTyCon { otIsNewtype = False } = IfOpenDataTyCon
1104 ifaceConDecls OpenTyCon { otIsNewtype = True } = IfOpenNewTyCon
1105 ifaceConDecls AbstractTyCon = IfAbstractTyCon
1106 -- The last case happens when a TyCon has been trimmed during tidying
1107 -- Furthermore, tyThingToIfaceDecl is also used
1108 -- in TcRnDriver for GHCi, when browsing a module, in which case the
1109 -- AbstractTyCon case is perfectly sensible.
1111 ifaceConDecl data_con
1112 = IfCon { ifConOcc = getOccName (dataConName data_con),
1113 ifConInfix = dataConIsInfix data_con,
1114 ifConUnivTvs = toIfaceTvBndrs (dataConUnivTyVars data_con),
1115 ifConExTvs = toIfaceTvBndrs (dataConExTyVars data_con),
1116 ifConEqSpec = to_eq_spec (dataConEqSpec data_con),
1117 ifConCtxt = toIfaceContext (dataConTheta data_con),
1118 ifConArgTys = map toIfaceType (dataConOrigArgTys data_con),
1119 ifConFields = map getOccName
1120 (dataConFieldLabels data_con),
1121 ifConStricts = dataConStrictMarks data_con }
1123 to_eq_spec spec = [(getOccName tv, toIfaceType ty) | (tv,ty) <- spec]
1125 famInstToIface Nothing = Nothing
1126 famInstToIface (Just (famTyCon, instTys)) =
1127 Just (toIfaceTyCon famTyCon, map toIfaceType instTys)
1129 tyThingToIfaceDecl (ADataCon dc)
1130 = pprPanic "toIfaceDecl" (ppr dc) -- Should be trimmed out earlier
1133 getFS x = occNameFS (getOccName x)
1135 --------------------------
1136 instanceToIfaceInst :: Instance -> IfaceInst
1137 instanceToIfaceInst ispec@(Instance { is_dfun = dfun_id, is_flag = oflag,
1138 is_cls = cls_name, is_tcs = mb_tcs })
1139 = ASSERT( cls_name == className cls )
1140 IfaceInst { ifDFun = dfun_name,
1142 ifInstCls = cls_name,
1143 ifInstTys = map do_rough mb_tcs,
1146 do_rough Nothing = Nothing
1147 do_rough (Just n) = Just (toIfaceTyCon_name n)
1149 dfun_name = idName dfun_id
1150 mod = nameModule dfun_name
1151 is_local name = nameIsLocalOrFrom mod name
1153 -- Compute orphanhood. See Note [Orphans] in IfaceSyn
1154 (_, _, cls, tys) = tcSplitDFunTy (idType dfun_id)
1155 -- Slightly awkward: we need the Class to get the fundeps
1156 (tvs, fds) = classTvsFds cls
1157 arg_names = [filterNameSet is_local (tyClsNamesOfType ty) | ty <- tys]
1158 orph | is_local cls_name = Just (nameOccName cls_name)
1159 | all isJust mb_ns = head mb_ns
1160 | otherwise = Nothing
1162 mb_ns :: [Maybe OccName] -- One for each fundep; a locally-defined name
1163 -- that is not in the "determined" arguments
1164 mb_ns | null fds = [choose_one arg_names]
1165 | otherwise = map do_one fds
1166 do_one (ltvs,rtvs) = choose_one [ns | (tv,ns) <- tvs `zip` arg_names
1167 , not (tv `elem` rtvs)]
1169 choose_one :: [NameSet] -> Maybe OccName
1170 choose_one nss = case nameSetToList (unionManyNameSets nss) of
1172 (n:ns) -> Just (nameOccName n)
1174 --------------------------
1175 famInstToIfaceFamInst :: FamInst -> IfaceFamInst
1176 famInstToIfaceFamInst fi@(FamInst { fi_tycon = tycon,
1177 fi_fam = fam, fi_tcs = mb_tcs })
1178 = IfaceFamInst { ifFamInstTyCon = toIfaceTyCon tycon
1179 , ifFamInstFam = fam
1180 , ifFamInstTys = map do_rough mb_tcs }
1182 do_rough Nothing = Nothing
1183 do_rough (Just n) = Just (toIfaceTyCon_name n)
1185 --------------------------
1186 toIfaceLetBndr id = IfLetBndr (occNameFS (getOccName id))
1187 (toIfaceType (idType id))
1190 -- Stripped-down version of tcIfaceIdInfo
1191 -- Change this if you want to export more IdInfo for
1192 -- non-top-level Ids. Don't forget to change
1193 -- CoreTidy.tidyLetBndr too!
1195 -- See Note [IdInfo on nested let-bindings] in IfaceSyn
1197 inline_prag = inlinePragInfo id_info
1198 prag_info | isAlwaysActive inline_prag = NoInfo
1199 | otherwise = HasInfo [HsInline inline_prag]
1201 --------------------------
1202 toIfaceIdInfo :: IdInfo -> [IfaceInfoItem]
1203 toIfaceIdInfo id_info
1204 = catMaybes [arity_hsinfo, caf_hsinfo, strict_hsinfo,
1205 inline_hsinfo, wrkr_hsinfo, unfold_hsinfo]
1207 ------------ Arity --------------
1208 arity_info = arityInfo id_info
1209 arity_hsinfo | arity_info == 0 = Nothing
1210 | otherwise = Just (HsArity arity_info)
1212 ------------ Caf Info --------------
1213 caf_info = cafInfo id_info
1214 caf_hsinfo = case caf_info of
1215 NoCafRefs -> Just HsNoCafRefs
1218 ------------ Strictness --------------
1219 -- No point in explicitly exporting TopSig
1220 strict_hsinfo = case newStrictnessInfo id_info of
1221 Just sig | not (isTopSig sig) -> Just (HsStrictness sig)
1224 ------------ Worker --------------
1225 work_info = workerInfo id_info
1226 has_worker = case work_info of { HasWorker _ _ -> True; other -> False }
1227 wrkr_hsinfo = case work_info of
1228 HasWorker work_id wrap_arity ->
1229 Just (HsWorker ((idName work_id)) wrap_arity)
1232 ------------ Unfolding --------------
1233 -- The unfolding is redundant if there is a worker
1234 unfold_info = unfoldingInfo id_info
1235 rhs = unfoldingTemplate unfold_info
1236 no_unfolding = neverUnfold unfold_info
1237 -- The CoreTidy phase retains unfolding info iff
1238 -- we want to expose the unfolding, taking into account
1239 -- unconditional NOINLINE, etc. See TidyPgm.addExternal
1240 unfold_hsinfo | no_unfolding = Nothing
1241 | has_worker = Nothing -- Unfolding is implicit
1242 | otherwise = Just (HsUnfold (toIfaceExpr rhs))
1244 ------------ Inline prag --------------
1245 inline_prag = inlinePragInfo id_info
1246 inline_hsinfo | isAlwaysActive inline_prag = Nothing
1247 | no_unfolding && not has_worker = Nothing
1248 -- If the iface file give no unfolding info, we
1249 -- don't need to say when inlining is OK!
1250 | otherwise = Just (HsInline inline_prag)
1252 --------------------------
1253 coreRuleToIfaceRule :: Module -> CoreRule -> IfaceRule
1254 coreRuleToIfaceRule mod (BuiltinRule { ru_fn = fn})
1255 = pprTrace "toHsRule: builtin" (ppr fn) $
1258 coreRuleToIfaceRule mod (Rule { ru_name = name, ru_fn = fn,
1259 ru_act = act, ru_bndrs = bndrs,
1260 ru_args = args, ru_rhs = rhs })
1261 = IfaceRule { ifRuleName = name, ifActivation = act,
1262 ifRuleBndrs = map toIfaceBndr bndrs,
1264 ifRuleArgs = map do_arg args,
1265 ifRuleRhs = toIfaceExpr rhs,
1268 -- For type args we must remove synonyms from the outermost
1269 -- level. Reason: so that when we read it back in we'll
1270 -- construct the same ru_rough field as we have right now;
1272 do_arg (Type ty) = IfaceType (toIfaceType (deNoteType ty))
1273 do_arg arg = toIfaceExpr arg
1275 -- Compute orphanhood. See Note [Orphans] in IfaceSyn
1276 -- A rule is an orphan only if none of the variables
1277 -- mentioned on its left-hand side are locally defined
1278 lhs_names = fn : nameSetToList (exprsFreeNames args)
1279 -- No need to delete bndrs, because
1280 -- exprsFreeNames finds only External names
1282 orph = case filter (nameIsLocalOrFrom mod) lhs_names of
1283 (n:ns) -> Just (nameOccName n)
1286 bogusIfaceRule :: Name -> IfaceRule
1287 bogusIfaceRule id_name
1288 = IfaceRule { ifRuleName = FSLIT("bogus"), ifActivation = NeverActive,
1289 ifRuleBndrs = [], ifRuleHead = id_name, ifRuleArgs = [],
1290 ifRuleRhs = IfaceExt id_name, ifRuleOrph = Nothing }
1292 ---------------------
1293 toIfaceExpr :: CoreExpr -> IfaceExpr
1294 toIfaceExpr (Var v) = toIfaceVar v
1295 toIfaceExpr (Lit l) = IfaceLit l
1296 toIfaceExpr (Type ty) = IfaceType (toIfaceType ty)
1297 toIfaceExpr (Lam x b) = IfaceLam (toIfaceBndr x) (toIfaceExpr b)
1298 toIfaceExpr (App f a) = toIfaceApp f [a]
1299 toIfaceExpr (Case s x ty as) = IfaceCase (toIfaceExpr s) (getFS x) (toIfaceType ty) (map toIfaceAlt as)
1300 toIfaceExpr (Let b e) = IfaceLet (toIfaceBind b) (toIfaceExpr e)
1301 toIfaceExpr (Cast e co) = IfaceCast (toIfaceExpr e) (toIfaceType co)
1302 toIfaceExpr (Note n e) = IfaceNote (toIfaceNote n) (toIfaceExpr e)
1304 ---------------------
1305 toIfaceNote (SCC cc) = IfaceSCC cc
1306 toIfaceNote InlineMe = IfaceInlineMe
1307 toIfaceNote (CoreNote s) = IfaceCoreNote s
1309 ---------------------
1310 toIfaceBind (NonRec b r) = IfaceNonRec (toIfaceLetBndr b) (toIfaceExpr r)
1311 toIfaceBind (Rec prs) = IfaceRec [(toIfaceLetBndr b, toIfaceExpr r) | (b,r) <- prs]
1313 ---------------------
1314 toIfaceAlt (c,bs,r) = (toIfaceCon c, map getFS bs, toIfaceExpr r)
1316 ---------------------
1317 toIfaceCon (DataAlt dc) | isTupleTyCon tc = IfaceTupleAlt (tupleTyConBoxity tc)
1318 | otherwise = IfaceDataAlt (getName dc)
1320 tc = dataConTyCon dc
1322 toIfaceCon (LitAlt l) = IfaceLitAlt l
1323 toIfaceCon DEFAULT = IfaceDefault
1325 ---------------------
1326 toIfaceApp (App f a) as = toIfaceApp f (a:as)
1327 toIfaceApp (Var v) as
1328 = case isDataConWorkId_maybe v of
1329 -- We convert the *worker* for tuples into IfaceTuples
1330 Just dc | isTupleTyCon tc && saturated
1331 -> IfaceTuple (tupleTyConBoxity tc) tup_args
1333 val_args = dropWhile isTypeArg as
1334 saturated = val_args `lengthIs` idArity v
1335 tup_args = map toIfaceExpr val_args
1336 tc = dataConTyCon dc
1338 other -> mkIfaceApps (toIfaceVar v) as
1340 toIfaceApp e as = mkIfaceApps (toIfaceExpr e) as
1342 mkIfaceApps f as = foldl (\f a -> IfaceApp f (toIfaceExpr a)) f as
1344 ---------------------
1345 toIfaceVar :: Id -> IfaceExpr
1347 | Just fcall <- isFCallId_maybe v = IfaceFCall fcall (toIfaceType (idType v))
1348 -- Foreign calls have special syntax
1349 | isExternalName name = IfaceExt name
1350 | otherwise = IfaceLcl (getFS name)