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