2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section{@Vars@: Variables}
11 setVarName, setVarUnique,
14 TyVar, mkTyVar, mkTcTyVar, mkWildCoVar,
16 setTyVarName, setTyVarUnique, setTyVarKind,
20 CoVar, coVarName, setCoVarUnique, setCoVarName, mkCoVar, isCoVar,
24 idName, idType, idUnique, idInfo, modifyIdInfo, maybeModifyIdInfo,
25 setIdName, setIdUnique, setIdType, setIdInfo, lazySetIdInfo,
26 setIdExported, setIdNotExported,
28 globalIdDetails, globaliseId,
30 mkLocalId, mkExportedLocalId, mkGlobalId,
32 isTyVar, isTcTyVar, isId, isLocalVar, isLocalId,
33 isGlobalId, isExportedId,
37 #include "HsVersions.h"
39 import {-# SOURCE #-} TypeRep( Type, Kind )
40 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
41 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId,
44 import {-# SOURCE #-} TypeRep( isCoercionKind )
47 import Name hiding (varName)
55 %************************************************************************
57 \subsection{The main data type declarations}
59 %************************************************************************
62 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
63 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
64 strictness). The essential info about different kinds of @Vars@ is
71 realUnique :: FastInt, -- Key for fast comparison
72 -- Identical to the Unique in the name,
73 -- cached here for speed
78 | TcTyVar { -- Used only during type inference
79 -- Used for kind variables during
82 realUnique :: FastInt,
84 tcTyVarDetails :: TcTyVarDetails }
86 | GlobalId { -- Used for imported Ids, dict selectors etc
87 -- See Note [GlobalId/LocalId] below
88 varName :: !Name, -- Always an External or WiredIn Name
89 realUnique :: FastInt,
92 gblDetails :: GlobalIdDetails }
94 | LocalId { -- Used for locally-defined Ids
95 -- See Note [GlobalId/LocalId] below
97 realUnique :: FastInt,
100 lclDetails :: LocalIdDetails }
103 = NotExported -- Not exported
104 | Exported -- Exported
105 -- Exported Ids are kept alive;
106 -- NotExported things may be discarded as dead code.
109 Note [GlobalId/LocalId]
110 ~~~~~~~~~~~~~~~~~~~~~~~
112 * always a constant (top-level)
113 * imported, or data constructor, or primop, or record selector
114 * has a Unique that is globally unique across the whole
115 GHC invocation (a single invocation may compile multiple modules)
116 * never treated as a candidate by the free-variable finder;
120 * bound within an expression (lambda, case, local let(rec))
121 * or defined at top level in the module being compiled
122 * always treated as a candidate by the free-variable finder
124 After CoreTidy, top-level LocalIds are turned into GlobalIds
128 instance Outputable Var where
129 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
132 GlobalId {} -> ptext SLIT("gid")
133 LocalId {} -> ptext SLIT("lid")
134 TyVar {} -> ptext SLIT("tv")
135 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
137 instance Show Var where
138 showsPrec p var = showsPrecSDoc p (ppr var)
140 instance NamedThing Var where
143 instance Uniquable Var where
144 getUnique = varUnique
146 instance Eq Var where
147 a == b = realUnique a ==# realUnique b
149 instance Ord Var where
150 a <= b = realUnique a <=# realUnique b
151 a < b = realUnique a <# realUnique b
152 a >= b = realUnique a >=# realUnique b
153 a > b = realUnique a ># realUnique b
154 a `compare` b = varUnique a `compare` varUnique b
159 varUnique :: Var -> Unique
160 varUnique var = mkUniqueGrimily (iBox (realUnique var))
162 setVarUnique :: Var -> Unique -> Var
163 setVarUnique var uniq
164 = var { realUnique = getKey# uniq,
165 varName = setNameUnique (varName var) uniq }
167 setVarName :: Var -> Name -> Var
168 setVarName var new_name
169 = var { realUnique = getKey# (getUnique new_name),
174 %************************************************************************
176 \subsection{Type variables}
178 %************************************************************************
185 setTyVarUnique = setVarUnique
186 setTyVarName = setVarName
188 setTyVarKind :: TyVar -> Kind -> TyVar
189 setTyVarKind tv k = tv {tyVarKind = k}
193 mkTyVar :: Name -> Kind -> TyVar
194 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
195 TyVar { varName = name
196 , realUnique = getKey# (nameUnique name)
198 , isCoercionVar = False
201 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
202 mkTcTyVar name kind details
203 = ASSERT( not (isCoercionKind kind) )
204 TcTyVar { varName = name,
205 realUnique = getKey# (nameUnique name),
207 tcTyVarDetails = details
211 %************************************************************************
213 \subsection{Coercion variables}
215 %************************************************************************
218 type CoVar = Var -- A coercion variable is simply a type
219 -- variable of kind (ty1 :=: ty2)
222 setCoVarUnique = setVarUnique
223 setCoVarName = setVarName
225 mkCoVar :: Name -> Kind -> CoVar
226 mkCoVar name kind = ASSERT( isCoercionKind kind )
227 TyVar { varName = name
228 , realUnique = getKey# (nameUnique name)
230 , isCoercionVar = True
233 mkWildCoVar :: Kind -> TyVar
234 -- A type variable that is never referred to,
235 -- so its unique doesn't matter
237 = ASSERT( isCoercionKind kind )
238 TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
239 realUnique = _ILIT(1),
241 isCoercionVar = True }
243 wild_uniq = mkBuiltinUnique 1
246 %************************************************************************
248 \subsection{Id Construction}
250 %************************************************************************
252 Most Id-related functions are in Id.lhs and MkId.lhs
263 setIdUnique :: Id -> Unique -> Id
264 setIdUnique = setVarUnique
266 setIdName :: Id -> Name -> Id
267 setIdName = setVarName
269 setIdType :: Id -> Type -> Id
270 setIdType id ty = id {idType = ty}
272 setIdExported :: Id -> Id
273 -- Can be called on GlobalIds, such as data cons and class ops,
274 -- which are "born" as GlobalIds and automatically exported
275 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
276 setIdExported other_id = ASSERT( isId other_id ) other_id
278 setIdNotExported :: Id -> Id
279 -- We can only do this to LocalIds
280 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
282 globaliseId :: GlobalIdDetails -> Id -> Id
283 -- If it's a local, make it global
284 globaliseId details id = GlobalId { varName = varName id,
285 realUnique = realUnique id,
288 gblDetails = details }
290 lazySetIdInfo :: Id -> IdInfo -> Id
291 lazySetIdInfo id info = id {idInfo = info}
293 setIdInfo :: Id -> IdInfo -> Id
294 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
295 -- Try to avoid spack leaks by seq'ing
297 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
299 = seqIdInfo new_info `seq` id {idInfo = new_info}
301 new_info = fn (idInfo id)
303 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
304 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
305 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
306 maybeModifyIdInfo Nothing id = id
309 %************************************************************************
311 \subsection{Predicates over variables
313 %************************************************************************
316 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
317 mkGlobalId details name ty info
318 = GlobalId { varName = name,
319 realUnique = getKey# (nameUnique name), -- Cache the unique
321 gblDetails = details,
324 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
325 mk_local_id name ty details info
326 = LocalId { varName = name,
327 realUnique = getKey# (nameUnique name), -- Cache the unique
329 lclDetails = details,
332 mkLocalId :: Name -> Type -> IdInfo -> Id
333 mkLocalId name ty info = mk_local_id name ty NotExported info
335 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
336 mkExportedLocalId name ty info = mk_local_id name ty Exported info
340 isTyVar, isTcTyVar :: Var -> Bool
341 isId, isLocalVar, isLocalId :: Var -> Bool
342 isGlobalId, isExportedId :: Var -> Bool
343 mustHaveLocalBinding :: Var -> Bool
345 isTyVar (TyVar {}) = True
346 isTyVar (TcTyVar {}) = True
347 isTyVar other = False
349 isTcTyVar (TcTyVar {}) = True
350 isTcTyVar other = False
352 isId (LocalId {}) = True
353 isId (GlobalId {}) = True
356 isLocalId (LocalId {}) = True
357 isLocalId other = False
359 isCoVar (v@(TyVar {})) = isCoercionVar v
360 isCoVar other = False
362 -- isLocalVar returns True for type variables as well as local Ids
363 -- These are the variables that we need to pay attention to when finding free
364 -- variables, or doing dependency analysis.
365 isLocalVar (GlobalId {}) = False
366 isLocalVar other = True
368 -- mustHaveLocalBinding returns True of Ids and TyVars
369 -- that must have a binding in this module. The converse
370 -- is not quite right: there are some GlobalIds that must have
371 -- bindings, such as record selectors. But that doesn't matter,
372 -- because it's only used for assertions
373 mustHaveLocalBinding var = isLocalVar var
375 isGlobalId (GlobalId {}) = True
376 isGlobalId other = False
378 -- isExportedId means "don't throw this away"
379 isExportedId (GlobalId {}) = True
380 isExportedId (LocalId {lclDetails = details})
384 isExportedId other = False
388 globalIdDetails :: Var -> GlobalIdDetails
389 -- Works OK on local Ids too, returning notGlobalId
390 globalIdDetails (GlobalId {gblDetails = details}) = details
391 globalIdDetails other = notGlobalId