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