Fix suggestions for unbound variables (#11680)
[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 mkNewTyCoOcc, mkClassOpAuxOcc,
60 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
61 mkClassDataConOcc, mkDictOcc, mkIPOcc,
62 mkSpecOcc, mkForeignExportOcc, mkRepEqOcc,
63 mkGenD, mkGenR, mkGen1R, mkGenRCo, mkGenC, mkGenS,
64 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
65 mkSuperDictSelOcc, mkSuperDictAuxOcc,
66 mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
67 mkInstTyCoOcc, mkEqPredCoOcc,
68 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
69 mkPDataTyConOcc, mkPDataDataConOcc,
70 mkPDatasTyConOcc, mkPDatasDataConOcc,
71 mkPReprTyConOcc,
72 mkPADFunOcc,
73 mkRecFldSelOcc,
74 mkTyConRepOcc,
75
76 -- ** Deconstruction
77 occNameFS, occNameString, occNameSpace,
78
79 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
80 parenSymOcc, startsWithUnderscore,
81
82 isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
83
84 -- * The 'OccEnv' type
85 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
86 lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
87 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
88 extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
89 alterOccEnv, pprOccEnv,
90
91 -- * The 'OccSet' type
92 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
93 extendOccSetList,
94 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
95 foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
96 filterOccSet,
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 = text "data constructor"
195 pprNameSpace VarName = text "variable"
196 pprNameSpace TvName = text "type variable"
197 pprNameSpace TcClsName = text "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 = text "tv"
207 pprNameSpaceBrief TcClsName = text "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 deriving (Data, Typeable)
390
391 emptyOccEnv :: OccEnv a
392 unitOccEnv :: OccName -> a -> OccEnv a
393 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
394 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
395 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
396 mkOccEnv :: [(OccName,a)] -> OccEnv a
397 mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
398 elemOccEnv :: OccName -> OccEnv a -> Bool
399 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
400 occEnvElts :: OccEnv a -> [a]
401 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
402 extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
403 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
404 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
405 mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
406 delFromOccEnv :: OccEnv a -> OccName -> OccEnv a
407 delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
408 filterOccEnv :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
409 alterOccEnv :: (Maybe elt -> Maybe elt) -> OccEnv elt -> OccName -> OccEnv elt
410
411 emptyOccEnv = A emptyUFM
412 unitOccEnv x y = A $ unitUFM x y
413 extendOccEnv (A x) y z = A $ addToUFM x y z
414 extendOccEnvList (A x) l = A $ addListToUFM x l
415 lookupOccEnv (A x) y = lookupUFM x y
416 mkOccEnv l = A $ listToUFM l
417 elemOccEnv x (A y) = elemUFM x y
418 foldOccEnv a b (A c) = foldUFM a b c
419 occEnvElts (A x) = eltsUFM x
420 plusOccEnv (A x) (A y) = A $ plusUFM x y
421 plusOccEnv_C f (A x) (A y) = A $ plusUFM_C f x y
422 extendOccEnv_C f (A x) y z = A $ addToUFM_C f x y z
423 extendOccEnv_Acc f g (A x) y z = A $ addToUFM_Acc f g x y z
424 mapOccEnv f (A x) = A $ mapUFM f x
425 mkOccEnv_C comb l = A $ addListToUFM_C comb emptyUFM l
426 delFromOccEnv (A x) y = A $ delFromUFM x y
427 delListFromOccEnv (A x) y = A $ delListFromUFM x y
428 filterOccEnv x (A y) = A $ filterUFM x y
429 alterOccEnv fn (A y) k = A $ alterUFM fn y k
430
431 instance Outputable a => Outputable (OccEnv a) where
432 ppr x = pprOccEnv ppr x
433
434 pprOccEnv :: (a -> SDoc) -> OccEnv a -> SDoc
435 pprOccEnv ppr_elt (A env) = pprUniqFM ppr_elt env
436
437 type OccSet = UniqSet OccName
438
439 emptyOccSet :: OccSet
440 unitOccSet :: OccName -> OccSet
441 mkOccSet :: [OccName] -> OccSet
442 extendOccSet :: OccSet -> OccName -> OccSet
443 extendOccSetList :: OccSet -> [OccName] -> OccSet
444 unionOccSets :: OccSet -> OccSet -> OccSet
445 unionManyOccSets :: [OccSet] -> OccSet
446 minusOccSet :: OccSet -> OccSet -> OccSet
447 elemOccSet :: OccName -> OccSet -> Bool
448 occSetElts :: OccSet -> [OccName]
449 foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
450 isEmptyOccSet :: OccSet -> Bool
451 intersectOccSet :: OccSet -> OccSet -> OccSet
452 intersectsOccSet :: OccSet -> OccSet -> Bool
453 filterOccSet :: (OccName -> Bool) -> OccSet -> OccSet
454
455 emptyOccSet = emptyUniqSet
456 unitOccSet = unitUniqSet
457 mkOccSet = mkUniqSet
458 extendOccSet = addOneToUniqSet
459 extendOccSetList = addListToUniqSet
460 unionOccSets = unionUniqSets
461 unionManyOccSets = unionManyUniqSets
462 minusOccSet = minusUniqSet
463 elemOccSet = elementOfUniqSet
464 occSetElts = uniqSetToList
465 foldOccSet = foldUniqSet
466 isEmptyOccSet = isEmptyUniqSet
467 intersectOccSet = intersectUniqSets
468 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
469 filterOccSet = filterUniqSet
470
471 {-
472 ************************************************************************
473 * *
474 \subsection{Predicates and taking them apart}
475 * *
476 ************************************************************************
477 -}
478
479 occNameString :: OccName -> String
480 occNameString (OccName _ s) = unpackFS s
481
482 setOccNameSpace :: NameSpace -> OccName -> OccName
483 setOccNameSpace sp (OccName _ occ) = OccName sp occ
484
485 isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
486
487 isVarOcc (OccName VarName _) = True
488 isVarOcc _ = False
489
490 isTvOcc (OccName TvName _) = True
491 isTvOcc _ = False
492
493 isTcOcc (OccName TcClsName _) = True
494 isTcOcc _ = False
495
496 -- | /Value/ 'OccNames's are those that are either in
497 -- the variable or data constructor namespaces
498 isValOcc :: OccName -> Bool
499 isValOcc (OccName VarName _) = True
500 isValOcc (OccName DataName _) = True
501 isValOcc _ = False
502
503 isDataOcc (OccName DataName _) = True
504 isDataOcc _ = False
505
506 -- | Test if the 'OccName' is a data constructor that starts with
507 -- a symbol (e.g. @:@, or @[]@)
508 isDataSymOcc :: OccName -> Bool
509 isDataSymOcc (OccName DataName s) = isLexConSym s
510 isDataSymOcc _ = False
511 -- Pretty inefficient!
512
513 -- | Test if the 'OccName' is that for any operator (whether
514 -- it is a data constructor or variable or whatever)
515 isSymOcc :: OccName -> Bool
516 isSymOcc (OccName DataName s) = isLexConSym s
517 isSymOcc (OccName TcClsName s) = isLexSym s
518 isSymOcc (OccName VarName s) = isLexSym s
519 isSymOcc (OccName TvName s) = isLexSym s
520 -- Pretty inefficient!
521
522 parenSymOcc :: OccName -> SDoc -> SDoc
523 -- ^ Wrap parens around an operator
524 parenSymOcc occ doc | isSymOcc occ = parens doc
525 | otherwise = doc
526
527 startsWithUnderscore :: OccName -> Bool
528 -- ^ Haskell 98 encourages compilers to suppress warnings about unsed
529 -- names in a pattern if they start with @_@: this implements that test
530 startsWithUnderscore occ = case occNameString occ of
531 ('_' : _) -> True
532 _other -> False
533
534 {-
535 ************************************************************************
536 * *
537 \subsection{Making system names}
538 * *
539 ************************************************************************
540
541 Here's our convention for splitting up the interface file name space:
542
543 d... dictionary identifiers
544 (local variables, so no name-clash worries)
545
546 All of these other OccNames contain a mixture of alphabetic
547 and symbolic characters, and hence cannot possibly clash with
548 a user-written type or function name
549
550 $f... Dict-fun identifiers (from inst decls)
551 $dmop Default method for 'op'
552 $pnC n'th superclass selector for class C
553 $wf Worker for function 'f'
554 $sf.. Specialised version of f
555 D:C Data constructor for dictionary for class C
556 NTCo:T Coercion connecting newtype T with its representation type
557 TFCo:R Coercion connecting a data family to its representation type R
558
559 In encoded form these appear as Zdfxxx etc
560
561 :... keywords (export:, letrec: etc.)
562 --- I THINK THIS IS WRONG!
563
564 This knowledge is encoded in the following functions.
565
566 @mk_deriv@ generates an @OccName@ from the prefix and a string.
567 NB: The string must already be encoded!
568 -}
569
570 mk_deriv :: NameSpace
571 -> String -- Distinguishes one sort of derived name from another
572 -> String
573 -> OccName
574
575 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
576
577 isDerivedOccName :: OccName -> Bool
578 -- ^ Test for definitions internally generated by GHC. This predicte
579 -- is used to suppress printing of internal definitions in some debug prints
580 isDerivedOccName occ =
581 case occNameString occ of
582 '$':c:_ | isAlphaNum c -> True -- E.g. $wfoo
583 c:':':_ | isAlphaNum c -> True -- E.g. N:blah newtype coercions
584 _other -> False
585
586 mkDataConWrapperOcc, mkWorkerOcc,
587 mkMatcherOcc, mkBuilderOcc,
588 mkDefaultMethodOcc,
589 mkClassDataConOcc, mkDictOcc,
590 mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkRepEqOcc,
591 mkGenR, mkGen1R, mkGenRCo,
592 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
593 mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
594 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
595 mkTyConRepOcc
596 :: OccName -> OccName
597
598 -- These derived variables have a prefix that no Haskell value could have
599 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
600 mkWorkerOcc = mk_simple_deriv varName "$w"
601 mkMatcherOcc = mk_simple_deriv varName "$m"
602 mkBuilderOcc = mk_simple_deriv varName "$b"
603 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
604 mkClassOpAuxOcc = mk_simple_deriv varName "$c"
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 mkClassDataConOcc = mk_simple_deriv dataName "C:" -- Data con for a class
611 mkNewTyCoOcc = mk_simple_deriv tcName "N:" -- Coercion for newtypes
612 mkInstTyCoOcc = mk_simple_deriv tcName "D:" -- Coercion for type functions
613 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
614
615 -- Used in derived instances
616 mkCon2TagOcc = mk_simple_deriv varName "$con2tag_"
617 mkTag2ConOcc = mk_simple_deriv varName "$tag2con_"
618 mkMaxTagOcc = mk_simple_deriv varName "$maxtag_"
619
620 -- TyConRepName stuff; see Note [Grand plan for Typeable] in TcTypeable
621 mkTyConRepOcc occ = mk_simple_deriv varName prefix occ
622 where
623 prefix | isDataOcc occ = "$tc'"
624 | otherwise = "$tc"
625
626 -- Generic deriving mechanism
627
628 -- | Generate a module-unique name, to be used e.g. while generating new names
629 -- for Generics types. We use module unit id to avoid name clashes when
630 -- package imports is used.
631 mkModPrefix :: Module -> String
632 mkModPrefix mod = pk ++ "_" ++ mn
633 where
634 pk = unitIdString (moduleUnitId mod)
635 mn = moduleNameString (moduleName mod)
636
637 mkGenD :: Module -> OccName -> OccName
638 mkGenD mod = mk_simple_deriv tcName ("D1_" ++ mkModPrefix mod ++ "_")
639
640 mkGenC :: Module -> OccName -> Int -> OccName
641 mkGenC mod occ m =
642 mk_deriv tcName ("C1_" ++ show m) $
643 mkModPrefix mod ++ "_" ++ occNameString occ
644
645 mkGenS :: Module -> OccName -> Int -> Int -> OccName
646 mkGenS mod occ m n =
647 mk_deriv tcName ("S1_" ++ show m ++ "_" ++ show n) $
648 mkModPrefix mod ++ "_" ++ occNameString occ
649
650 mkGenR = mk_simple_deriv tcName "Rep_"
651 mkGen1R = mk_simple_deriv tcName "Rep1_"
652 mkGenRCo = mk_simple_deriv tcName "CoRep_"
653
654 -- data T = MkT ... deriving( Data ) needs definitions for
655 -- $tT :: Data.Generics.Basics.DataType
656 -- $cMkT :: Data.Generics.Basics.Constr
657 mkDataTOcc = mk_simple_deriv varName "$t"
658 mkDataCOcc = mk_simple_deriv varName "$c"
659
660 -- Vectorisation
661 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
662 mkPADFunOcc, mkPReprTyConOcc,
663 mkPDataTyConOcc, mkPDataDataConOcc,
664 mkPDatasTyConOcc, mkPDatasDataConOcc
665 :: Maybe String -> OccName -> OccName
666 mkVectOcc = mk_simple_deriv_with varName "$v"
667 mkVectTyConOcc = mk_simple_deriv_with tcName "V:"
668 mkVectDataConOcc = mk_simple_deriv_with dataName "VD:"
669 mkVectIsoOcc = mk_simple_deriv_with varName "$vi"
670 mkPADFunOcc = mk_simple_deriv_with varName "$pa"
671 mkPReprTyConOcc = mk_simple_deriv_with tcName "VR:"
672 mkPDataTyConOcc = mk_simple_deriv_with tcName "VP:"
673 mkPDatasTyConOcc = mk_simple_deriv_with tcName "VPs:"
674 mkPDataDataConOcc = mk_simple_deriv_with dataName "VPD:"
675 mkPDatasDataConOcc = mk_simple_deriv_with dataName "VPDs:"
676
677 -- Overloaded record field selectors
678 mkRecFldSelOcc :: String -> OccName
679 mkRecFldSelOcc = mk_deriv varName "$sel"
680
681 mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
682 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
683
684 mk_simple_deriv_with :: NameSpace -> String -> Maybe String -> OccName -> OccName
685 mk_simple_deriv_with sp px Nothing occ = mk_deriv sp px (occNameString occ)
686 mk_simple_deriv_with sp px (Just with) occ = mk_deriv sp (px ++ with ++ "_") (occNameString occ)
687
688 -- Data constructor workers are made by setting the name space
689 -- of the data constructor OccName (which should be a DataName)
690 -- to VarName
691 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
692
693 mkSuperDictAuxOcc :: Int -> OccName -> OccName
694 mkSuperDictAuxOcc index cls_tc_occ
695 = mk_deriv varName "$cp" (show index ++ occNameString cls_tc_occ)
696
697 mkSuperDictSelOcc :: Int -- ^ Index of superclass, e.g. 3
698 -> OccName -- ^ Class, e.g. @Ord@
699 -> OccName -- ^ Derived 'Occname', e.g. @$p3Ord@
700 mkSuperDictSelOcc index cls_tc_occ
701 = mk_deriv varName "$p" (show index ++ occNameString cls_tc_occ)
702
703 mkLocalOcc :: Unique -- ^ Unique to combine with the 'OccName'
704 -> OccName -- ^ Local name, e.g. @sat@
705 -> OccName -- ^ Nice unique version, e.g. @$L23sat@
706 mkLocalOcc uniq occ
707 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
708 -- The Unique might print with characters
709 -- that need encoding (e.g. 'z'!)
710
711 -- | Derive a name for the representation type constructor of a
712 -- @data@\/@newtype@ instance.
713 mkInstTyTcOcc :: String -- ^ Family name, e.g. @Map@
714 -> OccSet -- ^ avoid these Occs
715 -> OccName -- ^ @R:Map@
716 mkInstTyTcOcc str set =
717 chooseUniqueOcc tcName ('R' : ':' : str) set
718
719 mkDFunOcc :: String -- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
720 -- Only used in debug mode, for extra clarity
721 -> Bool -- ^ Is this a hs-boot instance DFun?
722 -> OccSet -- ^ avoid these Occs
723 -> OccName -- ^ E.g. @$f3OrdMaybe@
724
725 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
726 -- thing when we compile the mother module. Reason: we don't know exactly
727 -- what the mother module will call it.
728
729 mkDFunOcc info_str is_boot set
730 = chooseUniqueOcc VarName (prefix ++ info_str) set
731 where
732 prefix | is_boot = "$fx"
733 | otherwise = "$f"
734
735 {-
736 Sometimes we need to pick an OccName that has not already been used,
737 given a set of in-use OccNames.
738 -}
739
740 chooseUniqueOcc :: NameSpace -> String -> OccSet -> OccName
741 chooseUniqueOcc ns str set = loop (mkOccName ns str) (0::Int)
742 where
743 loop occ n
744 | occ `elemOccSet` set = loop (mkOccName ns (str ++ show n)) (n+1)
745 | otherwise = occ
746
747 {-
748 We used to add a '$m' to indicate a method, but that gives rise to bad
749 error messages from the type checker when we print the function name or pattern
750 of an instance-decl binding. Why? Because the binding is zapped
751 to use the method name in place of the selector name.
752 (See TcClassDcl.tcMethodBind)
753
754 The way it is now, -ddump-xx output may look confusing, but
755 you can always say -dppr-debug to get the uniques.
756
757 However, we *do* have to zap the first character to be lower case,
758 because overloaded constructors (blarg) generate methods too.
759 And convert to VarName space
760
761 e.g. a call to constructor MkFoo where
762 data (Ord a) => Foo a = MkFoo a
763
764 If this is necessary, we do it by prefixing '$m'. These
765 guys never show up in error messages. What a hack.
766 -}
767
768 mkMethodOcc :: OccName -> OccName
769 mkMethodOcc occ@(OccName VarName _) = occ
770 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
771
772 {-
773 ************************************************************************
774 * *
775 \subsection{Tidying them up}
776 * *
777 ************************************************************************
778
779 Before we print chunks of code we like to rename it so that
780 we don't have to print lots of silly uniques in it. But we mustn't
781 accidentally introduce name clashes! So the idea is that we leave the
782 OccName alone unless it accidentally clashes with one that is already
783 in scope; if so, we tack on '1' at the end and try again, then '2', and
784 so on till we find a unique one.
785
786 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
787 because that isn't a single lexeme. So we encode it to 'lle' and *then*
788 tack on the '1', if necessary.
789
790 Note [TidyOccEnv]
791 ~~~~~~~~~~~~~~~~~
792 type TidyOccEnv = UniqFM Int
793
794 * Domain = The OccName's FastString. These FastStrings are "taken";
795 make sure that we don't re-use
796
797 * Int, n = A plausible starting point for new guesses
798 There is no guarantee that "FSn" is available;
799 you must look that up in the TidyOccEnv. But
800 it's a good place to start looking.
801
802 * When looking for a renaming for "foo2" we strip off the "2" and start
803 with "foo". Otherwise if we tidy twice we get silly names like foo23.
804
805 However, if it started with digits at the end, we always make a name
806 with digits at the end, rather than shortening "foo2" to just "foo",
807 even if "foo" is unused. Reasons:
808 - Plain "foo" might be used later
809 - We use trailing digits to subtly indicate a unification variable
810 in typechecker error message; see TypeRep.tidyTyVarBndr
811
812 We have to take care though! Consider a machine-generated module (Trac #10370)
813 module Foo where
814 a1 = e1
815 a2 = e2
816 ...
817 a2000 = e2000
818 Then "a1", "a2" etc are all marked taken. But now if we come across "a7" again,
819 we have to do a linear search to find a free one, "a2001". That might just be
820 acceptable once. But if we now come across "a8" again, we don't want to repeat
821 that search.
822
823 So we use the TidyOccEnv mapping for "a" (not "a7" or "a8") as our base for
824 starting the search; and we make sure to update the starting point for "a"
825 after we allocate a new one.
826
827 -}
828
829 type TidyOccEnv = UniqFM Int -- The in-scope OccNames
830 -- See Note [TidyOccEnv]
831
832 emptyTidyOccEnv :: TidyOccEnv
833 emptyTidyOccEnv = emptyUFM
834
835 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
836 initTidyOccEnv = foldl add emptyUFM
837 where
838 add env (OccName _ fs) = addToUFM env fs 1
839
840 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
841 tidyOccName env occ@(OccName occ_sp fs)
842 = case lookupUFM env fs of
843 Nothing -> (addToUFM env fs 1, occ) -- Desired OccName is free
844 Just {} -> case lookupUFM env base1 of
845 Nothing -> (addToUFM env base1 2, OccName occ_sp base1)
846 Just n -> find 1 n
847 where
848 base :: String -- Drop trailing digits (see Note [TidyOccEnv])
849 base = dropWhileEndLE isDigit (unpackFS fs)
850 base1 = mkFastString (base ++ "1")
851
852 find !k !n
853 = case lookupUFM env new_fs of
854 Just {} -> find (k+1 :: Int) (n+k)
855 -- By using n+k, the n argument to find goes
856 -- 1, add 1, add 2, add 3, etc which
857 -- moves at quadratic speed through a dense patch
858
859 Nothing -> (new_env, OccName occ_sp new_fs)
860 where
861 new_fs = mkFastString (base ++ show n)
862 new_env = addToUFM (addToUFM env new_fs 1) base1 (n+1)
863 -- Update: base1, so that next time we'll start where we left off
864 -- new_fs, so that we know it is taken
865 -- If they are the same (n==1), the former wins
866 -- See Note [TidyOccEnv]
867
868 {-
869 ************************************************************************
870 * *
871 Binary instance
872 Here rather than BinIface because OccName is abstract
873 * *
874 ************************************************************************
875 -}
876
877 instance Binary NameSpace where
878 put_ bh VarName = do
879 putByte bh 0
880 put_ bh DataName = do
881 putByte bh 1
882 put_ bh TvName = do
883 putByte bh 2
884 put_ bh TcClsName = do
885 putByte bh 3
886 get bh = do
887 h <- getByte bh
888 case h of
889 0 -> do return VarName
890 1 -> do return DataName
891 2 -> do return TvName
892 _ -> do return TcClsName
893
894 instance Binary OccName where
895 put_ bh (OccName aa ab) = do
896 put_ bh aa
897 put_ bh ab
898 get bh = do
899 aa <- get bh
900 ab <- get bh
901 return (OccName aa ab)