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 , isCoercionVar = True
240 mkWildCoVar :: Kind -> TyVar
241 -- A type variable that is never referred to,
242 -- so its unique doesn't matter
244 = ASSERT( isCoercionKind kind )
245 TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
246 realUnique = _ILIT(1),
248 isCoercionVar = True }
250 wild_uniq = mkBuiltinUnique 1
253 %************************************************************************
255 \subsection{Id Construction}
257 %************************************************************************
259 Most Id-related functions are in Id.lhs and MkId.lhs
271 setIdUnique :: Id -> Unique -> Id
272 setIdUnique = setVarUnique
274 setIdName :: Id -> Name -> Id
275 setIdName = setVarName
277 setIdType :: Id -> Type -> Id
278 setIdType id ty = id {varType = ty}
280 setIdExported :: Id -> Id
281 -- Can be called on GlobalIds, such as data cons and class ops,
282 -- which are "born" as GlobalIds and automatically exported
283 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
284 setIdExported other_id = ASSERT( isId other_id ) other_id
286 setIdNotExported :: Id -> Id
287 -- We can only do this to LocalIds
288 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
290 globaliseId :: GlobalIdDetails -> Id -> Id
291 -- If it's a local, make it global
292 globaliseId details id = GlobalId { varName = varName id,
293 realUnique = realUnique id,
294 varType = varType id,
296 gblDetails = details }
298 idInfo :: Id -> IdInfo
299 idInfo (GlobalId {idInfo_ = info}) = info
300 idInfo (LocalId {idInfo_ = info}) = info
301 idInfo other_var = pprPanic "idInfo" (ppr other_var)
303 lazySetIdInfo :: Id -> IdInfo -> Id
304 lazySetIdInfo id info = id {idInfo_ = info}
306 setIdInfo :: Id -> IdInfo -> Id
307 setIdInfo id info = seqIdInfo info `seq` id {idInfo_ = info}
308 -- Try to avoid spack leaks by seq'ing
310 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
312 = seqIdInfo new_info `seq` id {idInfo_ = new_info}
314 new_info = fn (idInfo id)
316 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
317 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
318 maybeModifyIdInfo (Just new_info) id = id {idInfo_ = new_info}
319 maybeModifyIdInfo Nothing id = id
322 %************************************************************************
324 \subsection{Predicates over variables
326 %************************************************************************
329 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
330 mkGlobalId details name ty info
331 = GlobalId { varName = name,
332 realUnique = getKey# (nameUnique name), -- Cache the unique
334 gblDetails = details,
337 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
338 mk_local_id name ty details info
339 = LocalId { varName = name,
340 realUnique = getKey# (nameUnique name), -- Cache the unique
342 lclDetails = details,
345 mkLocalId :: Name -> Type -> IdInfo -> Id
346 mkLocalId name ty info = mk_local_id name ty NotExported info
348 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
349 mkExportedLocalId name ty info = mk_local_id name ty Exported info
353 isTyVar, isTcTyVar :: Var -> Bool
354 isId, isLocalVar, isLocalId :: Var -> Bool
355 isGlobalId, isExportedId :: Var -> Bool
356 mustHaveLocalBinding :: Var -> Bool
358 isTyVar (TyVar {}) = True
359 isTyVar (TcTyVar {}) = True
360 isTyVar other = False
362 isTcTyVar (TcTyVar {}) = True
363 isTcTyVar other = False
365 isId (LocalId {}) = True
366 isId (GlobalId {}) = True
369 isLocalId (LocalId {}) = True
370 isLocalId other = False
372 isCoVar (v@(TyVar {})) = isCoercionVar v
373 isCoVar other = False
375 -- isLocalVar returns True for type variables as well as local Ids
376 -- These are the variables that we need to pay attention to when finding free
377 -- variables, or doing dependency analysis.
378 isLocalVar (GlobalId {}) = False
379 isLocalVar other = True
381 -- mustHaveLocalBinding returns True of Ids and TyVars
382 -- that must have a binding in this module. The converse
383 -- is not quite right: there are some GlobalIds that must have
384 -- bindings, such as record selectors. But that doesn't matter,
385 -- because it's only used for assertions
386 mustHaveLocalBinding var = isLocalVar var
388 isGlobalId (GlobalId {}) = True
389 isGlobalId other = False
391 -- isExportedId means "don't throw this away"
392 isExportedId (GlobalId {}) = True
393 isExportedId (LocalId {lclDetails = details})
397 isExportedId other = False
401 globalIdDetails :: Var -> GlobalIdDetails
402 -- Works OK on local Ids too, returning notGlobalId
403 globalIdDetails (GlobalId {gblDetails = details}) = details
404 globalIdDetails other = notGlobalId