OccName.lhs 31.6 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
{-# LANGUAGE DeriveDataTypeable #-}

9
10
11
12
13
-- |
-- #name_types#
-- GHC uses several kinds of name internally:
--
-- * 'OccName.OccName' represents names as strings with just a little more information:
Thomas Schilling's avatar
Thomas Schilling committed
14
--   the \"namespace\" that the name came from, e.g. the namespace of value, type constructors or
15
16
17
18
19
20
21
22
23
--   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
24

25
{-# OPTIONS_GHC -fno-warn-tabs #-}
Ian Lynagh's avatar
Ian Lynagh committed
26
27
28
-- 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
29
--     http://ghc.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
Ian Lynagh's avatar
Ian Lynagh committed
30
31
-- for details

32
module OccName (
33
34
	-- * The 'NameSpace' type
	NameSpace, -- Abstract
35
36

        nameSpacesRelated,
37
38
39
40
	
	-- ** Construction
	-- $real_vs_source_data_constructors
	tcName, clsName, tcClsName, dataName, varName, 
41
	tvName, srcDataName,
42

43
	-- ** Pretty Printing
44
45
	pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,

46
	-- * The 'OccName' type
47
	OccName, 	-- Abstract, instance of Outputable
48
	pprOccName, 
49

50
51
52
	-- ** Construction	
	mkOccName, mkOccNameFS, 
	mkVarOcc, mkVarOccFS,
53
54
55
56
	mkDataOcc, mkDataOccFS,
	mkTyVarOcc, mkTyVarOccFS,
	mkTcOcc, mkTcOccFS,
	mkClsOcc, mkClsOccFS,
57
        mkDFunOcc,
58
	setOccNameSpace,
dreixel's avatar
dreixel committed
59
        demoteOccName,
60
        HasOccName(..),
61

62
	-- ** Derived 'OccName's
63
        isDerivedOccName,
Gergő Érdi's avatar
Gergő Érdi committed
64
	mkDataConWrapperOcc, mkWorkerOcc, mkMatcherOcc, mkDefaultMethodOcc,
65
        mkGenDefMethodOcc, 
66
	mkDerivedTyConOcc, mkNewTyCoOcc, mkClassOpAuxOcc,
67
        mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
68
69
70
	mkClassDataConOcc, mkDictOcc, mkIPOcc,
	mkSpecOcc, mkForeignExportOcc, mkRepEqOcc, mkGenOcc1, mkGenOcc2,
	mkGenD, mkGenR, mkGen1R, mkGenRCo, mkGenC, mkGenS,
71
        mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
72
	mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
73
	mkInstTyCoOcc, mkEqPredCoOcc,
74
        mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
75
76
        mkPDataTyConOcc,  mkPDataDataConOcc,
	mkPDatasTyConOcc, mkPDatasDataConOcc,
77
        mkPReprTyConOcc, 
78
        mkPADFunOcc,
79
80
81
82
83

	-- ** Deconstruction
	occNameFS, occNameString, occNameSpace, 

	isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
84
	parenSymOcc, startsWithUnderscore, 
85
86
	
	isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
87

88
	-- * The 'OccEnv' type
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
89
	OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
90
	lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
91
	occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
92
        extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
93
        alterOccEnv, 
94

95
	-- * The 'OccSet' type
96
97
	OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet, 
	extendOccSetList,
98
99
	unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts, 
	foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
100
                  
101
	-- * Tidying up
102
103
	TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,

104
	-- * Lexical characteristics of Haskell names
105
106
	isLexCon, isLexVar, isLexId, isLexSym,
	isLexConId, isLexConSym, isLexVarId, isLexVarSym,
107
108
109
110
	startsVarSym, startsVarId, startsConSym, startsConId,

        -- FsEnv
        FastStringEnv, emptyFsEnv, lookupFsEnv, extendFsEnv, mkFsEnv
111
112
    ) where

Simon Marlow's avatar
Simon Marlow committed
113
114
import Util
import Unique
115
import DynFlags
116
117
import UniqFM
import UniqSet
118
import FastString
119
import Outputable
120
import Binary
Simon Marlow's avatar
Simon Marlow committed
121
import Data.Char
122
import Data.Data
123
\end{code}
124

125
126
127
128
129
130
%************************************************************************
%*									*
              FastStringEnv
%*									*
%************************************************************************

Gabor Greif's avatar
Gabor Greif committed
131
FastStringEnv can't be in FastString because the env depends on UniqFM
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

\begin{code}
type FastStringEnv a = UniqFM a         -- Keyed by FastString


emptyFsEnv  :: FastStringEnv a
lookupFsEnv :: FastStringEnv a -> FastString -> Maybe a
extendFsEnv :: FastStringEnv a -> FastString -> a -> FastStringEnv a
mkFsEnv     :: [(FastString,a)] -> FastStringEnv a

emptyFsEnv  = emptyUFM
lookupFsEnv = lookupUFM
extendFsEnv = addToUFM
mkFsEnv     = listToUFM
\end{code}

148
149
150
151
152
153
154
%************************************************************************
%*									*
\subsection{Name space}
%*									*
%************************************************************************

\begin{code}
155
156
data NameSpace = VarName	-- Variables, including "real" data constructors
	       | DataName	-- "Source" data constructors 
157
158
	       | TvName		-- Type variables
	       | TcClsName	-- Type constructors and classes; Haskell has them
159
				-- in the same name space for now.
160
	       deriving( Eq, Ord )
161
   {-! derive: Binary !-}
162

163
164
165
-- Note [Data Constructors]  
-- see also: Note [Data Constructor Naming] in DataCon.lhs
--
166
167
168
169
170
171
172
173
174
175
176
177
178
-- $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@
179
180
181
--
-- GHC chooses a representation based on the strictness etc.

twanvl's avatar
twanvl committed
182
183
184
tcName, clsName, tcClsName :: NameSpace
dataName, srcDataName      :: NameSpace
tvName, varName            :: NameSpace
185

186
187
188
189
190
191
-- 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!

192
193
194
dataName    = DataName
srcDataName = DataName	-- Haskell-source data constructors should be
			-- in the Data name space
195

196
197
tvName      = TvName
varName     = VarName
198

199
200
201
202
203
204
205
206
207
208
209
isDataConNameSpace :: NameSpace -> Bool
isDataConNameSpace DataName = True
isDataConNameSpace _        = False

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

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

211
212
213
214
215
216
217
218
219
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
220

221
pprNameSpace :: NameSpace -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
222
223
224
225
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")
226
227
228
229

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

twanvl's avatar
twanvl committed
231
pprNameSpaceBrief :: NameSpace -> SDoc
232
233
pprNameSpaceBrief DataName  = char 'd'
pprNameSpaceBrief VarName   = char 'v'
Ian Lynagh's avatar
Ian Lynagh committed
234
235
pprNameSpaceBrief TvName    = ptext (sLit "tv")
pprNameSpaceBrief TcClsName = ptext (sLit "tc")
dreixel's avatar
dreixel committed
236
237
238

-- demoteNameSpace lowers the NameSpace if possible.  We can not know
-- in advance, since a TvName can appear in an HsTyVar.
239
-- See Note [Demotion] in RnEnv
dreixel's avatar
dreixel committed
240
241
242
243
244
demoteNameSpace :: NameSpace -> Maybe NameSpace
demoteNameSpace VarName = Nothing
demoteNameSpace DataName = Nothing
demoteNameSpace TvName = Nothing
demoteNameSpace TcClsName = Just DataName
245
246
247
248
249
250
251
252
253
254
\end{code}


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

\begin{code}
255
data OccName = OccName 
256
    { occNameSpace  :: !NameSpace
257
    , occNameFS     :: !FastString
258
    }
Ian Lynagh's avatar
Ian Lynagh committed
259
    deriving Typeable
260
261
262
263
264
265
266
267
\end{code}


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

instance Ord OccName where
268
269
270
	-- Compares lexicographically, *not* by Unique of the string
    compare (OccName sp1 s1) (OccName sp2 s2) 
	= (s1  `compare` s2) `thenCmp` (sp1 `compare` sp2)
271
272
273
274
275
276

instance Data OccName where
  -- don't traverse?
  toConstr _   = abstractConstr "OccName"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = mkNoRepType "OccName"
277
278
279

instance HasOccName OccName where
  occName = id
280
281
282
283
284
285
286
287
288
289
290
\end{code}


%************************************************************************
%*									*
\subsection{Printing}
%*									*
%************************************************************************
 
\begin{code}
instance Outputable OccName where
291
    ppr = pprOccName
292

293
294
295
296
297
instance OutputableBndr OccName where
    pprBndr _ = ppr
    pprInfixOcc n = pprInfixVar (isSymOcc n) (ppr n)
    pprPrefixOcc n = pprPrefixVar (isSymOcc n) (ppr n)

298
pprOccName :: OccName -> SDoc
299
300
pprOccName (OccName sp occ) 
  = getPprStyle $ \ sty ->
301
    if codeStyle sty 
Ian Lynagh's avatar
Ian Lynagh committed
302
    then ztext (zEncodeFS occ)
303
304
305
306
307
    else pp_occ <> pp_debug sty
  where
    pp_debug sty | debugStyle sty = braces (pprNameSpaceBrief sp)
	         | otherwise      = empty

308
    pp_occ = sdocWithDynFlags $ \dflags ->
ian@well-typed.com's avatar
ian@well-typed.com committed
309
             if gopt Opt_SuppressUniques dflags
310
311
             then text (strip_th_unique (unpackFS occ))
             else ftext occ
312
313
314
315
316

	-- 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 []       = []
317
318
\end{code}

319
320
321
322
323
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
324
325
326
327
328

%************************************************************************
%*									*
\subsection{Construction}
%*									*
329
%************************************************************************
330
331

\begin{code}
332
mkOccName :: NameSpace -> String -> OccName
333
mkOccName occ_sp str = OccName occ_sp (mkFastString str)
334

335
336
mkOccNameFS :: NameSpace -> FastString -> OccName
mkOccNameFS occ_sp fs = OccName occ_sp fs
337

338
339
mkVarOcc :: String -> OccName
mkVarOcc s = mkOccName varName s
340

341
342
mkVarOccFS :: FastString -> OccName
mkVarOccFS fs = mkOccNameFS varName fs
343

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
dreixel's avatar
dreixel committed
367
368
369
370
371
372
373

-- demoteOccName lowers the Namespace of OccName.
-- see Note [Demotion]
demoteOccName :: OccName -> Maybe OccName
demoteOccName (OccName space name) = do
  space' <- demoteNameSpace space
  return $ OccName space' name
374

375
376
377
378
-- Name spaces are related if there is a chance to mean the one when one writes
-- the other, i.e. variables <-> data construtors and type variables <-> type constructors
nameSpacesRelated :: NameSpace -> NameSpace -> Bool
nameSpacesRelated ns1 ns2 = ns1 == ns2 || otherNameSpace ns1 == ns2
379
380
381
382
383
384
385
386

otherNameSpace :: NameSpace -> NameSpace
otherNameSpace VarName = DataName
otherNameSpace DataName = VarName
otherNameSpace TvName = TcClsName
otherNameSpace TcClsName = TvName


387

388
389
390
391
{- | Other names in the compiler add aditional information to an OccName.
This class provides a consistent way to access the underlying OccName. -}
class HasOccName name where
  occName :: name -> OccName
392
\end{code}
393

394

395
396
397
398
399
400
401
402
%************************************************************************
%*									*
		Environments
%*									*
%************************************************************************

OccEnvs are used mainly for the envts in ModIfaces.

403
404
Note [The Unique of an OccName]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
405
They are efficient, because FastStrings have unique Int# keys.  We assume
406
407
408
409
this key is less than 2^24, and indeed FastStrings are allocated keys 
sequentially starting at 0.

So we can make a Unique using
410
	mkUnique ns key  :: Unique
411
where 'ns' is a Char representing the name space.  This in turn makes it
412
413
414
415
easy to build an OccEnv.

\begin{code}
instance Uniquable OccName where
416
417
418
419
420
      -- 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
421

422
newtype OccEnv a = A (UniqFM a)
423
424
425
426
427
428
429

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
430
mkOccEnv_C   :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
431
432
433
434
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
435
extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
436
plusOccEnv     :: OccEnv a -> OccEnv a -> OccEnv a
437
plusOccEnv_C   :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
438
mapOccEnv      :: (a->b) -> OccEnv a -> OccEnv b
439
440
441
delFromOccEnv 	   :: OccEnv a -> OccName -> OccEnv a
delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
filterOccEnv	   :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
Simon Peyton Jones's avatar
Simon Peyton Jones committed
442
alterOccEnv	   :: (Maybe elt -> Maybe elt) -> OccEnv elt -> OccName -> OccEnv elt
443
444
445
446
447
448
449
450
451
452
453
454
455

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
456
extendOccEnv_Acc f g (A x) y z   = A $ addToUFM_Acc f g x y z
457
458
459
460
461
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
Simon Peyton Jones's avatar
Simon Peyton Jones committed
462
alterOccEnv fn (A y) k     = A $ alterUFM fn y k
463
464
465

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

467
type OccSet = UniqSet OccName
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

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}


501
502
%************************************************************************
%*									*
503
\subsection{Predicates and taking them apart}
504
505
506
%*									*
%************************************************************************

507
508
\begin{code}
occNameString :: OccName -> String
509
occNameString (OccName _ s) = unpackFS s
510

511
512
setOccNameSpace :: NameSpace -> OccName -> OccName
setOccNameSpace sp (OccName _ occ) = OccName sp occ
513

514
isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
515
516

isVarOcc (OccName VarName _) = True
twanvl's avatar
twanvl committed
517
isVarOcc _                   = False
518

519
isTvOcc (OccName TvName _) = True
twanvl's avatar
twanvl committed
520
isTvOcc _                  = False
521

522
isTcOcc (OccName TcClsName _) = True
twanvl's avatar
twanvl committed
523
isTcOcc _                     = False
524

525
526
527
-- | /Value/ 'OccNames's are those that are either in 
-- the variable or data constructor namespaces
isValOcc :: OccName -> Bool
528
529
isValOcc (OccName VarName  _) = True
isValOcc (OccName DataName _) = True
twanvl's avatar
twanvl committed
530
isValOcc _                    = False
531

532
isDataOcc (OccName DataName _) = True
twanvl's avatar
twanvl committed
533
isDataOcc _                    = False
534

535
536
537
538
539
-- | 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 _                    = False
540
-- Pretty inefficient!
541
542
543
544

-- | Test if the 'OccName' is that for any operator (whether 
-- it is a data constructor or variable or whatever)
isSymOcc :: OccName -> Bool
545
isSymOcc (OccName DataName s)  = isLexConSym s
546
isSymOcc (OccName TcClsName s) = isLexSym s
547
isSymOcc (OccName VarName s)   = isLexSym s
548
isSymOcc (OccName TvName s)    = isLexSym s
549
-- Pretty inefficient!
550
551

parenSymOcc :: OccName -> SDoc -> SDoc
552
-- ^ Wrap parens around an operator
553
554
parenSymOcc occ doc | isSymOcc occ = parens doc
		    | otherwise    = doc
555
\end{code}
556
557


558
\begin{code}
559
560
561
562
563
564
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
565
566
567
\end{code}


568
569
570
571
572
%************************************************************************
%*									*
\subsection{Making system names}
%*									*
%************************************************************************
573

574
Here's our convention for splitting up the interface file name space:
575

576
577
   d...		dictionary identifiers
   		(local variables, so no name-clash worries)
578

579
580
581
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
582

583
584
585
586
587
588
589
590
591
592
593
   $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
594

595
	:...		keywords (export:, letrec: etc.)
apt's avatar
apt committed
596
--- I THINK THIS IS WRONG!
597

598
599
This knowledge is encoded in the following functions.

apt's avatar
apt committed
600
@mk_deriv@ generates an @OccName@ from the prefix and a string.
601
NB: The string must already be encoded!
602
603

\begin{code}
604
605
mk_deriv :: NameSpace 
	 -> String		-- Distinguishes one sort of derived name from another
606
	 -> String
607
	 -> OccName
608

609
mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
610
611
612
613
614
615
616

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

619
\begin{code}
Gergő Érdi's avatar
Gergő Érdi committed
620
mkDataConWrapperOcc, mkWorkerOcc, mkMatcherOcc, mkDefaultMethodOcc,
621
        mkGenDefMethodOcc, mkDerivedTyConOcc, mkClassDataConOcc, mkDictOcc,
622
	mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkRepEqOcc, mkGenOcc1, mkGenOcc2,
jpm@cs.ox.ac.uk's avatar
jpm@cs.ox.ac.uk committed
623
 	mkGenD, mkGenR, mkGen1R, mkGenRCo,
batterseapower's avatar
batterseapower committed
624
625
626
	mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
	mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
        mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc
627
628
   :: OccName -> OccName

629
-- These derived variables have a prefix that no Haskell value could have
630
mkDataConWrapperOcc = mk_simple_deriv varName  "$W"
631
mkWorkerOcc         = mk_simple_deriv varName  "$w"
Gergő Érdi's avatar
Gergő Érdi committed
632
mkMatcherOcc        = mk_simple_deriv varName  "$m"
633
mkDefaultMethodOcc  = mk_simple_deriv varName  "$dm"
634
mkGenDefMethodOcc   = mk_simple_deriv varName  "$gdm"
635
mkClassOpAuxOcc     = mk_simple_deriv varName  "$c"
batterseapower's avatar
batterseapower committed
636
mkDerivedTyConOcc   = mk_simple_deriv tcName   ":"	-- The : prefix makes sure it classifies as a tycon/datacon
637
mkClassDataConOcc   = mk_simple_deriv dataName "D:"	-- We go straight to the "real" data con
638
							-- for datacons from classes
639
640
641
642
mkDictOcc	    = mk_simple_deriv varName  "$d"
mkIPOcc		    = mk_simple_deriv varName  "$i"
mkSpecOcc	    = mk_simple_deriv varName  "$s"
mkForeignExportOcc  = mk_simple_deriv varName  "$f"
643
mkRepEqOcc          = mk_simple_deriv tvName   "$r"      -- In RULES involving Coercible
644
645
646
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"
647

648
649
650
651
652
-- used in derived instances
mkCon2TagOcc        = mk_simple_deriv varName  "$con2tag_"
mkTag2ConOcc        = mk_simple_deriv varName  "$tag2con_"
mkMaxTagOcc         = mk_simple_deriv varName  "$maxtag_"

653
-- Generic derivable classes (old)
654
655
656
mkGenOcc1           = mk_simple_deriv varName  "$gfrom"
mkGenOcc2           = mk_simple_deriv varName  "$gto" 

657
658
-- Generic deriving mechanism (new)
mkGenD         = mk_simple_deriv tcName "D1"
659
660

mkGenC :: OccName -> Int -> OccName
661
mkGenC occ m   = mk_deriv tcName ("C1_" ++ show m) (occNameString occ)
662
663

mkGenS :: OccName -> Int -> Int -> OccName
664
665
666
mkGenS occ m n = mk_deriv tcName ("S1_" ++ show m ++ "_" ++ show n)
                   (occNameString occ)

667
mkGenR   = mk_simple_deriv tcName "Rep_"
jpm@cs.ox.ac.uk's avatar
jpm@cs.ox.ac.uk committed
668
mkGen1R  = mk_simple_deriv tcName "Rep1_"
669
mkGenRCo = mk_simple_deriv tcName "CoRep_"
670

Gabor Greif's avatar
Gabor Greif committed
671
-- data T = MkT ... deriving( Data ) needs definitions for 
672
673
674
675
676
--	$tT   :: Data.Generics.Basics.DataType
--	$cMkT :: Data.Generics.Basics.Constr
mkDataTOcc = mk_simple_deriv varName  "$t"
mkDataCOcc = mk_simple_deriv varName  "$c"

677
-- Vectorisation
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
 mkPADFunOcc,      mkPReprTyConOcc,
 mkPDataTyConOcc,  mkPDataDataConOcc,
 mkPDatasTyConOcc, mkPDatasDataConOcc
  :: Maybe String -> OccName -> OccName
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:"
mkPDatasTyConOcc   = mk_simple_deriv_with tcName   "VPs:"
mkPDataDataConOcc  = mk_simple_deriv_with dataName "VPD:"
mkPDatasDataConOcc = mk_simple_deriv_with dataName "VPDs:"
693

twanvl's avatar
twanvl committed
694
mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
695
mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
696

697
698
699
700
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)

