OccName.lhs 30.1 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3 4 5 6
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%

\begin{code}
7 8 9 10 11
-- |
-- #name_types#
-- GHC uses several kinds of name internally:
--
-- * '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
13 14 15 16 17 18 19 20 21
--   data constructors
--
-- * 'RdrName.RdrName': see "RdrName#name_types"
--
-- * 'Name.Name': see "Name#name_types"
--
-- * 'Id.Id': see "Id#name_types"
--
-- * 'Var.Var': see "Var#name_types"
Ian Lynagh's avatar
Ian Lynagh committed
22 23 24 25 26 27 28 29

{-# OPTIONS -fno-warn-tabs #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and
-- detab the module (please do the detabbing in a separate patch). See
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
-- for details

30
module OccName (
31 32 33 34 35 36
	-- * The 'NameSpace' type
	NameSpace, -- Abstract
	
	-- ** Construction
	-- $real_vs_source_data_constructors
	tcName, clsName, tcClsName, dataName, varName, 
37
	tvName, srcDataName,
38

39
	-- ** Pretty Printing
40 41
	pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,

42
	-- * The 'OccName' type
43
	OccName, 	-- Abstract, instance of Outputable
44
	pprOccName, 
45

46 47 48
	-- ** Construction	
	mkOccName, mkOccNameFS, 
	mkVarOcc, mkVarOccFS,
49 50 51 52
	mkDataOcc, mkDataOccFS,
	mkTyVarOcc, mkTyVarOccFS,
	mkTcOcc, mkTcOccFS,
	mkClsOcc, mkClsOccFS,
53
        mkDFunOcc,
54 55 56
	mkTupleOcc, 
	setOccNameSpace,

57
	-- ** Derived 'OccName's
58
        isDerivedOccName,
59
	mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkGenDefMethodOcc,
60
	mkDerivedTyConOcc, mkNewTyCoOcc, mkClassOpAuxOcc,
61
        mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
batterseapower's avatar
batterseapower committed
62
  	mkClassDataConOcc, mkDictOcc, mkIPOcc, 
63
 	mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
64
 	mkGenD, mkGenR, mkGenRCo, mkGenC, mkGenS,
65
        mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
66
	mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
67
	mkInstTyCoOcc, mkEqPredCoOcc,
68
        mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
69
        mkPDataTyConOcc, mkPDataDataConOcc,
70
        mkPReprTyConOcc, 
71
        mkPADFunOcc,
72 73 74 75 76

	-- ** Deconstruction
	occNameFS, occNameString, occNameSpace, 

	isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
77
	parenSymOcc, startsWithUnderscore, 
78 79
	
	isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
80 81 82

	isTupleOcc_maybe,

83
	-- * The 'OccEnv' type
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
84
	OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
85
	lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
86
	occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
87
        extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
88

89
	-- * The 'OccSet' type
90 91
	OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet, 
	extendOccSetList,
92 93
	unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts, 
	foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
94
                  
95
	-- * Tidying up
96 97
	TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,

98
	-- * Lexical characteristics of Haskell names
99 100
	isLexCon, isLexVar, isLexId, isLexSym,
	isLexConId, isLexConSym, isLexVarId, isLexVarSym,
101
	startsVarSym, startsVarId, startsConSym, startsConId
102 103
    ) where

104 105
#include "Typeable.h"

Simon Marlow's avatar
Simon Marlow committed
106 107 108
import Util
import Unique
import BasicTypes
109 110
import UniqFM
import UniqSet
111
import FastString
112
import Outputable
113
import Binary
114
import StaticFlags( opt_SuppressUniques )
Simon Marlow's avatar
Simon Marlow committed
115
import Data.Char
116
import Data.Data
117
\end{code}
118 119 120 121 122 123 124 125

%************************************************************************
%*									*
\subsection{Name space}
%*									*
%************************************************************************

\begin{code}
126 127
data NameSpace = VarName	-- Variables, including "real" data constructors
	       | DataName	-- "Source" data constructors 
128 129
	       | TvName		-- Type variables
	       | TcClsName	-- Type constructors and classes; Haskell has them
130
				-- in the same name space for now.
131
	       deriving( Eq, Ord )
132
   {-! derive: Binary !-}
133

134 135 136
-- Note [Data Constructors]  
-- see also: Note [Data Constructor Naming] in DataCon.lhs
--
137 138 139 140 141 142 143 144 145 146 147 148 149
-- $real_vs_source_data_constructors
-- There are two forms of data constructor:
--
--	[Source data constructors] The data constructors mentioned in Haskell source code
--
--	[Real data constructors] The data constructors of the representation type, which may not be the same as the source type
--
-- For example:
--
-- > data T = T !(Int, Int)
--
-- The source datacon has type @(Int, Int) -> T@
-- The real   datacon has type @Int -> Int -> T@
150 151 152
--
-- GHC chooses a representation based on the strictness etc.

153 154 155
tcName, clsName, tcClsName :: NameSpace
dataName, srcDataName      :: NameSpace
tvName, varName            :: NameSpace
156

157 158 159 160 161 162
-- Though type constructors and classes are in the same name space now,
-- the NameSpace type is abstract, so we can easily separate them later
tcName    = TcClsName		-- Type constructors
clsName   = TcClsName		-- Classes
tcClsName = TcClsName		-- Not sure which!

163 164 165
dataName    = DataName
srcDataName = DataName	-- Haskell-source data constructors should be
			-- in the Data name space
166

167 168
tvName      = TvName
varName     = VarName
169

170 171 172 173 174 175 176 177 178 179 180
isDataConNameSpace :: NameSpace -> Bool
isDataConNameSpace DataName = True
isDataConNameSpace _        = False

isTcClsNameSpace :: NameSpace -> Bool
isTcClsNameSpace TcClsName = True
isTcClsNameSpace _         = False

isTvNameSpace :: NameSpace -> Bool
isTvNameSpace TvName = True
isTvNameSpace _      = False
181

182 183 184 185 186 187 188 189 190
isVarNameSpace :: NameSpace -> Bool	-- Variables or type variables, but not constructors
isVarNameSpace TvName  = True
isVarNameSpace VarName = True
isVarNameSpace _       = False

isValNameSpace :: NameSpace -> Bool
isValNameSpace DataName = True
isValNameSpace VarName  = True
isValNameSpace _        = False
191

192
pprNameSpace :: NameSpace -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
193 194 195 196
pprNameSpace DataName  = ptext (sLit "data constructor")
pprNameSpace VarName   = ptext (sLit "variable")
pprNameSpace TvName    = ptext (sLit "type variable")
pprNameSpace TcClsName = ptext (sLit "type constructor or class")
197 198 199 200

pprNonVarNameSpace :: NameSpace -> SDoc
pprNonVarNameSpace VarName = empty
pprNonVarNameSpace ns = pprNameSpace ns
201

202
pprNameSpaceBrief :: NameSpace -> SDoc
203 204
pprNameSpaceBrief DataName  = char 'd'
pprNameSpaceBrief VarName   = char 'v'
Ian Lynagh's avatar
Ian Lynagh committed
205 206
pprNameSpaceBrief TvName    = ptext (sLit "tv")
pprNameSpaceBrief TcClsName = ptext (sLit "tc")
207 208 209 210 211 212 213 214 215 216
\end{code}


%************************************************************************
%*									*
\subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
%*									*
%************************************************************************

\begin{code}
217
data OccName = OccName 
218
    { occNameSpace  :: !NameSpace
219
    , occNameFS     :: !FastString
220
    }
Ian Lynagh's avatar
Ian Lynagh committed
221
    deriving Typeable
222 223 224 225 226 227 228 229
\end{code}


\begin{code}
instance Eq OccName where
    (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2

instance Ord OccName where
230 231 232
	-- Compares lexicographically, *not* by Unique of the string
    compare (OccName sp1 s1) (OccName sp2 s2) 
	= (s1  `compare` s2) `thenCmp` (sp1 `compare` sp2)
233 234 235 236 237 238

instance Data OccName where
  -- don't traverse?
  toConstr _   = abstractConstr "OccName"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = mkNoRepType "OccName"
239 240 241 242 243 244 245 246 247 248 249
\end{code}


%************************************************************************
%*									*
\subsection{Printing}
%*									*
%************************************************************************
 
\begin{code}
instance Outputable OccName where
250
    ppr = pprOccName
251 252

pprOccName :: OccName -> SDoc
253 254
pprOccName (OccName sp occ) 
  = getPprStyle $ \ sty ->
255
    if codeStyle sty 
256 257 258 259 260 261 262 263 264 265 266 267 268
    then ftext (zEncodeFS occ)
    else pp_occ <> pp_debug sty
  where
    pp_debug sty | debugStyle sty = braces (pprNameSpaceBrief sp)
	         | otherwise      = empty

    pp_occ | opt_SuppressUniques = text (strip_th_unique (unpackFS occ))
           | otherwise           = ftext occ

	-- See Note [Suppressing uniques in OccNames]
    strip_th_unique ('[' : c : _) | isAlphaNum c = []
    strip_th_unique (c : cs) = c : strip_th_unique cs
    strip_th_unique []       = []
269 270
\end{code}

271 272 273 274 275
Note [Suppressing uniques in OccNames]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a hack to de-wobblify the OccNames that contain uniques from
Template Haskell that have been turned into a string in the OccName.
See Note [Unique OccNames from Template Haskell] in Convert.hs
276 277 278 279 280

%************************************************************************
%*									*
\subsection{Construction}
%*									*
281
%************************************************************************
282 283

\begin{code}
284
mkOccName :: NameSpace -> String -> OccName
285
mkOccName occ_sp str = OccName occ_sp (mkFastString str)
286

287 288
mkOccNameFS :: NameSpace -> FastString -> OccName
mkOccNameFS occ_sp fs = OccName occ_sp fs
289

290 291
mkVarOcc :: String -> OccName
mkVarOcc s = mkOccName varName s
292

293 294
mkVarOccFS :: FastString -> OccName
mkVarOccFS fs = mkOccNameFS varName fs
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
mkDataOcc :: String -> OccName
mkDataOcc = mkOccName dataName

mkDataOccFS :: FastString -> OccName
mkDataOccFS = mkOccNameFS dataName

mkTyVarOcc :: String -> OccName
mkTyVarOcc = mkOccName tvName

mkTyVarOccFS :: FastString -> OccName
mkTyVarOccFS fs = mkOccNameFS tvName fs

mkTcOcc :: String -> OccName
mkTcOcc = mkOccName tcName

mkTcOccFS :: FastString -> OccName
mkTcOccFS = mkOccNameFS tcName

mkClsOcc :: String -> OccName
mkClsOcc = mkOccName clsName

mkClsOccFS :: FastString -> OccName
mkClsOccFS = mkOccNameFS clsName
319
\end{code}
320

321

322 323 324 325 326 327 328 329
%************************************************************************
%*									*
		Environments
%*									*
%************************************************************************

OccEnvs are used mainly for the envts in ModIfaces.

330 331
Note [The Unique of an OccName]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332
They are efficient, because FastStrings have unique Int# keys.  We assume
333 334 335 336
this key is less than 2^24, and indeed FastStrings are allocated keys 
sequentially starting at 0.

So we can make a Unique using
337 338 339 340 341 342
	mkUnique ns key  :: Unique
where 'ns' is a Char reprsenting the name space.  This in turn makes it
easy to build an OccEnv.

\begin{code}
instance Uniquable OccName where
343 344 345 346 347
      -- See Note [The Unique of an OccName]
  getUnique (OccName VarName   fs) = mkVarOccUnique  fs
  getUnique (OccName DataName  fs) = mkDataOccUnique fs
  getUnique (OccName TvName    fs) = mkTvOccUnique   fs
  getUnique (OccName TcClsName fs) = mkTcOccUnique   fs
348

349
newtype OccEnv a = A (UniqFM a)
350 351 352 353 354 355 356

emptyOccEnv :: OccEnv a
unitOccEnv  :: OccName -> a -> OccEnv a
extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
lookupOccEnv :: OccEnv a -> OccName -> Maybe a
mkOccEnv     :: [(OccName,a)] -> OccEnv a
357
mkOccEnv_C   :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
358 359 360 361
elemOccEnv   :: OccName -> OccEnv a -> Bool
foldOccEnv   :: (a -> b -> b) -> b -> OccEnv a -> b
occEnvElts   :: OccEnv a -> [a]
extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
362
extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
363
plusOccEnv     :: OccEnv a -> OccEnv a -> OccEnv a
364
plusOccEnv_C   :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
365
mapOccEnv      :: (a->b) -> OccEnv a -> OccEnv b
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
delFromOccEnv 	   :: OccEnv a -> OccName -> OccEnv a
delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
filterOccEnv	   :: (elt -> Bool) -> OccEnv elt -> OccEnv elt

emptyOccEnv  	 = A emptyUFM
unitOccEnv x y = A $ unitUFM x y 
extendOccEnv (A x) y z = A $ addToUFM x y z
extendOccEnvList (A x) l = A $ addListToUFM x l
lookupOccEnv (A x) y = lookupUFM x y
mkOccEnv     l    = A $ listToUFM l
elemOccEnv x (A y) 	 = elemUFM x y
foldOccEnv a b (A c)	 = foldUFM a b c 
occEnvElts (A x)	 = eltsUFM x
plusOccEnv (A x) (A y)	 = A $ plusUFM x y 
plusOccEnv_C f (A x) (A y)	 = A $ plusUFM_C f x y 
extendOccEnv_C f (A x) y z   = A $ addToUFM_C f x y z
382
extendOccEnv_Acc f g (A x) y z   = A $ addToUFM_Acc f g x y z
383 384 385 386 387 388 389 390
mapOccEnv f (A x)	 = A $ mapUFM f x
mkOccEnv_C comb l = A $ addListToUFM_C comb emptyUFM l
delFromOccEnv (A x) y    = A $ delFromUFM x y
delListFromOccEnv (A x) y  = A $ delListFromUFM x y
filterOccEnv x (A y)       = A $ filterUFM x y

instance Outputable a => Outputable (OccEnv a) where
    ppr (A x) = ppr x
391

392
type OccSet = UniqSet OccName
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

emptyOccSet	  :: OccSet
unitOccSet	  :: OccName -> OccSet
mkOccSet          :: [OccName] -> OccSet
extendOccSet      :: OccSet -> OccName -> OccSet
extendOccSetList  :: OccSet -> [OccName] -> OccSet
unionOccSets	  :: OccSet -> OccSet -> OccSet
unionManyOccSets  :: [OccSet] -> OccSet
minusOccSet 	  :: OccSet -> OccSet -> OccSet
elemOccSet	  :: OccName -> OccSet -> Bool
occSetElts	  :: OccSet -> [OccName]
foldOccSet	  :: (OccName -> b -> b) -> b -> OccSet -> b
isEmptyOccSet	  :: OccSet -> Bool
intersectOccSet   :: OccSet -> OccSet -> OccSet
intersectsOccSet  :: OccSet -> OccSet -> Bool

emptyOccSet	  = emptyUniqSet
unitOccSet	  = unitUniqSet
mkOccSet          = mkUniqSet
extendOccSet	  = addOneToUniqSet
extendOccSetList  = addListToUniqSet
unionOccSets      = unionUniqSets
unionManyOccSets  = unionManyUniqSets
minusOccSet	  = minusUniqSet
elemOccSet        = elementOfUniqSet
occSetElts        = uniqSetToList
foldOccSet	  = foldUniqSet
isEmptyOccSet     = isEmptyUniqSet
intersectOccSet   = intersectUniqSets
intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
\end{code}


426 427
%************************************************************************
%*									*
428
\subsection{Predicates and taking them apart}
429 430 431
%*									*
%************************************************************************

432 433
\begin{code}
occNameString :: OccName -> String
434
occNameString (OccName _ s) = unpackFS s
435

436 437
setOccNameSpace :: NameSpace -> OccName -> OccName
setOccNameSpace sp (OccName _ occ) = OccName sp occ
438

439
isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
440 441

isVarOcc (OccName VarName _) = True
442
isVarOcc _                   = False
443

444
isTvOcc (OccName TvName _) = True
445
isTvOcc _                  = False
446

447
isTcOcc (OccName TcClsName _) = True
448
isTcOcc _                     = False
449

450 451 452
-- | /Value/ 'OccNames's are those that are either in 
-- the variable or data constructor namespaces
isValOcc :: OccName -> Bool
453 454
isValOcc (OccName VarName  _) = True
isValOcc (OccName DataName _) = True
455
isValOcc _                    = False
456

457
isDataOcc (OccName DataName _) = True
458 459 460
isDataOcc (OccName VarName s)  
  | isLexCon s = pprPanic "isDataOcc: check me" (ppr s)
		-- Jan06: I don't think this should happen
461
isDataOcc _                    = False
462

463 464 465 466 467 468 469 470
-- | Test if the 'OccName' is a data constructor that starts with
-- a symbol (e.g. @:@, or @[]@)
isDataSymOcc :: OccName -> Bool
isDataSymOcc (OccName DataName s) = isLexConSym s
isDataSymOcc (OccName VarName s)  
  | isLexConSym s = pprPanic "isDataSymOcc: check me" (ppr s)
		-- Jan06: I don't think this should happen
isDataSymOcc _                    = False
471
-- Pretty inefficient!
472 473 474 475

-- | Test if the 'OccName' is that for any operator (whether 
-- it is a data constructor or variable or whatever)
isSymOcc :: OccName -> Bool
476 477 478
isSymOcc (OccName DataName s)  = isLexConSym s
isSymOcc (OccName TcClsName s) = isLexConSym s
isSymOcc (OccName VarName s)   = isLexSym s
479
isSymOcc (OccName TvName s)    = isLexSym s
480
-- Pretty inefficient!
481 482

parenSymOcc :: OccName -> SDoc -> SDoc
483
-- ^ Wrap parens around an operator
484 485
parenSymOcc occ doc | isSymOcc occ = parens doc
		    | otherwise    = doc
486
\end{code}
487 488


489
\begin{code}
490 491 492 493 494 495
startsWithUnderscore :: OccName -> Bool
-- ^ Haskell 98 encourages compilers to suppress warnings about unsed
-- names in a pattern if they start with @_@: this implements that test
startsWithUnderscore occ = case occNameString occ of
			     ('_' : _) -> True
			     _other    -> False
496 497 498
\end{code}


499 500 501 502 503
%************************************************************************
%*									*
\subsection{Making system names}
%*									*
%************************************************************************
504

505
Here's our convention for splitting up the interface file name space:
506

507 508
   d...		dictionary identifiers
   		(local variables, so no name-clash worries)
509

510 511 512
All of these other OccNames contain a mixture of alphabetic
and symbolic characters, and hence cannot possibly clash with
a user-written type or function name
513

514 515 516 517 518 519 520 521 522 523 524
   $f...	Dict-fun identifiers (from inst decls)
   $dmop	Default method for 'op'
   $pnC		n'th superclass selector for class C
   $wf		Worker for functtoin 'f'
   $sf..	Specialised version of f
   T:C		Tycon for dictionary for class C
   D:C		Data constructor for dictionary for class C
   NTCo:T       Coercion connecting newtype T with its representation type
   TFCo:R       Coercion connecting a data family to its respresentation type R

In encoded form these appear as Zdfxxx etc
525

526
	:...		keywords (export:, letrec: etc.)
apt's avatar
apt committed
527
--- I THINK THIS IS WRONG!
528

529 530
This knowledge is encoded in the following functions.

apt's avatar
apt committed
531
@mk_deriv@ generates an @OccName@ from the prefix and a string.
532
NB: The string must already be encoded!
533 534

\begin{code}
535 536
mk_deriv :: NameSpace 
	 -> String		-- Distinguishes one sort of derived name from another
537
	 -> String
538
	 -> OccName
539

540
mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
541 542 543 544 545 546 547

isDerivedOccName :: OccName -> Bool
isDerivedOccName occ = 
   case occNameString occ of
     '$':c:_ | isAlphaNum c -> True
     ':':c:_ | isAlphaNum c -> True
     _other                 -> False
548
\end{code}
549

550
\begin{code}
551
mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc, mkGenDefMethodOcc,
batterseapower's avatar
batterseapower committed
552 553 554 555 556 557
  	mkDerivedTyConOcc, mkClassDataConOcc, mkDictOcc,
 	mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
 	mkGenD, mkGenR, mkGenRCo,
	mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
	mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
        mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc
558 559
   :: OccName -> OccName

560
-- These derived variables have a prefix that no Haskell value could have
561
mkDataConWrapperOcc = mk_simple_deriv varName  "$W"
562 563
mkWorkerOcc         = mk_simple_deriv varName  "$w"
mkDefaultMethodOcc  = mk_simple_deriv varName  "$dm"
564
mkGenDefMethodOcc   = mk_simple_deriv varName  "$gdm"
565
mkClassOpAuxOcc     = mk_simple_deriv varName  "$c"
batterseapower's avatar
batterseapower committed
566
mkDerivedTyConOcc   = mk_simple_deriv tcName   ":"	-- The : prefix makes sure it classifies as a tycon/datacon
567
mkClassDataConOcc   = mk_simple_deriv dataName "D:"	-- We go straight to the "real" data con
568
							-- for datacons from classes
569 570 571 572
mkDictOcc	    = mk_simple_deriv varName  "$d"
mkIPOcc		    = mk_simple_deriv varName  "$i"
mkSpecOcc	    = mk_simple_deriv varName  "$s"
mkForeignExportOcc  = mk_simple_deriv varName  "$f"
573 574 575
mkNewTyCoOcc        = mk_simple_deriv tcName   "NTCo:"	-- Coercion for newtypes
mkInstTyCoOcc       = mk_simple_deriv tcName   "TFCo:"   -- Coercion for type functions
mkEqPredCoOcc	    = mk_simple_deriv tcName   "$co"
576

577 578 579 580 581
-- used in derived instances
mkCon2TagOcc        = mk_simple_deriv varName  "$con2tag_"
mkTag2ConOcc        = mk_simple_deriv varName  "$tag2con_"
mkMaxTagOcc         = mk_simple_deriv varName  "$maxtag_"

582
-- Generic derivable classes (old)
583 584 585
mkGenOcc1           = mk_simple_deriv varName  "$gfrom"
mkGenOcc2           = mk_simple_deriv varName  "$gto" 

586 587
-- Generic deriving mechanism (new)
mkGenD         = mk_simple_deriv tcName "D1"
588 589

mkGenC :: OccName -> Int -> OccName
590
mkGenC occ m   = mk_deriv tcName ("C1_" ++ show m) (occNameString occ)
591 592

mkGenS :: OccName -> Int -> Int -> OccName
593 594 595
mkGenS occ m n = mk_deriv tcName ("S1_" ++ show m ++ "_" ++ show n)
                   (occNameString occ)

596 597
mkGenR   = mk_simple_deriv tcName "Rep_"
mkGenRCo = mk_simple_deriv tcName "CoRep_"
598

599 600 601 602 603 604
-- data T = MkT ... deriving( Data ) needs defintions for 
--	$tT   :: Data.Generics.Basics.DataType
--	$cMkT :: Data.Generics.Basics.Constr
mkDataTOcc = mk_simple_deriv varName  "$t"
mkDataCOcc = mk_simple_deriv varName  "$c"

605
-- Vectorisation
606 607
mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc, mkPADFunOcc, mkPReprTyConOcc,
  mkPDataTyConOcc, mkPDataDataConOcc :: Maybe String -> OccName -> OccName
608 609 610 611 612 613 614 615
mkVectOcc         = mk_simple_deriv_with varName  "$v"
mkVectTyConOcc    = mk_simple_deriv_with tcName   "V:"
mkVectDataConOcc  = mk_simple_deriv_with dataName "VD:"
mkVectIsoOcc      = mk_simple_deriv_with varName  "$vi"
mkPADFunOcc       = mk_simple_deriv_with varName  "$pa"
mkPReprTyConOcc   = mk_simple_deriv_with tcName   "VR:"
mkPDataTyConOcc   = mk_simple_deriv_with tcName   "VP:"
mkPDataDataConOcc = mk_simple_deriv_with dataName "VPD:"
616

617
mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
618
mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
619

620 621 622 623
mk_simple_deriv_with :: NameSpace -> String -> Maybe String -> OccName -> OccName
mk_simple_deriv_with sp px Nothing     occ = mk_deriv sp px                  (occNameString occ)
mk_simple_deriv_with sp px (Just with) occ = mk_deriv sp (px ++ with ++ "_") (occNameString occ)

624 625
-- Data constructor workers are made by setting the name space
-- of the data constructor OccName (which should be a DataName)
626
-- to VarName
627
mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ 
628 629 630
\end{code}

\begin{code}
631 632 633
mkSuperDictSelOcc :: Int 	-- ^ Index of superclass, e.g. 3
		  -> OccName 	-- ^ Class, e.g. @Ord@
		  -> OccName	-- ^ Derived 'Occname', e.g. @$p3Ord@
batterseapower's avatar
batterseapower committed
634 635
mkSuperDictSelOcc index cls_tc_occ
  = mk_deriv varName "$p" (show index ++ occNameString cls_tc_occ)
636

637 638 639
mkLocalOcc :: Unique 		-- ^ Unique to combine with the 'OccName'
	   -> OccName		-- ^ Local name, e.g. @sat@
	   -> OccName		-- ^ Nice unique version, e.g. @$L23sat@
640
mkLocalOcc uniq occ
641 642 643
   = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
	-- The Unique might print with characters 
	-- that need encoding (e.g. 'z'!)
644 645
\end{code}

646
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
647 648
-- | Derive a name for the representation type constructor of a
-- @data@\/@newtype@ instance.
649 650 651 652 653
mkInstTyTcOcc :: String 		-- ^ Family name, e.g. @Map@
              -> OccSet                 -- ^ avoid these Occs
	      -> OccName		-- ^ @R:Map@
mkInstTyTcOcc str set =
  chooseUniqueOcc tcName ('R' : ':' : str) set
654
\end{code}
655 656

\begin{code}
657
mkDFunOcc :: String		-- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
658
				-- Only used in debug mode, for extra clarity
659
	  -> Bool		-- ^ Is this a hs-boot instance DFun?
660
          -> OccSet             -- ^ avoid these Occs
661
	  -> OccName		-- ^ E.g. @$f3OrdMaybe@
662 663 664 665 666

-- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
-- thing when we compile the mother module. Reason: we don't know exactly
-- what the  mother module will call it.

667 668
mkDFunOcc info_str is_boot set
  = chooseUniqueOcc VarName (prefix ++ info_str) set
669 670 671
  where
    prefix | is_boot   = "$fx"
	   | otherwise = "$f"
672 673 674 675 676 677 678 679 680 681 682 683
\end{code}

Sometimes we need to pick an OccName that has not already been used,
given a set of in-use OccNames.

\begin{code}
chooseUniqueOcc :: NameSpace -> String -> OccSet -> OccName
chooseUniqueOcc ns str set = loop (mkOccName ns str) (0::Int)
  where
  loop occ n
   | occ `elemOccSet` set = loop (mkOccName ns (str ++ show n)) (n+1)
   | otherwise            = occ
684 685
\end{code}

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
We used to add a '$m' to indicate a method, but that gives rise to bad
error messages from the type checker when we print the function name or pattern
of an instance-decl binding.  Why? Because the binding is zapped
to use the method name in place of the selector name.
(See TcClassDcl.tcMethodBind)

The way it is now, -ddump-xx output may look confusing, but
you can always say -dppr-debug to get the uniques.

However, we *do* have to zap the first character to be lower case,
because overloaded constructors (blarg) generate methods too.
And convert to VarName space

e.g. a call to constructor MkFoo where
	data (Ord a) => Foo a = MkFoo a

If this is necessary, we do it by prefixing '$m'.  These 
guys never show up in error messages.  What a hack.

\begin{code}
mkMethodOcc :: OccName -> OccName
707 708
mkMethodOcc occ@(OccName VarName _) = occ
mkMethodOcc occ                     = mk_simple_deriv varName "$m" occ
709 710
\end{code}

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

%************************************************************************
%*									*
\subsection{Tidying them up}
%*									*
%************************************************************************

Before we print chunks of code we like to rename it so that
we don't have to print lots of silly uniques in it.  But we mustn't
accidentally introduce name clashes!  So the idea is that we leave the
OccName alone unless it accidentally clashes with one that is already
in scope; if so, we tack on '1' at the end and try again, then '2', and
so on till we find a unique one.

There's a wrinkle for operators.  Consider '>>='.  We can't use '>>=1' 
because that isn't a single lexeme.  So we encode it to 'lle' and *then*
tack on the '1', if necessary.

\begin{code}
730 731 732
type TidyOccEnv = OccEnv Int	-- The in-scope OccNames
	-- Range gives a plausible starting point for new guesses

733
emptyTidyOccEnv :: TidyOccEnv
734
emptyTidyOccEnv = emptyOccEnv
735 736

initTidyOccEnv :: [OccName] -> TidyOccEnv	-- Initialise with names to avoid!
737
initTidyOccEnv = foldl (\env occ -> extendOccEnv env occ 1) emptyTidyOccEnv
738 739 740

tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)

741
tidyOccName in_scope occ@(OccName occ_sp fs)
742 743 744 745 746 747 748
  = case lookupOccEnv in_scope occ of
	Nothing -> 	-- Not already used: make it used
		   (extendOccEnv in_scope occ 1, occ)

	Just n  -> 	-- Already used: make a new guess, 
			-- change the guess base, and try again
		   tidyOccName  (extendOccEnv in_scope occ (n+1))
749 750 751
                                (mkOccName occ_sp (base_occ ++ show n))
  where
    base_occ = reverse (dropWhile isDigit (reverse (unpackFS fs)))
752 753
\end{code}

754 755 756 757 758 759 760
%************************************************************************
%*									*
		Stuff for dealing with tuples
%*									*
%************************************************************************

\begin{code}
batterseapower's avatar
batterseapower committed
761 762
mkTupleOcc :: NameSpace -> TupleSort -> Arity -> OccName
mkTupleOcc ns sort ar = OccName ns (mkFastString str)
763
  where
764 765
 	-- no need to cache these, the caching is done in the caller
	-- (TysWiredIn.mk_tuple)
batterseapower's avatar
batterseapower committed
766
    str = case sort of
767 768 769
		UnboxedTuple    -> '(' : '#' : commas ++ "#)"
		BoxedTuple      -> '(' : commas ++ ")"
                ConstraintTuple -> '(' : commas ++ ")"
batterseapower's avatar
batterseapower committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
                  -- Cute hack: reuse the standard tuple OccNames (and hence code)
                  -- for fact tuples, but give them different Uniques so they are not equal.
                  --
                  -- You might think that this will go wrong because isTupleOcc_maybe won't
                  -- be able to tell the difference between boxed tuples and fact tuples. BUT:
                  --  1. Fact tuples never occur directly in user code, so it doesn't matter
                  --     that we can't detect them in Orig OccNames originating from the user
                  --     programs (or those built by setRdrNameSpace used on an Exact tuple Name)
                  --  2. Interface files have a special representation for tuple *occurrences*
                  --     in IfaceTyCons, their workers (in IfaceSyn) and their DataCons (in case
                  --     alternatives). Thus we don't rely on the OccName to figure out what kind
                  --     of tuple an occurrence was trying to use in these situations.
                  --  3. We *don't* represent tuple data type declarations specially, so those
                  --     are still turned into wired-in names via isTupleOcc_maybe. But that's OK
                  --     because we don't actually need to declare fact tuples thanks to this hack.
                  --
                  -- So basically any OccName like (,,) flowing to isTupleOcc_maybe will always
                  -- refer to the standard boxed tuple. Cool :-)
