PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ghc-7.0.4/compiler/iface/LoadIface.lhs

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