[project @ 2000-11-01 17:15:28 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Name.lhs
index a11b797..5888124 100644 (file)
@@ -21,7 +21,7 @@ module Name (
        toRdrName, hashName,
 
        isUserExportedName,
-       nameSrcLoc, isLocallyDefinedName, isDllName,
+       nameSrcLoc, nameIsLocallyDefined, isDllName, nameIsFrom, nameIsLocalOrFrom,
 
        isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
        isTyVarName,
@@ -29,14 +29,15 @@ 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"
@@ -44,12 +45,12 @@ module Name (
 import OccName         -- All of it
 import Module          ( Module, moduleName, mkVanillaModule, 
                          printModulePrefix, isModuleInThisPackage )
-import RdrName         ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc, rdrNameModule )
+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, pprUnique10 )
-import Maybes          ( expectJust )
 import FastTypes
+import Maybes          ( expectJust )
 import UniqFM
 import Outputable
 \end{code}
@@ -121,7 +122,9 @@ 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
@@ -133,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
@@ -289,7 +301,7 @@ tidyTopName mod env
        System   -> localise            -- System local Ids
        Local    -> localise            -- User non-exported Ids
        Exported -> globalise           -- User-exported things
-       Global _ -> no_op               -- Constructors, class selectors etc
+       Global _ -> no_op               -- Constructors, class selectors, default methods
 
   where
     no_op     = (env, name)
@@ -342,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
 
 
@@ -418,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
@@ -463,11 +471,15 @@ pprLocal sty uniq occ pp_export
   | otherwise      = pprOccName occ
 
 pprGlobal sty uniq mod occ
-  | codeStyle sty         = ppr (moduleName mod) <> char '_' <> pprOccName occ
+  |  codeStyle sty        = ppr (moduleName mod) <> char '_' <> pprOccName occ
+
   | debugStyle sty        = ppr (moduleName mod) <> dot <> pprOccName occ <> 
                            text "{-" <> pprUnique10 uniq <> text "-}"
-  | printModulePrefix mod = ppr (moduleName mod) <> dot <> pprOccName occ
-  | otherwise             = pprOccName occ
+
+  | ifaceStyle sty     
+  || printModulePrefix mod = ppr (moduleName mod) <> dot <> pprOccName occ
+
+  | otherwise              = pprOccName occ
 
 pprSysLocal sty uniq occ
   | codeStyle sty  = pprUnique uniq
@@ -494,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
+isLocallyDefined    = nameIsLocallyDefined . getName
 getOccString       = occNameString        . getOccName
-toRdrName          = ifaceNameRdrName     . getName
+toRdrName          = nameRdrName          . getName
+isFrom mod x       = nameIsFrom mod (getName x)
+isLocalOrFrom mod x = nameIsLocalOrFrom mod ( getName x)
 \end{code}
 
 \begin{code}