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