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