PageRenderTime 118ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/iface/MkIface.lhs

https://github.com/luite/ghc
Haskell | 1864 lines | 1256 code | 254 blank | 354 comment | 55 complexity | 7c199006a747c421f3ffd16b12f6c782 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 Coercion( tidyCo )
  51. import Annotations
  52. import CoreSyn
  53. import CoreFVs
  54. import Class
  55. import Kind
  56. import TyCon
  57. import CoAxiom
  58. import DataCon
  59. import Type
  60. import TcType
  61. import InstEnv
  62. import FamInstEnv
  63. import TcRnMonad
  64. import HsSyn
  65. import HscTypes
  66. import Finder
  67. import DynFlags
  68. import VarEnv
  69. import VarSet
  70. import Var
  71. import Name
  72. import Avail
  73. import RdrName
  74. import NameEnv
  75. import NameSet
  76. import Module
  77. import BinIface
  78. import ErrUtils
  79. import Digraph
  80. import SrcLoc
  81. import Outputable
  82. import BasicTypes hiding ( SuccessFlag(..) )
  83. import UniqFM
  84. import Unique
  85. import Util hiding ( eqListBy )
  86. import FastString
  87. import Maybes
  88. import ListSetOps
  89. import Binary
  90. import Fingerprint
  91. import Bag
  92. import Exception
  93. import Control.Monad
  94. import Data.Function
  95. import Data.List
  96. import Data.Map (Map)
  97. import qualified Data.Map as Map
  98. import Data.Ord
  99. import Data.IORef
  100. import System.Directory
  101. import System.FilePath
  102. \end{code}
  103. %************************************************************************
  104. %* *
  105. \subsection{Completing an interface}
  106. %* *
  107. %************************************************************************
  108. \begin{code}
  109. mkIface :: HscEnv
  110. -> Maybe Fingerprint -- The old fingerprint, if we have it
  111. -> ModDetails -- The trimmed, tidied interface
  112. -> ModGuts -- Usages, deprecations, etc
  113. -> IO (Messages,
  114. Maybe (ModIface, -- The new one
  115. Bool)) -- True <=> there was an old Iface, and the
  116. -- new one is identical, so no need
  117. -- to write it
  118. mkIface hsc_env maybe_old_fingerprint mod_details
  119. ModGuts{ mg_module = this_mod,
  120. mg_boot = is_boot,
  121. mg_used_names = used_names,
  122. mg_used_th = used_th,
  123. mg_deps = deps,
  124. mg_dir_imps = dir_imp_mods,
  125. mg_rdr_env = rdr_env,
  126. mg_fix_env = fix_env,
  127. mg_warns = warns,
  128. mg_hpc_info = hpc_info,
  129. mg_safe_haskell = safe_mode,
  130. mg_trust_pkg = self_trust,
  131. mg_dependent_files = dependent_files
  132. }
  133. = mkIface_ hsc_env maybe_old_fingerprint
  134. this_mod is_boot used_names used_th deps rdr_env fix_env
  135. warns hpc_info dir_imp_mods self_trust dependent_files
  136. safe_mode mod_details
  137. -- | make an interface from the results of typechecking only. Useful
  138. -- for non-optimising compilation, or where we aren't generating any
  139. -- object code at all ('HscNothing').
  140. mkIfaceTc :: HscEnv
  141. -> Maybe Fingerprint -- The old fingerprint, if we have it
  142. -> SafeHaskellMode -- The safe haskell mode
  143. -> ModDetails -- gotten from mkBootModDetails, probably
  144. -> TcGblEnv -- Usages, deprecations, etc
  145. -> IO (Messages, Maybe (ModIface, Bool))
  146. mkIfaceTc hsc_env maybe_old_fingerprint safe_mode mod_details
  147. tc_result@TcGblEnv{ tcg_mod = this_mod,
  148. tcg_src = hsc_src,
  149. tcg_imports = imports,
  150. tcg_rdr_env = rdr_env,
  151. tcg_fix_env = fix_env,
  152. tcg_warns = warns,
  153. tcg_hpc = other_hpc_info,
  154. tcg_th_splice_used = tc_splice_used,
  155. tcg_dependent_files = dependent_files
  156. }
  157. = do
  158. let used_names = mkUsedNames tc_result
  159. deps <- mkDependencies tc_result
  160. let hpc_info = emptyHpcInfo other_hpc_info
  161. used_th <- readIORef tc_splice_used
  162. dep_files <- (readIORef dependent_files)
  163. mkIface_ hsc_env maybe_old_fingerprint
  164. this_mod (isHsBoot hsc_src) used_names used_th deps rdr_env
  165. fix_env warns hpc_info (imp_mods imports)
  166. (imp_trust_own_pkg imports) dep_files safe_mode mod_details
  167. mkUsedNames :: TcGblEnv -> NameSet
  168. mkUsedNames TcGblEnv{ tcg_dus = dus } = allUses dus
  169. -- | Extract information from the rename and typecheck phases to produce
  170. -- a dependencies information for the module being compiled.
  171. mkDependencies :: TcGblEnv -> IO Dependencies
  172. mkDependencies
  173. TcGblEnv{ tcg_mod = mod,
  174. tcg_imports = imports,
  175. tcg_th_used = th_var
  176. }
  177. = do
  178. -- Template Haskell used?
  179. th_used <- readIORef th_var
  180. let dep_mods = eltsUFM (delFromUFM (imp_dep_mods imports) (moduleName mod))
  181. -- M.hi-boot can be in the imp_dep_mods, but we must remove
  182. -- it before recording the modules on which this one depends!
  183. -- (We want to retain M.hi-boot in imp_dep_mods so that
  184. -- loadHiBootInterface can see if M's direct imports depend
  185. -- on M.hi-boot, and hence that we should do the hi-boot consistency
  186. -- check.)
  187. pkgs | th_used = insertList thPackageId (imp_dep_pkgs imports)
  188. | otherwise = imp_dep_pkgs imports
  189. -- Set the packages required to be Safe according to Safe Haskell.
  190. -- See Note [RnNames . Tracking Trust Transitively]
  191. sorted_pkgs = sortBy stablePackageIdCmp pkgs
  192. trust_pkgs = imp_trust_pkgs imports
  193. dep_pkgs' = map (\x -> (x, x `elem` trust_pkgs)) sorted_pkgs
  194. return Deps { dep_mods = sortBy (stableModuleNameCmp `on` fst) dep_mods,
  195. dep_pkgs = dep_pkgs',
  196. dep_orphs = sortBy stableModuleCmp (imp_orphs imports),
  197. dep_finsts = sortBy stableModuleCmp (imp_finsts imports) }
  198. -- sort to get into canonical order
  199. -- NB. remember to use lexicographic ordering
  200. mkIface_ :: HscEnv -> Maybe Fingerprint -> Module -> IsBootInterface
  201. -> NameSet -> Bool -> Dependencies -> GlobalRdrEnv
  202. -> NameEnv FixItem -> Warnings -> HpcInfo
  203. -> ImportedMods -> Bool
  204. -> [FilePath]
  205. -> SafeHaskellMode
  206. -> ModDetails
  207. -> IO (Messages, Maybe (ModIface, Bool))
  208. mkIface_ hsc_env maybe_old_fingerprint
  209. this_mod is_boot used_names used_th deps rdr_env fix_env src_warns
  210. hpc_info dir_imp_mods pkg_trust_req dependent_files safe_mode
  211. ModDetails{ md_insts = insts,
  212. md_fam_insts = fam_insts,
  213. md_rules = rules,
  214. md_anns = anns,
  215. md_vect_info = vect_info,
  216. md_types = type_env,
  217. md_exports = exports }
  218. -- NB: notice that mkIface does not look at the bindings
  219. -- only at the TypeEnv. The previous Tidy phase has
  220. -- put exactly the info into the TypeEnv that we want
  221. -- to expose in the interface
  222. = do { usages <- mkUsageInfo hsc_env this_mod dir_imp_mods used_names dependent_files
  223. ; let { entities = typeEnvElts type_env ;
  224. decls = [ tyThingToIfaceDecl entity
  225. | entity <- entities,
  226. let name = getName entity,
  227. not (isImplicitTyThing entity),
  228. -- No implicit Ids and class tycons in the interface file
  229. not (isWiredInName name),
  230. -- Nor wired-in things; the compiler knows about them anyhow
  231. nameIsLocalOrFrom this_mod name ]
  232. -- Sigh: see Note [Root-main Id] in TcRnDriver
  233. ; fixities = [(occ,fix) | FixItem occ fix <- nameEnvElts fix_env]
  234. ; warns = src_warns
  235. ; iface_rules = map (coreRuleToIfaceRule this_mod) rules
  236. ; iface_insts = map instanceToIfaceInst insts
  237. ; iface_fam_insts = map famInstToIfaceFamInst fam_insts
  238. ; iface_vect_info = flattenVectInfo vect_info
  239. ; trust_info = setSafeMode safe_mode
  240. ; intermediate_iface = ModIface {
  241. mi_module = this_mod,
  242. mi_boot = is_boot,
  243. mi_deps = deps,
  244. mi_usages = usages,
  245. mi_exports = mkIfaceExports exports,
  246. -- Sort these lexicographically, so that
  247. -- the result is stable across compilations
  248. mi_insts = sortBy cmp_inst iface_insts,
  249. mi_fam_insts = sortBy cmp_fam_inst iface_fam_insts,
  250. mi_rules = sortBy cmp_rule iface_rules,
  251. mi_vect_info = iface_vect_info,
  252. mi_fixities = fixities,
  253. mi_warns = warns,
  254. mi_anns = mkIfaceAnnotations anns,
  255. mi_globals = maybeGlobalRdrEnv rdr_env,
  256. -- Left out deliberately: filled in by addFingerprints
  257. mi_iface_hash = fingerprint0,
  258. mi_mod_hash = fingerprint0,
  259. mi_flag_hash = fingerprint0,
  260. mi_exp_hash = fingerprint0,
  261. mi_used_th = used_th,
  262. mi_orphan_hash = fingerprint0,
  263. mi_orphan = False, -- Always set by addFingerprints, but
  264. -- it's a strict field, so we can't omit it.
  265. mi_finsts = False, -- Ditto
  266. mi_decls = deliberatelyOmitted "decls",
  267. mi_hash_fn = deliberatelyOmitted "hash_fn",
  268. mi_hpc = isHpcUsed hpc_info,
  269. mi_trust = trust_info,
  270. mi_trust_pkg = pkg_trust_req,
  271. -- And build the cached values
  272. mi_warn_fn = mkIfaceWarnCache warns,
  273. mi_fix_fn = mkIfaceFixCache fixities }
  274. }
  275. ; (new_iface, no_change_at_all)
  276. <- {-# SCC "versioninfo" #-}
  277. addFingerprints hsc_env maybe_old_fingerprint
  278. intermediate_iface decls
  279. -- Warn about orphans
  280. ; let warn_orphs = wopt Opt_WarnOrphans dflags
  281. warn_auto_orphs = wopt Opt_WarnAutoOrphans dflags
  282. orph_warnings --- Laziness means no work done unless -fwarn-orphans
  283. | warn_orphs || warn_auto_orphs = rule_warns `unionBags` inst_warns
  284. | otherwise = emptyBag
  285. errs_and_warns = (orph_warnings, emptyBag)
  286. unqual = mkPrintUnqualified dflags rdr_env
  287. inst_warns = listToBag [ instOrphWarn dflags unqual d
  288. | (d,i) <- insts `zip` iface_insts
  289. , isNothing (ifInstOrph i) ]
  290. rule_warns = listToBag [ ruleOrphWarn dflags unqual this_mod r
  291. | r <- iface_rules
  292. , isNothing (ifRuleOrph r)
  293. , if ifRuleAuto r then warn_auto_orphs
  294. else warn_orphs ]
  295. ; if errorsFound dflags errs_and_warns
  296. then return ( errs_and_warns, Nothing )
  297. else do {
  298. -- Debug printing
  299. ; dumpIfSet_dyn dflags Opt_D_dump_hi "FINAL INTERFACE"
  300. (pprModIface new_iface)
  301. -- bug #1617: on reload we weren't updating the PrintUnqualified
  302. -- correctly. This stems from the fact that the interface had
  303. -- not changed, so addFingerprints returns the old ModIface
  304. -- with the old GlobalRdrEnv (mi_globals).
  305. ; let final_iface = new_iface{ mi_globals = maybeGlobalRdrEnv rdr_env }
  306. ; return (errs_and_warns, Just (final_iface, no_change_at_all)) }}
  307. where
  308. cmp_rule = comparing ifRuleName
  309. -- Compare these lexicographically by OccName, *not* by unique,
  310. -- because the latter is not stable across compilations:
  311. cmp_inst = comparing (nameOccName . ifDFun)
  312. cmp_fam_inst = comparing (nameOccName . ifFamInstTcName)
  313. dflags = hsc_dflags hsc_env
  314. -- We only fill in mi_globals if the module was compiled to byte
  315. -- code. Otherwise, the compiler may not have retained all the
  316. -- top-level bindings and they won't be in the TypeEnv (see
  317. -- Desugar.addExportFlagsAndRules). The mi_globals field is used
  318. -- by GHCi to decide whether the module has its full top-level
  319. -- scope available. (#5534)
  320. maybeGlobalRdrEnv :: GlobalRdrEnv -> Maybe GlobalRdrEnv
  321. maybeGlobalRdrEnv rdr_env
  322. | targetRetainsAllBindings (hscTarget dflags) = Just rdr_env
  323. | otherwise = Nothing
  324. deliberatelyOmitted :: String -> a
  325. deliberatelyOmitted x = panic ("Deliberately omitted: " ++ x)
  326. ifFamInstTcName = ifFamInstFam
  327. flattenVectInfo (VectInfo { vectInfoVar = vVar
  328. , vectInfoTyCon = vTyCon
  329. , vectInfoParallelVars = vParallelVars
  330. , vectInfoParallelTyCons = vParallelTyCons
  331. }) =
  332. IfaceVectInfo
  333. { ifaceVectInfoVar = [Var.varName v | (v, _ ) <- varEnvElts vVar]
  334. , ifaceVectInfoTyCon = [tyConName t | (t, t_v) <- nameEnvElts vTyCon, t /= t_v]
  335. , ifaceVectInfoTyConReuse = [tyConName t | (t, t_v) <- nameEnvElts vTyCon, t == t_v]
  336. , ifaceVectInfoParallelVars = [Var.varName v | v <- varSetElems vParallelVars]
  337. , ifaceVectInfoParallelTyCons = nameSetToList vParallelTyCons
  338. }
  339. -----------------------------
  340. writeIfaceFile :: DynFlags -> FilePath -> ModIface -> IO ()
  341. writeIfaceFile dflags hi_file_path new_iface
  342. = do createDirectoryIfMissing True (takeDirectory hi_file_path)
  343. writeBinIface dflags hi_file_path new_iface
  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 _ -> throwGhcExceptionIO (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
  973. case read_result of
  974. Failed err -> do
  975. traceIf (text "FYI: cannot 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. | gopt 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.
  1007. Just

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