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, lookupLocalRdrOcc, 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 lookupLocalRdrOcc :: LocalRdrEnv -> OccName -> Maybe Name
280 lookupLocalRdrOcc env occ = lookupOccEnv env occ
282 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
283 elemLocalRdrEnv rdr_name env
284 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
289 %************************************************************************
293 %************************************************************************
296 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
297 -- Keyed by OccName; when looking up a qualified name
298 -- we look up the OccName part, and then check the Provenance
299 -- to see if the appropriate qualification is valid. This
300 -- saves routinely doubling the size of the env by adding both
301 -- qualified and unqualified names to the domain.
303 -- The list in the range is reqd because there may be name clashes
304 -- These only get reported on lookup, not on construction
306 -- INVARIANT: All the members of the list have distinct
307 -- gre_name fields; that is, no duplicate Names
310 = GRE { gre_name :: Name,
312 gre_prov :: Provenance -- Why it's in scope
315 data Parent = NoParent | ParentIs Name
318 instance Outputable Parent where
320 ppr (ParentIs n) = ptext SLIT("parent:") <> ppr n
323 plusParent :: Parent -> Parent -> Parent
324 plusParent p1 p2 = ASSERT2( p1 == p2, parens (ppr p1) <+> parens (ppr p2) )
327 {- Why so complicated? -=chak
328 plusParent :: Parent -> Parent -> Parent
329 plusParent NoParent rel =
330 ASSERT2( case rel of { NoParent -> True; other -> False },
331 ptext SLIT("plusParent[NoParent]: ") <+> ppr rel )
333 plusParent (ParentIs n) rel =
334 ASSERT2( case rel of { ParentIs m -> n==m; other -> False },
335 ptext SLIT("plusParent[ParentIs]:") <+> ppr n <> comma <+> ppr rel )
339 emptyGlobalRdrEnv = emptyOccEnv
341 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
342 globalRdrEnvElts env = foldOccEnv (++) [] env
344 instance Outputable GlobalRdrElt where
345 ppr gre = ppr name <+> parens (ppr (gre_par gre) <+> pprNameProvenance gre)
349 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
351 = vcat (map pp (occEnvElts env))
353 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
354 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
359 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
360 lookupGlobalRdrEnv env occ_name = case lookupOccEnv env occ_name of
364 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
365 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
367 occ = nameOccName (gre_name gre)
368 add gres _ = gre:gres
370 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
371 lookupGRE_RdrName rdr_name env
372 = case lookupOccEnv env (rdrNameOcc rdr_name) of
374 Just gres -> pickGREs rdr_name gres
376 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
377 lookupGRE_Name env name
378 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
379 gre_name gre == name ]
381 getGRE_NameQualifier_maybes :: GlobalRdrEnv -> Name -> [Maybe [ModuleName]]
382 getGRE_NameQualifier_maybes env
383 = map qualifier_maybe . map gre_prov . lookupGRE_Name env
384 where qualifier_maybe LocalDef = Nothing
385 qualifier_maybe (Imported iss) = Just $ map (is_as . is_decl) iss
387 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
388 -- Take a list of GREs which have the right OccName
389 -- Pick those GREs that are suitable for this RdrName
390 -- And for those, keep only only the Provenances that are suitable
393 -- module A ( f ) where
394 -- import qualified Foo( f )
397 -- Let's suppose that Foo.f and Baz.f are the same entity really.
398 -- The export of f is ambiguous because it's in scope from the local def
399 -- and the import. The lookup of (Unqual f) should return a GRE for
400 -- the locally-defined f, and a GRE for the imported f, with a *single*
401 -- provenance, namely the one for Baz(f).
402 pickGREs rdr_name gres
403 = mapCatMaybes pick gres
405 rdr_is_unqual = isUnqual rdr_name
406 rdr_is_qual = isQual_maybe rdr_name
408 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
409 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
410 | rdr_is_unqual = Just gre
411 | Just (mod,_) <- rdr_is_qual,
412 mod == moduleName (nameModule n) = Just gre
413 | otherwise = Nothing
414 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
416 not (is_qual (is_decl is)) = Just gre
417 | Just (mod,_) <- rdr_is_qual,
418 mod == is_as (is_decl is) = Just gre
419 | otherwise = Nothing
420 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
421 | null filtered_is = Nothing
422 | otherwise = Just (gre {gre_prov = Imported filtered_is})
424 filtered_is | rdr_is_unqual
425 = filter (not . is_qual . is_decl) is
426 | Just (mod,_) <- rdr_is_qual
427 = filter ((== mod) . is_as . is_decl) is
431 isLocalGRE :: GlobalRdrElt -> Bool
432 isLocalGRE (GRE {gre_prov = LocalDef}) = True
433 isLocalGRE other = False
435 unQualOK :: GlobalRdrElt -> Bool
436 -- An unqualifed version of this thing is in scope
437 unQualOK (GRE {gre_prov = LocalDef}) = True
438 unQualOK (GRE {gre_prov = Imported is}) = any unQualSpecOK is
440 unQualSpecOK :: ImportSpec -> Bool
441 -- In scope unqualified
442 unQualSpecOK is = not (is_qual (is_decl is))
444 qualSpecOK :: ModuleName -> ImportSpec -> Bool
445 -- In scope qualified with M
446 qualSpecOK mod is = mod == is_as (is_decl is)
448 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
449 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
451 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
453 = foldr add emptyGlobalRdrEnv gres
455 add gre env = extendOccEnv_C (foldr insertGRE) env
456 (nameOccName (gre_name gre))
459 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
460 insertGRE new_g [] = [new_g]
461 insertGRE new_g (old_g : old_gs)
462 | gre_name new_g == gre_name old_g
463 = new_g `plusGRE` old_g : old_gs
465 = old_g : insertGRE new_g old_gs
467 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
468 -- Used when the gre_name fields match
470 = GRE { gre_name = gre_name g1,
471 gre_prov = gre_prov g1 `plusProv` gre_prov g2,
472 gre_par = gre_par g1 `plusParent` gre_par g2 }
474 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
475 -- Hide any unqualified bindings for the specified OccNames
476 -- This is used in TH, when renaming a declaration bracket
478 -- We want unqualified 'foo' in "..." to mean this foo, not
479 -- the one from the enclosing module. But the *qualified* name
480 -- from the enclosing moudule must certainly still be avaialable
481 -- Seems like 5 times as much work as it deserves!
482 hideSomeUnquals rdr_env occs
483 = foldr hide rdr_env occs
486 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
488 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
489 = gre { gre_prov = Imported [imp_spec] }
490 where -- Local defs get transfomed to (fake) imported things
491 mod = moduleName (nameModule name)
492 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
493 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
495 is_dloc = srcLocSpan (nameSrcLoc name) }
497 qual_gre gre@(GRE { gre_prov = Imported specs })
498 = gre { gre_prov = Imported (map qual_spec specs) }
500 qual_spec spec@(ImpSpec { is_decl = decl_spec })
501 = spec { is_decl = decl_spec { is_qual = True } }
505 %************************************************************************
509 %************************************************************************
511 The "provenance" of something says how it came to be in scope.
512 It's quite elaborate so that we can give accurate unused-name warnings.
516 = LocalDef -- Defined locally
517 | Imported -- Imported
518 [ImportSpec] -- INVARIANT: non-empty
520 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
521 is_item :: ImpItemSpec }
524 data ImpDeclSpec -- Describes a particular import declaration
525 -- Shared among all the Provenaces for that decl
527 is_mod :: ModuleName, -- 'import Muggle'
528 -- Note the Muggle may well not be
529 -- the defining module for this thing!
530 -- TODO: either should be Module, or there
531 -- should be a Maybe PackageId here too.
532 is_as :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
533 is_qual :: Bool, -- True <=> qualified (only)
534 is_dloc :: SrcSpan -- Location of import declaration
537 data ImpItemSpec -- Describes import info a particular Name
538 = ImpAll -- The import had no import list,
539 -- or had a hiding list
541 | ImpSome { -- The import had an import list
543 is_iloc :: SrcSpan -- Location of the import item
545 -- The is_explicit field is True iff the thing was named
546 -- *explicitly* in the import specs rather
547 -- than being imported as part of a "..." group
548 -- e.g. import C( T(..) )
549 -- Here the constructors of T are not named explicitly;
550 -- only T is named explicitly.
552 importSpecLoc :: ImportSpec -> SrcSpan
553 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
554 importSpecLoc (ImpSpec _ item) = is_iloc item
556 importSpecModule :: ImportSpec -> ModuleName
557 importSpecModule is = is_mod (is_decl is)
559 isExplicitItem :: ImpItemSpec -> Bool
560 isExplicitItem ImpAll = False
561 isExplicitItem (ImpSome {is_explicit = exp}) = exp
563 -- Note [Comparing provenance]
564 -- Comparison of provenance is just used for grouping
565 -- error messages (in RnEnv.warnUnusedBinds)
566 instance Eq Provenance where
567 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
569 instance Eq ImpDeclSpec where
570 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
572 instance Eq ImpItemSpec where
573 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
575 instance Ord Provenance where
576 compare LocalDef LocalDef = EQ
577 compare LocalDef (Imported _) = LT
578 compare (Imported _ ) LocalDef = GT
579 compare (Imported is1) (Imported is2) = compare (head is1)
580 {- See Note [Comparing provenance] -} (head is2)
582 instance Ord ImpDeclSpec where
583 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
584 (is_dloc is1 `compare` is_dloc is2)
586 instance Ord ImpItemSpec where
587 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
591 plusProv :: Provenance -> Provenance -> Provenance
592 -- Choose LocalDef over Imported
593 -- There is an obscure bug lurking here; in the presence
594 -- of recursive modules, something can be imported *and* locally
595 -- defined, and one might refer to it with a qualified name from
596 -- the import -- but I'm going to ignore that because it makes
597 -- the isLocalGRE predicate so much nicer this way
598 plusProv LocalDef LocalDef = panic "plusProv"
599 plusProv LocalDef p2 = LocalDef
600 plusProv p1 LocalDef = LocalDef
601 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
603 pprNameProvenance :: GlobalRdrElt -> SDoc
604 -- Print out the place where the name was imported
605 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
606 = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
607 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
609 (why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
610 [] -> panic "pprNameProvenance"
612 -- If we know the exact definition point (which we may do with GHCi)
613 -- then show that too. But not if it's just "imported from X".
614 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
617 instance Outputable ImportSpec where
619 = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec)
620 <+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
623 loc = importSpecLoc imp_spec