Make record selectors into ordinary functions
[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         idDetails  :: IdDetails,        -- Stable, doesn't change
131         idInfo     :: 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
141 \end{code}
142
143 Note [GlobalId/LocalId]
144 ~~~~~~~~~~~~~~~~~~~~~~~
145 A GlobalId is
146   * always a constant (top-level)
147   * imported, or data constructor, or primop, or record selector
148   * has a Unique that is globally unique across the whole
149     GHC invocation (a single invocation may compile multiple modules)
150   * never treated as a candidate by the free-variable finder;
151         it's a constant!
152
153 A LocalId is 
154   * bound within an expression (lambda, case, local let(rec))
155   * or defined at top level in the module being compiled
156   * always treated as a candidate by the free-variable finder
157
158 After CoreTidy, top-level LocalIds are turned into GlobalIds
159
160 \begin{code}
161 instance Outputable Var where
162   ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
163
164 ppr_debug :: Var -> SDoc
165 ppr_debug (TyVar {})                          = ptext (sLit "tv")
166 ppr_debug (TcTyVar {tcTyVarDetails = d})      = pprTcTyVarDetails d
167 ppr_debug (Id { idScope = s, idDetails = d }) = ppr_id_scope s <> pprIdDetails d
168
169 ppr_id_scope :: IdScope -> SDoc
170 ppr_id_scope GlobalId              = ptext (sLit "gid")
171 ppr_id_scope (LocalId Exported)    = ptext (sLit "lidx")
172 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
173
174 instance Show Var where
175   showsPrec p var = showsPrecSDoc p (ppr var)
176
177 instance NamedThing Var where
178   getName = varName
179
180 instance Uniquable Var where
181   getUnique = varUnique
182
183 instance Eq Var where
184     a == b = realUnique a ==# realUnique b
185
186 instance Ord Var where
187     a <= b = realUnique a <=# realUnique b
188     a <  b = realUnique a <#  realUnique b
189     a >= b = realUnique a >=# realUnique b
190     a >  b = realUnique a >#  realUnique b
191     a `compare` b = varUnique a `compare` varUnique b
192 \end{code}
193
194
195 \begin{code}
196 varUnique :: Var -> Unique
197 varUnique var = mkUniqueGrimily (iBox (realUnique var))
198
199 setVarUnique :: Var -> Unique -> Var
200 setVarUnique var uniq 
201   = var { realUnique = getKeyFastInt uniq, 
202           varName = setNameUnique (varName var) uniq }
203
204 setVarName :: Var -> Name -> Var
205 setVarName var new_name
206   = var { realUnique = getKeyFastInt (getUnique new_name), 
207           varName = new_name }
208
209 setVarType :: Id -> Type -> Id
210 setVarType id ty = id { varType = ty }
211 \end{code}
212
213
214 %************************************************************************
215 %*                                                                      *
216 \subsection{Type variables}
217 %*                                                                      *
218 %************************************************************************
219
220 \begin{code}
221 type TyVar = Var
222
223 tyVarName :: TyVar -> Name
224 tyVarName = varName
225
226 tyVarKind :: TyVar -> Kind
227 tyVarKind = varType
228
229 setTyVarUnique :: TyVar -> Unique -> TyVar
230 setTyVarUnique = setVarUnique
231
232 setTyVarName :: TyVar -> Name -> TyVar
233 setTyVarName   = setVarName
234
235 setTyVarKind :: TyVar -> Kind -> TyVar
236 setTyVarKind tv k = tv {varType = k}
237 \end{code}
238
239 \begin{code}
240 mkTyVar :: Name -> Kind -> TyVar
241 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
242                     TyVar { varName    = name
243                           , realUnique = getKeyFastInt (nameUnique name)
244                           , varType  = kind
245                           , isCoercionVar    = False
246                         }
247
248 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
249 mkTcTyVar name kind details
250   = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
251     TcTyVar {   varName    = name,
252                 realUnique = getKeyFastInt (nameUnique name),
253                 varType  = kind,
254                 tcTyVarDetails = details
255         }
256 \end{code}
257
258 %************************************************************************
259 %*                                                                      *
260 \subsection{Coercion variables}
261 %*                                                                      *
262 %************************************************************************
263
264 \begin{code}
265 type CoVar = TyVar -- A coercion variable is simply a type 
266                         -- variable of kind @ty1 ~ ty2@. Hence its
267                         -- 'varType' is always @PredTy (EqPred t1 t2)@
268
269 coVarName :: CoVar -> Name
270 coVarName = varName
271
272 setCoVarUnique :: CoVar -> Unique -> CoVar
273 setCoVarUnique = setVarUnique
274
275 setCoVarName :: CoVar -> Name -> CoVar
276 setCoVarName   = setVarName
277
278 mkCoVar :: Name -> Kind -> CoVar
279 mkCoVar name kind = ASSERT( isCoercionKind kind )
280                     TyVar { varName       = name
281                           , realUnique    = getKeyFastInt (nameUnique name)
282                           , varType       = kind
283                           , isCoercionVar = True
284                         }
285
286 mkWildCoVar :: Kind -> TyVar
287 -- ^ Create a type variable that is never referred to, so its unique doesn't 
288 -- matter
289 mkWildCoVar = mkCoVar (mkSysTvName (mkBuiltinUnique 1) (fsLit "co_wild"))
290 \end{code}
291
292 %************************************************************************
293 %*                                                                      *
294 \subsection{Ids}
295 %*                                                                      *
296 %************************************************************************
297
298 \begin{code}
299 -- These synonyms are here and not in Id because otherwise we need a very
300 -- large number of SOURCE imports of Id.hs :-(
301 type Id = Var
302 type DictId = Var
303
304 -- The next three have a 'Var' suffix even though they always build
305 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
306 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
307 mkGlobalVar details name ty info
308   = mk_id name ty GlobalId details info
309
310 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
311 mkLocalVar details name ty info
312   = mk_id name ty (LocalId NotExported) details  info
313
314 -- | Exported 'Var's will not be removed as dead code
315 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
316 mkExportedLocalVar details name ty info 
317   = mk_id name ty (LocalId Exported) details info
318
319 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
320 mk_id name ty scope details info
321   = Id { varName    = name, 
322          realUnique = getKeyFastInt (nameUnique name),
323          varType    = ty,       
324          idScope    = scope,
325          idDetails  = details,
326          idInfo     = info }
327
328 -------------------
329 lazySetIdInfo :: Id -> IdInfo -> Var
330 lazySetIdInfo id info = id { idInfo = info }
331
332 setIdDetails :: Id -> IdDetails -> Id
333 setIdDetails id details = id { idDetails = details }
334
335 globaliseId :: Id -> Id
336 -- ^ If it's a local, make it global
337 globaliseId id = id { idScope = GlobalId }
338
339 setIdExported :: Id -> Id
340 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
341 -- and class operations, which are born as global 'Id's and automatically exported
342 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
343 setIdExported id@(Id { idScope = GlobalId })   = id
344 setIdExported tv                               = pprPanic "setIdExported" (ppr tv)
345
346 setIdNotExported :: Id -> Id
347 -- ^ We can only do this to LocalIds
348 setIdNotExported id = ASSERT( isLocalId id ) 
349                       id { idScope = LocalId NotExported }
350 \end{code}
351
352 %************************************************************************
353 %*                                                                      *
354 \subsection{Predicates over variables}
355 %*                                                                      *
356 %************************************************************************
357
358 \begin{code}
359 isTyVar :: Var -> Bool
360 isTyVar (TyVar {})   = True
361 isTyVar (TcTyVar {}) = True
362 isTyVar _            = False
363
364 isTcTyVar :: Var -> Bool
365 isTcTyVar (TcTyVar {}) = True
366 isTcTyVar _            = False
367
368 isId :: Var -> Bool
369 isId (Id {}) = True
370 isId _       = False
371
372 isLocalId :: Var -> Bool
373 isLocalId (Id { idScope = LocalId _ }) = True
374 isLocalId _                            = False
375
376 isCoVar :: Var -> Bool
377 isCoVar (v@(TyVar {}))             = isCoercionVar v
378 isCoVar (TcTyVar {varType = kind}) = isCoercionKind kind  -- used during solving
379 isCoVar _                          = False
380
381 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
382 -- These are the variables that we need to pay attention to when finding free
383 -- variables, or doing dependency analysis.
384 isLocalVar :: Var -> Bool
385 isLocalVar v = not (isGlobalId v)
386
387 isGlobalId :: Var -> Bool
388 isGlobalId (Id { idScope = GlobalId }) = True
389 isGlobalId _                           = False
390
391 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
392 -- that must have a binding in this module.  The converse
393 -- is not quite right: there are some global 'Id's that must have
394 -- bindings, such as record selectors.  But that doesn't matter,
395 -- because it's only used for assertions
396 mustHaveLocalBinding        :: Var -> Bool
397 mustHaveLocalBinding var = isLocalVar var
398
399 -- | 'isExportedIdVar' means \"don't throw this away\"
400 isExportedId :: Var -> Bool
401 isExportedId (Id { idScope = GlobalId })        = True
402 isExportedId (Id { idScope = LocalId Exported}) = True
403 isExportedId _ = False
404 \end{code}