788 789

    commas = take (ar-1) (repeat ',')
790

batterseapower's avatar
batterseapower committed
791
isTupleOcc_maybe :: OccName -> Maybe (NameSpace, TupleSort, Arity)
792 793 794
-- Tuples are special, because there are so many of them!
isTupleOcc_maybe (OccName ns fs)
  = case unpackFS fs of
batterseapower's avatar
batterseapower committed
795 796 797
	'(':'#':',':rest     -> Just (ns, UnboxedTuple, 2 + count_commas rest)
	'(':',':rest         -> Just (ns, BoxedTuple,   2 + count_commas rest)
	_other               -> Nothing
798
  where
799 800
    count_commas (',':rest) = 1 + count_commas rest
    count_commas _          = 0
801
\end{code}
802 803 804

%************************************************************************
%*									*
apt's avatar
apt committed
805
\subsection{Lexical categories}
806 807 808
%*									*
%************************************************************************

809 810
These functions test strings to see if they fit the lexical categories
defined in the Haskell report.
811 812

\begin{code}
813 814
isLexCon,   isLexVar,    isLexId,    isLexSym    :: FastString -> Bool
isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
815

816 817
isLexCon cs = isLexConId  cs || isLexConSym cs
isLexVar cs = isLexVarId  cs || isLexVarSym cs
818

