[project @ 2004-08-31 09:48:28 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / RdrName.lhs
index f743100..82cabf6 100644 (file)
@@ -10,11 +10,9 @@ module RdrName (
 
        -- Construction
        mkRdrUnqual, mkRdrQual, 
-       mkUnqual, mkVarUnqual, mkQual, mkOrig, mkIfaceOrig, 
+       mkUnqual, mkVarUnqual, mkQual, mkOrig,
        nameRdrName, getRdrName, 
-       qualifyRdrName, unqualifyRdrName, 
        mkDerivedRdrName, 
-       dummyRdrVarName, dummyRdrTcName,
 
        -- Destruction
        rdrNameModule, rdrNameOcc, setRdrNameSpace,
@@ -22,7 +20,6 @@ module RdrName (
        isOrig, isOrig_maybe, isExact, isExact_maybe, isSrcRdrName,
 
        -- Printing;    instance Outputable RdrName
-       pprUnqualRdrName,
 
        -- LocalRdrEnv
        LocalRdrEnv, emptyLocalRdrEnv, extendLocalRdrEnv,
@@ -35,27 +32,25 @@ module RdrName (
 
        -- GlobalRdrElt, Provenance, ImportSpec
        GlobalRdrElt(..), Provenance(..), ImportSpec(..),
-       isLocalGRE, unQualOK, hasQual,
+       isLocalGRE, unQualOK,
        pprNameProvenance
   ) where 
 
 #include "HsVersions.h"
 
-import OccName ( NameSpace, tcName, varName,
-                 OccName, UserFS, EncodedFS,
-                 mkSysOccFS, setOccNameSpace,
-                 mkOccFS, mkVarOcc, occNameFlavour,
+import OccName ( NameSpace, varName,
+                 OccName, UserFS, 
+                 setOccNameSpace,
+                 mkOccFS, occNameFlavour,
                  isDataOcc, isTvOcc, isTcOcc,
                  OccEnv, emptyOccEnv, extendOccEnvList, lookupOccEnv, 
                  elemOccEnv, plusOccEnv_C, extendOccEnv_C, foldOccEnv,
                  occEnvElts
                )
-import Module   ( ModuleName, mkSysModuleNameFS, mkModuleNameFS        )
+import Module   ( ModuleName, mkModuleNameFS   )
 import Name    ( Name, NamedThing(getName), nameModuleName, nameParent_maybe,
                  nameOccName, isExternalName, nameSrcLoc )
-import Maybes  ( seqMaybe )
-import SrcLoc  ( SrcLoc, isGoodSrcLoc )
-import BasicTypes( DeprecTxt )
+import SrcLoc  ( isGoodSrcLoc, SrcSpan )
 import Outputable
 import Util    ( thenCmp )
 \end{code}
@@ -91,7 +86,7 @@ data RdrName
        --  (b) when converting names to the RdrNames in IfaceTypes
        --      Here an Exact RdrName always contains an External Name
        --      (Internal Names are converted to simple Unquals)
-       --  (c) possibly, by the meta-programming stuff
+       --  (c) by Template Haskell, when TH has generated a unique name
 \end{code}
 
 
@@ -141,9 +136,6 @@ mkRdrQual mod occ = Qual mod occ
 mkOrig :: ModuleName -> OccName -> RdrName
 mkOrig mod occ = Orig mod occ
 
-mkIfaceOrig :: NameSpace -> EncodedFS -> EncodedFS -> RdrName
-mkIfaceOrig ns m n = Orig (mkSysModuleNameFS m) (mkSysOccFS ns n)
-
 ---------------
 mkDerivedRdrName :: Name -> (OccName -> OccName) -> (RdrName)
 mkDerivedRdrName parent mk_occ
@@ -170,13 +162,6 @@ nameRdrName name = Exact name
 -- unique is still there for debug printing, particularly
 -- of Types (which are converted to IfaceTypes before printing)
 
-qualifyRdrName :: ModuleName -> RdrName -> RdrName
-       -- Sets the module name of a RdrName, even if it has one already
-qualifyRdrName mod rn = Qual mod (rdrNameOcc rn)
-
-unqualifyRdrName :: RdrName -> RdrName
-unqualifyRdrName rdr_name = Unqual (rdrNameOcc rdr_name)
-
 nukeExact :: Name -> RdrName
 nukeExact n 
   | isExternalName n = Orig (nameModuleName n) (nameOccName n)
@@ -184,17 +169,6 @@ nukeExact n
 \end{code}
 
 \begin{code}
-       -- This guy is used by the reader when HsSyn has a slot for
-       -- an implicit name that's going to be filled in by
-       -- the renamer.  We can't just put "error..." because
-       -- we sometimes want to print out stuff after reading but
-       -- before renaming
-dummyRdrVarName = Unqual (mkVarOcc FSLIT("V-DUMMY"))
-dummyRdrTcName  = Unqual (mkOccFS tcName FSLIT("TC-DUMMY"))
-\end{code}
-
-
-\begin{code}
 isRdrDataCon rn = isDataOcc (rdrNameOcc rn)
 isRdrTyVar   rn = isTvOcc   (rdrNameOcc rn)
 isRdrTc      rn = isTcOcc   (rdrNameOcc rn)
@@ -243,8 +217,6 @@ instance OutputableBndr RdrName where
        | isTvOcc (rdrNameOcc n) = char '@' <+> ppr n
        | otherwise              = ppr n
 
-pprUnqualRdrName rdr_name = ppr (rdrNameOcc rdr_name)
-
 instance Eq RdrName where
     (Exact n1)           == (Exact n2)    = n1==n2
        -- Convert exact to orig
@@ -262,21 +234,28 @@ instance Ord RdrName where
     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
     a >         b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
 
-       -- Unqual < Qual < Orig
-       -- We always convert Exact to Orig before comparing
-    compare (Exact n1) (Exact n2) | n1==n2 = EQ        -- Short cut
-                                 | otherwise = nukeExact n1 `compare` nukeExact n2
-    compare (Exact n1) n2                    = nukeExact n1 `compare` n2
-    compare n1       (Exact n2)              = n1 `compare` nukeExact n2
-
-
-    compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
-    compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
+       -- Exact < Unqual < Qual < Orig
+       -- [Note: Apr 2004] We used to use nukeExact to convert Exact to Orig 
+       --      before comparing so that Prelude.map == the exact Prelude.map, but 
+       --      that meant that we reported duplicates when renaming bindings 
+       --      generated by Template Haskell; e.g 
+       --      do { n1 <- newName "foo"; n2 <- newName "foo"; 
+       --           <decl involving n1,n2> }
+       --      I think we can do without this conversion
+    compare (Exact n1) (Exact n2) = n1 `compare` n2
+    compare (Exact n1) n2        = LT
+
+    compare (Unqual _)   (Exact _)    = GT
     compare (Unqual o1)  (Unqual  o2) = o1 `compare` o2
     compare (Unqual _)   _           = LT
+
+    compare (Qual _ _)   (Exact _)    = GT
+    compare (Qual _ _)   (Unqual _)   = GT
+    compare (Qual m1 o1) (Qual m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
     compare (Qual _ _)   (Orig _ _)   = LT
-    compare _           _            = GT
+
+    compare (Orig m1 o1) (Orig m2 o2) = (o1 `compare` o2) `thenCmp` (m1 `compare` m2) 
+    compare (Orig _ _)   _           = GT
 \end{code}
 
 
@@ -300,9 +279,9 @@ extendLocalRdrEnv env names
   = extendOccEnvList env [(nameOccName n, n) | n <- names]
 
 lookupLocalRdrEnv :: LocalRdrEnv -> RdrName -> Maybe Name
-lookupLocalRdrEnv env rdr_name
-  | isUnqual rdr_name = lookupOccEnv env (rdrNameOcc rdr_name)
-  | otherwise        = Nothing
+lookupLocalRdrEnv env (Exact name) = Just name
+lookupLocalRdrEnv env (Unqual occ) = lookupOccEnv env occ
+lookupLocalRdrEnv env other       = Nothing
 
 elemLocalRdrEnv :: RdrName -> LocalRdrEnv -> Bool
 elemLocalRdrEnv rdr_name env 
@@ -338,8 +317,7 @@ globalRdrEnvElts env = foldOccEnv (++) [] env
 
 data GlobalRdrElt 
   = GRE { gre_name   :: Name,
-         gre_prov   :: Provenance,     -- Why it's in scope
-         gre_deprec :: Maybe DeprecTxt -- Whether this name is deprecated
+         gre_prov   :: Provenance      -- Why it's in scope
     }
 
 instance Outputable GlobalRdrElt where
@@ -417,11 +395,8 @@ insertGRE new_g (old_g : old_gs)
 plusGRE :: GlobalRdrElt -> GlobalRdrElt -> GlobalRdrElt
 -- Used when the gre_name fields match
 plusGRE g1 g2
-  = GRE { gre_name   = gre_name g1,
-         gre_prov   = gre_prov g1 `plusProv` gre_prov g2,
-         gre_deprec = gre_deprec g1 `seqMaybe` gre_deprec g2 }
-       -- Could the deprecs be different?  If we re-export
-       -- something deprecated, is it propagated?  I forget.
+  = GRE { gre_name = gre_name g1,
+         gre_prov = gre_prov g1 `plusProv` gre_prov g2 }
 \end{code}
 
 
@@ -459,7 +434,7 @@ data ImportSpec             -- Describes a particular import declaration
                                        -- the defining module for this thing!
        is_as   :: ModuleName,          -- 'as M' (or 'Muggle' if there is no 'as' clause)
        is_qual :: Bool,                -- True <=> qualified (only)
-       is_loc  :: SrcLoc }             -- Location of import statment
+       is_loc  :: SrcSpan }            -- Location of import statment
 
 -- Comparison of provenance is just used for grouping 
 -- error messages (in RnEnv.warnUnusedBinds)
@@ -483,7 +458,7 @@ instance Ord ImportSpec where
 \begin{code}
 plusProv :: Provenance -> Provenance -> Provenance
 -- Choose LocalDef over Imported
--- There is an obscure bug lurking here, in the presence
+-- There is an obscure bug lurking here; in the presence
 -- of recursive modules, something can be imported *and* locally
 -- defined, and one might refer to it with a qualified name from
 -- the import -- but I'm going to ignore that because it makes
@@ -499,12 +474,15 @@ pprNameProvenance :: GlobalRdrElt -> SDoc
 pprNameProvenance (GRE {gre_name = name, gre_prov = LocalDef _})
   = ptext SLIT("defined at") <+> ppr (nameSrcLoc name)
 pprNameProvenance (GRE {gre_name = name, gre_prov = Imported (why:whys) _})
-  = sep [ppr_reason why, nest 2 (ppr_defn (nameSrcLoc name))]
-
-ppr_reason imp_spec
- = ptext SLIT("imported from") <+> ppr (is_mod imp_spec) 
-       <+> ptext SLIT("at") <+> ppr (is_loc imp_spec)
+  = sep [ppr why, nest 2 (ppr_defn (nameSrcLoc name))]
 
+-- If we know the exact definition point (which we may do with GHCi)
+-- then show that too.  But not if it's just "imported from X".
 ppr_defn loc | isGoodSrcLoc loc = parens (ptext SLIT("defined at") <+> ppr loc)
             | otherwise        = empty
+
+instance Outputable ImportSpec where
+   ppr imp_spec
+     = ptext SLIT("imported from") <+> ppr (is_mod imp_spec) 
+       <+> ptext SLIT("at") <+> ppr (is_loc imp_spec)
 \end{code}