2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
7 mkUsageInfo, -- Construct the usage info for a module
9 mkIface, -- Build a ModIface from a ModGuts,
10 -- including computing version information
12 writeIfaceFile, -- Write the interface file
14 checkOldIface, -- See if recompilation is required, by
15 -- comparing version information
17 tyThingToIfaceDecl -- Converting things to their Iface equivalents
21 -----------------------------------------------
22 MkIface.lhs deals with versioning
23 -----------------------------------------------
25 Here's the version-related info in an interface file
27 module Foo 8 -- module-version
28 3 -- export-list-version
30 Usages: -- Version info for what this compilation of Foo imported
31 Baz 3 -- Module version
32 [4] -- The export-list version if Foo depended on it
33 (g,2) -- Function and its version
34 (T,1) -- Type and its version
36 <version> f :: Int -> Int {- Unfolding: \x -> Wib.t[2] x -}
37 -- The [2] says that f's unfolding
38 -- mentions verison 2 of Wib.t
40 -----------------------------------------------
42 -----------------------------------------------
45 * In the mi_usages information in an interface, we record the
46 version number of each free variable of the module
48 * In mkIface, we compute the version number of each exported thing A.f
49 by comparing its A.f's info with its new info, and bumping its
50 version number if it differs. If A.f mentions B.g, and B.g's version
51 number has changed, then we count A.f as having changed too.
53 * In checkOldIface we compare the mi_usages for the module with
54 the actual version info for all each thing recorded in mi_usages
59 We count A.f as changing if its fixity changes
63 If a rule changes, we want to recompile any module that might be
64 affected by that rule. For non-orphan rules, this is relatively easy.
65 If module M defines f, and a rule for f, just arrange that the version
66 number for M.f changes if any of the rules for M.f change. Any module
67 that does not depend on M.f can't be affected by the rule-change
70 Orphan rules (ones whose 'head function' is not defined in M) are
71 harder. Here's what we do.
73 * We have a per-module orphan-rule version number which changes if
74 any orphan rule changes. (It's unaffected by non-orphan rules.)
76 * We record usage info for any orphan module 'below' this one,
77 giving the orphan-rule version number. We recompile if this
80 The net effect is that if an orphan rule changes, we recompile every
81 module above it. That's very conservative, but it's devilishly hard
82 to know what it might affect, so we just have to be conservative.
86 In an iface file we have
88 instance Eq a => Eq [a] = dfun29
91 We have a version number for dfun29, covering its unfolding
92 etc. Suppose we are compiling a module M that imports A only
93 indirectly. If typechecking M uses this instance decl, we record the
94 dependency on A.dfun29 as if it were a free variable of the module
95 (via the tcg_inst_usages accumulator). That means that A will appear
96 in M's usage list. If the shape of the instance declaration changes,
97 then so will dfun29's version, triggering a recompilation.
99 Adding an instance declaration, or changing an instance decl that is
100 not currently used, is more tricky. (This really only makes a
101 difference when we have overlapping instance decls, because then the
102 new instance decl might kick in to override the old one.) We handle
103 this in a very similar way that we handle rules above.
105 * For non-orphan instance decls, identify one locally-defined tycon/class
106 mentioned in the decl. Treat the instance decl as part of the defn of that
107 tycon/class, so that if the shape of the instance decl changes, so does the
108 tycon/class; that in turn will force recompilation of anything that uses
111 * For orphan instance decls, act the same way as for orphan rules.
112 Indeed, we use the same global orphan-rule version number.
116 mkUsageInfo figures out what the ``usage information'' for this
117 moudule is; that is, what it must record in its interface file as the
120 We produce a line for every module B below the module, A, currently being
123 to record the fact that A does import B indirectly. This is used to decide
124 to look to look for B.hi rather than B.hi-boot when compiling a module that
125 imports A. This line says that A imports B, but uses nothing in it.
126 So we'll get an early bale-out when compiling A if B's version changes.
128 The usage information records:
131 \item (a) anything reachable from its body code
132 \item (b) any module exported with a @module Foo@
133 \item (c) anything reachable from an exported item
136 Why (b)? Because if @Foo@ changes then this module's export list
137 will change, so we must recompile this module at least as far as
138 making a new interface file --- but in practice that means complete
141 Why (c)? Consider this:
143 module A( f, g ) where | module B( f ) where
144 import B( f ) | f = h 3
148 Here, @B.f@ isn't used in A. Should we nevertheless record @B.f@ in
149 @A@'s usages? Our idea is that we aren't going to touch A.hi if it is
150 *identical* to what it was before. If anything about @B.f@ changes
151 than anyone who imports @A@ should be recompiled in case they use
152 @B.f@ (they'll get an early exit if they don't). So, if anything
153 about @B.f@ changes we'd better make sure that something in A.hi
154 changes, and the convenient way to do that is to record the version
155 number @B.f@ in A.hi in the usage list. If B.f changes that'll force a
156 complete recompiation of A, which is overkill but it's the only way to
157 write a new, slightly different, A.hi.
159 But the example is tricker. Even if @B.f@ doesn't change at all,
160 @B.h@ may do so, and this change may not be reflected in @f@'s version
161 number. But with -O, a module that imports A must be recompiled if
162 @B.h@ changes! So A must record a dependency on @B.h@. So we treat
163 the occurrence of @B.f@ in the export list *just as if* it were in the
164 code of A, and thereby haul in all the stuff reachable from it.
166 *** Conclusion: if A mentions B.f in its export list,
167 behave just as if A mentioned B.f in its source code,
168 and slurp in B.f and all its transitive closure ***
170 [NB: If B was compiled with -O, but A isn't, we should really *still*
171 haul in all the unfoldings for B, in case the module that imports A *is*
172 compiled with -O. I think this is the case.]
176 #include "HsVersions.h"
178 import IfaceSyn -- All of it
179 import IfaceType ( toIfaceTvBndrs, toIfaceType, toIfaceContext )
180 import LoadIface ( readIface, loadInterface, pprModIface )
181 import Id ( Id, idName, idType, idInfo, idArity, isDataConWorkId_maybe, isFCallId_maybe )
182 import IdInfo ( IdInfo, CafInfo(..), WorkerInfo(..),
183 arityInfo, cafInfo, newStrictnessInfo,
184 workerInfo, unfoldingInfo, inlinePragInfo )
185 import NewDemand ( isTopSig )
187 import Class ( classExtraBigSig, classTyCon )
188 import TyCon ( TyCon, AlgTyConRhs(..), SynTyConRhs(..),
189 isRecursiveTyCon, isForeignTyCon,
190 isSynTyCon, isAlgTyCon, isPrimTyCon, isFunTyCon,
191 isTupleTyCon, tupleTyConBoxity, tyConStupidTheta,
192 tyConHasGenerics, synTyConRhs, isGadtSyntaxTyCon,
193 tyConArity, tyConTyVars, algTyConRhs, tyConExtName )
194 import DataCon ( dataConName, dataConFieldLabels, dataConStrictMarks,
195 dataConTyCon, dataConIsInfix, dataConUnivTyVars, dataConExTyVars, dataConEqSpec,
196 dataConTheta, dataConOrigArgTys )
197 import Type ( TyThing(..), splitForAllTys, funResultTy )
198 import TcType ( deNoteType )
199 import TysPrim ( alphaTyVars )
200 import InstEnv ( Instance(..) )
202 import HscTypes ( ModIface(..), ModDetails(..),
203 ModGuts(..), HscEnv(..), hscEPS, Dependencies(..), FixItem(..),
204 ModSummary(..), msHiFilePath,
205 mkIfaceDepCache, mkIfaceFixCache, mkIfaceVerCache,
207 GenAvailInfo(..), availName,
208 ExternalPackageState(..),
209 Usage(..), IsBootInterface,
210 Deprecs(..), IfaceDeprecs, Deprecations,
215 import DynFlags ( GhcMode(..), DynFlags(..), DynFlag(..), dopt )
216 import Name ( Name, nameModule, nameOccName, nameParent,
217 isExternalName, isInternalName, nameParent_maybe, isWiredInName,
218 isImplicitName, NamedThing(..) )
221 import OccName ( OccName, OccEnv, mkOccEnv, lookupOccEnv, emptyOccEnv,
223 OccSet, emptyOccSet, elemOccSet, occSetElts,
224 extendOccSet, extendOccSetList,
225 isEmptyOccSet, intersectOccSet, intersectsOccSet,
229 import BasicTypes ( Version, initialVersion, bumpVersion, isAlwaysActive,
230 Activation(..), RecFlag(..), boolToRecFlag )
232 import Util ( createDirectoryHierarchy, directoryOf, sortLe, seqList, lengthIs )
233 import BinIface ( writeBinIface )
234 import Unique ( Unique, Uniquable(..) )
235 import ErrUtils ( dumpIfSet_dyn, showPass )
236 import Digraph ( stronglyConnComp, SCC(..) )
237 import SrcLoc ( SrcSpan )
239 import PackageConfig ( PackageId )
243 import Monad ( when )
244 import List ( insert )
245 import Maybes ( orElse, mapCatMaybes, isNothing, isJust,
246 expectJust, catMaybes, MaybeErr(..) )
251 %************************************************************************
253 \subsection{Completing an interface}
255 %************************************************************************
259 -> Maybe ModIface -- The old interface, if we have it
260 -> ModGuts -- Usages, deprecations, etc
261 -> ModDetails -- The trimmed, tidied interface
262 -> IO (ModIface, -- The new one, complete with decls and versions
263 Bool) -- True <=> there was an old Iface, and the new one
264 -- is identical, so no need to write it
266 mkIface hsc_env maybe_old_iface
267 (ModGuts{ mg_module = this_mod,
271 mg_rdr_env = rdr_env,
272 mg_fix_env = fix_env,
273 mg_deprecs = src_deprecs })
274 (ModDetails{ md_insts = insts,
277 md_exports = exports })
279 -- NB: notice that mkIface does not look at the bindings
280 -- only at the TypeEnv. The previous Tidy phase has
281 -- put exactly the info into the TypeEnv that we want
282 -- to expose in the interface
284 = do { eps <- hscEPS hsc_env
285 ; let { ext_nm_rhs = mkExtNameFn hsc_env eps this_mod
286 ; ext_nm_lhs = mkLhsNameFn this_mod
288 ; decls = [ tyThingToIfaceDecl ext_nm_rhs thing
289 | thing <- typeEnvElts type_env,
290 let name = getName thing,
291 not (isImplicitName name || isWiredInName name) ]
292 -- Don't put implicit Ids and class tycons in the interface file
293 -- Nor wired-in things; the compiler knows about them anyhow
295 ; fixities = [(occ,fix) | FixItem occ fix _ <- nameEnvElts fix_env]
296 ; deprecs = mkIfaceDeprec src_deprecs
297 ; iface_rules = map (coreRuleToIfaceRule ext_nm_lhs ext_nm_rhs) rules
298 ; iface_insts = map (instanceToIfaceInst ext_nm_lhs) insts
300 ; intermediate_iface = ModIface {
301 mi_module = this_mod,
305 mi_exports = mkIfaceExports exports,
306 mi_insts = sortLe le_inst iface_insts,
307 mi_rules = sortLe le_rule iface_rules,
308 mi_fixities = fixities,
309 mi_deprecs = deprecs,
310 mi_globals = Just rdr_env,
312 -- Left out deliberately: filled in by addVersionInfo
313 mi_mod_vers = initialVersion,
314 mi_exp_vers = initialVersion,
315 mi_rule_vers = initialVersion,
316 mi_orphan = False, -- Always set by addVersionInfo, but
317 -- it's a strict field, so we can't omit it.
318 mi_decls = deliberatelyOmitted "decls",
319 mi_ver_fn = deliberatelyOmitted "ver_fn",
321 -- And build the cached values
322 mi_dep_fn = mkIfaceDepCache deprecs,
323 mi_fix_fn = mkIfaceFixCache fixities }
325 -- Add version information
326 ; (new_iface, no_change_at_all, pp_diffs, pp_orphs)
327 = _scc_ "versioninfo"
328 addVersionInfo maybe_old_iface intermediate_iface decls
332 ; when (isJust pp_orphs && dopt Opt_WarnOrphans dflags)
333 (printDump (expectJust "mkIface" pp_orphs))
334 ; when (dopt Opt_D_dump_hi_diffs dflags) (printDump pp_diffs)
335 ; dumpIfSet_dyn dflags Opt_D_dump_hi "FINAL INTERFACE"
336 (pprModIface new_iface)
338 ; return (new_iface, no_change_at_all) }
340 r1 `le_rule` r2 = ifRuleName r1 <= ifRuleName r2
341 i1 `le_inst` i2 = ifDFun i1 <= ifDFun i2
343 dflags = hsc_dflags hsc_env
344 deliberatelyOmitted x = panic ("Deliberately omitted: " ++ x)
347 -----------------------------
348 writeIfaceFile :: ModLocation -> ModIface -> IO ()
349 writeIfaceFile location new_iface
350 = do createDirectoryHierarchy (directoryOf hi_file_path)
351 writeBinIface hi_file_path new_iface
352 where hi_file_path = ml_hi_file location
355 -----------------------------
356 mkExtNameFn :: HscEnv -> ExternalPackageState -> Module -> Name -> IfaceExtName
357 mkExtNameFn hsc_env eps this_mod
360 hpt = hsc_HPT hsc_env
364 | mod == this_mod = case nameParent_maybe name of
365 Nothing -> LocalTop occ
366 Just par -> LocalTopSub occ (nameOccName par)
367 | isWiredInName name = ExtPkg mod occ
368 | is_home mod = HomePkg mod_name occ vers
369 | otherwise = ExtPkg mod occ
371 dflags = hsc_dflags hsc_env
372 this_pkg = thisPackage dflags
373 is_home mod = modulePackageId mod == this_pkg
375 mod = nameModule name
376 mod_name = moduleName mod
377 occ = nameOccName name
378 par_occ = nameOccName (nameParent name)
379 -- The version of the *parent* is the one want
380 vers = lookupVersion mod par_occ
382 lookupVersion :: Module -> OccName -> Version
383 -- Even though we're looking up a home-package thing, in
384 -- one-shot mode the imported interfaces may be in the PIT
385 lookupVersion mod occ
386 = mi_ver_fn iface occ `orElse`
387 pprPanic "lookupVers1" (ppr mod <+> ppr occ)
389 iface = lookupIfaceByModule (hsc_dflags hsc_env) hpt pit mod `orElse`
390 pprPanic "lookupVers2" (ppr mod <+> ppr occ)
393 ---------------------
394 -- mkLhsNameFn ignores versioning info altogether
395 -- It is used for the LHS of instance decls and rules, where we
396 -- there's no point in recording version info
397 mkLhsNameFn :: Module -> Name -> IfaceExtName
398 mkLhsNameFn this_mod name
399 | isInternalName name = pprTrace "mkLhsNameFn: unexpected internal" (ppr name) $
400 LocalTop occ -- Should not happen
401 | mod == this_mod = LocalTop occ
402 | otherwise = ExtPkg mod occ
404 mod = nameModule name
405 occ = nameOccName name
408 -----------------------------
409 -- Compute version numbers for local decls
411 addVersionInfo :: Maybe ModIface -- The old interface, read from M.hi
412 -> ModIface -- The new interface decls (lacking decls)
413 -> [IfaceDecl] -- The new decls
415 Bool, -- True <=> no changes at all; no need to write new Iface
417 Maybe SDoc) -- Warnings about orphans
419 addVersionInfo Nothing new_iface new_decls
420 -- No old interface, so definitely write a new one!
421 = (new_iface { mi_orphan = anyNothing ifInstOrph (mi_insts new_iface)
422 || anyNothing ifRuleOrph (mi_rules new_iface),
423 mi_decls = [(initialVersion, decl) | decl <- new_decls],
424 mi_ver_fn = \n -> Just initialVersion },
426 ptext SLIT("No old interface file"),
427 pprOrphans orph_insts orph_rules)
429 orph_insts = filter (isNothing . ifInstOrph) (mi_insts new_iface)
430 orph_rules = filter (isNothing . ifRuleOrph) (mi_rules new_iface)
432 addVersionInfo (Just old_iface@(ModIface { mi_mod_vers = old_mod_vers,
433 mi_exp_vers = old_exp_vers,
434 mi_rule_vers = old_rule_vers,
435 mi_decls = old_decls,
436 mi_ver_fn = old_decl_vers,
437 mi_fix_fn = old_fixities }))
438 new_iface@(ModIface { mi_fix_fn = new_fixities })
441 | no_change_at_all = (old_iface, True, ptext SLIT("Interface file unchanged"), pp_orphs)
442 | otherwise = (final_iface, False, vcat [ptext SLIT("Interface file has changed"),
443 nest 2 pp_diffs], pp_orphs)
445 final_iface = new_iface { mi_mod_vers = bump_unless no_output_change old_mod_vers,
446 mi_exp_vers = bump_unless no_export_change old_exp_vers,
447 mi_rule_vers = bump_unless no_rule_change old_rule_vers,
448 mi_orphan = not (null new_orph_rules && null new_orph_insts),
449 mi_decls = decls_w_vers,
450 mi_ver_fn = mkIfaceVerCache decls_w_vers }
452 decls_w_vers = [(add_vers decl, decl) | decl <- new_decls]
455 (old_non_orph_insts, old_orph_insts) = mkOrphMap ifInstOrph (mi_insts old_iface)
456 (new_non_orph_insts, new_orph_insts) = mkOrphMap ifInstOrph (mi_insts new_iface)
457 same_insts occ = eqMaybeBy (eqListBy eqIfInst)
458 (lookupOccEnv old_non_orph_insts occ)
459 (lookupOccEnv new_non_orph_insts occ)
461 (old_non_orph_rules, old_orph_rules) = mkOrphMap ifRuleOrph (mi_rules old_iface)
462 (new_non_orph_rules, new_orph_rules) = mkOrphMap ifRuleOrph (mi_rules new_iface)
463 same_rules occ = eqMaybeBy (eqListBy eqIfRule)
464 (lookupOccEnv old_non_orph_rules occ)
465 (lookupOccEnv new_non_orph_rules occ)
467 -- Computing what changed
468 no_output_change = no_decl_change && no_rule_change &&
469 no_export_change && no_deprec_change
470 no_export_change = mi_exports new_iface == mi_exports old_iface -- Kept sorted
471 no_decl_change = isEmptyOccSet changed_occs
472 no_rule_change = not (changedWrt changed_occs (eqListBy eqIfRule old_orph_rules new_orph_rules)
473 || changedWrt changed_occs (eqListBy eqIfInst old_orph_insts new_orph_insts))
474 no_deprec_change = mi_deprecs new_iface == mi_deprecs old_iface
476 -- If the usages havn't changed either, we don't need to write the interface file
477 no_other_changes = mi_usages new_iface == mi_usages old_iface &&
478 mi_deps new_iface == mi_deps old_iface
479 no_change_at_all = no_output_change && no_other_changes
481 pp_diffs = vcat [pp_change no_export_change "Export list"
482 (ppr old_exp_vers <+> arrow <+> ppr (mi_exp_vers final_iface)),
483 pp_change no_rule_change "Rules"
484 (ppr old_rule_vers <+> arrow <+> ppr (mi_rule_vers final_iface)),
485 pp_change no_deprec_change "Deprecations" empty,
486 pp_change no_other_changes "Usages" empty,
488 pp_change True what info = empty
489 pp_change False what info = text what <+> ptext SLIT("changed") <+> info
492 old_decl_env = mkOccEnv [(ifName decl, decl) | (_,decl) <- old_decls]
493 same_fixity n = bool (old_fixities n == new_fixities n)
496 -- Adding version info
497 new_version = bumpVersion old_mod_vers -- Start from the old module version, not from zero
498 -- so that if you remove f, and then add it again,
499 -- you don't thereby reduce f's version number
500 add_vers decl | occ `elemOccSet` changed_occs = new_version
501 | otherwise = expectJust "add_vers" (old_decl_vers occ)
502 -- If it's unchanged, there jolly well
503 where -- should be an old version number
507 changed_occs :: OccSet
508 changed_occs = computeChangedOccs eq_info
510 eq_info :: [(OccName, IfaceEq)]
511 eq_info = map check_eq new_decls
512 check_eq new_decl | Just old_decl <- lookupOccEnv old_decl_env occ
513 = (occ, new_decl `eqIfDecl` old_decl &&&
514 eq_indirects new_decl)
515 | otherwise {- No corresponding old decl -}
518 occ = ifName new_decl
520 eq_indirects :: IfaceDecl -> IfaceEq
521 -- When seeing if two decls are the same, remember to
522 -- check whether any relevant fixity or rules have changed
523 eq_indirects (IfaceId {ifName = occ}) = eq_ind_occ occ
524 eq_indirects (IfaceClass {ifName = cls_occ, ifSigs = sigs})
525 = same_insts cls_occ &&&
526 eq_ind_occs [op | IfaceClassOp op _ _ <- sigs]
527 eq_indirects (IfaceData {ifName = tc_occ, ifCons = cons})
528 = same_insts tc_occ &&& same_fixity tc_occ &&& -- The TyCon can have a fixity too
529 eq_ind_occs (map ifConOcc (visibleIfConDecls cons))
530 eq_indirects other = Equal -- Synonyms and foreign declarations
532 eq_ind_occ :: OccName -> IfaceEq -- For class ops and Ids; check fixity and rules
533 eq_ind_occ occ = same_fixity occ &&& same_rules occ
534 eq_ind_occs = foldr ((&&&) . eq_ind_occ) Equal
538 pp_decl_diffs :: SDoc -- Nothing => no changes
540 | isEmptyOccSet changed_occs = empty
542 = vcat [ptext SLIT("Changed occs:") <+> ppr (occSetElts changed_occs),
543 ptext SLIT("Version change for these decls:"),
544 nest 2 (vcat (map show_change new_decls))]
546 eq_env = mkOccEnv eq_info
548 | not (occ `elemOccSet` changed_occs) = empty
550 = vcat [ppr occ <+> ppr (old_decl_vers occ) <+> arrow <+> ppr new_version,
553 occ = ifName new_decl
554 why = case lookupOccEnv eq_env occ of
555 Just (EqBut occs) -> sep [ppr occ <> colon, ptext SLIT("Free vars (only) changed:"),
556 nest 2 (braces (fsep (map ppr (occSetElts
557 (occs `intersectOccSet` changed_occs)))))]
559 | Just old_decl <- lookupOccEnv old_decl_env occ
560 -> vcat [ptext SLIT("Old:") <+> ppr old_decl,
561 ptext SLIT("New:") <+> ppr new_decl]
563 -> ppr occ <+> ptext SLIT("only in new interface")
564 other -> pprPanic "MkIface.show_change" (ppr occ)
566 pp_orphs = pprOrphans new_orph_insts new_orph_rules
568 pprOrphans insts rules
569 | null insts && null rules = Nothing
572 if null insts then empty else
573 hang (ptext SLIT("Warning: orphan instances:"))
574 2 (vcat (map ppr insts)),
575 if null rules then empty else
576 hang (ptext SLIT("Warning: orphan rules:"))
577 2 (vcat (map ppr rules))
580 computeChangedOccs :: [(OccName, IfaceEq)] -> OccSet
581 computeChangedOccs eq_info
582 = foldl add_changes emptyOccSet (stronglyConnComp edges)
584 edges :: [((OccName,IfaceEq), Unique, [Unique])]
585 edges = [ (node, getUnique occ, map getUnique occs)
586 | node@(occ, iface_eq) <- eq_info
587 , let occs = case iface_eq of
588 EqBut occ_set -> occSetElts occ_set
591 -- Changes in declarations
592 add_changes :: OccSet -> SCC (OccName, IfaceEq) -> OccSet
593 add_changes so_far (AcyclicSCC (occ, iface_eq))
594 | changedWrt so_far iface_eq -- This one has changed
595 = extendOccSet so_far occ
596 add_changes so_far (CyclicSCC pairs)
597 | changedWrt so_far (foldr1 (&&&) (map snd pairs)) -- One of this group has changed
598 = extendOccSetList so_far (map fst pairs)
599 add_changes so_far other = so_far
601 changedWrt :: OccSet -> IfaceEq -> Bool
602 changedWrt so_far Equal = False
603 changedWrt so_far NotEqual = True
604 changedWrt so_far (EqBut kids) = so_far `intersectsOccSet` kids
606 ----------------------
607 -- mkOrphMap partitions instance decls or rules into
608 -- (a) an OccEnv for ones that are not orphans,
609 -- mapping the local OccName to a list of its decls
610 -- (b) a list of orphan decls
611 mkOrphMap :: (decl -> Maybe OccName) -- (Just occ) for a non-orphan decl, keyed by occ
612 -- Nothing for an orphan decl
613 -> [decl] -- Sorted into canonical order
614 -> (OccEnv [decl], -- Non-orphan decls associated with their key;
615 -- each sublist in canonical order
616 [decl]) -- Orphan decls; in canonical order
617 mkOrphMap get_key decls
618 = foldl go (emptyOccEnv, []) decls
620 go (non_orphs, orphs) d
621 | Just occ <- get_key d
622 = (extendOccEnv_C (\ ds _ -> d:ds) non_orphs occ [d], orphs)
623 | otherwise = (non_orphs, d:orphs)
625 anyNothing :: (a -> Maybe b) -> [a] -> Bool
626 anyNothing p [] = False
627 anyNothing p (x:xs) = isNothing (p x) || anyNothing p xs
629 ----------------------
630 mkIfaceDeprec :: Deprecations -> IfaceDeprecs
631 mkIfaceDeprec NoDeprecs = NoDeprecs
632 mkIfaceDeprec (DeprecAll t) = DeprecAll t
633 mkIfaceDeprec (DeprecSome env) = DeprecSome (sortLe (<=) (nameEnvElts env))
635 ----------------------
636 bump_unless :: Bool -> Version -> Version
637 bump_unless True v = v -- True <=> no change
638 bump_unless False v = bumpVersion v
642 %*********************************************************
644 \subsection{Keeping track of what we've slurped, and version numbers}
646 %*********************************************************
650 mkUsageInfo :: HscEnv
651 -> ModuleEnv (Module, Bool, SrcSpan)
652 -> [(ModuleName, IsBootInterface)]
653 -> NameSet -> IO [Usage]
654 mkUsageInfo hsc_env dir_imp_mods dep_mods used_names
655 = do { eps <- hscEPS hsc_env
656 ; let usages = mk_usage_info (eps_PIT eps) hsc_env
657 dir_imp_mods dep_mods used_names
658 ; usages `seqList` return usages }
659 -- seq the list of Usages returned: occasionally these
660 -- don't get evaluated for a while and we can end up hanging on to
661 -- the entire collection of Ifaces.
663 mk_usage_info pit hsc_env dir_imp_mods dep_mods proto_used_names
664 = mapCatMaybes mkUsage dep_mods
665 -- ToDo: do we need to sort into canonical order?
667 hpt = hsc_HPT hsc_env
668 dflags = hsc_dflags hsc_env
670 used_names = mkNameSet $ -- Eliminate duplicates
671 [ nameParent n -- Just record usage on the 'main' names
672 | n <- nameSetToList proto_used_names
673 , not (isWiredInName n) -- Don't record usages for wired-in names
674 , isExternalName n -- Ignore internal names
677 -- ent_map groups together all the things imported and used
678 -- from a particular module in this package
679 ent_map :: ModuleEnv [OccName]
680 ent_map = foldNameSet add_mv emptyModuleEnv used_names
681 add_mv name mv_map = extendModuleEnv_C add_item mv_map mod [occ]
683 occ = nameOccName name
684 mod = nameModule name
685 add_item occs _ = occ:occs
687 depend_on_exports mod = case lookupModuleEnv dir_imp_mods mod of
688 Just (_,no_imp,_) -> not no_imp
691 -- We want to create a Usage for a home module if
692 -- a) we used something from; has something in used_names
693 -- b) we imported all of it, even if we used nothing from it
694 -- (need to recompile if its export list changes: export_vers)
695 -- c) is a home-package orphan module (need to recompile if its
696 -- instance decls change: rules_vers)
697 mkUsage :: (ModuleName, IsBootInterface) -> Maybe Usage
698 mkUsage (mod_name, _)
699 | isNothing maybe_iface -- We can't depend on it if we didn't
700 || (null used_occs -- load its interface.
701 && isNothing export_vers
703 = Nothing -- Record no usage info
706 = Just (Usage { usg_name = mod_name,
708 usg_exports = export_vers,
709 usg_entities = ent_vers,
710 usg_rules = rules_vers })
712 maybe_iface = lookupIfaceByModule dflags hpt pit mod
713 -- In one-shot mode, the interfaces for home-package
714 -- modules accumulate in the PIT not HPT. Sigh.
716 mod = mkModule (thisPackage dflags) mod_name
718 Just iface = maybe_iface
719 orphan_mod = mi_orphan iface
720 version_env = mi_ver_fn iface
721 mod_vers = mi_mod_vers iface
722 rules_vers = mi_rule_vers iface
723 export_vers | depend_on_exports mod = Just (mi_exp_vers iface)
724 | otherwise = Nothing
726 -- The sort is to put them into canonical order
727 used_occs = lookupModuleEnv ent_map mod `orElse` []
728 ent_vers :: [(OccName,Version)]
729 ent_vers = [ (occ, version_env occ `orElse` initialVersion)
730 | occ <- sortLe (<=) used_occs]
734 mkIfaceExports :: NameSet -> [(Module, [GenAvailInfo OccName])]
735 -- Group by module and sort by occurrence
736 -- This keeps the list in canonical order
737 mkIfaceExports exports
738 = [ (mod, eltsUFM avails)
739 | (mod, avails) <- fmToList groupFM
742 groupFM :: ModuleEnv (UniqFM (GenAvailInfo OccName))
743 -- Deliberately use the FastString so we
744 -- get a canonical ordering
745 groupFM = foldl add emptyModuleEnv (nameSetToList exports)
747 add env name = extendModuleEnv_C add_avail env mod
748 (unitUFM avail_fs avail)
750 occ = nameOccName name
751 mod = nameModule name
752 avail | Just p <- nameParent_maybe name = AvailTC (nameOccName p) [occ]
753 | isTcOcc occ = AvailTC occ [occ]
754 | otherwise = Avail occ
755 avail_fs = occNameFS (availName avail)
756 add_avail avail_fm _ = addToUFM_C add_item avail_fm avail_fs avail
758 add_item (AvailTC p occs) _ = AvailTC p (List.insert occ occs)
759 add_item (Avail n) _ = pprPanic "MkIface.addAvail" (ppr n <+> ppr name)
763 %************************************************************************
765 Load the old interface file for this module (unless
766 we have it aleady), and check whether it is up to date
769 %************************************************************************
772 checkOldIface :: HscEnv
774 -> Bool -- Source unchanged
775 -> Maybe ModIface -- Old interface from compilation manager, if any
776 -> IO (RecompileRequired, Maybe ModIface)
778 checkOldIface hsc_env mod_summary source_unchanged maybe_iface
779 = do { showPass (hsc_dflags hsc_env)
780 ("Checking old interface for " ++
781 showSDoc (ppr (ms_mod mod_summary))) ;
783 ; initIfaceCheck hsc_env $
784 check_old_iface hsc_env mod_summary source_unchanged maybe_iface
787 check_old_iface hsc_env mod_summary source_unchanged maybe_iface
788 = -- CHECK WHETHER THE SOURCE HAS CHANGED
789 ifM (not source_unchanged)
790 (traceHiDiffs (nest 4 (text "Source file changed or recompilation check turned off")))
793 -- If the source has changed and we're in interactive mode, avoid reading
794 -- an interface; just return the one we might have been supplied with.
795 getGhcMode `thenM` \ ghc_mode ->
796 if (ghc_mode == Interactive || ghc_mode == JustTypecheck)
797 && not source_unchanged then
798 returnM (outOfDate, maybe_iface)
801 case maybe_iface of {
802 Just old_iface -> do -- Use the one we already have
803 recomp <- checkVersions hsc_env source_unchanged old_iface
804 return (recomp, Just old_iface)
808 -- Try and read the old interface for the current module
809 -- from the .hi file left from the last time we compiled it
811 iface_path = msHiFilePath mod_summary
813 readIface (ms_mod mod_summary) iface_path False `thenM` \ read_result ->
814 case read_result of {
815 Failed err -> -- Old interface file not found, or garbled; give up
816 traceIf (text "FYI: cannot read old interface file:"
817 $$ nest 4 err) `thenM_`
818 returnM (outOfDate, Nothing)
822 -- We have got the old iface; check its versions
823 checkVersions hsc_env source_unchanged iface `thenM` \ recomp ->
824 returnM (recomp, Just iface)
828 @recompileRequired@ is called from the HscMain. It checks whether
829 a recompilation is required. It needs access to the persistent state,
830 finder, etc, because it may have to load lots of interface files to
831 check their versions.
834 type RecompileRequired = Bool
835 upToDate = False -- Recompile not required
836 outOfDate = True -- Recompile required
838 checkVersions :: HscEnv
839 -> Bool -- True <=> source unchanged
840 -> ModIface -- Old interface
841 -> IfG RecompileRequired
842 checkVersions hsc_env source_unchanged iface
843 | not source_unchanged
846 = do { traceHiDiffs (text "Considering whether compilation is required for" <+>
847 ppr (mi_module iface) <> colon)
849 -- Source code unchanged and no errors yet... carry on
851 -- First put the dependent-module info, read from the old interface, into the envt,
852 -- so that when we look for interfaces we look for the right one (.hi or .hi-boot)
854 -- It's just temporary because either the usage check will succeed
855 -- (in which case we are done with this module) or it'll fail (in which
856 -- case we'll compile the module from scratch anyhow).
858 -- We do this regardless of compilation mode
859 ; updateEps_ $ \eps -> eps { eps_is_boot = mod_deps }
861 ; let this_pkg = thisPackage (hsc_dflags hsc_env)
862 ; checkList [checkModUsage this_pkg u | u <- mi_usages iface]
865 -- This is a bit of a hack really
866 mod_deps :: ModuleNameEnv (ModuleName, IsBootInterface)
867 mod_deps = mkModDeps (dep_mods (mi_deps iface))
869 checkModUsage :: PackageId ->Usage -> IfG RecompileRequired
870 -- Given the usage information extracted from the old
871 -- M.hi file for the module being compiled, figure out
872 -- whether M needs to be recompiled.
874 checkModUsage this_pkg (Usage { usg_name = mod_name, usg_mod = old_mod_vers,
875 usg_rules = old_rule_vers,
876 usg_exports = maybe_old_export_vers,
877 usg_entities = old_decl_vers })
878 = -- Load the imported interface is possible
880 doc_str = sep [ptext SLIT("need version info for"), ppr mod_name]
882 traceHiDiffs (text "Checking usages for module" <+> ppr mod_name) `thenM_`
885 mod = mkModule this_pkg mod_name
887 loadInterface doc_str mod ImportBySystem `thenM` \ mb_iface ->
888 -- Load the interface, but don't complain on failure;
889 -- Instead, get an Either back which we can test
892 Failed exn -> (out_of_date (sep [ptext SLIT("Can't find version number for module"),
894 -- Couldn't find or parse a module mentioned in the
895 -- old interface file. Don't complain -- it might just be that
896 -- the current module doesn't need that import and it's been deleted
900 new_mod_vers = mi_mod_vers iface
901 new_decl_vers = mi_ver_fn iface
902 new_export_vers = mi_exp_vers iface
903 new_rule_vers = mi_rule_vers iface
906 checkModuleVersion old_mod_vers new_mod_vers `thenM` \ recompile ->
907 if not recompile then
912 if checkExportList maybe_old_export_vers new_export_vers then
913 out_of_date_vers (ptext SLIT(" Export list changed"))
914 (expectJust "checkModUsage" maybe_old_export_vers)
919 if old_rule_vers /= new_rule_vers then
920 out_of_date_vers (ptext SLIT(" Rules changed"))
921 old_rule_vers new_rule_vers
924 -- CHECK ITEMS ONE BY ONE
925 checkList [checkEntityUsage new_decl_vers u | u <- old_decl_vers] `thenM` \ recompile ->
927 returnM outOfDate -- This one failed, so just bail out now
929 up_to_date (ptext SLIT(" Great! The bits I use are up to date"))
932 ------------------------
933 checkModuleVersion old_mod_vers new_mod_vers
934 | new_mod_vers == old_mod_vers
935 = up_to_date (ptext SLIT("Module version unchanged"))
938 = out_of_date_vers (ptext SLIT(" Module version has changed"))
939 old_mod_vers new_mod_vers
941 ------------------------
942 checkExportList Nothing new_vers = upToDate
943 checkExportList (Just v) new_vers = v /= new_vers
945 ------------------------
946 checkEntityUsage new_vers (name,old_vers)
947 = case new_vers name of
949 Nothing -> -- We used it before, but it ain't there now
950 out_of_date (sep [ptext SLIT("No longer exported:"), ppr name])
952 Just new_vers -- It's there, but is it up to date?
953 | new_vers == old_vers -> traceHiDiffs (text " Up to date" <+> ppr name <+> parens (ppr new_vers)) `thenM_`
955 | otherwise -> out_of_date_vers (ptext SLIT(" Out of date:") <+> ppr name)
958 up_to_date msg = traceHiDiffs msg `thenM_` returnM upToDate
959 out_of_date msg = traceHiDiffs msg `thenM_` returnM outOfDate
960 out_of_date_vers msg old_vers new_vers
961 = out_of_date (hsep [msg, ppr old_vers, ptext SLIT("->"), ppr new_vers])
963 ----------------------
964 checkList :: [IfG RecompileRequired] -> IfG RecompileRequired
965 -- This helper is used in two places
966 checkList [] = returnM upToDate
967 checkList (check:checks) = check `thenM` \ recompile ->
974 %************************************************************************
976 Converting things to their Iface equivalents
978 %************************************************************************
981 tyThingToIfaceDecl :: (Name -> IfaceExtName) -> TyThing -> IfaceDecl
982 -- Assumption: the thing is already tidied, so that locally-bound names
983 -- (lambdas, for-alls) already have non-clashing OccNames
984 -- Reason: Iface stuff uses OccNames, and the conversion here does
985 -- not do tidying on the way
986 tyThingToIfaceDecl ext (AnId id)
987 = IfaceId { ifName = getOccName id,
988 ifType = toIfaceType ext (idType id),
991 info = case toIfaceIdInfo ext (idInfo id) of
993 items -> HasInfo items
995 tyThingToIfaceDecl ext (AClass clas)
996 = IfaceClass { ifCtxt = toIfaceContext ext sc_theta,
997 ifName = getOccName clas,
998 ifTyVars = toIfaceTvBndrs clas_tyvars,
999 ifFDs = map toIfaceFD clas_fds,
1000 ifSigs = map toIfaceClassOp op_stuff,
1001 ifRec = boolToRecFlag (isRecursiveTyCon tycon) }
1003 (clas_tyvars, clas_fds, sc_theta, _, op_stuff) = classExtraBigSig clas
1004 tycon = classTyCon clas
1006 toIfaceClassOp (sel_id, def_meth)
1007 = ASSERT(sel_tyvars == clas_tyvars)
1008 IfaceClassOp (getOccName sel_id) def_meth (toIfaceType ext op_ty)
1010 -- Be careful when splitting the type, because of things
1011 -- like class Foo a where
1012 -- op :: (?x :: String) => a -> a
1013 -- and class Baz a where
1014 -- op :: (Ord a) => a -> a
1015 (sel_tyvars, rho_ty) = splitForAllTys (idType sel_id)
1016 op_ty = funResultTy rho_ty
1018 toIfaceFD (tvs1, tvs2) = (map (occNameFS.getOccName) tvs1, map (occNameFS.getOccName) tvs2)
1020 tyThingToIfaceDecl ext (ATyCon tycon)
1022 = IfaceSyn { ifName = getOccName tycon,
1023 ifTyVars = toIfaceTvBndrs tyvars,
1024 ifOpenSyn = syn_isOpen,
1025 ifSynRhs = toIfaceType ext syn_tyki }
1028 = IfaceData { ifName = getOccName tycon,
1029 ifTyVars = toIfaceTvBndrs tyvars,
1030 ifCtxt = toIfaceContext ext (tyConStupidTheta tycon),
1031 ifCons = ifaceConDecls (algTyConRhs tycon),
1032 ifRec = boolToRecFlag (isRecursiveTyCon tycon),
1033 ifGadtSyntax = isGadtSyntaxTyCon tycon,
1034 ifGeneric = tyConHasGenerics tycon }
1036 | isForeignTyCon tycon
1037 = IfaceForeign { ifName = getOccName tycon,
1038 ifExtName = tyConExtName tycon }
1040 | isPrimTyCon tycon || isFunTyCon tycon
1041 -- Needed in GHCi for ':info Int#', for example
1042 = IfaceData { ifName = getOccName tycon,
1043 ifTyVars = toIfaceTvBndrs (take (tyConArity tycon) alphaTyVars),
1045 ifCons = IfAbstractTyCon,
1046 ifGadtSyntax = False,
1048 ifRec = NonRecursive}
1050 | otherwise = pprPanic "toIfaceDecl" (ppr tycon)
1052 tyvars = tyConTyVars tycon
1053 (syn_isOpen, syn_tyki) = case synTyConRhs tycon of
1054 OpenSynTyCon ki -> (True , ki)
1055 SynonymTyCon ty -> (False, ty)
1057 ifaceConDecls (NewTyCon { data_con = con }) =
1058 IfNewTyCon (ifaceConDecl con)
1059 ifaceConDecls (DataTyCon { data_cons = cons }) =
1060 IfDataTyCon (map ifaceConDecl cons)
1061 ifaceConDecls OpenDataTyCon = IfOpenDataTyCon
1062 ifaceConDecls OpenNewTyCon = IfOpenNewTyCon
1063 ifaceConDecls AbstractTyCon = IfAbstractTyCon
1064 -- The last case happens when a TyCon has been trimmed during tidying
1065 -- Furthermore, tyThingToIfaceDecl is also used
1066 -- in TcRnDriver for GHCi, when browsing a module, in which case the
1067 -- AbstractTyCon case is perfectly sensible.
1069 ifaceConDecl data_con
1070 = IfCon { ifConOcc = getOccName (dataConName data_con),
1071 ifConInfix = dataConIsInfix data_con,
1072 ifConUnivTvs = toIfaceTvBndrs (dataConUnivTyVars data_con),
1073 ifConExTvs = toIfaceTvBndrs (dataConExTyVars data_con),
1074 ifConEqSpec = to_eq_spec (dataConEqSpec data_con),
1075 ifConCtxt = toIfaceContext ext (dataConTheta data_con),
1076 ifConArgTys = map (toIfaceType ext) (dataConOrigArgTys data_con),
1077 ifConFields = map getOccName (dataConFieldLabels data_con),
1078 ifConStricts = dataConStrictMarks data_con }
1080 to_eq_spec spec = [(getOccName tv, toIfaceType ext ty) | (tv,ty) <- spec]
1082 tyThingToIfaceDecl ext (ADataCon dc)
1083 = pprPanic "toIfaceDecl" (ppr dc) -- Should be trimmed out earlier
1086 --------------------------
1087 instanceToIfaceInst :: (Name -> IfaceExtName) -> Instance -> IfaceInst
1088 instanceToIfaceInst ext_lhs ispec@(Instance { is_dfun = dfun_id, is_flag = oflag,
1089 is_cls = cls, is_tcs = mb_tcs,
1091 = IfaceInst { ifDFun = getOccName dfun_id,
1093 ifInstCls = ext_lhs cls,
1094 ifInstTys = map do_rough mb_tcs,
1097 do_rough Nothing = Nothing
1098 do_rough (Just n) = Just (toIfaceTyCon_name ext_lhs n)
1100 --------------------------
1101 toIfaceIdInfo :: (Name -> IfaceExtName) -> IdInfo -> [IfaceInfoItem]
1102 toIfaceIdInfo ext id_info
1103 = catMaybes [arity_hsinfo, caf_hsinfo, strict_hsinfo,
1104 inline_hsinfo, wrkr_hsinfo, unfold_hsinfo]
1106 ------------ Arity --------------
1107 arity_info = arityInfo id_info
1108 arity_hsinfo | arity_info == 0 = Nothing
1109 | otherwise = Just (HsArity arity_info)
1111 ------------ Caf Info --------------
1112 caf_info = cafInfo id_info
1113 caf_hsinfo = case caf_info of
1114 NoCafRefs -> Just HsNoCafRefs
1117 ------------ Strictness --------------
1118 -- No point in explicitly exporting TopSig
1119 strict_hsinfo = case newStrictnessInfo id_info of
1120 Just sig | not (isTopSig sig) -> Just (HsStrictness sig)
1123 ------------ Worker --------------
1124 work_info = workerInfo id_info
1125 has_worker = case work_info of { HasWorker _ _ -> True; other -> False }
1126 wrkr_hsinfo = case work_info of
1127 HasWorker work_id wrap_arity ->
1128 Just (HsWorker (ext (idName work_id)) wrap_arity)
1131 ------------ Unfolding --------------
1132 -- The unfolding is redundant if there is a worker
1133 unfold_info = unfoldingInfo id_info
1134 rhs = unfoldingTemplate unfold_info
1135 no_unfolding = neverUnfold unfold_info
1136 -- The CoreTidy phase retains unfolding info iff
1137 -- we want to expose the unfolding, taking into account
1138 -- unconditional NOINLINE, etc. See TidyPgm.addExternal
1139 unfold_hsinfo | no_unfolding = Nothing
1140 | has_worker = Nothing -- Unfolding is implicit
1141 | otherwise = Just (HsUnfold (toIfaceExpr ext rhs))
1143 ------------ Inline prag --------------
1144 inline_prag = inlinePragInfo id_info
1145 inline_hsinfo | isAlwaysActive inline_prag = Nothing
1146 | no_unfolding && not has_worker = Nothing
1147 -- If the iface file give no unfolding info, we
1148 -- don't need to say when inlining is OK!
1149 | otherwise = Just (HsInline inline_prag)
1151 --------------------------
1152 coreRuleToIfaceRule :: (Name -> IfaceExtName) -- For the LHS names
1153 -> (Name -> IfaceExtName) -- For the RHS names
1154 -> CoreRule -> IfaceRule
1155 coreRuleToIfaceRule ext_lhs ext_rhs (BuiltinRule { ru_fn = fn})
1156 = pprTrace "toHsRule: builtin" (ppr fn) $
1157 bogusIfaceRule (mkIfaceExtName fn)
1159 coreRuleToIfaceRule ext_lhs ext_rhs
1160 (Rule { ru_name = name, ru_fn = fn, ru_act = act, ru_bndrs = bndrs,
1161 ru_args = args, ru_rhs = rhs, ru_orph = orph })
1162 = IfaceRule { ifRuleName = name, ifActivation = act,
1163 ifRuleBndrs = map (toIfaceBndr ext_lhs) bndrs,
1164 ifRuleHead = ext_lhs fn,
1165 ifRuleArgs = map do_arg args,
1166 ifRuleRhs = toIfaceExpr ext_rhs rhs,
1169 -- For type args we must remove synonyms from the outermost
1170 -- level. Reason: so that when we read it back in we'll
1171 -- construct the same ru_rough field as we have right now;
1173 do_arg (Type ty) = IfaceType (toIfaceType ext_lhs (deNoteType ty))
1174 do_arg arg = toIfaceExpr ext_lhs arg
1176 bogusIfaceRule :: IfaceExtName -> IfaceRule
1177 bogusIfaceRule id_name
1178 = IfaceRule { ifRuleName = FSLIT("bogus"), ifActivation = NeverActive,
1179 ifRuleBndrs = [], ifRuleHead = id_name, ifRuleArgs = [],
1180 ifRuleRhs = IfaceExt id_name, ifRuleOrph = Nothing }
1182 ---------------------
1183 toIfaceExpr :: (Name -> IfaceExtName) -> CoreExpr -> IfaceExpr
1184 toIfaceExpr ext (Var v) = toIfaceVar ext v
1185 toIfaceExpr ext (Lit l) = IfaceLit l
1186 toIfaceExpr ext (Type ty) = IfaceType (toIfaceType ext ty)
1187 toIfaceExpr ext (Lam x b) = IfaceLam (toIfaceBndr ext x) (toIfaceExpr ext b)
1188 toIfaceExpr ext (App f a) = toIfaceApp ext f [a]
1189 toIfaceExpr ext (Case s x ty as) = IfaceCase (toIfaceExpr ext s) (occNameFS (getOccName x)) (toIfaceType ext ty) (map (toIfaceAlt ext) as)
1190 toIfaceExpr ext (Let b e) = IfaceLet (toIfaceBind ext b) (toIfaceExpr ext e)
1191 toIfaceExpr ext (Cast e co) = IfaceCast (toIfaceExpr ext e) (toIfaceType ext co)
1192 toIfaceExpr ext (Note n e) = IfaceNote (toIfaceNote ext n) (toIfaceExpr ext e)
1194 ---------------------
1195 toIfaceNote ext (SCC cc) = IfaceSCC cc
1196 toIfaceNote ext InlineMe = IfaceInlineMe
1197 toIfaceNote ext (CoreNote s) = IfaceCoreNote s
1199 ---------------------
1200 toIfaceBind ext (NonRec b r) = IfaceNonRec (toIfaceIdBndr ext b) (toIfaceExpr ext r)
1201 toIfaceBind ext (Rec prs) = IfaceRec [(toIfaceIdBndr ext b, toIfaceExpr ext r) | (b,r) <- prs]
1203 ---------------------
1204 toIfaceAlt ext (c,bs,r) = (toIfaceCon c, map (occNameFS.getOccName) bs, toIfaceExpr ext r)
1206 ---------------------
1207 toIfaceCon (DataAlt dc) | isTupleTyCon tc = IfaceTupleAlt (tupleTyConBoxity tc)
1208 | otherwise = IfaceDataAlt (getOccName dc)
1210 tc = dataConTyCon dc
1212 toIfaceCon (LitAlt l) = IfaceLitAlt l
1213 toIfaceCon DEFAULT = IfaceDefault
1215 ---------------------
1216 toIfaceApp ext (App f a) as = toIfaceApp ext f (a:as)
1217 toIfaceApp ext (Var v) as
1218 = case isDataConWorkId_maybe v of
1219 -- We convert the *worker* for tuples into IfaceTuples
1220 Just dc | isTupleTyCon tc && saturated
1221 -> IfaceTuple (tupleTyConBoxity tc) tup_args
1223 val_args = dropWhile isTypeArg as
1224 saturated = val_args `lengthIs` idArity v
1225 tup_args = map (toIfaceExpr ext) val_args
1226 tc = dataConTyCon dc
1228 other -> mkIfaceApps ext (toIfaceVar ext v) as
1230 toIfaceApp ext e as = mkIfaceApps ext (toIfaceExpr ext e) as
1232 mkIfaceApps ext f as = foldl (\f a -> IfaceApp f (toIfaceExpr ext a)) f as
1234 ---------------------
1235 toIfaceVar :: (Name -> IfaceExtName) -> Id -> IfaceExpr
1237 | Just fcall <- isFCallId_maybe v = IfaceFCall fcall (toIfaceType ext (idType v))
1238 -- Foreign calls have special syntax
1239 | isExternalName name = IfaceExt (ext name)
1240 | otherwise = IfaceLcl (occNameFS (nameOccName name))