Embrace -XTypeInType, add -XStarIsType
[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
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 #-} !Unique,
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 :: HasDebugCallStack => Name -> Module
196 nameSrcLoc :: Name -> SrcLoc
197 nameSrcSpan :: Name -> SrcSpan
198
199 nameUnique name = 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 GHCi 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 -- package 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 = 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 = 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 = 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 = 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 = 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 = 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 (mkTyVarOccFS 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 = 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 {-
416 ************************************************************************
417 * *
418 \subsection{Hashing and comparison}
419 * *
420 ************************************************************************
421 -}
422
423 cmpName :: Name -> Name -> Ordering
424 cmpName n1 n2 = n_uniq n1 `nonDetCmpUnique` n_uniq n2
425
426 -- | Compare Names lexicographically
427 -- This only works for Names that originate in the source code or have been
428 -- tidied.
429 stableNameCmp :: Name -> Name -> Ordering
430 stableNameCmp (Name { n_sort = s1, n_occ = occ1 })
431 (Name { n_sort = s2, n_occ = occ2 })
432 = (s1 `sort_cmp` s2) `thenCmp` (occ1 `compare` occ2)
433 -- The ordinary compare on OccNames is lexicographic
434 where
435 -- Later constructors are bigger
436 sort_cmp (External m1) (External m2) = m1 `stableModuleCmp` m2
437 sort_cmp (External {}) _ = LT
438 sort_cmp (WiredIn {}) (External {}) = GT
439 sort_cmp (WiredIn m1 _ _) (WiredIn m2 _ _) = m1 `stableModuleCmp` m2
440 sort_cmp (WiredIn {}) _ = LT
441 sort_cmp Internal (External {}) = GT
442 sort_cmp Internal (WiredIn {}) = GT
443 sort_cmp Internal Internal = EQ
444 sort_cmp Internal System = LT
445 sort_cmp System System = EQ
446 sort_cmp System _ = GT
447
448 {-
449 ************************************************************************
450 * *
451 \subsection[Name-instances]{Instance declarations}
452 * *
453 ************************************************************************
454 -}
455
456 -- | The same comments as for `Name`'s `Ord` instance apply.
457 instance Eq Name where
458 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
459 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
460
461 -- | __Caution__: This instance is implemented via `nonDetCmpUnique`, which
462 -- means that the ordering is not stable across deserialization or rebuilds.
463 --
464 -- See `nonDetCmpUnique` for further information, and trac #15240 for a bug
465 -- caused by improper use of this instance.
466
467 -- For a deterministic lexicographic ordering, use `stableNameCmp`.
468 instance Ord Name where
469 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
470 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
471 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
472 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
473 compare a b = cmpName a b
474
475 instance Uniquable Name where
476 getUnique = nameUnique
477
478 instance NamedThing Name where
479 getName n = n
480
481 instance Data Name where
482 -- don't traverse?
483 toConstr _ = abstractConstr "Name"
484 gunfold _ _ = error "gunfold"
485 dataTypeOf _ = mkNoRepType "Name"
486
487 {-
488 ************************************************************************
489 * *
490 \subsection{Binary}
491 * *
492 ************************************************************************
493 -}
494
495 -- | Assumes that the 'Name' is a non-binding one. See
496 -- 'IfaceSyn.putIfaceTopBndr' and 'IfaceSyn.getIfaceTopBndr' for serializing
497 -- binding 'Name's. See 'UserData' for the rationale for this distinction.
498 instance Binary Name where
499 put_ bh name =
500 case getUserData bh of
501 UserData{ ud_put_nonbinding_name = put_name } -> put_name bh name
502
503 get bh =
504 case getUserData bh of
505 UserData { ud_get_name = get_name } -> get_name bh
506
507 {-
508 ************************************************************************
509 * *
510 \subsection{Pretty printing}
511 * *
512 ************************************************************************
513 -}
514
515 instance Outputable Name where
516 ppr name = pprName name
517
518 instance OutputableBndr Name where
519 pprBndr _ name = pprName name
520 pprInfixOcc = pprInfixName
521 pprPrefixOcc = pprPrefixName
522
523 pprName :: Name -> SDoc
524 pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ})
525 = getPprStyle $ \ sty ->
526 case sort of
527 WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
528 External mod -> pprExternal sty uniq mod occ False UserSyntax
529 System -> pprSystem sty uniq occ
530 Internal -> pprInternal sty uniq occ
531
532 -- | Print the string of Name unqualifiedly directly.
533 pprNameUnqualified :: Name -> SDoc
534 pprNameUnqualified Name { n_occ = occ } = ppr_occ_name occ
535
536 pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
537 pprExternal sty uniq mod occ is_wired is_builtin
538 | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
539 -- In code style, always qualify
540 -- ToDo: maybe we could print all wired-in things unqualified
541 -- in code style, to reduce symbol table bloat?
542 | debugStyle sty = pp_mod <> ppr_occ_name occ
543 <> braces (hsep [if is_wired then text "(w)" else empty,
544 pprNameSpaceBrief (occNameSpace occ),
545 pprUnique uniq])
546 | BuiltInSyntax <- is_builtin = ppr_occ_name occ -- Never qualify builtin syntax
547 | otherwise =
548 if isHoleModule mod
549 then case qualName sty mod occ of
550 NameUnqual -> ppr_occ_name occ
551 _ -> braces (ppr (moduleName mod) <> dot <> ppr_occ_name occ)
552 else pprModulePrefix sty mod occ <> ppr_occ_name occ
553 where
554 pp_mod = sdocWithDynFlags $ \dflags ->
555 if gopt Opt_SuppressModulePrefixes dflags
556 then empty
557 else ppr mod <> dot
558
559 pprInternal :: PprStyle -> Unique -> OccName -> SDoc
560 pprInternal sty uniq occ
561 | codeStyle sty = pprUniqueAlways uniq
562 | debugStyle sty = ppr_occ_name occ <> braces (hsep [pprNameSpaceBrief (occNameSpace occ),
563 pprUnique uniq])
564 | dumpStyle sty = ppr_occ_name occ <> ppr_underscore_unique uniq
565 -- For debug dumps, we're not necessarily dumping
566 -- tidied code, so we need to print the uniques.
567 | otherwise = ppr_occ_name occ -- User style
568
569 -- Like Internal, except that we only omit the unique in Iface style
570 pprSystem :: PprStyle -> Unique -> OccName -> SDoc
571 pprSystem sty uniq occ
572 | codeStyle sty = pprUniqueAlways uniq
573 | debugStyle sty = ppr_occ_name occ <> ppr_underscore_unique uniq
574 <> braces (pprNameSpaceBrief (occNameSpace occ))
575 | otherwise = ppr_occ_name occ <> ppr_underscore_unique uniq
576 -- If the tidy phase hasn't run, the OccName
577 -- is unlikely to be informative (like 's'),
578 -- so print the unique
579
580
581 pprModulePrefix :: PprStyle -> Module -> OccName -> SDoc
582 -- Print the "M." part of a name, based on whether it's in scope or not
583 -- See Note [Printing original names] in HscTypes
584 pprModulePrefix sty mod occ = sdocWithDynFlags $ \dflags ->
585 if gopt Opt_SuppressModulePrefixes dflags
586 then empty
587 else
588 case qualName sty mod occ of -- See Outputable.QualifyName:
589 NameQual modname -> ppr modname <> dot -- Name is in scope
590 NameNotInScope1 -> ppr mod <> dot -- Not in scope
591 NameNotInScope2 -> ppr (moduleUnitId mod) <> colon -- Module not in
592 <> ppr (moduleName mod) <> dot -- scope either
593 NameUnqual -> empty -- In scope unqualified
594
595 pprUnique :: Unique -> SDoc
596 -- Print a unique unless we are suppressing them
597 pprUnique uniq
598 = sdocWithDynFlags $ \dflags ->
599 ppUnless (gopt Opt_SuppressUniques dflags) $
600 pprUniqueAlways uniq
601
602 ppr_underscore_unique :: Unique -> SDoc
603 -- Print an underscore separating the name from its unique
604 -- But suppress it if we aren't printing the uniques anyway
605 ppr_underscore_unique uniq
606 = sdocWithDynFlags $ \dflags ->
607 ppUnless (gopt Opt_SuppressUniques dflags) $
608 char '_' <> pprUniqueAlways uniq
609
610 ppr_occ_name :: OccName -> SDoc
611 ppr_occ_name occ = ftext (occNameFS occ)
612 -- Don't use pprOccName; instead, just print the string of the OccName;
613 -- we print the namespace in the debug stuff above
614
615 -- In code style, we Z-encode the strings. The results of Z-encoding each FastString are
616 -- cached behind the scenes in the FastString implementation.
617 ppr_z_occ_name :: OccName -> SDoc
618 ppr_z_occ_name occ = ztext (zEncodeFS (occNameFS occ))
619
620 -- Prints (if mod information is available) "Defined at <loc>" or
621 -- "Defined in <mod>" information for a Name.
622 pprDefinedAt :: Name -> SDoc
623 pprDefinedAt name = text "Defined" <+> pprNameDefnLoc name
624
625 pprNameDefnLoc :: Name -> SDoc
626 -- Prints "at <loc>" or
627 -- or "in <mod>" depending on what info is available
628 pprNameDefnLoc name
629 = case nameSrcLoc name of
630 -- nameSrcLoc rather than nameSrcSpan
631 -- It seems less cluttered to show a location
632 -- rather than a span for the definition point
633 RealSrcLoc s -> text "at" <+> ppr s
634 UnhelpfulLoc s
635 | isInternalName name || isSystemName name
636 -> text "at" <+> ftext s
637 | otherwise
638 -> text "in" <+> quotes (ppr (nameModule name))
639
640
641 -- | Get a string representation of a 'Name' that's unique and stable
642 -- across recompilations. Used for deterministic generation of binds for
643 -- derived instances.
644 -- eg. "$aeson_70dylHtv1FFGeai1IoxcQr$Data.Aeson.Types.Internal$String"
645 nameStableString :: Name -> String
646 nameStableString Name{..} =
647 nameSortStableString n_sort ++ "$" ++ occNameString n_occ
648
649 nameSortStableString :: NameSort -> String
650 nameSortStableString System = "$_sys"
651 nameSortStableString Internal = "$_in"
652 nameSortStableString (External mod) = moduleStableString mod
653 nameSortStableString (WiredIn mod _ _) = moduleStableString mod
654
655 {-
656 ************************************************************************
657 * *
658 \subsection{Overloaded functions related to Names}
659 * *
660 ************************************************************************
661 -}
662
663 -- | A class allowing convenient access to the 'Name' of various datatypes
664 class NamedThing a where
665 getOccName :: a -> OccName
666 getName :: a -> Name
667
668 getOccName n = nameOccName (getName n) -- Default method
669
670 instance NamedThing e => NamedThing (GenLocated l e) where
671 getName = getName . unLoc
672
673 getSrcLoc :: NamedThing a => a -> SrcLoc
674 getSrcSpan :: NamedThing a => a -> SrcSpan
675 getOccString :: NamedThing a => a -> String
676 getOccFS :: NamedThing a => a -> FastString
677
678 getSrcLoc = nameSrcLoc . getName
679 getSrcSpan = nameSrcSpan . getName
680 getOccString = occNameString . getOccName
681 getOccFS = occNameFS . getOccName
682
683 pprInfixName :: (Outputable a, NamedThing a) => a -> SDoc
684 -- See Outputable.pprPrefixVar, pprInfixVar;
685 -- add parens or back-quotes as appropriate
686 pprInfixName n = pprInfixVar (isSymOcc (getOccName n)) (ppr n)
687
688 pprPrefixName :: NamedThing a => a -> SDoc
689 pprPrefixName thing = pprPrefixVar (isSymOcc (nameOccName name)) (ppr name)
690 where
691 name = getName thing