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