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