Fix Haddock errors.
[ghc-hetmet.git] / compiler / basicTypes / RdrName.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 \begin{code}
7 module RdrName (
8         RdrName(..),    -- Constructors exported only to BinIface
9
10         -- Construction
11         mkRdrUnqual, mkRdrQual, 
12         mkUnqual, mkVarUnqual, mkQual, mkOrig,
13         nameRdrName, getRdrName, 
14         mkDerivedRdrName, 
15
16         -- Destruction
17         rdrNameOcc, setRdrNameSpace,
18         isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual, 
19         isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
20
21         -- Printing;    instance Outputable RdrName
22
23         -- LocalRdrEnv
24         LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv,
25         lookupLocalRdrEnv, lookupLocalRdrOcc, elemLocalRdrEnv,
26
27         -- GlobalRdrEnv
28         GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv, 
29         lookupGlobalRdrEnv, extendGlobalRdrEnv,
30         pprGlobalRdrEnv, globalRdrEnvElts,
31         lookupGRE_RdrName, lookupGRE_Name, getGRE_NameQualifier_maybes,
32         hideSomeUnquals, findLocalDupsRdrEnv,
33
34         -- GlobalRdrElt, Provenance, ImportSpec
35         GlobalRdrElt(..), isLocalGRE, unQualOK, qualSpecOK, unQualSpecOK,
36         Provenance(..), pprNameProvenance,
37         Parent(..), 
38         ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..), 
39         importSpecLoc, importSpecModule, isExplicitItem
40   ) where 
41
42 #include "HsVersions.h"
43
44 import Module
45 import Name
46 import Maybes
47 import SrcLoc
48 import FastString
49 import Outputable
50 import Util
51 \end{code}
52
53 %************************************************************************
54 %*                                                                      *
55 \subsection{The main data type}
56 %*                                                                      *
57 %************************************************************************
58
59 \begin{code}
60 data RdrName 
61   = Unqual OccName
62         -- Used for ordinary, unqualified occurrences 
63
64   | Qual ModuleName OccName
65         -- A qualified name written by the user in 
66         --  *source* code.  The module isn't necessarily 
67         -- the module where the thing is defined; 
68         -- just the one from which it is imported
69
70   | Orig Module OccName
71         -- An original name; the module is the *defining* module.
72         -- This is used when GHC generates code that will be fed
73         -- into the renamer (e.g. from deriving clauses), but where
74         -- we want to say "Use Prelude.map dammit".  
75  
76   | Exact Name
77         -- We know exactly the Name. This is used 
78         --  (a) when the parser parses built-in syntax like "[]" 
79         --      and "(,)", but wants a RdrName from it
80         --  (b) by Template Haskell, when TH has generated a unique name
81 \end{code}
82
83
84 %************************************************************************
85 %*                                                                      *
86 \subsection{Simple functions}
87 %*                                                                      *
88 %************************************************************************
89
90 \begin{code}
91 rdrNameOcc :: RdrName -> OccName
92 rdrNameOcc (Qual _ occ) = occ
93 rdrNameOcc (Unqual occ) = occ
94 rdrNameOcc (Orig _ occ) = occ
95 rdrNameOcc (Exact name) = nameOccName name
96
97 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
98 -- This rather gruesome function is used mainly by the parser
99 -- When parsing         data T a = T | T1 Int
100 -- we parse the data constructors as *types* because of parser ambiguities,
101 -- so then we need to change the *type constr* to a *data constr*
102 --
103 -- The original-name case *can* occur when parsing
104 --              data [] a = [] | a : [a]
105 -- For the orig-name case we return an unqualified name.
106 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
107 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
108 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
109 setRdrNameSpace (Exact n)    ns = Orig (nameModule n)
110                                        (setOccNameSpace ns (nameOccName n))
111 \end{code}
112
113 \begin{code}
114         -- These two are the basic constructors
115 mkRdrUnqual :: OccName -> RdrName
116 mkRdrUnqual occ = Unqual occ
117
118 mkRdrQual :: ModuleName -> OccName -> RdrName
119 mkRdrQual mod occ = Qual mod occ
120
121 mkOrig :: Module -> OccName -> RdrName
122 mkOrig mod occ = Orig mod occ
123
124 ---------------
125 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
126 mkDerivedRdrName parent mk_occ
127   = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
128
129 ---------------
130         -- These two are used when parsing source files
131         -- They do encode the module and occurrence names
132 mkUnqual :: NameSpace -> FastString -> RdrName
133 mkUnqual sp n = Unqual (mkOccNameFS sp n)
134
135 mkVarUnqual :: FastString -> RdrName
136 mkVarUnqual n = Unqual (mkVarOccFS n)
137
138 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
139 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
140
141 getRdrName :: NamedThing thing => thing -> RdrName
142 getRdrName name = nameRdrName (getName name)
143
144 nameRdrName :: Name -> RdrName
145 nameRdrName name = Exact name
146 -- Keep the Name even for Internal names, so that the
147 -- unique is still there for debug printing, particularly
148 -- of Types (which are converted to IfaceTypes before printing)
149
150 nukeExact :: Name -> RdrName
151 nukeExact n 
152   | isExternalName n = Orig (nameModule n) (nameOccName n)
153   | otherwise        = Unqual (nameOccName n)
154 \end{code}
155
156 \begin{code}
157 isRdrDataCon :: RdrName -> Bool
158 isRdrTyVar   :: RdrName -> Bool
159 isRdrTc      :: RdrName -> Bool
160
161 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
162 isRdrTyVar   rn = isTvOcc   (rdrNameOcc rn)
163 isRdrTc      rn = isTcOcc   (rdrNameOcc rn)
164
165 isSrcRdrName :: RdrName -> Bool
166 isSrcRdrName (Unqual _) = True
167 isSrcRdrName (Qual _ _) = True
168 isSrcRdrName _          = False
169
170 isUnqual :: RdrName -> Bool
171 isUnqual (Unqual _) = True
172 isUnqual _          = False
173
174 isQual :: RdrName -> Bool
175 isQual (Qual _ _) = True
176 isQual _          = False
177
178 isQual_maybe :: RdrName -> Maybe (ModuleName, OccName)
179 isQual_maybe (Qual m n) = Just (m,n)
180 isQual_maybe _          = Nothing
181
182 isOrig :: RdrName -> Bool
183 isOrig (Orig _ _) = True
184 isOrig _          = False
185
186 isOrig_maybe :: RdrName -> Maybe (Module, OccName)
187 isOrig_maybe (Orig m n) = Just (m,n)
188 isOrig_maybe _          = Nothing
189
190 isExact :: RdrName -> Bool
191 isExact (Exact _) = True
192 isExact _         = False
193
194 isExact_maybe :: RdrName -> Maybe Name
195 isExact_maybe (Exact n) = Just n
196 isExact_maybe _         = Nothing
197 \end{code}
198
199
200 %************************************************************************
201 %*                                                                      *
202 \subsection{Instances}
203 %*                                                                      *
204 %************************************************************************
205
206 \begin{code}
207 instance Outputable RdrName where
208     ppr (Exact name)   = ppr name
209     ppr (Unqual occ)   = ppr occ
210     ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
211     ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
212
213 instance OutputableBndr RdrName where
214     pprBndr _ n 
215         | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
216         | otherwise              = ppr n
217
218 instance Eq RdrName where
219     (Exact n1)    == (Exact n2)    = n1==n2
220         -- Convert exact to orig
221     (Exact n1)    == r2@(Orig _ _) = nukeExact n1 == r2
222     r1@(Orig _ _) == (Exact n2)    = r1 == nukeExact n2
223
224     (Orig m1 o1)  == (Orig m2 o2)  = m1==m2 && o1==o2
225     (Qual m1 o1)  == (Qual m2 o2)  = m1==m2 && o1==o2
226     (Unqual o1)   == (Unqual o2)   = o1==o2
227     _             == _             = False
228
229 instance Ord RdrName where
230     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
231     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
232     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
233     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
234
235         -- Exact < Unqual < Qual < Orig
236         -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig 
237         --      before comparing so that Prelude.map == the exact Prelude.map, but 
238         --      that meant that we reported duplicates when renaming bindings 
239         --      generated by Template Haskell; e.g 
240         --      do { n1 <- newName "foo"; n2 <- newName "foo"; 
241         --           <decl involving n1,n2> }
242         --      I think we can do without this conversion
243     compare (Exact n1) (Exact n2) = n1 `compare` n2
244     compare (Exact _)  _          = LT
245
246     compare (Unqual _)   (Exact _)    = GT
247     compare (Unqual o1)  (Unqual  o2) = o1 `compare` o2
248     compare (Unqual _)   _            = LT
249
250     compare (Qual _ _)   (Exact _)    = GT
251     compare (Qual _ _)   (Unqual _)   = GT
252     compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
253     compare (Qual _ _)   (Orig _ _)   = LT
254
255     compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
256     compare (Orig _ _)   _            = GT
257 \end{code}
258
259
260
261 %************************************************************************
262 %*                                                                      *
263                         LocalRdrEnv
264 %*                                                                      *
265 %************************************************************************
266
267 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
268 It is keyed by OccName, because we never use it for qualified names.
269
270 \begin{code}
271 type LocalRdrEnv = OccEnv Name
272
273 emptyLocalRdrEnv :: LocalRdrEnv
274 emptyLocalRdrEnv = emptyOccEnv
275
276 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
277 extendLocalRdrEnv env names
278   = extendOccEnvList env [(nameOccName n, n) | n <- names]
279
280 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
281 lookupLocalRdrEnv _   (Exact name) = Just name
282 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
283 lookupLocalRdrEnv _   _            = Nothing
284
285 lookupLocalRdrOcc :: LocalRdrEnv -> OccName -> Maybe Name
286 lookupLocalRdrOcc env occ = lookupOccEnv env occ
287
288 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
289 elemLocalRdrEnv rdr_name env 
290   | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
291   | otherwise         = False
292 \end{code}
293
294
295 %************************************************************************
296 %*                                                                      *
297                         GlobalRdrEnv
298 %*                                                                      *
299 %************************************************************************
300
301 \begin{code}
302 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
303         -- Keyed by OccName; when looking up a qualified name
304         -- we look up the OccName part, and then check the Provenance
305         -- to see if the appropriate qualification is valid.  This
306         -- saves routinely doubling the size of the env by adding both
307         -- qualified and unqualified names to the domain.
308         --
309         -- The list in the range is reqd because there may be name clashes
310         -- These only get reported on lookup, not on construction
311
312         -- INVARIANT: All the members of the list have distinct 
313         --            gre_name fields; that is, no duplicate Names
314
315 data GlobalRdrElt 
316   = GRE { gre_name :: Name,
317           gre_par  :: Parent,
318           gre_prov :: Provenance        -- Why it's in scope
319     }
320
321 data Parent = NoParent | ParentIs Name
322               deriving (Eq)
323
324 instance Outputable Parent where
325    ppr NoParent     = empty
326    ppr (ParentIs n) = ptext (sLit "parent:") <> ppr n
327    
328
329 plusParent :: Parent -> Parent -> Parent
330 plusParent p1 p2 = ASSERT2( p1 == p2, parens (ppr p1) <+> parens (ppr p2) )
331                    p1
332
333 {- Why so complicated? -=chak
334 plusParent :: Parent -> Parent -> Parent
335 plusParent NoParent     rel = 
336   ASSERT2( case rel of { NoParent -> True; other -> False }, 
337            ptext (sLit "plusParent[NoParent]: ") <+> ppr rel )    
338   NoParent
339 plusParent (ParentIs n) rel = 
340   ASSERT2( case rel of { ParentIs m -> n==m;  other -> False }, 
341            ptext (sLit "plusParent[ParentIs]:") <+> ppr n <> comma <+> ppr rel )
342   ParentIs n
343  -}
344
345 emptyGlobalRdrEnv :: GlobalRdrEnv
346 emptyGlobalRdrEnv = emptyOccEnv
347
348 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
349 globalRdrEnvElts env = foldOccEnv (++) [] env
350
351 instance Outputable GlobalRdrElt where
352   ppr gre = ppr name <+> parens (ppr (gre_par gre) <+> pprNameProvenance gre)
353           where
354             name = gre_name gre
355
356 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
357 pprGlobalRdrEnv env
358   = vcat (map pp (occEnvElts env))
359   where
360     pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+> 
361               vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
362                    | gre <- gres]
363 \end{code}
364
365 \begin{code}
366 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
367 lookupGlobalRdrEnv env occ_name = case lookupOccEnv env occ_name of
368                                         Nothing   -> []
369                                         Just gres -> gres
370
371 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
372 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
373   where
374     occ = nameOccName (gre_name gre)
375     add gres _ = gre:gres
376
377 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
378 lookupGRE_RdrName rdr_name env
379   = case lookupOccEnv env (rdrNameOcc rdr_name) of
380         Nothing   -> []
381         Just gres -> pickGREs rdr_name gres
382
383 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
384 lookupGRE_Name env name
385   = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
386             gre_name gre == name ]
387
388 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
389 getGRE_NameQualifier_maybes env
390   = map qualifier_maybe . map gre_prov . lookupGRE_Name env
391   where qualifier_maybe LocalDef       = Nothing
392         qualifier_maybe (Imported iss) = Just $ map (is_as . is_decl) iss 
393
394 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
395 -- Take a list of GREs which have the right OccName
396 -- Pick those GREs that are suitable for this RdrName
397 -- And for those, keep only only the Provenances that are suitable
398 -- 
399 -- Consider
400 --       module A ( f ) where
401 --       import qualified Foo( f )
402 --       import Baz( f )
403 --       f = undefined
404 -- Let's suppose that Foo.f and Baz.f are the same entity really.
405 -- The export of f is ambiguous because it's in scope from the local def
406 -- and the import.  The lookup of (Unqual f) should return a GRE for
407 -- the locally-defined f, and a GRE for the imported f, with a *single* 
408 -- provenance, namely the one for Baz(f).
409 pickGREs rdr_name gres
410   = mapCatMaybes pick gres
411   where
412     rdr_is_unqual = isUnqual rdr_name
413     rdr_is_qual   = isQual_maybe rdr_name
414
415     pick :: GlobalRdrElt -> Maybe GlobalRdrElt
416     pick gre@(GRE {gre_prov = LocalDef, gre_name = n})  -- Local def
417         | rdr_is_unqual                         = Just gre
418         | Just (mod,_) <- rdr_is_qual, 
419           mod == moduleName (nameModule n)      = Just gre
420         | otherwise                             = Nothing
421     pick gre@(GRE {gre_prov = Imported [is]})   -- Single import (efficiency)
422         | rdr_is_unqual,
423           not (is_qual (is_decl is))            = Just gre
424         | Just (mod,_) <- rdr_is_qual, 
425           mod == is_as (is_decl is)             = Just gre
426         | otherwise                             = Nothing
427     pick gre@(GRE {gre_prov = Imported is})     -- Multiple import
428         | null filtered_is = Nothing
429         | otherwise        = Just (gre {gre_prov = Imported filtered_is})
430         where
431           filtered_is | rdr_is_unqual
432                       = filter (not . is_qual    . is_decl) is
433                       | Just (mod,_) <- rdr_is_qual 
434                       = filter ((== mod) . is_as . is_decl) is
435                       | otherwise
436                       = []
437
438 isLocalGRE :: GlobalRdrElt -> Bool
439 isLocalGRE (GRE {gre_prov = LocalDef}) = True
440 isLocalGRE _                           = False
441
442 unQualOK :: GlobalRdrElt -> Bool
443 -- An unqualifed version of this thing is in scope
444 unQualOK (GRE {gre_prov = LocalDef})    = True
445 unQualOK (GRE {gre_prov = Imported is}) = any unQualSpecOK is
446
447 unQualSpecOK :: ImportSpec -> Bool
448 -- In scope unqualified
449 unQualSpecOK is = not (is_qual (is_decl is))
450
451 qualSpecOK :: ModuleName -> ImportSpec -> Bool
452 -- In scope qualified with M
453 qualSpecOK mod is = mod == is_as (is_decl is)
454
455 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
456 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
457
458 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
459 mkGlobalRdrEnv gres
460   = foldr add emptyGlobalRdrEnv gres
461   where
462     add gre env = extendOccEnv_C (foldr insertGRE) env 
463                                  (nameOccName (gre_name gre)) 
464                                  [gre]
465
466 findLocalDupsRdrEnv :: GlobalRdrEnv -> [OccName] -> (GlobalRdrEnv, [[Name]])
467 -- For each OccName, see if there are multiple LocalDef definitions
468 -- for it.  If so, remove all but one (to suppress subsequent error messages)
469 -- and return a list of the duplicate bindings
470 findLocalDupsRdrEnv rdr_env occs 
471   = go rdr_env [] occs
472   where
473     go rdr_env dups [] = (rdr_env, dups)
474     go rdr_env dups (occ:occs)
475       = case filter isLocalGRE gres of
476           []       -> WARN( True, ppr occ <+> ppr rdr_env ) 
477                       go rdr_env dups occs      -- Weird!  No binding for occ
478           [_]      -> go rdr_env dups occs      -- The common case
479           dup_gres -> go (extendOccEnv rdr_env occ (head dup_gres : nonlocal_gres))
480                          (map gre_name dup_gres : dups)
481                          occs
482       where
483         gres = lookupOccEnv rdr_env occ `orElse` []
484         nonlocal_gres = filterOut isLocalGRE gres
485
486
487 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
488 insertGRE new_g [] = [new_g]
489 insertGRE new_g (old_g : old_gs)
490         | gre_name new_g == gre_name old_g
491         = new_g `plusGRE` old_g : old_gs
492         | otherwise
493         = old_g : insertGRE new_g old_gs
494
495 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
496 -- Used when the gre_name fields match
497 plusGRE g1 g2
498   = GRE { gre_name = gre_name g1,
499           gre_prov = gre_prov g1 `plusProv`   gre_prov g2,
500           gre_par  = gre_par  g1 `plusParent` gre_par  g2 }
501
502 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
503 -- Hide any unqualified bindings for the specified OccNames
504 -- This is used in TH, when renaming a declaration bracket
505 --      [d| foo = ... |]
506 -- We want unqualified 'foo' in "..." to mean this foo, not
507 -- the one from the enclosing module.  But the *qualified* name
508 -- from the enclosing moudule must certainly still be avaialable
509 --      Seems like 5 times as much work as it deserves!
510 hideSomeUnquals rdr_env occs
511   = foldr hide rdr_env occs
512   where
513     hide occ env 
514         | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
515         | otherwise                         = env
516     qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
517         = gre { gre_prov = Imported [imp_spec] }
518         where   -- Local defs get transfomed to (fake) imported things
519           mod = moduleName (nameModule name)
520           imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
521           decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod, 
522                                     is_qual = True, 
523                                     is_dloc = srcLocSpan (nameSrcLoc name) }
524
525     qual_gre gre@(GRE { gre_prov = Imported specs })
526         = gre { gre_prov = Imported (map qual_spec specs) }
527
528     qual_spec spec@(ImpSpec { is_decl = decl_spec })
529         = spec { is_decl = decl_spec { is_qual = True } }
530 \end{code}
531
532
533 %************************************************************************
534 %*                                                                      *
535                         Provenance
536 %*                                                                      *
537 %************************************************************************
538
539 The "provenance" of something says how it came to be in scope.
540 It's quite elaborate so that we can give accurate unused-name warnings.
541
542 \begin{code}
543 data Provenance
544   = LocalDef            -- Defined locally
545   | Imported            -- Imported
546         [ImportSpec]    -- INVARIANT: non-empty
547
548 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
549                             is_item ::  ImpItemSpec }
550                 deriving( Eq, Ord )
551
552 data ImpDeclSpec        -- Describes a particular import declaration
553                         -- Shared among all the Provenaces for that decl
554   = ImpDeclSpec {
555         is_mod      :: ModuleName, -- 'import Muggle'
556                                 -- Note the Muggle may well not be 
557                                 -- the defining module for this thing!
558                                 -- TODO: either should be Module, or there
559                                 -- should be a Maybe PackageId here too.
560         is_as       :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
561         is_qual     :: Bool,    -- True <=> qualified (only)
562         is_dloc     :: SrcSpan  -- Location of import declaration
563     }
564
565 data ImpItemSpec  -- Describes import info a particular Name
566   = ImpAll              -- The import had no import list, 
567                         -- or  had a hiding list
568
569   | ImpSome {           -- The import had an import list
570         is_explicit :: Bool,
571         is_iloc     :: SrcSpan  -- Location of the import item
572     }
573         -- The is_explicit field is True iff the thing was
574         -- named /explicitly/ in the import specs rather
575         -- than being imported as part of a "..." group
576         -- e.g.         import C( T(..) )
577         -- Here the constructors of T are not named explicitly; 
578         -- only T is named explicitly.
579
580 importSpecLoc :: ImportSpec -> SrcSpan
581 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
582 importSpecLoc (ImpSpec _    item)   = is_iloc item
583
584 importSpecModule :: ImportSpec -> ModuleName
585 importSpecModule is = is_mod (is_decl is)
586
587 isExplicitItem :: ImpItemSpec -> Bool
588 isExplicitItem ImpAll                        = False
589 isExplicitItem (ImpSome {is_explicit = exp}) = exp
590
591 -- Note [Comparing provenance]
592 -- Comparison of provenance is just used for grouping 
593 -- error messages (in RnEnv.warnUnusedBinds)
594 instance Eq Provenance where
595   p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
596
597 instance Eq ImpDeclSpec where
598   p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
599
600 instance Eq ImpItemSpec where
601   p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
602
603 instance Ord Provenance where
604    compare LocalDef      LocalDef        = EQ
605    compare LocalDef      (Imported _)    = LT
606    compare (Imported _ ) LocalDef        = GT
607    compare (Imported is1) (Imported is2) = compare (head is1) 
608         {- See Note [Comparing provenance] -}      (head is2)
609
610 instance Ord ImpDeclSpec where
611    compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp` 
612                      (is_dloc is1 `compare` is_dloc is2)
613
614 instance Ord ImpItemSpec where
615    compare is1 is2 = is_iloc is1 `compare` is_iloc is2
616 \end{code}
617
618 \begin{code}
619 plusProv :: Provenance -> Provenance -> Provenance
620 -- Choose LocalDef over Imported
621 -- There is an obscure bug lurking here; in the presence
622 -- of recursive modules, something can be imported *and* locally
623 -- defined, and one might refer to it with a qualified name from
624 -- the import -- but I'm going to ignore that because it makes
625 -- the isLocalGRE predicate so much nicer this way
626 plusProv LocalDef        LocalDef        = panic "plusProv"
627 plusProv LocalDef        _               = LocalDef
628 plusProv _               LocalDef        = LocalDef
629 plusProv (Imported is1)  (Imported is2)  = Imported (is1++is2)
630
631 pprNameProvenance :: GlobalRdrElt -> SDoc
632 -- Print out the place where the name was imported
633 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
634   = ptext (sLit "defined at") <+> ppr (nameSrcLoc name)
635 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
636   = case whys of
637         (why:_) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
638         [] -> panic "pprNameProvenance"
639
640 -- If we know the exact definition point (which we may do with GHCi)
641 -- then show that too.  But not if it's just "imported from X".
642 ppr_defn :: SrcLoc -> SDoc
643 ppr_defn loc | isGoodSrcLoc loc = parens (ptext (sLit "defined at") <+> ppr loc)
644              | otherwise        = empty
645
646 instance Outputable ImportSpec where
647    ppr imp_spec
648      = ptext (sLit "imported from") <+> ppr (importSpecModule imp_spec) 
649         <+> if isGoodSrcSpan loc then ptext (sLit "at") <+> ppr loc
650                                  else empty
651      where
652        loc = importSpecLoc imp_spec
653 \end{code}