Name, -- Abstract
mkLocalName, mkImportedLocalName, mkSysLocalName, mkCCallName,
mkTopName, mkIPName,
- mkDerivedName, mkGlobalName, mkKnownKeyGlobal,
- mkWiredInIdName, mkWiredInTyConName,
- mkUnboundName, isUnboundName,
+ mkDerivedName, mkGlobalName, mkKnownKeyGlobal, mkWiredInName,
- maybeWiredInIdName, maybeWiredInTyConName,
- isWiredInName, hashName,
-
- nameUnique, setNameUnique, setNameProvenance, getNameProvenance, setNameImportReason,
- tidyTopName,
- nameOccName, nameModule, setNameOcc, nameRdrName, setNameModule,
+ nameUnique, setNameUnique, setNameProvenance, getNameProvenance,
+ setNameImportReason, tidyTopName,
+ nameOccName, nameModule, setNameOcc, nameRdrName, setNameModule,
+ toRdrName, hashName,
isUserExportedName, isUserImportedName, isUserImportedExplicitlyName,
maybeUserImportedFrom,
nameSrcLoc, isLocallyDefinedName, isDllName,
isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
+ isTyVarName,
+ -- Environment
+ NameEnv, mkNameEnv,
+ emptyNameEnv, unitNameEnv, nameEnvElts,
+ extendNameEnv_C, extendNameEnv,
+ plusNameEnv, plusNameEnv_C, extendNameEnv, extendNameEnvList,
+ lookupNameEnv, lookupNameEnv_NF, delFromNameEnv, elemNameEnv,
+
-- Provenance
Provenance(..), ImportReason(..), pprProvenance,
#include "HsVersions.h"
-import {-# SOURCE #-} Var ( Id, setIdName )
-import {-# SOURCE #-} TyCon ( TyCon, setTyConName )
-
import OccName -- All of it
-import Module ( Module, moduleName, pprModule, mkVanillaModule, isLocalModule )
-import RdrName ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc, rdrNameModule )
-import CmdLineOpts ( opt_Static, opt_PprStyle_NoPrags, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
-
-import SrcLoc ( noSrcLoc, mkBuiltinSrcLoc, SrcLoc )
-import Unique ( pprUnique, Unique, Uniquable(..), unboundKey, u2i )
+import Module ( Module, moduleName, pprModule, mkVanillaModule,
+ isLocalModule )
+import RdrName ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc,
+ rdrNameModule )
+import CmdLineOpts ( opt_Static, opt_PprStyle_NoPrags,
+ opt_OmitInterfacePragmas, opt_EnsureSplittableC )
+
+import SrcLoc ( noSrcLoc, SrcLoc )
+import Unique ( Unique, Uniquable(..), u2i, pprUnique )
+import Maybes ( expectJust )
+import FastTypes
+import UniqFM
import Outputable
-import GlaExts
\end{code}
-
%************************************************************************
%* *
\subsection[Name-datatype]{The @Name@ datatype, and name construction}
data NameSort
= Local
| Global Module
- | WiredInId Module Id
- | WiredInTyCon Module TyCon
\end{code}
Things with a @Global@ name are given C static labels, so they finally
mkImportedLocalName :: Unique -> OccName -> SrcLoc -> Name
-- Just the same as mkLocalName, except the provenance is different
- -- Reason: this flags the name as one that came in from an interface file.
- -- This is useful when trying to decide which of two type variables
- -- should 'win' when unifying them.
+ -- Reason: this flags the name as one that came in from an interface
+ -- file. This is useful when trying to decide which of two type
+ -- variables should 'win' when unifying them.
-- NB: this is only for non-top-level names, so we use ImplicitImport
mkImportedLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ,
n_prov = NonLocalDef ImplicitImport True }
n_occ = occ, n_prov = prov }
-mkKnownKeyGlobal :: (RdrName, Unique) -> Name
-mkKnownKeyGlobal (rdr_name, uniq)
+mkKnownKeyGlobal :: RdrName -> Unique -> Name
+mkKnownKeyGlobal rdr_name uniq
= mkGlobalName uniq (mkVanillaModule (rdrNameModule rdr_name))
(rdrNameOcc rdr_name)
systemProvenance
+mkWiredInName :: Module -> OccName -> Unique -> Name
+mkWiredInName mod occ uniq = mkGlobalName uniq mod occ systemProvenance
+
mkSysLocalName :: Unique -> UserFS -> Name
mkSysLocalName uniq fs = Name { n_uniq = uniq, n_sort = Local,
- n_occ = mkSrcVarOcc fs, n_prov = systemProvenance }
+ n_occ = mkVarOcc fs, n_prov = systemProvenance }
mkCCallName :: Unique -> EncodedString -> Name
-- The encoded string completely describes the ccall
mkTopName uniq mod fs
= Name { n_uniq = uniq,
n_sort = mk_top_sort mod,
- n_occ = mkSrcVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
+ n_occ = mkVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
n_prov = LocalDef noSrcLoc NotExported }
mkIPName :: Unique -> OccName -> Name
-- ZZ is this an appropriate provinence?
n_prov = SystemProv }
-------------------------- Wired in names -------------------------
-
-mkWiredInIdName :: Unique -> Module -> OccName -> Id -> Name
-mkWiredInIdName uniq mod occ id = Name { n_uniq = uniq, n_sort = WiredInId mod id,
- n_occ = occ, n_prov = SystemProv }
-
--- mkWiredInTyConName takes a FAST_STRING instead of
--- an OccName, which is a bit yukky but that's what the
--- clients find easiest.
-mkWiredInTyConName :: Unique -> Module -> FAST_STRING -> TyCon -> Name
-mkWiredInTyConName uniq mod fs tycon
- = Name { n_uniq = uniq, n_sort = WiredInTyCon mod tycon,
- n_occ = mkSrcOccFS tcName fs, n_prov = SystemProv }
-
-
---------------------------------------------------------------------
mkDerivedName :: (OccName -> OccName)
-> Name -- Base name
-> Name -- Result is always a value name
mkDerivedName f name uniq = name {n_uniq = uniq, n_occ = f (n_occ name)}
-
--- mkUnboundName makes a place-holder Name; it shouldn't be looked at except possibly
--- during compiler debugging.
-mkUnboundName :: RdrName -> Name
-mkUnboundName rdr_name = mkLocalName unboundKey (rdrNameOcc rdr_name) noSrcLoc
-
-isUnboundName :: Name -> Bool
-isUnboundName name = getUnique name == unboundKey
\end{code}
\begin{code}
setNameModule name mod = name {n_sort = set (n_sort name)}
where
set (Global _) = Global mod
- set (WiredInId _ id) = WiredInId mod id
- set (WiredInTyCon _ tycon) = WiredInTyCon mod tycon
\end{code}
nameSrcLoc :: Name -> SrcLoc
isLocallyDefinedName :: Name -> Bool
isUserExportedName :: Name -> Bool
-isWiredInName :: Name -> Bool
isLocalName :: Name -> Bool
isGlobalName :: Name -> Bool
isExternallyVisibleName :: Name -> Bool
hashName :: Name -> Int
-hashName name = IBOX( u2i (nameUnique name) )
+hashName name = iBox (u2i (nameUnique name))
nameUnique name = n_uniq name
nameOccName name = n_occ name
x -> nameSortModule x
nameSortModule (Global mod) = mod
-nameSortModule (WiredInId mod _) = mod
-nameSortModule (WiredInTyCon mod _) = mod
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_sort = Local, n_occ = occ }) = mkRdrUnqual occ
nameRdrName (Name { n_sort = sort, n_occ = occ }) = mkRdrQual (moduleName (nameSortModule sort)) occ
-- Does this name refer to something in a different DLL?
isDllName nm = not opt_Static &&
not (isLocallyDefinedName nm) &&
+-- isLocallyDefinedName test is needed because nameModule won't work on local names
not (isLocalModule (nameModule nm))
nameSrcLoc name = provSrcLoc (n_prov name)
isLocallyDefinedName (Name {n_prov = LocalDef _ _}) = True -- Global, but defined here
isLocallyDefinedName other = False -- Other
--- Things the compiler "knows about" are in some sense
--- "imported". When we are compiling the module where
--- the entities are defined, we need to be able to pick
--- them out, often in combination with isLocallyDefined.
-isWiredInName (Name {n_sort = WiredInTyCon _ _}) = True
-isWiredInName (Name {n_sort = WiredInId _ _}) = True
-isWiredInName _ = False
-
-maybeWiredInIdName :: Name -> Maybe Id
-maybeWiredInIdName (Name {n_sort = WiredInId _ id}) = Just id
-maybeWiredInIdName other = Nothing
-
-maybeWiredInTyConName :: Name -> Maybe TyCon
-maybeWiredInTyConName (Name {n_sort = WiredInTyCon _ tc}) = Just tc
-maybeWiredInTyConName other = Nothing
-
-
isLocalName (Name {n_sort = Local}) = True
isLocalName _ = False
isGlobalName (Name {n_sort = Local}) = False
isGlobalName other = True
+isTyVarName :: Name -> Bool
+isTyVarName name = isTvOcc (nameOccName name)
+
-- 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).
isExternallyVisibleName name = isGlobalName name
hasBetterProv :: Name -> Name -> Bool
-hasBetterProv name1 name2
- = case n_prov name1 of
- LocalDef _ _ -> True
- SystemProv -> False
- NonLocalDef _ _ -> case n_prov name2 of
- LocalDef _ _ -> False
- other -> True
+-- Choose
+-- a local thing over an imported thing
+-- a user-imported thing over a non-user-imported thing
+-- an explicitly-imported thing over an implicitly imported thing
+hasBetterProv n1 n2
+ = case (n_prov n1, n_prov n2) of
+ (LocalDef _ _, _ ) -> True
+ (NonLocalDef (UserImport _ _ True) _, _ ) -> True
+ (NonLocalDef (UserImport _ _ _ ) _, NonLocalDef ImplicitImport _) -> True
+ other -> False
isSystemName (Name {n_prov = SystemProv}) = True
isSystemName other = False
%************************************************************************
%* *
+\subsection{Name environment}
+%* *
+%************************************************************************
+
+\begin{code}
+type NameEnv a = UniqFM a -- Domain is Name
+
+emptyNameEnv :: NameEnv a
+mkNameEnv :: [(Name,a)] -> NameEnv a
+nameEnvElts :: NameEnv a -> [a]
+extendNameEnv_C :: (a->a->a) -> NameEnv a -> Name -> a -> NameEnv a
+extendNameEnv :: NameEnv a -> Name -> a -> NameEnv a
+plusNameEnv :: NameEnv a -> NameEnv a -> NameEnv a
+plusNameEnv_C :: (a->a->a) -> NameEnv a -> NameEnv a -> NameEnv a
+extendNameEnvList:: NameEnv a -> [(Name,a)] -> NameEnv a
+delFromNameEnv :: NameEnv a -> Name -> NameEnv a
+elemNameEnv :: Name -> NameEnv a -> Bool
+unitNameEnv :: Name -> a -> NameEnv a
+lookupNameEnv :: NameEnv a -> Name -> Maybe a
+lookupNameEnv_NF :: NameEnv a -> Name -> a
+mapNameEnv :: (a->b) -> NameEnv a -> NameEnv b
+
+emptyNameEnv = emptyUFM
+mkNameEnv = listToUFM
+nameEnvElts = eltsUFM
+extendNameEnv_C = addToUFM_C
+extendNameEnv = addToUFM
+plusNameEnv = plusUFM
+plusNameEnv_C = plusUFM_C
+extendNameEnvList= addListToUFM
+delFromNameEnv = delFromUFM
+elemNameEnv = elemUFM
+mapNameEnv = mapUFM
+unitNameEnv = unitUFM
+
+lookupNameEnv = lookupUFM
+lookupNameEnv_NF env n = expectJust "lookupNameEnv_NF" (lookupUFM env n)
+\end{code}
+
+
+%************************************************************************
+%* *
\subsection{Pretty printing}
%* *
%************************************************************************
pp_mod_dot sty
= case prov of
- SystemProv -> pp_qual mod user_sty
- -- Hack alert! Omit the qualifier on SystemProv things in user style
- -- I claim such SystemProv things will also be WiredIn things.
- -- We can't get the omit flag right
- -- on wired in tycons etc (sigh) so we just leave it out in user style,
- -- and hope that leaving it out isn't too consfusing.
- -- (e.g. if the programmer hides Bool and redefines it. If so, use -dppr-debug.)
-
- LocalDef _ _ -> pp_qual mod (user_sty || iface_sty)
+ SystemProv -> pp_qual mod user_sty
+ -- ToDo (SDM): the following comment is out of date - do
+ -- we need to do anything different now that WiredInNames
+ -- don't exist any more?
+
+ -- Hack alert! Omit the qualifier on SystemProv things in
+ -- user style. I claim such SystemProv things will also be
+ -- WiredIn things. We can't get the omit flag right
+ -- on wired in tycons etc (sigh) so we just leave it out in
+ -- user style, and hope that leaving it out isn't too
+ -- consfusing. (e.g. if the programmer hides Bool and
+ -- redefines it. If so, use -dppr-debug.)
+
+ LocalDef _ _ -> pp_qual mod (user_sty || iface_sty)
NonLocalDef (UserImport imp_mod _ _) omit
| user_sty -> pp_qual imp_mod omit