[project @ 2000-11-01 17:15:28 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Name.lhs
index 9fe8142..5888124 100644 (file)
@@ -16,11 +16,12 @@ module Name (
 
        nameUnique, setNameUnique, setLocalNameSort,
        tidyTopName, 
-       nameOccName, nameModule, setNameOcc, nameRdrName, setNameModuleAndLoc, 
+       nameOccName, nameModule, nameModule_maybe,
+       setNameOcc, nameRdrName, setNameModuleAndLoc, 
        toRdrName, hashName,
 
        isUserExportedName,
-       nameSrcLoc, isLocallyDefinedName, isDllName,
+       nameSrcLoc, nameIsLocallyDefined, isDllName, nameIsFrom, nameIsLocalOrFrom,
 
        isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
        isTyVarName,
@@ -28,30 +29,28 @@ module Name (
        -- Environment
        NameEnv, mkNameEnv,
        emptyNameEnv, unitNameEnv, nameEnvElts, 
-       extendNameEnv_C, extendNameEnv, 
+       extendNameEnv_C, extendNameEnv, foldNameEnv,
        plusNameEnv, plusNameEnv_C, extendNameEnv, extendNameEnvList,
        lookupNameEnv, lookupNameEnv_NF, delFromNameEnv, elemNameEnv, 
 
 
        -- Class NamedThing and overloaded friends
        NamedThing(..),
-       getSrcLoc, isLocallyDefined, getOccString, toRdrName
+       getSrcLoc, isLocallyDefined, getOccString, toRdrName,
+       isFrom, isLocalOrFrom
     ) where
 
 #include "HsVersions.h"
 
 import OccName         -- All of it
-import Module          ( Module, moduleName, pprModule, mkVanillaModule, 
-                         isModuleInThisPackage )
-import RdrName         ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc, 
-                         rdrNameModule )
-import CmdLineOpts     ( opt_Static, opt_PprStyle_NoPrags, 
-                         opt_OmitInterfacePragmas, opt_EnsureSplittableC )
-
+import Module          ( Module, moduleName, mkVanillaModule, 
+                         printModulePrefix, isModuleInThisPackage )
+import RdrName         ( RdrName, mkRdrOrig, mkRdrUnqual, rdrNameOcc, rdrNameModule )
+import CmdLineOpts     ( opt_Static, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
 import SrcLoc          ( builtinSrcLoc, noSrcLoc, SrcLoc )
-import Unique          ( Unique, Uniquable(..), u2i, pprUnique )
-import Maybes          ( expectJust )
+import Unique          ( Unique, Uniquable(..), u2i, pprUnique, pprUnique10 )
 import FastTypes
+import Maybes          ( expectJust )
 import UniqFM
 import Outputable
 \end{code}
@@ -114,12 +113,18 @@ nameSrcLoc                :: Name -> SrcLoc
 nameUnique  name = n_uniq name
 nameOccName name = n_occ  name
 nameSrcLoc  name = n_loc  name
+
 nameModule (Name { n_sort = Global mod }) = mod
 nameModule name                                  = pprPanic "nameModule" (ppr name)
+
+nameModule_maybe (Name { n_sort = Global mod }) = Just mod
+nameModule_maybe name                          = Nothing
 \end{code}
 
 \begin{code}
-isLocallyDefinedName   :: Name -> Bool
+nameIsLocallyDefined   :: Name -> Bool
+nameIsFrom             :: Module -> Name -> Bool
+nameIsLocalOrFrom      :: Module -> Name -> Bool
 isUserExportedName     :: Name -> Bool
 isLocalName            :: Name -> Bool         -- Not globals
 isGlobalName           :: Name -> Bool
@@ -131,14 +136,23 @@ isGlobalName other                        = False
 
 isLocalName name = not (isGlobalName name)
 
-isLocallyDefinedName name = isLocalName name
+nameIsLocallyDefined name = isLocalName name
+
+nameIsLocalOrFrom from (Name {n_sort = Global mod}) = mod == from
+nameIsLocalOrFrom from other                       = True
+
+nameIsFrom from (Name {n_sort = Global mod}) = mod == from
+nameIsFrom from other                       = pprPanic "nameIsFrom" (ppr other)
 
 -- Global names are by definition those that are visible
 -- outside the module, *as seen by the linker*.  Externally visible
--- does not mean visible at the source level (that's isExported).
+-- does not mean visible at the source level (that's isUserExported).
 isExternallyVisibleName name = isGlobalName name
 
+-- Constructors, selectors and suchlike Globals, and are all exported
+-- Other Local things may or may not be exported
 isUserExportedName (Name { n_sort = Exported }) = True
+isUserExportedName (Name { n_sort = Global _ }) = True
 isUserExportedName other                       = False
 
 isSystemName (Name {n_sort = System}) = True
@@ -199,22 +213,6 @@ mkCCallName :: Unique -> EncodedString -> Name
 mkCCallName uniq str =  Name { n_uniq = uniq, n_sort = Local, 
                               n_occ = mkCCallOcc str, n_loc = noSrcLoc }
 
-mkTopName :: Unique -> Module -> FAST_STRING -> Name
-       -- Make a top-level name; make it Global if top-level
-       -- things should be externally visible; Local otherwise
-       -- This chap is only used *after* the tidyCore phase
-       -- Notably, it is used during STG lambda lifting
-       --
-       -- We have to make sure that the name is globally unique
-       -- and we don't have tidyCore to help us. So we append
-       -- the unique.  Hack!  Hack!
-       -- (Used only by the STG lambda lifter.)
-mkTopName uniq mod fs
-  = Name { n_uniq = uniq, 
-          n_sort = mk_top_sort mod,
-          n_occ  = mkVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
-          n_loc = noSrcLoc }
-
 mkIPName :: Unique -> OccName -> Name
 mkIPName uniq occ
   = Name { n_uniq = uniq,
@@ -297,20 +295,47 @@ are exported.  But also:
 
 \begin{code}
 tidyTopName :: Module -> TidyOccEnv -> Name -> (TidyOccEnv, Name)
-tidyTopName mod env name
-  = (env', name')
+tidyTopName mod env
+           name@(Name { n_occ = occ, n_sort = sort, n_uniq = uniq, n_loc = loc })
+  = case sort of
+       System   -> localise            -- System local Ids
+       Local    -> localise            -- User non-exported Ids
+       Exported -> globalise           -- User-exported things
+       Global _ -> no_op               -- Constructors, class selectors, default methods
+
   where
-    (env', occ') = tidyOccName env (n_occ name)
+    no_op     = (env, name)
 
-    name'        = Name { n_uniq = n_uniq name, n_sort = mk_top_sort mod,
-                         n_occ = occ', n_loc = n_loc name }
+    globalise = (env, name { n_sort = Global mod })    -- Don't change occurrence name
 
-mk_top_sort mod | all_toplev_ids_visible = Global mod
-               | otherwise              = Local
+    localise     = (env', name')
+    (env', occ') = tidyOccName env occ
+    name'        = name { n_occ = occ', n_sort = mkLocalTopSort mod }
 
-all_toplev_ids_visible = 
-       not opt_OmitInterfacePragmas ||  -- Pragmas can make them visible
-       opt_EnsureSplittableC            -- Splitting requires visiblilty
+mkTopName :: Unique -> Module -> FAST_STRING -> Name
+       -- Make a top-level name; make it Global if top-level
+       -- things should be externally visible; Local otherwise
+       -- This chap is only used *after* the tidyCore phase
+       -- Notably, it is used during STG lambda lifting
+       --
+       -- We have to make sure that the name is globally unique
+       -- and we don't have tidyCore to help us. So we append
+       -- the unique.  Hack!  Hack!
+       -- (Used only by the STG lambda lifter.)
+mkTopName uniq mod fs
+  = Name { n_uniq = uniq, 
+          n_sort = mkLocalTopSort mod,
+          n_occ  = mkVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
+          n_loc = noSrcLoc }
+
+mkLocalTopSort :: Module -> NameSort
+mkLocalTopSort mod
+  | all_toplev_ids_visible = Global mod
+  | otherwise             = Local
+
+all_toplev_ids_visible
+  = not opt_OmitInterfacePragmas ||  -- Pragmas can make them visible
+    opt_EnsureSplittableC            -- Splitting requires visiblilty
 \end{code}
 
 
@@ -329,19 +354,13 @@ hashName name = iBox (u2i (nameUnique name))
 nameRdrName :: Name -> RdrName
 -- Makes a qualified name for top-level (Global) names, whether locally defined or not
 -- and an unqualified name just for Locals
-nameRdrName (Name { n_occ = occ, n_sort = Global mod }) = mkRdrQual (moduleName mod) occ
+nameRdrName (Name { n_occ = occ, n_sort = Global mod }) = mkRdrOrig (moduleName mod) occ
 nameRdrName (Name { n_occ = occ })                     = mkRdrUnqual occ
 
-ifaceNameRdrName :: Name -> RdrName
--- Makes a qualified naem for imported things, 
--- and an unqualified one for local things
-ifaceNameRdrName n | isLocallyDefined n = mkRdrUnqual (nameOccName n)
-                  | otherwise          = mkRdrQual   (moduleName (nameModule n)) (nameOccName n) 
-
 isDllName :: Name -> Bool
        -- Does this name refer to something in a different DLL?
 isDllName nm = not opt_Static &&
-              not (isLocallyDefinedName nm) &&         -- isLocallyDefinedName test needed 'cos
+              not (nameIsLocallyDefined nm) &&                 -- isLocallyDefinedName test needed 'cos
               not (isModuleInThisPackage (nameModule nm))      -- nameModule won't work on local names
 
 
@@ -405,8 +424,10 @@ unitNameEnv         :: Name -> a -> NameEnv a
 lookupNameEnv           :: NameEnv a -> Name -> Maybe a
 lookupNameEnv_NF :: NameEnv a -> Name -> a
 mapNameEnv      :: (a->b) -> NameEnv a -> NameEnv b
+foldNameEnv     :: (a -> b -> b) -> b -> NameEnv a -> b
 
 emptyNameEnv            = emptyUFM
+foldNameEnv     = foldUFM
 mkNameEnv       = listToUFM
 nameEnvElts             = eltsUFM
 extendNameEnv_C  = addToUFM_C
@@ -437,24 +458,32 @@ instance Outputable Name where
 
 pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ})
   = getPprStyle $ \ sty ->
-    let local | debugStyle sty 
-              = pprOccName occ <> text "{-" <> pprUnique uniq <> text "-}"
-              | codeStyle sty
-              = pprUnique uniq
-              | otherwise
-              = pprOccName occ
-
-        global m | codeStyle sty
-                 = ppr (moduleName m) <> char '_' <> pprOccName occ
-                 | debugStyle sty || not (isModuleInThisPackage m)
-                 = ppr (moduleName m) <> dot <> pprOccName occ
-                 | otherwise
-                 = pprOccName occ
-     in case sort of
-           System     -> local
-           Local      -> local
-           Exported   -> local
-           Global mod -> global mod
+    case sort of
+      Global mod -> pprGlobal sty uniq mod occ
+      System     -> pprSysLocal sty uniq occ
+      Local      -> pprLocal sty uniq occ empty
+      Exported   -> pprLocal sty uniq occ (char 'x')
+
+pprLocal sty uniq occ pp_export
+  | codeStyle sty  = pprUnique uniq
+  | debugStyle sty = pprOccName occ <> 
+                    text "{-" <> pp_export <+> pprUnique10 uniq <> text "-}"
+  | otherwise      = pprOccName occ
+
+pprGlobal sty uniq mod occ
+  |  codeStyle sty        = ppr (moduleName mod) <> char '_' <> pprOccName occ
+
+  | debugStyle sty        = ppr (moduleName mod) <> dot <> pprOccName occ <> 
+                           text "{-" <> pprUnique10 uniq <> text "-}"
+
+  | ifaceStyle sty     
+  || printModulePrefix mod = ppr (moduleName mod) <> dot <> pprOccName occ
+
+  | otherwise              = pprOccName occ
+
+pprSysLocal sty uniq occ
+  | codeStyle sty  = pprUnique uniq
+  | otherwise     = pprOccName occ <> char '_' <> pprUnique uniq
 \end{code}
 
 
@@ -477,11 +506,15 @@ getSrcLoc     :: NamedThing a => a -> SrcLoc
 isLocallyDefined    :: NamedThing a => a -> Bool
 getOccString       :: NamedThing a => a -> String
 toRdrName          :: NamedThing a => a -> RdrName
+isFrom             :: NamedThing a => Module -> a -> Bool
+isLocalOrFrom      :: NamedThing a => Module -> a -> Bool
 
 getSrcLoc          = nameSrcLoc           . getName
-isLocallyDefined    = isLocallyDefinedName . getName
-getOccString x     = occNameString (getOccName x)
-toRdrName          = ifaceNameRdrName     . getName
+isLocallyDefined    = nameIsLocallyDefined . getName
+getOccString       = occNameString        . getOccName
+toRdrName          = nameRdrName          . getName
+isFrom mod x       = nameIsFrom mod (getName x)
+isLocalOrFrom mod x = nameIsLocalOrFrom mod ( getName x)
 \end{code}
 
 \begin{code}