[project @ 2000-11-08 14:52:06 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnNames.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnNames]{Extracting imported and top-level names in scope}
5
6 \begin{code}
7 module RnNames (
8         getGlobalNames
9     ) where
10
11 #include "HsVersions.h"
12
13 import CmdLineOpts      ( DynFlag(..) )
14
15 import HsSyn            ( HsModule(..), HsDecl(..), IE(..), ieName, ImportDecl(..),
16                           ForeignDecl(..), ForKind(..), isDynamicExtName,
17                           collectTopBinders
18                         )
19 import RdrHsSyn         ( RdrNameIE, RdrNameImportDecl,
20                           RdrNameHsModule, RdrNameHsDecl
21                         )
22 import RnIfaces         ( getInterfaceExports, recordLocalSlurps )
23 import RnHiFiles        ( getTyClDeclBinders )
24 import RnEnv
25 import RnMonad
26
27 import FiniteMap
28 import PrelNames        ( pRELUDE_Name, mAIN_Name, main_RDR )
29 import UniqFM           ( lookupUFM )
30 import Bag              ( bagToList )
31 import Module           ( ModuleName, moduleName, WhereFrom(..) )
32 import NameSet
33 import Name             ( Name, nameSrcLoc,
34                           setLocalNameSort, nameOccName,  nameEnvElts )
35 import HscTypes         ( Provenance(..), ImportReason(..), GlobalRdrEnv,
36                           GenAvailInfo(..), AvailInfo, Avails, AvailEnv )
37 import RdrName          ( RdrName, rdrNameOcc, setRdrNameOcc, mkRdrQual, mkRdrUnqual )
38 import OccName          ( setOccNameSpace, dataName )
39 import NameSet          ( elemNameSet, emptyNameSet )
40 import Outputable
41 import Maybes           ( maybeToBool, catMaybes, mapMaybe )
42 import UniqFM           ( emptyUFM, listToUFM )
43 import ListSetOps       ( removeDups )
44 import Util             ( sortLt )
45 import List             ( partition )
46 \end{code}
47
48
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection{Get global names}
53 %*                                                                      *
54 %************************************************************************
55
56 \begin{code}
57 getGlobalNames :: Module -> RdrNameHsModule
58                -> RnMG (GlobalRdrEnv,   -- Maps all in-scope things
59                         GlobalRdrEnv,   -- Maps just *local* things
60                         Avails,         -- The exported stuff
61                         AvailEnv)       -- Maps a name to its parent AvailInfo
62                                         -- Just for in-scope things only
63
64 getGlobalNames this_mod (HsModule _ _ exports imports decls _ mod_loc)
65   =     -- These two fix-loops are to get the right
66         -- provenance information into a Name
67     fixRn ( \ ~(rec_gbl_env, _, rec_export_avails, _) ->
68
69         let
70            rec_unqual_fn :: Name -> Bool        -- Is this chap in scope unqualified?
71            rec_unqual_fn = unQualInScope rec_gbl_env
72
73            rec_exp_fn :: Name -> Bool
74            rec_exp_fn = mk_export_fn (availsToNameSet rec_export_avails)
75         in
76
77                 -- PROCESS LOCAL DECLS
78                 -- Do these *first* so that the correct provenance gets
79                 -- into the global name cache.
80         importsFromLocalDecls this_mod rec_exp_fn decls         `thenRn` \ (local_gbl_env, local_mod_avails) ->
81
82                 -- PROCESS IMPORT DECLS
83                 -- Do the non {- SOURCE -} ones first, so that we get a helpful
84                 -- warning for {- SOURCE -} ones that are unnecessary
85         doptRn Opt_NoImplicitPrelude                            `thenRn` \ opt_no_prelude -> 
86         let
87           all_imports        = mk_prel_imports opt_no_prelude ++ imports
88           (source, ordinary) = partition is_source_import all_imports
89           is_source_import (ImportDecl _ ImportByUserSource _ _ _ _) = True
90           is_source_import other                                     = False
91
92           get_imports = importsFromImportDecl this_mod_name rec_unqual_fn 
93         in
94         mapAndUnzipRn get_imports ordinary      `thenRn` \ (imp_gbl_envs1, imp_avails_s1) ->
95         mapAndUnzipRn get_imports source        `thenRn` \ (imp_gbl_envs2, imp_avails_s2) ->
96
97                 -- COMBINE RESULTS
98                 -- We put the local env second, so that a local provenance
99                 -- "wins", even if a module imports itself.
100         let
101             gbl_env :: GlobalRdrEnv
102             imp_gbl_env = foldr plusGlobalRdrEnv emptyRdrEnv (imp_gbl_envs2 ++ imp_gbl_envs1)
103             gbl_env     = imp_gbl_env `plusGlobalRdrEnv` local_gbl_env
104
105             all_avails :: ExportAvails
106             all_avails = foldr plusExportAvails local_mod_avails (imp_avails_s2 ++ imp_avails_s1)
107
108             (_, global_avail_env) = all_avails
109         in
110
111                 -- PROCESS EXPORT LIST (but not if we've had errors already)
112         checkErrsRn             `thenRn` \ no_errs_so_far ->
113         (if no_errs_so_far then
114             exportsFromAvail this_mod_name exports all_avails gbl_env
115          else
116             returnRn []
117         )                                               `thenRn` \ export_avails ->
118         
119                 -- ALL DONE
120         returnRn (gbl_env, local_gbl_env, export_avails, global_avail_env)
121    )
122   where
123     this_mod_name = moduleName this_mod
124
125         -- NB: opt_NoImplicitPrelude is slightly different to import Prelude ();
126         -- because the former doesn't even look at Prelude.hi for instance declarations,
127         -- whereas the latter does.
128     mk_prel_imports no_prelude
129         | this_mod_name == pRELUDE_Name ||
130           explicit_prelude_import ||
131           no_prelude
132         = []
133
134         | otherwise = [ImportDecl pRELUDE_Name
135                                   ImportByUser
136                                   False {- Not qualified -}
137                                   Nothing       {- No "as" -}
138                                   Nothing       {- No import list -}
139                                   mod_loc]
140     
141     explicit_prelude_import
142       = not (null [ () | (ImportDecl mod _ _ _ _ _) <- imports, mod == pRELUDE_Name ])
143 \end{code}
144         
145 \begin{code}
146 importsFromImportDecl :: ModuleName
147                       -> (Name -> Bool)         -- OK to omit qualifier
148                       -> RdrNameImportDecl
149                       -> RnMG (GlobalRdrEnv, 
150                                ExportAvails) 
151
152 importsFromImportDecl this_mod_name is_unqual (ImportDecl imp_mod_name from qual_only as_mod import_spec iloc)
153   = pushSrcLocRn iloc $
154     getInterfaceExports imp_mod_name from       `thenRn` \ (imp_mod, avails_by_module) ->
155
156     if null avails_by_module then
157         -- If there's an error in getInterfaceExports, (e.g. interface
158         -- file not found) we get lots of spurious errors from 'filterImports'
159         returnRn (emptyRdrEnv, mkEmptyExportAvails imp_mod_name)
160     else
161
162     let
163         avails :: Avails
164         avails = [ avail | (mod_name, avails) <- avails_by_module,
165                            mod_name /= this_mod_name,
166                            avail <- avails ]
167         -- If the module exports anything defined in this module, just ignore it.
168         -- Reason: otherwise it looks as if there are two local definition sites
169         -- for the thing, and an error gets reported.  Easiest thing is just to
170         -- filter them out up front. This situation only arises if a module
171         -- imports itself, or another module that imported it.  (Necessarily,
172         -- this invoves a loop.)  
173         --
174         -- Tiresome consequence: if you say
175         --      module A where
176         --         import B( AType )
177         --         type AType = ...
178         --
179         --      module B( AType ) where
180         --         import {-# SOURCE #-} A( AType )
181         --
182         -- then you'll get a 'B does not export AType' message.  Oh well.
183
184     in
185     filterImports imp_mod_name from import_spec avails  `thenRn` \ (filtered_avails, hides, explicits) ->
186
187     let
188         mk_provenance name = NonLocalDef (UserImport imp_mod iloc (name `elemNameSet` explicits)) 
189                                          (is_unqual name)
190     in
191
192     qualifyImports imp_mod_name
193                    (not qual_only)      -- Maybe want unqualified names
194                    as_mod hides
195                    mk_provenance
196                    filtered_avails
197 \end{code}
198
199
200 \begin{code}
201 importsFromLocalDecls this_mod rec_exp_fn decls
202   = mapRn (getLocalDeclBinders this_mod rec_exp_fn) decls       `thenRn` \ avails_s ->
203
204     let
205         avails = concat avails_s
206
207         all_names :: [Name]     -- All the defns; no dups eliminated
208         all_names = [name | avail <- avails, name <- availNames avail]
209
210         dups :: [[Name]]
211         (_, dups) = removeDups compare all_names
212     in
213         -- Check for duplicate definitions
214     mapRn_ (addErrRn . dupDeclErr) dups         `thenRn_` 
215
216         -- Record that locally-defined things are available
217     recordLocalSlurps avails                    `thenRn_`
218
219         -- Build the environment
220     qualifyImports (moduleName this_mod)
221                    True                 -- Want unqualified names
222                    Nothing              -- no 'as M'
223                    []                   -- Hide nothing
224                    (\n -> LocalDef)     -- Provenance is local
225                    avails
226
227 ---------------------------
228 getLocalDeclBinders :: Module 
229                     -> (Name -> Bool)   -- Whether exported
230                     -> RdrNameHsDecl -> RnMG Avails
231 getLocalDeclBinders mod rec_exp_fn (TyClD tycl_decl)
232   =     -- For type and class decls, we generate Global names, with
233         -- no export indicator.  They need to be global because they get
234         -- permanently bound into the TyCons and Classes.  They don't need
235         -- an export indicator because they are all implicitly exported.
236     getTyClDeclBinders mod tycl_decl    `thenRn` \ avail ->
237     returnRn [avail]
238
239 getLocalDeclBinders mod rec_exp_fn (ValD binds)
240   = mapRn (newLocalBinder mod rec_exp_fn) 
241           (bagToList (collectTopBinders binds))
242
243 getLocalDeclBinders mod rec_exp_fn (ForD (ForeignDecl nm kind _ ext_nm _ loc))
244   | binds_haskell_name kind
245   = newLocalBinder mod rec_exp_fn (nm, loc)         `thenRn` \ avail ->
246     returnRn [avail]
247
248   | otherwise           -- a foreign export
249   = returnRn []
250   where
251     binds_haskell_name (FoImport _) = True
252     binds_haskell_name FoLabel      = True
253     binds_haskell_name FoExport     = isDynamicExtName ext_nm
254
255 getLocalDeclBinders mod rec_exp_fn (FixD _)    = returnRn []
256 getLocalDeclBinders mod rec_exp_fn (DeprecD _) = returnRn []
257 getLocalDeclBinders mod rec_exp_fn (DefD _)    = returnRn []
258 getLocalDeclBinders mod rec_exp_fn (InstD _)   = returnRn []
259 getLocalDeclBinders mod rec_exp_fn (RuleD _)   = returnRn []
260
261 ---------------------------
262 newLocalBinder mod rec_exp_fn (rdr_name, loc)
263   =     -- Generate a local name, and with a suitable export indicator
264     newTopBinder mod rdr_name loc       `thenRn` \ name ->
265     returnRn (Avail (setLocalNameSort name (rec_exp_fn name)))
266 \end{code}
267
268
269 %************************************************************************
270 %*                                                                      *
271 \subsection{Filtering imports}
272 %*                                                                      *
273 %************************************************************************
274
275 @filterImports@ takes the @ExportEnv@ telling what the imported module makes
276 available, and filters it through the import spec (if any).
277
278 \begin{code}
279 filterImports :: ModuleName                     -- The module being imported
280               -> WhereFrom                      -- Tells whether it's a {-# SOURCE #-} import
281               -> Maybe (Bool, [RdrNameIE])      -- Import spec; True => hiding
282               -> [AvailInfo]                    -- What's available
283               -> RnMG ([AvailInfo],             -- What's actually imported
284                        [AvailInfo],             -- What's to be hidden
285                                                 -- (the unqualified version, that is)
286                         -- (We need to return both the above sets, because
287                         --  the qualified version is never hidden; so we can't
288                         --  implement hiding by reducing what's imported.)
289                        NameSet)                 -- What was imported explicitly
290
291         -- Complains if import spec mentions things that the module doesn't export
292         -- Warns/informs if import spec contains duplicates.
293 filterImports mod from Nothing imports
294   = returnRn (imports, [], emptyNameSet)
295
296 filterImports mod from (Just (want_hiding, import_items)) total_avails
297   = flatMapRn get_item import_items             `thenRn` \ avails_w_explicits ->
298     let
299         (item_avails, explicits_s) = unzip avails_w_explicits
300         explicits                  = foldl addListToNameSet emptyNameSet explicits_s
301     in
302     if want_hiding 
303     then        
304         -- All imported; item_avails to be hidden
305         returnRn (total_avails, item_avails, emptyNameSet)
306     else
307         -- Just item_avails imported; nothing to be hidden
308         returnRn (item_avails, [], explicits)
309   where
310     import_fm :: FiniteMap OccName AvailInfo
311     import_fm = listToFM [ (nameOccName name, avail) 
312                          | avail <- total_avails,
313                            name  <- availNames avail]
314         -- Even though availNames returns data constructors too,
315         -- they won't make any difference because naked entities like T
316         -- in an import list map to TcOccs, not VarOccs.
317
318     bale_out item = addErrRn (badImportItemErr mod from item)   `thenRn_`
319                     returnRn []
320
321     get_item item@(IEModuleContents _) = bale_out item
322
323     get_item item@(IEThingAll _)
324       = case check_item item of
325           Nothing                    -> bale_out item
326           Just avail@(AvailTC _ [n]) ->         -- This occurs when you import T(..), but
327                                                 -- only export T abstractly.  The single [n]
328                                                 -- in the AvailTC is the type or class itself
329                                         addWarnRn (dodgyImportWarn mod item)    `thenRn_`
330                                         returnRn [(avail, [availName avail])]
331           Just avail                 -> returnRn [(avail, [availName avail])]
332
333     get_item item@(IEThingAbs n)
334       | want_hiding     -- hiding( C ) 
335                         -- Here the 'C' can be a data constructor *or* a type/class
336       = case catMaybes [check_item item, check_item (IEThingAbs data_n)] of
337                 []     -> bale_out item
338                 avails -> returnRn [(a, []) | a <- avails]
339                                 -- The 'explicits' list is irrelevant when hiding
340       where
341         data_n = setRdrNameOcc n (setOccNameSpace (rdrNameOcc n) dataName)
342
343     get_item item
344       = case check_item item of
345           Nothing    -> bale_out item
346           Just avail -> returnRn [(avail, availNames avail)]
347
348     check_item item
349       | not (maybeToBool maybe_in_import_avails) ||
350         not (maybeToBool maybe_filtered_avail)
351       = Nothing
352
353       | otherwise    
354       = Just filtered_avail
355                 
356       where
357         wanted_occ             = rdrNameOcc (ieName item)
358         maybe_in_import_avails = lookupFM import_fm wanted_occ
359
360         Just avail             = maybe_in_import_avails
361         maybe_filtered_avail   = filterAvail item avail
362         Just filtered_avail    = maybe_filtered_avail
363 \end{code}
364
365
366
367 %************************************************************************
368 %*                                                                      *
369 \subsection{Qualifiying imports}
370 %*                                                                      *
371 %************************************************************************
372
373 @qualifyImports@ takes the @ExportEnv@ after filtering through the import spec
374 of an import decl, and deals with producing an @RnEnv@ with the 
375 right qualified names.  It also turns the @Names@ in the @ExportEnv@ into
376 fully fledged @Names@.
377
378 \begin{code}
379 qualifyImports :: ModuleName            -- Imported module
380                -> Bool                  -- True <=> want unqualified import
381                -> Maybe ModuleName      -- Optional "as M" part 
382                -> [AvailInfo]           -- What's to be hidden
383                -> (Name -> Provenance)
384                -> Avails                -- Whats imported and how
385                -> RnMG (GlobalRdrEnv, ExportAvails)
386
387 qualifyImports this_mod unqual_imp as_mod hides mk_provenance avails
388   = 
389         -- Make the name environment.  We're talking about a 
390         -- single module here, so there must be no name clashes.
391         -- In practice there only ever will be if it's the module
392         -- being compiled.
393     let
394         -- Add the things that are available
395         name_env1 = foldl add_avail emptyRdrEnv avails
396
397         -- Delete things that are hidden
398         name_env2 = foldl del_avail name_env1 hides
399
400         -- Create the export-availability info
401         export_avails = mkExportAvails qual_mod unqual_imp name_env2 avails
402     in
403     returnRn (name_env2, export_avails)
404
405   where
406     qual_mod = case as_mod of
407                   Nothing           -> this_mod
408                   Just another_name -> another_name
409
410     add_avail :: GlobalRdrEnv -> AvailInfo -> GlobalRdrEnv
411     add_avail env avail = foldl add_name env (availNames avail)
412
413     add_name env name
414         | unqual_imp = env2
415         | otherwise  = env1
416         where
417           env1 = addOneToGlobalRdrEnv env  (mkRdrQual qual_mod occ) (name,prov)
418           env2 = addOneToGlobalRdrEnv env1 (mkRdrUnqual occ)        (name,prov)
419           occ  = nameOccName name
420           prov = mk_provenance name
421
422     del_avail env avail = foldl delOneFromGlobalRdrEnv env rdr_names
423                         where
424                           rdr_names = map (mkRdrUnqual . nameOccName) (availNames avail)
425
426
427 mkEmptyExportAvails :: ModuleName -> ExportAvails
428 mkEmptyExportAvails mod_name = (unitFM mod_name [], emptyUFM)
429
430 mkExportAvails :: ModuleName -> Bool -> GlobalRdrEnv -> [AvailInfo] -> ExportAvails
431 mkExportAvails mod_name unqual_imp name_env avails
432   = (mod_avail_env, entity_avail_env)
433   where
434     mod_avail_env = unitFM mod_name unqual_avails 
435
436         -- unqual_avails is the Avails that are visible in *unqualfied* form
437         -- (1.4 Report, Section 5.1.1)
438         -- For example, in 
439         --      import T hiding( f )
440         -- we delete f from avails
441
442     unqual_avails | not unqual_imp = [] -- Short cut when no unqualified imports
443                   | otherwise      = mapMaybe prune avails
444
445     prune (Avail n) | unqual_in_scope n = Just (Avail n)
446     prune (Avail n) | otherwise         = Nothing
447     prune (AvailTC n ns) | null uqs     = Nothing
448                          | otherwise    = Just (AvailTC n uqs)
449                          where
450                            uqs = filter unqual_in_scope ns
451
452     unqual_in_scope n = unQualInScope name_env n
453
454     entity_avail_env = listToUFM [ (name,avail) | avail <- avails, 
455                                                   name  <- availNames avail]
456
457 plusExportAvails ::  ExportAvails ->  ExportAvails ->  ExportAvails
458 plusExportAvails (m1, e1) (m2, e2)
459   = (plusFM_C (++) m1 m2, plusAvailEnv e1 e2)
460         -- ToDo: wasteful: we do this once for each constructor!
461 \end{code}
462
463
464 %************************************************************************
465 %*                                                                      *
466 \subsection{Export list processing}
467 %*                                                                      *
468 %************************************************************************
469
470 Processing the export list.
471
472 You might think that we should record things that appear in the export list
473 as ``occurrences'' (using @addOccurrenceName@), but you'd be wrong.
474 We do check (here) that they are in scope,
475 but there is no need to slurp in their actual declaration
476 (which is what @addOccurrenceName@ forces).
477
478 Indeed, doing so would big trouble when
479 compiling @PrelBase@, because it re-exports @GHC@, which includes @takeMVar#@,
480 whose type includes @ConcBase.StateAndSynchVar#@, and so on...
481
482 \begin{code}
483 type ExportAccum        -- The type of the accumulating parameter of
484                         -- the main worker function in exportsFromAvail
485      = ([ModuleName],           -- 'module M's seen so far
486         ExportOccMap,           -- Tracks exported occurrence names
487         AvailEnv)               -- The accumulated exported stuff, kept in an env
488                                 --   so we can common-up related AvailInfos
489
490 type ExportOccMap = FiniteMap OccName (Name, RdrNameIE)
491         -- Tracks what a particular exported OccName
492         --   in an export list refers to, and which item
493         --   it came from.  It's illegal to export two distinct things
494         --   that have the same occurrence name
495
496
497 exportsFromAvail :: ModuleName
498                  -> Maybe [RdrNameIE]   -- Export spec
499                  -> ExportAvails
500                  -> GlobalRdrEnv 
501                  -> RnMG Avails
502         -- Complains if two distinct exports have same OccName
503         -- Warns about identical exports.
504         -- Complains about exports items not in scope
505 exportsFromAvail this_mod Nothing export_avails global_name_env
506   = exportsFromAvail this_mod true_exports export_avails global_name_env
507   where
508     true_exports = Just $ if this_mod == mAIN_Name
509                           then [IEVar main_RDR]
510                                -- export Main.main *only* unless otherwise specified,
511                           else [IEModuleContents this_mod]
512                                -- but for all other modules export everything.
513
514 exportsFromAvail this_mod (Just export_items) 
515                  (mod_avail_env, entity_avail_env)
516                  global_name_env
517   = doptRn Opt_WarnDuplicateExports             `thenRn` \ warn_dup_exports ->
518     foldlRn (exports_from_item warn_dup_exports)
519             ([], emptyFM, emptyAvailEnv) export_items
520                                                 `thenRn` \ (_, _, export_avail_map) ->
521     let
522         export_avails :: [AvailInfo]
523         export_avails   = nameEnvElts export_avail_map
524     in
525     returnRn export_avails
526
527   where
528     exports_from_item :: Bool -> ExportAccum -> RdrNameIE -> RnMG ExportAccum
529
530     exports_from_item warn_dups acc@(mods, occs, avails) ie@(IEModuleContents mod)
531         | mod `elem` mods       -- Duplicate export of M
532         = warnCheckRn warn_dups (dupModuleExport mod)   `thenRn_`
533           returnRn acc
534
535         | otherwise
536         = case lookupFM mod_avail_env mod of
537                 Nothing         -> failWithRn acc (modExportErr mod)
538                 Just mod_avails -> foldlRn (check_occs ie) occs mod_avails
539                                    `thenRn` \ occs' ->
540                                    let
541                                         avails' = foldl addAvail avails mod_avails
542                                    in
543                                    returnRn (mod:mods, occs', avails')
544
545     exports_from_item warn_dups acc@(mods, occs, avails) ie
546         = lookupSrcName global_name_env (ieName ie)     `thenRn` \ name -> 
547
548                 -- See what's available in the current environment
549           case lookupUFM entity_avail_env name of {
550             Nothing ->  -- I can't see why this should ever happen; if the thing 
551                         -- is in scope at all it ought to have some availability
552                         pprTrace "exportsFromAvail: curious Nothing:" (ppr name)
553                         returnRn acc ;
554
555             Just avail ->
556
557                 -- Filter out the bits we want
558           case filterAvail ie avail of {
559             Nothing ->  -- Not enough availability
560                            failWithRn acc (exportItemErr ie) ;
561
562             Just export_avail ->        
563
564                 -- Phew!  It's OK!  Now to check the occurrence stuff!
565           warnCheckRn (ok_item ie avail) (dodgyExportWarn ie)   `thenRn_`
566           check_occs ie occs export_avail                       `thenRn` \ occs' ->
567           returnRn (mods, occs', addAvail avails export_avail)
568           }}
569
570
571
572 ok_item (IEThingAll _) (AvailTC _ [n]) = False
573   -- This occurs when you import T(..), but
574   -- only export T abstractly.  The single [n]
575   -- in the AvailTC is the type or class itself
576 ok_item _ _ = True
577
578 check_occs :: RdrNameIE -> ExportOccMap -> AvailInfo -> RnMG ExportOccMap
579 check_occs ie occs avail 
580   = doptRn Opt_WarnDuplicateExports     `thenRn` \ warn_dup_exports ->
581     foldlRn (check warn_dup_exports) occs (availNames avail)
582   where
583     check warn_dup occs name
584       = case lookupFM occs name_occ of
585           Nothing           -> returnRn (addToFM occs name_occ (name, ie))
586           Just (name', ie') 
587             | name == name' ->  -- Duplicate export
588                                 warnCheckRn warn_dup
589                                             (dupExportWarn name_occ ie ie')
590                                 `thenRn_` returnRn occs
591
592             | otherwise     ->  -- Same occ name but different names: an error
593                                 failWithRn occs (exportClashErr name_occ ie ie')
594       where
595         name_occ = nameOccName name
596         
597 mk_export_fn :: NameSet -> (Name -> Bool)       -- True => exported
598 mk_export_fn exported_names = \name ->  name `elemNameSet` exported_names
599 \end{code}
600
601 %************************************************************************
602 %*                                                                      *
603 \subsection{Errors}
604 %*                                                                      *
605 %************************************************************************
606
607 \begin{code}
608 badImportItemErr mod from ie
609   = sep [ptext SLIT("Module"), quotes (ppr mod), source_import,
610          ptext SLIT("does not export"), quotes (ppr ie)]
611   where
612     source_import = case from of
613                       ImportByUserSource -> ptext SLIT("(hi-boot interface)")
614                       other              -> empty
615
616 dodgyImportWarn mod item = dodgyMsg (ptext SLIT("import")) item
617 dodgyExportWarn     item = dodgyMsg (ptext SLIT("export")) item
618
619 dodgyMsg kind item@(IEThingAll tc)
620   = sep [ ptext SLIT("The") <+> kind <+> ptext SLIT("item") <+> quotes (ppr item),
621           ptext SLIT("suggests that") <+> quotes (ppr tc) <+> ptext SLIT("has constructor or class methods"),
622           ptext SLIT("but it has none; it is a type synonym or abstract type or class") ]
623           
624 modExportErr mod
625   = hsep [ ptext SLIT("Unknown module in export list: module"), quotes (ppr mod)]
626
627 exportItemErr export_item
628   = sep [ ptext SLIT("The export item") <+> quotes (ppr export_item),
629           ptext SLIT("attempts to export constructors or class methods that are not visible here") ]
630
631 exportClashErr occ_name ie1 ie2
632   = hsep [ptext SLIT("The export items"), quotes (ppr ie1)
633          ,ptext SLIT("and"), quotes (ppr ie2)
634          ,ptext SLIT("create conflicting exports for"), quotes (ppr occ_name)]
635
636 dupDeclErr (n:ns)
637   = vcat [ptext SLIT("Multiple declarations of") <+> quotes (ppr n),
638           nest 4 (vcat (map ppr sorted_locs))]
639   where
640     sorted_locs = sortLt occ'ed_before (map nameSrcLoc (n:ns))
641     occ'ed_before a b = LT == compare a b
642
643 dupExportWarn occ_name ie1 ie2
644   = hsep [quotes (ppr occ_name), 
645           ptext SLIT("is exported by"), quotes (ppr ie1),
646           ptext SLIT("and"),            quotes (ppr ie2)]
647
648 dupModuleExport mod
649   = hsep [ptext SLIT("Duplicate"),
650           quotes (ptext SLIT("Module") <+> ppr mod), 
651           ptext SLIT("in export list")]
652 \end{code}