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
111 -- infixl so you can say (id `set` a `set` b)
112 infixl 1 `setIdUnfolding`,
114 `setIdNewDemandInfo`,
115 `setIdNewStrictness`,
117 `setIdSpecialisation`,
120 #ifdef OLD_STRICTNESS
129 %************************************************************************
131 \subsection{Simple Id construction}
133 %************************************************************************
135 Absolutely all Ids are made by mkId. It is just like Var.mkId,
136 but in addition it pins free-tyvar-info onto the Id's type,
137 where it can easily be found.
140 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
141 mkLocalIdWithInfo name ty info = Var.mkLocalId name (addFreeTyVars ty) info
143 mkExportedLocalId :: Name -> Type -> Id
144 mkExportedLocalId name ty = Var.mkExportedLocalId name (addFreeTyVars ty) vanillaIdInfo
146 mkGlobalId :: GlobalIdDetails -> Name -> Type -> IdInfo -> Id
147 mkGlobalId details name ty info = Var.mkGlobalId details name (addFreeTyVars ty) info
151 mkLocalId :: Name -> Type -> Id
152 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
154 -- SysLocal: for an Id being created by the compiler out of thin air...
155 mkSysLocal :: FastString -> Unique -> Type -> Id
156 mkSysLocal fs uniq ty = mkLocalId (mkSystemVarName uniq fs) ty
159 -- UserLocal: an Id with a name the user might recognize...
160 mkUserLocal :: OccName -> Unique -> Type -> SrcSpan -> Id
161 mkVanillaGlobal :: Name -> Type -> IdInfo -> Id
163 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
164 mkVanillaGlobal = mkGlobalId VanillaGlobal
167 Make some local @Ids@ for a template @CoreExpr@. These have bogus
168 @Uniques@, but that's OK because the templates are supposed to be
169 instantiated before use.
172 -- "Wild Id" typically used when you need a binder that you don't expect to use
173 mkWildId :: Type -> Id
174 mkWildId ty = mkSysLocal FSLIT("wild") (mkBuiltinUnique 1) ty
176 mkWorkerId :: Unique -> Id -> Type -> Id
177 -- A worker gets a local name. CoreTidy will externalise it if necessary.
178 mkWorkerId uniq unwrkr ty
179 = mkLocalId wkr_name ty
181 wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcSpan unwrkr)
183 -- "Template locals" typically used in unfoldings
184 mkTemplateLocals :: [Type] -> [Id]
185 mkTemplateLocals tys = zipWith mkTemplateLocal [1..] tys
187 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
188 -- The Int gives the starting point for unique allocation
189 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
191 mkTemplateLocal :: Int -> Type -> Id
192 mkTemplateLocal i ty = mkSysLocal FSLIT("tpl") (mkBuiltinUnique i) ty
196 %************************************************************************
198 \subsection[Id-general-funs]{General @Id@-related functions}
200 %************************************************************************
203 setIdType :: Id -> Type -> Id
204 -- Add free tyvar info to the type
205 setIdType id ty = seqType ty `seq` Var.setIdType id (addFreeTyVars ty)
207 idPrimRep :: Id -> PrimRep
208 idPrimRep id = typePrimRep (idType id)
212 %************************************************************************
214 \subsection{Special Ids}
216 %************************************************************************
219 recordSelectorFieldLabel :: Id -> (TyCon, FieldLabel)
220 recordSelectorFieldLabel id
221 = case globalIdDetails id of
222 RecordSelId { sel_tycon = tycon, sel_label = lbl } -> (tycon,lbl)
223 _ -> panic "recordSelectorFieldLabel"
225 isRecordSelector :: Var -> Bool
226 isNaughtyRecordSelector :: Var -> Bool
227 isPrimOpId :: Var -> Bool
228 isFCallId :: Var -> Bool
229 isDataConWorkId :: Var -> Bool
230 hasNoBinding :: Var -> Bool
232 isClassOpId_maybe :: Var -> Maybe Class
233 isPrimOpId_maybe :: Var -> Maybe PrimOp
234 isFCallId_maybe :: Var -> Maybe ForeignCall
235 isDataConWorkId_maybe :: Var -> Maybe DataCon
237 isRecordSelector id = case globalIdDetails id of
238 RecordSelId {} -> True
241 isNaughtyRecordSelector id = case globalIdDetails id of
242 RecordSelId { sel_naughty = n } -> n
245 isClassOpId_maybe id = case globalIdDetails id of
246 ClassOpId cls -> Just cls
249 isPrimOpId id = case globalIdDetails id of
253 isPrimOpId_maybe id = case globalIdDetails id of
254 PrimOpId op -> Just op
257 isFCallId id = case globalIdDetails id of
261 isFCallId_maybe id = case globalIdDetails id of
262 FCallId call -> Just call
265 isDataConWorkId id = case globalIdDetails id of
266 DataConWorkId _ -> True
269 isDataConWorkId_maybe id = case globalIdDetails id of
270 DataConWorkId con -> Just con
273 isDataConId_maybe :: Id -> Maybe DataCon
274 isDataConId_maybe id = case globalIdDetails id of
275 DataConWorkId con -> Just con
276 DataConWrapId con -> Just con
279 idDataCon :: Id -> DataCon
280 -- Get from either the worker or the wrapper to the DataCon
281 -- Currently used only in the desugarer
282 -- INVARIANT: idDataCon (dataConWrapId d) = d
283 -- (Remember, dataConWrapId can return either the wrapper or the worker.)
284 idDataCon id = case globalIdDetails id of
285 DataConWorkId con -> con
286 DataConWrapId con -> con
287 _ -> pprPanic "idDataCon" (ppr id)
290 isDictId :: Id -> Bool
291 isDictId id = isDictTy (idType id)
293 -- hasNoBinding returns True of an Id which may not have a
294 -- binding, even though it is defined in this module.
295 -- Data constructor workers used to be things of this kind, but
296 -- they aren't any more. Instead, we inject a binding for
297 -- them at the CorePrep stage.
298 -- EXCEPT: unboxed tuples, which definitely have no binding
299 hasNoBinding id = case globalIdDetails id of
300 PrimOpId _ -> True -- See Note [Primop wrappers]
302 DataConWorkId dc -> isUnboxedTupleCon dc
305 isImplicitId :: Id -> Bool
306 -- isImplicitId tells whether an Id's info is implied by other
307 -- declarations, so we don't need to put its signature in an interface
308 -- file, even if it's mentioned in some other interface unfolding.
310 = case globalIdDetails id of
311 RecordSelId {} -> True
315 DataConWorkId _ -> True
316 DataConWrapId _ -> True
317 -- These are are implied by their type or class decl;
318 -- remember that all type and class decls appear in the interface file.
319 -- The dfun id is not an implicit Id; it must *not* be omitted, because
320 -- it carries version info for the instance decl
323 idIsFrom :: Module -> Id -> Bool
324 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
327 Note [Primop wrappers]
328 ~~~~~~~~~~~~~~~~~~~~~~
329 Currently hasNoBinding claims that PrimOpIds don't have a curried
330 function definition. But actually they do, in GHC.PrimopWrappers,
331 which is auto-generated from prelude/primops.txt.pp. So actually, hasNoBinding
332 could return 'False' for PrimOpIds.
334 But we'd need to add something in CoreToStg to swizzle any unsaturated
335 applications of GHC.Prim.plusInt# to GHC.PrimopWrappers.plusInt#.
337 Nota Bene: GHC.PrimopWrappers is needed *regardless*, because it's
338 used by GHCi, which does not implement primops direct at all.
343 isDeadBinder :: Id -> Bool
344 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
345 | otherwise = False -- TyVars count as not dead
349 isTickBoxOp :: Id -> Bool
351 case globalIdDetails id of
352 TickBoxOpId _ -> True
355 isTickBoxOp_maybe :: Id -> Maybe TickBoxOp
356 isTickBoxOp_maybe id =
357 case globalIdDetails id of
358 TickBoxOpId tick -> Just tick
362 %************************************************************************
364 \subsection{IdInfo stuff}
366 %************************************************************************
369 ---------------------------------
371 idArity :: Id -> Arity
372 idArity id = arityInfo (idInfo id)
374 setIdArity :: Id -> Arity -> Id
375 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
377 #ifdef OLD_STRICTNESS
378 ---------------------------------
380 idStrictness :: Id -> StrictnessInfo
381 idStrictness id = strictnessInfo (idInfo id)
383 setIdStrictness :: Id -> StrictnessInfo -> Id
384 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
387 -- isBottomingId returns true if an application to n args would diverge
388 isBottomingId :: Id -> Bool
389 isBottomingId id = isBottomingSig (idNewStrictness id)
391 idNewStrictness_maybe :: Id -> Maybe StrictSig
392 idNewStrictness :: Id -> StrictSig
394 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
395 idNewStrictness id = idNewStrictness_maybe id `orElse` topSig
397 setIdNewStrictness :: Id -> StrictSig -> Id
398 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
400 zapIdNewStrictness :: Id -> Id
401 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
404 This predicate says whether the id has a strict demand placed on it or
405 has a type such that it can always be evaluated strictly (e.g., an
406 unlifted type, but see the comment for isStrictType). We need to
407 check separately whether <id> has a so-called "strict type" because if
408 the demand for <id> hasn't been computed yet but <id> has a strict
409 type, we still want (isStrictId <id>) to be True.
411 isStrictId :: Id -> Bool
413 = ASSERT2( isId id, text "isStrictId: not an id: " <+> ppr id )
414 (isStrictDmd (idNewDemandInfo id)) ||
415 (isStrictType (idType id))
417 ---------------------------------
419 idWorkerInfo :: Id -> WorkerInfo
420 idWorkerInfo id = workerInfo (idInfo id)
422 setIdWorkerInfo :: Id -> WorkerInfo -> Id
423 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
425 ---------------------------------
427 idUnfolding :: Id -> Unfolding
428 idUnfolding id = unfoldingInfo (idInfo id)
430 setIdUnfolding :: Id -> Unfolding -> Id
431 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
433 #ifdef OLD_STRICTNESS
434 ---------------------------------
436 idDemandInfo :: Id -> Demand.Demand
437 idDemandInfo id = demandInfo (idInfo id)
439 setIdDemandInfo :: Id -> Demand.Demand -> Id
440 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
443 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
444 idNewDemandInfo :: Id -> NewDemand.Demand
446 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
447 idNewDemandInfo id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
449 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
450 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
452 ---------------------------------
454 idSpecialisation :: Id -> SpecInfo
455 idSpecialisation id = specInfo (idInfo id)
457 idCoreRules :: Id -> [CoreRule]
458 idCoreRules id = specInfoRules (idSpecialisation id)
460 idHasRules :: Id -> Bool
461 idHasRules id = not (isEmptySpecInfo (idSpecialisation id))
463 setIdSpecialisation :: Id -> SpecInfo -> Id
464 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
466 ---------------------------------
468 idCafInfo :: Id -> CafInfo
469 #ifdef OLD_STRICTNESS
470 idCafInfo id = case cgInfo (idInfo id) of
471 NoCgInfo -> pprPanic "idCafInfo" (ppr id)
472 info -> cgCafInfo info
474 idCafInfo id = cafInfo (idInfo id)
477 setIdCafInfo :: Id -> CafInfo -> Id
478 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
480 ---------------------------------
482 #ifdef OLD_STRICTNESS
483 idCprInfo :: Id -> CprInfo
484 idCprInfo id = cprInfo (idInfo id)
486 setIdCprInfo :: Id -> CprInfo -> Id
487 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
490 ---------------------------------
492 idOccInfo :: Id -> OccInfo
493 idOccInfo id = occInfo (idInfo id)
495 setIdOccInfo :: Id -> OccInfo -> Id
496 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
500 ---------------------------------
502 The inline pragma tells us to be very keen to inline this Id, but it's still
503 OK not to if optimisation is switched off.
506 idInlinePragma :: Id -> InlinePragInfo
507 idInlinePragma id = inlinePragInfo (idInfo id)
509 setInlinePragma :: Id -> InlinePragInfo -> Id
510 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
512 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
513 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
517 ---------------------------------
520 idLBVarInfo :: Id -> LBVarInfo
521 idLBVarInfo id = lbvarInfo (idInfo id)
523 isOneShotBndr :: Id -> Bool
524 -- This one is the "business end", called externally.
525 -- Its main purpose is to encapsulate the Horrible State Hack
526 isOneShotBndr id = isOneShotLambda id || isStateHackType (idType id)
528 isStateHackType :: Type -> Bool
533 = case splitTyConApp_maybe ty of
534 Just (tycon,_) -> tycon == statePrimTyCon
536 -- This is a gross hack. It claims that
537 -- every function over realWorldStatePrimTy is a one-shot
538 -- function. This is pretty true in practice, and makes a big
539 -- difference. For example, consider
540 -- a `thenST` \ r -> ...E...
541 -- The early full laziness pass, if it doesn't know that r is one-shot
542 -- will pull out E (let's say it doesn't mention r) to give
543 -- let lvl = E in a `thenST` \ r -> ...lvl...
544 -- When `thenST` gets inlined, we end up with
545 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
546 -- and we don't re-inline E.
548 -- It would be better to spot that r was one-shot to start with, but
549 -- I don't want to rely on that.
551 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
552 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
555 -- The OneShotLambda functions simply fiddle with the IdInfo flag
556 isOneShotLambda :: Id -> Bool
557 isOneShotLambda id = case idLBVarInfo id of
558 IsOneShotLambda -> True
561 setOneShotLambda :: Id -> Id
562 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
564 clearOneShotLambda :: Id -> Id
565 clearOneShotLambda id
566 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
569 -- But watch out: this may change the type of something else
571 -- If we change the one-shot-ness of x, f's type changes
575 zapInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
576 zapInfo zapper id = maybeModifyIdInfo (zapper (idInfo id)) id
578 zapLamIdInfo :: Id -> Id
579 zapLamIdInfo = zapInfo zapLamInfo
581 zapDemandIdInfo :: Id -> Id
582 zapDemandIdInfo = zapInfo zapDemandInfo
584 zapFragileIdInfo :: Id -> Id
585 zapFragileIdInfo = zapInfo zapFragileInfo