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