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
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, isTyVar, isTcTyVar,
45 isLocalVar, isLocalId,
46 isGlobalId, isExportedId,
49 -- * Type variable data type
52 -- ** Constructing 'TyVar's
53 mkTyVar, mkTcTyVar, mkWildCoVar,
55 -- ** Taking 'TyVar's apart
56 tyVarName, tyVarKind, tcTyVarDetails,
58 -- ** Modifying 'TyVar's
59 setTyVarName, setTyVarUnique, setTyVarKind,
61 -- * Coercion variable data type
64 -- ** Constructing 'CoVar's
67 -- ** Taking 'CoVar's apart
70 -- ** Modifying 'CoVar's
71 setCoVarUnique, setCoVarName,
73 -- * 'Var' type synonyms
77 #include "HsVersions.h"
79 import {-# SOURCE #-} TypeRep( Type, Kind )
80 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
81 import {-# SOURCE #-} IdInfo( IdDetails, IdInfo, pprIdDetails )
82 import {-# SOURCE #-} TypeRep( isCoercionKind )
84 import Name hiding (varName)
92 %************************************************************************
94 \subsection{The main data type declarations}
96 %************************************************************************
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
105 -- | Essentially a typed 'Name', that may also contain some additional information
106 -- about the 'Var' and it's use sites.
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
117 | TcTyVar { -- Used only during type inference
118 -- Used for kind variables during
119 -- inference, as well
121 realUnique :: FastInt,
123 tcTyVarDetails :: TcTyVarDetails }
127 realUnique :: FastInt,
130 id_details :: IdDetails, -- Stable, doesn't change
131 id_info :: IdInfo } -- Unstable, updated by simplifier
133 data IdScope -- See Note [GlobalId/LocalId]
138 = NotExported -- ^ Not exported: may be discarded as dead code.
139 | Exported -- ^ Exported: kept alive
142 Note [GlobalId/LocalId]
143 ~~~~~~~~~~~~~~~~~~~~~~~
145 * always a constant (top-level)
146 * imported, or data constructor, or primop, or record selector
147 * has a Unique that is globally unique across the whole
148 GHC invocation (a single invocation may compile multiple modules)
149 * never treated as a candidate by the free-variable finder;
153 * bound within an expression (lambda, case, local let(rec))
154 * or defined at top level in the module being compiled
155 * always treated as a candidate by the free-variable finder
157 After CoreTidy, top-level LocalIds are turned into GlobalIds
160 instance Outputable Var where
161 ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
163 ppr_debug :: Var -> SDoc
164 ppr_debug (TyVar {}) = ptext (sLit "tv")
165 ppr_debug (TcTyVar {tcTyVarDetails = d}) = pprTcTyVarDetails d
166 ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
168 ppr_id_scope :: IdScope -> SDoc
169 ppr_id_scope GlobalId = ptext (sLit "gid")
170 ppr_id_scope (LocalId Exported) = ptext (sLit "lidx")
171 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
173 instance Show Var where
174 showsPrec p var = showsPrecSDoc p (ppr var)
176 instance NamedThing Var where
179 instance Uniquable Var where
180 getUnique = varUnique
182 instance Eq Var where
183 a == b = realUnique a ==# realUnique b
185 instance Ord Var where
186 a <= b = realUnique a <=# realUnique b
187 a < b = realUnique a <# realUnique b
188 a >= b = realUnique a >=# realUnique b
189 a > b = realUnique a ># realUnique b
190 a `compare` b = varUnique a `compare` varUnique b
195 varUnique :: Var -> Unique
196 varUnique var = mkUniqueGrimily (iBox (realUnique var))
198 setVarUnique :: Var -> Unique -> Var
199 setVarUnique var uniq
200 = var { realUnique = getKeyFastInt uniq,
201 varName = setNameUnique (varName var) uniq }
203 setVarName :: Var -> Name -> Var
204 setVarName var new_name
205 = var { realUnique = getKeyFastInt (getUnique new_name),
208 setVarType :: Id -> Type -> Id
209 setVarType id ty = id { varType = ty }
213 %************************************************************************
215 \subsection{Type variables}
217 %************************************************************************
222 tyVarName :: TyVar -> Name
225 tyVarKind :: TyVar -> Kind
228 setTyVarUnique :: TyVar -> Unique -> TyVar
229 setTyVarUnique = setVarUnique
231 setTyVarName :: TyVar -> Name -> TyVar
232 setTyVarName = setVarName
234 setTyVarKind :: TyVar -> Kind -> TyVar
235 setTyVarKind tv k = tv {varType = k}
239 mkTyVar :: Name -> Kind -> TyVar
240 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
241 TyVar { varName = name
242 , realUnique = getKeyFastInt (nameUnique name)
244 , isCoercionVar = False
247 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
248 mkTcTyVar name kind details
249 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
250 TcTyVar { varName = name,
251 realUnique = getKeyFastInt (nameUnique name),
253 tcTyVarDetails = details
257 %************************************************************************
259 \subsection{Coercion variables}
261 %************************************************************************
264 type CoVar = TyVar -- A coercion variable is simply a type
265 -- variable of kind @ty1 ~ ty2@. Hence its
266 -- 'varType' is always @PredTy (EqPred t1 t2)@
268 coVarName :: CoVar -> Name
271 setCoVarUnique :: CoVar -> Unique -> CoVar
272 setCoVarUnique = setVarUnique
274 setCoVarName :: CoVar -> Name -> CoVar
275 setCoVarName = setVarName
277 mkCoVar :: Name -> Kind -> CoVar
278 mkCoVar name kind = ASSERT( isCoercionKind kind )
279 TyVar { varName = name
280 , realUnique = getKeyFastInt (nameUnique name)
282 , isCoercionVar = True
285 mkWildCoVar :: Kind -> TyVar
286 -- ^ Create a type variable that is never referred to, so its unique doesn't
288 mkWildCoVar = mkCoVar (mkSysTvName (mkBuiltinUnique 1) (fsLit "co_wild"))
291 %************************************************************************
295 %************************************************************************
298 -- These synonyms are here and not in Id because otherwise we need a very
299 -- large number of SOURCE imports of Id.hs :-(
303 idInfo :: Id -> IdInfo
304 idInfo (Id { id_info = info }) = info
305 idInfo other = pprPanic "idInfo" (ppr other)
307 idDetails :: Id -> IdDetails
308 idDetails (Id { id_details = details }) = details
309 idDetails other = pprPanic "idDetails" (ppr other)
311 -- The next three have a 'Var' suffix even though they always build
312 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
313 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
314 mkGlobalVar details name ty info
315 = mk_id name ty GlobalId details info
317 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
318 mkLocalVar details name ty info
319 = mk_id name ty (LocalId NotExported) details info
321 -- | Exported 'Var's will not be removed as dead code
322 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
323 mkExportedLocalVar details name ty info
324 = mk_id name ty (LocalId Exported) details info
326 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
327 mk_id name ty scope details info
328 = Id { varName = name,
329 realUnique = getKeyFastInt (nameUnique name),
332 id_details = details,
336 lazySetIdInfo :: Id -> IdInfo -> Var
337 lazySetIdInfo id info = id { id_info = info }
339 setIdDetails :: Id -> IdDetails -> Id
340 setIdDetails id details = id { id_details = details }
342 globaliseId :: Id -> Id
343 -- ^ If it's a local, make it global
344 globaliseId id = id { idScope = GlobalId }
346 setIdExported :: Id -> Id
347 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
348 -- and class operations, which are born as global 'Id's and automatically exported
349 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
350 setIdExported id@(Id { idScope = GlobalId }) = id
351 setIdExported tv = pprPanic "setIdExported" (ppr tv)
353 setIdNotExported :: Id -> Id
354 -- ^ We can only do this to LocalIds
355 setIdNotExported id = ASSERT( isLocalId id )
356 id { idScope = LocalId NotExported }
359 %************************************************************************
361 \subsection{Predicates over variables}
363 %************************************************************************
366 isTyVar :: Var -> Bool
367 isTyVar (TyVar {}) = True
368 isTyVar (TcTyVar {}) = True
371 isTcTyVar :: Var -> Bool
372 isTcTyVar (TcTyVar {}) = True
379 isLocalId :: Var -> Bool
380 isLocalId (Id { idScope = LocalId _ }) = True
383 isCoVar :: Var -> Bool
384 isCoVar (v@(TyVar {})) = isCoercionVar v
385 isCoVar (TcTyVar {varType = kind}) = isCoercionKind kind -- used during solving
388 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
389 -- These are the variables that we need to pay attention to when finding free
390 -- variables, or doing dependency analysis.
391 isLocalVar :: Var -> Bool
392 isLocalVar v = not (isGlobalId v)
394 isGlobalId :: Var -> Bool
395 isGlobalId (Id { idScope = GlobalId }) = True
398 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
399 -- that must have a binding in this module. The converse
400 -- is not quite right: there are some global 'Id's that must have
401 -- bindings, such as record selectors. But that doesn't matter,
402 -- because it's only used for assertions
403 mustHaveLocalBinding :: Var -> Bool
404 mustHaveLocalBinding var = isLocalVar var
406 -- | 'isExportedIdVar' means \"don't throw this away\"
407 isExportedId :: Var -> Bool
408 isExportedId (Id { idScope = GlobalId }) = True
409 isExportedId (Id { idScope = LocalId Exported}) = True
410 isExportedId _ = False