701
702
-- Data constructor workers are made by setting the name space
-- of the data constructor OccName (which should be a DataName)
703
-- to VarName
704
mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ 
705
706
707
\end{code}

\begin{code}
708
709
710
mkSuperDictSelOcc :: Int 	-- ^ Index of superclass, e.g. 3
		  -> OccName 	-- ^ Class, e.g. @Ord@
		  -> OccName	-- ^ Derived 'Occname', e.g. @$p3Ord@
batterseapower's avatar
batterseapower committed
711
712
mkSuperDictSelOcc index cls_tc_occ
  = mk_deriv varName "$p" (show index ++ occNameString cls_tc_occ)
713

714
715
716
mkLocalOcc :: Unique 		-- ^ Unique to combine with the 'OccName'
	   -> OccName		-- ^ Local name, e.g. @sat@
	   -> OccName		-- ^ Nice unique version, e.g. @$L23sat@
717
mkLocalOcc uniq occ
718
719
720
   = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
	-- The Unique might print with characters 
	-- that need encoding (e.g. 'z'!)
721
722
\end{code}

723
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
724
725
-- | Derive a name for the representation type constructor of a
-- @data@\/@newtype@ instance.
726
727
728
729
730
mkInstTyTcOcc :: String 		-- ^ Family name, e.g. @Map@
              -> OccSet                 -- ^ avoid these Occs
	      -> OccName		-- ^ @R:Map@
