2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Id]{@Ids@: Value and constructor identifiers}
10 -- Simple construction
11 mkVanillaId, mkImportedId, mkSysLocal, mkUserLocal,
12 mkTemplateLocals, mkTemplateLocal, mkWildId, mkUserId,
15 idName, idType, idUnique, idInfo, idDetails,
17 recordSelectorFieldLabel,
20 setIdName, setIdUnique, setIdType, setIdInfo,
27 -- Inline pragma stuff
28 getInlinePragma, setInlinePragma, modifyInlinePragma,
29 idWantsToBeINLINEd, idMustBeINLINEd, idMustNotBeINLINEd,
34 isPrimitiveId_maybe, isDataConId_maybe,
36 isBottomingId, idAppIsBottom,
61 #include "HsVersions.h"
63 import {-# SOURCE #-} CoreUnfold ( Unfolding )
65 import Var ( Id, DictId, VarDetails(..),
67 idName, idType, idUnique, idInfo, idDetails,
68 setIdName, setVarType, setIdUnique, setIdInfo, modifyIdInfo,
72 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars )
74 import Demand ( Demand )
75 import Name ( Name, OccName,
76 mkSysLocalName, mkLocalName,
79 import Const ( Con(..) )
80 import PrimRep ( PrimRep )
81 import PrimOp ( PrimOp )
82 import FieldLabel ( FieldLabel(..) )
83 import SrcLoc ( SrcLoc )
84 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
87 infixl 1 `setIdUnfolding`,
92 `setIdSpecialisation`,
98 -- infixl so you can say (id `set` a `set` b)
103 %************************************************************************
105 \subsection{Simple Id construction}
107 %************************************************************************
110 mkVanillaId :: Name -> Type -> Id
111 mkVanillaId name ty = mkId name (addFreeTyVars ty) VanillaId noIdInfo
113 mkImportedId :: Name -> Type -> IdInfo -> Id
114 mkImportedId name ty info = mkId name (addFreeTyVars ty) VanillaId info
116 mkUserId :: Name -> Type -> Id
117 mkUserId name ty = mkVanillaId name ty
119 -- SysLocal: for an Id being created by the compiler out of thin air...
120 -- UserLocal: an Id with a name the user might recognize...
121 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
122 mkSysLocal :: FAST_STRING -> Unique -> Type -> Id
124 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
125 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
128 Make some local @Ids@ for a template @CoreExpr@. These have bogus
129 @Uniques@, but that's OK because the templates are supposed to be
130 instantiated before use.
133 -- "Wild Id" typically used when you need a binder that you don't expect to use
134 mkWildId :: Type -> Id
135 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
137 -- "Template locals" typically used in unfoldings
138 mkTemplateLocals :: [Type] -> [Id]
139 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
140 (getBuiltinUniques (length tys))
143 mkTemplateLocal :: Int -> Type -> Id
144 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
148 %************************************************************************
150 \subsection[Id-general-funs]{General @Id@-related functions}
152 %************************************************************************
155 idFreeTyVars :: Id -> TyVarSet
156 idFreeTyVars id = tyVarsOfType (idType id)
158 setIdType :: Id -> Type -> Id
159 -- Add free tyvar info to the type
160 setIdType id ty = setVarType id (addFreeTyVars ty)
162 idPrimRep :: Id -> PrimRep
163 idPrimRep id = typePrimRep (idType id)
166 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
167 so we don't need to put its signature in an interface file, even if it's mentioned
168 in some other interface unfolding.
171 omitIfaceSigForId :: Id -> Bool
173 | isWiredInName (idName id)
177 = case idDetails id of
178 RecordSelId _ -> True -- Includes dictionary selectors
180 -- ConstantIds are implied by their type or class decl;
181 -- remember that all type and class decls appear in the interface file.
182 -- The dfun id must *not* be omitted, because it carries version info for
185 other -> False -- Don't omit!
188 %************************************************************************
190 \subsection{Special Ids}
192 %************************************************************************
195 recordSelectorFieldLabel :: Id -> FieldLabel
196 recordSelectorFieldLabel id = case idDetails id of
197 RecordSelId lbl -> lbl
199 isRecordSelector id = case idDetails id of
200 RecordSelId lbl -> True
203 isPrimitiveId_maybe id = case idDetails id of
204 ConstantId (PrimOp op) -> Just op
207 isDataConId_maybe id = case idDetails id of
208 ConstantId (DataCon con) -> Just con
211 isConstantId id = case idDetails id of
217 %************************************************************************
219 \subsection{IdInfo stuff}
221 %************************************************************************
224 ---------------------------------
226 getIdArity :: Id -> ArityInfo
227 getIdArity id = arityInfo (idInfo id)
229 setIdArity :: Id -> ArityInfo -> Id
230 setIdArity id arity = modifyIdInfo id (arity `setArityInfo`)
232 ---------------------------------
234 getIdStrictness :: Id -> StrictnessInfo
235 getIdStrictness id = strictnessInfo (idInfo id)
237 setIdStrictness :: Id -> StrictnessInfo -> Id
238 setIdStrictness id strict_info = modifyIdInfo id (strict_info `setStrictnessInfo`)
240 -- isBottomingId returns true if an application to n args would diverge
241 isBottomingId :: Id -> Bool
242 isBottomingId id = isBottomingStrictness (strictnessInfo (idInfo id))
244 idAppIsBottom :: Id -> Int -> Bool
245 idAppIsBottom id n = appIsBottom (strictnessInfo (idInfo id)) n
247 ---------------------------------
249 getIdWorkerInfo :: Id -> WorkerInfo
250 getIdWorkerInfo id = workerInfo (idInfo id)
252 setIdWorkerInfo :: Id -> WorkerInfo -> Id
253 setIdWorkerInfo id work_info = modifyIdInfo id (work_info `setWorkerInfo`)
255 ---------------------------------
257 getIdUnfolding :: Id -> Unfolding
258 getIdUnfolding id = unfoldingInfo (idInfo id)
260 setIdUnfolding :: Id -> Unfolding -> Id
261 setIdUnfolding id unfolding = modifyIdInfo id (unfolding `setUnfoldingInfo`)
263 ---------------------------------
265 getIdDemandInfo :: Id -> Demand
266 getIdDemandInfo id = demandInfo (idInfo id)
268 setIdDemandInfo :: Id -> Demand -> Id
269 setIdDemandInfo id demand_info = modifyIdInfo id (demand_info `setDemandInfo`)
271 ---------------------------------
273 getIdUpdateInfo :: Id -> UpdateInfo
274 getIdUpdateInfo id = updateInfo (idInfo id)
276 setIdUpdateInfo :: Id -> UpdateInfo -> Id
277 setIdUpdateInfo id upd_info = modifyIdInfo id (upd_info `setUpdateInfo`)
279 ---------------------------------
281 getIdSpecialisation :: Id -> IdSpecEnv
282 getIdSpecialisation id = specInfo (idInfo id)
284 setIdSpecialisation :: Id -> IdSpecEnv -> Id
285 setIdSpecialisation id spec_info = modifyIdInfo id (spec_info `setSpecInfo`)
287 ---------------------------------
289 getIdCafInfo :: Id -> CafInfo
290 getIdCafInfo id = cafInfo (idInfo id)
292 setIdCafInfo :: Id -> CafInfo -> Id
293 setIdCafInfo id caf_info = modifyIdInfo id (caf_info `setCafInfo`)
295 ---------------------------------
297 getIdCprInfo :: Id -> CprInfo
298 getIdCprInfo id = cprInfo (idInfo id)
300 setIdCprInfo :: Id -> CprInfo -> Id
301 setIdCprInfo id cpr_info = modifyIdInfo id (cpr_info `setCprInfo`)
306 ---------------------------------
308 The inline pragma tells us to be very keen to inline this Id, but it's still
309 OK not to if optimisation is switched off.
312 getInlinePragma :: Id -> InlinePragInfo
313 getInlinePragma id = inlinePragInfo (idInfo id)
315 setInlinePragma :: Id -> InlinePragInfo -> Id
316 setInlinePragma id prag = modifyIdInfo id (setInlinePragInfo prag)
318 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
319 modifyInlinePragma id fn = modifyIdInfo id (\info -> setInlinePragInfo (fn (inlinePragInfo info)) info)
321 idWantsToBeINLINEd :: Id -> Bool
322 idWantsToBeINLINEd id = case getInlinePragma id of
323 IWantToBeINLINEd -> True
324 IMustBeINLINEd -> True
327 idMustNotBeINLINEd id = case getInlinePragma id of
328 IMustNotBeINLINEd -> True
329 IAmASpecPragmaId -> True
330 IAmALoopBreaker -> True
333 idMustBeINLINEd id = case getInlinePragma id of
334 IMustBeINLINEd -> True
337 isSpecPragmaId id = case getInlinePragma id of
338 IAmASpecPragmaId -> True