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