2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
9 -- GHC uses several kinds of name internally:
11 -- * 'OccName.OccName' represents names as strings with just a little more information:
12 -- the \"namespace\" that the name came from, e.g. the namespace of value, type constructors or
15 -- * 'RdrName.RdrName': see "RdrName#name_types"
17 -- * 'Name.Name': see "Name#name_types"
19 -- * 'Id.Id': see "Id#name_types"
21 -- * 'Var.Var': see "Var#name_types"
23 -- * The 'NameSpace' type
24 NameSpace, -- Abstract
27 -- $real_vs_source_data_constructors
28 tcName, clsName, tcClsName, dataName, varName,
32 pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
34 -- * The 'OccName' type
35 OccName, -- Abstract, instance of Outputable
39 mkOccName, mkOccNameFS,
41 mkDataOcc, mkDataOccFS,
42 mkTyVarOcc, mkTyVarOccFS,
49 -- ** Derived 'OccName's
51 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc,
52 mkDerivedTyConOcc, mkNewTyCoOcc, mkClassOpAuxOcc,
53 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
54 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
55 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
56 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
57 mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
58 mkInstTyCoOcc, mkEqPredCoOcc,
59 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
60 mkPDataTyConOcc, mkPDataDataConOcc,
65 occNameFS, occNameString, occNameSpace,
67 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
68 parenSymOcc, startsWithUnderscore,
70 isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
74 -- * The 'OccEnv' type
75 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
76 lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
77 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
78 filterOccEnv, delListFromOccEnv, delFromOccEnv,
80 -- * The 'OccSet' type
81 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
83 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
84 foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
87 TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
89 -- * Lexical characteristics of Haskell names
90 isLexCon, isLexVar, isLexId, isLexSym,
91 isLexConId, isLexConSym, isLexVarId, isLexVarSym,
92 startsVarSym, startsVarId, startsConSym, startsConId
107 -- Unicode TODO: put isSymbol in libcompat
108 #if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ > 604
110 isSymbol :: a -> Bool
111 isSymbol = const False
116 %************************************************************************
118 \subsection{Name space}
120 %************************************************************************
123 data NameSpace = VarName -- Variables, including "real" data constructors
124 | DataName -- "Source" data constructors
125 | TvName -- Type variables
126 | TcClsName -- Type constructors and classes; Haskell has them
127 -- in the same name space for now.
129 {-! derive: Binary !-}
131 -- Note [Data Constructors]
132 -- see also: Note [Data Constructor Naming] in DataCon.lhs
134 -- $real_vs_source_data_constructors
135 -- There are two forms of data constructor:
137 -- [Source data constructors] The data constructors mentioned in Haskell source code
139 -- [Real data constructors] The data constructors of the representation type, which may not be the same as the source type
143 -- > data T = T !(Int, Int)
145 -- The source datacon has type @(Int, Int) -> T@
146 -- The real datacon has type @Int -> Int -> T@
148 -- GHC chooses a representation based on the strictness etc.
150 tcName, clsName, tcClsName :: NameSpace
151 dataName, srcDataName :: NameSpace
152 tvName, varName :: NameSpace
154 -- Though type constructors and classes are in the same name space now,
155 -- the NameSpace type is abstract, so we can easily separate them later
156 tcName = TcClsName -- Type constructors
157 clsName = TcClsName -- Classes
158 tcClsName = TcClsName -- Not sure which!
161 srcDataName = DataName -- Haskell-source data constructors should be
162 -- in the Data name space
167 isDataConNameSpace :: NameSpace -> Bool
168 isDataConNameSpace DataName = True
169 isDataConNameSpace _ = False
171 isTcClsNameSpace :: NameSpace -> Bool
172 isTcClsNameSpace TcClsName = True
173 isTcClsNameSpace _ = False
175 isTvNameSpace :: NameSpace -> Bool
176 isTvNameSpace TvName = True
177 isTvNameSpace _ = False
179 isVarNameSpace :: NameSpace -> Bool -- Variables or type variables, but not constructors
180 isVarNameSpace TvName = True
181 isVarNameSpace VarName = True
182 isVarNameSpace _ = False
184 isValNameSpace :: NameSpace -> Bool
185 isValNameSpace DataName = True
186 isValNameSpace VarName = True
187 isValNameSpace _ = False
189 pprNameSpace :: NameSpace -> SDoc
190 pprNameSpace DataName = ptext (sLit "data constructor")
191 pprNameSpace VarName = ptext (sLit "variable")
192 pprNameSpace TvName = ptext (sLit "type variable")
193 pprNameSpace TcClsName = ptext (sLit "type constructor or class")
195 pprNonVarNameSpace :: NameSpace -> SDoc
196 pprNonVarNameSpace VarName = empty
197 pprNonVarNameSpace ns = pprNameSpace ns
199 pprNameSpaceBrief :: NameSpace -> SDoc
200 pprNameSpaceBrief DataName = char 'd'
201 pprNameSpaceBrief VarName = char 'v'
202 pprNameSpaceBrief TvName = ptext (sLit "tv")
203 pprNameSpaceBrief TcClsName = ptext (sLit "tc")
207 %************************************************************************
209 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
211 %************************************************************************
214 data OccName = OccName
215 { occNameSpace :: !NameSpace
216 , occNameFS :: !FastString
222 instance Eq OccName where
223 (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
225 instance Ord OccName where
226 -- Compares lexicographically, *not* by Unique of the string
227 compare (OccName sp1 s1) (OccName sp2 s2)
228 = (s1 `compare` s2) `thenCmp` (sp1 `compare` sp2)
232 %************************************************************************
234 \subsection{Printing}
236 %************************************************************************
239 instance Outputable OccName where
242 pprOccName :: OccName -> SDoc
243 pprOccName (OccName sp occ)
244 = getPprStyle $ \ sty ->
246 then ftext (zEncodeFS occ)
247 else ftext occ <> if debugStyle sty
248 then braces (pprNameSpaceBrief sp)
253 %************************************************************************
255 \subsection{Construction}
257 %************************************************************************
260 mkOccName :: NameSpace -> String -> OccName
261 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
263 mkOccNameFS :: NameSpace -> FastString -> OccName
264 mkOccNameFS occ_sp fs = OccName occ_sp fs
266 mkVarOcc :: String -> OccName
267 mkVarOcc s = mkOccName varName s
269 mkVarOccFS :: FastString -> OccName
270 mkVarOccFS fs = mkOccNameFS varName fs
272 mkDataOcc :: String -> OccName
273 mkDataOcc = mkOccName dataName
275 mkDataOccFS :: FastString -> OccName
276 mkDataOccFS = mkOccNameFS dataName
278 mkTyVarOcc :: String -> OccName
279 mkTyVarOcc = mkOccName tvName
281 mkTyVarOccFS :: FastString -> OccName
282 mkTyVarOccFS fs = mkOccNameFS tvName fs
284 mkTcOcc :: String -> OccName
285 mkTcOcc = mkOccName tcName
287 mkTcOccFS :: FastString -> OccName
288 mkTcOccFS = mkOccNameFS tcName
290 mkClsOcc :: String -> OccName
291 mkClsOcc = mkOccName clsName
293 mkClsOccFS :: FastString -> OccName
294 mkClsOccFS = mkOccNameFS clsName
298 %************************************************************************
302 %************************************************************************
304 OccEnvs are used mainly for the envts in ModIfaces.
306 Note [The Unique of an OccName]
307 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308 They are efficient, because FastStrings have unique Int# keys. We assume
309 this key is less than 2^24, and indeed FastStrings are allocated keys
310 sequentially starting at 0.
312 So we can make a Unique using
313 mkUnique ns key :: Unique
314 where 'ns' is a Char reprsenting the name space. This in turn makes it
315 easy to build an OccEnv.
318 instance Uniquable OccName where
319 -- See Note [The Unique of an OccName]
320 getUnique (OccName VarName fs) = mkVarOccUnique fs
321 getUnique (OccName DataName fs) = mkDataOccUnique fs
322 getUnique (OccName TvName fs) = mkTvOccUnique fs
323 getUnique (OccName TcClsName fs) = mkTcOccUnique fs
325 newtype OccEnv a = A (UniqFM a)
327 emptyOccEnv :: OccEnv a
328 unitOccEnv :: OccName -> a -> OccEnv a
329 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
330 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
331 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
332 mkOccEnv :: [(OccName,a)] -> OccEnv a
333 mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
334 elemOccEnv :: OccName -> OccEnv a -> Bool
335 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
336 occEnvElts :: OccEnv a -> [a]
337 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
338 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
339 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
340 mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
341 delFromOccEnv :: OccEnv a -> OccName -> OccEnv a
342 delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
343 filterOccEnv :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
345 emptyOccEnv = A emptyUFM
346 unitOccEnv x y = A $ unitUFM x y
347 extendOccEnv (A x) y z = A $ addToUFM x y z
348 extendOccEnvList (A x) l = A $ addListToUFM x l
349 lookupOccEnv (A x) y = lookupUFM x y
350 mkOccEnv l = A $ listToUFM l
351 elemOccEnv x (A y) = elemUFM x y
352 foldOccEnv a b (A c) = foldUFM a b c
353 occEnvElts (A x) = eltsUFM x
354 plusOccEnv (A x) (A y) = A $ plusUFM x y
355 plusOccEnv_C f (A x) (A y) = A $ plusUFM_C f x y
356 extendOccEnv_C f (A x) y z = A $ addToUFM_C f x y z
357 mapOccEnv f (A x) = A $ mapUFM f x
358 mkOccEnv_C comb l = A $ addListToUFM_C comb emptyUFM l
359 delFromOccEnv (A x) y = A $ delFromUFM x y
360 delListFromOccEnv (A x) y = A $ delListFromUFM x y
361 filterOccEnv x (A y) = A $ filterUFM x y
363 instance Outputable a => Outputable (OccEnv a) where
366 type OccSet = UniqSet OccName
368 emptyOccSet :: OccSet
369 unitOccSet :: OccName -> OccSet
370 mkOccSet :: [OccName] -> OccSet
371 extendOccSet :: OccSet -> OccName -> OccSet
372 extendOccSetList :: OccSet -> [OccName] -> OccSet
373 unionOccSets :: OccSet -> OccSet -> OccSet
374 unionManyOccSets :: [OccSet] -> OccSet
375 minusOccSet :: OccSet -> OccSet -> OccSet
376 elemOccSet :: OccName -> OccSet -> Bool
377 occSetElts :: OccSet -> [OccName]
378 foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
379 isEmptyOccSet :: OccSet -> Bool
380 intersectOccSet :: OccSet -> OccSet -> OccSet
381 intersectsOccSet :: OccSet -> OccSet -> Bool
383 emptyOccSet = emptyUniqSet
384 unitOccSet = unitUniqSet
386 extendOccSet = addOneToUniqSet
387 extendOccSetList = addListToUniqSet
388 unionOccSets = unionUniqSets
389 unionManyOccSets = unionManyUniqSets
390 minusOccSet = minusUniqSet
391 elemOccSet = elementOfUniqSet
392 occSetElts = uniqSetToList
393 foldOccSet = foldUniqSet
394 isEmptyOccSet = isEmptyUniqSet
395 intersectOccSet = intersectUniqSets
396 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
400 %************************************************************************
402 \subsection{Predicates and taking them apart}
404 %************************************************************************
407 occNameString :: OccName -> String
408 occNameString (OccName _ s) = unpackFS s
410 setOccNameSpace :: NameSpace -> OccName -> OccName
411 setOccNameSpace sp (OccName _ occ) = OccName sp occ
413 isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
415 isVarOcc (OccName VarName _) = True
418 isTvOcc (OccName TvName _) = True
421 isTcOcc (OccName TcClsName _) = True
424 -- | /Value/ 'OccNames's are those that are either in
425 -- the variable or data constructor namespaces
426 isValOcc :: OccName -> Bool
427 isValOcc (OccName VarName _) = True
428 isValOcc (OccName DataName _) = True
431 isDataOcc (OccName DataName _) = True
432 isDataOcc (OccName VarName s)
433 | isLexCon s = pprPanic "isDataOcc: check me" (ppr s)
434 -- Jan06: I don't think this should happen
437 -- | Test if the 'OccName' is a data constructor that starts with
438 -- a symbol (e.g. @:@, or @[]@)
439 isDataSymOcc :: OccName -> Bool
440 isDataSymOcc (OccName DataName s) = isLexConSym s
441 isDataSymOcc (OccName VarName s)
442 | isLexConSym s = pprPanic "isDataSymOcc: check me" (ppr s)
443 -- Jan06: I don't think this should happen
444 isDataSymOcc _ = False
445 -- Pretty inefficient!
447 -- | Test if the 'OccName' is that for any operator (whether
448 -- it is a data constructor or variable or whatever)
449 isSymOcc :: OccName -> Bool
450 isSymOcc (OccName DataName s) = isLexConSym s
451 isSymOcc (OccName TcClsName s) = isLexConSym s
452 isSymOcc (OccName VarName s) = isLexSym s
453 isSymOcc (OccName TvName s) = isLexSym s
454 -- Pretty inefficient!
456 parenSymOcc :: OccName -> SDoc -> SDoc
457 -- ^ Wrap parens around an operator
458 parenSymOcc occ doc | isSymOcc occ = parens doc
464 startsWithUnderscore :: OccName -> Bool
465 -- ^ Haskell 98 encourages compilers to suppress warnings about unsed
466 -- names in a pattern if they start with @_@: this implements that test
467 startsWithUnderscore occ = case occNameString occ of
473 %************************************************************************
475 \subsection{Making system names}
477 %************************************************************************
479 Here's our convention for splitting up the interface file name space:
481 d... dictionary identifiers
482 (local variables, so no name-clash worries)
484 All of these other OccNames contain a mixture of alphabetic
485 and symbolic characters, and hence cannot possibly clash with
486 a user-written type or function name
488 $f... Dict-fun identifiers (from inst decls)
489 $dmop Default method for 'op'
490 $pnC n'th superclass selector for class C
491 $wf Worker for functtoin 'f'
492 $sf.. Specialised version of f
493 T:C Tycon for dictionary for class C
494 D:C Data constructor for dictionary for class C
495 NTCo:T Coercion connecting newtype T with its representation type
496 TFCo:R Coercion connecting a data family to its respresentation type R
498 In encoded form these appear as Zdfxxx etc
500 :... keywords (export:, letrec: etc.)
501 --- I THINK THIS IS WRONG!
503 This knowledge is encoded in the following functions.
505 @mk_deriv@ generates an @OccName@ from the prefix and a string.
506 NB: The string must already be encoded!
509 mk_deriv :: NameSpace
510 -> String -- Distinguishes one sort of derived name from another
514 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
516 isDerivedOccName :: OccName -> Bool
517 isDerivedOccName occ =
518 case occNameString occ of
519 '$':c:_ | isAlphaNum c -> True
520 ':':c:_ | isAlphaNum c -> True
525 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
526 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
527 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
528 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
529 mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
530 mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
531 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
532 mkPDataTyConOcc, mkPDataDataConOcc, mkPReprTyConOcc, mkPADFunOcc
533 :: OccName -> OccName
535 -- These derived variables have a prefix that no Haskell value could have
536 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
537 mkWorkerOcc = mk_simple_deriv varName "$w"
538 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
539 mkClassOpAuxOcc = mk_simple_deriv varName "$c"
540 mkDerivedTyConOcc = mk_simple_deriv tcName ":" -- The : prefix makes sure it classifies
541 mkClassTyConOcc = mk_simple_deriv tcName "T:" -- as a tycon/datacon
542 mkClassDataConOcc = mk_simple_deriv dataName "D:" -- We go straight to the "real" data con
543 -- for datacons from classes
544 mkDictOcc = mk_simple_deriv varName "$d"
545 mkIPOcc = mk_simple_deriv varName "$i"
546 mkSpecOcc = mk_simple_deriv varName "$s"
547 mkForeignExportOcc = mk_simple_deriv varName "$f"
548 mkNewTyCoOcc = mk_simple_deriv tcName "NTCo:" -- Coercion for newtypes
549 mkInstTyCoOcc = mk_simple_deriv tcName "TFCo:" -- Coercion for type functions
550 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
552 -- used in derived instances
553 mkCon2TagOcc = mk_simple_deriv varName "$con2tag_"
554 mkTag2ConOcc = mk_simple_deriv varName "$tag2con_"
555 mkMaxTagOcc = mk_simple_deriv varName "$maxtag_"
557 -- Generic derivable classes
558 mkGenOcc1 = mk_simple_deriv varName "$gfrom"
559 mkGenOcc2 = mk_simple_deriv varName "$gto"
561 -- data T = MkT ... deriving( Data ) needs defintions for
562 -- $tT :: Data.Generics.Basics.DataType
563 -- $cMkT :: Data.Generics.Basics.Constr
564 mkDataTOcc = mk_simple_deriv varName "$t"
565 mkDataCOcc = mk_simple_deriv varName "$c"
568 mkVectOcc = mk_simple_deriv varName "$v_"
569 mkVectTyConOcc = mk_simple_deriv tcName ":V_"
570 mkVectDataConOcc = mk_simple_deriv dataName ":VD_"
571 mkVectIsoOcc = mk_simple_deriv varName "$VI_"
572 mkPDataTyConOcc = mk_simple_deriv tcName ":VP_"
573 mkPDataDataConOcc = mk_simple_deriv dataName ":VPD_"
574 mkPReprTyConOcc = mk_simple_deriv tcName ":VR_"
575 mkPADFunOcc = mk_simple_deriv varName "$PA_"
577 mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
578 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
580 -- Data constructor workers are made by setting the name space
581 -- of the data constructor OccName (which should be a DataName)
583 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
587 mkSuperDictSelOcc :: Int -- ^ Index of superclass, e.g. 3
588 -> OccName -- ^ Class, e.g. @Ord@
589 -> OccName -- ^ Derived 'Occname', e.g. @$p3Ord@
590 mkSuperDictSelOcc index cls_occ
591 = mk_deriv varName "$p" (show index ++ occNameString cls_occ)
593 mkLocalOcc :: Unique -- ^ Unique to combine with the 'OccName'
594 -> OccName -- ^ Local name, e.g. @sat@
595 -> OccName -- ^ Nice unique version, e.g. @$L23sat@
597 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
598 -- The Unique might print with characters
599 -- that need encoding (e.g. 'z'!)
603 -- | Derive a name for the representation type constructor of a
604 -- @data@\/@newtype@ instance.
605 mkInstTyTcOcc :: String -- ^ Family name, e.g. @Map@
606 -> OccSet -- ^ avoid these Occs
607 -> OccName -- ^ @R:Map@
608 mkInstTyTcOcc str set =
609 chooseUniqueOcc tcName ('R' : ':' : str) set
613 mkDFunOcc :: String -- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
614 -- Only used in debug mode, for extra clarity
615 -> Bool -- ^ Is this a hs-boot instance DFun?
616 -> OccSet -- ^ avoid these Occs
617 -> OccName -- ^ E.g. @$f3OrdMaybe@
619 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
620 -- thing when we compile the mother module. Reason: we don't know exactly
621 -- what the mother module will call it.
623 mkDFunOcc info_str is_boot set
624 = chooseUniqueOcc VarName (prefix ++ info_str) set
626 prefix | is_boot = "$fx"
630 Sometimes we need to pick an OccName that has not already been used,
631 given a set of in-use OccNames.
634 chooseUniqueOcc :: NameSpace -> String -> OccSet -> OccName
635 chooseUniqueOcc ns str set = loop (mkOccName ns str) (0::Int)
638 | occ `elemOccSet` set = loop (mkOccName ns (str ++ show n)) (n+1)
642 We used to add a '$m' to indicate a method, but that gives rise to bad
643 error messages from the type checker when we print the function name or pattern
644 of an instance-decl binding. Why? Because the binding is zapped
645 to use the method name in place of the selector name.
646 (See TcClassDcl.tcMethodBind)
648 The way it is now, -ddump-xx output may look confusing, but
649 you can always say -dppr-debug to get the uniques.
651 However, we *do* have to zap the first character to be lower case,
652 because overloaded constructors (blarg) generate methods too.
653 And convert to VarName space
655 e.g. a call to constructor MkFoo where
656 data (Ord a) => Foo a = MkFoo a
658 If this is necessary, we do it by prefixing '$m'. These
659 guys never show up in error messages. What a hack.
662 mkMethodOcc :: OccName -> OccName
663 mkMethodOcc occ@(OccName VarName _) = occ
664 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
668 %************************************************************************
670 \subsection{Tidying them up}
672 %************************************************************************
674 Before we print chunks of code we like to rename it so that
675 we don't have to print lots of silly uniques in it. But we mustn't
676 accidentally introduce name clashes! So the idea is that we leave the
677 OccName alone unless it accidentally clashes with one that is already
678 in scope; if so, we tack on '1' at the end and try again, then '2', and
679 so on till we find a unique one.
681 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
682 because that isn't a single lexeme. So we encode it to 'lle' and *then*
683 tack on the '1', if necessary.
686 type TidyOccEnv = OccEnv Int -- The in-scope OccNames
687 -- Range gives a plausible starting point for new guesses
689 emptyTidyOccEnv :: TidyOccEnv
690 emptyTidyOccEnv = emptyOccEnv
692 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
693 initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
695 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
697 tidyOccName in_scope occ@(OccName occ_sp fs)
698 = case lookupOccEnv in_scope occ of
699 Nothing -> -- Not already used: make it used
700 (extendOccEnv in_scope occ 1, occ)
702 Just n -> -- Already used: make a new guess,
703 -- change the guess base, and try again
704 tidyOccName (extendOccEnv in_scope occ (n+1))
705 (mkOccName occ_sp (unpackFS fs ++ show n))
708 %************************************************************************
710 Stuff for dealing with tuples
712 %************************************************************************
715 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
716 mkTupleOcc ns bx ar = OccName ns (mkFastString str)
718 -- no need to cache these, the caching is done in the caller
719 -- (TysWiredIn.mk_tuple)
721 Boxed -> '(' : commas ++ ")"
722 Unboxed -> '(' : '#' : commas ++ "#)"
724 commas = take (ar-1) (repeat ',')
726 isTupleOcc_maybe :: OccName -> Maybe (NameSpace, Boxity, Arity)
727 -- Tuples are special, because there are so many of them!
728 isTupleOcc_maybe (OccName ns fs)
729 = case unpackFS fs of
730 '(':'#':',':rest -> Just (ns, Unboxed, 2 + count_commas rest)
731 '(':',':rest -> Just (ns, Boxed, 2 + count_commas rest)
734 count_commas (',':rest) = 1 + count_commas rest
738 %************************************************************************
740 \subsection{Lexical categories}
742 %************************************************************************
744 These functions test strings to see if they fit the lexical categories
745 defined in the Haskell report.
748 isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
749 isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
751 isLexCon cs = isLexConId cs || isLexConSym cs
752 isLexVar cs = isLexVarId cs || isLexVarSym cs
754 isLexId cs = isLexConId cs || isLexVarId cs
755 isLexSym cs = isLexConSym cs || isLexVarSym cs
759 isLexConId cs -- Prefix type or data constructors
760 | nullFS cs = False -- e.g. "Foo", "[]", "(,)"
761 | cs == (fsLit "[]") = True
762 | otherwise = startsConId (headFS cs)
764 isLexVarId cs -- Ordinary prefix identifiers
765 | nullFS cs = False -- e.g. "x", "_x"
766 | otherwise = startsVarId (headFS cs)
768 isLexConSym cs -- Infix type or data constructors
769 | nullFS cs = False -- e.g. ":-:", ":", "->"
770 | cs == (fsLit "->") = True
771 | otherwise = startsConSym (headFS cs)
773 isLexVarSym cs -- Infix identifiers
774 | nullFS cs = False -- e.g. "+"
775 | otherwise = startsVarSym (headFS cs)
778 startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
779 startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
780 startsConSym c = c == ':' -- Infix data constructors
781 startsVarId c = isLower c || c == '_' -- Ordinary Ids
782 startsConId c = isUpper c || c == '(' -- Ordinary type constructors and data constructors
784 isSymbolASCII :: Char -> Bool
785 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
788 %************************************************************************
791 Here rather than BinIface because OccName is abstract
793 %************************************************************************
796 instance Binary NameSpace where
799 put_ bh DataName = do
803 put_ bh TcClsName = do
808 0 -> do return VarName
809 1 -> do return DataName
810 2 -> do return TvName
811 _ -> do return TcClsName
813 instance Binary OccName where
814 put_ bh (OccName aa ab) = do
820 return (OccName aa ab)