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
150 data IdScope -- See Note [GlobalId/LocalId]
155 = NotExported -- ^ Not exported: may be discarded as dead code.
156 | Exported -- ^ Exported: kept alive
159 Note [GlobalId/LocalId]
160 ~~~~~~~~~~~~~~~~~~~~~~~
162 * always a constant (top-level)
163 * imported, or data constructor, or primop, or record selector
164 * has a Unique that is globally unique across the whole
165 GHC invocation (a single invocation may compile multiple modules)
166 * never treated as a candidate by the free-variable finder;
170 * bound within an expression (lambda, case, local let(rec))
171 * or defined at top level in the module being compiled
172 * always treated as a candidate by the free-variable finder
174 After CoreTidy, top-level LocalIds are turned into GlobalIds
177 instance Outputable Var where
178 ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
180 ppr_debug :: Var -> SDoc
181 ppr_debug (TyVar {}) = ptext (sLit "tv")
182 ppr_debug (TcTyVar {tc_tv_details = d}) = pprTcTyVarDetails d
183 ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
185 ppr_id_scope :: IdScope -> SDoc
186 ppr_id_scope GlobalId = ptext (sLit "gid")
187 ppr_id_scope (LocalId Exported) = ptext (sLit "lidx")
188 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
190 instance Show Var where
191 showsPrec p var = showsPrecSDoc p (ppr var)
193 instance NamedThing Var where
196 instance Uniquable Var where
197 getUnique = varUnique
199 instance Eq Var where
200 a == b = realUnique a ==# realUnique b
202 instance Ord Var where
203 a <= b = realUnique a <=# realUnique b
204 a < b = realUnique a <# realUnique b
205 a >= b = realUnique a >=# realUnique b
206 a > b = realUnique a ># realUnique b
207 a `compare` b = varUnique a `compare` varUnique b
209 instance Data Var where
211 toConstr _ = abstractConstr "Var"
212 gunfold _ _ = error "gunfold"
213 dataTypeOf _ = mkNoRepType "Var"
218 varUnique :: Var -> Unique
219 varUnique var = mkUniqueGrimily (iBox (realUnique var))
221 setVarUnique :: Var -> Unique -> Var
222 setVarUnique var uniq
223 = var { realUnique = getKeyFastInt uniq,
224 varName = setNameUnique (varName var) uniq }
226 setVarName :: Var -> Name -> Var
227 setVarName var new_name
228 = var { realUnique = getKeyFastInt (getUnique new_name),
231 setVarType :: Id -> Type -> Id
232 setVarType id ty = id { varType = ty }
236 %************************************************************************
238 \subsection{Type variables}
240 %************************************************************************
243 tyVarName :: TyVar -> Name
246 tyVarKind :: TyVar -> Kind
249 setTyVarUnique :: TyVar -> Unique -> TyVar
250 setTyVarUnique = setVarUnique
252 setTyVarName :: TyVar -> Name -> TyVar
253 setTyVarName = setVarName
255 setTyVarKind :: TyVar -> Kind -> TyVar
256 setTyVarKind tv k = tv {varType = k}
260 mkTyVar :: Name -> Kind -> TyVar
261 mkTyVar name kind = TyVar { varName = name
262 , realUnique = getKeyFastInt (nameUnique name)
266 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
267 mkTcTyVar name kind details
268 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
269 TcTyVar { varName = name,
270 realUnique = getKeyFastInt (nameUnique name),
272 tc_tv_details = details
275 tcTyVarDetails :: TyVar -> TcTyVarDetails
276 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
277 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
279 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
280 setTcTyVarDetails tv details = tv { tc_tv_details = details }
283 %************************************************************************
287 %************************************************************************
290 idInfo :: Id -> IdInfo
291 idInfo (Id { id_info = info }) = info
292 idInfo other = pprPanic "idInfo" (ppr other)
294 idDetails :: Id -> IdDetails
295 idDetails (Id { id_details = details }) = details
296 idDetails other = pprPanic "idDetails" (ppr other)
298 -- The next three have a 'Var' suffix even though they always build
299 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
300 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
301 mkGlobalVar details name ty info
302 = mk_id name ty GlobalId details info
304 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
305 mkLocalVar details name ty info
306 = mk_id name ty (LocalId NotExported) details info
308 mkCoVar :: Name -> Type -> CoVar
309 -- Coercion variables have no IdInfo
310 mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
312 -- | Exported 'Var's will not be removed as dead code
313 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
314 mkExportedLocalVar details name ty info
315 = mk_id name ty (LocalId Exported) details info
317 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
318 mk_id name ty scope details info
319 = Id { varName = name,
320 realUnique = getKeyFastInt (nameUnique name),
323 id_details = details,
327 lazySetIdInfo :: Id -> IdInfo -> Var
328 lazySetIdInfo id info = id { id_info = info }
330 setIdDetails :: Id -> IdDetails -> Id
331 setIdDetails id details = id { id_details = details }
333 globaliseId :: Id -> Id
334 -- ^ If it's a local, make it global
335 globaliseId id = id { idScope = GlobalId }
337 setIdExported :: Id -> Id
338 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
339 -- and class operations, which are born as global 'Id's and automatically exported
340 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
341 setIdExported id@(Id { idScope = GlobalId }) = id
342 setIdExported tv = pprPanic "setIdExported" (ppr tv)
344 setIdNotExported :: Id -> Id
345 -- ^ We can only do this to LocalIds
346 setIdNotExported id = ASSERT( isLocalId id )
347 id { idScope = LocalId NotExported }
350 %************************************************************************
352 \subsection{Predicates over variables}
354 %************************************************************************
357 isTyVar :: Var -> Bool -- True of both type variables only
358 isTyVar (TyVar {}) = True
359 isTyVar (TcTyVar {}) = True
362 isTcTyVar :: Var -> Bool
363 isTcTyVar (TcTyVar {}) = True
370 isLocalId :: Var -> Bool
371 isLocalId (Id { idScope = LocalId _ }) = True
374 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
375 -- These are the variables that we need to pay attention to when finding free
376 -- variables, or doing dependency analysis.
377 isLocalVar :: Var -> Bool
378 isLocalVar v = not (isGlobalId v)
380 isGlobalId :: Var -> Bool
381 isGlobalId (Id { idScope = GlobalId }) = True
384 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
385 -- that must have a binding in this module. The converse
386 -- is not quite right: there are some global 'Id's that must have
387 -- bindings, such as record selectors. But that doesn't matter,
388 -- because it's only used for assertions
389 mustHaveLocalBinding :: Var -> Bool
390 mustHaveLocalBinding var = isLocalVar var
392 -- | 'isExportedIdVar' means \"don't throw this away\"
393 isExportedId :: Var -> Bool
394 isExportedId (Id { idScope = GlobalId }) = True
395 isExportedId (Id { idScope = LocalId Exported}) = True
396 isExportedId _ = False