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) by Template Haskell, when TH has generated a unique name
85 %************************************************************************
87 \subsection{Simple functions}
89 %************************************************************************
92 rdrNameOcc :: RdrName -> OccName
93 rdrNameOcc (Qual _ occ) = occ
94 rdrNameOcc (Unqual occ) = occ
95 rdrNameOcc (Orig _ occ) = occ
96 rdrNameOcc (Exact name) = nameOccName name
98 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
99 -- This rather gruesome function is used mainly by the parser
100 -- When parsing data T a = T | T1 Int
101 -- we parse the data constructors as *types* because of parser ambiguities,
102 -- so then we need to change the *type constr* to a *data constr*
104 -- The original-name case *can* occur when parsing
105 -- data [] a = [] | a : [a]
106 -- For the orig-name case we return an unqualified name.
107 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
108 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
109 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
110 setRdrNameSpace (Exact n) ns = Orig (nameModule n)
111 (setOccNameSpace ns (nameOccName n))
115 -- These two are the basic constructors
116 mkRdrUnqual :: OccName -> RdrName
117 mkRdrUnqual occ = Unqual occ
119 mkRdrQual :: ModuleName -> OccName -> RdrName
120 mkRdrQual mod occ = Qual mod occ
122 mkOrig :: Module -> OccName -> RdrName
123 mkOrig mod occ = Orig mod occ
126 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
127 mkDerivedRdrName parent mk_occ
128 = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
131 -- These two are used when parsing source files
132 -- They do encode the module and occurrence names
133 mkUnqual :: NameSpace -> FastString -> RdrName
134 mkUnqual sp n = Unqual (mkOccNameFS sp n)
136 mkVarUnqual :: FastString -> RdrName
137 mkVarUnqual n = Unqual (mkVarOccFS n)
139 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
140 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
142 getRdrName :: NamedThing thing => thing -> RdrName
143 getRdrName name = nameRdrName (getName name)
145 nameRdrName :: Name -> RdrName
146 nameRdrName name = Exact name
147 -- Keep the Name even for Internal names, so that the
148 -- unique is still there for debug printing, particularly
149 -- of Types (which are converted to IfaceTypes before printing)
151 nukeExact :: Name -> RdrName
153 | isExternalName n = Orig (nameModule n) (nameOccName n)
154 | otherwise = Unqual (nameOccName n)
158 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
159 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
160 isRdrTc rn = isTcOcc (rdrNameOcc rn)
162 isSrcRdrName (Unqual _) = True
163 isSrcRdrName (Qual _ _) = True
164 isSrcRdrName _ = False
166 isUnqual (Unqual _) = True
167 isUnqual other = False
169 isQual (Qual _ _) = True
172 isQual_maybe (Qual m n) = Just (m,n)
173 isQual_maybe _ = Nothing
175 isOrig (Orig _ _) = True
178 isOrig_maybe (Orig m n) = Just (m,n)
179 isOrig_maybe _ = Nothing
181 isExact (Exact _) = True
182 isExact other = False
184 isExact_maybe (Exact n) = Just n
185 isExact_maybe other = Nothing
189 %************************************************************************
191 \subsection{Instances}
193 %************************************************************************
196 instance Outputable RdrName where
197 ppr (Exact name) = ppr name
198 ppr (Unqual occ) = ppr occ
199 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
200 ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
202 instance OutputableBndr RdrName where
204 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
207 instance Eq RdrName where
208 (Exact n1) == (Exact n2) = n1==n2
209 -- Convert exact to orig
210 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
211 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
213 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
214 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
215 (Unqual o1) == (Unqual o2) = o1==o2
218 instance Ord RdrName where
219 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
220 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
221 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
222 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
224 -- Exact < Unqual < Qual < Orig
225 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
226 -- before comparing so that Prelude.map == the exact Prelude.map, but
227 -- that meant that we reported duplicates when renaming bindings
228 -- generated by Template Haskell; e.g
229 -- do { n1 <- newName "foo"; n2 <- newName "foo";
230 -- <decl involving n1,n2> }
231 -- I think we can do without this conversion
232 compare (Exact n1) (Exact n2) = n1 `compare` n2
233 compare (Exact n1) n2 = LT
235 compare (Unqual _) (Exact _) = GT
236 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
237 compare (Unqual _) _ = LT
239 compare (Qual _ _) (Exact _) = GT
240 compare (Qual _ _) (Unqual _) = GT
241 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
242 compare (Qual _ _) (Orig _ _) = LT
244 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
245 compare (Orig _ _) _ = GT
250 %************************************************************************
254 %************************************************************************
256 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
257 It is keyed by OccName, because we never use it for qualified names.
260 type LocalRdrEnv = OccEnv Name
262 emptyLocalRdrEnv = emptyOccEnv
264 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
265 extendLocalRdrEnv env names
266 = extendOccEnvList env [(nameOccName n, n) | n <- names]
268 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
269 lookupLocalRdrEnv env (Exact name) = Just name
270 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
271 lookupLocalRdrEnv env other = Nothing
273 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
274 elemLocalRdrEnv rdr_name env
275 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
280 %************************************************************************
284 %************************************************************************
287 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
288 -- Keyed by OccName; when looking up a qualified name
289 -- we look up the OccName part, and then check the Provenance
290 -- to see if the appropriate qualification is valid. This
291 -- saves routinely doubling the size of the env by adding both
292 -- qualified and unqualified names to the domain.
294 -- The list in the range is reqd because there may be name clashes
295 -- These only get reported on lookup, not on construction
297 -- INVARIANT: All the members of the list have distinct
298 -- gre_name fields; that is, no duplicate Names
300 emptyGlobalRdrEnv = emptyOccEnv
302 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
303 globalRdrEnvElts env = foldOccEnv (++) [] env
306 = GRE { gre_name :: Name,
307 gre_prov :: Provenance -- Why it's in scope
310 instance Outputable GlobalRdrElt where
311 ppr gre = ppr name <+> pp_parent (nameParent_maybe name)
312 <+> parens (pprNameProvenance gre)
315 pp_parent (Just p) = brackets (text "parent:" <+> ppr p)
316 pp_parent Nothing = empty
318 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
320 = vcat (map pp (occEnvElts env))
322 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
323 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
328 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
329 lookupGlobalRdrEnv env rdr_name = case lookupOccEnv env rdr_name of
333 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
334 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
336 occ = nameOccName (gre_name gre)
337 add gres _ = gre:gres
339 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
340 lookupGRE_RdrName rdr_name env
341 = case lookupOccEnv env (rdrNameOcc rdr_name) of
343 Just gres -> pickGREs rdr_name gres
345 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
346 lookupGRE_Name env name
347 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
348 gre_name gre == name ]
351 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
352 -- Take a list of GREs which have the right OccName
353 -- Pick those GREs that are suitable for this RdrName
354 -- And for those, keep only only the Provenances that are suitable
357 -- module A ( f ) where
358 -- import qualified Foo( f )
361 -- Let's suppose that Foo.f and Baz.f are the same entity really.
362 -- The export of f is ambiguous because it's in scope from the local def
363 -- and the import. The lookup of (Unqual f) should return a GRE for
364 -- the locally-defined f, and a GRE for the imported f, with a *single*
365 -- provenance, namely the one for Baz(f).
366 pickGREs rdr_name gres
367 = mapCatMaybes pick gres
369 rdr_is_unqual = isUnqual rdr_name
370 rdr_is_qual = isQual_maybe rdr_name
372 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
373 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
374 | rdr_is_unqual = Just gre
375 | Just (mod,_) <- rdr_is_qual,
376 mod == moduleName (nameModule n) = Just gre
377 | otherwise = Nothing
378 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
380 not (is_qual (is_decl is)) = Just gre
381 | Just (mod,_) <- rdr_is_qual,
382 mod == is_as (is_decl is) = Just gre
383 | otherwise = Nothing
384 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
385 | null filtered_is = Nothing
386 | otherwise = Just (gre {gre_prov = Imported filtered_is})
388 filtered_is | rdr_is_unqual
389 = filter (not . is_qual . is_decl) is
390 | Just (mod,_) <- rdr_is_qual
391 = filter ((== mod) . is_as . is_decl) is
395 isLocalGRE :: GlobalRdrElt -> Bool
396 isLocalGRE (GRE {gre_prov = LocalDef}) = True
397 isLocalGRE other = False
399 unQualOK :: GlobalRdrElt -> Bool
400 -- An unqualifed version of this thing is in scope
401 unQualOK (GRE {gre_prov = LocalDef}) = True
402 unQualOK (GRE {gre_prov = Imported is}) = not (all (is_qual . is_decl) is)
404 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
405 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
407 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
409 = foldr add emptyGlobalRdrEnv gres
411 add gre env = extendOccEnv_C (foldr insertGRE) env
412 (nameOccName (gre_name gre))
415 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
416 insertGRE new_g [] = [new_g]
417 insertGRE new_g (old_g : old_gs)
418 | gre_name new_g == gre_name old_g
419 = new_g `plusGRE` old_g : old_gs
421 = old_g : insertGRE new_g old_gs
423 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
424 -- Used when the gre_name fields match
426 = GRE { gre_name = gre_name g1,
427 gre_prov = gre_prov g1 `plusProv` gre_prov g2 }
429 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
430 -- Hide any unqualified bindings for the specified OccNames
431 -- This is used in TH, when renaming a declaration bracket
433 -- We want unqualified 'foo' in "..." to mean this foo, not
434 -- the one from the enclosing module. But the *qualified* name
435 -- from the enclosing moudule must certainly still be avaialable
436 -- Seems like 5 times as much work as it deserves!
437 hideSomeUnquals rdr_env occs
438 = foldr hide rdr_env occs
441 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
443 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
444 = GRE { gre_name = name, gre_prov = Imported [imp_spec] }
445 where -- Local defs get transfomed to (fake) imported things
446 mod = moduleName (nameModule name)
447 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
448 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
450 is_dloc = srcLocSpan (nameSrcLoc name) }
452 qual_gre gre@(GRE { gre_prov = Imported specs })
453 = gre { gre_prov = Imported (map qual_spec specs) }
455 qual_spec spec@(ImpSpec { is_decl = decl_spec })
456 = spec { is_decl = decl_spec { is_qual = True } }
460 %************************************************************************
464 %************************************************************************
466 The "provenance" of something says how it came to be in scope.
467 It's quite elaborate so that we can give accurate unused-name warnings.
471 = LocalDef -- Defined locally
472 | Imported -- Imported
473 [ImportSpec] -- INVARIANT: non-empty
475 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
476 is_item :: ImpItemSpec }
479 data ImpDeclSpec -- Describes a particular import declaration
480 -- Shared among all the Provenaces for that decl
482 is_mod :: ModuleName, -- 'import Muggle'
483 -- Note the Muggle may well not be
484 -- the defining module for this thing!
485 -- TODO: either should be Module, or there
486 -- should be a Maybe PackageId here too.
487 is_as :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
488 is_qual :: Bool, -- True <=> qualified (only)
489 is_dloc :: SrcSpan -- Location of import declaration
492 data ImpItemSpec -- Describes import info a particular Name
493 = ImpAll -- The import had no import list,
494 -- or had a hiding list
496 | ImpSome { -- The import had an import list
498 is_iloc :: SrcSpan -- Location of the import item
500 -- The is_explicit field is True iff the thing was named
501 -- *explicitly* in the import specs rather
502 -- than being imported as part of a "..." group
503 -- e.g. import C( T(..) )
504 -- Here the constructors of T are not named explicitly;
505 -- only T is named explicitly.
507 importSpecLoc :: ImportSpec -> SrcSpan
508 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
509 importSpecLoc (ImpSpec _ item) = is_iloc item
511 importSpecModule :: ImportSpec -> ModuleName
512 importSpecModule is = is_mod (is_decl is)
514 -- Note [Comparing provenance]
515 -- Comparison of provenance is just used for grouping
516 -- error messages (in RnEnv.warnUnusedBinds)
517 instance Eq Provenance where
518 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
520 instance Eq ImpDeclSpec where
521 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
523 instance Eq ImpItemSpec where
524 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
526 instance Ord Provenance where
527 compare LocalDef LocalDef = EQ
528 compare LocalDef (Imported _) = LT
529 compare (Imported _ ) LocalDef = GT
530 compare (Imported is1) (Imported is2) = compare (head is1)
531 {- See Note [Comparing provenance] -} (head is2)
533 instance Ord ImpDeclSpec where
534 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
535 (is_dloc is1 `compare` is_dloc is2)
537 instance Ord ImpItemSpec where
538 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
542 plusProv :: Provenance -> Provenance -> Provenance
543 -- Choose LocalDef over Imported
544 -- There is an obscure bug lurking here; in the presence
545 -- of recursive modules, something can be imported *and* locally
546 -- defined, and one might refer to it with a qualified name from
547 -- the import -- but I'm going to ignore that because it makes
548 -- the isLocalGRE predicate so much nicer this way
549 plusProv LocalDef LocalDef = panic "plusProv"
550 plusProv LocalDef p2 = LocalDef
551 plusProv p1 LocalDef = LocalDef
552 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
554 pprNameProvenance :: GlobalRdrElt -> SDoc
555 -- Print out the place where the name was imported
556 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
557 = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
558 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
560 (why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
561 [] -> panic "pprNameProvenance"
563 -- If we know the exact definition point (which we may do with GHCi)
564 -- then show that too. But not if it's just "imported from X".
565 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
568 instance Outputable ImportSpec where
570 = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec)
571 <+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
574 loc = importSpecLoc imp_spec