OccName.lhs 34.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
{-# 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
module OccName (
26 27
        -- * The 'NameSpace' type
        NameSpace, -- Abstract
28 29

        nameSpacesRelated,
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

        -- ** Construction
        -- $real_vs_source_data_constructors
        tcName, clsName, tcClsName, dataName, varName,
        tvName, srcDataName,

        -- ** Pretty Printing
        pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,

        -- * The 'OccName' type
        OccName,        -- Abstract, instance of Outputable
        pprOccName,

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

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

73 74 75 76 77
        -- ** Deconstruction
        occNameFS, occNameString, occNameSpace,

        isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
        parenSymOcc, startsWithUnderscore,
78

79
        isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
80

81 82 83 84
        -- * The 'OccEnv' type
        OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
        lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
        occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
85
        extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
86
        alterOccEnv, pprOccEnv,
87

88 89 90 91 92
        -- * The 'OccSet' type
        OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
        extendOccSetList,
        unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
        foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
93

94 95 96 97 98 99 100
        -- * Tidying up
        TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,

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

        -- FsEnv
        FastStringEnv, emptyFsEnv, lookupFsEnv, extendFsEnv, mkFsEnv
104 105
    ) where

Simon Marlow's avatar
Simon Marlow committed
106 107
import Util
import Unique
108
import DynFlags
109 110
import UniqFM
import UniqSet
111
import FastString
112
import Outputable
113
import Binary
Simon Marlow's avatar
Simon Marlow committed
114
import Data.Char
115
import Data.Data
116
\end{code}
117

118
%************************************************************************
119
%*                                                                      *
120
              FastStringEnv
121
%*                                                                      *
122 123
%************************************************************************

Gabor Greif's avatar
Gabor Greif committed
124
FastStringEnv can't be in FastString because the env depends on UniqFM
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

\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}

141
%************************************************************************
142
%*                                                                      *
143
\subsection{Name space}
144
%*                                                                      *
145 146 147
%************************************************************************

\begin{code}
148 149 150 151 152 153
data NameSpace = VarName        -- Variables, including "real" data constructors
               | DataName       -- "Source" data constructors
               | TvName         -- Type variables
               | TcClsName      -- Type constructors and classes; Haskell has them
                                -- in the same name space for now.
               deriving( Eq, Ord )
154
   {-! derive: Binary !-}
155

156
-- Note [Data Constructors]
157 158
-- see also: Note [Data Constructor Naming] in DataCon.lhs
--
159 160 161
-- $real_vs_source_data_constructors
-- There are two forms of data constructor:
--
162
--      [Source data constructors] The data constructors mentioned in Haskell source code
163
--
164
--      [Real data constructors] The data constructors of the representation type, which may not be the same as the source type
165 166 167 168 169 170 171
--
-- For example:
--
-- > data T = T !(Int, Int)
--
-- The source datacon has type @(Int, Int) -> T@
-- The real   datacon has type @Int -> Int -> T@
172 173 174
--
-- GHC chooses a representation based on the strictness etc.

twanvl's avatar
twanvl committed
175 176 177
tcName, clsName, tcClsName :: NameSpace
dataName, srcDataName      :: NameSpace
tvName, varName            :: NameSpace
178

179 180
-- Though type constructors and classes are in the same name space now,
-- the NameSpace type is abstract, so we can easily separate them later
181 182 183
tcName    = TcClsName           -- Type constructors
clsName   = TcClsName           -- Classes
tcClsName = TcClsName           -- Not sure which!
184

185
dataName    = DataName
186 187
srcDataName = DataName  -- Haskell-source data constructors should be
                        -- in the Data name space
188

189 190
tvName      = TvName
varName     = VarName
191

192 193 194 195 196 197 198 199 200 201 202
isDataConNameSpace :: NameSpace -> Bool
isDataConNameSpace DataName = True
isDataConNameSpace _        = False

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

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

