2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{@Vars@: Variables}
10 setVarName, setVarUnique,
13 TyVar, mkTyVar, mkTcTyVar, mkWildCoVar,
15 setTyVarName, setTyVarUnique, setTyVarKind,
19 CoVar, coVarName, setCoVarUnique, setCoVarName, mkCoVar, isCoVar,
23 idName, idType, idUnique, idInfo, modifyIdInfo, maybeModifyIdInfo,
24 setIdName, setIdUnique, setIdType, setIdInfo, lazySetIdInfo,
25 setIdExported, setIdNotExported,
27 globalIdDetails, globaliseId,
29 mkLocalId, mkExportedLocalId, mkGlobalId,
31 isTyVar, isTcTyVar, isId, isLocalVar, isLocalId,
32 isGlobalId, isExportedId,
36 #include "HsVersions.h"
38 import {-# SOURCE #-} TypeRep( Type, Kind, isCoSuperKind )
39 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
40 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId, IdInfo, seqIdInfo )
42 import Name ( Name, NamedThing(..),
43 setNameUnique, nameUnique, mkSysTvName
45 import Unique ( Unique, Uniquable(..), mkUniqueGrimily, getKey#,
52 %************************************************************************
54 \subsection{The main data type declarations}
56 %************************************************************************
59 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
60 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
61 strictness). The essential info about different kinds of @Vars@ is
68 realUnique :: FastInt, -- Key for fast comparison
69 -- Identical to the Unique in the name,
70 -- cached here for speed
75 | TcTyVar { -- Used only during type inference
76 -- Used for kind variables during
79 realUnique :: FastInt,
81 tcTyVarDetails :: TcTyVarDetails }
83 | GlobalId { -- Used for imported Ids, dict selectors etc
84 -- See Note [GlobalId/LocalId] below
85 varName :: !Name, -- Always an External or WiredIn Name
86 realUnique :: FastInt,
89 gblDetails :: GlobalIdDetails }
91 | LocalId { -- Used for locally-defined Ids
92 -- See Note [GlobalId/LocalId] below
94 realUnique :: FastInt,
97 lclDetails :: LocalIdDetails }
100 = NotExported -- Not exported
101 | Exported -- Exported
102 -- Exported Ids are kept alive;
103 -- NotExported things may be discarded as dead code.
106 Note [GlobalId/LocalId]
107 ~~~~~~~~~~~~~~~~~~~~~~~
109 * always a constant (top-level)
110 * imported, or data constructor, or primop, or record selector
111 * has a Unique that is globally unique across the whole
112 GHC invocation (a single invocation may compile multiple modules)
113 * never treated as a candidate by the free-variable finder;
117 * bound within an expression (lambda, case, local let(rec))
118 * or defined at top level in the module being compiled
119 * always treated as a candidate by the free-variable finder
121 After CoreTidy, top-level LocalIds are turned into GlobalIds
125 instance Outputable Var where
126 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
129 GlobalId {} -> ptext SLIT("gid")
130 LocalId {} -> ptext SLIT("lid")
131 TyVar {} -> ptext SLIT("tv")
132 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
134 instance Show Var where
135 showsPrec p var = showsPrecSDoc p (ppr var)
137 instance NamedThing Var where
140 instance Uniquable Var where
141 getUnique = varUnique
143 instance Eq Var where
144 a == b = realUnique a ==# realUnique b
146 instance Ord Var where
147 a <= b = realUnique a <=# realUnique b
148 a < b = realUnique a <# realUnique b
149 a >= b = realUnique a >=# realUnique b
150 a > b = realUnique a ># realUnique b
151 a `compare` b = varUnique a `compare` varUnique b
156 varUnique :: Var -> Unique
157 varUnique var = mkUniqueGrimily (iBox (realUnique var))
159 setVarUnique :: Var -> Unique -> Var
160 setVarUnique var uniq
161 = var { realUnique = getKey# uniq,
162 varName = setNameUnique (varName var) uniq }
164 setVarName :: Var -> Name -> Var
165 setVarName var new_name
166 = var { realUnique = getKey# (getUnique new_name),
171 %************************************************************************
173 \subsection{Type variables}
175 %************************************************************************
182 setTyVarUnique = setVarUnique
183 setTyVarName = setVarName
185 setTyVarKind :: TyVar -> Kind -> TyVar
186 setTyVarKind tv k = tv {tyVarKind = k}
190 mkTyVar :: Name -> Kind -> TyVar
191 mkTyVar name kind = TyVar { varName = name
192 , realUnique = getKey# (nameUnique name)
194 , isCoercionVar = False
197 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
198 mkTcTyVar name kind details
199 = TcTyVar { varName = name,
200 realUnique = getKey# (nameUnique name),
202 tcTyVarDetails = details
205 mkWildCoVar :: Kind -> TyVar
207 = TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
208 realUnique = _ILIT(1),
210 isCoercionVar = True }
212 wild_uniq = (mkBuiltinUnique 1)
215 %************************************************************************
217 \subsection{Coercion variables}
219 %************************************************************************
222 type CoVar = Var -- A coercion variable is simply a type
223 -- variable of kind (ty1 :=: ty2)
226 setCoVarUnique = setVarUnique
227 setCoVarName = setVarName
229 mkCoVar :: Name -> Kind -> CoVar
230 mkCoVar name kind = TyVar { varName = name
231 , realUnique = getKey# (nameUnique name)
233 , isCoercionVar = True
238 %************************************************************************
240 \subsection{Id Construction}
242 %************************************************************************
244 Most Id-related functions are in Id.lhs and MkId.lhs
255 setIdUnique :: Id -> Unique -> Id
256 setIdUnique = setVarUnique
258 setIdName :: Id -> Name -> Id
259 setIdName = setVarName
261 setIdType :: Id -> Type -> Id
262 setIdType id ty = id {idType = ty}
264 setIdExported :: Id -> Id
265 -- Can be called on GlobalIds, such as data cons and class ops,
266 -- which are "born" as GlobalIds and automatically exported
267 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
268 setIdExported other_id = ASSERT( isId other_id ) other_id
270 setIdNotExported :: Id -> Id
271 -- We can only do this to LocalIds
272 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
274 globaliseId :: GlobalIdDetails -> Id -> Id
275 -- If it's a local, make it global
276 globaliseId details id = GlobalId { varName = varName id,
277 realUnique = realUnique id,
280 gblDetails = details }
282 lazySetIdInfo :: Id -> IdInfo -> Id
283 lazySetIdInfo id info = id {idInfo = info}
285 setIdInfo :: Id -> IdInfo -> Id
286 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
287 -- Try to avoid spack leaks by seq'ing
289 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
291 = seqIdInfo new_info `seq` id {idInfo = new_info}
293 new_info = fn (idInfo id)
295 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
296 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
297 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
298 maybeModifyIdInfo Nothing id = id
301 %************************************************************************
303 \subsection{Predicates over variables
305 %************************************************************************
308 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
309 mkGlobalId details name ty info
310 = GlobalId { varName = name,
311 realUnique = getKey# (nameUnique name), -- Cache the unique
313 gblDetails = details,
316 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
317 mk_local_id name ty details info
318 = LocalId { varName = name,
319 realUnique = getKey# (nameUnique name), -- Cache the unique
321 lclDetails = details,
324 mkLocalId :: Name -> Type -> IdInfo -> Id
325 mkLocalId name ty info = mk_local_id name ty NotExported info
327 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
328 mkExportedLocalId name ty info = mk_local_id name ty Exported info
332 isTyVar, isTcTyVar :: Var -> Bool
333 isId, isLocalVar, isLocalId :: Var -> Bool
334 isGlobalId, isExportedId :: Var -> Bool
335 mustHaveLocalBinding :: Var -> Bool
337 isTyVar (TyVar {}) = True
338 isTyVar (TcTyVar {}) = True
339 isTyVar other = False
341 isTcTyVar (TcTyVar {}) = True
342 isTcTyVar other = False
344 isId (LocalId {}) = True
345 isId (GlobalId {}) = True
348 isLocalId (LocalId {}) = True
349 isLocalId other = False
351 isCoVar (v@(TyVar {})) = isCoercionVar v
352 isCoVar other = False
354 -- isLocalVar returns True for type variables as well as local Ids
355 -- These are the variables that we need to pay attention to when finding free
356 -- variables, or doing dependency analysis.
357 isLocalVar (GlobalId {}) = False
358 isLocalVar other = True
360 -- mustHaveLocalBinding returns True of Ids and TyVars
361 -- that must have a binding in this module. The converse
362 -- is not quite right: there are some GlobalIds that must have
363 -- bindings, such as record selectors. But that doesn't matter,
364 -- because it's only used for assertions
365 mustHaveLocalBinding var = isLocalVar var
367 isGlobalId (GlobalId {}) = True
368 isGlobalId other = False
370 -- isExportedId means "don't throw this away"
371 isExportedId (GlobalId {}) = True
372 isExportedId (LocalId {lclDetails = details})
376 isExportedId other = False
380 globalIdDetails :: Var -> GlobalIdDetails
381 -- Works OK on local Ids too, returning notGlobalId
382 globalIdDetails (GlobalId {gblDetails = details}) = details
383 globalIdDetails other = notGlobalId