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