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
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.
25 -- These 'Var.Var' names may either be global or local, see "Var#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.
34 -- * The main data type and synonyms
35 Var, TyVar, CoVar, TyCoVar, Id, DictId, DFunId, EvVar, EvId, IpId,
37 -- ** Taking 'Var's apart
38 varName, varUnique, varType,
40 -- ** Modifying 'Var's
41 setVarName, setVarUnique, setVarType,
43 -- ** Constructing, taking apart, modifying 'Id's
44 mkGlobalVar, mkLocalVar, mkExportedLocalVar, mkCoVar,
46 lazySetIdInfo, setIdDetails, globaliseId,
47 setIdExported, setIdNotExported,
50 isId, isTyVar, isTcTyVar,
51 isLocalVar, isLocalId,
52 isGlobalId, isExportedId,
55 -- ** Constructing 'TyVar's
58 -- ** Taking 'TyVar's apart
59 tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
61 -- ** Modifying 'TyVar's
62 setTyVarName, setTyVarUnique, setTyVarKind
66 #include "HsVersions.h"
69 import {-# SOURCE #-} TypeRep( Type, Kind )
70 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
71 import {-# SOURCE #-} IdInfo( IdDetails, IdInfo, coVarDetails, vanillaIdInfo, pprIdDetails )
73 import Name hiding (varName)
84 %************************************************************************
88 %************************************************************************
89 -- These synonyms are here and not in Id because otherwise we need a very
90 -- large number of SOURCE imports of Id.hs :-(
93 type EvVar = Var -- An evidence variable: dictionary or equality constraint
94 -- Could be an DictId or a CoVar
96 type Id = Var -- A term-level identifier
97 type DFunId = Id -- A dictionary function
98 type EvId = Id -- Term-level evidence: DictId or IpId
99 type DictId = EvId -- A dictionary variable
100 type IpId = EvId -- A term-level implicit parameter
103 type CoVar = Id -- A coercion variable is simply an Id
104 -- variable of kind @ty1 ~ ty2@. Hence its
105 -- 'varType' is always @PredTy (EqPred t1 t2)@
106 type TyCoVar = TyVar -- Something that is a type OR coercion variable.
109 %************************************************************************
111 \subsection{The main data type declarations}
113 %************************************************************************
116 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
117 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
118 strictness). The essential info about different kinds of @Vars@ is
122 -- | Essentially a typed 'Name', that may also contain some additional information
123 -- about the 'Var' and it's use sites.
127 realUnique :: FastInt, -- Key for fast comparison
128 -- Identical to the Unique in the name,
129 -- cached here for speed
130 varType :: Kind -- ^ The type or kind of the 'Var' in question
133 | TcTyVar { -- Used only during type inference
134 -- Used for kind variables during
135 -- inference, as well
137 realUnique :: FastInt,
139 tc_tv_details :: TcTyVarDetails }
143 realUnique :: FastInt,
146 id_details :: IdDetails, -- Stable, doesn't change
147 id_info :: IdInfo } -- Unstable, updated by simplifier
149 data IdScope -- See Note [GlobalId/LocalId]
154 = NotExported -- ^ Not exported: may be discarded as dead code.
155 | Exported -- ^ Exported: kept alive
158 Note [GlobalId/LocalId]
159 ~~~~~~~~~~~~~~~~~~~~~~~
161 * always a constant (top-level)
162 * imported, or data constructor, or primop, or record selector
163 * has a Unique that is globally unique across the whole
164 GHC invocation (a single invocation may compile multiple modules)
165 * never treated as a candidate by the free-variable finder;
169 * bound within an expression (lambda, case, local let(rec))
170 * or defined at top level in the module being compiled
171 * always treated as a candidate by the free-variable finder
173 After CoreTidy, top-level LocalIds are turned into GlobalIds
176 instance Outputable Var where
177 ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
179 ppr_debug :: Var -> SDoc
180 ppr_debug (TyVar {}) = ptext (sLit "tv")
181 ppr_debug (TcTyVar {tc_tv_details = d}) = pprTcTyVarDetails d
182 ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
184 ppr_id_scope :: IdScope -> SDoc
185 ppr_id_scope GlobalId = ptext (sLit "gid")
186 ppr_id_scope (LocalId Exported) = ptext (sLit "lidx")
187 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
189 instance Show Var where
190 showsPrec p var = showsPrecSDoc p (ppr var)
192 instance NamedThing Var where
195 instance Uniquable Var where
196 getUnique = varUnique
198 instance Eq Var where
199 a == b = realUnique a ==# realUnique b
201 instance Ord Var where
202 a <= b = realUnique a <=# realUnique b
203 a < b = realUnique a <# realUnique b
204 a >= b = realUnique a >=# realUnique b
205 a > b = realUnique a ># realUnique b
206 a `compare` b = varUnique a `compare` varUnique b
208 INSTANCE_TYPEABLE0(Var,varTc,"Var")
210 instance Data Var where
212 toConstr _ = abstractConstr "Var"
213 gunfold _ _ = error "gunfold"
214 dataTypeOf _ = mkNoRepType "Var"
219 varUnique :: Var -> Unique
220 varUnique var = mkUniqueGrimily (iBox (realUnique var))
222 setVarUnique :: Var -> Unique -> Var
223 setVarUnique var uniq
224 = var { realUnique = getKeyFastInt uniq,
225 varName = setNameUnique (varName var) uniq }
227 setVarName :: Var -> Name -> Var
228 setVarName var new_name
229 = var { realUnique = getKeyFastInt (getUnique new_name),
232 setVarType :: Id -> Type -> Id
233 setVarType id ty = id { varType = ty }
237 %************************************************************************
239 \subsection{Type variables}
241 %************************************************************************
244 tyVarName :: TyVar -> Name
247 tyVarKind :: TyVar -> Kind
250 setTyVarUnique :: TyVar -> Unique -> TyVar
251 setTyVarUnique = setVarUnique
253 setTyVarName :: TyVar -> Name -> TyVar
254 setTyVarName = setVarName
256 setTyVarKind :: TyVar -> Kind -> TyVar
257 setTyVarKind tv k = tv {varType = k}
261 mkTyVar :: Name -> Kind -> TyVar
262 mkTyVar name kind = TyVar { varName = name
263 , realUnique = getKeyFastInt (nameUnique name)
267 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
268 mkTcTyVar name kind details
269 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
270 TcTyVar { varName = name,
271 realUnique = getKeyFastInt (nameUnique name),
273 tc_tv_details = details
276 tcTyVarDetails :: TyVar -> TcTyVarDetails
277 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
278 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
280 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
281 setTcTyVarDetails tv details = tv { tc_tv_details = details }
284 %************************************************************************
288 %************************************************************************
291 idInfo :: Id -> IdInfo
292 idInfo (Id { id_info = info }) = info
293 idInfo other = pprPanic "idInfo" (ppr other)
295 idDetails :: Id -> IdDetails
296 idDetails (Id { id_details = details }) = details
297 idDetails other = pprPanic "idDetails" (ppr other)
299 -- The next three have a 'Var' suffix even though they always build
300 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
301 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
302 mkGlobalVar details name ty info
303 = mk_id name ty GlobalId details info
305 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
306 mkLocalVar details name ty info
307 = mk_id name ty (LocalId NotExported) details info
309 mkCoVar :: Name -> Type -> CoVar
310 -- Coercion variables have no IdInfo
311 mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
313 -- | Exported 'Var's will not be removed as dead code
314 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
315 mkExportedLocalVar details name ty info
316 = mk_id name ty (LocalId Exported) details info
318 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
319 mk_id name ty scope details info
320 = Id { varName = name,
321 realUnique = getKeyFastInt (nameUnique name),
324 id_details = details,
328 lazySetIdInfo :: Id -> IdInfo -> Var
329 lazySetIdInfo id info = id { id_info = info }
331 setIdDetails :: Id -> IdDetails -> Id
332 setIdDetails id details = id { id_details = details }
334 globaliseId :: Id -> Id
335 -- ^ If it's a local, make it global
336 globaliseId id = id { idScope = GlobalId }
338 setIdExported :: Id -> Id
339 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
340 -- and class operations, which are born as global 'Id's and automatically exported
341 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
342 setIdExported id@(Id { idScope = GlobalId }) = id
343 setIdExported tv = pprPanic "setIdExported" (ppr tv)
345 setIdNotExported :: Id -> Id
346 -- ^ We can only do this to LocalIds
347 setIdNotExported id = ASSERT( isLocalId id )
348 id { idScope = LocalId NotExported }
351 %************************************************************************
353 \subsection{Predicates over variables}
355 %************************************************************************
358 isTyVar :: Var -> Bool -- True of both type variables only
359 isTyVar (TyVar {}) = True
360 isTyVar (TcTyVar {}) = True
363 isTcTyVar :: Var -> Bool
364 isTcTyVar (TcTyVar {}) = True
371 isLocalId :: Var -> Bool
372 isLocalId (Id { idScope = LocalId _ }) = True
375 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
376 -- These are the variables that we need to pay attention to when finding free
377 -- variables, or doing dependency analysis.
378 isLocalVar :: Var -> Bool
379 isLocalVar v = not (isGlobalId v)
381 isGlobalId :: Var -> Bool
382 isGlobalId (Id { idScope = GlobalId }) = True
385 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
386 -- that must have a binding in this module. The converse
387 -- is not quite right: there are some global 'Id's that must have
388 -- bindings, such as record selectors. But that doesn't matter,
389 -- because it's only used for assertions
390 mustHaveLocalBinding :: Var -> Bool
391 mustHaveLocalBinding var = isLocalVar var
393 -- | 'isExportedIdVar' means \"don't throw this away\"
394 isExportedId :: Var -> Bool
395 isExportedId (Id { idScope = GlobalId }) = True
396 isExportedId (Id { idScope = LocalId Exported}) = True
397 isExportedId _ = False