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