Var.lhs 13.4 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
20
21
22
23
24
25
26
-- |
-- #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"
--
-- * '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.
--   These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
--
-- #globalvslocal#
-- 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.
27
module Var (
28
29
        -- * The main data type and synonyms
	Var, TyVar, CoVar, Id, DictId, DFunId, EvVar, EvId, IpId,
30

31
	-- ** Taking 'Var's apart
32
	varName, varUnique, varType, 
33
34
35

	-- ** Modifying 'Var's
	setVarName, setVarUnique, setVarType,
36
37
38
39
40
41

	-- ** Constructing, taking apart, modifying 'Id's
	mkGlobalVar, mkLocalVar, mkExportedLocalVar, 
	idInfo, idDetails,
	lazySetIdInfo, setIdDetails, globaliseId,
	setIdExported, setIdNotExported,
42
43

        -- ** Predicates
44
        isCoVar, isId, isTyCoVar, isTyVar, isTcTyVar,
45
46
        isLocalVar, isLocalId,
	isGlobalId, isExportedId,
47
48
49
50
51
52
	mustHaveLocalBinding,

	-- ** Constructing 'TyVar's
	mkTyVar, mkTcTyVar, mkWildCoVar,

	-- ** Taking 'TyVar's apart
53
        tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
54
55

	-- ** Modifying 'TyVar's
56
	setTyVarName, setTyVarUnique, setTyVarKind,
57

58
59
        -- ** Constructing 'CoVar's
        mkCoVar,
60

61
62
        -- ** Taking 'CoVar's apart
        coVarName,
63

64
        -- ** Modifying 'CoVar's
65
        setCoVarUnique, setCoVarName
66

67
68
69
    ) where

#include "HsVersions.h"
70
#include "Typeable.h"
71

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
72
import {-# SOURCE #-}	TypeRep( Type, Kind )
73
import {-# SOURCE #-}	TcType( TcTyVarDetails, pprTcTyVarDetails )
74
import {-# SOURCE #-}	IdInfo( IdDetails, IdInfo, pprIdDetails )
75
76
import {-# SOURCE #-}	TypeRep( isCoercionKind )

Simon Marlow's avatar
Simon Marlow committed
77
78
import Name hiding (varName)
import Unique
79
import Util
80
import FastTypes
81
import FastString
82
import Outputable
83
84

import Data.Data
85
86
87
\end{code}


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
%************************************************************************
%*									*
                     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}
type EvVar = Var	-- An evidence variable: dictionary or equality constraint
     	       		-- 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

type TyVar = Var
type CoVar = TyVar	-- A coercion variable is simply a type 
			-- variable of kind @ty1 ~ ty2@. Hence its
			-- 'varType' is always @PredTy (EqPred t1 t2)@
\end{code}

112
113
114
115
116
117
118
119
120
121
122
123
124
%************************************************************************
%*									*
\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}
125
126
-- | Essentially a typed 'Name', that may also contain some additional information
-- about the 'Var' and it's use sites.
127
data Var
128
  = TyVar {
129
	varName    :: !Name,
130
	realUnique :: FastInt,		-- Key for fast comparison
131
132
					-- Identical to the Unique in the name,
					-- cached here for speed
133
	varType       :: Kind,          -- ^ The type or kind of the 'Var' in question
134
135
        isCoercionVar :: Bool
 }
136

137
  | TcTyVar { 				-- Used only during type inference
138
139
					-- Used for kind variables during 
					-- inference, as well
140
	varName        :: !Name,
141
	realUnique     :: FastInt,
142
	varType        :: Kind,
143
	tc_tv_details  :: TcTyVarDetails }
144

145
  | Id {
146
147
	varName    :: !Name,
	realUnique :: FastInt,
148
   	varType    :: Type,
149
	idScope    :: IdScope,
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
150
151
	id_details :: IdDetails,	-- Stable, doesn't change
	id_info    :: IdInfo }		-- Unstable, updated by simplifier
152

153
154
155
156
157
data IdScope	-- See Note [GlobalId/LocalId]
  = GlobalId 
  | LocalId ExportFlag

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

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

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

After CoreTidy, top-level LocalIds are turned into GlobalIds

179
\begin{code}
180
instance Outputable Var where
181
182
183
  ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))

ppr_debug :: Var -> SDoc
184
185
ppr_debug (TyVar { isCoercionVar = False })   = ptext (sLit "tv")
ppr_debug (TyVar { isCoercionVar = True })    = ptext (sLit "co")
186
ppr_debug (TcTyVar {tc_tv_details = d})       = pprTcTyVarDetails d
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
187
ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
188
189
190
191
192

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")
193

194
instance Show Var where
195
196
  showsPrec p var = showsPrecSDoc p (ppr var)

197
instance NamedThing Var where
198
199
  getName = varName

200
instance Uniquable Var where
201
202
  getUnique = varUnique

203
instance Eq Var where
204
205
    a == b = realUnique a ==# realUnique b

206
instance Ord Var where
207
208
209
210
211
    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
212
213
214
215
216
217
218
219

INSTANCE_TYPEABLE0(Var,varTc,"Var")

instance Data Var where
  -- don't traverse?
  toConstr _   = abstractConstr "Var"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = mkNoRepType "Var"
220
221
222
223
\end{code}


\begin{code}
224
varUnique :: Var -> Unique
225
varUnique var = mkUniqueGrimily (iBox (realUnique var))
226
227

setVarUnique :: Var -> Unique -> Var
228
setVarUnique var uniq 
229
  = var { realUnique = getKeyFastInt uniq, 
230
	  varName = setNameUnique (varName var) uniq }
231

