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 idDetails :: IdDetails, -- Stable, doesn't change
131 idInfo :: 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
143 Note [GlobalId/LocalId]
144 ~~~~~~~~~~~~~~~~~~~~~~~
146 * always a constant (top-level)
147 * imported, or data constructor, or primop, or record selector
148 * has a Unique that is globally unique across the whole
149 GHC invocation (a single invocation may compile multiple modules)
150 * never treated as a candidate by the free-variable finder;
154 * bound within an expression (lambda, case, local let(rec))
155 * or defined at top level in the module being compiled
156 * always treated as a candidate by the free-variable finder
158 After CoreTidy, top-level LocalIds are turned into GlobalIds
161 instance Outputable Var where
162 ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
164 ppr_debug :: Var -> SDoc
165 ppr_debug (TyVar {}) = ptext (sLit "tv")
166 ppr_debug (TcTyVar {tcTyVarDetails = d}) = pprTcTyVarDetails d
167 ppr_debug (Id { idScope = s, idDetails = d }) = ppr_id_scope s <> pprIdDetails d
169 ppr_id_scope :: IdScope -> SDoc
170 ppr_id_scope GlobalId = ptext (sLit "gid")
171 ppr_id_scope (LocalId Exported) = ptext (sLit "lidx")
172 ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
174 instance Show Var where
175 showsPrec p var = showsPrecSDoc p (ppr var)
177 instance NamedThing Var where
180 instance Uniquable Var where
181 getUnique = varUnique
183 instance Eq Var where
184 a == b = realUnique a ==# realUnique b
186 instance Ord Var where
187 a <= b = realUnique a <=# realUnique b
188 a < b = realUnique a <# realUnique b
189 a >= b = realUnique a >=# realUnique b
190 a > b = realUnique a ># realUnique b
191 a `compare` b = varUnique a `compare` varUnique b
196 varUnique :: Var -> Unique
197 varUnique var = mkUniqueGrimily (iBox (realUnique var))
199 setVarUnique :: Var -> Unique -> Var
200 setVarUnique var uniq
201 = var { realUnique = getKeyFastInt uniq,
202 varName = setNameUnique (varName var) uniq }
204 setVarName :: Var -> Name -> Var
205 setVarName var new_name
206 = var { realUnique = getKeyFastInt (getUnique new_name),
209 setVarType :: Id -> Type -> Id
210 setVarType id ty = id { varType = ty }
214 %************************************************************************
216 \subsection{Type variables}
218 %************************************************************************
223 tyVarName :: TyVar -> Name
226 tyVarKind :: TyVar -> Kind
229 setTyVarUnique :: TyVar -> Unique -> TyVar
230 setTyVarUnique = setVarUnique
232 setTyVarName :: TyVar -> Name -> TyVar
233 setTyVarName = setVarName
235 setTyVarKind :: TyVar -> Kind -> TyVar
236 setTyVarKind tv k = tv {varType = k}
240 mkTyVar :: Name -> Kind -> TyVar
241 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
242 TyVar { varName = name
243 , realUnique = getKeyFastInt (nameUnique name)
245 , isCoercionVar = False
248 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
249 mkTcTyVar name kind details
250 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
251 TcTyVar { varName = name,
252 realUnique = getKeyFastInt (nameUnique name),
254 tcTyVarDetails = details
258 %************************************************************************
260 \subsection{Coercion variables}
262 %************************************************************************
265 type CoVar = TyVar -- A coercion variable is simply a type
266 -- variable of kind @ty1 ~ ty2@. Hence its
267 -- 'varType' is always @PredTy (EqPred t1 t2)@
269 coVarName :: CoVar -> Name
272 setCoVarUnique :: CoVar -> Unique -> CoVar
273 setCoVarUnique = setVarUnique
275 setCoVarName :: CoVar -> Name -> CoVar
276 setCoVarName = setVarName
278 mkCoVar :: Name -> Kind -> CoVar
279 mkCoVar name kind = ASSERT( isCoercionKind kind )
280 TyVar { varName = name
281 , realUnique = getKeyFastInt (nameUnique name)
283 , isCoercionVar = True
286 mkWildCoVar :: Kind -> TyVar
287 -- ^ Create a type variable that is never referred to, so its unique doesn't
289 mkWildCoVar = mkCoVar (mkSysTvName (mkBuiltinUnique 1) (fsLit "co_wild"))
292 %************************************************************************
296 %************************************************************************
299 -- These synonyms are here and not in Id because otherwise we need a very
300 -- large number of SOURCE imports of Id.hs :-(
304 -- The next three have a 'Var' suffix even though they always build
305 -- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
306 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
307 mkGlobalVar details name ty info
308 = mk_id name ty GlobalId details info
310 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
311 mkLocalVar details name ty info
312 = mk_id name ty (LocalId NotExported) details info
314 -- | Exported 'Var's will not be removed as dead code
315 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
316 mkExportedLocalVar details name ty info
317 = mk_id name ty (LocalId Exported) details info
319 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
320 mk_id name ty scope details info
321 = Id { varName = name,
322 realUnique = getKeyFastInt (nameUnique name),
329 lazySetIdInfo :: Id -> IdInfo -> Var
330 lazySetIdInfo id info = id { idInfo = info }
332 setIdDetails :: Id -> IdDetails -> Id
333 setIdDetails id details = id { idDetails = details }
335 globaliseId :: Id -> Id
336 -- ^ If it's a local, make it global
337 globaliseId id = id { idScope = GlobalId }
339 setIdExported :: Id -> Id
340 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
341 -- and class operations, which are born as global 'Id's and automatically exported
342 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
343 setIdExported id@(Id { idScope = GlobalId }) = id
344 setIdExported tv = pprPanic "setIdExported" (ppr tv)
346 setIdNotExported :: Id -> Id
347 -- ^ We can only do this to LocalIds
348 setIdNotExported id = ASSERT( isLocalId id )
349 id { idScope = LocalId NotExported }
352 %************************************************************************
354 \subsection{Predicates over variables}
356 %************************************************************************
359 isTyVar :: Var -> Bool
360 isTyVar (TyVar {}) = True
361 isTyVar (TcTyVar {}) = True
364 isTcTyVar :: Var -> Bool
365 isTcTyVar (TcTyVar {}) = True
372 isLocalId :: Var -> Bool
373 isLocalId (Id { idScope = LocalId _ }) = True
376 isCoVar :: Var -> Bool
377 isCoVar (v@(TyVar {})) = isCoercionVar v
378 isCoVar (TcTyVar {varType = kind}) = isCoercionKind kind -- used during solving
381 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
382 -- These are the variables that we need to pay attention to when finding free
383 -- variables, or doing dependency analysis.
384 isLocalVar :: Var -> Bool
385 isLocalVar v = not (isGlobalId v)
387 isGlobalId :: Var -> Bool
388 isGlobalId (Id { idScope = GlobalId }) = True
391 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
392 -- that must have a binding in this module. The converse
393 -- is not quite right: there are some global 'Id's that must have
394 -- bindings, such as record selectors. But that doesn't matter,
395 -- because it's only used for assertions
396 mustHaveLocalBinding :: Var -> Bool
397 mustHaveLocalBinding var = isLocalVar var
399 -- | 'isExportedIdVar' means \"don't throw this away\"
400 isExportedId :: Var -> Bool
401 isExportedId (Id { idScope = GlobalId }) = True
402 isExportedId (Id { idScope = LocalId Exported}) = True
403 isExportedId _ = False