Remove dead generics-related code from OccName
[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, occSetElts,
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 occSetElts :: OccSet -> [OccName]
447 isEmptyOccSet :: OccSet -> Bool
448 intersectOccSet :: OccSet -> OccSet -> OccSet
449 intersectsOccSet :: OccSet -> OccSet -> Bool
450 filterOccSet :: (OccName -> Bool) -> OccSet -> OccSet
451
452 emptyOccSet = emptyUniqSet
453 unitOccSet = unitUniqSet
454 mkOccSet = mkUniqSet
455 extendOccSet = addOneToUniqSet
456 extendOccSetList = addListToUniqSet
457 unionOccSets = unionUniqSets
458 unionManyOccSets = unionManyUniqSets
459 minusOccSet = minusUniqSet
460 elemOccSet = elementOfUniqSet
461 occSetElts = uniqSetToList
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 = case occNameString occ of
527 ('_' : _) -> True
528 _other -> False
529
530 {-
531 ************************************************************************
532 * *
533 \subsection{Making system names}
534 * *
535 ************************************************************************
536
537 Here's our convention for splitting up the interface file name space:
538
539 d... dictionary identifiers
540 (local variables, so no name-clash worries)
541
542 All of these other OccNames contain a mixture of alphabetic
543 and symbolic characters, and hence cannot possibly clash with
544 a user-written type or function name
545
546 $f... Dict-fun identifiers (from inst decls)
547 $dmop Default method for 'op'
548 $pnC n'th superclass selector for class C
549 $wf Worker for function 'f'
550 $sf.. Specialised version of f
551 D:C Data constructor for dictionary for class C
552 NTCo:T Coercion connecting newtype T with its representation type
553 TFCo:R Coercion connecting a data family to its representation type R
554
555 In encoded form these appear as Zdfxxx etc
556
557 :... keywords (export:, letrec: etc.)
558 --- I THINK THIS IS WRONG!
559
560 This knowledge is encoded in the following functions.
561
562 @mk_deriv@ generates an @OccName@ from the prefix and a string.
563 NB: The string must already be encoded!
564 -}
565
566 mk_deriv :: NameSpace
567 -> String -- Distinguishes one sort of derived name from another
568 -> String
569 -> OccName
570
571 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
572
573 isDerivedOccName :: OccName -> Bool
574 -- ^ Test for definitions internally generated by GHC. This predicte
575 -- is used to suppress printing of internal definitions in some debug prints
576 isDerivedOccName occ =
577 case occNameString occ of
578 '$':c:_ | isAlphaNum c -> True -- E.g. $wfoo
579 c:':':_ | isAlphaNum c -> True -- E.g. N:blah newtype coercions
580 _other -> False
581
582 mkDataConWrapperOcc, mkWorkerOcc,
583 mkMatcherOcc, mkBuilderOcc,
584 mkDefaultMethodOcc,
585 mkClassDataConOcc, mkDictOcc,
586 mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkRepEqOcc,
587 mkGenR, mkGen1R,
588 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
589 mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
590 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
591 mkTyConRepOcc
592 :: OccName -> OccName
593
594 -- These derived variables have a prefix that no Haskell value could have
595 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
596 mkWorkerOcc = mk_simple_deriv varName "$w"
597 mkMatcherOcc = mk_simple_deriv varName "$m"
598 mkBuilderOcc = mk_simple_deriv varName "$b"
599 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
600 mkClassOpAuxOcc = mk_simple_deriv varName "$c"
601 mkDictOcc = mk_simple_deriv varName "$d"
602 mkIPOcc = mk_simple_deriv varName "$i"
603 mkSpecOcc = mk_simple_deriv varName "$s"
604 mkForeignExportOcc = mk_simple_deriv varName "$f"
605 mkRepEqOcc = mk_simple_deriv tvName "$r" -- In RULES involving Coercible
606 mkClassDataConOcc = mk_simple_deriv dataName "C:" -- Data con for a class
607 mkNewTyCoOcc = mk_simple_deriv tcName "N:" -- Coercion for newtypes
608 mkInstTyCoOcc = mk_simple_deriv tcName "D:" -- Coercion for type functions
609 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
610
611 -- Used in derived instances
612 mkCon2TagOcc = mk_simple_deriv varName "$con2tag_"
613 mkTag2ConOcc = mk_simple_deriv varName "$tag2con_"
614 mkMaxTagOcc = mk_simple_deriv varName "$maxtag_"
615
616 -- TyConRepName stuff; see Note [Grand plan for Typeable] in TcTypeable
617 mkTyConRepOcc occ = mk_simple_deriv varName prefix occ
618 where
619 prefix | isDataOcc occ = "$tc'"
620 | otherwise = "$tc"
621
622 -- Generic deriving mechanism
623 mkGenR = mk_simple_deriv tcName "Rep_"
624 mkGen1R = mk_simple_deriv tcName "Rep1_"
625
626 -- data T = MkT ... deriving( Data ) needs definitions for
627 -- $tT :: Data.Generics.Basics.DataType
628 -- $cMkT :: Data.Generics.Basics.Constr
629 mkDataTOcc = mk_simple_deriv varName "$t"
630 mkDataCOcc = mk_simple_deriv varName "$c"
631
632 -- Vectorisation
633 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
634 mkPADFunOcc, mkPReprTyConOcc,
635 mkPDataTyConOcc, mkPDataDataConOcc,
636 mkPDatasTyConOcc, mkPDatasDataConOcc
637 :: Maybe String -> OccName -> OccName
638 mkVectOcc = mk_simple_deriv_with varName "$v"
639 mkVectTyConOcc = mk_simple_deriv_with tcName "V:"
640 mkVectDataConOcc = mk_simple_deriv_with dataName "VD:"
641 mkVectIsoOcc = mk_simple_deriv_with varName "$vi"
642 mkPADFunOcc = mk_simple_deriv_with varName "$pa"
643 mkPReprTyConOcc = mk_simple_deriv_with tcName "VR:"
644 mkPDataTyConOcc = mk_simple_deriv_with tcName "VP:"
645 mkPDatasTyConOcc = mk_simple_deriv_with tcName "VPs:"
646 mkPDataDataConOcc = mk_simple_deriv_with dataName "VPD:"
647 mkPDatasDataConOcc = mk_simple_deriv_with dataName "VPDs:"
648
649 -- Overloaded record field selectors
650 mkRecFldSelOcc :: String -> OccName
651 mkRecFldSelOcc = mk_deriv varName "$sel"
652
653 mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
654 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
655
656 mk_simple_deriv_with :: NameSpace -> String -> Maybe String -> OccName -> OccName
657 mk_simple_deriv_with sp px Nothing occ = mk_deriv sp px (occNameString occ)
658 mk_simple_deriv_with sp px (Just with) occ = mk_deriv sp (px ++ with ++ "_") (occNameString occ)
659
660 -- Data constructor workers are made by setting the name space
661 -- of the data constructor OccName (which should be a DataName)
662 -- to VarName
663 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
664
665 mkSuperDictAuxOcc :: Int -> OccName -> OccName
666 mkSuperDictAuxOcc index cls_tc_occ
667 = mk_deriv varName "$cp" (show index ++ occNameString cls_tc_occ)
668
669 mkSuperDictSelOcc :: Int -- ^ Index of superclass, e.g. 3
670 -> OccName -- ^ Class, e.g. @Ord@
671 -> OccName -- ^ Derived 'Occname', e.g. @$p3Ord@
672 mkSuperDictSelOcc index cls_tc_occ
673 = mk_deriv varName "$p" (show index ++ occNameString cls_tc_occ)
674
675 mkLocalOcc :: Unique -- ^ Unique to combine with the 'OccName'
676 -> OccName -- ^ Local name, e.g. @sat@
677 -> OccName -- ^ Nice unique version, e.g. @$L23sat@
678 mkLocalOcc uniq occ
679 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
680 -- The Unique might print with characters
681 -- that need encoding (e.g. 'z'!)
682
683 -- | Derive a name for the representation type constructor of a
684 -- @data@\/@newtype@ instance.
685 mkInstTyTcOcc :: String -- ^ Family name, e.g. @Map@
686 -> OccSet -- ^ avoid these Occs
687 -> OccName -- ^ @R:Map@
688 mkInstTyTcOcc str set =
689 chooseUniqueOcc tcName ('R' : ':' : str) set
690
691 mkDFunOcc :: String -- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
692 -- Only used in debug mode, for extra clarity
693 -> Bool -- ^ Is this a hs-boot instance DFun?
694 -> OccSet -- ^ avoid these Occs
695 -> OccName -- ^ E.g. @$f3OrdMaybe@
696
697 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
698 -- thing when we compile the mother module. Reason: we don't know exactly
699 -- what the mother module will call it.
700
701 mkDFunOcc info_str is_boot set
702 = chooseUniqueOcc VarName (prefix ++ info_str) set
703 where
704 prefix | is_boot = "$fx"
705 | otherwise = "$f"
706
707 {-
708 Sometimes we need to pick an OccName that has not already been used,
709 given a set of in-use OccNames.
710 -}
711
712 chooseUniqueOcc :: NameSpace -> String -> OccSet -> OccName
713 chooseUniqueOcc ns str set = loop (mkOccName ns str) (0::Int)
714 where
715 loop occ n
716 | occ `elemOccSet` set = loop (mkOccName ns (str ++ show n)) (n+1)
717 | otherwise = occ
718
719 {-
720 We used to add a '$m' to indicate a method, but that gives rise to bad
721 error messages from the type checker when we print the function name or pattern
722 of an instance-decl binding. Why? Because the binding is zapped
723 to use the method name in place of the selector name.
724 (See TcClassDcl.tcMethodBind)
725
726 The way it is now, -ddump-xx output may look confusing, but
727 you can always say -dppr-debug to get the uniques.
728
729 However, we *do* have to zap the first character to be lower case,
730 because overloaded constructors (blarg) generate methods too.
731 And convert to VarName space
732
733 e.g. a call to constructor MkFoo where
734 data (Ord a) => Foo a = MkFoo a
735
736 If this is necessary, we do it by prefixing '$m'. These
737 guys never show up in error messages. What a hack.
738 -}
739
740 mkMethodOcc :: OccName -> OccName
741 mkMethodOcc occ@(OccName VarName _) = occ
742 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
743
744 {-
745 ************************************************************************
746 * *
747 \subsection{Tidying them up}
748 * *
749 ************************************************************************
750
751 Before we print chunks of code we like to rename it so that
752 we don't have to print lots of silly uniques in it. But we mustn't
753 accidentally introduce name clashes! So the idea is that we leave the
754 OccName alone unless it accidentally clashes with one that is already
755 in scope; if so, we tack on '1' at the end and try again, then '2', and
756 so on till we find a unique one.
757
758 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
759 because that isn't a single lexeme. So we encode it to 'lle' and *then*
760 tack on the '1', if necessary.
761
762 Note [TidyOccEnv]
763 ~~~~~~~~~~~~~~~~~
764 type TidyOccEnv = UniqFM Int
765
766 * Domain = The OccName's FastString. These FastStrings are "taken";
767 make sure that we don't re-use
768
769 * Int, n = A plausible starting point for new guesses
770 There is no guarantee that "FSn" is available;
771 you must look that up in the TidyOccEnv. But
772 it's a good place to start looking.
773
774 * When looking for a renaming for "foo2" we strip off the "2" and start
775 with "foo". Otherwise if we tidy twice we get silly names like foo23.
776
777 However, if it started with digits at the end, we always make a name
778 with digits at the end, rather than shortening "foo2" to just "foo",
779 even if "foo" is unused. Reasons:
780 - Plain "foo" might be used later
781 - We use trailing digits to subtly indicate a unification variable
782 in typechecker error message; see TypeRep.tidyTyVarBndr
783
784 We have to take care though! Consider a machine-generated module (Trac #10370)
785 module Foo where
786 a1 = e1
787 a2 = e2
788 ...
789 a2000 = e2000
790 Then "a1", "a2" etc are all marked taken. But now if we come across "a7" again,
791 we have to do a linear search to find a free one, "a2001". That might just be
792 acceptable once. But if we now come across "a8" again, we don't want to repeat
793 that search.
794
795 So we use the TidyOccEnv mapping for "a" (not "a7" or "a8") as our base for
796 starting the search; and we make sure to update the starting point for "a"
797 after we allocate a new one.
798
799 -}
800
801 type TidyOccEnv = UniqFM Int -- The in-scope OccNames
802 -- See Note [TidyOccEnv]
803
804 emptyTidyOccEnv :: TidyOccEnv
805 emptyTidyOccEnv = emptyUFM
806
807 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
808 initTidyOccEnv = foldl add emptyUFM
809 where
810 add env (OccName _ fs) = addToUFM env fs 1
811
812 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
813 tidyOccName env occ@(OccName occ_sp fs)
814 = case lookupUFM env fs of
815 Nothing -> (addToUFM env fs 1, occ) -- Desired OccName is free
816 Just {} -> case lookupUFM env base1 of
817 Nothing -> (addToUFM env base1 2, OccName occ_sp base1)
818 Just n -> find 1 n
819 where
820 base :: String -- Drop trailing digits (see Note [TidyOccEnv])
821 base = dropWhileEndLE isDigit (unpackFS fs)
822 base1 = mkFastString (base ++ "1")
823
824 find !k !n
825 = case lookupUFM env new_fs of
826 Just {} -> find (k+1 :: Int) (n+k)
827 -- By using n+k, the n argument to find goes
828 -- 1, add 1, add 2, add 3, etc which
829 -- moves at quadratic speed through a dense patch
830
831 Nothing -> (new_env, OccName occ_sp new_fs)
832 where
833 new_fs = mkFastString (base ++ show n)
834 new_env = addToUFM (addToUFM env new_fs 1) base1 (n+1)
835 -- Update: base1, so that next time we'll start where we left off
836 -- new_fs, so that we know it is taken
837 -- If they are the same (n==1), the former wins
838 -- See Note [TidyOccEnv]
839
840 {-
841 ************************************************************************
842 * *
843 Binary instance
844 Here rather than BinIface because OccName is abstract
845 * *
846 ************************************************************************
847 -}
848
849 instance Binary NameSpace where
850 put_ bh VarName = do
851 putByte bh 0
852 put_ bh DataName = do
853 putByte bh 1
854 put_ bh TvName = do
855 putByte bh 2
856 put_ bh TcClsName = do
857 putByte bh 3
858 get bh = do
859 h <- getByte bh
860 case h of
861 0 -> do return VarName
862 1 -> do return DataName
863 2 -> do return TvName
864 _ -> do return TcClsName
865
866 instance Binary OccName where
867 put_ bh (OccName aa ab) = do
868 put_ bh aa
869 put_ bh ab
870 get bh = do
871 aa <- get bh
872 ab <- get bh
873 return (OccName aa ab)