mkInstTyTcOcc str set =
  chooseUniqueOcc tcName ('R' : ':' : str) set
731
\end{code}
732
733

\begin{code}
734
mkDFunOcc :: String		-- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
735
				-- Only used in debug mode, for extra clarity
736
	  -> Bool		-- ^ Is this a hs-boot instance DFun?
737
          -> OccSet             -- ^ avoid these Occs
738
	  -> OccName		-- ^ E.g. @$f3OrdMaybe@
739
740
741
742
743

-- 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.

744
745
mkDFunOcc info_str is_boot set
  = chooseUniqueOcc VarName (prefix ++ info_str) set
746
747
748
  where
    prefix | is_boot   = "$fx"
	   | otherwise = "$f"
749
750
751
752
753
754
755
756
757
758
759
760
\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
761
762
\end{code}

763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
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
twanvl's avatar
twanvl committed
784
785
mkMethodOcc occ@(OccName VarName _) = occ
mkMethodOcc occ                     = mk_simple_deriv varName "$m" occ
786
787
\end{code}

788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805

%************************************************************************
%*									*
\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.

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
Note [TidyOccEnv]
~~~~~~~~~~~~~~~~~
type TidyOccEnv = UniqFM Int

* Domain = The OccName's FastString. These FastStrings are "taken";
           make sure that we don't re-use

