2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{@Vars@: Variables}
10 setVarName, setVarUnique, setVarOcc,
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 )
37 import {-# SOURCE #-} IdInfo( GlobalIdDetails, notGlobalId, IdInfo, seqIdInfo )
39 import Name ( Name, OccName, NamedThing(..),
40 setNameUnique, setNameOcc, 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)
116 instance Show Var where
117 showsPrec p var = showsPrecSDoc p (ppr var)
119 instance NamedThing Var where
122 instance Uniquable Var where
123 getUnique = varUnique
125 instance Eq Var where
126 a == b = realUnique a ==# realUnique b
128 instance Ord Var where
129 a <= b = realUnique a <=# realUnique b
130 a < b = realUnique a <# realUnique b
131 a >= b = realUnique a >=# realUnique b
132 a > b = realUnique a ># realUnique b
133 a `compare` b = varUnique a `compare` varUnique b
138 varUnique :: Var -> Unique
139 varUnique var = mkUniqueGrimily (iBox (realUnique var))
141 setVarUnique :: Var -> Unique -> Var
142 setVarUnique var uniq
143 = var { realUnique = getKey# uniq,
144 varName = setNameUnique (varName var) uniq }
146 setVarName :: Var -> Name -> Var
147 setVarName var new_name
148 = var { realUnique = getKey# (getUnique new_name),
151 setVarOcc :: Var -> OccName -> Var
152 setVarOcc var new_occ
153 = var { varName = setNameOcc (varName var) new_occ }
157 %************************************************************************
159 \subsection{Type variables}
161 %************************************************************************
168 setTyVarUnique = setVarUnique
169 setTyVarName = setVarName
173 mkTyVar :: Name -> Kind -> TyVar
174 mkTyVar name kind = TyVar { varName = name
175 , realUnique = getKey# (nameUnique name)
179 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
180 mkTcTyVar name kind details
181 = TcTyVar { varName = name,
182 realUnique = getKey# (nameUnique name),
184 tcTyVarDetails = details
189 %************************************************************************
191 \subsection{Id Construction}
193 %************************************************************************
195 Most Id-related functions are in Id.lhs and MkId.lhs
206 setIdUnique :: Id -> Unique -> Id
207 setIdUnique = setVarUnique
209 setIdName :: Id -> Name -> Id
210 setIdName = setVarName
212 setIdType :: Id -> Type -> Id
213 setIdType id ty = id {idType = ty}
215 setIdExported :: Id -> Id
216 -- Can be called on GlobalIds, such as data cons and class ops,
217 -- which are "born" as GlobalIds and automatically exported
218 setIdExported id@(LocalId {}) = id { lclDetails = Exported }
219 setIdExported other_id = ASSERT( isId other_id ) other_id
221 setIdNotExported :: Id -> Id
222 -- We can only do this to LocalIds
223 setIdNotExported id = ASSERT( isLocalId id ) id { lclDetails = NotExported }
225 globaliseId :: GlobalIdDetails -> Id -> Id
226 -- If it's a local, make it global
227 globaliseId details id = GlobalId { varName = varName id,
228 realUnique = realUnique id,
231 gblDetails = details }
233 lazySetIdInfo :: Id -> IdInfo -> Id
234 lazySetIdInfo id info = id {idInfo = info}
236 setIdInfo :: Id -> IdInfo -> Id
237 setIdInfo id info = seqIdInfo info `seq` id {idInfo = info}
238 -- Try to avoid spack leaks by seq'ing
240 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
242 = seqIdInfo new_info `seq` id {idInfo = new_info}
244 new_info = fn (idInfo id)
246 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
247 maybeModifyIdInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
248 maybeModifyIdInfo fn id
249 = case fn (idInfo id) of
251 Just new_info -> id {idInfo = new_info}
254 %************************************************************************
256 \subsection{Predicates over variables
258 %************************************************************************
261 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
262 mkGlobalId details name ty info
263 = GlobalId { varName = name,
264 realUnique = getKey# (nameUnique name), -- Cache the unique
266 gblDetails = details,
269 mk_local_id :: Name -> Type -> LocalIdDetails -> IdInfo -> Id
270 mk_local_id name ty details info
271 = LocalId { varName = name,
272 realUnique = getKey# (nameUnique name), -- Cache the unique
274 lclDetails = details,
277 mkLocalId :: Name -> Type -> IdInfo -> Id
278 mkLocalId name ty info = mk_local_id name ty NotExported info
280 mkExportedLocalId :: Name -> Type -> IdInfo -> Id
281 mkExportedLocalId name ty info = mk_local_id name ty Exported info
285 isTyVar, isTcTyVar :: Var -> Bool
286 isId, isLocalVar, isLocalId :: Var -> Bool
287 isGlobalId, isExportedId :: Var -> Bool
288 mustHaveLocalBinding :: Var -> Bool
290 isTyVar (TyVar {}) = True
291 isTyVar (TcTyVar {}) = True
292 isTyVar other = False
294 isTcTyVar (TcTyVar {}) = True
295 isTcTyVar other = False
297 isId (LocalId {}) = True
298 isId (GlobalId {}) = True
301 isLocalId (LocalId {}) = True
302 isLocalId other = False
304 -- isLocalVar returns True for type variables as well as local Ids
305 -- These are the variables that we need to pay attention to when finding free
306 -- variables, or doing dependency analysis.
307 isLocalVar (GlobalId {}) = False
308 isLocalVar other = True
310 -- mustHaveLocalBinding returns True of Ids and TyVars
311 -- that must have a binding in this module. The converse
312 -- is not quite right: there are some GlobalIds that must have
313 -- bindings, such as record selectors. But that doesn't matter,
314 -- because it's only used for assertions
315 mustHaveLocalBinding var = isLocalVar var
317 isGlobalId (GlobalId {}) = True
318 isGlobalId other = False
320 -- isExportedId means "don't throw this away"
321 isExportedId (GlobalId {}) = True
322 isExportedId (LocalId {lclDetails = details})
326 isExportedId other = False
330 globalIdDetails :: Var -> GlobalIdDetails
331 -- Works OK on local Ids too, returning notGlobalId
332 globalIdDetails (GlobalId {gblDetails = details}) = details
333 globalIdDetails other = notGlobalId