PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/basicTypes/OccName.lhs

http://github.com/ghc/ghc
Haskell | 938 lines | 648 code | 163 blank | 127 comment | 40 complexity | c5a3dd60528ec7e371b23ddb54da2206 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-3.0
  1. %
  2. % (c) The University of Glasgow 2006
  3. % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
  4. %
  5. \begin{code}
  6. -- |
  7. -- #name_types#
  8. -- GHC uses several kinds of name internally:
  9. --
  10. -- * 'OccName.OccName' represents names as strings with just a little more information:
  11. -- the \"namespace\" that the name came from, e.g. the namespace of value, type constructors or
  12. -- data constructors
  13. --
  14. -- * 'RdrName.RdrName': see "RdrName#name_types"
  15. --
  16. -- * 'Name.Name': see "Name#name_types"
  17. --
  18. -- * 'Id.Id': see "Id#name_types"
  19. --
  20. -- * 'Var.Var': see "Var#name_types"
  21. {-# OPTIONS -fno-warn-tabs #-}
  22. -- The above warning supression flag is a temporary kludge.
  23. -- While working on this module you are encouraged to remove it and
  24. -- detab the module (please do the detabbing in a separate patch). See
  25. -- http://ghc.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
  26. -- for details
  27. module OccName (
  28. -- * The 'NameSpace' type
  29. NameSpace, -- Abstract
  30. -- ** Construction
  31. -- $real_vs_source_data_constructors
  32. tcName, clsName, tcClsName, dataName, varName,
  33. tvName, srcDataName,
  34. -- ** Pretty Printing
  35. pprNameSpace, pprNonVarNameSpace, pprNameSpaceBrief,
  36. -- * The 'OccName' type
  37. OccName, -- Abstract, instance of Outputable
  38. pprOccName,
  39. -- ** Construction
  40. mkOccName, mkOccNameFS,
  41. mkVarOcc, mkVarOccFS,
  42. mkDataOcc, mkDataOccFS,
  43. mkTyVarOcc, mkTyVarOccFS,
  44. mkTcOcc, mkTcOccFS,
  45. mkClsOcc, mkClsOccFS,
  46. mkDFunOcc,
  47. mkTupleOcc,
  48. setOccNameSpace,
  49. demoteOccName,
  50. HasOccName(..),
  51. -- ** Derived 'OccName's
  52. isDerivedOccName,
  53. mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc,
  54. mkGenDefMethodOcc,
  55. mkDerivedTyConOcc, mkNewTyCoOcc, mkClassOpAuxOcc,
  56. mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc,
  57. mkClassDataConOcc, mkDictOcc, mkIPOcc,
  58. mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
  59. mkGenD, mkGenR, mkGen1R, mkGenRCo, mkGenC, mkGenS,
  60. mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc,
  61. mkSuperDictSelOcc, mkLocalOcc, mkMethodOcc, mkInstTyTcOcc,
  62. mkInstTyCoOcc, mkEqPredCoOcc,
  63. mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
  64. mkPDataTyConOcc, mkPDataDataConOcc,
  65. mkPDatasTyConOcc, mkPDatasDataConOcc,
  66. mkPReprTyConOcc,
  67. mkPADFunOcc,
  68. -- ** Deconstruction
  69. occNameFS, occNameString, occNameSpace,
  70. isVarOcc, isTvOcc, isTcOcc, isDataOcc, isDataSymOcc, isSymOcc, isValOcc,
  71. parenSymOcc, startsWithUnderscore,
  72. isTcClsNameSpace, isTvNameSpace, isDataConNameSpace, isVarNameSpace, isValNameSpace,
  73. isTupleOcc_maybe,
  74. -- * The 'OccEnv' type
  75. OccEnv, emptyOccEnv, unitOccEnv, extendOccEnv, mapOccEnv,
  76. lookupOccEnv, mkOccEnv, mkOccEnv_C, extendOccEnvList, elemOccEnv,
  77. occEnvElts, foldOccEnv, plusOccEnv, plusOccEnv_C, extendOccEnv_C,
  78. extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
  79. -- * The 'OccSet' type
  80. OccSet, emptyOccSet, unitOccSet, mkOccSet, extendOccSet,
  81. extendOccSetList,
  82. unionOccSets, unionManyOccSets, minusOccSet, elemOccSet, occSetElts,
  83. foldOccSet, isEmptyOccSet, intersectOccSet, intersectsOccSet,
  84. -- * Tidying up
  85. TidyOccEnv, emptyTidyOccEnv, tidyOccName, initTidyOccEnv,
  86. -- * Lexical characteristics of Haskell names
  87. isLexCon, isLexVar, isLexId, isLexSym,
  88. isLexConId, isLexConSym, isLexVarId, isLexVarSym,
  89. startsVarSym, startsVarId, startsConSym, startsConId
  90. ) where
  91. #include "Typeable.h"
  92. import Util
  93. import Unique
  94. import BasicTypes
  95. import DynFlags
  96. import UniqFM
  97. import UniqSet
  98. import FastString
  99. import Outputable
  100. import Binary
  101. import Data.Char
  102. import Data.Data
  103. \end{code}
  104. %************************************************************************
  105. %* *
  106. \subsection{Name space}
  107. %* *
  108. %************************************************************************
  109. \begin{code}
  110. data NameSpace = VarName -- Variables, including "real" data constructors
  111. | DataName -- "Source" data constructors
  112. | TvName -- Type variables
  113. | TcClsName -- Type constructors and classes; Haskell has them
  114. -- in the same name space for now.
  115. deriving( Eq, Ord )
  116. {-! derive: Binary !-}
  117. -- Note [Data Constructors]
  118. -- see also: Note [Data Constructor Naming] in DataCon.lhs
  119. --
  120. -- $real_vs_source_data_constructors
  121. -- There are two forms of data constructor:
  122. --
  123. -- [Source data constructors] The data constructors mentioned in Haskell source code
  124. --
  125. -- [Real data constructors] The data constructors of the representation type, which may not be the same as the source type
  126. --
  127. -- For example:
  128. --
  129. -- > data T = T !(Int, Int)
  130. --
  131. -- The source datacon has type @(Int, Int) -> T@
  132. -- The real datacon has type @Int -> Int -> T@
  133. --
  134. -- GHC chooses a representation based on the strictness etc.
  135. tcName, clsName, tcClsName :: NameSpace
  136. dataName, srcDataName :: NameSpace
  137. tvName, varName :: NameSpace
  138. -- Though type constructors and classes are in the same name space now,
  139. -- the NameSpace type is abstract, so we can easily separate them later
  140. tcName = TcClsName -- Type constructors
  141. clsName = TcClsName -- Classes
  142. tcClsName = TcClsName -- Not sure which!
  143. dataName = DataName
  144. srcDataName = DataName -- Haskell-source data constructors should be
  145. -- in the Data name space
  146. tvName = TvName
  147. varName = VarName
  148. isDataConNameSpace :: NameSpace -> Bool
  149. isDataConNameSpace DataName = True
  150. isDataConNameSpace _ = False
  151. isTcClsNameSpace :: NameSpace -> Bool
  152. isTcClsNameSpace TcClsName = True
  153. isTcClsNameSpace _ = False
  154. isTvNameSpace :: NameSpace -> Bool
  155. isTvNameSpace TvName = True
  156. isTvNameSpace _ = False
  157. isVarNameSpace :: NameSpace -> Bool -- Variables or type variables, but not constructors
  158. isVarNameSpace TvName = True
  159. isVarNameSpace VarName = True
  160. isVarNameSpace _ = False
  161. isValNameSpace :: NameSpace -> Bool
  162. isValNameSpace DataName = True
  163. isValNameSpace VarName = True
  164. isValNameSpace _ = False
  165. pprNameSpace :: NameSpace -> SDoc
  166. pprNameSpace DataName = ptext (sLit "data constructor")
  167. pprNameSpace VarName = ptext (sLit "variable")
  168. pprNameSpace TvName = ptext (sLit "type variable")
  169. pprNameSpace TcClsName = ptext (sLit "type constructor or class")
  170. pprNonVarNameSpace :: NameSpace -> SDoc
  171. pprNonVarNameSpace VarName = empty
  172. pprNonVarNameSpace ns = pprNameSpace ns
  173. pprNameSpaceBrief :: NameSpace -> SDoc
  174. pprNameSpaceBrief DataName = char 'd'
  175. pprNameSpaceBrief VarName = char 'v'
  176. pprNameSpaceBrief TvName = ptext (sLit "tv")
  177. pprNameSpaceBrief TcClsName = ptext (sLit "tc")
  178. -- demoteNameSpace lowers the NameSpace if possible. We can not know
  179. -- in advance, since a TvName can appear in an HsTyVar.
  180. -- See Note [Demotion] in RnEnv
  181. demoteNameSpace :: NameSpace -> Maybe NameSpace
  182. demoteNameSpace VarName = Nothing
  183. demoteNameSpace DataName = Nothing
  184. demoteNameSpace TvName = Nothing
  185. demoteNameSpace TcClsName = Just DataName
  186. \end{code}
  187. %************************************************************************
  188. %* *
  189. \subsection[Name-pieces-datatypes]{The @OccName@ datatypes}
  190. %* *
  191. %************************************************************************
  192. \begin{code}
  193. data OccName = OccName
  194. { occNameSpace :: !NameSpace
  195. , occNameFS :: !FastString
  196. }
  197. deriving Typeable
  198. \end{code}
  199. \begin{code}
  200. instance Eq OccName where
  201. (OccName sp1 s1) == (OccName sp2 s2) = s1 == s2 && sp1 == sp2
  202. instance Ord OccName where
  203. -- Compares lexicographically, *not* by Unique of the string
  204. compare (OccName sp1 s1) (OccName sp2 s2)
  205. = (s1 `compare` s2) `thenCmp` (sp1 `compare` sp2)
  206. instance Data OccName where
  207. -- don't traverse?
  208. toConstr _ = abstractConstr "OccName"
  209. gunfold _ _ = error "gunfold"
  210. dataTypeOf _ = mkNoRepType "OccName"
  211. \end{code}
  212. %************************************************************************
  213. %* *
  214. \subsection{Printing}
  215. %* *
  216. %************************************************************************
  217. \begin{code}
  218. instance Outputable OccName where
  219. ppr = pprOccName
  220. pprOccName :: OccName -> SDoc
  221. pprOccName (OccName sp occ)
  222. = getPprStyle $ \ sty ->
  223. if codeStyle sty
  224. then ztext (zEncodeFS occ)
  225. else pp_occ <> pp_debug sty
  226. where
  227. pp_debug sty | debugStyle sty = braces (pprNameSpaceBrief sp)
  228. | otherwise = empty
  229. pp_occ = sdocWithDynFlags $ \dflags ->
  230. if gopt Opt_SuppressUniques dflags
  231. then text (strip_th_unique (unpackFS occ))
  232. else ftext occ
  233. -- See Note [Suppressing uniques in OccNames]
  234. strip_th_unique ('[' : c : _) | isAlphaNum c = []
  235. strip_th_unique (c : cs) = c : strip_th_unique cs
  236. strip_th_unique [] = []
  237. \end{code}
  238. Note [Suppressing uniques in OccNames]
  239. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  240. This is a hack to de-wobblify the OccNames that contain uniques from
  241. Template Haskell that have been turned into a string in the OccName.
  242. See Note [Unique OccNames from Template Haskell] in Convert.hs
  243. %************************************************************************
  244. %* *
  245. \subsection{Construction}
  246. %* *
  247. %************************************************************************
  248. \begin{code}
  249. mkOccName :: NameSpace -> String -> OccName
  250. mkOccName occ_sp str = OccName occ_sp (mkFastString str)
  251. mkOccNameFS :: NameSpace -> FastString -> OccName
  252. mkOccNameFS occ_sp fs = OccName occ_sp fs
  253. mkVarOcc :: String -> OccName
  254. mkVarOcc s = mkOccName varName s
  255. mkVarOccFS :: FastString -> OccName
  256. mkVarOccFS fs = mkOccNameFS varName fs
  257. mkDataOcc :: String -> OccName
  258. mkDataOcc = mkOccName dataName
  259. mkDataOccFS :: FastString -> OccName
  260. mkDataOccFS = mkOccNameFS dataName
  261. mkTyVarOcc :: String -> OccName
  262. mkTyVarOcc = mkOccName tvName
  263. mkTyVarOccFS :: FastString -> OccName
  264. mkTyVarOccFS fs = mkOccNameFS tvName fs
  265. mkTcOcc :: String -> OccName
  266. mkTcOcc = mkOccName tcName
  267. mkTcOccFS :: FastString -> OccName
  268. mkTcOccFS = mkOccNameFS tcName
  269. mkClsOcc :: String -> OccName
  270. mkClsOcc = mkOccName clsName
  271. mkClsOccFS :: FastString -> OccName
  272. mkClsOccFS = mkOccNameFS clsName
  273. -- demoteOccName lowers the Namespace of OccName.
  274. -- see Note [Demotion]
  275. demoteOccName :: OccName -> Maybe OccName
  276. demoteOccName (OccName space name) = do
  277. space' <- demoteNameSpace space
  278. return $ OccName space' name
  279. {- | Other names in the compiler add aditional information to an OccName.
  280. This class provides a consistent way to access the underlying OccName. -}
  281. class HasOccName name where
  282. occName :: name -> OccName
  283. \end{code}
  284. %************************************************************************
  285. %* *
  286. Environments
  287. %* *
  288. %************************************************************************
  289. OccEnvs are used mainly for the envts in ModIfaces.
  290. Note [The Unique of an OccName]
  291. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  292. They are efficient, because FastStrings have unique Int# keys. We assume
  293. this key is less than 2^24, and indeed FastStrings are allocated keys
  294. sequentially starting at 0.
  295. So we can make a Unique using
  296. mkUnique ns key :: Unique
  297. where 'ns' is a Char representing the name space. This in turn makes it
  298. easy to build an OccEnv.
  299. \begin{code}
  300. instance Uniquable OccName where
  301. -- See Note [The Unique of an OccName]
  302. getUnique (OccName VarName fs) = mkVarOccUnique fs
  303. getUnique (OccName DataName fs) = mkDataOccUnique fs
  304. getUnique (OccName TvName fs) = mkTvOccUnique fs
  305. getUnique (OccName TcClsName fs) = mkTcOccUnique fs
  306. newtype OccEnv a = A (UniqFM a)
  307. emptyOccEnv :: OccEnv a
  308. unitOccEnv :: OccName -> a -> OccEnv a
  309. extendOccEnv :: OccEnv a -> OccName -> a -> OccEnv a
  310. extendOccEnvList :: OccEnv a -> [(OccName, a)] -> OccEnv a
  311. lookupOccEnv :: OccEnv a -> OccName -> Maybe a
  312. mkOccEnv :: [(OccName,a)] -> OccEnv a
  313. mkOccEnv_C :: (a -> a -> a) -> [(OccName,a)] -> OccEnv a
  314. elemOccEnv :: OccName -> OccEnv a -> Bool
  315. foldOccEnv :: (a -> b -> b) -> b -> OccEnv a -> b
  316. occEnvElts :: OccEnv a -> [a]
  317. extendOccEnv_C :: (a->a->a) -> OccEnv a -> OccName -> a -> OccEnv a
  318. extendOccEnv_Acc :: (a->b->b) -> (a->b) -> OccEnv b -> OccName -> a -> OccEnv b
  319. plusOccEnv :: OccEnv a -> OccEnv a -> OccEnv a
  320. plusOccEnv_C :: (a->a->a) -> OccEnv a -> OccEnv a -> OccEnv a
  321. mapOccEnv :: (a->b) -> OccEnv a -> OccEnv b
  322. delFromOccEnv :: OccEnv a -> OccName -> OccEnv a
  323. delListFromOccEnv :: OccEnv a -> [OccName] -> OccEnv a
  324. filterOccEnv :: (elt -> Bool) -> OccEnv elt -> OccEnv elt
  325. emptyOccEnv = A emptyUFM
  326. unitOccEnv x y = A $ unitUFM x y
  327. extendOccEnv (A x) y z = A $ addToUFM x y z
  328. extendOccEnvList (A x) l = A $ addListToUFM x l
  329. lookupOccEnv (A x) y = lookupUFM x y
  330. mkOccEnv l = A $ listToUFM l
  331. elemOccEnv x (A y) = elemUFM x y
  332. foldOccEnv a b (A c) = foldUFM a b c
  333. occEnvElts (A x) = eltsUFM x
  334. plusOccEnv (A x) (A y) = A $ plusUFM x y
  335. plusOccEnv_C f (A x) (A y) = A $ plusUFM_C f x y
  336. extendOccEnv_C f (A x) y z = A $ addToUFM_C f x y z
  337. extendOccEnv_Acc f g (A x) y z = A $ addToUFM_Acc f g x y z
  338. mapOccEnv f (A x) = A $ mapUFM f x
  339. mkOccEnv_C comb l = A $ addListToUFM_C comb emptyUFM l
  340. delFromOccEnv (A x) y = A $ delFromUFM x y
  341. delListFromOccEnv (A x) y = A $ delListFromUFM x y
  342. filterOccEnv x (A y) = A $ filterUFM x y
  343. instance Outputable a => Outputable (OccEnv a) where
  344. ppr (A x) = ppr x
  345. type OccSet = UniqSet OccName
  346. emptyOccSet :: OccSet
  347. unitOccSet :: OccName -> OccSet
  348. mkOccSet :: [OccName] -> OccSet
  349. extendOccSet :: OccSet -> OccName -> OccSet
  350. extendOccSetList :: OccSet -> [OccName] -> OccSet
  351. unionOccSets :: OccSet -> OccSet -> OccSet
  352. unionManyOccSets :: [OccSet] -> OccSet
  353. minusOccSet :: OccSet -> OccSet -> OccSet
  354. elemOccSet :: OccName -> OccSet -> Bool
  355. occSetElts :: OccSet -> [OccName]
  356. foldOccSet :: (OccName -> b -> b) -> b -> OccSet -> b
  357. isEmptyOccSet :: OccSet -> Bool
  358. intersectOccSet :: OccSet -> OccSet -> OccSet
  359. intersectsOccSet :: OccSet -> OccSet -> Bool
  360. emptyOccSet = emptyUniqSet
  361. unitOccSet = unitUniqSet
  362. mkOccSet = mkUniqSet
  363. extendOccSet = addOneToUniqSet
  364. extendOccSetList = addListToUniqSet
  365. unionOccSets = unionUniqSets
  366. unionManyOccSets = unionManyUniqSets
  367. minusOccSet = minusUniqSet
  368. elemOccSet = elementOfUniqSet
  369. occSetElts = uniqSetToList
  370. foldOccSet = foldUniqSet
  371. isEmptyOccSet = isEmptyUniqSet
  372. intersectOccSet = intersectUniqSets
  373. intersectsOccSet s1 s2 = not (isEmptyOccSet (s1 `intersectOccSet` s2))
  374. \end{code}
  375. %************************************************************************
  376. %* *
  377. \subsection{Predicates and taking them apart}
  378. %* *
  379. %************************************************************************
  380. \begin{code}
  381. occNameString :: OccName -> String
  382. occNameString (OccName _ s) = unpackFS s
  383. setOccNameSpace :: NameSpace -> OccName -> OccName
  384. setOccNameSpace sp (OccName _ occ) = OccName sp occ
  385. isVarOcc, isTvOcc, isTcOcc, isDataOcc :: OccName -> Bool
  386. isVarOcc (OccName VarName _) = True
  387. isVarOcc _ = False
  388. isTvOcc (OccName TvName _) = True
  389. isTvOcc _ = False
  390. isTcOcc (OccName TcClsName _) = True
  391. isTcOcc _ = False
  392. -- | /Value/ 'OccNames's are those that are either in
  393. -- the variable or data constructor namespaces
  394. isValOcc :: OccName -> Bool
  395. isValOcc (OccName VarName _) = True
  396. isValOcc (OccName DataName _) = True
  397. isValOcc _ = False
  398. isDataOcc (OccName DataName _) = True
  399. isDataOcc _ = False
  400. -- | Test if the 'OccName' is a data constructor that starts with
  401. -- a symbol (e.g. @:@, or @[]@)
  402. isDataSymOcc :: OccName -> Bool
  403. isDataSymOcc (OccName DataName s) = isLexConSym s
  404. isDataSymOcc _ = False
  405. -- Pretty inefficient!
  406. -- | Test if the 'OccName' is that for any operator (whether
  407. -- it is a data constructor or variable or whatever)
  408. isSymOcc :: OccName -> Bool
  409. isSymOcc (OccName DataName s) = isLexConSym s
  410. isSymOcc (OccName TcClsName s) = isLexConSym s || isLexVarSym s
  411. isSymOcc (OccName VarName s) = isLexSym s
  412. isSymOcc (OccName TvName s) = isLexSym s
  413. -- Pretty inefficient!
  414. parenSymOcc :: OccName -> SDoc -> SDoc
  415. -- ^ Wrap parens around an operator
  416. parenSymOcc occ doc | isSymOcc occ = parens doc
  417. | otherwise = doc
  418. \end{code}
  419. \begin{code}
  420. startsWithUnderscore :: OccName -> Bool
  421. -- ^ Haskell 98 encourages compilers to suppress warnings about unsed
  422. -- names in a pattern if they start with @_@: this implements that test
  423. startsWithUnderscore occ = case occNameString occ of
  424. ('_' : _) -> True
  425. _other -> False
  426. \end{code}
  427. %************************************************************************
  428. %* *
  429. \subsection{Making system names}
  430. %* *
  431. %************************************************************************
  432. Here's our convention for splitting up the interface file name space:
  433. d... dictionary identifiers
  434. (local variables, so no name-clash worries)
  435. All of these other OccNames contain a mixture of alphabetic
  436. and symbolic characters, and hence cannot possibly clash with
  437. a user-written type or function name
  438. $f... Dict-fun identifiers (from inst decls)
  439. $dmop Default method for 'op'
  440. $pnC n'th superclass selector for class C
  441. $wf Worker for functtoin 'f'
  442. $sf.. Specialised version of f
  443. T:C Tycon for dictionary for class C
  444. D:C Data constructor for dictionary for class C
  445. NTCo:T Coercion connecting newtype T with its representation type
  446. TFCo:R Coercion connecting a data family to its respresentation type R
  447. In encoded form these appear as Zdfxxx etc
  448. :... keywords (export:, letrec: etc.)
  449. --- I THINK THIS IS WRONG!
  450. This knowledge is encoded in the following functions.
  451. @mk_deriv@ generates an @OccName@ from the prefix and a string.
  452. NB: The string must already be encoded!
  453. \begin{code}
  454. mk_deriv :: NameSpace
  455. -> String -- Distinguishes one sort of derived name from another
  456. -> String
  457. -> OccName
  458. mk_deriv occ_sp sys_prefix str = mkOccName occ_sp (sys_prefix ++ str)
  459. isDerivedOccName :: OccName -> Bool
  460. isDerivedOccName occ =
  461. case occNameString occ of
  462. '$':c:_ | isAlphaNum c -> True
  463. ':':c:_ | isAlphaNum c -> True
  464. _other -> False
  465. \end{code}
  466. \begin{code}
  467. mkDataConWrapperOcc, mkWorkerOcc, mkDefaultMethodOcc,
  468. mkGenDefMethodOcc, mkDerivedTyConOcc, mkClassDataConOcc, mkDictOcc,
  469. mkIPOcc, mkSpecOcc, mkForeignExportOcc, mkGenOcc1, mkGenOcc2,
  470. mkGenD, mkGenR, mkGen1R, mkGenRCo,
  471. mkDataTOcc, mkDataCOcc, mkDataConWorkerOcc, mkNewTyCoOcc,
  472. mkInstTyCoOcc, mkEqPredCoOcc, mkClassOpAuxOcc,
  473. mkCon2TagOcc, mkTag2ConOcc, mkMaxTagOcc
  474. :: OccName -> OccName
  475. -- These derived variables have a prefix that no Haskell value could have
  476. mkDataConWrapperOcc = mk_simple_deriv varName "$W"
  477. mkWorkerOcc = mk_simple_deriv varName "$w"
  478. mkDefaultMethodOcc = mk_simple_deriv varName "$dm"
  479. mkGenDefMethodOcc = mk_simple_deriv varName "$gdm"
  480. mkClassOpAuxOcc = mk_simple_deriv varName "$c"
  481. mkDerivedTyConOcc = mk_simple_deriv tcName ":" -- The : prefix makes sure it classifies as a tycon/datacon
  482. mkClassDataConOcc = mk_simple_deriv dataName "D:" -- We go straight to the "real" data con
  483. -- for datacons from classes
  484. mkDictOcc = mk_simple_deriv varName "$d"
  485. mkIPOcc = mk_simple_deriv varName "$i"
  486. mkSpecOcc = mk_simple_deriv varName "$s"
  487. mkForeignExportOcc = mk_simple_deriv varName "$f"
  488. mkNewTyCoOcc = mk_simple_deriv tcName "NTCo:" -- Coercion for newtypes
  489. mkInstTyCoOcc = mk_simple_deriv tcName "TFCo:" -- Coercion for type functions
  490. mkEqPredCoOcc = mk_simple_deriv tcName "$co"
  491. -- used in derived instances
  492. mkCon2TagOcc = mk_simple_deriv varName "$con2tag_"
  493. mkTag2ConOcc = mk_simple_deriv varName "$tag2con_"
  494. mkMaxTagOcc = mk_simple_deriv varName "$maxtag_"
  495. -- Generic derivable classes (old)
  496. mkGenOcc1 = mk_simple_deriv varName "$gfrom"
  497. mkGenOcc2 = mk_simple_deriv varName "$gto"
  498. -- Generic deriving mechanism (new)
  499. mkGenD = mk_simple_deriv tcName "D1"
  500. mkGenC :: OccName -> Int -> OccName
  501. mkGenC occ m = mk_deriv tcName ("C1_" ++ show m) (occNameString occ)
  502. mkGenS :: OccName -> Int -> Int -> OccName
  503. mkGenS occ m n = mk_deriv tcName ("S1_" ++ show m ++ "_" ++ show n)
  504. (occNameString occ)
  505. mkGenR = mk_simple_deriv tcName "Rep_"
  506. mkGen1R = mk_simple_deriv tcName "Rep1_"
  507. mkGenRCo = mk_simple_deriv tcName "CoRep_"
  508. -- data T = MkT ... deriving( Data ) needs definitions for
  509. -- $tT :: Data.Generics.Basics.DataType
  510. -- $cMkT :: Data.Generics.Basics.Constr
  511. mkDataTOcc = mk_simple_deriv varName "$t"
  512. mkDataCOcc = mk_simple_deriv varName "$c"
  513. -- Vectorisation
  514. mkVectOcc, mkVectTyConOcc, mkVectDataConOcc, mkVectIsoOcc,
  515. mkPADFunOcc, mkPReprTyConOcc,
  516. mkPDataTyConOcc, mkPDataDataConOcc,
  517. mkPDatasTyConOcc, mkPDatasDataConOcc
  518. :: Maybe String -> OccName -> OccName
  519. mkVectOcc = mk_simple_deriv_with varName "$v"
  520. mkVectTyConOcc = mk_simple_deriv_with tcName "V:"
  521. mkVectDataConOcc = mk_simple_deriv_with dataName "VD:"
  522. mkVectIsoOcc = mk_simple_deriv_with varName "$vi"
  523. mkPADFunOcc = mk_simple_deriv_with varName "$pa"
  524. mkPReprTyConOcc = mk_simple_deriv_with tcName "VR:"
  525. mkPDataTyConOcc = mk_simple_deriv_with tcName "VP:"
  526. mkPDatasTyConOcc = mk_simple_deriv_with tcName "VPs:"
  527. mkPDataDataConOcc = mk_simple_deriv_with dataName "VPD:"
  528. mkPDatasDataConOcc = mk_simple_deriv_with dataName "VPDs:"
  529. mk_simple_deriv :: NameSpace -> String -> OccName -> OccName
  530. mk_simple_deriv sp px occ = mk_deriv sp px (occNameString occ)
  531. mk_simple_deriv_with :: NameSpace -> String -> Maybe String -> OccName -> OccName
  532. mk_simple_deriv_with sp px Nothing occ = mk_deriv sp px (occNameString occ)
  533. mk_simple_deriv_with sp px (Just with) occ = mk_deriv sp (px ++ with ++ "_") (occNameString occ)
  534. -- Data constructor workers are made by setting the name space
  535. -- of the data constructor OccName (which should be a DataName)
  536. -- to VarName
  537. mkDataConWorkerOcc datacon_occ = setOccNameSpace varName datacon_occ
  538. \end{code}
  539. \begin{code}
  540. mkSuperDictSelOcc :: Int -- ^ Index of superclass, e.g. 3
  541. -> OccName -- ^ Class, e.g. @Ord@
  542. -> OccName -- ^ Derived 'Occname', e.g. @$p3Ord@
  543. mkSuperDictSelOcc index cls_tc_occ
  544. = mk_deriv varName "$p" (show index ++ occNameString cls_tc_occ)
  545. mkLocalOcc :: Unique -- ^ Unique to combine with the 'OccName'
  546. -> OccName -- ^ Local name, e.g. @sat@
  547. -> OccName -- ^ Nice unique version, e.g. @$L23sat@
  548. mkLocalOcc uniq occ
  549. = mk_deriv varName ("$L" ++ show uniq) (occNameString occ)
  550. -- The Unique might print with characters
  551. -- that need encoding (e.g. 'z'!)
  552. \end{code}
  553. \begin{code}
  554. -- | Derive a name for the representation type constructor of a
  555. -- @data@\/@newtype@ instance.
  556. mkInstTyTcOcc :: String -- ^ Family name, e.g. @Map@
  557. -> OccSet -- ^ avoid these Occs
  558. -> OccName -- ^ @R:Map@
  559. mkInstTyTcOcc str set =
  560. chooseUniqueOcc tcName ('R' : ':' : str) set
  561. \end{code}
  562. \begin{code}
  563. mkDFunOcc :: String -- ^ Typically the class and type glommed together e.g. @OrdMaybe@.
  564. -- Only used in debug mode, for extra clarity
  565. -> Bool -- ^ Is this a hs-boot instance DFun?
  566. -> OccSet -- ^ avoid these Occs
  567. -> OccName -- ^ E.g. @$f3OrdMaybe@
  568. -- In hs-boot files we make dict funs like $fx7ClsTy, which get bound to the real
  569. -- thing when we compile the mother module. Reason: we don't know exactly
  570. -- what the mother module will call it.
  571. mkDFunOcc info_str is_boot set
  572. = chooseUniqueOcc VarName (prefix ++ info_str) set
  573. where
  574. prefix | is_boot = "$fx"
  575. | otherwise = "$f"
  576. \end{code}
  577. Sometimes we need to pick an OccName that has not already been used,
  578. given a set of in-use OccNames.
  579. \begin{code}
  580. chooseUniqueOcc :: NameSpace -> String -> OccSet -> OccName
  581. chooseUniqueOcc ns str set = loop (mkOccName ns str) (0::Int)
  582. where
  583. loop occ n
  584. | occ `elemOccSet` set = loop (mkOccName ns (str ++ show n)) (n+1)
  585. | otherwise = occ
  586. \end{code}
  587. We used to add a '$m' to indicate a method, but that gives rise to bad
  588. error messages from the type checker when we print the function name or pattern
  589. of an instance-decl binding. Why? Because the binding is zapped
  590. to use the method name in place of the selector name.
  591. (See TcClassDcl.tcMethodBind)
  592. The way it is now, -ddump-xx output may look confusing, but
  593. you can always say -dppr-debug to get the uniques.
  594. However, we *do* have to zap the first character to be lower case,
  595. because overloaded constructors (blarg) generate methods too.
  596. And convert to VarName space
  597. e.g. a call to constructor MkFoo where
  598. data (Ord a) => Foo a = MkFoo a
  599. If this is necessary, we do it by prefixing '$m'. These
  600. guys never show up in error messages. What a hack.
  601. \begin{code}
  602. mkMethodOcc :: OccName -> OccName
  603. mkMethodOcc occ@(OccName VarName _) = occ
  604. mkMethodOcc occ = mk_simple_deriv varName "$m" occ
  605. \end{code}
  606. %************************************************************************
  607. %* *
  608. \subsection{Tidying them up}
  609. %* *
  610. %************************************************************************
  611. Before we print chunks of code we like to rename it so that
  612. we don't have to print lots of silly uniques in it. But we mustn't
  613. accidentally introduce name clashes! So the idea is that we leave the
  614. OccName alone unless it accidentally clashes with one that is already
  615. in scope; if so, we tack on '1' at the end and try again, then '2', and
  616. so on till we find a unique one.
  617. There's a wrinkle for operators. Consider '>>='. We can't use '>>=1'
  618. because that isn't a single lexeme. So we encode it to 'lle' and *then*
  619. tack on the '1', if necessary.
  620. Note [TidyOccEnv]
  621. ~~~~~~~~~~~~~~~~~
  622. type TidyOccEnv = UniqFM Int
  623. * Domain = The OccName's FastString. These FastStrings are "taken";
  624. make sure that we don't re-use
  625. * Int, n = A plausible starting point for new guesses
  626. There is no guarantee that "FSn" is available;
  627. you must look that up in the TidyOccEnv. But
  628. it's a good place to start looking.
  629. * When looking for a renaming for "foo2" we strip off the "2" and start
  630. with "foo". Otherwise if we tidy twice we get silly names like foo23.
  631. \begin{code}
  632. type TidyOccEnv = UniqFM Int -- The in-scope OccNames
  633. -- See Note [TidyOccEnv]
  634. emptyTidyOccEnv :: TidyOccEnv
  635. emptyTidyOccEnv = emptyUFM
  636. initTidyOccEnv :: [OccName] -> TidyOccEnv -- Initialise with names to avoid!
  637. initTidyOccEnv = foldl add emptyUFM
  638. where
  639. add env (OccName _ fs) = addToUFM env fs 1
  640. tidyOccName :: TidyOccEnv -> OccName -> (TidyOccEnv, OccName)
  641. tidyOccName env occ@(OccName occ_sp fs)
  642. = case lookupUFM env fs of
  643. Just n -> find n
  644. Nothing -> (addToUFM env fs 1, occ)
  645. where
  646. base :: String -- Drop trailing digits (see Note [TidyOccEnv])
  647. base = reverse (dropWhile isDigit (reverse (unpackFS fs)))
  648. find n
  649. = case lookupUFM env new_fs of
  650. Just n' -> find (n1 `max` n')
  651. -- The max ensures that n increases, avoiding loops
  652. Nothing -> (addToUFM (addToUFM env fs n1) new_fs n1,
  653. OccName occ_sp new_fs)
  654. -- We update only the beginning and end of the
  655. -- chain that find explores; it's a little harder to
  656. -- update the middle and there's no real need.
  657. where
  658. n1 = n+1
  659. new_fs = mkFastString (base ++ show n)
  660. \end{code}
  661. %************************************************************************
  662. %* *
  663. Stuff for dealing with tuples
  664. %* *
  665. %************************************************************************
  666. \begin{code}
  667. mkTupleOcc :: NameSpace -> TupleSort -> Arity -> OccName
  668. mkTupleOcc ns sort ar = OccName ns (mkFastString str)
  669. where
  670. -- no need to cache these, the caching is done in the caller
  671. -- (TysWiredIn.mk_tuple)
  672. str = case sort of
  673. UnboxedTuple -> '(' : '#' : commas ++ "#)"
  674. BoxedTuple -> '(' : commas ++ ")"
  675. ConstraintTuple -> '(' : commas ++ ")"
  676. -- Cute hack: reuse the standard tuple OccNames (and hence code)
  677. -- for fact tuples, but give them different Uniques so they are not equal.
  678. --
  679. -- You might think that this will go wrong because isTupleOcc_maybe won't
  680. -- be able to tell the difference between boxed tuples and fact tuples. BUT:
  681. -- 1. Fact tuples never occur directly in user code, so it doesn't matter
  682. -- that we can't detect them in Orig OccNames originating from the user
  683. -- programs (or those built by setRdrNameSpace used on an Exact tuple Name)
  684. -- 2. Interface files have a special representation for tuple *occurrences*
  685. -- in IfaceTyCons, their workers (in IfaceSyn) and their DataCons (in case
  686. -- alternatives). Thus we don't rely on the OccName to figure out what kind
  687. -- of tuple an occurrence was trying to use in these situations.
  688. -- 3. We *don't* represent tuple data type declarations specially, so those
  689. -- are still turned into wired-in names via isTupleOcc_maybe. But that's OK
  690. -- because we don't actually need to declare fact tuples thanks to this hack.
  691. --
  692. -- So basically any OccName like (,,) flowing to isTupleOcc_maybe will always
  693. -- refer to the standard boxed tuple. Cool :-)
  694. commas = take (ar-1) (repeat ',')
  695. isTupleOcc_maybe :: OccName -> Maybe (NameSpace, TupleSort, Arity)
  696. -- Tuples are special, because there are so many of them!
  697. isTupleOcc_maybe (OccName ns fs)
  698. = case unpackFS fs of
  699. '(':'#':',':rest -> Just (ns, UnboxedTuple, 2 + count_commas rest)
  700. '(':',':rest -> Just (ns, BoxedTuple, 2 + count_commas rest)
  701. _other -> Nothing
  702. where
  703. count_commas (',':rest) = 1 + count_commas rest
  704. count_commas _ = 0
  705. \end{code}
  706. %************************************************************************
  707. %* *
  708. \subsection{Lexical categories}
  709. %* *
  710. %************************************************************************
  711. These functions test strings to see if they fit the lexical categories
  712. defined in the Haskell report.
  713. \begin{code}
  714. isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
  715. isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
  716. isLexCon cs = isLexConId cs || isLexConSym cs
  717. isLexVar cs = isLexVarId cs || isLexVarSym cs
  718. isLexId cs = isLexConId cs || isLexVarId cs
  719. isLexSym cs = isLexConSym cs || isLexVarSym cs
  720. -------------
  721. isLexConId cs -- Prefix type or data constructors
  722. | nullFS cs = False -- e.g. "Foo", "[]", "(,)"
  723. | cs == (fsLit "[]") = True
  724. | otherwise = startsConId (headFS cs)
  725. isLexVarId cs -- Ordinary prefix identifiers
  726. | nullFS cs = False -- e.g. "x", "_x"
  727. | otherwise = startsVarId (headFS cs)
  728. isLexConSym cs -- Infix type or data constructors
  729. | nullFS cs = False -- e.g. ":-:", ":", "->"
  730. | cs == (fsLit "->") = True
  731. | otherwise = startsConSym (headFS cs)
  732. isLexVarSym cs -- Infix identifiers
  733. | nullFS cs = False -- e.g. "+"
  734. | otherwise = startsVarSym (headFS cs)
  735. -------------
  736. startsVarSym, startsVarId, startsConSym, startsConId :: Char -> Bool
  737. startsVarSym c = isSymbolASCII c || (ord c > 0x7f && isSymbol c) -- Infix Ids
  738. startsConSym c = c == ':' -- Infix data constructors
  739. startsVarId c = isLower c || c == '_' -- Ordinary Ids
  740. startsConId c = isUpper c || c == '(' -- Ordinary type constructors and data constructors
  741. isSymbolASCII :: Char -> Bool
  742. isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
  743. \end{code}
  744. %************************************************************************
  745. %* *
  746. Binary instance
  747. Here rather than BinIface because OccName is abstract
  748. %* *
  749. %************************************************************************
  750. \begin{code}
  751. instance Binary NameSpace where
  752. put_ bh VarName = do
  753. putByte bh 0
  754. put_ bh DataName = do
  755. putByte bh 1
  756. put_ bh TvName = do
  757. putByte bh 2
  758. put_ bh TcClsName = do
  759. putByte bh 3
  760. get bh = do
  761. h <- getByte bh
  762. case h of
  763. 0 -> do return VarName
  764. 1 -> do return DataName
  765. 2 -> do return TvName
  766. _ -> do return TcClsName
  767. instance Binary OccName where
  768. put_ bh (OccName aa ab) = do
  769. put_ bh aa
  770. put_ bh ab
  771. get bh = do
  772. aa <- get bh
  773. ab <- get bh
  774. return (OccName aa ab)
  775. \end{code}