bfb11e0b198fd80f4afe3beacdb8bcb185cfb736
[ghc.git] / compiler / basicTypes / RdrName.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 -}
5
6 {-# LANGUAGE CPP, DeriveDataTypeable #-}
7
8 -- |
9 -- #name_types#
10 -- GHC uses several kinds of name internally:
11 --
12 -- * 'OccName.OccName': see "OccName#name_types"
13 --
14 -- * 'RdrName.RdrName' is the type of names that come directly from the parser. They
15 -- have not yet had their scoping and binding resolved by the renamer and can be
16 -- thought of to a first approximation as an 'OccName.OccName' with an optional module
17 -- qualifier
18 --
19 -- * 'Name.Name': see "Name#name_types"
20 --
21 -- * 'Id.Id': see "Id#name_types"
22 --
23 -- * 'Var.Var': see "Var#name_types"
24
25 module RdrName (
26 -- * The main type
27 RdrName(..), -- Constructors exported only to BinIface
28
29 -- ** Construction
30 mkRdrUnqual, mkRdrQual,
31 mkUnqual, mkVarUnqual, mkQual, mkOrig,
32 nameRdrName, getRdrName,
33
34 -- ** Destruction
35 rdrNameOcc, rdrNameSpace, demoteRdrName,
36 isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual,
37 isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
38
39 -- * Local mapping of 'RdrName' to 'Name.Name'
40 LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv, extendLocalRdrEnvList,
41 lookupLocalRdrEnv, lookupLocalRdrOcc,
42 elemLocalRdrEnv, inLocalRdrEnvScope,
43 localRdrEnvElts, delLocalRdrEnvList,
44
45 -- * Global mapping of 'RdrName' to 'GlobalRdrElt's
46 GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv,
47 lookupGlobalRdrEnv, extendGlobalRdrEnv, greOccName, shadowNames,
48 pprGlobalRdrEnv, globalRdrEnvElts,
49 lookupGRE_RdrName, lookupGRE_Name, lookupGRE_Field_Name, getGRE_NameQualifier_maybes,
50 transformGREs, pickGREs, pickGREsModExp,
51
52 -- * GlobalRdrElts
53 gresFromAvails, gresFromAvail, localGREsFromAvail, availFromGRE,
54 greUsedRdrName, greRdrNames, greSrcSpan, greQualModName,
55
56 -- ** Global 'RdrName' mapping elements: 'GlobalRdrElt', 'Provenance', 'ImportSpec'
57 GlobalRdrElt(..), isLocalGRE, isRecFldGRE, greLabel,
58 unQualOK, qualSpecOK, unQualSpecOK,
59 pprNameProvenance,
60 Parent(..),
61 ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
62 importSpecLoc, importSpecModule, isExplicitItem, bestImport
63 ) where
64
65 #include "HsVersions.h"
66
67 import Module
68 import Name
69 import Avail
70 import NameSet
71 import Maybes
72 import SrcLoc
73 import FastString
74 import FieldLabel
75 import Outputable
76 import Unique
77 import Util
78 import StaticFlags( opt_PprStyle_Debug )
79
80 import Data.Data
81 import Data.List( sortBy )
82
83 {-
84 ************************************************************************
85 * *
86 \subsection{The main data type}
87 * *
88 ************************************************************************
89 -}
90
91 -- | Do not use the data constructors of RdrName directly: prefer the family
92 -- of functions that creates them, such as 'mkRdrUnqual'
93 --
94 -- - Note: A Located RdrName will only have API Annotations if it is a
95 -- compound one,
96 -- e.g.
97 --
98 -- > `bar`
99 -- > ( ~ )
100 --
101 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnType',
102 -- 'ApiAnnotation.AnnOpen' @'('@ or @'['@ or @'[:'@,
103 -- 'ApiAnnotation.AnnClose' @')'@ or @']'@ or @':]'@,,
104 -- 'ApiAnnotation.AnnBackquote' @'`'@,
105 -- 'ApiAnnotation.AnnVal','ApiAnnotation.AnnTildehsh',
106 -- 'ApiAnnotation.AnnTilde',
107
108 -- For details on above see note [Api annotations] in ApiAnnotation
109 data RdrName
110 = Unqual OccName
111 -- ^ Used for ordinary, unqualified occurrences, e.g. @x@, @y@ or @Foo@.
112 -- Create such a 'RdrName' with 'mkRdrUnqual'
113
114 | Qual ModuleName OccName
115 -- ^ A qualified name written by the user in
116 -- /source/ code. The module isn't necessarily
117 -- the module where the thing is defined;
118 -- just the one from which it is imported.
119 -- Examples are @Bar.x@, @Bar.y@ or @Bar.Foo@.
120 -- Create such a 'RdrName' with 'mkRdrQual'
121
122 | Orig Module OccName
123 -- ^ An original name; the module is the /defining/ module.
124 -- This is used when GHC generates code that will be fed
125 -- into the renamer (e.g. from deriving clauses), but where
126 -- we want to say \"Use Prelude.map dammit\". One of these
127 -- can be created with 'mkOrig'
128
129 | Exact Name
130 -- ^ We know exactly the 'Name'. This is used:
131 --
132 -- (1) When the parser parses built-in syntax like @[]@
133 -- and @(,)@, but wants a 'RdrName' from it
134 --
135 -- (2) By Template Haskell, when TH has generated a unique name
136 --
137 -- Such a 'RdrName' can be created by using 'getRdrName' on a 'Name'
138 deriving (Data, Typeable)
139
140 {-
141 ************************************************************************
142 * *
143 \subsection{Simple functions}
144 * *
145 ************************************************************************
146 -}
147
148 instance HasOccName RdrName where
149 occName = rdrNameOcc
150
151 rdrNameOcc :: RdrName -> OccName
152 rdrNameOcc (Qual _ occ) = occ
153 rdrNameOcc (Unqual occ) = occ
154 rdrNameOcc (Orig _ occ) = occ
155 rdrNameOcc (Exact name) = nameOccName name
156
157 rdrNameSpace :: RdrName -> NameSpace
158 rdrNameSpace = occNameSpace . rdrNameOcc
159
160 -- demoteRdrName lowers the NameSpace of RdrName.
161 -- see Note [Demotion] in OccName
162 demoteRdrName :: RdrName -> Maybe RdrName
163 demoteRdrName (Unqual occ) = fmap Unqual (demoteOccName occ)
164 demoteRdrName (Qual m occ) = fmap (Qual m) (demoteOccName occ)
165 demoteRdrName (Orig _ _) = panic "demoteRdrName"
166 demoteRdrName (Exact _) = panic "demoteRdrName"
167
168 -- These two are the basic constructors
169 mkRdrUnqual :: OccName -> RdrName
170 mkRdrUnqual occ = Unqual occ
171
172 mkRdrQual :: ModuleName -> OccName -> RdrName
173 mkRdrQual mod occ = Qual mod occ
174
175 mkOrig :: Module -> OccName -> RdrName
176 mkOrig mod occ = Orig mod occ
177
178 ---------------
179 -- These two are used when parsing source files
180 -- They do encode the module and occurrence names
181 mkUnqual :: NameSpace -> FastString -> RdrName
182 mkUnqual sp n = Unqual (mkOccNameFS sp n)
183
184 mkVarUnqual :: FastString -> RdrName
185 mkVarUnqual n = Unqual (mkVarOccFS n)
186
187 -- | Make a qualified 'RdrName' in the given namespace and where the 'ModuleName' and
188 -- the 'OccName' are taken from the first and second elements of the tuple respectively
189 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
190 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
191
192 getRdrName :: NamedThing thing => thing -> RdrName
193 getRdrName name = nameRdrName (getName name)
194
195 nameRdrName :: Name -> RdrName
196 nameRdrName name = Exact name
197 -- Keep the Name even for Internal names, so that the
198 -- unique is still there for debug printing, particularly
199 -- of Types (which are converted to IfaceTypes before printing)
200
201 nukeExact :: Name -> RdrName
202 nukeExact n
203 | isExternalName n = Orig (nameModule n) (nameOccName n)
204 | otherwise = Unqual (nameOccName n)
205
206 isRdrDataCon :: RdrName -> Bool
207 isRdrTyVar :: RdrName -> Bool
208 isRdrTc :: RdrName -> Bool
209
210 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
211 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
212 isRdrTc rn = isTcOcc (rdrNameOcc rn)
213
214 isSrcRdrName :: RdrName -> Bool
215 isSrcRdrName (Unqual _) = True
216 isSrcRdrName (Qual _ _) = True
217 isSrcRdrName _ = False
218
219 isUnqual :: RdrName -> Bool
220 isUnqual (Unqual _) = True
221 isUnqual _ = False
222
223 isQual :: RdrName -> Bool
224 isQual (Qual _ _) = True
225 isQual _ = False
226
227 isQual_maybe :: RdrName -> Maybe (ModuleName, OccName)
228 isQual_maybe (Qual m n) = Just (m,n)
229 isQual_maybe _ = Nothing
230
231 isOrig :: RdrName -> Bool
232 isOrig (Orig _ _) = True
233 isOrig _ = False
234
235 isOrig_maybe :: RdrName -> Maybe (Module, OccName)
236 isOrig_maybe (Orig m n) = Just (m,n)
237 isOrig_maybe _ = Nothing
238
239 isExact :: RdrName -> Bool
240 isExact (Exact _) = True
241 isExact _ = False
242
243 isExact_maybe :: RdrName -> Maybe Name
244 isExact_maybe (Exact n) = Just n
245 isExact_maybe _ = Nothing
246
247 {-
248 ************************************************************************
249 * *
250 \subsection{Instances}
251 * *
252 ************************************************************************
253 -}
254
255 instance Outputable RdrName where
256 ppr (Exact name) = ppr name
257 ppr (Unqual occ) = ppr occ
258 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
259 ppr (Orig mod occ) = getPprStyle (\sty -> pprModulePrefix sty mod occ <> ppr occ)
260
261 instance OutputableBndr RdrName where
262 pprBndr _ n
263 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
264 | otherwise = ppr n
265
266 pprInfixOcc rdr = pprInfixVar (isSymOcc (rdrNameOcc rdr)) (ppr rdr)
267 pprPrefixOcc rdr
268 | Just name <- isExact_maybe rdr = pprPrefixName name
269 -- pprPrefixName has some special cases, so
270 -- we delegate to them rather than reproduce them
271 | otherwise = pprPrefixVar (isSymOcc (rdrNameOcc rdr)) (ppr rdr)
272
273 instance Eq RdrName where
274 (Exact n1) == (Exact n2) = n1==n2
275 -- Convert exact to orig
276 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
277 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
278
279 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
280 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
281 (Unqual o1) == (Unqual o2) = o1==o2
282 _ == _ = False
283
284 instance Ord RdrName where
285 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
286 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
287 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
288 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
289
290 -- Exact < Unqual < Qual < Orig
291 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
292 -- before comparing so that Prelude.map == the exact Prelude.map, but
293 -- that meant that we reported duplicates when renaming bindings
294 -- generated by Template Haskell; e.g
295 -- do { n1 <- newName "foo"; n2 <- newName "foo";
296 -- <decl involving n1,n2> }
297 -- I think we can do without this conversion
298 compare (Exact n1) (Exact n2) = n1 `compare` n2
299 compare (Exact _) _ = LT
300
301 compare (Unqual _) (Exact _) = GT
302 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
303 compare (Unqual _) _ = LT
304
305 compare (Qual _ _) (Exact _) = GT
306 compare (Qual _ _) (Unqual _) = GT
307 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
308 compare (Qual _ _) (Orig _ _) = LT
309
310 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
311 compare (Orig _ _) _ = GT
312
313 {-
314 ************************************************************************
315 * *
316 LocalRdrEnv
317 * *
318 ************************************************************************
319 -}
320
321 -- | This environment is used to store local bindings (@let@, @where@, lambda, @case@).
322 -- It is keyed by OccName, because we never use it for qualified names
323 -- We keep the current mapping, *and* the set of all Names in scope
324 -- Reason: see Note [Splicing Exact Names] in RnEnv
325 data LocalRdrEnv = LRE { lre_env :: OccEnv Name
326 , lre_in_scope :: NameSet }
327
328 instance Outputable LocalRdrEnv where
329 ppr (LRE {lre_env = env, lre_in_scope = ns})
330 = hang (ptext (sLit "LocalRdrEnv {"))
331 2 (vcat [ ptext (sLit "env =") <+> pprOccEnv ppr_elt env
332 , ptext (sLit "in_scope =") <+> braces (pprWithCommas ppr (nameSetElems ns))
333 ] <+> char '}')
334 where
335 ppr_elt name = parens (ppr (getUnique (nameOccName name))) <+> ppr name
336 -- So we can see if the keys line up correctly
337
338 emptyLocalRdrEnv :: LocalRdrEnv
339 emptyLocalRdrEnv = LRE { lre_env = emptyOccEnv, lre_in_scope = emptyNameSet }
340
341 extendLocalRdrEnv :: LocalRdrEnv -> Name -> LocalRdrEnv
342 -- The Name should be a non-top-level thing
343 extendLocalRdrEnv (LRE { lre_env = env, lre_in_scope = ns }) name
344 = WARN( isExternalName name, ppr name )
345 LRE { lre_env = extendOccEnv env (nameOccName name) name
346 , lre_in_scope = extendNameSet ns name }
347
348 extendLocalRdrEnvList :: LocalRdrEnv -> [Name] -> LocalRdrEnv
349 extendLocalRdrEnvList (LRE { lre_env = env, lre_in_scope = ns }) names
350 = WARN( any isExternalName names, ppr names )
351 LRE { lre_env = extendOccEnvList env [(nameOccName n, n) | n <- names]
352 , lre_in_scope = extendNameSetList ns names }
353
354 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
355 lookupLocalRdrEnv (LRE { lre_env = env }) (Unqual occ) = lookupOccEnv env occ
356 lookupLocalRdrEnv _ _ = Nothing
357
358 lookupLocalRdrOcc :: LocalRdrEnv -> OccName -> Maybe Name
359 lookupLocalRdrOcc (LRE { lre_env = env }) occ = lookupOccEnv env occ
360
361 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
362 elemLocalRdrEnv rdr_name (LRE { lre_env = env, lre_in_scope = ns })
363 = case rdr_name of
364 Unqual occ -> occ `elemOccEnv` env
365 Exact name -> name `elemNameSet` ns -- See Note [Local bindings with Exact Names]
366 Qual {} -> False
367 Orig {} -> False
368
369 localRdrEnvElts :: LocalRdrEnv -> [Name]
370 localRdrEnvElts (LRE { lre_env = env }) = occEnvElts env
371
372 inLocalRdrEnvScope :: Name -> LocalRdrEnv -> Bool
373 -- This is the point of the NameSet
374 inLocalRdrEnvScope name (LRE { lre_in_scope = ns }) = name `elemNameSet` ns
375
376 delLocalRdrEnvList :: LocalRdrEnv -> [OccName] -> LocalRdrEnv
377 delLocalRdrEnvList (LRE { lre_env = env, lre_in_scope = ns }) occs
378 = LRE { lre_env = delListFromOccEnv env occs
379 , lre_in_scope = ns }
380
381 {-
382 Note [Local bindings with Exact Names]
383 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
384 With Template Haskell we can make local bindings that have Exact Names.
385 Computing shadowing etc may use elemLocalRdrEnv (at least it certainly
386 does so in RnTpes.bindHsTyVars), so for an Exact Name we must consult
387 the in-scope-name-set.
388
389
390 ************************************************************************
391 * *
392 GlobalRdrEnv
393 * *
394 ************************************************************************
395 -}
396
397 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
398 -- ^ Keyed by 'OccName'; when looking up a qualified name
399 -- we look up the 'OccName' part, and then check the 'Provenance'
400 -- to see if the appropriate qualification is valid. This
401 -- saves routinely doubling the size of the env by adding both
402 -- qualified and unqualified names to the domain.
403 --
404 -- The list in the codomain is required because there may be name clashes
405 -- These only get reported on lookup, not on construction
406 --
407 -- INVARIANT 1: All the members of the list have distinct
408 -- 'gre_name' fields; that is, no duplicate Names
409 --
410 -- INVARIANT 2: Imported provenance => Name is an ExternalName
411 -- However LocalDefs can have an InternalName. This
412 -- happens only when type-checking a [d| ... |] Template
413 -- Haskell quotation; see this note in RnNames
414 -- Note [Top-level Names in Template Haskell decl quotes]
415
416 -- | An element of the 'GlobalRdrEnv'
417 data GlobalRdrElt
418 = GRE { gre_name :: Name
419 , gre_par :: Parent
420 , gre_lcl :: Bool -- ^ True <=> the thing was defined locally
421 , gre_imp :: [ImportSpec] -- ^ In scope through these imports
422 } -- INVARIANT: either gre_lcl = True or gre_imp is non-empty
423 -- See Note [GlobalRdrElt provenance]
424
425 -- | The children of a Name are the things that are abbreviated by the ".."
426 -- notation in export lists. See Note [Parents]
427 data Parent = NoParent
428 | ParentIs { par_is :: Name }
429 | FldParent { par_is :: Name, par_lbl :: Maybe FieldLabelString }
430 -- ^ See Note [Parents for record fields]
431 | PatternSynonym
432 deriving (Eq)
433
434 instance Outputable Parent where
435 ppr NoParent = empty
436 ppr (ParentIs n) = ptext (sLit "parent:") <> ppr n
437 ppr (FldParent n f) = ptext (sLit "fldparent:")
438 <> ppr n <> colon <> ppr f
439 ppr (PatternSynonym) = ptext (sLit "pattern synonym")
440
441 plusParent :: Parent -> Parent -> Parent
442 -- See Note [Combining parents]
443 plusParent p1@(ParentIs _) p2 = hasParent p1 p2
444 plusParent p1@(FldParent _ _) p2 = hasParent p1 p2
445 plusParent p1 p2@(ParentIs _) = hasParent p2 p1
446 plusParent p1 p2@(FldParent _ _) = hasParent p2 p1
447 plusParent PatternSynonym PatternSynonym = PatternSynonym
448 plusParent _ _ = NoParent
449
450 hasParent :: Parent -> Parent -> Parent
451 #ifdef DEBUG
452 hasParent p NoParent = p
453 hasParent p p'
454 | p /= p' = pprPanic "hasParent" (ppr p <+> ppr p') -- Parents should agree
455 #endif
456 hasParent p _ = p
457
458
459 {- Note [GlobalRdrElt provenance]
460 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
461 The gre_lcl and gre_imp fields of a GlobalRdrElt describe its "provenance",
462 i.e. how the Name came to be in scope. It can be in scope two ways:
463 - gre_lcl = True: it is bound in this module
464 - gre_imp: a list of all the imports that brought it into scope
465
466 It's an INVARIANT that you have one or the other; that is, either
467 gre_lcl is True, or gre_imp is non-empty.
468
469 It is just possible to have *both* if there is a module loop: a Name
470 is defined locally in A, and also brought into scope by importing a
471 module that SOURCE-imported A. Exapmle (Trac #7672):
472
473 A.hs-boot module A where
474 data T
475
476 B.hs module B(Decl.T) where
477 import {-# SOURCE #-} qualified A as Decl
478
479 A.hs module A where
480 import qualified B
481 data T = Z | S B.T
482
483 In A.hs, 'T' is locally bound, *and* imported as B.T.
484
485 Note [Parents]
486 ~~~~~~~~~~~~~~~~~
487 Parent Children
488 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
489 data T Data constructors
490 Record-field ids
491
492 data family T Data constructors and record-field ids
493 of all visible data instances of T
494
495 class C Class operations
496 Associated type constructors
497
498 The `PatternSynonym` constructor is so called as pattern synonyms can be
499 bundled with any type constructor (during renaming). In other words, they can
500 have any parent.
501
502 ~~~~~~~~~~~~~~~~~~~~~~~~~
503 Constructor Meaning
504 ~~~~~~~~~~~~~~~~~~~~~~~~
505 NoParent Can not be bundled with a type constructor.
506 ParentIs n Can be bundled with the type constructor corresponding to
507 n.
508 PatternSynonym Can be bundled with any type constructor. It is so called
509 because only pattern synonyms can be bundled with any type
510 constructor.
511 FldParent See Note [Parents for record fields]
512
513
514
515
516 Note [Parents for record fields]
517 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
518
519 For record fields, in addition to the Name of the type constructor
520 (stored in par_is), we use FldParent to store the field label. This
521 extra information is used for identifying overloaded record fields
522 during renaming.
523
524 In a definition arising from a normal module (without
525 -XDuplicateRecordFields), par_lbl will be Nothing, meaning that the
526 field's label is the same as the OccName of the selector's Name. The
527 GlobalRdrEnv will contain an entry like this:
528
529 "x" |-> GRE x (FldParent T Nothing) LocalDef
530
531 When -XDuplicateRecordFields is enabled for the module that contains
532 T, the selector's Name will be mangled (see comments in FieldLabel).
533 Thus we store the actual field label in par_lbl, and the GlobalRdrEnv
534 entry looks like this:
535
536 "x" |-> GRE $sel:x:MkT (FldParent T (Just "x")) LocalDef
537
538 Note that the OccName used when adding a GRE to the environment
539 (greOccName) now depends on the parent field: for FldParent it is the
540 field label, if present, rather than the selector name.
541
542
543 Note [Combining parents]
544 ~~~~~~~~~~~~~~~~~~~~~~~~
545 With an associated type we might have
546 module M where
547 class C a where
548 data T a
549 op :: T a -> a
550 instance C Int where
551 data T Int = TInt
552 instance C Bool where
553 data T Bool = TBool
554
555 Then: C is the parent of T
556 T is the parent of TInt and TBool
557 So: in an export list
558 C(..) is short for C( op, T )
559 T(..) is short for T( TInt, TBool )
560
561 Module M exports everything, so its exports will be
562 AvailTC C [C,T,op]
563 AvailTC T [T,TInt,TBool]
564 On import we convert to GlobalRdrElt and then combine
565 those. For T that will mean we have
566 one GRE with Parent C
567 one GRE with NoParent
568 That's why plusParent picks the "best" case.
569 -}
570
571 -- | make a 'GlobalRdrEnv' where all the elements point to the same
572 -- Provenance (useful for "hiding" imports, or imports with no details).
573 gresFromAvails :: Maybe ImportSpec -> [AvailInfo] -> [GlobalRdrElt]
574 -- prov = Nothing => locally bound
575 -- Just spec => imported as described by spec
576 gresFromAvails prov avails
577 = concatMap (gresFromAvail (const prov)) avails
578
579 localGREsFromAvail :: AvailInfo -> [GlobalRdrElt]
580 -- Turn an Avail into a list of LocalDef GlobalRdrElts
581 localGREsFromAvail = gresFromAvail (const Nothing)
582
583 gresFromAvail :: (Name -> Maybe ImportSpec) -> AvailInfo -> [GlobalRdrElt]
584 gresFromAvail prov_fn avail
585 = map mk_gre (availNonFldNames avail) ++ map mk_fld_gre (availFlds avail)
586 where
587 mk_gre n
588 = case prov_fn n of -- Nothing => bound locally
589 -- Just is => imported from 'is'
590 Nothing -> GRE { gre_name = n, gre_par = mkParent n avail
591 , gre_lcl = True, gre_imp = [] }
592 Just is -> GRE { gre_name = n, gre_par = mkParent n avail
593 , gre_lcl = False, gre_imp = [is] }
594
595 mk_fld_gre (FieldLabel lbl is_overloaded n)
596 = case prov_fn n of -- Nothing => bound locally
597 -- Just is => imported from 'is'
598 Nothing -> GRE { gre_name = n, gre_par = FldParent (availName avail) mb_lbl
599 , gre_lcl = True, gre_imp = [] }
600 Just is -> GRE { gre_name = n, gre_par = FldParent (availName avail) mb_lbl
601 , gre_lcl = False, gre_imp = [is] }
602 where
603 mb_lbl | is_overloaded = Just lbl
604 | otherwise = Nothing
605
606
607 greQualModName :: GlobalRdrElt -> ModuleName
608 -- Get a suitable module qualifier for the GRE
609 -- (used in mkPrintUnqualified)
610 -- Prerecondition: the gre_name is always External
611 greQualModName gre@(GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss })
612 | lcl, Just mod <- nameModule_maybe name = moduleName mod
613 | (is:_) <- iss = is_as (is_decl is)
614 | otherwise = pprPanic "greQualModName" (ppr gre)
615
616 greUsedRdrName :: GlobalRdrElt -> RdrName
617 -- For imported things, return a RdrName to add to the used-RdrName
618 -- set, which is used to generate unused-import-decl warnings.
619 -- Return a Qual RdrName if poss, so that identifies the most
620 -- specific ImportSpec. See Trac #10890 for some good examples.
621 greUsedRdrName gre@GRE{ gre_name = name, gre_lcl = lcl, gre_imp = iss }
622 | lcl, Just mod <- nameModule_maybe name = Qual (moduleName mod) occ
623 | not (null iss), is <- bestImport iss = Qual (is_as (is_decl is)) occ
624 | otherwise = pprTrace "greUsedRdrName" (ppr gre) (Unqual occ)
625 where
626 occ = greOccName gre
627
628 greRdrNames :: GlobalRdrElt -> [RdrName]
629 greRdrNames GRE{ gre_name = name, gre_lcl = lcl, gre_imp = iss }
630 = (if lcl then [unqual] else []) ++ concatMap do_spec (map is_decl iss)
631 where
632 occ = nameOccName name
633 unqual = Unqual occ
634 do_spec decl_spec
635 | is_qual decl_spec = [qual]
636 | otherwise = [unqual,qual]
637 where qual = Qual (is_as decl_spec) occ
638
639 -- the SrcSpan that pprNameProvenance prints out depends on whether
640 -- the Name is defined locally or not: for a local definition the
641 -- definition site is used, otherwise the location of the import
642 -- declaration. We want to sort the export locations in
643 -- exportClashErr by this SrcSpan, we need to extract it:
644 greSrcSpan :: GlobalRdrElt -> SrcSpan
645 greSrcSpan gre@(GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss } )
646 | lcl = nameSrcSpan name
647 | (is:_) <- iss = is_dloc (is_decl is)
648 | otherwise = pprPanic "greSrcSpan" (ppr gre)
649
650 mkParent :: Name -> AvailInfo -> Parent
651 mkParent _ (Avail NotPatSyn _) = NoParent
652 mkParent _ (Avail IsPatSyn _) = PatternSynonym
653 mkParent n (AvailTC m _ _) | n == m = NoParent
654 | otherwise = ParentIs m
655
656 availFromGRE :: GlobalRdrElt -> AvailInfo
657 availFromGRE (GRE { gre_name = me, gre_par = parent })
658 = case parent of
659 ParentIs p -> AvailTC p [me] []
660 NoParent | isTyConName me -> AvailTC me [me] []
661 | otherwise -> avail me
662 FldParent p Nothing -> AvailTC p [] [FieldLabel (occNameFS $ nameOccName me) False me]
663 FldParent p (Just lbl) -> AvailTC p [] [FieldLabel lbl True me]
664 PatternSynonym -> patSynAvail me
665
666 emptyGlobalRdrEnv :: GlobalRdrEnv
667 emptyGlobalRdrEnv = emptyOccEnv
668
669 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
670 globalRdrEnvElts env = foldOccEnv (++) [] env
671
672 instance Outputable GlobalRdrElt where
673 ppr gre = hang (ppr (gre_name gre) <+> ppr (gre_par gre))
674 2 (pprNameProvenance gre)
675
676 pprGlobalRdrEnv :: Bool -> GlobalRdrEnv -> SDoc
677 pprGlobalRdrEnv locals_only env
678 = vcat [ ptext (sLit "GlobalRdrEnv") <+> ppWhen locals_only (ptext (sLit "(locals only)"))
679 <+> lbrace
680 , nest 2 (vcat [ pp (remove_locals gre_list) | gre_list <- occEnvElts env ]
681 <+> rbrace) ]
682 where
683 remove_locals gres | locals_only = filter isLocalGRE gres
684 | otherwise = gres
685 pp [] = empty
686 pp gres = hang (ppr occ
687 <+> parens (ptext (sLit "unique") <+> ppr (getUnique occ))
688 <> colon)
689 2 (vcat (map ppr gres))
690 where
691 occ = nameOccName (gre_name (head gres))
692
693 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
694 lookupGlobalRdrEnv env occ_name = case lookupOccEnv env occ_name of
695 Nothing -> []
696 Just gres -> gres
697
698 greOccName :: GlobalRdrElt -> OccName
699 greOccName (GRE{gre_par = FldParent{par_lbl = Just lbl}}) = mkVarOccFS lbl
700 greOccName gre = nameOccName (gre_name gre)
701
702 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
703 lookupGRE_RdrName rdr_name env
704 = case lookupOccEnv env (rdrNameOcc rdr_name) of
705 Nothing -> []
706 Just gres -> pickGREs rdr_name gres
707
708 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
709 lookupGRE_Name env name
710 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
711 gre_name gre == name ]
712
713 lookupGRE_Field_Name :: GlobalRdrEnv -> Name -> FastString -> [GlobalRdrElt]
714 -- Used when looking up record fields, where the selector name and
715 -- field label are different: the GlobalRdrEnv is keyed on the label
716 lookupGRE_Field_Name env sel_name lbl
717 = [ gre | gre <- lookupGlobalRdrEnv env (mkVarOccFS lbl),
718 gre_name gre == sel_name ]
719
720
721 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
722 -- Returns all the qualifiers by which 'x' is in scope
723 -- Nothing means "the unqualified version is in scope"
724 -- [] means the thing is not in scope at all
725 getGRE_NameQualifier_maybes env
726 = map (qualifier_maybe) . lookupGRE_Name env
727 where
728 qualifier_maybe (GRE { gre_lcl = lcl, gre_imp = iss })
729 | lcl = Nothing
730 | otherwise = Just $ map (is_as . is_decl) iss
731
732 isLocalGRE :: GlobalRdrElt -> Bool
733 isLocalGRE (GRE {gre_lcl = lcl }) = lcl
734
735 isRecFldGRE :: GlobalRdrElt -> Bool
736 isRecFldGRE (GRE {gre_par = FldParent{}}) = True
737 isRecFldGRE _ = False
738
739 -- Returns the field label of this GRE, if it has one
740 greLabel :: GlobalRdrElt -> Maybe FieldLabelString
741 greLabel (GRE{gre_par = FldParent{par_lbl = Just lbl}}) = Just lbl
742 greLabel (GRE{gre_name = n, gre_par = FldParent{}}) = Just (occNameFS (nameOccName n))
743 greLabel _ = Nothing
744
745 unQualOK :: GlobalRdrElt -> Bool
746 -- ^ Test if an unqualifed version of this thing would be in scope
747 unQualOK (GRE {gre_lcl = lcl, gre_imp = iss })
748 | lcl = True
749 | otherwise = any unQualSpecOK iss
750
751 {- Note [GRE filtering]
752 ~~~~~~~~~~~~~~~~~~~~~~~
753 (pickGREs rdr gres) takes a list of GREs which have the same OccName
754 as 'rdr', say "x". It does two things:
755
756 (a) filters the GREs to a subset that are in scope
757 * Qualified, as 'M.x' if want_qual is Qual M _
758 * Unqualified, as 'x' if want_unqual is Unqual _
759
760 (b) for that subset, filter the provenance field (gre_lcl and gre_imp)
761 to ones that brought it into scope qualifed or unqualified resp.
762
763 Example:
764 module A ( f ) where
765 import qualified Foo( f )
766 import Baz( f )
767 f = undefined
768
769 Let's suppose that Foo.f and Baz.f are the same entity really, but the local
770 'f' is different, so there will be two GREs matching "f":
771 gre1: gre_lcl = True, gre_imp = []
772 gre2: gre_lcl = False, gre_imp = [ imported from Foo, imported from Bar ]
773
774 The use of "f" in the export list is ambiguous because it's in scope
775 from the local def and the import Baz(f); but *not* the import qualified Foo.
776 pickGREs returns two GRE
777 gre1: gre_lcl = True, gre_imp = []
778 gre2: gre_lcl = False, gre_imp = [ imported from Bar ]
779
780 Now the the "ambiguous occurrence" message can correctly report how the
781 ambiguity arises.
782 -}
783
784 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
785 -- ^ Takes a list of GREs which have the right OccName 'x'
786 -- Pick those GREs that are are in scope
787 -- * Qualified, as 'M.x' if want_qual is Qual M _
788 -- * Unqualified, as 'x' if want_unqual is Unqual _
789 --
790 -- Return each such GRE, with its ImportSpecs filtered, to reflect
791 -- how it is in scope qualifed or unqualified respectively.
792 -- See Note [GRE filtering]
793 pickGREs (Unqual {}) gres = mapMaybe pickUnqualGRE gres
794 pickGREs (Qual mod _) gres = mapMaybe (pickQualGRE mod) gres
795 pickGREs _ _ = [] -- I don't think this actually happens
796
797 pickUnqualGRE :: GlobalRdrElt -> Maybe GlobalRdrElt
798 pickUnqualGRE gre@(GRE { gre_lcl = lcl, gre_imp = iss })
799 | not lcl, null iss' = Nothing
800 | otherwise = Just (gre { gre_imp = iss' })
801 where
802 iss' = filter unQualSpecOK iss
803
804 pickQualGRE :: ModuleName -> GlobalRdrElt -> Maybe GlobalRdrElt
805 pickQualGRE mod gre@(GRE { gre_name = n, gre_lcl = lcl, gre_imp = iss })
806 | not lcl', null iss' = Nothing
807 | otherwise = Just (gre { gre_lcl = lcl', gre_imp = iss' })
808 where
809 iss' = filter (qualSpecOK mod) iss
810 lcl' = lcl && name_is_from mod n
811
812 name_is_from :: ModuleName -> Name -> Bool
813 name_is_from mod name = case nameModule_maybe name of
814 Just n_mod -> moduleName n_mod == mod
815 Nothing -> False
816
817 pickGREsModExp :: ModuleName -> [GlobalRdrElt] -> [(GlobalRdrElt,GlobalRdrElt)]
818 -- ^ Pick GREs that are in scope *both* qualified *and* unqualified
819 -- Return each GRE that is, as a pair
820 -- (qual_gre, unqual_gre)
821 -- These two GREs are the original GRE with imports filtered to express how
822 -- it is in scope qualified an unqualified respectively
823 --
824 -- Used only for the 'module M' item in export list;
825 -- see RnNames.exports_from_avail
826 pickGREsModExp mod gres = mapMaybe (pickBothGRE mod) gres
827
828 pickBothGRE :: ModuleName -> GlobalRdrElt -> Maybe (GlobalRdrElt, GlobalRdrElt)
829 pickBothGRE mod gre@(GRE { gre_name = n })
830 | isBuiltInSyntax n = Nothing
831 | Just gre1 <- pickQualGRE mod gre
832 , Just gre2 <- pickUnqualGRE gre = Just (gre1, gre2)
833 | otherwise = Nothing
834 where
835 -- isBuiltInSyntax filter out names for built-in syntax They
836 -- just clutter up the environment (esp tuples), and the
837 -- parser will generate Exact RdrNames for them, so the
838 -- cluttered envt is no use. Really, it's only useful for
839 -- GHC.Base and GHC.Tuple.
840
841 -- Building GlobalRdrEnvs
842
843 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
844 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
845
846 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
847 mkGlobalRdrEnv gres
848 = foldr add emptyGlobalRdrEnv gres
849 where
850 add gre env = extendOccEnv_Acc insertGRE singleton env
851 (greOccName gre)
852 gre
853
854 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
855 insertGRE new_g [] = [new_g]
856 insertGRE new_g (old_g : old_gs)
857 | gre_name new_g == gre_name old_g
858 = new_g `plusGRE` old_g : old_gs
859 | otherwise
860 = old_g : insertGRE new_g old_gs
861
862 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
863 -- Used when the gre_name fields match
864 plusGRE g1 g2
865 = GRE { gre_name = gre_name g1
866 , gre_lcl = gre_lcl g1 || gre_lcl g2
867 , gre_imp = gre_imp g1 ++ gre_imp g2
868 , gre_par = gre_par g1 `plusParent` gre_par g2 }
869
870 transformGREs :: (GlobalRdrElt -> GlobalRdrElt)
871 -> [OccName]
872 -> GlobalRdrEnv -> GlobalRdrEnv
873 -- ^ Apply a transformation function to the GREs for these OccNames
874 transformGREs trans_gre occs rdr_env
875 = foldr trans rdr_env occs
876 where
877 trans occ env
878 = case lookupOccEnv env occ of
879 Just gres -> extendOccEnv env occ (map trans_gre gres)
880 Nothing -> env
881
882 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
883 extendGlobalRdrEnv env gre
884 = extendOccEnv_Acc insertGRE singleton env
885 (greOccName gre) gre
886
887 shadowNames :: GlobalRdrEnv -> [Name] -> GlobalRdrEnv
888 shadowNames = foldl shadowName
889
890 {- Note [GlobalRdrEnv shadowing]
891 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
892 Before adding new names to the GlobalRdrEnv we nuke some existing entries;
893 this is "shadowing". The actual work is done by RdrEnv.shadowNames.
894 There are two reasons for shadowing:
895
896 * The GHCi REPL
897
898 - Ids bought into scope on the command line (eg let x = True) have
899 External Names, like Ghci4.x. We want a new binding for 'x' (say)
900 to override the existing binding for 'x'.
901 See Note [Interactively-bound Ids in GHCi] in HscTypes
902
903 - Data types also have Extenal Names, like Ghci4.T; but we still want
904 'T' to mean the newly-declared 'T', not an old one.
905
906 * Nested Template Haskell declaration brackets
907 See Note [Top-level Names in Template Haskell decl quotes] in RnNames
908
909 Consider a TH decl quote:
910 module M where
911 f x = h [d| f = 3 |]
912 We must shadow the outer declaration of 'f', else we'll get a
913 complaint when extending the GlobalRdrEnv, saying that there are two
914 bindings for 'f'. There are several tricky points:
915
916 - This shadowing applies even if the binding for 'f' is in a
917 where-clause, and hence is in the *local* RdrEnv not the *global*
918 RdrEnv. This is done in lcl_env_TH in extendGlobalRdrEnvRn.
919
920 - The External Name M.f from the enclosing module must certainly
921 still be available. So we don't nuke it entirely; we just make
922 it seem like qualified import.
923
924 - We only shadow *External* names (which come from the main module),
925 or from earlier GHCi commands. Do not shadow *Internal* names
926 because in the bracket
927 [d| class C a where f :: a
928 f = 4 |]
929 rnSrcDecls will first call extendGlobalRdrEnvRn with C[f] from the
930 class decl, and *separately* extend the envt with the value binding.
931 At that stage, the class op 'f' will have an Internal name.
932 -}
933
934 shadowName :: GlobalRdrEnv -> Name -> GlobalRdrEnv
935 -- Remove certain old GREs that share the same OccName as this new Name.
936 -- See Note [GlobalRdrEnv shadowing] for details
937 shadowName env name
938 = alterOccEnv (fmap alter_fn) env (nameOccName name)
939 where
940 alter_fn :: [GlobalRdrElt] -> [GlobalRdrElt]
941 alter_fn gres = mapMaybe (shadow_with name) gres
942
943 shadow_with :: Name -> GlobalRdrElt -> Maybe GlobalRdrElt
944 shadow_with new_name
945 old_gre@(GRE { gre_name = old_name, gre_lcl = lcl, gre_imp = iss })
946 = case nameModule_maybe old_name of
947 Nothing -> Just old_gre -- Old name is Internal; do not shadow
948 Just old_mod
949 | Just new_mod <- nameModule_maybe new_name
950 , new_mod == old_mod -- Old name same as new name; shadow completely
951 -> Nothing
952
953 | null iss' -- Nothing remains
954 -> Nothing
955
956 | otherwise
957 -> Just (old_gre { gre_lcl = False, gre_imp = iss' })
958
959 where
960 iss' = lcl_imp ++ mapMaybe (shadow_is new_name) iss
961 lcl_imp | lcl = [mk_fake_imp_spec old_name old_mod]
962 | otherwise = []
963
964 mk_fake_imp_spec old_name old_mod -- Urgh!
965 = ImpSpec id_spec ImpAll
966 where
967 old_mod_name = moduleName old_mod
968 id_spec = ImpDeclSpec { is_mod = old_mod_name
969 , is_as = old_mod_name
970 , is_qual = True
971 , is_dloc = nameSrcSpan old_name }
972
973 shadow_is :: Name -> ImportSpec -> Maybe ImportSpec
974 shadow_is new_name is@(ImpSpec { is_decl = id_spec })
975 | Just new_mod <- nameModule_maybe new_name
976 , is_as id_spec == moduleName new_mod
977 = Nothing -- Shadow both qualified and unqualified
978 | otherwise -- Shadow unqualified only
979 = Just (is { is_decl = id_spec { is_qual = True } })
980
981
982 {-
983 ************************************************************************
984 * *
985 ImportSpec
986 * *
987 ************************************************************************
988 -}
989
990 -- | The 'ImportSpec' of something says how it came to be imported
991 -- It's quite elaborate so that we can give accurate unused-name warnings.
992 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
993 is_item :: ImpItemSpec }
994 deriving( Eq, Ord )
995
996 -- | Describes a particular import declaration and is
997 -- shared among all the 'Provenance's for that decl
998 data ImpDeclSpec
999 = ImpDeclSpec {
1000 is_mod :: ModuleName, -- ^ Module imported, e.g. @import Muggle@
1001 -- Note the @Muggle@ may well not be
1002 -- the defining module for this thing!
1003
1004 -- TODO: either should be Module, or there
1005 -- should be a Maybe UnitId here too.
1006 is_as :: ModuleName, -- ^ Import alias, e.g. from @as M@ (or @Muggle@ if there is no @as@ clause)
1007 is_qual :: Bool, -- ^ Was this import qualified?
1008 is_dloc :: SrcSpan -- ^ The location of the entire import declaration
1009 }
1010
1011 -- | Describes import info a particular Name
1012 data ImpItemSpec
1013 = ImpAll -- ^ The import had no import list,
1014 -- or had a hiding list
1015
1016 | ImpSome {
1017 is_explicit :: Bool,
1018 is_iloc :: SrcSpan -- Location of the import item
1019 } -- ^ The import had an import list.
1020 -- The 'is_explicit' field is @True@ iff the thing was named
1021 -- /explicitly/ in the import specs rather
1022 -- than being imported as part of a "..." group. Consider:
1023 --
1024 -- > import C( T(..) )
1025 --
1026 -- Here the constructors of @T@ are not named explicitly;
1027 -- only @T@ is named explicitly.
1028
1029 instance Eq ImpDeclSpec where
1030 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
1031
1032 instance Ord ImpDeclSpec where
1033 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
1034 (is_dloc is1 `compare` is_dloc is2)
1035
1036 instance Eq ImpItemSpec where
1037 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
1038
1039 instance Ord ImpItemSpec where
1040 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
1041
1042
1043 bestImport :: [ImportSpec] -> ImportSpec
1044 -- Given a non-empty bunch of ImportSpecs, return the one that
1045 -- imported the item most specficially (e.g. by name), using
1046 -- textually-first as a tie breaker. This is used when reporting
1047 -- redundant imports
1048 bestImport iss
1049 = case sortBy best iss of
1050 (is:_) -> is
1051 [] -> pprPanic "bestImport" (ppr iss)
1052 where
1053 best :: ImportSpec -> ImportSpec -> Ordering
1054 -- Less means better
1055 best (ImpSpec { is_item = item1, is_decl = d1 })
1056 (ImpSpec { is_item = item2, is_decl = d2 })
1057 = best_item item1 item2 `thenCmp` (is_dloc d1 `compare` is_dloc d2)
1058
1059 best_item :: ImpItemSpec -> ImpItemSpec -> Ordering
1060 best_item ImpAll ImpAll = EQ
1061 best_item ImpAll (ImpSome {}) = GT
1062 best_item (ImpSome {}) ImpAll = LT
1063 best_item (ImpSome { is_explicit = e1 })
1064 (ImpSome { is_explicit = e2 }) = e2 `compare` e1
1065 -- False < True, so if e1 is explicit and e2 is not, we get LT
1066
1067 unQualSpecOK :: ImportSpec -> Bool
1068 -- ^ Is in scope unqualified?
1069 unQualSpecOK is = not (is_qual (is_decl is))
1070
1071 qualSpecOK :: ModuleName -> ImportSpec -> Bool
1072 -- ^ Is in scope qualified with the given module?
1073 qualSpecOK mod is = mod == is_as (is_decl is)
1074
1075 importSpecLoc :: ImportSpec -> SrcSpan
1076 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
1077 importSpecLoc (ImpSpec _ item) = is_iloc item
1078
1079 importSpecModule :: ImportSpec -> ModuleName
1080 importSpecModule is = is_mod (is_decl is)
1081
1082 isExplicitItem :: ImpItemSpec -> Bool
1083 isExplicitItem ImpAll = False
1084 isExplicitItem (ImpSome {is_explicit = exp}) = exp
1085
1086 pprNameProvenance :: GlobalRdrElt -> SDoc
1087 -- ^ Print out one place where the name was define/imported
1088 -- (With -dppr-debug, print them all)
1089 pprNameProvenance (GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss })
1090 | opt_PprStyle_Debug = vcat pp_provs
1091 | otherwise = head pp_provs
1092 where
1093 pp_provs = pp_lcl ++ map pp_is iss
1094 pp_lcl = if lcl then [ptext (sLit "defined at") <+> ppr (nameSrcLoc name)]
1095 else []
1096 pp_is is = sep [ppr is, ppr_defn_site is name]
1097
1098 -- If we know the exact definition point (which we may do with GHCi)
1099 -- then show that too. But not if it's just "imported from X".
1100 ppr_defn_site :: ImportSpec -> Name -> SDoc
1101 ppr_defn_site imp_spec name
1102 | same_module && not (isGoodSrcSpan loc)
1103 = empty -- Nothing interesting to say
1104 | otherwise
1105 = parens $ hang (ptext (sLit "and originally defined") <+> pp_mod)
1106 2 (pprLoc loc)
1107 where
1108 loc = nameSrcSpan name
1109 defining_mod = nameModule name
1110 same_module = importSpecModule imp_spec == moduleName defining_mod
1111 pp_mod | same_module = empty
1112 | otherwise = ptext (sLit "in") <+> quotes (ppr defining_mod)
1113
1114
1115 instance Outputable ImportSpec where
1116 ppr imp_spec
1117 = ptext (sLit "imported") <+> qual
1118 <+> ptext (sLit "from") <+> quotes (ppr (importSpecModule imp_spec))
1119 <+> pprLoc (importSpecLoc imp_spec)
1120 where
1121 qual | is_qual (is_decl imp_spec) = ptext (sLit "qualified")
1122 | otherwise = empty
1123
1124 pprLoc :: SrcSpan -> SDoc
1125 pprLoc (RealSrcSpan s) = ptext (sLit "at") <+> ppr s
1126 pprLoc (UnhelpfulSpan {}) = empty