OccName.hs 32.6 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3 4
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998
-}
5

6
{-# LANGUAGE DeriveDataTypeable, BangPatterns #-}
7

8 9 10 11 12
-- |
-- #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
13
--   the \"namespace\" that the name came from, e.g. the namespace of value, type constructors or
14 15 16 17 18 19 20 21 22
--   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
23

24
module OccName (
25 26
        -- * The 'NameSpace' type
        NameSpace, -- Abstract
27 28

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

        -- ** 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,
49
        mkDFunOcc,
50
        setOccNameSpace,
dreixel's avatar
dreixel committed
51
        demoteOccName,
52
        HasOccName(..),
53

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

76 77 78 79 80
        -- ** Deconstruction
        occNameFS, occNameString, occNameSpace,

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

82
        isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
83

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

91 92 93 94 95
        -- * The 'OccSet' type
        OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
        extendOccSetList,
        unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
        foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
96
        filterOccSet,
97

98 99 100
        -- * Tidying up
        TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,

101 102
        -- FsEnv
        FastStringEnv, emptyFsEnv, lookupFsEnv, extendFsEnv, mkFsEnv
103 104
    ) where

Simon Marlow's avatar
Simon Marlow committed
105 106
import Util
import Unique
107
import DynFlags
108 109
import UniqFM
import UniqSet
110
import FastString
Adam Gundry's avatar
Adam Gundry committed
111
import FastStringEnv
112
import Outputable
113
import Lexeme
114
import Binary
115
import Module
Simon Marlow's avatar
Simon Marlow committed
116
import Data.Char
117
import Data.Data
118

Austin Seipp's avatar
Austin Seipp committed
119 120 121
{-
************************************************************************
*                                                                      *
122
\subsection{Name space}
Austin Seipp's avatar
Austin Seipp committed
123 124 125
*                                                                      *
************************************************************************
-}
126

127 128 129 130 131 132
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 )
133
   {-! derive: Binary !-}
134

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

twanvl's avatar
twanvl committed
154 155 156
tcName, clsName, tcClsName :: NameSpace
dataName, srcDataName      :: NameSpace
tvName, varName            :: NameSpace
157

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

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

168 169
tvName      = TvName
varName     = VarName
170

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

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

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

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

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

193
pprNameSpace :: NameSpace -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
194 195 196 197
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")
198 199 200 201

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

twanvl's avatar
twanvl committed
203
pprNameSpaceBrief :: NameSpace -> SDoc
204 205
pprNameSpaceBrief DataName  = char 'd'
pprNameSpaceBrief VarName   = char 'v'
Ian Lynagh's avatar
Ian Lynagh committed
206 207
pprNameSpaceBrief TvName    = ptext (sLit "tv")
pprNameSpaceBrief TcClsName = ptext (sLit "tc")
dreixel's avatar
dreixel committed
208 209 210

-- demoteNameSpace lowers the NameSpace if possible.  We can not know
-- in advance, since a TvName can appear in an HsTyVar.
211
-- See Note [Demotion] in RnEnv
dreixel's avatar
dreixel committed
212 213 214 215 216
demoteNameSpace :: NameSpace -> Maybe NameSpace
demoteNameSpace VarName = Nothing
demoteNameSpace DataName = Nothing
demoteNameSpace TvName = Nothing
demoteNameSpace TcClsName = Just DataName
217

Austin Seipp's avatar
Austin Seipp committed
218 219 220
{-
************************************************************************
*                                                                      *
221
\subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
Austin Seipp's avatar
Austin Seipp committed
222 223 224
*                                                                      *
************************************************************************
-}
225

226
data OccName = OccName
227
    { occNameSpace  :: !NameSpace
228
    , occNameFS     :: !FastString
229
    }
Ian Lynagh's avatar
Ian Lynagh committed
230
    deriving Typeable
231 232 233 234 235

