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/WorkingConventions#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,
51 import {-# SOURCE #-} TypeRep( isCoercionKind )
54 import Name hiding (varName)
62 %************************************************************************
64 \subsection{The main data type declarations}
66 %************************************************************************
69 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
70 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
71 strictness). The essential info about different kinds of @Vars@ is
78 realUnique :: FastInt, -- Key for fast comparison
79 -- Identical to the Unique in the name,
80 -- cached here for speed
85 | TcTyVar { -- Used only during type inference
86 -- Used for kind variables during
89 realUnique :: FastInt,
91 tcTyVarDetails :: TcTyVarDetails }
93 | GlobalId { -- Used for imported Ids, dict selectors etc
94 -- See Note [GlobalId/LocalId] below
95 varName :: !Name, -- Always an External or WiredIn Name
96 realUnique :: FastInt,
99 gblDetails :: GlobalIdDetails }
101 | LocalId { -- Used for locally-defined Ids
102 -- See Note [GlobalId/LocalId] below
104 realUnique :: FastInt,
107 lclDetails :: LocalIdDetails }
110 = NotExported -- Not exported
111 | Exported -- Exported
112 -- Exported Ids are kept alive;
113 -- NotExported things may be discarded as dead code.
116 Note [GlobalId/LocalId]
117 ~~~~~~~~~~~~~~~~~~~~~~~
119 * always a constant (top-level)
120 * imported, or data constructor, or primop, or record selector
121 * has a Unique that is globally unique across the whole
122 GHC invocation (a single invocation may compile multiple modules)
123 * never treated as a candidate by the free-variable finder;
127 * bound within an expression (lambda, case, local let(rec))
128 * or defined at top level in the module being compiled
129 * always treated as a candidate by the free-variable finder
131 After CoreTidy, top-level LocalIds are turned into GlobalIds
135 instance Outputable Var where
136 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
139 GlobalId {} -> ptext SLIT("gid")
140 LocalId {} -> ptext SLIT("lid")
141 TyVar {} -> ptext SLIT("tv")
142 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
144 instance Show Var where
145 showsPrec p var = showsPrecSDoc p (ppr var)
147 instance NamedThing Var where
150 instance Uniquable Var where
151 getUnique = varUnique
153 instance Eq Var where
154 a == b = realUnique a ==# realUnique b
156 instance Ord Var where
157 a <= b = realUnique a <=# realUnique b
158 a < b = realUnique a <# realUnique b
159 a >= b = realUnique a >=# realUnique b
160 a > b = realUnique a ># realUnique b
161 a `compare` b = varUnique a `compare` varUnique b
166 varUnique :: Var -> Unique
167 varUnique var = mkUniqueGrimily (iBox (realUnique var))
169 setVarUnique :: Var -> Unique -> Var
170 setVarUnique var uniq
171 = var { realUnique = getKey# uniq,
172 varName = setNameUnique (varName var) uniq }
174 setVarName :: Var -> Name -> Var
175 setVarName var new_name
176 = var { realUnique = getKey# (getUnique new_name),
181 %************************************************************************
183 \subsection{Type variables}
185 %************************************************************************
193 setTyVarUnique = setVarUnique
194 setTyVarName = setVarName
196 setTyVarKind :: TyVar -> Kind -> TyVar
197 setTyVarKind tv k = tv {varType = k}
201 mkTyVar :: Name -> Kind -> TyVar
202 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
203 TyVar { varName = name
204 , realUnique = getKey# (nameUnique name)
206 , isCoercionVar = False
209 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
210 mkTcTyVar name kind details
211 = -- TOM: no longer valid assertion?
212 -- ASSERT( not (isCoercionKind kind) )
213 TcTyVar { varName = name,
214 realUnique = getKey# (nameUnique name),
216 tcTyVarDetails = details
220 %************************************************************************
222 \subsection{Coercion variables}
224 %************************************************************************
227 type CoVar = Var -- A coercion variable is simply a type
228 -- variable of kind (ty1 :=: ty2)
231 setCoVarUnique = setVarUnique
232 setCoVarName = setVarName
234 mkCoVar :: Name -> Kind -> CoVar
235 mkCoVar name kind = ASSERT( isCoercionKind kind )
236 TyVar { varName = name
237 , realUnique = getKey# (nameUnique name)
239 , isCoercionVar = True
242 mkWildCoVar :: Kind -> TyVar
243 -- A type variable that is never referred to,
244 -- so its unique doesn't matter
246 = ASSERT( isCoercionKind kind )
247 TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
248 realUnique = _ILIT(1),
250 isCoercionVar = True }
252 wild_uniq = mkBuiltinUnique 1
255 %************************************************************************
257 \subsection{Id Construction}
259 %************************************************************************
261 Most Id-related functions are in Id.lhs and MkId.lhs
273 setIdUnique :: Id -> Unique -> Id
274 setIdUnique = setVarUnique
276 setIdName :: Id -> Name -> Id
277 setIdName = setVarName
279 setIdType :: Id -> Type -> Id
280 setIdType id ty = id {varType = ty}
282 setIdExported :: Id -> Id
283 -- Can be called on GlobalIds, such as data cons and class ops,
284 -- which are "born" as GlobalIds and automatically exported
285 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
286 setIdExported other_id = ASSERT( isId other_id ) other_id
288 setIdNotExported :: Id -> Id
289 -- We can only do this to LocalIds
290 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
292 globaliseId :: GlobalIdDetails -> Id -> Id
293 -- If it's a local, make it global
294 globaliseId details id = GlobalId { varName = varName id,
295 realUnique = realUnique id,
296 varType = varType id,
298 gblDetails = details }
300 idInfo :: Id -> IdInfo
301 idInfo (GlobalId {idInfo_ = info}) = info
302 idInfo (LocalId {idInfo_ = info}) = info
303 idInfo other_var = pprPanic "idInfo" (ppr other_var)
305 lazySetIdInfo :: Id -> IdInfo -> Id
306 lazySetIdInfo id info = id {idInfo_ = info}
308 setIdInfo :: Id -> IdInfo -> Id
309 setIdInfo id info = seqIdInfo info `seq` id {idInfo_ = info}
310 -- Try to avoid spack leaks by seq'ing
312 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
314 = seqIdInfo new_info `seq` id {idInfo_ = new_info}
316 new_info = fn (idInfo id)
318 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
319 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
320 maybeModifyIdInfo (Just new_info) id = id {idInfo_ = new_info}
321 maybeModifyIdInfo Nothing id = id
324 %************************************************************************
326 \subsection{Predicates over variables
328 %************************************************************************
331 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
332 mkGlobalId details name ty info
333 = GlobalId { varName = name,
334 realUnique = getKey# (nameUnique name), -- Cache the unique
336 gblDetails = details,
339 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
340 mk_local_id name ty details info
341 = LocalId { varName = name,
342 realUnique = getKey# (nameUnique name), -- Cache the unique
344 lclDetails = details,
347 mkLocalId :: Name -> Type -> IdInfo -> Id
348 mkLocalId name ty info = mk_local_id name ty NotExported info
350 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
351 mkExportedLocalId name ty info = mk_local_id name ty Exported info
355 isTyVar, isTcTyVar :: Var -> Bool
356 isId, isLocalVar, isLocalId :: Var -> Bool
357 isGlobalId, isExportedId :: Var -> Bool
358 mustHaveLocalBinding :: Var -> Bool
360 isTyVar (TyVar {}) = True
361 isTyVar (TcTyVar {}) = True
362 isTyVar other = False
364 isTcTyVar (TcTyVar {}) = True
365 isTcTyVar other = False
367 isId (LocalId {}) = True
368 isId (GlobalId {}) = True
371 isLocalId (LocalId {}) = True
372 isLocalId other = False
374 isCoVar (v@(TyVar {})) = isCoercionVar v
375 isCoVar other = False
377 -- isLocalVar returns True for type variables as well as local Ids
378 -- These are the variables that we need to pay attention to when finding free
379 -- variables, or doing dependency analysis.
380 isLocalVar (GlobalId {}) = False
381 isLocalVar other = True
383 -- mustHaveLocalBinding returns True of Ids and TyVars
384 -- that must have a binding in this module. The converse
385 -- is not quite right: there are some GlobalIds that must have
386 -- bindings, such as record selectors. But that doesn't matter,
387 -- because it's only used for assertions
388 mustHaveLocalBinding var = isLocalVar var
390 isGlobalId (GlobalId {}) = True
391 isGlobalId other = False
393 -- isExportedId means "don't throw this away"
394 isExportedId (GlobalId {}) = True
395 isExportedId (LocalId {lclDetails = details})
399 isExportedId other = False
403 globalIdDetails :: Var -> GlobalIdDetails
404 -- Works OK on local Ids too, returning notGlobalId
405 globalIdDetails (GlobalId {gblDetails = details}) = details
406 globalIdDetails other = notGlobalId