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