2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section{@Vars@: Variables}
9 -- The above warning supression flag is a temporary kludge.
10 -- While working on this module you are encouraged to remove it and fix
11 -- any warnings in the module. See
12 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17 varName, varUnique, varType,
18 setVarName, setVarUnique,
21 TyVar, mkTyVar, mkTcTyVar, mkWildCoVar,
23 setTyVarName, setTyVarUnique, setTyVarKind,
27 CoVar, coVarName, setCoVarUnique, setCoVarName, mkCoVar, isCoVar,
31 idName, idType, idUnique, idInfo, modifyIdInfo, maybeModifyIdInfo,
32 setIdName, setIdUnique, setIdType, setIdInfo, lazySetIdInfo,
33 setIdExported, setIdNotExported,
35 globalIdDetails, globaliseId,
37 mkLocalId, mkExportedLocalId, mkGlobalId,
39 isTyVar, isTcTyVar, isId, isLocalVar, isLocalId,
40 isGlobalId, isExportedId,
44 #include "HsVersions.h"
46 import {-# SOURCE #-} TypeRep( Type, Kind )
47 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
48 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId,
50 import {-# SOURCE #-} TypeRep( isCoercionKind )
52 import Name hiding (varName)
60 %************************************************************************
62 \subsection{The main data type declarations}
64 %************************************************************************
67 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
68 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
69 strictness). The essential info about different kinds of @Vars@ is
76 realUnique :: FastInt, -- Key for fast comparison
77 -- Identical to the Unique in the name,
78 -- cached here for speed
83 | TcTyVar { -- Used only during type inference
84 -- Used for kind variables during
87 realUnique :: FastInt,
89 tcTyVarDetails :: TcTyVarDetails }
91 | GlobalId { -- Used for imported Ids, dict selectors etc
92 -- See Note [GlobalId/LocalId] below
93 varName :: !Name, -- Always an External or WiredIn Name
94 realUnique :: FastInt,
97 gblDetails :: GlobalIdDetails }
99 | LocalId { -- Used for locally-defined Ids
100 -- See Note [GlobalId/LocalId] below
102 realUnique :: FastInt,
105 lclDetails :: LocalIdDetails }
108 = NotExported -- Not exported
109 | Exported -- Exported
110 -- Exported Ids are kept alive;
111 -- NotExported things may be discarded as dead code.
114 Note [GlobalId/LocalId]
115 ~~~~~~~~~~~~~~~~~~~~~~~
117 * always a constant (top-level)
118 * imported, or data constructor, or primop, or record selector
119 * has a Unique that is globally unique across the whole
120 GHC invocation (a single invocation may compile multiple modules)
121 * never treated as a candidate by the free-variable finder;
125 * bound within an expression (lambda, case, local let(rec))
126 * or defined at top level in the module being compiled
127 * always treated as a candidate by the free-variable finder
129 After CoreTidy, top-level LocalIds are turned into GlobalIds
133 instance Outputable Var where
134 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
137 GlobalId {} -> ptext SLIT("gid")
138 LocalId {} -> ptext SLIT("lid")
139 TyVar {} -> ptext SLIT("tv")
140 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
142 instance Show Var where
143 showsPrec p var = showsPrecSDoc p (ppr var)
145 instance NamedThing Var where
148 instance Uniquable Var where
149 getUnique = varUnique
151 instance Eq Var where
152 a == b = realUnique a ==# realUnique b
154 instance Ord Var where
155 a <= b = realUnique a <=# realUnique b
156 a < b = realUnique a <# realUnique b
157 a >= b = realUnique a >=# realUnique b
158 a > b = realUnique a ># realUnique b
159 a `compare` b = varUnique a `compare` varUnique b
164 varUnique :: Var -> Unique
165 varUnique var = mkUniqueGrimily (iBox (realUnique var))
167 setVarUnique :: Var -> Unique -> Var
168 setVarUnique var uniq
169 = var { realUnique = getKey# uniq,
170 varName = setNameUnique (varName var) uniq }
172 setVarName :: Var -> Name -> Var
173 setVarName var new_name
174 = var { realUnique = getKey# (getUnique new_name),
179 %************************************************************************
181 \subsection{Type variables}
183 %************************************************************************
191 setTyVarUnique = setVarUnique
192 setTyVarName = setVarName
194 setTyVarKind :: TyVar -> Kind -> TyVar
195 setTyVarKind tv k = tv {varType = k}
199 mkTyVar :: Name -> Kind -> TyVar
200 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
201 TyVar { varName = name
202 , realUnique = getKey# (nameUnique name)
204 , isCoercionVar = False
207 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
208 mkTcTyVar name kind details
209 = -- TOM: no longer valid assertion?
210 -- ASSERT( not (isCoercionKind kind) )
211 TcTyVar { varName = name,
212 realUnique = getKey# (nameUnique name),
214 tcTyVarDetails = details
218 %************************************************************************
220 \subsection{Coercion variables}
222 %************************************************************************
225 type CoVar = Var -- A coercion variable is simply a type
226 -- variable of kind (ty1 :=: ty2)
229 setCoVarUnique = setVarUnique
230 setCoVarName = setVarName
232 mkCoVar :: Name -> Kind -> CoVar
233 mkCoVar name kind = ASSERT( isCoercionKind kind )
234 TyVar { varName = name
235 , realUnique = getKey# (nameUnique name)
237 -- varType is always PredTy (EqPred t1 t2)
238 , isCoercionVar = True
241 mkWildCoVar :: Kind -> TyVar
242 -- A type variable that is never referred to,
243 -- so its unique doesn't matter
245 = ASSERT( isCoercionKind kind )
246 TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
247 realUnique = _ILIT(1),
249 isCoercionVar = True }
251 wild_uniq = mkBuiltinUnique 1
254 %************************************************************************
256 \subsection{Id Construction}
258 %************************************************************************
260 Most Id-related functions are in Id.lhs and MkId.lhs
272 setIdUnique :: Id -> Unique -> Id
273 setIdUnique = setVarUnique
275 setIdName :: Id -> Name -> Id
276 setIdName = setVarName
278 setIdType :: Id -> Type -> Id
279 setIdType id ty = id {varType = ty}
281 setIdExported :: Id -> Id
282 -- Can be called on GlobalIds, such as data cons and class ops,
283 -- which are "born" as GlobalIds and automatically exported
284 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
285 setIdExported other_id = ASSERT( isId other_id ) other_id
287 setIdNotExported :: Id -> Id
288 -- We can only do this to LocalIds
289 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
291 globaliseId :: GlobalIdDetails -> Id -> Id
292 -- If it's a local, make it global
293 globaliseId details id = GlobalId { varName = varName id,
294 realUnique = realUnique id,
295 varType = varType id,
297 gblDetails = details }
299 idInfo :: Id -> IdInfo
300 idInfo (GlobalId {idInfo_ = info}) = info
301 idInfo (LocalId {idInfo_ = info}) = info
302 idInfo other_var = pprPanic "idInfo" (ppr other_var)
304 lazySetIdInfo :: Id -> IdInfo -> Id
305 lazySetIdInfo id info = id {idInfo_ = info}
307 setIdInfo :: Id -> IdInfo -> Id
308 setIdInfo id info = seqIdInfo info `seq` id {idInfo_ = info}
309 -- Try to avoid spack leaks by seq'ing
311 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
313 = seqIdInfo new_info `seq` id {idInfo_ = new_info}
315 new_info = fn (idInfo id)
317 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
318 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
319 maybeModifyIdInfo (Just new_info) id = id {idInfo_ = new_info}
320 maybeModifyIdInfo Nothing id = id
323 %************************************************************************
325 \subsection{Predicates over variables
327 %************************************************************************
330 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
331 mkGlobalId details name ty info
332 = GlobalId { varName = name,
333 realUnique = getKey# (nameUnique name), -- Cache the unique
335 gblDetails = details,
338 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
339 mk_local_id name ty details info
340 = LocalId { varName = name,
341 realUnique = getKey# (nameUnique name), -- Cache the unique
343 lclDetails = details,
346 mkLocalId :: Name -> Type -> IdInfo -> Id
347 mkLocalId name ty info = mk_local_id name ty NotExported info
349 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
350 mkExportedLocalId name ty info = mk_local_id name ty Exported info
354 isTyVar, isTcTyVar :: Var -> Bool
355 isId, isLocalVar, isLocalId :: Var -> Bool
356 isGlobalId, isExportedId :: Var -> Bool
357 mustHaveLocalBinding :: Var -> Bool
359 isTyVar (TyVar {}) = True
360 isTyVar (TcTyVar {}) = True
361 isTyVar other = False
363 isTcTyVar (TcTyVar {}) = True
364 isTcTyVar other = False
366 isId (LocalId {}) = True
367 isId (GlobalId {}) = True
370 isLocalId (LocalId {}) = True
371 isLocalId other = False
373 isCoVar (v@(TyVar {})) = isCoercionVar v
374 isCoVar other = False
376 -- isLocalVar returns True for type variables as well as local Ids
377 -- These are the variables that we need to pay attention to when finding free
378 -- variables, or doing dependency analysis.
379 isLocalVar (GlobalId {}) = False
380 isLocalVar other = True
382 -- mustHaveLocalBinding returns True of Ids and TyVars
383 -- that must have a binding in this module. The converse
384 -- is not quite right: there are some GlobalIds that must have
385 -- bindings, such as record selectors. But that doesn't matter,
386 -- because it's only used for assertions
387 mustHaveLocalBinding var = isLocalVar var
389 isGlobalId (GlobalId {}) = True
390 isGlobalId other = False
392 -- isExportedId means "don't throw this away"
393 isExportedId (GlobalId {}) = True
394 isExportedId (LocalId {lclDetails = details})
398 isExportedId other = False
402 globalIdDetails :: Var -> GlobalIdDetails
403 -- Works OK on local Ids too, returning notGlobalId
404 globalIdDetails (GlobalId {gblDetails = details}) = details
405 globalIdDetails other = notGlobalId