update submodule pointer
[ghc-hetmet.git] / compiler / basicTypes / Var.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5 \section{@Vars@: Variables}
6
7 \begin{code}
8 -- |
9 -- #name_types#
10 -- GHC uses several kinds of name internally:
11 --
12 -- * 'OccName.OccName': see "OccName#name_types"
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' is a synonym for the 'Id.Id' type but it may additionally 
21 --   potentially contain type variables, which have a 'TypeRep.Kind' 
22 --   rather than a 'TypeRep.Type' and only contain some extra 
23 --   details during typechecking.
24 -- 
25 --   These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
26 --
27 -- #globalvslocal#
28 -- Global 'Id's and 'Var's are those that are imported or correspond 
29 --    to a data constructor, primitive operation, or record selectors.
30 -- Local 'Id's and 'Var's are those bound within an expression 
31 --    (e.g. by a lambda) or at the top level of the module being compiled.
32
33 module Var (
34         -- * The main data type and synonyms
35         Var, TyVar, CoVar, Id, DictId, DFunId, EvVar, EvId, IpId,
36
37         -- ** Taking 'Var's apart
38         varName, varUnique, varType, 
39
40         -- ** Modifying 'Var's
41         setVarName, setVarUnique, setVarType,
42
43         -- ** Constructing, taking apart, modifying 'Id's
44         mkGlobalVar, mkLocalVar, mkExportedLocalVar, 
45         idInfo, idDetails,
46         lazySetIdInfo, setIdDetails, globaliseId,
47         setIdExported, setIdNotExported,
48
49         -- ** Predicates
50         isCoVar, isId, isTyCoVar, isTyVar, isTcTyVar,
51         isLocalVar, isLocalId,
52         isGlobalId, isExportedId,
53         mustHaveLocalBinding,
54
55         -- ** Constructing 'TyVar's
56         mkTyVar, mkTcTyVar, mkWildCoVar,
57
58         -- ** Taking 'TyVar's apart
59         tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
60
61         -- ** Modifying 'TyVar's
62         setTyVarName, setTyVarUnique, setTyVarKind,
63
64         -- ** Constructing 'CoVar's
65         mkCoVar,
66
67         -- ** Taking 'CoVar's apart
68         coVarName,
69
70         -- ** Modifying 'CoVar's
71         setCoVarUnique, setCoVarName
72
73     ) where
74
75 #include "HsVersions.h"
76 #include "Typeable.h"
77
78 import {-# SOURCE #-}   TypeRep( Type, Kind )
79 import {-# SOURCE #-}   TcType( TcTyVarDetails, pprTcTyVarDetails )
80 import {-# SOURCE #-}   IdInfo( IdDetails, IdInfo, pprIdDetails )
81 import {-# SOURCE #-}   TypeRep( isCoercionKind )
82
83 import Name hiding (varName)
84 import Unique
85 import Util
86 import FastTypes
87 import FastString
88 import Outputable
89
90 import Data.Data
91 \end{code}
92
93
94 %************************************************************************
95 %*                                                                      *
96                      Synonyms                                                                   
97 %*                                                                      *
98 %************************************************************************
99 -- These synonyms are here and not in Id because otherwise we need a very
100 -- large number of SOURCE imports of Id.hs :-(
101
102 \begin{code}
103 type EvVar = Var        -- An evidence variable: dictionary or equality constraint
104                         -- Could be an DictId or a CoVar
105
106 type Id     = Var       -- A term-level identifier
107 type DFunId = Id        -- A dictionary function
108 type EvId   = Id        -- Term-level evidence: DictId or IpId
109 type DictId = EvId      -- A dictionary variable
110 type IpId   = EvId      -- A term-level implicit parameter
111
112 type TyVar = Var
113 type CoVar = TyVar      -- A coercion variable is simply a type 
114                         -- variable of kind @ty1 ~ ty2@. Hence its
115                         -- 'varType' is always @PredTy (EqPred t1 t2)@
116 \end{code}
117
118 %************************************************************************
119 %*                                                                      *
120 \subsection{The main data type declarations}
121 %*                                                                      *
122 %************************************************************************
123
124
125 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
126 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
127 strictness).  The essential info about different kinds of @Vars@ is
128 in its @VarDetails@.
129
130 \begin{code}
131 -- | Essentially a typed 'Name', that may also contain some additional information
132 -- about the 'Var' and it's use sites.
133 data Var
134   = TyVar {
135         varName    :: !Name,
136         realUnique :: FastInt,          -- Key for fast comparison
137                                         -- Identical to the Unique in the name,
138                                         -- cached here for speed
139         varType       :: Kind,          -- ^ The type or kind of the 'Var' in question
140         isCoercionVar :: Bool
141  }
142
143   | TcTyVar {                           -- Used only during type inference
144                                         -- Used for kind variables during 
145                                         -- inference, as well
146         varName        :: !Name,
147         realUnique     :: FastInt,
148         varType        :: Kind,
149         tc_tv_details  :: TcTyVarDetails }
150
151   | Id {
152         varName    :: !Name,
153         realUnique :: FastInt,
154         varType    :: Type,
155         idScope    :: IdScope,
156         id_details :: IdDetails,        -- Stable, doesn't change
157         id_info    :: IdInfo }          -- Unstable, updated by simplifier
158     deriving Typeable
159
160 data IdScope    -- See Note [GlobalId/LocalId]
161   = GlobalId 
162   | LocalId ExportFlag
163
164 data ExportFlag 
165   = NotExported -- ^ Not exported: may be discarded as dead code.
166   | Exported    -- ^ Exported: kept alive
167 \end{code}
168
169 Note [GlobalId/LocalId]
170 ~~~~~~~~~~~~~~~~~~~~~~~
171 A GlobalId is
172   * always a constant (top-level)
173   * imported, or data constructor, or primop, or record selector
174   * has a Unique that is globally unique across the whole
175     GHC invocation (a single invocation may compile multiple modules)
176   * never treated as a candidate by the free-variable finder;
177         it's a constant!
178
179 A LocalId is 
180   * bound within an expression (lambda, case, local let(rec))
181   * or defined at top level in the module being compiled
182   * always treated as a candidate by the free-variable finder
183
184 After CoreTidy, top-level LocalIds are turned into GlobalIds
185
186 \begin{code}
187 instance Outputable Var where
188   ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
189
190 ppr_debug :: Var -> SDoc
191 ppr_debug (TyVar { isCoercionVar = False })   = ptext (sLit "tv")
192 ppr_debug (TyVar { isCoercionVar = True })    = ptext (sLit "co")
193 ppr_debug (TcTyVar {tc_tv_details = d})       = pprTcTyVarDetails d
194 ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
195
196 ppr_id_scope :: IdScope -> SDoc
197 ppr_id_scope GlobalId              = ptext (sLit "gid")
198 ppr_id_scope (LocalId Exported)    = ptext (sLit "lidx")
199 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
200
201 instance Show Var where
202   showsPrec p var = showsPrecSDoc p (ppr var)
203
204 instance NamedThing Var where
205   getName = varName
206
207 instance Uniquable Var where
208   getUnique = varUnique
209
210 instance Eq Var where
211     a == b = realUnique a ==# realUnique b
212
213 instance Ord Var where
214     a <= b = realUnique a <=# realUnique b
215     a <  b = realUnique a <#  realUnique b
216     a >= b = realUnique a >=# realUnique b
217     a >  b = realUnique a >#  realUnique b
218     a `compare` b = varUnique a `compare` varUnique b
219
220 instance Data Var where
221   -- don't traverse?
222   toConstr _   = abstractConstr "Var"
223   gunfold _ _  = error "gunfold"
224   dataTypeOf _ = mkNoRepType "Var"
225 \end{code}
226
227
228 \begin{code}
229 varUnique :: Var -> Unique
230 varUnique var = mkUniqueGrimily (iBox (realUnique var))
231
232 setVarUnique :: Var -> Unique -> Var
233 setVarUnique var uniq 
234   = var { realUnique = getKeyFastInt uniq, 
235           varName = setNameUnique (varName var) uniq }
236
237 setVarName :: Var -> Name -> Var
238 setVarName var new_name
239   = var { realUnique = getKeyFastInt (getUnique new_name), 
240           varName = new_name }
241
242 setVarType :: Id -> Type -> Id
243 setVarType id ty = id { varType = ty }
244 \end{code}
245
246
247 %************************************************************************
248 %*                                                                      *
249 \subsection{Type variables}
250 %*                                                                      *
251 %************************************************************************
252
253 \begin{code}
254 tyVarName :: TyVar -> Name
255 tyVarName = varName
256
257 tyVarKind :: TyVar -> Kind
258 tyVarKind = varType
259
260 setTyVarUnique :: TyVar -> Unique -> TyVar
261 setTyVarUnique = setVarUnique
262
263 setTyVarName :: TyVar -> Name -> TyVar
264 setTyVarName   = setVarName
265
266 setTyVarKind :: TyVar -> Kind -> TyVar
267 setTyVarKind tv k = tv {varType = k}
268 \end{code}
269
270 \begin{code}
271 mkTyVar :: Name -> Kind -> TyVar
272 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
273                     TyVar { varName    = name
274                           , realUnique = getKeyFastInt (nameUnique name)
275                           , varType  = kind
276                           , isCoercionVar    = False
277                         }
278
279 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
280 mkTcTyVar name kind details
281   = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
282     TcTyVar {   varName    = name,
283                 realUnique = getKeyFastInt (nameUnique name),
284                 varType  = kind,
285                 tc_tv_details = details
286         }
287
288 tcTyVarDetails :: TyVar -> TcTyVarDetails
289 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
290 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
291
292 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
293 setTcTyVarDetails tv details = tv { tc_tv_details = details }
294 \end{code}
295
296 %************************************************************************
297 %*                                                                      *
298 \subsection{Coercion variables}
299 %*                                                                      *
300 %************************************************************************
301
302 \begin{code}
303 coVarName :: CoVar -> Name
304 coVarName = varName
305
306 setCoVarUnique :: CoVar -> Unique -> CoVar
307 setCoVarUnique = setVarUnique
308
309 setCoVarName :: CoVar -> Name -> CoVar
310 setCoVarName   = setVarName
311
312 mkCoVar :: Name -> Kind -> CoVar
313 mkCoVar name kind = ASSERT( isCoercionKind kind )
314                     TyVar { varName       = name
315                           , realUnique    = getKeyFastInt (nameUnique name)
316                           , varType       = kind
317                           , isCoercionVar = True
318                         }
319
320 mkWildCoVar :: Kind -> TyVar
321 -- ^ Create a type variable that is never referred to, so its unique doesn't 
322 -- matter
323 mkWildCoVar = mkCoVar (mkSysTvName (mkBuiltinUnique 1) (fsLit "co_wild"))
324 \end{code}
325
326 %************************************************************************
327 %*                                                                      *
328 \subsection{Ids}
329 %*                                                                      *
330 %************************************************************************
331
332 \begin{code}
333 idInfo :: Id -> IdInfo
334 idInfo (Id { id_info = info }) = info
335 idInfo other                   = pprPanic "idInfo" (ppr other)
336
337 idDetails :: Id -> IdDetails
338 idDetails (Id { id_details = details }) = details
339 idDetails other                         = pprPanic "idDetails" (ppr other)
340
341 -- The next three have a 'Var' suffix even though they always build
342 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
343 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
344 mkGlobalVar details name ty info
345   = mk_id name ty GlobalId details info
346
347 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
348 mkLocalVar details name ty info
349   = mk_id name ty (LocalId NotExported) details  info
350
351 -- | Exported 'Var's will not be removed as dead code
352 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
353 mkExportedLocalVar details name ty info 
354   = mk_id name ty (LocalId Exported) details info
355
356 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
357 mk_id name ty scope details info
358   = Id { varName    = name, 
359          realUnique = getKeyFastInt (nameUnique name),
360          varType    = ty,       
361          idScope    = scope,
362          id_details = details,
363          id_info    = info }
364
365 -------------------
366 lazySetIdInfo :: Id -> IdInfo -> Var
367 lazySetIdInfo id info = id { id_info = info }
368
369 setIdDetails :: Id -> IdDetails -> Id
370 setIdDetails id details = id { id_details = details }
371
372 globaliseId :: Id -> Id
373 -- ^ If it's a local, make it global
374 globaliseId id = id { idScope = GlobalId }
375
376 setIdExported :: Id -> Id
377 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
378 -- and class operations, which are born as global 'Id's and automatically exported
379 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
380 setIdExported id@(Id { idScope = GlobalId })   = id
381 setIdExported tv                               = pprPanic "setIdExported" (ppr tv)
382
383 setIdNotExported :: Id -> Id
384 -- ^ We can only do this to LocalIds
385 setIdNotExported id = ASSERT( isLocalId id ) 
386                       id { idScope = LocalId NotExported }
387 \end{code}
388
389 %************************************************************************
390 %*                                                                      *
391 \subsection{Predicates over variables}
392 %*                                                                      *
393 %************************************************************************
394
395 \begin{code}
396 isTyCoVar :: Var -> Bool        -- True of both type and coercion variables
397 isTyCoVar (TyVar {})   = True
398 isTyCoVar (TcTyVar {}) = True
399 isTyCoVar _            = False
400
401 isTyVar :: Var -> Bool          -- True of both type variables only
402 isTyVar v@(TyVar {}) = not (isCoercionVar v)
403 isTyVar (TcTyVar {}) = True
404 isTyVar _            = False
405
406 isCoVar :: Var -> Bool          -- Only works after type checking (sigh)
407 isCoVar v@(TyVar {}) = isCoercionVar v
408 isCoVar _            = False
409
410 isTcTyVar :: Var -> Bool
411 isTcTyVar (TcTyVar {}) = True
412 isTcTyVar _            = False
413
414 isId :: Var -> Bool
415 isId (Id {}) = True
416 isId _       = False
417
418 isLocalId :: Var -> Bool
419 isLocalId (Id { idScope = LocalId _ }) = True
420 isLocalId _                            = False
421
422 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
423 -- These are the variables that we need to pay attention to when finding free
424 -- variables, or doing dependency analysis.
425 isLocalVar :: Var -> Bool
426 isLocalVar v = not (isGlobalId v)
427
428 isGlobalId :: Var -> Bool
429 isGlobalId (Id { idScope = GlobalId }) = True
430 isGlobalId _                           = False
431
432 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
433 -- that must have a binding in this module.  The converse
434 -- is not quite right: there are some global 'Id's that must have
435 -- bindings, such as record selectors.  But that doesn't matter,
436 -- because it's only used for assertions
437 mustHaveLocalBinding        :: Var -> Bool
438 mustHaveLocalBinding var = isLocalVar var
439
440 -- | 'isExportedIdVar' means \"don't throw this away\"
441 isExportedId :: Var -> Bool
442 isExportedId (Id { idScope = GlobalId })        = True
443 isExportedId (Id { idScope = LocalId Exported}) = True
444 isExportedId _ = False
445 \end{code}