instance Eq OccName where
    (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2

instance Ord OccName where
236 237 238
        -- Compares lexicographically, *not* by Unique of the string
    compare (OccName sp1 s1) (OccName sp2 s2)
        = (s1  `compare` s2) `thenCmp` (sp1 `compare` sp2)
239 240 241 242 243 244

instance Data OccName where
  -- don't traverse?
  toConstr _   = abstractConstr "OccName"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = mkNoRepType "OccName"
245 246 247

instance HasOccName OccName where
  occName = id
248

Austin Seipp's avatar
Austin Seipp committed
249 250 251
{-
************************************************************************
*                                                                      *
252
\subsection{Printing}
Austin Seipp's avatar
Austin Seipp committed
253 254 255
*                                                                      *
************************************************************************
-}
256

257
instance Outputable OccName where
258
    ppr = pprOccName
259

260 261 262 263 264
instance OutputableBndr OccName where
    pprBndr _ = ppr
    pprInfixOcc n = pprInfixVar (isSymOcc n) (ppr n)
    pprPrefixOcc n = pprPrefixVar (isSymOcc n) (ppr n)

265
pprOccName :: OccName -> SDoc
266
pprOccName (OccName sp occ)
267
  = getPprStyle $ \ sty ->
268
    if codeStyle sty
Ian Lynagh's avatar
Ian Lynagh committed
269
    then ztext (zEncodeFS occ)
270 271 272
    else pp_occ <> pp_debug sty
  where
    pp_debug sty | debugStyle sty = braces (pprNameSpaceBrief sp)
273
                 | otherwise      = empty
274

275
    pp_occ = sdocWithDynFlags $ \dflags ->
ian@well-typed.com's avatar
ian@well-typed.com committed
276
             if gopt Opt_SuppressUniques dflags
277 278
             then text (strip_th_unique (unpackFS occ))
             else ftext occ
279

280
        -- See Note [Suppressing uniques in OccNames]
281 282 283
    strip_th_unique ('[' : c : _) | isAlphaNum c = []
    strip_th_unique (c : cs) = c : strip_th_unique cs
    strip_th_unique []       = []
284

Austin Seipp's avatar
Austin Seipp committed
285
{-
286 287 288 289 290
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
291

Austin Seipp's avatar
Austin Seipp committed
292 293
************************************************************************
*                                                                      *
294
\subsection{Construction}
Austin Seipp's avatar
Austin Seipp committed
295 296 297
*                                                                      *
************************************************************************
-}
298

299
mkOccName :: NameSpace -> String -> OccName
300
mkOccName occ_sp str = OccName occ_sp (mkFastString str)
301

302 303
mkOccNameFS :: NameSpace -> FastString -> OccName
mkOccNameFS occ_sp fs = OccName occ_sp fs
304

305 306
mkVarOcc :: String -> OccName
mkVarOcc s = mkOccName varName s
307

308 309
mkVarOccFS :: FastString -> OccName
mkVarOccFS fs = mkOccNameFS varName fs
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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
334 335 336 337 338 339 340

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

342
-- Name spaces are related if there is a chance to mean the one when one writes
Gabor Greif's avatar
Gabor Greif committed
343
-- the other, i.e. variables <-> data constructors and type variables <-> type constructors
344 345
nameSpacesRelated :: NameSpace -> NameSpace -> Bool
nameSpacesRelated ns1 ns2 = ns1 == ns2 || otherNameSpace ns1 == ns2
346 347 348 349 350 351 352 353

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


354

Gabor Greif's avatar
Gabor Greif committed
355
{- | Other names in the compiler add additional information to an OccName.
356 357 358
This class provides a consistent way to access the underlying OccName. -}
class HasOccName name where
  occName :: name -> OccName
359

Austin Seipp's avatar
Austin Seipp committed
360 361 362
{-
************************************************************************
*                                                                      *
363
                Environments
Austin Seipp's avatar
Austin Seipp committed
364 365
*                                                                      *
************************************************************************
366 367 368

OccEnvs are used mainly for the envts in ModIfaces.

369 370
Note [The Unique of an OccName]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371
They are efficient, because FastStrings have unique Int# keys.  We assume
372
this key is less than 2^24, and indeed FastStrings are allocated keys
373 374 375
sequentially starting at 0.

So we can make a Unique using
376
        mkUnique ns key  :: Unique
377
where 'ns' is a Char representing the name space.  This in turn makes it
378
easy to build an OccEnv.
Austin Seipp's avatar
Austin Seipp committed
379
-}
380 381

instance Uniquable OccName where
382 383 384 385 386
      -- 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
387

388
newtype OccEnv a = A (UniqFM a)
389 390 391 392 393 394 395

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
396
mkOccEnv_C   :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
397 398 399 400
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
401
extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
402
plusOccEnv     :: OccEnv a -> OccEnv a -> OccEnv a
403
plusOccEnv_C   :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
404
mapOccEnv      :: (a->b) -> OccEnv a -> OccEnv b
405
delFromOccEnv      :: OccEnv a -> OccName -> OccEnv a
406
delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
407 408
filterOccEnv       :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
alterOccEnv        :: (Maybe elt -> Maybe elt) -> OccEnv elt -> OccName -> OccEnv elt
409

410 411
emptyOccEnv      = A emptyUFM
unitOccEnv x y = A $ unitUFM x y
412 413 414 415
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
416 417 418 419 420
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
421
extendOccEnv_C f (A x) y z   = A $ addToUFM_C f x y z
422
extendOccEnv_Acc f g (A x) y z   = A $ addToUFM_Acc f g x y z
423
mapOccEnv f (A x)        = A $ mapUFM f x
424 425 426 427
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
428
alterOccEnv fn (A y) k     = A $ alterUFM fn y k
429 430

instance Outputable a => Outputable (OccEnv a) where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
431 432 433 434
    ppr x = pprOccEnv ppr x

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

436
type OccSet = UniqSet OccName
437

438 439
emptyOccSet       :: OccSet
unitOccSet        :: OccName -> OccSet
440 441 442
mkOccSet          :: [OccName] -> OccSet
extendOccSet      :: OccSet -> OccName -> OccSet
extendOccSetList  :: OccSet -> [OccName] -> OccSet
443
unionOccSets      :: OccSet -> OccSet -> OccSet
444
unionManyOccSets  :: [OccSet] -> OccSet
445 446 447 448 449
minusOccSet       :: OccSet -> OccSet -> OccSet
elemOccSet        :: OccName -> OccSet -> Bool
occSetElts        :: OccSet -> [OccName]
foldOccSet        :: (OccName -> b -> b) -> b -> OccSet -> b
isEmptyOccSet     :: OccSet -> Bool
450 451
intersectOccSet   :: OccSet -> OccSet -> OccSet
intersectsOccSet  :: OccSet -> OccSet -> Bool
452
filterOccSet      :: (OccName -> Bool) -> OccSet -> OccSet
453

454 455
emptyOccSet       = emptyUniqSet
unitOccSet        = unitUniqSet
456
mkOccSet          = mkUniqSet
457
extendOccSet      = addOneToUniqSet
458 459 460
extendOccSetList  = addListToUniqSet
unionOccSets      = unionUniqSets
unionManyOccSets  = unionManyUniqSets
461
minusOccSet       = minusUniqSet
462 463
elemOccSet        = elementOfUniqSet
occSetElts        = uniqSetToList
464
foldOccSet        = foldUniqSet
465 466 467
isEmptyOccSet     = isEmptyUniqSet
intersectOccSet   = intersectUniqSets
intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
468
filterOccSet      = filterUniqSet
469

Austin Seipp's avatar
Austin Seipp committed
470 471 472
{-
************************************************************************
*                                                                      *
473
\subsection{Predicates and taking them apart}
Austin Seipp's avatar
Austin Seipp committed
474 475 476
*                                                                      *
************************************************************************
-}
477

478
occNameString :: OccName -> String
479
occNameString (OccName _ s) = unpackFS s
480

481 482
setOccNameSpace :: NameSpace -> OccName -> OccName
setOccNameSpace sp (OccName _ occ) = OccName sp occ
483

484
isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
485 486

isVarOcc (OccName VarName _) = True
twanvl's avatar
twanvl committed
487
isVarOcc _                   = False
488

489
isTvOcc (OccName TvName _) = True
twanvl's avatar
twanvl committed
490
isTvOcc _                  = False
491

492
isTcOcc (OccName TcClsName _) = True
twanvl's avatar
twanvl committed
493
isTcOcc _                     = False
494

495
-- | /Value/ 'OccNames's are those that are either in
496 497
-- the variable or data constructor namespaces
isValOcc :: OccName -> Bool
498 499
isValOcc (OccName VarName  _) = True
isValOcc (OccName DataName _) = True
twanvl's avatar
twanvl committed
500
isValOcc _                    = False
501

502
isDataOcc (OccName DataName _) = True
twanvl's avatar
twanvl committed
503
isDataOcc _                    = False
504

505 506 507 508 509
-- | 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
510
-- Pretty inefficient!
511

512
-- | Test if the 'OccName' is that for any operator (whether
513 514
-- it is a data constructor or variable or whatever)
isSymOcc :: OccName -> Bool
515
isSymOcc (OccName DataName s)  = isLexConSym s
516
isSymOcc (OccName TcClsName s) = isLexSym s
517
isSymOcc (OccName VarName s)   = isLexSym s
518
isSymOcc (OccName TvName s)    = isLexSym s
519
-- Pretty inefficient!
520 521

parenSymOcc :: OccName -> SDoc -> SDoc
522
-- ^ Wrap parens around an operator
523
parenSymOcc occ doc | isSymOcc occ = parens doc
524
                    | otherwise    = doc
525

526 527 528 529
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
530 531
                             ('_' : _) -> True
                             _other    -> False
532

Austin Seipp's avatar
Austin Seipp committed
533 534 535
{-
************************************************************************
*                                                                      *
536
\subsection{Making system names}
Austin Seipp's avatar
Austin Seipp committed
537 538
*                                                                      *
************************************************************************
539

540
Here's our convention for splitting up the interface file name space:
541

542 543
   d...         dictionary identifiers
                (local variables, so no name-clash worries)
544

545 546 547
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
548

549 550 551
   $f...        Dict-fun identifiers (from inst decls)
   $dmop        Default method for 'op'
   $pnC         n'th superclass selector for class C
Gabor Greif's avatar
Gabor Greif committed
552
   $wf          Worker for function 'f'
553 554 555
   $sf..        Specialised version of f
   T:C          Tycon for dictionary for class C
   D:C          Data constructor for dictionary for class C
556 557 558 559
   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
560

561
        :...            keywords (export:, letrec: etc.)
apt's avatar
apt committed
562
--- I THINK THIS IS WRONG!
563

564 565
This knowledge is encoded in the following functions.

apt's avatar
apt committed
566
@mk_deriv@ generates an @OccName@ from the prefix and a string.
567
NB: The string must already be encoded!
Austin Seipp's avatar
Austin Seipp committed
568
-}
569

570 571 572 573
mk_deriv :: NameSpace
         -> String              -- Distinguishes one sort of derived name from another
         -> String
         -> OccName
574

575
mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
576 577

isDerivedOccName :: OccName -> Bool
578
isDerivedOccName occ =
579 580 581 582
   case occNameString occ of
     '$':c:_ | isAlphaNum c -> True
     ':':c:_ | isAlphaNum c -> True
     _other                 -> False
583

584 585 586
mkDataConWrapperOcc, mkWorkerOcc,
        mkMatcherOcc, mkBuilderOcc,
        mkDefaultMethodOcc,
587
        mkDerivedTyConOcc, mkClassDataConOcc, mkDictOcc,
588 589
        mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkRepEqOcc,
        mkGenR, mkGen1R, mkGenRCo,
590 591
        mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
        mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
592 593
        mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
        mkTyConRepUserOcc, mkTyConRepSysOcc
594 595
   :: OccName -> OccName

596
-- These derived variables have a prefix that no Haskell value could have
597
mkDataConWrapperOcc = mk_simple_deriv varName  "$W"
598
mkWorkerOcc         = mk_simple_deriv varName  "$w"
cactus's avatar
cactus committed
599
mkMatcherOcc        = mk_simple_deriv varName  "$m"
600
mkBuilderOcc        = mk_simple_deriv varName  "$b"
601
mkDefaultMethodOcc  = mk_simple_deriv varName  "$dm"
602
mkClassOpAuxOcc     = mk_simple_deriv varName  "$c"
603 604 605 606 607 608
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"
609
mkForeignExportOcc  = mk_simple_deriv varName  "$f"
610
mkRepEqOcc          = mk_simple_deriv tvName   "$r"      -- In RULES involving Coercible
611
mkNewTyCoOcc        = mk_simple_deriv tcName   "NTCo:"  -- Coercion for newtypes
612
mkInstTyCoOcc       = mk_simple_deriv tcName   "TFCo:"   -- Coercion for type functions
613
mkEqPredCoOcc       = mk_simple_deriv tcName   "$co"
614

615
-- Used in derived instances
616 617 618 619
mkCon2TagOcc        = mk_simple_deriv varName  "$con2tag_"
mkTag2ConOcc        = mk_simple_deriv varName  "$tag2con_"
mkMaxTagOcc         = mk_simple_deriv varName  "$maxtag_"

620 621 622 623 624 625 626 627 628 629 630 631 632
-- TyConRepName stuff; see Note [Grand plan for Typeable] in TcTypeable
-- incluing the wrinkle about mkSpecialTyConRepName
mkTyConRepSysOcc occ = mk_simple_deriv varName prefix occ
  where
    prefix | isDataOcc occ = "$tc'"
           | otherwise     = "$tc"

mkTyConRepUserOcc occ = mk_simple_deriv varName prefix occ
  where
    -- *User-writable* prefix, for types in gHC_TYPES
    prefix | isDataOcc occ = "tc'"
           | otherwise     = "tc"

633
-- Generic deriving mechanism
634

635
-- | Generate a module-unique name, to be used e.g. while generating new names
636
-- for Generics types. We use module unit id to avoid name clashes when
637 638 639 640
-- package imports is used.
mkModPrefix :: Module -> String
mkModPrefix mod = pk ++ "_" ++ mn
  where
641
    pk = unitIdString (moduleUnitId mod)
642 643 644 645
    mn = moduleNameString (moduleName mod)

mkGenD :: Module -> OccName -> OccName
mkGenD mod = mk_simple_deriv tcName ("D1_" ++ mkModPrefix mod ++ "_")
646

647 648 649 650
mkGenC :: Module -> OccName -> Int -> OccName
mkGenC mod occ m   =
  mk_deriv tcName ("C1_" ++ show m) $
    mkModPrefix mod ++ "_" ++ occNameString occ
651

652 653 654 655
mkGenS :: Module -> OccName -> Int -> Int -> OccName
mkGenS mod occ m n =
  mk_deriv tcName ("S1_" ++ show m ++ "_" ++ show n) $
    mkModPrefix mod ++ "_" ++ occNameString occ
656

657
mkGenR   = mk_simple_deriv tcName "Rep_"
jpm@cs.ox.ac.uk's avatar
jpm@cs.ox.ac.uk committed
658
mkGen1R  = mk_simple_deriv tcName "Rep1_"
659
mkGenRCo = mk_simple_deriv tcName "CoRep_"
660

661 662 663
-- data T = MkT ... deriving( Data ) needs definitions for
--      $tT   :: Data.Generics.Basics.DataType
--      $cMkT :: Data.Generics.Basics.Constr
664 665 666
mkDataTOcc = mk_simple_deriv varName  "$t"
mkDataCOcc = mk_simple_deriv varName  "$c"

667
-- Vectorisation
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
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:"
683

Adam Gundry's avatar
Adam Gundry committed
684 685 686 687
-- Overloaded record field selectors
mkRecFldSelOcc :: String -> OccName
mkRecFldSelOcc   = mk_deriv varName "$sel"

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

691 692 693 694
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)