* Int, n = A plausible starting point for new guesses
           There is no guarantee that "FSn" is available; 
           you must look that up in the TidyOccEnv.  But
           it's a good place to start looking.

* When looking for a renaming for "foo2" we strip off the "2" and start
  with "foo".  Otherwise if we tidy twice we get silly names like foo23.

821
\begin{code}
822
823
type TidyOccEnv = UniqFM Int	-- The in-scope OccNames
  -- See Note [TidyOccEnv]
824

twanvl's avatar
twanvl committed
825
emptyTidyOccEnv :: TidyOccEnv
826
emptyTidyOccEnv = emptyUFM
827
828

initTidyOccEnv :: [OccName] -> TidyOccEnv	-- Initialise with names to avoid!
829
830
831
initTidyOccEnv = foldl add emptyUFM
  where
    add env (OccName _ fs) = addToUFM env fs 1
832
833

tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
834
835
836
837
tidyOccName env occ@(OccName occ_sp fs)
  = case lookupUFM env fs of
	Just n  -> find n
	Nothing -> (addToUFM env fs 1, occ)
838
  where
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
    base :: String  -- Drop trailing digits (see Note [TidyOccEnv])
    base = reverse (dropWhile isDigit (reverse (unpackFS fs)))
 
    find n 
      = case lookupUFM env new_fs of
          Just n' -> find (n1 `max` n')
                     -- The max ensures that n increases, avoiding loops
          Nothing -> (addToUFM (addToUFM env fs n1) new_fs n1,
                      OccName occ_sp new_fs)
                     -- We update only the beginning and end of the
                     -- chain that find explores; it's a little harder to
                     -- update the middle and there's no real need.
       where
         n1 = n+1
         new_fs = mkFastString (base ++ show n)