232
setVarName :: Var -> Name -> Var
233
setVarName var new_name
234
  = var { realUnique = getKeyFastInt (getUnique new_name), 
235
   	  varName = new_name }
236
237
238

setVarType :: Id -> Type -> Id
setVarType id ty = id { varType = ty }
239
240
241
242
243
244
245
246
247
248
\end{code}


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

\begin{code}
twanvl's avatar
twanvl committed
249
tyVarName :: TyVar -> Name
250
tyVarName = varName
twanvl's avatar
twanvl committed
251
252

tyVarKind :: TyVar -> Kind
253
tyVarKind = varType
254

twanvl's avatar
twanvl committed
255
setTyVarUnique :: TyVar -> Unique -> TyVar
256
setTyVarUnique = setVarUnique
twanvl's avatar
twanvl committed
257
258

setTyVarName :: TyVar -> Name -> TyVar
259
setTyVarName   = setVarName
260
261

setTyVarKind :: TyVar -> Kind -> TyVar
262
setTyVarKind tv k = tv {varType = k}
263
264
265
\end{code}

\begin{code}
266
mkTyVar :: Name -> Kind -> TyVar
267
268
mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
		    TyVar { varName    = name
269
			  , realUnique = getKeyFastInt (nameUnique name)
270
			  , varType  = kind
271
                          , isCoercionVar    = False
272
			}
273

274
275
mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
mkTcTyVar name kind details
276
  = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
277
    TcTyVar {	varName    = name,
278
		realUnique = getKeyFastInt (nameUnique name),
279
		varType  = kind,
280
		tc_tv_details = details
281
	}
282
283
284
285

tcTyVarDetails :: TyVar -> TcTyVarDetails
tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
286
287
288

setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
setTcTyVarDetails tv details = tv { tc_tv_details = details }
289
290
\end{code}

291
292
293
294
295
296
297
%************************************************************************
%*									*
\subsection{Coercion variables}
%*									*
%************************************************************************

\begin{code}
twanvl's avatar
twanvl committed
298
coVarName :: CoVar -> Name
299
300
coVarName = varName

twanvl's avatar
twanvl committed
301
setCoVarUnique :: CoVar -> Unique -> CoVar
302
setCoVarUnique = setVarUnique
twanvl's avatar
twanvl committed
303
304

setCoVarName :: CoVar -> Name -> CoVar
305
306
307
setCoVarName   = setVarName

mkCoVar :: Name -> Kind -> CoVar
308
mkCoVar name kind = ASSERT( isCoercionKind kind )
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
309
		    TyVar { varName    	  = name
310
			  , realUnique 	  = getKeyFastInt (nameUnique name)
311
			  , varType    	  = kind
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
312
                          , isCoercionVar = True
313
			}
314

315
mkWildCoVar :: Kind -> TyVar
316
317
318
-- ^ Create a type variable that is never referred to, so its unique doesn't 
-- matter
mkWildCoVar = mkCoVar (mkSysTvName (mkBuiltinUnique 1) (fsLit "co_wild"))
319
\end{code}
320
321
322

%************************************************************************
%*									*
323
\subsection{Ids}
324
325
326
327
%*									*
%************************************************************************

\begin{code}
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
328
329
330
331
332
333
334
335
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)

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
-- 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

-- | 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
357
358
	 id_details = details,
	 id_info    = info }
359
360
361

-------------------
lazySetIdInfo :: Id -> IdInfo -> Var
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
362
lazySetIdInfo id info = id { id_info = info }
363
364

setIdDetails :: Id -> IdDetails -> Id
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
365
setIdDetails id details = id { id_details = details }
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

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 }
382
383
\end{code}

384
385
%************************************************************************
%*									*
386
\subsection{Predicates over variables}
387
388
389
%*									*
%************************************************************************

390
\begin{code}
391
392
393
394
395
396
397
isTyCoVar :: Var -> Bool	-- True of both type and coercion variables
isTyCoVar (TyVar {})   = True
isTyCoVar (TcTyVar {}) = True
isTyCoVar _            = False

isTyVar :: Var -> Bool		-- True of both type variables only
isTyVar v@(TyVar {}) = not (isCoercionVar v)
398
isTyVar (TcTyVar {}) = True
twanvl's avatar
twanvl committed
399
isTyVar _            = False
400

401
402
403
404
isCoVar :: Var -> Bool		-- Only works after type checking (sigh)
isCoVar v@(TyVar {}) = isCoercionVar v
isCoVar _            = False

405
isTcTyVar :: Var -> Bool
406
isTcTyVar (TcTyVar {}) = True
twanvl's avatar
twanvl committed
407
isTcTyVar _            = False
408

409
410
411
isId :: Var -> Bool
isId (Id {}) = True
isId _       = False
412

413
414
415
isLocalId :: Var -> Bool
isLocalId (Id { idScope = LocalId _ }) = True
isLocalId _                            = False
416

417
-- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
418
419
-- These are the variables that we need to pay attention to when finding free
-- variables, or doing dependency analysis.
420
isLocalVar :: Var -> Bool
421
422
423
424
425
isLocalVar v = not (isGlobalId v)

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

427
-- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
428
-- that must have a binding in this module.  The converse
429
-- is not quite right: there are some global 'Id's that must have
430
431
-- bindings, such as record selectors.  But that doesn't matter,
-- because it's only used for assertions
432
mustHaveLocalBinding	    :: Var -> Bool
433
434
mustHaveLocalBinding var = isLocalVar var

435
-- | 'isExportedIdVar' means \"don't throw this away\"
436
437
438
439
isExportedId :: Var -> Bool
isExportedId (Id { idScope = GlobalId })        = True
isExportedId (Id { idScope = LocalId Exported}) = True
isExportedId _ = False
440
\end{code}