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