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