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