2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{@Vars@: Variables}
10 setVarName, setVarUnique,
13 TyVar, mkTyVar, mkTcTyVar,
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
45 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
72 | TcTyVar { -- Used only during type inference
73 -- Used for kind variables during
76 realUnique :: FastInt,
78 tcTyVarDetails :: TcTyVarDetails }
80 | GlobalId { -- Used for imported Ids, dict selectors etc
81 -- See Note [GlobalId/LocalId] below
82 varName :: !Name, -- Always an External or WiredIn Name
83 realUnique :: FastInt,
86 gblDetails :: GlobalIdDetails }
88 | LocalId { -- Used for locally-defined Ids
89 -- See Note [GlobalId/LocalId] below
91 realUnique :: FastInt,
94 lclDetails :: LocalIdDetails }
97 = NotExported -- Not exported
98 | Exported -- Exported
99 -- Exported Ids are kept alive;
100 -- NotExported things may be discarded as dead code.
103 Note [GlobalId/LocalId]
104 ~~~~~~~~~~~~~~~~~~~~~~~
106 * always a constant (top-level)
107 * imported, or data constructor, or primop, or record selector
108 * has a Unique that is globally unique across the whole
109 GHC invocation (a single invocation may compile multiple modules)
110 * never treated as a candidate by the free-variable finder;
114 * bound within an expression (lambda, case, local let(rec))
115 * or defined at top level in the module being compiled
116 * always treated as a candidate by the free-variable finder
118 After CoreTidy, top-level LocalIds are turned into GlobalIds
122 instance Outputable Var where
123 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
126 GlobalId {} -> ptext SLIT("gid")
127 LocalId {} -> ptext SLIT("lid")
128 TyVar {} -> ptext SLIT("tv")
129 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
131 instance Show Var where
132 showsPrec p var = showsPrecSDoc p (ppr var)
134 instance NamedThing Var where
137 instance Uniquable Var where
138 getUnique = varUnique
140 instance Eq Var where
141 a == b = realUnique a ==# realUnique b
143 instance Ord Var where
144 a <= b = realUnique a <=# realUnique b
145 a < b = realUnique a <# realUnique b
146 a >= b = realUnique a >=# realUnique b
147 a > b = realUnique a ># realUnique b
148 a `compare` b = varUnique a `compare` varUnique b
153 varUnique :: Var -> Unique
154 varUnique var = mkUniqueGrimily (iBox (realUnique var))
156 setVarUnique :: Var -> Unique -> Var
157 setVarUnique var uniq
158 = var { realUnique = getKey# uniq,
159 varName = setNameUnique (varName var) uniq }
161 setVarName :: Var -> Name -> Var
162 setVarName var new_name
163 = var { realUnique = getKey# (getUnique new_name),
168 %************************************************************************
170 \subsection{Type variables}
172 %************************************************************************
179 setTyVarUnique = setVarUnique
180 setTyVarName = setVarName
182 setTyVarKind :: TyVar -> Kind -> TyVar
183 setTyVarKind tv k = tv {tyVarKind = k}
187 mkTyVar :: Name -> Kind -> TyVar
188 mkTyVar name kind = TyVar { varName = name
189 , realUnique = getKey# (nameUnique name)
193 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
194 mkTcTyVar name kind details
195 = TcTyVar { varName = name,
196 realUnique = getKey# (nameUnique name),
198 tcTyVarDetails = details
202 %************************************************************************
204 \subsection{Coercion variables}
206 %************************************************************************
209 type CoVar = Var -- A coercion variable is simply a type
210 -- variable of kind (ty1 :=: ty2)
213 setCoVarUnique = setVarUnique
214 setCoVarName = setVarName
216 mkCoVar :: Name -> Kind -> CoVar
217 mkCoVar name kind = mkTyVar name kind
219 isCoVar :: TyVar -> Bool
220 isCoVar ty = isCoSuperKind (tyVarKind ty)
223 %************************************************************************
225 \subsection{Id Construction}
227 %************************************************************************
229 Most Id-related functions are in Id.lhs and MkId.lhs
240 setIdUnique :: Id -> Unique -> Id
241 setIdUnique = setVarUnique
243 setIdName :: Id -> Name -> Id
244 setIdName = setVarName
246 setIdType :: Id -> Type -> Id
247 setIdType id ty = id {idType = ty}
249 setIdExported :: Id -> Id
250 -- Can be called on GlobalIds, such as data cons and class ops,
251 -- which are "born" as GlobalIds and automatically exported
252 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
253 setIdExported other_id = ASSERT( isId other_id ) other_id
255 setIdNotExported :: Id -> Id
256 -- We can only do this to LocalIds
257 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
259 globaliseId :: GlobalIdDetails -> Id -> Id
260 -- If it's a local, make it global
261 globaliseId details id = GlobalId { varName = varName id,
262 realUnique = realUnique id,
265 gblDetails = details }
267 lazySetIdInfo :: Id -> IdInfo -> Id
268 lazySetIdInfo id info = id {idInfo = info}
270 setIdInfo :: Id -> IdInfo -> Id
271 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
272 -- Try to avoid spack leaks by seq'ing
274 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
276 = seqIdInfo new_info `seq` id {idInfo = new_info}
278 new_info = fn (idInfo id)
280 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
281 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
282 maybeModifyIdInfo (Just new_info) id = id {idInfo = new_info}
283 maybeModifyIdInfo Nothing id = id
286 %************************************************************************
288 \subsection{Predicates over variables
290 %************************************************************************
293 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
294 mkGlobalId details name ty info
295 = GlobalId { varName = name,
296 realUnique = getKey# (nameUnique name), -- Cache the unique
298 gblDetails = details,
301 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
302 mk_local_id name ty details info
303 = LocalId { varName = name,
304 realUnique = getKey# (nameUnique name), -- Cache the unique
306 lclDetails = details,
309 mkLocalId :: Name -> Type -> IdInfo -> Id
310 mkLocalId name ty info = mk_local_id name ty NotExported info
312 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
313 mkExportedLocalId name ty info = mk_local_id name ty Exported info
317 isTyVar, isTcTyVar :: Var -> Bool
318 isId, isLocalVar, isLocalId :: Var -> Bool
319 isGlobalId, isExportedId :: Var -> Bool
320 mustHaveLocalBinding :: Var -> Bool
322 isTyVar (TyVar {}) = True
323 isTyVar (TcTyVar {}) = True
324 isTyVar other = False
326 isTcTyVar (TcTyVar {}) = True
327 isTcTyVar other = False
329 isId (LocalId {}) = True
330 isId (GlobalId {}) = True
333 isLocalId (LocalId {}) = True
334 isLocalId other = False
336 -- isLocalVar returns True for type variables as well as local Ids
337 -- These are the variables that we need to pay attention to when finding free
338 -- variables, or doing dependency analysis.
339 isLocalVar (GlobalId {}) = False
340 isLocalVar other = True
342 -- mustHaveLocalBinding returns True of Ids and TyVars
343 -- that must have a binding in this module. The converse
344 -- is not quite right: there are some GlobalIds that must have
345 -- bindings, such as record selectors. But that doesn't matter,
346 -- because it's only used for assertions
347 mustHaveLocalBinding var = isLocalVar var
349 isGlobalId (GlobalId {}) = True
350 isGlobalId other = False
352 -- isExportedId means "don't throw this away"
353 isExportedId (GlobalId {}) = True
354 isExportedId (LocalId {lclDetails = details})
358 isExportedId other = False
362 globalIdDetails :: Var -> GlobalIdDetails
363 -- Works OK on local Ids too, returning notGlobalId
364 globalIdDetails (GlobalId {gblDetails = details}) = details
365 globalIdDetails other = notGlobalId