2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{@Vars@: Variables}
10 setVarName, setVarUnique,
13 TyVar, mkTyVar, mkTcTyVar, mkWildTyVar,
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
73 | TcTyVar { -- Used only during type inference
74 -- Used for kind variables during
77 realUnique :: FastInt,
79 tcTyVarDetails :: TcTyVarDetails }
81 | GlobalId { -- Used for imported Ids, dict selectors etc
82 -- See Note [GlobalId/LocalId] below
83 varName :: !Name, -- Always an External or WiredIn Name
84 realUnique :: FastInt,
87 gblDetails :: GlobalIdDetails }
89 | LocalId { -- Used for locally-defined Ids
90 -- See Note [GlobalId/LocalId] below
92 realUnique :: FastInt,
95 lclDetails :: LocalIdDetails }
98 = NotExported -- Not exported
99 | Exported -- Exported
100 -- Exported Ids are kept alive;
101 -- NotExported things may be discarded as dead code.
104 Note [GlobalId/LocalId]
105 ~~~~~~~~~~~~~~~~~~~~~~~
107 * always a constant (top-level)
108 * imported, or data constructor, or primop, or record selector
109 * has a Unique that is globally unique across the whole
110 GHC invocation (a single invocation may compile multiple modules)
111 * never treated as a candidate by the free-variable finder;
115 * bound within an expression (lambda, case, local let(rec))
116 * or defined at top level in the module being compiled
117 * always treated as a candidate by the free-variable finder
119 After CoreTidy, top-level LocalIds are turned into GlobalIds
123 instance Outputable Var where
124 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
127 GlobalId {} -> ptext SLIT("gid")
128 LocalId {} -> ptext SLIT("lid")
129 TyVar {} -> ptext SLIT("tv")
130 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
132 instance Show Var where
133 showsPrec p var = showsPrecSDoc p (ppr var)
135 instance NamedThing Var where
138 instance Uniquable Var where
139 getUnique = varUnique
141 instance Eq Var where
142 a == b = realUnique a ==# realUnique b
144 instance Ord Var where
145 a <= b = realUnique a <=# realUnique b
146 a < b = realUnique a <# realUnique b
147 a >= b = realUnique a >=# realUnique b
148 a > b = realUnique a ># realUnique b
149 a `compare` b = varUnique a `compare` varUnique b
154 varUnique :: Var -> Unique
155 varUnique var = mkUniqueGrimily (iBox (realUnique var))
157 setVarUnique :: Var -> Unique -> Var
158 setVarUnique var uniq
159 = var { realUnique = getKey# uniq,
160 varName = setNameUnique (varName var) uniq }
162 setVarName :: Var -> Name -> Var
163 setVarName var new_name
164 = var { realUnique = getKey# (getUnique new_name),
169 %************************************************************************
171 \subsection{Type variables}
173 %************************************************************************
180 setTyVarUnique = setVarUnique
181 setTyVarName = setVarName
183 setTyVarKind :: TyVar -> Kind -> TyVar
184 setTyVarKind tv k = tv {tyVarKind = k}
188 mkTyVar :: Name -> Kind -> TyVar
189 mkTyVar name kind = TyVar { varName = name
190 , realUnique = getKey# (nameUnique name)
194 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
195 mkTcTyVar name kind details
196 = TcTyVar { varName = name,
197 realUnique = getKey# (nameUnique name),
199 tcTyVarDetails = details
202 mkWildTyVar :: Kind -> TyVar
204 = TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
205 realUnique = _ILIT(1),
208 wild_uniq = (mkBuiltinUnique 1)
211 %************************************************************************
213 \subsection{Coercion variables}
215 %************************************************************************
218 type CoVar = Var -- A coercion variable is simply a type
219 -- variable of kind (ty1 :=: ty2)
222 setCoVarUnique = setVarUnique
223 setCoVarName = setVarName
225 mkCoVar :: Name -> Kind -> CoVar
226 mkCoVar name kind = mkTyVar name kind
228 isCoVar :: TyVar -> Bool
229 isCoVar ty = isCoSuperKind (tyVarKind ty)
232 %************************************************************************
234 \subsection{Id Construction}
236 %************************************************************************
238 Most Id-related functions are in Id.lhs and MkId.lhs
249 setIdUnique :: Id -> Unique -> Id
250 setIdUnique = setVarUnique
252 setIdName :: Id -> Name -> Id
253 setIdName = setVarName
255 setIdType :: Id -> Type -> Id
256 setIdType id ty = id {idType = ty}
258 setIdExported :: Id -> Id
259 -- Can be called on GlobalIds, such as data cons and class ops,
260 -- which are "born" as GlobalIds and automatically exported
261 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
262 setIdExported other_id = ASSERT( isId other_id ) other_id
264 setIdNotExported :: Id -> Id
265 -- We can only do this to LocalIds
266 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
268 globaliseId :: GlobalIdDetails -> Id -> Id
269 -- If it's a local, make it global
270 globaliseId details id = GlobalId { varName = varName id,
271 realUnique = realUnique id,
274 gblDetails = details }
276 lazySetIdInfo :: Id -> IdInfo -> Id
277 lazySetIdInfo id info = id {idInfo = info}
279 setIdInfo :: Id -> IdInfo -> Id
280 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
281 -- Try to avoid spack leaks by seq'ing
283 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
285 = seqIdInfo new_info `seq` id {idInfo = new_info}
287 new_info = fn (idInfo id)
289 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
290 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
291 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
292 maybeModifyIdInfo Nothing id = id
295 %************************************************************************
297 \subsection{Predicates over variables
299 %************************************************************************
302 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
303 mkGlobalId details name ty info
304 = GlobalId { varName = name,
305 realUnique = getKey# (nameUnique name), -- Cache the unique
307 gblDetails = details,
310 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
311 mk_local_id name ty details info
312 = LocalId { varName = name,
313 realUnique = getKey# (nameUnique name), -- Cache the unique
315 lclDetails = details,
318 mkLocalId :: Name -> Type -> IdInfo -> Id
319 mkLocalId name ty info = mk_local_id name ty NotExported info
321 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
322 mkExportedLocalId name ty info = mk_local_id name ty Exported info
326 isTyVar, isTcTyVar :: Var -> Bool
327 isId, isLocalVar, isLocalId :: Var -> Bool
328 isGlobalId, isExportedId :: Var -> Bool
329 mustHaveLocalBinding :: Var -> Bool
331 isTyVar (TyVar {}) = True
332 isTyVar (TcTyVar {}) = True
333 isTyVar other = False
335 isTcTyVar (TcTyVar {}) = True
336 isTcTyVar other = False
338 isId (LocalId {}) = True
339 isId (GlobalId {}) = True
342 isLocalId (LocalId {}) = True
343 isLocalId other = False
345 -- isLocalVar returns True for type variables as well as local Ids
346 -- These are the variables that we need to pay attention to when finding free
347 -- variables, or doing dependency analysis.
348 isLocalVar (GlobalId {}) = False
349 isLocalVar other = True
351 -- mustHaveLocalBinding returns True of Ids and TyVars
352 -- that must have a binding in this module. The converse
353 -- is not quite right: there are some GlobalIds that must have
354 -- bindings, such as record selectors. But that doesn't matter,
355 -- because it's only used for assertions
356 mustHaveLocalBinding var = isLocalVar var
358 isGlobalId (GlobalId {}) = True
359 isGlobalId other = False
361 -- isExportedId means "don't throw this away"
362 isExportedId (GlobalId {}) = True
363 isExportedId (LocalId {lclDetails = details})
367 isExportedId other = False
371 globalIdDetails :: Var -> GlobalIdDetails
372 -- Works OK on local Ids too, returning notGlobalId
373 globalIdDetails (GlobalId {gblDetails = details}) = details
374 globalIdDetails other = notGlobalId