204
isVarNameSpace :: NameSpace -> Bool     -- Variables or type variables, but not constructors
205 206 207 208 209 210 211 212
isVarNameSpace TvName  = True
isVarNameSpace VarName = True
isVarNameSpace _       = False

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

214
pprNameSpace :: NameSpace -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
215 216 217 218
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")
219 220 221 222

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

twanvl's avatar
twanvl committed
224
pprNameSpaceBrief :: NameSpace -> SDoc
225 226
pprNameSpaceBrief DataName  = char 'd'
pprNameSpaceBrief VarName   = char 'v'
Ian Lynagh's avatar
Ian Lynagh committed
227 228
pprNameSpaceBrief TvName    = ptext (sLit "tv")
pprNameSpaceBrief TcClsName = ptext (sLit "tc")
dreixel's avatar
dreixel committed
229 230 231

-- demoteNameSpace lowers the NameSpace if possible.  We can not know
-- in advance, since a TvName can appear in an HsTyVar.
232
-- See Note [Demotion] in RnEnv
dreixel's avatar
dreixel committed
233 234 235 236 237
demoteNameSpace :: NameSpace -> Maybe NameSpace
demoteNameSpace VarName = Nothing
demoteNameSpace DataName = Nothing
demoteNameSpace TvName = Nothing
demoteNameSpace TcClsName = Just DataName
238 239 240 241
\end{code}


%************************************************************************
242
%*                                                                      *
243
\subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
244
%*                                                                      *
245 246 247
%************************************************************************

\begin{code}
248
data OccName = OccName
249
    { occNameSpace  :: !NameSpace
250
    , occNameFS     :: !FastString
251
    }
Ian Lynagh's avatar
Ian Lynagh committed
252
    deriving Typeable
253 254 255 256 257 258 259 260
\end{code}


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

instance Ord OccName where
261 262 263
        -- Compares lexicographically, *not* by Unique of the string
    compare (OccName sp1 s1) (OccName sp2 s2)
        = (s1  `compare` s2) `thenCmp` (sp1 `compare` sp2)
264 265 266 267 268 269

instance Data OccName where
  -- don't traverse?
  toConstr _   = abstractConstr "OccName"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = mkNoRepType "OccName"
270 271 272

instance HasOccName OccName where
  occName = id
273 274 275 276
\end{code}


%************************************************************************
277
%*                                                                      *
278
\subsection{Printing}
279
%*                                                                      *
280
%************************************************************************
281

282 283
\begin{code}
instance Outputable OccName where
284
    ppr = pprOccName
285

286 287 288 289 290
instance OutputableBndr OccName where
    pprBndr _ = ppr
    pprInfixOcc n = pprInfixVar (isSymOcc n) (ppr n)
    pprPrefixOcc n = pprPrefixVar (isSymOcc n) (ppr n)

291
pprOccName :: OccName -> SDoc
292
pprOccName (OccName sp occ)
293
  = getPprStyle $ \ sty ->
294
    if codeStyle sty
Ian Lynagh's avatar
Ian Lynagh committed
295
    then ztext (zEncodeFS occ)
296 297 298
    else pp_occ <> pp_debug sty
  where
    pp_debug sty | debugStyle sty = braces (pprNameSpaceBrief sp)
299
                 | otherwise      = empty
300

301
    pp_occ = sdocWithDynFlags $ \dflags ->
ian@well-typed.com's avatar
ian@well-typed.com committed
302
             if gopt Opt_SuppressUniques dflags
303 304
             then text (strip_th_unique (unpackFS occ))
             else ftext occ
305

306
        -- See Note [Suppressing uniques in OccNames]
307 308 309
    strip_th_unique ('[' : c : _) | isAlphaNum c = []
    strip_th_unique (c : cs) = c : strip_th_unique cs
    strip_th_unique []       = []
310 311
\end{code}

312 313 314 315 316
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
317 318

%************************************************************************
319
%*                                                                      *
320
\subsection{Construction}
321
%*                                                                      *
322
%************************************************************************
323 324

