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