2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section{@Vars@: Variables}
10 -- GHC uses several kinds of name internally:
12 -- * 'OccName.OccName': see "OccName#name_types"
14 -- * 'RdrName.RdrName': see "RdrName#name_types"
16 -- * 'Name.Name': see "Name#name_types"
18 -- * 'Id.Id': see "Id#name_types"
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"
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.
28 -- * The main data type and synonyms
29 Var, TyVar, CoVar, Id, DictId, DFunId, EvVar, EvId, IpId,
31 -- ** Taking 'Var's apart
32 varName, varUnique, varType,
34 -- ** Modifying 'Var's
35 setVarName, setVarUnique, setVarType,
37 -- ** Constructing, taking apart, modifying 'Id's
38 mkGlobalVar, mkLocalVar, mkExportedLocalVar,
40 lazySetIdInfo, setIdDetails, globaliseId,
41 setIdExported, setIdNotExported,
44 isCoVar, isId, isTyCoVar, isTyVar, isTcTyVar,
45 isLocalVar, isLocalId,
46 isGlobalId, isExportedId,
49 -- ** Constructing 'TyVar's
50 mkTyVar, mkTcTyVar, mkWildCoVar,
52 -- ** Taking 'TyVar's apart
53 tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
55 -- ** Modifying 'TyVar's
56 setTyVarName, setTyVarUnique, setTyVarKind,
58 -- ** Constructing 'CoVar's
61 -- ** Taking 'CoVar's apart
64 -- ** Modifying 'CoVar's
65 setCoVarUnique, setCoVarName
69 #include "HsVersions.h"
72 import {-# SOURCE #-} TypeRep( Type, Kind )
73 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
74 import {-# SOURCE #-} IdInfo( IdDetails, IdInfo, pprIdDetails )
75 import {-# SOURCE #-} TypeRep( isCoercionKind )
77 import Name hiding (varName)
88 %************************************************************************
92 %************************************************************************
93 -- These synonyms are here and not in Id because otherwise we need a very
94 -- large number of SOURCE imports of Id.hs :-(
97 type EvVar = Var -- An evidence variable: dictionary or equality constraint
98 -- Could be an DictId or a CoVar
100 type Id = Var -- A term-level identifier
101 type DFunId = Id -- A dictionary function
102 type EvId = Id -- Term-level evidence: DictId or IpId
103 type DictId = EvId -- A dictionary variable
104 type IpId = EvId -- A term-level implicit parameter
107 type CoVar = TyVar -- A coercion variable is simply a type
108 -- variable of kind @ty1 ~ ty2@. Hence its
109 -- 'varType' is always @PredTy (EqPred t1 t2)@
112 %************************************************************************
114 \subsection{The main data type declarations}
116 %************************************************************************
119 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
120 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
121 strictness). The essential info about different kinds of @Vars@ is
125 -- | Essentially a typed 'Name', that may also contain some additional information
126 -- about the 'Var' and it's use sites.
130 realUnique :: FastInt, -- Key for fast comparison
131 -- Identical to the Unique in the name,
132 -- cached here for speed
133 varType :: Kind, -- ^ The type or kind of the 'Var' in question
134 isCoercionVar :: Bool
137 | TcTyVar { -- Used only during type inference
138 -- Used for kind variables during
139 -- inference, as well
141 realUnique :: FastInt,
143 tc_tv_details :: TcTyVarDetails }
147 realUnique :: FastInt,
150 id_details :: IdDetails, -- Stable, doesn't change
151 id_info :: IdInfo } -- Unstable, updated by simplifier
153 data IdScope -- See Note [GlobalId/LocalId]
158 = NotExported -- ^ Not exported: may be discarded as dead code.
159 | Exported -- ^ Exported: kept alive
162 Note [GlobalId/LocalId]
163 ~~~~~~~~~~~~~~~~~~~~~~~
165 * always a constant (top-level)
166 * imported, or data constructor, or primop, or record selector
167 * has a Unique that is globally unique across the whole
168 GHC invocation (a single invocation may compile multiple modules)
169 * never treated as a candidate by the free-variable finder;
173 * bound within an expression (lambda, case, local let(rec))
174 * or defined at top level in the module being compiled
175 * always treated as a candidate by the free-variable finder
177 After CoreTidy, top-level LocalIds are turned into GlobalIds
180 instance Outputable Var where
181 ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
183 ppr_debug :: Var -> SDoc
184 ppr_debug (TyVar { isCoercionVar = False }) = ptext (sLit "tv")
185 ppr_debug (TyVar { isCoercionVar = True }) = ptext (sLit "co")
186 ppr_debug (TcTyVar {tc_tv_details = d}) = pprTcTyVarDetails d
187 ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
189 ppr_id_scope :: IdScope -> SDoc
190 ppr_id_scope GlobalId = ptext (sLit "gid")
191 ppr_id_scope (LocalId Exported) = ptext (sLit "lidx")
192 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
194 instance Show Var where
195 showsPrec p var = showsPrecSDoc p (ppr var)
197 instance NamedThing Var where
200 instance Uniquable Var where
201 getUnique = varUnique
203 instance Eq Var where
204 a == b = realUnique a ==# realUnique b
206 instance Ord Var where
207 a <= b = realUnique a <=# realUnique b
208 a < b = realUnique a <# realUnique b
209 a >= b = realUnique a >=# realUnique b
210 a > b = realUnique a ># realUnique b
211 a `compare` b = varUnique a `compare` varUnique b
213 INSTANCE_TYPEABLE0(Var,varTc,"Var")
215 instance Data Var where
217 toConstr _ = abstractConstr "Var"
218 gunfold _ _ = error "gunfold"
219 dataTypeOf _ = mkNoRepType "Var"
224 varUnique :: Var -> Unique
225 varUnique var = mkUniqueGrimily (iBox (realUnique var))
227 setVarUnique :: Var -> Unique -> Var
228 setVarUnique var uniq
229 = var { realUnique = getKeyFastInt uniq,
230 varName = setNameUnique (varName var) uniq }
232 setVarName :: Var -> Name -> Var
233 setVarName var new_name
234 = var { realUnique = getKeyFastInt (getUnique new_name),
237 setVarType :: Id -> Type -> Id
238 setVarType id ty = id { varType = ty }
242 %************************************************************************
244 \subsection{Type variables}
246 %************************************************************************
249 tyVarName :: TyVar -> Name
252 tyVarKind :: TyVar -> Kind
255 setTyVarUnique :: TyVar -> Unique -> TyVar
256 setTyVarUnique = setVarUnique
258 setTyVarName :: TyVar -> Name -> TyVar
259 setTyVarName = setVarName
261 setTyVarKind :: TyVar -> Kind -> TyVar
262 setTyVarKind tv k = tv {varType = k}
266 mkTyVar :: Name -> Kind -> TyVar
267 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
268 TyVar { varName = name
269 , realUnique = getKeyFastInt (nameUnique name)
271 , isCoercionVar = False
274 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
275 mkTcTyVar name kind details
276 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
277 TcTyVar { varName = name,
278 realUnique = getKeyFastInt (nameUnique name),
280 tc_tv_details = details
283 tcTyVarDetails :: TyVar -> TcTyVarDetails
284 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
285 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
287 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
288 setTcTyVarDetails tv details = tv { tc_tv_details = details }
291 %************************************************************************
293 \subsection{Coercion variables}
295 %************************************************************************
298 coVarName :: CoVar -> Name
301 setCoVarUnique :: CoVar -> Unique -> CoVar
302 setCoVarUnique = setVarUnique
304 setCoVarName :: CoVar -> Name -> CoVar
305 setCoVarName = setVarName
307 mkCoVar :: Name -> Kind -> CoVar
308 mkCoVar name kind = ASSERT( isCoercionKind kind )
309 TyVar { varName = name
310 , realUnique = getKeyFastInt (nameUnique name)
312 , isCoercionVar = True
315 mkWildCoVar :: Kind -> TyVar
316 -- ^ Create a type variable that is never referred to, so its unique doesn't
318 mkWildCoVar = mkCoVar (mkSysTvName (mkBuiltinUnique 1) (fsLit "co_wild"))
321 %************************************************************************
325 %************************************************************************
328 idInfo :: Id -> IdInfo
329 idInfo (Id { id_info = info }) = info
330 idInfo other = pprPanic "idInfo" (ppr other)
332 idDetails :: Id -> IdDetails
333 idDetails (Id { id_details = details }) = details
334 idDetails other = pprPanic "idDetails" (ppr other)
336 -- The next three have a 'Var' suffix even though they always build
337 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
338 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
339 mkGlobalVar details name ty info
340 = mk_id name ty GlobalId details info
342 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
343 mkLocalVar details name ty info
344 = mk_id name ty (LocalId NotExported) details info
346 -- | Exported 'Var's will not be removed as dead code
347 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
348 mkExportedLocalVar details name ty info
349 = mk_id name ty (LocalId Exported) details info
351 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
352 mk_id name ty scope details info
353 = Id { varName = name,
354 realUnique = getKeyFastInt (nameUnique name),
357 id_details = details,
361 lazySetIdInfo :: Id -> IdInfo -> Var
362 lazySetIdInfo id info = id { id_info = info }
364 setIdDetails :: Id -> IdDetails -> Id
365 setIdDetails id details = id { id_details = details }
367 globaliseId :: Id -> Id
368 -- ^ If it's a local, make it global
369 globaliseId id = id { idScope = GlobalId }
371 setIdExported :: Id -> Id
372 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
373 -- and class operations, which are born as global 'Id's and automatically exported
374 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
375 setIdExported id@(Id { idScope = GlobalId }) = id
376 setIdExported tv = pprPanic "setIdExported" (ppr tv)
378 setIdNotExported :: Id -> Id
379 -- ^ We can only do this to LocalIds
380 setIdNotExported id = ASSERT( isLocalId id )
381 id { idScope = LocalId NotExported }
384 %************************************************************************
386 \subsection{Predicates over variables}
388 %************************************************************************
391 isTyCoVar :: Var -> Bool -- True of both type and coercion variables
392 isTyCoVar (TyVar {}) = True
393 isTyCoVar (TcTyVar {}) = True
396 isTyVar :: Var -> Bool -- True of both type variables only
397 isTyVar v@(TyVar {}) = not (isCoercionVar v)
398 isTyVar (TcTyVar {}) = True
401 isCoVar :: Var -> Bool -- Only works after type checking (sigh)
402 isCoVar v@(TyVar {}) = isCoercionVar v
405 isTcTyVar :: Var -> Bool
406 isTcTyVar (TcTyVar {}) = True
413 isLocalId :: Var -> Bool
414 isLocalId (Id { idScope = LocalId _ }) = True
417 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
418 -- These are the variables that we need to pay attention to when finding free
419 -- variables, or doing dependency analysis.
420 isLocalVar :: Var -> Bool
421 isLocalVar v = not (isGlobalId v)
423 isGlobalId :: Var -> Bool
424 isGlobalId (Id { idScope = GlobalId }) = True
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 -- bindings, such as record selectors. But that doesn't matter,
431 -- because it's only used for assertions
432 mustHaveLocalBinding :: Var -> Bool
433 mustHaveLocalBinding var = isLocalVar var
435 -- | 'isExportedIdVar' means \"don't throw this away\"
436 isExportedId :: Var -> Bool
437 isExportedId (Id { idScope = GlobalId }) = True
438 isExportedId (Id { idScope = LocalId Exported}) = True
439 isExportedId _ = False