\begin{code}
325
mkOccName :: NameSpace -> String -> OccName
326
mkOccName occ_sp str = OccName occ_sp (mkFastString str)
327

328 329
mkOccNameFS :: NameSpace -> FastString -> OccName
mkOccNameFS occ_sp fs = OccName occ_sp fs
330

331 332
mkVarOcc :: String -> OccName
mkVarOcc s = mkOccName varName s
333

334 335
mkVarOccFS :: FastString -> OccName
mkVarOccFS fs = mkOccNameFS varName fs
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
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
360 361 362 363 364 365 366

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

368
-- Name spaces are related if there is a chance to mean the one when one writes
Gabor Greif's avatar
Gabor Greif committed
369
-- the other, i.e. variables <-> data constructors and type variables <-> type constructors
370 371
nameSpacesRelated :: NameSpace -> NameSpace -> Bool
nameSpacesRelated ns1 ns2 = ns1 == ns2 || otherNameSpace ns1 == ns2
372 373 374 375 376 377 378 379

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


380

Gabor Greif's avatar
Gabor Greif committed
381
{- | Other names in the compiler add additional information to an OccName.
382 383 384
This class provides a consistent way to access the underlying OccName. -}
class HasOccName name where
  occName :: name -> OccName
385
\end{code}
386

387

388
%************************************************************************
389 390 391
%*                                                                      *
                Environments
%*                                                                      *
392 393 394 395
%************************************************************************

OccEnvs are used mainly for the envts in ModIfaces.

396 397
Note [The Unique of an OccName]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
398
They are efficient, because FastStrings have unique Int# keys.  We assume
399
this key is less than 2^24, and indeed FastStrings are allocated keys
400 401 402
sequentially starting at 0.

So we can make a Unique using
403
        mkUnique ns key  :: Unique
404
where 'ns' is a Char representing the name space.  This in turn makes it
405 406 407 408
easy to build an OccEnv.

\begin{code}
instance Uniquable OccName where
409 410 411 412 413
      -- 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
414

415
newtype OccEnv a = A (UniqFM a)
416 417 418 419 420 421 422

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
423
mkOccEnv_C   :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
424 425 426 427
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
428
extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
429
plusOccEnv     :: OccEnv a -> OccEnv a -> OccEnv a
430
plusOccEnv_C   :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
431
mapOccEnv      :: (a->b) -> OccEnv a -> OccEnv b
432
delFromOccEnv      :: OccEnv a -> OccName -> OccEnv a
433
delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
434 435
filterOccEnv       :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
alterOccEnv        :: (Maybe elt -> Maybe elt) -> OccEnv elt -> OccName -> OccEnv elt
436

437 438
emptyOccEnv      = A emptyUFM
unitOccEnv x y = A $ unitUFM x y
439 440 441 442
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
443 444 445 446 447
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
448
extendOccEnv_C f (A x) y z   = A $ addToUFM_C f x y z
449
extendOccEnv_Acc f g (A x) y z   = A $ addToUFM_Acc f g x y z
450
mapOccEnv f (A x)        = A $ mapUFM f x
451 452 453 454
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
455
alterOccEnv fn (A y) k     = A $ alterUFM fn y k
456 457

instance Outputable a => Outputable (OccEnv a) where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
458 459 460 461
    ppr x = pprOccEnv ppr x

pprOccEnv :: (a -> SDoc) -> OccEnv a -> SDoc
pprOccEnv ppr_elt (A env) = pprUniqFM ppr_elt env
462

463
type OccSet = UniqSet OccName
464

