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