1 {-% DrIFT (Automatic class derivations for Haskell) v1.1 %-}
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 \section[OccName]{@OccName@}
10 -- * The NameSpace type; abstact
11 NameSpace, tcName, clsName, tcClsName, dataName, varName,
15 pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
18 OccName, -- Abstract, instance of Outputable
22 mkOccName, mkOccNameFS,
29 -- ** Derived OccNames
30 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
31 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
32 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
33 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
34 mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc,
37 occNameFS, occNameString, occNameSpace,
39 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
40 parenSymOcc, reportIfUnused, isTcClsName, isVarName,
45 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv,
46 lookupOccEnv, mkOccEnv, extendOccEnvList, elemOccEnv,
47 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
50 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
52 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
53 foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
56 TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
58 -- The basic form of names
59 isLexCon, isLexVar, isLexId, isLexSym,
60 isLexConId, isLexConSym, isLexVarId, isLexVarSym,
61 startsVarSym, startsVarId, startsConSym, startsConId
64 #include "HsVersions.h"
66 import Util ( thenCmp )
67 import Unique ( Unique, mkUnique, Uniquable(..) )
68 import BasicTypes ( Boxity(..), Arity )
69 import StaticFlags ( opt_PprStyle_Debug )
78 import Data.Char ( isUpper, isLower, ord )
80 -- Unicode TODO: put isSymbol in libcompat
81 #if __GLASGOW_HASKELL__ > 604
82 import Data.Char ( isSymbol )
84 isSymbol = const False
89 %************************************************************************
91 \subsection{Name space}
93 %************************************************************************
96 data NameSpace = VarName -- Variables, including "source" data constructors
97 | DataName -- "Real" data constructors
98 | TvName -- Type variables
99 | TcClsName -- Type constructors and classes; Haskell has them
100 -- in the same name space for now.
102 {-! derive: Binary !-}
104 -- Note [Data Constructors]
105 -- see also: Note [Data Constructor Naming] in DataCon.lhs
107 -- "Source" data constructors are the data constructors mentioned
108 -- in Haskell source code
110 -- "Real" data constructors are the data constructors of the
111 -- representation type, which may not be the same as the source
115 -- data T = T !(Int,Int)
117 -- The source datacon has type (Int,Int) -> T
118 -- The real datacon has type Int -> Int -> T
119 -- GHC chooses a representation based on the strictness etc.
122 -- Though type constructors and classes are in the same name space now,
123 -- the NameSpace type is abstract, so we can easily separate them later
124 tcName = TcClsName -- Type constructors
125 clsName = TcClsName -- Classes
126 tcClsName = TcClsName -- Not sure which!
129 srcDataName = DataName -- Haskell-source data constructors should be
130 -- in the Data name space
135 isTcClsName :: NameSpace -> Bool
136 isTcClsName TcClsName = True
137 isTcClsName _ = False
139 isVarName :: NameSpace -> Bool -- Variables or type variables, but not constructors
140 isVarName TvName = True
141 isVarName VarName = True
142 isVarName other = False
144 pprNameSpace :: NameSpace -> SDoc
145 pprNameSpace DataName = ptext SLIT("data constructor")
146 pprNameSpace VarName = ptext SLIT("variable")
147 pprNameSpace TvName = ptext SLIT("type variable")
148 pprNameSpace TcClsName = ptext SLIT("type constructor or class")
150 pprNonVarNameSpace :: NameSpace -> SDoc
151 pprNonVarNameSpace VarName = empty
152 pprNonVarNameSpace ns = pprNameSpace ns
154 pprNameSpaceBrief DataName = char 'd'
155 pprNameSpaceBrief VarName = char 'v'
156 pprNameSpaceBrief TvName = ptext SLIT("tv")
157 pprNameSpaceBrief TcClsName = ptext SLIT("tc")
161 %************************************************************************
163 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
165 %************************************************************************
168 data OccName = OccName
169 { occNameSpace :: !NameSpace
170 , occNameFS :: !FastString
176 instance Eq OccName where
177 (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
179 instance Ord OccName where
180 compare (OccName sp1 s1) (OccName sp2 s2) = (s1 `compare` s2) `thenCmp`
185 %************************************************************************
187 \subsection{Printing}
189 %************************************************************************
192 instance Outputable OccName where
195 pprOccName :: OccName -> SDoc
196 pprOccName (OccName sp occ)
197 = getPprStyle $ \ sty ->
199 then ftext (zEncodeFS occ)
200 else ftext occ <> if debugStyle sty
201 then braces (pprNameSpaceBrief sp)
206 %************************************************************************
208 \subsection{Construction}
210 %************************************************************************
213 mkOccName :: NameSpace -> String -> OccName
214 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
216 mkOccNameFS :: NameSpace -> FastString -> OccName
217 mkOccNameFS occ_sp fs = OccName occ_sp fs
219 mkVarOcc :: String -> OccName
220 mkVarOcc s = mkOccName varName s
222 mkVarOccFS :: FastString -> OccName
223 mkVarOccFS fs = mkOccNameFS varName fs
225 mkTyVarOcc :: FastString -> OccName
226 mkTyVarOcc fs = mkOccNameFS tvName fs
230 %************************************************************************
234 %************************************************************************
236 OccEnvs are used mainly for the envts in ModIfaces.
238 They are efficient, because FastStrings have unique Int# keys. We assume
239 this key is less than 2^24, so we can make a Unique using
240 mkUnique ns key :: Unique
241 where 'ns' is a Char reprsenting the name space. This in turn makes it
242 easy to build an OccEnv.
245 instance Uniquable OccName where
246 getUnique (OccName ns fs)
247 = mkUnique char (I# (uniqueOfFS fs))
248 where -- See notes above about this getUnique function
255 type OccEnv a = UniqFM a
257 emptyOccEnv :: OccEnv a
258 unitOccEnv :: OccName -> a -> OccEnv a
259 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
260 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
261 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
262 mkOccEnv :: [(OccName,a)] -> OccEnv a
263 elemOccEnv :: OccName -> OccEnv a -> Bool
264 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
265 occEnvElts :: OccEnv a -> [a]
266 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
267 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
268 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
270 emptyOccEnv = emptyUFM
272 extendOccEnv = addToUFM
273 extendOccEnvList = addListToUFM
274 lookupOccEnv = lookupUFM
280 plusOccEnv_C = plusUFM_C
281 extendOccEnv_C = addToUFM_C
284 type OccSet = UniqFM OccName
286 emptyOccSet :: OccSet
287 unitOccSet :: OccName -> OccSet
288 mkOccSet :: [OccName] -> OccSet
289 extendOccSet :: OccSet -> OccName -> OccSet
290 extendOccSetList :: OccSet -> [OccName] -> OccSet
291 unionOccSets :: OccSet -> OccSet -> OccSet
292 unionManyOccSets :: [OccSet] -> OccSet
293 minusOccSet :: OccSet -> OccSet -> OccSet
294 elemOccSet :: OccName -> OccSet -> Bool
295 occSetElts :: OccSet -> [OccName]
296 foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
297 isEmptyOccSet :: OccSet -> Bool
298 intersectOccSet :: OccSet -> OccSet -> OccSet
299 intersectsOccSet :: OccSet -> OccSet -> Bool
301 emptyOccSet = emptyUniqSet
302 unitOccSet = unitUniqSet
304 extendOccSet = addOneToUniqSet
305 extendOccSetList = addListToUniqSet
306 unionOccSets = unionUniqSets
307 unionManyOccSets = unionManyUniqSets
308 minusOccSet = minusUniqSet
309 elemOccSet = elementOfUniqSet
310 occSetElts = uniqSetToList
311 foldOccSet = foldUniqSet
312 isEmptyOccSet = isEmptyUniqSet
313 intersectOccSet = intersectUniqSets
314 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
318 %************************************************************************
320 \subsection{Predicates and taking them apart}
322 %************************************************************************
325 occNameString :: OccName -> String
326 occNameString (OccName _ s) = unpackFS s
328 setOccNameSpace :: NameSpace -> OccName -> OccName
329 setOccNameSpace sp (OccName _ occ) = OccName sp occ
331 isVarOcc, isTvOcc, isDataSymOcc, isSymOcc, isTcOcc :: OccName -> Bool
333 isVarOcc (OccName VarName _) = True
334 isVarOcc other = False
336 isTvOcc (OccName TvName _) = True
337 isTvOcc other = False
339 isTcOcc (OccName TcClsName _) = True
340 isTcOcc other = False
342 isValOcc (OccName VarName _) = True
343 isValOcc (OccName DataName _) = True
344 isValOcc other = False
346 -- Data constructor operator (starts with ':', or '[]')
347 -- Pretty inefficient!
348 isDataSymOcc (OccName DataName s) = isLexConSym s
349 isDataSymOcc (OccName VarName s) = isLexConSym s
350 isDataSymOcc other = False
352 isDataOcc (OccName DataName _) = True
353 isDataOcc (OccName VarName s) = isLexCon s
354 isDataOcc other = False
356 -- Any operator (data constructor or variable)
357 -- Pretty inefficient!
358 isSymOcc (OccName DataName s) = isLexConSym s
359 isSymOcc (OccName TcClsName s) = isLexConSym s
360 isSymOcc (OccName VarName s) = isLexSym s
361 isSymOcc other = False
363 parenSymOcc :: OccName -> SDoc -> SDoc
364 -- Wrap parens around an operator
365 parenSymOcc occ doc | isSymOcc occ = parens doc
371 reportIfUnused :: OccName -> Bool
372 -- Haskell 98 encourages compilers to suppress warnings about
373 -- unused names in a pattern if they start with "_".
374 reportIfUnused occ = case occNameString occ of
380 %************************************************************************
382 \subsection{Making system names}
384 %************************************************************************
386 Here's our convention for splitting up the interface file name space:
388 d... dictionary identifiers
389 (local variables, so no name-clash worries)
391 $f... dict-fun identifiers (from inst decls)
392 $dm... default methods
393 $p... superclass selectors
395 :T... compiler-generated tycons for dictionaries
396 :D... ...ditto data cons
397 $sf.. specialised version of f
399 in encoded form these appear as Zdfxxx etc
401 :... keywords (export:, letrec: etc.)
402 --- I THINK THIS IS WRONG!
404 This knowledge is encoded in the following functions.
407 @mk_deriv@ generates an @OccName@ from the prefix and a string.
408 NB: The string must already be encoded!
411 mk_deriv :: NameSpace
412 -> String -- Distinguishes one sort of derived name from another
416 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
420 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
421 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
422 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
423 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc
424 :: OccName -> OccName
426 -- These derived variables have a prefix that no Haskell value could have
427 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
428 mkWorkerOcc = mk_simple_deriv varName "$w"
429 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
430 mkDerivedTyConOcc = mk_simple_deriv tcName ":" -- The : prefix makes sure it classifies
431 mkClassTyConOcc = mk_simple_deriv tcName ":T" -- as a tycon/datacon
432 mkClassDataConOcc = mk_simple_deriv dataName ":D" -- We go straight to the "real" data con
433 -- for datacons from classes
434 mkDictOcc = mk_simple_deriv varName "$d"
435 mkIPOcc = mk_simple_deriv varName "$i"
436 mkSpecOcc = mk_simple_deriv varName "$s"
437 mkForeignExportOcc = mk_simple_deriv varName "$f"
439 -- Generic derivable classes
440 mkGenOcc1 = mk_simple_deriv varName "$gfrom"
441 mkGenOcc2 = mk_simple_deriv varName "$gto"
443 -- data T = MkT ... deriving( Data ) needs defintions for
444 -- $tT :: Data.Generics.Basics.DataType
445 -- $cMkT :: Data.Generics.Basics.Constr
446 mkDataTOcc = mk_simple_deriv varName "$t"
447 mkDataCOcc = mk_simple_deriv varName "$c"
449 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
451 -- Data constructor workers are made by setting the name space
452 -- of the data constructor OccName (which should be a DataName)
454 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
458 mkSuperDictSelOcc :: Int -- Index of superclass, eg 3
459 -> OccName -- Class, eg "Ord"
460 -> OccName -- eg "$p3Ord"
461 mkSuperDictSelOcc index cls_occ
462 = mk_deriv varName "$p" (show index ++ occNameString cls_occ)
464 mkLocalOcc :: Unique -- Unique
465 -> OccName -- Local name (e.g. "sat")
466 -> OccName -- Nice unique version ("$L23sat")
468 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
469 -- The Unique might print with characters
470 -- that need encoding (e.g. 'z'!)
475 mkDFunOcc :: String -- Typically the class and type glommed together e.g. "OrdMaybe"
476 -- Only used in debug mode, for extra clarity
477 -> Bool -- True <=> hs-boot instance dfun
478 -> Int -- Unique index
479 -> OccName -- "$f3OrdMaybe"
481 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
482 -- thing when we compile the mother module. Reason: we don't know exactly
483 -- what the mother module will call it.
485 mkDFunOcc info_str is_boot index
486 = mk_deriv VarName prefix string
488 prefix | is_boot = "$fx"
490 string | opt_PprStyle_Debug = show index ++ info_str
491 | otherwise = show index
494 We used to add a '$m' to indicate a method, but that gives rise to bad
495 error messages from the type checker when we print the function name or pattern
496 of an instance-decl binding. Why? Because the binding is zapped
497 to use the method name in place of the selector name.
498 (See TcClassDcl.tcMethodBind)
500 The way it is now, -ddump-xx output may look confusing, but
501 you can always say -dppr-debug to get the uniques.
503 However, we *do* have to zap the first character to be lower case,
504 because overloaded constructors (blarg) generate methods too.
505 And convert to VarName space
507 e.g. a call to constructor MkFoo where
508 data (Ord a) => Foo a = MkFoo a
510 If this is necessary, we do it by prefixing '$m'. These
511 guys never show up in error messages. What a hack.
514 mkMethodOcc :: OccName -> OccName
515 mkMethodOcc occ@(OccName VarName fs) = occ
516 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
520 %************************************************************************
522 \subsection{Tidying them up}
524 %************************************************************************
526 Before we print chunks of code we like to rename it so that
527 we don't have to print lots of silly uniques in it. But we mustn't
528 accidentally introduce name clashes! So the idea is that we leave the
529 OccName alone unless it accidentally clashes with one that is already
530 in scope; if so, we tack on '1' at the end and try again, then '2', and
531 so on till we find a unique one.
533 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
534 because that isn't a single lexeme. So we encode it to 'lle' and *then*
535 tack on the '1', if necessary.
538 type TidyOccEnv = OccEnv Int -- The in-scope OccNames
539 -- Range gives a plausible starting point for new guesses
541 emptyTidyOccEnv = emptyOccEnv
543 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
544 initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
546 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
548 tidyOccName in_scope occ@(OccName occ_sp fs)
549 = case lookupOccEnv in_scope occ of
550 Nothing -> -- Not already used: make it used
551 (extendOccEnv in_scope occ 1, occ)
553 Just n -> -- Already used: make a new guess,
554 -- change the guess base, and try again
555 tidyOccName (extendOccEnv in_scope occ (n+1))
556 (mkOccName occ_sp (unpackFS fs ++ show n))
559 %************************************************************************
561 Stuff for dealing with tuples
563 %************************************************************************
566 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
567 mkTupleOcc ns bx ar = OccName ns (mkFastString str)
569 -- no need to cache these, the caching is done in the caller
570 -- (TysWiredIn.mk_tuple)
572 Boxed -> '(' : commas ++ ")"
573 Unboxed -> '(' : '#' : commas ++ "#)"
575 commas = take (ar-1) (repeat ',')
577 isTupleOcc_maybe :: OccName -> Maybe (NameSpace, Boxity, Arity)
578 -- Tuples are special, because there are so many of them!
579 isTupleOcc_maybe (OccName ns fs)
580 = case unpackFS fs of
581 '(':'#':',':rest -> Just (ns, Unboxed, 2 + count_commas rest)
582 '(':',':rest -> Just (ns, Boxed, 2 + count_commas rest)
585 count_commas (',':rest) = 1 + count_commas rest
589 %************************************************************************
591 \subsection{Lexical categories}
593 %************************************************************************
595 These functions test strings to see if they fit the lexical categories
596 defined in the Haskell report.
599 isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
600 isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
602 isLexCon cs = isLexConId cs || isLexConSym cs
603 isLexVar cs = isLexVarId cs || isLexVarSym cs
605 isLexId cs = isLexConId cs || isLexVarId cs
606 isLexSym cs = isLexConSym cs || isLexVarSym cs
610 isLexConId cs -- Prefix type or data constructors
611 | nullFS cs = False -- e.g. "Foo", "[]", "(,)"
612 | cs == FSLIT("[]") = True
613 | otherwise = startsConId (headFS cs)
615 isLexVarId cs -- Ordinary prefix identifiers
616 | nullFS cs = False -- e.g. "x", "_x"
617 | otherwise = startsVarId (headFS cs)
619 isLexConSym cs -- Infix type or data constructors
620 | nullFS cs = False -- e.g. ":-:", ":", "->"
621 | cs == FSLIT("->") = True
622 | otherwise = startsConSym (headFS cs)
624 isLexVarSym cs -- Infix identifiers
625 | nullFS cs = False -- e.g. "+"
626 | otherwise = startsVarSym (headFS cs)
629 startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
630 startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
631 startsConSym c = c == ':' -- Infix data constructors
632 startsVarId c = isLower c || c == '_' -- Ordinary Ids
633 startsConId c = isUpper c || c == '(' -- Ordinary type constructors and data constructors
635 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
638 %************************************************************************
641 Here rather than BinIface because OccName is abstract
643 %************************************************************************
646 instance Binary NameSpace where
649 put_ bh DataName = do
653 put_ bh TcClsName = do
658 0 -> do return VarName
659 1 -> do return DataName
660 2 -> do return TvName
661 _ -> do return TcClsName
663 instance Binary OccName where
664 put_ bh (OccName aa ab) = do
670 return (OccName aa ab)