465 466
emptyOccSet       :: OccSet
unitOccSet        :: OccName -> OccSet
467 468 469
mkOccSet          :: [OccName] -> OccSet
extendOccSet      :: OccSet -> OccName -> OccSet
extendOccSetList  :: OccSet -> [OccName] -> OccSet
470
unionOccSets      :: OccSet -> OccSet -> OccSet
471
unionManyOccSets  :: [OccSet] -> OccSet
472 473 474 475 476
minusOccSet       :: OccSet -> OccSet -> OccSet
elemOccSet        :: OccName -> OccSet -> Bool
occSetElts        :: OccSet -> [OccName]
foldOccSet        :: (OccName -> b -> b) -> b -> OccSet -> b
isEmptyOccSet     :: OccSet -> Bool
477 478 479
intersectOccSet   :: OccSet -> OccSet -> OccSet
intersectsOccSet  :: OccSet -> OccSet -> Bool

480 481
emptyOccSet       = emptyUniqSet
unitOccSet        = unitUniqSet
482
mkOccSet          = mkUniqSet
483
extendOccSet      = addOneToUniqSet
484 485 486
extendOccSetList  = addListToUniqSet
unionOccSets      = unionUniqSets
unionManyOccSets  = unionManyUniqSets
487
minusOccSet       = minusUniqSet
488 489
elemOccSet        = elementOfUniqSet
occSetElts        = uniqSetToList
490
foldOccSet        = foldUniqSet
491 492 493 494 495 496
isEmptyOccSet     = isEmptyUniqSet
intersectOccSet   = intersectUniqSets
intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
\end{code}


497
%************************************************************************
498
%*                                                                      *
499
\subsection{Predicates and taking them apart}
500
%*                                                                      *
501 502
%************************************************************************

503 504
\begin{code}
occNameString :: OccName -> String
505
occNameString (OccName _ s) = unpackFS s
506

507 508
setOccNameSpace :: NameSpace -> OccName -> OccName
setOccNameSpace sp (OccName _ occ) = OccName sp occ
509

510
isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
511 512

isVarOcc (OccName VarName _) = True
twanvl's avatar
twanvl committed
513
isVarOcc _                   = False
514

515
isTvOcc (OccName TvName _) = True
twanvl's avatar
twanvl committed
516
isTvOcc _                  = False
517

518
isTcOcc (OccName TcClsName _) = True
twanvl's avatar
twanvl committed
519
isTcOcc _                     = False
520

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

528
isDataOcc (OccName DataName _) = True
twanvl's avatar
twanvl committed
529
isDataOcc _                    = False
530

531 532 533 534 535
-- | 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
536
-- Pretty inefficient!
537

538
-- | Test if the 'OccName' is that for any operator (whether
539 540
-- it is a data constructor or variable or whatever)
isSymOcc :: OccName -> Bool
541
isSymOcc (OccName DataName s)  = isLexConSym s
542
isSymOcc (OccName TcClsName s) = isLexSym s
543
isSymOcc (OccName VarName s)   = isLexSym s
544
isSymOcc (OccName TvName s)    = isLexSym s
545
-- Pretty inefficient!
546 547

parenSymOcc :: OccName -> SDoc -> SDoc
548
-- ^ Wrap parens around an operator
549
parenSymOcc occ doc | isSymOcc occ = parens doc
550
                    | otherwise    = doc
551
\end{code}
552 553


554
\begin{code}
555 556 557 558
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
559 560
                             ('_' : _) -> True
                             _other    -> False
561 562 563
\end{code}


564
%************************************************************************
565
%*                                                                      *
566
\subsection{Making system names}
567
%*                                                                      *
568
%************************************************************************
569

570
Here's our convention for splitting up the interface file name space:
571

572 573
   d...         dictionary identifiers
                (local variables, so no name-clash worries)
574

575 576 577
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
578

579 580 581 582 583 584 585
   $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
586 587 588 589
   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
590

591
        :...            keywords (export:, letrec: etc.)
apt's avatar
apt committed
592
--- I THINK THIS IS WRONG!
593

594 595
This knowledge is encoded in the following functions.

apt's avatar
apt committed
596
@mk_deriv@ generates an @OccName@ from the prefix and a string.
597
NB: The string must already be encoded!
598 599