854
855
\end{code}

856
%************************************************************************
857
%*									*
apt's avatar
apt committed
858
\subsection{Lexical categories}
859
860
861
%*									*
%************************************************************************

862
863
These functions test strings to see if they fit the lexical categories
defined in the Haskell report.
864

865
866
867
868
869
870
871
872
873
Note [Classification of generated names]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Some names generated for internal use can show up in debugging output,
e.g.  when using -ddump-simpl. These generated names start with a $
but should still be pretty-printed using prefix notation. We make sure
this is the case in isLexVarSym by only classifying a name as a symbol
if all its characters are symbols, not just its first one.

874
\begin{code}
875
876
isLexCon,   isLexVar,    isLexId,    isLexSym    :: FastString -> Bool
isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
877

878
879
isLexCon cs = isLexConId  cs || isLexConSym cs
isLexVar cs = isLexVarId  cs || isLexVarSym cs
880

881
882
isLexId  cs = isLexConId  cs || isLexVarId  cs
isLexSym cs = isLexConSym cs || isLexVarSym cs
883

884
-------------
885

886
isLexConId cs				-- Prefix type or data constructors
Ian Lynagh's avatar
Ian Lynagh committed
887
888
889
  | nullFS cs	       = False		-- 	e.g. "Foo", "[]", "(,)" 
  | cs == (fsLit "[]") = True
  | otherwise	       = startsConId (headFS cs)
