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 )
39 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
40 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId, IdInfo, seqIdInfo )
42 import Name ( Name, NamedThing(..), setNameUnique, nameUnique, mkSysTvName )
43 import Unique ( Unique, Uniquable(..), mkUniqueGrimily, getKey#,
51 %************************************************************************
53 \subsection{The main data type declarations}
55 %************************************************************************
58 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
59 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
60 strictness). The essential info about different kinds of @Vars@ is
67 realUnique :: FastInt, -- Key for fast comparison
68 -- Identical to the Unique in the name,
69 -- cached here for speed
74 | TcTyVar { -- Used only during type inference
75 -- Used for kind variables during
78 realUnique :: FastInt,
80 tcTyVarDetails :: TcTyVarDetails }
82 | GlobalId { -- Used for imported Ids, dict selectors etc
83 -- See Note [GlobalId/LocalId] below
84 varName :: !Name, -- Always an External or WiredIn Name
85 realUnique :: FastInt,
88 gblDetails :: GlobalIdDetails }
90 | LocalId { -- Used for locally-defined Ids
91 -- See Note [GlobalId/LocalId] below
93 realUnique :: FastInt,
96 lclDetails :: LocalIdDetails }
99 = NotExported -- Not exported
100 | Exported -- Exported
101 -- Exported Ids are kept alive;
102 -- NotExported things may be discarded as dead code.
105 Note [GlobalId/LocalId]
106 ~~~~~~~~~~~~~~~~~~~~~~~
108 * always a constant (top-level)
109 * imported, or data constructor, or primop, or record selector
110 * has a Unique that is globally unique across the whole
111 GHC invocation (a single invocation may compile multiple modules)
112 * never treated as a candidate by the free-variable finder;
116 * bound within an expression (lambda, case, local let(rec))
117 * or defined at top level in the module being compiled
118 * always treated as a candidate by the free-variable finder
120 After CoreTidy, top-level LocalIds are turned into GlobalIds
124 instance Outputable Var where
125 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
128 GlobalId {} -> ptext SLIT("gid")
129 LocalId {} -> ptext SLIT("lid")
130 TyVar {} -> ptext SLIT("tv")
131 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
133 instance Show Var where
134 showsPrec p var = showsPrecSDoc p (ppr var)
136 instance NamedThing Var where
139 instance Uniquable Var where
140 getUnique = varUnique
142 instance Eq Var where
143 a == b = realUnique a ==# realUnique b
145 instance Ord Var where
146 a <= b = realUnique a <=# realUnique b
147 a < b = realUnique a <# realUnique b
148 a >= b = realUnique a >=# realUnique b
149 a > b = realUnique a ># realUnique b
150 a `compare` b = varUnique a `compare` varUnique b
155 varUnique :: Var -> Unique
156 varUnique var = mkUniqueGrimily (iBox (realUnique var))
158 setVarUnique :: Var -> Unique -> Var
159 setVarUnique var uniq
160 = var { realUnique = getKey# uniq,
161 varName = setNameUnique (varName var) uniq }
163 setVarName :: Var -> Name -> Var
164 setVarName var new_name
165 = var { realUnique = getKey# (getUnique new_name),
170 %************************************************************************
172 \subsection{Type variables}
174 %************************************************************************
181 setTyVarUnique = setVarUnique
182 setTyVarName = setVarName
184 setTyVarKind :: TyVar -> Kind -> TyVar
185 setTyVarKind tv k = tv {tyVarKind = k}
189 mkTyVar :: Name -> Kind -> TyVar
190 mkTyVar name kind = TyVar { varName = name
191 , realUnique = getKey# (nameUnique name)
193 , isCoercionVar = False
196 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
197 mkTcTyVar name kind details
198 = TcTyVar { varName = name,
199 realUnique = getKey# (nameUnique name),
201 tcTyVarDetails = details
204 mkWildCoVar :: Kind -> TyVar
205 -- A type variable that is never referred to,
206 -- so its unique doesn't matter
208 = TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
209 realUnique = _ILIT(1),
211 isCoercionVar = True }
213 wild_uniq = mkBuiltinUnique 1
216 %************************************************************************
218 \subsection{Coercion variables}
220 %************************************************************************
223 type CoVar = Var -- A coercion variable is simply a type
224 -- variable of kind (ty1 :=: ty2)
227 setCoVarUnique = setVarUnique
228 setCoVarName = setVarName
230 mkCoVar :: Name -> Kind -> CoVar
231 mkCoVar name kind = TyVar { varName = name
232 , realUnique = getKey# (nameUnique name)
234 , isCoercionVar = True
239 %************************************************************************
241 \subsection{Id Construction}
243 %************************************************************************
245 Most Id-related functions are in Id.lhs and MkId.lhs
256 setIdUnique :: Id -> Unique -> Id
257 setIdUnique = setVarUnique
259 setIdName :: Id -> Name -> Id
260 setIdName = setVarName
262 setIdType :: Id -> Type -> Id
263 setIdType id ty = id {idType = ty}
265 setIdExported :: Id -> Id
266 -- Can be called on GlobalIds, such as data cons and class ops,
267 -- which are "born" as GlobalIds and automatically exported
268 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
269 setIdExported other_id = ASSERT( isId other_id ) other_id
271 setIdNotExported :: Id -> Id
272 -- We can only do this to LocalIds
273 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
275 globaliseId :: GlobalIdDetails -> Id -> Id
276 -- If it's a local, make it global
277 globaliseId details id = GlobalId { varName = varName id,
278 realUnique = realUnique id,
281 gblDetails = details }
283 lazySetIdInfo :: Id -> IdInfo -> Id
284 lazySetIdInfo id info = id {idInfo = info}
286 setIdInfo :: Id -> IdInfo -> Id
287 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
288 -- Try to avoid spack leaks by seq'ing
290 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
292 = seqIdInfo new_info `seq` id {idInfo = new_info}
294 new_info = fn (idInfo id)
296 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
297 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
298 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
299 maybeModifyIdInfo Nothing id = id
302 %************************************************************************
304 \subsection{Predicates over variables
306 %************************************************************************
309 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
310 mkGlobalId details name ty info
311 = GlobalId { varName = name,
312 realUnique = getKey# (nameUnique name), -- Cache the unique
314 gblDetails = details,
317 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
318 mk_local_id name ty details info
319 = LocalId { varName = name,
320 realUnique = getKey# (nameUnique name), -- Cache the unique
322 lclDetails = details,
325 mkLocalId :: Name -> Type -> IdInfo -> Id
326 mkLocalId name ty info = mk_local_id name ty NotExported info
328 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
329 mkExportedLocalId name ty info = mk_local_id name ty Exported info
333 isTyVar, isTcTyVar :: Var -> Bool
334 isId, isLocalVar, isLocalId :: Var -> Bool
335 isGlobalId, isExportedId :: Var -> Bool
336 mustHaveLocalBinding :: Var -> Bool
338 isTyVar (TyVar {}) = True
339 isTyVar (TcTyVar {}) = True
340 isTyVar other = False
342 isTcTyVar (TcTyVar {}) = True
343 isTcTyVar other = False
345 isId (LocalId {}) = True
346 isId (GlobalId {}) = True
349 isLocalId (LocalId {}) = True
350 isLocalId other = False
352 isCoVar (v@(TyVar {})) = isCoercionVar v
353 isCoVar other = False
355 -- isLocalVar returns True for type variables as well as local Ids
356 -- These are the variables that we need to pay attention to when finding free
357 -- variables, or doing dependency analysis.
358 isLocalVar (GlobalId {}) = False
359 isLocalVar other = True
361 -- mustHaveLocalBinding returns True of Ids and TyVars
362 -- that must have a binding in this module. The converse
363 -- is not quite right: there are some GlobalIds that must have
364 -- bindings, such as record selectors. But that doesn't matter,
365 -- because it's only used for assertions
366 mustHaveLocalBinding var = isLocalVar var
368 isGlobalId (GlobalId {}) = True
369 isGlobalId other = False
371 -- isExportedId means "don't throw this away"
372 isExportedId (GlobalId {}) = True
373 isExportedId (LocalId {lclDetails = details})
377 isExportedId other = False
381 globalIdDetails :: Var -> GlobalIdDetails
382 -- Works OK on local Ids too, returning notGlobalId
383 globalIdDetails (GlobalId {gblDetails = details}) = details
384 globalIdDetails other = notGlobalId