Teach lookupLocalRdrEnv about Exacts. (#11813)
[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 -- The field lre_nwcs is used to keep names of type variables that should
326 -- be replaced with named wildcards.
327 -- See Note [Renaming named wild cards] in RnTypes
328 data LocalRdrEnv = LRE { lre_env :: OccEnv Name
329 , lre_in_scope :: NameSet }
330
331 instance Outputable LocalRdrEnv where
332 ppr (LRE {lre_env = env, lre_in_scope = ns})
333 = hang (text "LocalRdrEnv {")
334 2 (vcat [ text "env =" <+> pprOccEnv ppr_elt env
335 , text "in_scope ="
336 <+> braces (pprWithCommas ppr (nameSetElems ns))
337 ] <+> char '}')
338 where
339 ppr_elt name = parens (ppr (getUnique (nameOccName name))) <+> ppr name
340 -- So we can see if the keys line up correctly
341
342 emptyLocalRdrEnv :: LocalRdrEnv
343 emptyLocalRdrEnv = LRE { lre_env = emptyOccEnv
344 , lre_in_scope = emptyNameSet }
345
346 extendLocalRdrEnv :: LocalRdrEnv -> Name -> LocalRdrEnv
347 -- The Name should be a non-top-level thing
348 extendLocalRdrEnv lre@(LRE { lre_env = env, lre_in_scope = ns }) name
349 = WARN( isExternalName name, ppr name )
350 lre { lre_env = extendOccEnv env (nameOccName name) name
351 , lre_in_scope = extendNameSet ns name }
352
353 extendLocalRdrEnvList :: LocalRdrEnv -> [Name] -> LocalRdrEnv
354 extendLocalRdrEnvList lre@(LRE { lre_env = env, lre_in_scope = ns }) names
355 = WARN( any isExternalName names, ppr names )
356 lre { lre_env = extendOccEnvList env [(nameOccName n, n) | n <- names]
357 , lre_in_scope = extendNameSetList ns names }
358
359 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
360 lookupLocalRdrEnv (LRE { lre_env = env, lre_in_scope = ns }) rdr
361 | Unqual occ <- rdr
362 = lookupOccEnv env occ
363
364 -- See Note [Local bindings with Exact Names]
365 | Exact name <- rdr
366 , name `elemNameSet` ns
367 = Just name
368
369 | otherwise
370 = Nothing
371
372 lookupLocalRdrOcc :: LocalRdrEnv -> OccName -> Maybe Name
373 lookupLocalRdrOcc (LRE { lre_env = env }) occ = lookupOccEnv env occ
374
375 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
376 elemLocalRdrEnv rdr_name (LRE { lre_env = env, lre_in_scope = ns })
377 = case rdr_name of
378 Unqual occ -> occ `elemOccEnv` env
379 Exact name -> name `elemNameSet` ns -- See Note [Local bindings with Exact Names]
380 Qual {} -> False
381 Orig {} -> False
382
383 localRdrEnvElts :: LocalRdrEnv -> [Name]
384 localRdrEnvElts (LRE { lre_env = env }) = occEnvElts env
385
386 inLocalRdrEnvScope :: Name -> LocalRdrEnv -> Bool
387 -- This is the point of the NameSet
388 inLocalRdrEnvScope name (LRE { lre_in_scope = ns }) = name `elemNameSet` ns
389
390 delLocalRdrEnvList :: LocalRdrEnv -> [OccName] -> LocalRdrEnv
391 delLocalRdrEnvList lre@(LRE { lre_env = env }) occs
392 = lre { lre_env = delListFromOccEnv env occs }
393
394 {-
395 Note [Local bindings with Exact Names]
396 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
397 With Template Haskell we can make local bindings that have Exact Names.
398 Computing shadowing etc may use elemLocalRdrEnv (at least it certainly
399 does so in RnTpes.bindHsQTyVars), so for an Exact Name we must consult
400 the in-scope-name-set.
401
402
403 ************************************************************************
404 * *
405 GlobalRdrEnv
406 * *
407 ************************************************************************
408 -}
409
410 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
411 -- ^ Keyed by 'OccName'; when looking up a qualified name
412 -- we look up the 'OccName' part, and then check the 'Provenance'
413 -- to see if the appropriate qualification is valid. This
414 -- saves routinely doubling the size of the env by adding both
415 -- qualified and unqualified names to the domain.
416 --
417 -- The list in the codomain is required because there may be name clashes
418 -- These only get reported on lookup, not on construction
419 --
420 -- INVARIANT 1: All the members of the list have distinct
421 -- 'gre_name' fields; that is, no duplicate Names
422 --
423 -- INVARIANT 2: Imported provenance => Name is an ExternalName
424 -- However LocalDefs can have an InternalName. This
425 -- happens only when type-checking a [d| ... |] Template
426 -- Haskell quotation; see this note in RnNames
427 -- Note [Top-level Names in Template Haskell decl quotes]
428 --
429 -- INVARIANT 3: If the GlobalRdrEnv maps [occ -> gre], then
430 -- greOccName gre = occ
431 --
432 -- NB: greOccName gre is usually the same as
433 -- nameOccName (gre_name gre), but not always in the
434 -- case of record seectors; see greOccName
435
436 -- | An element of the 'GlobalRdrEnv'
437 data GlobalRdrElt
438 = GRE { gre_name :: Name
439 , gre_par :: Parent
440 , gre_lcl :: Bool -- ^ True <=> the thing was defined locally
441 , gre_imp :: [ImportSpec] -- ^ In scope through these imports
442 } deriving (Data, Typeable)
443 -- INVARIANT: either gre_lcl = True or gre_imp is non-empty
444 -- See Note [GlobalRdrElt provenance]
445
446 -- | The children of a Name are the things that are abbreviated by the ".."
447 -- notation in export lists. See Note [Parents]
448 data Parent = NoParent
449 | ParentIs { par_is :: Name }
450 | FldParent { par_is :: Name, par_lbl :: Maybe FieldLabelString }
451 -- ^ See Note [Parents for record fields]
452 | PatternSynonym
453 deriving (Eq, Data, Typeable)
454
455 instance Outputable Parent where
456 ppr NoParent = empty
457 ppr (ParentIs n) = text "parent:" <> ppr n
458 ppr (FldParent n f) = text "fldparent:"
459 <> ppr n <> colon <> ppr f
460 ppr (PatternSynonym) = text "pattern synonym"
461
462 plusParent :: Parent -> Parent -> Parent
463 -- See Note [Combining parents]
464 plusParent p1@(ParentIs _) p2 = hasParent p1 p2
465 plusParent p1@(FldParent _ _) p2 = hasParent p1 p2
466 plusParent p1 p2@(ParentIs _) = hasParent p2 p1
467 plusParent p1 p2@(FldParent _ _) = hasParent p2 p1
468 plusParent PatternSynonym PatternSynonym = PatternSynonym
469 plusParent _ _ = NoParent
470
471 hasParent :: Parent -> Parent -> Parent
472 #ifdef DEBUG
473 hasParent p NoParent = p
474 hasParent p p'
475 | p /= p' = pprPanic "hasParent" (ppr p <+> ppr p') -- Parents should agree
476 #endif
477 hasParent p _ = p
478
479
480 {- Note [GlobalRdrElt provenance]
481 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
482 The gre_lcl and gre_imp fields of a GlobalRdrElt describe its "provenance",
483 i.e. how the Name came to be in scope. It can be in scope two ways:
484 - gre_lcl = True: it is bound in this module
485 - gre_imp: a list of all the imports that brought it into scope
486
487 It's an INVARIANT that you have one or the other; that is, either
488 gre_lcl is True, or gre_imp is non-empty.
489
490 It is just possible to have *both* if there is a module loop: a Name
491 is defined locally in A, and also brought into scope by importing a
492 module that SOURCE-imported A. Exapmle (Trac #7672):
493
494 A.hs-boot module A where
495 data T
496
497 B.hs module B(Decl.T) where
498 import {-# SOURCE #-} qualified A as Decl
499
500 A.hs module A where
501 import qualified B
502 data T = Z | S B.T
503
504 In A.hs, 'T' is locally bound, *and* imported as B.T.
505
506 Note [Parents]
507 ~~~~~~~~~~~~~~~~~
508 Parent Children
509 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
510 data T Data constructors
511 Record-field ids
512
513 data family T Data constructors and record-field ids
514 of all visible data instances of T
515
516 class C Class operations
517 Associated type constructors
518
519 The `PatternSynonym` constructor is so called as pattern synonyms can be
520 bundled with any type constructor (during renaming). In other words, they can
521 have any parent.
522
523 ~~~~~~~~~~~~~~~~~~~~~~~~~
524 Constructor Meaning
525 ~~~~~~~~~~~~~~~~~~~~~~~~
526 NoParent Can not be bundled with a type constructor.
527 ParentIs n Can be bundled with the type constructor corresponding to
528 n.
529 PatternSynonym Can be bundled with any type constructor. It is so called
530 because only pattern synonyms can be bundled with any type
531 constructor.
532 FldParent See Note [Parents for record fields]
533
534
535
536
537 Note [Parents for record fields]
538 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
539 For record fields, in addition to the Name of the type constructor
540 (stored in par_is), we use FldParent to store the field label. This
541 extra information is used for identifying overloaded record fields
542 during renaming.
543
544 In a definition arising from a normal module (without
545 -XDuplicateRecordFields), par_lbl will be Nothing, meaning that the
546 field's label is the same as the OccName of the selector's Name. The
547 GlobalRdrEnv will contain an entry like this:
548
549 "x" |-> GRE x (FldParent T Nothing) LocalDef
550
551 When -XDuplicateRecordFields is enabled for the module that contains
552 T, the selector's Name will be mangled (see comments in FieldLabel).
553 Thus we store the actual field label in par_lbl, and the GlobalRdrEnv
554 entry looks like this:
555
556 "x" |-> GRE $sel:x:MkT (FldParent T (Just "x")) LocalDef
557
558 Note that the OccName used when adding a GRE to the environment
559 (greOccName) now depends on the parent field: for FldParent it is the
560 field label, if present, rather than the selector name.
561
562
563 Note [Combining parents]
564 ~~~~~~~~~~~~~~~~~~~~~~~~
565 With an associated type we might have
566 module M where
567 class C a where
568 data T a
569 op :: T a -> a
570 instance C Int where
571 data T Int = TInt
572 instance C Bool where
573 data T Bool = TBool
574
575 Then: C is the parent of T
576 T is the parent of TInt and TBool
577 So: in an export list
578 C(..) is short for C( op, T )
579 T(..) is short for T( TInt, TBool )
580
581 Module M exports everything, so its exports will be
582 AvailTC C [C,T,op]
583 AvailTC T [T,TInt,TBool]
584 On import we convert to GlobalRdrElt and then combine
585 those. For T that will mean we have
586 one GRE with Parent C
587 one GRE with NoParent
588 That's why plusParent picks the "best" case.
589 -}
590
591 -- | make a 'GlobalRdrEnv' where all the elements point to the same
592 -- Provenance (useful for "hiding" imports, or imports with no details).
593 gresFromAvails :: Maybe ImportSpec -> [AvailInfo] -> [GlobalRdrElt]
594 -- prov = Nothing => locally bound
595 -- Just spec => imported as described by spec
596 gresFromAvails prov avails
597 = concatMap (gresFromAvail (const prov)) avails
598
599 localGREsFromAvail :: AvailInfo -> [GlobalRdrElt]
600 -- Turn an Avail into a list of LocalDef GlobalRdrElts
601 localGREsFromAvail = gresFromAvail (const Nothing)
602
603 gresFromAvail :: (Name -> Maybe ImportSpec) -> AvailInfo -> [GlobalRdrElt]
604 gresFromAvail prov_fn avail
605 = map mk_gre (availNonFldNames avail) ++ map mk_fld_gre (availFlds avail)
606 where
607 mk_gre n
608 = case prov_fn n of -- Nothing => bound locally
609 -- Just is => imported from 'is'
610 Nothing -> GRE { gre_name = n, gre_par = mkParent n avail
611 , gre_lcl = True, gre_imp = [] }
612 Just is -> GRE { gre_name = n, gre_par = mkParent n avail
613 , gre_lcl = False, gre_imp = [is] }
614
615 mk_fld_gre (FieldLabel lbl is_overloaded n)
616 = case prov_fn n of -- Nothing => bound locally
617 -- Just is => imported from 'is'
618 Nothing -> GRE { gre_name = n, gre_par = FldParent (availName avail) mb_lbl
619 , gre_lcl = True, gre_imp = [] }
620 Just is -> GRE { gre_name = n, gre_par = FldParent (availName avail) mb_lbl
621 , gre_lcl = False, gre_imp = [is] }
622 where
623 mb_lbl | is_overloaded = Just lbl
624 | otherwise = Nothing
625
626
627 greQualModName :: GlobalRdrElt -> ModuleName
628 -- Get a suitable module qualifier for the GRE
629 -- (used in mkPrintUnqualified)
630 -- Prerecondition: the gre_name is always External
631 greQualModName gre@(GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss })
632 | lcl, Just mod <- nameModule_maybe name = moduleName mod
633 | (is:_) <- iss = is_as (is_decl is)
634 | otherwise = pprPanic "greQualModName" (ppr gre)
635
636 greUsedRdrName :: GlobalRdrElt -> RdrName
637 -- For imported things, return a RdrName to add to the used-RdrName
638 -- set, which is used to generate unused-import-decl warnings.
639 -- Return a Qual RdrName if poss, so that identifies the most
640 -- specific ImportSpec. See Trac #10890 for some good examples.
641 greUsedRdrName gre@GRE{ gre_name = name, gre_lcl = lcl, gre_imp = iss }
642 | lcl, Just mod <- nameModule_maybe name = Qual (moduleName mod) occ
643 | not (null iss), is <- bestImport iss = Qual (is_as (is_decl is)) occ
644 | otherwise = pprTrace "greUsedRdrName" (ppr gre) (Unqual occ)
645 where
646 occ = greOccName gre
647
648 greRdrNames :: GlobalRdrElt -> [RdrName]
649 greRdrNames gre@GRE{ gre_lcl = lcl, gre_imp = iss }
650 = (if lcl then [unqual] else []) ++ concatMap do_spec (map is_decl iss)
651 where
652 occ = greOccName gre
653 unqual = Unqual occ
654 do_spec decl_spec
655 | is_qual decl_spec = [qual]
656 | otherwise = [unqual,qual]
657 where qual = Qual (is_as decl_spec) occ
658
659 -- the SrcSpan that pprNameProvenance prints out depends on whether
660 -- the Name is defined locally or not: for a local definition the
661 -- definition site is used, otherwise the location of the import
662 -- declaration. We want to sort the export locations in
663 -- exportClashErr by this SrcSpan, we need to extract it:
664 greSrcSpan :: GlobalRdrElt -> SrcSpan
665 greSrcSpan gre@(GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss } )
666 | lcl = nameSrcSpan name
667 | (is:_) <- iss = is_dloc (is_decl is)
668 | otherwise = pprPanic "greSrcSpan" (ppr gre)
669
670 mkParent :: Name -> AvailInfo -> Parent
671 mkParent _ (Avail NotPatSyn _) = NoParent
672 mkParent _ (Avail IsPatSyn _) = PatternSynonym
673 mkParent n (AvailTC m _ _) | n == m = NoParent
674 | otherwise = ParentIs m
675
676 availFromGRE :: GlobalRdrElt -> AvailInfo
677 availFromGRE (GRE { gre_name = me, gre_par = parent })
678 = case parent of
679 ParentIs p -> AvailTC p [me] []
680 NoParent | isTyConName me -> AvailTC me [me] []
681 | otherwise -> avail me
682 FldParent p Nothing -> AvailTC p [] [FieldLabel (occNameFS $ nameOccName me) False me]
683 FldParent p (Just lbl) -> AvailTC p [] [FieldLabel lbl True me]
684 PatternSynonym -> patSynAvail me
685
686 emptyGlobalRdrEnv :: GlobalRdrEnv
687 emptyGlobalRdrEnv = emptyOccEnv
688
689 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
690 globalRdrEnvElts env = foldOccEnv (++) [] env
691
692 instance Outputable GlobalRdrElt where
693 ppr gre = hang (ppr (gre_name gre) <+> ppr (gre_par gre))
694 2 (pprNameProvenance gre)
695
696 pprGlobalRdrEnv :: Bool -> GlobalRdrEnv -> SDoc
697 pprGlobalRdrEnv locals_only env
698 = vcat [ text "GlobalRdrEnv" <+> ppWhen locals_only (ptext (sLit "(locals only)"))
699 <+> lbrace
700 , nest 2 (vcat [ pp (remove_locals gre_list) | gre_list <- occEnvElts env ]
701 <+> rbrace) ]
702 where
703 remove_locals gres | locals_only = filter isLocalGRE gres
704 | otherwise = gres
705 pp [] = empty
706 pp gres = hang (ppr occ
707 <+> parens (text "unique" <+> ppr (getUnique occ))
708 <> colon)
709 2 (vcat (map ppr gres))
710 where
711 occ = nameOccName (gre_name (head gres))
712
713 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
714 lookupGlobalRdrEnv env occ_name = case lookupOccEnv env occ_name of
715 Nothing -> []
716 Just gres -> gres
717
718 greOccName :: GlobalRdrElt -> OccName
719 greOccName (GRE{gre_par = FldParent{par_lbl = Just lbl}}) = mkVarOccFS lbl
720 greOccName gre = nameOccName (gre_name gre)
721
722 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
723 lookupGRE_RdrName rdr_name env
724 = case lookupOccEnv env (rdrNameOcc rdr_name) of
725 Nothing -> []
726 Just gres -> pickGREs rdr_name gres
727
728 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
729 lookupGRE_Name env name
730 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
731 gre_name gre == name ]
732
733 lookupGRE_Field_Name :: GlobalRdrEnv -> Name -> FastString -> [GlobalRdrElt]
734 -- Used when looking up record fields, where the selector name and
735 -- field label are different: the GlobalRdrEnv is keyed on the label
736 lookupGRE_Field_Name env sel_name lbl
737 = [ gre | gre <- lookupGlobalRdrEnv env (mkVarOccFS lbl),
738 gre_name gre == sel_name ]
739
740
741 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
742 -- Returns all the qualifiers by which 'x' is in scope
743 -- Nothing means "the unqualified version is in scope"
744 -- [] means the thing is not in scope at all
745 getGRE_NameQualifier_maybes env
746 = map (qualifier_maybe) . lookupGRE_Name env
747 where
748 qualifier_maybe (GRE { gre_lcl = lcl, gre_imp = iss })
749 | lcl = Nothing
750 | otherwise = Just $ map (is_as . is_decl) iss
751
752 isLocalGRE :: GlobalRdrElt -> Bool
753 isLocalGRE (GRE {gre_lcl = lcl }) = lcl
754
755 isRecFldGRE :: GlobalRdrElt -> Bool
756 isRecFldGRE (GRE {gre_par = FldParent{}}) = True
757 isRecFldGRE _ = False
758
759 -- Returns the field label of this GRE, if it has one
760 greLabel :: GlobalRdrElt -> Maybe FieldLabelString
761 greLabel (GRE{gre_par = FldParent{par_lbl = Just lbl}}) = Just lbl
762 greLabel (GRE{gre_name = n, gre_par = FldParent{}}) = Just (occNameFS (nameOccName n))
763 greLabel _ = Nothing
764
765 unQualOK :: GlobalRdrElt -> Bool
766 -- ^ Test if an unqualifed version of this thing would be in scope
767 unQualOK (GRE {gre_lcl = lcl, gre_imp = iss })
768 | lcl = True
769 | otherwise = any unQualSpecOK iss
770
771 {- Note [GRE filtering]
772 ~~~~~~~~~~~~~~~~~~~~~~~
773 (pickGREs rdr gres) takes a list of GREs which have the same OccName
774 as 'rdr', say "x". It does two things:
775
776 (a) filters the GREs to a subset that are in scope
777 * Qualified, as 'M.x' if want_qual is Qual M _
778 * Unqualified, as 'x' if want_unqual is Unqual _
779
780 (b) for that subset, filter the provenance field (gre_lcl and gre_imp)
781 to ones that brought it into scope qualifed or unqualified resp.
782
783 Example:
784 module A ( f ) where
785 import qualified Foo( f )
786 import Baz( f )
787 f = undefined
788
789 Let's suppose that Foo.f and Baz.f are the same entity really, but the local
790 'f' is different, so there will be two GREs matching "f":
791 gre1: gre_lcl = True, gre_imp = []
792 gre2: gre_lcl = False, gre_imp = [ imported from Foo, imported from Bar ]
793
794 The use of "f" in the export list is ambiguous because it's in scope
795 from the local def and the import Baz(f); but *not* the import qualified Foo.
796 pickGREs returns two GRE
797 gre1: gre_lcl = True, gre_imp = []
798 gre2: gre_lcl = False, gre_imp = [ imported from Bar ]
799
800 Now the the "ambiguous occurrence" message can correctly report how the
801 ambiguity arises.
802 -}
803
804 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
805 -- ^ Takes a list of GREs which have the right OccName 'x'
806 -- Pick those GREs that are are in scope
807 -- * Qualified, as 'M.x' if want_qual is Qual M _
808 -- * Unqualified, as 'x' if want_unqual is Unqual _
809 --
810 -- Return each such GRE, with its ImportSpecs filtered, to reflect
811 -- how it is in scope qualifed or unqualified respectively.
812 -- See Note [GRE filtering]
813 pickGREs (Unqual {}) gres = mapMaybe pickUnqualGRE gres
814 pickGREs (Qual mod _) gres = mapMaybe (pickQualGRE mod) gres
815 pickGREs _ _ = [] -- I don't think this actually happens
816
817 pickUnqualGRE :: GlobalRdrElt -> Maybe GlobalRdrElt
818 pickUnqualGRE gre@(GRE { gre_lcl = lcl, gre_imp = iss })
819 | not lcl, null iss' = Nothing
820 | otherwise = Just (gre { gre_imp = iss' })
821 where
822 iss' = filter unQualSpecOK iss
823
824 pickQualGRE :: ModuleName -> GlobalRdrElt -> Maybe GlobalRdrElt
825 pickQualGRE mod gre@(GRE { gre_name = n, gre_lcl = lcl, gre_imp = iss })
826 | not lcl', null iss' = Nothing
827 | otherwise = Just (gre { gre_lcl = lcl', gre_imp = iss' })
828 where
829 iss' = filter (qualSpecOK mod) iss
830 lcl' = lcl && name_is_from mod n
831
832 name_is_from :: ModuleName -> Name -> Bool
833 name_is_from mod name = case nameModule_maybe name of
834 Just n_mod -> moduleName n_mod == mod
835 Nothing -> False
836
837 pickGREsModExp :: ModuleName -> [GlobalRdrElt] -> [(GlobalRdrElt,GlobalRdrElt)]
838 -- ^ Pick GREs that are in scope *both* qualified *and* unqualified
839 -- Return each GRE that is, as a pair
840 -- (qual_gre, unqual_gre)
841 -- These two GREs are the original GRE with imports filtered to express how
842 -- it is in scope qualified an unqualified respectively
843 --
844 -- Used only for the 'module M' item in export list;
845 -- see RnNames.exports_from_avail
846 pickGREsModExp mod gres = mapMaybe (pickBothGRE mod) gres
847
848 pickBothGRE :: ModuleName -> GlobalRdrElt -> Maybe (GlobalRdrElt, GlobalRdrElt)
849 pickBothGRE mod gre@(GRE { gre_name = n })
850 | isBuiltInSyntax n = Nothing
851 | Just gre1 <- pickQualGRE mod gre
852 , Just gre2 <- pickUnqualGRE gre = Just (gre1, gre2)
853 | otherwise = Nothing
854 where
855 -- isBuiltInSyntax filter out names for built-in syntax They
856 -- just clutter up the environment (esp tuples), and the
857 -- parser will generate Exact RdrNames for them, so the
858 -- cluttered envt is no use. Really, it's only useful for
859 -- GHC.Base and GHC.Tuple.
860
861 -- Building GlobalRdrEnvs
862
863 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
864 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
865
866 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
867 mkGlobalRdrEnv gres
868 = foldr add emptyGlobalRdrEnv gres
869 where
870 add gre env = extendOccEnv_Acc insertGRE singleton env
871 (greOccName gre)
872 gre
873
874 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
875 insertGRE new_g [] = [new_g]
876 insertGRE new_g (old_g : old_gs)
877 | gre_name new_g == gre_name old_g
878 = new_g `plusGRE` old_g : old_gs
879 | otherwise
880 = old_g : insertGRE new_g old_gs
881
882 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
883 -- Used when the gre_name fields match
884 plusGRE g1 g2
885 = GRE { gre_name = gre_name g1
886 , gre_lcl = gre_lcl g1 || gre_lcl g2
887 , gre_imp = gre_imp g1 ++ gre_imp g2
888 , gre_par = gre_par g1 `plusParent` gre_par g2 }
889
890 transformGREs :: (GlobalRdrElt -> GlobalRdrElt)
891 -> [OccName]
892 -> GlobalRdrEnv -> GlobalRdrEnv
893 -- ^ Apply a transformation function to the GREs for these OccNames
894 transformGREs trans_gre occs rdr_env
895 = foldr trans rdr_env occs
896 where
897 trans occ env
898 = case lookupOccEnv env occ of
899 Just gres -> extendOccEnv env occ (map trans_gre gres)
900 Nothing -> env
901
902 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
903 extendGlobalRdrEnv env gre
904 = extendOccEnv_Acc insertGRE singleton env
905 (greOccName gre) gre
906
907 shadowNames :: GlobalRdrEnv -> [Name] -> GlobalRdrEnv
908 shadowNames = foldl shadowName
909
910 {- Note [GlobalRdrEnv shadowing]
911 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
912 Before adding new names to the GlobalRdrEnv we nuke some existing entries;
913 this is "shadowing". The actual work is done by RdrEnv.shadowNames.
914 There are two reasons for shadowing:
915
916 * The GHCi REPL
917
918 - Ids bought into scope on the command line (eg let x = True) have
919 External Names, like Ghci4.x. We want a new binding for 'x' (say)
920 to override the existing binding for 'x'.
921 See Note [Interactively-bound Ids in GHCi] in HscTypes
922
923 - Data types also have Extenal Names, like Ghci4.T; but we still want
924 'T' to mean the newly-declared 'T', not an old one.
925
926 * Nested Template Haskell declaration brackets
927 See Note [Top-level Names in Template Haskell decl quotes] in RnNames
928
929 Consider a TH decl quote:
930 module M where
931 f x = h [d| f = 3 |]
932 We must shadow the outer declaration of 'f', else we'll get a
933 complaint when extending the GlobalRdrEnv, saying that there are two
934 bindings for 'f'. There are several tricky points:
935
936 - This shadowing applies even if the binding for 'f' is in a
937 where-clause, and hence is in the *local* RdrEnv not the *global*
938 RdrEnv. This is done in lcl_env_TH in extendGlobalRdrEnvRn.
939
940 - The External Name M.f from the enclosing module must certainly
941 still be available. So we don't nuke it entirely; we just make
942 it seem like qualified import.
943
944 - We only shadow *External* names (which come from the main module),
945 or from earlier GHCi commands. Do not shadow *Internal* names
946 because in the bracket
947 [d| class C a where f :: a
948 f = 4 |]
949 rnSrcDecls will first call extendGlobalRdrEnvRn with C[f] from the
950 class decl, and *separately* extend the envt with the value binding.
951 At that stage, the class op 'f' will have an Internal name.
952 -}
953
954 shadowName :: GlobalRdrEnv -> Name -> GlobalRdrEnv
955 -- Remove certain old GREs that share the same OccName as this new Name.
956 -- See Note [GlobalRdrEnv shadowing] for details
957 shadowName env name
958 = alterOccEnv (fmap alter_fn) env (nameOccName name)
959 where
960 alter_fn :: [GlobalRdrElt] -> [GlobalRdrElt]
961 alter_fn gres = mapMaybe (shadow_with name) gres
962
963 shadow_with :: Name -> GlobalRdrElt -> Maybe GlobalRdrElt
964 shadow_with new_name
965 old_gre@(GRE { gre_name = old_name, gre_lcl = lcl, gre_imp = iss })
966 = case nameModule_maybe old_name of
967 Nothing -> Just old_gre -- Old name is Internal; do not shadow
968 Just old_mod
969 | Just new_mod <- nameModule_maybe new_name
970 , new_mod == old_mod -- Old name same as new name; shadow completely
971 -> Nothing
972
973 | null iss' -- Nothing remains
974 -> Nothing
975
976 | otherwise
977 -> Just (old_gre { gre_lcl = False, gre_imp = iss' })
978
979 where
980 iss' = lcl_imp ++ mapMaybe (shadow_is new_name) iss
981 lcl_imp | lcl = [mk_fake_imp_spec old_name old_mod]
982 | otherwise = []
983
984 mk_fake_imp_spec old_name old_mod -- Urgh!
985 = ImpSpec id_spec ImpAll
986 where
987 old_mod_name = moduleName old_mod
988 id_spec = ImpDeclSpec { is_mod = old_mod_name
989 , is_as = old_mod_name
990 , is_qual = True
991 , is_dloc = nameSrcSpan old_name }
992
993 shadow_is :: Name -> ImportSpec -> Maybe ImportSpec
994 shadow_is new_name is@(ImpSpec { is_decl = id_spec })
995 | Just new_mod <- nameModule_maybe new_name
996 , is_as id_spec == moduleName new_mod
997 = Nothing -- Shadow both qualified and unqualified
998 | otherwise -- Shadow unqualified only
999 = Just (is { is_decl = id_spec { is_qual = True } })
1000
1001
1002 {-
1003 ************************************************************************
1004 * *
1005 ImportSpec
1006 * *
1007 ************************************************************************
1008 -}
1009
1010 -- | The 'ImportSpec' of something says how it came to be imported
1011 -- It's quite elaborate so that we can give accurate unused-name warnings.
1012 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
1013 is_item :: ImpItemSpec }
1014 deriving( Eq, Ord, Data, Typeable )
1015
1016 -- | Describes a particular import declaration and is
1017 -- shared among all the 'Provenance's for that decl
1018 data ImpDeclSpec
1019 = ImpDeclSpec {
1020 is_mod :: ModuleName, -- ^ Module imported, e.g. @import Muggle@
1021 -- Note the @Muggle@ may well not be
1022 -- the defining module for this thing!
1023
1024 -- TODO: either should be Module, or there
1025 -- should be a Maybe UnitId here too.
1026 is_as :: ModuleName, -- ^ Import alias, e.g. from @as M@ (or @Muggle@ if there is no @as@ clause)
1027 is_qual :: Bool, -- ^ Was this import qualified?
1028 is_dloc :: SrcSpan -- ^ The location of the entire import declaration
1029 } deriving (Data, Typeable)
1030
1031 -- | Describes import info a particular Name
1032 data ImpItemSpec
1033 = ImpAll -- ^ The import had no import list,
1034 -- or had a hiding list
1035
1036 | ImpSome {
1037 is_explicit :: Bool,
1038 is_iloc :: SrcSpan -- Location of the import item
1039 } -- ^ The import had an import list.
1040 -- The 'is_explicit' field is @True@ iff the thing was named
1041 -- /explicitly/ in the import specs rather
1042 -- than being imported as part of a "..." group. Consider:
1043 --
1044 -- > import C( T(..) )
1045 --
1046 -- Here the constructors of @T@ are not named explicitly;
1047 -- only @T@ is named explicitly.
1048 deriving (Data, Typeable)
1049
1050 instance Eq ImpDeclSpec where
1051 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
1052
1053 instance Ord ImpDeclSpec where
1054 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
1055 (is_dloc is1 `compare` is_dloc is2)
1056
1057 instance Eq ImpItemSpec where
1058 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
1059
1060 instance Ord ImpItemSpec where
1061 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
1062
1063
1064 bestImport :: [ImportSpec] -> ImportSpec
1065 -- Given a non-empty bunch of ImportSpecs, return the one that
1066 -- imported the item most specficially (e.g. by name), using
1067 -- textually-first as a tie breaker. This is used when reporting
1068 -- redundant imports
1069 bestImport iss
1070 = case sortBy best iss of
1071 (is:_) -> is
1072 [] -> pprPanic "bestImport" (ppr iss)
1073 where
1074 best :: ImportSpec -> ImportSpec -> Ordering
1075 -- Less means better
1076 best (ImpSpec { is_item = item1, is_decl = d1 })
1077 (ImpSpec { is_item = item2, is_decl = d2 })
1078 = best_item item1 item2 `thenCmp` (is_dloc d1 `compare` is_dloc d2)
1079
1080 best_item :: ImpItemSpec -> ImpItemSpec -> Ordering
1081 best_item ImpAll ImpAll = EQ
1082 best_item ImpAll (ImpSome {}) = GT
1083 best_item (ImpSome {}) ImpAll = LT
1084 best_item (ImpSome { is_explicit = e1 })
1085 (ImpSome { is_explicit = e2 }) = e2 `compare` e1
1086 -- False < True, so if e1 is explicit and e2 is not, we get LT
1087
1088 unQualSpecOK :: ImportSpec -> Bool
1089 -- ^ Is in scope unqualified?
1090 unQualSpecOK is = not (is_qual (is_decl is))
1091
1092 qualSpecOK :: ModuleName -> ImportSpec -> Bool
1093 -- ^ Is in scope qualified with the given module?
1094 qualSpecOK mod is = mod == is_as (is_decl is)
1095
1096 importSpecLoc :: ImportSpec -> SrcSpan
1097 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
1098 importSpecLoc (ImpSpec _ item) = is_iloc item
1099
1100 importSpecModule :: ImportSpec -> ModuleName
1101 importSpecModule is = is_mod (is_decl is)
1102
1103 isExplicitItem :: ImpItemSpec -> Bool
1104 isExplicitItem ImpAll = False
1105 isExplicitItem (ImpSome {is_explicit = exp}) = exp
1106
1107 pprNameProvenance :: GlobalRdrElt -> SDoc
1108 -- ^ Print out one place where the name was define/imported
1109 -- (With -dppr-debug, print them all)
1110 pprNameProvenance (GRE { gre_name = name, gre_lcl = lcl, gre_imp = iss })
1111 | opt_PprStyle_Debug = vcat pp_provs
1112 | otherwise = head pp_provs
1113 where
1114 pp_provs = pp_lcl ++ map pp_is iss
1115 pp_lcl = if lcl then [text "defined at" <+> ppr (nameSrcLoc name)]
1116 else []
1117 pp_is is = sep [ppr is, ppr_defn_site is name]
1118
1119 -- If we know the exact definition point (which we may do with GHCi)
1120 -- then show that too. But not if it's just "imported from X".
1121 ppr_defn_site :: ImportSpec -> Name -> SDoc
1122 ppr_defn_site imp_spec name
1123 | same_module && not (isGoodSrcSpan loc)
1124 = empty -- Nothing interesting to say
1125 | otherwise
1126 = parens $ hang (text "and originally defined" <+> pp_mod)
1127 2 (pprLoc loc)
1128 where
1129 loc = nameSrcSpan name
1130 defining_mod = nameModule name
1131 same_module = importSpecModule imp_spec == moduleName defining_mod
1132 pp_mod | same_module = empty
1133 | otherwise = text "in" <+> quotes (ppr defining_mod)
1134
1135
1136 instance Outputable ImportSpec where
1137 ppr imp_spec
1138 = text "imported" <+> qual
1139 <+> text "from" <+> quotes (ppr (importSpecModule imp_spec))
1140 <+> pprLoc (importSpecLoc imp_spec)
1141 where
1142 qual | is_qual (is_decl imp_spec) = text "qualified"
1143 | otherwise = empty
1144
1145 pprLoc :: SrcSpan -> SDoc
1146 pprLoc (RealSrcSpan s) = text "at" <+> ppr s
1147 pprLoc (UnhelpfulSpan {}) = empty