Var.lhs 12.1 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3
4
5
6
7
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
\section{@Vars@: Variables}

\begin{code}
8
9
10
11
12
13
14
15
16
17
18
19
-- |
-- #name_types#
-- GHC uses several kinds of name internally:
--
-- * 'OccName.OccName': see "OccName#name_types"
--
-- * 'RdrName.RdrName': see "RdrName#name_types"
--
-- * 'Name.Name': see "Name#name_types"
--
-- * 'Id.Id': see "Id#name_types"
--
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
20
21
22
23
24
-- * 'Var.Var' is a synonym for the 'Id.Id' type but it may additionally 
--   potentially contain type variables, which have a 'TypeRep.Kind' 
--   rather than a 'TypeRep.Type' and only contain some extra 
--   details during typechecking.
-- 
25
26
27
--   These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
--
-- #globalvslocal#
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
28
29
30
31
32
-- Global 'Id's and 'Var's are those that are imported or correspond 
--    to a data constructor, primitive operation, or record selectors.
-- Local 'Id's and 'Var's are those bound within an expression 
--    (e.g. by a lambda) or at the top level of the module being compiled.

33
module Var (
34
        -- * The main data type and synonyms
batterseapower's avatar
batterseapower committed
35
        Var, TyVar, CoVar, Id, DictId, DFunId, EvVar, EqVar, EvId, IpId,
36

37
	-- ** Taking 'Var's apart
38
	varName, varUnique, varType, 
39
40
41

	-- ** Modifying 'Var's
	setVarName, setVarUnique, setVarType,
42
43

	-- ** Constructing, taking apart, modifying 'Id's
44
	mkGlobalVar, mkLocalVar, mkExportedLocalVar, mkCoVar,
45
46
47
	idInfo, idDetails,
	lazySetIdInfo, setIdDetails, globaliseId,
	setIdExported, setIdNotExported,
48
49

        -- ** Predicates
50
        isId, isTyVar, isTcTyVar,
51
52
        isLocalVar, isLocalId,
	isGlobalId, isExportedId,
53
54
55
	mustHaveLocalBinding,

	-- ** Constructing 'TyVar's
56
	mkTyVar, mkTcTyVar, 
57
58

	-- ** Taking 'TyVar's apart
59
        tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
60
61

	-- ** Modifying 'TyVar's
62
	setTyVarName, setTyVarUnique, setTyVarKind
63

64
65
66
    ) where

