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