2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[Id]{@Ids@: Value and constructor identifiers}
11 -- Simple construction
12 mkGlobalId, mkLocalId, mkLocalIdWithInfo,
13 mkSysLocal, mkUserLocal, mkVanillaGlobal,
14 mkTemplateLocals, mkTemplateLocalsNum, mkWildId, mkTemplateLocal,
15 mkWorkerId, mkExportedLocalId,
18 idName, idType, idUnique, idInfo,
19 isId, globalIdDetails, idPrimRep,
20 recordSelectorFieldLabel,
23 setIdName, setIdUnique, Id.setIdType, setIdExported, setIdNotExported,
24 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
25 zapLamIdInfo, zapDemandIdInfo, zapFragileIdInfo,
28 isImplicitId, isDeadBinder, isDictId, isStrictId,
29 isExportedId, isLocalId, isGlobalId,
30 isRecordSelector, isNaughtyRecordSelector,
32 isPrimOpId, isPrimOpId_maybe,
33 isFCallId, isFCallId_maybe,
34 isDataConWorkId, isDataConWorkId_maybe, isDataConId_maybe, idDataCon,
35 isBottomingId, idIsFrom,
36 isTickBoxOp, isTickBoxOp_maybe,
39 -- Inline pragma stuff
40 idInlinePragma, setInlinePragma, modifyInlinePragma,
43 -- One shot lambda stuff
44 isOneShotBndr, isOneShotLambda, isStateHackType,
45 setOneShotLambda, clearOneShotLambda,
51 setIdNewStrictness, zapIdNewStrictness,
67 idNewDemandInfo, idNewDemandInfo_maybe,
68 idNewStrictness, idNewStrictness_maybe,
71 idSpecialisation, idCoreRules, idHasRules,
77 newStrictnessFromOld -- Temporary
82 #include "HsVersions.h"
87 import Var hiding (mkLocalId, mkGlobalId, mkExportedLocalId)
94 import qualified Demand
108 -- infixl so you can say (id `set` a `set` b)
109 infixl 1 `setIdUnfolding`,
111 `setIdNewDemandInfo`,
112 `setIdNewStrictness`,
114 `setIdSpecialisation`,
117 #ifdef OLD_STRICTNESS
126 %************************************************************************
128 \subsection{Simple Id construction}
130 %************************************************************************
132 Absolutely all Ids are made by mkId. It is just like Var.mkId,
133 but in addition it pins free-tyvar-info onto the Id's type,
134 where it can easily be found.
137 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
138 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
140 mkExportedLocalId :: Name -> Type -> Id
141 mkExportedLocalId name ty = Var.mkExportedLocalId name (addFreeTyVars ty) vanillaIdInfo
143 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
144 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
148 mkLocalId :: Name -> Type -> Id
149 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
151 -- SysLocal: for an Id being created by the compiler out of thin air...
152 mkSysLocal :: FastString -> Unique -> Type -> Id
153 mkSysLocal fs uniq ty = mkLocalId (mkSystemVarName uniq fs) ty
156 -- UserLocal: an Id with a name the user might recognize...
157 mkUserLocal :: OccName -> Unique -> Type -> SrcLoc -> Id
158 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
160 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
161 mkVanillaGlobal = mkGlobalId VanillaGlobal
164 Make some local @Ids@ for a template @CoreExpr@. These have bogus
165 @Uniques@, but that's OK because the templates are supposed to be
166 instantiated before use.
169 -- "Wild Id" typically used when you need a binder that you don't expect to use
170 mkWildId :: Type -> Id
171 mkWildId ty = mkSysLocal FSLIT("wild") (mkBuiltinUnique 1) ty
173 mkWorkerId :: Unique -> Id -> Type -> Id
174 -- A worker gets a local name. CoreTidy will externalise it if necessary.
175 mkWorkerId uniq unwrkr ty
176 = mkLocalId wkr_name ty
178 wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
180 -- "Template locals" typically used in unfoldings
181 mkTemplateLocals :: [Type] -> [Id]
182 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
184 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
185 -- The Int gives the starting point for unique allocation
186 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
188 mkTemplateLocal :: Int -> Type -> Id
189 mkTemplateLocal i ty = mkSysLocal FSLIT("tpl") (mkBuiltinUnique i) ty
193 %************************************************************************
195 \subsection[Id-general-funs]{General @Id@-related functions}
197 %************************************************************************
200 setIdType :: Id -> Type -> Id
201 -- Add free tyvar info to the type
202 setIdType id ty = seqType ty `seq` Var.setIdType id (addFreeTyVars ty)
204 idPrimRep :: Id -> PrimRep
205 idPrimRep id = typePrimRep (idType id)
209 %************************************************************************
211 \subsection{Special Ids}
213 %************************************************************************
216 recordSelectorFieldLabel :: Id -> (TyCon, FieldLabel)
217 recordSelectorFieldLabel id = case globalIdDetails id of
218 RecordSelId tycon lbl _ -> (tycon,lbl)
219 other -> panic "recordSelectorFieldLabel"
221 isRecordSelector id = case globalIdDetails id of
222 RecordSelId {} -> True
225 isNaughtyRecordSelector id = case globalIdDetails id of
226 RecordSelId { sel_naughty = n } -> n
229 isClassOpId_maybe id = case globalIdDetails id of
230 ClassOpId cls -> Just cls
233 isPrimOpId id = case globalIdDetails id of
237 isPrimOpId_maybe id = case globalIdDetails id of
238 PrimOpId op -> Just op
241 isFCallId id = case globalIdDetails id of
245 isFCallId_maybe id = case globalIdDetails id of
246 FCallId call -> Just call
249 isDataConWorkId id = case globalIdDetails id of
250 DataConWorkId _ -> True
253 isDataConWorkId_maybe id = case globalIdDetails id of
254 DataConWorkId con -> Just con
257 isDataConId_maybe :: Id -> Maybe DataCon
258 isDataConId_maybe id = case globalIdDetails id of
259 DataConWorkId con -> Just con
260 DataConWrapId con -> Just con
263 idDataCon :: Id -> DataCon
264 -- Get from either the worker or the wrapper to the DataCon
265 -- Currently used only in the desugarer
266 -- INVARIANT: idDataCon (dataConWrapId d) = d
267 -- (Remember, dataConWrapId can return either the wrapper or the worker.)
268 idDataCon id = case globalIdDetails id of
269 DataConWorkId con -> con
270 DataConWrapId con -> con
271 other -> pprPanic "idDataCon" (ppr id)
274 isDictId :: Id -> Bool
275 isDictId id = isDictTy (idType id)
277 -- hasNoBinding returns True of an Id which may not have a
278 -- binding, even though it is defined in this module.
279 -- Data constructor workers used to be things of this kind, but
280 -- they aren't any more. Instead, we inject a binding for
281 -- them at the CorePrep stage.
282 -- EXCEPT: unboxed tuples, which definitely have no binding
283 hasNoBinding id = case globalIdDetails id of
286 DataConWorkId dc -> isUnboxedTupleCon dc
289 isImplicitId :: Id -> Bool
290 -- isImplicitId tells whether an Id's info is implied by other
291 -- declarations, so we don't need to put its signature in an interface
292 -- file, even if it's mentioned in some other interface unfolding.
294 = case globalIdDetails id of
295 RecordSelId {} -> True
299 DataConWorkId _ -> True
300 DataConWrapId _ -> True
301 -- These are are implied by their type or class decl;
302 -- remember that all type and class decls appear in the interface file.
303 -- The dfun id is not an implicit Id; it must *not* be omitted, because
304 -- it carries version info for the instance decl
307 idIsFrom :: Module -> Id -> Bool
308 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
312 isDeadBinder :: Id -> Bool
313 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
314 | otherwise = False -- TyVars count as not dead
318 isTickBoxOp :: Id -> Bool
320 case globalIdDetails id of
321 TickBoxOpId tick -> True
324 isTickBoxOp_maybe :: Id -> Maybe TickBoxOp
325 isTickBoxOp_maybe id =
326 case globalIdDetails id of
327 TickBoxOpId tick -> Just tick
331 %************************************************************************
333 \subsection{IdInfo stuff}
335 %************************************************************************
338 ---------------------------------
340 idArity :: Id -> Arity
341 idArity id = arityInfo (idInfo id)
343 setIdArity :: Id -> Arity -> Id
344 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
346 #ifdef OLD_STRICTNESS
347 ---------------------------------
349 idStrictness :: Id -> StrictnessInfo
350 idStrictness id = strictnessInfo (idInfo id)
352 setIdStrictness :: Id -> StrictnessInfo -> Id
353 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
356 -- isBottomingId returns true if an application to n args would diverge
357 isBottomingId :: Id -> Bool
358 isBottomingId id = isBottomingSig (idNewStrictness id)
360 idNewStrictness_maybe :: Id -> Maybe StrictSig
361 idNewStrictness :: Id -> StrictSig
363 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
364 idNewStrictness id = idNewStrictness_maybe id `orElse` topSig
366 setIdNewStrictness :: Id -> StrictSig -> Id
367 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
369 zapIdNewStrictness :: Id -> Id
370 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
373 This predicate says whether the id has a strict demand placed on it or
374 has a type such that it can always be evaluated strictly (e.g., an
375 unlifted type, but see the comment for isStrictType). We need to
376 check separately whether <id> has a so-called "strict type" because if
377 the demand for <id> hasn't been computed yet but <id> has a strict
378 type, we still want (isStrictId <id>) to be True.
380 isStrictId :: Id -> Bool
382 = ASSERT2( isId id, text "isStrictId: not an id: " <+> ppr id )
383 (isStrictDmd (idNewDemandInfo id)) ||
384 (isStrictType (idType id))
386 ---------------------------------
388 idWorkerInfo :: Id -> WorkerInfo
389 idWorkerInfo id = workerInfo (idInfo id)
391 setIdWorkerInfo :: Id -> WorkerInfo -> Id
392 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
394 ---------------------------------
396 idUnfolding :: Id -> Unfolding
397 idUnfolding id = unfoldingInfo (idInfo id)
399 setIdUnfolding :: Id -> Unfolding -> Id
400 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
402 #ifdef OLD_STRICTNESS
403 ---------------------------------
405 idDemandInfo :: Id -> Demand.Demand
406 idDemandInfo id = demandInfo (idInfo id)
408 setIdDemandInfo :: Id -> Demand.Demand -> Id
409 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
412 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
413 idNewDemandInfo :: Id -> NewDemand.Demand
415 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
416 idNewDemandInfo id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
418 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
419 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
421 ---------------------------------
423 idSpecialisation :: Id -> SpecInfo
424 idSpecialisation id = specInfo (idInfo id)
426 idCoreRules :: Id -> [CoreRule]
427 idCoreRules id = specInfoRules (idSpecialisation id)
429 idHasRules :: Id -> Bool
430 idHasRules id = not (isEmptySpecInfo (idSpecialisation id))
432 setIdSpecialisation :: Id -> SpecInfo -> Id
433 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
435 ---------------------------------
437 idCafInfo :: Id -> CafInfo
438 #ifdef OLD_STRICTNESS
439 idCafInfo id = case cgInfo (idInfo id) of
440 NoCgInfo -> pprPanic "idCafInfo" (ppr id)
441 info -> cgCafInfo info
443 idCafInfo id = cafInfo (idInfo id)
446 setIdCafInfo :: Id -> CafInfo -> Id
447 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
449 ---------------------------------
451 #ifdef OLD_STRICTNESS
452 idCprInfo :: Id -> CprInfo
453 idCprInfo id = cprInfo (idInfo id)
455 setIdCprInfo :: Id -> CprInfo -> Id
456 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
459 ---------------------------------
461 idOccInfo :: Id -> OccInfo
462 idOccInfo id = occInfo (idInfo id)
464 setIdOccInfo :: Id -> OccInfo -> Id
465 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
469 ---------------------------------
471 The inline pragma tells us to be very keen to inline this Id, but it's still
472 OK not to if optimisation is switched off.
475 idInlinePragma :: Id -> InlinePragInfo
476 idInlinePragma id = inlinePragInfo (idInfo id)
478 setInlinePragma :: Id -> InlinePragInfo -> Id
479 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
481 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
482 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
486 ---------------------------------
489 idLBVarInfo :: Id -> LBVarInfo
490 idLBVarInfo id = lbvarInfo (idInfo id)
492 isOneShotBndr :: Id -> Bool
493 -- This one is the "business end", called externally.
494 -- Its main purpose is to encapsulate the Horrible State Hack
495 isOneShotBndr id = isOneShotLambda id || isStateHackType (idType id)
497 isStateHackType :: Type -> Bool
502 = case splitTyConApp_maybe ty of
503 Just (tycon,_) -> tycon == statePrimTyCon
505 -- This is a gross hack. It claims that
506 -- every function over realWorldStatePrimTy is a one-shot
507 -- function. This is pretty true in practice, and makes a big
508 -- difference. For example, consider
509 -- a `thenST` \ r -> ...E...
510 -- The early full laziness pass, if it doesn't know that r is one-shot
511 -- will pull out E (let's say it doesn't mention r) to give
512 -- let lvl = E in a `thenST` \ r -> ...lvl...
513 -- When `thenST` gets inlined, we end up with
514 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
515 -- and we don't re-inline E.
517 -- It would be better to spot that r was one-shot to start with, but
518 -- I don't want to rely on that.
520 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
521 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
524 -- The OneShotLambda functions simply fiddle with the IdInfo flag
525 isOneShotLambda :: Id -> Bool
526 isOneShotLambda id = case idLBVarInfo id of
527 IsOneShotLambda -> True
530 setOneShotLambda :: Id -> Id
531 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
533 clearOneShotLambda :: Id -> Id
534 clearOneShotLambda id
535 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
538 -- But watch out: this may change the type of something else
540 -- If we change the one-shot-ness of x, f's type changes
544 zapInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
545 zapInfo zapper id = maybeModifyIdInfo (zapper (idInfo id)) id
547 zapLamIdInfo :: Id -> Id
548 zapLamIdInfo = zapInfo zapLamInfo
550 zapDemandIdInfo = zapInfo zapDemandInfo
552 zapFragileIdInfo :: Id -> Id
553 zapFragileIdInfo = zapInfo zapFragileInfo