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