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,
46 import Unique ( Unique, Uniquable(..), mkUniqueGrimily, getKey#,
54 %************************************************************************
56 \subsection{The main data type declarations}
58 %************************************************************************
61 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
62 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
63 strictness). The essential info about different kinds of @Vars@ is
70 realUnique :: FastInt, -- Key for fast comparison
71 -- Identical to the Unique in the name,
72 -- cached here for speed
77 | TcTyVar { -- Used only during type inference
78 -- Used for kind variables during
81 realUnique :: FastInt,
83 tcTyVarDetails :: TcTyVarDetails }
85 | GlobalId { -- Used for imported Ids, dict selectors etc
86 -- See Note [GlobalId/LocalId] below
87 varName :: !Name, -- Always an External or WiredIn Name
88 realUnique :: FastInt,
91 gblDetails :: GlobalIdDetails }
93 | LocalId { -- Used for locally-defined Ids
94 -- See Note [GlobalId/LocalId] below
96 realUnique :: FastInt,
99 lclDetails :: LocalIdDetails }
102 = NotExported -- Not exported
103 | Exported -- Exported
104 -- Exported Ids are kept alive;
105 -- NotExported things may be discarded as dead code.
108 Note [GlobalId/LocalId]
109 ~~~~~~~~~~~~~~~~~~~~~~~
111 * always a constant (top-level)
112 * imported, or data constructor, or primop, or record selector
113 * has a Unique that is globally unique across the whole
114 GHC invocation (a single invocation may compile multiple modules)
115 * never treated as a candidate by the free-variable finder;
119 * bound within an expression (lambda, case, local let(rec))
120 * or defined at top level in the module being compiled
121 * always treated as a candidate by the free-variable finder
123 After CoreTidy, top-level LocalIds are turned into GlobalIds
127 instance Outputable Var where
128 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
131 GlobalId {} -> ptext SLIT("gid")
132 LocalId {} -> ptext SLIT("lid")
133 TyVar {} -> ptext SLIT("tv")
134 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
136 instance Show Var where
137 showsPrec p var = showsPrecSDoc p (ppr var)
139 instance NamedThing Var where
142 instance Uniquable Var where
143 getUnique = varUnique
145 instance Eq Var where
146 a == b = realUnique a ==# realUnique b
148 instance Ord Var where
149 a <= b = realUnique a <=# realUnique b
150 a < b = realUnique a <# realUnique b
151 a >= b = realUnique a >=# realUnique b
152 a > b = realUnique a ># realUnique b
153 a `compare` b = varUnique a `compare` varUnique b
158 varUnique :: Var -> Unique
159 varUnique var = mkUniqueGrimily (iBox (realUnique var))
161 setVarUnique :: Var -> Unique -> Var
162 setVarUnique var uniq
163 = var { realUnique = getKey# uniq,
164 varName = setNameUnique (varName var) uniq }
166 setVarName :: Var -> Name -> Var
167 setVarName var new_name
168 = var { realUnique = getKey# (getUnique new_name),
173 %************************************************************************
175 \subsection{Type variables}
177 %************************************************************************
184 setTyVarUnique = setVarUnique
185 setTyVarName = setVarName
187 setTyVarKind :: TyVar -> Kind -> TyVar
188 setTyVarKind tv k = tv {tyVarKind = k}
192 mkTyVar :: Name -> Kind -> TyVar
193 mkTyVar name kind = TyVar { varName = name
194 , realUnique = getKey# (nameUnique name)
196 , isCoercionVar = False
199 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
200 mkTcTyVar name kind details
201 = TcTyVar { varName = name,
202 realUnique = getKey# (nameUnique name),
204 tcTyVarDetails = details
207 mkWildCoVar :: Kind -> TyVar
208 -- A type variable that is never referred to,
209 -- so its unique doesn't matter
211 = TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
212 realUnique = _ILIT(1),
214 isCoercionVar = True }
216 wild_uniq = mkBuiltinUnique 1
219 %************************************************************************
221 \subsection{Coercion variables}
223 %************************************************************************
226 type CoVar = Var -- A coercion variable is simply a type
227 -- variable of kind (ty1 :=: ty2)
230 setCoVarUnique = setVarUnique
231 setCoVarName = setVarName
233 mkCoVar :: Name -> Kind -> CoVar
234 mkCoVar name kind = TyVar { varName = name
235 , realUnique = getKey# (nameUnique name)
237 , isCoercionVar = True
242 %************************************************************************
244 \subsection{Id Construction}
246 %************************************************************************
248 Most Id-related functions are in Id.lhs and MkId.lhs
259 setIdUnique :: Id -> Unique -> Id
260 setIdUnique = setVarUnique
262 setIdName :: Id -> Name -> Id
263 setIdName = setVarName
265 setIdType :: Id -> Type -> Id
266 setIdType id ty = id {idType = ty}
268 setIdExported :: Id -> Id
269 -- Can be called on GlobalIds, such as data cons and class ops,
270 -- which are "born" as GlobalIds and automatically exported
271 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
272 setIdExported other_id = ASSERT( isId other_id ) other_id
274 setIdNotExported :: Id -> Id
275 -- We can only do this to LocalIds
276 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
278 globaliseId :: GlobalIdDetails -> Id -> Id
279 -- If it's a local, make it global
280 globaliseId details id = GlobalId { varName = varName id,
281 realUnique = realUnique id,
284 gblDetails = details }
286 lazySetIdInfo :: Id -> IdInfo -> Id
287 lazySetIdInfo id info = id {idInfo = info}
289 setIdInfo :: Id -> IdInfo -> Id
290 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
291 -- Try to avoid spack leaks by seq'ing
293 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
295 = seqIdInfo new_info `seq` id {idInfo = new_info}
297 new_info = fn (idInfo id)
299 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
300 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
301 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
302 maybeModifyIdInfo Nothing id = id
305 %************************************************************************
307 \subsection{Predicates over variables
309 %************************************************************************
312 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
313 mkGlobalId details name ty info
314 = GlobalId { varName = name,
315 realUnique = getKey# (nameUnique name), -- Cache the unique
317 gblDetails = details,
320 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
321 mk_local_id name ty details info
322 = LocalId { varName = name,
323 realUnique = getKey# (nameUnique name), -- Cache the unique
325 lclDetails = details,
328 mkLocalId :: Name -> Type -> IdInfo -> Id
329 mkLocalId name ty info = mk_local_id name ty NotExported info
331 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
332 mkExportedLocalId name ty info = mk_local_id name ty Exported info
336 isTyVar, isTcTyVar :: Var -> Bool
337 isId, isLocalVar, isLocalId :: Var -> Bool
338 isGlobalId, isExportedId :: Var -> Bool
339 mustHaveLocalBinding :: Var -> Bool
341 isTyVar (TyVar {}) = True
342 isTyVar (TcTyVar {}) = True
343 isTyVar other = False
345 isTcTyVar (TcTyVar {}) = True
346 isTcTyVar other = False
348 isId (LocalId {}) = True
349 isId (GlobalId {}) = True
352 isLocalId (LocalId {}) = True
353 isLocalId other = False
355 isCoVar (v@(TyVar {})) = isCoercionVar v
356 isCoVar other = False
358 -- isLocalVar returns True for type variables as well as local Ids
359 -- These are the variables that we need to pay attention to when finding free
360 -- variables, or doing dependency analysis.
361 isLocalVar (GlobalId {}) = False
362 isLocalVar other = True
364 -- mustHaveLocalBinding returns True of Ids and TyVars
365 -- that must have a binding in this module. The converse
366 -- is not quite right: there are some GlobalIds that must have
367 -- bindings, such as record selectors. But that doesn't matter,
368 -- because it's only used for assertions
369 mustHaveLocalBinding var = isLocalVar var
371 isGlobalId (GlobalId {}) = True
372 isGlobalId other = False
374 -- isExportedId means "don't throw this away"
375 isExportedId (GlobalId {}) = True
376 isExportedId (LocalId {lclDetails = details})
380 isExportedId other = False
384 globalIdDetails :: Var -> GlobalIdDetails
385 -- Works OK on local Ids too, returning notGlobalId
386 globalIdDetails (GlobalId {gblDetails = details}) = details
387 globalIdDetails other = notGlobalId