2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Id]{@Ids@: Value and constructor identifiers}
10 -- Simple construction
11 mkId, mkVanillaId, mkSysLocal, mkUserLocal,
12 mkTemplateLocals, mkWildId, mkTemplateLocal,
15 idName, idType, idUnique, idInfo,
17 recordSelectorFieldLabel,
20 setIdName, setIdUnique, setIdType, setIdNoDiscard,
21 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
22 zapFragileIdInfo, zapLamIdInfo,
25 omitIfaceSigForId, isDeadBinder,
26 exportWithOrigOccName,
30 isSpecPragmaId, isRecordSelector,
31 isPrimOpId, isPrimOpId_maybe,
32 isDataConId, isDataConId_maybe, isDataConWrapId, isDataConWrapId_maybe,
34 isExportedId, isUserExportedId,
37 -- Inline pragma stuff
38 idInlinePragma, setInlinePragma, modifyInlinePragma,
41 -- One shot lambda stuff
42 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
71 #include "HsVersions.h"
74 import CoreSyn ( Unfolding, CoreRules )
75 import BasicTypes ( Arity )
76 import Var ( Id, DictId,
78 idName, idType, idUnique, idInfo,
79 setIdName, setVarType, setIdUnique,
80 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
84 import Type ( Type, tyVarsOfType, typePrimRep, addFreeTyVars, seqType, splitTyConApp_maybe )
88 import Demand ( Demand, isStrict, wwLazy )
89 import Name ( Name, OccName,
90 mkSysLocalName, mkLocalName,
91 isWiredInName, isUserExportedName,
94 import OccName ( UserFS )
95 import PrimRep ( PrimRep )
96 import PrimOp ( PrimOp, primOpIsCheap )
97 import TysPrim ( statePrimTyCon )
98 import FieldLabel ( FieldLabel )
99 import SrcLoc ( SrcLoc )
100 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques )
103 infixl 1 `setIdUnfolding`,
108 `setIdSpecialisation`,
114 -- infixl so you can say (id `set` a `set` b)
119 %************************************************************************
121 \subsection{Simple Id construction}
123 %************************************************************************
125 Absolutely all Ids are made by mkId. It
126 a) Pins free-tyvar-info onto the Id's type,
127 where it can easily be found.
128 b) Ensures that exported Ids are
131 mkId :: Name -> Type -> IdInfo -> Id
132 mkId name ty info = mkIdVar name (addFreeTyVars ty) info'
134 info' | isUserExportedName name = setNoDiscardInfo info
139 mkVanillaId :: Name -> Type -> Id
140 mkVanillaId name ty = mkId name ty vanillaIdInfo
142 -- SysLocal: for an Id being created by the compiler out of thin air...
143 -- UserLocal: an Id with a name the user might recognize...
144 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
145 mkSysLocal :: UserFS -> Unique -> Type -> Id
147 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
148 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
151 Make some local @Ids@ for a template @CoreExpr@. These have bogus
152 @Uniques@, but that's OK because the templates are supposed to be
153 instantiated before use.
156 -- "Wild Id" typically used when you need a binder that you don't expect to use
157 mkWildId :: Type -> Id
158 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
160 -- "Template locals" typically used in unfoldings
161 mkTemplateLocals :: [Type] -> [Id]
162 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
163 (getBuiltinUniques (length tys))
166 mkTemplateLocal :: Int -> Type -> Id
167 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
171 %************************************************************************
173 \subsection[Id-general-funs]{General @Id@-related functions}
175 %************************************************************************
178 idFreeTyVars :: Id -> TyVarSet
179 idFreeTyVars id = tyVarsOfType (idType id)
181 setIdType :: Id -> Type -> Id
182 -- Add free tyvar info to the type
183 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
185 idPrimRep :: Id -> PrimRep
186 idPrimRep id = typePrimRep (idType id)
190 %************************************************************************
192 \subsection{Special Ids}
194 %************************************************************************
197 idFlavour :: Id -> IdFlavour
198 idFlavour id = flavourInfo (idInfo id)
200 setIdNoDiscard :: Id -> Id
201 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
202 = modifyIdInfo setNoDiscardInfo id
204 recordSelectorFieldLabel :: Id -> FieldLabel
205 recordSelectorFieldLabel id = case idFlavour id of
206 RecordSelId lbl -> lbl
208 isRecordSelector id = case idFlavour id of
209 RecordSelId lbl -> True
212 isPrimOpId id = case idFlavour id of
216 isPrimOpId_maybe id = case idFlavour id of
217 PrimOpId op -> Just op
220 isDataConId id = case idFlavour id of
224 isDataConId_maybe id = case idFlavour id of
225 DataConId con -> Just con
228 isDataConWrapId_maybe id = case idFlavour id of
229 DataConWrapId con -> Just con
232 isDataConWrapId id = case idFlavour id of
233 DataConWrapId con -> True
236 isSpecPragmaId id = case idFlavour id of
240 hasNoBinding id = case idFlavour id of
244 -- hasNoBinding returns True of an Id which may not have a
245 -- binding, even though it is defined in this module. Notably,
246 -- the constructors of a dictionary are in this situation.
248 -- Don't drop a binding for an exported Id,
249 -- if it otherwise looks dead.
250 isExportedId :: Id -> Bool
251 isExportedId id = case idFlavour id of
253 other -> True -- All the others are no-discard
255 -- Say if an Id was exported by the user
256 -- Implies isExportedId (see mkId above)
257 isUserExportedId :: Id -> Bool
258 isUserExportedId id = isUserExportedName (idName id)
262 omitIfaceSigForId tells whether an Id's info is implied by other declarations,
263 so we don't need to put its signature in an interface file, even if it's mentioned
264 in some other interface unfolding.
267 omitIfaceSigForId :: Id -> Bool
269 | isWiredInName (idName id)
273 = case idFlavour id of
274 RecordSelId _ -> True -- Includes dictionary selectors
277 DataConWrapId _ -> True
278 -- These are are implied by their type or class decl;
279 -- remember that all type and class decls appear in the interface file.
280 -- The dfun id must *not* be omitted, because it carries version info for
283 other -> False -- Don't omit!
285 -- Certain names must be exported with their original occ names, because
286 -- these names are bound by either a class declaration or a data declaration
287 -- or an explicit user export.
288 exportWithOrigOccName :: Id -> Bool
289 exportWithOrigOccName id = omitIfaceSigForId id || isUserExportedId id
293 isDeadBinder :: Id -> Bool
294 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
295 | otherwise = False -- TyVars count as not dead
297 isIP id = isIPOcc (getOccName id)
301 %************************************************************************
303 \subsection{IdInfo stuff}
305 %************************************************************************
308 ---------------------------------
310 idArityInfo :: Id -> ArityInfo
311 idArityInfo id = arityInfo (idInfo id)
313 idArity :: Id -> Arity
314 idArity id = arityLowerBound (idArityInfo id)
316 setIdArityInfo :: Id -> ArityInfo -> Id
317 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
319 ---------------------------------
321 idStrictness :: Id -> StrictnessInfo
322 idStrictness id = strictnessInfo (idInfo id)
324 setIdStrictness :: Id -> StrictnessInfo -> Id
325 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
327 -- isBottomingId returns true if an application to n args would diverge
328 isBottomingId :: Id -> Bool
329 isBottomingId id = isBottomingStrictness (idStrictness id)
331 ---------------------------------
333 idWorkerInfo :: Id -> WorkerInfo
334 idWorkerInfo id = workerInfo (idInfo id)
336 setIdWorkerInfo :: Id -> WorkerInfo -> Id
337 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
339 ---------------------------------
341 idUnfolding :: Id -> Unfolding
342 idUnfolding id = unfoldingInfo (idInfo id)
344 setIdUnfolding :: Id -> Unfolding -> Id
345 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
347 ---------------------------------
349 idDemandInfo :: Id -> Demand
350 idDemandInfo id = demandInfo (idInfo id)
352 setIdDemandInfo :: Id -> Demand -> Id
353 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
355 ---------------------------------
357 idUpdateInfo :: Id -> UpdateInfo
358 idUpdateInfo id = updateInfo (idInfo id)
360 setIdUpdateInfo :: Id -> UpdateInfo -> Id
361 setIdUpdateInfo id upd_info = modifyIdInfo (`setUpdateInfo` upd_info) id
363 ---------------------------------
365 idSpecialisation :: Id -> CoreRules
366 idSpecialisation id = specInfo (idInfo id)
368 setIdSpecialisation :: Id -> CoreRules -> Id
369 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
371 ---------------------------------
373 idCafInfo :: Id -> CafInfo
374 idCafInfo id = cafInfo (idInfo id)
376 setIdCafInfo :: Id -> CafInfo -> Id
377 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
379 ---------------------------------
381 idCprInfo :: Id -> CprInfo
382 idCprInfo id = cprInfo (idInfo id)
384 setIdCprInfo :: Id -> CprInfo -> Id
385 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
387 ---------------------------------
389 idOccInfo :: Id -> OccInfo
390 idOccInfo id = occInfo (idInfo id)
392 setIdOccInfo :: Id -> OccInfo -> Id
393 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
397 ---------------------------------
399 The inline pragma tells us to be very keen to inline this Id, but it's still
400 OK not to if optimisation is switched off.
403 idInlinePragma :: Id -> InlinePragInfo
404 idInlinePragma id = inlinePragInfo (idInfo id)
406 setInlinePragma :: Id -> InlinePragInfo -> Id
407 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
409 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
410 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
414 ---------------------------------
417 idLBVarInfo :: Id -> LBVarInfo
418 idLBVarInfo id = lbvarInfo (idInfo id)
420 isOneShotLambda :: Id -> Bool
421 isOneShotLambda id = case idLBVarInfo id of
422 IsOneShotLambda -> True
423 NoLBVarInfo -> case splitTyConApp_maybe (idType id) of
424 Just (tycon,_) -> tycon == statePrimTyCon
426 -- The last clause is a gross hack. It claims that
427 -- every function over realWorldStatePrimTy is a one-shot
428 -- function. This is pretty true in practice, and makes a big
429 -- difference. For example, consider
430 -- a `thenST` \ r -> ...E...
431 -- The early full laziness pass, if it doesn't know that r is one-shot
432 -- will pull out E (let's say it doesn't mention r) to give
433 -- let lvl = E in a `thenST` \ r -> ...lvl...
434 -- When `thenST` gets inlined, we end up with
435 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
436 -- and we don't re-inline E.
438 -- It would be better to spot that r was one-shot to start with, but
439 -- I don't want to rely on that.
441 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
442 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
444 setOneShotLambda :: Id -> Id
445 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
447 clearOneShotLambda :: Id -> Id
448 clearOneShotLambda id
449 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
452 -- But watch out: this may change the type of something else
454 -- If we change the one-shot-ness of x, f's type changes
458 zapFragileIdInfo :: Id -> Id
459 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
461 zapLamIdInfo :: Id -> Id
462 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id