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, hideSomeUnquals,
40 -- GlobalRdrElt, Provenance, ImportSpec
41 GlobalRdrElt(..), isLocalGRE, unQualOK, qualSpecOK, unQualSpecOK,
42 Provenance(..), pprNameProvenance,
44 ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
45 importSpecLoc, importSpecModule, isExplicitItem
48 #include "HsVersions.h"
59 %************************************************************************
61 \subsection{The main data type}
63 %************************************************************************
68 -- Used for ordinary, unqualified occurrences
70 | Qual ModuleName OccName
71 -- A qualified name written by the user in
72 -- *source* code. The module isn't necessarily
73 -- the module where the thing is defined;
74 -- just the one from which it is imported
77 -- An original name; the module is the *defining* module.
78 -- This is used when GHC generates code that will be fed
79 -- into the renamer (e.g. from deriving clauses), but where
80 -- we want to say "Use Prelude.map dammit".
83 -- We know exactly the Name. This is used
84 -- (a) when the parser parses built-in syntax like "[]"
85 -- and "(,)", but wants a RdrName from it
86 -- (b) by Template Haskell, when TH has generated a unique name
90 %************************************************************************
92 \subsection{Simple functions}
94 %************************************************************************
97 rdrNameOcc :: RdrName -> OccName
98 rdrNameOcc (Qual _ occ) = occ
99 rdrNameOcc (Unqual occ) = occ
100 rdrNameOcc (Orig _ occ) = occ
101 rdrNameOcc (Exact name) = nameOccName name
103 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
104 -- This rather gruesome function is used mainly by the parser
105 -- When parsing data T a = T | T1 Int
106 -- we parse the data constructors as *types* because of parser ambiguities,
107 -- so then we need to change the *type constr* to a *data constr*
109 -- The original-name case *can* occur when parsing
110 -- data [] a = [] | a : [a]
111 -- For the orig-name case we return an unqualified name.
112 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
113 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
114 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
115 setRdrNameSpace (Exact n) ns = Orig (nameModule n)
116 (setOccNameSpace ns (nameOccName n))
120 -- These two are the basic constructors
121 mkRdrUnqual :: OccName -> RdrName
122 mkRdrUnqual occ = Unqual occ
124 mkRdrQual :: ModuleName -> OccName -> RdrName
125 mkRdrQual mod occ = Qual mod occ
127 mkOrig :: Module -> OccName -> RdrName
128 mkOrig mod occ = Orig mod occ
131 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
132 mkDerivedRdrName parent mk_occ
133 = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
136 -- These two are used when parsing source files
137 -- They do encode the module and occurrence names
138 mkUnqual :: NameSpace -> FastString -> RdrName
139 mkUnqual sp n = Unqual (mkOccNameFS sp n)
141 mkVarUnqual :: FastString -> RdrName
142 mkVarUnqual n = Unqual (mkVarOccFS n)
144 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
145 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
147 getRdrName :: NamedThing thing => thing -> RdrName
148 getRdrName name = nameRdrName (getName name)
150 nameRdrName :: Name -> RdrName
151 nameRdrName name = Exact name
152 -- Keep the Name even for Internal names, so that the
153 -- unique is still there for debug printing, particularly
154 -- of Types (which are converted to IfaceTypes before printing)
156 nukeExact :: Name -> RdrName
158 | isExternalName n = Orig (nameModule n) (nameOccName n)
159 | otherwise = Unqual (nameOccName n)
163 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
164 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
165 isRdrTc rn = isTcOcc (rdrNameOcc rn)
167 isSrcRdrName (Unqual _) = True
168 isSrcRdrName (Qual _ _) = True
169 isSrcRdrName _ = False
171 isUnqual (Unqual _) = True
172 isUnqual other = False
174 isQual (Qual _ _) = True
177 isQual_maybe (Qual m n) = Just (m,n)
178 isQual_maybe _ = Nothing
180 isOrig (Orig _ _) = True
183 isOrig_maybe (Orig m n) = Just (m,n)
184 isOrig_maybe _ = Nothing
186 isExact (Exact _) = True
187 isExact other = False
189 isExact_maybe (Exact n) = Just n
190 isExact_maybe other = Nothing
194 %************************************************************************
196 \subsection{Instances}
198 %************************************************************************
201 instance Outputable RdrName where
202 ppr (Exact name) = ppr name
203 ppr (Unqual occ) = ppr occ
204 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
205 ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
207 instance OutputableBndr RdrName where
209 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
212 instance Eq RdrName where
213 (Exact n1) == (Exact n2) = n1==n2
214 -- Convert exact to orig
215 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
216 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
218 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
219 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
220 (Unqual o1) == (Unqual o2) = o1==o2
223 instance Ord RdrName where
224 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
225 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
226 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
227 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
229 -- Exact < Unqual < Qual < Orig
230 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
231 -- before comparing so that Prelude.map == the exact Prelude.map, but
232 -- that meant that we reported duplicates when renaming bindings
233 -- generated by Template Haskell; e.g
234 -- do { n1 <- newName "foo"; n2 <- newName "foo";
235 -- <decl involving n1,n2> }
236 -- I think we can do without this conversion
237 compare (Exact n1) (Exact n2) = n1 `compare` n2
238 compare (Exact n1) n2 = LT
240 compare (Unqual _) (Exact _) = GT
241 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
242 compare (Unqual _) _ = LT
244 compare (Qual _ _) (Exact _) = GT
245 compare (Qual _ _) (Unqual _) = GT
246 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
247 compare (Qual _ _) (Orig _ _) = LT
249 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
250 compare (Orig _ _) _ = GT
255 %************************************************************************
259 %************************************************************************
261 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
262 It is keyed by OccName, because we never use it for qualified names.
265 type LocalRdrEnv = OccEnv Name
267 emptyLocalRdrEnv = emptyOccEnv
269 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
270 extendLocalRdrEnv env names
271 = extendOccEnvList env [(nameOccName n, n) | n <- names]
273 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
274 lookupLocalRdrEnv env (Exact name) = Just name
275 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
276 lookupLocalRdrEnv env other = Nothing
278 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
279 elemLocalRdrEnv rdr_name env
280 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
285 %************************************************************************
289 %************************************************************************
292 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
293 -- Keyed by OccName; when looking up a qualified name
294 -- we look up the OccName part, and then check the Provenance
295 -- to see if the appropriate qualification is valid. This
296 -- saves routinely doubling the size of the env by adding both
297 -- qualified and unqualified names to the domain.
299 -- The list in the range is reqd because there may be name clashes
300 -- These only get reported on lookup, not on construction
302 -- INVARIANT: All the members of the list have distinct
303 -- gre_name fields; that is, no duplicate Names
306 = GRE { gre_name :: Name,
308 gre_prov :: Provenance -- Why it's in scope
311 data Parent = NoParent | ParentIs Name
314 instance Outputable Parent where
316 ppr (ParentIs n) = ptext SLIT("parent:") <> ppr n
319 plusParent :: Parent -> Parent -> Parent
320 plusParent p1 p2 = ASSERT2( p1 == p2, parens (ppr p1) <+> parens (ppr p2) )
323 {- Why so complicated? -=chak
324 plusParent :: Parent -> Parent -> Parent
325 plusParent NoParent rel =
326 ASSERT2( case rel of { NoParent -> True; other -> False },
327 ptext SLIT("plusParent[NoParent]: ") <+> ppr rel )
329 plusParent (ParentIs n) rel =
330 ASSERT2( case rel of { ParentIs m -> n==m; other -> False },
331 ptext SLIT("plusParent[ParentIs]:") <+> ppr n <> comma <+> ppr rel )
335 emptyGlobalRdrEnv = emptyOccEnv
337 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
338 globalRdrEnvElts env = foldOccEnv (++) [] env
340 instance Outputable GlobalRdrElt where
341 ppr gre = ppr name <+> parens (ppr (gre_par gre) <+> pprNameProvenance gre)
345 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
347 = vcat (map pp (occEnvElts env))
349 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
350 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
355 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
356 lookupGlobalRdrEnv env rdr_name = case lookupOccEnv env rdr_name of
360 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
361 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
363 occ = nameOccName (gre_name gre)
364 add gres _ = gre:gres
366 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
367 lookupGRE_RdrName rdr_name env
368 = case lookupOccEnv env (rdrNameOcc rdr_name) of
370 Just gres -> pickGREs rdr_name gres
372 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
373 lookupGRE_Name env name
374 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
375 gre_name gre == name ]
378 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
379 -- Take a list of GREs which have the right OccName
380 -- Pick those GREs that are suitable for this RdrName
381 -- And for those, keep only only the Provenances that are suitable
384 -- module A ( f ) where
385 -- import qualified Foo( f )
388 -- Let's suppose that Foo.f and Baz.f are the same entity really.
389 -- The export of f is ambiguous because it's in scope from the local def
390 -- and the import. The lookup of (Unqual f) should return a GRE for
391 -- the locally-defined f, and a GRE for the imported f, with a *single*
392 -- provenance, namely the one for Baz(f).
393 pickGREs rdr_name gres
394 = mapCatMaybes pick gres
396 rdr_is_unqual = isUnqual rdr_name
397 rdr_is_qual = isQual_maybe rdr_name
399 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
400 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
401 | rdr_is_unqual = Just gre
402 | Just (mod,_) <- rdr_is_qual,
403 mod == moduleName (nameModule n) = Just gre
404 | otherwise = Nothing
405 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
407 not (is_qual (is_decl is)) = Just gre
408 | Just (mod,_) <- rdr_is_qual,
409 mod == is_as (is_decl is) = Just gre
410 | otherwise = Nothing
411 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
412 | null filtered_is = Nothing
413 | otherwise = Just (gre {gre_prov = Imported filtered_is})
415 filtered_is | rdr_is_unqual
416 = filter (not . is_qual . is_decl) is
417 | Just (mod,_) <- rdr_is_qual
418 = filter ((== mod) . is_as . is_decl) is
422 isLocalGRE :: GlobalRdrElt -> Bool
423 isLocalGRE (GRE {gre_prov = LocalDef}) = True
424 isLocalGRE other = False
426 unQualOK :: GlobalRdrElt -> Bool
427 -- An unqualifed version of this thing is in scope
428 unQualOK (GRE {gre_prov = LocalDef}) = True
429 unQualOK (GRE {gre_prov = Imported is}) = any unQualSpecOK is
431 unQualSpecOK :: ImportSpec -> Bool
432 -- In scope unqualified
433 unQualSpecOK is = not (is_qual (is_decl is))
435 qualSpecOK :: ModuleName -> ImportSpec -> Bool
436 -- In scope qualified with M
437 qualSpecOK mod is = mod == is_as (is_decl is)
439 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
440 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
442 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
444 = foldr add emptyGlobalRdrEnv gres
446 add gre env = extendOccEnv_C (foldr insertGRE) env
447 (nameOccName (gre_name gre))
450 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
451 insertGRE new_g [] = [new_g]
452 insertGRE new_g (old_g : old_gs)
453 | gre_name new_g == gre_name old_g
454 = new_g `plusGRE` old_g : old_gs
456 = old_g : insertGRE new_g old_gs
458 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
459 -- Used when the gre_name fields match
461 = GRE { gre_name = gre_name g1,
462 gre_prov = gre_prov g1 `plusProv` gre_prov g2,
463 gre_par = gre_par g1 `plusParent` gre_par g2 }
465 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
466 -- Hide any unqualified bindings for the specified OccNames
467 -- This is used in TH, when renaming a declaration bracket
469 -- We want unqualified 'foo' in "..." to mean this foo, not
470 -- the one from the enclosing module. But the *qualified* name
471 -- from the enclosing moudule must certainly still be avaialable
472 -- Seems like 5 times as much work as it deserves!
473 hideSomeUnquals rdr_env occs
474 = foldr hide rdr_env occs
477 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
479 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
480 = gre { gre_prov = Imported [imp_spec] }
481 where -- Local defs get transfomed to (fake) imported things
482 mod = moduleName (nameModule name)
483 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
484 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
486 is_dloc = srcLocSpan (nameSrcLoc name) }
488 qual_gre gre@(GRE { gre_prov = Imported specs })
489 = gre { gre_prov = Imported (map qual_spec specs) }
491 qual_spec spec@(ImpSpec { is_decl = decl_spec })
492 = spec { is_decl = decl_spec { is_qual = True } }
496 %************************************************************************
500 %************************************************************************
502 The "provenance" of something says how it came to be in scope.
503 It's quite elaborate so that we can give accurate unused-name warnings.
507 = LocalDef -- Defined locally
508 | Imported -- Imported
509 [ImportSpec] -- INVARIANT: non-empty
511 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
512 is_item :: ImpItemSpec }
515 data ImpDeclSpec -- Describes a particular import declaration
516 -- Shared among all the Provenaces for that decl
518 is_mod :: ModuleName, -- 'import Muggle'
519 -- Note the Muggle may well not be
520 -- the defining module for this thing!
521 -- TODO: either should be Module, or there
522 -- should be a Maybe PackageId here too.
523 is_as :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
524 is_qual :: Bool, -- True <=> qualified (only)
525 is_dloc :: SrcSpan -- Location of import declaration
528 data ImpItemSpec -- Describes import info a particular Name
529 = ImpAll -- The import had no import list,
530 -- or had a hiding list
532 | ImpSome { -- The import had an import list
534 is_iloc :: SrcSpan -- Location of the import item
536 -- The is_explicit field is True iff the thing was named
537 -- *explicitly* in the import specs rather
538 -- than being imported as part of a "..." group
539 -- e.g. import C( T(..) )
540 -- Here the constructors of T are not named explicitly;
541 -- only T is named explicitly.
543 importSpecLoc :: ImportSpec -> SrcSpan
544 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
545 importSpecLoc (ImpSpec _ item) = is_iloc item
547 importSpecModule :: ImportSpec -> ModuleName
548 importSpecModule is = is_mod (is_decl is)
550 isExplicitItem :: ImpItemSpec -> Bool
551 isExplicitItem ImpAll = False
552 isExplicitItem (ImpSome {is_explicit = exp}) = exp
554 -- Note [Comparing provenance]
555 -- Comparison of provenance is just used for grouping
556 -- error messages (in RnEnv.warnUnusedBinds)
557 instance Eq Provenance where
558 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
560 instance Eq ImpDeclSpec where
561 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
563 instance Eq ImpItemSpec where
564 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
566 instance Ord Provenance where
567 compare LocalDef LocalDef = EQ
568 compare LocalDef (Imported _) = LT
569 compare (Imported _ ) LocalDef = GT
570 compare (Imported is1) (Imported is2) = compare (head is1)
571 {- See Note [Comparing provenance] -} (head is2)
573 instance Ord ImpDeclSpec where
574 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
575 (is_dloc is1 `compare` is_dloc is2)
577 instance Ord ImpItemSpec where
578 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
582 plusProv :: Provenance -> Provenance -> Provenance
583 -- Choose LocalDef over Imported
584 -- There is an obscure bug lurking here; in the presence
585 -- of recursive modules, something can be imported *and* locally
586 -- defined, and one might refer to it with a qualified name from
587 -- the import -- but I'm going to ignore that because it makes
588 -- the isLocalGRE predicate so much nicer this way
589 plusProv LocalDef LocalDef = panic "plusProv"
590 plusProv LocalDef p2 = LocalDef
591 plusProv p1 LocalDef = LocalDef
592 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
594 pprNameProvenance :: GlobalRdrElt -> SDoc
595 -- Print out the place where the name was imported
596 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
597 = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
598 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
600 (why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
601 [] -> panic "pprNameProvenance"
603 -- If we know the exact definition point (which we may do with GHCi)
604 -- then show that too. But not if it's just "imported from X".
605 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
608 instance Outputable ImportSpec where
610 = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec)
611 <+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
614 loc = importSpecLoc imp_spec