#include "HsVersions.h"
67
#include "Typeable.h"
68

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
69
import {-# SOURCE #-}	TypeRep( Type, Kind )
70
import {-# SOURCE #-}	TcType( TcTyVarDetails, pprTcTyVarDetails )
71
import {-# SOURCE #-}	IdInfo( IdDetails, IdInfo, coVarDetails, vanillaIdInfo, pprIdDetails )
72

Simon Marlow's avatar
Simon Marlow committed
73
74
import Name hiding (varName)
import Unique
75
import Util
76
import FastTypes
77
import FastString
78
import Outputable
79
80

import Data.Data
81
82
83
\end{code}


84
85
86
87
88
89
90
91
92
%************************************************************************
%*									*
                     Synonyms									
%*									*
%************************************************************************
-- These synonyms are here and not in Id because otherwise we need a very
-- large number of SOURCE imports of Id.hs :-(

\begin{code}
93
type EvVar = Var        -- An evidence variable: dictionary or equality constraint
94
95
96
97
98
99
100
     	       		-- Could be an DictId or a CoVar

type Id     = Var       -- A term-level identifier
type DFunId = Id	-- A dictionary function
type EvId   = Id        -- Term-level evidence: DictId or IpId
type DictId = EvId	-- A dictionary variable
type IpId   = EvId      -- A term-level implicit parameter
batterseapower's avatar
batterseapower committed
101
type EqVar  = EvId      -- Boxed equality evidence
102
103

type TyVar = Var
104
type CoVar = Id		-- A coercion variable is simply an Id
batterseapower's avatar
batterseapower committed
105
106
			-- variable of kind @#@. Its
			-- 'varType' is always @ty1 ~# ty2@
107
108
\end{code}

109
110
111
112
113
114
115
116
117
118
119
120
121
%************************************************************************
%*									*
\subsection{The main data type declarations}
%*									*
%************************************************************************


Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
@Type@, and an @IdInfo@ (non-essential info about it, e.g.,
strictness).  The essential info about different kinds of @Vars@ is
in its @VarDetails@.

\begin{code}
122
123
-- | Essentially a typed 'Name', that may also contain some additional information
-- about the 'Var' and it's use sites.
124
data Var
125
  = TyVar {
126
	varName    :: !Name,
127
	realUnique :: FastInt,		-- Key for fast comparison
128
129
					-- Identical to the Unique in the name,
					-- cached here for speed
130
	varType       :: Kind           -- ^ The type or kind of the 'Var' in question
131
 }
132

133
  | TcTyVar { 				-- Used only during type inference
134
135
					-- Used for kind variables during 
					-- inference, as well
136
	varName        :: !Name,
137
	realUnique     :: FastInt,
138
	varType        :: Kind,
139
	tc_tv_details  :: TcTyVarDetails }
140

141
  | Id {
142
143
	varName    :: !Name,
	realUnique :: FastInt,
144
   	varType    :: Type,
145
	idScope    :: IdScope,
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
146
147
	id_details :: IdDetails,	-- Stable, doesn't change
	id_info    :: IdInfo }		-- Unstable, updated by simplifier
Ian Lynagh's avatar
Ian Lynagh committed
148
    deriving Typeable
149

150
151
152
153
154
data IdScope	-- See Note [GlobalId/LocalId]
  = GlobalId 
  | LocalId ExportFlag

data ExportFlag 
155
156
  = NotExported	-- ^ Not exported: may be discarded as dead code.
  | Exported	-- ^ Exported: kept alive
157
158
\end{code}

159
160
Note [GlobalId/LocalId]
~~~~~~~~~~~~~~~~~~~~~~~
161
162
163
A GlobalId is
  * always a constant (top-level)
  * imported, or data constructor, or primop, or record selector
164
165
  * has a Unique that is globally unique across the whole
    GHC invocation (a single invocation may compile multiple modules)
166
167
  * never treated as a candidate by the free-variable finder;
	it's a constant!
168
169
170
171

A LocalId is 
  * bound within an expression (lambda, case, local let(rec))
  * or defined at top level in the module being compiled
172
  * always treated as a candidate by the free-variable finder
173
174
175

After CoreTidy, top-level LocalIds are turned into GlobalIds

176
\begin{code}
177
instance Outputable Var where
178
179
180
  ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))

ppr_debug :: Var -> SDoc
181
182
ppr_debug (TyVar {})                           = ptext (sLit "tv")
ppr_debug (TcTyVar {tc_tv_details = d})        = pprTcTyVarDetails d
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
183
ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
184
185
186
187
188

ppr_id_scope :: IdScope -> SDoc
ppr_id_scope GlobalId              = ptext (sLit "gid")
ppr_id_scope (LocalId Exported)    = ptext (sLit "lidx")
ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
189

190
instance Show Var where
191
192
  showsPrec p var = showsPrecSDoc p (ppr var)

193
instance NamedThing Var where
194
195
  getName = varName

196
instance Uniquable Var where
197
198
  getUnique = varUnique

199
instance Eq Var where
200
201
    a == b = realUnique a ==# realUnique b

202
instance Ord Var where
203
204
205
206
207
    a <= b = realUnique a <=# realUnique b
    a <	 b = realUnique a <#  realUnique b
    a >= b = realUnique a >=# realUnique b
    a >	 b = realUnique a >#  realUnique b
    a `compare` b = varUnique a `compare` varUnique b
208
209
210
211
212
213

instance Data Var where
  -- don't traverse?
  toConstr _   = abstractConstr "Var"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = mkNoRepType "Var"
214
215
216
217
\end{code}


\begin{code}
218
varUnique :: Var -> Unique
219
varUnique var = mkUniqueGrimily (iBox (realUnique var))
220
221

setVarUnique :: Var -> Unique -> Var
222
setVarUnique var uniq 
223
  = var { realUnique = getKeyFastInt uniq, 
224
	  varName = setNameUnique (varName var) uniq }
225

226
setVarName :: Var -> Name -> Var
227
setVarName var new_name
228
  = var { realUnique = getKeyFastInt (getUnique new_name), 
229
   	  varName = new_name }
230
231
232

setVarType :: Id -> Type -> Id
setVarType id ty = id { varType = ty }
233
234
235
236
237
238
239
240
241
242
\end{code}


%************************************************************************
%*									*
\subsection{Type variables}
%*									*
%************************************************************************

\begin{code}
twanvl's avatar
twanvl committed
243
tyVarName :: TyVar -> Name
244
tyVarName = varName
twanvl's avatar
twanvl committed
245
246

tyVarKind :: TyVar -> Kind
247
tyVarKind = varType
248

twanvl's avatar
twanvl committed
249
setTyVarUnique :: TyVar -> Unique -> TyVar
250
setTyVarUnique = setVarUnique
twanvl's avatar
twanvl committed
251
252

setTyVarName :: TyVar -> Name -> TyVar
253
setTyVarName   = setVarName
254
255

setTyVarKind :: TyVar -> Kind -> TyVar
256
setTyVarKind tv k = tv {varType = k}
257
258
259
\end{code}

\begin{code}
260
mkTyVar :: Name -> Kind -> TyVar
261
mkTyVar name kind = TyVar { varName    = name
262
			  , realUnique = getKeyFastInt (nameUnique name)
263
			  , varType  = kind
264
			}
265

266
267
mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
mkTcTyVar name kind details
268
  = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
269
    TcTyVar {	varName    = name,
270
		realUnique = getKeyFastInt (nameUnique name),
271
		varType  = kind,
272
		tc_tv_details = details
273
	}
274
275
276
277

tcTyVarDetails :: TyVar -> TcTyVarDetails
tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
278
279
280

setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
setTcTyVarDetails tv details = tv { tc_tv_details = details }
281
282
283
284
\end{code}

%************************************************************************
%*									*
285
\subsection{Ids}
286
287
288
289
%*									*
%************************************************************************

\begin{code}
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
290
291
292
293
294
295
296
297
idInfo :: Id -> IdInfo
idInfo (Id { id_info = info }) = info
idInfo other 	       	       = pprPanic "idInfo" (ppr other)

idDetails :: Id -> IdDetails
idDetails (Id { id_details = details }) = details
idDetails other 	       	        = pprPanic "idDetails" (ppr other)

298
299
300
301
302
303
304
305
306
307
-- The next three have a 'Var' suffix even though they always build
-- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
mkGlobalVar details name ty info
  = mk_id name ty GlobalId details info

mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
mkLocalVar details name ty info
  = mk_id name ty (LocalId NotExported) details  info

308
309
310
311
mkCoVar :: Name -> Type -> CoVar
-- Coercion variables have no IdInfo
mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo

312
313
314
315
316
317
318
319
320
321
322
-- | Exported 'Var's will not be removed as dead code
mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
mkExportedLocalVar details name ty info 
  = mk_id name ty (LocalId Exported) details info

mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
mk_id name ty scope details info
  = Id { varName    = name, 
	 realUnique = getKeyFastInt (nameUnique name),
	 varType    = ty,	
	 idScope    = scope,
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
323
324
	 id_details = details,
	 id_info    = info }
325
326
327

-------------------
lazySetIdInfo :: Id -> IdInfo -> Var
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
328
lazySetIdInfo id info = id { id_info = info }
329
330

setIdDetails :: Id -> IdDetails -> Id
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
331
setIdDetails id details = id { id_details = details }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

globaliseId :: Id -> Id
-- ^ If it's a local, make it global
globaliseId id = id { idScope = GlobalId }

setIdExported :: Id -> Id
-- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
-- and class operations, which are born as global 'Id's and automatically exported
setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
setIdExported id@(Id { idScope = GlobalId })   = id
setIdExported tv	  	    	       = pprPanic "setIdExported" (ppr tv)

setIdNotExported :: Id -> Id
-- ^ We can only do this to LocalIds
setIdNotExported id = ASSERT( isLocalId id ) 
                      id { idScope = LocalId NotExported }
348
349
\end{code}

350
351
%************************************************************************
%*									*
352
\subsection{Predicates over variables}
353
354
355
%*									*
%************************************************************************

356
\begin{code}
357
358
isTyVar :: Var -> Bool          -- True of both type variables only
isTyVar (TyVar {})   = True
359
isTyVar (TcTyVar {}) = True
twanvl's avatar
twanvl committed
360
isTyVar _            = False
361

362
isTcTyVar :: Var -> Bool
363
isTcTyVar (TcTyVar {}) = True
twanvl's avatar
twanvl committed
364
isTcTyVar _            = False
365

366
367
368
isId :: Var -> Bool
isId (Id {}) = True
isId _       = False
369

370
371
372
isLocalId :: Var -> Bool
isLocalId (Id { idScope = LocalId _ }) = True
isLocalId _                            = False
373

374
-- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
375
376
-- These are the variables that we need to pay attention to when finding free
-- variables, or doing dependency analysis.
377
isLocalVar :: Var -> Bool
378
379
380
381
382
isLocalVar v = not (isGlobalId v)

isGlobalId :: Var -> Bool
isGlobalId (Id { idScope = GlobalId }) = True
isGlobalId _                           = False
383

384
-- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
385
-- that must have a binding in this module.  The converse
386
-- is not quite right: there are some global 'Id's that must have
387
388
-- bindings, such as record selectors.  But that doesn't matter,
-- because it's only used for assertions
389
mustHaveLocalBinding	    :: Var -> Bool
390
391
mustHaveLocalBinding var = isLocalVar var

392
-- | 'isExportedIdVar' means \"don't throw this away\"
393
394
395
396
isExportedId :: Var -> Bool
isExportedId (Id { idScope = GlobalId })        = True
isExportedId (Id { idScope = LocalId Exported}) = True
isExportedId _ = False
397
\end{code}