890

891
isLexVarId cs				-- Ordinary prefix identifiers
892
  | nullFS cs	      = False		-- 	e.g. "x", "_x"
893
  | otherwise         = startsVarId (headFS cs)
894

895
isLexConSym cs				-- Infix type or data constructors
Ian Lynagh's avatar
Ian Lynagh committed
896
897
898
  | nullFS cs	       = False		--	e.g. ":-:", ":", "->"
  | cs == (fsLit "->") = True
  | otherwise	       = startsConSym (headFS cs)
899

900
isLexVarSym fs				-- Infix identifiers e.g. "+"
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
901
902
  | fs == (fsLit "~R#") = True
  | otherwise
903
904
905
  = case (if nullFS fs then [] else unpackFS fs) of
      [] -> False
      (c:cs) -> startsVarSym c && all isVarSymChar cs
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
906
        -- See Note [Classification of generated names]
907
908

-------------
909
startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
910
911
startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c)  -- Infix Ids
startsConSym c = c == ':'		-- Infix data constructors
912
913
startsVarId c  = isLower c || c == '_'	-- Ordinary Ids
startsConId c  = isUpper c || c == '('	-- Ordinary type constructors and data constructors
914

twanvl's avatar
twanvl committed
915
isSymbolASCII :: Char -> Bool
916
isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
917
918
919

isVarSymChar :: Char -> Bool
isVarSymChar c = c == ':' || startsVarSym c
920
\end{code}
921
922
923
924
925
926
927
928

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

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
\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}