f4ca912eb52456d167d31650b67b0369aa9ed832
[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.bindHsQTyVars), 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 For record fields, in addition to the Name of the type constructor
519 (stored in par_is), we use FldParent to store the field label. This
520 extra information is used for identifying overloaded record fields
521 during renaming.
522
523 In a definition arising from a normal module (without
524 -XDuplicateRecordFields), par_lbl will be Nothing, meaning that the
525 field's label is the same as the OccName of the selector's Name. The
526 GlobalRdrEnv will contain an entry like this:
527
528 "x" |-> GRE x (FldParent T Nothing) LocalDef
529
530 When -XDuplicateRecordFields is enabled for the module that contains
531 T, the selector's Name will be mangled (see comments in FieldLabel).
532 Thus we store the actual field label in par_lbl, and the GlobalRdrEnv
533 entry looks like this:
534
535 "x" |-> GRE $sel:x:MkT (FldParent T (Just "x")) LocalDef
536
537 Note that the OccName used when adding a GRE to the environment
538 (greOccName) now depends on the parent field: for FldParent it is the
539 field label, if present, rather than the selector name.
540
541
542 Note [Combining parents]
543 ~~~~~~~~~~~~~~~~~~~~~~~~
544 With an associated type we might have
545 module M where
546 class C a where
547 data T a
548 op :: T a -> a
549 instance C Int where
550 data T Int = TInt
551 instance C Bool where
552 data T Bool = TBool
553
554 Then: C is the parent of T
555 T is the parent of TInt and TBool
556 So: in an export list
557 C(..) is short for C( op, T )
558 T(..) is short for T( TInt, TBool )
559
560 Module M exports everything, so its exports will be
561 AvailTC C [C,T,op]
562 AvailTC T [T,TInt,TBool]
563 On import we convert to GlobalRdrElt and then combine
564 those. For T that will mean we have
565 one GRE with Parent C
566 one GRE with NoParent
567 That's why plusParent picks the "best" case.
568 -}
569
570 -- | make a 'GlobalRdrEnv' where all the elements point to the same
571 -- Provenance (useful for "hiding" imports, or imports with no details).
572 gresFromAvails :: Maybe ImportSpec -> [AvailInfo] -> [GlobalRdrElt]
573 -- prov = Nothing => locally bound
574 -- Just spec => imported as described by spec
575 gresFromAvails prov avails
576 = concatMap (gresFromAvail (const prov)) avails
577
578 localGREsFromAvail :: AvailInfo -> [GlobalRdrElt]
579 -- Turn an Avail into a list of LocalDef GlobalRdrElts
580 localGREsFromAvail = gresFromAvail (const Nothing)
581
582 gresFromAvail :: (Name -> Maybe ImportSpec) -> AvailInfo -> [GlobalRdrElt]
583 gresFromAvail prov_fn avail
584 = map mk_gre (availNonFldNames avail) ++ map mk_fld_gre (availFlds avail)
585 where
586 mk_gre n
587 = case prov_fn n of -- Nothing => bound locally
588 -- Just is => imported from 'is'
589 Nothing -> GRE { gre_name = n, gre_par = mkParent n avail
590 , gre_lcl = True, gre_imp = [] }
591 Just is -> GRE { gre_name = n, gre_par = mkParent n avail
592 , gre_lcl = False, gre_imp = [is] }
593
594 mk_fld_gre (FieldLabel lbl is_overloaded n)
595 = case prov_fn n of -- Nothing => bound locally
596 -- Just is => imported from 'is'
597 Nothing -> GRE { gre_name = n, gre_par = FldParent (availName avail) mb_lbl
598 , gre_lcl = True, gre_imp = [] }
599 Just is -> GRE { gre_name = n, gre_par = FldParent (availName avail) mb_lbl
600 , gre_lcl = False, gre_imp = [is] }
601 where
602 mb_lbl | is_overloaded = Just lbl
603 | otherwise = Nothing
604
605
606 greQualModName :: GlobalRdrElt -> ModuleName
607 -- Get a suitable module qualifier for the GRE
608 -- (used in mkPrintUnqualified)
609 -- Prerecondition: the gre_name is always External
610 greQualModName gre@(GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss })
611 | lcl, Just mod <- nameModule_maybe name = moduleName mod
612 | (is:_) <- iss = is_as (is_decl is)
613 | otherwise = pprPanic "greQualModName" (ppr gre)
614
615 greUsedRdrName :: GlobalRdrElt -> RdrName
616 -- For imported things, return a RdrName to add to the used-RdrName
617 -- set, which is used to generate unused-import-decl warnings.
618 -- Return a Qual RdrName if poss, so that identifies the most
619 -- specific ImportSpec. See Trac #10890 for some good examples.
620 greUsedRdrName gre@GRE{ gre_name = name, gre_lcl = lcl, gre_imp = iss }
621 | lcl, Just mod <- nameModule_maybe name = Qual (moduleName mod) occ
622 | not (null iss), is <- bestImport iss = Qual (is_as (is_decl is)) occ
623 | otherwise = pprTrace "greUsedRdrName" (ppr gre) (Unqual occ)
624 where
625 occ = greOccName gre
626
627 greRdrNames :: GlobalRdrElt -> [RdrName]
628 greRdrNames GRE{ gre_name = name, gre_lcl = lcl, gre_imp = iss }
629 = (if lcl then [unqual] else []) ++ concatMap do_spec (map is_decl iss)
630 where
631 occ = nameOccName name
632 unqual = Unqual occ
633 do_spec decl_spec
634 | is_qual decl_spec = [qual]
635 | otherwise = [unqual,qual]
636 where qual = Qual (is_as decl_spec) occ
637
638 -- the SrcSpan that pprNameProvenance prints out depends on whether
639 -- the Name is defined locally or not: for a local definition the
640 -- definition site is used, otherwise the location of the import
641 -- declaration. We want to sort the export locations in
642 -- exportClashErr by this SrcSpan, we need to extract it:
643 greSrcSpan :: GlobalRdrElt -> SrcSpan
644 greSrcSpan gre@(GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss } )
645 | lcl = nameSrcSpan name
646 | (is:_) <- iss = is_dloc (is_decl is)
647 | otherwise = pprPanic "greSrcSpan" (ppr gre)
648
649 mkParent :: Name -> AvailInfo -> Parent
650 mkParent _ (Avail NotPatSyn _) = NoParent
651 mkParent _ (Avail IsPatSyn _) = PatternSynonym
652 mkParent n (AvailTC m _ _) | n == m = NoParent
653 | otherwise = ParentIs m
654
655 availFromGRE :: GlobalRdrElt -> AvailInfo
656 availFromGRE (GRE { gre_name = me, gre_par = parent })
657 = case parent of
658 ParentIs p -> AvailTC p [me] []
659 NoParent | isTyConName me -> AvailTC me [me] []
660 | otherwise -> avail me
661 FldParent p Nothing -> AvailTC p [] [FieldLabel (occNameFS $ nameOccName me) False me]
662 FldParent p (Just lbl) -> AvailTC p [] [FieldLabel lbl True me]
663 PatternSynonym -> patSynAvail me
664
665 emptyGlobalRdrEnv :: GlobalRdrEnv
666 emptyGlobalRdrEnv = emptyOccEnv
667
668 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
669 globalRdrEnvElts env = foldOccEnv (++) [] env
670
671 instance Outputable GlobalRdrElt where
672 ppr gre = hang (ppr (gre_name gre) <+> ppr (gre_par gre))
673 2 (pprNameProvenance gre)
674
675 pprGlobalRdrEnv :: Bool -> GlobalRdrEnv -> SDoc
676 pprGlobalRdrEnv locals_only env
677 = vcat [ ptext (sLit "GlobalRdrEnv") <+> ppWhen locals_only (ptext (sLit "(locals only)"))
678 <+> lbrace
679 , nest 2 (vcat [ pp (remove_locals gre_list) | gre_list <- occEnvElts env ]
680 <+> rbrace) ]
681 where
682 remove_locals gres | locals_only = filter isLocalGRE gres
683 | otherwise = gres
684 pp [] = empty
685 pp gres = hang (ppr occ
686 <+> parens (ptext (sLit "unique") <+> ppr (getUnique occ))
687 <> colon)
688 2 (vcat (map ppr gres))
689 where
690 occ = nameOccName (gre_name (head gres))
691
692 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
693 lookupGlobalRdrEnv env occ_name = case lookupOccEnv env occ_name of
694 Nothing -> []
695 Just gres -> gres
696
697 greOccName :: GlobalRdrElt -> OccName
698 greOccName (GRE{gre_par = FldParent{par_lbl = Just lbl}}) = mkVarOccFS lbl
699 greOccName gre = nameOccName (gre_name gre)
700
701 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
702 lookupGRE_RdrName rdr_name env
703 = case lookupOccEnv env (rdrNameOcc rdr_name) of
704 Nothing -> []
705 Just gres -> pickGREs rdr_name gres
706
707 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
708 lookupGRE_Name env name
709 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
710 gre_name gre == name ]
711
712 lookupGRE_Field_Name :: GlobalRdrEnv -> Name -> FastString -> [GlobalRdrElt]
713 -- Used when looking up record fields, where the selector name and
714 -- field label are different: the GlobalRdrEnv is keyed on the label
715 lookupGRE_Field_Name env sel_name lbl
716 = [ gre | gre <- lookupGlobalRdrEnv env (mkVarOccFS lbl),
717 gre_name gre == sel_name ]
718
719
720 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
721 -- Returns all the qualifiers by which 'x' is in scope
722 -- Nothing means "the unqualified version is in scope"
723 -- [] means the thing is not in scope at all
724 getGRE_NameQualifier_maybes env
725 = map (qualifier_maybe) . lookupGRE_Name env
726 where
727 qualifier_maybe (GRE { gre_lcl = lcl, gre_imp = iss })
728 | lcl = Nothing
729 | otherwise = Just $ map (is_as . is_decl) iss
730
731 isLocalGRE :: GlobalRdrElt -> Bool
732 isLocalGRE (GRE {gre_lcl = lcl }) = lcl
733
734 isRecFldGRE :: GlobalRdrElt -> Bool
735 isRecFldGRE (GRE {gre_par = FldParent{}}) = True
736 isRecFldGRE _ = False
737
738 -- Returns the field label of this GRE, if it has one
739 greLabel :: GlobalRdrElt -> Maybe FieldLabelString
740 greLabel (GRE{gre_par = FldParent{par_lbl = Just lbl}}) = Just lbl
741 greLabel (GRE{gre_name = n, gre_par = FldParent{}}) = Just (occNameFS (nameOccName n))
742 greLabel _ = Nothing
743
744 unQualOK :: GlobalRdrElt -> Bool
745 -- ^ Test if an unqualifed version of this thing would be in scope
746 unQualOK (GRE {gre_lcl = lcl, gre_imp = iss })
747 | lcl = True
748 | otherwise = any unQualSpecOK iss
749
750 {- Note [GRE filtering]
751 ~~~~~~~~~~~~~~~~~~~~~~~
752 (pickGREs rdr gres) takes a list of GREs which have the same OccName
753 as 'rdr', say "x". It does two things:
754
755 (a) filters the GREs to a subset that are in scope
756 * Qualified, as 'M.x' if want_qual is Qual M _
757 * Unqualified, as 'x' if want_unqual is Unqual _
758
759 (b) for that subset, filter the provenance field (gre_lcl and gre_imp)
760 to ones that brought it into scope qualifed or unqualified resp.
761
762 Example:
763 module A ( f ) where
764 import qualified Foo( f )
765 import Baz( f )
766 f = undefined
767
768 Let's suppose that Foo.f and Baz.f are the same entity really, but the local
769 'f' is different, so there will be two GREs matching "f":
770 gre1: gre_lcl = True, gre_imp = []
771 gre2: gre_lcl = False, gre_imp = [ imported from Foo, imported from Bar ]
772
773 The use of "f" in the export list is ambiguous because it's in scope
774 from the local def and the import Baz(f); but *not* the import qualified Foo.
775 pickGREs returns two GRE
776 gre1: gre_lcl = True, gre_imp = []
777 gre2: gre_lcl = False, gre_imp = [ imported from Bar ]
778
779 Now the the "ambiguous occurrence" message can correctly report how the
780 ambiguity arises.
781 -}
782
783 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
784 -- ^ Takes a list of GREs which have the right OccName 'x'
785 -- Pick those GREs that are are in scope
786 -- * Qualified, as 'M.x' if want_qual is Qual M _
787 -- * Unqualified, as 'x' if want_unqual is Unqual _
788 --
789 -- Return each such GRE, with its ImportSpecs filtered, to reflect
790 -- how it is in scope qualifed or unqualified respectively.
791 -- See Note [GRE filtering]
792 pickGREs (Unqual {}) gres = mapMaybe pickUnqualGRE gres
793 pickGREs (Qual mod _) gres = mapMaybe (pickQualGRE mod) gres
794 pickGREs _ _ = [] -- I don't think this actually happens
795
796 pickUnqualGRE :: GlobalRdrElt -> Maybe GlobalRdrElt
797 pickUnqualGRE gre@(GRE { gre_lcl = lcl, gre_imp = iss })
798 | not lcl, null iss' = Nothing
799 | otherwise = Just (gre { gre_imp = iss' })
800 where
801 iss' = filter unQualSpecOK iss
802
803 pickQualGRE :: ModuleName -> GlobalRdrElt -> Maybe GlobalRdrElt
804 pickQualGRE mod gre@(GRE { gre_name = n, gre_lcl = lcl, gre_imp = iss })
805 | not lcl', null iss' = Nothing
806 | otherwise = Just (gre { gre_lcl = lcl', gre_imp = iss' })
807 where
808 iss' = filter (qualSpecOK mod) iss
809 lcl' = lcl && name_is_from mod n
810
811 name_is_from :: ModuleName -> Name -> Bool
812 name_is_from mod name = case nameModule_maybe name of
813 Just n_mod -> moduleName n_mod == mod
814 Nothing -> False
815
816 pickGREsModExp :: ModuleName -> [GlobalRdrElt] -> [(GlobalRdrElt,GlobalRdrElt)]
817 -- ^ Pick GREs that are in scope *both* qualified *and* unqualified
818 -- Return each GRE that is, as a pair
819 -- (qual_gre, unqual_gre)
820 -- These two GREs are the original GRE with imports filtered to express how
821 -- it is in scope qualified an unqualified respectively
822 --
823 -- Used only for the 'module M' item in export list;
824 -- see RnNames.exports_from_avail
825 pickGREsModExp mod gres = mapMaybe (pickBothGRE mod) gres
826
827 pickBothGRE :: ModuleName -> GlobalRdrElt -> Maybe (GlobalRdrElt, GlobalRdrElt)
828 pickBothGRE mod gre@(GRE { gre_name = n })
829 | isBuiltInSyntax n = Nothing
830 | Just gre1 <- pickQualGRE mod gre
831 , Just gre2 <- pickUnqualGRE gre = Just (gre1, gre2)
832 | otherwise = Nothing
833 where
834 -- isBuiltInSyntax filter out names for built-in syntax They
835 -- just clutter up the environment (esp tuples), and the
836 -- parser will generate Exact RdrNames for them, so the
837 -- cluttered envt is no use. Really, it's only useful for
838 -- GHC.Base and GHC.Tuple.
839
840 -- Building GlobalRdrEnvs
841
842 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
843 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
844
845 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
846 mkGlobalRdrEnv gres
847 = foldr add emptyGlobalRdrEnv gres
848 where
849 add gre env = extendOccEnv_Acc insertGRE singleton env
850 (greOccName gre)
851 gre
852
853 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
854 insertGRE new_g [] = [new_g]
855 insertGRE new_g (old_g : old_gs)
856 | gre_name new_g == gre_name old_g
857 = new_g `plusGRE` old_g : old_gs
858 | otherwise
859 = old_g : insertGRE new_g old_gs
860
861 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
862 -- Used when the gre_name fields match
863 plusGRE g1 g2
864 = GRE { gre_name = gre_name g1
865 , gre_lcl = gre_lcl g1 || gre_lcl g2
866 , gre_imp = gre_imp g1 ++ gre_imp g2
867 , gre_par = gre_par g1 `plusParent` gre_par g2 }
868
869 transformGREs :: (GlobalRdrElt -> GlobalRdrElt)
870 -> [OccName]
871 -> GlobalRdrEnv -> GlobalRdrEnv
872 -- ^ Apply a transformation function to the GREs for these OccNames
873 transformGREs trans_gre occs rdr_env
874 = foldr trans rdr_env occs
875 where
876 trans occ env
877 = case lookupOccEnv env occ of
878 Just gres -> extendOccEnv env occ (map trans_gre gres)
879 Nothing -> env
880
881 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
882 extendGlobalRdrEnv env gre
883 = extendOccEnv_Acc insertGRE singleton env
884 (greOccName gre) gre
885
886 shadowNames :: GlobalRdrEnv -> [Name] -> GlobalRdrEnv
887 shadowNames = foldl shadowName
888
889 {- Note [GlobalRdrEnv shadowing]
890 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
891 Before adding new names to the GlobalRdrEnv we nuke some existing entries;
892 this is "shadowing". The actual work is done by RdrEnv.shadowNames.
893 There are two reasons for shadowing:
894
895 * The GHCi REPL
896
897 - Ids bought into scope on the command line (eg let x = True) have
898 External Names, like Ghci4.x. We want a new binding for 'x' (say)
899 to override the existing binding for 'x'.
900 See Note [Interactively-bound Ids in GHCi] in HscTypes
901
902 - Data types also have Extenal Names, like Ghci4.T; but we still want
903 'T' to mean the newly-declared 'T', not an old one.
904
905 * Nested Template Haskell declaration brackets
906 See Note [Top-level Names in Template Haskell decl quotes] in RnNames
907
908 Consider a TH decl quote:
909 module M where
910 f x = h [d| f = 3 |]
911 We must shadow the outer declaration of 'f', else we'll get a
912 complaint when extending the GlobalRdrEnv, saying that there are two
913 bindings for 'f'. There are several tricky points:
914
915 - This shadowing applies even if the binding for 'f' is in a
916 where-clause, and hence is in the *local* RdrEnv not the *global*
917 RdrEnv. This is done in lcl_env_TH in extendGlobalRdrEnvRn.
918
919 - The External Name M.f from the enclosing module must certainly
920 still be available. So we don't nuke it entirely; we just make
921 it seem like qualified import.
922
923 - We only shadow *External* names (which come from the main module),
924 or from earlier GHCi commands. Do not shadow *Internal* names
925 because in the bracket
926 [d| class C a where f :: a
927 f = 4 |]
928 rnSrcDecls will first call extendGlobalRdrEnvRn with C[f] from the
929 class decl, and *separately* extend the envt with the value binding.
930 At that stage, the class op 'f' will have an Internal name.
931 -}
932
933 shadowName :: GlobalRdrEnv -> Name -> GlobalRdrEnv
934 -- Remove certain old GREs that share the same OccName as this new Name.
935 -- See Note [GlobalRdrEnv shadowing] for details
936 shadowName env name
937 = alterOccEnv (fmap alter_fn) env (nameOccName name)
938 where
939 alter_fn :: [GlobalRdrElt] -> [GlobalRdrElt]
940 alter_fn gres = mapMaybe (shadow_with name) gres
941
942 shadow_with :: Name -> GlobalRdrElt -> Maybe GlobalRdrElt
943 shadow_with new_name
944 old_gre@(GRE { gre_name = old_name, gre_lcl = lcl, gre_imp = iss })
945 = case nameModule_maybe old_name of
946 Nothing -> Just old_gre -- Old name is Internal; do not shadow
947 Just old_mod
948 | Just new_mod <- nameModule_maybe new_name
949 , new_mod == old_mod -- Old name same as new name; shadow completely
950 -> Nothing
951
952 | null iss' -- Nothing remains
953 -> Nothing
954
955 | otherwise
956 -> Just (old_gre { gre_lcl = False, gre_imp = iss' })
957
958 where
959 iss' = lcl_imp ++ mapMaybe (shadow_is new_name) iss
960 lcl_imp | lcl = [mk_fake_imp_spec old_name old_mod]
961 | otherwise = []
962
963 mk_fake_imp_spec old_name old_mod -- Urgh!
964 = ImpSpec id_spec ImpAll
965 where
966 old_mod_name = moduleName old_mod
967 id_spec = ImpDeclSpec { is_mod = old_mod_name
968 , is_as = old_mod_name
969 , is_qual = True
970 , is_dloc = nameSrcSpan old_name }
971
972 shadow_is :: Name -> ImportSpec -> Maybe ImportSpec
973 shadow_is new_name is@(ImpSpec { is_decl = id_spec })
974 | Just new_mod <- nameModule_maybe new_name
975 , is_as id_spec == moduleName new_mod
976 = Nothing -- Shadow both qualified and unqualified
977 | otherwise -- Shadow unqualified only
978 = Just (is { is_decl = id_spec { is_qual = True } })
979
980
981 {-
982 ************************************************************************
983 * *
984 ImportSpec
985 * *
986 ************************************************************************
987 -}
988
989 -- | The 'ImportSpec' of something says how it came to be imported
990 -- It's quite elaborate so that we can give accurate unused-name warnings.
991 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
992 is_item :: ImpItemSpec }
993 deriving( Eq, Ord )
994
995 -- | Describes a particular import declaration and is
996 -- shared among all the 'Provenance's for that decl
997 data ImpDeclSpec
998 = ImpDeclSpec {
999 is_mod :: ModuleName, -- ^ Module imported, e.g. @import Muggle@
1000 -- Note the @Muggle@ may well not be
1001 -- the defining module for this thing!
1002
1003 -- TODO: either should be Module, or there
1004 -- should be a Maybe UnitId here too.
1005 is_as :: ModuleName, -- ^ Import alias, e.g. from @as M@ (or @Muggle@ if there is no @as@ clause)
1006 is_qual :: Bool, -- ^ Was this import qualified?
1007 is_dloc :: SrcSpan -- ^ The location of the entire import declaration
1008 }
1009
1010 -- | Describes import info a particular Name
1011 data ImpItemSpec
1012 = ImpAll -- ^ The import had no import list,
1013 -- or had a hiding list
1014
1015 | ImpSome {
1016 is_explicit :: Bool,
1017 is_iloc :: SrcSpan -- Location of the import item
1018 } -- ^ The import had an import list.
1019 -- The 'is_explicit' field is @True@ iff the thing was named
1020 -- /explicitly/ in the import specs rather
1021 -- than being imported as part of a "..." group. Consider:
1022 --
1023 -- > import C( T(..) )
1024 --
1025 -- Here the constructors of @T@ are not named explicitly;
1026 -- only @T@ is named explicitly.
1027
1028 instance Eq ImpDeclSpec where
1029 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
1030
1031 instance Ord ImpDeclSpec where
1032 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
1033 (is_dloc is1 `compare` is_dloc is2)
1034
1035 instance Eq ImpItemSpec where
1036 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
1037
1038 instance Ord ImpItemSpec where
1039 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
1040
1041
1042 bestImport :: [ImportSpec] -> ImportSpec
1043 -- Given a non-empty bunch of ImportSpecs, return the one that
1044 -- imported the item most specficially (e.g. by name), using
1045 -- textually-first as a tie breaker. This is used when reporting
1046 -- redundant imports
1047 bestImport iss
1048 = case sortBy best iss of
1049 (is:_) -> is
1050 [] -> pprPanic "bestImport" (ppr iss)
1051 where
1052 best :: ImportSpec -> ImportSpec -> Ordering
1053 -- Less means better
1054 best (ImpSpec { is_item = item1, is_decl = d1 })
1055 (ImpSpec { is_item = item2, is_decl = d2 })
1056 = best_item item1 item2 `thenCmp` (is_dloc d1 `compare` is_dloc d2)
1057
1058 best_item :: ImpItemSpec -> ImpItemSpec -> Ordering
1059 best_item ImpAll ImpAll = EQ
1060 best_item ImpAll (ImpSome {}) = GT
1061 best_item (ImpSome {}) ImpAll = LT
1062 best_item (ImpSome { is_explicit = e1 })
1063 (ImpSome { is_explicit = e2 }) = e2 `compare` e1
1064 -- False < True, so if e1 is explicit and e2 is not, we get LT
1065
1066 unQualSpecOK :: ImportSpec -> Bool
1067 -- ^ Is in scope unqualified?
1068 unQualSpecOK is = not (is_qual (is_decl is))
1069
1070 qualSpecOK :: ModuleName -> ImportSpec -> Bool
1071 -- ^ Is in scope qualified with the given module?
1072 qualSpecOK mod is = mod == is_as (is_decl is)
1073
1074 importSpecLoc :: ImportSpec -> SrcSpan
1075 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
1076 importSpecLoc (ImpSpec _ item) = is_iloc item
1077
1078 importSpecModule :: ImportSpec -> ModuleName
1079 importSpecModule is = is_mod (is_decl is)
1080
1081 isExplicitItem :: ImpItemSpec -> Bool
1082 isExplicitItem ImpAll = False
1083 isExplicitItem (ImpSome {is_explicit = exp}) = exp
1084
1085 pprNameProvenance :: GlobalRdrElt -> SDoc
1086 -- ^ Print out one place where the name was define/imported
1087 -- (With -dppr-debug, print them all)
1088 pprNameProvenance (GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss })
1089 | opt_PprStyle_Debug = vcat pp_provs
1090 | otherwise = head pp_provs
1091 where
1092 pp_provs = pp_lcl ++ map pp_is iss
1093 pp_lcl = if lcl then [ptext (sLit "defined at") <+> ppr (nameSrcLoc name)]
1094 else []
1095 pp_is is = sep [ppr is, ppr_defn_site is name]
1096
1097 -- If we know the exact definition point (which we may do with GHCi)
1098 -- then show that too. But not if it's just "imported from X".
1099 ppr_defn_site :: ImportSpec -> Name -> SDoc
1100 ppr_defn_site imp_spec name
1101 | same_module && not (isGoodSrcSpan loc)
1102 = empty -- Nothing interesting to say
1103 | otherwise
1104 = parens $ hang (ptext (sLit "and originally defined") <+> pp_mod)
1105 2 (pprLoc loc)
1106 where
1107 loc = nameSrcSpan name
1108 defining_mod = nameModule name
1109 same_module = importSpecModule imp_spec == moduleName defining_mod
1110 pp_mod | same_module = empty
1111 | otherwise = ptext (sLit "in") <+> quotes (ppr defining_mod)
1112
1113
1114 instance Outputable ImportSpec where
1115 ppr imp_spec
1116 = ptext (sLit "imported") <+> qual
1117 <+> ptext (sLit "from") <+> quotes (ppr (importSpecModule imp_spec))
1118 <+> pprLoc (importSpecLoc imp_spec)
1119 where
1120 qual | is_qual (is_decl imp_spec) = ptext (sLit "qualified")
1121 | otherwise = empty
1122
1123 pprLoc :: SrcSpan -> SDoc
1124 pprLoc (RealSrcSpan s) = ptext (sLit "at") <+> ppr s
1125 pprLoc (UnhelpfulSpan {}) = empty