[project @ 2000-10-12 13:11:45 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Name.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
5
6 \begin{code}
7 module Name (
8         -- Re-export the OccName stuff
9         module OccName,
10
11         -- The Name type
12         Name,                                   -- Abstract
13         mkLocalName, mkImportedLocalName, mkSysLocalName, mkCCallName,
14         mkTopName, mkIPName,
15         mkDerivedName, mkGlobalName, mkKnownKeyGlobal,
16         mkWiredInIdName, mkWiredInTyConName,
17
18         maybeWiredInIdName, maybeWiredInTyConName,
19         isWiredInName, hashName,
20
21         nameUnique, setNameUnique, setNameProvenance, getNameProvenance, setNameImportReason,
22         tidyTopName, 
23         nameOccName, nameModule, setNameOcc, nameRdrName, setNameModule, toRdrName,
24
25         isUserExportedName, isUserImportedName, isUserImportedExplicitlyName, 
26         maybeUserImportedFrom,
27         nameSrcLoc, isLocallyDefinedName, isDllName,
28
29         isSystemName, isLocalName, isGlobalName, isExternallyVisibleName,
30         isTyVarName,
31         
32         -- Environment
33         NameEnv, mkNameEnv,
34         emptyNameEnv, unitNameEnv, nameEnvElts, 
35         extendNameEnv_C, extendNameEnv, 
36         plusNameEnv, plusNameEnv_C, extendNameEnv, extendNameEnvList,
37         lookupNameEnv, lookupNameEnv_NF, delFromNameEnv, elemNameEnv, 
38
39
40         -- Provenance
41         Provenance(..), ImportReason(..), pprProvenance,
42         ExportFlag(..), PrintUnqualified,
43         pprNameProvenance, hasBetterProv,
44
45         -- Class NamedThing and overloaded friends
46         NamedThing(..),
47         getSrcLoc, isLocallyDefined, getOccString, toRdrName
48     ) where
49
50 #include "HsVersions.h"
51
52 import {-# SOURCE #-} Var   ( Id )
53 import {-# SOURCE #-} TyCon ( TyCon )
54
55 import OccName          -- All of it
56 import Module           ( Module, moduleName, pprModule, mkVanillaModule, isLocalModule )
57 import RdrName          ( RdrName, mkRdrQual, mkRdrUnqual, rdrNameOcc, rdrNameModule )
58 import CmdLineOpts      ( opt_Static, opt_PprStyle_NoPrags, opt_OmitInterfacePragmas, opt_EnsureSplittableC )
59
60 import SrcLoc           ( noSrcLoc, SrcLoc )
61 import Unique           ( Unique, Uniquable(..), u2i, hasKey, pprUnique )
62 import Maybes           ( expectJust )
63 import FastTypes
64 import UniqFM
65 import Outputable
66 \end{code}
67
68
69 %************************************************************************
70 %*                                                                      *
71 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
72 %*                                                                      *
73 %************************************************************************
74  
75 \begin{code}
76 data Name = Name {
77                 n_sort :: NameSort,     -- What sort of name it is
78                 n_uniq :: Unique,
79                 n_occ  :: OccName,      -- Its occurrence name
80                 n_prov :: Provenance    -- How it was made
81             }
82
83 data NameSort
84   = Local
85   | Global Module
86   | WiredInId Module Id
87   | WiredInTyCon Module TyCon
88 \end{code}
89
90 Things with a @Global@ name are given C static labels, so they finally
91 appear in the .o file's symbol table.  They appear in the symbol table
92 in the form M.n.  If originally-local things have this property they
93 must be made @Global@ first.
94
95 \begin{code}
96 mkLocalName :: Unique -> OccName -> SrcLoc -> Name
97 mkLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ, 
98                                   n_prov = LocalDef loc NotExported }
99         -- NB: You might worry that after lots of huffing and
100         -- puffing we might end up with two local names with distinct
101         -- uniques, but the same OccName.  Indeed we can, but that's ok
102         --      * the insides of the compiler don't care: they use the Unique
103         --      * when printing for -ddump-xxx you can switch on -dppr-debug to get the
104         --        uniques if you get confused
105         --      * for interface files we tidyCore first, which puts the uniques
106         --        into the print name (see setNameVisibility below)
107
108 mkImportedLocalName :: Unique -> OccName -> SrcLoc -> Name
109         -- Just the same as mkLocalName, except the provenance is different
110         -- Reason: this flags the name as one that came in from an interface file.
111         -- This is useful when trying to decide which of two type variables
112         -- should 'win' when unifying them.
113         -- NB: this is only for non-top-level names, so we use ImplicitImport
114 mkImportedLocalName uniq occ loc = Name { n_uniq = uniq, n_sort = Local, n_occ = occ, 
115                                           n_prov = NonLocalDef ImplicitImport True }
116
117
118 mkGlobalName :: Unique -> Module -> OccName -> Provenance -> Name
119 mkGlobalName uniq mod occ prov = Name { n_uniq = uniq, n_sort = Global mod,
120                                         n_occ = occ, n_prov = prov }
121                                 
122
123 mkKnownKeyGlobal :: RdrName -> Unique -> Name
124 mkKnownKeyGlobal rdr_name uniq
125   = mkGlobalName uniq (mkVanillaModule (rdrNameModule rdr_name))
126                       (rdrNameOcc rdr_name)
127                       systemProvenance
128
129 mkSysLocalName :: Unique -> UserFS -> Name
130 mkSysLocalName uniq fs = Name { n_uniq = uniq, n_sort = Local, 
131                                 n_occ = mkVarOcc fs, n_prov = systemProvenance }
132
133 mkCCallName :: Unique -> EncodedString -> Name
134         -- The encoded string completely describes the ccall
135 mkCCallName uniq str =  Name { n_uniq = uniq, n_sort = Local, 
136                                n_occ = mkCCallOcc str, 
137                                n_prov = NonLocalDef ImplicitImport True }
138
139 mkTopName :: Unique -> Module -> FAST_STRING -> Name
140         -- Make a top-level name; make it Global if top-level
141         -- things should be externally visible; Local otherwise
142         -- This chap is only used *after* the tidyCore phase
143         -- Notably, it is used during STG lambda lifting
144         --
145         -- We have to make sure that the name is globally unique
146         -- and we don't have tidyCore to help us. So we append
147         -- the unique.  Hack!  Hack!
148 mkTopName uniq mod fs
149   = Name { n_uniq = uniq, 
150            n_sort = mk_top_sort mod,
151            n_occ  = mkVarOcc (_PK_ ((_UNPK_ fs) ++ show uniq)),
152            n_prov = LocalDef noSrcLoc NotExported }
153
154 mkIPName :: Unique -> OccName -> Name
155 mkIPName uniq occ
156   = Name { n_uniq = uniq,
157            n_sort = Local,
158            n_occ  = occ,
159            -- ZZ is this an appropriate provinence?
160            n_prov = SystemProv }
161
162 ------------------------- Wired in names -------------------------
163
164 mkWiredInIdName :: Unique -> Module -> OccName -> Id -> Name
165 mkWiredInIdName uniq mod occ id = Name { n_uniq = uniq, n_sort = WiredInId mod id,
166                                          n_occ = occ, n_prov = SystemProv }
167
168 mkWiredInTyConName :: Unique -> Module -> OccName -> TyCon -> Name
169 mkWiredInTyConName uniq mod occ tycon
170   = Name { n_uniq = uniq, n_sort = WiredInTyCon mod tycon,
171            n_occ = occ, n_prov = SystemProv }
172
173
174 ---------------------------------------------------------------------
175 mkDerivedName :: (OccName -> OccName)
176               -> Name           -- Base name
177               -> Unique         -- New unique
178               -> Name           -- Result is always a value name
179
180 mkDerivedName f name uniq = name {n_uniq = uniq, n_occ = f (n_occ name)}
181 \end{code}
182
183 \begin{code}
184 -- When we renumber/rename things, we need to be
185 -- able to change a Name's Unique to match the cached
186 -- one in the thing it's the name of.  If you know what I mean.
187 setNameUnique name uniq = name {n_uniq = uniq}
188
189 setNameOcc :: Name -> OccName -> Name
190         -- Give the thing a new OccName, *and*
191         -- record that it's no longer a sys-local
192         -- This is used by the tidy-up pass
193 setNameOcc name occ = name {n_occ = occ}
194
195 setNameModule :: Name -> Module -> Name
196 setNameModule name mod = name {n_sort = set (n_sort name)}
197                        where
198                          set (Global _)             = Global mod
199                          set (WiredInId _ id)       = WiredInId mod id
200                          set (WiredInTyCon _ tycon) = WiredInTyCon mod tycon
201 \end{code}
202
203
204 %************************************************************************
205 %*                                                                      *
206 \subsection{Setting provenance and visibility
207 %*                                                                      *
208 %************************************************************************
209
210 tidyTopName is applied to top-level names in the final program
211
212 For top-level things, it globalises Local names 
213                                 (if all top-level things should be visible)
214                          and localises non-exported Global names
215                                  (if only exported things should be visible)
216
217 In all cases except an exported global, it gives it a new occurrence name.
218
219 The "visibility" here concerns whether the .o file's symbol table
220 mentions the thing; if so, it needs a module name in its symbol.
221 The Global things are "visible" and the Local ones are not
222
223 Why should things be "visible"?  Certainly they must be if they
224 are exported.  But also:
225
226 (a) In certain (prelude only) modules we split up the .hc file into
227     lots of separate little files, which are separately compiled by the C
228     compiler.  That gives lots of little .o files.  The idea is that if
229     you happen to mention one of them you don't necessarily pull them all
230     in.  (Pulling in a piece you don't need can be v bad, because it may
231     mention other pieces you don't need either, and so on.)
232     
233     Sadly, splitting up .hc files means that local names (like s234) are
234     now globally visible, which can lead to clashes between two .hc
235     files. So unlocaliseWhatnot goes through making all the local things
236     into global things, essentially by giving them full names so when they
237     are printed they'll have their module name too.  Pretty revolting
238     really.
239
240 (b) When optimisation is on we want to make all the internal
241     top-level defns externally visible
242
243 \begin{code}
244 tidyTopName :: Module -> TidyOccEnv -> Name -> (TidyOccEnv, Name)
245 tidyTopName mod env name
246   = (env', name')
247   where
248     (env', occ') = tidyOccName env (n_occ name)
249
250     name'        = Name { n_uniq = n_uniq name, n_sort = mk_top_sort mod,
251                           n_occ = occ', n_prov = LocalDef noSrcLoc NotExported }
252
253 mk_top_sort mod | all_toplev_ids_visible = Global mod
254                 | otherwise              = Local
255
256 all_toplev_ids_visible = 
257         not opt_OmitInterfacePragmas ||  -- Pragmas can make them visible
258         opt_EnsureSplittableC            -- Splitting requires visiblilty
259 \end{code}
260
261
262 \begin{code}
263 setNameProvenance :: Name -> Provenance -> Name 
264         -- setNameProvenance used to only change the provenance of 
265         -- Implicit-provenance things, but that gives bad error messages 
266         -- for names defined twice in the same module, so I changed it to 
267         -- set the provenance of *any* global (SLPJ Jun 97)
268 setNameProvenance name prov = name {n_prov = prov}
269
270 getNameProvenance :: Name -> Provenance
271 getNameProvenance name = n_prov name
272
273 setNameImportReason :: Name -> ImportReason -> Name
274 setNameImportReason name reason
275   = name { n_prov = new_prov }
276   where
277         -- It's important that we don't do the pattern matching
278         -- in the top-level clause, else we get a black hole in 
279         -- the renamer.  Rather a yukky constraint.  There's only
280         -- one call, in RnNames
281     old_prov = n_prov name
282     new_prov = case old_prov of
283                   NonLocalDef _ omit -> NonLocalDef reason omit
284                   other              -> old_prov
285 \end{code}
286
287
288 %************************************************************************
289 %*                                                                      *
290 \subsection{Provenance and export info}
291 %*                                                                      *
292 %************************************************************************
293
294 \begin{code}
295 data Provenance
296   = LocalDef                    -- Defined locally
297         SrcLoc                  -- Defn site
298         ExportFlag              -- Whether it's exported
299
300   | NonLocalDef                 -- Defined non-locally
301         ImportReason
302         PrintUnqualified
303
304   | SystemProv                  -- Either (a) a system-generated local with 
305                                 --            a v short name OccName
306                                 -- or     (b) a known-key global which should have a proper
307                                 --            provenance attached by the renamer
308 \end{code}
309
310 Sys-provs are only used internally.  When the compiler generates (say)
311 a fresh desguar variable it always calls it "ds", and of course it gets
312 a fresh unique.  But when printing -ddump-xx dumps, we must print it with
313 its unique, because there'll be a lot of "ds" variables.
314
315 Names with SystemProv differ in the following ways:
316         a) locals have unique attached when printing dumps
317         b) unifier eliminates sys tyvars in favour of user provs where possible
318         c) renamer replaces SystemProv with a better one
319
320 Before anything gets printed in interface files or output code, it's
321 fed through a 'tidy' processor, which zaps the OccNames to have
322 unique names; and converts all sys-locals to user locals
323 If any desugarer sys-locals have survived that far, they get changed to
324 "ds1", "ds2", etc.
325
326 \begin{code}
327 data ImportReason
328   = UserImport Module SrcLoc Bool       -- Imported from module M on line L
329                                         -- Note the M may well not be the defining module
330                                         -- for this thing!
331         -- The Bool is true iff the thing was named *explicitly* in the import spec,
332         -- rather than being imported as part of a group; e.g.
333         --      import B
334         --      import C( T(..) )
335         -- Here, everything imported by B, and the constructors of T
336         -- are not named explicitly; only T is named explicitly.
337         -- This info is used when warning of unused names.
338
339   | ImplicitImport                      -- Imported implicitly for some other reason
340                         
341
342 type PrintUnqualified = Bool    -- True <=> the unqualified name of this thing is
343                                 -- in scope in this module, so print it 
344                                 -- unqualified in error messages
345
346 data ExportFlag = Exported  | NotExported
347 \end{code}
348
349 Something is "Exported" if it may be mentioned by another module without
350 warning.  The crucial thing about Exported things is that they must
351 never be dropped as dead code, even if they aren't used in this module.
352 Furthermore, being Exported means that we can't see all call sites of the thing.
353
354 Exported things include:
355
356         - explicitly exported Ids, including data constructors, 
357           class method selectors
358
359         - dfuns from instance decls
360
361 Being Exported is *not* the same as finally appearing in the .o file's 
362 symbol table.  For example, a local Id may be mentioned in an Exported
363 Id's unfolding in the interface file, in which case the local Id goes
364 out too.
365
366
367 \begin{code}
368 systemProvenance :: Provenance
369 systemProvenance = SystemProv
370
371 -- pprNameProvenance is used in error messages to say where a name came from
372 pprNameProvenance :: Name -> SDoc
373 pprNameProvenance name = pprProvenance (getNameProvenance name)
374
375 pprProvenance :: Provenance -> SDoc
376 pprProvenance SystemProv             = ptext SLIT("System")
377 pprProvenance (LocalDef loc _)       = ptext SLIT("defined at")    <+> ppr loc
378 pprProvenance (NonLocalDef ImplicitImport _)
379   = ptext SLIT("implicitly imported")
380 pprProvenance (NonLocalDef (UserImport mod loc _) _) 
381   =  ptext SLIT("imported from") <+> ppr mod <+> ptext SLIT("at") <+> ppr loc
382 \end{code}
383
384
385 %************************************************************************
386 %*                                                                      *
387 \subsection{Predicates and selectors}
388 %*                                                                      *
389 %************************************************************************
390
391 \begin{code}
392 nameUnique              :: Name -> Unique
393 nameOccName             :: Name -> OccName 
394 nameModule              :: Name -> Module
395 nameSrcLoc              :: Name -> SrcLoc
396 isLocallyDefinedName    :: Name -> Bool
397 isUserExportedName      :: Name -> Bool
398 isWiredInName           :: Name -> Bool
399 isLocalName             :: Name -> Bool
400 isGlobalName            :: Name -> Bool
401 isExternallyVisibleName :: Name -> Bool
402
403
404
405 hashName :: Name -> Int
406 hashName name = iBox (u2i (nameUnique name))
407
408 nameUnique name = n_uniq name
409 nameOccName name = n_occ name
410
411 nameModule name =
412   case n_sort name of
413     Local -> pprPanic "nameModule" (ppr name)
414     x     -> nameSortModule x
415
416 nameSortModule (Global       mod)   = mod
417 nameSortModule (WiredInId    mod _) = mod
418 nameSortModule (WiredInTyCon mod _) = mod
419
420 nameRdrName :: Name -> RdrName
421 -- Makes a qualified name for top-level (Global) names, whether locally defined or not
422 -- and an unqualified name just for Locals
423 nameRdrName (Name { n_sort = Local, n_occ = occ }) = mkRdrUnqual occ
424 nameRdrName (Name { n_sort = sort,  n_occ = occ }) = mkRdrQual (moduleName (nameSortModule sort)) occ
425
426 ifaceNameRdrName :: Name -> RdrName
427 -- Makes a qualified naem for imported things, 
428 -- and an unqualified one for local things
429 ifaceNameRdrName n | isLocallyDefined n = mkRdrUnqual (nameOccName n)
430                    | otherwise          = mkRdrQual   (moduleName (nameModule n)) (nameOccName n) 
431
432 isUserExportedName (Name { n_prov = LocalDef _ Exported }) = True
433 isUserExportedName other                                   = False
434
435 isUserImportedExplicitlyName (Name { n_prov = NonLocalDef (UserImport _ _ explicit) _ }) = explicit
436 isUserImportedExplicitlyName other                                                       = False
437
438 isUserImportedName (Name { n_prov = NonLocalDef (UserImport _ _ _) _ }) = True
439 isUserImportedName other                                                = False
440
441 maybeUserImportedFrom (Name { n_prov = NonLocalDef (UserImport m _ _) _ }) = Just m
442 maybeUserImportedFrom other                                                = Nothing
443
444 isDllName :: Name -> Bool
445         -- Does this name refer to something in a different DLL?
446 isDllName nm = not opt_Static &&
447                not (isLocallyDefinedName nm) && 
448 -- isLocallyDefinedName test is needed because nameModule won't work on local names
449                not (isLocalModule (nameModule nm))
450
451 nameSrcLoc name = provSrcLoc (n_prov name)
452
453 provSrcLoc (LocalDef loc _)                     = loc        
454 provSrcLoc (NonLocalDef (UserImport _ loc _) _) = loc
455 provSrcLoc other                                = noSrcLoc   
456   
457 isLocallyDefinedName (Name {n_sort = Local})        = True      -- Local (might have SystemProv)
458 isLocallyDefinedName (Name {n_prov = LocalDef _ _}) = True      -- Global, but defined here
459 isLocallyDefinedName other                          = False     -- Other
460
461 -- Things the compiler "knows about" are in some sense
462 -- "imported".  When we are compiling the module where
463 -- the entities are defined, we need to be able to pick
464 -- them out, often in combination with isLocallyDefined.
465 isWiredInName (Name {n_sort = WiredInTyCon _ _}) = True
466 isWiredInName (Name {n_sort = WiredInId    _ _}) = True
467 isWiredInName _                                  = False
468
469 maybeWiredInIdName :: Name -> Maybe Id
470 maybeWiredInIdName (Name {n_sort = WiredInId _ id}) = Just id
471 maybeWiredInIdName other                            = Nothing
472
473 maybeWiredInTyConName :: Name -> Maybe TyCon
474 maybeWiredInTyConName (Name {n_sort = WiredInTyCon _ tc}) = Just tc
475 maybeWiredInTyConName other                               = Nothing
476
477
478 isLocalName (Name {n_sort = Local}) = True
479 isLocalName _                       = False
480
481 isGlobalName (Name {n_sort = Local}) = False
482 isGlobalName other                   = True
483
484 isTyVarName :: Name -> Bool
485 isTyVarName name = isTvOcc (nameOccName name)
486
487 -- Global names are by definition those that are visible
488 -- outside the module, *as seen by the linker*.  Externally visible
489 -- does not mean visible at the source level (that's isExported).
490 isExternallyVisibleName name = isGlobalName name
491
492 hasBetterProv :: Name -> Name -> Bool
493 -- Choose 
494 --      a local thing                 over an   imported thing
495 --      a user-imported thing         over a    non-user-imported thing
496 --      an explicitly-imported thing  over an   implicitly imported thing
497 hasBetterProv n1 n2
498   = case (n_prov n1, n_prov n2) of
499         (LocalDef _ _,                        _                           ) -> True
500         (NonLocalDef (UserImport _ _ True) _, _                           ) -> True
501         (NonLocalDef (UserImport _ _ _   ) _, NonLocalDef ImplicitImport _) -> True
502         other                                                               -> False
503
504 isSystemName (Name {n_prov = SystemProv}) = True
505 isSystemName other                        = False
506 \end{code}
507
508
509 %************************************************************************
510 %*                                                                      *
511 \subsection[Name-instances]{Instance declarations}
512 %*                                                                      *
513 %************************************************************************
514
515 \begin{code}
516 cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
517 \end{code}
518
519 \begin{code}
520 instance Eq Name where
521     a == b = case (a `compare` b) of { EQ -> True;  _ -> False }
522     a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
523
524 instance Ord Name where
525     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
526     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
527     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
528     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
529     compare a b = cmpName a b
530
531 instance Uniquable Name where
532     getUnique = nameUnique
533
534 instance NamedThing Name where
535     getName n = n
536 \end{code}
537
538
539 %************************************************************************
540 %*                                                                      *
541 \subsection{Name environment}
542 %*                                                                      *
543 %************************************************************************
544
545 \begin{code}
546 type NameEnv a = UniqFM a       -- Domain is Name
547
548 emptyNameEnv     :: NameEnv a
549 mkNameEnv        :: [(Name,a)] -> NameEnv a
550 nameEnvElts      :: NameEnv a -> [a]
551 extendNameEnv_C  :: (a->a->a) -> NameEnv a -> Name -> a -> NameEnv a
552 extendNameEnv    :: NameEnv a -> Name -> a -> NameEnv a
553 plusNameEnv      :: NameEnv a -> NameEnv a -> NameEnv a
554 plusNameEnv_C    :: (a->a->a) -> NameEnv a -> NameEnv a -> NameEnv a
555 extendNameEnvList:: NameEnv a -> [(Name,a)] -> NameEnv a
556 delFromNameEnv   :: NameEnv a -> Name -> NameEnv a
557 elemNameEnv      :: Name -> NameEnv a -> Bool
558 unitNameEnv      :: Name -> a -> NameEnv a
559 lookupNameEnv    :: NameEnv a -> Name -> Maybe a
560 lookupNameEnv_NF :: NameEnv a -> Name -> a
561 mapNameEnv       :: (a->b) -> NameEnv a -> NameEnv b
562
563 emptyNameEnv     = emptyUFM
564 mkNameEnv        = listToUFM
565 nameEnvElts      = eltsUFM
566 extendNameEnv_C  = addToUFM_C
567 extendNameEnv    = addToUFM
568 plusNameEnv      = plusUFM
569 plusNameEnv_C    = plusUFM_C
570 extendNameEnvList= addListToUFM
571 delFromNameEnv   = delFromUFM
572 elemNameEnv      = elemUFM
573 mapNameEnv       = mapUFM
574 unitNameEnv      = unitUFM
575
576 lookupNameEnv          = lookupUFM
577 lookupNameEnv_NF env n = expectJust "lookupNameEnv_NF" (lookupUFM env n)
578 \end{code}
579
580
581 %************************************************************************
582 %*                                                                      *
583 \subsection{Pretty printing}
584 %*                                                                      *
585 %************************************************************************
586
587 \begin{code}
588 instance Outputable Name where
589         -- When printing interfaces, all Locals have been given nice print-names
590     ppr name = pprName name
591
592 pprName (Name {n_sort = Local, n_uniq = uniq, n_occ = occ, n_prov = prov})
593         -- Locals
594   = getPprStyle $ \ sty ->
595     if codeStyle sty then
596         pprUnique uniq          -- When printing in code we required all names to 
597                                 -- be globally unique; for example, we use this identifier
598                                 -- for the closure name.  So we just print the unique alone.
599     else
600         pprOccName occ <> pp_local_extra sty uniq
601   where
602     sys_local = case prov of
603                   SystemProv -> True
604                   other      -> False
605
606     pp_local_extra sty uniq
607         | sys_local      = underscore <> pprUnique uniq         -- Must print uniques for sys_locals
608         | debugStyle sty = text "{-" <> pprUnique uniq <> text "-}"
609         | otherwise      = empty
610
611
612 pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ, n_prov = prov})
613         -- Globals, and wired in things
614   = getPprStyle $ \ sty ->
615     if codeStyle sty then
616         ppr mod <> underscore <> ppr occ
617     else
618         pp_mod_dot sty <> ppr occ <> pp_global_debug sty uniq prov
619   where
620     mod = nameSortModule sort
621
622     pp_mod_dot sty
623       = case prov of
624            SystemProv                                -> pp_qual mod user_sty
625                 -- Hack alert!  Omit the qualifier on SystemProv things in user style
626                 -- I claim such SystemProv things will also be WiredIn things.
627                 -- We can't get the omit flag right
628                 -- on wired in tycons etc (sigh) so we just leave it out in user style, 
629                 -- and hope that leaving it out isn't too consfusing.
630                 -- (e.g. if the programmer hides Bool and  redefines it.  If so, use -dppr-debug.)
631
632            LocalDef _ _                              -> pp_qual mod (user_sty || iface_sty)
633
634            NonLocalDef (UserImport imp_mod _ _) omit 
635                 | user_sty                           -> pp_qual imp_mod omit
636                 | otherwise                          -> pp_qual mod     False
637            NonLocalDef ImplicitImport           omit -> pp_qual mod     (user_sty && omit)
638       where
639         user_sty  = userStyle sty
640         iface_sty = ifaceStyle sty
641     
642     pp_qual mod omit_qual
643         | omit_qual  = empty
644         | otherwise  = pprModule mod <> dot
645     
646     pp_global_debug sty uniq prov
647       | debugStyle sty = hcat [text "{-", pprUnique uniq, prov_p prov, text "-}"]
648       | otherwise      = empty
649
650     prov_p prov | opt_PprStyle_NoPrags = empty
651                 | otherwise            = comma <> pp_prov prov
652
653 pp_prov (LocalDef _ Exported)          = char 'x'
654 pp_prov (LocalDef _ NotExported)       = char 'l'
655 pp_prov (NonLocalDef ImplicitImport _) = char 'j'
656 pp_prov (NonLocalDef (UserImport _ _ True ) _) = char 'I'       -- Imported by name
657 pp_prov (NonLocalDef (UserImport _ _ False) _) = char 'i'       -- Imported by ..
658 pp_prov SystemProv                     = char 's'
659 \end{code}
660
661
662 %************************************************************************
663 %*                                                                      *
664 \subsection{Overloaded functions related to Names}
665 %*                                                                      *
666 %************************************************************************
667
668 \begin{code}
669 class NamedThing a where
670     getOccName :: a -> OccName
671     getName    :: a -> Name
672
673     getOccName n = nameOccName (getName n)      -- Default method
674 \end{code}
675
676 \begin{code}
677 getSrcLoc           :: NamedThing a => a -> SrcLoc
678 isLocallyDefined    :: NamedThing a => a -> Bool
679 getOccString        :: NamedThing a => a -> String
680 toRdrName           :: NamedThing a => a -> RdrName
681
682 getSrcLoc           = nameSrcLoc           . getName
683 isLocallyDefined    = isLocallyDefinedName . getName
684 getOccString x      = occNameString (getOccName x)
685 toRdrName           = ifaceNameRdrName     . getName
686 \end{code}
687
688 \begin{code}
689 {-# SPECIALIZE isLocallyDefined :: Name -> Bool #-}
690 \end{code}