2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
10 -- GHC uses several kinds of name internally:
12 -- * 'OccName.OccName': see "OccName#name_types"
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
19 -- * 'Name.Name': see "Name#name_types"
21 -- * 'Id.Id': see "Id#name_types"
23 -- * 'Var.Var': see "Var#name_types"
26 RdrName(..), -- Constructors exported only to BinIface
29 mkRdrUnqual, mkRdrQual,
30 mkUnqual, mkVarUnqual, mkQual, mkOrig,
31 nameRdrName, getRdrName,
35 rdrNameOcc, rdrNameSpace, setRdrNameSpace,
36 isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual,
37 isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
42 -- * Local mapping of 'RdrName' to 'Name.Name'
43 LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv, extendLocalRdrEnvList,
44 lookupLocalRdrEnv, lookupLocalRdrOcc, elemLocalRdrEnv,
46 -- * Global mapping of 'RdrName' to 'GlobalRdrElt's
47 GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv,
48 lookupGlobalRdrEnv, extendGlobalRdrEnv,
49 pprGlobalRdrEnv, globalRdrEnvElts,
50 lookupGRE_RdrName, lookupGRE_Name, getGRE_NameQualifier_maybes,
51 hideSomeUnquals, findLocalDupsRdrEnv, pickGREs,
53 -- ** Global 'RdrName' mapping elements: 'GlobalRdrElt', 'Provenance', 'ImportSpec'
54 GlobalRdrElt(..), isLocalGRE, unQualOK, qualSpecOK, unQualSpecOK,
55 Provenance(..), pprNameProvenance,
57 ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
58 importSpecLoc, importSpecModule, isExplicitItem
61 #include "HsVersions.h"
72 %************************************************************************
74 \subsection{The main data type}
76 %************************************************************************
79 -- | Do not use the data constructors of RdrName directly: prefer the family
80 -- of functions that creates them, such as 'mkRdrUnqual'
83 -- ^ Used for ordinary, unqualified occurrences, e.g. @x@, @y@ or @Foo@.
84 -- Create such a 'RdrName' with 'mkRdrUnqual'
86 | Qual ModuleName OccName
87 -- ^ A qualified name written by the user in
88 -- /source/ code. The module isn't necessarily
89 -- the module where the thing is defined;
90 -- just the one from which it is imported.
91 -- Examples are @Bar.x@, @Bar.y@ or @Bar.Foo@.
92 -- Create such a 'RdrName' with 'mkRdrQual'
95 -- ^ An original name; the module is the /defining/ module.
96 -- This is used when GHC generates code that will be fed
97 -- into the renamer (e.g. from deriving clauses), but where
98 -- we want to say \"Use Prelude.map dammit\". One of these
99 -- can be created with 'mkOrig'
102 -- ^ We know exactly the 'Name'. This is used:
104 -- (1) When the parser parses built-in syntax like @[]@
105 -- and @(,)@, but wants a 'RdrName' from it
107 -- (2) By Template Haskell, when TH has generated a unique name
109 -- Such a 'RdrName' can be created by using 'getRdrName' on a 'Name'
113 %************************************************************************
115 \subsection{Simple functions}
117 %************************************************************************
120 rdrNameOcc :: RdrName -> OccName
121 rdrNameOcc (Qual _ occ) = occ
122 rdrNameOcc (Unqual occ) = occ
123 rdrNameOcc (Orig _ occ) = occ
124 rdrNameOcc (Exact name) = nameOccName name
126 rdrNameSpace :: RdrName -> NameSpace
127 rdrNameSpace = occNameSpace . rdrNameOcc
129 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
130 -- ^ This rather gruesome function is used mainly by the parser.
133 -- > data T a = T | T1 Int
135 -- we parse the data constructors as /types/ because of parser ambiguities,
136 -- so then we need to change the /type constr/ to a /data constr/
138 -- The exact-name case /can/ occur when parsing:
140 -- > data [] a = [] | a : [a]
142 -- For the exact-name case we return an original name.
143 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
144 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
145 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
146 setRdrNameSpace (Exact n) ns = ASSERT( isExternalName n )
148 (setOccNameSpace ns (nameOccName n))
152 -- These two are the basic constructors
153 mkRdrUnqual :: OccName -> RdrName
154 mkRdrUnqual occ = Unqual occ
156 mkRdrQual :: ModuleName -> OccName -> RdrName
157 mkRdrQual mod occ = Qual mod occ
159 mkOrig :: Module -> OccName -> RdrName
160 mkOrig mod occ = Orig mod occ
163 -- | Produce an original 'RdrName' whose module that of a parent 'Name' but its 'OccName'
164 -- is derived from that of it's parent using the supplied function
165 mkDerivedRdrName :: Name -> (OccName -> OccName) -> RdrName
166 mkDerivedRdrName parent mk_occ
167 = ASSERT2( isExternalName parent, ppr parent )
168 mkOrig (nameModule parent) (mk_occ (nameOccName parent))
171 -- These two are used when parsing source files
172 -- They do encode the module and occurrence names
173 mkUnqual :: NameSpace -> FastString -> RdrName
174 mkUnqual sp n = Unqual (mkOccNameFS sp n)
176 mkVarUnqual :: FastString -> RdrName
177 mkVarUnqual n = Unqual (mkVarOccFS n)
179 -- | Make a qualified 'RdrName' in the given namespace and where the 'ModuleName' and
180 -- the 'OccName' are taken from the first and second elements of the tuple respectively
181 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
182 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
184 getRdrName :: NamedThing thing => thing -> RdrName
185 getRdrName name = nameRdrName (getName name)
187 nameRdrName :: Name -> RdrName
188 nameRdrName name = Exact name
189 -- Keep the Name even for Internal names, so that the
190 -- unique is still there for debug printing, particularly
191 -- of Types (which are converted to IfaceTypes before printing)
193 nukeExact :: Name -> RdrName
195 | isExternalName n = Orig (nameModule n) (nameOccName n)
196 | otherwise = Unqual (nameOccName n)
200 isRdrDataCon :: RdrName -> Bool
201 isRdrTyVar :: RdrName -> Bool
202 isRdrTc :: RdrName -> Bool
204 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
205 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
206 isRdrTc rn = isTcOcc (rdrNameOcc rn)
208 isSrcRdrName :: RdrName -> Bool
209 isSrcRdrName (Unqual _) = True
210 isSrcRdrName (Qual _ _) = True
211 isSrcRdrName _ = False
213 isUnqual :: RdrName -> Bool
214 isUnqual (Unqual _) = True
217 isQual :: RdrName -> Bool
218 isQual (Qual _ _) = True
221 isQual_maybe :: RdrName -> Maybe (ModuleName, OccName)
222 isQual_maybe (Qual m n) = Just (m,n)
223 isQual_maybe _ = Nothing
225 isOrig :: RdrName -> Bool
226 isOrig (Orig _ _) = True
229 isOrig_maybe :: RdrName -> Maybe (Module, OccName)
230 isOrig_maybe (Orig m n) = Just (m,n)
231 isOrig_maybe _ = Nothing
233 isExact :: RdrName -> Bool
234 isExact (Exact _) = True
237 isExact_maybe :: RdrName -> Maybe Name
238 isExact_maybe (Exact n) = Just n
239 isExact_maybe _ = Nothing
243 %************************************************************************
245 \subsection{Instances}
247 %************************************************************************
250 instance Outputable RdrName where
251 ppr (Exact name) = ppr name
252 ppr (Unqual occ) = ppr occ
253 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
254 ppr (Orig mod occ) = getPprStyle (\sty -> pprModulePrefix sty mod occ <> ppr occ)
256 instance OutputableBndr RdrName where
258 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
261 showRdrName :: RdrName -> String
262 showRdrName r = showSDoc (ppr r)
264 instance Eq RdrName where
265 (Exact n1) == (Exact n2) = n1==n2
266 -- Convert exact to orig
267 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
268 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
270 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
271 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
272 (Unqual o1) == (Unqual o2) = o1==o2
275 instance Ord RdrName where
276 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
277 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
278 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
279 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
281 -- Exact < Unqual < Qual < Orig
282 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
283 -- before comparing so that Prelude.map == the exact Prelude.map, but
284 -- that meant that we reported duplicates when renaming bindings
285 -- generated by Template Haskell; e.g
286 -- do { n1 <- newName "foo"; n2 <- newName "foo";
287 -- <decl involving n1,n2> }
288 -- I think we can do without this conversion
289 compare (Exact n1) (Exact n2) = n1 `compare` n2
290 compare (Exact _) _ = LT
292 compare (Unqual _) (Exact _) = GT
293 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
294 compare (Unqual _) _ = LT
296 compare (Qual _ _) (Exact _) = GT
297 compare (Qual _ _) (Unqual _) = GT
298 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
299 compare (Qual _ _) (Orig _ _) = LT
301 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
302 compare (Orig _ _) _ = GT
305 %************************************************************************
309 %************************************************************************
312 -- | This environment is used to store local bindings (@let@, @where@, lambda, @case@).
313 -- It is keyed by OccName, because we never use it for qualified names
314 type LocalRdrEnv = OccEnv Name
316 emptyLocalRdrEnv :: LocalRdrEnv
317 emptyLocalRdrEnv = emptyOccEnv
319 extendLocalRdrEnv :: LocalRdrEnv -> Name -> LocalRdrEnv
320 extendLocalRdrEnv env name
321 = extendOccEnv env (nameOccName name) name
323 extendLocalRdrEnvList :: LocalRdrEnv -> [Name] -> LocalRdrEnv
324 extendLocalRdrEnvList env names
325 = extendOccEnvList env [(nameOccName n, n) | n <- names]
327 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
328 lookupLocalRdrEnv _ (Exact name) = Just name
329 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
330 lookupLocalRdrEnv _ _ = Nothing
332 lookupLocalRdrOcc :: LocalRdrEnv -> OccName -> Maybe Name
333 lookupLocalRdrOcc env occ = lookupOccEnv env occ
335 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
336 elemLocalRdrEnv rdr_name env
337 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
341 %************************************************************************
345 %************************************************************************
348 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
349 -- ^ Keyed by 'OccName'; when looking up a qualified name
350 -- we look up the 'OccName' part, and then check the 'Provenance'
351 -- to see if the appropriate qualification is valid. This
352 -- saves routinely doubling the size of the env by adding both
353 -- qualified and unqualified names to the domain.
355 -- The list in the codomain is required because there may be name clashes
356 -- These only get reported on lookup, not on construction
358 -- INVARIANT: All the members of the list have distinct
359 -- 'gre_name' fields; that is, no duplicate Names
361 -- INVARIANT: Imported provenance => Name is an ExternalName
362 -- However LocalDefs can have an InternalName. This
363 -- happens only when type-checking a [d| ... |] Template
364 -- Haskell quotation; see this note in RnNames
365 -- Note [Top-level Names in Template Haskell decl quotes]
367 -- | An element of the 'GlobalRdrEnv'
369 = GRE { gre_name :: Name,
371 gre_prov :: Provenance -- ^ Why it's in scope
374 -- | The children of a Name are the things that are abbreviated by the ".."
375 -- notation in export lists. Specifically:
376 -- TyCon Children are * data constructors
377 -- * record field ids
378 -- Class Children are * class operations
379 -- Each child has the parent thing as its Parent
380 data Parent = NoParent | ParentIs Name
383 instance Outputable Parent where
385 ppr (ParentIs n) = ptext (sLit "parent:") <> ppr n
388 plusParent :: Parent -> Parent -> Parent
389 plusParent p1 p2 = ASSERT2( p1 == p2, parens (ppr p1) <+> parens (ppr p2) )
392 {- Why so complicated? -=chak
393 plusParent :: Parent -> Parent -> Parent
394 plusParent NoParent rel =
395 ASSERT2( case rel of { NoParent -> True; other -> False },
396 ptext (sLit "plusParent[NoParent]: ") <+> ppr rel )
398 plusParent (ParentIs n) rel =
399 ASSERT2( case rel of { ParentIs m -> n==m; other -> False },
400 ptext (sLit "plusParent[ParentIs]:") <+> ppr n <> comma <+> ppr rel )
404 emptyGlobalRdrEnv :: GlobalRdrEnv
405 emptyGlobalRdrEnv = emptyOccEnv
407 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
408 globalRdrEnvElts env = foldOccEnv (++) [] env
410 instance Outputable GlobalRdrElt where
411 ppr gre = ppr name <+> parens (ppr (gre_par gre) <+> pprNameProvenance gre)
415 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
417 = vcat (map pp (occEnvElts env))
419 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
420 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
425 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
426 lookupGlobalRdrEnv env occ_name = case lookupOccEnv env occ_name of
430 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
431 extendGlobalRdrEnv env gre = extendOccEnv_Acc (:) singleton env occ gre
433 occ = nameOccName (gre_name gre)
435 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
436 lookupGRE_RdrName rdr_name env
437 = case lookupOccEnv env (rdrNameOcc rdr_name) of
439 Just gres -> pickGREs rdr_name gres
441 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
442 lookupGRE_Name env name
443 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
444 gre_name gre == name ]
446 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
447 getGRE_NameQualifier_maybes env
448 = map qualifier_maybe . map gre_prov . lookupGRE_Name env
449 where qualifier_maybe LocalDef = Nothing
450 qualifier_maybe (Imported iss) = Just $ map (is_as . is_decl) iss
452 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
453 -- ^ Take a list of GREs which have the right OccName
454 -- Pick those GREs that are suitable for this RdrName
455 -- And for those, keep only only the Provenances that are suitable
460 -- module A ( f ) where
461 -- import qualified Foo( f )
466 -- Let's suppose that @Foo.f@ and @Baz.f@ are the same entity really.
467 -- The export of @f@ is ambiguous because it's in scope from the local def
468 -- and the import. The lookup of @Unqual f@ should return a GRE for
469 -- the locally-defined @f@, and a GRE for the imported @f@, with a /single/
470 -- provenance, namely the one for @Baz(f)@.
471 pickGREs rdr_name gres
472 = mapCatMaybes pick gres
474 rdr_is_unqual = isUnqual rdr_name
475 rdr_is_qual = isQual_maybe rdr_name
477 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
478 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
479 | rdr_is_unqual = Just gre
480 | Just (mod,_) <- rdr_is_qual -- Qualified name
481 , Just n_mod <- nameModule_maybe n -- Binder is External
482 , mod == moduleName n_mod = Just gre
483 | otherwise = Nothing
484 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
486 not (is_qual (is_decl is)) = Just gre
487 | Just (mod,_) <- rdr_is_qual,
488 mod == is_as (is_decl is) = Just gre
489 | otherwise = Nothing
490 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
491 | null filtered_is = Nothing
492 | otherwise = Just (gre {gre_prov = Imported filtered_is})
494 filtered_is | rdr_is_unqual
495 = filter (not . is_qual . is_decl) is
496 | Just (mod,_) <- rdr_is_qual
497 = filter ((== mod) . is_as . is_decl) is
501 isLocalGRE :: GlobalRdrElt -> Bool
502 isLocalGRE (GRE {gre_prov = LocalDef}) = True
505 unQualOK :: GlobalRdrElt -> Bool
506 -- ^ Test if an unqualifed version of this thing would be in scope
507 unQualOK (GRE {gre_prov = LocalDef}) = True
508 unQualOK (GRE {gre_prov = Imported is}) = any unQualSpecOK is
510 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
511 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
513 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
515 = foldr add emptyGlobalRdrEnv gres
517 add gre env = extendOccEnv_Acc insertGRE singleton env
518 (nameOccName (gre_name gre))
521 findLocalDupsRdrEnv :: GlobalRdrEnv -> [OccName] -> (GlobalRdrEnv, [[Name]])
522 -- ^ For each 'OccName', see if there are multiple local definitions
523 -- for it. If so, remove all but one (to suppress subsequent error messages)
524 -- and return a list of the duplicate bindings
525 findLocalDupsRdrEnv rdr_env occs
528 go rdr_env dups [] = (rdr_env, dups)
529 go rdr_env dups (occ:occs)
530 = case filter isLocalGRE gres of
531 [] -> WARN( True, ppr occ <+> ppr rdr_env )
532 go rdr_env dups occs -- Weird! No binding for occ
533 [_] -> go rdr_env dups occs -- The common case
534 dup_gres -> go (extendOccEnv rdr_env occ (head dup_gres : nonlocal_gres))
535 (map gre_name dup_gres : dups)
538 gres = lookupOccEnv rdr_env occ `orElse` []
539 nonlocal_gres = filterOut isLocalGRE gres
542 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
543 insertGRE new_g [] = [new_g]
544 insertGRE new_g (old_g : old_gs)
545 | gre_name new_g == gre_name old_g
546 = new_g `plusGRE` old_g : old_gs
548 = old_g : insertGRE new_g old_gs
550 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
551 -- Used when the gre_name fields match
553 = GRE { gre_name = gre_name g1,
554 gre_prov = gre_prov g1 `plusProv` gre_prov g2,
555 gre_par = gre_par g1 `plusParent` gre_par g2 }
557 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
558 -- ^ Hide any unqualified bindings for the specified OccNames
559 -- This is used in TH, when renaming a declaration bracket
561 -- > [d| foo = ... |]
563 -- We want unqualified @foo@ in "..." to mean this @foo@, not
564 -- the one from the enclosing module. But the /qualified/ name
565 -- from the enclosing module must certainly still be available
567 -- Seems like 5 times as much work as it deserves!
568 hideSomeUnquals rdr_env occs
569 = foldr hide rdr_env occs
572 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
574 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
575 = gre { gre_prov = Imported [imp_spec] }
576 where -- Local defs get transfomed to (fake) imported things
577 mod = ASSERT2( isExternalName name, ppr name) moduleName (nameModule name)
578 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
579 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
581 is_dloc = srcLocSpan (nameSrcLoc name) }
583 qual_gre gre@(GRE { gre_prov = Imported specs })
584 = gre { gre_prov = Imported (map qual_spec specs) }
586 qual_spec spec@(ImpSpec { is_decl = decl_spec })
587 = spec { is_decl = decl_spec { is_qual = True } }
590 %************************************************************************
594 %************************************************************************
597 -- | The 'Provenance' of something says how it came to be in scope.
598 -- It's quite elaborate so that we can give accurate unused-name warnings.
600 = LocalDef -- ^ The thing was defined locally
602 [ImportSpec] -- ^ The thing was imported.
604 -- INVARIANT: the list of 'ImportSpec' is non-empty
606 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
607 is_item :: ImpItemSpec }
610 -- | Describes a particular import declaration and is
611 -- shared among all the 'Provenance's for that decl
614 is_mod :: ModuleName, -- ^ Module imported, e.g. @import Muggle@
615 -- Note the @Muggle@ may well not be
616 -- the defining module for this thing!
618 -- TODO: either should be Module, or there
619 -- should be a Maybe PackageId here too.
620 is_as :: ModuleName, -- ^ Import alias, e.g. from @as M@ (or @Muggle@ if there is no @as@ clause)
621 is_qual :: Bool, -- ^ Was this import qualified?
622 is_dloc :: SrcSpan -- ^ The location of the entire import declaration
625 -- | Describes import info a particular Name
627 = ImpAll -- ^ The import had no import list,
628 -- or had a hiding list
632 is_iloc :: SrcSpan -- Location of the import item
633 } -- ^ The import had an import list.
634 -- The 'is_explicit' field is @True@ iff the thing was named
635 -- /explicitly/ in the import specs rather
636 -- than being imported as part of a "..." group. Consider:
638 -- > import C( T(..) )
640 -- Here the constructors of @T@ are not named explicitly;
641 -- only @T@ is named explicitly.
643 unQualSpecOK :: ImportSpec -> Bool
644 -- ^ Is in scope unqualified?
645 unQualSpecOK is = not (is_qual (is_decl is))
647 qualSpecOK :: ModuleName -> ImportSpec -> Bool
648 -- ^ Is in scope qualified with the given module?
649 qualSpecOK mod is = mod == is_as (is_decl is)
651 importSpecLoc :: ImportSpec -> SrcSpan
652 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
653 importSpecLoc (ImpSpec _ item) = is_iloc item
655 importSpecModule :: ImportSpec -> ModuleName
656 importSpecModule is = is_mod (is_decl is)
658 isExplicitItem :: ImpItemSpec -> Bool
659 isExplicitItem ImpAll = False
660 isExplicitItem (ImpSome {is_explicit = exp}) = exp
662 -- Note [Comparing provenance]
663 -- Comparison of provenance is just used for grouping
664 -- error messages (in RnEnv.warnUnusedBinds)
665 instance Eq Provenance where
666 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
668 instance Eq ImpDeclSpec where
669 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
671 instance Eq ImpItemSpec where
672 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
674 instance Ord Provenance where
675 compare LocalDef LocalDef = EQ
676 compare LocalDef (Imported _) = LT
677 compare (Imported _ ) LocalDef = GT
678 compare (Imported is1) (Imported is2) = compare (head is1)
679 {- See Note [Comparing provenance] -} (head is2)
681 instance Ord ImpDeclSpec where
682 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
683 (is_dloc is1 `compare` is_dloc is2)
685 instance Ord ImpItemSpec where
686 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
690 plusProv :: Provenance -> Provenance -> Provenance
691 -- Choose LocalDef over Imported
692 -- There is an obscure bug lurking here; in the presence
693 -- of recursive modules, something can be imported *and* locally
694 -- defined, and one might refer to it with a qualified name from
695 -- the import -- but I'm going to ignore that because it makes
696 -- the isLocalGRE predicate so much nicer this way
697 plusProv LocalDef LocalDef = panic "plusProv"
698 plusProv LocalDef _ = LocalDef
699 plusProv _ LocalDef = LocalDef
700 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
702 pprNameProvenance :: GlobalRdrElt -> SDoc
703 -- ^ Print out the place where the name was imported
704 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
705 = ptext (sLit "defined at") <+> ppr (nameSrcLoc name)
706 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
708 (why:_) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
709 [] -> panic "pprNameProvenance"
711 -- If we know the exact definition point (which we may do with GHCi)
712 -- then show that too. But not if it's just "imported from X".
713 ppr_defn :: SrcLoc -> SDoc
714 ppr_defn loc | isGoodSrcLoc loc = parens (ptext (sLit "defined at") <+> ppr loc)
717 instance Outputable ImportSpec where
719 = ptext (sLit "imported from") <+> ppr (importSpecModule imp_spec)
720 <+> if isGoodSrcSpan loc then ptext (sLit "at") <+> ppr loc
723 loc = importSpecLoc imp_spec