PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/ghc-7.4.1/compiler/iface/MkIface.lhs

#
Haskell | 1768 lines | 1191 code | 235 blank | 342 comment | 57 complexity | d89c6f60a76f4cd2d54d854a8bbf89ff MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, BSD-2-Clause

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

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