PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/iface/MkIface.lhs

https://bitbucket.org/carter/ghc
Haskell | 1851 lines | 1248 code | 250 blank | 353 comment | 55 complexity | 0a10bcd2d5e130a2f1e748d0980bf01c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. %
  2. % (c) The University of Glasgow 2006-2008
  3. % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
  4. %
  5. \begin{code}
  6. -- | Module for constructing @ModIface@ values (interface files),
  7. -- writing them to disk and comparing two versions to see if
  8. -- recompilation is required.
  9. module MkIface (
  10. mkUsedNames,
  11. mkDependencies,
  12. mkIface, -- Build a ModIface from a ModGuts,
  13. -- including computing version information
  14. mkIfaceTc,
  15. writeIfaceFile, -- Write the interface file
  16. checkOldIface, -- See if recompilation is required, by
  17. -- comparing version information
  18. RecompileRequired(..), recompileRequired,
  19. tyThingToIfaceDecl -- Converting things to their Iface equivalents
  20. ) where
  21. \end{code}
  22. -----------------------------------------------
  23. Recompilation checking
  24. -----------------------------------------------
  25. A complete description of how recompilation checking works can be
  26. found in the wiki commentary:
  27. http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/RecompilationAvoidance
  28. Please read the above page for a top-down description of how this all
  29. works. Notes below cover specific issues related to the implementation.
  30. Basic idea:
  31. * In the mi_usages information in an interface, we record the
  32. fingerprint of each free variable of the module
  33. * In mkIface, we compute the fingerprint of each exported thing A.f.
  34. For each external thing that A.f refers to, we include the fingerprint
  35. of the external reference when computing the fingerprint of A.f. So
  36. if anything that A.f depends on changes, then A.f's fingerprint will
  37. change.
  38. Also record any dependent files added with addDependentFile.
  39. In the future record any #include usages.
  40. * In checkOldIface we compare the mi_usages for the module with
  41. the actual fingerprint for all each thing recorded in mi_usages
  42. \begin{code}
  43. #include "HsVersions.h"
  44. import IfaceSyn
  45. import LoadIface
  46. import FlagChecker
  47. import Id
  48. import IdInfo
  49. import Demand
  50. import Annotations
  51. import CoreSyn
  52. import CoreFVs
  53. import Class
  54. import Kind
  55. import TyCon
  56. import Coercion ( coAxiomSplitLHS )
  57. import DataCon
  58. import Type
  59. import TcType
  60. import InstEnv
  61. import FamInstEnv
  62. import TcRnMonad
  63. import HsSyn
  64. import HscTypes
  65. import Finder
  66. import DynFlags
  67. import VarEnv
  68. import VarSet
  69. import Var
  70. import Name
  71. import Avail
  72. import RdrName
  73. import NameEnv
  74. import NameSet
  75. import Module
  76. import BinIface
  77. import ErrUtils
  78. import Digraph
  79. import SrcLoc
  80. import Outputable
  81. import BasicTypes hiding ( SuccessFlag(..) )
  82. import UniqFM
  83. import Unique
  84. import Util hiding ( eqListBy )
  85. import FastString
  86. import Maybes
  87. import ListSetOps
  88. import Binary
  89. import Fingerprint
  90. import Bag
  91. import Exception
  92. import Control.Monad
  93. import Data.Function
  94. import Data.List
  95. import Data.Map (Map)
  96. import qualified Data.Map as Map
  97. import Data.Ord
  98. import Data.IORef
  99. import System.Directory
  100. import System.FilePath
  101. \end{code}
  102. %************************************************************************
  103. %* *
  104. \subsection{Completing an interface}
  105. %* *
  106. %************************************************************************
  107. \begin{code}
  108. mkIface :: HscEnv
  109. -> Maybe Fingerprint -- The old fingerprint, if we have it
  110. -> ModDetails -- The trimmed, tidied interface
  111. -> ModGuts -- Usages, deprecations, etc
  112. -> IO (Messages,
  113. Maybe (ModIface, -- The new one
  114. Bool)) -- True <=> there was an old Iface, and the
  115. -- new one is identical, so no need
  116. -- to write it
  117. mkIface hsc_env maybe_old_fingerprint mod_details
  118. ModGuts{ mg_module = this_mod,
  119. mg_boot = is_boot,
  120. mg_used_names = used_names,
  121. mg_used_th = used_th,
  122. mg_deps = deps,
  123. mg_dir_imps = dir_imp_mods,
  124. mg_rdr_env = rdr_env,
  125. mg_fix_env = fix_env,
  126. mg_warns = warns,
  127. mg_hpc_info = hpc_info,
  128. mg_safe_haskell = safe_mode,
  129. mg_trust_pkg = self_trust,
  130. mg_dependent_files = dependent_files
  131. }
  132. = mkIface_ hsc_env maybe_old_fingerprint
  133. this_mod is_boot used_names used_th deps rdr_env fix_env
  134. warns hpc_info dir_imp_mods self_trust dependent_files
  135. safe_mode mod_details
  136. -- | make an interface from the results of typechecking only. Useful
  137. -- for non-optimising compilation, or where we aren't generating any
  138. -- object code at all ('HscNothing').
  139. mkIfaceTc :: HscEnv
  140. -> Maybe Fingerprint -- The old fingerprint, if we have it
  141. -> SafeHaskellMode -- The safe haskell mode
  142. -> ModDetails -- gotten from mkBootModDetails, probably
  143. -> TcGblEnv -- Usages, deprecations, etc
  144. -> IO (Messages, Maybe (ModIface, Bool))
  145. mkIfaceTc hsc_env maybe_old_fingerprint safe_mode mod_details
  146. tc_result@TcGblEnv{ tcg_mod = this_mod,
  147. tcg_src = hsc_src,
  148. tcg_imports = imports,
  149. tcg_rdr_env = rdr_env,
  150. tcg_fix_env = fix_env,
  151. tcg_warns = warns,
  152. tcg_hpc = other_hpc_info,
  153. tcg_th_splice_used = tc_splice_used,
  154. tcg_dependent_files = dependent_files
  155. }
  156. = do
  157. let used_names = mkUsedNames tc_result
  158. deps <- mkDependencies tc_result
  159. let hpc_info = emptyHpcInfo other_hpc_info
  160. used_th <- readIORef tc_splice_used
  161. dep_files <- (readIORef dependent_files)
  162. mkIface_ hsc_env maybe_old_fingerprint
  163. this_mod (isHsBoot hsc_src) used_names used_th deps rdr_env
  164. fix_env warns hpc_info (imp_mods imports)
  165. (imp_trust_own_pkg imports) dep_files safe_mode mod_details
  166. mkUsedNames :: TcGblEnv -> NameSet
  167. mkUsedNames TcGblEnv{ tcg_dus = dus } = allUses dus
  168. -- | Extract information from the rename and typecheck phases to produce
  169. -- a dependencies information for the module being compiled.
  170. mkDependencies :: TcGblEnv -> IO Dependencies
  171. mkDependencies
  172. TcGblEnv{ tcg_mod = mod,
  173. tcg_imports = imports,
  174. tcg_th_used = th_var
  175. }
  176. = do
  177. -- Template Haskell used?
  178. th_used <- readIORef th_var
  179. let dep_mods = eltsUFM (delFromUFM (imp_dep_mods imports) (moduleName mod))
  180. -- M.hi-boot can be in the imp_dep_mods, but we must remove
  181. -- it before recording the modules on which this one depends!
  182. -- (We want to retain M.hi-boot in imp_dep_mods so that
  183. -- loadHiBootInterface can see if M's direct imports depend
  184. -- on M.hi-boot, and hence that we should do the hi-boot consistency
  185. -- check.)
  186. pkgs | th_used = insertList thPackageId (imp_dep_pkgs imports)
  187. | otherwise = imp_dep_pkgs imports
  188. -- Set the packages required to be Safe according to Safe Haskell.
  189. -- See Note [RnNames . Tracking Trust Transitively]
  190. sorted_pkgs = sortBy stablePackageIdCmp pkgs
  191. trust_pkgs = imp_trust_pkgs imports
  192. dep_pkgs' = map (\x -> (x, x `elem` trust_pkgs)) sorted_pkgs
  193. return Deps { dep_mods = sortBy (stableModuleNameCmp `on` fst) dep_mods,
  194. dep_pkgs = dep_pkgs',
  195. dep_orphs = sortBy stableModuleCmp (imp_orphs imports),
  196. dep_finsts = sortBy stableModuleCmp (imp_finsts imports) }
  197. -- sort to get into canonical order
  198. -- NB. remember to use lexicographic ordering
  199. mkIface_ :: HscEnv -> Maybe Fingerprint -> Module -> IsBootInterface
  200. -> NameSet -> Bool -> Dependencies -> GlobalRdrEnv
  201. -> NameEnv FixItem -> Warnings -> HpcInfo
  202. -> ImportedMods -> Bool
  203. -> [FilePath]
  204. -> SafeHaskellMode
  205. -> ModDetails
  206. -> IO (Messages, Maybe (ModIface, Bool))
  207. mkIface_ hsc_env maybe_old_fingerprint
  208. this_mod is_boot used_names used_th deps rdr_env fix_env src_warns
  209. hpc_info dir_imp_mods pkg_trust_req dependent_files safe_mode
  210. ModDetails{ md_insts = insts,
  211. md_fam_insts = fam_insts,
  212. md_rules = rules,
  213. md_anns = anns,
  214. md_vect_info = vect_info,
  215. md_types = type_env,
  216. md_exports = exports }
  217. -- NB: notice that mkIface does not look at the bindings
  218. -- only at the TypeEnv. The previous Tidy phase has
  219. -- put exactly the info into the TypeEnv that we want
  220. -- to expose in the interface
  221. = do { usages <- mkUsageInfo hsc_env this_mod dir_imp_mods used_names dependent_files
  222. ; let { entities = typeEnvElts type_env ;
  223. decls = [ tyThingToIfaceDecl entity
  224. | entity <- entities,
  225. let name = getName entity,
  226. not (isImplicitTyThing entity),
  227. -- No implicit Ids and class tycons in the interface file
  228. not (isWiredInName name),
  229. -- Nor wired-in things; the compiler knows about them anyhow
  230. nameIsLocalOrFrom this_mod name ]
  231. -- Sigh: see Note [Root-main Id] in TcRnDriver
  232. ; fixities = [(occ,fix) | FixItem occ fix <- nameEnvElts fix_env]
  233. ; warns = src_warns
  234. ; iface_rules = map (coreRuleToIfaceRule this_mod) rules
  235. ; iface_insts = map instanceToIfaceInst insts
  236. ; iface_fam_insts = map famInstToIfaceFamInst fam_insts
  237. ; iface_vect_info = flattenVectInfo vect_info
  238. ; trust_info = setSafeMode safe_mode
  239. ; intermediate_iface = ModIface {
  240. mi_module = this_mod,
  241. mi_boot = is_boot,
  242. mi_deps = deps,
  243. mi_usages = usages,
  244. mi_exports = mkIfaceExports exports,
  245. -- Sort these lexicographically, so that
  246. -- the result is stable across compilations
  247. mi_insts = sortBy cmp_inst iface_insts,
  248. mi_fam_insts = sortBy cmp_fam_inst iface_fam_insts,
  249. mi_rules = sortBy cmp_rule iface_rules,
  250. mi_vect_info = iface_vect_info,
  251. mi_fixities = fixities,
  252. mi_warns = warns,
  253. mi_anns = mkIfaceAnnotations anns,
  254. mi_globals = maybeGlobalRdrEnv rdr_env,
  255. -- Left out deliberately: filled in by addFingerprints
  256. mi_iface_hash = fingerprint0,
  257. mi_mod_hash = fingerprint0,
  258. mi_flag_hash = fingerprint0,
  259. mi_exp_hash = fingerprint0,
  260. mi_used_th = used_th,
  261. mi_orphan_hash = fingerprint0,
  262. mi_orphan = False, -- Always set by addFingerprints, but
  263. -- it's a strict field, so we can't omit it.
  264. mi_finsts = False, -- Ditto
  265. mi_decls = deliberatelyOmitted "decls",
  266. mi_hash_fn = deliberatelyOmitted "hash_fn",
  267. mi_hpc = isHpcUsed hpc_info,
  268. mi_trust = trust_info,
  269. mi_trust_pkg = pkg_trust_req,
  270. -- And build the cached values
  271. mi_warn_fn = mkIfaceWarnCache warns,
  272. mi_fix_fn = mkIfaceFixCache fixities }
  273. }
  274. ; (new_iface, no_change_at_all)
  275. <- {-# SCC "versioninfo" #-}
  276. addFingerprints hsc_env maybe_old_fingerprint
  277. intermediate_iface decls
  278. -- Warn about orphans
  279. ; let warn_orphs = wopt Opt_WarnOrphans dflags
  280. warn_auto_orphs = wopt Opt_WarnAutoOrphans dflags
  281. orph_warnings --- Laziness means no work done unless -fwarn-orphans
  282. | warn_orphs || warn_auto_orphs = rule_warns `unionBags` inst_warns
  283. | otherwise = emptyBag
  284. errs_and_warns = (orph_warnings, emptyBag)
  285. unqual = mkPrintUnqualified dflags rdr_env
  286. inst_warns = listToBag [ instOrphWarn dflags unqual d
  287. | (d,i) <- insts `zip` iface_insts
  288. , isNothing (ifInstOrph i) ]
  289. rule_warns = listToBag [ ruleOrphWarn dflags unqual this_mod r
  290. | r <- iface_rules
  291. , isNothing (ifRuleOrph r)
  292. , if ifRuleAuto r then warn_auto_orphs
  293. else warn_orphs ]
  294. ; if errorsFound dflags errs_and_warns
  295. then return ( errs_and_warns, Nothing )
  296. else do {
  297. -- Debug printing
  298. ; dumpIfSet_dyn dflags Opt_D_dump_hi "FINAL INTERFACE"
  299. (pprModIface new_iface)
  300. -- bug #1617: on reload we weren't updating the PrintUnqualified
  301. -- correctly. This stems from the fact that the interface had
  302. -- not changed, so addFingerprints returns the old ModIface
  303. -- with the old GlobalRdrEnv (mi_globals).
  304. ; let final_iface = new_iface{ mi_globals = maybeGlobalRdrEnv rdr_env }
  305. ; return (errs_and_warns, Just (final_iface, no_change_at_all)) }}
  306. where
  307. cmp_rule = comparing ifRuleName
  308. -- Compare these lexicographically by OccName, *not* by unique,
  309. -- because the latter is not stable across compilations:
  310. cmp_inst = comparing (nameOccName . ifDFun)
  311. cmp_fam_inst = comparing (nameOccName . ifFamInstTcName)
  312. dflags = hsc_dflags hsc_env
  313. -- We only fill in mi_globals if the module was compiled to byte
  314. -- code. Otherwise, the compiler may not have retained all the
  315. -- top-level bindings and they won't be in the TypeEnv (see
  316. -- Desugar.addExportFlagsAndRules). The mi_globals field is used
  317. -- by GHCi to decide whether the module has its full top-level
  318. -- scope available. (#5534)
  319. maybeGlobalRdrEnv :: GlobalRdrEnv -> Maybe GlobalRdrEnv
  320. maybeGlobalRdrEnv rdr_env
  321. | targetRetainsAllBindings (hscTarget dflags) = Just rdr_env
  322. | otherwise = Nothing
  323. deliberatelyOmitted :: String -> a
  324. deliberatelyOmitted x = panic ("Deliberately omitted: " ++ x)
  325. ifFamInstTcName = ifFamInstFam
  326. flattenVectInfo (VectInfo { vectInfoVar = vVar
  327. , vectInfoTyCon = vTyCon
  328. , vectInfoScalarVars = vScalarVars
  329. , vectInfoScalarTyCons = vScalarTyCons
  330. }) =
  331. IfaceVectInfo
  332. { ifaceVectInfoVar = [Var.varName v | (v, _ ) <- varEnvElts vVar]
  333. , ifaceVectInfoTyCon = [tyConName t | (t, t_v) <- nameEnvElts vTyCon, t /= t_v]
  334. , ifaceVectInfoTyConReuse = [tyConName t | (t, t_v) <- nameEnvElts vTyCon, t == t_v]
  335. , ifaceVectInfoScalarVars = [Var.varName v | v <- varSetElems vScalarVars]
  336. , ifaceVectInfoScalarTyCons = nameSetToList vScalarTyCons
  337. }
  338. -----------------------------
  339. writeIfaceFile :: DynFlags -> ModLocation -> ModIface -> IO ()
  340. writeIfaceFile dflags location new_iface
  341. = do createDirectoryIfMissing True (takeDirectory hi_file_path)
  342. writeBinIface dflags hi_file_path new_iface
  343. where hi_file_path = ml_hi_file location
  344. -- -----------------------------------------------------------------------------
  345. -- Look up parents and versions of Names
  346. -- This is like a global version of the mi_hash_fn field in each ModIface.
  347. -- Given a Name, it finds the ModIface, and then uses mi_hash_fn to get
  348. -- the parent and version info.
  349. mkHashFun
  350. :: HscEnv -- needed to look up versions
  351. -> ExternalPackageState -- ditto
  352. -> (Name -> Fingerprint)
  353. mkHashFun hsc_env eps
  354. = \name ->
  355. let
  356. mod = ASSERT2( isExternalName name, ppr name ) nameModule name
  357. occ = nameOccName name
  358. iface = lookupIfaceByModule (hsc_dflags hsc_env) hpt pit mod `orElse`
  359. pprPanic "lookupVers2" (ppr mod <+> ppr occ)
  360. in
  361. snd (mi_hash_fn iface occ `orElse`
  362. pprPanic "lookupVers1" (ppr mod <+> ppr occ))
  363. where
  364. hpt = hsc_HPT hsc_env
  365. pit = eps_PIT eps
  366. -- ---------------------------------------------------------------------------
  367. -- Compute fingerprints for the interface
  368. addFingerprints
  369. :: HscEnv
  370. -> Maybe Fingerprint -- the old fingerprint, if any
  371. -> ModIface -- The new interface (lacking decls)
  372. -> [IfaceDecl] -- The new decls
  373. -> IO (ModIface, -- Updated interface
  374. Bool) -- True <=> no changes at all;
  375. -- no need to write Iface
  376. addFingerprints hsc_env mb_old_fingerprint iface0 new_decls
  377. = do
  378. eps <- hscEPS hsc_env
  379. let
  380. -- The ABI of a declaration represents everything that is made
  381. -- visible about the declaration that a client can depend on.
  382. -- see IfaceDeclABI below.
  383. declABI :: IfaceDecl -> IfaceDeclABI
  384. declABI decl = (this_mod, decl, extras)
  385. where extras = declExtras fix_fn non_orph_rules non_orph_insts
  386. non_orph_fis decl
  387. edges :: [(IfaceDeclABI, Unique, [Unique])]
  388. edges = [ (abi, getUnique (ifName decl), out)
  389. | decl <- new_decls
  390. , let abi = declABI decl
  391. , let out = localOccs $ freeNamesDeclABI abi
  392. ]
  393. name_module n = ASSERT2( isExternalName n, ppr n ) nameModule n
  394. localOccs = map (getUnique . getParent . getOccName)
  395. . filter ((== this_mod) . name_module)
  396. . nameSetToList
  397. where getParent occ = lookupOccEnv parent_map occ `orElse` occ
  398. -- maps OccNames to their parents in the current module.
  399. -- e.g. a reference to a constructor must be turned into a reference
  400. -- to the TyCon for the purposes of calculating dependencies.
  401. parent_map :: OccEnv OccName
  402. parent_map = foldr extend emptyOccEnv new_decls
  403. where extend d env =
  404. extendOccEnvList env [ (b,n) | b <- ifaceDeclImplicitBndrs d ]
  405. where n = ifName d
  406. -- strongly-connected groups of declarations, in dependency order
  407. groups = stronglyConnCompFromEdgedVertices edges
  408. global_hash_fn = mkHashFun hsc_env eps
  409. -- how to output Names when generating the data to fingerprint.
  410. -- Here we want to output the fingerprint for each top-level
  411. -- Name, whether it comes from the current module or another
  412. -- module. In this way, the fingerprint for a declaration will
  413. -- change if the fingerprint for anything it refers to (transitively)
  414. -- changes.
  415. mk_put_name :: (OccEnv (OccName,Fingerprint))
  416. -> BinHandle -> Name -> IO ()
  417. mk_put_name local_env bh name
  418. | isWiredInName name = putNameLiterally bh name
  419. -- wired-in names don't have fingerprints
  420. | otherwise
  421. = ASSERT2( isExternalName name, ppr name )
  422. let hash | nameModule name /= this_mod = global_hash_fn name
  423. | otherwise = snd (lookupOccEnv local_env (getOccName name)
  424. `orElse` pprPanic "urk! lookup local fingerprint"
  425. (ppr name)) -- (undefined,fingerprint0))
  426. -- This panic indicates that we got the dependency
  427. -- analysis wrong, because we needed a fingerprint for
  428. -- an entity that wasn't in the environment. To debug
  429. -- it, turn the panic into a trace, uncomment the
  430. -- pprTraces below, run the compile again, and inspect
  431. -- the output and the generated .hi file with
  432. -- --show-iface.
  433. in put_ bh hash
  434. -- take a strongly-connected group of declarations and compute
  435. -- its fingerprint.
  436. fingerprint_group :: (OccEnv (OccName,Fingerprint),
  437. [(Fingerprint,IfaceDecl)])
  438. -> SCC IfaceDeclABI
  439. -> IO (OccEnv (OccName,Fingerprint),
  440. [(Fingerprint,IfaceDecl)])
  441. fingerprint_group (local_env, decls_w_hashes) (AcyclicSCC abi)
  442. = do let hash_fn = mk_put_name local_env
  443. decl = abiDecl abi
  444. -- pprTrace "fingerprinting" (ppr (ifName decl) ) $ do
  445. hash <- computeFingerprint hash_fn abi
  446. env' <- extend_hash_env local_env (hash,decl)
  447. return (env', (hash,decl) : decls_w_hashes)
  448. fingerprint_group (local_env, decls_w_hashes) (CyclicSCC abis)
  449. = do let decls = map abiDecl abis
  450. local_env1 <- foldM extend_hash_env local_env
  451. (zip (repeat fingerprint0) decls)
  452. let hash_fn = mk_put_name local_env1
  453. -- pprTrace "fingerprinting" (ppr (map ifName decls) ) $ do
  454. let stable_abis = sortBy cmp_abiNames abis
  455. -- put the cycle in a canonical order
  456. hash <- computeFingerprint hash_fn stable_abis
  457. let pairs = zip (repeat hash) decls
  458. local_env2 <- foldM extend_hash_env local_env pairs
  459. return (local_env2, pairs ++ decls_w_hashes)
  460. -- we have fingerprinted the whole declaration, but we now need
  461. -- to assign fingerprints to all the OccNames that it binds, to
  462. -- use when referencing those OccNames in later declarations.
  463. --
  464. extend_hash_env :: OccEnv (OccName,Fingerprint)
  465. -> (Fingerprint,IfaceDecl)
  466. -> IO (OccEnv (OccName,Fingerprint))
  467. extend_hash_env env0 (hash,d) = do
  468. return (foldr (\(b,fp) env -> extendOccEnv env b (b,fp)) env0
  469. (ifaceDeclFingerprints hash d))
  470. --
  471. (local_env, decls_w_hashes) <-
  472. foldM fingerprint_group (emptyOccEnv, []) groups
  473. -- when calculating fingerprints, we always need to use canonical
  474. -- ordering for lists of things. In particular, the mi_deps has various
  475. -- lists of modules and suchlike, so put these all in canonical order:
  476. let sorted_deps = sortDependencies (mi_deps iface0)
  477. -- the export hash of a module depends on the orphan hashes of the
  478. -- orphan modules below us in the dependency tree. This is the way
  479. -- that changes in orphans get propagated all the way up the
  480. -- dependency tree. We only care about orphan modules in the current
  481. -- package, because changes to orphans outside this package will be
  482. -- tracked by the usage on the ABI hash of package modules that we import.
  483. let orph_mods = filter ((== this_pkg) . modulePackageId)
  484. $ dep_orphs sorted_deps
  485. dep_orphan_hashes <- getOrphanHashes hsc_env orph_mods
  486. orphan_hash <- computeFingerprint (mk_put_name local_env)
  487. (map ifDFun orph_insts, orph_rules, orph_fis)
  488. -- the export list hash doesn't depend on the fingerprints of
  489. -- the Names it mentions, only the Names themselves, hence putNameLiterally.
  490. export_hash <- computeFingerprint putNameLiterally
  491. (mi_exports iface0,
  492. orphan_hash,
  493. dep_orphan_hashes,
  494. dep_pkgs (mi_deps iface0),
  495. -- dep_pkgs: see "Package Version Changes" on
  496. -- wiki/Commentary/Compiler/RecompilationAvoidance
  497. mi_trust iface0)
  498. -- Make sure change of Safe Haskell mode causes recomp.
  499. -- put the declarations in a canonical order, sorted by OccName
  500. let sorted_decls = Map.elems $ Map.fromList $
  501. [(ifName d, e) | e@(_, d) <- decls_w_hashes]
  502. -- the flag hash depends on:
  503. -- - (some of) dflags
  504. -- it returns two hashes, one that shouldn't change
  505. -- the abi hash and one that should
  506. flag_hash <- fingerprintDynFlags dflags this_mod putNameLiterally
  507. -- the ABI hash depends on:
  508. -- - decls
  509. -- - export list
  510. -- - orphans
  511. -- - deprecations
  512. -- - vect info
  513. -- - flag abi hash
  514. mod_hash <- computeFingerprint putNameLiterally
  515. (map fst sorted_decls,
  516. export_hash, -- includes orphan_hash
  517. mi_warns iface0,
  518. mi_vect_info iface0)
  519. -- The interface hash depends on:
  520. -- - the ABI hash, plus
  521. -- - usages
  522. -- - deps
  523. -- - hpc
  524. iface_hash <- computeFingerprint putNameLiterally
  525. (mod_hash,
  526. mi_usages iface0,
  527. sorted_deps,
  528. mi_hpc iface0)
  529. let
  530. no_change_at_all = Just iface_hash == mb_old_fingerprint
  531. final_iface = iface0 {
  532. mi_mod_hash = mod_hash,
  533. mi_iface_hash = iface_hash,
  534. mi_exp_hash = export_hash,
  535. mi_orphan_hash = orphan_hash,
  536. mi_flag_hash = flag_hash,
  537. mi_orphan = not ( null orph_rules
  538. && null orph_insts
  539. && null orph_fis
  540. && isNoIfaceVectInfo (mi_vect_info iface0)),
  541. mi_finsts = not . null $ mi_fam_insts iface0,
  542. mi_decls = sorted_decls,
  543. mi_hash_fn = lookupOccEnv local_env }
  544. --
  545. return (final_iface, no_change_at_all)
  546. where
  547. this_mod = mi_module iface0
  548. dflags = hsc_dflags hsc_env
  549. this_pkg = thisPackage dflags
  550. (non_orph_insts, orph_insts) = mkOrphMap ifInstOrph (mi_insts iface0)
  551. (non_orph_rules, orph_rules) = mkOrphMap ifRuleOrph (mi_rules iface0)
  552. (non_orph_fis, orph_fis) = mkOrphMap ifFamInstOrph (mi_fam_insts iface0)
  553. fix_fn = mi_fix_fn iface0
  554. getOrphanHashes :: HscEnv -> [Module] -> IO [Fingerprint]
  555. getOrphanHashes hsc_env mods = do
  556. eps <- hscEPS hsc_env
  557. let
  558. hpt = hsc_HPT hsc_env
  559. pit = eps_PIT eps
  560. dflags = hsc_dflags hsc_env
  561. get_orph_hash mod =
  562. case lookupIfaceByModule dflags hpt pit mod of
  563. Nothing -> pprPanic "moduleOrphanHash" (ppr mod)
  564. Just iface -> mi_orphan_hash iface
  565. --
  566. return (map get_orph_hash mods)
  567. sortDependencies :: Dependencies -> Dependencies
  568. sortDependencies d
  569. = Deps { dep_mods = sortBy (compare `on` (moduleNameFS.fst)) (dep_mods d),
  570. dep_pkgs = sortBy (stablePackageIdCmp `on` fst) (dep_pkgs d),
  571. dep_orphs = sortBy stableModuleCmp (dep_orphs d),
  572. dep_finsts = sortBy stableModuleCmp (dep_finsts d) }
  573. \end{code}
  574. %************************************************************************
  575. %* *
  576. The ABI of an IfaceDecl
  577. %* *
  578. %************************************************************************
  579. Note [The ABI of an IfaceDecl]
  580. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  581. The ABI of a declaration consists of:
  582. (a) the full name of the identifier (inc. module and package,
  583. because these are used to construct the symbol name by which
  584. the identifier is known externally).
  585. (b) the declaration itself, as exposed to clients. That is, the
  586. definition of an Id is included in the fingerprint only if
  587. it is made available as as unfolding in the interface.
  588. (c) the fixity of the identifier
  589. (d) for Ids: rules
  590. (e) for classes: instances, fixity & rules for methods
  591. (f) for datatypes: instances, fixity & rules for constrs
  592. Items (c)-(f) are not stored in the IfaceDecl, but instead appear
  593. elsewhere in the interface file. But they are *fingerprinted* with
  594. the declaration itself. This is done by grouping (c)-(f) in IfaceDeclExtras,
  595. and fingerprinting that as part of the declaration.
  596. \begin{code}
  597. type IfaceDeclABI = (Module, IfaceDecl, IfaceDeclExtras)
  598. data IfaceDeclExtras
  599. = IfaceIdExtras Fixity [IfaceRule]
  600. | IfaceDataExtras
  601. Fixity -- Fixity of the tycon itself
  602. [IfaceInstABI] -- Local class and family instances of this tycon
  603. -- See Note [Orphans] in IfaceSyn
  604. [(Fixity,[IfaceRule])] -- For each construcotr, fixity and RULES
  605. | IfaceClassExtras
  606. Fixity -- Fixity of the class itself
  607. [IfaceInstABI] -- Local instances of this class *or*
  608. -- of its associated data types
  609. -- See Note [Orphans] in IfaceSyn
  610. [(Fixity,[IfaceRule])] -- For each class method, fixity and RULES
  611. | IfaceSynExtras Fixity [IfaceInstABI]
  612. | IfaceOtherDeclExtras
  613. -- When hashing a class or family instance, we hash only the
  614. -- DFunId or CoAxiom, because that depends on all the
  615. -- information about the instance.
  616. --
  617. type IfaceInstABI = IfExtName -- Name of DFunId or CoAxiom that is evidence for the instance
  618. abiDecl :: IfaceDeclABI -> IfaceDecl
  619. abiDecl (_, decl, _) = decl
  620. cmp_abiNames :: IfaceDeclABI -> IfaceDeclABI -> Ordering
  621. cmp_abiNames abi1 abi2 = ifName (abiDecl abi1) `compare`
  622. ifName (abiDecl abi2)
  623. freeNamesDeclABI :: IfaceDeclABI -> NameSet
  624. freeNamesDeclABI (_mod, decl, extras) =
  625. freeNamesIfDecl decl `unionNameSets` freeNamesDeclExtras extras
  626. freeNamesDeclExtras :: IfaceDeclExtras -> NameSet
  627. freeNamesDeclExtras (IfaceIdExtras _ rules)
  628. = unionManyNameSets (map freeNamesIfRule rules)
  629. freeNamesDeclExtras (IfaceDataExtras _ insts subs)
  630. = unionManyNameSets (mkNameSet insts : map freeNamesSub subs)
  631. freeNamesDeclExtras (IfaceClassExtras _ insts subs)
  632. = unionManyNameSets (mkNameSet insts : map freeNamesSub subs)
  633. freeNamesDeclExtras (IfaceSynExtras _ insts)
  634. = mkNameSet insts
  635. freeNamesDeclExtras IfaceOtherDeclExtras
  636. = emptyNameSet
  637. freeNamesSub :: (Fixity,[IfaceRule]) -> NameSet
  638. freeNamesSub (_,rules) = unionManyNameSets (map freeNamesIfRule rules)
  639. instance Outputable IfaceDeclExtras where
  640. ppr IfaceOtherDeclExtras = empty
  641. ppr (IfaceIdExtras fix rules) = ppr_id_extras fix rules
  642. ppr (IfaceSynExtras fix finsts) = vcat [ppr fix, ppr finsts]
  643. ppr (IfaceDataExtras fix insts stuff) = vcat [ppr fix, ppr_insts insts,
  644. ppr_id_extras_s stuff]
  645. ppr (IfaceClassExtras fix insts stuff) = vcat [ppr fix, ppr_insts insts,
  646. ppr_id_extras_s stuff]
  647. ppr_insts :: [IfaceInstABI] -> SDoc
  648. ppr_insts _ = ptext (sLit "<insts>")
  649. ppr_id_extras_s :: [(Fixity, [IfaceRule])] -> SDoc
  650. ppr_id_extras_s stuff = vcat [ppr_id_extras f r | (f,r)<- stuff]
  651. ppr_id_extras :: Fixity -> [IfaceRule] -> SDoc
  652. ppr_id_extras fix rules = ppr fix $$ vcat (map ppr rules)
  653. -- This instance is used only to compute fingerprints
  654. instance Binary IfaceDeclExtras where
  655. get _bh = panic "no get for IfaceDeclExtras"
  656. put_ bh (IfaceIdExtras fix rules) = do
  657. putByte bh 1; put_ bh fix; put_ bh rules
  658. put_ bh (IfaceDataExtras fix insts cons) = do
  659. putByte bh 2; put_ bh fix; put_ bh insts; put_ bh cons
  660. put_ bh (IfaceClassExtras fix insts methods) = do
  661. putByte bh 3; put_ bh fix; put_ bh insts; put_ bh methods
  662. put_ bh (IfaceSynExtras fix finsts) = do
  663. putByte bh 4; put_ bh fix; put_ bh finsts
  664. put_ bh IfaceOtherDeclExtras = do
  665. putByte bh 5
  666. declExtras :: (OccName -> Fixity)
  667. -> OccEnv [IfaceRule]
  668. -> OccEnv [IfaceClsInst]
  669. -> OccEnv [IfaceFamInst]
  670. -> IfaceDecl
  671. -> IfaceDeclExtras
  672. declExtras fix_fn rule_env inst_env fi_env decl
  673. = case decl of
  674. IfaceId{} -> IfaceIdExtras (fix_fn n)
  675. (lookupOccEnvL rule_env n)
  676. IfaceData{ifCons=cons} ->
  677. IfaceDataExtras (fix_fn n)
  678. (map ifFamInstAxiom (lookupOccEnvL fi_env n) ++
  679. map ifDFun (lookupOccEnvL inst_env n))
  680. (map (id_extras . ifConOcc) (visibleIfConDecls cons))
  681. IfaceClass{ifSigs=sigs, ifATs=ats} ->
  682. IfaceClassExtras (fix_fn n)
  683. (map ifDFun $ (concatMap at_extras ats)
  684. ++ lookupOccEnvL inst_env n)
  685. -- Include instances of the associated types
  686. -- as well as instances of the class (Trac #5147)
  687. [id_extras op | IfaceClassOp op _ _ <- sigs]
  688. IfaceSyn{} -> IfaceSynExtras (fix_fn n)
  689. (map ifFamInstAxiom (lookupOccEnvL fi_env n))
  690. _other -> IfaceOtherDeclExtras
  691. where
  692. n = ifName decl
  693. id_extras occ = (fix_fn occ, lookupOccEnvL rule_env occ)
  694. at_extras (IfaceAT decl _) = lookupOccEnvL inst_env (ifName decl)
  695. lookupOccEnvL :: OccEnv [v] -> OccName -> [v]
  696. lookupOccEnvL env k = lookupOccEnv env k `orElse` []
  697. -- used when we want to fingerprint a structure without depending on the
  698. -- fingerprints of external Names that it refers to.
  699. putNameLiterally :: BinHandle -> Name -> IO ()
  700. putNameLiterally bh name = ASSERT( isExternalName name )
  701. do { put_ bh $! nameModule name
  702. ; put_ bh $! nameOccName name }
  703. {-
  704. -- for testing: use the md5sum command to generate fingerprints and
  705. -- compare the results against our built-in version.
  706. fp' <- oldMD5 dflags bh
  707. if fp /= fp' then pprPanic "computeFingerprint" (ppr fp <+> ppr fp')
  708. else return fp
  709. oldMD5 dflags bh = do
  710. tmp <- newTempName dflags "bin"
  711. writeBinMem bh tmp
  712. tmp2 <- newTempName dflags "md5"
  713. let cmd = "md5sum " ++ tmp ++ " >" ++ tmp2
  714. r <- system cmd
  715. case r of
  716. ExitFailure _ -> ghcError (PhaseFailed cmd r)
  717. ExitSuccess -> do
  718. hash_str <- readFile tmp2
  719. return $! readHexFingerprint hash_str
  720. -}
  721. instOrphWarn :: DynFlags -> PrintUnqualified -> ClsInst -> WarnMsg
  722. instOrphWarn dflags unqual inst
  723. = mkWarnMsg dflags (getSrcSpan inst) unqual $
  724. hang (ptext (sLit "Orphan instance:")) 2 (pprInstanceHdr inst)
  725. ruleOrphWarn :: DynFlags -> PrintUnqualified -> Module -> IfaceRule -> WarnMsg
  726. ruleOrphWarn dflags unqual mod rule
  727. = mkWarnMsg dflags silly_loc unqual $
  728. ptext (sLit "Orphan rule:") <+> ppr rule
  729. where
  730. silly_loc = srcLocSpan (mkSrcLoc (moduleNameFS (moduleName mod)) 1 1)
  731. -- We don't have a decent SrcSpan for a Rule, not even the CoreRule
  732. -- Could readily be fixed by adding a SrcSpan to CoreRule, if we wanted to
  733. ----------------------
  734. -- mkOrphMap partitions instance decls or rules into
  735. -- (a) an OccEnv for ones that are not orphans,
  736. -- mapping the local OccName to a list of its decls
  737. -- (b) a list of orphan decls
  738. mkOrphMap :: (decl -> Maybe OccName) -- (Just occ) for a non-orphan decl, keyed by occ
  739. -- Nothing for an orphan decl
  740. -> [decl] -- Sorted into canonical order
  741. -> (OccEnv [decl], -- Non-orphan decls associated with their key;
  742. -- each sublist in canonical order
  743. [decl]) -- Orphan decls; in canonical order
  744. mkOrphMap get_key decls
  745. = foldl go (emptyOccEnv, []) decls
  746. where
  747. go (non_orphs, orphs) d
  748. | Just occ <- get_key d
  749. = (extendOccEnv_Acc (:) singleton non_orphs occ d, orphs)
  750. | otherwise = (non_orphs, d:orphs)
  751. \end{code}
  752. %************************************************************************
  753. %* *
  754. Keeping track of what we've slurped, and fingerprints
  755. %* *
  756. %************************************************************************
  757. \begin{code}
  758. mkUsageInfo :: HscEnv -> Module -> ImportedMods -> NameSet -> [FilePath] -> IO [Usage]
  759. mkUsageInfo hsc_env this_mod dir_imp_mods used_names dependent_files
  760. = do { eps <- hscEPS hsc_env
  761. ; mtimes <- mapM getModificationUTCTime dependent_files
  762. ; let mod_usages = mk_mod_usage_info (eps_PIT eps) hsc_env this_mod
  763. dir_imp_mods used_names
  764. ; let usages = mod_usages ++ map to_file_usage (zip dependent_files mtimes)
  765. ; usages `seqList` return usages }
  766. -- seq the list of Usages returned: occasionally these
  767. -- don't get evaluated for a while and we can end up hanging on to
  768. -- the entire collection of Ifaces.
  769. where
  770. to_file_usage (f, mtime) = UsageFile { usg_file_path = f, usg_mtime = mtime }
  771. mk_mod_usage_info :: PackageIfaceTable
  772. -> HscEnv
  773. -> Module
  774. -> ImportedMods
  775. -> NameSet
  776. -> [Usage]
  777. mk_mod_usage_info pit hsc_env this_mod direct_imports used_names
  778. = mapCatMaybes mkUsage usage_mods
  779. where
  780. hpt = hsc_HPT hsc_env
  781. dflags = hsc_dflags hsc_env
  782. this_pkg = thisPackage dflags
  783. used_mods = moduleEnvKeys ent_map
  784. dir_imp_mods = moduleEnvKeys direct_imports
  785. all_mods = used_mods ++ filter (`notElem` used_mods) dir_imp_mods
  786. usage_mods = sortBy stableModuleCmp all_mods
  787. -- canonical order is imported, to avoid interface-file
  788. -- wobblage.
  789. -- ent_map groups together all the things imported and used
  790. -- from a particular module
  791. ent_map :: ModuleEnv [OccName]
  792. ent_map = foldNameSet add_mv emptyModuleEnv used_names
  793. where
  794. add_mv name mv_map
  795. | isWiredInName name = mv_map -- ignore wired-in names
  796. | otherwise
  797. = case nameModule_maybe name of
  798. Nothing -> ASSERT2( isSystemName name, ppr name ) mv_map
  799. -- See Note [Internal used_names]
  800. Just mod -> -- This lambda function is really just a
  801. -- specialised (++); originally came about to
  802. -- avoid quadratic behaviour (trac #2680)
  803. extendModuleEnvWith (\_ xs -> occ:xs) mv_map mod [occ]
  804. where occ = nameOccName name
  805. -- We want to create a Usage for a home module if
  806. -- a) we used something from it; has something in used_names
  807. -- b) we imported it, even if we used nothing from it
  808. -- (need to recompile if its export list changes: export_fprint)
  809. mkUsage :: Module -> Maybe Usage
  810. mkUsage mod
  811. | isNothing maybe_iface -- We can't depend on it if we didn't
  812. -- load its interface.
  813. || mod == this_mod -- We don't care about usages of
  814. -- things in *this* module
  815. = Nothing
  816. | modulePackageId mod /= this_pkg
  817. = Just UsagePackageModule{ usg_mod = mod,
  818. usg_mod_hash = mod_hash,
  819. usg_safe = imp_safe }
  820. -- for package modules, we record the module hash only
  821. | (null used_occs
  822. && isNothing export_hash
  823. && not is_direct_import
  824. && not finsts_mod)
  825. = Nothing -- Record no usage info
  826. -- for directly-imported modules, we always want to record a usage
  827. -- on the orphan hash. This is what triggers a recompilation if
  828. -- an orphan is added or removed somewhere below us in the future.
  829. | otherwise
  830. = Just UsageHomeModule {
  831. usg_mod_name = moduleName mod,
  832. usg_mod_hash = mod_hash,
  833. usg_exports = export_hash,
  834. usg_entities = Map.toList ent_hashs,
  835. usg_safe = imp_safe }
  836. where
  837. maybe_iface = lookupIfaceByModule dflags hpt pit mod
  838. -- In one-shot mode, the interfaces for home-package
  839. -- modules accumulate in the PIT not HPT. Sigh.
  840. Just iface = maybe_iface
  841. finsts_mod = mi_finsts iface
  842. hash_env = mi_hash_fn iface
  843. mod_hash = mi_mod_hash iface
  844. export_hash | depend_on_exports = Just (mi_exp_hash iface)
  845. | otherwise = Nothing
  846. (is_direct_import, imp_safe)
  847. = case lookupModuleEnv direct_imports mod of
  848. Just ((_,_,_,safe):_xs) -> (True, safe)
  849. Just _ -> pprPanic "mkUsage: empty direct import" empty
  850. Nothing -> (False, safeImplicitImpsReq dflags)
  851. -- Nothing case is for implicit imports like 'System.IO' when 'putStrLn'
  852. -- is used in the source code. We require them to be safe in Safe Haskell
  853. used_occs = lookupModuleEnv ent_map mod `orElse` []
  854. -- Making a Map here ensures that (a) we remove duplicates
  855. -- when we have usages on several subordinates of a single parent,
  856. -- and (b) that the usages emerge in a canonical order, which
  857. -- is why we use Map rather than OccEnv: Map works
  858. -- using Ord on the OccNames, which is a lexicographic ordering.
  859. ent_hashs :: Map OccName Fingerprint
  860. ent_hashs = Map.fromList (map lookup_occ used_occs)
  861. lookup_occ occ =
  862. case hash_env occ of
  863. Nothing -> pprPanic "mkUsage" (ppr mod <+> ppr occ <+> ppr used_names)
  864. Just r -> r
  865. depend_on_exports = is_direct_import
  866. {- True
  867. Even if we used 'import M ()', we have to register a
  868. usage on the export list because we are sensitive to
  869. changes in orphan instances/rules.
  870. False
  871. In GHC 6.8.x we always returned true, and in
  872. fact it recorded a dependency on *all* the
  873. modules underneath in the dependency tree. This
  874. happens to make orphans work right, but is too
  875. expensive: it'll read too many interface files.
  876. The 'isNothing maybe_iface' check above saved us
  877. from generating many of these usages (at least in
  878. one-shot mode), but that's even more bogus!
  879. -}
  880. \end{code}
  881. \begin{code}
  882. mkIfaceAnnotations :: [Annotation] -> [IfaceAnnotation]
  883. mkIfaceAnnotations = map mkIfaceAnnotation
  884. mkIfaceAnnotation :: Annotation -> IfaceAnnotation
  885. mkIfaceAnnotation (Annotation { ann_target = target, ann_value = serialized }) = IfaceAnnotation {
  886. ifAnnotatedTarget = fmap nameOccName target,
  887. ifAnnotatedValue = serialized
  888. }
  889. \end{code}
  890. \begin{code}
  891. mkIfaceExports :: [AvailInfo] -> [IfaceExport] -- Sort to make canonical
  892. mkIfaceExports exports
  893. = sortBy stableAvailCmp (map sort_subs exports)
  894. where
  895. sort_subs :: AvailInfo -> AvailInfo
  896. sort_subs (Avail n) = Avail n
  897. sort_subs (AvailTC n []) = AvailTC n []
  898. sort_subs (AvailTC n (m:ms))
  899. | n==m = AvailTC n (m:sortBy stableNameCmp ms)
  900. | otherwise = AvailTC n (sortBy stableNameCmp (m:ms))
  901. -- Maintain the AvailTC Invariant
  902. \end{code}
  903. Note [Orignal module]
  904. ~~~~~~~~~~~~~~~~~~~~~
  905. Consider this:
  906. module X where { data family T }
  907. module Y( T(..) ) where { import X; data instance T Int = MkT Int }
  908. The exported Avail from Y will look like
  909. X.T{X.T, Y.MkT}
  910. That is, in Y,
  911. - only MkT is brought into scope by the data instance;
  912. - but the parent (used for grouping and naming in T(..) exports) is X.T
  913. - and in this case we export X.T too
  914. In the result of MkIfaceExports, the names are grouped by defining module,
  915. so we may need to split up a single Avail into multiple ones.
  916. Note [Internal used_names]
  917. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  918. Most of the used_names are External Names, but we can have Internal
  919. Names too: see Note [Binders in Template Haskell] in Convert, and
  920. Trac #5362 for an example. Such Names are always
  921. - Such Names are always for locally-defined things, for which we
  922. don't gather usage info, so we can just ignore them in ent_map
  923. - They are always System Names, hence the assert, just as a double check.
  924. %************************************************************************
  925. %* *
  926. Load the old interface file for this module (unless
  927. we have it already), and check whether it is up to date
  928. %* *
  929. %************************************************************************
  930. \begin{code}
  931. data RecompileRequired
  932. = UpToDate
  933. -- ^ everything is up to date, recompilation is not required
  934. | MustCompile
  935. -- ^ The .hs file has been touched, or the .o/.hi file does not exist
  936. | RecompBecause String
  937. -- ^ The .o/.hi files are up to date, but something else has changed
  938. -- to force recompilation; the String says what (one-line summary)
  939. deriving Eq
  940. recompileRequired :: RecompileRequired -> Bool
  941. recompileRequired UpToDate = False
  942. recompileRequired _ = True
  943. -- | Top level function to check if the version of an old interface file
  944. -- is equivalent to the current source file the user asked us to compile.
  945. -- If the same, we can avoid recompilation. We return a tuple where the
  946. -- first element is a bool saying if we should recompile the object file
  947. -- and the second is maybe the interface file, where Nothng means to
  948. -- rebuild the interface file not use the exisitng one.
  949. checkOldIface :: HscEnv
  950. -> ModSummary
  951. -> SourceModified
  952. -> Maybe ModIface -- Old interface from compilation manager, if any
  953. -> IO (RecompileRequired, Maybe ModIface)
  954. checkOldIface hsc_env mod_summary source_modified maybe_iface
  955. = do let dflags = hsc_dflags hsc_env
  956. showPass dflags $
  957. "Checking old interface for " ++ (showPpr dflags $ ms_mod mod_summary)
  958. initIfaceCheck hsc_env $
  959. check_old_iface hsc_env mod_summary source_modified maybe_iface
  960. check_old_iface :: HscEnv -> ModSummary -> SourceModified -> Maybe ModIface
  961. -> IfG (RecompileRequired, Maybe ModIface)
  962. check_old_iface hsc_env mod_summary src_modified maybe_iface
  963. = let dflags = hsc_dflags hsc_env
  964. getIface =
  965. case maybe_iface of
  966. Just _ -> do
  967. traceIf (text "We already have the old interface for" <+> ppr (ms_mod mod_summary))
  968. return maybe_iface
  969. Nothing -> loadIface
  970. loadIface = do
  971. let iface_path = msHiFilePath mod_summary
  972. read_result <- readIface (ms_mod mod_summary) iface_path False
  973. case read_result of
  974. Failed err -> do
  975. traceIf (text "FYI: cannont read old interface file:" $$ nest 4 err)
  976. return Nothing
  977. Succeeded iface -> do
  978. traceIf (text "Read the interface file" <+> text iface_path)
  979. return $ Just iface
  980. src_changed
  981. | dopt Opt_ForceRecomp (hsc_dflags hsc_env) = True
  982. | SourceModified <- src_modified = True
  983. | otherwise = False
  984. in do
  985. when src_changed $
  986. traceHiDiffs (nest 4 $ text "Source file changed or recompilation check turned off")
  987. case src_changed of
  988. -- If the source has changed and we're in interactive mode,
  989. -- avoid reading an interface; just return the one we might
  990. -- have been supplied with.
  991. True | not (isObjectTarget $ hscTarget dflags) ->
  992. return (MustCompile, maybe_iface)
  993. -- Try and read the old interface for the current module
  994. -- from the .hi file left from the last time we compiled it
  995. True -> do
  996. maybe_iface' <- getIface
  997. return (MustCompile, maybe_iface')
  998. False -> do
  999. maybe_iface' <- getIface
  1000. case maybe_iface' of
  1001. -- We can't retrieve the iface
  1002. Nothing -> return (MustCompile, Nothing)
  1003. -- We have got the old iface; check its versions
  1004. -- even in the SourceUnmodifiedAndStable case we
  1005. -- should check versions because some packages
  1006. -- might have changed or gone away.

Large files files are truncated, but you can click here to view the full file