695 696
-- Data constructor workers are made by setting the name space
-- of the data constructor OccName (which should be a DataName)
697
-- to VarName
698
mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
699

700 701 702 703
mkSuperDictAuxOcc :: Int -> OccName -> OccName
mkSuperDictAuxOcc index cls_tc_occ
  = mk_deriv varName "$cp" (show index ++ occNameString cls_tc_occ)

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

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

726 727 728
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?
729
          -> OccSet             -- ^ avoid these Occs
730
          -> OccName            -- ^ E.g. @$f3OrdMaybe@
731 732 733 734 735

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

736 737
mkDFunOcc info_str is_boot set
  = chooseUniqueOcc VarName (prefix ++ info_str) set
738 739
  where
    prefix | is_boot   = "$fx"
740
           | otherwise = "$f"
741

Austin Seipp's avatar
Austin Seipp committed
742
{-
743 744
Sometimes we need to pick an OccName that has not already been used,
given a set of in-use OccNames.
Austin Seipp's avatar
Austin Seipp committed
745
-}
746 747 748 749 750 751 752

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
753

Austin Seipp's avatar
Austin Seipp committed
754
{-
755 756 757 758 759 760 761 762 763 764 765 766 767 768
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
769
        data (Ord a) => Foo a = MkFoo a
770

771
If this is necessary, we do it by prefixing '$m'.  These
772
guys never show up in error messages.  What a hack.
Austin Seipp's avatar
Austin Seipp committed
773
-}
774 775

