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,
41 occNameFS, occNameString, occNameSpace,
43 isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
44 parenSymOcc, reportIfUnused, isTcClsName, isVarName,
49 OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
50 lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
51 occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
54 OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
56 unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
57 foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
60 TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
62 -- The basic form of names
63 isLexCon, isLexVar, isLexId, isLexSym,
64 isLexConId, isLexConSym, isLexVarId, isLexVarSym,
65 startsVarSym, startsVarId, startsConSym, startsConId
68 #include "HsVersions.h"
83 -- Unicode TODO: put isSymbol in libcompat
84 #if __GLASGOW_HASKELL__ > 604
86 isSymbol = const False
91 %************************************************************************
93 \subsection{Name space}
95 %************************************************************************
98 data NameSpace = VarName -- Variables, including "real" data constructors
99 | DataName -- "Source" data constructors
100 | TvName -- Type variables
101 | TcClsName -- Type constructors and classes; Haskell has them
102 -- in the same name space for now.
104 {-! derive: Binary !-}
106 -- Note [Data Constructors]
107 -- see also: Note [Data Constructor Naming] in DataCon.lhs
109 -- "Source" data constructors are the data constructors mentioned
110 -- in Haskell source code
112 -- "Real" data constructors are the data constructors of the
113 -- representation type, which may not be the same as the source
117 -- data T = T !(Int,Int)
119 -- The source datacon has type (Int,Int) -> T
120 -- The real datacon has type Int -> Int -> T
121 -- GHC chooses a representation based on the strictness etc.
124 -- Though type constructors and classes are in the same name space now,
125 -- the NameSpace type is abstract, so we can easily separate them later
126 tcName = TcClsName -- Type constructors
127 clsName = TcClsName -- Classes
128 tcClsName = TcClsName -- Not sure which!
131 srcDataName = DataName -- Haskell-source data constructors should be
132 -- in the Data name space
137 isTcClsName :: NameSpace -> Bool
138 isTcClsName TcClsName = True
139 isTcClsName _ = False
141 isVarName :: NameSpace -> Bool -- Variables or type variables, but not constructors
142 isVarName TvName = True
143 isVarName VarName = True
144 isVarName other = False
146 pprNameSpace :: NameSpace -> SDoc
147 pprNameSpace DataName = ptext SLIT("data constructor")
148 pprNameSpace VarName = ptext SLIT("variable")
149 pprNameSpace TvName = ptext SLIT("type variable")
150 pprNameSpace TcClsName = ptext SLIT("type constructor or class")
152 pprNonVarNameSpace :: NameSpace -> SDoc
153 pprNonVarNameSpace VarName = empty
154 pprNonVarNameSpace ns = pprNameSpace ns
156 pprNameSpaceBrief DataName = char 'd'
157 pprNameSpaceBrief VarName = char 'v'
158 pprNameSpaceBrief TvName = ptext SLIT("tv")
159 pprNameSpaceBrief TcClsName = ptext SLIT("tc")
163 %************************************************************************
165 \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
167 %************************************************************************
170 data OccName = OccName
171 { occNameSpace :: !NameSpace
172 , occNameFS :: !FastString
178 instance Eq OccName where
179 (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
181 instance Ord OccName where
182 -- Compares lexicographically, *not* by Unique of the string
183 compare (OccName sp1 s1) (OccName sp2 s2)
184 = (s1 `compare` s2) `thenCmp` (sp1 `compare` sp2)
188 %************************************************************************
190 \subsection{Printing}
192 %************************************************************************
195 instance Outputable OccName where
198 pprOccName :: OccName -> SDoc
199 pprOccName (OccName sp occ)
200 = getPprStyle $ \ sty ->
202 then ftext (zEncodeFS occ)
203 else ftext occ <> if debugStyle sty
204 then braces (pprNameSpaceBrief sp)
209 %************************************************************************
211 \subsection{Construction}
213 %************************************************************************
216 mkOccName :: NameSpace -> String -> OccName
217 mkOccName occ_sp str = OccName occ_sp (mkFastString str)
219 mkOccNameFS :: NameSpace -> FastString -> OccName
220 mkOccNameFS occ_sp fs = OccName occ_sp fs
222 mkVarOcc :: String -> OccName
223 mkVarOcc s = mkOccName varName s
225 mkVarOccFS :: FastString -> OccName
226 mkVarOccFS fs = mkOccNameFS varName fs
228 mkTyVarOcc :: FastString -> OccName
229 mkTyVarOcc fs = mkOccNameFS tvName fs
233 %************************************************************************
237 %************************************************************************
239 OccEnvs are used mainly for the envts in ModIfaces.
241 They are efficient, because FastStrings have unique Int# keys. We assume
242 this key is less than 2^24, so we can make a Unique using
243 mkUnique ns key :: Unique
244 where 'ns' is a Char reprsenting the name space. This in turn makes it
245 easy to build an OccEnv.
248 instance Uniquable OccName where
249 getUnique (OccName ns fs)
250 = mkUnique char (I# (uniqueOfFS fs))
251 where -- See notes above about this getUnique function
258 type OccEnv a = UniqFM a
260 emptyOccEnv :: OccEnv a
261 unitOccEnv :: OccName -> a -> OccEnv a
262 extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
263 extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
264 lookupOccEnv :: OccEnv a -> OccName -> Maybe a
265 mkOccEnv :: [(OccName,a)] -> OccEnv a
266 mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
267 elemOccEnv :: OccName -> OccEnv a -> Bool
268 foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
269 occEnvElts :: OccEnv a -> [a]
270 extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
271 plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
272 plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
273 mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
275 emptyOccEnv = emptyUFM
277 extendOccEnv = addToUFM
278 extendOccEnvList = addListToUFM
279 lookupOccEnv = lookupUFM
285 plusOccEnv_C = plusUFM_C
286 extendOccEnv_C = addToUFM_C
289 mkOccEnv_C comb l = addListToUFM_C comb emptyOccEnv l
291 type OccSet = UniqFM OccName
293 emptyOccSet :: OccSet
294 unitOccSet :: OccName -> OccSet
295 mkOccSet :: [OccName] -> OccSet
296 extendOccSet :: OccSet -> OccName -> OccSet
297 extendOccSetList :: OccSet -> [OccName] -> OccSet
298 unionOccSets :: OccSet -> OccSet -> OccSet
299 unionManyOccSets :: [OccSet] -> OccSet
300 minusOccSet :: OccSet -> OccSet -> OccSet
301 elemOccSet :: OccName -> OccSet -> Bool
302 occSetElts :: OccSet -> [OccName]
303 foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
304 isEmptyOccSet :: OccSet -> Bool
305 intersectOccSet :: OccSet -> OccSet -> OccSet
306 intersectsOccSet :: OccSet -> OccSet -> Bool
308 emptyOccSet = emptyUniqSet
309 unitOccSet = unitUniqSet
311 extendOccSet = addOneToUniqSet
312 extendOccSetList = addListToUniqSet
313 unionOccSets = unionUniqSets
314 unionManyOccSets = unionManyUniqSets
315 minusOccSet = minusUniqSet
316 elemOccSet = elementOfUniqSet
317 occSetElts = uniqSetToList
318 foldOccSet = foldUniqSet
319 isEmptyOccSet = isEmptyUniqSet
320 intersectOccSet = intersectUniqSets
321 intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
325 %************************************************************************
327 \subsection{Predicates and taking them apart}
329 %************************************************************************
332 occNameString :: OccName -> String
333 occNameString (OccName _ s) = unpackFS s
335 setOccNameSpace :: NameSpace -> OccName -> OccName
336 setOccNameSpace sp (OccName _ occ) = OccName sp occ
338 isVarOcc, isTvOcc, isDataSymOcc, isSymOcc, isTcOcc :: OccName -> Bool
340 isVarOcc (OccName VarName _) = True
341 isVarOcc other = False
343 isTvOcc (OccName TvName _) = True
344 isTvOcc other = False
346 isTcOcc (OccName TcClsName _) = True
347 isTcOcc other = False
349 isValOcc (OccName VarName _) = True
350 isValOcc (OccName DataName _) = True
351 isValOcc other = False
353 -- Data constructor operator (starts with ':', or '[]')
354 -- Pretty inefficient!
355 isDataSymOcc (OccName DataName s) = isLexConSym s
356 isDataSymOcc (OccName VarName s)
357 | isLexConSym s = pprPanic "isDataSymOcc: check me" (ppr s)
358 -- Jan06: I don't think this should happen
359 isDataSymOcc other = False
361 isDataOcc (OccName DataName _) = True
362 isDataOcc (OccName VarName s)
363 | isLexCon s = pprPanic "isDataOcc: check me" (ppr s)
364 -- Jan06: I don't think this should happen
365 isDataOcc other = False
367 -- Any operator (data constructor or variable)
368 -- Pretty inefficient!
369 isSymOcc (OccName DataName s) = isLexConSym s
370 isSymOcc (OccName TcClsName s) = isLexConSym s
371 isSymOcc (OccName VarName s) = isLexSym s
372 isSymOcc other = False
374 parenSymOcc :: OccName -> SDoc -> SDoc
375 -- Wrap parens around an operator
376 parenSymOcc occ doc | isSymOcc occ = parens doc
382 reportIfUnused :: OccName -> Bool
383 -- Haskell 98 encourages compilers to suppress warnings about
384 -- unused names in a pattern if they start with "_".
385 reportIfUnused occ = case occNameString occ of
391 %************************************************************************
393 \subsection{Making system names}
395 %************************************************************************
397 Here's our convention for splitting up the interface file name space:
399 d... dictionary identifiers
400 (local variables, so no name-clash worries)
402 $f... dict-fun identifiers (from inst decls)
403 $dm... default methods
404 $p... superclass selectors
406 :T... compiler-generated tycons for dictionaries
407 :D... ...ditto data cons
408 :Co... ...ditto coercions
409 $sf.. specialised version of f
411 in encoded form these appear as Zdfxxx etc
413 :... keywords (export:, letrec: etc.)
414 --- I THINK THIS IS WRONG!
416 This knowledge is encoded in the following functions.
419 @mk_deriv@ generates an @OccName@ from the prefix and a string.
420 NB: The string must already be encoded!
423 mk_deriv :: NameSpace
424 -> String -- Distinguishes one sort of derived name from another
428 mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
432 mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkDerivedTyConOcc,
433 mkClassTyConOcc, mkClassDataConOcc, mkDictOcc, mkIPOcc,
434 mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
435 mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc
436 :: OccName -> OccName
438 -- These derived variables have a prefix that no Haskell value could have
439 mkDataConWrapperOcc = mk_simple_deriv varName "$W"
440 mkWorkerOcc = mk_simple_deriv varName "$w"
441 mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
442 mkDerivedTyConOcc = mk_simple_deriv tcName ":" -- The : prefix makes sure it classifies
443 mkClassTyConOcc = mk_simple_deriv tcName ":T" -- as a tycon/datacon
444 mkClassDataConOcc = mk_simple_deriv dataName ":D" -- We go straight to the "real" data con
445 -- for datacons from classes
446 mkDictOcc = mk_simple_deriv varName "$d"
447 mkIPOcc = mk_simple_deriv varName "$i"
448 mkSpecOcc = mk_simple_deriv varName "$s"
449 mkForeignExportOcc = mk_simple_deriv varName "$f"
450 mkNewTyCoOcc = mk_simple_deriv tcName ":Co"
451 mkInstTyCoOcc = mk_simple_deriv tcName ":CoF" -- derived from rep ty
452 mkEqPredCoOcc = mk_simple_deriv tcName "$co"
454 -- Generic derivable classes
455 mkGenOcc1 = mk_simple_deriv varName "$gfrom"
456 mkGenOcc2 = mk_simple_deriv varName "$gto"
458 -- data T = MkT ... deriving( Data ) needs defintions for
459 -- $tT :: Data.Generics.Basics.DataType
460 -- $cMkT :: Data.Generics.Basics.Constr
461 mkDataTOcc = mk_simple_deriv varName "$t"
462 mkDataCOcc = mk_simple_deriv varName "$c"
465 mkVectOcc = mk_simple_deriv varName "$v_"
466 mkVectTyConOcc = mk_simple_deriv tcName ":V_"
467 mkVectDataConOcc = mk_simple_deriv dataName ":VD_"
468 mkVectIsoOcc = mk_simple_deriv varName "$VI_"
469 mkPArrayTyConOcc = mk_simple_deriv tcName ":VP_"
470 mkPArrayDataConOcc = mk_simple_deriv dataName ":VPD_"
471 mkPReprTyConOcc = mk_simple_deriv tcName ":VR_"
472 mkPADFunOcc = mk_simple_deriv varName "$PA_"
474 mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
476 -- Data constructor workers are made by setting the name space
477 -- of the data constructor OccName (which should be a DataName)
479 mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
483 mkSuperDictSelOcc :: Int -- Index of superclass, eg 3
484 -> OccName -- Class, eg "Ord"
485 -> OccName -- eg "$p3Ord"
486 mkSuperDictSelOcc index cls_occ
487 = mk_deriv varName "$p" (show index ++ occNameString cls_occ)
489 mkLocalOcc :: Unique -- Unique
490 -> OccName -- Local name (e.g. "sat")
491 -> OccName -- Nice unique version ("$L23sat")
493 = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
494 -- The Unique might print with characters
495 -- that need encoding (e.g. 'z'!)
498 Derive a name for the representation type constructor of a data/newtype
502 mkInstTyTcOcc :: Int -- Index
503 -> OccName -- Family name (e.g. "Map")
504 -> OccName -- Nice unique version (":R23Map")
505 mkInstTyTcOcc index occ
506 = mk_deriv tcName (":R" ++ show index) (occNameString occ)
510 mkDFunOcc :: String -- Typically the class and type glommed together e.g. "OrdMaybe"
511 -- Only used in debug mode, for extra clarity
512 -> Bool -- True <=> hs-boot instance dfun
513 -> Int -- Unique index
514 -> OccName -- "$f3OrdMaybe"
516 -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
517 -- thing when we compile the mother module. Reason: we don't know exactly
518 -- what the mother module will call it.
520 mkDFunOcc info_str is_boot index
521 = mk_deriv VarName prefix string
523 prefix | is_boot = "$fx"
525 string | opt_PprStyle_Debug = show index ++ info_str
526 | otherwise = show index
529 We used to add a '$m' to indicate a method, but that gives rise to bad
530 error messages from the type checker when we print the function name or pattern
531 of an instance-decl binding. Why? Because the binding is zapped
532 to use the method name in place of the selector name.
533 (See TcClassDcl.tcMethodBind)
535 The way it is now, -ddump-xx output may look confusing, but
536 you can always say -dppr-debug to get the uniques.
538 However, we *do* have to zap the first character to be lower case,
539 because overloaded constructors (blarg) generate methods too.
540 And convert to VarName space
542 e.g. a call to constructor MkFoo where
543 data (Ord a) => Foo a = MkFoo a
545 If this is necessary, we do it by prefixing '$m'. These
546 guys never show up in error messages. What a hack.
549 mkMethodOcc :: OccName -> OccName
550 mkMethodOcc occ@(OccName VarName fs) = occ
551 mkMethodOcc occ = mk_simple_deriv varName "$m" occ
555 %************************************************************************
557 \subsection{Tidying them up}
559 %************************************************************************
561 Before we print chunks of code we like to rename it so that
562 we don't have to print lots of silly uniques in it. But we mustn't
563 accidentally introduce name clashes! So the idea is that we leave the
564 OccName alone unless it accidentally clashes with one that is already
565 in scope; if so, we tack on '1' at the end and try again, then '2', and
566 so on till we find a unique one.
568 There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
569 because that isn't a single lexeme. So we encode it to 'lle' and *then*
570 tack on the '1', if necessary.
573 type TidyOccEnv = OccEnv Int -- The in-scope OccNames
574 -- Range gives a plausible starting point for new guesses
576 emptyTidyOccEnv = emptyOccEnv
578 initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
579 initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
581 tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
583 tidyOccName in_scope occ@(OccName occ_sp fs)
584 = case lookupOccEnv in_scope occ of
585 Nothing -> -- Not already used: make it used
586 (extendOccEnv in_scope occ 1, occ)
588 Just n -> -- Already used: make a new guess,
589 -- change the guess base, and try again
590 tidyOccName (extendOccEnv in_scope occ (n+1))
591 (mkOccName occ_sp (unpackFS fs ++ show n))
594 %************************************************************************
596 Stuff for dealing with tuples
598 %************************************************************************
601 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
602 mkTupleOcc ns bx ar = OccName ns (mkFastString str)
604 -- no need to cache these, the caching is done in the caller
605 -- (TysWiredIn.mk_tuple)
607 Boxed -> '(' : commas ++ ")"
608 Unboxed -> '(' : '#' : commas ++ "#)"
610 commas = take (ar-1) (repeat ',')
612 isTupleOcc_maybe :: OccName -> Maybe (NameSpace, Boxity, Arity)
613 -- Tuples are special, because there are so many of them!
614 isTupleOcc_maybe (OccName ns fs)
615 = case unpackFS fs of
616 '(':'#':',':rest -> Just (ns, Unboxed, 2 + count_commas rest)
617 '(':',':rest -> Just (ns, Boxed, 2 + count_commas rest)
620 count_commas (',':rest) = 1 + count_commas rest
624 %************************************************************************
626 \subsection{Lexical categories}
628 %************************************************************************
630 These functions test strings to see if they fit the lexical categories
631 defined in the Haskell report.
634 isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
635 isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
637 isLexCon cs = isLexConId cs || isLexConSym cs
638 isLexVar cs = isLexVarId cs || isLexVarSym cs
640 isLexId cs = isLexConId cs || isLexVarId cs
641 isLexSym cs = isLexConSym cs || isLexVarSym cs
645 isLexConId cs -- Prefix type or data constructors
646 | nullFS cs = False -- e.g. "Foo", "[]", "(,)"
647 | cs == FSLIT("[]") = True
648 | otherwise = startsConId (headFS cs)
650 isLexVarId cs -- Ordinary prefix identifiers
651 | nullFS cs = False -- e.g. "x", "_x"
652 | otherwise = startsVarId (headFS cs)
654 isLexConSym cs -- Infix type or data constructors
655 | nullFS cs = False -- e.g. ":-:", ":", "->"
656 | cs == FSLIT("->") = True
657 | otherwise = startsConSym (headFS cs)
659 isLexVarSym cs -- Infix identifiers
660 | nullFS cs = False -- e.g. "+"
661 | otherwise = startsVarSym (headFS cs)
664 startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
665 startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
666 startsConSym c = c == ':' -- Infix data constructors
667 startsVarId c = isLower c || c == '_' -- Ordinary Ids
668 startsConId c = isUpper c || c == '(' -- Ordinary type constructors and data constructors
670 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
673 %************************************************************************
676 Here rather than BinIface because OccName is abstract
678 %************************************************************************
681 instance Binary NameSpace where
684 put_ bh DataName = do
688 put_ bh TcClsName = do
693 0 -> do return VarName
694 1 -> do return DataName
695 2 -> do return TvName
696 _ -> do return TcClsName
698 instance Binary OccName where
699 put_ bh (OccName aa ab) = do
705 return (OccName aa ab)