2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 RdrName(..), -- Constructors exported only to BinIface
11 mkRdrUnqual, mkRdrQual,
12 mkUnqual, mkVarUnqual, mkQual, mkOrig,
13 nameRdrName, getRdrName,
17 rdrNameOcc, setRdrNameSpace,
18 isRdrDataCon, isRdrTyVar, isRdrTc, isQual, isQual_maybe, isUnqual,
19 isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
21 -- Printing; instance Outputable RdrName
24 LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv,
25 lookupLocalRdrEnv, elemLocalRdrEnv,
28 GlobalRdrEnv, emptyGlobalRdrEnv, mkGlobalRdrEnv, plusGlobalRdrEnv,
29 lookupGlobalRdrEnv, extendGlobalRdrEnv,
30 pprGlobalRdrEnv, globalRdrEnvElts,
31 lookupGRE_RdrName, lookupGRE_Name, hideSomeUnquals,
33 -- GlobalRdrElt, Provenance, ImportSpec
34 GlobalRdrElt(..), isLocalGRE, unQualOK, qualSpecOK, unQualSpecOK,
35 Provenance(..), pprNameProvenance,
37 ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
38 importSpecLoc, importSpecModule, isExplicitItem
41 #include "HsVersions.h"
52 %************************************************************************
54 \subsection{The main data type}
56 %************************************************************************
61 -- Used for ordinary, unqualified occurrences
63 | Qual ModuleName OccName
64 -- A qualified name written by the user in
65 -- *source* code. The module isn't necessarily
66 -- the module where the thing is defined;
67 -- just the one from which it is imported
70 -- An original name; the module is the *defining* module.
71 -- This is used when GHC generates code that will be fed
72 -- into the renamer (e.g. from deriving clauses), but where
73 -- we want to say "Use Prelude.map dammit".
76 -- We know exactly the Name. This is used
77 -- (a) when the parser parses built-in syntax like "[]"
78 -- and "(,)", but wants a RdrName from it
79 -- (b) by Template Haskell, when TH has generated a unique name
83 %************************************************************************
85 \subsection{Simple functions}
87 %************************************************************************
90 rdrNameOcc :: RdrName -> OccName
91 rdrNameOcc (Qual _ occ) = occ
92 rdrNameOcc (Unqual occ) = occ
93 rdrNameOcc (Orig _ occ) = occ
94 rdrNameOcc (Exact name) = nameOccName name
96 setRdrNameSpace :: RdrName -> NameSpace -> RdrName
97 -- This rather gruesome function is used mainly by the parser
98 -- When parsing data T a = T | T1 Int
99 -- we parse the data constructors as *types* because of parser ambiguities,
100 -- so then we need to change the *type constr* to a *data constr*
102 -- The original-name case *can* occur when parsing
103 -- data [] a = [] | a : [a]
104 -- For the orig-name case we return an unqualified name.
105 setRdrNameSpace (Unqual occ) ns = Unqual (setOccNameSpace ns occ)
106 setRdrNameSpace (Qual m occ) ns = Qual m (setOccNameSpace ns occ)
107 setRdrNameSpace (Orig m occ) ns = Orig m (setOccNameSpace ns occ)
108 setRdrNameSpace (Exact n) ns = Orig (nameModule n)
109 (setOccNameSpace ns (nameOccName n))
113 -- These two are the basic constructors
114 mkRdrUnqual :: OccName -> RdrName
115 mkRdrUnqual occ = Unqual occ
117 mkRdrQual :: ModuleName -> OccName -> RdrName
118 mkRdrQual mod occ = Qual mod occ
120 mkOrig :: Module -> OccName -> RdrName
121 mkOrig mod occ = Orig mod occ
124 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
125 mkDerivedRdrName parent mk_occ
126 = mkOrig (nameModule parent) (mk_occ (nameOccName parent))
129 -- These two are used when parsing source files
130 -- They do encode the module and occurrence names
131 mkUnqual :: NameSpace -> FastString -> RdrName
132 mkUnqual sp n = Unqual (mkOccNameFS sp n)
134 mkVarUnqual :: FastString -> RdrName
135 mkVarUnqual n = Unqual (mkVarOccFS n)
137 mkQual :: NameSpace -> (FastString, FastString) -> RdrName
138 mkQual sp (m, n) = Qual (mkModuleNameFS m) (mkOccNameFS sp n)
140 getRdrName :: NamedThing thing => thing -> RdrName
141 getRdrName name = nameRdrName (getName name)
143 nameRdrName :: Name -> RdrName
144 nameRdrName name = Exact name
145 -- Keep the Name even for Internal names, so that the
146 -- unique is still there for debug printing, particularly
147 -- of Types (which are converted to IfaceTypes before printing)
149 nukeExact :: Name -> RdrName
151 | isExternalName n = Orig (nameModule n) (nameOccName n)
152 | otherwise = Unqual (nameOccName n)
156 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
157 isRdrTyVar rn = isTvOcc (rdrNameOcc rn)
158 isRdrTc rn = isTcOcc (rdrNameOcc rn)
160 isSrcRdrName (Unqual _) = True
161 isSrcRdrName (Qual _ _) = True
162 isSrcRdrName _ = False
164 isUnqual (Unqual _) = True
165 isUnqual other = False
167 isQual (Qual _ _) = True
170 isQual_maybe (Qual m n) = Just (m,n)
171 isQual_maybe _ = Nothing
173 isOrig (Orig _ _) = True
176 isOrig_maybe (Orig m n) = Just (m,n)
177 isOrig_maybe _ = Nothing
179 isExact (Exact _) = True
180 isExact other = False
182 isExact_maybe (Exact n) = Just n
183 isExact_maybe other = Nothing
187 %************************************************************************
189 \subsection{Instances}
191 %************************************************************************
194 instance Outputable RdrName where
195 ppr (Exact name) = ppr name
196 ppr (Unqual occ) = ppr occ
197 ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
198 ppr (Orig mod occ) = ppr mod <> dot <> ppr occ
200 instance OutputableBndr RdrName where
202 | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
205 instance Eq RdrName where
206 (Exact n1) == (Exact n2) = n1==n2
207 -- Convert exact to orig
208 (Exact n1) == r2@(Orig _ _) = nukeExact n1 == r2
209 r1@(Orig _ _) == (Exact n2) = r1 == nukeExact n2
211 (Orig m1 o1) == (Orig m2 o2) = m1==m2 && o1==o2
212 (Qual m1 o1) == (Qual m2 o2) = m1==m2 && o1==o2
213 (Unqual o1) == (Unqual o2) = o1==o2
216 instance Ord RdrName where
217 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
218 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
219 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
220 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
222 -- Exact < Unqual < Qual < Orig
223 -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig
224 -- before comparing so that Prelude.map == the exact Prelude.map, but
225 -- that meant that we reported duplicates when renaming bindings
226 -- generated by Template Haskell; e.g
227 -- do { n1 <- newName "foo"; n2 <- newName "foo";
228 -- <decl involving n1,n2> }
229 -- I think we can do without this conversion
230 compare (Exact n1) (Exact n2) = n1 `compare` n2
231 compare (Exact n1) n2 = LT
233 compare (Unqual _) (Exact _) = GT
234 compare (Unqual o1) (Unqual o2) = o1 `compare` o2
235 compare (Unqual _) _ = LT
237 compare (Qual _ _) (Exact _) = GT
238 compare (Qual _ _) (Unqual _) = GT
239 compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
240 compare (Qual _ _) (Orig _ _) = LT
242 compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2)
243 compare (Orig _ _) _ = GT
248 %************************************************************************
252 %************************************************************************
254 A LocalRdrEnv is used for local bindings (let, where, lambda, case)
255 It is keyed by OccName, because we never use it for qualified names.
258 type LocalRdrEnv = OccEnv Name
260 emptyLocalRdrEnv = emptyOccEnv
262 extendLocalRdrEnv :: LocalRdrEnv -> [Name] -> LocalRdrEnv
263 extendLocalRdrEnv env names
264 = extendOccEnvList env [(nameOccName n, n) | n <- names]
266 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
267 lookupLocalRdrEnv env (Exact name) = Just name
268 lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
269 lookupLocalRdrEnv env other = Nothing
271 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
272 elemLocalRdrEnv rdr_name env
273 | isUnqual rdr_name = rdrNameOcc rdr_name `elemOccEnv` env
278 %************************************************************************
282 %************************************************************************
285 type GlobalRdrEnv = OccEnv [GlobalRdrElt]
286 -- Keyed by OccName; when looking up a qualified name
287 -- we look up the OccName part, and then check the Provenance
288 -- to see if the appropriate qualification is valid. This
289 -- saves routinely doubling the size of the env by adding both
290 -- qualified and unqualified names to the domain.
292 -- The list in the range is reqd because there may be name clashes
293 -- These only get reported on lookup, not on construction
295 -- INVARIANT: All the members of the list have distinct
296 -- gre_name fields; that is, no duplicate Names
299 = GRE { gre_name :: Name,
301 gre_prov :: Provenance -- Why it's in scope
304 data Parent = NoParent | ParentIs Name
307 instance Outputable Parent where
309 ppr (ParentIs n) = ptext SLIT("parent:") <> ppr n
312 plusParent :: Parent -> Parent -> Parent
313 plusParent p1 p2 = ASSERT2( p1 == p2, parens (ppr p1) <+> parens (ppr p2) )
316 {- Why so complicated? -=chak
317 plusParent :: Parent -> Parent -> Parent
318 plusParent NoParent rel =
319 ASSERT2( case rel of { NoParent -> True; other -> False },
320 ptext SLIT("plusParent[NoParent]: ") <+> ppr rel )
322 plusParent (ParentIs n) rel =
323 ASSERT2( case rel of { ParentIs m -> n==m; other -> False },
324 ptext SLIT("plusParent[ParentIs]:") <+> ppr n <> comma <+> ppr rel )
328 emptyGlobalRdrEnv = emptyOccEnv
330 globalRdrEnvElts :: GlobalRdrEnv -> [GlobalRdrElt]
331 globalRdrEnvElts env = foldOccEnv (++) [] env
333 instance Outputable GlobalRdrElt where
334 ppr gre = ppr name <+> parens (ppr (gre_par gre) <+> pprNameProvenance gre)
338 pprGlobalRdrEnv :: GlobalRdrEnv -> SDoc
340 = vcat (map pp (occEnvElts env))
342 pp gres = ppr (nameOccName (gre_name (head gres))) <> colon <+>
343 vcat [ ppr (gre_name gre) <+> pprNameProvenance gre
348 lookupGlobalRdrEnv :: GlobalRdrEnv -> OccName -> [GlobalRdrElt]
349 lookupGlobalRdrEnv env rdr_name = case lookupOccEnv env rdr_name of
353 extendGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrElt -> GlobalRdrEnv
354 extendGlobalRdrEnv env gre = extendOccEnv_C add env occ [gre]
356 occ = nameOccName (gre_name gre)
357 add gres _ = gre:gres
359 lookupGRE_RdrName :: RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
360 lookupGRE_RdrName rdr_name env
361 = case lookupOccEnv env (rdrNameOcc rdr_name) of
363 Just gres -> pickGREs rdr_name gres
365 lookupGRE_Name :: GlobalRdrEnv -> Name -> [GlobalRdrElt]
366 lookupGRE_Name env name
367 = [ gre | gre <- lookupGlobalRdrEnv env (nameOccName name),
368 gre_name gre == name ]
371 pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
372 -- Take a list of GREs which have the right OccName
373 -- Pick those GREs that are suitable for this RdrName
374 -- And for those, keep only only the Provenances that are suitable
377 -- module A ( f ) where
378 -- import qualified Foo( f )
381 -- Let's suppose that Foo.f and Baz.f are the same entity really.
382 -- The export of f is ambiguous because it's in scope from the local def
383 -- and the import. The lookup of (Unqual f) should return a GRE for
384 -- the locally-defined f, and a GRE for the imported f, with a *single*
385 -- provenance, namely the one for Baz(f).
386 pickGREs rdr_name gres
387 = mapCatMaybes pick gres
389 rdr_is_unqual = isUnqual rdr_name
390 rdr_is_qual = isQual_maybe rdr_name
392 pick :: GlobalRdrElt -> Maybe GlobalRdrElt
393 pick gre@(GRE {gre_prov = LocalDef, gre_name = n}) -- Local def
394 | rdr_is_unqual = Just gre
395 | Just (mod,_) <- rdr_is_qual,
396 mod == moduleName (nameModule n) = Just gre
397 | otherwise = Nothing
398 pick gre@(GRE {gre_prov = Imported [is]}) -- Single import (efficiency)
400 not (is_qual (is_decl is)) = Just gre
401 | Just (mod,_) <- rdr_is_qual,
402 mod == is_as (is_decl is) = Just gre
403 | otherwise = Nothing
404 pick gre@(GRE {gre_prov = Imported is}) -- Multiple import
405 | null filtered_is = Nothing
406 | otherwise = Just (gre {gre_prov = Imported filtered_is})
408 filtered_is | rdr_is_unqual
409 = filter (not . is_qual . is_decl) is
410 | Just (mod,_) <- rdr_is_qual
411 = filter ((== mod) . is_as . is_decl) is
415 isLocalGRE :: GlobalRdrElt -> Bool
416 isLocalGRE (GRE {gre_prov = LocalDef}) = True
417 isLocalGRE other = False
419 unQualOK :: GlobalRdrElt -> Bool
420 -- An unqualifed version of this thing is in scope
421 unQualOK (GRE {gre_prov = LocalDef}) = True
422 unQualOK (GRE {gre_prov = Imported is}) = any unQualSpecOK is
424 unQualSpecOK :: ImportSpec -> Bool
425 -- In scope unqualified
426 unQualSpecOK is = not (is_qual (is_decl is))
428 qualSpecOK :: ModuleName -> ImportSpec -> Bool
429 -- In scope qualified with M
430 qualSpecOK mod is = mod == is_as (is_decl is)
432 plusGlobalRdrEnv :: GlobalRdrEnv -> GlobalRdrEnv -> GlobalRdrEnv
433 plusGlobalRdrEnv env1 env2 = plusOccEnv_C (foldr insertGRE) env1 env2
435 mkGlobalRdrEnv :: [GlobalRdrElt] -> GlobalRdrEnv
437 = foldr add emptyGlobalRdrEnv gres
439 add gre env = extendOccEnv_C (foldr insertGRE) env
440 (nameOccName (gre_name gre))
443 insertGRE :: GlobalRdrElt -> [GlobalRdrElt] -> [GlobalRdrElt]
444 insertGRE new_g [] = [new_g]
445 insertGRE new_g (old_g : old_gs)
446 | gre_name new_g == gre_name old_g
447 = new_g `plusGRE` old_g : old_gs
449 = old_g : insertGRE new_g old_gs
451 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
452 -- Used when the gre_name fields match
454 = GRE { gre_name = gre_name g1,
455 gre_prov = gre_prov g1 `plusProv` gre_prov g2,
456 gre_par = gre_par g1 `plusParent` gre_par g2 }
458 hideSomeUnquals :: GlobalRdrEnv -> [OccName] -> GlobalRdrEnv
459 -- Hide any unqualified bindings for the specified OccNames
460 -- This is used in TH, when renaming a declaration bracket
462 -- We want unqualified 'foo' in "..." to mean this foo, not
463 -- the one from the enclosing module. But the *qualified* name
464 -- from the enclosing moudule must certainly still be avaialable
465 -- Seems like 5 times as much work as it deserves!
466 hideSomeUnquals rdr_env occs
467 = foldr hide rdr_env occs
470 | Just gres <- lookupOccEnv env occ = extendOccEnv env occ (map qual_gre gres)
472 qual_gre gre@(GRE { gre_name = name, gre_prov = LocalDef })
473 = gre { gre_prov = Imported [imp_spec] }
474 where -- Local defs get transfomed to (fake) imported things
475 mod = moduleName (nameModule name)
476 imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
477 decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
479 is_dloc = srcLocSpan (nameSrcLoc name) }
481 qual_gre gre@(GRE { gre_prov = Imported specs })
482 = gre { gre_prov = Imported (map qual_spec specs) }
484 qual_spec spec@(ImpSpec { is_decl = decl_spec })
485 = spec { is_decl = decl_spec { is_qual = True } }
489 %************************************************************************
493 %************************************************************************
495 The "provenance" of something says how it came to be in scope.
496 It's quite elaborate so that we can give accurate unused-name warnings.
500 = LocalDef -- Defined locally
501 | Imported -- Imported
502 [ImportSpec] -- INVARIANT: non-empty
504 data ImportSpec = ImpSpec { is_decl :: ImpDeclSpec,
505 is_item :: ImpItemSpec }
508 data ImpDeclSpec -- Describes a particular import declaration
509 -- Shared among all the Provenaces for that decl
511 is_mod :: ModuleName, -- 'import Muggle'
512 -- Note the Muggle may well not be
513 -- the defining module for this thing!
514 -- TODO: either should be Module, or there
515 -- should be a Maybe PackageId here too.
516 is_as :: ModuleName, -- 'as M' (or 'Muggle' if there is no 'as' clause)
517 is_qual :: Bool, -- True <=> qualified (only)
518 is_dloc :: SrcSpan -- Location of import declaration
521 data ImpItemSpec -- Describes import info a particular Name
522 = ImpAll -- The import had no import list,
523 -- or had a hiding list
525 | ImpSome { -- The import had an import list
527 is_iloc :: SrcSpan -- Location of the import item
529 -- The is_explicit field is True iff the thing was named
530 -- *explicitly* in the import specs rather
531 -- than being imported as part of a "..." group
532 -- e.g. import C( T(..) )
533 -- Here the constructors of T are not named explicitly;
534 -- only T is named explicitly.
536 importSpecLoc :: ImportSpec -> SrcSpan
537 importSpecLoc (ImpSpec decl ImpAll) = is_dloc decl
538 importSpecLoc (ImpSpec _ item) = is_iloc item
540 importSpecModule :: ImportSpec -> ModuleName
541 importSpecModule is = is_mod (is_decl is)
543 isExplicitItem :: ImpItemSpec -> Bool
544 isExplicitItem ImpAll = False
545 isExplicitItem (ImpSome {is_explicit = exp}) = exp
547 -- Note [Comparing provenance]
548 -- Comparison of provenance is just used for grouping
549 -- error messages (in RnEnv.warnUnusedBinds)
550 instance Eq Provenance where
551 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
553 instance Eq ImpDeclSpec where
554 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
556 instance Eq ImpItemSpec where
557 p1 == p2 = case p1 `compare` p2 of EQ -> True; _ -> False
559 instance Ord Provenance where
560 compare LocalDef LocalDef = EQ
561 compare LocalDef (Imported _) = LT
562 compare (Imported _ ) LocalDef = GT
563 compare (Imported is1) (Imported is2) = compare (head is1)
564 {- See Note [Comparing provenance] -} (head is2)
566 instance Ord ImpDeclSpec where
567 compare is1 is2 = (is_mod is1 `compare` is_mod is2) `thenCmp`
568 (is_dloc is1 `compare` is_dloc is2)
570 instance Ord ImpItemSpec where
571 compare is1 is2 = is_iloc is1 `compare` is_iloc is2
575 plusProv :: Provenance -> Provenance -> Provenance
576 -- Choose LocalDef over Imported
577 -- There is an obscure bug lurking here; in the presence
578 -- of recursive modules, something can be imported *and* locally
579 -- defined, and one might refer to it with a qualified name from
580 -- the import -- but I'm going to ignore that because it makes
581 -- the isLocalGRE predicate so much nicer this way
582 plusProv LocalDef LocalDef = panic "plusProv"
583 plusProv LocalDef p2 = LocalDef
584 plusProv p1 LocalDef = LocalDef
585 plusProv (Imported is1) (Imported is2) = Imported (is1++is2)
587 pprNameProvenance :: GlobalRdrElt -> SDoc
588 -- Print out the place where the name was imported
589 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef})
590 = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
591 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported whys})
593 (why:whys) -> sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
594 [] -> panic "pprNameProvenance"
596 -- If we know the exact definition point (which we may do with GHCi)
597 -- then show that too. But not if it's just "imported from X".
598 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
601 instance Outputable ImportSpec where
603 = ptext SLIT("imported from") <+> ppr (importSpecModule imp_spec)
604 <+> if isGoodSrcSpan loc then ptext SLIT("at") <+> ppr loc
607 loc = importSpecLoc imp_spec