Remove unused tidyOccNames and update Note
[ghc.git] / compiler / basicTypes / OccName.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 -}
5
6 {-# LANGUAGE DeriveDataTypeable #-}
7 {-# LANGUAGE BangPatterns #-}
8 {-# LANGUAGE OverloadedStrings #-}
9
10 -- |
11 -- #name_types#
12 -- GHC uses several kinds of name internally:
13 --
14 -- * 'OccName.OccName' represents names as strings with just a little more information:
15 -- the \"namespace\" that the name came from, e.g. the namespace of value, type constructors or
16 -- data constructors
17 --
18 -- * 'RdrName.RdrName': see "RdrName#name_types"
19 --
20 -- * 'Name.Name': see "Name#name_types"
21 --
22 -- * 'Id.Id': see "Id#name_types"
23 --
24 -- * 'Var.Var': see "Var#name_types"
25
26 module OccName (
27 -- * The 'NameSpace' type
28 NameSpace, -- Abstract
29
30 nameSpacesRelated,
31
32 -- ** Construction
33 -- $real_vs_source_data_constructors
34 tcName, clsName, tcClsName, dataName, varName,
35 tvName, srcDataName,
36
37 -- ** Pretty Printing
38 pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
39
40 -- * The 'OccName' type
41 OccName, -- Abstract, instance of Outputable
42 pprOccName,
43
44 -- ** Construction
45 mkOccName, mkOccNameFS,
46 mkVarOcc, mkVarOccFS,
47 mkDataOcc, mkDataOccFS,
48 mkTyVarOcc, mkTyVarOccFS,
49 mkTcOcc, mkTcOccFS,
50 mkClsOcc, mkClsOccFS,
51 mkDFunOcc,
52 setOccNameSpace,
53 demoteOccName,
54 HasOccName(..),
55
56 -- ** Derived 'OccName's
57 isDerivedOccName,
58 mkDataConWrapperOcc, mkWorkerOcc,
59 mkMatcherOcc, mkBuilderOcc,
60 mkDefaultMethodOcc, isDefaultMethodOcc, isTypeableBindOcc,
61 mkNewTyCoOcc, mkClassOpAuxOcc,
62 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
63 mkClassDataConOcc, mkDictOcc, mkIPOcc,
64 mkSpecOcc, mkForeignExportOcc, mkRepEqOcc,
65 mkGenR, mkGen1R,
66 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
67 mkSuperDictSelOcc, mkSuperDictAuxOcc,
68 mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
69 mkInstTyCoOcc, mkEqPredCoOcc,
70 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
71 mkPDataTyConOcc, mkPDataDataConOcc,
72 mkPDatasTyConOcc, mkPDatasDataConOcc,
73 mkPReprTyConOcc,
74 mkPADFunOcc,
75 mkRecFldSelOcc,
76 mkTyConRepOcc,
77
78 -- ** Deconstruction
79 occNameFS, occNameString, occNameSpace,
80
81 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
82 parenSymOcc, startsWithUnderscore,
83
84 isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
85
86 -- * The 'OccEnv' type
87 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
88 lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
89 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
90 extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
91 alterOccEnv, pprOccEnv,
92
93 -- * The 'OccSet' type
94 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
95 extendOccSetList,
96 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet,
97 isEmptyOccSet, intersectOccSet, intersectsOccSet,
98 filterOccSet,
99
100 -- * Tidying up
101 TidyOccEnv, emptyTidyOccEnv, initTidyOccEnv,
102 tidyOccName, avoidClashesOccEnv,
103
104 -- FsEnv
105 FastStringEnv, emptyFsEnv, lookupFsEnv, extendFsEnv, mkFsEnv
106 ) where
107
108 import Util
109 import Unique
110 import DynFlags
111 import UniqFM
112 import UniqSet
113 import FastString
114 import FastStringEnv
115 import Outputable
116 import Lexeme
117 import Binary
118 import Control.DeepSeq
119 import Data.Char
120 import Data.Data
121
122 {-
123 ************************************************************************
124 * *
125 \subsection{Name space}
126 * *
127 ************************************************************************
128 -}
129
130 data NameSpace = VarName -- Variables, including "real" data constructors
131 | DataName -- "Source" data constructors
132 | TvName -- Type variables
133 | TcClsName -- Type constructors and classes; Haskell has them
134 -- in the same name space for now.
135 deriving( Eq, Ord )
136
137 -- Note [Data Constructors]
138 -- see also: Note [Data Constructor Naming] in DataCon.hs
139 --
140 -- $real_vs_source_data_constructors
141 -- There are two forms of data constructor:
142 --
143 -- [Source data constructors] The data constructors mentioned in Haskell source code
144 --
145 -- [Real data constructors] The data constructors of the representation type, which may not be the same as the source type
146 --
147 -- For example:
148 --
149 -- > data T = T !(Int, Int)
150 --
151 -- The source datacon has type @(Int, Int) -> T@
152 -- The real datacon has type @Int -> Int -> T@
153 --
154 -- GHC chooses a representation based on the strictness etc.
155
156 tcName, clsName, tcClsName :: NameSpace
157 dataName, srcDataName :: NameSpace
158 tvName, varName :: NameSpace
159
160 -- Though type constructors and classes are in the same name space now,
161 -- the NameSpace type is abstract, so we can easily separate them later
162 tcName = TcClsName -- Type constructors
163 clsName = TcClsName -- Classes
164 tcClsName = TcClsName -- Not sure which!
165
166 dataName = DataName
167 srcDataName = DataName -- Haskell-source data constructors should be
168 -- in the Data name space
169
170 tvName = TvName
171 varName = VarName
172
173 isDataConNameSpace :: NameSpace -> Bool
174 isDataConNameSpace DataName = True
175 isDataConNameSpace _ = False
176
177 isTcClsNameSpace :: NameSpace -> Bool
178 isTcClsNameSpace TcClsName = True
179 isTcClsNameSpace _ = False
180
181 isTvNameSpace :: NameSpace -> Bool
182 isTvNameSpace TvName = True
183 isTvNameSpace _ = False
184
185 isVarNameSpace :: NameSpace -> Bool -- Variables or type variables, but not constructors
186 isVarNameSpace TvName = True
187 isVarNameSpace VarName = True
188 isVarNameSpace _ = False
189
190 isValNameSpace :: NameSpace -> Bool
191 isValNameSpace DataName = True
192 isValNameSpace VarName = True
193 isValNameSpace _ = False
194
195 pprNameSpace :: NameSpace -> SDoc
196 pprNameSpace DataName = text "data constructor"
197 pprNameSpace VarName = text "variable"
198 pprNameSpace TvName = text "type variable"
199 pprNameSpace TcClsName = text "type constructor or class"
200
201 pprNonVarNameSpace :: NameSpace -> SDoc
202 pprNonVarNameSpace VarName = empty
203 pprNonVarNameSpace ns = pprNameSpace ns
204
205 pprNameSpaceBrief :: NameSpace -> SDoc
206 pprNameSpaceBrief DataName = char 'd'
207 pprNameSpaceBrief VarName = char 'v'
208 pprNameSpaceBrief TvName = text "tv"
209 pprNameSpaceBrief TcClsName = text "tc"
210
211 -- demoteNameSpace lowers the NameSpace if possible. We can not know
212 -- in advance, since a TvName can appear in an HsTyVar.
213 -- See Note [Demotion] in RnEnv
214 demoteNameSpace :: NameSpace -> Maybe NameSpace
215 demoteNameSpace VarName = Nothing
216 demoteNameSpace DataName = Nothing
217 demoteNameSpace TvName = Nothing
218 demoteNameSpace TcClsName = Just DataName
219
220 {-
221 ************************************************************************
222 * *
223 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
224 * *
225 ************************************************************************
226 -}
227
228 -- | Occurrence Name
229 --
230 -- In this context that means:
231 -- "classified (i.e. as a type name, value name, etc) but not qualified
232 -- and not yet resolved"
233 data OccName = OccName
234 { occNameSpace :: !NameSpace
235 , occNameFS :: !FastString
236 }
237
238 instance Eq OccName where
239 (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
240
241 instance Ord OccName where
242 -- Compares lexicographically, *not* by Unique of the string
243 compare (OccName sp1 s1) (OccName sp2 s2)
244 = (s1 `compare` s2) `thenCmp` (sp1 `compare` sp2)
245
246 instance Data OccName where
247 -- don't traverse?
248 toConstr _ = abstractConstr "OccName"
249 gunfold _ _ = error "gunfold"
250 dataTypeOf _ = mkNoRepType "OccName"
251
252 instance HasOccName OccName where
253 occName = id
254
255 instance NFData OccName where
256 rnf x = x `seq` ()
257
258 {-
259 ************************************************************************
260 * *
261 \subsection{Printing}
262 * *
263 ************************************************************************
264 -}
265
266 instance Outputable OccName where
267 ppr = pprOccName
268
269 instance OutputableBndr OccName where
270 pprBndr _ = ppr
271 pprInfixOcc n = pprInfixVar (isSymOcc n) (ppr n)
272 pprPrefixOcc n = pprPrefixVar (isSymOcc n) (ppr n)
273
274 pprOccName :: OccName -> SDoc
275 pprOccName (OccName sp occ)
276 = getPprStyle $ \ sty ->
277 if codeStyle sty
278 then ztext (zEncodeFS occ)
279 else pp_occ <> pp_debug sty
280 where
281 pp_debug sty | debugStyle sty = braces (pprNameSpaceBrief sp)
282 | otherwise = empty
283
284 pp_occ = sdocWithDynFlags $ \dflags ->
285 if gopt Opt_SuppressUniques dflags
286 then text (strip_th_unique (unpackFS occ))
287 else ftext occ
288
289 -- See Note [Suppressing uniques in OccNames]
290 strip_th_unique ('[' : c : _) | isAlphaNum c = []
291 strip_th_unique (c : cs) = c : strip_th_unique cs
292 strip_th_unique [] = []
293
294 {-
295 Note [Suppressing uniques in OccNames]
296 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
297 This is a hack to de-wobblify the OccNames that contain uniques from
298 Template Haskell that have been turned into a string in the OccName.
299 See Note [Unique OccNames from Template Haskell] in Convert.hs
300
301 ************************************************************************
302 * *
303 \subsection{Construction}
304 * *
305 ************************************************************************
306 -}
307
308 mkOccName :: NameSpace -> String -> OccName
309 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
310
311 mkOccNameFS :: NameSpace -> FastString -> OccName
312 mkOccNameFS occ_sp fs = OccName occ_sp fs
313
314 mkVarOcc :: String -> OccName
315 mkVarOcc s = mkOccName varName s
316
317 mkVarOccFS :: FastString -> OccName
318 mkVarOccFS fs = mkOccNameFS varName fs
319
320 mkDataOcc :: String -> OccName
321 mkDataOcc = mkOccName dataName
322
323 mkDataOccFS :: FastString -> OccName
324 mkDataOccFS = mkOccNameFS dataName
325
326 mkTyVarOcc :: String -> OccName
327 mkTyVarOcc = mkOccName tvName
328
329 mkTyVarOccFS :: FastString -> OccName
330 mkTyVarOccFS fs = mkOccNameFS tvName fs
331
332 mkTcOcc :: String -> OccName
333 mkTcOcc = mkOccName tcName
334
335 mkTcOccFS :: FastString -> OccName
336 mkTcOccFS = mkOccNameFS tcName
337
338 mkClsOcc :: String -> OccName
339 mkClsOcc = mkOccName clsName
340
341 mkClsOccFS :: FastString -> OccName
342 mkClsOccFS = mkOccNameFS clsName
343
344 -- demoteOccName lowers the Namespace of OccName.
345 -- see Note [Demotion]
346 demoteOccName :: OccName -> Maybe OccName
347 demoteOccName (OccName space name) = do
348 space' <- demoteNameSpace space
349 return $ OccName space' name
350
351 -- Name spaces are related if there is a chance to mean the one when one writes
352 -- the other, i.e. variables <-> data constructors and type variables <-> type constructors
353 nameSpacesRelated :: NameSpace -> NameSpace -> Bool
354 nameSpacesRelated ns1 ns2 = ns1 == ns2 || otherNameSpace ns1 == ns2
355
356 otherNameSpace :: NameSpace -> NameSpace
357 otherNameSpace VarName = DataName
358 otherNameSpace DataName = VarName
359 otherNameSpace TvName = TcClsName
360 otherNameSpace TcClsName = TvName
361
362
363
364 {- | Other names in the compiler add additional information to an OccName.
365 This class provides a consistent way to access the underlying OccName. -}
366 class HasOccName name where
367 occName :: name -> OccName
368
369 {-
370 ************************************************************************
371 * *
372 Environments
373 * *
374 ************************************************************************
375
376 OccEnvs are used mainly for the envts in ModIfaces.
377
378 Note [The Unique of an OccName]
379 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
380 They are efficient, because FastStrings have unique Int# keys. We assume
381 this key is less than 2^24, and indeed FastStrings are allocated keys
382 sequentially starting at 0.
383
384 So we can make a Unique using
385 mkUnique ns key :: Unique
386 where 'ns' is a Char representing the name space. This in turn makes it
387 easy to build an OccEnv.
388 -}
389
390 instance Uniquable OccName where
391 -- See Note [The Unique of an OccName]
392 getUnique (OccName VarName fs) = mkVarOccUnique fs
393 getUnique (OccName DataName fs) = mkDataOccUnique fs
394 getUnique (OccName TvName fs) = mkTvOccUnique fs
395 getUnique (OccName TcClsName fs) = mkTcOccUnique fs
396
397 newtype OccEnv a = A (UniqFM a)
398 deriving Data
399
400 emptyOccEnv :: OccEnv a
401 unitOccEnv :: OccName -> a -> OccEnv a
402 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
403 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
404 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
405 mkOccEnv :: [(OccName,a)] -> OccEnv a
406 mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
407 elemOccEnv :: OccName -> OccEnv a -> Bool
408 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
409 occEnvElts :: OccEnv a -> [a]
410 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
411 extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
412 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
413 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
414 mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
415 delFromOccEnv :: OccEnv a -> OccName -> OccEnv a
416 delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
417 filterOccEnv :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
418 alterOccEnv :: (Maybe elt -> Maybe elt) -> OccEnv elt -> OccName -> OccEnv elt
419
420 emptyOccEnv = A emptyUFM
421 unitOccEnv x y = A $ unitUFM x y
422 extendOccEnv (A x) y z = A $ addToUFM x y z
423 extendOccEnvList (A x) l = A $ addListToUFM x l
424 lookupOccEnv (A x) y = lookupUFM x y
425 mkOccEnv l = A $ listToUFM l
426 elemOccEnv x (A y) = elemUFM x y
427 foldOccEnv a b (A c) = foldUFM a b c
428 occEnvElts (A x) = eltsUFM x
429 plusOccEnv (A x) (A y) = A $ plusUFM x y
430 plusOccEnv_C f (A x) (A y) = A $ plusUFM_C f x y
431 extendOccEnv_C f (A x) y z = A $ addToUFM_C f x y z
432 extendOccEnv_Acc f g (A x) y z = A $ addToUFM_Acc f g x y z
433 mapOccEnv f (A x) = A $ mapUFM f x
434 mkOccEnv_C comb l = A $ addListToUFM_C comb emptyUFM l
435 delFromOccEnv (A x) y = A $ delFromUFM x y
436 delListFromOccEnv (A x) y = A $ delListFromUFM x y
437 filterOccEnv x (A y) = A $ filterUFM x y
438 alterOccEnv fn (A y) k = A $ alterUFM fn y k
439
440 instance Outputable a => Outputable (OccEnv a) where
441 ppr x = pprOccEnv ppr x
442
443 pprOccEnv :: (a -> SDoc) -> OccEnv a -> SDoc
444 pprOccEnv ppr_elt (A env) = pprUniqFM ppr_elt env
445
446 type OccSet = UniqSet OccName
447
448 emptyOccSet :: OccSet
449 unitOccSet :: OccName -> OccSet
450 mkOccSet :: [OccName] -> OccSet
451 extendOccSet :: OccSet -> OccName -> OccSet
452 extendOccSetList :: OccSet -> [OccName] -> OccSet
453 unionOccSets :: OccSet -> OccSet -> OccSet
454 unionManyOccSets :: [OccSet] -> OccSet
455 minusOccSet :: OccSet -> OccSet -> OccSet
456 elemOccSet :: OccName -> OccSet -> Bool
457 isEmptyOccSet :: OccSet -> Bool
458 intersectOccSet :: OccSet -> OccSet -> OccSet
459 intersectsOccSet :: OccSet -> OccSet -> Bool
460 filterOccSet :: (OccName -> Bool) -> OccSet -> OccSet
461
462 emptyOccSet = emptyUniqSet
463 unitOccSet = unitUniqSet
464 mkOccSet = mkUniqSet
465 extendOccSet = addOneToUniqSet
466 extendOccSetList = addListToUniqSet
467 unionOccSets = unionUniqSets
468 unionManyOccSets = unionManyUniqSets
469 minusOccSet = minusUniqSet
470 elemOccSet = elementOfUniqSet
471 isEmptyOccSet = isEmptyUniqSet
472 intersectOccSet = intersectUniqSets
473 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
474 filterOccSet = filterUniqSet
475
476 {-
477 ************************************************************************
478 * *
479 \subsection{Predicates and taking them apart}
480 * *
481 ************************************************************************
482 -}
483
484 occNameString :: OccName -> String
485 occNameString (OccName _ s) = unpackFS s
486
487 setOccNameSpace :: NameSpace -> OccName -> OccName
488 setOccNameSpace sp (OccName _ occ) = OccName sp occ
489
490 isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
491
492 isVarOcc (OccName VarName _) = True
493 isVarOcc _ = False
494
495 isTvOcc (OccName TvName _) = True
496 isTvOcc _ = False
497
498 isTcOcc (OccName TcClsName _) = True
499 isTcOcc _ = False
500
501 -- | /Value/ 'OccNames's are those that are either in
502 -- the variable or data constructor namespaces
503 isValOcc :: OccName -> Bool
504 isValOcc (OccName VarName _) = True
505 isValOcc (OccName DataName _) = True
506 isValOcc _ = False
507
508 isDataOcc (OccName DataName _) = True
509 isDataOcc _ = False
510
511 -- | Test if the 'OccName' is a data constructor that starts with
512 -- a symbol (e.g. @:@, or @[]@)
513 isDataSymOcc :: OccName -> Bool
514 isDataSymOcc (OccName DataName s) = isLexConSym s
515 isDataSymOcc _ = False
516 -- Pretty inefficient!
517
518 -- | Test if the 'OccName' is that for any operator (whether
519 -- it is a data constructor or variable or whatever)
520 isSymOcc :: OccName -> Bool
521 isSymOcc (OccName DataName s) = isLexConSym s
522 isSymOcc (OccName TcClsName s) = isLexSym s
523 isSymOcc (OccName VarName s) = isLexSym s
524 isSymOcc (OccName TvName s) = isLexSym s
525 -- Pretty inefficient!
526
527 parenSymOcc :: OccName -> SDoc -> SDoc
528 -- ^ Wrap parens around an operator
529 parenSymOcc occ doc | isSymOcc occ = parens doc
530 | otherwise = doc
531
532 startsWithUnderscore :: OccName -> Bool
533 -- ^ Haskell 98 encourages compilers to suppress warnings about unsed
534 -- names in a pattern if they start with @_@: this implements that test
535 startsWithUnderscore occ = headFS (occNameFS occ) == '_'
536
537 {-
538 ************************************************************************
539 * *
540 \subsection{Making system names}
541 * *
542 ************************************************************************
543
544 Here's our convention for splitting up the interface file name space:
545
546 d... dictionary identifiers
547 (local variables, so no name-clash worries)
548
549 All of these other OccNames contain a mixture of alphabetic
550 and symbolic characters, and hence cannot possibly clash with
551 a user-written type or function name
552
553 $f... Dict-fun identifiers (from inst decls)
554 $dmop Default method for 'op'
555 $pnC n'th superclass selector for class C
556 $wf Worker for function 'f'
557 $sf.. Specialised version of f
558 D:C Data constructor for dictionary for class C
559 NTCo:T Coercion connecting newtype T with its representation type
560 TFCo:R Coercion connecting a data family to its representation type R
561
562 In encoded form these appear as Zdfxxx etc
563
564 :... keywords (export:, letrec: etc.)
565 --- I THINK THIS IS WRONG!
566
567 This knowledge is encoded in the following functions.
568
569 @mk_deriv@ generates an @OccName@ from the prefix and a string.
570 NB: The string must already be encoded!
571 -}
572
573 -- | Build an 'OccName' derived from another 'OccName'.
574 --
575 -- Note that the pieces of the name are passed in as a @[FastString]@ so that
576 -- the whole name can be constructed with a single 'concatFS', minimizing
577 -- unnecessary intermediate allocations.
578 mk_deriv :: NameSpace
579 -> FastString -- ^ A prefix which distinguishes one sort of
580 -- derived name from another
581 -> [FastString] -- ^ The name we are deriving from in pieces which
582 -- will be concatenated.
583 -> OccName
584 mk_deriv occ_sp sys_prefix str =
585 mkOccNameFS occ_sp (concatFS $ sys_prefix : str)
586
587 isDerivedOccName :: OccName -> Bool
588 -- ^ Test for definitions internally generated by GHC. This predicte
589 -- is used to suppress printing of internal definitions in some debug prints
590 isDerivedOccName occ =
591 case occNameString occ of
592 '$':c:_ | isAlphaNum c -> True -- E.g. $wfoo
593 c:':':_ | isAlphaNum c -> True -- E.g. N:blah newtype coercions
594 _other -> False
595
596 isDefaultMethodOcc :: OccName -> Bool
597 isDefaultMethodOcc occ =
598 case occNameString occ of
599 '$':'d':'m':_ -> True
600 _ -> False
601
602 -- | Is an 'OccName' one of a Typeable @TyCon@ or @Module@ binding?
603 -- This is needed as these bindings are renamed differently.
604 -- See Note [Grand plan for Typeable] in TcTypeable.
605 isTypeableBindOcc :: OccName -> Bool
606 isTypeableBindOcc occ =
607 case occNameString occ of
608 '$':'t':'c':_ -> True -- mkTyConRepOcc
609 '$':'t':'r':_ -> True -- Module binding
610 _ -> False
611
612 mkDataConWrapperOcc, mkWorkerOcc,
613 mkMatcherOcc, mkBuilderOcc,
614 mkDefaultMethodOcc,
615 mkClassDataConOcc, mkDictOcc,
616 mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkRepEqOcc,
617 mkGenR, mkGen1R,
618 mkDataConWorkerOcc, mkNewTyCoOcc,
619 mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
620 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
621 mkTyConRepOcc
622 :: OccName -> OccName
623
624 -- These derived variables have a prefix that no Haskell value could have
625 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
626 mkWorkerOcc = mk_simple_deriv varName "$w"
627 mkMatcherOcc = mk_simple_deriv varName "$m"
628 mkBuilderOcc = mk_simple_deriv varName "$b"
629 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
630 mkClassOpAuxOcc = mk_simple_deriv varName "$c"
631 mkDictOcc = mk_simple_deriv varName "$d"
632 mkIPOcc = mk_simple_deriv varName "$i"
633 mkSpecOcc = mk_simple_deriv varName "$s"
634 mkForeignExportOcc = mk_simple_deriv varName "$f"
635 mkRepEqOcc = mk_simple_deriv tvName "$r" -- In RULES involving Coercible
636 mkClassDataConOcc = mk_simple_deriv dataName "C:" -- Data con for a class
637 mkNewTyCoOcc = mk_simple_deriv tcName "N:" -- Coercion for newtypes
638 mkInstTyCoOcc = mk_simple_deriv tcName "D:" -- Coercion for type functions
639 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
640
641 -- Used in derived instances
642 mkCon2TagOcc = mk_simple_deriv varName "$con2tag_"
643 mkTag2ConOcc = mk_simple_deriv varName "$tag2con_"
644 mkMaxTagOcc = mk_simple_deriv varName "$maxtag_"
645
646 -- TyConRepName stuff; see Note [Grand plan for Typeable] in TcTypeable
647 mkTyConRepOcc occ = mk_simple_deriv varName prefix occ
648 where
649 prefix | isDataOcc occ = "$tc'"
650 | otherwise = "$tc"
651
652 -- Generic deriving mechanism
653 mkGenR = mk_simple_deriv tcName "Rep_"
654 mkGen1R = mk_simple_deriv tcName "Rep1_"
655
656 -- Vectorisation
657 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
658 mkPADFunOcc, mkPReprTyConOcc,
659 mkPDataTyConOcc, mkPDataDataConOcc,
660 mkPDatasTyConOcc, mkPDatasDataConOcc
661 :: Maybe String -> OccName -> OccName
662 mkVectOcc = mk_simple_deriv_with varName "$v"
663 mkVectTyConOcc = mk_simple_deriv_with tcName "V:"
664 mkVectDataConOcc = mk_simple_deriv_with dataName "VD:"
665 mkVectIsoOcc = mk_simple_deriv_with varName "$vi"
666 mkPADFunOcc = mk_simple_deriv_with varName "$pa"
667 mkPReprTyConOcc = mk_simple_deriv_with tcName "VR:"
668 mkPDataTyConOcc = mk_simple_deriv_with tcName "VP:"
669 mkPDatasTyConOcc = mk_simple_deriv_with tcName "VPs:"
670 mkPDataDataConOcc = mk_simple_deriv_with dataName "VPD:"
671 mkPDatasDataConOcc = mk_simple_deriv_with dataName "VPDs:"
672
673 -- Overloaded record field selectors
674 mkRecFldSelOcc :: String -> OccName
675 mkRecFldSelOcc s = mk_deriv varName "$sel" [fsLit s]
676
677 mk_simple_deriv :: NameSpace -> FastString -> OccName -> OccName
678 mk_simple_deriv sp px occ = mk_deriv sp px [occNameFS occ]
679
680 mk_simple_deriv_with :: NameSpace -- ^ the namespace
681 -> FastString -- ^ an identifying prefix
682 -> Maybe String -- ^ another optional prefix
683 -> OccName -- ^ the 'OccName' to derive from
684 -> OccName
685 mk_simple_deriv_with sp px Nothing occ = mk_deriv sp px [occNameFS occ]
686 mk_simple_deriv_with sp px (Just with) occ =
687 mk_deriv sp px [fsLit with, fsLit "_", occNameFS occ]
688
689 -- Data constructor workers are made by setting the name space
690 -- of the data constructor OccName (which should be a DataName)
691 -- to VarName
692 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
693
694 mkSuperDictAuxOcc :: Int -> OccName -> OccName
695 mkSuperDictAuxOcc index cls_tc_occ
696 = mk_deriv varName "$cp" [fsLit $ show index, occNameFS cls_tc_occ]
697
698 mkSuperDictSelOcc :: Int -- ^ Index of superclass, e.g. 3
699 -> OccName -- ^ Class, e.g. @Ord@
700 -> OccName -- ^ Derived 'Occname', e.g. @$p3Ord@
701 mkSuperDictSelOcc index cls_tc_occ
702 = mk_deriv varName "$p" [fsLit $ show index, occNameFS cls_tc_occ]
703
704 mkLocalOcc :: Unique -- ^ Unique to combine with the 'OccName'
705 -> OccName -- ^ Local name, e.g. @sat@
706 -> OccName -- ^ Nice unique version, e.g. @$L23sat@
707 mkLocalOcc uniq occ
708 = mk_deriv varName "$L" [fsLit $ show uniq, occNameFS occ]
709 -- The Unique might print with characters
710 -- that need encoding (e.g. 'z'!)
711
712 -- | Derive a name for the representation type constructor of a
713 -- @data@\/@newtype@ instance.
714 mkInstTyTcOcc :: String -- ^ Family name, e.g. @Map@
715 -> OccSet -- ^ avoid these Occs
716 -> OccName -- ^ @R:Map@
717 mkInstTyTcOcc str = chooseUniqueOcc tcName ('R' : ':' : str)
718
719 mkDFunOcc :: String -- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
720 -- Only used in debug mode, for extra clarity
721 -> Bool -- ^ Is this a hs-boot instance DFun?
722 -> OccSet -- ^ avoid these Occs
723 -> OccName -- ^ E.g. @$f3OrdMaybe@
724
725 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
726 -- thing when we compile the mother module. Reason: we don't know exactly
727 -- what the mother module will call it.
728
729 mkDFunOcc info_str is_boot set
730 = chooseUniqueOcc VarName (prefix ++ info_str) set
731 where
732 prefix | is_boot = "$fx"
733 | otherwise = "$f"
734
735 mkDataTOcc, mkDataCOcc
736 :: OccName -- ^ TyCon or data con string
737 -> OccSet -- ^ avoid these Occs
738 -> OccName -- ^ E.g. @$f3OrdMaybe@
739 -- data T = MkT ... deriving( Data ) needs definitions for
740 -- $tT :: Data.Generics.Basics.DataType
741 -- $cMkT :: Data.Generics.Basics.Constr
742 mkDataTOcc occ = chooseUniqueOcc VarName ("$t" ++ occNameString occ)
743 mkDataCOcc occ = chooseUniqueOcc VarName ("$c" ++ occNameString occ)
744
745 {-
746 Sometimes we need to pick an OccName that has not already been used,
747 given a set of in-use OccNames.
748 -}
749
750 chooseUniqueOcc :: NameSpace -> String -> OccSet -> OccName
751 chooseUniqueOcc ns str set = loop (mkOccName ns str) (0::Int)
752 where
753 loop occ n
754 | occ `elemOccSet` set = loop (mkOccName ns (str ++ show n)) (n+1)
755 | otherwise = occ
756
757 {-
758 We used to add a '$m' to indicate a method, but that gives rise to bad
759 error messages from the type checker when we print the function name or pattern
760 of an instance-decl binding. Why? Because the binding is zapped
761 to use the method name in place of the selector name.
762 (See TcClassDcl.tcMethodBind)
763
764 The way it is now, -ddump-xx output may look confusing, but
765 you can always say -dppr-debug to get the uniques.
766
767 However, we *do* have to zap the first character to be lower case,
768 because overloaded constructors (blarg) generate methods too.
769 And convert to VarName space
770
771 e.g. a call to constructor MkFoo where
772 data (Ord a) => Foo a = MkFoo a
773
774 If this is necessary, we do it by prefixing '$m'. These
775 guys never show up in error messages. What a hack.
776 -}
777
778 mkMethodOcc :: OccName -> OccName
779 mkMethodOcc occ@(OccName VarName _) = occ
780 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
781
782 {-
783 ************************************************************************
784 * *
785 \subsection{Tidying them up}
786 * *
787 ************************************************************************
788
789 Before we print chunks of code we like to rename it so that
790 we don't have to print lots of silly uniques in it. But we mustn't
791 accidentally introduce name clashes! So the idea is that we leave the
792 OccName alone unless it accidentally clashes with one that is already
793 in scope; if so, we tack on '1' at the end and try again, then '2', and
794 so on till we find a unique one.
795
796 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
797 because that isn't a single lexeme. So we encode it to 'lle' and *then*
798 tack on the '1', if necessary.
799
800 Note [TidyOccEnv]
801 ~~~~~~~~~~~~~~~~~
802 type TidyOccEnv = UniqFM Int
803
804 * Domain = The OccName's FastString. These FastStrings are "taken";
805 make sure that we don't re-use
806
807 * Int, n = A plausible starting point for new guesses
808 There is no guarantee that "FSn" is available;
809 you must look that up in the TidyOccEnv. But
810 it's a good place to start looking.
811
812 * When looking for a renaming for "foo2" we strip off the "2" and start
813 with "foo". Otherwise if we tidy twice we get silly names like foo23.
814
815 However, if it started with digits at the end, we always make a name
816 with digits at the end, rather than shortening "foo2" to just "foo",
817 even if "foo" is unused. Reasons:
818 - Plain "foo" might be used later
819 - We use trailing digits to subtly indicate a unification variable
820 in typechecker error message; see TypeRep.tidyTyVarBndr
821
822 We have to take care though! Consider a machine-generated module (Trac #10370)
823 module Foo where
824 a1 = e1
825 a2 = e2
826 ...
827 a2000 = e2000
828 Then "a1", "a2" etc are all marked taken. But now if we come across "a7" again,
829 we have to do a linear search to find a free one, "a2001". That might just be
830 acceptable once. But if we now come across "a8" again, we don't want to repeat
831 that search.
832
833 So we use the TidyOccEnv mapping for "a" (not "a7" or "a8") as our base for
834 starting the search; and we make sure to update the starting point for "a"
835 after we allocate a new one.
836
837
838 Node [Tidying multiple names at once]
839 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
840
841 Consider
842
843 > :t (id,id,id)
844
845 Every id contributes a type variable to the type signature, and all of them are
846 "a". If we tidy them one by one, we get
847
848 (id,id,id) :: (a2 -> a2, a1 -> a1, a -> a)
849
850 which is a bit unfortunate, as it unfairly renames only one of them. What we
851 would like to see is
852
853 (id,id,id) :: (a3 -> a3, a2 -> a2, a1 -> a1)
854
855 To achieve this, the function avoidClashesOccEnv can be used to prepare the
856 TidyEnv, by “blocking” every name that occurs twice in the map. This way, none
857 of the "a"s will get the priviledge of keeping this name, and all of them will
858 get a suitable number by tidyOccName.
859
860 This prepared TidyEnv can then be used with tidyOccName. See tidyTyCoVarBndrs
861 for an example where this is used.
862
863 This is #12382.
864
865 -}
866
867 type TidyOccEnv = UniqFM Int -- The in-scope OccNames
868 -- See Note [TidyOccEnv]
869
870 emptyTidyOccEnv :: TidyOccEnv
871 emptyTidyOccEnv = emptyUFM
872
873 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
874 initTidyOccEnv = foldl add emptyUFM
875 where
876 add env (OccName _ fs) = addToUFM env fs 1
877
878 -- see Note [Tidying multiple names at once]
879 avoidClashesOccEnv :: TidyOccEnv -> [OccName] -> TidyOccEnv
880 avoidClashesOccEnv env occs = go env emptyUFM occs
881 where
882 go env _ [] = env
883 go env seenOnce ((OccName _ fs):occs)
884 | fs `elemUFM` env = go env seenOnce occs
885 | fs `elemUFM` seenOnce = go (addToUFM env fs 1) seenOnce occs
886 | otherwise = go env (addToUFM seenOnce fs ()) occs
887
888 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
889 tidyOccName env occ@(OccName occ_sp fs)
890 | not (fs `elemUFM` env)
891 = (addToUFM env fs 1, occ) -- Desired OccName is free
892 | otherwise
893 = case lookupUFM env base1 of
894 Nothing -> (addToUFM env base1 2, OccName occ_sp base1)
895 Just n -> find 1 n
896 where
897 base :: String -- Drop trailing digits (see Note [TidyOccEnv])
898 base = dropWhileEndLE isDigit (unpackFS fs)
899 base1 = mkFastString (base ++ "1")
900
901 find !k !n
902 = case lookupUFM env new_fs of
903 Just {} -> find (k+1 :: Int) (n+k)
904 -- By using n+k, the n argument to find goes
905 -- 1, add 1, add 2, add 3, etc which
906 -- moves at quadratic speed through a dense patch
907
908 Nothing -> (new_env, OccName occ_sp new_fs)
909 where
910 new_fs = mkFastString (base ++ show n)
911 new_env = addToUFM (addToUFM env new_fs 1) base1 (n+1)
912 -- Update: base1, so that next time we'll start where we left off
913 -- new_fs, so that we know it is taken
914 -- If they are the same (n==1), the former wins
915 -- See Note [TidyOccEnv]
916
917 {-
918 ************************************************************************
919 * *
920 Binary instance
921 Here rather than BinIface because OccName is abstract
922 * *
923 ************************************************************************
924 -}
925
926 instance Binary NameSpace where
927 put_ bh VarName = do
928 putByte bh 0
929 put_ bh DataName = do
930 putByte bh 1
931 put_ bh TvName = do
932 putByte bh 2
933 put_ bh TcClsName = do
934 putByte bh 3
935 get bh = do
936 h <- getByte bh
937 case h of
938 0 -> do return VarName
939 1 -> do return DataName
940 2 -> do return TvName
941 _ -> do return TcClsName
942
943 instance Binary OccName where
944 put_ bh (OccName aa ab) = do
945 put_ bh aa
946 put_ bh ab
947 get bh = do
948 aa <- get bh
949 ab <- get bh
950 return (OccName aa ab)