\begin{code}
600 601 602 603
mk_deriv :: NameSpace
         -> String              -- Distinguishes one sort of derived name from another
         -> String
         -> OccName
604

605
mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
606 607

isDerivedOccName :: OccName -> Bool
608
isDerivedOccName occ =
609 610 611 612
   case occNameString occ of
     '$':c:_ | isAlphaNum c -> True
     ':':c:_ | isAlphaNum c -> True
     _other                 -> False
613
\end{code}
614

615
\begin{code}
cactus's avatar
cactus committed
616
mkDataConWrapperOcc, mkWorkerOcc, mkMatcherOcc, mkDefaultMethodOcc,
617
        mkGenDefMethodOcc, mkDerivedTyConOcc, mkClassDataConOcc, mkDictOcc,
618 619 620 621
        mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkRepEqOcc, mkGenOcc1, mkGenOcc2,
        mkGenD, mkGenR, mkGen1R, mkGenRCo,
        mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
        mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
batterseapower's avatar
batterseapower committed
622
        mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc
623 624
   :: OccName -> OccName

625
-- These derived variables have a prefix that no Haskell value could have
626
mkDataConWrapperOcc = mk_simple_deriv varName  "$W"
627
mkWorkerOcc         = mk_simple_deriv varName  "$w"
cactus's avatar
cactus committed
628
mkMatcherOcc        = mk_simple_deriv varName  "$m"
629
mkDefaultMethodOcc  = mk_simple_deriv varName  "$dm"
630
mkGenDefMethodOcc   = mk_simple_deriv varName  "$gdm"
631
mkClassOpAuxOcc     = mk_simple_deriv varName  "$c"
632 633 634 635 636 637
mkDerivedTyConOcc   = mk_simple_deriv tcName   ":"      -- The : prefix makes sure it classifies as a tycon/datacon
mkClassDataConOcc   = mk_simple_deriv dataName "D:"     -- We go straight to the "real" data con
                                                        -- for datacons from classes
mkDictOcc           = mk_simple_deriv varName  "$d"
mkIPOcc             = mk_simple_deriv varName  "$i"
mkSpecOcc           = mk_simple_deriv varName  "$s"
638
mkForeignExportOcc  = mk_simple_deriv varName  "$f"
639
mkRepEqOcc          = mk_simple_deriv tvName   "$r"      -- In RULES involving Coercible
640
mkNewTyCoOcc        = mk_simple_deriv tcName   "NTCo:"  -- Coercion for newtypes
641
mkInstTyCoOcc       = mk_simple_deriv tcName   "TFCo:"   -- Coercion for type functions
642
mkEqPredCoOcc       = mk_simple_deriv tcName   "$co"
643

644 645 646 647 648
-- used in derived instances
mkCon2TagOcc        = mk_simple_deriv varName  "$con2tag_"
mkTag2ConOcc        = mk_simple_deriv varName  "$tag2con_"
mkMaxTagOcc         = mk_simple_deriv varName  "$maxtag_"

649
-- Generic derivable classes (old)
650
mkGenOcc1           = mk_simple_deriv varName  "$gfrom"
651
mkGenOcc2           = mk_simple_deriv varName  "$gto"
652

653 654
-- Generic deriving mechanism (new)
mkGenD         = mk_simple_deriv tcName "D1"
655 656

mkGenC :: OccName -> Int -> OccName
657
mkGenC occ m   = mk_deriv tcName ("C1_" ++ show m) (occNameString occ)
658 659

mkGenS :: OccName -> Int -> Int -> OccName
660 661 662
mkGenS occ m n = mk_deriv tcName ("S1_" ++ show m ++ "_" ++ show n)
                   (occNameString occ)

663
mkGenR   = mk_simple_deriv tcName "Rep_"
jpm@cs.ox.ac.uk's avatar
jpm@cs.ox.ac.uk committed
664
mkGen1R  = mk_simple_deriv tcName "Rep1_"
665
mkGenRCo = mk_simple_deriv tcName "CoRep_"
666

