2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 -- * The NameSpace type; abstact
9 NameSpace, tcName, clsName, tcClsName, dataName, varName,
13 pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
16 OccName, -- Abstract, instance of Outputable
20 mkOccName, mkOccNameFS,
27 -- ** Derived OccNames
28 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc,
29 mkDerivedTyConOcc, mkNewTyCoOcc,
30 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
31 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
32 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
33 mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
34 mkInstTyCoOcc, mkEqPredCoOcc,
35 mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
36 mkPArrayTyConOcc, mkPArrayDataConOcc, mkPADFunOcc,
39 occNameFS, occNameString, occNameSpace,
41 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
42 parenSymOcc, reportIfUnused, isTcClsName, isVarName,
47 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
48 lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
49 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
52 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
54 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
55 foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
58 TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
60 -- The basic form of names
61 isLexCon, isLexVar, isLexId, isLexSym,
62 isLexConId, isLexConSym, isLexVarId, isLexVarSym,
63 startsVarSym, startsVarId, startsConSym, startsConId
66 #include "HsVersions.h"
81 -- Unicode TODO: put isSymbol in libcompat
82 #if __GLASGOW_HASKELL__ > 604
84 isSymbol = const False
89 %************************************************************************
91 \subsection{Name space}
93 %************************************************************************
96 data NameSpace = VarName -- Variables, including "real" data constructors
97 | DataName -- "Source" 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 -- Compares lexicographically, *not* by Unique of the string
181 compare (OccName sp1 s1) (OccName sp2 s2)
182 = (s1 `compare` s2) `thenCmp` (sp1 `compare` sp2)
186 %************************************************************************
188 \subsection{Printing}
190 %************************************************************************
193 instance Outputable OccName where
196 pprOccName :: OccName -> SDoc
197 pprOccName (OccName sp occ)
198 = getPprStyle $ \ sty ->
200 then ftext (zEncodeFS occ)
201 else ftext occ <> if debugStyle sty
202 then braces (pprNameSpaceBrief sp)
207 %************************************************************************
209 \subsection{Construction}
211 %************************************************************************
214 mkOccName :: NameSpace -> String -> OccName
215 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
217 mkOccNameFS :: NameSpace -> FastString -> OccName
218 mkOccNameFS occ_sp fs = OccName occ_sp fs
220 mkVarOcc :: String -> OccName
221 mkVarOcc s = mkOccName varName s
223 mkVarOccFS :: FastString -> OccName
224 mkVarOccFS fs = mkOccNameFS varName fs
226 mkTyVarOcc :: FastString -> OccName
227 mkTyVarOcc fs = mkOccNameFS tvName fs
231 %************************************************************************
235 %************************************************************************
237 OccEnvs are used mainly for the envts in ModIfaces.
239 They are efficient, because FastStrings have unique Int# keys. We assume
240 this key is less than 2^24, so we can make a Unique using
241 mkUnique ns key :: Unique
242 where 'ns' is a Char reprsenting the name space. This in turn makes it
243 easy to build an OccEnv.
246 instance Uniquable OccName where
247 getUnique (OccName ns fs)
248 = mkUnique char (I# (uniqueOfFS fs))
249 where -- See notes above about this getUnique function
256 type OccEnv a = UniqFM a
258 emptyOccEnv :: OccEnv a
259 unitOccEnv :: OccName -> a -> OccEnv a
260 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
261 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
262 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
263 mkOccEnv :: [(OccName,a)] -> OccEnv a
264 mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
265 elemOccEnv :: OccName -> OccEnv a -> Bool
266 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
267 occEnvElts :: OccEnv a -> [a]
268 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
269 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
270 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
271 mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
273 emptyOccEnv = emptyUFM
275 extendOccEnv = addToUFM
276 extendOccEnvList = addListToUFM
277 lookupOccEnv = lookupUFM
283 plusOccEnv_C = plusUFM_C
284 extendOccEnv_C = addToUFM_C
287 mkOccEnv_C comb l = addListToUFM_C comb emptyOccEnv l
289 type OccSet = UniqFM OccName
291 emptyOccSet :: OccSet
292 unitOccSet :: OccName -> OccSet
293 mkOccSet :: [OccName] -> OccSet
294 extendOccSet :: OccSet -> OccName -> OccSet
295 extendOccSetList :: OccSet -> [OccName] -> OccSet
296 unionOccSets :: OccSet -> OccSet -> OccSet
297 unionManyOccSets :: [OccSet] -> OccSet
298 minusOccSet :: OccSet -> OccSet -> OccSet
299 elemOccSet :: OccName -> OccSet -> Bool
300 occSetElts :: OccSet -> [OccName]
301 foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
302 isEmptyOccSet :: OccSet -> Bool
303 intersectOccSet :: OccSet -> OccSet -> OccSet
304 intersectsOccSet :: OccSet -> OccSet -> Bool
306 emptyOccSet = emptyUniqSet
307 unitOccSet = unitUniqSet
309 extendOccSet = addOneToUniqSet
310 extendOccSetList = addListToUniqSet
311 unionOccSets = unionUniqSets
312 unionManyOccSets = unionManyUniqSets
313 minusOccSet = minusUniqSet
314 elemOccSet = elementOfUniqSet
315 occSetElts = uniqSetToList
316 foldOccSet = foldUniqSet
317 isEmptyOccSet = isEmptyUniqSet
318 intersectOccSet = intersectUniqSets
319 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
323 %************************************************************************
325 \subsection{Predicates and taking them apart}
327 %************************************************************************
330 occNameString :: OccName -> String
331 occNameString (OccName _ s) = unpackFS s
333 setOccNameSpace :: NameSpace -> OccName -> OccName
334 setOccNameSpace sp (OccName _ occ) = OccName sp occ
336 isVarOcc, isTvOcc, isDataSymOcc, isSymOcc, isTcOcc :: OccName -> Bool
338 isVarOcc (OccName VarName _) = True
339 isVarOcc other = False
341 isTvOcc (OccName TvName _) = True
342 isTvOcc other = False
344 isTcOcc (OccName TcClsName _) = True
345 isTcOcc other = False
347 isValOcc (OccName VarName _) = True
348 isValOcc (OccName DataName _) = True
349 isValOcc other = False
351 -- Data constructor operator (starts with ':', or '[]')
352 -- Pretty inefficient!
353 isDataSymOcc (OccName DataName s) = isLexConSym s
354 isDataSymOcc (OccName VarName s)
355 | isLexConSym s = pprPanic "isDataSymOcc: check me" (ppr s)
356 -- Jan06: I don't think this should happen
357 isDataSymOcc other = False
359 isDataOcc (OccName DataName _) = True
360 isDataOcc (OccName VarName s)
361 | isLexCon s = pprPanic "isDataOcc: check me" (ppr s)
362 -- Jan06: I don't think this should happen
363 isDataOcc other = False
365 -- Any operator (data constructor or variable)
366 -- Pretty inefficient!
367 isSymOcc (OccName DataName s) = isLexConSym s
368 isSymOcc (OccName TcClsName s) = isLexConSym s
369 isSymOcc (OccName VarName s) = isLexSym s
370 isSymOcc other = False
372 parenSymOcc :: OccName -> SDoc -> SDoc
373 -- Wrap parens around an operator
374 parenSymOcc occ doc | isSymOcc occ = parens doc
380 reportIfUnused :: OccName -> Bool
381 -- Haskell 98 encourages compilers to suppress warnings about
382 -- unused names in a pattern if they start with "_".
383 reportIfUnused occ = case occNameString occ of
389 %************************************************************************
391 \subsection{Making system names}
393 %************************************************************************
395 Here's our convention for splitting up the interface file name space:
397 d... dictionary identifiers
398 (local variables, so no name-clash worries)
400 $f... dict-fun identifiers (from inst decls)
401 $dm... default methods
402 $p... superclass selectors
404 :T... compiler-generated tycons for dictionaries
405 :D... ...ditto data cons
406 :Co... ...ditto coercions
407 $sf.. specialised version of f
409 in encoded form these appear as Zdfxxx etc
411 :... keywords (export:, letrec: etc.)
412 --- I THINK THIS IS WRONG!
414 This knowledge is encoded in the following functions.
417 @mk_deriv@ generates an @OccName@ from the prefix and a string.
418 NB: The string must already be encoded!
421 mk_deriv :: NameSpace
422 -> String -- Distinguishes one sort of derived name from another
426 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
430 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
431 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
432 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
433 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc
434 :: OccName -> OccName
436 -- These derived variables have a prefix that no Haskell value could have
437 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
438 mkWorkerOcc = mk_simple_deriv varName "$w"
439 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
440 mkDerivedTyConOcc = mk_simple_deriv tcName ":" -- The : prefix makes sure it classifies
441 mkClassTyConOcc = mk_simple_deriv tcName ":T" -- as a tycon/datacon
442 mkClassDataConOcc = mk_simple_deriv dataName ":D" -- We go straight to the "real" data con
443 -- for datacons from classes
444 mkDictOcc = mk_simple_deriv varName "$d"
445 mkIPOcc = mk_simple_deriv varName "$i"
446 mkSpecOcc = mk_simple_deriv varName "$s"
447 mkForeignExportOcc = mk_simple_deriv varName "$f"
448 mkNewTyCoOcc = mk_simple_deriv tcName ":Co"
449 mkInstTyCoOcc = mk_simple_deriv tcName ":CoF" -- derived from rep ty
450 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
452 -- Generic derivable classes
453 mkGenOcc1 = mk_simple_deriv varName "$gfrom"
454 mkGenOcc2 = mk_simple_deriv varName "$gto"
456 -- data T = MkT ... deriving( Data ) needs defintions for
457 -- $tT :: Data.Generics.Basics.DataType
458 -- $cMkT :: Data.Generics.Basics.Constr
459 mkDataTOcc = mk_simple_deriv varName "$t"
460 mkDataCOcc = mk_simple_deriv varName "$c"
463 mkVectOcc = mk_simple_deriv varName "$v_"
464 mkVectTyConOcc = mk_simple_deriv tcName ":V_"
465 mkVectDataConOcc = mk_simple_deriv dataName ":VD_"
466 mkVectIsoOcc = mk_simple_deriv varName "$VI_"
467 mkPArrayTyConOcc = mk_simple_deriv tcName ":VP_"
468 mkPArrayDataConOcc = mk_simple_deriv dataName ":VPD_"
469 mkPADFunOcc = mk_simple_deriv varName "$PA_"
471 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
473 -- Data constructor workers are made by setting the name space
474 -- of the data constructor OccName (which should be a DataName)
476 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
480 mkSuperDictSelOcc :: Int -- Index of superclass, eg 3
481 -> OccName -- Class, eg "Ord"
482 -> OccName -- eg "$p3Ord"
483 mkSuperDictSelOcc index cls_occ
484 = mk_deriv varName "$p" (show index ++ occNameString cls_occ)
486 mkLocalOcc :: Unique -- Unique
487 -> OccName -- Local name (e.g. "sat")
488 -> OccName -- Nice unique version ("$L23sat")
490 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
491 -- The Unique might print with characters
492 -- that need encoding (e.g. 'z'!)
495 Derive a name for the representation type constructor of a data/newtype
499 mkInstTyTcOcc :: Int -- Index
500 -> OccName -- Family name (e.g. "Map")
501 -> OccName -- Nice unique version (":R23Map")
502 mkInstTyTcOcc index occ
503 = mk_deriv tcName (":R" ++ show index) (occNameString occ)
507 mkDFunOcc :: String -- Typically the class and type glommed together e.g. "OrdMaybe"
508 -- Only used in debug mode, for extra clarity
509 -> Bool -- True <=> hs-boot instance dfun
510 -> Int -- Unique index
511 -> OccName -- "$f3OrdMaybe"
513 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
514 -- thing when we compile the mother module. Reason: we don't know exactly
515 -- what the mother module will call it.
517 mkDFunOcc info_str is_boot index
518 = mk_deriv VarName prefix string
520 prefix | is_boot = "$fx"
522 string | opt_PprStyle_Debug = show index ++ info_str
523 | otherwise = show index
526 We used to add a '$m' to indicate a method, but that gives rise to bad
527 error messages from the type checker when we print the function name or pattern
528 of an instance-decl binding. Why? Because the binding is zapped
529 to use the method name in place of the selector name.
530 (See TcClassDcl.tcMethodBind)
532 The way it is now, -ddump-xx output may look confusing, but
533 you can always say -dppr-debug to get the uniques.
535 However, we *do* have to zap the first character to be lower case,
536 because overloaded constructors (blarg) generate methods too.
537 And convert to VarName space
539 e.g. a call to constructor MkFoo where
540 data (Ord a) => Foo a = MkFoo a
542 If this is necessary, we do it by prefixing '$m'. These
543 guys never show up in error messages. What a hack.
546 mkMethodOcc :: OccName -> OccName
547 mkMethodOcc occ@(OccName VarName fs) = occ
548 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
552 %************************************************************************
554 \subsection{Tidying them up}
556 %************************************************************************
558 Before we print chunks of code we like to rename it so that
559 we don't have to print lots of silly uniques in it. But we mustn't
560 accidentally introduce name clashes! So the idea is that we leave the
561 OccName alone unless it accidentally clashes with one that is already
562 in scope; if so, we tack on '1' at the end and try again, then '2', and
563 so on till we find a unique one.
565 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
566 because that isn't a single lexeme. So we encode it to 'lle' and *then*
567 tack on the '1', if necessary.
570 type TidyOccEnv = OccEnv Int -- The in-scope OccNames
571 -- Range gives a plausible starting point for new guesses
573 emptyTidyOccEnv = emptyOccEnv
575 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
576 initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
578 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
580 tidyOccName in_scope occ@(OccName occ_sp fs)
581 = case lookupOccEnv in_scope occ of
582 Nothing -> -- Not already used: make it used
583 (extendOccEnv in_scope occ 1, occ)
585 Just n -> -- Already used: make a new guess,
586 -- change the guess base, and try again
587 tidyOccName (extendOccEnv in_scope occ (n+1))
588 (mkOccName occ_sp (unpackFS fs ++ show n))
591 %************************************************************************
593 Stuff for dealing with tuples
595 %************************************************************************
598 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
599 mkTupleOcc ns bx ar = OccName ns (mkFastString str)
601 -- no need to cache these, the caching is done in the caller
602 -- (TysWiredIn.mk_tuple)
604 Boxed -> '(' : commas ++ ")"
605 Unboxed -> '(' : '#' : commas ++ "#)"
607 commas = take (ar-1) (repeat ',')
609 isTupleOcc_maybe :: OccName -> Maybe (NameSpace, Boxity, Arity)
610 -- Tuples are special, because there are so many of them!
611 isTupleOcc_maybe (OccName ns fs)
612 = case unpackFS fs of
613 '(':'#':',':rest -> Just (ns, Unboxed, 2 + count_commas rest)
614 '(':',':rest -> Just (ns, Boxed, 2 + count_commas rest)
617 count_commas (',':rest) = 1 + count_commas rest
621 %************************************************************************
623 \subsection{Lexical categories}
625 %************************************************************************
627 These functions test strings to see if they fit the lexical categories
628 defined in the Haskell report.
631 isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
632 isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
634 isLexCon cs = isLexConId cs || isLexConSym cs
635 isLexVar cs = isLexVarId cs || isLexVarSym cs
637 isLexId cs = isLexConId cs || isLexVarId cs
638 isLexSym cs = isLexConSym cs || isLexVarSym cs
642 isLexConId cs -- Prefix type or data constructors
643 | nullFS cs = False -- e.g. "Foo", "[]", "(,)"
644 | cs == FSLIT("[]") = True
645 | otherwise = startsConId (headFS cs)
647 isLexVarId cs -- Ordinary prefix identifiers
648 | nullFS cs = False -- e.g. "x", "_x"
649 | otherwise = startsVarId (headFS cs)
651 isLexConSym cs -- Infix type or data constructors
652 | nullFS cs = False -- e.g. ":-:", ":", "->"
653 | cs == FSLIT("->") = True
654 | otherwise = startsConSym (headFS cs)
656 isLexVarSym cs -- Infix identifiers
657 | nullFS cs = False -- e.g. "+"
658 | otherwise = startsVarSym (headFS cs)
661 startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
662 startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
663 startsConSym c = c == ':' -- Infix data constructors
664 startsVarId c = isLower c || c == '_' -- Ordinary Ids
665 startsConId c = isUpper c || c == '(' -- Ordinary type constructors and data constructors
667 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
670 %************************************************************************
673 Here rather than BinIface because OccName is abstract
675 %************************************************************************
678 instance Binary NameSpace where
681 put_ bh DataName = do
685 put_ bh TcClsName = do
690 0 -> do return VarName
691 1 -> do return DataName
692 2 -> do return TvName
693 _ -> do return TcClsName
695 instance Binary OccName where
696 put_ bh (OccName aa ab) = do
702 return (OccName aa ab)