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}
10 -- GHC uses several kinds of name internally:
12 -- * 'OccName.OccName': see "OccName#name_types"
14 -- * 'RdrName.RdrName': see "RdrName#name_types"
16 -- * 'Name.Name': see "Name#name_types"
18 -- * 'Id.Id' represents names that not only have a 'Name.Name' but also a 'TypeRep.Type' and some additional
19 -- details (a 'IdInfo.IdInfo' and one of 'Var.LocalIdDetails' or 'IdInfo.GlobalIdDetails') that
20 -- are added, modified and inspected by various compiler passes. These 'Var.Var' names may either
21 -- be global or local, see "Var#globalvslocal"
23 -- * 'Var.Var': see "Var#name_types"
28 -- ** Simple construction
29 mkGlobalId, mkVanillaGlobal, mkVanillaGlobalWithInfo,
30 mkLocalId, mkLocalIdWithInfo, mkExportedLocalId,
31 mkSysLocal, mkSysLocalM, mkUserLocal, mkUserLocalM,
32 mkTemplateLocals, mkTemplateLocalsNum, mkTemplateLocal,
35 -- ** Taking an Id apart
36 idName, idType, idUnique, idInfo, idDetails,
38 recordSelectorFieldLabel,
41 setIdName, setIdUnique, Id.setIdType,
42 setIdExported, setIdNotExported,
43 globaliseId, localiseId,
44 setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
45 zapLamIdInfo, zapDemandIdInfo, zapFragileIdInfo, transferPolyIdInfo,
48 -- ** Predicates on Ids
49 isImplicitId, isDeadBinder, isDictId, isStrictId,
50 isExportedId, isLocalId, isGlobalId,
51 isRecordSelector, isNaughtyRecordSelector,
53 isPrimOpId, isPrimOpId_maybe,
54 isFCallId, isFCallId_maybe,
55 isDataConWorkId, isDataConWorkId_maybe, isDataConId_maybe, idDataCon,
56 isBottomingId, idIsFrom,
57 isTickBoxOp, isTickBoxOp_maybe,
60 -- ** Inline pragma stuff
61 idInlinePragma, setInlinePragma, modifyInlinePragma,
63 -- ** One-shot lambdas
64 isOneShotBndr, isOneShotLambda, isStateHackType,
65 setOneShotLambda, clearOneShotLambda,
67 -- ** Reading 'IdInfo' fields
69 idNewDemandInfo, idNewDemandInfo_maybe,
70 idNewStrictness, idNewStrictness_maybe,
73 idSpecialisation, idCoreRules, idHasRules,
84 -- ** Writing 'IdInfo' fields
88 setIdNewStrictness, zapIdNewStrictness,
92 setIdOccInfo, zapIdOccInfo,
101 #include "HsVersions.h"
103 import CoreSyn ( CoreRule, Unfolding )
108 -- Imported and re-exported
109 import Var( Id, DictId,
110 idInfo, idDetails, globaliseId,
111 isId, isLocalId, isGlobalId, isExportedId )
118 #ifdef OLD_STRICTNESS
119 import qualified Demand
137 -- infixl so you can say (id `set` a `set` b)
138 infixl 1 `setIdUnfolding`,
140 `setIdNewDemandInfo`,
141 `setIdNewStrictness`,
143 `setIdSpecialisation`,
146 #ifdef OLD_STRICTNESS
153 %************************************************************************
155 \subsection{Basic Id manipulation}
157 %************************************************************************
163 idUnique :: Id -> Unique
164 idUnique = Var.varUnique
169 idPrimRep :: Id -> PrimRep
170 idPrimRep id = typePrimRep (idType id)
172 setIdName :: Id -> Name -> Id
173 setIdName = Var.setVarName
175 setIdUnique :: Id -> Unique -> Id
176 setIdUnique = Var.setVarUnique
178 -- | Not only does this set the 'Id' 'Type', it also evaluates the type to try and
179 -- reduce space usage
180 setIdType :: Id -> Type -> Id
181 setIdType id ty = seqType ty `seq` Var.setVarType id ty
183 setIdExported :: Id -> Id
184 setIdExported = Var.setIdExported
186 setIdNotExported :: Id -> Id
187 setIdNotExported = Var.setIdNotExported
189 localiseId :: Id -> Id
190 -- Make an with the same unique and type as the
191 -- incoming Id, but with an *Internal* Name and *LocalId* flavour
193 | isLocalId id && isInternalName name
196 = mkLocalIdWithInfo (localiseName name) (idType id) (idInfo id)
200 lazySetIdInfo :: Id -> IdInfo -> Id
201 lazySetIdInfo = Var.lazySetIdInfo
203 setIdInfo :: Id -> IdInfo -> Id
204 setIdInfo id info = seqIdInfo info `seq` (lazySetIdInfo id info)
205 -- Try to avoid spack leaks by seq'ing
207 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
208 modifyIdInfo fn id = setIdInfo id (fn (idInfo id))
210 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
211 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
212 maybeModifyIdInfo (Just new_info) id = lazySetIdInfo id new_info
213 maybeModifyIdInfo Nothing id = id
216 %************************************************************************
218 \subsection{Simple Id construction}
220 %************************************************************************
222 Absolutely all Ids are made by mkId. It is just like Var.mkId,
223 but in addition it pins free-tyvar-info onto the Id's type,
224 where it can easily be found.
226 Note [Free type variables]
227 ~~~~~~~~~~~~~~~~~~~~~~~~~~
228 At one time we cached the free type variables of the type of an Id
229 at the root of the type in a TyNote. The idea was to avoid repeating
230 the free-type-variable calculation. But it turned out to slow down
231 the compiler overall. I don't quite know why; perhaps finding free
232 type variables of an Id isn't all that common whereas applying a
233 substitution (which changes the free type variables) is more common.
234 Anyway, we removed it in March 2008.
237 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
238 mkGlobalId :: IdDetails -> Name -> Type -> IdInfo -> Id
239 mkGlobalId = Var.mkGlobalVar
241 -- | Make a global 'Id' without any extra information at all
242 mkVanillaGlobal :: Name -> Type -> Id
243 mkVanillaGlobal name ty = mkVanillaGlobalWithInfo name ty vanillaIdInfo
245 -- | Make a global 'Id' with no global information but some generic 'IdInfo'
246 mkVanillaGlobalWithInfo :: Name -> Type -> IdInfo -> Id
247 mkVanillaGlobalWithInfo = mkGlobalId VanillaId
250 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
251 mkLocalId :: Name -> Type -> Id
252 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
254 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
255 mkLocalIdWithInfo name ty info = Var.mkLocalVar VanillaId name ty info
256 -- Note [Free type variables]
258 -- | Create a local 'Id' that is marked as exported.
259 -- This prevents things attached to it from being removed as dead code.
260 mkExportedLocalId :: Name -> Type -> Id
261 mkExportedLocalId name ty = Var.mkExportedLocalVar VanillaId name ty vanillaIdInfo
262 -- Note [Free type variables]
265 -- | Create a system local 'Id'. These are local 'Id's (see "Var#globalvslocal")
266 -- that are created by the compiler out of thin air
267 mkSysLocal :: FastString -> Unique -> Type -> Id
268 mkSysLocal fs uniq ty = mkLocalId (mkSystemVarName uniq fs) ty
270 mkSysLocalM :: MonadUnique m => FastString -> Type -> m Id
271 mkSysLocalM fs ty = getUniqueM >>= (\uniq -> return (mkSysLocal fs uniq ty))
274 -- | Create a user local 'Id'. These are local 'Id's (see "Var#globalvslocal") with a name and location that the user might recognize
275 mkUserLocal :: OccName -> Unique -> Type -> SrcSpan -> Id
276 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
278 mkUserLocalM :: MonadUnique m => OccName -> Type -> SrcSpan -> m Id
279 mkUserLocalM occ ty loc = getUniqueM >>= (\uniq -> return (mkUserLocal occ uniq ty loc))
283 Make some local @Ids@ for a template @CoreExpr@. These have bogus
284 @Uniques@, but that's OK because the templates are supposed to be
285 instantiated before use.
288 -- | Workers get local names. "CoreTidy" will externalise these if necessary
289 mkWorkerId :: Unique -> Id -> Type -> Id
290 mkWorkerId uniq unwrkr ty
291 = mkLocalId wkr_name ty
293 wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcSpan unwrkr)
295 -- | Create a /template local/: a family of system local 'Id's in bijection with @Int@s, typically used in unfoldings
296 mkTemplateLocal :: Int -> Type -> Id
297 mkTemplateLocal i ty = mkSysLocal (fsLit "tpl") (mkBuiltinUnique i) ty
299 -- | Create a template local for a series of types
300 mkTemplateLocals :: [Type] -> [Id]
301 mkTemplateLocals = mkTemplateLocalsNum 1
303 -- | Create a template local for a series of type, but start from a specified template local
304 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
305 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
309 %************************************************************************
311 \subsection{Special Ids}
313 %************************************************************************
316 -- | If the 'Id' is that for a record selector, extract the 'sel_tycon' and label. Panic otherwise
317 recordSelectorFieldLabel :: Id -> (TyCon, FieldLabel)
318 recordSelectorFieldLabel id
319 = case Var.idDetails id of
320 RecSelId { sel_tycon = tycon } -> (tycon, idName id)
321 _ -> panic "recordSelectorFieldLabel"
323 isRecordSelector :: Id -> Bool
324 isNaughtyRecordSelector :: Id -> Bool
325 isPrimOpId :: Id -> Bool
326 isFCallId :: Id -> Bool
327 isDataConWorkId :: Id -> Bool
329 isClassOpId_maybe :: Id -> Maybe Class
330 isPrimOpId_maybe :: Id -> Maybe PrimOp
331 isFCallId_maybe :: Id -> Maybe ForeignCall
332 isDataConWorkId_maybe :: Id -> Maybe DataCon
334 isRecordSelector id = case Var.idDetails id of
338 isNaughtyRecordSelector id = case Var.idDetails id of
339 RecSelId { sel_naughty = n } -> n
342 isClassOpId_maybe id = case Var.idDetails id of
343 ClassOpId cls -> Just cls
346 isPrimOpId id = case Var.idDetails id of
350 isPrimOpId_maybe id = case Var.idDetails id of
351 PrimOpId op -> Just op
354 isFCallId id = case Var.idDetails id of
358 isFCallId_maybe id = case Var.idDetails id of
359 FCallId call -> Just call
362 isDataConWorkId id = case Var.idDetails id of
363 DataConWorkId _ -> True
366 isDataConWorkId_maybe id = case Var.idDetails id of
367 DataConWorkId con -> Just con
370 isDataConId_maybe :: Id -> Maybe DataCon
371 isDataConId_maybe id = case Var.idDetails id of
372 DataConWorkId con -> Just con
373 DataConWrapId con -> Just con
376 idDataCon :: Id -> DataCon
377 -- ^ Get from either the worker or the wrapper 'Id' to the 'DataCon'. Currently used only in the desugarer.
379 -- INVARIANT: @idDataCon (dataConWrapId d) = d@: remember, 'dataConWrapId' can return either the wrapper or the worker
380 idDataCon id = isDataConId_maybe id `orElse` pprPanic "idDataCon" (ppr id)
383 isDictId :: Id -> Bool
384 isDictId id = isDictTy (idType id)
386 hasNoBinding :: Id -> Bool
387 -- ^ Returns @True@ of an 'Id' which may not have a
388 -- binding, even though it is defined in this module.
390 -- Data constructor workers used to be things of this kind, but
391 -- they aren't any more. Instead, we inject a binding for
392 -- them at the CorePrep stage.
393 -- EXCEPT: unboxed tuples, which definitely have no binding
394 hasNoBinding id = case Var.idDetails id of
395 PrimOpId _ -> True -- See Note [Primop wrappers]
397 DataConWorkId dc -> isUnboxedTupleCon dc
400 isImplicitId :: Id -> Bool
401 -- ^ 'isImplicitId' tells whether an 'Id's info is implied by other
402 -- declarations, so we don't need to put its signature in an interface
403 -- file, even if it's mentioned in some other interface unfolding.
405 = case Var.idDetails id of
409 DataConWorkId _ -> True
410 DataConWrapId _ -> True
411 -- These are are implied by their type or class decl;
412 -- remember that all type and class decls appear in the interface file.
413 -- The dfun id is not an implicit Id; it must *not* be omitted, because
414 -- it carries version info for the instance decl
417 idIsFrom :: Module -> Id -> Bool
418 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
421 Note [Primop wrappers]
422 ~~~~~~~~~~~~~~~~~~~~~~
423 Currently hasNoBinding claims that PrimOpIds don't have a curried
424 function definition. But actually they do, in GHC.PrimopWrappers,
425 which is auto-generated from prelude/primops.txt.pp. So actually, hasNoBinding
426 could return 'False' for PrimOpIds.
428 But we'd need to add something in CoreToStg to swizzle any unsaturated
429 applications of GHC.Prim.plusInt# to GHC.PrimopWrappers.plusInt#.
431 Nota Bene: GHC.PrimopWrappers is needed *regardless*, because it's
432 used by GHCi, which does not implement primops direct at all.
437 isDeadBinder :: Id -> Bool
438 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
439 | otherwise = False -- TyVars count as not dead
443 isTickBoxOp :: Id -> Bool
445 case Var.idDetails id of
446 TickBoxOpId _ -> True
449 isTickBoxOp_maybe :: Id -> Maybe TickBoxOp
450 isTickBoxOp_maybe id =
451 case Var.idDetails id of
452 TickBoxOpId tick -> Just tick
456 %************************************************************************
458 \subsection{IdInfo stuff}
460 %************************************************************************
463 ---------------------------------
465 idArity :: Id -> Arity
466 idArity id = arityInfo (idInfo id)
468 setIdArity :: Id -> Arity -> Id
469 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
471 #ifdef OLD_STRICTNESS
472 ---------------------------------
474 idStrictness :: Id -> StrictnessInfo
475 idStrictness id = strictnessInfo (idInfo id)
477 setIdStrictness :: Id -> StrictnessInfo -> Id
478 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
481 -- | Returns true if an application to n args would diverge
482 isBottomingId :: Id -> Bool
483 isBottomingId id = isBottomingSig (idNewStrictness id)
485 idNewStrictness_maybe :: Id -> Maybe StrictSig
486 idNewStrictness :: Id -> StrictSig
488 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
489 idNewStrictness id = idNewStrictness_maybe id `orElse` topSig
491 setIdNewStrictness :: Id -> StrictSig -> Id
492 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
494 zapIdNewStrictness :: Id -> Id
495 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
497 -- | This predicate says whether the 'Id' has a strict demand placed on it or
498 -- has a type such that it can always be evaluated strictly (e.g., an
499 -- unlifted type, but see the comment for 'isStrictType'). We need to
500 -- check separately whether the 'Id' has a so-called \"strict type\" because if
501 -- the demand for the given @id@ hasn't been computed yet but @id@ has a strict
502 -- type, we still want @isStrictId id@ to be @True@.
503 isStrictId :: Id -> Bool
505 = ASSERT2( isId id, text "isStrictId: not an id: " <+> ppr id )
506 (isStrictDmd (idNewDemandInfo id)) ||
507 (isStrictType (idType id))
509 ---------------------------------
511 idWorkerInfo :: Id -> WorkerInfo
512 idWorkerInfo id = workerInfo (idInfo id)
514 setIdWorkerInfo :: Id -> WorkerInfo -> Id
515 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
517 ---------------------------------
519 idUnfolding :: Id -> Unfolding
520 idUnfolding id = unfoldingInfo (idInfo id)
522 setIdUnfolding :: Id -> Unfolding -> Id
523 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
525 #ifdef OLD_STRICTNESS
526 ---------------------------------
528 idDemandInfo :: Id -> Demand.Demand
529 idDemandInfo id = demandInfo (idInfo id)
531 setIdDemandInfo :: Id -> Demand.Demand -> Id
532 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
535 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
536 idNewDemandInfo :: Id -> NewDemand.Demand
538 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
539 idNewDemandInfo id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
541 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
542 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
544 ---------------------------------
546 idSpecialisation :: Id -> SpecInfo
547 idSpecialisation id = specInfo (idInfo id)
549 idCoreRules :: Id -> [CoreRule]
550 idCoreRules id = specInfoRules (idSpecialisation id)
552 idHasRules :: Id -> Bool
553 idHasRules id = not (isEmptySpecInfo (idSpecialisation id))
555 setIdSpecialisation :: Id -> SpecInfo -> Id
556 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
558 ---------------------------------
560 idCafInfo :: Id -> CafInfo
561 #ifdef OLD_STRICTNESS
562 idCafInfo id = case cgInfo (idInfo id) of
563 NoCgInfo -> pprPanic "idCafInfo" (ppr id)
564 info -> cgCafInfo info
566 idCafInfo id = cafInfo (idInfo id)
569 setIdCafInfo :: Id -> CafInfo -> Id
570 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
572 ---------------------------------
574 #ifdef OLD_STRICTNESS
575 idCprInfo :: Id -> CprInfo
576 idCprInfo id = cprInfo (idInfo id)
578 setIdCprInfo :: Id -> CprInfo -> Id
579 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
582 ---------------------------------
584 idOccInfo :: Id -> OccInfo
585 idOccInfo id = occInfo (idInfo id)
587 setIdOccInfo :: Id -> OccInfo -> Id
588 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
590 zapIdOccInfo :: Id -> Id
591 zapIdOccInfo b = b `setIdOccInfo` NoOccInfo
595 ---------------------------------
597 The inline pragma tells us to be very keen to inline this Id, but it's still
598 OK not to if optimisation is switched off.
601 idInlinePragma :: Id -> InlinePragInfo
602 idInlinePragma id = inlinePragInfo (idInfo id)
604 setInlinePragma :: Id -> InlinePragInfo -> Id
605 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
607 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
608 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
612 ---------------------------------
615 idLBVarInfo :: Id -> LBVarInfo
616 idLBVarInfo id = lbvarInfo (idInfo id)
618 -- | Returns whether the lambda associated with the 'Id' is certainly applied at most once
619 -- OR we are applying the \"state hack\" which makes it appear as if theis is the case for
620 -- lambdas used in @IO@. You should prefer using this over 'isOneShotLambda'
621 isOneShotBndr :: Id -> Bool
622 -- This one is the "business end", called externally.
623 -- Its main purpose is to encapsulate the Horrible State Hack
624 isOneShotBndr id = isOneShotLambda id || isStateHackType (idType id)
626 -- | Should we apply the state hack to values of this 'Type'?
627 isStateHackType :: Type -> Bool
632 = case splitTyConApp_maybe ty of
633 Just (tycon,_) -> tycon == statePrimTyCon
635 -- This is a gross hack. It claims that
636 -- every function over realWorldStatePrimTy is a one-shot
637 -- function. This is pretty true in practice, and makes a big
638 -- difference. For example, consider
639 -- a `thenST` \ r -> ...E...
640 -- The early full laziness pass, if it doesn't know that r is one-shot
641 -- will pull out E (let's say it doesn't mention r) to give
642 -- let lvl = E in a `thenST` \ r -> ...lvl...
643 -- When `thenST` gets inlined, we end up with
644 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
645 -- and we don't re-inline E.
647 -- It would be better to spot that r was one-shot to start with, but
648 -- I don't want to rely on that.
650 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
651 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
654 -- | Returns whether the lambda associated with the 'Id' is certainly applied at most once.
655 -- You probably want to use 'isOneShotBndr' instead
656 isOneShotLambda :: Id -> Bool
657 isOneShotLambda id = case idLBVarInfo id of
658 IsOneShotLambda -> True
661 setOneShotLambda :: Id -> Id
662 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
664 clearOneShotLambda :: Id -> Id
665 clearOneShotLambda id
666 | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
669 -- The OneShotLambda functions simply fiddle with the IdInfo flag
670 -- But watch out: this may change the type of something else
672 -- If we change the one-shot-ness of x, f's type changes
676 zapInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
677 zapInfo zapper id = maybeModifyIdInfo (zapper (idInfo id)) id
679 zapLamIdInfo :: Id -> Id
680 zapLamIdInfo = zapInfo zapLamInfo
682 zapDemandIdInfo :: Id -> Id
683 zapDemandIdInfo = zapInfo zapDemandInfo
685 zapFragileIdInfo :: Id -> Id
686 zapFragileIdInfo = zapInfo zapFragileInfo
689 Note [transferPolyIdInfo]
690 ~~~~~~~~~~~~~~~~~~~~~~~~~
693 f = /\a. let g = rhs in ...
695 where g has interesting strictness information. Then if we float thus
700 we *do not* want to lose the strictness information on g. Nor arity.
702 It's simple to retain strictness and arity, but not so simple to retain
705 so we simply discard those. Sooner or later this may bite us.
707 This transfer is used in two places:
708 FloatOut (long-distance let-floating)
709 SimplUtils.abstractFloats (short-distance let-floating)
712 transferPolyIdInfo :: Id -> Id -> Id
713 transferPolyIdInfo old_id new_id
714 = modifyIdInfo transfer new_id
716 old_info = idInfo old_id
717 transfer new_info = new_info `setNewStrictnessInfo` (newStrictnessInfo old_info)
718 `setArityInfo` (arityInfo old_info)