2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
5 \section[MkIface]{Print an interface for a module}
9 showIface, mkIface, mkUsageInfo,
14 #include "HsVersions.h"
17 import HsCore ( HsIdInfo(..), UfExpr(..), toUfExpr, toUfBndr )
18 import HsTypes ( toHsTyVars )
19 import TysPrim ( alphaTyVars )
20 import BasicTypes ( NewOrData(..), Activation(..), FixitySig(..),
21 Version, initialVersion, bumpVersion
23 import NewDemand ( isTopSig )
25 import TcRnTypes ( ImportAvails(..) )
26 import RnHsSyn ( RenamedInstDecl, RenamedTyClDecl )
27 import HscTypes ( VersionInfo(..), ModIface(..),
29 GhciMode(..), HscEnv(..), Dependencies(..),
30 FixityEnv, lookupFixity, collectFixities,
31 IfaceDecls, mkIfaceDecls, dcl_tycl, dcl_rules, dcl_insts,
33 Avails, AvailInfo, GenAvailInfo(..), availName,
34 ExternalPackageState(..),
35 ParsedIface(..), Usage(..),
36 Deprecations(..), initialVersionInfo,
37 lookupVersion, lookupIfaceByModName
41 import Id ( idType, idInfo, isImplicitId, idCgInfo )
42 import DataCon ( dataConName, dataConSig, dataConFieldLabels, dataConStrictMarks )
44 import CoreSyn ( CoreRule(..), IdCoreRule )
45 import CoreFVs ( ruleLhsFreeNames )
46 import CoreUnfold ( neverUnfold, unfoldingTemplate )
47 import Name ( getName, nameModule, nameModule_maybe, nameOccName,
48 nameIsLocalOrFrom, Name, NamedThing(..) )
51 import OccName ( OccName, pprOccName )
52 import TyCon ( DataConDetails(..), tyConTyVars, tyConDataCons, tyConTheta,
53 isFunTyCon, isPrimTyCon, isNewTyCon, isClassTyCon,
54 isSynTyCon, isAlgTyCon, isForeignTyCon,
55 getSynTyConDefn, tyConGenInfo, tyConDataConDetails, tyConArity )
56 import Class ( classExtraBigSig, classTyCon, DefMeth(..) )
57 import FieldLabel ( fieldLabelType )
58 import TcType ( tcSplitForAllTys, tcFunResultTy, tidyTopType, deNoteType, tyClsNamesOfDFunHead )
59 import SrcLoc ( noSrcLoc )
60 import Module ( Module, ModuleName, moduleNameFS, moduleName, isHomeModule,
61 ModLocation(..), mkSysModuleNameFS,
62 ModuleEnv, emptyModuleEnv, lookupModuleEnv,
63 extendModuleEnv_C, moduleEnvElts
66 import Util ( sortLt, dropList, seqList )
67 import Binary ( getBinFileWithDict )
68 import BinIface ( writeBinIface, v_IgnoreHiVersion )
69 import ErrUtils ( dumpIfSet_dyn )
73 import DATA_IOREF ( writeIORef )
75 import Maybe ( catMaybes, isJust, isNothing )
76 import Maybes ( orElse )
77 import IO ( putStrLn )
81 %************************************************************************
83 \subsection{Print out the contents of a binary interface}
85 %************************************************************************
88 showIface :: FilePath -> IO ()
89 showIface filename = do
90 -- skip the version check; we don't want to worry about profiled vs.
91 -- non-profiled interfaces, for example.
92 writeIORef v_IgnoreHiVersion True
93 parsed_iface <- Binary.getBinFileWithDict filename
95 pi_mod=pi_mod, pi_pkg=pi_pkg, pi_vers=pi_vers,
97 pi_orphan=pi_orphan, pi_usages=pi_usages,
98 pi_exports=pi_exports, pi_decls=pi_decls,
99 pi_fixity=pi_fixity, pi_insts=pi_insts,
100 pi_rules=pi_rules, pi_deprecs=pi_deprecs } = parsed_iface
101 putStrLn (showSDoc (vcat [
102 text "__interface" <+> doubleQuotes (ppr pi_pkg)
103 <+> ppr pi_mod <+> ppr pi_vers
104 <+> (if pi_orphan then char '!' else empty)
105 <+> ptext SLIT("where"),
106 -- no instance Outputable (WhatsImported):
107 pprExports id (snd pi_exports),
109 pprUsages id pi_usages,
110 hsep (map ppr_fix pi_fixity) <> semi,
111 vcat (map ppr_inst pi_insts),
112 vcat (map ppr_decl pi_decls),
114 -- no instance Outputable (Either):
118 ppr_fix (FixitySig n f _) = ppr f <+> ppr n
119 ppr_inst i = ppr i <+> semi
120 ppr_decl (v,d) = int v <+> ppr d <> semi
123 %************************************************************************
125 \subsection{Completing an interface}
127 %************************************************************************
132 -> Maybe ModIface -- The old interface, if we have it
133 -> ModGuts -- The compiled, tidied module
134 -> IO ModIface -- The new one, complete with decls and versions
136 -- a) completes the interface
137 -- b) writes it out to a file if necessary
139 mkIface hsc_env location maybe_old_iface
140 impl@ModGuts{ mg_module = this_mod,
143 mg_exports = exports,
144 mg_rdr_env = rdr_env,
145 mg_fix_env = fix_env,
146 mg_deprecs = deprecs,
150 = do { -- Sort the exports to make them easier to compare for versions
151 let { my_exports = groupAvails this_mod exports ;
153 iface_w_decls = ModIface { mi_module = this_mod,
154 mi_package = opt_InPackage,
155 mi_version = initialVersionInfo,
158 mi_exports = my_exports,
159 mi_decls = new_decls,
160 mi_orphan = orphan_mod,
162 mi_fixities = fix_env,
163 mi_globals = Just rdr_env,
164 mi_deprecs = deprecs } }
166 -- Add version information
167 ; let (final_iface, maybe_diffs) = _scc_ "versioninfo" addVersionInfo maybe_old_iface iface_w_decls
169 -- Write the interface file, if necessary
170 ; when (must_write_hi_file maybe_diffs)
171 (writeBinIface hi_file_path final_iface)
172 -- (writeIface hi_file_path final_iface)
175 ; write_diffs dflags final_iface maybe_diffs
181 dflags = hsc_dflags hsc_env
182 ghci_mode = hsc_mode hsc_env
184 must_write_hi_file Nothing = False
185 must_write_hi_file (Just _diffs) = ghci_mode /= Interactive
186 -- We must write a new .hi file if there are some changes
187 -- and we're not in interactive mode
188 -- maybe_diffs = 'Nothing' means that even the usages havn't changed,
189 -- so there's no need to write a new interface file. But even if
190 -- the usages have changed, the module version may not have.
192 hi_file_path = ml_hi_file location
193 new_decls = mkIfaceDecls ty_cls_dcls rule_dcls inst_dcls
194 inst_dcls = map ifaceInstance insts
195 ty_cls_dcls = foldNameEnv ifaceTyThing_acc [] types
196 rule_dcls = map ifaceRule rules
197 orphan_mod = isOrphanModule impl
199 write_diffs :: DynFlags -> ModIface -> Maybe SDoc -> IO ()
200 write_diffs dflags new_iface Nothing
201 = do when (dopt Opt_D_dump_hi_diffs dflags) (printDump (text "INTERFACE UNCHANGED"))
202 dumpIfSet_dyn dflags Opt_D_dump_hi "UNCHANGED FINAL INTERFACE" (pprIface new_iface)
204 write_diffs dflags new_iface (Just sdoc_diffs)
205 = do dumpIfSet_dyn dflags Opt_D_dump_hi_diffs "INTERFACE HAS CHANGED" sdoc_diffs
206 dumpIfSet_dyn dflags Opt_D_dump_hi "NEW FINAL INTERFACE" (pprIface new_iface)
210 isOrphanModule :: ModGuts -> Bool
211 isOrphanModule (ModGuts {mg_module = this_mod, mg_insts = insts, mg_rules = rules})
212 = any orphan_inst insts || any orphan_rule rules
214 -- A rule is an orphan if the LHS mentions nothing defined locally
215 orphan_inst dfun_id = no_locals (tyClsNamesOfDFunHead (idType dfun_id))
216 -- A instance is an orphan if its head mentions nothing defined locally
217 orphan_rule rule = no_locals (ruleLhsFreeNames rule)
219 no_locals names = isEmptyNameSet (filterNameSet (nameIsLocalOrFrom this_mod) names)
222 Implicit Ids and class tycons aren't included in interface files, so
223 we miss them out of the accumulating parameter here.
226 ifaceTyThing_acc :: TyThing -> [RenamedTyClDecl] -> [RenamedTyClDecl]
227 ifaceTyThing_acc (ADataCon dc) so_far = so_far
228 ifaceTyThing_acc (AnId id) so_far | isImplicitId id = so_far
229 ifaceTyThing_acc (ATyCon id) so_far | isClassTyCon id = so_far
230 ifaceTyThing_acc other so_far = ifaceTyThing other : so_far
233 Convert *any* TyThing into a RenamedTyClDecl. Used both for
234 generating interface files and for the ':info' command in GHCi.
237 ifaceTyThing :: TyThing -> RenamedTyClDecl
238 ifaceTyThing (AClass clas) = cls_decl
240 cls_decl = ClassDecl { tcdCtxt = toHsContext sc_theta,
241 tcdName = getName clas,
242 tcdTyVars = toHsTyVars clas_tyvars,
243 tcdFDs = toHsFDs clas_fds,
244 tcdSigs = map toClassOpSig op_stuff,
248 (clas_tyvars, clas_fds, sc_theta, sc_sels, op_stuff) = classExtraBigSig clas
249 tycon = classTyCon clas
250 data_con = head (tyConDataCons tycon)
252 toClassOpSig (sel_id, def_meth)
253 = ASSERT(sel_tyvars == clas_tyvars)
254 ClassOpSig (getName sel_id) def_meth (toHsType op_ty) noSrcLoc
256 -- Be careful when splitting the type, because of things
257 -- like class Foo a where
258 -- op :: (?x :: String) => a -> a
259 -- and class Baz a where
260 -- op :: (Ord a) => a -> a
261 (sel_tyvars, rho_ty) = tcSplitForAllTys (idType sel_id)
262 op_ty = tcFunResultTy rho_ty
264 ifaceTyThing (ATyCon tycon) = ty_decl
266 ty_decl | isSynTyCon tycon
267 = TySynonym { tcdName = getName tycon,
268 tcdTyVars = toHsTyVars tyvars,
269 tcdSynRhs = toHsType syn_ty,
273 = TyData { tcdND = new_or_data,
274 tcdCtxt = toHsContext (tyConTheta tycon),
275 tcdName = getName tycon,
276 tcdTyVars = toHsTyVars tyvars,
277 tcdCons = ifaceConDecls (tyConDataConDetails tycon),
279 tcdGeneric = Just (isJust (tyConGenInfo tycon)),
280 -- Just True <=> has generic stuff
283 | isForeignTyCon tycon
284 = ForeignType { tcdName = getName tycon,
285 tcdExtName = Nothing,
286 tcdFoType = DNType, -- The only case at present
289 | isPrimTyCon tycon || isFunTyCon tycon
290 -- needed in GHCi for ':info Int#', for example
291 = TyData { tcdND = DataType,
293 tcdName = getName tycon,
294 tcdTyVars = toHsTyVars (take (tyConArity tycon) alphaTyVars),
297 tcdGeneric = Just False,
300 | otherwise = pprPanic "ifaceTyThing" (ppr tycon)
302 tyvars = tyConTyVars tycon
303 (_, syn_ty) = getSynTyConDefn tycon
304 new_or_data | isNewTyCon tycon = NewType
305 | otherwise = DataType
307 ifaceConDecls Unknown = Unknown
308 ifaceConDecls (HasCons n) = HasCons n
309 ifaceConDecls (DataCons cs) = DataCons (map ifaceConDecl cs)
311 ifaceConDecl data_con
312 = ConDecl (dataConName data_con)
313 (toHsTyVars ex_tyvars)
314 (toHsContext ex_theta)
317 (tyvars1, _, ex_tyvars, ex_theta, arg_tys, tycon1) = dataConSig data_con
318 field_labels = dataConFieldLabels data_con
319 strict_marks = dropList ex_theta (dataConStrictMarks data_con)
320 -- The 'drop' is because dataConStrictMarks
321 -- includes the existential dictionaries
322 details | null field_labels
323 = ASSERT( tycon == tycon1 && tyvars == tyvars1 )
324 PrefixCon (zipWith BangType strict_marks (map toHsType arg_tys))
327 = RecCon (zipWith mk_field strict_marks field_labels)
329 mk_field strict_mark field_label
330 = (getName field_label, BangType strict_mark (toHsType (fieldLabelType field_label)))
332 ifaceTyThing (AnId id) = iface_sig
334 iface_sig = IfaceSig { tcdName = getName id,
335 tcdType = toHsType id_type,
336 tcdIdInfo = hs_idinfo,
341 cg_info = idCgInfo id
342 arity_info = arityInfo id_info
343 caf_info = cgCafInfo cg_info
345 hs_idinfo | opt_OmitInterfacePragmas
348 = catMaybes [arity_hsinfo, caf_hsinfo,
349 strict_hsinfo, wrkr_hsinfo,
352 ------------ Arity --------------
353 arity_hsinfo | arity_info == 0 = Nothing
354 | otherwise = Just (HsArity arity_info)
356 ------------ Caf Info --------------
357 caf_hsinfo = case caf_info of
358 NoCafRefs -> Just HsNoCafRefs
361 ------------ Strictness --------------
362 -- No point in explicitly exporting TopSig
363 strict_hsinfo = case newStrictnessInfo id_info of
364 Just sig | not (isTopSig sig) -> Just (HsStrictness sig)
367 ------------ Worker --------------
368 work_info = workerInfo id_info
369 has_worker = case work_info of { HasWorker _ _ -> True; other -> False }
370 wrkr_hsinfo = case work_info of
371 HasWorker work_id wrap_arity ->
372 Just (HsWorker (getName work_id) wrap_arity)
375 ------------ Unfolding --------------
376 -- The unfolding is redundant if there is a worker
377 unfold_info = unfoldingInfo id_info
378 inline_prag = inlinePragInfo id_info
379 rhs = unfoldingTemplate unfold_info
380 unfold_hsinfo | neverUnfold unfold_info
381 || has_worker = Nothing
382 | otherwise = Just (HsUnfold inline_prag (toUfExpr rhs))
386 ifaceInstance :: DFunId -> RenamedInstDecl
387 ifaceInstance dfun_id
388 = InstDecl (toHsType tidy_ty) EmptyMonoBinds [] (Just (getName dfun_id)) noSrcLoc
390 tidy_ty = tidyTopType (deNoteType (idType dfun_id))
391 -- The deNoteType is very important. It removes all type
392 -- synonyms from the instance type in interface files.
393 -- That in turn makes sure that when reading in instance decls
394 -- from interface files that the 'gating' mechanism works properly.
395 -- Otherwise you could have
396 -- type Tibble = T Int
397 -- instance Foo Tibble where ...
398 -- and this instance decl wouldn't get imported into a module
399 -- that mentioned T but not Tibble.
401 ifaceRule :: IdCoreRule -> RuleDecl Name
402 ifaceRule (id, BuiltinRule _ _)
403 = pprTrace "toHsRule: builtin" (ppr id) (bogusIfaceRule id)
405 ifaceRule (id, Rule name act bndrs args rhs)
406 = IfaceRule name act (map toUfBndr bndrs) (getName id)
407 (map toUfExpr args) (toUfExpr rhs) noSrcLoc
409 bogusIfaceRule :: (NamedThing a) => a -> RuleDecl Name
411 = IfaceRule FSLIT("bogus") NeverActive [] (getName id) [] (UfVar (getName id)) noSrcLoc
415 %*********************************************************
417 \subsection{Keeping track of what we've slurped, and version numbers}
419 %*********************************************************
421 mkUsageInfo figures out what the ``usage information'' for this
422 moudule is; that is, what it must record in its interface file as the
425 We produce a line for every module B below the module, A, currently being
428 to record the fact that A does import B indirectly. This is used to decide
429 to look to look for B.hi rather than B.hi-boot when compiling a module that
430 imports A. This line says that A imports B, but uses nothing in it.
431 So we'll get an early bale-out when compiling A if B's version changes.
433 The usage information records:
436 \item (a) anything reachable from its body code
437 \item (b) any module exported with a @module Foo@
438 \item (c) anything reachable from an exported item
441 Why (b)? Because if @Foo@ changes then this module's export list
442 will change, so we must recompile this module at least as far as
443 making a new interface file --- but in practice that means complete
446 Why (c)? Consider this:
448 module A( f, g ) where | module B( f ) where
449 import B( f ) | f = h 3
453 Here, @B.f@ isn't used in A. Should we nevertheless record @B.f@ in
454 @A@'s usages? Our idea is that we aren't going to touch A.hi if it is
455 *identical* to what it was before. If anything about @B.f@ changes
456 than anyone who imports @A@ should be recompiled in case they use
457 @B.f@ (they'll get an early exit if they don't). So, if anything
458 about @B.f@ changes we'd better make sure that something in A.hi
459 changes, and the convenient way to do that is to record the version
460 number @B.f@ in A.hi in the usage list. If B.f changes that'll force a
461 complete recompiation of A, which is overkill but it's the only way to
462 write a new, slightly different, A.hi.
464 But the example is tricker. Even if @B.f@ doesn't change at all,
465 @B.h@ may do so, and this change may not be reflected in @f@'s version
466 number. But with -O, a module that imports A must be recompiled if
467 @B.h@ changes! So A must record a dependency on @B.h@. So we treat
468 the occurrence of @B.f@ in the export list *just as if* it were in the
469 code of A, and thereby haul in all the stuff reachable from it.
471 *** Conclusion: if A mentions B.f in its export list,
472 behave just as if A mentioned B.f in its source code,
473 and slurp in B.f and all its transitive closure ***
475 [NB: If B was compiled with -O, but A isn't, we should really *still*
476 haul in all the unfoldings for B, in case the module that imports A *is*
477 compiled with -O. I think this is the case.]
480 mkUsageInfo :: HscEnv -> ExternalPackageState
481 -> ImportAvails -> EntityUsage
484 mkUsageInfo hsc_env eps
485 (ImportAvails { imp_mods = dir_imp_mods,
486 imp_dep_mods = dep_mods })
488 = -- seq the list of Usages returned: occasionally these
489 -- don't get evaluated for a while and we can end up hanging on to
490 -- the entire collection of Ifaces.
491 usages `seqList` usages
493 usages = catMaybes [ mkUsage mod_name
494 | (mod_name,_) <- moduleEnvElts dep_mods]
496 hpt = hsc_HPT hsc_env
499 import_all mod = case lookupModuleEnv dir_imp_mods mod of
500 Just (_,imp_all) -> imp_all
503 -- ent_map groups together all the things imported and used
504 -- from a particular module in this package
505 ent_map :: ModuleEnv [Name]
506 ent_map = foldNameSet add_mv emptyModuleEnv used_names
507 add_mv name mv_map = extendModuleEnv_C add_item mv_map mod [name]
509 mod = nameModule name
510 add_item names _ = name:names
512 -- We want to create a Usage for a home module if
513 -- a) we used something from; has something in used_names
514 -- b) we imported all of it, even if we used nothing from it
515 -- (need to recompile if its export list changes: export_vers)
516 -- c) is a home-package orphan module (need to recompile if its
517 -- instance decls change: rules_vers)
518 mkUsage :: ModuleName -> Maybe (Usage Name)
520 | isNothing maybe_iface -- We can't depend on it if we didn't
521 || not (isHomeModule mod) -- even open the interface!
525 = Nothing -- Record no usage info
528 = Just (Usage { usg_name = moduleName mod,
530 usg_exports = export_vers,
531 usg_entities = ent_vers,
532 usg_rules = rules_vers })
534 maybe_iface = lookupIfaceByModName hpt pit mod_name
535 -- In one-shot mode, the interfaces for home-package
536 -- modules accumulate in the PIT not HPT. Sigh.
538 Just iface = maybe_iface
539 mod = mi_module iface
540 version_info = mi_version iface
541 orphan_mod = mi_orphan iface
542 version_env = vers_decls version_info
543 mod_vers = vers_module version_info
544 rules_vers = vers_rules version_info
545 all_imported = import_all mod
546 export_vers | all_imported = Just (vers_exports version_info)
547 | otherwise = Nothing
549 -- The sort is to put them into canonical order
550 used_names = lookupModuleEnv ent_map mod `orElse` []
551 ent_vers = [(n, lookupVersion version_env n)
552 | n <- sortLt lt_occ used_names ]
553 lt_occ n1 n2 = nameOccName n1 < nameOccName n2
557 groupAvails :: Module -> Avails -> [(ModuleName, Avails)]
558 -- Group by module and sort by occurrence
559 -- This keeps the list in canonical order
560 groupAvails this_mod avails
561 = [ (mkSysModuleNameFS fs, sortLt lt avails)
562 | (fs,avails) <- fmToList groupFM
565 groupFM :: FiniteMap FastString Avails
566 -- Deliberately use the FastString so we
567 -- get a canonical ordering
568 groupFM = foldl add emptyFM avails
570 add env avail = addToFM_C combine env mod_fs [avail']
572 mod_fs = moduleNameFS (moduleName avail_mod)
573 avail_mod = case nameModule_maybe (availName avail) of
576 combine old _ = avail':old
577 avail' = sortAvail avail
579 a1 `lt` a2 = occ1 < occ2
581 occ1 = nameOccName (availName a1)
582 occ2 = nameOccName (availName a2)
584 sortAvail :: AvailInfo -> AvailInfo
585 -- Sort the sub-names into canonical order.
586 -- The canonical order has the "main name" at the beginning
587 -- (if it's there at all)
588 sortAvail (Avail n) = Avail n
589 sortAvail (AvailTC n ns) | n `elem` ns = AvailTC n (n : sortLt lt (filter (/= n) ns))
590 | otherwise = AvailTC n ( sortLt lt ns)
592 n1 `lt` n2 = nameOccName n1 < nameOccName n2
595 %************************************************************************
597 \subsection{Checking if the new interface is up to date
599 %************************************************************************
602 addVersionInfo :: Maybe ModIface -- The old interface, read from M.hi
603 -> ModIface -- The new interface decls
604 -> (ModIface, Maybe SDoc) -- Nothing => no change; no need to write new Iface
605 -- Just mi => Here is the new interface to write
606 -- with correct version numbers
608 -- NB: the fixities, declarations, rules are all assumed
609 -- to be sorted by increasing order of hsDeclName, so that
610 -- we can compare for equality
612 addVersionInfo Nothing new_iface
613 -- No old interface, so definitely write a new one!
614 = (new_iface, Just (text "No old interface available"))
616 addVersionInfo (Just old_iface@(ModIface { mi_version = old_version,
617 mi_decls = old_decls,
618 mi_fixities = old_fixities,
619 mi_deprecs = old_deprecs }))
620 new_iface@(ModIface { mi_decls = new_decls,
621 mi_fixities = new_fixities,
622 mi_deprecs = new_deprecs })
624 | no_output_change && no_usage_change
625 = (new_iface, Nothing)
626 -- don't return the old iface because it may not have an
627 -- mi_globals field set to anything reasonable.
629 | otherwise -- Add updated version numbers
630 = --pprTrace "completeIface" (ppr (dcl_tycl old_decls))
631 (final_iface, Just pp_diffs)
634 final_iface = new_iface { mi_version = new_version }
635 old_mod_vers = vers_module old_version
636 new_version = VersionInfo { vers_module = bumpVersion no_output_change old_mod_vers,
637 vers_exports = bumpVersion no_export_change (vers_exports old_version),
638 vers_rules = bumpVersion no_rule_change (vers_rules old_version),
639 vers_decls = tc_vers }
641 no_output_change = no_tc_change && no_rule_change && no_export_change && no_deprec_change
642 no_usage_change = mi_usages old_iface == mi_usages new_iface
644 no_export_change = mi_exports old_iface == mi_exports new_iface -- Kept sorted
645 no_rule_change = dcl_rules old_decls == dcl_rules new_decls -- Ditto
646 && dcl_insts old_decls == dcl_insts new_decls
647 no_deprec_change = old_deprecs == new_deprecs
649 -- Fill in the version number on the new declarations by looking at the old declarations.
650 -- Set the flag if anything changes.
651 -- Assumes that the decls are sorted by hsDeclName.
652 (no_tc_change, pp_tc_diffs, tc_vers) = diffDecls old_version old_fixities new_fixities
653 (dcl_tycl old_decls) (dcl_tycl new_decls)
654 pp_diffs = vcat [pp_tc_diffs,
655 pp_change no_export_change "Export list",
656 pp_change no_rule_change "Rules",
657 pp_change no_deprec_change "Deprecations",
658 pp_change no_usage_change "Usages"]
659 pp_change True what = empty
660 pp_change False what = text what <+> ptext SLIT("changed")
662 diffDecls :: VersionInfo -- Old version
663 -> FixityEnv -> FixityEnv -- Old and new fixities
664 -> [RenamedTyClDecl] -> [RenamedTyClDecl] -- Old and new decls
665 -> (Bool, -- True <=> no change
666 SDoc, -- Record of differences
667 NameEnv Version) -- New version map
669 diffDecls (VersionInfo { vers_module = old_mod_vers, vers_decls = old_decls_vers })
670 old_fixities new_fixities old new
671 = diff True empty emptyNameEnv old new
673 -- When seeing if two decls are the same,
674 -- remember to check whether any relevant fixity has changed
675 eq_tc d1 d2 = d1 == d2 && all (same_fixity . fst) (tyClDeclNames d1)
676 same_fixity n = lookupFixity old_fixities n == lookupFixity new_fixities n
678 diff ok_so_far pp new_vers [] [] = (ok_so_far, pp, new_vers)
679 diff ok_so_far pp new_vers (od:ods) [] = diff False (pp $$ only_old od) new_vers ods []
680 diff ok_so_far pp new_vers [] (nd:nds) = diff False (pp $$ only_new nd) new_vers_with_new [] nds
682 new_vers_with_new = extendNameEnv new_vers (tyClDeclName nd) (bumpVersion False old_mod_vers)
683 -- When adding a new item, start from the old module version
684 -- This way, if you have version 4 of f, then delete f, then add f again,
685 -- you'll get version 6 of f, which will (correctly) force recompilation of
688 diff ok_so_far pp new_vers (od:ods) (nd:nds)
689 = case od_name `compare` nd_name of
690 LT -> diff False (pp $$ only_old od) new_vers ods (nd:nds)
691 GT -> diff False (pp $$ only_new nd) new_vers (od:ods) nds
692 EQ | od `eq_tc` nd -> diff ok_so_far pp new_vers ods nds
693 | otherwise -> diff False (pp $$ changed od nd) new_vers_with_diff ods nds
695 od_name = tyClDeclName od
696 nd_name = tyClDeclName nd
697 new_vers_with_diff = extendNameEnv new_vers nd_name (bumpVersion False old_version)
698 old_version = lookupVersion old_decls_vers od_name
700 only_old d = ptext SLIT("Only in old iface:") <+> ppr d
701 only_new d = ptext SLIT("Only in new iface:") <+> ppr d
702 changed od nd = ptext SLIT("Changed in iface: ") <+> ((ptext SLIT("Old:") <+> ppr od) $$
703 (ptext SLIT("New:") <+> ppr nd))
707 b%************************************************************************
709 \subsection{Writing an interface file}
711 %************************************************************************
714 pprIface :: ModIface -> SDoc
716 = vcat [ ptext SLIT("__interface")
717 <+> doubleQuotes (ftext (mi_package iface))
718 <+> ppr (mi_module iface) <+> ppr (vers_module version_info)
720 <+> (if mi_orphan iface then char '!' else empty)
721 <+> int opt_HiVersion
722 <+> ptext SLIT("where")
724 , pprExports nameOccName (mi_exports iface)
725 , pprDeps (mi_deps iface)
726 , pprUsages nameOccName (mi_usages iface)
728 , pprFixities (mi_fixities iface) (dcl_tycl decls)
729 , pprIfaceDecls (vers_decls version_info) decls
730 , pprRulesAndDeprecs (dcl_rules decls) (mi_deprecs iface)
733 version_info = mi_version iface
734 decls = mi_decls iface
735 exp_vers = vers_exports version_info
737 rule_vers = vers_rules version_info
739 pp_sub_vers | exp_vers == initialVersion && rule_vers == initialVersion = empty
740 | otherwise = brackets (ppr exp_vers <+> ppr rule_vers)
743 When printing export lists, we print like this:
745 AvailTC C [C, x, y] C(x,y)
746 AvailTC C [x, y] C!(x,y) -- Exporting x, y but not C
749 pprExports :: Eq a => (a -> OccName) -> [(ModuleName, [GenAvailInfo a])] -> SDoc
750 pprExports getOcc exports = vcat (map (pprExport getOcc) exports)
752 pprExport :: Eq a => (a -> OccName) -> (ModuleName, [GenAvailInfo a]) -> SDoc
753 pprExport getOcc (mod, items)
754 = hsep [ ptext SLIT("__export "), ppr mod, hsep (map pp_avail items) ] <> semi
756 --pp_avail :: GenAvailInfo a -> SDoc
757 pp_avail (Avail name) = ppr (getOcc name)
758 pp_avail (AvailTC _ []) = empty
759 pp_avail (AvailTC n (n':ns))
760 | n==n' = ppr (getOcc n) <> pp_export ns
761 | otherwise = ppr (getOcc n) <> char '|' <> pp_export (n':ns)
764 pp_export names = braces (hsep (map (ppr.getOcc) names))
766 pprOcc :: Name -> SDoc -- Print the occurrence name only
767 pprOcc n = pprOccName (nameOccName n)
772 pprUsages :: (a -> OccName) -> [Usage a] -> SDoc
773 pprUsages getOcc usages = vcat (map (pprUsage getOcc) usages)
775 pprUsage :: (a -> OccName) -> Usage a -> SDoc
776 pprUsage getOcc usage
777 = hsep [ptext SLIT("import"), ppr (usg_name usage),
779 pp_export_version (usg_exports usage),
780 int (usg_rules usage),
781 pp_versions (usg_entities usage)
784 pp_versions nvs = hsep [ ppr (getOcc n) <+> int v | (n,v) <- nvs ]
786 pp_export_version Nothing = empty
787 pp_export_version (Just v) = int v
790 pprDeps :: Dependencies -> SDoc
791 pprDeps (Deps { dep_mods = mods, dep_pkgs = pkgs, dep_orphs = orphs})
792 = vcat [ptext SLIT("module dependencies:") <+> fsep (map ppr_mod mods),
793 ptext SLIT("package dependencies:") <+> fsep (map ppr pkgs),
794 ptext SLIT("orphans:") <+> fsep (map ppr orphs)
797 ppr_mod (mod_name, boot) = ppr mod_name <+> ppr_boot boot
799 ppr_boot True = text "[boot]"
800 ppr_boot False = empty
804 pprIfaceDecls :: NameEnv Int -> IfaceDecls -> SDoc
805 pprIfaceDecls version_map decls
806 = vcat [ vcat [ppr i <+> semi | i <- dcl_insts decls]
807 , vcat (map ppr_decl (dcl_tycl decls))
810 ppr_decl d = ppr_vers d <+> ppr d <> semi
812 -- Print the version for the decl
813 ppr_vers d = case lookupNameEnv version_map (tyClDeclName d) of
819 pprFixities :: FixityEnv
822 pprFixities fixity_map decls
823 = hsep [ ppr fix <+> ppr n
824 | FixitySig n fix _ <- collectFixities fixity_map decls ] <> semi
826 -- Disgusting to print these two together, but that's
827 -- the way the interface parser currently expects them.
828 pprRulesAndDeprecs :: (Outputable a) => [a] -> Deprecations -> SDoc
829 pprRulesAndDeprecs [] NoDeprecs = empty
830 pprRulesAndDeprecs rules deprecs
831 = ptext SLIT("{-##") <+> (pp_rules rules $$ pp_deprecs deprecs) <+> ptext SLIT("##-}")
834 pp_rules rules = ptext SLIT("__R") <+> vcat (map ppr rules)
836 pp_deprecs NoDeprecs = empty
837 pp_deprecs deprecs = ptext SLIT("__D") <+> guts
839 guts = case deprecs of
840 DeprecAll txt -> doubleQuotes (ftext txt)
841 DeprecSome env -> ppr_deprec_env env
843 ppr_deprec_env :: NameEnv (Name, FastString) -> SDoc
844 ppr_deprec_env env = vcat (punctuate semi (map pp_deprec (nameEnvElts env)))
846 pp_deprec (name, txt) = pprOcc name <+> doubleQuotes (ftext txt)