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