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