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