mkMethodOcc :: OccName -> OccName
twanvl's avatar
twanvl committed
776 777
mkMethodOcc occ@(OccName VarName _) = occ
mkMethodOcc occ                     = mk_simple_deriv varName "$m" occ
778

Austin Seipp's avatar
Austin Seipp committed
779 780 781
{-
************************************************************************
*                                                                      *
782
\subsection{Tidying them up}
Austin Seipp's avatar
Austin Seipp committed
783 784
*                                                                      *
************************************************************************
785 786 787 788 789 790 791 792

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.

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

797 798 799 800 801 802 803 804
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
805
           There is no guarantee that "FSn" is available;
806 807 808 809 810
           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.
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

  However, if it started with digits at the end, we always make a name
  with digits at the end, rather than shortening "foo2" to just "foo",
  even if "foo" is unused.  Reasons:
     - Plain "foo" might be used later
     - We use trailing digits to subtly indicate a unification variable
       in typechecker error message; see TypeRep.tidyTyVarBndr

We have to take care though! Consider a machine-generated module (Trac #10370)
  module Foo where
     a1 = e1
     a2 = e2
     ...
     a2000 = e2000
Then "a1", "a2" etc are all marked taken.  But now if we come across "a7" again,
we have to do a linear search to find a free one, "a20001".  That might just be
acceptable once.  But if we now come across "a8" again, we don't want to repeat
that search.

So we use the TidyOccEnv mapping for "a" (not "a7" or "a8") as our base for
starting the search; and we make sure to update the starting point for "a"
after we allocate a new one.

Austin Seipp's avatar
Austin Seipp committed
834
-}
835

836
type TidyOccEnv = UniqFM Int    -- The in-scope OccNames
837
  -- See Note [TidyOccEnv]
838

twanvl's avatar
twanvl committed
839
emptyTidyOccEnv :: TidyOccEnv
840
emptyTidyOccEnv = emptyUFM
841

842
initTidyOccEnv :: [OccName] -> TidyOccEnv       -- Initialise with names to avoid!
843 844 845
initTidyOccEnv = foldl add emptyUFM
  where
    add env (OccName _ fs) = addToUFM env fs 1
846 847

tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
848 849
tidyOccName env occ@(OccName occ_sp fs)
  = case lookupUFM env fs of
850 851 852 853
      Nothing -> (addToUFM env fs 1, occ)   -- Desired OccName is free
      Just {} -> case lookupUFM env base1 of
                   Nothing -> (addToUFM env base1 2, OccName occ_sp base1)
                   Just n  -> find 1 n
854
  where
855
    base :: String  -- Drop trailing digits (see Note [TidyOccEnv])
856 857
    base  = dropWhileEndLE isDigit (unpackFS fs)
    base1 = mkFastString (base ++ "1")
858