2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
15 RdrName(..), -- Constructors exported only to BinIface
18 mkRdrUnqual, mkRdrQual,
19 mkUnqual, mkVarUnqual, mkQual, mkOrig,
20 nameRdrName, getRdrName,
24 rdrNameOcc, setRdrNameSpace,
25 isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual,
26 isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
28 -- Printing; instance Outputable RdrName
31 LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv,
32 lookupLocalRdrEnv, elemLocalRdrEnv,
35 GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv,
36 lookupGlobalRdrEnv, extendGlobalRdrEnv,
37 pprGlobalRdrEnv, globalRdrEnvElts,
38 lookupGRE_RdrName, lookupGRE_Name, getGRE_NameQualifier_maybes,
41 -- GlobalRdrElt, Provenance, ImportSpec
42 GlobalRdrElt(..), isLocalGRE, unQualOK, qualSpecOK, unQualSpecOK,
43 Provenance(..), pprNameProvenance,
45 ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
46 importSpecLoc, importSpecModule, isExplicitItem
49 #include "HsVersions.h"
60 %************************************************************************
62 \subsection{The main data type}
64 %************************************************************************
69 -- Used for ordinary, unqualified occurrences
71 | Qual ModuleName OccName
72 -- A qualified name written by the user in
73 -- *source* code. The module isn't necessarily
74 -- the module where the thing is defined;
75 -- just the one from which it is imported
78 -- An original name; the module is the *defining* module.
79 -- This is used when GHC generates code that will be fed
80 -- into the renamer (e.g. from deriving clauses), but where
81 -- we want to say "Use Prelude.map dammit".
84 -- We know exactly the Name. This is used
85 -- (a) when the parser parses built-in syntax like "[]"
86 -- and "(,)", but wants a RdrName from it
87 -- (b) by Template Haskell, when TH has generated a unique name
91 %************************************************************************
93 \subsection{Simple functions}
95 %************************************************************************
98 rdrNameOcc :: RdrName -> OccName
99 rdrNameOcc (Qual _ occ) = occ
100 rdrNameOcc (Unqual occ) = occ
101 rdrNameOcc (Orig _ occ) = occ
102 rdrNameOcc (Exact name) = nameOccName name
104 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
105 -- This rather gruesome function is used mainly by the parser
106 -- When parsing data T a = T | T1 Int
107 -- we parse the data constructors as *types* because of parser ambiguities,
108 -- so then we need to change the *type constr* to a *data constr*
110 -- The original-name case *can* occur when parsing
111 -- data [] a = [] | a : [a]
112 -- For the orig-name case we return an unqualified name.
113 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
114 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
115 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
116 setRdrNameSpace (Exact n) ns = Orig (nameModule n)
117 (setOccNameSpace ns (nameOccName n))
121 -- These two are the basic constructors
122 mkRdrUnqual :: OccName -> RdrName
123 mkRdrUnqual occ = Unqual occ
125 mkRdrQual :: ModuleName -> OccName -> RdrName
126 mkRdrQual mod occ = Qual mod occ
128 mkOrig :: Module -> OccName -> RdrName
129 mkOrig mod occ = Orig mod occ
132 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
133 mkDerivedRdrName parent mk_occ
134 = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
137 -- These two are used when parsing source files
138 -- They do encode the module and occurrence names
139 mkUnqual :: NameSpace -> FastString -> RdrName
140 mkUnqual sp n = Unqual (mkOccNameFS sp n)
142 mkVarUnqual :: FastString -> RdrName
143 mkVarUnqual n = Unqual (mkVarOccFS n)
145 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
146 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
148 getRdrName :: NamedThing thing => thing -> RdrName
149 getRdrName name = nameRdrName (getName name)
151 nameRdrName :: Name -> RdrName
152 nameRdrName name = Exact name
153 -- Keep the Name even for Internal names, so that the
154 -- unique is still there for debug printing, particularly
155 -- of Types (which are converted to IfaceTypes before printing)
157 nukeExact :: Name -> RdrName
159 | isExternalName n = Orig (nameModule n) (nameOccName n)
160 | otherwise = Unqual (nameOccName n)
164 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
165 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
166 isRdrTc rn = isTcOcc (rdrNameOcc rn)
168 isSrcRdrName (Unqual _) = True
169 isSrcRdrName (Qual _ _) = True
170 isSrcRdrName _ = False
172 isUnqual (Unqual _) = True
173 isUnqual other = False
175 isQual (Qual _ _) = True
178 isQual_maybe (Qual m n) = Just (m,n)
179 isQual_maybe _ = Nothing
181 isOrig (Orig _ _) = True
184 isOrig_maybe (Orig m n) = Just (m,n)
185 isOrig_maybe _ = Nothing
187 isExact (Exact _) = True
188 isExact other = False
190 isExact_maybe (Exact n) = Just n
191 isExact_maybe other = Nothing
195 %************************************************************************
197 \subsection{Instances}
199 %************************************************************************
202 instance Outputable RdrName where
203 ppr (Exact name) = ppr name
204 ppr (Unqual occ) = ppr occ
205 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
206 ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
208 instance OutputableBndr RdrName where
210 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
213 instance Eq RdrName where
214 (Exact n1) == (Exact n2) = n1==n2
215 -- Convert exact to orig
216 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
217 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
219 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
220 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
221 (Unqual o1) == (Unqual o2) = o1==o2
224 instance Ord RdrName where
225 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
226 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
227 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
228 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
230 -- Exact < Unqual < Qual < Orig
231 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
232 -- before comparing so that Prelude.map == the exact Prelude.map, but
233 -- that meant that we reported duplicates when renaming bindings
234 -- generated by Template Haskell; e.g
235 -- do { n1 <- newName "foo"; n2 <- newName "foo";
236 -- <decl involving n1,n2> }
237 -- I think we can do without this conversion
238 compare (Exact n1) (Exact n2) = n1 `compare` n2
239 compare (Exact n1) n2 = LT
241 compare (Unqual _) (Exact _) = GT
242 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
243 compare (Unqual _) _ = LT
245 compare (Qual _ _) (Exact _) = GT
246 compare (Qual _ _) (Unqual _) = GT
247 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
248 compare (Qual _ _) (Orig _ _) = LT
250 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
251 compare (Orig _ _) _ = GT
256 %************************************************************************
260 %************************************************************************
262 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
263 It is keyed by OccName, because we never use it for qualified names.
266 type LocalRdrEnv = OccEnv Name
268 emptyLocalRdrEnv = emptyOccEnv
270 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
271 extendLocalRdrEnv env names
272 = extendOccEnvList env [(nameOccName n, n) | n <- names]
274 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
275 lookupLocalRdrEnv env (Exact name) = Just name
276 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
277 lookupLocalRdrEnv env other = Nothing
279 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
280 elemLocalRdrEnv rdr_name env
281 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
286 %************************************************************************
290 %************************************************************************
293 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
294 -- Keyed by OccName; when looking up a qualified name
295 -- we look up the OccName part, and then check the Provenance
296 -- to see if the appropriate qualification is valid. This
297 -- saves routinely doubling the size of the env by adding both
298 -- qualified and unqualified names to the domain.
300 -- The list in the range is reqd because there may be name clashes
301 -- These only get reported on lookup, not on construction
303 -- INVARIANT: All the members of the list have distinct
304 -- gre_name fields; that is, no duplicate Names
307 = GRE { gre_name :: Name,
309 gre_prov :: Provenance -- Why it's in scope
312 data Parent = NoParent | ParentIs Name
315 instance Outputable Parent where
317 ppr (ParentIs n) = ptext SLIT("parent:") <> ppr n
320 plusParent :: Parent -> Parent -> Parent
321 plusParent p1 p2 = ASSERT2( p1 == p2, parens (ppr p1) <+> parens (ppr p2) )
324 {- Why so complicated? -=chak
325 plusParent :: Parent -> Parent -> Parent
326 plusParent NoParent rel =
327 ASSERT2( case rel of { NoParent -> True; other -> False },
328 ptext SLIT("plusParent[NoParent]: ") <+> ppr rel )
330 plusParent (ParentIs n) rel =
331 ASSERT2( case rel of { ParentIs m -> n==m; other -> False },
332 ptext SLIT("plusParent[ParentIs]:") <+> ppr n <> comma <+> ppr rel )
336 emptyGlobalRdrEnv = emptyOccEnv
338 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
339 globalRdrEnvElts env = foldOccEnv (++) [] env
341 instance Outputable GlobalRdrElt where
342 ppr gre = ppr name <+> parens (ppr (gre_par gre) <+> pprNameProvenance gre)
346 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
348 = vcat (map pp (occEnvElts env))
350 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
351 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
356 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
357 lookupGlobalRdrEnv env rdr_name = case lookupOccEnv env rdr_name of
361 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
362 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
364 occ = nameOccName (gre_name gre)
365 add gres _ = gre:gres
367 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
368 lookupGRE_RdrName rdr_name env
369 = case lookupOccEnv env (rdrNameOcc rdr_name) of
371 Just gres -> pickGREs rdr_name gres
373 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
374 lookupGRE_Name env name
375 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
376 gre_name gre == name ]
378 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
379 getGRE_NameQualifier_maybes env
380 = map qualifier_maybe . map gre_prov . lookupGRE_Name env
381 where qualifier_maybe LocalDef = Nothing
382 qualifier_maybe (Imported iss) = Just $ map (is_as . is_decl) iss
384 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
385 -- Take a list of GREs which have the right OccName
386 -- Pick those GREs that are suitable for this RdrName
387 -- And for those, keep only only the Provenances that are suitable
390 -- module A ( f ) where
391 -- import qualified Foo( f )
394 -- Let's suppose that Foo.f and Baz.f are the same entity really.
395 -- The export of f is ambiguous because it's in scope from the local def
396 -- and the import. The lookup of (Unqual f) should return a GRE for
397 -- the locally-defined f, and a GRE for the imported f, with a *single*
398 -- provenance, namely the one for Baz(f).
399 pickGREs rdr_name gres
400 = mapCatMaybes pick gres
402 rdr_is_unqual = isUnqual rdr_name
403 rdr_is_qual = isQual_maybe rdr_name
405 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
406 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
407 | rdr_is_unqual = Just gre
408 | Just (mod,_) <- rdr_is_qual,
409 mod == moduleName (nameModule n) = Just gre
410 | otherwise = Nothing
411 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
413 not (is_qual (is_decl is)) = Just gre
414 | Just (mod,_) <- rdr_is_qual,
415 mod == is_as (is_decl is) = Just gre
416 | otherwise = Nothing
417 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
418 | null filtered_is = Nothing
419 | otherwise = Just (gre {gre_prov = Imported filtered_is})
421 filtered_is | rdr_is_unqual
422 = filter (not . is_qual . is_decl) is
423 | Just (mod,_) <- rdr_is_qual
424 = filter ((== mod) . is_as . is_decl) is
428 isLocalGRE :: GlobalRdrElt -> Bool
429 isLocalGRE (GRE {gre_prov = LocalDef}) = True
430 isLocalGRE other = False
432 unQualOK :: GlobalRdrElt -> Bool
433 -- An unqualifed version of this thing is in scope
434 unQualOK (GRE {gre_prov = LocalDef}) = True
435 unQualOK (GRE {gre_prov = Imported is}) = any unQualSpecOK is
437 unQualSpecOK :: ImportSpec -> Bool
438 -- In scope unqualified
439 unQualSpecOK is = not (is_qual (is_decl is))
441 qualSpecOK :: ModuleName -> ImportSpec -> Bool
442 -- In scope qualified with M
443 qualSpecOK mod is = mod == is_as (is_decl is)
445 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
446 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
448 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
450 = foldr add emptyGlobalRdrEnv gres
452 add gre env = extendOccEnv_C (foldr insertGRE) env
453 (nameOccName (gre_name gre))
456 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
457 insertGRE new_g [] = [new_g]
458 insertGRE new_g (old_g : old_gs)
459 | gre_name new_g == gre_name old_g
460 = new_g `plusGRE` old_g : old_gs
462 = old_g : insertGRE new_g old_gs
464 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
465 -- Used when the gre_name fields match
467 = GRE { gre_name = gre_name g1,
468 gre_prov = gre_prov g1 `plusProv` gre_prov g2,
469 gre_par = gre_par g1 `plusParent` gre_par g2 }
471 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
472 -- Hide any unqualified bindings for the specified OccNames
473 -- This is used in TH, when renaming a declaration bracket
475 -- We want unqualified 'foo' in "..." to mean this foo, not
476 -- the one from the enclosing module. But the *qualified* name
477 -- from the enclosing moudule must certainly still be avaialable
478 -- Seems like 5 times as much work as it deserves!
479 hideSomeUnquals rdr_env occs
480 = foldr hide rdr_env occs
483 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
485 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
486 = gre { gre_prov = Imported [imp_spec] }
487 where -- Local defs get transfomed to (fake) imported things
488 mod = moduleName (nameModule name)
489 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
490 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
492 is_dloc = srcLocSpan (nameSrcLoc name) }
494 qual_gre gre@(GRE { gre_prov = Imported specs })
495 = gre { gre_prov = Imported (map qual_spec specs) }
497 qual_spec spec@(ImpSpec { is_decl = decl_spec })
498 = spec { is_decl = decl_spec { is_qual = True } }
502 %************************************************************************
506 %************************************************************************
508 The "provenance" of something says how it came to be in scope.
509 It's quite elaborate so that we can give accurate unused-name warnings.
513 = LocalDef -- Defined locally
514 | Imported -- Imported
515 [ImportSpec] -- INVARIANT: non-empty
517 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
518 is_item :: ImpItemSpec }
521 data ImpDeclSpec -- Describes a particular import declaration
522 -- Shared among all the Provenaces for that decl
524 is_mod :: ModuleName, -- 'import Muggle'
525 -- Note the Muggle may well not be
526 -- the defining module for this thing!
527 -- TODO: either should be Module, or there
528 -- should be a Maybe PackageId here too.
529 is_as :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
530 is_qual :: Bool, -- True <=> qualified (only)
531 is_dloc :: SrcSpan -- Location of import declaration
534 data ImpItemSpec -- Describes import info a particular Name
535 = ImpAll -- The import had no import list,
536 -- or had a hiding list
538 | ImpSome { -- The import had an import list
540 is_iloc :: SrcSpan -- Location of the import item
542 -- The is_explicit field is True iff the thing was named
543 -- *explicitly* in the import specs rather
544 -- than being imported as part of a "..." group
545 -- e.g. import C( T(..) )
546 -- Here the constructors of T are not named explicitly;
547 -- only T is named explicitly.
549 importSpecLoc :: ImportSpec -> SrcSpan
550 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
551 importSpecLoc (ImpSpec _ item) = is_iloc item
553 importSpecModule :: ImportSpec -> ModuleName
554 importSpecModule is = is_mod (is_decl is)
556 isExplicitItem :: ImpItemSpec -> Bool
557 isExplicitItem ImpAll = False
558 isExplicitItem (ImpSome {is_explicit = exp}) = exp
560 -- Note [Comparing provenance]
561 -- Comparison of provenance is just used for grouping
562 -- error messages (in RnEnv.warnUnusedBinds)
563 instance Eq Provenance where
564 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
566 instance Eq ImpDeclSpec where
567 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
569 instance Eq ImpItemSpec where
570 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
572 instance Ord Provenance where
573 compare LocalDef LocalDef = EQ
574 compare LocalDef (Imported _) = LT
575 compare (Imported _ ) LocalDef = GT
576 compare (Imported is1) (Imported is2) = compare (head is1)
577 {- See Note [Comparing provenance] -} (head is2)
579 instance Ord ImpDeclSpec where
580 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
581 (is_dloc is1 `compare` is_dloc is2)
583 instance Ord ImpItemSpec where
584 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
588 plusProv :: Provenance -> Provenance -> Provenance
589 -- Choose LocalDef over Imported
590 -- There is an obscure bug lurking here; in the presence
591 -- of recursive modules, something can be imported *and* locally
592 -- defined, and one might refer to it with a qualified name from
593 -- the import -- but I'm going to ignore that because it makes
594 -- the isLocalGRE predicate so much nicer this way
595 plusProv LocalDef LocalDef = panic "plusProv"
596 plusProv LocalDef p2 = LocalDef
597 plusProv p1 LocalDef = LocalDef
598 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
600 pprNameProvenance :: GlobalRdrElt -> SDoc
601 -- Print out the place where the name was imported
602 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
603 = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
604 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
606 (why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
607 [] -> panic "pprNameProvenance"
609 -- If we know the exact definition point (which we may do with GHCi)
610 -- then show that too. But not if it's just "imported from X".
611 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
614 instance Outputable ImportSpec where
616 = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec)
617 <+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
620 loc = importSpecLoc imp_spec