667 668 669
-- data T = MkT ... deriving( Data ) needs definitions for
--      $tT   :: Data.Generics.Basics.DataType
--      $cMkT :: Data.Generics.Basics.Constr
670 671 672
mkDataTOcc = mk_simple_deriv varName  "$t"
mkDataCOcc = mk_simple_deriv varName  "$c"

673
-- Vectorisation
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
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:"
689

twanvl's avatar
twanvl committed
690
mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
691
mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
692

693 694 695 696
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)

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

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

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

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

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

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

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

759 760 761 762 763 764 765 766 767 768 769 770 771 772
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
773
        data (Ord a) => Foo a = MkFoo a
774

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

\begin{code}
mkMethodOcc :: OccName -> OccName
twanvl's avatar
twanvl committed
780 781
mkMethodOcc occ@(OccName VarName _) = occ
mkMethodOcc occ                     = mk_simple_deriv varName "$m" occ
782 783
\end{code}

784 785

%************************************************************************
786
%*                                                                      *
787
\subsection{Tidying them up}
788
%*                                                                      *
789 790 791 792 793 794 795 796 797
%************************************************************************

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.

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

802 803 804 805 806 807 808 809
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
810
           There is no guarantee that "FSn" is available;
811 812 813 814 815 816
           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.

817
\begin{code}
818
type TidyOccEnv = UniqFM Int    -- The in-scope OccNames
819
  -- See Note [TidyOccEnv]
820

twanvl's avatar
twanvl committed
821
emptyTidyOccEnv :: TidyOccEnv
822
emptyTidyOccEnv = emptyUFM
823

824
initTidyOccEnv :: [OccName] -> TidyOccEnv       -- Initialise with names to avoid!
825 826 827
initTidyOccEnv = foldl add emptyUFM
  where
    add env (OccName _ fs) = addToUFM env fs 1
828 829

tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
830 831
tidyOccName env occ@(OccName occ_sp fs)
  = case lookupUFM env fs of
832 833
        Just n  -> find n
        Nothing -> (addToUFM env fs 1, occ)
834
  where
835
    base :: String  -- Drop trailing digits (see Note [TidyOccEnv])
836
    base = dropWhileEndLE isDigit (unpackFS fs)
837 838

    find n
839 840 841 842 843 844 845 846 847 848 849
      = 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)
850 851
\end{code}

852
%************************************************************************
853
%*                                                                      *
apt's avatar
apt committed
854
\subsection{Lexical categories}
855
%*                                                                      *
856 857
%************************************************************************

858 859
These functions test strings to see if they fit the lexical categories
defined in the Haskell report.
860

861 862 863 864 865 866 867 868 869
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.

870
\begin{code}
871 872
isLexCon,   isLexVar,    isLexId,    isLexSym    :: FastString -> Bool
isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
873

874 875
isLexCon cs = isLexConId  cs || isLexConSym cs
isLexVar cs = isLexVarId  cs || isLexVarSym cs
876

877 878
isLexId  cs = isLexConId  cs || isLexVarId  cs
isLexSym cs = isLexConSym cs || isLexVarSym cs
879

880
-------------
881

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

887 888
isLexVarId cs                           -- Ordinary prefix identifiers
  | nullFS cs         = False           --      e.g. "x", "_x"
889
  | otherwise         = startsVarId (headFS cs)
890

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

896
isLexVarSym fs                          -- Infix identifiers e.g. "+"
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
897 898
  | fs == (fsLit "~R#") = True
  | otherwise
899 900 901
  = 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
902
        -- See Note [Classification of generated names]
903 904

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

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

isVarSymChar :: Char -> Bool
isVarSymChar c = c == ':' || startsVarSym c
916
\end{code}
917 918

%************************************************************************
919 920
%*                                                                      *
                Binary instance
921
    Here rather than BinIface because OccName is abstract
922
%*                                                                      *
923 924
%************************************************************************

925 926 927 <