819 820
isLexId  cs = isLexConId  cs || isLexVarId  cs
isLexSym cs = isLexConSym cs || isLexVarSym cs
821

822
-------------
823

824
isLexConId cs				-- Prefix type or data constructors
Ian Lynagh's avatar
Ian Lynagh committed
825 826 827
  | nullFS cs	       = False		-- 	e.g. "Foo", "[]", "(,)" 
  | cs == (fsLit "[]") = True
  | otherwise	       = startsConId (headFS cs)
828

829
isLexVarId cs				-- Ordinary prefix identifiers
830
  | nullFS cs	      = False		-- 	e.g. "x", "_x"
831
  | otherwise         = startsVarId (headFS cs)
832

833
isLexConSym cs				-- Infix type or data constructors
Ian Lynagh's avatar
Ian Lynagh committed
834 835 836
  | nullFS cs	       = False		--	e.g. ":-:", ":", "->"
  | cs == (fsLit "->") = True
  | otherwise	       = startsConSym (headFS cs)
837 838

isLexVarSym cs				-- Infix identifiers
839
  | nullFS cs	      = False		-- 	e.g. "+"
840
  | otherwise         = startsVarSym (headFS cs)
841 842

-------------
843
startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
844
startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
845
startsConSym c = c == ':'				-- Infix data constructors
846 847
startsVarId c  = isLower c || c == '_'	-- Ordinary Ids
startsConId c  = isUpper c || c == '('	-- Ordinary type constructors and data constructors
848

849
isSymbolASCII :: Char -> Bool
850
isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
851
\end{code}
852 853 854 855 856 857 858 859

%************************************************************************
%*									*
		Binary instance
    Here rather than BinIface because OccName is abstract
%*									*
%************************************************************************

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
\begin{code}
instance Binary NameSpace where
    put_ bh VarName = do
	    putByte bh 0
    put_ bh DataName = do
	    putByte bh 1
    put_ bh TvName = do
	    putByte bh 2
    put_ bh TcClsName = do
	    putByte bh 3
    get bh = do
	    h <- getByte bh
	    case h of
	      0 -> do return VarName
	      1 -> do return DataName
	      2 -> do return TvName
	      _ -> do return TcClsName

instance Binary OccName where
    put_ bh (OccName aa ab) = do
	    put_ bh aa
	    put_ bh ab
    get bh = do
	  aa <- get bh
	  ab <- get bh
	  return (OccName aa ab)
\end{code}