2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section{@Vars@: Variables}
10 varName, varUnique, varType,
11 setVarName, setVarUnique,
14 TyVar, mkTyVar, mkTcTyVar, mkWildCoVar,
16 setTyVarName, setTyVarUnique, setTyVarKind,
20 CoVar, coVarName, setCoVarUnique, setCoVarName, mkCoVar, isCoVar,
24 idName, idType, idUnique, idInfo, modifyIdInfo, maybeModifyIdInfo,
25 setIdName, setIdUnique, setIdType, setIdInfo, lazySetIdInfo,
26 setIdExported, setIdNotExported,
28 globalIdDetails, globaliseId,
30 mkLocalId, mkExportedLocalId, mkGlobalId,
32 isTyVar, isTcTyVar, isId, isLocalVar, isLocalId,
33 isGlobalId, isExportedId,
37 #include "HsVersions.h"
39 import {-# SOURCE #-} TypeRep( Type, Kind )
40 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
41 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId,
44 import {-# SOURCE #-} TypeRep( isCoercionKind )
47 import Name hiding (varName)
55 %************************************************************************
57 \subsection{The main data type declarations}
59 %************************************************************************
62 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
63 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
64 strictness). The essential info about different kinds of @Vars@ is
71 realUnique :: FastInt, -- Key for fast comparison
72 -- Identical to the Unique in the name,
73 -- cached here for speed
78 | TcTyVar { -- Used only during type inference
79 -- Used for kind variables during
82 realUnique :: FastInt,
84 tcTyVarDetails :: TcTyVarDetails }
86 | GlobalId { -- Used for imported Ids, dict selectors etc
87 -- See Note [GlobalId/LocalId] below
88 varName :: !Name, -- Always an External or WiredIn Name
89 realUnique :: FastInt,
92 gblDetails :: GlobalIdDetails }
94 | LocalId { -- Used for locally-defined Ids
95 -- See Note [GlobalId/LocalId] below
97 realUnique :: FastInt,
100 lclDetails :: LocalIdDetails }
103 = NotExported -- Not exported
104 | Exported -- Exported
105 -- Exported Ids are kept alive;
106 -- NotExported things may be discarded as dead code.
109 Note [GlobalId/LocalId]
110 ~~~~~~~~~~~~~~~~~~~~~~~
112 * always a constant (top-level)
113 * imported, or data constructor, or primop, or record selector
114 * has a Unique that is globally unique across the whole
115 GHC invocation (a single invocation may compile multiple modules)
116 * never treated as a candidate by the free-variable finder;
120 * bound within an expression (lambda, case, local let(rec))
121 * or defined at top level in the module being compiled
122 * always treated as a candidate by the free-variable finder
124 After CoreTidy, top-level LocalIds are turned into GlobalIds
128 instance Outputable Var where
129 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
132 GlobalId {} -> ptext SLIT("gid")
133 LocalId {} -> ptext SLIT("lid")
134 TyVar {} -> ptext SLIT("tv")
135 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
137 instance Show Var where
138 showsPrec p var = showsPrecSDoc p (ppr var)
140 instance NamedThing Var where
143 instance Uniquable Var where
144 getUnique = varUnique
146 instance Eq Var where
147 a == b = realUnique a ==# realUnique b
149 instance Ord Var where
150 a <= b = realUnique a <=# realUnique b
151 a < b = realUnique a <# realUnique b
152 a >= b = realUnique a >=# realUnique b
153 a > b = realUnique a ># realUnique b
154 a `compare` b = varUnique a `compare` varUnique b
159 varUnique :: Var -> Unique
160 varUnique var = mkUniqueGrimily (iBox (realUnique var))
162 setVarUnique :: Var -> Unique -> Var
163 setVarUnique var uniq
164 = var { realUnique = getKey# uniq,
165 varName = setNameUnique (varName var) uniq }
167 setVarName :: Var -> Name -> Var
168 setVarName var new_name
169 = var { realUnique = getKey# (getUnique new_name),
174 %************************************************************************
176 \subsection{Type variables}
178 %************************************************************************
186 setTyVarUnique = setVarUnique
187 setTyVarName = setVarName
189 setTyVarKind :: TyVar -> Kind -> TyVar
190 setTyVarKind tv k = tv {varType = k}
194 mkTyVar :: Name -> Kind -> TyVar
195 mkTyVar name kind = ASSERT( not (isCoercionKind kind ) )
196 TyVar { varName = name
197 , realUnique = getKey# (nameUnique name)
199 , isCoercionVar = False
202 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
203 mkTcTyVar name kind details
204 = -- TOM: no longer valid assertion?
205 -- ASSERT( not (isCoercionKind kind) )
206 TcTyVar { varName = name,
207 realUnique = getKey# (nameUnique name),
209 tcTyVarDetails = details
213 %************************************************************************
215 \subsection{Coercion variables}
217 %************************************************************************
220 type CoVar = Var -- A coercion variable is simply a type
221 -- variable of kind (ty1 :=: ty2)
224 setCoVarUnique = setVarUnique
225 setCoVarName = setVarName
227 mkCoVar :: Name -> Kind -> CoVar
228 mkCoVar name kind = ASSERT( isCoercionKind kind )
229 TyVar { varName = name
230 , realUnique = getKey# (nameUnique name)
232 , isCoercionVar = True
235 mkWildCoVar :: Kind -> TyVar
236 -- A type variable that is never referred to,
237 -- so its unique doesn't matter
239 = ASSERT( isCoercionKind kind )
240 TyVar { varName = mkSysTvName wild_uniq FSLIT("co_wild"),
241 realUnique = _ILIT(1),
243 isCoercionVar = True }
245 wild_uniq = mkBuiltinUnique 1
248 %************************************************************************
250 \subsection{Id Construction}
252 %************************************************************************
254 Most Id-related functions are in Id.lhs and MkId.lhs
266 setIdUnique :: Id -> Unique -> Id
267 setIdUnique = setVarUnique
269 setIdName :: Id -> Name -> Id
270 setIdName = setVarName
272 setIdType :: Id -> Type -> Id
273 setIdType id ty = id {varType = ty}
275 setIdExported :: Id -> Id
276 -- Can be called on GlobalIds, such as data cons and class ops,
277 -- which are "born" as GlobalIds and automatically exported
278 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
279 setIdExported other_id = ASSERT( isId other_id ) other_id
281 setIdNotExported :: Id -> Id
282 -- We can only do this to LocalIds
283 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
285 globaliseId :: GlobalIdDetails -> Id -> Id
286 -- If it's a local, make it global
287 globaliseId details id = GlobalId { varName = varName id,
288 realUnique = realUnique id,
289 varType = varType id,
291 gblDetails = details }
293 idInfo :: Id -> IdInfo
294 idInfo (GlobalId {idInfo_ = info}) = info
295 idInfo (LocalId {idInfo_ = info}) = info
296 idInfo other_var = pprPanic "idInfo" (ppr other_var)
298 lazySetIdInfo :: Id -> IdInfo -> Id
299 lazySetIdInfo id info = id {idInfo_ = info}
301 setIdInfo :: Id -> IdInfo -> Id
302 setIdInfo id info = seqIdInfo info `seq` id {idInfo_ = info}
303 -- Try to avoid spack leaks by seq'ing
305 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
307 = seqIdInfo new_info `seq` id {idInfo_ = new_info}
309 new_info = fn (idInfo id)
311 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
312 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
313 maybeModifyIdInfo (Just new_info) id = id {idInfo_ = new_info}
314 maybeModifyIdInfo Nothing id = id
317 %************************************************************************
319 \subsection{Predicates over variables
321 %************************************************************************
324 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
325 mkGlobalId details name ty info
326 = GlobalId { varName = name,
327 realUnique = getKey# (nameUnique name), -- Cache the unique
329 gblDetails = details,
332 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
333 mk_local_id name ty details info
334 = LocalId { varName = name,
335 realUnique = getKey# (nameUnique name), -- Cache the unique
337 lclDetails = details,
340 mkLocalId :: Name -> Type -> IdInfo -> Id
341 mkLocalId name ty info = mk_local_id name ty NotExported info
343 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
344 mkExportedLocalId name ty info = mk_local_id name ty Exported info
348 isTyVar, isTcTyVar :: Var -> Bool
349 isId, isLocalVar, isLocalId :: Var -> Bool
350 isGlobalId, isExportedId :: Var -> Bool
351 mustHaveLocalBinding :: Var -> Bool
353 isTyVar (TyVar {}) = True
354 isTyVar (TcTyVar {}) = True
355 isTyVar other = False
357 isTcTyVar (TcTyVar {}) = True
358 isTcTyVar other = False
360 isId (LocalId {}) = True
361 isId (GlobalId {}) = True
364 isLocalId (LocalId {}) = True
365 isLocalId other = False
367 isCoVar (v@(TyVar {})) = isCoercionVar v
368 isCoVar other = False
370 -- isLocalVar returns True for type variables as well as local Ids
371 -- These are the variables that we need to pay attention to when finding free
372 -- variables, or doing dependency analysis.
373 isLocalVar (GlobalId {}) = False
374 isLocalVar other = True
376 -- mustHaveLocalBinding returns True of Ids and TyVars
377 -- that must have a binding in this module. The converse
378 -- is not quite right: there are some GlobalIds that must have
379 -- bindings, such as record selectors. But that doesn't matter,
380 -- because it's only used for assertions
381 mustHaveLocalBinding var = isLocalVar var
383 isGlobalId (GlobalId {}) = True
384 isGlobalId other = False
386 -- isExportedId means "don't throw this away"
387 isExportedId (GlobalId {}) = True
388 isExportedId (LocalId {lclDetails = details})
392 isExportedId other = False
396 globalIdDetails :: Var -> GlobalIdDetails
397 -- Works OK on local Ids too, returning notGlobalId
398 globalIdDetails (GlobalId {gblDetails = details}) = details
399 globalIdDetails other = notGlobalId