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