PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/iface/LoadIface.lhs

https://bitbucket.org/carter/ghc
Haskell | 832 lines | 549 code | 104 blank | 179 comment | 22 complexity | b435afbf03604de61b2c1f50e8c10b2e MD5 | raw file
  1. %
  2. % (c) The University of Glasgow 2006
  3. % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
  4. %
  5. Loading interface files
  6. \begin{code}
  7. {-# OPTIONS_GHC -fno-warn-orphans #-}
  8. module LoadIface (
  9. -- RnM/TcM functions
  10. loadModuleInterface, loadModuleInterfaces,
  11. loadSrcInterface, loadInterfaceForName,
  12. -- IfM functions
  13. loadInterface, loadWiredInHomeIface,
  14. loadSysInterface, loadUserInterface,
  15. findAndReadIface, readIface, -- Used when reading the module's old interface
  16. loadDecls, -- Should move to TcIface and be renamed
  17. initExternalPackageState,
  18. ifaceStats, pprModIface, showIface
  19. ) where
  20. #include "HsVersions.h"
  21. import {-# SOURCE #-} TcIface( tcIfaceDecl, tcIfaceRules, tcIfaceInst,
  22. tcIfaceFamInst, tcIfaceVectInfo, tcIfaceAnnotations )
  23. import DynFlags
  24. import IfaceSyn
  25. import IfaceEnv
  26. import HscTypes
  27. import BasicTypes hiding (SuccessFlag(..))
  28. import TcRnMonad
  29. import Constants
  30. import PrelNames
  31. import PrelInfo
  32. import MkId ( seqId )
  33. import Rules
  34. import Annotations
  35. import InstEnv
  36. import FamInstEnv
  37. import Name
  38. import NameEnv
  39. import Avail
  40. import Module
  41. import Maybes
  42. import ErrUtils
  43. import Finder
  44. import UniqFM
  45. import SrcLoc
  46. import Outputable
  47. import BinIface
  48. import Panic
  49. import Util
  50. import FastString
  51. import Fingerprint
  52. import Control.Monad
  53. \end{code}
  54. %************************************************************************
  55. %* *
  56. loadSrcInterface, loadOrphanModules, loadInterfaceForName
  57. These three are called from TcM-land
  58. %* *
  59. %************************************************************************
  60. \begin{code}
  61. -- | Load the interface corresponding to an @import@ directive in
  62. -- source code. On a failure, fail in the monad with an error message.
  63. loadSrcInterface :: SDoc
  64. -> ModuleName
  65. -> IsBootInterface -- {-# SOURCE #-} ?
  66. -> Maybe FastString -- "package", if any
  67. -> RnM ModIface
  68. loadSrcInterface doc mod want_boot maybe_pkg = do
  69. -- We must first find which Module this import refers to. This involves
  70. -- calling the Finder, which as a side effect will search the filesystem
  71. -- and create a ModLocation. If successful, loadIface will read the
  72. -- interface; it will call the Finder again, but the ModLocation will be
  73. -- cached from the first search.
  74. hsc_env <- getTopEnv
  75. res <- liftIO $ findImportedModule hsc_env mod maybe_pkg
  76. case res of
  77. Found _ mod -> do
  78. mb_iface <- initIfaceTcRn $ loadInterface doc mod (ImportByUser want_boot)
  79. case mb_iface of
  80. Failed err -> failWithTc err
  81. Succeeded iface -> return iface
  82. err ->
  83. let dflags = hsc_dflags hsc_env in
  84. failWithTc (cannotFindInterface dflags mod err)
  85. -- | Load interface for a module.
  86. loadModuleInterface :: SDoc -> Module -> TcM ModIface
  87. loadModuleInterface doc mod = initIfaceTcRn (loadSysInterface doc mod)
  88. -- | Load interfaces for a collection of modules.
  89. loadModuleInterfaces :: SDoc -> [Module] -> TcM ()
  90. loadModuleInterfaces doc mods
  91. | null mods = return ()
  92. | otherwise = initIfaceTcRn (mapM_ load mods)
  93. where
  94. load mod = loadSysInterface (doc <+> parens (ppr mod)) mod
  95. -- | Loads the interface for a given Name.
  96. loadInterfaceForName :: SDoc -> Name -> TcRn ModIface
  97. loadInterfaceForName doc name
  98. = do {
  99. when debugIsOn $ do
  100. -- Should not be called with a name from the module being compiled
  101. { this_mod <- getModule
  102. ; MASSERT2( not (nameIsLocalOrFrom this_mod name), ppr name <+> parens doc )
  103. }
  104. ; ASSERT2( isExternalName name, ppr name )
  105. initIfaceTcRn $ loadSysInterface doc (nameModule name)
  106. }
  107. \end{code}
  108. %*********************************************************
  109. %* *
  110. loadInterface
  111. The main function to load an interface
  112. for an imported module, and put it in
  113. the External Package State
  114. %* *
  115. %*********************************************************
  116. \begin{code}
  117. -- | An 'IfM' function to load the home interface for a wired-in thing,
  118. -- so that we're sure that we see its instance declarations and rules
  119. -- See Note [Loading instances for wired-in things] in TcIface
  120. loadWiredInHomeIface :: Name -> IfM lcl ()
  121. loadWiredInHomeIface name
  122. = ASSERT( isWiredInName name )
  123. do _ <- loadSysInterface doc (nameModule name); return ()
  124. where
  125. doc = ptext (sLit "Need home interface for wired-in thing") <+> ppr name
  126. ------------------
  127. -- | Loads a system interface and throws an exception if it fails
  128. loadSysInterface :: SDoc -> Module -> IfM lcl ModIface
  129. loadSysInterface doc mod_name = loadInterfaceWithException doc mod_name ImportBySystem
  130. ------------------
  131. -- | Loads a user interface and throws an exception if it fails. The first parameter indicates
  132. -- whether we should import the boot variant of the module
  133. loadUserInterface :: Bool -> SDoc -> Module -> IfM lcl ModIface
  134. loadUserInterface is_boot doc mod_name
  135. = loadInterfaceWithException doc mod_name (ImportByUser is_boot)
  136. ------------------
  137. -- | A wrapper for 'loadInterface' that throws an exception if it fails
  138. loadInterfaceWithException :: SDoc -> Module -> WhereFrom -> IfM lcl ModIface
  139. loadInterfaceWithException doc mod_name where_from
  140. = do { mb_iface <- loadInterface doc mod_name where_from
  141. ; dflags <- getDynFlags
  142. ; case mb_iface of
  143. Failed err -> ghcError (ProgramError (showSDoc dflags err))
  144. Succeeded iface -> return iface }
  145. ------------------
  146. loadInterface :: SDoc -> Module -> WhereFrom
  147. -> IfM lcl (MaybeErr MsgDoc ModIface)
  148. -- loadInterface looks in both the HPT and PIT for the required interface
  149. -- If not found, it loads it, and puts it in the PIT (always).
  150. -- If it can't find a suitable interface file, we
  151. -- a) modify the PackageIfaceTable to have an empty entry
  152. -- (to avoid repeated complaints)
  153. -- b) return (Left message)
  154. --
  155. -- It's not necessarily an error for there not to be an interface
  156. -- file -- perhaps the module has changed, and that interface
  157. -- is no longer used
  158. loadInterface doc_str mod from
  159. = do { -- Read the state
  160. (eps,hpt) <- getEpsAndHpt
  161. ; traceIf (text "Considering whether to load" <+> ppr mod <+> ppr from)
  162. -- Check whether we have the interface already
  163. ; dflags <- getDynFlags
  164. ; case lookupIfaceByModule dflags hpt (eps_PIT eps) mod of {
  165. Just iface
  166. -> return (Succeeded iface) ; -- Already loaded
  167. -- The (src_imp == mi_boot iface) test checks that the already-loaded
  168. -- interface isn't a boot iface. This can conceivably happen,
  169. -- if an earlier import had a before we got to real imports. I think.
  170. _ -> do {
  171. -- READ THE MODULE IN
  172. ; read_result <- case (wantHiBootFile dflags eps mod from) of
  173. Failed err -> return (Failed err)
  174. Succeeded hi_boot_file -> findAndReadIface doc_str mod hi_boot_file
  175. ; case read_result of {
  176. Failed err -> do
  177. { let fake_iface = emptyModIface mod
  178. ; updateEps_ $ \eps ->
  179. eps { eps_PIT = extendModuleEnv (eps_PIT eps) (mi_module fake_iface) fake_iface }
  180. -- Not found, so add an empty iface to
  181. -- the EPS map so that we don't look again
  182. ; return (Failed err) } ;
  183. -- Found and parsed!
  184. -- We used to have a sanity check here that looked for:
  185. -- * System importing ..
  186. -- * a home package module ..
  187. -- * that we know nothing about (mb_dep == Nothing)!
  188. --
  189. -- But this is no longer valid because thNameToGhcName allows users to
  190. -- cause the system to load arbitrary interfaces (by supplying an appropriate
  191. -- Template Haskell original-name).
  192. Succeeded (iface, file_path) ->
  193. let
  194. loc_doc = text file_path
  195. in
  196. initIfaceLcl mod loc_doc $ do
  197. -- Load the new ModIface into the External Package State
  198. -- Even home-package interfaces loaded by loadInterface
  199. -- (which only happens in OneShot mode; in Batch/Interactive
  200. -- mode, home-package modules are loaded one by one into the HPT)
  201. -- are put in the EPS.
  202. --
  203. -- The main thing is to add the ModIface to the PIT, but
  204. -- we also take the
  205. -- IfaceDecls, IfaceClsInst, IfaceFamInst, IfaceRules, IfaceVectInfo
  206. -- out of the ModIface and put them into the big EPS pools
  207. -- NB: *first* we do loadDecl, so that the provenance of all the locally-defined
  208. --- names is done correctly (notably, whether this is an .hi file or .hi-boot file).
  209. -- If we do loadExport first the wrong info gets into the cache (unless we
  210. -- explicitly tag each export which seems a bit of a bore)
  211. ; ignore_prags <- doptM Opt_IgnoreInterfacePragmas
  212. ; new_eps_decls <- loadDecls ignore_prags (mi_decls iface)
  213. ; new_eps_insts <- mapM tcIfaceInst (mi_insts iface)
  214. ; new_eps_fam_insts <- mapM tcIfaceFamInst (mi_fam_insts iface)
  215. ; new_eps_rules <- tcIfaceRules ignore_prags (mi_rules iface)
  216. ; new_eps_anns <- tcIfaceAnnotations (mi_anns iface)
  217. ; new_eps_vect_info <- tcIfaceVectInfo mod (mkNameEnv new_eps_decls) (mi_vect_info iface)
  218. ; let { final_iface = iface {
  219. mi_decls = panic "No mi_decls in PIT",
  220. mi_insts = panic "No mi_insts in PIT",
  221. mi_fam_insts = panic "No mi_fam_insts in PIT",
  222. mi_rules = panic "No mi_rules in PIT",
  223. mi_anns = panic "No mi_anns in PIT"
  224. }
  225. }
  226. ; updateEps_ $ \ eps ->
  227. if elemModuleEnv mod (eps_PIT eps) then eps else
  228. eps {
  229. eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface,
  230. eps_PTE = addDeclsToPTE (eps_PTE eps) new_eps_decls,
  231. eps_rule_base = extendRuleBaseList (eps_rule_base eps)
  232. new_eps_rules,
  233. eps_inst_env = extendInstEnvList (eps_inst_env eps)
  234. new_eps_insts,
  235. eps_fam_inst_env = extendFamInstEnvList (eps_fam_inst_env eps)
  236. new_eps_fam_insts,
  237. eps_vect_info = plusVectInfo (eps_vect_info eps)
  238. new_eps_vect_info,
  239. eps_ann_env = extendAnnEnvList (eps_ann_env eps)
  240. new_eps_anns,
  241. eps_mod_fam_inst_env
  242. = let
  243. fam_inst_env =
  244. extendFamInstEnvList emptyFamInstEnv
  245. new_eps_fam_insts
  246. in
  247. extendModuleEnv (eps_mod_fam_inst_env eps)
  248. mod
  249. fam_inst_env,
  250. eps_stats = addEpsInStats (eps_stats eps)
  251. (length new_eps_decls)
  252. (length new_eps_insts)
  253. (length new_eps_rules) }
  254. ; return (Succeeded final_iface)
  255. }}}}
  256. wantHiBootFile :: DynFlags -> ExternalPackageState -> Module -> WhereFrom
  257. -> MaybeErr MsgDoc IsBootInterface
  258. -- Figure out whether we want Foo.hi or Foo.hi-boot
  259. wantHiBootFile dflags eps mod from
  260. = case from of
  261. ImportByUser usr_boot
  262. | usr_boot && not this_package
  263. -> Failed (badSourceImport mod)
  264. | otherwise -> Succeeded usr_boot
  265. ImportBySystem
  266. | not this_package -- If the module to be imported is not from this package
  267. -> Succeeded False -- don't look it up in eps_is_boot, because that is keyed
  268. -- on the ModuleName of *home-package* modules only.
  269. -- We never import boot modules from other packages!
  270. | otherwise
  271. -> case lookupUFM (eps_is_boot eps) (moduleName mod) of
  272. Just (_, is_boot) -> Succeeded is_boot
  273. Nothing -> Succeeded False
  274. -- The boot-ness of the requested interface,
  275. -- based on the dependencies in directly-imported modules
  276. where
  277. this_package = thisPackage dflags == modulePackageId mod
  278. badSourceImport :: Module -> SDoc
  279. badSourceImport mod
  280. = hang (ptext (sLit "You cannot {-# SOURCE #-} import a module from another package"))
  281. 2 (ptext (sLit "but") <+> quotes (ppr mod) <+> ptext (sLit "is from package")
  282. <+> quotes (ppr (modulePackageId mod)))
  283. \end{code}
  284. {-
  285. Used to be used for the loadInterface sanity check on system imports. That has been removed, but I'm leaving this in pending
  286. review of this decision by SPJ - MCB 10/2008
  287. badDepMsg :: Module -> SDoc
  288. badDepMsg mod
  289. = hang (ptext (sLit "Interface file inconsistency:"))
  290. 2 (sep [ptext (sLit "home-package module") <+> quotes (ppr mod) <+> ptext (sLit "is needed,"),
  291. ptext (sLit "but is not listed in the dependencies of the interfaces directly imported by the module being compiled")])
  292. -}
  293. \begin{code}
  294. -----------------------------------------------------
  295. -- Loading type/class/value decls
  296. -- We pass the full Module name here, replete with
  297. -- its package info, so that we can build a Name for
  298. -- each binder with the right package info in it
  299. -- All subsequent lookups, including crucially lookups during typechecking
  300. -- the declaration itself, will find the fully-glorious Name
  301. --
  302. -- We handle ATs specially. They are not main declarations, but also not
  303. -- implict things (in particular, adding them to `implicitTyThings' would mess
  304. -- things up in the renaming/type checking of source programs).
  305. -----------------------------------------------------
  306. addDeclsToPTE :: PackageTypeEnv -> [(Name,TyThing)] -> PackageTypeEnv
  307. addDeclsToPTE pte things = extendNameEnvList pte things
  308. loadDecls :: Bool
  309. -> [(Fingerprint, IfaceDecl)]
  310. -> IfL [(Name,TyThing)]
  311. loadDecls ignore_prags ver_decls
  312. = do { mod <- getIfModule
  313. ; thingss <- mapM (loadDecl ignore_prags mod) ver_decls
  314. ; return (concat thingss)
  315. }
  316. loadDecl :: Bool -- Don't load pragmas into the decl pool
  317. -> Module
  318. -> (Fingerprint, IfaceDecl)
  319. -> IfL [(Name,TyThing)] -- The list can be poked eagerly, but the
  320. -- TyThings are forkM'd thunks
  321. loadDecl ignore_prags mod (_version, decl)
  322. = do { -- Populate the name cache with final versions of all
  323. -- the names associated with the decl
  324. main_name <- lookupOrig mod (ifName decl)
  325. -- ; traceIf (text "Loading decl for " <> ppr main_name)
  326. -- Typecheck the thing, lazily
  327. -- NB. Firstly, the laziness is there in case we never need the
  328. -- declaration (in one-shot mode), and secondly it is there so that
  329. -- we don't look up the occurrence of a name before calling mk_new_bndr
  330. -- on the binder. This is important because we must get the right name
  331. -- which includes its nameParent.
  332. ; thing <- forkM doc $ do { bumpDeclStats main_name
  333. ; tcIfaceDecl ignore_prags decl }
  334. -- Populate the type environment with the implicitTyThings too.
  335. --
  336. -- Note [Tricky iface loop]
  337. -- ~~~~~~~~~~~~~~~~~~~~~~~~
  338. -- Summary: The delicate point here is that 'mini-env' must be
  339. -- buildable from 'thing' without demanding any of the things
  340. -- 'forkM'd by tcIfaceDecl.
  341. --
  342. -- In more detail: Consider the example
  343. -- data T a = MkT { x :: T a }
  344. -- The implicitTyThings of T are: [ <datacon MkT>, <selector x>]
  345. -- (plus their workers, wrappers, coercions etc etc)
  346. --
  347. -- We want to return an environment
  348. -- [ "MkT" -> <datacon MkT>, "x" -> <selector x>, ... ]
  349. -- (where the "MkT" is the *Name* associated with MkT, etc.)
  350. --
  351. -- We do this by mapping the implict_names to the associated
  352. -- TyThings. By the invariant on ifaceDeclImplicitBndrs and
  353. -- implicitTyThings, we can use getOccName on the implicit
  354. -- TyThings to make this association: each Name's OccName should
  355. -- be the OccName of exactly one implictTyThing. So the key is
  356. -- to define a "mini-env"
  357. --
  358. -- [ 'MkT' -> <datacon MkT>, 'x' -> <selector x>, ... ]
  359. -- where the 'MkT' here is the *OccName* associated with MkT.
  360. --
  361. -- However, there is a subtlety: due to how type checking needs
  362. -- to be staged, we can't poke on the forkM'd thunks inside the
  363. -- implictTyThings while building this mini-env.
  364. -- If we poke these thunks too early, two problems could happen:
  365. -- (1) When processing mutually recursive modules across
  366. -- hs-boot boundaries, poking too early will do the
  367. -- type-checking before the recursive knot has been tied,
  368. -- so things will be type-checked in the wrong
  369. -- environment, and necessary variables won't be in
  370. -- scope.
  371. --
  372. -- (2) Looking up one OccName in the mini_env will cause
  373. -- others to be looked up, which might cause that
  374. -- original one to be looked up again, and hence loop.
  375. --
  376. -- The code below works because of the following invariant:
  377. -- getOccName on a TyThing does not force the suspended type
  378. -- checks in order to extract the name. For example, we don't
  379. -- poke on the "T a" type of <selector x> on the way to
  380. -- extracting <selector x>'s OccName. Of course, there is no
  381. -- reason in principle why getting the OccName should force the
  382. -- thunks, but this means we need to be careful in
  383. -- implicitTyThings and its helper functions.
  384. --
  385. -- All a bit too finely-balanced for my liking.
  386. -- This mini-env and lookup function mediates between the
  387. --'Name's n and the map from 'OccName's to the implicit TyThings
  388. ; let mini_env = mkOccEnv [(getOccName t, t) | t <- implicitTyThings thing]
  389. lookup n = case lookupOccEnv mini_env (getOccName n) of
  390. Just thing -> thing
  391. Nothing ->
  392. pprPanic "loadDecl" (ppr main_name <+> ppr n $$ ppr (decl))
  393. ; implicit_names <- mapM (lookupOrig mod) (ifaceDeclImplicitBndrs decl)
  394. ; return $ (main_name, thing) :
  395. -- uses the invariant that implicit_names and
  396. -- implictTyThings are bijective
  397. [(n, lookup n) | n <- implicit_names]
  398. }
  399. where
  400. doc = ptext (sLit "Declaration for") <+> ppr (ifName decl)
  401. bumpDeclStats :: Name -> IfL () -- Record that one more declaration has actually been used
  402. bumpDeclStats name
  403. = do { traceIf (text "Loading decl for" <+> ppr name)
  404. ; updateEps_ (\eps -> let stats = eps_stats eps
  405. in eps { eps_stats = stats { n_decls_out = n_decls_out stats + 1 } })
  406. }
  407. \end{code}
  408. %*********************************************************
  409. %* *
  410. \subsection{Reading an interface file}
  411. %* *
  412. %*********************************************************
  413. \begin{code}
  414. findAndReadIface :: SDoc -> Module
  415. -> IsBootInterface -- True <=> Look for a .hi-boot file
  416. -- False <=> Look for .hi file
  417. -> TcRnIf gbl lcl (MaybeErr MsgDoc (ModIface, FilePath))
  418. -- Nothing <=> file not found, or unreadable, or illegible
  419. -- Just x <=> successfully found and parsed
  420. -- It *doesn't* add an error to the monad, because
  421. -- sometimes it's ok to fail... see notes with loadInterface
  422. findAndReadIface doc_str mod hi_boot_file
  423. = do { traceIf (sep [hsep [ptext (sLit "Reading"),
  424. if hi_boot_file
  425. then ptext (sLit "[boot]")
  426. else empty,
  427. ptext (sLit "interface for"),
  428. ppr mod <> semi],
  429. nest 4 (ptext (sLit "reason:") <+> doc_str)])
  430. -- Check for GHC.Prim, and return its static interface
  431. ; dflags <- getDynFlags
  432. ; if mod == gHC_PRIM
  433. then return (Succeeded (ghcPrimIface,
  434. "<built in interface for GHC.Prim>"))
  435. else do
  436. -- Look for the file
  437. ; hsc_env <- getTopEnv
  438. ; mb_found <- liftIO (findExactModule hsc_env mod)
  439. ; case mb_found of {
  440. Found loc mod -> do
  441. -- Found file, so read it
  442. { let { file_path = addBootSuffix_maybe hi_boot_file (ml_hi_file loc) }
  443. -- If the interface is in the current package then if we could
  444. -- load it would already be in the HPT and we assume that our
  445. -- callers checked that.
  446. ; if thisPackage dflags == modulePackageId mod
  447. && not (isOneShot (ghcMode dflags))
  448. then return (Failed (homeModError mod loc))
  449. else do {
  450. ; traceIf (ptext (sLit "readIFace") <+> text file_path)
  451. ; read_result <- readIface mod file_path hi_boot_file
  452. ; case read_result of
  453. Failed err -> return (Failed (badIfaceFile file_path err))
  454. Succeeded iface
  455. | mi_module iface /= mod ->
  456. return (Failed (wrongIfaceModErr iface mod file_path))
  457. | otherwise ->
  458. return (Succeeded (iface, file_path))
  459. -- Don't forget to fill in the package name...
  460. }}
  461. ; err -> do
  462. { traceIf (ptext (sLit "...not found"))
  463. ; dflags <- getDynFlags
  464. ; return (Failed (cannotFindInterface dflags
  465. (moduleName mod) err)) }
  466. }
  467. }
  468. \end{code}
  469. @readIface@ tries just the one file.
  470. \begin{code}
  471. readIface :: Module -> FilePath -> IsBootInterface
  472. -> TcRnIf gbl lcl (MaybeErr MsgDoc ModIface)
  473. -- Failed err <=> file not found, or unreadable, or illegible
  474. -- Succeeded iface <=> successfully found and parsed
  475. readIface wanted_mod file_path _
  476. = do { res <- tryMostM $
  477. readBinIface CheckHiWay QuietBinIFaceReading file_path
  478. ; case res of
  479. Right iface
  480. | wanted_mod == actual_mod -> return (Succeeded iface)
  481. | otherwise -> return (Failed err)
  482. where
  483. actual_mod = mi_module iface
  484. err = hiModuleNameMismatchWarn wanted_mod actual_mod
  485. Left exn -> return (Failed (text (showException exn)))
  486. }
  487. \end{code}
  488. %*********************************************************
  489. %* *
  490. Wired-in interface for GHC.Prim
  491. %* *
  492. %*********************************************************
  493. \begin{code}
  494. initExternalPackageState :: ExternalPackageState
  495. initExternalPackageState
  496. = EPS {
  497. eps_is_boot = emptyUFM,
  498. eps_PIT = emptyPackageIfaceTable,
  499. eps_PTE = emptyTypeEnv,
  500. eps_inst_env = emptyInstEnv,
  501. eps_fam_inst_env = emptyFamInstEnv,
  502. eps_rule_base = mkRuleBase builtinRules,
  503. -- Initialise the EPS rule pool with the built-in rules
  504. eps_mod_fam_inst_env
  505. = emptyModuleEnv,
  506. eps_vect_info = noVectInfo,
  507. eps_ann_env = emptyAnnEnv,
  508. eps_stats = EpsStats { n_ifaces_in = 0, n_decls_in = 0, n_decls_out = 0
  509. , n_insts_in = 0, n_insts_out = 0
  510. , n_rules_in = length builtinRules, n_rules_out = 0 }
  511. }
  512. \end{code}
  513. %*********************************************************
  514. %* *
  515. Wired-in interface for GHC.Prim
  516. %* *
  517. %*********************************************************
  518. \begin{code}
  519. ghcPrimIface :: ModIface
  520. ghcPrimIface
  521. = (emptyModIface gHC_PRIM) {
  522. mi_exports = ghcPrimExports,
  523. mi_decls = [],
  524. mi_fixities = fixities,
  525. mi_fix_fn = mkIfaceFixCache fixities
  526. }
  527. where
  528. fixities = [(getOccName seqId, Fixity 0 InfixR)]
  529. -- seq is infixr 0
  530. \end{code}
  531. %*********************************************************
  532. %* *
  533. \subsection{Statistics}
  534. %* *
  535. %*********************************************************
  536. \begin{code}
  537. ifaceStats :: ExternalPackageState -> SDoc
  538. ifaceStats eps
  539. = hcat [text "Renamer stats: ", msg]
  540. where
  541. stats = eps_stats eps
  542. msg = vcat
  543. [int (n_ifaces_in stats) <+> text "interfaces read",
  544. hsep [ int (n_decls_out stats), text "type/class/variable imported, out of",
  545. int (n_decls_in stats), text "read"],
  546. hsep [ int (n_insts_out stats), text "instance decls imported, out of",
  547. int (n_insts_in stats), text "read"],
  548. hsep [ int (n_rules_out stats), text "rule decls imported, out of",
  549. int (n_rules_in stats), text "read"]
  550. ]
  551. \end{code}
  552. %************************************************************************
  553. %* *
  554. Printing interfaces
  555. %* *
  556. %************************************************************************
  557. \begin{code}
  558. -- | Read binary interface, and print it out
  559. showIface :: HscEnv -> FilePath -> IO ()
  560. showIface hsc_env filename = do
  561. -- skip the hi way check; we don't want to worry about profiled vs.
  562. -- non-profiled interfaces, for example.
  563. iface <- initTcRnIf 's' hsc_env () () $
  564. readBinIface IgnoreHiWay TraceBinIFaceReading filename
  565. let dflags = hsc_dflags hsc_env
  566. log_action dflags dflags SevDump noSrcSpan defaultDumpStyle (pprModIface iface)
  567. \end{code}
  568. \begin{code}
  569. pprModIface :: ModIface -> SDoc
  570. -- Show a ModIface
  571. pprModIface iface
  572. = vcat [ ptext (sLit "interface")
  573. <+> ppr (mi_module iface) <+> pp_boot
  574. <+> (if mi_orphan iface then ptext (sLit "[orphan module]") else empty)
  575. <+> (if mi_finsts iface then ptext (sLit "[family instance module]") else empty)
  576. <+> (if mi_hpc iface then ptext (sLit "[hpc]") else empty)
  577. <+> integer hiVersion
  578. , nest 2 (text "interface hash:" <+> ppr (mi_iface_hash iface))
  579. , nest 2 (text "ABI hash:" <+> ppr (mi_mod_hash iface))
  580. , nest 2 (text "export-list hash:" <+> ppr (mi_exp_hash iface))
  581. , nest 2 (text "orphan hash:" <+> ppr (mi_orphan_hash iface))
  582. , nest 2 (text "flag hash:" <+> ppr (mi_flag_hash iface))
  583. , nest 2 (text "used TH splices:" <+> ppr (mi_used_th iface))
  584. , nest 2 (ptext (sLit "where"))
  585. , ptext (sLit "exports:")
  586. , nest 2 (vcat (map pprExport (mi_exports iface)))
  587. , pprDeps (mi_deps iface)
  588. , vcat (map pprUsage (mi_usages iface))
  589. , vcat (map pprIfaceAnnotation (mi_anns iface))
  590. , pprFixities (mi_fixities iface)
  591. , vcat (map pprIfaceDecl (mi_decls iface))
  592. , vcat (map ppr (mi_insts iface))
  593. , vcat (map ppr (mi_fam_insts iface))
  594. , vcat (map ppr (mi_rules iface))
  595. , pprVectInfo (mi_vect_info iface)
  596. , ppr (mi_warns iface)
  597. , pprTrustInfo (mi_trust iface)
  598. , pprTrustPkg (mi_trust_pkg iface)
  599. ]
  600. where
  601. pp_boot | mi_boot iface = ptext (sLit "[boot]")
  602. | otherwise = empty
  603. \end{code}
  604. When printing export lists, we print like this:
  605. Avail f f
  606. AvailTC C [C, x, y] C(x,y)
  607. AvailTC C [x, y] C!(x,y) -- Exporting x, y but not C
  608. \begin{code}
  609. pprExport :: IfaceExport -> SDoc
  610. pprExport (Avail n) = ppr n
  611. pprExport (AvailTC _ []) = empty
  612. pprExport (AvailTC n (n':ns))
  613. | n==n' = ppr n <> pp_export ns
  614. | otherwise = ppr n <> char '|' <> pp_export (n':ns)
  615. where
  616. pp_export [] = empty
  617. pp_export names = braces (hsep (map ppr names))
  618. pprUsage :: Usage -> SDoc
  619. pprUsage usage@UsagePackageModule{}
  620. = pprUsageImport usage usg_mod
  621. pprUsage usage@UsageHomeModule{}
  622. = pprUsageImport usage usg_mod_name $$
  623. nest 2 (
  624. maybe empty (\v -> text "exports: " <> ppr v) (usg_exports usage) $$
  625. vcat [ ppr n <+> ppr v | (n,v) <- usg_entities usage ]
  626. )
  627. pprUsage usage@UsageFile{}
  628. = hsep [ptext (sLit "addDependentFile"),
  629. doubleQuotes (text (usg_file_path usage))]
  630. pprUsageImport :: Outputable a => Usage -> (Usage -> a) -> SDoc
  631. pprUsageImport usage usg_mod'
  632. = hsep [ptext (sLit "import"), safe, ppr (usg_mod' usage),
  633. ppr (usg_mod_hash usage)]
  634. where
  635. safe | usg_safe usage = ptext $ sLit "safe"
  636. | otherwise = ptext $ sLit " -/ "
  637. pprDeps :: Dependencies -> SDoc
  638. pprDeps (Deps { dep_mods = mods, dep_pkgs = pkgs, dep_orphs = orphs,
  639. dep_finsts = finsts })
  640. = vcat [ptext (sLit "module dependencies:") <+> fsep (map ppr_mod mods),
  641. ptext (sLit "package dependencies:") <+> fsep (map ppr_pkg pkgs),
  642. ptext (sLit "orphans:") <+> fsep (map ppr orphs),
  643. ptext (sLit "family instance modules:") <+> fsep (map ppr finsts)
  644. ]
  645. where
  646. ppr_mod (mod_name, boot) = ppr mod_name <+> ppr_boot boot
  647. ppr_pkg (pkg,trust_req) = ppr pkg <>
  648. (if trust_req then text "*" else empty)
  649. ppr_boot True = text "[boot]"
  650. ppr_boot False = empty
  651. pprIfaceDecl :: (Fingerprint, IfaceDecl) -> SDoc
  652. pprIfaceDecl (ver, decl)
  653. = ppr ver $$ nest 2 (ppr decl)
  654. pprFixities :: [(OccName, Fixity)] -> SDoc
  655. pprFixities [] = empty
  656. pprFixities fixes = ptext (sLit "fixities") <+> pprWithCommas pprFix fixes
  657. where
  658. pprFix (occ,fix) = ppr fix <+> ppr occ
  659. pprVectInfo :: IfaceVectInfo -> SDoc
  660. pprVectInfo (IfaceVectInfo { ifaceVectInfoVar = vars
  661. , ifaceVectInfoTyCon = tycons
  662. , ifaceVectInfoTyConReuse = tyconsReuse
  663. , ifaceVectInfoScalarVars = scalarVars
  664. , ifaceVectInfoScalarTyCons = scalarTyCons
  665. }) =
  666. vcat
  667. [ ptext (sLit "vectorised variables:") <+> hsep (map ppr vars)
  668. , ptext (sLit "vectorised tycons:") <+> hsep (map ppr tycons)
  669. , ptext (sLit "vectorised reused tycons:") <+> hsep (map ppr tyconsReuse)
  670. , ptext (sLit "scalar variables:") <+> hsep (map ppr scalarVars)
  671. , ptext (sLit "scalar tycons:") <+> hsep (map ppr scalarTyCons)
  672. ]
  673. pprTrustInfo :: IfaceTrustInfo -> SDoc
  674. pprTrustInfo trust = ptext (sLit "trusted:") <+> ppr trust
  675. pprTrustPkg :: Bool -> SDoc
  676. pprTrustPkg tpkg = ptext (sLit "require own pkg trusted:") <+> ppr tpkg
  677. instance Outputable Warnings where
  678. ppr = pprWarns
  679. pprWarns :: Warnings -> SDoc
  680. pprWarns NoWarnings = empty
  681. pprWarns (WarnAll txt) = ptext (sLit "Warn all") <+> ppr txt
  682. pprWarns (WarnSome prs) = ptext (sLit "Warnings")
  683. <+> vcat (map pprWarning prs)
  684. where pprWarning (name, txt) = ppr name <+> ppr txt
  685. pprIfaceAnnotation :: IfaceAnnotation -> SDoc
  686. pprIfaceAnnotation (IfaceAnnotation { ifAnnotatedTarget = target, ifAnnotatedValue = serialized })
  687. = ppr target <+> ptext (sLit "annotated by") <+> ppr serialized
  688. \end{code}
  689. %*********************************************************
  690. %* *
  691. \subsection{Errors}
  692. %* *
  693. %*********************************************************
  694. \begin{code}
  695. badIfaceFile :: String -> SDoc -> SDoc
  696. badIfaceFile file err
  697. = vcat [ptext (sLit "Bad interface file:") <+> text file,
  698. nest 4 err]
  699. hiModuleNameMismatchWarn :: Module -> Module -> MsgDoc
  700. hiModuleNameMismatchWarn requested_mod read_mod =
  701. withPprStyle defaultUserStyle $
  702. -- we want the Modules below to be qualified with package names,
  703. -- so reset the PrintUnqualified setting.
  704. hsep [ ptext (sLit "Something is amiss; requested module ")
  705. , ppr requested_mod
  706. , ptext (sLit "differs from name found in the interface file")
  707. , ppr read_mod
  708. ]
  709. wrongIfaceModErr :: ModIface -> Module -> String -> SDoc
  710. wrongIfaceModErr iface mod_name file_path
  711. = sep [ptext (sLit "Interface file") <+> iface_file,
  712. ptext (sLit "contains module") <+> quotes (ppr (mi_module iface)) <> comma,
  713. ptext (sLit "but we were expecting module") <+> quotes (ppr mod_name),
  714. sep [ptext (sLit "Probable cause: the source code which generated"),
  715. nest 2 iface_file,
  716. ptext (sLit "has an incompatible module name")
  717. ]
  718. ]
  719. where iface_file = doubleQuotes (text file_path)
  720. homeModError :: Module -> ModLocation -> SDoc
  721. homeModError mod location
  722. = ptext (sLit "attempting to use module ") <> quotes (ppr mod)
  723. <> (case ml_hs_file location of
  724. Just file -> space <> parens (text file)
  725. Nothing -> empty)
  726. <+> ptext (sLit "which is not loaded")
  727. \end{code}