2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[RdrName]{@RdrName@}
9 RdrName(..), -- Constructors exported only to BinIface
12 mkRdrUnqual, mkRdrQual,
13 mkUnqual, mkVarUnqual, mkQual, mkOrig,
14 nameRdrName, getRdrName,
18 rdrNameOcc, setRdrNameSpace,
19 isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual,
20 isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
22 -- Printing; instance Outputable RdrName
25 LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv,
26 lookupLocalRdrEnv, elemLocalRdrEnv,
29 GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv,
30 lookupGlobalRdrEnv, extendGlobalRdrEnv,
31 pprGlobalRdrEnv, globalRdrEnvElts,
32 lookupGRE_RdrName, lookupGRE_Name, hideSomeUnquals,
34 -- GlobalRdrElt, Provenance, ImportSpec
35 GlobalRdrElt(..), isLocalGRE, unQualOK,
36 Provenance(..), pprNameProvenance,
37 ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
38 importSpecLoc, importSpecModule
41 #include "HsVersions.h"
44 import Module ( ModuleName, mkModuleNameFS, Module, moduleName )
45 import Name ( Name, NamedThing(getName), nameModule, nameParent_maybe,
46 nameOccName, isExternalName, nameSrcLoc )
47 import Maybes ( mapCatMaybes )
48 import SrcLoc ( isGoodSrcLoc, isGoodSrcSpan, srcLocSpan, SrcSpan )
49 import FastString ( FastString )
51 import Util ( thenCmp )
54 %************************************************************************
56 \subsection{The main data type}
58 %************************************************************************
63 -- Used for ordinary, unqualified occurrences
65 | Qual ModuleName OccName
66 -- A qualified name written by the user in
67 -- *source* code. The module isn't necessarily
68 -- the module where the thing is defined;
69 -- just the one from which it is imported
72 -- An original name; the module is the *defining* module.
73 -- This is used when GHC generates code that will be fed
74 -- into the renamer (e.g. from deriving clauses), but where
75 -- we want to say "Use Prelude.map dammit".
78 -- We know exactly the Name. This is used
79 -- (a) when the parser parses built-in syntax like "[]"
80 -- and "(,)", but wants a RdrName from it
81 -- (b) when converting names to the RdrNames in IfaceTypes
82 -- Here an Exact RdrName always contains an External Name
83 -- (Internal Names are converted to simple Unquals)
84 -- (c) by Template Haskell, when TH has generated a unique name
88 %************************************************************************
90 \subsection{Simple functions}
92 %************************************************************************
95 rdrNameOcc :: RdrName -> OccName
96 rdrNameOcc (Qual _ occ) = occ
97 rdrNameOcc (Unqual occ) = occ
98 rdrNameOcc (Orig _ occ) = occ
99 rdrNameOcc (Exact name) = nameOccName name
101 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
102 -- This rather gruesome function is used mainly by the parser
103 -- When parsing data T a = T | T1 Int
104 -- we parse the data constructors as *types* because of parser ambiguities,
105 -- so then we need to change the *type constr* to a *data constr*
107 -- The original-name case *can* occur when parsing
108 -- data [] a = [] | a : [a]
109 -- For the orig-name case we return an unqualified name.
110 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
111 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
112 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
113 setRdrNameSpace (Exact n) ns = Orig (nameModule n)
114 (setOccNameSpace ns (nameOccName n))
118 -- These two are the basic constructors
119 mkRdrUnqual :: OccName -> RdrName
120 mkRdrUnqual occ = Unqual occ
122 mkRdrQual :: ModuleName -> OccName -> RdrName
123 mkRdrQual mod occ = Qual mod occ
125 mkOrig :: Module -> OccName -> RdrName
126 mkOrig mod occ = Orig mod occ
129 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
130 mkDerivedRdrName parent mk_occ
131 = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
134 -- These two are used when parsing source files
135 -- They do encode the module and occurrence names
136 mkUnqual :: NameSpace -> FastString -> RdrName
137 mkUnqual sp n = Unqual (mkOccNameFS sp n)
139 mkVarUnqual :: FastString -> RdrName
140 mkVarUnqual n = Unqual (mkVarOccFS n)
142 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
143 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
145 getRdrName :: NamedThing thing => thing -> RdrName
146 getRdrName name = nameRdrName (getName name)
148 nameRdrName :: Name -> RdrName
149 nameRdrName name = Exact name
150 -- Keep the Name even for Internal names, so that the
151 -- unique is still there for debug printing, particularly
152 -- of Types (which are converted to IfaceTypes before printing)
154 nukeExact :: Name -> RdrName
156 | isExternalName n = Orig (nameModule n) (nameOccName n)
157 | otherwise = Unqual (nameOccName n)
161 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
162 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
163 isRdrTc rn = isTcOcc (rdrNameOcc rn)
165 isSrcRdrName (Unqual _) = True
166 isSrcRdrName (Qual _ _) = True
167 isSrcRdrName _ = False
169 isUnqual (Unqual _) = True
170 isUnqual other = False
172 isQual (Qual _ _) = True
175 isQual_maybe (Qual m n) = Just (m,n)
176 isQual_maybe _ = Nothing
178 isOrig (Orig _ _) = True
181 isOrig_maybe (Orig m n) = Just (m,n)
182 isOrig_maybe _ = Nothing
184 isExact (Exact _) = True
185 isExact other = False
187 isExact_maybe (Exact n) = Just n
188 isExact_maybe other = Nothing
192 %************************************************************************
194 \subsection{Instances}
196 %************************************************************************
199 instance Outputable RdrName where
200 ppr (Exact name) = ppr name
201 ppr (Unqual occ) = ppr occ
202 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
203 ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
205 instance OutputableBndr RdrName where
207 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
210 instance Eq RdrName where
211 (Exact n1) == (Exact n2) = n1==n2
212 -- Convert exact to orig
213 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
214 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
216 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
217 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
218 (Unqual o1) == (Unqual o2) = o1==o2
221 instance Ord RdrName where
222 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
223 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
224 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
225 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
227 -- Exact < Unqual < Qual < Orig
228 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
229 -- before comparing so that Prelude.map == the exact Prelude.map, but
230 -- that meant that we reported duplicates when renaming bindings
231 -- generated by Template Haskell; e.g
232 -- do { n1 <- newName "foo"; n2 <- newName "foo";
233 -- <decl involving n1,n2> }
234 -- I think we can do without this conversion
235 compare (Exact n1) (Exact n2) = n1 `compare` n2
236 compare (Exact n1) n2 = LT
238 compare (Unqual _) (Exact _) = GT
239 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
240 compare (Unqual _) _ = LT
242 compare (Qual _ _) (Exact _) = GT
243 compare (Qual _ _) (Unqual _) = GT
244 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
245 compare (Qual _ _) (Orig _ _) = LT
247 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
248 compare (Orig _ _) _ = GT
253 %************************************************************************
257 %************************************************************************
259 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
260 It is keyed by OccName, because we never use it for qualified names.
263 type LocalRdrEnv = OccEnv Name
265 emptyLocalRdrEnv = emptyOccEnv
267 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
268 extendLocalRdrEnv env names
269 = extendOccEnvList env [(nameOccName n, n) | n <- names]
271 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
272 lookupLocalRdrEnv env (Exact name) = Just name
273 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
274 lookupLocalRdrEnv env other = Nothing
276 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
277 elemLocalRdrEnv rdr_name env
278 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
283 %************************************************************************
287 %************************************************************************
290 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
291 -- Keyed by OccName; when looking up a qualified name
292 -- we look up the OccName part, and then check the Provenance
293 -- to see if the appropriate qualification is valid. This
294 -- saves routinely doubling the size of the env by adding both
295 -- qualified and unqualified names to the domain.
297 -- The list in the range is reqd because there may be name clashes
298 -- These only get reported on lookup, not on construction
300 -- INVARIANT: All the members of the list have distinct
301 -- gre_name fields; that is, no duplicate Names
303 emptyGlobalRdrEnv = emptyOccEnv
305 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
306 globalRdrEnvElts env = foldOccEnv (++) [] env
309 = GRE { gre_name :: Name,
310 gre_prov :: Provenance -- Why it's in scope
313 instance Outputable GlobalRdrElt where
314 ppr gre = ppr name <+> pp_parent (nameParent_maybe name)
315 <+> parens (pprNameProvenance gre)
318 pp_parent (Just p) = brackets (text "parent:" <+> ppr p)
319 pp_parent Nothing = empty
321 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
323 = vcat (map pp (occEnvElts env))
325 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
326 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
331 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
332 lookupGlobalRdrEnv env rdr_name = case lookupOccEnv env rdr_name of
336 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
337 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
339 occ = nameOccName (gre_name gre)
340 add gres _ = gre:gres
342 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
343 lookupGRE_RdrName rdr_name env
344 = case lookupOccEnv env (rdrNameOcc rdr_name) of
346 Just gres -> pickGREs rdr_name gres
348 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
349 lookupGRE_Name env name
350 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
351 gre_name gre == name ]
354 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
355 -- Take a list of GREs which have the right OccName
356 -- Pick those GREs that are suitable for this RdrName
357 -- And for those, keep only only the Provenances that are suitable
360 -- module A ( f ) where
361 -- import qualified Foo( f )
364 -- Let's suppose that Foo.f and Baz.f are the same entity really.
365 -- The export of f is ambiguous because it's in scope from the local def
366 -- and the import. The lookup of (Unqual f) should return a GRE for
367 -- the locally-defined f, and a GRE for the imported f, with a *single*
368 -- provenance, namely the one for Baz(f).
369 pickGREs rdr_name gres
370 = mapCatMaybes pick gres
372 rdr_is_unqual = isUnqual rdr_name
373 rdr_is_qual = isQual_maybe rdr_name
375 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
376 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
377 | rdr_is_unqual = Just gre
378 | Just (mod,_) <- rdr_is_qual,
379 mod == moduleName (nameModule n) = Just gre
380 | otherwise = Nothing
381 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
383 not (is_qual (is_decl is)) = Just gre
384 | Just (mod,_) <- rdr_is_qual,
385 mod == is_as (is_decl is) = Just gre
386 | otherwise = Nothing
387 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
388 | null filtered_is = Nothing
389 | otherwise = Just (gre {gre_prov = Imported filtered_is})
391 filtered_is | rdr_is_unqual
392 = filter (not . is_qual . is_decl) is
393 | Just (mod,_) <- rdr_is_qual
394 = filter ((== mod) . is_as . is_decl) is
398 isLocalGRE :: GlobalRdrElt -> Bool
399 isLocalGRE (GRE {gre_prov = LocalDef}) = True
400 isLocalGRE other = False
402 unQualOK :: GlobalRdrElt -> Bool
403 -- An unqualifed version of this thing is in scope
404 unQualOK (GRE {gre_prov = LocalDef}) = True
405 unQualOK (GRE {gre_prov = Imported is}) = not (all (is_qual . is_decl) is)
407 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
408 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
410 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
412 = foldr add emptyGlobalRdrEnv gres
414 add gre env = extendOccEnv_C (foldr insertGRE) env
415 (nameOccName (gre_name gre))
418 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
419 insertGRE new_g [] = [new_g]
420 insertGRE new_g (old_g : old_gs)
421 | gre_name new_g == gre_name old_g
422 = new_g `plusGRE` old_g : old_gs
424 = old_g : insertGRE new_g old_gs
426 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
427 -- Used when the gre_name fields match
429 = GRE { gre_name = gre_name g1,
430 gre_prov = gre_prov g1 `plusProv` gre_prov g2 }
432 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
433 -- Hide any unqualified bindings for the specified OccNames
434 -- This is used in TH, when renaming a declaration bracket
436 -- We want unqualified 'foo' in "..." to mean this foo, not
437 -- the one from the enclosing module. But the *qualified* name
438 -- from the enclosing moudule must certainly still be avaialable
439 -- Seems like 5 times as much work as it deserves!
440 hideSomeUnquals rdr_env occs
441 = foldr hide rdr_env occs
444 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
446 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
447 = GRE { gre_name = name, gre_prov = Imported [imp_spec] }
448 where -- Local defs get transfomed to (fake) imported things
449 mod = moduleName (nameModule name)
450 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
451 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
453 is_dloc = srcLocSpan (nameSrcLoc name) }
455 qual_gre gre@(GRE { gre_prov = Imported specs })
456 = gre { gre_prov = Imported (map qual_spec specs) }
458 qual_spec spec@(ImpSpec { is_decl = decl_spec })
459 = spec { is_decl = decl_spec { is_qual = True } }
463 %************************************************************************
467 %************************************************************************
469 The "provenance" of something says how it came to be in scope.
470 It's quite elaborate so that we can give accurate unused-name warnings.
474 = LocalDef -- Defined locally
475 | Imported -- Imported
476 [ImportSpec] -- INVARIANT: non-empty
478 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
479 is_item :: ImpItemSpec }
482 data ImpDeclSpec -- Describes a particular import declaration
483 -- Shared among all the Provenaces for that decl
485 is_mod :: ModuleName, -- 'import Muggle'
486 -- Note the Muggle may well not be
487 -- the defining module for this thing!
488 -- TODO: either should be Module, or there
489 -- should be a Maybe PackageId here too.
490 is_as :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
491 is_qual :: Bool, -- True <=> qualified (only)
492 is_dloc :: SrcSpan -- Location of import declaration
495 data ImpItemSpec -- Describes import info a particular Name
496 = ImpAll -- The import had no import list,
497 -- or had a hiding list
499 | ImpSome { -- The import had an import list
501 is_iloc :: SrcSpan -- Location of the import item
503 -- The is_explicit field is True iff the thing was named
504 -- *explicitly* in the import specs rather
505 -- than being imported as part of a "..." group
506 -- e.g. import C( T(..) )
507 -- Here the constructors of T are not named explicitly;
508 -- only T is named explicitly.
510 importSpecLoc :: ImportSpec -> SrcSpan
511 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
512 importSpecLoc (ImpSpec _ item) = is_iloc item
514 importSpecModule :: ImportSpec -> ModuleName
515 importSpecModule is = is_mod (is_decl is)
517 -- Note [Comparing provenance]
518 -- Comparison of provenance is just used for grouping
519 -- error messages (in RnEnv.warnUnusedBinds)
520 instance Eq Provenance where
521 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
523 instance Eq ImpDeclSpec where
524 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
526 instance Eq ImpItemSpec where
527 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
529 instance Ord Provenance where
530 compare LocalDef LocalDef = EQ
531 compare LocalDef (Imported _) = LT
532 compare (Imported _ ) LocalDef = GT
533 compare (Imported is1) (Imported is2) = compare (head is1)
534 {- See Note [Comparing provenance] -} (head is2)
536 instance Ord ImpDeclSpec where
537 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
538 (is_dloc is1 `compare` is_dloc is2)
540 instance Ord ImpItemSpec where
541 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
545 plusProv :: Provenance -> Provenance -> Provenance
546 -- Choose LocalDef over Imported
547 -- There is an obscure bug lurking here; in the presence
548 -- of recursive modules, something can be imported *and* locally
549 -- defined, and one might refer to it with a qualified name from
550 -- the import -- but I'm going to ignore that because it makes
551 -- the isLocalGRE predicate so much nicer this way
552 plusProv LocalDef LocalDef = panic "plusProv"
553 plusProv LocalDef p2 = LocalDef
554 plusProv p1 LocalDef = LocalDef
555 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
557 pprNameProvenance :: GlobalRdrElt -> SDoc
558 -- Print out the place where the name was imported
559 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
560 = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
561 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
563 (why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
564 [] -> panic "pprNameProvenance"
566 -- If we know the exact definition point (which we may do with GHCi)
567 -- then show that too. But not if it's just "imported from X".
568 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
571 instance Outputable ImportSpec where
573 = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec)
574 <+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
577 loc = importSpecLoc imp_spec