+\begin{code}
+instance Outputable Name where
+ -- When printing interfaces, all Locals have been given nice print-names
+ ppr name = pprName name
+
+pprName name
+ = getPprStyle $ \ sty ->
+ let
+ -- when printing local names for interface files, prepend the '_'
+ -- to avoid clashes with user-defined names. In fact, these names
+ -- will always begin with 'g' for top-level ids and 'x' otherwise,
+ -- because these are the unique supplies going into the tidy phase.
+ ppr (Local u n) | codeStyle sty = pprUnique u
+ | ifaceStyle sty = char '_' <> pprUnique u
+
+ ppr (Local u Nothing) = pprUnique u
+ ppr (Local u (Just occ)) | userStyle sty = ptext (occNameString occ)
+ | otherwise = ptext (occNameString occ) <> char '_' <> pprUnique u
+
+ ppr name@(Global u m n prov)
+ | codeStyle sty
+ = identToC (m _APPEND_ SLIT(".") _APPEND_ occNameString n)
+
+ | otherwise
+ = hcat [pp_mod_dot, ptext (occNameString n), pp_debug sty name]
+ where
+ pp_mod_dot
+ = 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
+
+ pp_qual sep omit_qual
+ | omit_qual = empty
+ | otherwise = pprModule m <> sep
+
+ dot = text "."
+ pp_hif HiFile = dot -- Vanilla case
+ pp_hif HiBootFile = text "!" -- M!t indicates a name imported from a .hi-boot interface
+
+ user_sty = userStyle sty
+ iface_sty = ifaceStyle sty
+ in
+ ppr name
+
+
+pp_debug sty (Global uniq m n prov)
+ | debugStyle sty = hcat [text "{-", pprUnique uniq, prov_p, text "-}"]
+ | otherwise = empty
+ where
+ prov_p | opt_PprStyle_NoPrags = empty
+ | otherwise = comma <> pp_prov prov
+
+pp_prov (LocalDef _ Exported) = char 'x'
+pp_prov (LocalDef _ NotExported) = char 'l'
+pp_prov (NonLocalDef _ _ _) = char 'n'
+pp_prov (WiredInTyCon _) = char 'W'
+pp_prov (WiredInId _) = char 'w'
+pp_prov NoProvenance = char '?'
+
+-- pprNameProvenance is used in error messages to say where a name came from
+pprNameProvenance :: Name -> SDoc
+pprNameProvenance (Local _ _) = pprProvenance (LocalDef noSrcLoc NotExported)
+pprNameProvenance (Global _ _ _ prov) = pprProvenance prov
+
+pprProvenance :: Provenance -> SDoc
+pprProvenance (LocalDef loc _) = ptext SLIT("Locally defined at") <+> ppr loc
+pprProvenance (NonLocalDef loc _ _) = ptext SLIT("Non-locally defined at") <+> ppr loc
+pprProvenance (WiredInTyCon tc) = ptext SLIT("Wired-in tycon")
+pprProvenance (WiredInId id) = ptext SLIT("Wired-in id")
+pprProvenance NoProvenance = ptext SLIT("No provenance")