maybeWiredInIdName, maybeWiredInTyConName,
isWiredInName,
- nameUnique, setNameUnique, setNameProvenance, getNameProvenance,
- tidyTopName, mkNameVisible,
- nameOccName, nameModule, setNameOcc,
+ nameUnique, setNameUnique, setNameProvenance, getNameProvenance, setNameImportReason,
+ tidyTopName,
+ nameOccName, nameModule, setNameOcc, nameRdrName, setNameModule,
isExportedName, nameSrcLoc,
isLocallyDefinedName,
- isSysLocalName, isLocalName, isGlobalName, isExternallyVisibleName,
+ isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
- pprNameProvenance,
- -- Misc
+ -- Provenance
Provenance(..), ImportReason(..), pprProvenance,
ExportFlag(..), PrintUnqualified,
+ pprNameProvenance, systemProvenance,
-- Class NamedThing and overloaded friends
NamedThing(..),
- modAndOcc, isExported,
+ isExported,
getSrcLoc, isLocallyDefined, getOccString
) where
#include "HsVersions.h"
-import {-# SOURCE #-} Var ( Id )
-import {-# SOURCE #-} TyCon ( TyCon )
+import {-# SOURCE #-} Var ( Id, setIdName )
+import {-# SOURCE #-} TyCon ( TyCon, setTyConName )
import OccName -- All of it
+import Module
+import RdrName ( RdrName, mkRdrQual, mkRdrUnqual )
import CmdLineOpts ( opt_PprStyle_NoPrags, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
-import BasicTypes ( IfaceFlavour(..) )
import SrcLoc ( noSrcLoc, mkBuiltinSrcLoc, SrcLoc )
import Unique ( pprUnique, Unique, Uniquable(..) )
%************************************************************************
\begin{code}
-data Name
- = Local Unique
- OccName -- How to print it
- Bool -- True <=> this is a "sys-local"
- -- see notes just below
-
-
- | Global Unique
- Module -- The defining module
- OccName -- Its name in that module
- Provenance -- How it was defined
+data Name = Name {
+ n_sort :: NameSort, -- What sort of name it is
+ n_uniq :: Unique,
+ n_occ :: OccName, -- Its occurrence name
+ n_prov :: Provenance -- How it was made
+ }
+
+data NameSort
+ = Local
+ | Global Module
+ | WiredInId Module Id
+ | WiredInTyCon Module TyCon
\end{code}
-Sys-locals are only used internally. When the compiler generates (say)
-a fresh desguar variable it always calls it "ds", and of course it gets
-a fresh unique. But when printing -ddump-xx dumps, we must print it with
-its unique, because there'll be a lot of "ds" variables. That debug
-printing issue is the ONLY way in which sys-locals are different. I think.
-
-Before anything gets printed in interface files or output code, it's
-fed through a 'tidy' processor, which zaps the OccNames to have
-unique names; and converts all sys-locals to ordinary locals
-If any desugarer sys-locals have survived that far, they get changed to
-"ds1", "ds2", etc.
-
Things with a @Global@ name are given C static labels, so they finally
appear in the .o file's symbol table. They appear in the symbol table
in the form M.n. If originally-local things have this property they
must be made @Global@ first.
-
\begin{code}
-mkLocalName :: Unique -> OccName -> Name
-mkLocalName uniq occ = Local uniq occ False
+mkLocalName :: Unique -> OccName -> SrcLoc -> Name
+mkLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ,
+ n_prov = LocalDef loc NotExported }
-- NB: You might worry that after lots of huffing and
-- puffing we might end up with two local names with distinct
-- uniques, but the same OccName. Indeed we can, but that's ok
-- into the print name (see setNameVisibility below)
mkGlobalName :: Unique -> Module -> OccName -> Provenance -> Name
-mkGlobalName = Global
+mkGlobalName uniq mod occ prov = Name { n_uniq = uniq, n_sort = Global mod,
+ n_occ = occ, n_prov = prov }
+
mkSysLocalName :: Unique -> FAST_STRING -> Name
-mkSysLocalName uniq fs = Local uniq (varOcc fs) True
+mkSysLocalName uniq fs = Name { n_uniq = uniq, n_sort = Local,
+ n_occ = mkSrcVarOcc fs, n_prov = SystemProv }
mkTopName :: Unique -> Module -> FAST_STRING -> Name
-- Make a top-level name; make it Global if top-level
-- 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!
-mkTopName uniq mod fs
- | all_toplev_ids_visible = Global uniq mod occ (LocalDef noSrcLoc NotExported)
- | otherwise = Local uniq occ False
- where
- occ = varOcc (_PK_ ((_UNPK_ fs) ++ show uniq))
+mkTopName uniq mod fs
+ = Name { n_uniq = uniq,
+ n_sort = mk_top_sort mod,
+ n_occ = mkSrcVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
+ n_prov = LocalDef noSrcLoc NotExported }
+
+------------------------- Wired in names -------------------------
mkWiredInIdName :: Unique -> Module -> OccName -> Id -> Name
-mkWiredInIdName uniq mod occ id = Global uniq mod occ (WiredInId id)
+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 occ tycon
- = Global uniq mod (tcOcc occ) (WiredInTyCon tycon)
+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
-> Unique -- New unique
-> Name -- Result is always a value name
-mkDerivedName f (Global _ mod occ prov) uniq = Global uniq mod (f occ) prov
-mkDerivedName f (Local _ occ sys) uniq = Local uniq (f occ) sys
+mkDerivedName f name uniq = name {n_uniq = uniq, n_occ = f (n_occ name)}
-- When we renumber/rename things, we need to be
-- able to change a Name's Unique to match the cached
-- one in the thing it's the name of. If you know what I mean.
-setNameUnique (Local _ occ sys) u = Local u occ sys
-setNameUnique (Global _ mod occ prov) u = Global u mod occ prov
+setNameUnique name uniq = name {n_uniq = uniq}
setNameOcc :: Name -> OccName -> Name
-- Give the thing a new OccName, *and*
-- record that it's no longer a sys-local
-- This is used by the tidy-up pass
-setNameOcc (Global uniq mod _ prov) occ = Global uniq mod occ prov
-setNameOcc (Local uniq _ sys) occ = Local uniq occ False
+setNameOcc name occ = name {n_occ = occ}
+
+setNameModule :: Name -> Module -> Name
+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}
-- It should be in the TidyOccEnv already
| otherwise = (env', name')
where
- prov = getNameProvenance name
- uniq = nameUnique name
- (env', occ') = tidyOccName env (nameOccName name)
+ (env', occ') = tidyOccName env (n_occ name)
+
+ name' = Name { n_uniq = n_uniq name, n_sort = mk_top_sort mod,
+ n_occ = occ', n_prov = LocalDef noSrcLoc NotExported }
- name' | all_toplev_ids_visible = Global uniq mod occ' prov
- | otherwise = Local uniq occ' False
+mk_top_sort mod | all_toplev_ids_visible = Global mod
+ | otherwise = Local
all_toplev_ids_visible =
not opt_OmitInterfacePragmas || -- Pragmas can make them visible
-- Implicit-provenance things, but that gives bad error messages
-- for names defined twice in the same module, so I changed it to
-- set the provenance of *any* global (SLPJ Jun 97)
-setNameProvenance (Global uniq mod occ _) prov = Global uniq mod occ prov
-setNameProvenance other_name prov = other_name
+setNameProvenance name prov = name {n_prov = prov}
getNameProvenance :: Name -> Provenance
-getNameProvenance (Global uniq mod occ prov) = prov
-getNameProvenance (Local _ _ _) = LocalDef noSrcLoc NotExported
-\end{code}
+getNameProvenance name = n_prov name
-\begin{code}
--- make the Name globally visible regardless.
-mkNameVisible :: Module -> Unique -> Name -> Name
-mkNameVisible mod occ_uniq nm@(Global _ _ _ _) = nm
-mkNameVisible mod occ_uniq nm@(Local uniq occ _)
- = Global uniq mod occ (LocalDef noSrcLoc Exported)
+setNameImportReason :: Name -> ImportReason -> Name
+setNameImportReason name reason
+ = name { n_prov = new_prov }
+ where
+ -- It's important that we don't do the pattern matching
+ -- in the top-level clause, else we get a black hole in
+ -- the renamer. Rather a yukky constraint. There's only
+ -- one call, in RnNames
+ old_prov = n_prov name
+ new_prov = case old_prov of
+ NonLocalDef _ omit -> NonLocalDef reason omit
+ other -> old_prov
\end{code}
\begin{code}
data Provenance
- = NoProvenance
-
- | LocalDef -- Defined locally
+ = LocalDef -- Defined locally
SrcLoc -- Defn site
ExportFlag -- Whether it's exported
| NonLocalDef -- Defined non-locally
ImportReason
- IfaceFlavour -- Whether the defn site is an .hi-boot file
PrintUnqualified
- | WiredInTyCon TyCon -- There's a wired-in version
- | WiredInId Id -- ...ditto...
+ | SystemProv -- Either (a) a system-generated local with
+ -- a v short name OccName
+ -- or (b) a known-key global which should have a proper
+ -- provenance attached by the renamer
+\end{code}
+
+Sys-provs are only used internally. When the compiler generates (say)
+a fresh desguar variable it always calls it "ds", and of course it gets
+a fresh unique. But when printing -ddump-xx dumps, we must print it with
+its unique, because there'll be a lot of "ds" variables.
+
+Names with SystemProv differ in the following ways:
+ a) locals have unique attached when printing dumps
+ b) unifier eliminates sys tyvars in favour of user provs where possible
+ c) renamer replaces SystemProv with a better one
+
+Before anything gets printed in interface files or output code, it's
+fed through a 'tidy' processor, which zaps the OccNames to have
+unique names; and converts all sys-locals to user locals
+If any desugarer sys-locals have survived that far, they get changed to
+"ds1", "ds2", etc.
+\begin{code}
data ImportReason
= UserImport Module SrcLoc Bool -- Imported from module M on line L
-- Note the M may well not be the defining module
\begin{code}
+systemProvenance :: Provenance
+systemProvenance = SystemProv
+
-- pprNameProvenance is used in error messages to say where a name came from
pprNameProvenance :: Name -> SDoc
pprNameProvenance name = pprProvenance (getNameProvenance name)
pprProvenance :: Provenance -> SDoc
-pprProvenance NoProvenance = ptext SLIT("No provenance")
+pprProvenance SystemProv = ptext SLIT("System")
pprProvenance (LocalDef loc _) = ptext SLIT("defined at") <+> ppr loc
-pprProvenance (WiredInTyCon tc) = ptext SLIT("Wired-in tycon")
-pprProvenance (WiredInId id) = ptext SLIT("Wired-in id")
-pprProvenance (NonLocalDef ImplicitImport _ _)
+pprProvenance (NonLocalDef ImplicitImport _)
= ptext SLIT("implicitly imported")
-pprProvenance (NonLocalDef (UserImport mod loc _) _ _)
+pprProvenance (NonLocalDef (UserImport mod loc _) _)
= ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
\end{code}
\begin{code}
nameUnique :: Name -> Unique
-nameModAndOcc :: Name -> (Module, OccName) -- Globals only
nameOccName :: Name -> OccName
nameModule :: Name -> Module
nameSrcLoc :: Name -> SrcLoc
-nameUnique (Local u _ _) = u
-nameUnique (Global u _ _ _) = u
+nameUnique name = n_uniq name
+nameOccName name = n_occ name
-nameOccName (Local _ occ _) = occ
-nameOccName (Global _ _ occ _) = occ
+nameModule name =
+ case n_sort name of
+ Local -> pprPanic "nameModule" (ppr name)
+ x -> nameSortModule x
-nameModule (Global _ mod occ _) = mod
+nameSortModule (Global mod) = mod
+nameSortModule (WiredInId mod _) = mod
+nameSortModule (WiredInTyCon mod _) = mod
-nameModAndOcc (Global _ mod occ _) = (mod,occ)
+nameRdrName :: Name -> RdrName
+nameRdrName (Name { n_sort = Local, n_occ = occ }) = mkRdrUnqual occ
+nameRdrName (Name { n_sort = sort, n_occ = occ }) = mkRdrQual (nameSortModule sort) occ
-isExportedName (Global _ _ _ (LocalDef _ Exported)) = True
-isExportedName other = False
+isExportedName (Name { n_prov = LocalDef _ Exported }) = True
+isExportedName other = False
-nameSrcLoc (Global _ _ _ (LocalDef loc _)) = loc
-nameSrcLoc (Global _ _ _ (NonLocalDef (UserImport _ loc _) _ _)) = loc
-nameSrcLoc (Global _ _ _ (WiredInTyCon _)) = mkBuiltinSrcLoc
-nameSrcLoc (Global _ _ _ (WiredInId _)) = mkBuiltinSrcLoc
-nameSrcLoc other = noSrcLoc
+nameSrcLoc name = provSrcLoc (n_prov name)
+
+provSrcLoc (LocalDef loc _) = loc
+provSrcLoc (NonLocalDef (UserImport _ loc _) _) = loc
+provSrcLoc SystemProv = noSrcLoc
-isLocallyDefinedName (Local _ _ _) = True
-isLocallyDefinedName (Global _ _ _ (LocalDef _ _)) = True
-isLocallyDefinedName other = False
+isLocallyDefinedName (Name {n_sort = Local}) = True -- Local (might have SystemProv)
+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 (Global _ _ _ (WiredInTyCon _)) = True
-isWiredInName (Global _ _ _ (WiredInId _)) = True
-isWiredInName _ = False
+isWiredInName (Name {n_sort = WiredInTyCon _ _}) = True
+isWiredInName (Name {n_sort = WiredInId _ _}) = True
+isWiredInName _ = False
maybeWiredInIdName :: Name -> Maybe Id
-maybeWiredInIdName (Global _ _ _ (WiredInId id)) = Just id
-maybeWiredInIdName other = Nothing
+maybeWiredInIdName (Name {n_sort = WiredInId _ id}) = Just id
+maybeWiredInIdName other = Nothing
maybeWiredInTyConName :: Name -> Maybe TyCon
-maybeWiredInTyConName (Global _ _ _ (WiredInTyCon tc)) = Just tc
-maybeWiredInTyConName other = Nothing
-
+maybeWiredInTyConName (Name {n_sort = WiredInTyCon _ tc}) = Just tc
+maybeWiredInTyConName other = Nothing
-isLocalName (Local _ _ _) = True
-isLocalName _ = False
-isSysLocalName (Local _ _ sys) = sys
-isSysLocalName other = False
+isLocalName (Name {n_sort = Local}) = True
+isLocalName _ = False
-isGlobalName (Global _ _ _ _) = True
-isGlobalName other = False
+isGlobalName (Name {n_sort = Local}) = False
+isGlobalName other = True
-- 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
+
+isSystemName (Name {n_prov = SystemProv}) = True
+isSystemName other = False
\end{code}
%************************************************************************
\begin{code}
-cmpName n1 n2 = c n1 n2
- where
- c (Local u1 _ _) (Local u2 _ _) = compare u1 u2
- c (Local _ _ _) _ = LT
- c (Global u1 _ _ _) (Global u2 _ _ _) = compare u1 u2
- c (Global _ _ _ _) _ = GT
+cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
\end{code}
\begin{code}
-- When printing interfaces, all Locals have been given nice print-names
ppr name = pprName name
-pprName (Local uniq occ sys_local)
+pprName (Name {n_sort = Local, n_uniq = uniq, n_occ = occ, n_prov = prov})
+ -- Locals
= getPprStyle $ \ sty ->
if codeStyle sty then
pprUnique uniq -- When printing in code we required all names to
else
pprOccName occ <> pp_local_extra sty uniq
where
+ sys_local = case prov of
+ SystemProv -> True
+ other -> False
+
pp_local_extra sty uniq
| sys_local = underscore <> pprUnique uniq -- Must print uniques for sys_locals
| debugStyle sty = text "{-" <> pprUnique uniq <> text "-}"
| otherwise = empty
-pprName (Global uniq mod occ prov)
+pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ, n_prov = prov})
+ -- Globals, and wired in things
= getPprStyle $ \ sty ->
if codeStyle sty then
ppr mod <> underscore <> ppr occ
else
pp_mod_dot sty <> ppr occ <> pp_global_debug sty uniq prov
where
+ mod = nameSortModule sort
+
pp_mod_dot sty
- = case prov of -- Omit home module qualifier if in scope
- LocalDef _ _ -> pp_qual dot (user_sty || iface_sty)
- NonLocalDef _ hif omit -> pp_qual (pp_hif hif) (omit && user_sty)
- -- Hack: omit qualifers on wired in things
- -- in user style only
- WiredInTyCon _ -> pp_qual dot user_sty
- WiredInId _ -> pp_qual dot user_sty
- NoProvenance -> pp_qual dot False
+ = case prov of
+ SystemProv -> pp_qual mod pp_sep 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 dot (user_sty || iface_sty)
+
+ NonLocalDef (UserImport imp_mod _ _) omit
+ | user_sty -> pp_qual imp_mod pp_sep omit
+ | otherwise -> pp_qual mod pp_sep False
+ NonLocalDef ImplicitImport omit -> pp_qual mod pp_sep (user_sty && omit)
where
user_sty = userStyle sty
iface_sty = ifaceStyle sty
- pp_qual sep omit_qual
+ pp_qual mod sep omit_qual
| omit_qual = empty
- | otherwise = pprModule mod <> sep
+ | otherwise = pprModule mod <> sep
- pp_hif HiFile = dot -- Vanilla case
- pp_hif HiBootFile = text "!" -- M!t indicates a name imported from a .hi-boot interface
+ pp_sep | bootFlavour (moduleIfaceFlavour mod) = text "!" -- M!t indicates a name imported
+ -- from a .hi-boot interface
+ | otherwise = dot -- Vanilla case
pp_global_debug sty uniq prov
| debugStyle sty = hcat [text "{-", pprUnique uniq, prov_p prov, text "-}"]
prov_p prov | opt_PprStyle_NoPrags = empty
| otherwise = comma <> pp_prov prov
-pp_prov (LocalDef _ Exported) = char 'x'
-pp_prov (LocalDef _ NotExported) = char 'l'
-pp_prov (NonLocalDef ImplicitImport _ _) = char 'i'
-pp_prov (NonLocalDef explicitimport _ _) = char 'I'
-pp_prov (WiredInTyCon _) = char 'W'
-pp_prov (WiredInId _) = char 'w'
-pp_prov NoProvenance = char '?'
+pp_prov (LocalDef _ Exported) = char 'x'
+pp_prov (LocalDef _ NotExported) = char 'l'
+pp_prov (NonLocalDef ImplicitImport _) = char 'j'
+pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I' -- Imported by name
+pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i' -- Imported by ..
+pp_prov SystemProv = char 's'
\end{code}
\begin{code}
class NamedThing a where
- getOccName :: a -> OccName -- Even RdrNames can do this!
+ getOccName :: a -> OccName
getName :: a -> Name
getOccName n = nameOccName (getName n) -- Default method
\end{code}
\begin{code}
-modAndOcc :: NamedThing a => a -> (Module, OccName)
getSrcLoc :: NamedThing a => a -> SrcLoc
isLocallyDefined :: NamedThing a => a -> Bool
isExported :: NamedThing a => a -> Bool
getOccString :: NamedThing a => a -> String
-modAndOcc = nameModAndOcc . getName
isExported = isExportedName . getName
getSrcLoc = nameSrcLoc . getName
isLocallyDefined = isLocallyDefinedName . getName