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