[project @ 2002-02-11 08:20:38 by chak]
[ghc-hetmet.git] / ghc / compiler / rename / RnHiFiles.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Dealing with interface files}
5
6 \begin{code}
7 module RnHiFiles (
8         readIface, findAndReadIface, loadInterface, loadHomeInterface, 
9         tryLoadInterface, loadOrphanModules,
10         loadExports, loadFixDecls, loadDeprecs,
11
12         lookupFixityRn, 
13
14         getTyClDeclBinders
15    ) where
16
17 #include "HsVersions.h"
18
19 import DriverState      ( v_GhcMode, isCompManagerMode )
20 import DriverUtil       ( splitFilename )
21 import CmdLineOpts      ( opt_IgnoreIfacePragmas )
22 import HscTypes         ( ModuleLocation(..),
23                           ModIface(..), emptyModIface,
24                           VersionInfo(..), ImportedModuleInfo,
25                           lookupIfaceByModName, 
26                           ImportVersion, WhetherHasOrphans, IsBootInterface,
27                           DeclsMap, GatedDecl, IfaceInsts, IfaceRules,
28                           AvailInfo, GenAvailInfo(..), Avails, Deprecations(..)
29                          )
30 import HsSyn            ( TyClDecl(..), InstDecl(..),
31                           FixitySig(..), RuleDecl(..),
32                           tyClDeclNames, tyClDeclSysNames, hsTyVarNames, getHsInstHead,
33                         )
34 import RdrHsSyn         ( RdrNameTyClDecl, RdrNameInstDecl, RdrNameRuleDecl )
35 import RnHsSyn          ( extractHsTyNames_s )
36 import BasicTypes       ( Version, defaultFixity )
37 import RnTypes          ( rnHsType )
38 import RnEnv
39 import RnMonad
40 import ParseIface       ( parseIface )
41
42 import Name             ( Name {-instance NamedThing-}, 
43                           nameModule, isLocalName, nameIsLocalOrFrom
44                          )
45 import NameEnv
46 import NameSet
47 import Module
48 import RdrName          ( rdrNameOcc )
49 import SrcLoc           ( mkSrcLoc )
50 import Maybes           ( maybeToBool, orElse )
51 import StringBuffer     ( hGetStringBuffer )
52 import FastString       ( mkFastString )
53 import ErrUtils         ( Message )
54 import Finder           ( findModule, findPackageModule )
55 import Lex
56 import FiniteMap
57 import ListSetOps       ( minusList )
58 import Outputable
59 import Bag
60 import Config
61
62 import IOExts
63 import Directory
64 \end{code}
65
66
67 %*********************************************************
68 %*                                                      *
69 \subsection{Loading a new interface file}
70 %*                                                      *
71 %*********************************************************
72
73 \begin{code}
74 loadHomeInterface :: SDoc -> Name -> RnM d ModIface
75 loadHomeInterface doc_str name
76   = ASSERT2( not (isLocalName name), ppr name <+> parens doc_str )
77     loadInterface doc_str (moduleName (nameModule name)) ImportBySystem
78
79 loadOrphanModules :: [ModuleName] -> RnM d ()
80 loadOrphanModules mods
81   | null mods = returnRn ()
82   | otherwise = traceRn (text "Loading orphan modules:" <+> 
83                          fsep (map ppr mods))                   `thenRn_` 
84                 mapRn_ load mods                                `thenRn_`
85                 returnRn ()
86   where
87     load mod   = loadInterface (mk_doc mod) mod ImportBySystem
88     mk_doc mod = ppr mod <+> ptext SLIT("is a orphan-instance module")
89
90 loadInterface :: SDoc -> ModuleName -> WhereFrom -> RnM d ModIface
91 loadInterface doc mod from 
92   = tryLoadInterface doc mod from       `thenRn` \ (ifaces, maybe_err) ->
93     case maybe_err of
94         Nothing  -> returnRn ifaces
95         Just err -> failWithRn ifaces (elaborate err)
96   where
97     elaborate err = hang (ptext SLIT("failed to load interface for") <+> quotes (ppr mod) <> colon)
98                          4 err
99
100 tryLoadInterface :: SDoc -> ModuleName -> WhereFrom -> RnM d (ModIface, Maybe Message)
101   -- Returns (Just err) if an error happened
102   -- It *doesn't* add an error to the monad, because sometimes it's ok to fail...
103   -- Specifically, when we read the usage information from an interface file,
104   -- we try to read the interfaces it mentions.  But it's OK to fail; perhaps
105   -- the module has changed, and that interface is no longer used.
106   
107   -- tryLoadInterface guarantees to return with iImpModInfo m --> (..., True)
108   -- (If the load fails, we plug in a vanilla placeholder)
109 tryLoadInterface doc_str mod_name from
110  = getHomeIfaceTableRn          `thenRn` \ hit ->
111    getModuleRn                  `thenRn` \ this_mod ->
112    getIfacesRn                  `thenRn` \ ifaces@(Ifaces { iPIT = pit }) ->
113
114         -- CHECK WHETHER WE HAVE IT ALREADY
115    case lookupIfaceByModName hit pit mod_name of {
116         Just iface |  case from of
117                         ImportByUser       -> not (mi_boot iface)
118                         ImportByUserSource -> mi_boot iface
119                         ImportBySystem     -> True
120                    -> returnRn (iface, Nothing) ;       -- Already loaded
121                         -- The not (mi_boot iface) test checks that the already-loaded
122                         -- interface isn't a boot iface.  This can conceivably happen,
123                         -- if the version checking happened to load a boot interface
124                         -- before we got to real imports.  
125         other       -> 
126
127    let
128         mod_map  = iImpModInfo ifaces
129         mod_info = lookupFM mod_map mod_name
130
131         hi_boot_file 
132           = case (from, mod_info) of
133                 (ImportByUser,       _)             -> False    -- Not hi-boot
134                 (ImportByUserSource, _)             -> True     -- hi-boot
135                 (ImportBySystem, Just (_, is_boot)) -> is_boot
136                 (ImportBySystem, Nothing)           -> False
137                         -- We're importing a module we know absolutely
138                         -- nothing about, so we assume it's from
139                         -- another package, where we aren't doing 
140                         -- dependency tracking. So it won't be a hi-boot file.
141
142         redundant_source_import 
143           = case (from, mod_info) of 
144                 (ImportByUserSource, Just (_,False)) -> True
145                 other                                -> False
146    in
147
148         -- Issue a warning for a redundant {- SOURCE -} import
149         -- NB that we arrange to read all the ordinary imports before 
150         -- any of the {- SOURCE -} imports
151    warnCheckRn  (not redundant_source_import)
152                 (warnRedundantSourceImport mod_name)    `thenRn_`
153
154         -- Check that we aren't importing ourselves. 
155         -- That only happens in Rename.checkOldIface, 
156         -- which doesn't call tryLoadInterface
157    warnCheckRn  
158         (not (isHomeModule this_mod) || moduleName this_mod /= mod_name)
159         (warnSelfImport this_mod)               `thenRn_`
160
161         -- READ THE MODULE IN
162    findAndReadIface doc_str mod_name hi_boot_file
163                                             `thenRn` \ read_result ->
164    case read_result of {
165         Left err ->     -- Not found, so add an empty export env to the Ifaces map
166                         -- so that we don't look again
167            let
168                 fake_mod    = mkVanillaModule mod_name
169                 fake_iface  = emptyModIface fake_mod
170                 new_ifaces  = ifaces { iPIT = extendModuleEnv pit fake_mod fake_iface }
171            in
172            setIfacesRn new_ifaces               `thenRn_`
173            returnRn (fake_iface, Just err) ;
174
175         -- Found and parsed!
176         Right (mod, iface) ->
177
178         -- LOAD IT INTO Ifaces
179
180         -- NB: *first* we do loadDecl, so that the provenance of all the locally-defined
181         ---    names is done correctly (notably, whether this is an .hi file or .hi-boot file).
182         --     If we do loadExport first the wrong info gets into the cache (unless we
183         --      explicitly tag each export which seems a bit of a bore)
184
185
186         -- Sanity check.  If we're system-importing a module we know nothing at all
187         -- about, it should be from a different package to this one
188     WARN( not (maybeToBool mod_info) && 
189           case from of { ImportBySystem -> True; other -> False } &&
190           isHomeModule mod,
191           ppr mod )
192
193     loadDecls mod               (iDecls ifaces)   (pi_decls iface)      `thenRn` \ (decls_vers, new_decls) ->
194     loadRules mod               (iRules ifaces)   (pi_rules iface)      `thenRn` \ (rule_vers, new_rules) ->
195     loadInstDecls mod           (iInsts ifaces)   (pi_insts iface)      `thenRn` \ new_insts ->
196     loadExports                                   (pi_exports iface)    `thenRn` \ (export_vers, avails) ->
197     loadFixDecls mod                              (pi_fixity iface)     `thenRn` \ fix_env ->
198     loadDeprecs mod                               (pi_deprecs iface)    `thenRn` \ deprec_env ->
199     let
200         version = VersionInfo { vers_module  = pi_vers iface, 
201                                 vers_exports = export_vers,
202                                 vers_rules = rule_vers,
203                                 vers_decls = decls_vers }
204
205         -- For an explicit user import, add to mod_map info about
206         -- the things the imported module depends on, extracted
207         -- from its usage info; and delete the module itself, which is now in the PIT
208         mod_map1 = case from of
209                         ImportByUser -> addModDeps mod is_loaded (pi_usages iface) mod_map
210                         other        -> mod_map
211         mod_map2 = delFromFM mod_map1 mod_name
212
213         this_mod_name = moduleName this_mod
214         is_loaded m   =  m == this_mod_name 
215                       || maybeToBool (lookupIfaceByModName hit pit m)
216                 -- We treat the currently-being-compiled module as 'loaded' because
217                 -- even though it isn't yet in the HIT or PIT; otherwise it gets
218                 -- put into iImpModInfo, and then spat out into its own interface
219                 -- file as a dependency
220
221         -- Now add info about this module to the PIT
222         has_orphans = pi_orphan iface
223         new_pit   = extendModuleEnv pit mod mod_iface
224         mod_iface = ModIface { mi_module = mod, mi_package = pi_pkg iface,
225                                mi_version = version,
226                                mi_orphan = has_orphans, mi_boot = hi_boot_file,
227                                mi_exports = avails, 
228                                mi_fixities = fix_env, mi_deprecs = deprec_env,
229                                mi_usages  = [], -- Will be filled in later
230                                mi_decls   = panic "No mi_decls in PIT",
231                                mi_globals = Nothing
232                     }
233
234         new_ifaces = ifaces { iPIT        = new_pit,
235                               iDecls      = new_decls,
236                               iInsts      = new_insts,
237                               iRules      = new_rules,
238                               iImpModInfo = mod_map2  }
239     in
240     setIfacesRn new_ifaces              `thenRn_`
241     returnRn (mod_iface, Nothing)
242     }}
243
244 -----------------------------------------------------
245 --      Adding module dependencies from the 
246 --      import decls in the interface file
247 -----------------------------------------------------
248
249 addModDeps :: Module 
250            -> (ModuleName -> Bool)      -- True for modules that are already loaded
251            -> [ImportVersion a] 
252            -> ImportedModuleInfo -> ImportedModuleInfo
253 -- (addModDeps M ivs deps)
254 -- We are importing module M, and M.hi contains 'import' decls given by ivs
255 addModDeps mod is_loaded new_deps mod_deps
256   = foldr add mod_deps filtered_new_deps
257   where
258         -- Don't record dependencies when importing a module from another package
259         -- Except for its descendents which contain orphans,
260         -- and in that case, forget about the boot indicator
261     filtered_new_deps :: [(ModuleName, (WhetherHasOrphans, IsBootInterface))]
262     filtered_new_deps
263         | isHomeModule mod  = [ (imp_mod, (has_orphans, is_boot))
264                               | (imp_mod, has_orphans, is_boot, _) <- new_deps,
265                                 not (is_loaded imp_mod)
266                               ]                       
267         | otherwise         = [ (imp_mod, (True, False))
268                               | (imp_mod, has_orphans, _, _) <- new_deps,
269                                 not (is_loaded imp_mod) && has_orphans
270                               ]
271     add (imp_mod, dep) deps = addToFM_C combine deps imp_mod dep
272
273     combine old@(old_has_orphans, old_is_boot) new@(new_has_orphans, new_is_boot)
274         | old_is_boot = new     -- Record the best is_boot info
275         | otherwise   = old
276
277 -----------------------------------------------------
278 --      Loading the export list
279 -----------------------------------------------------
280
281 loadExports :: (Version, [ExportItem]) -> RnM d (Version, [(ModuleName,Avails)])
282 loadExports (vers, items)
283   = mapRn loadExport items      `thenRn` \ avails_s ->
284     returnRn (vers, avails_s)
285
286
287 loadExport :: ExportItem -> RnM d (ModuleName, Avails)
288 loadExport (mod, entities)
289   = mapRn (load_entity mod) entities    `thenRn` \ avails ->
290     returnRn (mod, avails)
291   where
292     load_entity mod (Avail occ)
293       = newGlobalName mod occ   `thenRn` \ name ->
294         returnRn (Avail name)
295     load_entity mod (AvailTC occ occs)
296       = newGlobalName mod occ           `thenRn` \ name ->
297         mapRn (newGlobalName mod) occs  `thenRn` \ names ->
298         returnRn (AvailTC name names)
299
300
301 -----------------------------------------------------
302 --      Loading type/class/value decls
303 -----------------------------------------------------
304
305 loadDecls :: Module 
306           -> DeclsMap
307           -> [(Version, RdrNameTyClDecl)]
308           -> RnM d (NameEnv Version, DeclsMap)
309 loadDecls mod (decls_map, n_slurped) decls
310   = foldlRn (loadDecl mod) (emptyNameEnv, decls_map) decls      `thenRn` \ (vers, decls_map') -> 
311     returnRn (vers, (decls_map', n_slurped))
312
313 loadDecl mod (version_map, decls_map) (version, decl)
314   = getTyClDeclBinders mod decl `thenRn` \ (avail, sys_names) ->
315     let
316         full_avail    = case avail of
317                           Avail n -> avail
318                           AvailTC n ns -> AvailTC n (sys_names ++ ns)
319         main_name     = availName full_avail
320         new_decls_map = extendNameEnvList decls_map stuff
321         stuff         = [ (name, (full_avail, name==main_name, (mod, decl))) 
322                         | name <- availNames full_avail]
323
324         new_version_map = extendNameEnv version_map main_name version
325     in
326     traceRn (text "Loading" <+> ppr full_avail) `thenRn_`
327     returnRn (new_version_map, new_decls_map)
328
329 -----------------------------------------------------
330 --      Loading fixity decls
331 -----------------------------------------------------
332
333 loadFixDecls mod decls
334   = mapRn (loadFixDecl mod_name) decls  `thenRn` \ to_add ->
335     returnRn (mkNameEnv to_add)
336   where
337     mod_name = moduleName mod
338
339 loadFixDecl mod_name sig@(FixitySig rdr_name fixity loc)
340   = newGlobalName mod_name (rdrNameOcc rdr_name)        `thenRn` \ name ->
341     returnRn (name, fixity)
342
343
344 -----------------------------------------------------
345 --      Loading instance decls
346 -----------------------------------------------------
347
348 loadInstDecls :: Module
349               -> IfaceInsts
350               -> [RdrNameInstDecl]
351               -> RnM d IfaceInsts
352 loadInstDecls mod (insts, n_slurped) decls
353   = setModuleRn mod $
354     foldlRn (loadInstDecl mod) insts decls      `thenRn` \ insts' ->
355     returnRn (insts', n_slurped)
356
357
358 loadInstDecl mod insts decl@(InstDecl inst_ty _ _ _ _)
359   =     -- Find out what type constructors and classes are "gates" for the
360         -- instance declaration.  If all these "gates" are slurped in then
361         -- we should slurp the instance decl too.
362         -- 
363         -- We *don't* want to count names in the context part as gates, though.
364         -- For example:
365         --              instance Foo a => Baz (T a) where ...
366         --
367         -- Here the gates are Baz and T, but *not* Foo.
368         -- 
369         -- HOWEVER: functional dependencies make things more complicated
370         --      class C a b | a->b where ...
371         --      instance C Foo Baz where ...
372         -- Here, the gates are really only C and Foo, *not* Baz.
373         -- That is, if C and Foo are visible, even if Baz isn't, we must
374         -- slurp the decl.
375         --
376         -- Rather than take fundeps into account "properly", we just slurp
377         -- if C is visible and *any one* of the Names in the types
378         -- This is a slightly brutal approximation, but most instance decls
379         -- are regular H98 ones and it's perfect for them.
380         --
381         -- NOTICE that we rename the type before extracting its free
382         -- variables.  The free-variable finder for a renamed HsType 
383         -- does the Right Thing for built-in syntax like [] and (,).
384     initIfaceRnMS mod (
385         rnHsType (text "In an interface instance decl") inst_ty
386     )                                   `thenRn` \ inst_ty' ->
387     let 
388         (tvs,(cls,tys)) = getHsInstHead inst_ty'
389         free_tcs  = nameSetToList (extractHsTyNames_s tys) `minusList` hsTyVarNames tvs
390
391         gate_fn vis_fn = vis_fn cls && (null free_tcs || any vis_fn free_tcs)
392         -- Here is the implementation of HOWEVER above
393         -- (Note that we do let the inst decl in if it mentions 
394         --  no tycons at all.  Hence the null free_ty_names.)
395     in
396     returnRn ((gate_fn, (mod, decl)) `consBag` insts)
397
398
399
400 -----------------------------------------------------
401 --      Loading Rules
402 -----------------------------------------------------
403
404 loadRules :: Module -> IfaceRules 
405           -> (Version, [RdrNameRuleDecl])
406           -> RnM d (Version, IfaceRules)
407 loadRules mod (rule_bag, n_slurped) (version, rules)
408   | null rules || opt_IgnoreIfacePragmas 
409   = returnRn (version, (rule_bag, n_slurped))
410   | otherwise
411   = setModuleRn mod                     $
412     mapRn (loadRule mod) rules          `thenRn` \ new_rules ->
413     returnRn (version, (rule_bag `unionBags` listToBag new_rules, n_slurped))
414
415 loadRule :: Module -> RdrNameRuleDecl -> RnM d (GatedDecl RdrNameRuleDecl)
416 -- "Gate" the rule simply by whether the rule variable is
417 -- needed.  We can refine this later.
418 loadRule mod decl@(IfaceRule _ _ _ var _ _ src_loc)
419   = lookupIfaceName var         `thenRn` \ var_name ->
420     returnRn (\vis_fn -> vis_fn var_name, (mod, decl))
421
422
423 -----------------------------------------------------
424 --      Loading Deprecations
425 -----------------------------------------------------
426
427 loadDeprecs :: Module -> IfaceDeprecs -> RnM d Deprecations
428 loadDeprecs m Nothing                                  = returnRn NoDeprecs
429 loadDeprecs m (Just (Left txt))  = returnRn (DeprecAll txt)
430 loadDeprecs m (Just (Right prs)) = setModuleRn m                                $
431                                    foldlRn loadDeprec emptyNameEnv prs  `thenRn` \ env ->
432                                    returnRn (DeprecSome env)
433 loadDeprec deprec_env (n, txt)
434   = lookupIfaceName n           `thenRn` \ name ->
435     traceRn (text "Loaded deprecation(s) for" <+> ppr name <> colon <+> ppr txt) `thenRn_`
436     returnRn (extendNameEnv deprec_env name (name,txt))
437 \end{code}
438
439
440 %*********************************************************
441 %*                                                      *
442 \subsection{Getting binders out of a declaration}
443 %*                                                      *
444 %*********************************************************
445
446 @getDeclBinders@ returns the names for a @RdrNameHsDecl@.
447 It's used for both source code (from @availsFromDecl@) and interface files
448 (from @loadDecl@).
449
450 It doesn't deal with source-code specific things: @ValD@, @DefD@.  They
451 are handled by the sourc-code specific stuff in @RnNames@.
452
453         *** See "THE NAMING STORY" in HsDecls ****
454
455
456 \begin{code}
457 getTyClDeclBinders
458         :: Module
459         -> RdrNameTyClDecl
460         -> RnM d (AvailInfo, [Name])    -- The [Name] are the system names
461
462 -----------------
463 getTyClDeclBinders mod (IfaceSig {tcdName = var, tcdLoc = src_loc})
464   = newTopBinder mod var src_loc                        `thenRn` \ var_name ->
465     returnRn (Avail var_name, [])
466
467 getTyClDeclBinders mod tycl_decl
468   = new_top_bndrs mod (tyClDeclNames tycl_decl)         `thenRn` \ names@(main_name:_) ->
469     new_top_bndrs mod (tyClDeclSysNames tycl_decl)      `thenRn` \ sys_names ->
470     returnRn (AvailTC main_name names, sys_names)
471
472 -----------------
473 new_top_bndrs mod names_w_locs
474   = sequenceRn [newTopBinder mod name loc | (name,loc) <- names_w_locs]
475 \end{code}
476
477
478 %*********************************************************
479 %*                                                      *
480 \subsection{Reading an interface file}
481 %*                                                      *
482 %*********************************************************
483
484 \begin{code}
485 findAndReadIface :: SDoc -> ModuleName 
486                  -> IsBootInterface     -- True  <=> Look for a .hi-boot file
487                                         -- False <=> Look for .hi file
488                  -> RnM d (Either Message (Module, ParsedIface))
489         -- Nothing <=> file not found, or unreadable, or illegible
490         -- Just x  <=> successfully found and parsed 
491
492 findAndReadIface doc_str mod_name hi_boot_file
493   = traceRn trace_msg                   `thenRn_`
494
495     -- In interactive or --make mode, we are *not allowed* to demand-load
496     -- a home package .hi file.  So don't even look for them.
497     -- This helps in the case where you are sitting in eg. ghc/lib/std
498     -- and start up GHCi - it won't complain that all the modules it tries
499     -- to load are found in the home location.
500     ioToRnM_no_fail (readIORef v_GhcMode) `thenRn` \ mode ->
501     let home_allowed = hi_boot_file || not (isCompManagerMode mode)
502     in
503
504     ioToRnM (if home_allowed 
505                 then findModule mod_name
506                 else findPackageModule mod_name) `thenRn` \ maybe_found ->
507
508     case maybe_found of
509
510       Right (Just (wanted_mod,locn))
511         -> mkHiPath hi_boot_file locn `thenRn` \ file -> 
512            readIface file `thenRn` \ read_result ->
513            case read_result of
514                 Left bad -> returnRn (Left bad)
515                 Right iface ->  -- check that the module names agree
516                       let read_mod_name = pi_mod iface
517                           wanted_mod_name = moduleName wanted_mod
518                       in
519                       checkRn
520                           (wanted_mod_name == read_mod_name)
521                           (hiModuleNameMismatchWarn wanted_mod_name read_mod_name)
522                                         `thenRn_`
523                          returnRn (Right (wanted_mod, iface))
524         -- Can't find it
525       other   -> traceRn (ptext SLIT("...not found"))   `thenRn_`
526                  returnRn (Left (noIfaceErr mod_name hi_boot_file))
527
528   where
529     trace_msg = sep [hsep [ptext SLIT("Reading"), 
530                            if hi_boot_file then ptext SLIT("[boot]") else empty,
531                            ptext SLIT("interface for"), 
532                            ppr mod_name <> semi],
533                      nest 4 (ptext SLIT("reason:") <+> doc_str)]
534
535 mkHiPath hi_boot_file locn
536   | hi_boot_file = 
537         ioToRnM_no_fail (doesFileExist hi_boot_ver_path) `thenRn` \ b ->
538         if b then returnRn hi_boot_ver_path
539              else returnRn hi_boot_path
540   | otherwise    = returnRn hi_path
541         where hi_path            = ml_hi_file locn
542               (hi_base, _hi_suf) = splitFilename hi_path
543               hi_boot_path       = hi_base ++ ".hi-boot"
544               hi_boot_ver_path   = hi_base ++ ".hi-boot-" ++ cHscIfaceFileVersion
545 \end{code}
546
547 @readIface@ tries just the one file.
548
549 \begin{code}
550 readIface :: String -> RnM d (Either Message ParsedIface)
551         -- Nothing <=> file not found, or unreadable, or illegible
552         -- Just x  <=> successfully found and parsed 
553 readIface file_path
554   = --ioToRnM (putStrLn ("reading iface " ++ file_path)) `thenRn_`
555     traceRn (ptext SLIT("readIFace") <+> text file_path)        `thenRn_` 
556
557     ioToRnM (hGetStringBuffer False file_path)                  `thenRn` \ read_result ->
558     case read_result of {
559         Left io_error  -> bale_out (text (show io_error)) ;
560         Right contents -> 
561
562     case parseIface contents (mkPState loc exts) of
563         POk _ iface          -> returnRn (Right iface)
564         PFailed err          -> bale_out err
565     }
566   where
567     exts = ExtFlags {glasgowExtsEF = True,
568                      parrEF        = True}
569     loc  = mkSrcLoc (mkFastString file_path) 1
570
571     bale_out err = returnRn (Left (badIfaceFile file_path err))
572 \end{code}
573
574 %*********************************************************
575 %*                                                      *
576 \subsection{Looking up fixities}
577 %*                                                      *
578 %*********************************************************
579
580 @lookupFixityRn@ has to be in RnIfaces (or RnHiFiles), instead of
581 its obvious home in RnEnv,  because it calls @loadHomeInterface@.
582
583 lookupFixity is a bit strange.  
584
585 * Nested local fixity decls are put in the local fixity env, which we
586   find with getFixtyEnv
587
588 * Imported fixities are found in the HIT or PIT
589
590 * Top-level fixity decls in this module may be for Names that are
591     either  Global         (constructors, class operations)
592     or      Local/Exported (everything else)
593   (See notes with RnNames.getLocalDeclBinders for why we have this split.)
594   We put them all in the local fixity environment
595
596 \begin{code}
597 lookupFixityRn :: Name -> RnMS Fixity
598 lookupFixityRn name
599   = getModuleRn                         `thenRn` \ this_mod ->
600     if nameIsLocalOrFrom this_mod name
601     then        -- It's defined in this module
602         getFixityEnv                    `thenRn` \ local_fix_env ->
603         returnRn (lookupLocalFixity local_fix_env name)
604
605     else        -- It's imported
606       -- For imported names, we have to get their fixities by doing a
607       -- loadHomeInterface, and consulting the Ifaces that comes back
608       -- from that, because the interface file for the Name might not
609       -- have been loaded yet.  Why not?  Suppose you import module A,
610       -- which exports a function 'f', thus;
611       --        module CurrentModule where
612       --          import A( f )
613       --        module A( f ) where
614       --          import B( f )
615       -- Then B isn't loaded right away (after all, it's possible that
616       -- nothing from B will be used).  When we come across a use of
617       -- 'f', we need to know its fixity, and it's then, and only
618       -- then, that we load B.hi.  That is what's happening here.
619         loadHomeInterface doc name              `thenRn` \ iface ->
620         returnRn (lookupNameEnv (mi_fixities iface) name `orElse` defaultFixity)
621   where
622     doc      = ptext SLIT("Checking fixity for") <+> ppr name
623 \end{code}
624
625
626 %*********************************************************
627 %*                                                       *
628 \subsection{Errors}
629 %*                                                       *
630 %*********************************************************
631
632 \begin{code}
633 noIfaceErr mod_name boot_file
634   = ptext SLIT("Could not find interface file for") <+> quotes (ppr mod_name)
635         -- We used to print the search path, but we can't do that
636         -- now, because it's hidden inside the finder.
637         -- Maybe the finder should expose more functions.
638
639 badIfaceFile file err
640   = vcat [ptext SLIT("Bad interface file:") <+> text file, 
641           nest 4 err]
642
643 hiModuleNameMismatchWarn :: ModuleName -> ModuleName -> Message
644 hiModuleNameMismatchWarn requested_mod read_mod = 
645     hsep [ ptext SLIT("Something is amiss; requested module name")
646          , ppr requested_mod
647          , ptext SLIT("differs from name found in the interface file")
648          , ppr read_mod
649          ]
650
651 warnRedundantSourceImport mod_name
652   = ptext SLIT("Unnecessary {- SOURCE -} in the import of module")
653           <+> quotes (ppr mod_name)
654
655 warnSelfImport mod
656   = ptext SLIT("Importing my own interface: module") <+> ppr mod
657 \end{code}