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