Honour -dsuppress-uniques more thoroughly
[ghc.git] / compiler / basicTypes / Name.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section[Name]{@Name@: to transmit name info from renamer to typechecker}
6 -}
7
8 {-# LANGUAGE RecordWildCards #-}
9
10 -- |
11 -- #name_types#
12 -- GHC uses several kinds of name internally:
13 --
14 -- * 'OccName.OccName': see "OccName#name_types"
15 --
16 -- * 'RdrName.RdrName': see "RdrName#name_types"
17 --
18 -- * 'Name.Name' is the type of names that have had their scoping and binding resolved. They
19 -- have an 'OccName.OccName' but also a 'Unique.Unique' that disambiguates Names that have
20 -- the same 'OccName.OccName' and indeed is used for all 'Name.Name' comparison. Names
21 -- also contain information about where they originated from, see "Name#name_sorts"
22 --
23 -- * 'Id.Id': see "Id#name_types"
24 --
25 -- * 'Var.Var': see "Var#name_types"
26 --
27 -- #name_sorts#
28 -- Names are one of:
29 --
30 -- * External, if they name things declared in other modules. Some external
31 -- Names are wired in, i.e. they name primitives defined in the compiler itself
32 --
33 -- * Internal, if they name things in the module being compiled. Some internal
34 -- Names are system names, if they are names manufactured by the compiler
35
36 module Name (
37 -- * The main types
38 Name, -- Abstract
39 BuiltInSyntax(..),
40
41 -- ** Creating 'Name's
42 mkSystemName, mkSystemNameAt,
43 mkInternalName, mkClonedInternalName, mkDerivedInternalName,
44 mkSystemVarName, mkSysTvName,
45 mkFCallName,
46 mkExternalName, mkWiredInName,
47
48 -- ** Manipulating and deconstructing 'Name's
49 nameUnique, setNameUnique,
50 nameOccName, nameModule, nameModule_maybe,
51 setNameLoc,
52 tidyNameOcc,
53 localiseName,
54 mkLocalisedOccName,
55
56 nameSrcLoc, nameSrcSpan, pprNameDefnLoc, pprDefinedAt,
57
58 -- ** Predicates on 'Name's
59 isSystemName, isInternalName, isExternalName,
60 isTyVarName, isTyConName, isDataConName,
61 isValName, isVarName,
62 isWiredInName, isBuiltInSyntax,
63 isHoleName,
64 wiredInNameTyThing_maybe,
65 nameIsLocalOrFrom, nameIsHomePackage,
66 nameIsHomePackageImport, nameIsFromExternalPackage,
67 stableNameCmp,
68
69 -- * Class 'NamedThing' and overloaded friends
70 NamedThing(..),
71 getSrcLoc, getSrcSpan, getOccString, getOccFS,
72
73 pprInfixName, pprPrefixName, pprModulePrefix,
74 nameStableString,
75
76 -- Re-export the OccName stuff
77 module OccName
78 ) where
79
80 import {-# SOURCE #-} TyCoRep( TyThing )
81 import {-# SOURCE #-} PrelNames( starKindTyConKey, unicodeStarKindTyConKey )
82
83 import OccName
84 import Module
85 import SrcLoc
86 import Unique
87 import Util
88 import Maybes
89 import Binary
90 import DynFlags
91 import FastString
92 import Outputable
93
94 import Control.DeepSeq
95 import Data.Data
96
97 {-
98 ************************************************************************
99 * *
100 \subsection[Name-datatype]{The @Name@ datatype, and name construction}
101 * *
102 ************************************************************************
103 -}
104
105 -- | A unique, unambiguous name for something, containing information about where
106 -- that thing originated.
107 data Name = Name {
108 n_sort :: NameSort, -- What sort of name it is
109 n_occ :: !OccName, -- Its occurrence name
110 n_uniq :: {-# UNPACK #-} !Int,
111 n_loc :: !SrcSpan -- Definition site
112 }
113
114 -- NOTE: we make the n_loc field strict to eliminate some potential
115 -- (and real!) space leaks, due to the fact that we don't look at
116 -- the SrcLoc in a Name all that often.
117
118 data NameSort
119 = External Module
120
121 | WiredIn Module TyThing BuiltInSyntax
122 -- A variant of External, for wired-in things
123
124 | Internal -- A user-defined Id or TyVar
125 -- defined in the module being compiled
126
127 | System -- A system-defined Id or TyVar. Typically the
128 -- OccName is very uninformative (like 's')
129
130 instance Outputable NameSort where
131 ppr (External _) = text "external"
132 ppr (WiredIn _ _ _) = text "wired-in"
133 ppr Internal = text "internal"
134 ppr System = text "system"
135
136 instance NFData Name where
137 rnf Name{..} = rnf n_sort
138
139 instance NFData NameSort where
140 rnf (External m) = rnf m
141 rnf (WiredIn m t b) = rnf m `seq` t `seq` b `seq` ()
142 -- XXX this is a *lie*, we're not going to rnf the TyThing, but
143 -- since the TyThings for WiredIn Names are all static they can't
144 -- be hiding space leaks or errors.
145 rnf Internal = ()
146 rnf System = ()
147
148 -- | BuiltInSyntax is for things like @(:)@, @[]@ and tuples,
149 -- which have special syntactic forms. They aren't in scope
150 -- as such.
151 data BuiltInSyntax = BuiltInSyntax | UserSyntax
152
153 {-
154 Notes about the NameSorts:
155
156 1. Initially, top-level Ids (including locally-defined ones) get External names,
157 and all other local Ids get Internal names
158
159 2. In any invocation of GHC, an External Name for "M.x" has one and only one
160 unique. This unique association is ensured via the Name Cache;
161 see Note [The Name Cache] in IfaceEnv.
162
163 3. Things with a External name are given C static labels, so they finally
164 appear in the .o file's symbol table. They appear in the symbol table
165 in the form M.n. If originally-local things have this property they
166 must be made @External@ first.
167
168 4. In the tidy-core phase, a External that is not visible to an importer
169 is changed to Internal, and a Internal that is visible is changed to External
170
171 5. A System Name differs in the following ways:
172 a) has unique attached when printing dumps
173 b) unifier eliminates sys tyvars in favour of user provs where possible
174
175 Before anything gets printed in interface files or output code, it's
176 fed through a 'tidy' processor, which zaps the OccNames to have
177 unique names; and converts all sys-locals to user locals
178 If any desugarer sys-locals have survived that far, they get changed to
179 "ds1", "ds2", etc.
180
181 Built-in syntax => It's a syntactic form, not "in scope" (e.g. [])
182
183 Wired-in thing => The thing (Id, TyCon) is fully known to the compiler,
184 not read from an interface file.
185 E.g. Bool, True, Int, Float, and many others
186
187 All built-in syntax is for wired-in things.
188 -}
189
190 instance HasOccName Name where
191 occName = nameOccName
192
193 nameUnique :: Name -> Unique
194 nameOccName :: Name -> OccName
195 nameModule :: Name -> Module
196 nameSrcLoc :: Name -> SrcLoc
197 nameSrcSpan :: Name -> SrcSpan
198
199 nameUnique name = mkUniqueGrimily (n_uniq name)
200 nameOccName name = n_occ name
201 nameSrcLoc name = srcSpanStart (n_loc name)
202 nameSrcSpan name = n_loc name
203
204 {-
205 ************************************************************************
206 * *
207 \subsection{Predicates on names}
208 * *
209 ************************************************************************
210 -}
211
212 isInternalName :: Name -> Bool
213 isExternalName :: Name -> Bool
214 isSystemName :: Name -> Bool
215 isWiredInName :: Name -> Bool
216
217 isWiredInName (Name {n_sort = WiredIn _ _ _}) = True
218 isWiredInName _ = False
219
220 wiredInNameTyThing_maybe :: Name -> Maybe TyThing
221 wiredInNameTyThing_maybe (Name {n_sort = WiredIn _ thing _}) = Just thing
222 wiredInNameTyThing_maybe _ = Nothing
223
224 isBuiltInSyntax :: Name -> Bool
225 isBuiltInSyntax (Name {n_sort = WiredIn _ _ BuiltInSyntax}) = True
226 isBuiltInSyntax _ = False
227
228 isExternalName (Name {n_sort = External _}) = True
229 isExternalName (Name {n_sort = WiredIn _ _ _}) = True
230 isExternalName _ = False
231
232 isInternalName name = not (isExternalName name)
233
234 isHoleName :: Name -> Bool
235 isHoleName = isHoleModule . nameModule
236
237 nameModule name =
238 nameModule_maybe name `orElse`
239 pprPanic "nameModule" (ppr (n_sort name) <+> ppr name)
240
241 nameModule_maybe :: Name -> Maybe Module
242 nameModule_maybe (Name { n_sort = External mod}) = Just mod
243 nameModule_maybe (Name { n_sort = WiredIn mod _ _}) = Just mod
244 nameModule_maybe _ = Nothing
245
246 nameIsLocalOrFrom :: Module -> Name -> Bool
247 -- ^ Returns True if the name is
248 -- (a) Internal
249 -- (b) External but from the specified module
250 -- (c) External but from the 'interactive' package
251 --
252 -- The key idea is that
253 -- False means: the entity is defined in some other module
254 -- you can find the details (type, fixity, instances)
255 -- in some interface file
256 -- those details will be stored in the EPT or HPT
257 --
258 -- True means: the entity is defined in this module or earlier in
259 -- the GHCi session
260 -- you can find details (type, fixity, instances) in the
261 -- TcGblEnv or TcLclEnv
262 --
263 -- The isInteractiveModule part is because successive interactions of a GCHi session
264 -- each give rise to a fresh module (Ghci1, Ghci2, etc), but they all come
265 -- from the magic 'interactive' package; and all the details are kept in the
266 -- TcLclEnv, TcGblEnv, NOT in the HPT or EPT.
267 -- See Note [The interactive package] in HscTypes
268
269 nameIsLocalOrFrom from name
270 | Just mod <- nameModule_maybe name = from == mod || isInteractiveModule mod
271 | otherwise = True
272
273 nameIsHomePackage :: Module -> Name -> Bool
274 -- True if the Name is defined in module of this package
275 nameIsHomePackage this_mod
276 = \nm -> case n_sort nm of
277 External nm_mod -> moduleUnitId nm_mod == this_pkg
278 WiredIn nm_mod _ _ -> moduleUnitId nm_mod == this_pkg
279 Internal -> True
280 System -> False
281 where
282 this_pkg = moduleUnitId this_mod
283
284 nameIsHomePackageImport :: Module -> Name -> Bool
285 -- True if the Name is defined in module of this package
286 -- /other than/ the this_mod
287 nameIsHomePackageImport this_mod
288 = \nm -> case nameModule_maybe nm of
289 Nothing -> False
290 Just nm_mod -> nm_mod /= this_mod
291 && moduleUnitId nm_mod == this_pkg
292 where
293 this_pkg = moduleUnitId this_mod
294
295 -- | Returns True if the Name comes from some other package: neither this
296 -- pacakge nor the interactive package.
297 nameIsFromExternalPackage :: UnitId -> Name -> Bool
298 nameIsFromExternalPackage this_pkg name
299 | Just mod <- nameModule_maybe name
300 , moduleUnitId mod /= this_pkg -- Not this package
301 , not (isInteractiveModule mod) -- Not the 'interactive' package
302 = True
303 | otherwise
304 = False
305
306 isTyVarName :: Name -> Bool
307 isTyVarName name = isTvOcc (nameOccName name)
308
309 isTyConName :: Name -> Bool
310 isTyConName name = isTcOcc (nameOccName name)
311
312 isDataConName :: Name -> Bool
313 isDataConName name = isDataOcc (nameOccName name)
314
315 isValName :: Name -> Bool
316 isValName name = isValOcc (nameOccName name)
317
318 isVarName :: Name -> Bool
319 isVarName = isVarOcc . nameOccName
320
321 isSystemName (Name {n_sort = System}) = True
322 isSystemName _ = False
323
324 {-
325 ************************************************************************
326 * *
327 \subsection{Making names}
328 * *
329 ************************************************************************
330 -}
331
332 -- | Create a name which is (for now at least) local to the current module and hence
333 -- does not need a 'Module' to disambiguate it from other 'Name's
334 mkInternalName :: Unique -> OccName -> SrcSpan -> Name
335 mkInternalName uniq occ loc = Name { n_uniq = getKey uniq
336 , n_sort = Internal
337 , n_occ = occ
338 , n_loc = loc }
339 -- NB: You might worry that after lots of huffing and
340 -- puffing we might end up with two local names with distinct
341 -- uniques, but the same OccName. Indeed we can, but that's ok
342 -- * the insides of the compiler don't care: they use the Unique
343 -- * when printing for -ddump-xxx you can switch on -dppr-debug to get the
344 -- uniques if you get confused
345 -- * for interface files we tidyCore first, which makes
346 -- the OccNames distinct when they need to be
347
348 mkClonedInternalName :: Unique -> Name -> Name
349 mkClonedInternalName uniq (Name { n_occ = occ, n_loc = loc })
350 = Name { n_uniq = getKey uniq, n_sort = Internal
351 , n_occ = occ, n_loc = loc }
352
353 mkDerivedInternalName :: (OccName -> OccName) -> Unique -> Name -> Name
354 mkDerivedInternalName derive_occ uniq (Name { n_occ = occ, n_loc = loc })
355 = Name { n_uniq = getKey uniq, n_sort = Internal
356 , n_occ = derive_occ occ, n_loc = loc }
357
358 -- | Create a name which definitely originates in the given module
359 mkExternalName :: Unique -> Module -> OccName -> SrcSpan -> Name
360 -- WATCH OUT! External Names should be in the Name Cache
361 -- (see Note [The Name Cache] in IfaceEnv), so don't just call mkExternalName
362 -- with some fresh unique without populating the Name Cache
363 mkExternalName uniq mod occ loc
364 = Name { n_uniq = getKey uniq, n_sort = External mod,
365 n_occ = occ, n_loc = loc }
366
367 -- | Create a name which is actually defined by the compiler itself
368 mkWiredInName :: Module -> OccName -> Unique -> TyThing -> BuiltInSyntax -> Name
369 mkWiredInName mod occ uniq thing built_in
370 = Name { n_uniq = getKey uniq,
371 n_sort = WiredIn mod thing built_in,
372 n_occ = occ, n_loc = wiredInSrcSpan }
373
374 -- | Create a name brought into being by the compiler
375 mkSystemName :: Unique -> OccName -> Name
376 mkSystemName uniq occ = mkSystemNameAt uniq occ noSrcSpan
377
378 mkSystemNameAt :: Unique -> OccName -> SrcSpan -> Name
379 mkSystemNameAt uniq occ loc = Name { n_uniq = getKey uniq, n_sort = System
380 , n_occ = occ, n_loc = loc }
381
382 mkSystemVarName :: Unique -> FastString -> Name
383 mkSystemVarName uniq fs = mkSystemName uniq (mkVarOccFS fs)
384
385 mkSysTvName :: Unique -> FastString -> Name
386 mkSysTvName uniq fs = mkSystemName uniq (mkOccNameFS tvName fs)
387
388 -- | Make a name for a foreign call
389 mkFCallName :: Unique -> String -> Name
390 mkFCallName uniq str = mkInternalName uniq (mkVarOcc str) noSrcSpan
391 -- The encoded string completely describes the ccall
392
393 -- When we renumber/rename things, we need to be
394 -- able to change a Name's Unique to match the cached
395 -- one in the thing it's the name of. If you know what I mean.
396 setNameUnique :: Name -> Unique -> Name
397 setNameUnique name uniq = name {n_uniq = getKey uniq}
398
399 -- This is used for hsigs: we want to use the name of the originally exported
400 -- entity, but edit the location to refer to the reexport site
401 setNameLoc :: Name -> SrcSpan -> Name
402 setNameLoc name loc = name {n_loc = loc}
403
404 tidyNameOcc :: Name -> OccName -> Name
405 -- We set the OccName of a Name when tidying
406 -- In doing so, we change System --> Internal, so that when we print
407 -- it we don't get the unique by default. It's tidy now!
408 tidyNameOcc name@(Name { n_sort = System }) occ = name { n_occ = occ, n_sort = Internal}
409 tidyNameOcc name occ = name { n_occ = occ }
410
411 -- | Make the 'Name' into an internal name, regardless of what it was to begin with
412 localiseName :: Name -> Name
413 localiseName n = n { n_sort = Internal }
414
415 -- |Create a localised variant of a name.
416 --
417 -- If the name is external, encode the original's module name to disambiguate.
418 -- SPJ says: this looks like a rather odd-looking function; but it seems to
419 -- be used only during vectorisation, so I'm not going to worry
420 mkLocalisedOccName :: Module -> (Maybe String -> OccName -> OccName) -> Name -> OccName
421 mkLocalisedOccName this_mod mk_occ name = mk_occ origin (nameOccName name)
422 where
423 origin
424 | nameIsLocalOrFrom this_mod name = Nothing
425 | otherwise = Just (moduleNameColons . moduleName . nameModule $ name)
426
427 {-
428 ************************************************************************
429 * *
430 \subsection{Hashing and comparison}
431 * *
432 ************************************************************************
433 -}
434
435 cmpName :: Name -> Name -> Ordering
436 cmpName n1 n2 = n_uniq n1 `compare` n_uniq n2
437
438 -- | Compare Names lexicographically
439 -- This only works for Names that originate in the source code or have been
440 -- tidied.
441 stableNameCmp :: Name -> Name -> Ordering
442 stableNameCmp (Name { n_sort = s1, n_occ = occ1 })
443 (Name { n_sort = s2, n_occ = occ2 })
444 = (s1 `sort_cmp` s2) `thenCmp` (occ1 `compare` occ2)
445 -- The ordinary compare on OccNames is lexicographic
446 where
447 -- Later constructors are bigger
448 sort_cmp (External m1) (External m2) = m1 `stableModuleCmp` m2
449 sort_cmp (External {}) _ = LT
450 sort_cmp (WiredIn {}) (External {}) = GT
451 sort_cmp (WiredIn m1 _ _) (WiredIn m2 _ _) = m1 `stableModuleCmp` m2
452 sort_cmp (WiredIn {}) _ = LT
453 sort_cmp Internal (External {}) = GT
454 sort_cmp Internal (WiredIn {}) = GT
455 sort_cmp Internal Internal = EQ
456 sort_cmp Internal System = LT
457 sort_cmp System System = EQ
458 sort_cmp System _ = GT
459
460 {-
461 ************************************************************************
462 * *
463 \subsection[Name-instances]{Instance declarations}
464 * *
465 ************************************************************************
466 -}
467
468 instance Eq Name where
469 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
470 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
471
472 instance Ord Name where
473 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
474 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
475 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
476 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
477 compare a b = cmpName a b
478
479 instance Uniquable Name where
480 getUnique = nameUnique
481
482 instance NamedThing Name where
483 getName n = n
484
485 instance Data Name where
486 -- don't traverse?
487 toConstr _ = abstractConstr "Name"
488 gunfold _ _ = error "gunfold"
489 dataTypeOf _ = mkNoRepType "Name"
490
491 {-
492 ************************************************************************
493 * *
494 \subsection{Binary}
495 * *
496 ************************************************************************
497 -}
498
499 -- | Assumes that the 'Name' is a non-binding one. See
500 -- 'IfaceSyn.putIfaceTopBndr' and 'IfaceSyn.getIfaceTopBndr' for serializing
501 -- binding 'Name's. See 'UserData' for the rationale for this distinction.
502 instance Binary Name where
503 put_ bh name =
504 case getUserData bh of
505 UserData{ ud_put_nonbinding_name = put_name } -> put_name bh name
506
507 get bh =
508 case getUserData bh of
509 UserData { ud_get_name = get_name } -> get_name bh
510
511 {-
512 ************************************************************************
513 * *
514 \subsection{Pretty printing}
515 * *
516 ************************************************************************
517 -}
518
519 instance Outputable Name where
520 ppr name = pprName name
521
522 instance OutputableBndr Name where
523 pprBndr _ name = pprName name
524 pprInfixOcc = pprInfixName
525 pprPrefixOcc = pprPrefixName
526
527
528 pprName :: Name -> SDoc
529 pprName (Name {n_sort = sort, n_uniq = u, n_occ = occ})
530 = getPprStyle $ \ sty ->
531 case sort of
532 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
533 External mod -> pprExternal sty uniq mod occ False UserSyntax
534 System -> pprSystem sty uniq occ
535 Internal -> pprInternal sty uniq occ
536 where uniq = mkUniqueGrimily u
537
538 pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
539 pprExternal sty uniq mod occ is_wired is_builtin
540 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
541 -- In code style, always qualify
542 -- ToDo: maybe we could print all wired-in things unqualified
543 -- in code style, to reduce symbol table bloat?
544 | debugStyle sty = pp_mod <> ppr_occ_name occ
545 <> braces (hsep [if is_wired then text "(w)" else empty,
546 pprNameSpaceBrief (occNameSpace occ),
547 pprUnique uniq])
548 | BuiltInSyntax <- is_builtin = ppr_occ_name occ -- Never qualify builtin syntax
549 | otherwise =
550 if isHoleModule mod
551 then case qualName sty mod occ of
552 NameUnqual -> ppr_occ_name occ
553 _ -> braces (ppr (moduleName mod) <> dot <> ppr_occ_name occ)
554 else pprModulePrefix sty mod occ <> ppr_occ_name occ
555 where
556 pp_mod = sdocWithDynFlags $ \dflags ->
557 if gopt Opt_SuppressModulePrefixes dflags
558 then empty
559 else ppr mod <> dot
560
561 pprInternal :: PprStyle -> Unique -> OccName -> SDoc
562 pprInternal sty uniq occ
563 | codeStyle sty = pprUniqueAlways uniq
564 | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ),
565 pprUnique uniq])
566 | dumpStyle sty = ppr_occ_name occ <> ppr_underscore_unique uniq
567 -- For debug dumps, we're not necessarily dumping
568 -- tidied code, so we need to print the uniques.
569 | otherwise = ppr_occ_name occ -- User style
570
571 -- Like Internal, except that we only omit the unique in Iface style
572 pprSystem :: PprStyle -> Unique -> OccName -> SDoc
573 pprSystem sty uniq occ
574 | codeStyle sty = pprUniqueAlways uniq
575 | debugStyle sty = ppr_occ_name occ <> ppr_underscore_unique uniq
576 <> braces (pprNameSpaceBrief (occNameSpace occ))
577 | otherwise = ppr_occ_name occ <> ppr_underscore_unique uniq
578 -- If the tidy phase hasn't run, the OccName
579 -- is unlikely to be informative (like 's'),
580 -- so print the unique
581
582
583 pprModulePrefix :: PprStyle -> Module -> OccName -> SDoc
584 -- Print the "M." part of a name, based on whether it's in scope or not
585 -- See Note [Printing original names] in HscTypes
586 pprModulePrefix sty mod occ = sdocWithDynFlags $ \dflags ->
587 if gopt Opt_SuppressModulePrefixes dflags
588 then empty
589 else
590 case qualName sty mod occ of -- See Outputable.QualifyName:
591 NameQual modname -> ppr modname <> dot -- Name is in scope
592 NameNotInScope1 -> ppr mod <> dot -- Not in scope
593 NameNotInScope2 -> ppr (moduleUnitId mod) <> colon -- Module not in
594 <> ppr (moduleName mod) <> dot -- scope either
595 NameUnqual -> empty -- In scope unqualified
596
597 pprUnique :: Unique -> SDoc
598 -- Print a unique unless we are suppressing them
599 pprUnique uniq
600 = sdocWithDynFlags $ \dflags ->
601 ppUnless (gopt Opt_SuppressUniques dflags) $
602 pprUniqueAlways uniq
603
604 ppr_underscore_unique :: Unique -> SDoc
605 -- Print an underscore separating the name from its unique
606 -- But suppress it if we aren't printing the uniques anyway
607 ppr_underscore_unique uniq
608 = sdocWithDynFlags $ \dflags ->
609 ppUnless (gopt Opt_SuppressUniques dflags) $
610 char '_' <> pprUniqueAlways uniq
611
612 ppr_occ_name :: OccName -> SDoc
613 ppr_occ_name occ = ftext (occNameFS occ)
614 -- Don't use pprOccName; instead, just print the string of the OccName;
615 -- we print the namespace in the debug stuff above
616
617 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
618 -- cached behind the scenes in the FastString implementation.
619 ppr_z_occ_name :: OccName -> SDoc
620 ppr_z_occ_name occ = ztext (zEncodeFS (occNameFS occ))
621
622 -- Prints (if mod information is available) "Defined at <loc>" or
623 -- "Defined in <mod>" information for a Name.
624 pprDefinedAt :: Name -> SDoc
625 pprDefinedAt name = text "Defined" <+> pprNameDefnLoc name
626
627 pprNameDefnLoc :: Name -> SDoc
628 -- Prints "at <loc>" or
629 -- or "in <mod>" depending on what info is available
630 pprNameDefnLoc name
631 = case nameSrcLoc name of
632 -- nameSrcLoc rather than nameSrcSpan
633 -- It seems less cluttered to show a location
634 -- rather than a span for the definition point
635 RealSrcLoc s -> text "at" <+> ppr s
636 UnhelpfulLoc s
637 | isInternalName name || isSystemName name
638 -> text "at" <+> ftext s
639 | otherwise
640 -> text "in" <+> quotes (ppr (nameModule name))
641
642
643 -- | Get a string representation of a 'Name' that's unique and stable
644 -- across recompilations. Used for deterministic generation of binds for
645 -- derived instances.
646 -- eg. "$aeson_70dylHtv1FFGeai1IoxcQr$Data.Aeson.Types.Internal$String"
647 nameStableString :: Name -> String
648 nameStableString Name{..} =
649 nameSortStableString n_sort ++ "$" ++ occNameString n_occ
650
651 nameSortStableString :: NameSort -> String
652 nameSortStableString System = "$_sys"
653 nameSortStableString Internal = "$_in"
654 nameSortStableString (External mod) = moduleStableString mod
655 nameSortStableString (WiredIn mod _ _) = moduleStableString mod
656
657 {-
658 ************************************************************************
659 * *
660 \subsection{Overloaded functions related to Names}
661 * *
662 ************************************************************************
663 -}
664
665 -- | A class allowing convenient access to the 'Name' of various datatypes
666 class NamedThing a where
667 getOccName :: a -> OccName
668 getName :: a -> Name
669
670 getOccName n = nameOccName (getName n) -- Default method
671
672 instance NamedThing e => NamedThing (GenLocated l e) where
673 getName = getName . unLoc
674
675 getSrcLoc :: NamedThing a => a -> SrcLoc
676 getSrcSpan :: NamedThing a => a -> SrcSpan
677 getOccString :: NamedThing a => a -> String
678 getOccFS :: NamedThing a => a -> FastString
679
680 getSrcLoc = nameSrcLoc . getName
681 getSrcSpan = nameSrcSpan . getName
682 getOccString = occNameString . getOccName
683 getOccFS = occNameFS . getOccName
684
685 pprInfixName :: (Outputable a, NamedThing a) => a -> SDoc
686 -- See Outputable.pprPrefixVar, pprInfixVar;
687 -- add parens or back-quotes as appropriate
688 pprInfixName n = pprInfixVar (isSymOcc (getOccName n)) (ppr n)
689
690 pprPrefixName :: NamedThing a => a -> SDoc
691 pprPrefixName thing
692 | name `hasKey` starKindTyConKey || name `hasKey` unicodeStarKindTyConKey
693 = ppr name -- See Note [Special treatment for kind *]
694 | otherwise
695 = pprPrefixVar (isSymOcc (nameOccName name)) (ppr name)
696 where
697 name = getName thing
698
699 {-
700 Note [Special treatment for kind *]
701 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
702 Do not put parens around the kind '*'. Even though it looks like
703 an operator, it is really a special case.
704
705 This pprPrefixName stuff is really only used when printing HsSyn,
706 which has to be polymorphic in the name type, and hence has to go via
707 the overloaded function pprPrefixOcc. It's easier where we know the
708 type being pretty printed; eg the pretty-printing code in TyCoRep.
709
710 See Trac #7645, which led to this.
711 -}