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