2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{@Vars@: Variables}
10 setVarName, setVarUnique,
13 TyVar, mkTyVar, mkTcTyVar,
15 setTyVarName, setTyVarUnique,
20 idName, idType, idUnique, idInfo, modifyIdInfo, maybeModifyIdInfo,
21 setIdName, setIdUnique, setIdType, setIdInfo, lazySetIdInfo,
22 setIdExported, setIdNotExported,
24 globalIdDetails, globaliseId,
26 mkLocalId, mkExportedLocalId, mkGlobalId,
28 isTyVar, isTcTyVar, isId, isLocalVar, isLocalId,
29 isGlobalId, isExportedId,
33 #include "HsVersions.h"
35 import {-# SOURCE #-} TypeRep( Type )
36 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails )
37 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId, IdInfo, seqIdInfo )
39 import Name ( Name, NamedThing(..),
40 setNameUnique, nameUnique
43 import Unique ( Unique, Uniquable(..), mkUniqueGrimily, getKey# )
49 %************************************************************************
51 \subsection{The main data type declarations}
53 %************************************************************************
56 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
57 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
58 strictness). The essential info about different kinds of @Vars@ is
65 realUnique :: FastInt, -- Key for fast comparison
66 -- Identical to the Unique in the name,
67 -- cached here for speed
70 | TcTyVar { -- Used only during type inference
72 realUnique :: FastInt,
74 tcTyVarDetails :: TcTyVarDetails }
76 | GlobalId { -- Used for imported Ids, dict selectors etc
78 realUnique :: FastInt,
81 gblDetails :: GlobalIdDetails }
83 | LocalId { -- Used for locally-defined Ids (see NOTE below)
85 realUnique :: FastInt,
88 lclDetails :: LocalIdDetails }
91 = NotExported -- Not exported
92 | Exported -- Exported
93 -- Exported Ids are kept alive;
94 -- NotExported things may be discarded as dead code.
100 * always a constant (top-level)
101 * imported, or data constructor, or primop, or record selector
102 * has a Unique that is globally unique across the whole
103 GHC invocation (a single invocation may compile multiple modules)
106 * bound within an expression (lambda, case, local let(rec))
107 * or defined at top level in the module being compiled
109 After CoreTidy, top-level LocalIds are turned into GlobalIds
113 instance Outputable Var where
114 ppr var = ppr (varName var) <+> ifPprDebug (brackets extra)
117 GlobalId {} -> ptext SLIT("gid")
118 LocalId {} -> ptext SLIT("lid")
119 TyVar {} -> ptext SLIT("tv")
120 TcTyVar {tcTyVarDetails = details} -> pprTcTyVarDetails details
122 instance Show Var where
123 showsPrec p var = showsPrecSDoc p (ppr var)
125 instance NamedThing Var where
128 instance Uniquable Var where
129 getUnique = varUnique
131 instance Eq Var where
132 a == b = realUnique a ==# realUnique b
134 instance Ord Var where
135 a <= b = realUnique a <=# realUnique b
136 a < b = realUnique a <# realUnique b
137 a >= b = realUnique a >=# realUnique b
138 a > b = realUnique a ># realUnique b
139 a `compare` b = varUnique a `compare` varUnique b
144 varUnique :: Var -> Unique
145 varUnique var = mkUniqueGrimily (iBox (realUnique var))
147 setVarUnique :: Var -> Unique -> Var
148 setVarUnique var uniq
149 = var { realUnique = getKey# uniq,
150 varName = setNameUnique (varName var) uniq }
152 setVarName :: Var -> Name -> Var
153 setVarName var new_name
154 = var { realUnique = getKey# (getUnique new_name),
159 %************************************************************************
161 \subsection{Type variables}
163 %************************************************************************
170 setTyVarUnique = setVarUnique
171 setTyVarName = setVarName
175 mkTyVar :: Name -> Kind -> TyVar
176 mkTyVar name kind = TyVar { varName = name
177 , realUnique = getKey# (nameUnique name)
181 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
182 mkTcTyVar name kind details
183 = TcTyVar { varName = name,
184 realUnique = getKey# (nameUnique name),
186 tcTyVarDetails = details
191 %************************************************************************
193 \subsection{Id Construction}
195 %************************************************************************
197 Most Id-related functions are in Id.lhs and MkId.lhs
208 setIdUnique :: Id -> Unique -> Id
209 setIdUnique = setVarUnique
211 setIdName :: Id -> Name -> Id
212 setIdName = setVarName
214 setIdType :: Id -> Type -> Id
215 setIdType id ty = id {idType = ty}
217 setIdExported :: Id -> Id
218 -- Can be called on GlobalIds, such as data cons and class ops,
219 -- which are "born" as GlobalIds and automatically exported
220 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
221 setIdExported other_id = ASSERT( isId other_id ) other_id
223 setIdNotExported :: Id -> Id
224 -- We can only do this to LocalIds
225 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
227 globaliseId :: GlobalIdDetails -> Id -> Id
228 -- If it's a local, make it global
229 globaliseId details id = GlobalId { varName = varName id,
230 realUnique = realUnique id,
233 gblDetails = details }
235 lazySetIdInfo :: Id -> IdInfo -> Id
236 lazySetIdInfo id info = id {idInfo = info}
238 setIdInfo :: Id -> IdInfo -> Id
239 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
240 -- Try to avoid spack leaks by seq'ing
242 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
244 = seqIdInfo new_info `seq` id {idInfo = new_info}
246 new_info = fn (idInfo id)
248 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
249 maybeModifyIdInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
250 maybeModifyIdInfo fn id
251 = case fn (idInfo id) of
253 Just new_info -> id {idInfo = new_info}
256 %************************************************************************
258 \subsection{Predicates over variables
260 %************************************************************************
263 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
264 mkGlobalId details name ty info
265 = GlobalId { varName = name,
266 realUnique = getKey# (nameUnique name), -- Cache the unique
268 gblDetails = details,
271 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
272 mk_local_id name ty details info
273 = LocalId { varName = name,
274 realUnique = getKey# (nameUnique name), -- Cache the unique
276 lclDetails = details,
279 mkLocalId :: Name -> Type -> IdInfo -> Id
280 mkLocalId name ty info = mk_local_id name ty NotExported info
282 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
283 mkExportedLocalId name ty info = mk_local_id name ty Exported info
287 isTyVar, isTcTyVar :: Var -> Bool
288 isId, isLocalVar, isLocalId :: Var -> Bool
289 isGlobalId, isExportedId :: Var -> Bool
290 mustHaveLocalBinding :: Var -> Bool
292 isTyVar (TyVar {}) = True
293 isTyVar (TcTyVar {}) = True
294 isTyVar other = False
296 isTcTyVar (TcTyVar {}) = True
297 isTcTyVar other = False
299 isId (LocalId {}) = True
300 isId (GlobalId {}) = True
303 isLocalId (LocalId {}) = True
304 isLocalId other = False
306 -- isLocalVar returns True for type variables as well as local Ids
307 -- These are the variables that we need to pay attention to when finding free
308 -- variables, or doing dependency analysis.
309 isLocalVar (GlobalId {}) = False
310 isLocalVar other = True
312 -- mustHaveLocalBinding returns True of Ids and TyVars
313 -- that must have a binding in this module. The converse
314 -- is not quite right: there are some GlobalIds that must have
315 -- bindings, such as record selectors. But that doesn't matter,
316 -- because it's only used for assertions
317 mustHaveLocalBinding var = isLocalVar var
319 isGlobalId (GlobalId {}) = True
320 isGlobalId other = False
322 -- isExportedId means "don't throw this away"
323 isExportedId (GlobalId {}) = True
324 isExportedId (LocalId {lclDetails = details})
328 isExportedId other = False
332 globalIdDetails :: Var -> GlobalIdDetails
333 -- Works OK on local Ids too, returning notGlobalId
334 globalIdDetails (GlobalId {gblDetails = details}) = details
335 globalIdDetails other = notGlobalId