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