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, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
15 idName, idType, idUnique, idInfo,
17 recordSelectorFieldLabel,
20 setIdName, setIdUnique, setIdType, setIdNoDiscard,
21 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
22 zapFragileIdInfo, zapLamIdInfo,
25 isImplicitId, isDeadBinder,
28 isSpecPragmaId, isRecordSelector,
29 isPrimOpId, isPrimOpId_maybe, isDictFunId,
30 isDataConId, isDataConId_maybe,
31 isDataConWrapId, isDataConWrapId_maybe,
33 isExportedId, isLocalId,
36 -- Inline pragma stuff
37 idInlinePragma, setInlinePragma, modifyInlinePragma,
40 -- One shot lambda stuff
41 isOneShotLambda, setOneShotLambda, clearOneShotLambda,
70 #include "HsVersions.h"
73 import CoreSyn ( Unfolding, CoreRules )
74 import BasicTypes ( Arity )
75 import Var ( Id, DictId,
77 idName, idType, idUnique, idInfo,
78 setIdName, setVarType, setIdUnique,
79 setIdInfo, lazySetIdInfo, modifyIdInfo,
83 import Type ( Type, typePrimRep, addFreeTyVars,
84 usOnce, seqType, splitTyConApp_maybe )
88 import Demand ( Demand )
89 import Name ( Name, OccName,
90 mkSysLocalName, mkLocalName,
93 import OccName ( UserFS )
94 import PrimRep ( PrimRep )
95 import TysPrim ( statePrimTyCon )
96 import FieldLabel ( FieldLabel )
97 import SrcLoc ( SrcLoc )
98 import Unique ( Unique, mkBuiltinUnique, getBuiltinUniques,
99 getNumBuiltinUniques )
102 infixl 1 `setIdUnfolding`,
108 `setIdSpecialisation`,
113 -- infixl so you can say (id `set` a `set` b)
118 %************************************************************************
120 \subsection{Simple Id construction}
122 %************************************************************************
124 Absolutely all Ids are made by mkId. It
125 a) Pins free-tyvar-info onto the Id's type,
126 where it can easily be found.
127 b) Ensures that exported Ids are
130 mkId :: Name -> Type -> IdInfo -> Id
131 mkId name ty info = mkIdVar name (addFreeTyVars ty) info
135 mkVanillaId :: Name -> Type -> Id
136 mkVanillaId name ty = mkId name ty vanillaIdInfo
138 -- SysLocal: for an Id being created by the compiler out of thin air...
139 -- UserLocal: an Id with a name the user might recognize...
140 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
141 mkSysLocal :: UserFS -> Unique -> Type -> Id
143 mkSysLocal fs uniq ty = mkVanillaId (mkSysLocalName uniq fs) ty
144 mkUserLocal occ uniq ty loc = mkVanillaId (mkLocalName uniq occ loc) ty
147 Make some local @Ids@ for a template @CoreExpr@. These have bogus
148 @Uniques@, but that's OK because the templates are supposed to be
149 instantiated before use.
152 -- "Wild Id" typically used when you need a binder that you don't expect to use
153 mkWildId :: Type -> Id
154 mkWildId ty = mkSysLocal SLIT("wild") (mkBuiltinUnique 1) ty
156 -- "Template locals" typically used in unfoldings
157 mkTemplateLocals :: [Type] -> [Id]
158 mkTemplateLocals tys = zipWith (mkSysLocal SLIT("tpl"))
159 (getBuiltinUniques (length tys))
162 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
163 mkTemplateLocalsNum n tys = zipWith (mkSysLocal SLIT("tpl"))
164 (getNumBuiltinUniques n (length tys))
167 mkTemplateLocal :: Int -> Type -> Id
168 mkTemplateLocal i ty = mkSysLocal SLIT("tpl") (mkBuiltinUnique i) ty
172 %************************************************************************
174 \subsection[Id-general-funs]{General @Id@-related functions}
176 %************************************************************************
179 setIdType :: Id -> Type -> Id
180 -- Add free tyvar info to the type
181 setIdType id ty = seqType ty `seq` setVarType id (addFreeTyVars ty)
183 idPrimRep :: Id -> PrimRep
184 idPrimRep id = typePrimRep (idType id)
188 %************************************************************************
190 \subsection{Special Ids}
192 %************************************************************************
195 idFlavour :: Id -> IdFlavour
196 idFlavour id = flavourInfo (idInfo id)
198 setIdNoDiscard :: Id -> Id
199 setIdNoDiscard id -- Make an Id into a NoDiscardId, unless it is already
200 = modifyIdInfo setNoDiscardInfo id
202 recordSelectorFieldLabel :: Id -> FieldLabel
203 recordSelectorFieldLabel id = case idFlavour id of
204 RecordSelId lbl -> lbl
206 isRecordSelector id = case idFlavour id of
207 RecordSelId lbl -> True
210 isPrimOpId id = case idFlavour id of
214 isPrimOpId_maybe id = case idFlavour id of
215 PrimOpId op -> Just op
218 isDataConId id = case idFlavour id of
222 isDataConId_maybe id = case idFlavour id of
223 DataConId con -> Just con
226 isDataConWrapId_maybe id = case idFlavour id of
227 DataConWrapId con -> Just con
230 isDataConWrapId id = case idFlavour id of
231 DataConWrapId con -> True
234 isSpecPragmaId id = case idFlavour id of
238 hasNoBinding id = case idFlavour id of
242 -- hasNoBinding returns True of an Id which may not have a
243 -- binding, even though it is defined in this module. Notably,
244 -- the constructors of a dictionary are in this situation.
246 isDictFunId id = case idFlavour id of
250 -- Don't drop a binding for an exported Id,
251 -- if it otherwise looks dead.
252 -- Perhaps a better name would be isDiscardableId
253 isExportedId :: Id -> Bool
254 isExportedId id = case idFlavour id of
258 isLocalId :: Id -> Bool
259 -- True of Ids that are locally defined, but are not constants
260 -- like data constructors, record selectors, and the like.
261 -- See comments with CoreFVs.isLocalVar
264 | not (isId id) = pprTrace "isLocalid" (ppr id) False
267 = case idFlavour id of
275 isImplicitId tells whether an Id's info is implied by other
276 declarations, so we don't need to put its signature in an interface
277 file, even if it's mentioned in some other interface unfolding.
280 isImplicitId :: Id -> Bool
282 = case idFlavour id of
283 RecordSelId _ -> True -- Includes dictionary selectors
286 DataConWrapId _ -> True
287 -- These are are implied by their type or class decl;
288 -- remember that all type and class decls appear in the interface file.
289 -- The dfun id must *not* be omitted, because it carries version info for
295 isDeadBinder :: Id -> Bool
296 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
297 | otherwise = False -- TyVars count as not dead
299 isIP id = isIPOcc (getOccName id)
303 %************************************************************************
305 \subsection{IdInfo stuff}
307 %************************************************************************
310 ---------------------------------
312 idArityInfo :: Id -> ArityInfo
313 idArityInfo id = arityInfo (idInfo id)
315 idArity :: Id -> Arity
316 idArity id = arityLowerBound (idArityInfo id)
318 setIdArityInfo :: Id -> ArityInfo -> Id
319 setIdArityInfo id arity = modifyIdInfo (`setArityInfo` arity) id
321 ---------------------------------
323 idStrictness :: Id -> StrictnessInfo
324 idStrictness id = strictnessInfo (idInfo id)
326 setIdStrictness :: Id -> StrictnessInfo -> Id
327 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
329 -- isBottomingId returns true if an application to n args would diverge
330 isBottomingId :: Id -> Bool
331 isBottomingId id = isBottomingStrictness (idStrictness id)
333 ---------------------------------
334 -- TYPE GENERALISATION
335 idTyGenInfo :: Id -> TyGenInfo
336 idTyGenInfo id = tyGenInfo (idInfo id)
338 setIdTyGenInfo :: Id -> TyGenInfo -> Id
339 setIdTyGenInfo id tygen_info = modifyIdInfo (`setTyGenInfo` tygen_info) id
341 ---------------------------------
343 idWorkerInfo :: Id -> WorkerInfo
344 idWorkerInfo id = workerInfo (idInfo id)
346 setIdWorkerInfo :: Id -> WorkerInfo -> Id
347 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
349 ---------------------------------
351 idUnfolding :: Id -> Unfolding
352 idUnfolding id = unfoldingInfo (idInfo id)
354 setIdUnfolding :: Id -> Unfolding -> Id
355 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
357 ---------------------------------
359 idDemandInfo :: Id -> Demand
360 idDemandInfo id = demandInfo (idInfo id)
362 setIdDemandInfo :: Id -> Demand -> Id
363 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
365 ---------------------------------
367 idSpecialisation :: Id -> CoreRules
368 idSpecialisation id = specInfo (idInfo id)
370 setIdSpecialisation :: Id -> CoreRules -> Id
371 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
373 ---------------------------------
375 idCafInfo :: Id -> CafInfo
376 idCafInfo id = cafInfo (idInfo id)
378 setIdCafInfo :: Id -> CafInfo -> Id
379 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
381 ---------------------------------
383 idCprInfo :: Id -> CprInfo
384 idCprInfo id = cprInfo (idInfo id)
386 setIdCprInfo :: Id -> CprInfo -> Id
387 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
389 ---------------------------------
391 idOccInfo :: Id -> OccInfo
392 idOccInfo id = occInfo (idInfo id)
394 setIdOccInfo :: Id -> OccInfo -> Id
395 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
399 ---------------------------------
401 The inline pragma tells us to be very keen to inline this Id, but it's still
402 OK not to if optimisation is switched off.
405 idInlinePragma :: Id -> InlinePragInfo
406 idInlinePragma id = inlinePragInfo (idInfo id)
408 setInlinePragma :: Id -> InlinePragInfo -> Id
409 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
411 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
412 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
416 ---------------------------------
419 idLBVarInfo :: Id -> LBVarInfo
420 idLBVarInfo id = lbvarInfo (idInfo id)
422 isOneShotLambda :: Id -> Bool
423 isOneShotLambda id = analysis || hack
424 where analysis = case idLBVarInfo id of
425 LBVarInfo u | u == usOnce -> True
427 hack = case splitTyConApp_maybe (idType id) of
428 Just (tycon,_) | tycon == statePrimTyCon -> True
431 -- The last clause is a gross hack. It claims that
432 -- every function over realWorldStatePrimTy is a one-shot
433 -- function. This is pretty true in practice, and makes a big
434 -- difference. For example, consider
435 -- a `thenST` \ r -> ...E...
436 -- The early full laziness pass, if it doesn't know that r is one-shot
437 -- will pull out E (let's say it doesn't mention r) to give
438 -- let lvl = E in a `thenST` \ r -> ...lvl...
439 -- When `thenST` gets inlined, we end up with
440 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
441 -- and we don't re-inline E.
443 -- It would be better to spot that r was one-shot to start with, but
444 -- I don't want to rely on that.
446 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
447 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
449 setOneShotLambda :: Id -> Id
450 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` LBVarInfo usOnce) id
452 clearOneShotLambda :: Id -> Id
453 clearOneShotLambda id
454 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
457 -- But watch out: this may change the type of something else
459 -- If we change the one-shot-ness of x, f's type changes
463 zapFragileIdInfo :: Id -> Id
464 zapFragileIdInfo id = maybeModifyIdInfo zapFragileInfo id
466 zapLamIdInfo :: Id -> Id
467 zapLamIdInfo id = maybeModifyIdInfo zapLamInfo id