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