2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
15 -- * The NameSpace type; abstact
16 NameSpace, tcName, clsName, tcClsName, dataName, varName,
20 pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
23 OccName, -- Abstract, instance of Outputable
27 mkOccName, mkOccNameFS,
34 -- ** Derived OccNames
35 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc,
36 mkDerivedTyConOcc, mkNewTyCoOcc,
37 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
38 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
39 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
40 mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
41 mkInstTyCoOcc, mkEqPredCoOcc,
42 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
43 mkPArrayTyConOcc, mkPArrayDataConOcc,
48 occNameFS, occNameString, occNameSpace,
50 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
51 parenSymOcc, reportIfUnused, isTcClsName, isVarName,
56 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
57 lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
58 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
61 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
63 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
64 foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
67 TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
69 -- The basic form of names
70 isLexCon, isLexVar, isLexId, isLexSym,
71 isLexConId, isLexConSym, isLexVarId, isLexVarSym,
72 startsVarSym, startsVarId, startsConSym, startsConId
75 #include "HsVersions.h"
90 -- Unicode TODO: put isSymbol in libcompat
91 #if __GLASGOW_HASKELL__ > 604
93 isSymbol = const False
98 %************************************************************************
100 \subsection{Name space}
102 %************************************************************************
105 data NameSpace = VarName -- Variables, including "real" data constructors
106 | DataName -- "Source" data constructors
107 | TvName -- Type variables
108 | TcClsName -- Type constructors and classes; Haskell has them
109 -- in the same name space for now.
111 {-! derive: Binary !-}
113 -- Note [Data Constructors]
114 -- see also: Note [Data Constructor Naming] in DataCon.lhs
116 -- "Source" data constructors are the data constructors mentioned
117 -- in Haskell source code
119 -- "Real" data constructors are the data constructors of the
120 -- representation type, which may not be the same as the source
124 -- data T = T !(Int,Int)
126 -- The source datacon has type (Int,Int) -> T
127 -- The real datacon has type Int -> Int -> T
128 -- GHC chooses a representation based on the strictness etc.
131 -- Though type constructors and classes are in the same name space now,
132 -- the NameSpace type is abstract, so we can easily separate them later
133 tcName = TcClsName -- Type constructors
134 clsName = TcClsName -- Classes
135 tcClsName = TcClsName -- Not sure which!
138 srcDataName = DataName -- Haskell-source data constructors should be
139 -- in the Data name space
144 isTcClsName :: NameSpace -> Bool
145 isTcClsName TcClsName = True
146 isTcClsName _ = False
148 isVarName :: NameSpace -> Bool -- Variables or type variables, but not constructors
149 isVarName TvName = True
150 isVarName VarName = True
151 isVarName other = False
153 pprNameSpace :: NameSpace -> SDoc
154 pprNameSpace DataName = ptext SLIT("data constructor")
155 pprNameSpace VarName = ptext SLIT("variable")
156 pprNameSpace TvName = ptext SLIT("type variable")
157 pprNameSpace TcClsName = ptext SLIT("type constructor or class")
159 pprNonVarNameSpace :: NameSpace -> SDoc
160 pprNonVarNameSpace VarName = empty
161 pprNonVarNameSpace ns = pprNameSpace ns
163 pprNameSpaceBrief DataName = char 'd'
164 pprNameSpaceBrief VarName = char 'v'
165 pprNameSpaceBrief TvName = ptext SLIT("tv")
166 pprNameSpaceBrief TcClsName = ptext SLIT("tc")
170 %************************************************************************
172 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
174 %************************************************************************
177 data OccName = OccName
178 { occNameSpace :: !NameSpace
179 , occNameFS :: !FastString
185 instance Eq OccName where
186 (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
188 instance Ord OccName where
189 -- Compares lexicographically, *not* by Unique of the string
190 compare (OccName sp1 s1) (OccName sp2 s2)
191 = (s1 `compare` s2) `thenCmp` (sp1 `compare` sp2)
195 %************************************************************************
197 \subsection{Printing}
199 %************************************************************************
202 instance Outputable OccName where
205 pprOccName :: OccName -> SDoc
206 pprOccName (OccName sp occ)
207 = getPprStyle $ \ sty ->
209 then ftext (zEncodeFS occ)
210 else ftext occ <> if debugStyle sty
211 then braces (pprNameSpaceBrief sp)
216 %************************************************************************
218 \subsection{Construction}
220 %************************************************************************
223 mkOccName :: NameSpace -> String -> OccName
224 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
226 mkOccNameFS :: NameSpace -> FastString -> OccName
227 mkOccNameFS occ_sp fs = OccName occ_sp fs
229 mkVarOcc :: String -> OccName
230 mkVarOcc s = mkOccName varName s
232 mkVarOccFS :: FastString -> OccName
233 mkVarOccFS fs = mkOccNameFS varName fs
235 mkTyVarOcc :: FastString -> OccName
236 mkTyVarOcc fs = mkOccNameFS tvName fs
240 %************************************************************************
244 %************************************************************************
246 OccEnvs are used mainly for the envts in ModIfaces.
248 They are efficient, because FastStrings have unique Int# keys. We assume
249 this key is less than 2^24, so we can make a Unique using
250 mkUnique ns key :: Unique
251 where 'ns' is a Char reprsenting the name space. This in turn makes it
252 easy to build an OccEnv.
255 instance Uniquable OccName where
256 getUnique (OccName ns fs)
257 = mkUnique char (I# (uniqueOfFS fs))
258 where -- See notes above about this getUnique function
265 type OccEnv a = UniqFM a
267 emptyOccEnv :: OccEnv a
268 unitOccEnv :: OccName -> a -> OccEnv a
269 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
270 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
271 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
272 mkOccEnv :: [(OccName,a)] -> OccEnv a
273 mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
274 elemOccEnv :: OccName -> OccEnv a -> Bool
275 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
276 occEnvElts :: OccEnv a -> [a]
277 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
278 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
279 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
280 mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
282 emptyOccEnv = emptyUFM
284 extendOccEnv = addToUFM
285 extendOccEnvList = addListToUFM
286 lookupOccEnv = lookupUFM
292 plusOccEnv_C = plusUFM_C
293 extendOccEnv_C = addToUFM_C
296 mkOccEnv_C comb l = addListToUFM_C comb emptyOccEnv l
298 type OccSet = UniqFM OccName
300 emptyOccSet :: OccSet
301 unitOccSet :: OccName -> OccSet
302 mkOccSet :: [OccName] -> OccSet
303 extendOccSet :: OccSet -> OccName -> OccSet
304 extendOccSetList :: OccSet -> [OccName] -> OccSet
305 unionOccSets :: OccSet -> OccSet -> OccSet
306 unionManyOccSets :: [OccSet] -> OccSet
307 minusOccSet :: OccSet -> OccSet -> OccSet
308 elemOccSet :: OccName -> OccSet -> Bool
309 occSetElts :: OccSet -> [OccName]
310 foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
311 isEmptyOccSet :: OccSet -> Bool
312 intersectOccSet :: OccSet -> OccSet -> OccSet
313 intersectsOccSet :: OccSet -> OccSet -> Bool
315 emptyOccSet = emptyUniqSet
316 unitOccSet = unitUniqSet
318 extendOccSet = addOneToUniqSet
319 extendOccSetList = addListToUniqSet
320 unionOccSets = unionUniqSets
321 unionManyOccSets = unionManyUniqSets
322 minusOccSet = minusUniqSet
323 elemOccSet = elementOfUniqSet
324 occSetElts = uniqSetToList
325 foldOccSet = foldUniqSet
326 isEmptyOccSet = isEmptyUniqSet
327 intersectOccSet = intersectUniqSets
328 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
332 %************************************************************************
334 \subsection{Predicates and taking them apart}
336 %************************************************************************
339 occNameString :: OccName -> String
340 occNameString (OccName _ s) = unpackFS s
342 setOccNameSpace :: NameSpace -> OccName -> OccName
343 setOccNameSpace sp (OccName _ occ) = OccName sp occ
345 isVarOcc, isTvOcc, isDataSymOcc, isSymOcc, isTcOcc :: OccName -> Bool
347 isVarOcc (OccName VarName _) = True
348 isVarOcc other = False
350 isTvOcc (OccName TvName _) = True
351 isTvOcc other = False
353 isTcOcc (OccName TcClsName _) = True
354 isTcOcc other = False
356 isValOcc (OccName VarName _) = True
357 isValOcc (OccName DataName _) = True
358 isValOcc other = False
360 -- Data constructor operator (starts with ':', or '[]')
361 -- Pretty inefficient!
362 isDataSymOcc (OccName DataName s) = isLexConSym s
363 isDataSymOcc (OccName VarName s)
364 | isLexConSym s = pprPanic "isDataSymOcc: check me" (ppr s)
365 -- Jan06: I don't think this should happen
366 isDataSymOcc other = False
368 isDataOcc (OccName DataName _) = True
369 isDataOcc (OccName VarName s)
370 | isLexCon s = pprPanic "isDataOcc: check me" (ppr s)
371 -- Jan06: I don't think this should happen
372 isDataOcc other = False
374 -- Any operator (data constructor or variable)
375 -- Pretty inefficient!
376 isSymOcc (OccName DataName s) = isLexConSym s
377 isSymOcc (OccName TcClsName s) = isLexConSym s
378 isSymOcc (OccName VarName s) = isLexSym s
379 isSymOcc other = False
381 parenSymOcc :: OccName -> SDoc -> SDoc
382 -- Wrap parens around an operator
383 parenSymOcc occ doc | isSymOcc occ = parens doc
389 reportIfUnused :: OccName -> Bool
390 -- Haskell 98 encourages compilers to suppress warnings about
391 -- unused names in a pattern if they start with "_".
392 reportIfUnused occ = case occNameString occ of
398 %************************************************************************
400 \subsection{Making system names}
402 %************************************************************************
404 Here's our convention for splitting up the interface file name space:
406 d... dictionary identifiers
407 (local variables, so no name-clash worries)
409 $f... dict-fun identifiers (from inst decls)
410 $dm... default methods
411 $p... superclass selectors
413 :T... compiler-generated tycons for dictionaries
414 :D... ...ditto data cons
415 :Co... ...ditto coercions
416 $sf.. specialised version of f
418 in encoded form these appear as Zdfxxx etc
420 :... keywords (export:, letrec: etc.)
421 --- I THINK THIS IS WRONG!
423 This knowledge is encoded in the following functions.
426 @mk_deriv@ generates an @OccName@ from the prefix and a string.
427 NB: The string must already be encoded!
430 mk_deriv :: NameSpace
431 -> String -- Distinguishes one sort of derived name from another
435 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
439 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
440 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
441 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
442 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc
443 :: OccName -> OccName
445 -- These derived variables have a prefix that no Haskell value could have
446 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
447 mkWorkerOcc = mk_simple_deriv varName "$w"
448 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
449 mkDerivedTyConOcc = mk_simple_deriv tcName ":" -- The : prefix makes sure it classifies
450 mkClassTyConOcc = mk_simple_deriv tcName ":T" -- as a tycon/datacon
451 mkClassDataConOcc = mk_simple_deriv dataName ":D" -- We go straight to the "real" data con
452 -- for datacons from classes
453 mkDictOcc = mk_simple_deriv varName "$d"
454 mkIPOcc = mk_simple_deriv varName "$i"
455 mkSpecOcc = mk_simple_deriv varName "$s"
456 mkForeignExportOcc = mk_simple_deriv varName "$f"
457 mkNewTyCoOcc = mk_simple_deriv tcName ":Co"
458 mkInstTyCoOcc = mk_simple_deriv tcName ":CoF" -- derived from rep ty
459 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
461 -- Generic derivable classes
462 mkGenOcc1 = mk_simple_deriv varName "$gfrom"
463 mkGenOcc2 = mk_simple_deriv varName "$gto"
465 -- data T = MkT ... deriving( Data ) needs defintions for
466 -- $tT :: Data.Generics.Basics.DataType
467 -- $cMkT :: Data.Generics.Basics.Constr
468 mkDataTOcc = mk_simple_deriv varName "$t"
469 mkDataCOcc = mk_simple_deriv varName "$c"
472 mkVectOcc = mk_simple_deriv varName "$v_"
473 mkVectTyConOcc = mk_simple_deriv tcName ":V_"
474 mkVectDataConOcc = mk_simple_deriv dataName ":VD_"
475 mkVectIsoOcc = mk_simple_deriv varName "$VI_"
476 mkPArrayTyConOcc = mk_simple_deriv tcName ":VP_"
477 mkPArrayDataConOcc = mk_simple_deriv dataName ":VPD_"
478 mkPReprTyConOcc = mk_simple_deriv tcName ":VR_"
479 mkPADFunOcc = mk_simple_deriv varName "$PA_"
481 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
483 -- Data constructor workers are made by setting the name space
484 -- of the data constructor OccName (which should be a DataName)
486 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
490 mkSuperDictSelOcc :: Int -- Index of superclass, eg 3
491 -> OccName -- Class, eg "Ord"
492 -> OccName -- eg "$p3Ord"
493 mkSuperDictSelOcc index cls_occ
494 = mk_deriv varName "$p" (show index ++ occNameString cls_occ)
496 mkLocalOcc :: Unique -- Unique
497 -> OccName -- Local name (e.g. "sat")
498 -> OccName -- Nice unique version ("$L23sat")
500 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
501 -- The Unique might print with characters
502 -- that need encoding (e.g. 'z'!)
505 Derive a name for the representation type constructor of a data/newtype
509 mkInstTyTcOcc :: Int -- Index
510 -> OccName -- Family name (e.g. "Map")
511 -> OccName -- Nice unique version (":R23Map")
512 mkInstTyTcOcc index occ
513 = mk_deriv tcName (":R" ++ show index) (occNameString occ)
517 mkDFunOcc :: String -- Typically the class and type glommed together e.g. "OrdMaybe"
518 -- Only used in debug mode, for extra clarity
519 -> Bool -- True <=> hs-boot instance dfun
520 -> Int -- Unique index
521 -> OccName -- "$f3OrdMaybe"
523 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
524 -- thing when we compile the mother module. Reason: we don't know exactly
525 -- what the mother module will call it.
527 mkDFunOcc info_str is_boot index
528 = mk_deriv VarName prefix string
530 prefix | is_boot = "$fx"
532 string | opt_PprStyle_Debug = show index ++ info_str
533 | otherwise = show index
536 We used to add a '$m' to indicate a method, but that gives rise to bad
537 error messages from the type checker when we print the function name or pattern
538 of an instance-decl binding. Why? Because the binding is zapped
539 to use the method name in place of the selector name.
540 (See TcClassDcl.tcMethodBind)
542 The way it is now, -ddump-xx output may look confusing, but
543 you can always say -dppr-debug to get the uniques.
545 However, we *do* have to zap the first character to be lower case,
546 because overloaded constructors (blarg) generate methods too.
547 And convert to VarName space
549 e.g. a call to constructor MkFoo where
550 data (Ord a) => Foo a = MkFoo a
552 If this is necessary, we do it by prefixing '$m'. These
553 guys never show up in error messages. What a hack.
556 mkMethodOcc :: OccName -> OccName
557 mkMethodOcc occ@(OccName VarName fs) = occ
558 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
562 %************************************************************************
564 \subsection{Tidying them up}
566 %************************************************************************
568 Before we print chunks of code we like to rename it so that
569 we don't have to print lots of silly uniques in it. But we mustn't
570 accidentally introduce name clashes! So the idea is that we leave the
571 OccName alone unless it accidentally clashes with one that is already
572 in scope; if so, we tack on '1' at the end and try again, then '2', and
573 so on till we find a unique one.
575 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
576 because that isn't a single lexeme. So we encode it to 'lle' and *then*
577 tack on the '1', if necessary.
580 type TidyOccEnv = OccEnv Int -- The in-scope OccNames
581 -- Range gives a plausible starting point for new guesses
583 emptyTidyOccEnv = emptyOccEnv
585 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
586 initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
588 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
590 tidyOccName in_scope occ@(OccName occ_sp fs)
591 = case lookupOccEnv in_scope occ of
592 Nothing -> -- Not already used: make it used
593 (extendOccEnv in_scope occ 1, occ)
595 Just n -> -- Already used: make a new guess,
596 -- change the guess base, and try again
597 tidyOccName (extendOccEnv in_scope occ (n+1))
598 (mkOccName occ_sp (unpackFS fs ++ show n))
601 %************************************************************************
603 Stuff for dealing with tuples
605 %************************************************************************
608 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
609 mkTupleOcc ns bx ar = OccName ns (mkFastString str)
611 -- no need to cache these, the caching is done in the caller
612 -- (TysWiredIn.mk_tuple)
614 Boxed -> '(' : commas ++ ")"
615 Unboxed -> '(' : '#' : commas ++ "#)"
617 commas = take (ar-1) (repeat ',')
619 isTupleOcc_maybe :: OccName -> Maybe (NameSpace, Boxity, Arity)
620 -- Tuples are special, because there are so many of them!
621 isTupleOcc_maybe (OccName ns fs)
622 = case unpackFS fs of
623 '(':'#':',':rest -> Just (ns, Unboxed, 2 + count_commas rest)
624 '(':',':rest -> Just (ns, Boxed, 2 + count_commas rest)
627 count_commas (',':rest) = 1 + count_commas rest
631 %************************************************************************
633 \subsection{Lexical categories}
635 %************************************************************************
637 These functions test strings to see if they fit the lexical categories
638 defined in the Haskell report.
641 isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
642 isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
644 isLexCon cs = isLexConId cs || isLexConSym cs
645 isLexVar cs = isLexVarId cs || isLexVarSym cs
647 isLexId cs = isLexConId cs || isLexVarId cs
648 isLexSym cs = isLexConSym cs || isLexVarSym cs
652 isLexConId cs -- Prefix type or data constructors
653 | nullFS cs = False -- e.g. "Foo", "[]", "(,)"
654 | cs == FSLIT("[]") = True
655 | otherwise = startsConId (headFS cs)
657 isLexVarId cs -- Ordinary prefix identifiers
658 | nullFS cs = False -- e.g. "x", "_x"
659 | otherwise = startsVarId (headFS cs)
661 isLexConSym cs -- Infix type or data constructors
662 | nullFS cs = False -- e.g. ":-:", ":", "->"
663 | cs == FSLIT("->") = True
664 | otherwise = startsConSym (headFS cs)
666 isLexVarSym cs -- Infix identifiers
667 | nullFS cs = False -- e.g. "+"
668 | otherwise = startsVarSym (headFS cs)
671 startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
672 startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
673 startsConSym c = c == ':' -- Infix data constructors
674 startsVarId c = isLower c || c == '_' -- Ordinary Ids
675 startsConId c = isUpper c || c == '(' -- Ordinary type constructors and data constructors
677 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
680 %************************************************************************
683 Here rather than BinIface because OccName is abstract
685 %************************************************************************
688 instance Binary NameSpace where
691 put_ bh DataName = do
695 put_ bh TcClsName = do
700 0 -> do return VarName
701 1 -> do return DataName
702 2 -> do return TvName
703 _ -> do return TcClsName
705 instance Binary OccName where
706 put_ bh (OccName aa ab) = do
712 return (OccName aa ab)